ast_to_hir.cpp revision 1e7ec3ce128a9d30d7d9e1707a22b270eb525075
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
760104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick/**
770104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * If a conversion is available, convert one operand to a different type
780104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick *
790104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * The \c from \c ir_rvalue is converted "in place".
800104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick *
810104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param to     Type that the operand it to be converted to
820104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param from   Operand that is being converted
830104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param state  GLSL compiler state
840104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick *
850104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \return
860104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * If a conversion is possible (or unnecessary), \c true is returned.
870104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * Otherwise \c false is returned.
880104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick */
890104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanickstatic bool
90bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickapply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
910104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick			  struct _mesa_glsl_parse_state *state)
920104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick{
93bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   if (to->base_type == from->type->base_type)
940104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return true;
950104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
960104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   /* This conversion was added in GLSL 1.20.  If the compilation mode is
970104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    * GLSL 1.10, the conversion is skipped.
980104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    */
990104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   if (state->language_version < 120)
1000104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return false;
1010104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1020104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
1030104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *
1040104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    "There are no implicit array or structure conversions. For
1050104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    example, an array of int cannot be implicitly converted to an
1060104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    array of float. There are no implicit conversions between
1070104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    signed and unsigned integers."
1080104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    */
1090104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   /* FINISHME: The above comment is partially a lie.  There is int/uint
1100104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    * FINISHME: conversion for immediate constants.
1110104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    */
112bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   if (!to->is_float() || !from->type->is_numeric())
1130104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return false;
1140104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
115bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   switch (from->type->base_type) {
1160104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   case GLSL_TYPE_INT:
117bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      from = new ir_expression(ir_unop_i2f, to, from, NULL);
1180104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      break;
1190104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   case GLSL_TYPE_UINT:
120bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      from = new ir_expression(ir_unop_u2f, to, from, NULL);
1210104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      break;
1220104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   case GLSL_TYPE_BOOL:
123dc58b3f8ccd817fdee390a3df5b8e0fb29d5397cEric Anholt      from = new ir_expression(ir_unop_b2f, to, from, NULL);
124dc58b3f8ccd817fdee390a3df5b8e0fb29d5397cEric Anholt      break;
1250104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   default:
1260104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      assert(0);
1270104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   }
1280104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1290104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   return true;
1300104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick}
1310104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1320104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
133a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
134bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickarithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
135a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick		       bool multiply,
136a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
137a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
138bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   const glsl_type *const type_a = value_a->type;
139bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   const glsl_type *const type_b = value_b->type;
1400104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
141a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
142a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
143a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The arithmetic binary operators add (+), subtract (-),
144a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    multiply (*), and divide (/) operate on integer and
145a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    floating-point scalars, vectors, and matrices."
146a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
14760b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   if (!type_a->is_numeric() || !type_b->is_numeric()) {
148a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state,
149a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "Operands to arithmetic operators must be numeric");
1500471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
151a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
152a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
153a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
154a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "If one operand is floating-point based and the other is
155a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    not, then the conversions from Section 4.1.10 "Implicit
156a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    Conversions" are applied to the non-floating-point-based operand."
157a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1580104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   if (!apply_implicit_conversion(type_a, value_b, state)
1590104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick       && !apply_implicit_conversion(type_b, value_a, state)) {
160a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state,
161a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "Could not implicitly convert operands to "
162a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "arithmetic operator");
1630104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return glsl_type::error_type;
164a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
165a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
166a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "If the operands are integer types, they must both be signed or
167a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    both be unsigned."
168a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
169a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * From this rule and the preceeding conversion it can be inferred that
170a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
17160b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick    * The is_numeric check above already filtered out the case where either
17260b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick    * type is not one of these, so now the base types need only be tested for
17360b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick    * equality.
174a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
175a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (type_a->base_type != type_b->base_type) {
176a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state,
177a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "base type mismatch for arithmetic operator");
1780471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
179a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
180a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
181a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "All arithmetic binary operators result in the same fundamental type
182a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    (signed integer, unsigned integer, or floating-point) as the
183a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    operands they operate on, after operand type conversion. After
184a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    conversion, the following cases are valid
185a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
186a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    * The two operands are scalars. In this case the operation is
187a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      applied, resulting in a scalar."
188a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
189cb36f8aaeeb09660843316270a781948f773d90bIan Romanick   if (type_a->is_scalar() && type_b->is_scalar())
190a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return type_a;
191a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
192a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*   "* One operand is a scalar, and the other is a vector or matrix.
193a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      In this case, the scalar operation is applied independently to each
194a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      component of the vector or matrix, resulting in the same size
195a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      vector or matrix."
196a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
197cb36f8aaeeb09660843316270a781948f773d90bIan Romanick   if (type_a->is_scalar()) {
198cb36f8aaeeb09660843316270a781948f773d90bIan Romanick      if (!type_b->is_scalar())
199a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 return type_b;
200cb36f8aaeeb09660843316270a781948f773d90bIan Romanick   } else if (type_b->is_scalar()) {
201a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return type_a;
202a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
203a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
204a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* All of the combinations of <scalar, scalar>, <vector, scalar>,
205a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
206a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * handled.
207a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
20860b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   assert(!type_a->is_scalar());
20960b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   assert(!type_b->is_scalar());
210a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
211a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*   "* The two operands are vectors of the same size. In this case, the
212a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operation is done component-wise resulting in the same size
213a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      vector."
214a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
215a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick   if (type_a->is_vector() && type_b->is_vector()) {
216a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      if (type_a == type_b) {
217a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 return type_a;
218a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      } else {
219a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 _mesa_glsl_error(loc, state,
220a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt			  "vector size mismatch for arithmetic operator");
221a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 return glsl_type::error_type;
222a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      }
223a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
224a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
225a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* All of the combinations of <scalar, scalar>, <vector, scalar>,
226a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
227a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * <vector, vector> have been handled.  At least one of the operands must
228a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * be matrix.  Further, since there are no integer matrix types, the base
229a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * type of both operands must be float.
230a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
23160b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   assert(type_a->is_matrix() || type_b->is_matrix());
232a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   assert(type_a->base_type == GLSL_TYPE_FLOAT);
233a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   assert(type_b->base_type == GLSL_TYPE_FLOAT);
234a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
235a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*   "* The operator is add (+), subtract (-), or divide (/), and the
236a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operands are matrices with the same number of rows and the same
237a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      number of columns. In this case, the operation is done component-
238a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      wise resulting in the same size matrix."
239a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    * The operator is multiply (*), where both operands are matrices or
240a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      one operand is a vector and the other a matrix. A right vector
241a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operand is treated as a column vector and a left vector operand as a
242a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      row vector. In all these cases, it is required that the number of
243a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      columns of the left operand is equal to the number of rows of the
244a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      right operand. Then, the multiply (*) operation does a linear
245a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      algebraic multiply, yielding an object that has the same number of
246a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      rows as the left operand and the same number of columns as the right
247a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operand. Section 5.10 "Vector and Matrix Operations" explains in
248a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      more detail how vectors and matrices are operated on."
249a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
250a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (! multiply) {
251a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      if (type_a == type_b)
252a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 return type_a;
253a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
254fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick      if (type_a->is_matrix() && type_b->is_matrix()) {
255c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 /* Matrix multiply.  The columns of A must match the rows of B.  Given
256c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * the other previously tested constraints, this means the vector type
257c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * of a row from A must be the same as the vector type of a column from
258c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * B.
259c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  */
260c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 if (type_a->row_type() == type_b->column_type()) {
261c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	    /* The resulting matrix has the number of columns of matrix B and
262c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     * the number of rows of matrix A.  We get the row count of A by
263c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     * looking at the size of a vector that makes up a column.  The
264c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     * transpose (size of a row) is done for B.
265c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     */
266a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	    const glsl_type *const type =
267c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	       glsl_type::get_instance(type_a->base_type,
268c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick				       type_a->column_type()->vector_elements,
269c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick				       type_b->row_type()->vector_elements);
270a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	    assert(type != glsl_type::error_type);
271a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt
272a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	    return type;
273a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
274fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick      } else if (type_a->is_matrix()) {
275a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 /* A is a matrix and B is a column vector.  Columns of A must match
276c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * rows of B.  Given the other previously tested constraints, this
277c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * means the vector type of a row from A must be the same as the
278c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * vector the type of B.
279a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  */
280c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 if (type_a->row_type() == type_b)
281a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    return type_b;
282a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
283fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick	 assert(type_b->is_matrix());
284a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
285c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 /* A is a row vector and B is a matrix.  Columns of A must match rows
286c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * of B.  Given the other previously tested constraints, this means
287c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * the type of A must be the same as the vector type of a column from
288c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * B.
289a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  */
290c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 if (type_a == type_b->column_type())
291a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    return type_a;
292a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
293a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt
294a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication");
295a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      return glsl_type::error_type;
296a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
297a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
298a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
299a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "All other cases are illegal."
300a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
301a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt   _mesa_glsl_error(loc, state, "type mismatch");
3020471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::error_type;
303a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
304a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
305a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
306a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
30765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholtunary_arithmetic_result_type(const struct glsl_type *type,
30865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt			     struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
309a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
310a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 57:
311a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
312a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The arithmetic unary operators negate (-), post- and pre-increment
313a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     and decrement (-- and ++) operate on integer or floating-point
314a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     values (including vectors and matrices). All unary operators work
315a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     component-wise on their operands. These result with the same type
316a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     they operated on."
317a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
31865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   if (!type->is_numeric()) {
31965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state,
32065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "Operands to arithmetic operators must be numeric");
3210471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
32265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   }
323a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
324a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return type;
325a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
326a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
327a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
328a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
329a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickmodulus_result_type(const struct glsl_type *type_a,
33065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		    const struct glsl_type *type_b,
33165e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		    struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
332a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
333a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
334a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The operator modulus (%) operates on signed or unsigned integers or
335a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    integer vectors. The operand types must both be signed or both be
336a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    unsigned."
337a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
33840176e249f72b6090204611873b19aed3da67c71Ian Romanick   if (!type_a->is_integer() || !type_b->is_integer()
339a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       || (type_a->base_type != type_b->base_type)) {
34065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state, "type mismatch");
3410471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
342a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
343a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
344a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The operands cannot be vectors of differing size. If one operand is
345a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    a scalar and the other vector, then the scalar is applied component-
346a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    wise to the vector, resulting in the same type as the vector. If both
347a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    are vectors of the same size, the result is computed component-wise."
348a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
349a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick   if (type_a->is_vector()) {
350a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick      if (!type_b->is_vector()
351a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  || (type_a->vector_elements == type_b->vector_elements))
352a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 return type_a;
353a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else
354a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return type_b;
355a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
356a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The operator modulus (%) is not defined for any other data types
357a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    (non-integer types)."
358a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
35965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   _mesa_glsl_error(loc, state, "type mismatch");
3600471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::error_type;
361a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
362a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
363a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
364a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
365bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickrelational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
36665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
367a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
368bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   const glsl_type *const type_a = value_a->type;
369bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   const glsl_type *const type_b = value_b->type;
3700150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick
371a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
372a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The relational operators greater than (>), less than (<), greater
373a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    than or equal (>=), and less than or equal (<=) operate only on
374a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    scalar integer and scalar floating-point expressions."
375a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
376a6d653dcbbee3158f9ea7b284bdeb1a8432f0fcbIan Romanick   if (!type_a->is_numeric()
377a6d653dcbbee3158f9ea7b284bdeb1a8432f0fcbIan Romanick       || !type_b->is_numeric()
378cb36f8aaeeb09660843316270a781948f773d90bIan Romanick       || !type_a->is_scalar()
37965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt       || !type_b->is_scalar()) {
38065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state,
38165e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "Operands to relational operators must be scalar and "
38265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "numeric");
3830471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
38465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   }
385a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
386a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "Either the operands' types must match, or the conversions from
387a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    Section 4.1.10 "Implicit Conversions" will be applied to the integer
388a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    operand, after which the types must match."
389a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
3900150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick   if (!apply_implicit_conversion(type_a, value_b, state)
3910150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick       && !apply_implicit_conversion(type_b, value_a, state)) {
39265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state,
39365e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "Could not implicitly convert operands to "
39465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "relational operator");
3950150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick      return glsl_type::error_type;
396a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
397a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
39865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   if (type_a->base_type != type_b->base_type) {
39965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state, "base type mismatch");
4000471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
40165e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   }
402a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
403a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The result is scalar Boolean."
404a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
4050471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::bool_type;
406a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
407a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
408a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
4090bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick/**
4100bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Validates that a value can be assigned to a location with a specified type
4110bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
4120bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Validates that \c rhs can be assigned to some location.  If the types are
4130bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * not an exact match but an automatic conversion is possible, \c rhs will be
4140bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * converted.
4150bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
4160bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \return
4170bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
4180bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Otherwise the actual RHS to be assigned will be returned.  This may be
4190bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \c rhs, or it may be \c rhs after some type conversion.
4200bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
4210bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \note
4220bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * In addition to being used for assignments, this function is used to
4230bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * type-check return values.
4240bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick */
425fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
426fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkevalidate_assignment(const glsl_type *lhs_type, ir_rvalue *rhs)
4270bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick{
4280bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   const glsl_type *const rhs_type = rhs->type;
4290bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
4300bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   /* If there is already some error in the RHS, just return it.  Anything
4310bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    * else will lead to an avalanche of error message back to the user.
4320bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    */
4330bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   if (rhs_type->is_error())
4340bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick      return rhs;
4350bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
4360bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   /* If the types are identical, the assignment can trivially proceed.
4370bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    */
4380bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   if (rhs_type == lhs_type)
4390bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick      return rhs;
4400bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
4410157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick   /* If the array element types are the same and the size of the LHS is zero,
4420157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    * the assignment is okay.
4430157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    *
4440157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
4450157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    * is handled by ir_dereference::is_lvalue.
4460157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    */
4470157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick   if (lhs_type->is_array() && rhs->type->is_array()
4480157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       && (lhs_type->element_type() == rhs->type->element_type())
4490157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       && (lhs_type->array_size() == 0)) {
4500157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      return rhs;
4510157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick   }
4520157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
4530bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   /* FINISHME: Check for and apply automatic conversions. */
4540bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   return NULL;
4550bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick}
4560bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
45710a685218610e737e23d2d8a243ed6ff6613becdEric Anholtir_rvalue *
45810a685218610e737e23d2d8a243ed6ff6613becdEric Anholtdo_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
45910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	      ir_rvalue *lhs, ir_rvalue *rhs,
46010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	      YYLTYPE lhs_loc)
46110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt{
46210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
46310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
46410a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   if (!error_emitted) {
46510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
46610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      if (!lhs->is_lvalue()) {
46710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	 _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
46810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	 error_emitted = true;
46910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      }
47010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   }
47110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
47210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   ir_rvalue *new_rhs = validate_assignment(lhs->type, rhs);
47310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   if (new_rhs == NULL) {
47410a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      _mesa_glsl_error(& lhs_loc, state, "type mismatch");
47510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   } else {
47610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      rhs = new_rhs;
4770157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
4780157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      /* If the LHS array was not declared with a size, it takes it size from
4790157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       * the RHS.  If the LHS is an l-value and a whole array, it must be a
4800157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       * dereference of a variable.  Any other case would require that the LHS
4810157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       * is either not an l-value or not a whole array.
4820157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       */
4830157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      if (lhs->type->array_size() == 0) {
4840157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 ir_dereference *const d = lhs->as_dereference();
4850157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
4860157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 assert(d != NULL);
4870157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
4880157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 ir_variable *const var = d->var->as_variable();
4890157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
4900157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 assert(var != NULL);
4910157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
49263f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	 if (var->max_array_access >= unsigned(rhs->type->array_size())) {
49363f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	    /* FINISHME: This should actually log the location of the RHS. */
49463f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	    _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
49563f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick			     "previous access",
49663f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick			     var->max_array_access);
49763f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	 }
49863f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick
4990157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 var->type = glsl_type::get_array_instance(lhs->type->element_type(),
5000157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick						   rhs->type->array_size());
5010157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      }
50210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   }
50310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
50410a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   ir_instruction *tmp = new ir_assignment(lhs, rhs, NULL);
50510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   instructions->push_tail(tmp);
50610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
50710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   return rhs;
50810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt}
5090bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
5105185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick
5115185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick/**
5125185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick * Generate a new temporary and add its declaration to the instruction stream
5135185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick */
5145185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanickstatic ir_variable *
5155185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanickgenerate_temporary(const glsl_type *type, exec_list *instructions,
5165185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick		   struct _mesa_glsl_parse_state *state)
5175185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick{
5185185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick   char *name = (char *) malloc(sizeof(char) * 13);
5195185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick
5205185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick   snprintf(name, 13, "tmp_%08X", state->temp_index);
5215185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick   state->temp_index++;
5225185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick
5235185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick   ir_variable *const var = new ir_variable(type, name);
5245185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick   instructions->push_tail(var);
5255185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick
5265185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick   return var;
5275185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick}
5285185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick
5295185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick
530de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholtstatic ir_rvalue *
531de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholtget_lvalue_copy(exec_list *instructions, struct _mesa_glsl_parse_state *state,
532de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt		ir_rvalue *lvalue, YYLTYPE loc)
533de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt{
534de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   ir_variable *var;
535de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   ir_rvalue *var_deref;
536de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
537de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   /* FINISHME: Give unique names to the temporaries. */
538de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   var = new ir_variable(lvalue->type, "_internal_tmp");
539de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   var->mode = ir_var_auto;
540de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
541de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   var_deref = new ir_dereference(var);
542de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   do_assignment(instructions, state, var_deref, lvalue, loc);
543de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
544de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   /* Once we've created this temporary, mark it read only so it's no
545de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt    * longer considered an lvalue.
546de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt    */
547de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   var->read_only = true;
548de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
549de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   return var_deref;
550de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt}
551de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
552de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
553fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
5540044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_node::hir(exec_list *instructions,
55518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick	      struct _mesa_glsl_parse_state *state)
55618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick{
55718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   (void) instructions;
55818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   (void) state;
55918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
56018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   return NULL;
56118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick}
56218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
56318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
564fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
5650044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_expression::hir(exec_list *instructions,
56618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick		    struct _mesa_glsl_parse_state *state)
567a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
568a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   static const int operations[AST_NUM_OPERATORS] = {
569a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_assign doesn't convert to ir_expression. */
570a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_plus doesn't convert to ir_expression. */
571a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_neg,
572a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_add,
573a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_sub,
574a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mul,
575a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_div,
576a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mod,
577a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lshift,
578a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_rshift,
579a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_less,
580a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_greater,
581a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lequal,
582a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_gequal,
583a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_equal,
584a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_nequal,
585a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_and,
586a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_xor,
587a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_or,
588a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_bit_not,
589a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_and,
590a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_xor,
591a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_or,
592a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_logic_not,
593a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
594a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Note: The following block of expression types actually convert
595a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * to multiple IR instructions.
596a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
597a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mul,     /* ast_mul_assign */
598a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_div,     /* ast_div_assign */
599a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mod,     /* ast_mod_assign */
600a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_add,     /* ast_add_assign */
601a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_sub,     /* ast_sub_assign */
602a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lshift,  /* ast_ls_assign */
603a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_rshift,  /* ast_rs_assign */
604a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_and, /* ast_and_assign */
605a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_xor, /* ast_xor_assign */
606a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_or,  /* ast_or_assign */
607a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
608a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_conditional doesn't convert to ir_expression. */
609de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_add,     /* ast_pre_inc. */
610de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_sub,     /* ast_pre_dec. */
611de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_add,     /* ast_post_inc. */
612de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_sub,     /* ast_post_dec. */
613a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_field_selection doesn't conv to ir_expression. */
614a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_array_index doesn't convert to ir_expression. */
615a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_function_call doesn't conv to ir_expression. */
616a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_identifier doesn't convert to ir_expression. */
617a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_int_constant doesn't convert to ir_expression. */
618a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_uint_constant doesn't conv to ir_expression. */
619a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_float_constant doesn't conv to ir_expression. */
620a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_bool_constant doesn't conv to ir_expression. */
621a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_sequence doesn't convert to ir_expression. */
622a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   };
623fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke   ir_rvalue *result = NULL;
624fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke   ir_rvalue *op[2];
625a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   struct simple_node op_list;
6260471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   const struct glsl_type *type = glsl_type::error_type;
627a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   bool error_emitted = false;
628a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   YYLTYPE loc;
629a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
63018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   loc = this->get_location();
631a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   make_empty_list(& op_list);
632a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
63318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   switch (this->oper) {
6346652af36fe8994b1621d882fcc230d320908a2a3Ian Romanick   case ast_assign: {
63518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
63618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
637a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
63810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      result = do_assignment(instructions, state, op[0], op[1],
63910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt			     this->subexpressions[0]->get_location());
64010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      error_emitted = result->type->is_error();
64110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      type = result->type;
642a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
6436652af36fe8994b1621d882fcc230d320908a2a3Ian Romanick   }
644a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
645a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_plus:
64618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
647a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
648a43817a483a8c4a480ef4e6dfda2cef899300eb0Ian Romanick      error_emitted = op[0]->type->is_error();
649a43817a483a8c4a480ef4e6dfda2cef899300eb0Ian Romanick      if (type->is_error())
650a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 op[0]->type = type;
651a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
652a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      result = op[0];
653a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
654a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
655a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_neg:
65618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
657a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
65865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = unary_arithmetic_result_type(op[0]->type, state, & loc);
659a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
66065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
661a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
66218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_expression(operations[this->oper], type,
663a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick				 op[0], NULL);
664a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
665a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
666a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_add:
667a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sub:
668a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mul:
669a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_div:
67018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
67118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
672a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
673bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      type = arithmetic_result_type(op[0], op[1],
67418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick				    (this->oper == ast_mul),
675a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt				    state, & loc);
676a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      error_emitted = type->is_error();
677a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
67818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_expression(operations[this->oper], type,
679a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick				 op[0], op[1]);
680a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
681a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
682a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mod:
68318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
68418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
685a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
68665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
687a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
68818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      assert(operations[this->oper] == ir_binop_mod);
689a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
69018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_expression(operations[this->oper], type,
691a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick				 op[0], op[1]);
69265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
693a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
694a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
695a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_lshift:
696a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_rshift:
697183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      _mesa_glsl_error(& loc, state, "FINISHME: implement bit-shift operators");
698183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      error_emitted = true;
699a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
700a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
701a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_less:
702a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_greater:
703a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_lequal:
704a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_gequal:
70518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
70618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
707a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
70865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = relational_result_type(op[0], op[1], state, & loc);
709a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
710a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* The relational operators must either generate an error or result
711a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * in a scalar boolean.  See page 57 of the GLSL 1.50 spec.
712a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
713a43817a483a8c4a480ef4e6dfda2cef899300eb0Ian Romanick      assert(type->is_error()
714a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	     || ((type->base_type == GLSL_TYPE_BOOL)
715cb36f8aaeeb09660843316270a781948f773d90bIan Romanick		 && type->is_scalar()));
716a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
71718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_expression(operations[this->oper], type,
718a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick				 op[0], op[1]);
71965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
720a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
721a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
722a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_nequal:
723a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_equal:
7246e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
7256e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
7266e659caaa946339a2de3890a8bed091ccb65102aIan Romanick
7276e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
7286e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *
7296e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    "The equality operators equal (==), and not equal (!=)
7306e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    operate on all types. They result in a scalar Boolean. If
7316e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    the operand types do not match, then there must be a
7326e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    conversion from Section 4.1.10 "Implicit Conversions"
7336e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    applied to one operand that can make them match, in which
7346e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    case this conversion is done."
7356e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       */
736bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      if ((!apply_implicit_conversion(op[0]->type, op[1], state)
737bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick	   && !apply_implicit_conversion(op[1]->type, op[0], state))
738212b0327b47033442842a7be3d7fb10e08e2bf66Ian Romanick	  || (op[0]->type != op[1]->type)) {
7396e659caaa946339a2de3890a8bed091ccb65102aIan Romanick	 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
7406e659caaa946339a2de3890a8bed091ccb65102aIan Romanick			  "type", (this->oper == ast_equal) ? "==" : "!=");
7416e659caaa946339a2de3890a8bed091ccb65102aIan Romanick	 error_emitted = true;
742a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick      } else if ((state->language_version <= 110)
743a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick		 && (op[0]->type->is_array() || op[1]->type->is_array())) {
744a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick	 _mesa_glsl_error(& loc, state, "array comparisons forbidden in "
745a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick			  "GLSL 1.10");
746a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick	 error_emitted = true;
7476e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      }
7486e659caaa946339a2de3890a8bed091ccb65102aIan Romanick
7496e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      result = new ir_expression(operations[this->oper], glsl_type::bool_type,
7506e659caaa946339a2de3890a8bed091ccb65102aIan Romanick				 op[0], op[1]);
7516e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      type = glsl_type::bool_type;
7526e659caaa946339a2de3890a8bed091ccb65102aIan Romanick
7536e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      assert(result->type == glsl_type::bool_type);
754a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
755a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
756a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_and:
757a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_xor:
758a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_or:
759a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_not:
760183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      _mesa_glsl_error(& loc, state, "FINISHME: implement bit-wise operators");
761183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      error_emitted = true;
762a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
763a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
764a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_logic_and:
765a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_logic_xor:
766a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_logic_or:
767b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
768b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      op[1] = this->subexpressions[1]->hir(instructions, state);
769b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
770b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
771b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt	 YYLTYPE loc = this->subexpressions[0]->get_location();
772b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
773b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt	 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
774b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt			  operator_string(this->oper));
775ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt	 error_emitted = true;
776b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      }
777b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
778b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
779b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt	 YYLTYPE loc = this->subexpressions[1]->get_location();
780b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
781b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt	 _mesa_glsl_error(& loc, state, "RHS of `%s' must be scalar boolean",
782b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt			  operator_string(this->oper));
783ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt	 error_emitted = true;
784b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      }
785b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
786b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      result = new ir_expression(operations[this->oper], glsl_type::bool_type,
787b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt				 op[0], op[1]);
788ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt      type = glsl_type::bool_type;
789a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
790a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
791a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt   case ast_logic_not:
792a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
793a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
794a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
795a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt	 YYLTYPE loc = this->subexpressions[0]->get_location();
796a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
797a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt	 _mesa_glsl_error(& loc, state,
798a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt			  "operand of `!' must be scalar boolean");
799ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt	 error_emitted = true;
800a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      }
801a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
802a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      result = new ir_expression(operations[this->oper], glsl_type::bool_type,
803a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt				 op[0], NULL);
804ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt      type = glsl_type::bool_type;
805a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      break;
806a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
807a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mul_assign:
808a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_div_assign:
809a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_add_assign:
810a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sub_assign: {
81118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
81218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
813a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
814bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      type = arithmetic_result_type(op[0], op[1],
81518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick				    (this->oper == ast_mul_assign),
816a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt				    state, & loc);
817a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
818fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke      ir_rvalue *temp_rhs = new ir_expression(operations[this->oper], type,
819fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke				              op[0], op[1]);
820a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
82110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      result = do_assignment(instructions, state, op[0], temp_rhs,
82210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt			     this->subexpressions[0]->get_location());
82310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      type = result->type;
82410a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      error_emitted = (op[0]->type->is_error());
825a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
826a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* GLSL 1.10 does not allow array assignment.  However, we don't have to
827a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * explicitly test for this because none of the binary expression
828a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * operators allow array operands either.
829a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
830a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
831a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
832a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
833a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
83448a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt   case ast_mod_assign: {
83548a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
83648a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      op[1] = this->subexpressions[1]->hir(instructions, state);
83748a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
83865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
83948a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
84048a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      assert(operations[this->oper] == ir_binop_mod);
84148a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
84248a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      struct ir_rvalue *temp_rhs;
84348a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      temp_rhs = new ir_expression(operations[this->oper], type,
84448a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt				   op[0], op[1]);
84548a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
84648a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      result = do_assignment(instructions, state, op[0], temp_rhs,
84748a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt			     this->subexpressions[0]->get_location());
84848a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      type = result->type;
84965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
85048a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      break;
85148a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt   }
852a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
853a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_ls_assign:
854a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_rs_assign:
855183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      _mesa_glsl_error(& loc, state,
856183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt		       "FINISHME: implement bit-shift assignment operators");
857183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      error_emitted = true;
858251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
859a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
860a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_and_assign:
861a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_xor_assign:
862a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_or_assign:
863183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      _mesa_glsl_error(& loc, state,
864183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt		       "FINISHME: implement logic assignment operators");
865183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      error_emitted = true;
866251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
867a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
86896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick   case ast_conditional: {
86996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
87096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
87196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
87296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *
87396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    "The ternary selection operator (?:). It operates on three
87496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    expressions (exp1 ? exp2 : exp3). This operator evaluates the
87596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    first expression, which must result in a scalar Boolean."
87696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
87796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
87896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 YYLTYPE loc = this->subexpressions[0]->get_location();
87996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
88096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 _mesa_glsl_error(& loc, state, "?: condition must be scalar boolean");
88196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 error_emitted = true;
88296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      }
88396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
88496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* The :? operator is implemented by generating an anonymous temporary
88596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * followed by an if-statement.  The last instruction in each branch of
88696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * the if-statement assigns a value to the anonymous temporary.  This
88796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * temporary is the r-value of the expression.
88896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
88996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      ir_variable *const tmp = generate_temporary(glsl_type::error_type,
89096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick						  instructions, state);
89196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
89296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      ir_if *const stmt = new ir_if(op[0]);
89396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      instructions->push_tail(stmt);
89496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
89596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      op[1] = this->subexpressions[1]->hir(& stmt->then_instructions, state);
89696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      ir_dereference *const then_deref = new ir_dereference(tmp);
89796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      ir_assignment *const then_assign =
89896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 new ir_assignment(then_deref, op[1], NULL);
89996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      stmt->then_instructions.push_tail(then_assign);
90096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
90196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      op[2] = this->subexpressions[2]->hir(& stmt->else_instructions, state);
90296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      ir_dereference *const else_deref = new ir_dereference(tmp);
90396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      ir_assignment *const else_assign =
90496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 new ir_assignment(else_deref, op[2], NULL);
90596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      stmt->else_instructions.push_tail(else_assign);
90696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
90796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
90896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *
90996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     "The second and third expressions can be any type, as
91096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     long their types match, or there is a conversion in
91196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     Section 4.1.10 "Implicit Conversions" that can be applied
91296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     to one of the expressions to make their types match. This
91396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     resulting matching type is the type of the entire
91496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     expression."
91596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
916bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      if ((!apply_implicit_conversion(op[1]->type, op[2], state)
917bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick	   && !apply_implicit_conversion(op[2]->type, op[1], state))
918db9be2e7aa3a56e43b725ad7725fe6b424e4933eIan Romanick	  || (op[1]->type != op[2]->type)) {
91996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 YYLTYPE loc = this->subexpressions[1]->get_location();
92096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
92196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
92296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick			  "operator must have matching types.");
92396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 error_emitted = true;
924db9be2e7aa3a56e43b725ad7725fe6b424e4933eIan Romanick      } else {
925db9be2e7aa3a56e43b725ad7725fe6b424e4933eIan Romanick	 tmp->type = op[1]->type;
92696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      }
92796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
92896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      result = new ir_dereference(tmp);
92996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      type = tmp->type;
930251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
93196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick   }
932a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
933a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_pre_inc:
93476ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt   case ast_pre_dec: {
93576ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
93676ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
93776ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt	 op[1] = new ir_constant(1.0f);
93876ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      else
93976ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt	 op[1] = new ir_constant(1);
94076ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
941a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      type = arithmetic_result_type(op[0], op[1], false, state, & loc);
94276ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
94376ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      struct ir_rvalue *temp_rhs;
94476ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      temp_rhs = new ir_expression(operations[this->oper], type,
94576ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt				   op[0], op[1]);
94676ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
94776ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      result = do_assignment(instructions, state, op[0], temp_rhs,
94876ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt			     this->subexpressions[0]->get_location());
94976ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      type = result->type;
95076ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      error_emitted = op[0]->type->is_error();
95176ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      break;
95276ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt   }
953a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
954a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_post_inc:
955de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   case ast_post_dec: {
956de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
957de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
958de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt	 op[1] = new ir_constant(1.0f);
959de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      else
960de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt	 op[1] = new ir_constant(1);
961de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
962de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
963de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
964a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      type = arithmetic_result_type(op[0], op[1], false, state, & loc);
965de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
966de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      struct ir_rvalue *temp_rhs;
967de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      temp_rhs = new ir_expression(operations[this->oper], type,
968de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt				   op[0], op[1]);
969de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
970de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      /* Get a temporary of a copy of the lvalue before it's modified.
971de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt       * This may get thrown away later.
972de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt       */
973de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      result = get_lvalue_copy(instructions, state, op[0],
974de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt			       this->subexpressions[0]->get_location());
975de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
976de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      (void)do_assignment(instructions, state, op[0], temp_rhs,
977de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt			  this->subexpressions[0]->get_location());
978de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
979de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      type = result->type;
980de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      error_emitted = op[0]->type->is_error();
981a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
982de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   }
983a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
984a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_field_selection:
98518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = _mesa_ast_field_selection_to_hir(this, instructions, state);
986a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = result->type;
987a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
988a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
98927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick   case ast_array_index: {
99027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      YYLTYPE index_loc = subexpressions[1]->get_location();
99127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
99227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      op[0] = subexpressions[0]->hir(instructions, state);
99327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      op[1] = subexpressions[1]->hir(instructions, state);
99427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
99527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
99627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
997b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      ir_dereference *const lhs = op[0]->as_dereference();
998b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      ir_instruction *array;
999b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      if ((lhs != NULL)
1000b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	  && (lhs->mode == ir_dereference::ir_reference_variable)) {
1001b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	 result = new ir_dereference(lhs->var, op[1]);
1002b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
1003b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	 delete op[0];
1004b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	 array = lhs->var;
1005b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      } else {
1006b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	 result = new ir_dereference(op[0], op[1]);
1007b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	 array = op[0];
1008b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      }
1009b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
1010b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      /* Do not use op[0] after this point.  Use array.
1011b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick       */
1012b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      op[0] = NULL;
1013b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
101427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
101527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (error_emitted)
101627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 break;
101727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
101863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick      if (!array->type->is_array()
101963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	  && !array->type->is_matrix()
102063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	  && !array->type->is_vector()) {
102127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
102263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			  "cannot dereference non-array / non-matrix / "
102363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			  "non-vector");
102427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
102527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
102627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
102727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (!op[1]->type->is_integer()) {
102827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
102927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick			  "array index must be integer type");
103027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
103127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      } else if (!op[1]->type->is_scalar()) {
103227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
103327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick			  "array index must be scalar");
103427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
103527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
103627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
103727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      /* If the array index is a constant expression and the array has a
103827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * declared size, ensure that the access is in-bounds.  If the array
103927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * index is not a constant expression, ensure that the array has a
104027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * declared size.
104127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       */
104227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      ir_constant *const const_index = op[1]->constant_expression_value();
104327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (const_index != NULL) {
104427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 const int idx = const_index->value.i[0];
104563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 const char *type_name;
104663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 unsigned bound = 0;
104763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick
104863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (array->type->is_matrix()) {
104963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    type_name = "matrix";
105063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else if (array->type->is_vector()) {
105163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    type_name = "vector";
105263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else {
105363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    type_name = "array";
105463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 }
105527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
105627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
105727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *
105827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    "It is illegal to declare an array with a size, and then
105927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    later (in the same shader) index the same array with an
106027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    integral constant expression greater than or equal to the
106127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    declared size. It is also illegal to index an array with a
106227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    negative constant expression."
106327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  */
106463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (array->type->is_matrix()) {
106563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if (array->type->row_type()->vector_elements <= idx) {
106663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       bound = array->type->row_type()->vector_elements;
106763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    }
106863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else if (array->type->is_vector()) {
106963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if (array->type->vector_elements <= idx) {
107063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       bound = array->type->vector_elements;
107163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    }
107263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else {
107363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if ((array->type->array_size() > 0)
107463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick		&& (array->type->array_size() <= idx)) {
107563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       bound = array->type->array_size();
107663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    }
107727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 }
107827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
107963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (bound > 0) {
108063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    _mesa_glsl_error(& loc, state, "%s index must be < %u",
108163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			     type_name, bound);
108263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    error_emitted = true;
108363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else if (idx < 0) {
108463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    _mesa_glsl_error(& loc, state, "%s index must be >= 0",
108563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			     type_name);
108627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	    error_emitted = true;
108727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 }
1088b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
108963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (array->type->is_array()) {
109063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    ir_variable *const v = array->as_variable();
109163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if ((v != NULL) && (unsigned(idx) > v->max_array_access))
109263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       v->max_array_access = idx;
109363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 }
109427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
109527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
109627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (error_emitted)
109727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 result->type = glsl_type::error_type;
109827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
109927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      type = result->type;
1100a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
110127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick   }
1102a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1103a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_function_call:
11047cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick      /* Should *NEVER* get here.  ast_function_call should always be handled
11057cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick       * by ast_function_expression::hir.
1106a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
11077cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick      assert(0);
1108a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1109a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1110a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_identifier: {
1111a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* ast_identifier can appear several places in a full abstract syntax
1112a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * tree.  This particular use must be at location specified in the grammar
1113a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * as 'variable_identifier'.
1114a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
11158bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      ir_variable *var =
11168bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick	 state->symbols->get_variable(this->primary_expression.identifier);
1117a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1118a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      result = new ir_dereference(var);
1119a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1120a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (var != NULL) {
1121a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 type = result->type;
1122a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
112371d0bbfcb2853f37b580ec7b705e55bb0eb426faIan Romanick	 _mesa_glsl_error(& loc, state, "`%s' undeclared",
112418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  this->primary_expression.identifier);
1125a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1126a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 error_emitted = true;
1127a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1128a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1129a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1130a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1131a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_int_constant:
11320471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::int_type;
113318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_constant(type, & this->primary_expression);
1134a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1135a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1136a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_uint_constant:
11370471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::uint_type;
113818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_constant(type, & this->primary_expression);
1139a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1140a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1141a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_float_constant:
11420471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::float_type;
114318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_constant(type, & this->primary_expression);
1144a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1145a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1146a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bool_constant:
11470471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::bool_type;
114818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_constant(type, & this->primary_expression);
1149a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1150a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1151a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sequence: {
1152a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      struct simple_node *ptr;
1153a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1154a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* It should not be possible to generate a sequence in the AST without
1155a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * any expressions in it.
1156a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
115718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      assert(!is_empty_list(&this->expressions));
1158a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1159a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* The r-value of a sequence is the last expression in the sequence.  If
1160a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * the other expressions in the sequence do not have side-effects (and
1161a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * therefore add instructions to the instruction list), they get dropped
1162a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * on the floor.
1163a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
116418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      foreach (ptr, &this->expressions)
116518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick	 result = ((ast_node *)ptr)->hir(instructions, state);
1166a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1167a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = result->type;
1168a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1169a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Any errors should have already been emitted in the loop above.
1170a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1171a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      error_emitted = true;
1172a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1173a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1174a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1175a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1176cef3baecf636a30b62cd7a1e8de57c7650f7003eIan Romanick   if (type->is_error() && !error_emitted)
117771d0bbfcb2853f37b580ec7b705e55bb0eb426faIan Romanick      _mesa_glsl_error(& loc, state, "type mismatch");
1178a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1179a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return result;
1180a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1181a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1182a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1183fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
11840044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_expression_statement::hir(exec_list *instructions,
118518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			      struct _mesa_glsl_parse_state *state)
1186a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1187a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* It is possible to have expression statements that don't have an
1188a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * expression.  This is the solitary semicolon:
1189a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
1190a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * for (i = 0; i < 5; i++)
1191a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     ;
1192a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
1193a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * In this case the expression will be NULL.  Test for NULL and don't do
1194a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * anything in that case.
1195a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
119618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (expression != NULL)
119718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      expression->hir(instructions, state);
1198a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1199a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Statements do not have r-values.
1200a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1201a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1202a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1203a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1204a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1205fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
12060044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_compound_statement::hir(exec_list *instructions,
120718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			    struct _mesa_glsl_parse_state *state)
1208a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1209a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   struct simple_node *ptr;
1210a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1211a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
121218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (new_scope)
12138bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      state->symbols->push_scope();
1214a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
121518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   foreach (ptr, &statements)
121618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      ((ast_node *)ptr)->hir(instructions, state);
1217a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
121818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (new_scope)
12198bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      state->symbols->pop_scope();
1220a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1221a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Compound statements do not have r-values.
1222a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1223a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1224a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1225a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1226a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
122728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanickstatic const glsl_type *
122828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanickprocess_array_type(const glsl_type *base, ast_node *array_size,
122928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick		   struct _mesa_glsl_parse_state *state)
123028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick{
123128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   unsigned length = 0;
123228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
123328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   /* FINISHME: Reject delcarations of multidimensional arrays. */
123428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
123528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   if (array_size != NULL) {
123628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      exec_list dummy_instructions;
123728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
123828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      YYLTYPE loc = array_size->get_location();
123928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
124028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      /* FINISHME: Verify that the grammar forbids side-effects in array
124128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick       * FINISHME: sizes.   i.e., 'vec4 [x = 12] data'
124228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick       */
124328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      assert(dummy_instructions.is_empty());
124428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
124528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      if (ir != NULL) {
124628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 if (!ir->type->is_integer()) {
124728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    _mesa_glsl_error(& loc, state, "array size must be integer type");
124828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 } else if (!ir->type->is_scalar()) {
124928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    _mesa_glsl_error(& loc, state, "array size must be scalar type");
125028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 } else {
125128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    ir_constant *const size = ir->constant_expression_value();
125228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
125328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    if (size == NULL) {
125428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       _mesa_glsl_error(& loc, state, "array size must be a "
125528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick				"constant valued expression");
125628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    } else if (size->value.i[0] <= 0) {
125728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       _mesa_glsl_error(& loc, state, "array size must be > 0");
125828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    } else {
125928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       assert(size->type == ir->type);
126028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       length = size->value.u[0];
126128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    }
126228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 }
126328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      }
126428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   }
126528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
126628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   return glsl_type::get_array_instance(base, length);
126728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick}
126828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
126928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
1270d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanickconst glsl_type *
1271d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanickast_type_specifier::glsl_type(const char **name,
1272d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick			      struct _mesa_glsl_parse_state *state) const
1273a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1274d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   const struct glsl_type *type;
1275a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1276d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   if (this->type_specifier == ast_struct) {
1277a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* FINISHME: Handle annonymous structures. */
1278a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = NULL;
1279a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
1280d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick      type = state->symbols->get_type(this->type_name);
1281d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick      *name = this->type_name;
1282a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1283d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick      if (this->is_array) {
1284d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick	 type = process_array_type(type, this->array_size, state);
128528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      }
1286a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1287a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1288a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return type;
1289a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1290a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1291a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1292a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic void
1293a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickapply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
1294a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick				 struct ir_variable *var,
12952e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				 struct _mesa_glsl_parse_state *state,
12962e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				 YYLTYPE *loc)
1297a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1298a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->invariant)
1299a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->invariant = 1;
1300a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1301a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* FINISHME: Mark 'in' variables at global scope as read-only. */
1302a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->constant || qual->attribute || qual->uniform
1303a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       || (qual->varying && (state->target == fragment_shader)))
1304a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->read_only = 1;
1305a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1306a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->centroid)
1307a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->centroid = 1;
1308a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1309ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick   if (qual->attribute && state->target != vertex_shader) {
13102e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      var->type = glsl_type::error_type;
13112e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      _mesa_glsl_error(loc, state,
13122e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt		       "`attribute' variables may not be declared in the "
1313ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick		       "%s shader",
1314ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick		       _mesa_glsl_shader_target_name(state->target));
13152e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   }
13162e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt
131790b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt   /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
131890b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *
131990b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     "The varying qualifier can be used only with the data types
132090b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
132190b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     these."
132290b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    */
132390b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt   if (qual->varying && var->type->base_type != GLSL_TYPE_FLOAT) {
132490b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt      var->type = glsl_type::error_type;
132590b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt      _mesa_glsl_error(loc, state,
132690b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt		       "varying variables must be of base type float");
132790b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt   }
132890b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt
1329a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->in && qual->out)
1330a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_inout;
1331a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else if (qual->attribute || qual->in
1332a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    || (qual->varying && (state->target == fragment_shader)))
1333a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_in;
13340b678234625fac67a89285ad2871dedc891fb1b1Ian Romanick   else if (qual->out || (qual->varying && (state->target == vertex_shader)))
1335a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_out;
1336a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else if (qual->uniform)
1337a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_uniform;
1338a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else
1339a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_auto;
1340a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1341a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->flat)
1342a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->interpolation = ir_var_flat;
1343a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else if (qual->noperspective)
1344a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->interpolation = ir_var_noperspective;
1345a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else
1346a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->interpolation = ir_var_smooth;
13479d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick
13489d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick   if (var->type->is_array() && (state->language_version >= 120)) {
13499d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick      var->array_lvalue = true;
13509d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick   }
1351a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1352a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1353a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1354fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
13550044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_declarator_list::hir(exec_list *instructions,
135618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			 struct _mesa_glsl_parse_state *state)
1357a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1358a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   struct simple_node *ptr;
1359a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const struct glsl_type *decl_type;
1360a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const char *type_name = NULL;
13618558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt   ir_rvalue *result = NULL;
1362a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1363a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* FINISHME: Handle vertex shader "invariant" declarations that do not
1364a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * FINISHME: include a type.  These re-declare built-in variables to be
1365a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * FINISHME: invariant.
1366a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1367a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1368d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   decl_type = this->type->specifier->glsl_type(& type_name, state);
1369a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
137018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   foreach (ptr, &this->declarations) {
1371a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      struct ast_declaration *const decl = (struct ast_declaration * )ptr;
1372a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      const struct glsl_type *var_type;
1373a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      struct ir_variable *var;
13742e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      YYLTYPE loc = this->get_location();
1375a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1376a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* FINISHME: Emit a warning if a variable declaration shadows a
1377a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * FINISHME: declaration at a higher scope.
1378a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1379a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1380cec65a6b76290ee4da91691bd3ef01c3fb8a0c37Ian Romanick      if ((decl_type == NULL) || decl_type->is_void()) {
1381a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 if (type_name != NULL) {
1382a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    _mesa_glsl_error(& loc, state,
1383a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     "invalid type `%s' in declaration of `%s'",
1384a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     type_name, decl->identifier);
1385a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 } else {
1386a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    _mesa_glsl_error(& loc, state,
1387a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     "invalid type in declaration of `%s'",
1388a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     decl->identifier);
1389a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
1390a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 continue;
1391a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1392a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1393a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (decl->is_array) {
139428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 var_type = process_array_type(decl_type, decl->array_size, state);
1395a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
1396a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 var_type = decl_type;
1397a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1398a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1399a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var = new ir_variable(var_type, decl->identifier);
1400a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
14013f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
14023f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *
14033f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     "Global variables can only use the qualifiers const,
14043f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     attribute, uni form, or varying. Only one may be
14053f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     specified.
14063f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *
14073f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     Local variables can only use the qualifier const."
14083f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *
14093f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       * This is relaxed in GLSL 1.30.
14103f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       */
14113f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      if (state->language_version < 120) {
14123f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 if (this->type->qualifier.out) {
14133f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	    _mesa_glsl_error(& loc, state,
14143f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "`out' qualifier in declaration of `%s' "
14153f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "only valid for function parameters in GLSL 1.10.",
14163f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     decl->identifier);
14173f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 }
14183f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 if (this->type->qualifier.in) {
14193f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	    _mesa_glsl_error(& loc, state,
14203f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "`in' qualifier in declaration of `%s' "
14213f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "only valid for function parameters in GLSL 1.10.",
14223f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     decl->identifier);
14233f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 }
14243f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 /* FINISHME: Test for other invalid qualifiers. */
14253f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      }
14263f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt
14272e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
14282e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				       & loc);
1429a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1430a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Attempt to add the variable to the symbol table.  If this fails, it
1431a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick       * means the variable has already been declared at this scope.  Arrays
1432a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick       * fudge this rule a little bit.
1433a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick       *
1434a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick       * From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
1435a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick       *
1436a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick       *    "It is legal to declare an array without a size and then
1437a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick       *    later re-declare the same name as an array of the same
1438a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick       *    type and specify a size."
1439a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
14403359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      if (state->symbols->name_declared_this_scope(decl->identifier)) {
1441a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	 ir_variable *const earlier =
1442a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	    state->symbols->get_variable(decl->identifier);
1443a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick
1444a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	 if ((earlier != NULL)
1445a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	     && (earlier->type->array_size() == 0)
1446a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	     && var->type->is_array()
1447a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	     && (var->type->element_type() == earlier->type->element_type())) {
1448a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	    /* FINISHME: This doesn't match the qualifiers on the two
1449a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	     * FINISHME: declarations.  It's not 100% clear whether this is
1450a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	     * FINISHME: required or not.
1451a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	     */
1452b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
1453894ea972a4defdaafeaa3a248c113b06c7ae0c7eEric Anholt	    if (var->type->array_size() <= (int)earlier->max_array_access) {
1454b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	       YYLTYPE loc = this->get_location();
1455b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
1456b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	       _mesa_glsl_error(& loc, state, "array size must be > %u due to "
1457b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick				"previous access",
1458b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick				earlier->max_array_access);
1459b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	    }
1460b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
1461a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	    earlier->type = var->type;
1462a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	    delete var;
1463a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	    var = NULL;
1464a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	 } else {
1465a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	    YYLTYPE loc = this->get_location();
1466a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick
1467a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	    _mesa_glsl_error(& loc, state, "`%s' redeclared",
1468a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick			     decl->identifier);
1469a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	 }
1470a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1471a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 continue;
1472a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1473a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1474b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt      /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
1475b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt       *
1476b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt       *   "Identifiers starting with "gl_" are reserved for use by
1477b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt       *   OpenGL, and may not be declared in a shader as either a
1478b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt       *   variable or a function."
1479b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt       */
1480b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt      if (strncmp(decl->identifier, "gl_", 3) == 0) {
1481b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt	 /* FINISHME: This should only trigger if we're not redefining
1482b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt	  * FINISHME: a builtin (to add a qualifier, for example).
1483b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt	  */
1484b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt	 _mesa_glsl_error(& loc, state,
1485b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt			  "identifier `%s' uses reserved `gl_' prefix",
1486b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt			  decl->identifier);
1487b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt      }
1488b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt
14890044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick      instructions->push_tail(var);
1490a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1491e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick      if (state->current_function != NULL) {
1492b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 const char *mode = NULL;
1493e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 const char *extra = "";
1494b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick
1495e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 /* There is no need to check for 'inout' here because the parser will
1496e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	  * only allow that in function parameter lists.
1497e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	  */
1498e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	 if (this->type->qualifier.attribute) {
1499b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	    mode = "attribute";
1500b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 } else if (this->type->qualifier.uniform) {
1501b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	    mode = "uniform";
1502b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 } else if (this->type->qualifier.varying) {
1503b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	    mode = "varying";
1504e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 } else if (this->type->qualifier.in) {
1505e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    mode = "in";
1506e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    extra = " or in function parameter list";
1507e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 } else if (this->type->qualifier.out) {
1508e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    mode = "out";
1509e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    extra = " or in function parameter list";
1510b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 }
1511b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick
1512b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 if (mode) {
1513e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	    _mesa_glsl_error(& loc, state,
1514b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick			     "%s variable `%s' must be declared at "
1515e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick			     "global scope%s",
1516e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick			     mode, var->name, extra);
1517e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	 }
1518e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick      } else if (var->mode == ir_var_in) {
1519fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	 if (state->target == vertex_shader) {
1520fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    bool error_emitted = false;
1521fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
1522fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1523fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
1524fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    "Vertex shader inputs can only be float, floating-point
1525fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    vectors, matrices, signed and unsigned integers and integer
1526fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    vectors. Vertex shader inputs can also form arrays of these
1527fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    types, but not structures."
1528fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
15292d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
15302d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *
15312d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *    "Vertex shader inputs can only be float, floating-point
15322d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *    vectors, matrices, signed and unsigned integers and integer
15332d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *    vectors. They cannot be arrays or structures."
15342d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *
1535fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
1536fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
1537fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    "The attribute qualifier can be used only with float,
1538fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    floating-point vectors, and matrices. Attribute variables
1539fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    cannot be declared as arrays or structures."
1540fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     */
1541fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    const glsl_type *check_type = var->type->is_array()
1542fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       ? var->type->fields.array : var->type;
1543fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
1544fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    switch (check_type->base_type) {
1545fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_FLOAT:
1546fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       break;
1547fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_UINT:
1548fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_INT:
1549fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       if (state->language_version > 120)
1550fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick		  break;
1551fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       /* FALLTHROUGH */
1552fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    default:
1553fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       _mesa_glsl_error(& loc, state,
1554fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"vertex shader input / attribute cannot have "
1555fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"type %s`%s'",
1556fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				var->type->is_array() ? "array of " : "",
1557fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				check_type->name);
1558fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       error_emitted = true;
1559fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    }
1560fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
15612d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	    if (!error_emitted && (state->language_version <= 130)
1562fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick		&& var->type->is_array()) {
1563fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       _mesa_glsl_error(& loc, state,
1564fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"vertex shader input / attribute cannot have "
1565fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"array type");
1566fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       error_emitted = true;
1567fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    }
1568fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	 }
1569fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick      }
1570fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
157166faec4895b7bb59a614087a200c05157191b4aeIan Romanick      if (decl->initializer != NULL) {
157243de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 YYLTYPE initializer_loc = decl->initializer->get_location();
157343de17282017bdf187d6e646de3262cc64b7f46bIan Romanick
157466faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
157566faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *
157666faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *    "All uniform variables are read-only and are initialized either
157766faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *    directly by an application via API commands, or indirectly by
157866faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *    OpenGL."
157966faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  */
158066faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 if ((state->language_version <= 110)
158166faec4895b7bb59a614087a200c05157191b4aeIan Romanick	     && (var->mode == ir_var_uniform)) {
158243de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	    _mesa_glsl_error(& initializer_loc, state,
158343de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     "cannot initialize uniforms in GLSL 1.10");
158443de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 }
158543de17282017bdf187d6e646de3262cc64b7f46bIan Romanick
158643de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 if (var->type->is_sampler()) {
158743de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	    _mesa_glsl_error(& initializer_loc, state,
158843de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     "cannot initialize samplers");
158943de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 }
159019360152f5bd8cff93359dbfe5a50a90b699c118Ian Romanick
159143de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 if ((var->mode == ir_var_in) && (state->current_function == NULL)) {
159243de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	    _mesa_glsl_error(& initializer_loc, state,
159343de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     "cannot initialize %s shader input / %s",
1594ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick			     _mesa_glsl_shader_target_name(state->target),
159543de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     (state->target == vertex_shader)
159643de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     ? "attribute" : "varying");
159766faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 }
159866faec4895b7bb59a614087a200c05157191b4aeIan Romanick
159966faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 ir_dereference *const lhs = new ir_dereference(var);
1600307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	 ir_rvalue *rhs = decl->initializer->hir(instructions, state);
160119360152f5bd8cff93359dbfe5a50a90b699c118Ian Romanick
1602307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	 /* Calculate the constant value if this is a const
1603307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	  * declaration.
160466faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  */
1605307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	 if (this->type->qualifier.constant) {
1606326c676236e6a3c90db63e4d0c893aa4f9c21876Eric Anholt	    ir_constant *constant_value = rhs->constant_expression_value();
1607326c676236e6a3c90db63e4d0c893aa4f9c21876Eric Anholt	    if (!constant_value) {
1608307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	       _mesa_glsl_error(& initializer_loc, state,
1609307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt				"initializer of const variable `%s' must be a "
1610307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt				"constant expression",
1611307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt				decl->identifier);
1612326c676236e6a3c90db63e4d0c893aa4f9c21876Eric Anholt	    } else {
1613326c676236e6a3c90db63e4d0c893aa4f9c21876Eric Anholt	       rhs = constant_value;
1614326c676236e6a3c90db63e4d0c893aa4f9c21876Eric Anholt	       var->constant_value = constant_value;
1615307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	    }
1616307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	 }
161766faec4895b7bb59a614087a200c05157191b4aeIan Romanick
1618307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	 if (rhs && !rhs->type->is_error()) {
1619ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt	    bool temp = var->read_only;
1620ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt	    if (this->type->qualifier.constant)
1621ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt	       var->read_only = false;
16228558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt	    result = do_assignment(instructions, state, lhs, rhs,
16238558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt				   this->get_location());
1624ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt	    var->read_only = temp;
162566faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 }
162666faec4895b7bb59a614087a200c05157191b4aeIan Romanick      }
162717d86f4371da413176ba365ca26a58bac172d365Ian Romanick
16280ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt      /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
16290ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *
16300ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *     "It is an error to write to a const variable outside of
16310ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *      its declaration, so they must be initialized when
16320ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *      declared."
16330ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       */
16340ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt      if (this->type->qualifier.constant && decl->initializer == NULL) {
16350ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt	 _mesa_glsl_error(& loc, state,
16360ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt			  "const declaration of `%s' must be initialized");
16370ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt      }
16380ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt
163917d86f4371da413176ba365ca26a58bac172d365Ian Romanick      /* Add the vairable to the symbol table after processing the initializer.
164017d86f4371da413176ba365ca26a58bac172d365Ian Romanick       * This differs from most C-like languages, but it follows the GLSL
164117d86f4371da413176ba365ca26a58bac172d365Ian Romanick       * specification.  From page 28 (page 34 of the PDF) of the GLSL 1.50
164217d86f4371da413176ba365ca26a58bac172d365Ian Romanick       * spec:
164317d86f4371da413176ba365ca26a58bac172d365Ian Romanick       *
164417d86f4371da413176ba365ca26a58bac172d365Ian Romanick       *     "Within a declaration, the scope of a name starts immediately
164517d86f4371da413176ba365ca26a58bac172d365Ian Romanick       *     after the initializer if present or immediately after the name
164617d86f4371da413176ba365ca26a58bac172d365Ian Romanick       *     being declared if not."
164717d86f4371da413176ba365ca26a58bac172d365Ian Romanick       */
164817d86f4371da413176ba365ca26a58bac172d365Ian Romanick      const bool added_variable =
164917d86f4371da413176ba365ca26a58bac172d365Ian Romanick	 state->symbols->add_variable(decl->identifier, var);
165017d86f4371da413176ba365ca26a58bac172d365Ian Romanick      assert(added_variable);
1651a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1652a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
16538558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt
16548558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt   /* Generally, variable declarations do not have r-values.  However,
16558558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * one is used for the declaration in
16568558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    *
16578558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * while (bool b = some_condition()) {
16588558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    *   ...
16598558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * }
16608558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    *
16618558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * so we return the rvalue from the last seen declaration here.
1662a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
16638558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt   return result;
1664a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1665a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1666a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1667fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
16680044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_parameter_declarator::hir(exec_list *instructions,
166918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			      struct _mesa_glsl_parse_state *state)
1670a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1671a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const struct glsl_type *type;
1672a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const char *name = NULL;
16732e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   YYLTYPE loc = this->get_location();
1674a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1675d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   type = this->type->specifier->glsl_type(& name, state);
1676a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1677a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (type == NULL) {
1678a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (name != NULL) {
1679a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 _mesa_glsl_error(& loc, state,
1680a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			  "invalid type `%s' in declaration of `%s'",
168118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  name, this->identifier);
1682a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
1683a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 _mesa_glsl_error(& loc, state,
1684a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			  "invalid type in declaration of `%s'",
168518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  this->identifier);
1686a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1687a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
16880471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::error_type;
1689a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1690a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1691068c80cfe0a280490353b6b007165d717c672eedEric Anholt   /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
1692068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *
1693068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    "Functions that accept no input arguments need not use void in the
1694068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    argument list because prototypes (or definitions) are required and
1695068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    therefore there is no ambiguity when an empty argument list "( )" is
1696068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    declared. The idiom "(void)" as a parameter list is provided for
1697068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    convenience."
1698068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *
1699068c80cfe0a280490353b6b007165d717c672eedEric Anholt    * Placing this check here prevents a void parameter being set up
1700068c80cfe0a280490353b6b007165d717c672eedEric Anholt    * for a function, which avoids tripping up checks for main taking
1701068c80cfe0a280490353b6b007165d717c672eedEric Anholt    * parameters and lookups of an unnamed symbol.
1702068c80cfe0a280490353b6b007165d717c672eedEric Anholt    */
1703cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   if (type->is_void()) {
1704cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      if (this->identifier != NULL)
1705cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick	 _mesa_glsl_error(& loc, state,
1706cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick			  "named parameter cannot have type `void'");
1707cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
1708cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      is_void = true;
1709068c80cfe0a280490353b6b007165d717c672eedEric Anholt      return NULL;
1710cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   }
1711068c80cfe0a280490353b6b007165d717c672eedEric Anholt
171245d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   if (formal_parameter && (this->identifier == NULL)) {
171345d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick      _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
171445d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick      return NULL;
171545d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   }
171645d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick
1717cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   is_void = false;
171818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   ir_variable *var = new ir_variable(type, this->identifier);
1719a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1720a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* FINISHME: Handle array declarations.  Note that this requires
1721a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * FINISHME: complete handling of constant expressions.
1722a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1723a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1724cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick   /* Apply any specified qualifiers to the parameter declaration.  Note that
1725cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick    * for function parameters the default mode is 'in'.
1726cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick    */
17272e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc);
1728cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick   if (var->mode == ir_var_auto)
1729cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick      var->mode = ir_var_in;
1730a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
17310044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick   instructions->push_tail(var);
1732a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1733a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Parameter declarations do not have r-values.
1734a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1735a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1736a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1737a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1738a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
173945d8a70c12ee6ea956baaf898324a828496382f6Ian Romanickvoid
174045d8a70c12ee6ea956baaf898324a828496382f6Ian Romanickast_parameter_declarator::parameters_to_hir(struct simple_node *ast_parameters,
174145d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					    bool formal,
174245d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					    exec_list *ir_parameters,
174345d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					    _mesa_glsl_parse_state *state)
1744a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1745a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   struct simple_node *ptr;
1746cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   ast_parameter_declarator *void_param = NULL;
1747cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   unsigned count = 0;
1748a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1749a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   foreach (ptr, ast_parameters) {
175045d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick      ast_parameter_declarator *param = (ast_parameter_declarator *)ptr;
175145d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick      param->formal_parameter = formal;
1752068c80cfe0a280490353b6b007165d717c672eedEric Anholt      param->hir(ir_parameters, state);
1753cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
1754cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      if (param->is_void)
1755cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick	 void_param = param;
1756cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
1757cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      count++;
1758cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   }
1759cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
1760cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   if ((void_param != NULL) && (count > 1)) {
1761cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      YYLTYPE loc = void_param->get_location();
1762cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
1763cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      _mesa_glsl_error(& loc, state,
1764cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick		       "`void' parameter must be only parameter");
1765a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1766a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1767a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1768a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1769a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic bool
17700044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickparameter_lists_match(exec_list *list_a, exec_list *list_b)
1771a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
17720044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick   exec_list_iterator iter_a = list_a->iterator();
17730044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick   exec_list_iterator iter_b = list_b->iterator();
1774a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
17750044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick   while (iter_a.has_next()) {
17765150c567a0bf082d93f25ba7e29d5573c9dffb8bEric Anholt      ir_variable *a = (ir_variable *)iter_a.get();
17775150c567a0bf082d93f25ba7e29d5573c9dffb8bEric Anholt      ir_variable *b = (ir_variable *)iter_b.get();
17785150c567a0bf082d93f25ba7e29d5573c9dffb8bEric Anholt
1779a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* If all of the parameters from the other parameter list have been
1780a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * exhausted, the lists have different length and, by definition,
1781a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * do not match.
1782a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
17830044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick      if (!iter_b.has_next())
1784a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 return false;
1785a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1786a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* If the types of the parameters do not match, the parameters lists
1787a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * are different.
1788a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
17895150c567a0bf082d93f25ba7e29d5573c9dffb8bEric Anholt      if (a->type != b->type)
17905150c567a0bf082d93f25ba7e29d5573c9dffb8bEric Anholt	 return false;
1791a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
17920044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick      iter_a.next();
17930044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick      iter_b.next();
1794a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1795a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1796a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return true;
1797a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1798a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1799a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1800fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
180192318a947958892497722772b03c643ebc943294Ian Romanickast_function::hir(exec_list *instructions,
180292318a947958892497722772b03c643ebc943294Ian Romanick		  struct _mesa_glsl_parse_state *state)
1803a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
180418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   ir_function *f = NULL;
180592318a947958892497722772b03c643ebc943294Ian Romanick   ir_function_signature *sig = NULL;
180692318a947958892497722772b03c643ebc943294Ian Romanick   exec_list hir_parameters;
1807a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1808a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
180992318a947958892497722772b03c643ebc943294Ian Romanick   /* The prototype part of a function does not generate anything in the IR
181092318a947958892497722772b03c643ebc943294Ian Romanick    * instruction stream.
181192318a947958892497722772b03c643ebc943294Ian Romanick    */
181292318a947958892497722772b03c643ebc943294Ian Romanick   (void) instructions;
181392318a947958892497722772b03c643ebc943294Ian Romanick
1814a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Convert the list of function parameters to HIR now so that they can be
1815a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * used below to compare this function's signature with previously seen
1816a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * signatures for functions with the same name.
1817a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
181845d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   ast_parameter_declarator::parameters_to_hir(& this->parameters,
181945d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					       is_definition,
182045d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					       & hir_parameters, state);
1821a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1822e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick   const char *return_type_name;
1823e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick   const glsl_type *return_type =
182492318a947958892497722772b03c643ebc943294Ian Romanick      this->return_type->specifier->glsl_type(& return_type_name, state);
1825e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick
1826e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick   assert(return_type != NULL);
1827e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick
1828a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Verify that this function's signature either doesn't match a previously
1829a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * seen signature for a function with the same name, or, if a match is found,
1830a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * that the previously seen signature does not have an associated definition.
1831a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
183292318a947958892497722772b03c643ebc943294Ian Romanick   const char *const name = identifier;
18333359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick   f = state->symbols->get_function(name);
1834a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (f != NULL) {
183595cd6cc195f4652378d7ecf614c6e1c568311a04Ian Romanick      foreach_iter(exec_list_iterator, iter, *f) {
183692318a947958892497722772b03c643ebc943294Ian Romanick	 sig = (struct ir_function_signature *) iter.get();
1837a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1838a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 /* Compare the parameter list of the function being defined to the
1839a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  * existing function.  If the parameter lists match, then the return
1840a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  * type must also match and the existing function must not have a
1841a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  * definition.
1842a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  */
184392318a947958892497722772b03c643ebc943294Ian Romanick	 if (parameter_lists_match(& hir_parameters, & sig->parameters)) {
18441e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt	    exec_list_iterator iter_a = hir_parameters.iterator();
18451e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt	    exec_list_iterator iter_b = sig->parameters.iterator();
18461e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt
18471e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt	    /* check that the qualifiers match. */
18481e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt	    while (iter_a.has_next()) {
18491e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt	       ir_variable *a = (ir_variable *)iter_a.get();
18501e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt	       ir_variable *b = (ir_variable *)iter_b.get();
18511e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt
18521e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt	       if (a->read_only != b->read_only ||
18531e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt		   a->interpolation != b->interpolation ||
18541e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt		   a->centroid != b->centroid) {
18551e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt		  YYLTYPE loc = this->get_location();
18561e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt
18571e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt		  _mesa_glsl_error(& loc, state,
18581e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt				   "function `%s' parameter `%s' qualifiers "
18591e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt				   "don't match prototype",
18601e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt				   name, a->name);
18611e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt	       }
18621e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt
18631e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt	       iter_a.next();
18641e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt	       iter_b.next();
18651e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt	    }
18661e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt
1867a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    /* FINISHME: Compare return types. */
1868a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
186992318a947958892497722772b03c643ebc943294Ian Romanick	    if (is_definition && (sig->definition != NULL)) {
187018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick	       YYLTYPE loc = this->get_location();
1871a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
18723359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	       _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
187392318a947958892497722772b03c643ebc943294Ian Romanick	       sig = NULL;
1874a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	       break;
1875a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    }
1876a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
1877a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
187892318a947958892497722772b03c643ebc943294Ian Romanick	 sig = NULL;
1879a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1880a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
18813359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick   } else if (state->symbols->name_declared_this_scope(name)) {
18823359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      /* This function name shadows a non-function use of the same name.
18833359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick       */
18843359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      YYLTYPE loc = this->get_location();
18853359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick
18863359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      _mesa_glsl_error(& loc, state, "function name `%s' conflicts with "
18873359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick		       "non-function", name);
188892318a947958892497722772b03c643ebc943294Ian Romanick      sig = NULL;
1889a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
1890882dad75408fc4071a9dd700309f9e54f6ad2650Ian Romanick      f = new ir_function(name);
18918bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      state->symbols->add_function(f->name, f);
1892a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1893a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1894ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   /* Verify the return type of main() */
1895ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   if (strcmp(name, "main") == 0) {
189625711a85c22bed305c9b52b89feb9c600d1892dfIan Romanick      if (! return_type->is_void()) {
1897ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt	 YYLTYPE loc = this->get_location();
1898ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt
1899ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt	 _mesa_glsl_error(& loc, state, "main() must return void");
1900ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt      }
1901174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt
190292318a947958892497722772b03c643ebc943294Ian Romanick      if (!hir_parameters.is_empty()) {
1903174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt	 YYLTYPE loc = this->get_location();
1904174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt
1905174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt	 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
1906174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt      }
1907ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   }
1908a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1909a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Finish storing the information about this new function in its signature.
1910a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
191192318a947958892497722772b03c643ebc943294Ian Romanick   if (sig == NULL) {
191292318a947958892497722772b03c643ebc943294Ian Romanick      sig = new ir_function_signature(return_type);
191392318a947958892497722772b03c643ebc943294Ian Romanick      f->add_signature(sig);
191492318a947958892497722772b03c643ebc943294Ian Romanick   } else if (is_definition) {
1915a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Destroy all of the previous parameter information.  The previous
1916a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * parameter information comes from the function prototype, and it can
1917a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * either include invalid parameter names or may not have names at all.
1918a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
191992318a947958892497722772b03c643ebc943294Ian Romanick      foreach_iter(exec_list_iterator, iter, sig->parameters) {
192044e1dfa2df4de3e2de963f0505cdadade6fe8180Kenneth Graunke	 assert(((ir_instruction *) iter.get())->as_variable() != NULL);
1921a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
19220044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick	 iter.remove();
19230044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick	 delete iter.get();
1924a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1925a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1926a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
192792318a947958892497722772b03c643ebc943294Ian Romanick   hir_parameters.move_nodes_to(& sig->parameters);
192892318a947958892497722772b03c643ebc943294Ian Romanick   signature = sig;
192992318a947958892497722772b03c643ebc943294Ian Romanick
193092318a947958892497722772b03c643ebc943294Ian Romanick   /* Function declarations (prototypes) do not have r-values.
193192318a947958892497722772b03c643ebc943294Ian Romanick    */
193292318a947958892497722772b03c643ebc943294Ian Romanick   return NULL;
193392318a947958892497722772b03c643ebc943294Ian Romanick}
193492318a947958892497722772b03c643ebc943294Ian Romanick
193592318a947958892497722772b03c643ebc943294Ian Romanick
193692318a947958892497722772b03c643ebc943294Ian Romanickir_rvalue *
193792318a947958892497722772b03c643ebc943294Ian Romanickast_function_definition::hir(exec_list *instructions,
193892318a947958892497722772b03c643ebc943294Ian Romanick			     struct _mesa_glsl_parse_state *state)
193992318a947958892497722772b03c643ebc943294Ian Romanick{
194092318a947958892497722772b03c643ebc943294Ian Romanick   prototype->is_definition = true;
194192318a947958892497722772b03c643ebc943294Ian Romanick   prototype->hir(instructions, state);
1942e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick
194392318a947958892497722772b03c643ebc943294Ian Romanick   ir_function_signature *signature = prototype->signature;
1944a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
194541ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   assert(state->current_function == NULL);
194641ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = signature;
194741ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick
1948894ea972a4defdaafeaa3a248c113b06c7ae0c7eEric Anholt   ir_label *label = new ir_label(signature->function_name(), signature);
1949a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (signature->definition == NULL) {
1950a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      signature->definition = label;
1951a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
19520044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick   instructions->push_tail(label);
1953a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1954e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick   /* Duplicate parameters declared in the prototype as concrete variables.
1955e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick    * Add these to the symbol table.
1956a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
19578bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick   state->symbols->push_scope();
1958e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick   foreach_iter(exec_list_iterator, iter, signature->parameters) {
1959fbc7c0b8f2e161bce1c048c63d2d5cfcdeb096f1Eric Anholt      ir_variable *const var = ((ir_instruction *) iter.get())->as_variable();
1960e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick
1961fbc7c0b8f2e161bce1c048c63d2d5cfcdeb096f1Eric Anholt      assert(var != NULL);
1962a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
19633359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      /* The only way a parameter would "exist" is if two parameters have
19643359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick       * the same name.
19653359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick       */
19663359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      if (state->symbols->name_declared_this_scope(var->name)) {
19673359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 YYLTYPE loc = this->get_location();
19683359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick
19693359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
19703359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      } else {
19713359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 state->symbols->add_variable(var->name, var);
19723359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      }
1973a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1974a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1975a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Convert the body of the function to HIR, and append the resulting
1976a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * instructions to the list that currently consists of the function label
1977a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * and the function parameters.
1978a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1979894ea972a4defdaafeaa3a248c113b06c7ae0c7eEric Anholt   this->body->hir(&signature->body, state);
1980a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
19818bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick   state->symbols->pop_scope();
1982a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
198341ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   assert(state->current_function == signature);
198441ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = NULL;
1985a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1986a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Function definitions do not have r-values.
1987a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1988a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1989a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
199016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
199116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
1992fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
199316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanickast_jump_statement::hir(exec_list *instructions,
199416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick			struct _mesa_glsl_parse_state *state)
199516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick{
199616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
1997c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   switch (mode) {
1998c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_return: {
199916a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      ir_return *inst;
2000aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt      assert(state->current_function);
200116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
200216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      if (opt_return_value) {
2003ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	 if (state->current_function->return_type->base_type ==
2004ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	     GLSL_TYPE_VOID) {
2005ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	    YYLTYPE loc = this->get_location();
2006ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt
2007ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	    _mesa_glsl_error(& loc, state,
2008ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt			     "`return` with a value, in function `%s' "
2009ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt			     "returning void",
2010ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt			     state->current_function->definition->label);
2011ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	 }
201216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
201316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 ir_expression *const ret = (ir_expression *)
201416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	    opt_return_value->hir(instructions, state);
201516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 assert(ret != NULL);
201616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
201716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 /* FINISHME: Make sure the type of the return value matches the return
201816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	  * FINISHME: type of the enclosing function.
201916a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	  */
202016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
202116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 inst = new ir_return(ret);
202216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      } else {
2023aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	 if (state->current_function->return_type->base_type !=
2024aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	     GLSL_TYPE_VOID) {
2025aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	    YYLTYPE loc = this->get_location();
2026aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt
2027aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	    _mesa_glsl_error(& loc, state,
2028aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt			     "`return' with no value, in function %s returning "
2029aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt			     "non-void",
2030aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt			     state->current_function->definition->label);
2031aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	 }
203216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 inst = new ir_return;
203316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      }
203416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
203516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      instructions->push_tail(inst);
2036c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick      break;
203716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   }
203816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
2039c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_discard:
2040b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt      /* FINISHME: discard support */
2041b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt      if (state->target != fragment_shader) {
2042b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt	 YYLTYPE loc = this->get_location();
2043b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt
2044b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt	 _mesa_glsl_error(& loc, state,
2045b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt			  "`discard' may only appear in a fragment shader");
2046b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt      }
2047c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick      break;
2048c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick
2049c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_break:
2050c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_continue:
20514cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      /* FINISHME: Handle switch-statements.  They cannot contain 'continue',
20524cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick       * FINISHME: and they use a different IR instruction for 'break'.
20534cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick       */
20544cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      /* FINISHME: Correctly handle the nesting.  If a switch-statement is
20554cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick       * FINISHME: inside a loop, a 'continue' is valid and will bind to the
20564cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick       * FINISHME: loop.
20574cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick       */
20584cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      if (state->loop_or_switch_nesting == NULL) {
20594cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 YYLTYPE loc = this->get_location();
20604cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
20614cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 _mesa_glsl_error(& loc, state,
20624cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick			  "`%s' may only appear in a loop",
20634cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick			  (mode == ast_break) ? "break" : "continue");
20644cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      } else {
20654cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 ir_loop *const loop = state->loop_or_switch_nesting->as_loop();
20664cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
20674cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 if (loop != NULL) {
20684cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	    ir_loop_jump *const jump =
20694cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	       new ir_loop_jump(loop,
20704cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick				(mode == ast_break)
20714cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick				? ir_loop_jump::jump_break
20724cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick				: ir_loop_jump::jump_continue);
20734cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	    instructions->push_tail(jump);
20744cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 }
20754cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      }
20764cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
2077c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick      break;
2078b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt   }
2079b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt
208016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   /* Jump instructions do not have r-values.
208116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick    */
208216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   return NULL;
208316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick}
20843c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
20853c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
20863c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanickir_rvalue *
20873c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanickast_selection_statement::hir(exec_list *instructions,
20883c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick			     struct _mesa_glsl_parse_state *state)
20893c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick{
20903c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   ir_rvalue *const condition = this->condition->hir(instructions, state);
20913c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
20923c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
20933c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *
20943c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    "Any expression whose type evaluates to a Boolean can be used as the
20953c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    conditional expression bool-expression. Vector types are not accepted
20963c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    as the expression to if."
20973c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *
20983c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    * The checks are separated so that higher quality diagnostics can be
20993c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    * generated for cases where both rules are violated.
21003c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    */
21013c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
21023c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      YYLTYPE loc = this->condition->get_location();
21033c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
21043c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
21053c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick		       "boolean");
21063c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   }
21073c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
21083c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   ir_if *const stmt = new ir_if(condition);
21093c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
21103c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   if (then_statement != NULL) {
21113c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      ast_node *node = (ast_node *) then_statement;
21123c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      do {
21133c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick	 node->hir(& stmt->then_instructions, state);
21143c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick	 node = (ast_node *) node->next;
21153c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      } while (node != then_statement);
21163c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   }
21173c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
21183c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   if (else_statement != NULL) {
21193c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      ast_node *node = (ast_node *) else_statement;
21203c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      do {
21213c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick	 node->hir(& stmt->else_instructions, state);
21223c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick	 node = (ast_node *) node->next;
21233c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      } while (node != else_statement);
21243c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   }
21253c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
21263c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   instructions->push_tail(stmt);
21273c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
21283c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   /* if-statements do not have r-values.
21293c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    */
21303c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   return NULL;
21313c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick}
21329e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
21339e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
21348c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickvoid
21358c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickast_iteration_statement::condition_to_hir(ir_loop *stmt,
21368c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick					  struct _mesa_glsl_parse_state *state)
21379e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick{
21389e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   if (condition != NULL) {
21399e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      ir_rvalue *const cond =
21409e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 condition->hir(& stmt->body_instructions, state);
21419e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
21429e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      if ((cond == NULL)
21439e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	  || !cond->type->is_boolean() || !cond->type->is_scalar()) {
21449e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 YYLTYPE loc = condition->get_location();
21459e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
21469e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 _mesa_glsl_error(& loc, state,
21479e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick			  "loop condition must be scalar boolean");
21489e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      } else {
21499e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 /* As the first code in the loop body, generate a block that looks
21509e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	  * like 'if (!condition) break;' as the loop termination condition.
21519e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	  */
21529e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 ir_rvalue *const not_cond =
21539e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	    new ir_expression(ir_unop_logic_not, glsl_type::bool_type, cond,
21549e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick			      NULL);
21559e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
21569e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 ir_if *const if_stmt = new ir_if(not_cond);
21579e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
21589e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 ir_jump *const break_stmt =
21599e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	    new ir_loop_jump(stmt, ir_loop_jump::jump_break);
21609e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
21619e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 if_stmt->then_instructions.push_tail(break_stmt);
21629e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 stmt->body_instructions.push_tail(if_stmt);
21639e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      }
21649e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   }
21658c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick}
21668c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
21678c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
21688c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickir_rvalue *
21698c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickast_iteration_statement::hir(exec_list *instructions,
21708c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick			     struct _mesa_glsl_parse_state *state)
21718c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick{
21728c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   /* For loops start a new scope, but while and do-while loops do not.
21738c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick    */
21748c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   if (mode == ast_for)
21758c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick      state->symbols->push_scope();
21768c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
21778c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   if (init_statement != NULL)
21788c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick      init_statement->hir(instructions, state);
21798c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
21808c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   ir_loop *const stmt = new ir_loop();
21818c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   instructions->push_tail(stmt);
21828c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
21838c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   /* Track the current loop and / or switch-statement nesting.
21848c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick    */
21858c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   ir_instruction *const nesting = state->loop_or_switch_nesting;
21868c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   state->loop_or_switch_nesting = stmt;
21878c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
21888c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   if (mode != ast_do_while)
21898c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick      condition_to_hir(stmt, state);
21909e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
21919e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   if (body != NULL) {
21929e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      ast_node *node = (ast_node *) body;
21939e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      do {
21949e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 node->hir(& stmt->body_instructions, state);
21959e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 node = (ast_node *) node->next;
21969e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      } while (node != body);
21979e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   }
21989e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
21999e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   if (rest_expression != NULL)
22009e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      rest_expression->hir(& stmt->body_instructions, state);
22019e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
22028c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   if (mode == ast_do_while)
22038c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick      condition_to_hir(stmt, state);
22048c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
22059e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   if (mode == ast_for)
22069e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      state->symbols->pop_scope();
22079e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
2208e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick   /* Restore previous nesting before returning.
2209e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick    */
2210e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick   state->loop_or_switch_nesting = nesting;
2211e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick
22129e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   /* Loops do not have r-values.
22139e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick    */
22149e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   return NULL;
22159e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick}
2216