ast_to_hir.cpp revision ac04c257e31fe012dac750bcf5bf3134ba07ebdc
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 */
51ac95f2f8c88d39aaa878f61172d9748af13e2c80Eric Anholt
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{
62adfb0cd7401251bef0c854ac945fce78f0ed11dbIan Romanick   _mesa_glsl_initialize_variables(instructions, state);
63a4e92c4b26578614d76ce71b53194ea5c0f58d6cIan Romanick   _mesa_glsl_initialize_constructors(instructions, state);
64c22c40015db32b68b33c4944b9d94bf499135ec5Eric Anholt   _mesa_glsl_initialize_functions(instructions, state);
65adfb0cd7401251bef0c854ac945fce78f0ed11dbIan Romanick
6641ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = NULL;
6741ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick
682b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_node, ast, link, & state->translation_unit)
69304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      ast->hir(instructions, state);
70d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick}
71d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick
72d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick
730104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick/**
740104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * If a conversion is available, convert one operand to a different type
750104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick *
760104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * The \c from \c ir_rvalue is converted "in place".
770104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick *
780104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param to     Type that the operand it to be converted to
790104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param from   Operand that is being converted
800104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param state  GLSL compiler state
810104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick *
820104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \return
830104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * If a conversion is possible (or unnecessary), \c true is returned.
840104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * Otherwise \c false is returned.
850104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick */
860104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanickstatic bool
87bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickapply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
880104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick			  struct _mesa_glsl_parse_state *state)
890104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick{
901660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   void *ctx = talloc_parent(state);
91bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   if (to->base_type == from->type->base_type)
920104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return true;
930104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
940104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   /* This conversion was added in GLSL 1.20.  If the compilation mode is
950104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    * GLSL 1.10, the conversion is skipped.
960104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    */
970104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   if (state->language_version < 120)
980104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return false;
990104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1000104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
1010104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *
1020104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    "There are no implicit array or structure conversions. For
1030104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    example, an array of int cannot be implicitly converted to an
1040104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    array of float. There are no implicit conversions between
1050104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    signed and unsigned integers."
1060104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    */
1070104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   /* FINISHME: The above comment is partially a lie.  There is int/uint
1080104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    * FINISHME: conversion for immediate constants.
1090104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    */
110bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   if (!to->is_float() || !from->type->is_numeric())
1110104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return false;
1120104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
113bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   switch (from->type->base_type) {
1140104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   case GLSL_TYPE_INT:
1151660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      from = new(ctx) ir_expression(ir_unop_i2f, to, from, NULL);
1160104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      break;
1170104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   case GLSL_TYPE_UINT:
1181660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      from = new(ctx) ir_expression(ir_unop_u2f, to, from, NULL);
1190104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      break;
1200104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   case GLSL_TYPE_BOOL:
1211660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      from = new(ctx) ir_expression(ir_unop_b2f, to, from, NULL);
122dc58b3f8ccd817fdee390a3df5b8e0fb29d5397cEric Anholt      break;
1230104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   default:
1240104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      assert(0);
1250104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   }
1260104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1270104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   return true;
1280104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick}
1290104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1300104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
131a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
132bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickarithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
133a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick		       bool multiply,
134a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
135a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
136336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *type_a = value_a->type;
137336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *type_b = value_b->type;
1380104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
139a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
140a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
141a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The arithmetic binary operators add (+), subtract (-),
142a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    multiply (*), and divide (/) operate on integer and
143a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    floating-point scalars, vectors, and matrices."
144a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
14560b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   if (!type_a->is_numeric() || !type_b->is_numeric()) {
146a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state,
147a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "Operands to arithmetic operators must be numeric");
1480471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
149a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
150a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
151a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
152a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "If one operand is floating-point based and the other is
153a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    not, then the conversions from Section 4.1.10 "Implicit
154a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    Conversions" are applied to the non-floating-point-based operand."
155a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1560104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   if (!apply_implicit_conversion(type_a, value_b, state)
1570104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick       && !apply_implicit_conversion(type_b, value_a, state)) {
158a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state,
159a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "Could not implicitly convert operands to "
160a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "arithmetic operator");
1610104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return glsl_type::error_type;
162a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
163336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   type_a = value_a->type;
164336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   type_b = value_b->type;
165336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt
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{
368336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *type_a = value_a->type;
369336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *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   }
397336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   type_a = value_a->type;
398336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   type_b = value_b->type;
399a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
40065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   if (type_a->base_type != type_b->base_type) {
40165e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state, "base type mismatch");
4020471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
40365e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   }
404a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
405a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The result is scalar Boolean."
406a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
4070471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::bool_type;
408a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
409a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
410a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
4110bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick/**
4120bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Validates that a value can be assigned to a location with a specified type
4130bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
4140bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Validates that \c rhs can be assigned to some location.  If the types are
4150bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * not an exact match but an automatic conversion is possible, \c rhs will be
4160bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * converted.
4170bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
4180bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \return
4190bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
4200bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Otherwise the actual RHS to be assigned will be returned.  This may be
4210bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \c rhs, or it may be \c rhs after some type conversion.
4220bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
4230bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \note
4240bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * In addition to being used for assignments, this function is used to
4250bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * type-check return values.
4260bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick */
427fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
428336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholtvalidate_assignment(struct _mesa_glsl_parse_state *state,
429336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt		    const glsl_type *lhs_type, ir_rvalue *rhs)
4300bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick{
431336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *rhs_type = rhs->type;
4320bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
4330bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   /* If there is already some error in the RHS, just return it.  Anything
4340bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    * else will lead to an avalanche of error message back to the user.
4350bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    */
4360bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   if (rhs_type->is_error())
4370bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick      return rhs;
4380bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
4390bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   /* If the types are identical, the assignment can trivially proceed.
4400bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    */
4410bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   if (rhs_type == lhs_type)
4420bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick      return rhs;
4430bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
4440157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick   /* If the array element types are the same and the size of the LHS is zero,
4450157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    * the assignment is okay.
4460157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    *
4470157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
4480157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    * is handled by ir_dereference::is_lvalue.
4490157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    */
4500157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick   if (lhs_type->is_array() && rhs->type->is_array()
4510157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       && (lhs_type->element_type() == rhs->type->element_type())
4520157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       && (lhs_type->array_size() == 0)) {
4530157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      return rhs;
4540157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick   }
4550157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
456336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   /* Check for implicit conversion in GLSL 1.20 */
457336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   if (apply_implicit_conversion(lhs_type, rhs, state)) {
458336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt      rhs_type = rhs->type;
459336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt      if (rhs_type == lhs_type)
460336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt	 return rhs;
461336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   }
462336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt
4630bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   return NULL;
4640bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick}
4650bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
46610a685218610e737e23d2d8a243ed6ff6613becdEric Anholtir_rvalue *
46710a685218610e737e23d2d8a243ed6ff6613becdEric Anholtdo_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
46810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	      ir_rvalue *lhs, ir_rvalue *rhs,
46910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	      YYLTYPE lhs_loc)
47010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt{
4711660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   void *ctx = talloc_parent(state);
47210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
47310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
47410a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   if (!error_emitted) {
47510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
47610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      if (!lhs->is_lvalue()) {
47710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	 _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
47810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	 error_emitted = true;
47910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      }
48010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   }
48110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
482336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   ir_rvalue *new_rhs = validate_assignment(state, lhs->type, rhs);
48310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   if (new_rhs == NULL) {
48410a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      _mesa_glsl_error(& lhs_loc, state, "type mismatch");
48510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   } else {
48610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      rhs = new_rhs;
4870157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
4880157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      /* If the LHS array was not declared with a size, it takes it size from
4890157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       * the RHS.  If the LHS is an l-value and a whole array, it must be a
4900157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       * dereference of a variable.  Any other case would require that the LHS
4910157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       * is either not an l-value or not a whole array.
4920157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       */
4930157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      if (lhs->type->array_size() == 0) {
4940157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 ir_dereference *const d = lhs->as_dereference();
4950157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
4960157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 assert(d != NULL);
4970157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
49836ea28646c666ac2af9b43c47e65f9f53ffcc390Ian Romanick	 ir_variable *const var = d->variable_referenced();
4990157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
5000157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 assert(var != NULL);
5010157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
50263f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	 if (var->max_array_access >= unsigned(rhs->type->array_size())) {
50363f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	    /* FINISHME: This should actually log the location of the RHS. */
50463f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	    _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
50563f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick			     "previous access",
50663f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick			     var->max_array_access);
50763f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	 }
50863f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick
50912c411504ca86341f8b96c349c15413ee198cc71Carl Worth	 var->type = glsl_type::get_array_instance(state,
51012c411504ca86341f8b96c349c15413ee198cc71Carl Worth						   lhs->type->element_type(),
5110157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick						   rhs->type->array_size());
5120157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      }
51310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   }
51410a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
5152731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt   /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
5162731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * but not post_inc) need the converted assigned value as an rvalue
5172731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * to handle things like:
5182731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    *
5192731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * i = j += 1;
5202731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    *
5212731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * So we always just store the computed value being assigned to a
5222731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * temporary and return a deref of that temporary.  If the rvalue
5232731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * ends up not being used, the temp will get copy-propagated out.
5242731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    */
5251660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp");
526e33c10328caec29616a5433b1d1df9088f3a84dfEric Anholt   ir_dereference_variable *deref_var = new(ctx) ir_dereference_variable(var);
527ae805922b7e3cdaf3aee26c3b799fe3608669bbaEric Anholt   instructions->push_tail(var);
528e33c10328caec29616a5433b1d1df9088f3a84dfEric Anholt   instructions->push_tail(new(ctx) ir_assignment(deref_var,
5291660a2954797e056caba319c5d6c70b0d4be22feCarl Worth						  rhs,
5301660a2954797e056caba319c5d6c70b0d4be22feCarl Worth						  NULL));
531e33c10328caec29616a5433b1d1df9088f3a84dfEric Anholt   deref_var = new(ctx) ir_dereference_variable(var);
5322731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt
5331660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   instructions->push_tail(new(ctx) ir_assignment(lhs,
534e33c10328caec29616a5433b1d1df9088f3a84dfEric Anholt						  deref_var,
5351660a2954797e056caba319c5d6c70b0d4be22feCarl Worth						  NULL));
53610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
5371660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   return new(ctx) ir_dereference_variable(var);
53810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt}
5390bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
5405185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick
5415185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick/**
5425185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick * Generate a new temporary and add its declaration to the instruction stream
5435185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick */
5445185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanickstatic ir_variable *
5455185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanickgenerate_temporary(const glsl_type *type, exec_list *instructions,
5465185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick		   struct _mesa_glsl_parse_state *state)
5475185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick{
5481660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   void *ctx = talloc_parent(state);
5495185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick   char *name = (char *) malloc(sizeof(char) * 13);
5505185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick
5515185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick   snprintf(name, 13, "tmp_%08X", state->temp_index);
5525185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick   state->temp_index++;
5535185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick
5541660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   ir_variable *const var = new(ctx) ir_variable(type, name);
5555185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick   instructions->push_tail(var);
5565185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick
5575185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick   return var;
5585185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick}
5595185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick
5605185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick
561de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholtstatic ir_rvalue *
562959a9ecdd8fbc3375e4149f2b44d253622ff12eeEric Anholtget_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue)
563de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt{
5641660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   void *ctx = talloc_parent(lvalue);
565de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   ir_variable *var;
566de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
567de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   /* FINISHME: Give unique names to the temporaries. */
5681660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp");
569de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   var->mode = ir_var_auto;
570de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
5711660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
5721660a2954797e056caba319c5d6c70b0d4be22feCarl Worth						  lvalue, NULL));
573de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
574de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   /* Once we've created this temporary, mark it read only so it's no
575de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt    * longer considered an lvalue.
576de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt    */
577de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   var->read_only = true;
578de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
5791660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   return new(ctx) ir_dereference_variable(var);
580de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt}
581de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
582de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
583fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
5840044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_node::hir(exec_list *instructions,
58518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick	      struct _mesa_glsl_parse_state *state)
58618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick{
58718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   (void) instructions;
58818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   (void) state;
58918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
59018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   return NULL;
59118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick}
59218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
59318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
594fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
5950044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_expression::hir(exec_list *instructions,
59618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick		    struct _mesa_glsl_parse_state *state)
597a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
5981660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   void *ctx = talloc_parent(state);
599a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   static const int operations[AST_NUM_OPERATORS] = {
600a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_assign doesn't convert to ir_expression. */
601a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_plus doesn't convert to ir_expression. */
602a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_neg,
603a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_add,
604a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_sub,
605a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mul,
606a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_div,
607a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mod,
608a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lshift,
609a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_rshift,
610a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_less,
611a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_greater,
612a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lequal,
613a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_gequal,
614a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_equal,
615a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_nequal,
616a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_and,
617a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_xor,
618a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_or,
619a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_bit_not,
620a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_and,
621a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_xor,
622a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_or,
623a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_logic_not,
624a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
625a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Note: The following block of expression types actually convert
626a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * to multiple IR instructions.
627a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
628a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mul,     /* ast_mul_assign */
629a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_div,     /* ast_div_assign */
630a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mod,     /* ast_mod_assign */
631a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_add,     /* ast_add_assign */
632a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_sub,     /* ast_sub_assign */
633a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lshift,  /* ast_ls_assign */
634a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_rshift,  /* ast_rs_assign */
635a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_and, /* ast_and_assign */
636a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_xor, /* ast_xor_assign */
637a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_or,  /* ast_or_assign */
638a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
639a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_conditional doesn't convert to ir_expression. */
640de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_add,     /* ast_pre_inc. */
641de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_sub,     /* ast_pre_dec. */
642de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_add,     /* ast_post_inc. */
643de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_sub,     /* ast_post_dec. */
644a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_field_selection doesn't conv to ir_expression. */
645a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_array_index doesn't convert to ir_expression. */
646a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_function_call doesn't conv to ir_expression. */
647a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_identifier doesn't convert to ir_expression. */
648a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_int_constant doesn't convert to ir_expression. */
649a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_uint_constant doesn't conv to ir_expression. */
650a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_float_constant doesn't conv to ir_expression. */
651a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_bool_constant doesn't conv to ir_expression. */
652a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_sequence doesn't convert to ir_expression. */
653a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   };
654fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke   ir_rvalue *result = NULL;
655fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke   ir_rvalue *op[2];
6560471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   const struct glsl_type *type = glsl_type::error_type;
657a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   bool error_emitted = false;
658a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   YYLTYPE loc;
659a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
66018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   loc = this->get_location();
661a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
66218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   switch (this->oper) {
6636652af36fe8994b1621d882fcc230d320908a2a3Ian Romanick   case ast_assign: {
66418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
66518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
666a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
66710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      result = do_assignment(instructions, state, op[0], op[1],
66810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt			     this->subexpressions[0]->get_location());
66910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      error_emitted = result->type->is_error();
67010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      type = result->type;
671a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
6726652af36fe8994b1621d882fcc230d320908a2a3Ian Romanick   }
673a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
674a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_plus:
67518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
676a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
677a43817a483a8c4a480ef4e6dfda2cef899300eb0Ian Romanick      error_emitted = op[0]->type->is_error();
678a43817a483a8c4a480ef4e6dfda2cef899300eb0Ian Romanick      if (type->is_error())
679a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 op[0]->type = type;
680a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
681a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      result = op[0];
682a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
683a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
684a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_neg:
68518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
686a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
68765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = unary_arithmetic_result_type(op[0]->type, state, & loc);
688a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
68965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
690a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
6911660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
6921660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], NULL);
693a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
694a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
695a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_add:
696a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sub:
697a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mul:
698a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_div:
69918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
70018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
701a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
702bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      type = arithmetic_result_type(op[0], op[1],
70318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick				    (this->oper == ast_mul),
704a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt				    state, & loc);
705a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      error_emitted = type->is_error();
706a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
7071660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
7081660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
709a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
710a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
711a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mod:
71218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
71318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
714a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
71565e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
716a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
71718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      assert(operations[this->oper] == ir_binop_mod);
718a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
7191660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
7201660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
72165e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
722a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
723a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
724a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_lshift:
725a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_rshift:
726183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      _mesa_glsl_error(& loc, state, "FINISHME: implement bit-shift operators");
727183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      error_emitted = true;
728a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
729a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
730a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_less:
731a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_greater:
732a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_lequal:
733a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_gequal:
73418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
73518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
736a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
73765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = relational_result_type(op[0], op[1], state, & loc);
738a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
739a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* The relational operators must either generate an error or result
740a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * in a scalar boolean.  See page 57 of the GLSL 1.50 spec.
741a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
742a43817a483a8c4a480ef4e6dfda2cef899300eb0Ian Romanick      assert(type->is_error()
743a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	     || ((type->base_type == GLSL_TYPE_BOOL)
744cb36f8aaeeb09660843316270a781948f773d90bIan Romanick		 && type->is_scalar()));
745a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
7461660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
7471660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
74865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
749a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
750a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
751a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_nequal:
752a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_equal:
7536e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
7546e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
7556e659caaa946339a2de3890a8bed091ccb65102aIan Romanick
7566e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
7576e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *
7586e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    "The equality operators equal (==), and not equal (!=)
7596e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    operate on all types. They result in a scalar Boolean. If
7606e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    the operand types do not match, then there must be a
7616e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    conversion from Section 4.1.10 "Implicit Conversions"
7626e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    applied to one operand that can make them match, in which
7636e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    case this conversion is done."
7646e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       */
765bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      if ((!apply_implicit_conversion(op[0]->type, op[1], state)
766bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick	   && !apply_implicit_conversion(op[1]->type, op[0], state))
767212b0327b47033442842a7be3d7fb10e08e2bf66Ian Romanick	  || (op[0]->type != op[1]->type)) {
7686e659caaa946339a2de3890a8bed091ccb65102aIan Romanick	 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
7696e659caaa946339a2de3890a8bed091ccb65102aIan Romanick			  "type", (this->oper == ast_equal) ? "==" : "!=");
7706e659caaa946339a2de3890a8bed091ccb65102aIan Romanick	 error_emitted = true;
771a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick      } else if ((state->language_version <= 110)
772a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick		 && (op[0]->type->is_array() || op[1]->type->is_array())) {
773a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick	 _mesa_glsl_error(& loc, state, "array comparisons forbidden in "
774a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick			  "GLSL 1.10");
775a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick	 error_emitted = true;
7766e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      }
7776e659caaa946339a2de3890a8bed091ccb65102aIan Romanick
7781660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
7791660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
7806e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      type = glsl_type::bool_type;
7816e659caaa946339a2de3890a8bed091ccb65102aIan Romanick
7826e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      assert(result->type == glsl_type::bool_type);
783a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
784a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
785a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_and:
786a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_xor:
787a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_or:
788a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_not:
789183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      _mesa_glsl_error(& loc, state, "FINISHME: implement bit-wise operators");
790183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      error_emitted = true;
791a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
792a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
7934950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   case ast_logic_and: {
794b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
795b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
796b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
797b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt	 YYLTYPE loc = this->subexpressions[0]->get_location();
798b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
799b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt	 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
800b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt			  operator_string(this->oper));
801ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt	 error_emitted = true;
802b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      }
803b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
80444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      ir_constant *op0_const = op[0]->constant_expression_value();
80544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      if (op0_const) {
80644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 if (op0_const->value.b[0]) {
80744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    op[1] = this->subexpressions[1]->hir(instructions, state);
80844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt
80944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
81044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       YYLTYPE loc = this->subexpressions[1]->get_location();
81144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt
81244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       _mesa_glsl_error(& loc, state,
81344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt				"RHS of `%s' must be scalar boolean",
81444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt				operator_string(this->oper));
81544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       error_emitted = true;
81644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    }
81744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    result = op[1];
81844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 } else {
81944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    result = op0_const;
82044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 }
82144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 type = glsl_type::bool_type;
82244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      } else {
8231660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_if *const stmt = new(ctx) ir_if(op[0]);
82444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 instructions->push_tail(stmt);
8254950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
82644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 op[1] = this->subexpressions[1]->hir(&stmt->then_instructions, state);
8274950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
82844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
82944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    YYLTYPE loc = this->subexpressions[1]->get_location();
8304950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
83144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    _mesa_glsl_error(& loc, state,
83244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt			     "RHS of `%s' must be scalar boolean",
83344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt			     operator_string(this->oper));
83444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    error_emitted = true;
83544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 }
836b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
83744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_variable *const tmp = generate_temporary(glsl_type::bool_type,
83844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt						     instructions, state);
839b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
8401660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
84144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const then_assign =
8421660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(then_deref, op[1], NULL);
84344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->then_instructions.push_tail(then_assign);
8444950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
8451660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
84644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const else_assign =
8471660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false), NULL);
84844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->else_instructions.push_tail(else_assign);
8494950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
8501660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 result = new(ctx) ir_dereference_variable(tmp);
85144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 type = tmp->type;
85244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      }
8534950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      break;
8544950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   }
8554950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
8564950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   case ast_logic_or: {
8574950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
8584950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
8594950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
8604950a68bf22ede6f4f368c9783e5401816159574Eric Anholt	 YYLTYPE loc = this->subexpressions[0]->get_location();
8614950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
8624950a68bf22ede6f4f368c9783e5401816159574Eric Anholt	 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
8634950a68bf22ede6f4f368c9783e5401816159574Eric Anholt			  operator_string(this->oper));
8644950a68bf22ede6f4f368c9783e5401816159574Eric Anholt	 error_emitted = true;
8654950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      }
8664950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
86744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      ir_constant *op0_const = op[0]->constant_expression_value();
86844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      if (op0_const) {
86944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 if (op0_const->value.b[0]) {
87044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    result = op0_const;
87144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 } else {
87244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    op[1] = this->subexpressions[1]->hir(instructions, state);
87344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt
87444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
87544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       YYLTYPE loc = this->subexpressions[1]->get_location();
87644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt
87744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       _mesa_glsl_error(& loc, state,
87844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt				"RHS of `%s' must be scalar boolean",
87944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt				operator_string(this->oper));
88044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       error_emitted = true;
88144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    }
88244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    result = op[1];
88344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 }
88444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 type = glsl_type::bool_type;
88544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      } else {
8861660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_if *const stmt = new(ctx) ir_if(op[0]);
88744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 instructions->push_tail(stmt);
8884950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
88944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_variable *const tmp = generate_temporary(glsl_type::bool_type,
89044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt						     instructions, state);
8914950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
89244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 op[1] = this->subexpressions[1]->hir(&stmt->then_instructions, state);
8934950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
89444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
89544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    YYLTYPE loc = this->subexpressions[1]->get_location();
8964950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
89744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    _mesa_glsl_error(& loc, state, "RHS of `%s' must be scalar boolean",
89844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt			     operator_string(this->oper));
89944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    error_emitted = true;
90044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 }
9014950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9021660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
90344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const then_assign =
9041660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true), NULL);
90544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->then_instructions.push_tail(then_assign);
9064950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9071660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
90844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const else_assign =
9091660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(else_deref, op[1], NULL);
91044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->else_instructions.push_tail(else_assign);
9114950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9121660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 result = new(ctx) ir_dereference_variable(tmp);
91344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 type = tmp->type;
91444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      }
9154950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      break;
9164950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   }
9174950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9184950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   case ast_logic_xor:
9194950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
9204950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      op[1] = this->subexpressions[1]->hir(instructions, state);
9214950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9224950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9231660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
9241660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
925ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt      type = glsl_type::bool_type;
926a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
927a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
928a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt   case ast_logic_not:
929a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
930a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
931a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
932a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt	 YYLTYPE loc = this->subexpressions[0]->get_location();
933a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
934a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt	 _mesa_glsl_error(& loc, state,
935a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt			  "operand of `!' must be scalar boolean");
936ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt	 error_emitted = true;
937a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      }
938a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
9391660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
9401660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], NULL);
941ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt      type = glsl_type::bool_type;
942a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      break;
943a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
944a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mul_assign:
945a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_div_assign:
946a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_add_assign:
947a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sub_assign: {
94818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
94918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
950a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
951bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      type = arithmetic_result_type(op[0], op[1],
95218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick				    (this->oper == ast_mul_assign),
953a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt				    state, & loc);
954a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
9551660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
9561660a2954797e056caba319c5d6c70b0d4be22feCarl Worth						   op[0], op[1]);
957a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
9583e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      result = do_assignment(instructions, state,
9593e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt			     (ir_rvalue *)op[0]->clone(NULL), temp_rhs,
96010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt			     this->subexpressions[0]->get_location());
96110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      type = result->type;
96210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      error_emitted = (op[0]->type->is_error());
963a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
964a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* GLSL 1.10 does not allow array assignment.  However, we don't have to
965a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * explicitly test for this because none of the binary expression
966a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * operators allow array operands either.
967a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
968a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
969a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
970a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
971a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
97248a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt   case ast_mod_assign: {
97348a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
97448a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      op[1] = this->subexpressions[1]->hir(instructions, state);
97548a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
97665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
97748a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
97848a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      assert(operations[this->oper] == ir_binop_mod);
97948a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
98048a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      struct ir_rvalue *temp_rhs;
9811660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
9821660a2954797e056caba319c5d6c70b0d4be22feCarl Worth					op[0], op[1]);
98348a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
9843e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      result = do_assignment(instructions, state,
9853e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt			     (ir_rvalue *)op[0]->clone(NULL), temp_rhs,
98648a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt			     this->subexpressions[0]->get_location());
98748a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      type = result->type;
98865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
98948a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      break;
99048a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt   }
991a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
992a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_ls_assign:
993a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_rs_assign:
994183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      _mesa_glsl_error(& loc, state,
995183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt		       "FINISHME: implement bit-shift assignment operators");
996183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      error_emitted = true;
997251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
998a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
999a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_and_assign:
1000a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_xor_assign:
1001a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_or_assign:
1002183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      _mesa_glsl_error(& loc, state,
1003183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt		       "FINISHME: implement logic assignment operators");
1004183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      error_emitted = true;
1005251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
1006a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
100796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick   case ast_conditional: {
100896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
100996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
101096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
101196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *
101296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    "The ternary selection operator (?:). It operates on three
101396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    expressions (exp1 ? exp2 : exp3). This operator evaluates the
101496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    first expression, which must result in a scalar Boolean."
101596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
101696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
101796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 YYLTYPE loc = this->subexpressions[0]->get_location();
101896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
101996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 _mesa_glsl_error(& loc, state, "?: condition must be scalar boolean");
102096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 error_emitted = true;
102196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      }
102296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
102396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* The :? operator is implemented by generating an anonymous temporary
102496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * followed by an if-statement.  The last instruction in each branch of
102596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * the if-statement assigns a value to the anonymous temporary.  This
102696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * temporary is the r-value of the expression.
102796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
10280ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      exec_list then_instructions;
10290ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      exec_list else_instructions;
103096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
10310ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      op[1] = this->subexpressions[1]->hir(&then_instructions, state);
10320ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      op[2] = this->subexpressions[2]->hir(&else_instructions, state);
103396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
103496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
103596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *
103696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     "The second and third expressions can be any type, as
103796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     long their types match, or there is a conversion in
103896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     Section 4.1.10 "Implicit Conversions" that can be applied
103996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     to one of the expressions to make their types match. This
104096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     resulting matching type is the type of the entire
104196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     expression."
104296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
1043bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      if ((!apply_implicit_conversion(op[1]->type, op[2], state)
1044bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick	   && !apply_implicit_conversion(op[2]->type, op[1], state))
1045db9be2e7aa3a56e43b725ad7725fe6b424e4933eIan Romanick	  || (op[1]->type != op[2]->type)) {
104696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 YYLTYPE loc = this->subexpressions[1]->get_location();
104796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
104896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
104996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick			  "operator must have matching types.");
105096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 error_emitted = true;
10510ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick	 type = glsl_type::error_type;
1052db9be2e7aa3a56e43b725ad7725fe6b424e4933eIan Romanick      } else {
10530ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick	 type = op[1]->type;
105496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      }
105596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
10567825d3d15710fdfcfc503754862963aac8065480Ian Romanick      ir_constant *cond_val = op[0]->constant_expression_value();
10577825d3d15710fdfcfc503754862963aac8065480Ian Romanick      ir_constant *then_val = op[1]->constant_expression_value();
10587825d3d15710fdfcfc503754862963aac8065480Ian Romanick      ir_constant *else_val = op[2]->constant_expression_value();
10597825d3d15710fdfcfc503754862963aac8065480Ian Romanick
10607825d3d15710fdfcfc503754862963aac8065480Ian Romanick      if (then_instructions.is_empty()
10617825d3d15710fdfcfc503754862963aac8065480Ian Romanick	  && else_instructions.is_empty()
10627825d3d15710fdfcfc503754862963aac8065480Ian Romanick	  && (cond_val != NULL) && (then_val != NULL) && (else_val != NULL)) {
10637825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 result = (cond_val->value.b[0]) ? then_val : else_val;
10647825d3d15710fdfcfc503754862963aac8065480Ian Romanick      } else {
10657825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 ir_variable *const tmp = generate_temporary(type,
10667825d3d15710fdfcfc503754862963aac8065480Ian Romanick						     instructions, state);
10670ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
10681660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_if *const stmt = new(ctx) ir_if(op[0]);
10697825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 instructions->push_tail(stmt);
10700ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
10717825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 then_instructions.move_nodes_to(& stmt->then_instructions);
10721660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const then_deref =
10731660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_dereference_variable(tmp);
10747825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 ir_assignment *const then_assign =
10751660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(then_deref, op[1], NULL);
10767825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 stmt->then_instructions.push_tail(then_assign);
10770ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
10787825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 else_instructions.move_nodes_to(& stmt->else_instructions);
10791660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const else_deref =
10801660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_dereference_variable(tmp);
10817825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 ir_assignment *const else_assign =
10821660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(else_deref, op[2], NULL);
10837825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 stmt->else_instructions.push_tail(else_assign);
10840ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
10851660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 result = new(ctx) ir_dereference_variable(tmp);
10867825d3d15710fdfcfc503754862963aac8065480Ian Romanick      }
1087251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
108896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick   }
1089a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1090a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_pre_inc:
109176ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt   case ast_pre_dec: {
109276ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
109376ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
10941660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 op[1] = new(ctx) ir_constant(1.0f);
109576ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      else
10961660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 op[1] = new(ctx) ir_constant(1);
109776ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
1098a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      type = arithmetic_result_type(op[0], op[1], false, state, & loc);
109976ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
110076ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      struct ir_rvalue *temp_rhs;
11011660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
11021660a2954797e056caba319c5d6c70b0d4be22feCarl Worth					op[0], op[1]);
110376ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
11043e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      result = do_assignment(instructions, state,
11053e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt			     (ir_rvalue *)op[0]->clone(NULL), temp_rhs,
110676ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt			     this->subexpressions[0]->get_location());
110776ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      type = result->type;
110876ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      error_emitted = op[0]->type->is_error();
110976ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      break;
111076ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt   }
1111a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1112a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_post_inc:
1113de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   case ast_post_dec: {
1114de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
1115de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
11161660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 op[1] = new(ctx) ir_constant(1.0f);
1117de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      else
11181660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 op[1] = new(ctx) ir_constant(1);
1119de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1120de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1121de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1122a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1123de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1124de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      struct ir_rvalue *temp_rhs;
11251660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
11261660a2954797e056caba319c5d6c70b0d4be22feCarl Worth					op[0], op[1]);
1127de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1128de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      /* Get a temporary of a copy of the lvalue before it's modified.
1129de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt       * This may get thrown away later.
1130de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt       */
1131959a9ecdd8fbc3375e4149f2b44d253622ff12eeEric Anholt      result = get_lvalue_copy(instructions, (ir_rvalue *)op[0]->clone(NULL));
1132de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
11333e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      (void)do_assignment(instructions, state,
11343e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt			  (ir_rvalue *)op[0]->clone(NULL), temp_rhs,
1135de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt			  this->subexpressions[0]->get_location());
1136de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1137de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      type = result->type;
1138de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      error_emitted = op[0]->type->is_error();
1139a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1140de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   }
1141a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1142a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_field_selection:
114318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = _mesa_ast_field_selection_to_hir(this, instructions, state);
1144a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = result->type;
1145a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1146a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
114727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick   case ast_array_index: {
114827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      YYLTYPE index_loc = subexpressions[1]->get_location();
114927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
115027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      op[0] = subexpressions[0]->hir(instructions, state);
115127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      op[1] = subexpressions[1]->hir(instructions, state);
115227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
115327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
115427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
1155a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick      ir_rvalue *const array = op[0];
1156b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
11571660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_dereference_array(op[0], op[1]);
1158b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
1159b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      /* Do not use op[0] after this point.  Use array.
1160b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick       */
1161b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      op[0] = NULL;
1162b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
116327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
116427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (error_emitted)
116527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 break;
116627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
116763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick      if (!array->type->is_array()
116863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	  && !array->type->is_matrix()
116963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	  && !array->type->is_vector()) {
117027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
117163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			  "cannot dereference non-array / non-matrix / "
117263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			  "non-vector");
117327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
117427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
117527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
117627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (!op[1]->type->is_integer()) {
117727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
117827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick			  "array index must be integer type");
117927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
118027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      } else if (!op[1]->type->is_scalar()) {
118127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
118227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick			  "array index must be scalar");
118327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
118427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
118527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
118627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      /* If the array index is a constant expression and the array has a
118727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * declared size, ensure that the access is in-bounds.  If the array
118827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * index is not a constant expression, ensure that the array has a
118927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * declared size.
119027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       */
119127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      ir_constant *const const_index = op[1]->constant_expression_value();
119227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (const_index != NULL) {
119327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 const int idx = const_index->value.i[0];
119463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 const char *type_name;
119563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 unsigned bound = 0;
119663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick
119763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (array->type->is_matrix()) {
119863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    type_name = "matrix";
119963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else if (array->type->is_vector()) {
120063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    type_name = "vector";
120163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else {
120263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    type_name = "array";
120363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 }
120427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
120527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
120627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *
120727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    "It is illegal to declare an array with a size, and then
120827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    later (in the same shader) index the same array with an
120927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    integral constant expression greater than or equal to the
121027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    declared size. It is also illegal to index an array with a
121127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    negative constant expression."
121227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  */
121363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (array->type->is_matrix()) {
121463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if (array->type->row_type()->vector_elements <= idx) {
121563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       bound = array->type->row_type()->vector_elements;
121663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    }
121763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else if (array->type->is_vector()) {
121863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if (array->type->vector_elements <= idx) {
121963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       bound = array->type->vector_elements;
122063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    }
122163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else {
122263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if ((array->type->array_size() > 0)
122363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick		&& (array->type->array_size() <= idx)) {
122463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       bound = array->type->array_size();
122563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    }
122627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 }
122727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
122863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (bound > 0) {
122963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    _mesa_glsl_error(& loc, state, "%s index must be < %u",
123063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			     type_name, bound);
123163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    error_emitted = true;
123263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else if (idx < 0) {
123363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    _mesa_glsl_error(& loc, state, "%s index must be >= 0",
123463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			     type_name);
123527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	    error_emitted = true;
123627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 }
1237b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
123863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (array->type->is_array()) {
1239a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	    /* If the array is a variable dereference, it dereferences the
1240a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     * whole array, by definition.  Use this to get the variable.
1241a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     *
1242a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     * FINISHME: Should some methods for getting / setting / testing
1243a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     * FINISHME: array access limits be added to ir_dereference?
1244a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     */
1245a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	    ir_variable *const v = array->whole_variable_referenced();
124663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if ((v != NULL) && (unsigned(idx) > v->max_array_access))
124763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       v->max_array_access = idx;
124863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 }
124927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
125027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
125127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (error_emitted)
125227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 result->type = glsl_type::error_type;
125327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
125427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      type = result->type;
1255a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
125627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick   }
1257a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1258a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_function_call:
12597cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick      /* Should *NEVER* get here.  ast_function_call should always be handled
12607cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick       * by ast_function_expression::hir.
1261a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
12627cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick      assert(0);
1263a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1264a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1265a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_identifier: {
1266a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* ast_identifier can appear several places in a full abstract syntax
1267a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * tree.  This particular use must be at location specified in the grammar
1268a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * as 'variable_identifier'.
1269a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
12708bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      ir_variable *var =
12718bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick	 state->symbols->get_variable(this->primary_expression.identifier);
1272a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
12731660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_dereference_variable(var);
1274a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1275a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (var != NULL) {
1276a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 type = result->type;
1277a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
127871d0bbfcb2853f37b580ec7b705e55bb0eb426faIan Romanick	 _mesa_glsl_error(& loc, state, "`%s' undeclared",
127918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  this->primary_expression.identifier);
1280a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1281a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 error_emitted = true;
1282a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1283a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1284a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1285a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1286a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_int_constant:
12870471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::int_type;
12881660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(this->primary_expression.int_constant);
1289a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1290a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1291a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_uint_constant:
12920471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::uint_type;
12931660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(this->primary_expression.uint_constant);
1294a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1295a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1296a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_float_constant:
12970471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::float_type;
12981660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(this->primary_expression.float_constant);
1299a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1300a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1301a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bool_constant:
13020471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::bool_type;
13031660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant));
1304a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1305a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1306a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sequence: {
1307a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* It should not be possible to generate a sequence in the AST without
1308a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * any expressions in it.
1309a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1310304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      assert(!this->expressions.is_empty());
1311a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1312a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* The r-value of a sequence is the last expression in the sequence.  If
1313a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * the other expressions in the sequence do not have side-effects (and
1314a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * therefore add instructions to the instruction list), they get dropped
1315a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * on the floor.
1316a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
13172b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick      foreach_list_typed (ast_node, ast, link, &this->expressions)
1318304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick	 result = ast->hir(instructions, state);
1319a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1320a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = result->type;
1321a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1322a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Any errors should have already been emitted in the loop above.
1323a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1324a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      error_emitted = true;
1325a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1326a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1327a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1328a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1329cef3baecf636a30b62cd7a1e8de57c7650f7003eIan Romanick   if (type->is_error() && !error_emitted)
133071d0bbfcb2853f37b580ec7b705e55bb0eb426faIan Romanick      _mesa_glsl_error(& loc, state, "type mismatch");
1331a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1332a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return result;
1333a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1334a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1335a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1336fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
13370044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_expression_statement::hir(exec_list *instructions,
133818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			      struct _mesa_glsl_parse_state *state)
1339a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1340a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* It is possible to have expression statements that don't have an
1341a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * expression.  This is the solitary semicolon:
1342a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
1343a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * for (i = 0; i < 5; i++)
1344a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     ;
1345a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
1346a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * In this case the expression will be NULL.  Test for NULL and don't do
1347a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * anything in that case.
1348a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
134918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (expression != NULL)
135018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      expression->hir(instructions, state);
1351a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1352a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Statements do not have r-values.
1353a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1354a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1355a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1356a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1357a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1358fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
13590044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_compound_statement::hir(exec_list *instructions,
136018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			    struct _mesa_glsl_parse_state *state)
1361a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
136218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (new_scope)
13638bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      state->symbols->push_scope();
1364a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
13652b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_node, ast, link, &this->statements)
1366304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      ast->hir(instructions, state);
1367a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
136818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (new_scope)
13698bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      state->symbols->pop_scope();
1370a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1371a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Compound statements do not have r-values.
1372a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1373a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1374a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1375a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1376a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
137728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanickstatic const glsl_type *
137828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanickprocess_array_type(const glsl_type *base, ast_node *array_size,
137928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick		   struct _mesa_glsl_parse_state *state)
138028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick{
138128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   unsigned length = 0;
138228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
138328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   /* FINISHME: Reject delcarations of multidimensional arrays. */
138428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
138528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   if (array_size != NULL) {
138628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      exec_list dummy_instructions;
138728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
138828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      YYLTYPE loc = array_size->get_location();
138928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
139028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      /* FINISHME: Verify that the grammar forbids side-effects in array
139128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick       * FINISHME: sizes.   i.e., 'vec4 [x = 12] data'
139228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick       */
139328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      assert(dummy_instructions.is_empty());
139428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
139528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      if (ir != NULL) {
139628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 if (!ir->type->is_integer()) {
139728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    _mesa_glsl_error(& loc, state, "array size must be integer type");
139828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 } else if (!ir->type->is_scalar()) {
139928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    _mesa_glsl_error(& loc, state, "array size must be scalar type");
140028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 } else {
140128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    ir_constant *const size = ir->constant_expression_value();
140228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
140328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    if (size == NULL) {
140428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       _mesa_glsl_error(& loc, state, "array size must be a "
140528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick				"constant valued expression");
140628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    } else if (size->value.i[0] <= 0) {
140728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       _mesa_glsl_error(& loc, state, "array size must be > 0");
140828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    } else {
140928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       assert(size->type == ir->type);
141028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       length = size->value.u[0];
141128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    }
141228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 }
141328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      }
141428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   }
141528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
141612c411504ca86341f8b96c349c15413ee198cc71Carl Worth   return glsl_type::get_array_instance(state, base, length);
141728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick}
141828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
141928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
1420d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanickconst glsl_type *
1421d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanickast_type_specifier::glsl_type(const char **name,
1422d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick			      struct _mesa_glsl_parse_state *state) const
1423a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1424d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   const struct glsl_type *type;
1425a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
14263455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   if ((this->type_specifier == ast_struct) && (this->type_name == NULL)) {
1427a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* FINISHME: Handle annonymous structures. */
1428a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = NULL;
1429a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
1430d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick      type = state->symbols->get_type(this->type_name);
1431d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick      *name = this->type_name;
1432a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1433d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick      if (this->is_array) {
1434d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick	 type = process_array_type(type, this->array_size, state);
143528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      }
1436a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1437a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1438a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return type;
1439a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1440a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1441a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1442a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic void
1443a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickapply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
1444a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick				 struct ir_variable *var,
14452e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				 struct _mesa_glsl_parse_state *state,
14462e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				 YYLTYPE *loc)
1447a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1448a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->invariant)
1449a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->invariant = 1;
1450a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1451a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* FINISHME: Mark 'in' variables at global scope as read-only. */
1452a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->constant || qual->attribute || qual->uniform
1453a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       || (qual->varying && (state->target == fragment_shader)))
1454a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->read_only = 1;
1455a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1456a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->centroid)
1457a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->centroid = 1;
1458a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1459ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick   if (qual->attribute && state->target != vertex_shader) {
14602e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      var->type = glsl_type::error_type;
14612e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      _mesa_glsl_error(loc, state,
14622e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt		       "`attribute' variables may not be declared in the "
1463ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick		       "%s shader",
1464ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick		       _mesa_glsl_shader_target_name(state->target));
14652e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   }
14662e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt
146790b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt   /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
146890b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *
146990b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     "The varying qualifier can be used only with the data types
147090b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
147190b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     these."
147290b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    */
14730ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt   if (qual->varying) {
14740ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      const glsl_type *non_array_type;
14750ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt
14760ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      if (var->type && var->type->is_array())
14770ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 non_array_type = var->type->fields.array;
14780ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      else
14790ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 non_array_type = var->type;
14800ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt
14810ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      if (non_array_type && non_array_type->base_type != GLSL_TYPE_FLOAT) {
14820ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 var->type = glsl_type::error_type;
14830ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 _mesa_glsl_error(loc, state,
14840ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt			  "varying variables must be of base type float");
14850ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      }
148690b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt   }
148790b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt
1488a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->in && qual->out)
1489a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_inout;
1490a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else if (qual->attribute || qual->in
1491a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    || (qual->varying && (state->target == fragment_shader)))
1492a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_in;
14930b678234625fac67a89285ad2871dedc891fb1b1Ian Romanick   else if (qual->out || (qual->varying && (state->target == vertex_shader)))
1494a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_out;
1495a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else if (qual->uniform)
1496a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_uniform;
1497a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else
1498a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_auto;
1499a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
150071df19f5ef6e78beb5160801f81468184b75447eEric Anholt   if (qual->uniform)
150171df19f5ef6e78beb5160801f81468184b75447eEric Anholt      var->shader_in = true;
1502c96822cf311d764a3cf6a2c62145851e8326c896Ian Romanick
1503c96822cf311d764a3cf6a2c62145851e8326c896Ian Romanick   /* Any 'in' or 'inout' variables at global scope must be marked as being
1504c96822cf311d764a3cf6a2c62145851e8326c896Ian Romanick    * shader inputs.  Likewise, any 'out' or 'inout' variables at global scope
1505c96822cf311d764a3cf6a2c62145851e8326c896Ian Romanick    * must be marked as being shader outputs.
1506c96822cf311d764a3cf6a2c62145851e8326c896Ian Romanick    */
1507c96822cf311d764a3cf6a2c62145851e8326c896Ian Romanick   if (state->current_function == NULL) {
1508c96822cf311d764a3cf6a2c62145851e8326c896Ian Romanick      switch (var->mode) {
1509c96822cf311d764a3cf6a2c62145851e8326c896Ian Romanick      case ir_var_in:
1510c96822cf311d764a3cf6a2c62145851e8326c896Ian Romanick      case ir_var_uniform:
1511c96822cf311d764a3cf6a2c62145851e8326c896Ian Romanick	 var->shader_in = true;
1512c96822cf311d764a3cf6a2c62145851e8326c896Ian Romanick	 break;
1513c96822cf311d764a3cf6a2c62145851e8326c896Ian Romanick      case ir_var_out:
1514c96822cf311d764a3cf6a2c62145851e8326c896Ian Romanick	 var->shader_out = true;
1515c96822cf311d764a3cf6a2c62145851e8326c896Ian Romanick	 break;
1516c96822cf311d764a3cf6a2c62145851e8326c896Ian Romanick      case ir_var_inout:
151771df19f5ef6e78beb5160801f81468184b75447eEric Anholt	 var->shader_in = true;
151871df19f5ef6e78beb5160801f81468184b75447eEric Anholt	 var->shader_out = true;
1519c96822cf311d764a3cf6a2c62145851e8326c896Ian Romanick	 break;
1520c96822cf311d764a3cf6a2c62145851e8326c896Ian Romanick      default:
1521c96822cf311d764a3cf6a2c62145851e8326c896Ian Romanick	 break;
1522c96822cf311d764a3cf6a2c62145851e8326c896Ian Romanick      }
152371df19f5ef6e78beb5160801f81468184b75447eEric Anholt   }
152471df19f5ef6e78beb5160801f81468184b75447eEric Anholt
1525a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->flat)
1526a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->interpolation = ir_var_flat;
1527a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else if (qual->noperspective)
1528a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->interpolation = ir_var_noperspective;
1529a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else
1530a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->interpolation = ir_var_smooth;
15319d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick
15329d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick   if (var->type->is_array() && (state->language_version >= 120)) {
15339d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick      var->array_lvalue = true;
15349d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick   }
1535a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1536a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1537a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1538fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
15390044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_declarator_list::hir(exec_list *instructions,
154018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			 struct _mesa_glsl_parse_state *state)
1541a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
15421660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   void *ctx = talloc_parent(state);
1543a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const struct glsl_type *decl_type;
1544a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const char *type_name = NULL;
15458558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt   ir_rvalue *result = NULL;
1546c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick   YYLTYPE loc = this->get_location();
1547a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
15483455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* The type specifier may contain a structure definition.  Process that
15493455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * before any of the variable declarations.
15503455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
15513455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   (void) this->type->specifier->hir(instructions, state);
15523455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
1553a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* FINISHME: Handle vertex shader "invariant" declarations that do not
1554a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * FINISHME: include a type.  These re-declare built-in variables to be
1555a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * FINISHME: invariant.
1556a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1557a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1558d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   decl_type = this->type->specifier->glsl_type(& type_name, state);
1559304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick   if (this->declarations.is_empty()) {
1560c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick      /* There are only two valid cases where the declaration list can be
1561c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick       * empty.
1562c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick       *
1563c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick       * 1. The declaration is setting the default precision of a built-in
1564c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick       *    type (e.g., 'precision highp vec4;').
1565c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick       *
1566c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick       * 2. Adding 'invariant' to an existing vertex shader output.
1567c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick       */
1568c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick
1569c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick      if (this->type->qualifier.invariant) {
1570c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick      } else if (decl_type != NULL) {
1571c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick      } else {
1572c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick	    _mesa_glsl_error(& loc, state, "incomplete declaration");
1573c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick      }
1574c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick   }
1575a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
15762b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
1577a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      const struct glsl_type *var_type;
1578a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      struct ir_variable *var;
1579a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1580a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* FINISHME: Emit a warning if a variable declaration shadows a
1581a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * FINISHME: declaration at a higher scope.
1582a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1583a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1584cec65a6b76290ee4da91691bd3ef01c3fb8a0c37Ian Romanick      if ((decl_type == NULL) || decl_type->is_void()) {
1585a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 if (type_name != NULL) {
1586a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    _mesa_glsl_error(& loc, state,
1587a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     "invalid type `%s' in declaration of `%s'",
1588a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     type_name, decl->identifier);
1589a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 } else {
1590a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    _mesa_glsl_error(& loc, state,
1591a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     "invalid type in declaration of `%s'",
1592a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     decl->identifier);
1593a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
1594a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 continue;
1595a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1596a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1597a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (decl->is_array) {
159828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 var_type = process_array_type(decl_type, decl->array_size, state);
1599a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
1600a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 var_type = decl_type;
1601a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1602a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
16031660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      var = new(ctx) ir_variable(var_type, decl->identifier);
1604a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
16053f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
16063f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *
16073f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     "Global variables can only use the qualifiers const,
16083f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     attribute, uni form, or varying. Only one may be
16093f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     specified.
16103f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *
16113f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     Local variables can only use the qualifier const."
16123f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *
16133f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       * This is relaxed in GLSL 1.30.
16143f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       */
16153f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      if (state->language_version < 120) {
16163f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 if (this->type->qualifier.out) {
16173f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	    _mesa_glsl_error(& loc, state,
16183f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "`out' qualifier in declaration of `%s' "
16193f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "only valid for function parameters in GLSL 1.10.",
16203f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     decl->identifier);
16213f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 }
16223f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 if (this->type->qualifier.in) {
16233f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	    _mesa_glsl_error(& loc, state,
16243f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "`in' qualifier in declaration of `%s' "
16253f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "only valid for function parameters in GLSL 1.10.",
16263f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     decl->identifier);
16273f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 }
16283f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 /* FINISHME: Test for other invalid qualifiers. */
16293f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      }
16303f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt
16312e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
16322e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				       & loc);
1633a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1634a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Attempt to add the variable to the symbol table.  If this fails, it
1635a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick       * means the variable has already been declared at this scope.  Arrays
1636a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick       * fudge this rule a little bit.
1637a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick       *
1638a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick       * From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
1639a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick       *
1640a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick       *    "It is legal to declare an array without a size and then
1641a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick       *    later re-declare the same name as an array of the same
1642a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick       *    type and specify a size."
1643a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
16443359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      if (state->symbols->name_declared_this_scope(decl->identifier)) {
1645a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	 ir_variable *const earlier =
1646a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	    state->symbols->get_variable(decl->identifier);
1647a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick
1648a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	 if ((earlier != NULL)
1649a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	     && (earlier->type->array_size() == 0)
1650a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	     && var->type->is_array()
1651a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	     && (var->type->element_type() == earlier->type->element_type())) {
1652a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	    /* FINISHME: This doesn't match the qualifiers on the two
1653a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	     * FINISHME: declarations.  It's not 100% clear whether this is
1654a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	     * FINISHME: required or not.
1655a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	     */
1656b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
1657894ea972a4defdaafeaa3a248c113b06c7ae0c7eEric Anholt	    if (var->type->array_size() <= (int)earlier->max_array_access) {
1658b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	       YYLTYPE loc = this->get_location();
1659b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
1660b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	       _mesa_glsl_error(& loc, state, "array size must be > %u due to "
1661b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick				"previous access",
1662b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick				earlier->max_array_access);
1663b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	    }
1664b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
1665a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	    earlier->type = var->type;
1666a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	    delete var;
1667a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	    var = NULL;
1668a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	 } else {
1669a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	    YYLTYPE loc = this->get_location();
1670a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick
1671a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	    _mesa_glsl_error(& loc, state, "`%s' redeclared",
1672a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick			     decl->identifier);
1673a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	 }
1674a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1675a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 continue;
1676a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1677a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1678b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt      /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
1679b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt       *
1680b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt       *   "Identifiers starting with "gl_" are reserved for use by
1681b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt       *   OpenGL, and may not be declared in a shader as either a
1682b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt       *   variable or a function."
1683b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt       */
1684b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt      if (strncmp(decl->identifier, "gl_", 3) == 0) {
1685b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt	 /* FINISHME: This should only trigger if we're not redefining
1686b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt	  * FINISHME: a builtin (to add a qualifier, for example).
1687b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt	  */
1688b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt	 _mesa_glsl_error(& loc, state,
1689b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt			  "identifier `%s' uses reserved `gl_' prefix",
1690b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt			  decl->identifier);
1691b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt      }
1692b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt
16930044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick      instructions->push_tail(var);
1694a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1695e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick      if (state->current_function != NULL) {
1696b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 const char *mode = NULL;
1697e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 const char *extra = "";
1698b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick
1699e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 /* There is no need to check for 'inout' here because the parser will
1700e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	  * only allow that in function parameter lists.
1701e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	  */
1702e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	 if (this->type->qualifier.attribute) {
1703b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	    mode = "attribute";
1704b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 } else if (this->type->qualifier.uniform) {
1705b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	    mode = "uniform";
1706b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 } else if (this->type->qualifier.varying) {
1707b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	    mode = "varying";
1708e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 } else if (this->type->qualifier.in) {
1709e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    mode = "in";
1710e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    extra = " or in function parameter list";
1711e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 } else if (this->type->qualifier.out) {
1712e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    mode = "out";
1713e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    extra = " or in function parameter list";
1714b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 }
1715b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick
1716b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 if (mode) {
1717e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	    _mesa_glsl_error(& loc, state,
1718b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick			     "%s variable `%s' must be declared at "
1719e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick			     "global scope%s",
1720e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick			     mode, var->name, extra);
1721e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	 }
1722e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick      } else if (var->mode == ir_var_in) {
1723fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	 if (state->target == vertex_shader) {
1724fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    bool error_emitted = false;
1725fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
1726fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1727fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
1728fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    "Vertex shader inputs can only be float, floating-point
1729fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    vectors, matrices, signed and unsigned integers and integer
1730fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    vectors. Vertex shader inputs can also form arrays of these
1731fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    types, but not structures."
1732fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
17332d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
17342d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *
17352d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *    "Vertex shader inputs can only be float, floating-point
17362d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *    vectors, matrices, signed and unsigned integers and integer
17372d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *    vectors. They cannot be arrays or structures."
17382d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *
1739fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
1740fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
1741fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    "The attribute qualifier can be used only with float,
1742fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    floating-point vectors, and matrices. Attribute variables
1743fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    cannot be declared as arrays or structures."
1744fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     */
1745fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    const glsl_type *check_type = var->type->is_array()
1746fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       ? var->type->fields.array : var->type;
1747fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
1748fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    switch (check_type->base_type) {
1749fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_FLOAT:
1750fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       break;
1751fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_UINT:
1752fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_INT:
1753fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       if (state->language_version > 120)
1754fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick		  break;
1755fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       /* FALLTHROUGH */
1756fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    default:
1757fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       _mesa_glsl_error(& loc, state,
1758fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"vertex shader input / attribute cannot have "
1759fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"type %s`%s'",
1760fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				var->type->is_array() ? "array of " : "",
1761fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				check_type->name);
1762fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       error_emitted = true;
1763fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    }
1764fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
17652d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	    if (!error_emitted && (state->language_version <= 130)
1766fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick		&& var->type->is_array()) {
1767fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       _mesa_glsl_error(& loc, state,
1768fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"vertex shader input / attribute cannot have "
1769fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"array type");
1770fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       error_emitted = true;
1771fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    }
1772fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	 }
1773fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick      }
1774fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
177566faec4895b7bb59a614087a200c05157191b4aeIan Romanick      if (decl->initializer != NULL) {
177643de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 YYLTYPE initializer_loc = decl->initializer->get_location();
177743de17282017bdf187d6e646de3262cc64b7f46bIan Romanick
177866faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
177966faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *
178066faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *    "All uniform variables are read-only and are initialized either
178166faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *    directly by an application via API commands, or indirectly by
178266faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *    OpenGL."
178366faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  */
178466faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 if ((state->language_version <= 110)
178566faec4895b7bb59a614087a200c05157191b4aeIan Romanick	     && (var->mode == ir_var_uniform)) {
178643de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	    _mesa_glsl_error(& initializer_loc, state,
178743de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     "cannot initialize uniforms in GLSL 1.10");
178843de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 }
178943de17282017bdf187d6e646de3262cc64b7f46bIan Romanick
179043de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 if (var->type->is_sampler()) {
179143de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	    _mesa_glsl_error(& initializer_loc, state,
179243de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     "cannot initialize samplers");
179343de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 }
179419360152f5bd8cff93359dbfe5a50a90b699c118Ian Romanick
179543de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 if ((var->mode == ir_var_in) && (state->current_function == NULL)) {
179643de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	    _mesa_glsl_error(& initializer_loc, state,
179743de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     "cannot initialize %s shader input / %s",
1798ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick			     _mesa_glsl_shader_target_name(state->target),
179943de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     (state->target == vertex_shader)
180043de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     ? "attribute" : "varying");
180166faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 }
180266faec4895b7bb59a614087a200c05157191b4aeIan Romanick
18031660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const lhs = new(ctx) ir_dereference_variable(var);
1804307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	 ir_rvalue *rhs = decl->initializer->hir(instructions, state);
180519360152f5bd8cff93359dbfe5a50a90b699c118Ian Romanick
1806ce030884064046925a655413097dd8257e9392ddIan Romanick	 /* Calculate the constant value if this is a const or uniform
1807307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	  * declaration.
180866faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  */
1809ce030884064046925a655413097dd8257e9392ddIan Romanick	 if (this->type->qualifier.constant || this->type->qualifier.uniform) {
1810326c676236e6a3c90db63e4d0c893aa4f9c21876Eric Anholt	    ir_constant *constant_value = rhs->constant_expression_value();
1811326c676236e6a3c90db63e4d0c893aa4f9c21876Eric Anholt	    if (!constant_value) {
1812307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	       _mesa_glsl_error(& initializer_loc, state,
1813ce030884064046925a655413097dd8257e9392ddIan Romanick				"initializer of %s variable `%s' must be a "
1814307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt				"constant expression",
1815ce030884064046925a655413097dd8257e9392ddIan Romanick				(this->type->qualifier.constant)
1816ce030884064046925a655413097dd8257e9392ddIan Romanick				? "const" : "uniform",
1817307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt				decl->identifier);
1818326c676236e6a3c90db63e4d0c893aa4f9c21876Eric Anholt	    } else {
1819326c676236e6a3c90db63e4d0c893aa4f9c21876Eric Anholt	       rhs = constant_value;
1820326c676236e6a3c90db63e4d0c893aa4f9c21876Eric Anholt	       var->constant_value = constant_value;
1821307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	    }
1822307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	 }
182366faec4895b7bb59a614087a200c05157191b4aeIan Romanick
1824307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	 if (rhs && !rhs->type->is_error()) {
1825ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt	    bool temp = var->read_only;
1826ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt	    if (this->type->qualifier.constant)
1827ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt	       var->read_only = false;
1828ce030884064046925a655413097dd8257e9392ddIan Romanick
1829ce030884064046925a655413097dd8257e9392ddIan Romanick	    /* Never emit code to initialize a uniform.
1830ce030884064046925a655413097dd8257e9392ddIan Romanick	     */
1831ce030884064046925a655413097dd8257e9392ddIan Romanick	    if (!this->type->qualifier.uniform)
1832ce030884064046925a655413097dd8257e9392ddIan Romanick	       result = do_assignment(instructions, state, lhs, rhs,
1833ce030884064046925a655413097dd8257e9392ddIan Romanick				      this->get_location());
1834ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt	    var->read_only = temp;
183566faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 }
183666faec4895b7bb59a614087a200c05157191b4aeIan Romanick      }
183717d86f4371da413176ba365ca26a58bac172d365Ian Romanick
18380ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt      /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
18390ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *
18400ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *     "It is an error to write to a const variable outside of
18410ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *      its declaration, so they must be initialized when
18420ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *      declared."
18430ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       */
18440ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt      if (this->type->qualifier.constant && decl->initializer == NULL) {
18450ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt	 _mesa_glsl_error(& loc, state,
18460ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt			  "const declaration of `%s' must be initialized");
18470ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt      }
18480ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt
184917d86f4371da413176ba365ca26a58bac172d365Ian Romanick      /* Add the vairable to the symbol table after processing the initializer.
185017d86f4371da413176ba365ca26a58bac172d365Ian Romanick       * This differs from most C-like languages, but it follows the GLSL
185117d86f4371da413176ba365ca26a58bac172d365Ian Romanick       * specification.  From page 28 (page 34 of the PDF) of the GLSL 1.50
185217d86f4371da413176ba365ca26a58bac172d365Ian Romanick       * spec:
185317d86f4371da413176ba365ca26a58bac172d365Ian Romanick       *
185417d86f4371da413176ba365ca26a58bac172d365Ian Romanick       *     "Within a declaration, the scope of a name starts immediately
185517d86f4371da413176ba365ca26a58bac172d365Ian Romanick       *     after the initializer if present or immediately after the name
185617d86f4371da413176ba365ca26a58bac172d365Ian Romanick       *     being declared if not."
185717d86f4371da413176ba365ca26a58bac172d365Ian Romanick       */
185817d86f4371da413176ba365ca26a58bac172d365Ian Romanick      const bool added_variable =
185917d86f4371da413176ba365ca26a58bac172d365Ian Romanick	 state->symbols->add_variable(decl->identifier, var);
186017d86f4371da413176ba365ca26a58bac172d365Ian Romanick      assert(added_variable);
1861a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1862a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
18638558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt
18648558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt   /* Generally, variable declarations do not have r-values.  However,
18658558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * one is used for the declaration in
18668558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    *
18678558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * while (bool b = some_condition()) {
18688558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    *   ...
18698558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * }
18708558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    *
18718558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * so we return the rvalue from the last seen declaration here.
1872a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
18738558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt   return result;
1874a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1875a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1876a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1877fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
18780044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_parameter_declarator::hir(exec_list *instructions,
187918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			      struct _mesa_glsl_parse_state *state)
1880a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
18811660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   void *ctx = talloc_parent(state);
1882a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const struct glsl_type *type;
1883a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const char *name = NULL;
18842e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   YYLTYPE loc = this->get_location();
1885a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1886d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   type = this->type->specifier->glsl_type(& name, state);
1887a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1888a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (type == NULL) {
1889a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (name != NULL) {
1890a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 _mesa_glsl_error(& loc, state,
1891a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			  "invalid type `%s' in declaration of `%s'",
189218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  name, this->identifier);
1893a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
1894a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 _mesa_glsl_error(& loc, state,
1895a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			  "invalid type in declaration of `%s'",
189618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  this->identifier);
1897a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1898a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
18990471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::error_type;
1900a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1901a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1902068c80cfe0a280490353b6b007165d717c672eedEric Anholt   /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
1903068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *
1904068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    "Functions that accept no input arguments need not use void in the
1905068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    argument list because prototypes (or definitions) are required and
1906068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    therefore there is no ambiguity when an empty argument list "( )" is
1907068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    declared. The idiom "(void)" as a parameter list is provided for
1908068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    convenience."
1909068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *
1910068c80cfe0a280490353b6b007165d717c672eedEric Anholt    * Placing this check here prevents a void parameter being set up
1911068c80cfe0a280490353b6b007165d717c672eedEric Anholt    * for a function, which avoids tripping up checks for main taking
1912068c80cfe0a280490353b6b007165d717c672eedEric Anholt    * parameters and lookups of an unnamed symbol.
1913068c80cfe0a280490353b6b007165d717c672eedEric Anholt    */
1914cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   if (type->is_void()) {
1915cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      if (this->identifier != NULL)
1916cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick	 _mesa_glsl_error(& loc, state,
1917cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick			  "named parameter cannot have type `void'");
1918cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
1919cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      is_void = true;
1920068c80cfe0a280490353b6b007165d717c672eedEric Anholt      return NULL;
1921cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   }
1922068c80cfe0a280490353b6b007165d717c672eedEric Anholt
192345d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   if (formal_parameter && (this->identifier == NULL)) {
192445d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick      _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
192545d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick      return NULL;
192645d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   }
192745d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick
1928cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   is_void = false;
19291660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   ir_variable *var = new(ctx) ir_variable(type, this->identifier);
1930a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1931a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* FINISHME: Handle array declarations.  Note that this requires
1932a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * FINISHME: complete handling of constant expressions.
1933a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1934a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1935cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick   /* Apply any specified qualifiers to the parameter declaration.  Note that
1936cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick    * for function parameters the default mode is 'in'.
1937cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick    */
19382e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc);
1939cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick   if (var->mode == ir_var_auto)
1940cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick      var->mode = ir_var_in;
1941a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
19420044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick   instructions->push_tail(var);
1943a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1944a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Parameter declarations do not have r-values.
1945a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1946a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1947a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1948a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1949a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
195045d8a70c12ee6ea956baaf898324a828496382f6Ian Romanickvoid
1951304ea90233baeac6801a98e981658cb7a2d2501cIan Romanickast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters,
195245d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					    bool formal,
195345d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					    exec_list *ir_parameters,
195445d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					    _mesa_glsl_parse_state *state)
1955a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1956cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   ast_parameter_declarator *void_param = NULL;
1957cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   unsigned count = 0;
1958a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
19592b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) {
196045d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick      param->formal_parameter = formal;
1961068c80cfe0a280490353b6b007165d717c672eedEric Anholt      param->hir(ir_parameters, state);
1962cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
1963cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      if (param->is_void)
1964cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick	 void_param = param;
1965cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
1966cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      count++;
1967cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   }
1968cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
1969cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   if ((void_param != NULL) && (count > 1)) {
1970cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      YYLTYPE loc = void_param->get_location();
1971cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
1972cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      _mesa_glsl_error(& loc, state,
1973cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick		       "`void' parameter must be only parameter");
1974a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1975a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1976a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1977a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1978fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
197992318a947958892497722772b03c643ebc943294Ian Romanickast_function::hir(exec_list *instructions,
198092318a947958892497722772b03c643ebc943294Ian Romanick		  struct _mesa_glsl_parse_state *state)
1981a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
19821660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   void *ctx = talloc_parent(state);
198318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   ir_function *f = NULL;
198492318a947958892497722772b03c643ebc943294Ian Romanick   ir_function_signature *sig = NULL;
198592318a947958892497722772b03c643ebc943294Ian Romanick   exec_list hir_parameters;
1986a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1987ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   const char *const name = identifier;
1988a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1989a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Convert the list of function parameters to HIR now so that they can be
1990a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * used below to compare this function's signature with previously seen
1991a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * signatures for functions with the same name.
1992a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
199345d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   ast_parameter_declarator::parameters_to_hir(& this->parameters,
199445d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					       is_definition,
199545d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					       & hir_parameters, state);
1996a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1997e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick   const char *return_type_name;
1998e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick   const glsl_type *return_type =
199992318a947958892497722772b03c643ebc943294Ian Romanick      this->return_type->specifier->glsl_type(& return_type_name, state);
2000e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick
2001e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick   assert(return_type != NULL);
2002e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick
2003ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
2004ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke    * "No qualifier is allowed on the return type of a function."
2005ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke    */
2006ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   if (this->return_type->has_qualifiers()) {
2007ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke      YYLTYPE loc = this->get_location();
2008ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke      _mesa_glsl_error(& loc, state,
2009ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke		       "function `%s' return type has qualifiers", name);
2010ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   }
2011ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke
2012a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Verify that this function's signature either doesn't match a previously
2013a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * seen signature for a function with the same name, or, if a match is found,
2014a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * that the previously seen signature does not have an associated definition.
2015a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
20163359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick   f = state->symbols->get_function(name);
2017a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (f != NULL) {
20180d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke      ir_function_signature *sig = f->exact_matching_signature(&hir_parameters);
20190d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke      if (sig != NULL) {
20200d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 const char *badvar = sig->qualifiers_match(&hir_parameters);
20210d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 if (badvar != NULL) {
20220d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    YYLTYPE loc = this->get_location();
2023abd40b15210c17b2a3ba8fcffc868fda203efa01Kenneth Graunke
20240d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
20250d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke			     "qualifiers don't match prototype", name, badvar);
20260d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 }
20271e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt
20280d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 if (sig->return_type != return_type) {
20290d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    YYLTYPE loc = this->get_location();
203060be7626b829af7e1d07330b9a88468924ba350eEric Anholt
20310d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
20320d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke			     "match prototype", name);
20330d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 }
2034a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
20350d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 if (is_definition && sig->is_defined) {
20360d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    YYLTYPE loc = this->get_location();
2037a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
20380d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
20390d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    sig = NULL;
2040a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
2041a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
20423359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick   } else if (state->symbols->name_declared_this_scope(name)) {
20433359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      /* This function name shadows a non-function use of the same name.
20443359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick       */
20453359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      YYLTYPE loc = this->get_location();
20463359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick
20473359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      _mesa_glsl_error(& loc, state, "function name `%s' conflicts with "
20483359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick		       "non-function", name);
204992318a947958892497722772b03c643ebc943294Ian Romanick      sig = NULL;
2050a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
20511660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      f = new(ctx) ir_function(name);
20528bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      state->symbols->add_function(f->name, f);
20539fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke
20549fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke      /* Emit the new function header */
20559fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke      instructions->push_tail(f);
2056a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2057a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2058ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   /* Verify the return type of main() */
2059ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   if (strcmp(name, "main") == 0) {
206025711a85c22bed305c9b52b89feb9c600d1892dfIan Romanick      if (! return_type->is_void()) {
2061ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt	 YYLTYPE loc = this->get_location();
2062ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt
2063ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt	 _mesa_glsl_error(& loc, state, "main() must return void");
2064ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt      }
2065174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt
206692318a947958892497722772b03c643ebc943294Ian Romanick      if (!hir_parameters.is_empty()) {
2067174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt	 YYLTYPE loc = this->get_location();
2068174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt
2069174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt	 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
2070174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt      }
2071ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   }
2072a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2073a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Finish storing the information about this new function in its signature.
2074a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
207592318a947958892497722772b03c643ebc943294Ian Romanick   if (sig == NULL) {
20761660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      sig = new(ctx) ir_function_signature(return_type);
207792318a947958892497722772b03c643ebc943294Ian Romanick      f->add_signature(sig);
2078a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2079a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2080bff6013d469b3d4e54cdc5731801c56994a523ecKenneth Graunke   sig->replace_parameters(&hir_parameters);
208192318a947958892497722772b03c643ebc943294Ian Romanick   signature = sig;
208292318a947958892497722772b03c643ebc943294Ian Romanick
208392318a947958892497722772b03c643ebc943294Ian Romanick   /* Function declarations (prototypes) do not have r-values.
208492318a947958892497722772b03c643ebc943294Ian Romanick    */
208592318a947958892497722772b03c643ebc943294Ian Romanick   return NULL;
208692318a947958892497722772b03c643ebc943294Ian Romanick}
208792318a947958892497722772b03c643ebc943294Ian Romanick
208892318a947958892497722772b03c643ebc943294Ian Romanick
208992318a947958892497722772b03c643ebc943294Ian Romanickir_rvalue *
209092318a947958892497722772b03c643ebc943294Ian Romanickast_function_definition::hir(exec_list *instructions,
209192318a947958892497722772b03c643ebc943294Ian Romanick			     struct _mesa_glsl_parse_state *state)
209292318a947958892497722772b03c643ebc943294Ian Romanick{
209392318a947958892497722772b03c643ebc943294Ian Romanick   prototype->is_definition = true;
209492318a947958892497722772b03c643ebc943294Ian Romanick   prototype->hir(instructions, state);
2095e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick
209692318a947958892497722772b03c643ebc943294Ian Romanick   ir_function_signature *signature = prototype->signature;
2097a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
209841ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   assert(state->current_function == NULL);
209941ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = signature;
210041ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick
2101e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick   /* Duplicate parameters declared in the prototype as concrete variables.
2102e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick    * Add these to the symbol table.
2103a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
21048bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick   state->symbols->push_scope();
2105e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick   foreach_iter(exec_list_iterator, iter, signature->parameters) {
2106fbc7c0b8f2e161bce1c048c63d2d5cfcdeb096f1Eric Anholt      ir_variable *const var = ((ir_instruction *) iter.get())->as_variable();
2107e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick
2108fbc7c0b8f2e161bce1c048c63d2d5cfcdeb096f1Eric Anholt      assert(var != NULL);
2109a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
21103359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      /* The only way a parameter would "exist" is if two parameters have
21113359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick       * the same name.
21123359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick       */
21133359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      if (state->symbols->name_declared_this_scope(var->name)) {
21143359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 YYLTYPE loc = this->get_location();
21153359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick
21163359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
21173359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      } else {
21183359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 state->symbols->add_variable(var->name, var);
21193359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      }
2120a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2121a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
21229fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke   /* Convert the body of the function to HIR. */
2123894ea972a4defdaafeaa3a248c113b06c7ae0c7eEric Anholt   this->body->hir(&signature->body, state);
21249fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke   signature->is_defined = true;
2125a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
21268bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick   state->symbols->pop_scope();
2127a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
212841ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   assert(state->current_function == signature);
212941ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = NULL;
2130a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2131a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Function definitions do not have r-values.
2132a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
2133a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
2134a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
213516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
213616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
2137fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
213816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanickast_jump_statement::hir(exec_list *instructions,
213916a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick			struct _mesa_glsl_parse_state *state)
214016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick{
21411660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   void *ctx = talloc_parent(state);
214216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
2143c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   switch (mode) {
2144c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_return: {
214516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      ir_return *inst;
2146aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt      assert(state->current_function);
214716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
214816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      if (opt_return_value) {
2149ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	 if (state->current_function->return_type->base_type ==
2150ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	     GLSL_TYPE_VOID) {
2151ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	    YYLTYPE loc = this->get_location();
2152ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt
2153ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	    _mesa_glsl_error(& loc, state,
2154ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt			     "`return` with a value, in function `%s' "
2155ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt			     "returning void",
2156f96c52ba2e290e3ba5f14cd7f87ba5b4382a1785Kenneth Graunke			     state->current_function->function_name());
2157ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	 }
215816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
215916a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 ir_expression *const ret = (ir_expression *)
216016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	    opt_return_value->hir(instructions, state);
216116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 assert(ret != NULL);
216216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
216318707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	 /* Implicit conversions are not allowed for return values. */
216418707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	 if (state->current_function->return_type != ret->type) {
216518707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	    YYLTYPE loc = this->get_location();
216618707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke
216718707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	    _mesa_glsl_error(& loc, state,
216818707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     "`return' with wrong type %s, in function `%s' "
216918707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     "returning %s",
217018707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     ret->type->name,
217118707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     state->current_function->function_name(),
217218707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     state->current_function->return_type->name);
217318707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	 }
217416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
21751660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 inst = new(ctx) ir_return(ret);
217616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      } else {
2177aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	 if (state->current_function->return_type->base_type !=
2178aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	     GLSL_TYPE_VOID) {
2179aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	    YYLTYPE loc = this->get_location();
2180aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt
2181aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	    _mesa_glsl_error(& loc, state,
2182aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt			     "`return' with no value, in function %s returning "
2183aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt			     "non-void",
2184f96c52ba2e290e3ba5f14cd7f87ba5b4382a1785Kenneth Graunke			     state->current_function->function_name());
2185aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	 }
21861660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 inst = new(ctx) ir_return;
218716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      }
218816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
218916a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      instructions->push_tail(inst);
2190c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick      break;
219116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   }
219216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
2193c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_discard:
2194b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt      /* FINISHME: discard support */
2195b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt      if (state->target != fragment_shader) {
2196b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt	 YYLTYPE loc = this->get_location();
2197b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt
2198b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt	 _mesa_glsl_error(& loc, state,
2199b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt			  "`discard' may only appear in a fragment shader");
2200b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt      }
2201c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick      break;
2202c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick
2203c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_break:
2204c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_continue:
22054cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      /* FINISHME: Handle switch-statements.  They cannot contain 'continue',
22064cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick       * FINISHME: and they use a different IR instruction for 'break'.
22074cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick       */
22084cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      /* FINISHME: Correctly handle the nesting.  If a switch-statement is
22094cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick       * FINISHME: inside a loop, a 'continue' is valid and will bind to the
22104cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick       * FINISHME: loop.
22114cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick       */
22124cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      if (state->loop_or_switch_nesting == NULL) {
22134cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 YYLTYPE loc = this->get_location();
22144cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
22154cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 _mesa_glsl_error(& loc, state,
22164cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick			  "`%s' may only appear in a loop",
22174cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick			  (mode == ast_break) ? "break" : "continue");
22184cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      } else {
22194cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 ir_loop *const loop = state->loop_or_switch_nesting->as_loop();
22204cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
22214cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 if (loop != NULL) {
22224cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	    ir_loop_jump *const jump =
22231660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	       new(ctx) ir_loop_jump((mode == ast_break)
22241660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				     ? ir_loop_jump::jump_break
22251660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				     : ir_loop_jump::jump_continue);
22264cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	    instructions->push_tail(jump);
22274cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 }
22284cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      }
22294cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
2230c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick      break;
2231b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt   }
2232b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt
223316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   /* Jump instructions do not have r-values.
223416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick    */
223516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   return NULL;
223616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick}
22373c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
22383c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
22393c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanickir_rvalue *
22403c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanickast_selection_statement::hir(exec_list *instructions,
22413c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick			     struct _mesa_glsl_parse_state *state)
22423c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick{
22431660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   void *ctx = talloc_parent(state);
22441660a2954797e056caba319c5d6c70b0d4be22feCarl Worth
22453c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   ir_rvalue *const condition = this->condition->hir(instructions, state);
22463c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
22473c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
22483c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *
22493c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    "Any expression whose type evaluates to a Boolean can be used as the
22503c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    conditional expression bool-expression. Vector types are not accepted
22513c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    as the expression to if."
22523c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *
22533c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    * The checks are separated so that higher quality diagnostics can be
22543c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    * generated for cases where both rules are violated.
22553c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    */
22563c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
22573c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      YYLTYPE loc = this->condition->get_location();
22583c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
22593c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
22603c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick		       "boolean");
22613c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   }
22623c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
22631660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   ir_if *const stmt = new(ctx) ir_if(condition);
22643c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
22654f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick   if (then_statement != NULL)
22664f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick      then_statement->hir(& stmt->then_instructions, state);
22673c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
22684f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick   if (else_statement != NULL)
22694f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick      else_statement->hir(& stmt->else_instructions, state);
22703c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
22713c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   instructions->push_tail(stmt);
22723c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
22733c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   /* if-statements do not have r-values.
22743c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    */
22753c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   return NULL;
22763c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick}
22779e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
22789e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
22798c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickvoid
22808c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickast_iteration_statement::condition_to_hir(ir_loop *stmt,
22818c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick					  struct _mesa_glsl_parse_state *state)
22829e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick{
22831660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   void *ctx = talloc_parent(state);
22841660a2954797e056caba319c5d6c70b0d4be22feCarl Worth
22859e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   if (condition != NULL) {
22869e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      ir_rvalue *const cond =
22879e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 condition->hir(& stmt->body_instructions, state);
22889e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
22899e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      if ((cond == NULL)
22909e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	  || !cond->type->is_boolean() || !cond->type->is_scalar()) {
22919e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 YYLTYPE loc = condition->get_location();
22929e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
22939e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 _mesa_glsl_error(& loc, state,
22949e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick			  "loop condition must be scalar boolean");
22959e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      } else {
22969e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 /* As the first code in the loop body, generate a block that looks
22979e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	  * like 'if (!condition) break;' as the loop termination condition.
22989e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	  */
22999e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 ir_rvalue *const not_cond =
23001660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_expression(ir_unop_logic_not, glsl_type::bool_type, cond,
23011660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				   NULL);
23029e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
23031660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_if *const if_stmt = new(ctx) ir_if(not_cond);
23049e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
23059e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 ir_jump *const break_stmt =
23061660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
23079e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
23089e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 if_stmt->then_instructions.push_tail(break_stmt);
23099e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 stmt->body_instructions.push_tail(if_stmt);
23109e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      }
23119e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   }
23128c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick}
23138c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
23148c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
23158c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickir_rvalue *
23168c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickast_iteration_statement::hir(exec_list *instructions,
23178c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick			     struct _mesa_glsl_parse_state *state)
23188c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick{
23191660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   void *ctx = talloc_parent(state);
23201660a2954797e056caba319c5d6c70b0d4be22feCarl Worth
2321484606610e36ec7598f7733e9787d888b6a63d64Ian Romanick   /* For-loops and while-loops start a new scope, but do-while loops do not.
23228c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick    */
2323484606610e36ec7598f7733e9787d888b6a63d64Ian Romanick   if (mode != ast_do_while)
23248c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick      state->symbols->push_scope();
23258c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
23268c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   if (init_statement != NULL)
23278c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick      init_statement->hir(instructions, state);
23288c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
23291660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   ir_loop *const stmt = new(ctx) ir_loop();
23308c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   instructions->push_tail(stmt);
23318c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
23328c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   /* Track the current loop and / or switch-statement nesting.
23338c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick    */
23348c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   ir_instruction *const nesting = state->loop_or_switch_nesting;
23358c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   state->loop_or_switch_nesting = stmt;
23368c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
23378c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   if (mode != ast_do_while)
23388c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick      condition_to_hir(stmt, state);
23399e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
23404f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick   if (body != NULL)
23414f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick      body->hir(& stmt->body_instructions, state);
23429e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
23439e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   if (rest_expression != NULL)
23449e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      rest_expression->hir(& stmt->body_instructions, state);
23459e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
23468c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   if (mode == ast_do_while)
23478c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick      condition_to_hir(stmt, state);
23488c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
2349484606610e36ec7598f7733e9787d888b6a63d64Ian Romanick   if (mode != ast_do_while)
23509e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      state->symbols->pop_scope();
23519e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
2352e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick   /* Restore previous nesting before returning.
2353e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick    */
2354e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick   state->loop_or_switch_nesting = nesting;
2355e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick
23569e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   /* Loops do not have r-values.
23579e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick    */
23589e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   return NULL;
23599e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick}
23603455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
23613455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
23623455ce614424a5a23a23037e23d0454e476bceeaIan Romanickir_rvalue *
23633455ce614424a5a23a23037e23d0454e476bceeaIan Romanickast_type_specifier::hir(exec_list *instructions,
23643455ce614424a5a23a23037e23d0454e476bceeaIan Romanick			  struct _mesa_glsl_parse_state *state)
23653455ce614424a5a23a23037e23d0454e476bceeaIan Romanick{
23663455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   if (this->structure != NULL)
23673455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      return this->structure->hir(instructions, state);
236885ba37b97df8edd18b757bc475b12b66f4b117edIan Romanick
236985ba37b97df8edd18b757bc475b12b66f4b117edIan Romanick   return NULL;
23703455ce614424a5a23a23037e23d0454e476bceeaIan Romanick}
23713455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
23723455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
23733455ce614424a5a23a23037e23d0454e476bceeaIan Romanickir_rvalue *
23743455ce614424a5a23a23037e23d0454e476bceeaIan Romanickast_struct_specifier::hir(exec_list *instructions,
23753455ce614424a5a23a23037e23d0454e476bceeaIan Romanick			  struct _mesa_glsl_parse_state *state)
23763455ce614424a5a23a23037e23d0454e476bceeaIan Romanick{
2377b3bd77da56ce8aa225ee91565e4d1e640685728cCarl Worth   void *ctx = talloc_parent(state);
23783455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   unsigned decl_count = 0;
23793455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
23803455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* Make an initial pass over the list of structure fields to determine how
23813455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * many there are.  Each element in this list is an ast_declarator_list.
23823455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * This means that we actually need to count the number of elements in the
23833455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * 'declarations' list in each of the elements.
23843455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
23852b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_declarator_list, decl_list, link,
23862b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick		       &this->declarations) {
2387304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      foreach_list_const (decl_ptr, & decl_list->declarations) {
23883455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 decl_count++;
23893455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      }
23903455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   }
23913455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
23923455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
23933455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* Allocate storage for the structure fields and process the field
23943455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * declarations.  As the declarations are processed, try to also convert
23953455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * the types to HIR.  This ensures that structure definitions embedded in
23963455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * other structure definitions are processed.
23973455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
23983455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   glsl_struct_field *const fields = (glsl_struct_field *)
23993455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      malloc(sizeof(*fields) * decl_count);
24003455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
24013455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   unsigned i = 0;
24022b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_declarator_list, decl_list, link,
24032b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick		       &this->declarations) {
24043455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      const char *type_name;
24053455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
24063455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      decl_list->type->specifier->hir(instructions, state);
24073455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
24083455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      const glsl_type *decl_type =
24093455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 decl_list->type->specifier->glsl_type(& type_name, state);
24103455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
24112b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick      foreach_list_typed (ast_declaration, decl, link,
24122b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick			  &decl_list->declarations) {
24133455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 const struct glsl_type *const field_type =
24143455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	    (decl->is_array)
24153455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	    ? process_array_type(decl_type, decl->array_size, state)
24163455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	    : decl_type;
24173455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
241873986a7a262807ab2cfd6d46ae17cfc7a30cdfecIan Romanick	 fields[i].type = (field_type != NULL)
241973986a7a262807ab2cfd6d46ae17cfc7a30cdfecIan Romanick	    ? field_type : glsl_type::error_type;
24203455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 fields[i].name = decl->identifier;
24213455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 i++;
24223455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      }
24233455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   }
24243455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
24253455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   assert(i == decl_count);
24263455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
24271d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick   const char *name;
24281d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick   if (this->name == NULL) {
24291d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick      static unsigned anon_count = 1;
24301d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick      char buf[32];
24313455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
24321d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick      snprintf(buf, sizeof(buf), "#anon_struct_%04x", anon_count);
24331d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick      anon_count++;
24341d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick
24351d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick      name = strdup(buf);
24361d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick   } else {
24371d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick      name = this->name;
24381d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick   }
24391d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick
2440b3bd77da56ce8aa225ee91565e4d1e640685728cCarl Worth   glsl_type *t = new(ctx) glsl_type(fields, decl_count, name);
24411d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick
2442ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick   YYLTYPE loc = this->get_location();
2443ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick   if (!state->symbols->add_type(name, t)) {
2444ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick      _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
2445ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick   } else {
2446ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick      /* This logic is a bit tricky.  It is an error to declare a structure at
2447ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick       * global scope if there is also a function with the same name.
2448ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick       */
2449ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick      if ((state->current_function == NULL)
2450ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick	  && (state->symbols->get_function(name) != NULL)) {
2451ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick	 _mesa_glsl_error(& loc, state, "name `%s' previously defined", name);
2452ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick      } else {
2453ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick	 t->generate_constructor(state->symbols);
2454ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick      }
2455a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick
2456a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick      const glsl_type **s = (const glsl_type **)
2457a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick	 realloc(state->user_structures,
2458a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick		 sizeof(state->user_structures[0]) *
2459a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick		 (state->num_user_structures + 1));
2460a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick      if (s != NULL) {
2461a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick	 s[state->num_user_structures] = t;
2462a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick	 state->user_structures = s;
2463a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick	 state->num_user_structures++;
2464a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick      }
2465ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick   }
24663455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
24673455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* Structure type definitions do not have r-values.
24683455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
24693455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   return NULL;
24703455ce614424a5a23a23037e23d0454e476bceeaIan Romanick}
2471