ast_to_hir.cpp revision 251eb753187fee83e6413f44f8b3cf0be1b4f4cb
1a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick/*
2a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * Copyright © 2010 Intel Corporation
3a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *
4a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * Permission is hereby granted, free of charge, to any person obtaining a
5a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * copy of this software and associated documentation files (the "Software"),
6a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * to deal in the Software without restriction, including without limitation
7a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * and/or sell copies of the Software, and to permit persons to whom the
9a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * Software is furnished to do so, subject to the following conditions:
10a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *
11a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * The above copyright notice and this permission notice (including the next
12a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * paragraph) shall be included in all copies or substantial portions of the
13a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * Software.
14a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *
15a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * DEALINGS IN THE SOFTWARE.
22a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */
23a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
24a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick/**
25a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * \file ast_to_hir.c
26a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * Convert abstract syntax to to high-level intermediate reprensentation (HIR).
27a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *
28a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * During the conversion to HIR, the majority of the symantic checking is
29a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * preformed on the program.  This includes:
30a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *
31a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *    * Symbol table management
32a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *    * Type checking
33a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *    * Function binding
34a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *
35a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * The majority of this work could be done during parsing, and the parser could
36a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * probably generate HIR directly.  However, this results in frequent changes
37a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * to the parser code.  Since we do not assume that every system this complier
38a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * is built on will have Flex and Bison installed, we have to store the code
39a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * generated by these tools in our version control system.  In other parts of
40a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * the system we've seen problems where a parser was changed but the generated
41a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * code was not committed, merge conflicts where created because two developers
42a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * had slightly different versions of Bison installed, etc.
43a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *
44a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * I have also noticed that running Bison generated parsers in GDB is very
45a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * irritating.  When you get a segfault on '$$ = $1->foo', you can't very
46a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * well 'print $1' in GDB.
47a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *
48a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * As a result, my preference is to put as little C code as possible in the
49a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * parser (and lexer) sources.
50a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */
51a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick#include <stdio.h>
52a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick#include "main/imports.h"
538bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick#include "glsl_symbol_table.h"
54a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick#include "glsl_parser_extras.h"
55a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick#include "ast.h"
56a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick#include "glsl_types.h"
57a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick#include "ir.h"
58a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
59d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanickvoid
60d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick_mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
61d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick{
62d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick   struct simple_node *ptr;
63d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick
64adfb0cd7401251bef0c854ac945fce78f0ed11dbIan Romanick   _mesa_glsl_initialize_variables(instructions, state);
65a4e92c4b26578614d76ce71b53194ea5c0f58d6cIan Romanick   _mesa_glsl_initialize_constructors(instructions, state);
66c22c40015db32b68b33c4944b9d94bf499135ec5Eric Anholt   _mesa_glsl_initialize_functions(instructions, state);
67adfb0cd7401251bef0c854ac945fce78f0ed11dbIan Romanick
6841ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = NULL;
6941ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick
70d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick   foreach (ptr, & state->translation_unit) {
71d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick      ((ast_node *)ptr)->hir(instructions, state);
72d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick   }
73d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick}
74d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick
75d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick
76a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
77a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickarithmetic_result_type(const struct glsl_type *type_a,
78a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick		       const struct glsl_type *type_b,
79a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick		       bool multiply,
80a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick		       struct _mesa_glsl_parse_state *state)
81a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
82a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
83a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
84a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The arithmetic binary operators add (+), subtract (-),
85a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    multiply (*), and divide (/) operate on integer and
86a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    floating-point scalars, vectors, and matrices."
87a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
8860b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   if (!type_a->is_numeric() || !type_b->is_numeric()) {
890471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
90a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
91a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
92a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
93a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "If one operand is floating-point based and the other is
94a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    not, then the conversions from Section 4.1.10 "Implicit
95a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    Conversions" are applied to the non-floating-point-based operand."
96a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
97a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * This conversion was added in GLSL 1.20.  If the compilation mode is
98a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * GLSL 1.10, the conversion is skipped.
99a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
100a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (state->language_version >= 120) {
101a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if ((type_a->base_type == GLSL_TYPE_FLOAT)
102a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  && (type_b->base_type != GLSL_TYPE_FLOAT)) {
103a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else if ((type_a->base_type != GLSL_TYPE_FLOAT)
104a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick		 && (type_b->base_type == GLSL_TYPE_FLOAT)) {
105a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
106a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
107a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
108a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "If the operands are integer types, they must both be signed or
109a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    both be unsigned."
110a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
111a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * From this rule and the preceeding conversion it can be inferred that
112a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
11360b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick    * The is_numeric check above already filtered out the case where either
11460b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick    * type is not one of these, so now the base types need only be tested for
11560b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick    * equality.
116a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
117a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (type_a->base_type != type_b->base_type) {
1180471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
119a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
120a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
121a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "All arithmetic binary operators result in the same fundamental type
122a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    (signed integer, unsigned integer, or floating-point) as the
123a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    operands they operate on, after operand type conversion. After
124a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    conversion, the following cases are valid
125a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
126a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    * The two operands are scalars. In this case the operation is
127a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      applied, resulting in a scalar."
128a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
129cb36f8aaeeb09660843316270a781948f773d90bIan Romanick   if (type_a->is_scalar() && type_b->is_scalar())
130a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return type_a;
131a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
132a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*   "* One operand is a scalar, and the other is a vector or matrix.
133a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      In this case, the scalar operation is applied independently to each
134a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      component of the vector or matrix, resulting in the same size
135a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      vector or matrix."
136a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
137cb36f8aaeeb09660843316270a781948f773d90bIan Romanick   if (type_a->is_scalar()) {
138cb36f8aaeeb09660843316270a781948f773d90bIan Romanick      if (!type_b->is_scalar())
139a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 return type_b;
140cb36f8aaeeb09660843316270a781948f773d90bIan Romanick   } else if (type_b->is_scalar()) {
141a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return type_a;
142a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
143a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
144a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* All of the combinations of <scalar, scalar>, <vector, scalar>,
145a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
146a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * handled.
147a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
14860b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   assert(!type_a->is_scalar());
14960b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   assert(!type_b->is_scalar());
150a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
151a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*   "* The two operands are vectors of the same size. In this case, the
152a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operation is done component-wise resulting in the same size
153a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      vector."
154a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
155a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick   if (type_a->is_vector() && type_b->is_vector()) {
1560471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return (type_a == type_b) ? type_a : glsl_type::error_type;
157a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
158a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
159a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* All of the combinations of <scalar, scalar>, <vector, scalar>,
160a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
161a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * <vector, vector> have been handled.  At least one of the operands must
162a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * be matrix.  Further, since there are no integer matrix types, the base
163a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * type of both operands must be float.
164a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
16560b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   assert(type_a->is_matrix() || type_b->is_matrix());
166a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   assert(type_a->base_type == GLSL_TYPE_FLOAT);
167a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   assert(type_b->base_type == GLSL_TYPE_FLOAT);
168a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
169a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*   "* The operator is add (+), subtract (-), or divide (/), and the
170a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operands are matrices with the same number of rows and the same
171a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      number of columns. In this case, the operation is done component-
172a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      wise resulting in the same size matrix."
173a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    * The operator is multiply (*), where both operands are matrices or
174a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      one operand is a vector and the other a matrix. A right vector
175a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operand is treated as a column vector and a left vector operand as a
176a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      row vector. In all these cases, it is required that the number of
177a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      columns of the left operand is equal to the number of rows of the
178a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      right operand. Then, the multiply (*) operation does a linear
179a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      algebraic multiply, yielding an object that has the same number of
180a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      rows as the left operand and the same number of columns as the right
181a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operand. Section 5.10 "Vector and Matrix Operations" explains in
182a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      more detail how vectors and matrices are operated on."
183a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
184a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (! multiply) {
1850471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return (type_a == type_b) ? type_a : glsl_type::error_type;
186a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
187fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick      if (type_a->is_matrix() && type_b->is_matrix()) {
188c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 /* Matrix multiply.  The columns of A must match the rows of B.  Given
189c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * the other previously tested constraints, this means the vector type
190c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * of a row from A must be the same as the vector type of a column from
191c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * B.
192c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  */
193c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 if (type_a->row_type() == type_b->column_type()) {
194c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	    /* The resulting matrix has the number of columns of matrix B and
195c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     * the number of rows of matrix A.  We get the row count of A by
196c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     * looking at the size of a vector that makes up a column.  The
197c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     * transpose (size of a row) is done for B.
198c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     */
199c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	    return
200c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	       glsl_type::get_instance(type_a->base_type,
201c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick				       type_a->column_type()->vector_elements,
202c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick				       type_b->row_type()->vector_elements);
203a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
204fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick      } else if (type_a->is_matrix()) {
205a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 /* A is a matrix and B is a column vector.  Columns of A must match
206c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * rows of B.  Given the other previously tested constraints, this
207c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * means the vector type of a row from A must be the same as the
208c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * vector the type of B.
209a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  */
210c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 if (type_a->row_type() == type_b)
211a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    return type_b;
212a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
213fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick	 assert(type_b->is_matrix());
214a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
215c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 /* A is a row vector and B is a matrix.  Columns of A must match rows
216c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * of B.  Given the other previously tested constraints, this means
217c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * the type of A must be the same as the vector type of a column from
218c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * B.
219a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  */
220c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 if (type_a == type_b->column_type())
221a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    return type_a;
222a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
223a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
224a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
225a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
226a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "All other cases are illegal."
227a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
2280471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::error_type;
229a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
230a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
231a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
232a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
233a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickunary_arithmetic_result_type(const struct glsl_type *type)
234a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
235a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 57:
236a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
237a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The arithmetic unary operators negate (-), post- and pre-increment
238a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     and decrement (-- and ++) operate on integer or floating-point
239a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     values (including vectors and matrices). All unary operators work
240a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     component-wise on their operands. These result with the same type
241a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     they operated on."
242a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
243a6d653dcbbee3158f9ea7b284bdeb1a8432f0fcbIan Romanick   if (!type->is_numeric())
2440471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
245a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
246a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return type;
247a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
248a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
249a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
250a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
251a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickmodulus_result_type(const struct glsl_type *type_a,
252a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick		    const struct glsl_type *type_b)
253a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
254a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
255a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The operator modulus (%) operates on signed or unsigned integers or
256a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    integer vectors. The operand types must both be signed or both be
257a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    unsigned."
258a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
25940176e249f72b6090204611873b19aed3da67c71Ian Romanick   if (!type_a->is_integer() || !type_b->is_integer()
260a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       || (type_a->base_type != type_b->base_type)) {
2610471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
262a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
263a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
264a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The operands cannot be vectors of differing size. If one operand is
265a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    a scalar and the other vector, then the scalar is applied component-
266a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    wise to the vector, resulting in the same type as the vector. If both
267a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    are vectors of the same size, the result is computed component-wise."
268a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
269a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick   if (type_a->is_vector()) {
270a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick      if (!type_b->is_vector()
271a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  || (type_a->vector_elements == type_b->vector_elements))
272a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 return type_a;
273a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else
274a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return type_b;
275a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
276a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The operator modulus (%) is not defined for any other data types
277a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    (non-integer types)."
278a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
2790471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::error_type;
280a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
281a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
282a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
283a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
284a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickrelational_result_type(const struct glsl_type *type_a,
285a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick		       const struct glsl_type *type_b,
286a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick		       struct _mesa_glsl_parse_state *state)
287a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
288a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
289a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The relational operators greater than (>), less than (<), greater
290a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    than or equal (>=), and less than or equal (<=) operate only on
291a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    scalar integer and scalar floating-point expressions."
292a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
293a6d653dcbbee3158f9ea7b284bdeb1a8432f0fcbIan Romanick   if (!type_a->is_numeric()
294a6d653dcbbee3158f9ea7b284bdeb1a8432f0fcbIan Romanick       || !type_b->is_numeric()
295cb36f8aaeeb09660843316270a781948f773d90bIan Romanick       || !type_a->is_scalar()
296cb36f8aaeeb09660843316270a781948f773d90bIan Romanick       || !type_b->is_scalar())
2970471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
298a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
299a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "Either the operands' types must match, or the conversions from
300a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    Section 4.1.10 "Implicit Conversions" will be applied to the integer
301a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    operand, after which the types must match."
302a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
303a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * This conversion was added in GLSL 1.20.  If the compilation mode is
304a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * GLSL 1.10, the conversion is skipped.
305a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
306a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (state->language_version >= 120) {
307a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if ((type_a->base_type == GLSL_TYPE_FLOAT)
308a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  && (type_b->base_type != GLSL_TYPE_FLOAT)) {
309a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 /* FINISHME: Generate the implicit type conversion. */
310a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else if ((type_a->base_type != GLSL_TYPE_FLOAT)
311a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick		 && (type_b->base_type == GLSL_TYPE_FLOAT)) {
312a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 /* FINISHME: Generate the implicit type conversion. */
313a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
314a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
315a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
316a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (type_a->base_type != type_b->base_type)
3170471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
318a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
319a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The result is scalar Boolean."
320a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
3210471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::bool_type;
322a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
323a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
324a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3250bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick/**
3260bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Validates that a value can be assigned to a location with a specified type
3270bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
3280bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Validates that \c rhs can be assigned to some location.  If the types are
3290bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * not an exact match but an automatic conversion is possible, \c rhs will be
3300bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * converted.
3310bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
3320bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \return
3330bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
3340bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Otherwise the actual RHS to be assigned will be returned.  This may be
3350bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \c rhs, or it may be \c rhs after some type conversion.
3360bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
3370bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \note
3380bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * In addition to being used for assignments, this function is used to
3390bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * type-check return values.
3400bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick */
341fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
342fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkevalidate_assignment(const glsl_type *lhs_type, ir_rvalue *rhs)
3430bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick{
3440bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   const glsl_type *const rhs_type = rhs->type;
3450bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
3460bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   /* If there is already some error in the RHS, just return it.  Anything
3470bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    * else will lead to an avalanche of error message back to the user.
3480bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    */
3490bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   if (rhs_type->is_error())
3500bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick      return rhs;
3510bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
3520bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   /* FINISHME: For GLSL 1.10, check that the types are not arrays. */
3530bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
3540bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   /* If the types are identical, the assignment can trivially proceed.
3550bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    */
3560bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   if (rhs_type == lhs_type)
3570bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick      return rhs;
3580bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
3590bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   /* FINISHME: Check for and apply automatic conversions. */
3600bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   return NULL;
3610bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick}
3620bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
36310a685218610e737e23d2d8a243ed6ff6613becdEric Anholtir_rvalue *
36410a685218610e737e23d2d8a243ed6ff6613becdEric Anholtdo_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
36510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	      ir_rvalue *lhs, ir_rvalue *rhs,
36610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	      YYLTYPE lhs_loc)
36710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt{
36810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
36910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
37010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   if (!error_emitted) {
37110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
37210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      if (!lhs->is_lvalue()) {
37310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	 _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
37410a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	 error_emitted = true;
37510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      }
37610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   }
37710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
37810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   ir_rvalue *new_rhs = validate_assignment(lhs->type, rhs);
37910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   if (new_rhs == NULL) {
38010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      _mesa_glsl_error(& lhs_loc, state, "type mismatch");
38110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   } else {
38210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      rhs = new_rhs;
38310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   }
38410a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
38510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   ir_instruction *tmp = new ir_assignment(lhs, rhs, NULL);
38610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   instructions->push_tail(tmp);
38710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
38810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   return rhs;
38910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt}
3900bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
391de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholtstatic ir_rvalue *
392de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholtget_lvalue_copy(exec_list *instructions, struct _mesa_glsl_parse_state *state,
393de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt		ir_rvalue *lvalue, YYLTYPE loc)
394de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt{
395de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   ir_variable *var;
396de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   ir_rvalue *var_deref;
397de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
398de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   /* FINISHME: Give unique names to the temporaries. */
399de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   var = new ir_variable(lvalue->type, "_internal_tmp");
400de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   var->mode = ir_var_auto;
401de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
402de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   var_deref = new ir_dereference(var);
403de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   do_assignment(instructions, state, var_deref, lvalue, loc);
404de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
405de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   /* Once we've created this temporary, mark it read only so it's no
406de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt    * longer considered an lvalue.
407de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt    */
408de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   var->read_only = true;
409de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
410de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   return var_deref;
411de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt}
412de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
413de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
414fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
4150044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_node::hir(exec_list *instructions,
41618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick	      struct _mesa_glsl_parse_state *state)
41718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick{
41818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   (void) instructions;
41918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   (void) state;
42018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
42118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   return NULL;
42218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick}
42318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
42418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
425fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
4260044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_expression::hir(exec_list *instructions,
42718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick		    struct _mesa_glsl_parse_state *state)
428a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
429a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   static const int operations[AST_NUM_OPERATORS] = {
430a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_assign doesn't convert to ir_expression. */
431a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_plus doesn't convert to ir_expression. */
432a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_neg,
433a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_add,
434a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_sub,
435a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mul,
436a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_div,
437a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mod,
438a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lshift,
439a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_rshift,
440a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_less,
441a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_greater,
442a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lequal,
443a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_gequal,
444a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_equal,
445a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_nequal,
446a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_and,
447a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_xor,
448a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_or,
449a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_bit_not,
450a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_and,
451a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_xor,
452a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_or,
453a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_logic_not,
454a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
455a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Note: The following block of expression types actually convert
456a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * to multiple IR instructions.
457a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
458a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mul,     /* ast_mul_assign */
459a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_div,     /* ast_div_assign */
460a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mod,     /* ast_mod_assign */
461a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_add,     /* ast_add_assign */
462a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_sub,     /* ast_sub_assign */
463a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lshift,  /* ast_ls_assign */
464a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_rshift,  /* ast_rs_assign */
465a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_and, /* ast_and_assign */
466a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_xor, /* ast_xor_assign */
467a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_or,  /* ast_or_assign */
468a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
469a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_conditional doesn't convert to ir_expression. */
470de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_add,     /* ast_pre_inc. */
471de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_sub,     /* ast_pre_dec. */
472de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_add,     /* ast_post_inc. */
473de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_sub,     /* ast_post_dec. */
474a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_field_selection doesn't conv to ir_expression. */
475a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_array_index doesn't convert to ir_expression. */
476a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_function_call doesn't conv to ir_expression. */
477a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_identifier doesn't convert to ir_expression. */
478a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_int_constant doesn't convert to ir_expression. */
479a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_uint_constant doesn't conv to ir_expression. */
480a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_float_constant doesn't conv to ir_expression. */
481a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_bool_constant doesn't conv to ir_expression. */
482a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_sequence doesn't convert to ir_expression. */
483a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   };
484fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke   ir_rvalue *result = NULL;
485fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke   ir_rvalue *op[2];
486a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   struct simple_node op_list;
4870471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   const struct glsl_type *type = glsl_type::error_type;
488a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   bool error_emitted = false;
489a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   YYLTYPE loc;
490a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
49118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   loc = this->get_location();
492a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   make_empty_list(& op_list);
493a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
49418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   switch (this->oper) {
4956652af36fe8994b1621d882fcc230d320908a2a3Ian Romanick   case ast_assign: {
49618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
49718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
498a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
49910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      result = do_assignment(instructions, state, op[0], op[1],
50010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt			     this->subexpressions[0]->get_location());
50110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      error_emitted = result->type->is_error();
50210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      type = result->type;
503a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
5046652af36fe8994b1621d882fcc230d320908a2a3Ian Romanick   }
505a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
506a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_plus:
50718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
508a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
509a43817a483a8c4a480ef4e6dfda2cef899300eb0Ian Romanick      error_emitted = op[0]->type->is_error();
510a43817a483a8c4a480ef4e6dfda2cef899300eb0Ian Romanick      if (type->is_error())
511a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 op[0]->type = type;
512a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
513a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      result = op[0];
514a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
515a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
516a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_neg:
51718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
518a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
519a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = unary_arithmetic_result_type(op[0]->type);
520a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
521a43817a483a8c4a480ef4e6dfda2cef899300eb0Ian Romanick      error_emitted = op[0]->type->is_error();
522a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
52318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_expression(operations[this->oper], type,
524a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick				 op[0], NULL);
525a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
526a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
527a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_add:
528a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sub:
529a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mul:
530a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_div:
53118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
53218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
533a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
534a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = arithmetic_result_type(op[0]->type, op[1]->type,
53518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick				    (this->oper == ast_mul),
536a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick				    state);
537a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
53818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_expression(operations[this->oper], type,
539a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick				 op[0], op[1]);
540a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
541a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
542a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mod:
54318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
54418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
545a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
546a43817a483a8c4a480ef4e6dfda2cef899300eb0Ian Romanick      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
547a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
548a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = modulus_result_type(op[0]->type, op[1]->type);
549a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
55018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      assert(operations[this->oper] == ir_binop_mod);
551a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
55218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_expression(operations[this->oper], type,
553a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick				 op[0], op[1]);
554a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
555a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
556a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_lshift:
557a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_rshift:
558a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* FINISHME: Implement bit-shift operators. */
559a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
560a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
561a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_less:
562a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_greater:
563a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_lequal:
564a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_gequal:
56518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
56618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
567a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
568a43817a483a8c4a480ef4e6dfda2cef899300eb0Ian Romanick      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
569a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
570a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = relational_result_type(op[0]->type, op[1]->type, state);
571a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
572a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* The relational operators must either generate an error or result
573a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * in a scalar boolean.  See page 57 of the GLSL 1.50 spec.
574a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
575a43817a483a8c4a480ef4e6dfda2cef899300eb0Ian Romanick      assert(type->is_error()
576a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	     || ((type->base_type == GLSL_TYPE_BOOL)
577cb36f8aaeeb09660843316270a781948f773d90bIan Romanick		 && type->is_scalar()));
578a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
57918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_expression(operations[this->oper], type,
580a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick				 op[0], op[1]);
581a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
582a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
583a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_nequal:
584a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_equal:
585a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* FINISHME: Implement equality operators. */
586a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
587a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
588a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_and:
589a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_xor:
590a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_or:
591a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_not:
592a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* FINISHME: Implement bit-wise operators. */
593a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
594a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
595a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_logic_and:
596a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_logic_xor:
597a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_logic_or:
598a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_logic_not:
599a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* FINISHME: Implement logical operators. */
600a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
601a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
602a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mul_assign:
603a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_div_assign:
604a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_add_assign:
605a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sub_assign: {
60618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
60718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
608a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
609a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = arithmetic_result_type(op[0]->type, op[1]->type,
61018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick				    (this->oper == ast_mul_assign),
611a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick				    state);
612a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
613fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke      ir_rvalue *temp_rhs = new ir_expression(operations[this->oper], type,
614fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke				              op[0], op[1]);
615a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
61610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      result = do_assignment(instructions, state, op[0], temp_rhs,
61710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt			     this->subexpressions[0]->get_location());
61810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      type = result->type;
61910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      error_emitted = (op[0]->type->is_error());
620a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
621a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* GLSL 1.10 does not allow array assignment.  However, we don't have to
622a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * explicitly test for this because none of the binary expression
623a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * operators allow array operands either.
624a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
625a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
626a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
627a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
628a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
62948a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt   case ast_mod_assign: {
63048a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
63148a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      op[1] = this->subexpressions[1]->hir(instructions, state);
63248a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
63348a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
63448a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
63548a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      type = modulus_result_type(op[0]->type, op[1]->type);
63648a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
63748a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      assert(operations[this->oper] == ir_binop_mod);
63848a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
63948a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      struct ir_rvalue *temp_rhs;
64048a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      temp_rhs = new ir_expression(operations[this->oper], type,
64148a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt				   op[0], op[1]);
64248a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
64348a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      result = do_assignment(instructions, state, op[0], temp_rhs,
64448a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt			     this->subexpressions[0]->get_location());
64548a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      type = result->type;
64648a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      error_emitted = op[0]->type->is_error();
64748a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      break;
64848a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt   }
649a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
650a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_ls_assign:
651a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_rs_assign:
652251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
653a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
654a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_and_assign:
655a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_xor_assign:
656a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_or_assign:
657251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
658a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
659a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_conditional:
660251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
661a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
662a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_pre_inc:
66376ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt   case ast_pre_dec: {
66476ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
66576ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
66676ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt	 op[1] = new ir_constant(1.0f);
66776ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      else
66876ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt	 op[1] = new ir_constant(1);
66976ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
67076ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      type = arithmetic_result_type(op[0]->type, op[1]->type,
67176ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt				    false, state);
67276ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
67376ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      struct ir_rvalue *temp_rhs;
67476ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      temp_rhs = new ir_expression(operations[this->oper], type,
67576ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt				   op[0], op[1]);
67676ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
67776ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      result = do_assignment(instructions, state, op[0], temp_rhs,
67876ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt			     this->subexpressions[0]->get_location());
67976ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      type = result->type;
68076ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      error_emitted = op[0]->type->is_error();
68176ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      break;
68276ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt   }
683a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
684a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_post_inc:
685de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   case ast_post_dec: {
686de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
687de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
688de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt	 op[1] = new ir_constant(1.0f);
689de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      else
690de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt	 op[1] = new ir_constant(1);
691de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
692de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
693de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
694de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      type = arithmetic_result_type(op[0]->type, op[1]->type,
695de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt				    false, state);
696de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
697de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      struct ir_rvalue *temp_rhs;
698de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      temp_rhs = new ir_expression(operations[this->oper], type,
699de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt				   op[0], op[1]);
700de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
701de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      /* Get a temporary of a copy of the lvalue before it's modified.
702de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt       * This may get thrown away later.
703de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt       */
704de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      result = get_lvalue_copy(instructions, state, op[0],
705de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt			       this->subexpressions[0]->get_location());
706de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
707de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      (void)do_assignment(instructions, state, op[0], temp_rhs,
708de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt			  this->subexpressions[0]->get_location());
709de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
710de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      type = result->type;
711de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      error_emitted = op[0]->type->is_error();
712a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
713de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   }
714a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
715a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_field_selection:
71618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = _mesa_ast_field_selection_to_hir(this, instructions, state);
717a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = result->type;
718a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
719a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
720a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_array_index:
721a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
722a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
723a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_function_call:
7247cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick      /* Should *NEVER* get here.  ast_function_call should always be handled
7257cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick       * by ast_function_expression::hir.
726a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
7277cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick      assert(0);
728a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
729a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
730a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_identifier: {
731a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* ast_identifier can appear several places in a full abstract syntax
732a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * tree.  This particular use must be at location specified in the grammar
733a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * as 'variable_identifier'.
734a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
7358bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      ir_variable *var =
7368bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick	 state->symbols->get_variable(this->primary_expression.identifier);
737a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
738a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      result = new ir_dereference(var);
739a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
740a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (var != NULL) {
741a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 type = result->type;
742a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
74371d0bbfcb2853f37b580ec7b705e55bb0eb426faIan Romanick	 _mesa_glsl_error(& loc, state, "`%s' undeclared",
74418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  this->primary_expression.identifier);
745a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
746a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 error_emitted = true;
747a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
748a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
749a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
750a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
751a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_int_constant:
7520471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::int_type;
75318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_constant(type, & this->primary_expression);
754a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
755a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
756a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_uint_constant:
7570471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::uint_type;
75818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_constant(type, & this->primary_expression);
759a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
760a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
761a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_float_constant:
7620471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::float_type;
76318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_constant(type, & this->primary_expression);
764a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
765a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
766a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bool_constant:
7670471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::bool_type;
76818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_constant(type, & this->primary_expression);
769a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
770a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
771a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sequence: {
772a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      struct simple_node *ptr;
773a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
774a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* It should not be possible to generate a sequence in the AST without
775a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * any expressions in it.
776a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
77718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      assert(!is_empty_list(&this->expressions));
778a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
779a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* The r-value of a sequence is the last expression in the sequence.  If
780a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * the other expressions in the sequence do not have side-effects (and
781a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * therefore add instructions to the instruction list), they get dropped
782a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * on the floor.
783a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
78418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      foreach (ptr, &this->expressions)
78518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick	 result = ((ast_node *)ptr)->hir(instructions, state);
786a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
787a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = result->type;
788a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
789a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Any errors should have already been emitted in the loop above.
790a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
791a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      error_emitted = true;
792a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
793a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
794a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
795a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
796cef3baecf636a30b62cd7a1e8de57c7650f7003eIan Romanick   if (type->is_error() && !error_emitted)
79771d0bbfcb2853f37b580ec7b705e55bb0eb426faIan Romanick      _mesa_glsl_error(& loc, state, "type mismatch");
798a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
799a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return result;
800a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
801a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
802a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
803fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
8040044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_expression_statement::hir(exec_list *instructions,
80518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			      struct _mesa_glsl_parse_state *state)
806a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
807a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* It is possible to have expression statements that don't have an
808a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * expression.  This is the solitary semicolon:
809a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
810a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * for (i = 0; i < 5; i++)
811a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     ;
812a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
813a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * In this case the expression will be NULL.  Test for NULL and don't do
814a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * anything in that case.
815a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
81618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (expression != NULL)
81718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      expression->hir(instructions, state);
818a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
819a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Statements do not have r-values.
820a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
821a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
822a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
823a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
824a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
825fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
8260044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_compound_statement::hir(exec_list *instructions,
82718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			    struct _mesa_glsl_parse_state *state)
828a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
829a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   struct simple_node *ptr;
830a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
831a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
83218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (new_scope)
8338bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      state->symbols->push_scope();
834a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
83518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   foreach (ptr, &statements)
83618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      ((ast_node *)ptr)->hir(instructions, state);
837a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
83818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (new_scope)
8398bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      state->symbols->pop_scope();
840a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
841a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Compound statements do not have r-values.
842a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
843a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
844a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
845a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
846a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
847a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
848a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanicktype_specifier_to_glsl_type(const struct ast_type_specifier *spec,
849a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			    const char **name,
850a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			    struct _mesa_glsl_parse_state *state)
851a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
852a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   struct glsl_type *type;
853a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
854a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (spec->type_specifier == ast_struct) {
855a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* FINISHME: Handle annonymous structures. */
856a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = NULL;
857a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
8588bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      type = state->symbols->get_type(spec->type_name);
8597f9d30974317a4050fb8990ce1a3eebbb190483aIan Romanick      *name = spec->type_name;
860a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
861a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* FINISHME: Handle array declarations.  Note that this requires complete
862f3f111eac45162e9634208cd7305981d8393328dIan Romanick       * FINISHME: handling of constant expressions.
863a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
864a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
865a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
866a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return type;
867a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
868a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
869a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
870a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic void
871a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickapply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
872a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick				 struct ir_variable *var,
8732e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				 struct _mesa_glsl_parse_state *state,
8742e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				 YYLTYPE *loc)
875a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
876a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->invariant)
877a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->invariant = 1;
878a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
879a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* FINISHME: Mark 'in' variables at global scope as read-only. */
880a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->constant || qual->attribute || qual->uniform
881a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       || (qual->varying && (state->target == fragment_shader)))
882a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->read_only = 1;
883a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
884a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->centroid)
885a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->centroid = 1;
886a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
8872e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   if (qual->attribute && state->target == fragment_shader) {
8882e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      var->type = glsl_type::error_type;
8892e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      _mesa_glsl_error(loc, state,
8902e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt		       "`attribute' variables may not be declared in the "
8912e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt		       "fragment shader");
8922e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   }
8932e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt
894a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->in && qual->out)
895a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_inout;
896a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else if (qual->attribute || qual->in
897a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    || (qual->varying && (state->target == fragment_shader)))
898a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_in;
8990b678234625fac67a89285ad2871dedc891fb1b1Ian Romanick   else if (qual->out || (qual->varying && (state->target == vertex_shader)))
900a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_out;
901a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else if (qual->uniform)
902a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_uniform;
903a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else
904a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_auto;
905a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
906a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->flat)
907a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->interpolation = ir_var_flat;
908a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else if (qual->noperspective)
909a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->interpolation = ir_var_noperspective;
910a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else
911a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->interpolation = ir_var_smooth;
912a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
913a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
914a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
915fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
9160044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_declarator_list::hir(exec_list *instructions,
91718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			 struct _mesa_glsl_parse_state *state)
918a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
919a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   struct simple_node *ptr;
920a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const struct glsl_type *decl_type;
921a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const char *type_name = NULL;
922a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
923a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
924a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* FINISHME: Handle vertex shader "invariant" declarations that do not
925a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * FINISHME: include a type.  These re-declare built-in variables to be
926a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * FINISHME: invariant.
927a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
928a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
92918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   decl_type = type_specifier_to_glsl_type(this->type->specifier,
930a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick					   & type_name, state);
931a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
93218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   foreach (ptr, &this->declarations) {
933a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      struct ast_declaration *const decl = (struct ast_declaration * )ptr;
934a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      const struct glsl_type *var_type;
935a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      struct ir_variable *var;
9362e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      YYLTYPE loc = this->get_location();
937a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
938a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* FINISHME: Emit a warning if a variable declaration shadows a
939a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * FINISHME: declaration at a higher scope.
940a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
941a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
942cec65a6b76290ee4da91691bd3ef01c3fb8a0c37Ian Romanick      if ((decl_type == NULL) || decl_type->is_void()) {
943a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 if (type_name != NULL) {
944a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    _mesa_glsl_error(& loc, state,
945a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     "invalid type `%s' in declaration of `%s'",
946a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     type_name, decl->identifier);
947a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 } else {
948a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    _mesa_glsl_error(& loc, state,
949a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     "invalid type in declaration of `%s'",
950a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     decl->identifier);
951a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
952a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 continue;
953a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
954a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
955a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (decl->is_array) {
956a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 /* FINISHME: Handle array declarations.  Note that this requires
957a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  * FINISHME: complete handling of constant expressions.
958a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  */
9598518e75d289638b10cb9350f287ccbdf1c927040Eric Anholt	 var_type = glsl_type::error_type;
960a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
961a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 /* FINISHME: Reject delcarations of multidimensional arrays. */
962a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
963a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 var_type = decl_type;
964a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
965a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
966a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var = new ir_variable(var_type, decl->identifier);
967a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
968f3f111eac45162e9634208cd7305981d8393328dIan Romanick      /* FINISHME: Variables that are attribute, uniform, varying, in, or
969a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * FINISHME: out varibles must be declared either at global scope or
970a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * FINISHME: in a parameter list (in and out only).
971a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
972a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
9732e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
9742e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				       & loc);
975a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
976a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Attempt to add the variable to the symbol table.  If this fails, it
977a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * means the variable has already been declared at this scope.
978a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
9793359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      if (state->symbols->name_declared_this_scope(decl->identifier)) {
98018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick	 YYLTYPE loc = this->get_location();
981a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
982a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 _mesa_glsl_error(& loc, state, "`%s' redeclared",
983a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			  decl->identifier);
984a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 continue;
985a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
986a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
9870044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick      instructions->push_tail(var);
988a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
98966faec4895b7bb59a614087a200c05157191b4aeIan Romanick      if (decl->initializer != NULL) {
99043de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 YYLTYPE initializer_loc = decl->initializer->get_location();
99143de17282017bdf187d6e646de3262cc64b7f46bIan Romanick
99266faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
99366faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *
99466faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *    "All uniform variables are read-only and are initialized either
99566faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *    directly by an application via API commands, or indirectly by
99666faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *    OpenGL."
99766faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  */
99866faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 if ((state->language_version <= 110)
99966faec4895b7bb59a614087a200c05157191b4aeIan Romanick	     && (var->mode == ir_var_uniform)) {
100043de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	    _mesa_glsl_error(& initializer_loc, state,
100143de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     "cannot initialize uniforms in GLSL 1.10");
100243de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 }
100343de17282017bdf187d6e646de3262cc64b7f46bIan Romanick
100443de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 if (var->type->is_sampler()) {
100543de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	    _mesa_glsl_error(& initializer_loc, state,
100643de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     "cannot initialize samplers");
100743de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 }
100819360152f5bd8cff93359dbfe5a50a90b699c118Ian Romanick
100943de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 if ((var->mode == ir_var_in) && (state->current_function == NULL)) {
101043de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	    _mesa_glsl_error(& initializer_loc, state,
101143de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     "cannot initialize %s shader input / %s",
101243de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     (state->target == vertex_shader)
101343de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     ? "vertex" : "fragment",
101443de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     (state->target == vertex_shader)
101543de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     ? "attribute" : "varying");
101666faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 }
101766faec4895b7bb59a614087a200c05157191b4aeIan Romanick
101866faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 ir_dereference *const lhs = new ir_dereference(var);
101966faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 ir_rvalue *const rhs = decl->initializer->hir(instructions, state);
102019360152f5bd8cff93359dbfe5a50a90b699c118Ian Romanick
102166faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 /* FINISHME: If the declaration is either 'const' or 'uniform', the
102266faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  * FINISHME: initializer (rhs) must be a constant expression.
102366faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  */
102466faec4895b7bb59a614087a200c05157191b4aeIan Romanick
102566faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 if (!rhs->type->is_error()) {
102666faec4895b7bb59a614087a200c05157191b4aeIan Romanick	    (void) do_assignment(instructions, state, lhs, rhs,
102766faec4895b7bb59a614087a200c05157191b4aeIan Romanick				 this->get_location());
102866faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 }
102966faec4895b7bb59a614087a200c05157191b4aeIan Romanick      }
103017d86f4371da413176ba365ca26a58bac172d365Ian Romanick
103117d86f4371da413176ba365ca26a58bac172d365Ian Romanick      /* Add the vairable to the symbol table after processing the initializer.
103217d86f4371da413176ba365ca26a58bac172d365Ian Romanick       * This differs from most C-like languages, but it follows the GLSL
103317d86f4371da413176ba365ca26a58bac172d365Ian Romanick       * specification.  From page 28 (page 34 of the PDF) of the GLSL 1.50
103417d86f4371da413176ba365ca26a58bac172d365Ian Romanick       * spec:
103517d86f4371da413176ba365ca26a58bac172d365Ian Romanick       *
103617d86f4371da413176ba365ca26a58bac172d365Ian Romanick       *     "Within a declaration, the scope of a name starts immediately
103717d86f4371da413176ba365ca26a58bac172d365Ian Romanick       *     after the initializer if present or immediately after the name
103817d86f4371da413176ba365ca26a58bac172d365Ian Romanick       *     being declared if not."
103917d86f4371da413176ba365ca26a58bac172d365Ian Romanick       */
104017d86f4371da413176ba365ca26a58bac172d365Ian Romanick      const bool added_variable =
104117d86f4371da413176ba365ca26a58bac172d365Ian Romanick	 state->symbols->add_variable(decl->identifier, var);
104217d86f4371da413176ba365ca26a58bac172d365Ian Romanick      assert(added_variable);
1043a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1044a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1045a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Variable declarations do not have r-values.
1046a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1047a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1048a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1049a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1050a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1051fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
10520044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_parameter_declarator::hir(exec_list *instructions,
105318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			      struct _mesa_glsl_parse_state *state)
1054a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1055a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const struct glsl_type *type;
1056a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const char *name = NULL;
10572e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   YYLTYPE loc = this->get_location();
1058a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
105918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   type = type_specifier_to_glsl_type(this->type->specifier, & name, state);
1060a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1061a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (type == NULL) {
1062a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (name != NULL) {
1063a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 _mesa_glsl_error(& loc, state,
1064a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			  "invalid type `%s' in declaration of `%s'",
106518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  name, this->identifier);
1066a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
1067a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 _mesa_glsl_error(& loc, state,
1068a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			  "invalid type in declaration of `%s'",
106918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  this->identifier);
1070a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1071a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
10720471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::error_type;
1073a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1074a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
107518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   ir_variable *var = new ir_variable(type, this->identifier);
1076a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1077a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* FINISHME: Handle array declarations.  Note that this requires
1078a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * FINISHME: complete handling of constant expressions.
1079a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1080a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1081cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick   /* Apply any specified qualifiers to the parameter declaration.  Note that
1082cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick    * for function parameters the default mode is 'in'.
1083cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick    */
10842e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc);
1085cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick   if (var->mode == ir_var_auto)
1086cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick      var->mode = ir_var_in;
1087a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
10880044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick   instructions->push_tail(var);
1089a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1090a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Parameter declarations do not have r-values.
1091a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1092a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1093a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1094a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1095a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1096a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic void
1097a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickast_function_parameters_to_hir(struct simple_node *ast_parameters,
10980044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick			       exec_list *ir_parameters,
1099a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			       struct _mesa_glsl_parse_state *state)
1100a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1101a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   struct simple_node *ptr;
1102a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1103a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   foreach (ptr, ast_parameters) {
110418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      ((ast_node *)ptr)->hir(ir_parameters, state);
1105a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1106a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1107a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1108a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1109a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic bool
11100044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickparameter_lists_match(exec_list *list_a, exec_list *list_b)
1111a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
11120044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick   exec_list_iterator iter_a = list_a->iterator();
11130044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick   exec_list_iterator iter_b = list_b->iterator();
1114a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
11150044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick   while (iter_a.has_next()) {
1116a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* If all of the parameters from the other parameter list have been
1117a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * exhausted, the lists have different length and, by definition,
1118a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * do not match.
1119a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
11200044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick      if (!iter_b.has_next())
1121a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 return false;
1122a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1123a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* If the types of the parameters do not match, the parameters lists
1124a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * are different.
1125a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1126a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* FINISHME */
1127a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1128a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
11290044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick      iter_a.next();
11300044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick      iter_b.next();
1131a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1132a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1133a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return true;
1134a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1135a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1136a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1137fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
11380044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_function_definition::hir(exec_list *instructions,
113918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			     struct _mesa_glsl_parse_state *state)
1140a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
114118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   ir_label *label;
114218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   ir_function_signature *signature = NULL;
114318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   ir_function *f = NULL;
11440044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick   exec_list parameters;
1145a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1146a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1147a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Convert the list of function parameters to HIR now so that they can be
1148a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * used below to compare this function's signature with previously seen
1149a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * signatures for functions with the same name.
1150a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
115118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   ast_function_parameters_to_hir(& this->prototype->parameters, & parameters,
1152a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick				  state);
1153a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1154e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick   const char *return_type_name;
1155e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick   const glsl_type *return_type =
1156e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick      type_specifier_to_glsl_type(this->prototype->return_type->specifier,
1157e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick				  & return_type_name, state);
1158e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick
1159e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick   assert(return_type != NULL);
1160e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick
1161a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Verify that this function's signature either doesn't match a previously
1162a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * seen signature for a function with the same name, or, if a match is found,
1163a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * that the previously seen signature does not have an associated definition.
1164a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
11653359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick   const char *const name = this->prototype->identifier;
11663359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick   f = state->symbols->get_function(name);
1167a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (f != NULL) {
11680044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick      foreach_iter(exec_list_iterator, iter, f->signatures) {
11690044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick	 signature = (struct ir_function_signature *) iter.get();
1170a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1171a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 /* Compare the parameter list of the function being defined to the
1172a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  * existing function.  If the parameter lists match, then the return
1173a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  * type must also match and the existing function must not have a
1174a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  * definition.
1175a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  */
1176a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 if (parameter_lists_match(& parameters, & signature->parameters)) {
1177a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    /* FINISHME: Compare return types. */
1178a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1179a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    if (signature->definition != NULL) {
118018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick	       YYLTYPE loc = this->get_location();
1181a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
11823359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	       _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
1183a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	       signature = NULL;
1184a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	       break;
1185a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    }
1186a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
1187a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1188a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 signature = NULL;
1189a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1190a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
11913359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick   } else if (state->symbols->name_declared_this_scope(name)) {
11923359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      /* This function name shadows a non-function use of the same name.
11933359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick       */
11943359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      YYLTYPE loc = this->get_location();
11953359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick
11963359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      _mesa_glsl_error(& loc, state, "function name `%s' conflicts with "
11973359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick		       "non-function", name);
11983359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      signature = NULL;
1199a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
1200882dad75408fc4071a9dd700309f9e54f6ad2650Ian Romanick      f = new ir_function(name);
12018bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      state->symbols->add_function(f->name, f);
1202a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1203a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1204ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   /* Verify the return type of main() */
1205ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   if (strcmp(name, "main") == 0) {
1206ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt      if (return_type != glsl_type::get_instance(GLSL_TYPE_VOID, 0, 0)) {
1207ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt	 YYLTYPE loc = this->get_location();
1208ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt
1209ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt	 _mesa_glsl_error(& loc, state, "main() must return void");
1210ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt      }
1211ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   }
1212a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1213a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Finish storing the information about this new function in its signature.
1214a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1215a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (signature == NULL) {
1216e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick      signature = new ir_function_signature(return_type);
12170044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick      f->signatures.push_tail(signature);
1218a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
1219a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Destroy all of the previous parameter information.  The previous
1220a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * parameter information comes from the function prototype, and it can
1221a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * either include invalid parameter names or may not have names at all.
1222a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
12230044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick      foreach_iter(exec_list_iterator, iter, signature->parameters) {
122444e1dfa2df4de3e2de963f0505cdadade6fe8180Kenneth Graunke	 assert(((ir_instruction *) iter.get())->as_variable() != NULL);
1225a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
12260044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick	 iter.remove();
12270044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick	 delete iter.get();
1228a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1229a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1230a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1231a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
123241ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   assert(state->current_function == NULL);
123341ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = signature;
123441ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick
123518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   ast_function_parameters_to_hir(& this->prototype->parameters,
1236a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick				  & signature->parameters,
1237a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick				  state);
1238a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* FINISHME: Set signature->return_type */
1239a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
12403359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick   label = new ir_label(name);
1241a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (signature->definition == NULL) {
1242a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      signature->definition = label;
1243a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
12440044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick   instructions->push_tail(label);
1245a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1246a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Add the function parameters to the symbol table.  During this step the
1247a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * parameter declarations are also moved from the temporary "parameters" list
1248a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * to the instruction list.  There are other more efficient ways to do this,
1249a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * but they involve ugly linked-list gymnastics.
1250a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
12518bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick   state->symbols->push_scope();
12520044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick   foreach_iter(exec_list_iterator, iter, parameters) {
12530044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick      ir_variable *const var = (ir_variable *) iter.get();
1254a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
125544e1dfa2df4de3e2de963f0505cdadade6fe8180Kenneth Graunke      assert(((ir_instruction *) var)->as_variable() != NULL);
1256a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
12570044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick      iter.remove();
12580044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick      instructions->push_tail(var);
1259a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
12603359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      /* The only way a parameter would "exist" is if two parameters have
12613359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick       * the same name.
12623359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick       */
12633359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      if (state->symbols->name_declared_this_scope(var->name)) {
12643359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 YYLTYPE loc = this->get_location();
12653359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick
12663359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
12673359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      } else {
12683359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 state->symbols->add_variable(var->name, var);
12693359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      }
1270a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1271a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1272a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Convert the body of the function to HIR, and append the resulting
1273a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * instructions to the list that currently consists of the function label
1274a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * and the function parameters.
1275a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
127618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   this->body->hir(instructions, state);
1277a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
12788bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick   state->symbols->pop_scope();
1279a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
128041ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   assert(state->current_function == signature);
128141ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = NULL;
1282a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1283a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Function definitions do not have r-values.
1284a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1285a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1286a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
128716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
128816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
1289fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
129016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanickast_jump_statement::hir(exec_list *instructions,
129116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick			struct _mesa_glsl_parse_state *state)
129216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick{
129316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
129416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   if (mode == ast_return) {
129516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      ir_return *inst;
129616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
129716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      if (opt_return_value) {
129816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 /* FINISHME: Make sure the enclosing function has a non-void return
129916a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	  * FINISHME: type.
130016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	  */
130116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
130216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 ir_expression *const ret = (ir_expression *)
130316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	    opt_return_value->hir(instructions, state);
130416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 assert(ret != NULL);
130516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
130616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 /* FINISHME: Make sure the type of the return value matches the return
130716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	  * FINISHME: type of the enclosing function.
130816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	  */
130916a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
131016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 inst = new ir_return(ret);
131116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      } else {
131216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 /* FINISHME: Make sure the enclosing function has a void return type.
131316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	  */
131416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 inst = new ir_return;
131516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      }
131616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
131716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      instructions->push_tail(inst);
131816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   }
131916a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
132016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   /* Jump instructions do not have r-values.
132116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick    */
132216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   return NULL;
132316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick}
13243c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
13253c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
13263c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanickir_rvalue *
13273c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanickast_selection_statement::hir(exec_list *instructions,
13283c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick			     struct _mesa_glsl_parse_state *state)
13293c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick{
13303c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   ir_rvalue *const condition = this->condition->hir(instructions, state);
13313c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   struct simple_node *ptr;
13323c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
13333c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
13343c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *
13353c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    "Any expression whose type evaluates to a Boolean can be used as the
13363c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    conditional expression bool-expression. Vector types are not accepted
13373c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    as the expression to if."
13383c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *
13393c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    * The checks are separated so that higher quality diagnostics can be
13403c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    * generated for cases where both rules are violated.
13413c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    */
13423c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
13433c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      YYLTYPE loc = this->condition->get_location();
13443c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
13453c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
13463c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick		       "boolean");
13473c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   }
13483c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
13493c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   ir_if *const stmt = new ir_if(condition);
13503c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
13513c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   if (then_statement != NULL) {
13523c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      ast_node *node = (ast_node *) then_statement;
13533c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      do {
13543c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick	 node->hir(& stmt->then_instructions, state);
13553c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick	 node = (ast_node *) node->next;
13563c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      } while (node != then_statement);
13573c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   }
13583c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
13593c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   if (else_statement != NULL) {
13603c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      ast_node *node = (ast_node *) else_statement;
13613c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      do {
13623c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick	 node->hir(& stmt->else_instructions, state);
13633c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick	 node = (ast_node *) node->next;
13643c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      } while (node != else_statement);
13653c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   }
13663c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
13673c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   instructions->push_tail(stmt);
13683c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
13693c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   /* if-statements do not have r-values.
13703c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    */
13713c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   return NULL;
13723c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick}
1373