ast_to_hir.cpp revision a789ca649cb143c0c5bf3209ff1bde398fbd777e
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
52bfd7c9ac228c7ed8aec04c3b3aa33f40ee00b035Chia-I Wu#include "main/core.h" /* for struct gl_extensions */
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);
63c22c40015db32b68b33c4944b9d94bf499135ec5Eric Anholt   _mesa_glsl_initialize_functions(instructions, state);
64adfb0cd7401251bef0c854ac945fce78f0ed11dbIan Romanick
6541ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = NULL;
6641ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick
67a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke   /* Section 4.2 of the GLSL 1.20 specification states:
68a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    * "The built-in functions are scoped in a scope outside the global scope
69a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    *  users declare global variables in.  That is, a shader's global scope,
70a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    *  available for user-defined functions and global variables, is nested
71a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    *  inside the scope containing the built-in functions."
72a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    *
73a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    * Since built-in functions like ftransform() access built-in variables,
74a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    * it follows that those must be in the outer scope as well.
75a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    *
76a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    * We push scope here to create this nesting effect...but don't pop.
77a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    * This way, a shader's globals are still in the symbol table for use
78a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    * by the linker.
79a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    */
80a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke   state->symbols->push_scope();
81a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke
822b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_node, ast, link, & state->translation_unit)
83304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      ast->hir(instructions, state);
84d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick}
85d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick
86d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick
870104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick/**
880104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * If a conversion is available, convert one operand to a different type
890104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick *
900104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * The \c from \c ir_rvalue is converted "in place".
910104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick *
920104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param to     Type that the operand it to be converted to
930104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param from   Operand that is being converted
940104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param state  GLSL compiler state
950104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick *
960104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \return
970104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * If a conversion is possible (or unnecessary), \c true is returned.
980104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * Otherwise \c false is returned.
990104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick */
1000104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanickstatic bool
101bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickapply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
1020104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick			  struct _mesa_glsl_parse_state *state)
1030104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick{
104953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
105bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   if (to->base_type == from->type->base_type)
1060104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return true;
1070104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1080104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   /* This conversion was added in GLSL 1.20.  If the compilation mode is
1090104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    * GLSL 1.10, the conversion is skipped.
1100104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    */
1110104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   if (state->language_version < 120)
1120104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return false;
1130104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1140104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
1150104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *
1160104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    "There are no implicit array or structure conversions. For
1170104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    example, an array of int cannot be implicitly converted to an
1180104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    array of float. There are no implicit conversions between
1190104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    signed and unsigned integers."
1200104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    */
1210104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   /* FINISHME: The above comment is partially a lie.  There is int/uint
1220104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    * FINISHME: conversion for immediate constants.
1230104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    */
124bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   if (!to->is_float() || !from->type->is_numeric())
1250104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return false;
1260104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
127506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke   /* Convert to a floating point type with the same number of components
128506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke    * as the original type - i.e. int to float, not int to vec4.
129506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke    */
130506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke   to = glsl_type::get_instance(GLSL_TYPE_FLOAT, from->type->vector_elements,
131506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke			        from->type->matrix_columns);
132506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke
133bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   switch (from->type->base_type) {
1340104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   case GLSL_TYPE_INT:
1351660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      from = new(ctx) ir_expression(ir_unop_i2f, to, from, NULL);
1360104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      break;
1370104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   case GLSL_TYPE_UINT:
1381660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      from = new(ctx) ir_expression(ir_unop_u2f, to, from, NULL);
1390104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      break;
1400104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   case GLSL_TYPE_BOOL:
1411660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      from = new(ctx) ir_expression(ir_unop_b2f, to, from, NULL);
142dc58b3f8ccd817fdee390a3df5b8e0fb29d5397cEric Anholt      break;
1430104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   default:
1440104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      assert(0);
1450104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   }
1460104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1470104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   return true;
1480104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick}
1490104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1500104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
151a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
152bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickarithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
153a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick		       bool multiply,
154a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
155a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
156336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *type_a = value_a->type;
157336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *type_b = value_b->type;
1580104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
159a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
160a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
161a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The arithmetic binary operators add (+), subtract (-),
162a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    multiply (*), and divide (/) operate on integer and
163a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    floating-point scalars, vectors, and matrices."
164a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
16560b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   if (!type_a->is_numeric() || !type_b->is_numeric()) {
166a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state,
167a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "Operands to arithmetic operators must be numeric");
1680471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
169a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
170a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
171a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
172a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "If one operand is floating-point based and the other is
173a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    not, then the conversions from Section 4.1.10 "Implicit
174a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    Conversions" are applied to the non-floating-point-based operand."
175a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1760104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   if (!apply_implicit_conversion(type_a, value_b, state)
1770104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick       && !apply_implicit_conversion(type_b, value_a, state)) {
178a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state,
179a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "Could not implicitly convert operands to "
180a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "arithmetic operator");
1810104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return glsl_type::error_type;
182a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
183336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   type_a = value_a->type;
184336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   type_b = value_b->type;
185336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt
186a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "If the operands are integer types, they must both be signed or
187a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    both be unsigned."
188a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
189a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * From this rule and the preceeding conversion it can be inferred that
190a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
19160b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick    * The is_numeric check above already filtered out the case where either
19260b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick    * type is not one of these, so now the base types need only be tested for
19360b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick    * equality.
194a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
195a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (type_a->base_type != type_b->base_type) {
196a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state,
197a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "base type mismatch for arithmetic operator");
1980471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
199a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
200a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
201a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "All arithmetic binary operators result in the same fundamental type
202a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    (signed integer, unsigned integer, or floating-point) as the
203a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    operands they operate on, after operand type conversion. After
204a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    conversion, the following cases are valid
205a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
206a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    * The two operands are scalars. In this case the operation is
207a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      applied, resulting in a scalar."
208a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
209cb36f8aaeeb09660843316270a781948f773d90bIan Romanick   if (type_a->is_scalar() && type_b->is_scalar())
210a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return type_a;
211a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
212a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*   "* One operand is a scalar, and the other is a vector or matrix.
213a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      In this case, the scalar operation is applied independently to each
214a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      component of the vector or matrix, resulting in the same size
215a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      vector or matrix."
216a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
217cb36f8aaeeb09660843316270a781948f773d90bIan Romanick   if (type_a->is_scalar()) {
218cb36f8aaeeb09660843316270a781948f773d90bIan Romanick      if (!type_b->is_scalar())
219a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 return type_b;
220cb36f8aaeeb09660843316270a781948f773d90bIan Romanick   } else if (type_b->is_scalar()) {
221a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return type_a;
222a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
223a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
224a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* All of the combinations of <scalar, scalar>, <vector, scalar>,
225a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
226a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * handled.
227a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
22860b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   assert(!type_a->is_scalar());
22960b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   assert(!type_b->is_scalar());
230a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
231a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*   "* The two operands are vectors of the same size. In this case, the
232a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operation is done component-wise resulting in the same size
233a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      vector."
234a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
235a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick   if (type_a->is_vector() && type_b->is_vector()) {
236a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      if (type_a == type_b) {
237a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 return type_a;
238a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      } else {
239a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 _mesa_glsl_error(loc, state,
240a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt			  "vector size mismatch for arithmetic operator");
241a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 return glsl_type::error_type;
242a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      }
243a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
244a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
245a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* All of the combinations of <scalar, scalar>, <vector, scalar>,
246a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
247a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * <vector, vector> have been handled.  At least one of the operands must
248a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * be matrix.  Further, since there are no integer matrix types, the base
249a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * type of both operands must be float.
250a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
25160b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   assert(type_a->is_matrix() || type_b->is_matrix());
252a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   assert(type_a->base_type == GLSL_TYPE_FLOAT);
253a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   assert(type_b->base_type == GLSL_TYPE_FLOAT);
254a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
255a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*   "* The operator is add (+), subtract (-), or divide (/), and the
256a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operands are matrices with the same number of rows and the same
257a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      number of columns. In this case, the operation is done component-
258a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      wise resulting in the same size matrix."
259a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    * The operator is multiply (*), where both operands are matrices or
260a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      one operand is a vector and the other a matrix. A right vector
261a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operand is treated as a column vector and a left vector operand as a
262a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      row vector. In all these cases, it is required that the number of
263a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      columns of the left operand is equal to the number of rows of the
264a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      right operand. Then, the multiply (*) operation does a linear
265a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      algebraic multiply, yielding an object that has the same number of
266a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      rows as the left operand and the same number of columns as the right
267a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operand. Section 5.10 "Vector and Matrix Operations" explains in
268a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      more detail how vectors and matrices are operated on."
269a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
270a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (! multiply) {
271a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      if (type_a == type_b)
272a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 return type_a;
273a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
274fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick      if (type_a->is_matrix() && type_b->is_matrix()) {
275c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 /* Matrix multiply.  The columns of A must match the rows of B.  Given
276c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * the other previously tested constraints, this means the vector type
277c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * of a row from A must be the same as the vector type of a column from
278c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * B.
279c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  */
280c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 if (type_a->row_type() == type_b->column_type()) {
281c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	    /* The resulting matrix has the number of columns of matrix B and
282c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     * the number of rows of matrix A.  We get the row count of A by
283c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     * looking at the size of a vector that makes up a column.  The
284c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     * transpose (size of a row) is done for B.
285c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     */
286a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	    const glsl_type *const type =
287c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	       glsl_type::get_instance(type_a->base_type,
288c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick				       type_a->column_type()->vector_elements,
289c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick				       type_b->row_type()->vector_elements);
290a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	    assert(type != glsl_type::error_type);
291a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt
292a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	    return type;
293a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
294fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick      } else if (type_a->is_matrix()) {
295a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 /* A is a matrix and B is a column vector.  Columns of A must match
296c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * rows of B.  Given the other previously tested constraints, this
297c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * means the vector type of a row from A must be the same as the
298c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * vector the type of B.
299a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  */
30047c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	 if (type_a->row_type() == type_b) {
30147c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    /* The resulting vector has a number of elements equal to
30247c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	     * the number of rows of matrix A. */
30347c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    const glsl_type *const type =
30447c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	       glsl_type::get_instance(type_a->base_type,
30547c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth				       type_a->column_type()->vector_elements,
30647c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth				       1);
30747c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    assert(type != glsl_type::error_type);
30847c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth
30947c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    return type;
31047c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	 }
311a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
312fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick	 assert(type_b->is_matrix());
313a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
314c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 /* A is a row vector and B is a matrix.  Columns of A must match rows
315c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * of B.  Given the other previously tested constraints, this means
316c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * the type of A must be the same as the vector type of a column from
317c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * B.
318a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  */
31947c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	 if (type_a == type_b->column_type()) {
32047c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    /* The resulting vector has a number of elements equal to
32147c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	     * the number of columns of matrix B. */
32247c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    const glsl_type *const type =
32347c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	       glsl_type::get_instance(type_a->base_type,
32447c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth				       type_b->row_type()->vector_elements,
32547c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth				       1);
32647c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    assert(type != glsl_type::error_type);
32747c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth
32847c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    return type;
32947c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	 }
330a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
331a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt
332a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication");
333a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      return glsl_type::error_type;
334a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
335a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
336a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
337a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "All other cases are illegal."
338a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
339a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt   _mesa_glsl_error(loc, state, "type mismatch");
3400471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::error_type;
341a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
342a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
343a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
344a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
34565e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholtunary_arithmetic_result_type(const struct glsl_type *type,
34665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt			     struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
347a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
348a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 57:
349a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
350a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The arithmetic unary operators negate (-), post- and pre-increment
351a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     and decrement (-- and ++) operate on integer or floating-point
352a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     values (including vectors and matrices). All unary operators work
353a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     component-wise on their operands. These result with the same type
354a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     they operated on."
355a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
35665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   if (!type->is_numeric()) {
35765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state,
35865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "Operands to arithmetic operators must be numeric");
3590471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
36065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   }
361a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
362a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return type;
363a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
364a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
365a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
366a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
367a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickmodulus_result_type(const struct glsl_type *type_a,
36865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		    const struct glsl_type *type_b,
36965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		    struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
370a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
371a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
372a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The operator modulus (%) operates on signed or unsigned integers or
373a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    integer vectors. The operand types must both be signed or both be
374a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    unsigned."
375a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
37640176e249f72b6090204611873b19aed3da67c71Ian Romanick   if (!type_a->is_integer() || !type_b->is_integer()
377a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       || (type_a->base_type != type_b->base_type)) {
37865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state, "type mismatch");
3790471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
380a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
381a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
382a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The operands cannot be vectors of differing size. If one operand is
383a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    a scalar and the other vector, then the scalar is applied component-
384a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    wise to the vector, resulting in the same type as the vector. If both
385a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    are vectors of the same size, the result is computed component-wise."
386a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
387a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick   if (type_a->is_vector()) {
388a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick      if (!type_b->is_vector()
389a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  || (type_a->vector_elements == type_b->vector_elements))
390a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 return type_a;
391a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else
392a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return type_b;
393a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
394a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The operator modulus (%) is not defined for any other data types
395a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    (non-integer types)."
396a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
39765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   _mesa_glsl_error(loc, state, "type mismatch");
3980471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::error_type;
399a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
400a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
401a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
402a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
403bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickrelational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
40465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
405a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
406336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *type_a = value_a->type;
407336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *type_b = value_b->type;
4080150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick
409a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
410a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The relational operators greater than (>), less than (<), greater
411a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    than or equal (>=), and less than or equal (<=) operate only on
412a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    scalar integer and scalar floating-point expressions."
413a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
414a6d653dcbbee3158f9ea7b284bdeb1a8432f0fcbIan Romanick   if (!type_a->is_numeric()
415a6d653dcbbee3158f9ea7b284bdeb1a8432f0fcbIan Romanick       || !type_b->is_numeric()
416cb36f8aaeeb09660843316270a781948f773d90bIan Romanick       || !type_a->is_scalar()
41765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt       || !type_b->is_scalar()) {
41865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state,
41965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "Operands to relational operators must be scalar and "
42065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "numeric");
4210471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
42265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   }
423a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
424a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "Either the operands' types must match, or the conversions from
425a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    Section 4.1.10 "Implicit Conversions" will be applied to the integer
426a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    operand, after which the types must match."
427a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
4280150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick   if (!apply_implicit_conversion(type_a, value_b, state)
4290150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick       && !apply_implicit_conversion(type_b, value_a, state)) {
43065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state,
43165e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "Could not implicitly convert operands to "
43265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "relational operator");
4330150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick      return glsl_type::error_type;
434a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
435336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   type_a = value_a->type;
436336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   type_b = value_b->type;
437a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
43865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   if (type_a->base_type != type_b->base_type) {
43965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state, "base type mismatch");
4400471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
44165e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   }
442a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
443a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The result is scalar Boolean."
444a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
4450471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::bool_type;
446a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
447a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
448a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
4490bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick/**
4500bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Validates that a value can be assigned to a location with a specified type
4510bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
4520bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Validates that \c rhs can be assigned to some location.  If the types are
4530bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * not an exact match but an automatic conversion is possible, \c rhs will be
4540bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * converted.
4550bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
4560bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \return
4570bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
4580bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Otherwise the actual RHS to be assigned will be returned.  This may be
4590bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \c rhs, or it may be \c rhs after some type conversion.
4600bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
4610bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \note
4620bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * In addition to being used for assignments, this function is used to
4630bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * type-check return values.
4640bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick */
465fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
466336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholtvalidate_assignment(struct _mesa_glsl_parse_state *state,
467336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt		    const glsl_type *lhs_type, ir_rvalue *rhs)
4680bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick{
469336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *rhs_type = rhs->type;
4700bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
4710bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   /* If there is already some error in the RHS, just return it.  Anything
4720bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    * else will lead to an avalanche of error message back to the user.
4730bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    */
4740bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   if (rhs_type->is_error())
4750bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick      return rhs;
4760bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
4770bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   /* If the types are identical, the assignment can trivially proceed.
4780bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    */
4790bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   if (rhs_type == lhs_type)
4800bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick      return rhs;
4810bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
4820157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick   /* If the array element types are the same and the size of the LHS is zero,
4830157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    * the assignment is okay.
4840157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    *
4850157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
4860157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    * is handled by ir_dereference::is_lvalue.
4870157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    */
4880157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick   if (lhs_type->is_array() && rhs->type->is_array()
4890157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       && (lhs_type->element_type() == rhs->type->element_type())
4900157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       && (lhs_type->array_size() == 0)) {
4910157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      return rhs;
4920157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick   }
4930157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
494336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   /* Check for implicit conversion in GLSL 1.20 */
495336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   if (apply_implicit_conversion(lhs_type, rhs, state)) {
496336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt      rhs_type = rhs->type;
497336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt      if (rhs_type == lhs_type)
498336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt	 return rhs;
499336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   }
500336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt
5010bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   return NULL;
5020bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick}
5030bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
50410a685218610e737e23d2d8a243ed6ff6613becdEric Anholtir_rvalue *
50510a685218610e737e23d2d8a243ed6ff6613becdEric Anholtdo_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
50610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	      ir_rvalue *lhs, ir_rvalue *rhs,
50710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	      YYLTYPE lhs_loc)
50810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt{
509953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
51010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
51110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
51210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   if (!error_emitted) {
51310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      if (!lhs->is_lvalue()) {
51410a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	 _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
51510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	 error_emitted = true;
51610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      }
51710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   }
51810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
519336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   ir_rvalue *new_rhs = validate_assignment(state, lhs->type, rhs);
52010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   if (new_rhs == NULL) {
52110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      _mesa_glsl_error(& lhs_loc, state, "type mismatch");
52210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   } else {
52310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      rhs = new_rhs;
5240157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
5250157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      /* If the LHS array was not declared with a size, it takes it size from
5260157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       * the RHS.  If the LHS is an l-value and a whole array, it must be a
5270157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       * dereference of a variable.  Any other case would require that the LHS
5280157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       * is either not an l-value or not a whole array.
5290157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       */
5300157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      if (lhs->type->array_size() == 0) {
5310157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 ir_dereference *const d = lhs->as_dereference();
5320157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
5330157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 assert(d != NULL);
5340157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
53536ea28646c666ac2af9b43c47e65f9f53ffcc390Ian Romanick	 ir_variable *const var = d->variable_referenced();
5360157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
5370157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 assert(var != NULL);
5380157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
53963f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	 if (var->max_array_access >= unsigned(rhs->type->array_size())) {
54063f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	    /* FINISHME: This should actually log the location of the RHS. */
54163f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	    _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
54263f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick			     "previous access",
54363f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick			     var->max_array_access);
54463f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	 }
54563f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick
546f38d15b80d4e4c8ecb7a76087cdc49835f0aa271Ian Romanick	 var->type = glsl_type::get_array_instance(lhs->type->element_type(),
5470157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick						   rhs->type->array_size());
5489703ed05e684f4269cd8af27c94e9b6bf8781d85Eric Anholt	 d->type = var->type;
5490157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      }
55010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   }
55110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
5522731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt   /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
5532731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * but not post_inc) need the converted assigned value as an rvalue
5542731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * to handle things like:
5552731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    *
5562731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * i = j += 1;
5572731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    *
5582731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * So we always just store the computed value being assigned to a
5592731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * temporary and return a deref of that temporary.  If the rvalue
5602731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * ends up not being used, the temp will get copy-propagated out.
5612731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    */
5627e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick   ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp",
5637e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick					   ir_var_temporary);
564e33c10328caec29616a5433b1d1df9088f3a84dfEric Anholt   ir_dereference_variable *deref_var = new(ctx) ir_dereference_variable(var);
565ae805922b7e3cdaf3aee26c3b799fe3608669bbaEric Anholt   instructions->push_tail(var);
566e33c10328caec29616a5433b1d1df9088f3a84dfEric Anholt   instructions->push_tail(new(ctx) ir_assignment(deref_var,
5671660a2954797e056caba319c5d6c70b0d4be22feCarl Worth						  rhs,
5681660a2954797e056caba319c5d6c70b0d4be22feCarl Worth						  NULL));
569e33c10328caec29616a5433b1d1df9088f3a84dfEric Anholt   deref_var = new(ctx) ir_dereference_variable(var);
5702731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt
5718e9ce2eb56a087c2544112700ae1abe3f96648ddIan Romanick   if (!error_emitted)
5728e9ce2eb56a087c2544112700ae1abe3f96648ddIan Romanick      instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var, NULL));
57310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
5741660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   return new(ctx) ir_dereference_variable(var);
57510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt}
5760bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
577de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholtstatic ir_rvalue *
578959a9ecdd8fbc3375e4149f2b44d253622ff12eeEric Anholtget_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue)
579de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt{
5801660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   void *ctx = talloc_parent(lvalue);
581de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   ir_variable *var;
582de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
5837e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick   var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp",
5847e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick			      ir_var_temporary);
58543b5b03d67ce890e867c81d4a5cfc4871d711d43Eric Anholt   instructions->push_tail(var);
586de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   var->mode = ir_var_auto;
587de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
5881660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
5891660a2954797e056caba319c5d6c70b0d4be22feCarl Worth						  lvalue, NULL));
590de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
591de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   /* Once we've created this temporary, mark it read only so it's no
592de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt    * longer considered an lvalue.
593de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt    */
594de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   var->read_only = true;
595de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
5961660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   return new(ctx) ir_dereference_variable(var);
597de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt}
598de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
599de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
600fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
6010044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_node::hir(exec_list *instructions,
60218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick	      struct _mesa_glsl_parse_state *state)
60318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick{
60418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   (void) instructions;
60518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   (void) state;
60618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
60718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   return NULL;
60818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick}
60918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
61018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
611fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
6120044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_expression::hir(exec_list *instructions,
61318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick		    struct _mesa_glsl_parse_state *state)
614a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
615953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
616a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   static const int operations[AST_NUM_OPERATORS] = {
617a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_assign doesn't convert to ir_expression. */
618a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_plus doesn't convert to ir_expression. */
619a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_neg,
620a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_add,
621a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_sub,
622a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mul,
623a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_div,
624a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mod,
625a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lshift,
626a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_rshift,
627a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_less,
628a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_greater,
629a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lequal,
630a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_gequal,
631a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_equal,
632a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_nequal,
633a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_and,
634a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_xor,
635a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_or,
636a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_bit_not,
637a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_and,
638a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_xor,
639a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_or,
640a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_logic_not,
641a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
642a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Note: The following block of expression types actually convert
643a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * to multiple IR instructions.
644a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
645a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mul,     /* ast_mul_assign */
646a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_div,     /* ast_div_assign */
647a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mod,     /* ast_mod_assign */
648a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_add,     /* ast_add_assign */
649a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_sub,     /* ast_sub_assign */
650a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lshift,  /* ast_ls_assign */
651a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_rshift,  /* ast_rs_assign */
652a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_and, /* ast_and_assign */
653a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_xor, /* ast_xor_assign */
654a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_or,  /* ast_or_assign */
655a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
656a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_conditional doesn't convert to ir_expression. */
657de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_add,     /* ast_pre_inc. */
658de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_sub,     /* ast_pre_dec. */
659de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_add,     /* ast_post_inc. */
660de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_sub,     /* ast_post_dec. */
661a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_field_selection doesn't conv to ir_expression. */
662a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_array_index doesn't convert to ir_expression. */
663a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_function_call doesn't conv to ir_expression. */
664a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_identifier doesn't convert to ir_expression. */
665a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_int_constant doesn't convert to ir_expression. */
666a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_uint_constant doesn't conv to ir_expression. */
667a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_float_constant doesn't conv to ir_expression. */
668a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_bool_constant doesn't conv to ir_expression. */
669a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_sequence doesn't convert to ir_expression. */
670a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   };
671fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke   ir_rvalue *result = NULL;
6721c325af4d6b907e0a47ab7f868a2a78f054f153fAras Pranckevicius   ir_rvalue *op[3];
6730471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   const struct glsl_type *type = glsl_type::error_type;
674a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   bool error_emitted = false;
675a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   YYLTYPE loc;
676a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
67718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   loc = this->get_location();
678a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
67918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   switch (this->oper) {
6806652af36fe8994b1621d882fcc230d320908a2a3Ian Romanick   case ast_assign: {
68118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
68218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
683a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
68410a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      result = do_assignment(instructions, state, op[0], op[1],
68510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt			     this->subexpressions[0]->get_location());
68610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      error_emitted = result->type->is_error();
68710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      type = result->type;
688a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
6896652af36fe8994b1621d882fcc230d320908a2a3Ian Romanick   }
690a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
691a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_plus:
69218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
693a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
694c24bcad9f88379ffba9e2f0ff92f22cdf60c2927Carl Worth      type = unary_arithmetic_result_type(op[0]->type, state, & loc);
695c24bcad9f88379ffba9e2f0ff92f22cdf60c2927Carl Worth
696c24bcad9f88379ffba9e2f0ff92f22cdf60c2927Carl Worth      error_emitted = type->is_error();
697a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
698a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      result = op[0];
699a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
700a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
701a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_neg:
70218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
703a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
70465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = unary_arithmetic_result_type(op[0]->type, state, & loc);
705a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
70665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
707a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
7081660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
7091660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], NULL);
710a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
711a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
712a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_add:
713a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sub:
714a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mul:
715a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_div:
71618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
71718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
718a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
719bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      type = arithmetic_result_type(op[0], op[1],
72018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick				    (this->oper == ast_mul),
721a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt				    state, & loc);
722a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      error_emitted = type->is_error();
723a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
7241660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
7251660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
726a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
727a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
728a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mod:
72918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
73018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
731a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
73265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
733a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
73418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      assert(operations[this->oper] == ir_binop_mod);
735a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
7361660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
7371660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
73865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
739a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
740a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
741a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_lshift:
742a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_rshift:
743183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      _mesa_glsl_error(& loc, state, "FINISHME: implement bit-shift operators");
744183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      error_emitted = true;
745a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
746a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
747a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_less:
748a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_greater:
749a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_lequal:
750a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_gequal:
75118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
75218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
753a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
75465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = relational_result_type(op[0], op[1], state, & loc);
755a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
756a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* The relational operators must either generate an error or result
757a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * in a scalar boolean.  See page 57 of the GLSL 1.50 spec.
758a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
759a43817a483a8c4a480ef4e6dfda2cef899300eb0Ian Romanick      assert(type->is_error()
760a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	     || ((type->base_type == GLSL_TYPE_BOOL)
761cb36f8aaeeb09660843316270a781948f773d90bIan Romanick		 && type->is_scalar()));
762a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
7631660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
7641660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
76565e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
766a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
767a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
768a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_nequal:
769a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_equal:
7706e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
7716e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
7726e659caaa946339a2de3890a8bed091ccb65102aIan Romanick
7736e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
7746e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *
7756e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    "The equality operators equal (==), and not equal (!=)
7766e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    operate on all types. They result in a scalar Boolean. If
7776e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    the operand types do not match, then there must be a
7786e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    conversion from Section 4.1.10 "Implicit Conversions"
7796e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    applied to one operand that can make them match, in which
7806e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    case this conversion is done."
7816e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       */
782bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      if ((!apply_implicit_conversion(op[0]->type, op[1], state)
783bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick	   && !apply_implicit_conversion(op[1]->type, op[0], state))
784212b0327b47033442842a7be3d7fb10e08e2bf66Ian Romanick	  || (op[0]->type != op[1]->type)) {
7856e659caaa946339a2de3890a8bed091ccb65102aIan Romanick	 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
7866e659caaa946339a2de3890a8bed091ccb65102aIan Romanick			  "type", (this->oper == ast_equal) ? "==" : "!=");
7876e659caaa946339a2de3890a8bed091ccb65102aIan Romanick	 error_emitted = true;
788a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick      } else if ((state->language_version <= 110)
789a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick		 && (op[0]->type->is_array() || op[1]->type->is_array())) {
790a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick	 _mesa_glsl_error(& loc, state, "array comparisons forbidden in "
791a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick			  "GLSL 1.10");
792a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick	 error_emitted = true;
7936e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      }
7946e659caaa946339a2de3890a8bed091ccb65102aIan Romanick
7951660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
7961660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
7976e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      type = glsl_type::bool_type;
7986e659caaa946339a2de3890a8bed091ccb65102aIan Romanick
7996e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      assert(result->type == glsl_type::bool_type);
800a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
801a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
802a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_and:
803a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_xor:
804a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_or:
8051eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      op[0] = this->subexpressions[0]->hir(instructions, state);
8061eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      op[1] = this->subexpressions[1]->hir(instructions, state);
8071eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
8081eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      if (state->language_version < 130) {
8091eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 _mesa_glsl_error(&loc, state, "bit-wise operations require GLSL 1.30");
8101eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 error_emitted = true;
8111eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      }
8121eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
8131eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      if (!op[0]->type->is_integer()) {
8141eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 _mesa_glsl_error(&loc, state, "LHS of `%s' must be an integer",
8151eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke			  operator_string(this->oper));
8161eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 error_emitted = true;
8171eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      }
8181eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
8191eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      if (!op[1]->type->is_integer()) {
8201eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 _mesa_glsl_error(&loc, state, "RHS of `%s' must be an integer",
8211eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke			  operator_string(this->oper));
8221eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 error_emitted = true;
8231eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      }
8241eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
8251eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      if (op[0]->type->base_type != op[1]->type->base_type) {
8261eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 _mesa_glsl_error(&loc, state, "operands of `%s' must have the same "
8271eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke			  "base type", operator_string(this->oper));
8281eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 error_emitted = true;
8291eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      }
8301eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
8311eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      if (op[0]->type->is_vector() && op[1]->type->is_vector()
8321eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	  && op[0]->type->vector_elements != op[1]->type->vector_elements) {
8331eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 _mesa_glsl_error(&loc, state, "operands of `%s' cannot be vectors of "
8341eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke			  "different sizes", operator_string(this->oper));
8351eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 error_emitted = true;
8361eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      }
8371eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
8381eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      type = op[0]->type->is_scalar() ? op[1]->type : op[0]->type;
8391eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      result = new(ctx) ir_expression(operations[this->oper], type,
8401eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke				      op[0], op[1]);
8411eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
8421eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      break;
8431eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
844a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_not:
8451eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      op[0] = this->subexpressions[0]->hir(instructions, state);
8461eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
8471eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      if (state->language_version < 130) {
8481eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 _mesa_glsl_error(&loc, state, "bit-wise operations require GLSL 1.30");
8491eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 error_emitted = true;
8501eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      }
8511eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
8521eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      if (!op[0]->type->is_integer()) {
8531eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 _mesa_glsl_error(&loc, state, "operand of `~' must be an integer");
8541eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 error_emitted = true;
8551eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      }
8561eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
8571eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      type = op[0]->type;
8581eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      result = new(ctx) ir_expression(ir_unop_bit_not, type, op[0], NULL);
859a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
860a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
8614950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   case ast_logic_and: {
862b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
863b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
864b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
865b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt	 YYLTYPE loc = this->subexpressions[0]->get_location();
866b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
867b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt	 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
868b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt			  operator_string(this->oper));
869ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt	 error_emitted = true;
870b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      }
871b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
87244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      ir_constant *op0_const = op[0]->constant_expression_value();
87344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      if (op0_const) {
87444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 if (op0_const->value.b[0]) {
87544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    op[1] = this->subexpressions[1]->hir(instructions, state);
87644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt
87744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
87844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       YYLTYPE loc = this->subexpressions[1]->get_location();
87944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt
88044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       _mesa_glsl_error(& loc, state,
88144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt				"RHS of `%s' must be scalar boolean",
88244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt				operator_string(this->oper));
88344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       error_emitted = true;
88444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    }
88544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    result = op[1];
88644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 } else {
88744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    result = op0_const;
88844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 }
88944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 type = glsl_type::bool_type;
89044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      } else {
89181d664f099a5fd5fac777480532fb4307d591451Ian Romanick	 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
8927e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick						       "and_tmp",
8937e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick						       ir_var_temporary);
89481d664f099a5fd5fac777480532fb4307d591451Ian Romanick	 instructions->push_tail(tmp);
89581d664f099a5fd5fac777480532fb4307d591451Ian Romanick
8961660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_if *const stmt = new(ctx) ir_if(op[0]);
89744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 instructions->push_tail(stmt);
8984950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
89944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 op[1] = this->subexpressions[1]->hir(&stmt->then_instructions, state);
9004950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
90144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
90244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    YYLTYPE loc = this->subexpressions[1]->get_location();
9034950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
90444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    _mesa_glsl_error(& loc, state,
90544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt			     "RHS of `%s' must be scalar boolean",
90644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt			     operator_string(this->oper));
90744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    error_emitted = true;
90844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 }
909b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
9101660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
91144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const then_assign =
9121660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(then_deref, op[1], NULL);
91344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->then_instructions.push_tail(then_assign);
9144950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9151660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
91644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const else_assign =
9171660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false), NULL);
91844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->else_instructions.push_tail(else_assign);
9194950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9201660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 result = new(ctx) ir_dereference_variable(tmp);
92144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 type = tmp->type;
92244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      }
9234950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      break;
9244950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   }
9254950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9264950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   case ast_logic_or: {
9274950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
9284950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9294950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
9304950a68bf22ede6f4f368c9783e5401816159574Eric Anholt	 YYLTYPE loc = this->subexpressions[0]->get_location();
9314950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9324950a68bf22ede6f4f368c9783e5401816159574Eric Anholt	 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
9334950a68bf22ede6f4f368c9783e5401816159574Eric Anholt			  operator_string(this->oper));
9344950a68bf22ede6f4f368c9783e5401816159574Eric Anholt	 error_emitted = true;
9354950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      }
9364950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
93744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      ir_constant *op0_const = op[0]->constant_expression_value();
93844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      if (op0_const) {
93944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 if (op0_const->value.b[0]) {
94044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    result = op0_const;
94144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 } else {
94244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    op[1] = this->subexpressions[1]->hir(instructions, state);
94344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt
94444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
94544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       YYLTYPE loc = this->subexpressions[1]->get_location();
94644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt
94744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       _mesa_glsl_error(& loc, state,
94844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt				"RHS of `%s' must be scalar boolean",
94944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt				operator_string(this->oper));
95044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       error_emitted = true;
95144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    }
95244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    result = op[1];
95344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 }
95444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 type = glsl_type::bool_type;
95544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      } else {
956dfd30ca6a95a7d95835dad78ffe1fba4d1f4ef69Kenneth Graunke	 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
9577e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick						       "or_tmp",
9587e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick						       ir_var_temporary);
9590b9ae3befb0bf80e000b159fd44c961a144f9c36Ian Romanick	 instructions->push_tail(tmp);
9604950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
96181d664f099a5fd5fac777480532fb4307d591451Ian Romanick	 ir_if *const stmt = new(ctx) ir_if(op[0]);
96281d664f099a5fd5fac777480532fb4307d591451Ian Romanick	 instructions->push_tail(stmt);
96381d664f099a5fd5fac777480532fb4307d591451Ian Romanick
964a0879b9dd438d78635f047cdd5ed4c72bc831b60Eric Anholt	 op[1] = this->subexpressions[1]->hir(&stmt->else_instructions, state);
9654950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
96644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
96744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    YYLTYPE loc = this->subexpressions[1]->get_location();
9684950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
96944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    _mesa_glsl_error(& loc, state, "RHS of `%s' must be scalar boolean",
97044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt			     operator_string(this->oper));
97144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    error_emitted = true;
97244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 }
9734950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9741660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
97544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const then_assign =
9761660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true), NULL);
97744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->then_instructions.push_tail(then_assign);
9784950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9791660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
98044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const else_assign =
9811660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(else_deref, op[1], NULL);
98244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->else_instructions.push_tail(else_assign);
9834950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9841660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 result = new(ctx) ir_dereference_variable(tmp);
98544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 type = tmp->type;
98644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      }
9874950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      break;
9884950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   }
9894950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9904950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   case ast_logic_xor:
9914950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
9924950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      op[1] = this->subexpressions[1]->hir(instructions, state);
9934950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9944950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9951660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
9961660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
997ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt      type = glsl_type::bool_type;
998a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
999a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1000a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt   case ast_logic_not:
1001a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
1002a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
1003a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
1004a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt	 YYLTYPE loc = this->subexpressions[0]->get_location();
1005a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
1006a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt	 _mesa_glsl_error(& loc, state,
1007a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt			  "operand of `!' must be scalar boolean");
1008ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt	 error_emitted = true;
1009a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      }
1010a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
10111660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
10121660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], NULL);
1013ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt      type = glsl_type::bool_type;
1014a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      break;
1015a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
1016a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mul_assign:
1017a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_div_assign:
1018a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_add_assign:
1019a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sub_assign: {
102018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
102118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
1022a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1023bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      type = arithmetic_result_type(op[0], op[1],
102418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick				    (this->oper == ast_mul_assign),
1025a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt				    state, & loc);
1026a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
10271660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
10281660a2954797e056caba319c5d6c70b0d4be22feCarl Worth						   op[0], op[1]);
1029a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
10303e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      result = do_assignment(instructions, state,
10318273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt			     op[0]->clone(ctx, NULL), temp_rhs,
103210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt			     this->subexpressions[0]->get_location());
103310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      type = result->type;
103410a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      error_emitted = (op[0]->type->is_error());
1035a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1036a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* GLSL 1.10 does not allow array assignment.  However, we don't have to
1037a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * explicitly test for this because none of the binary expression
1038a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * operators allow array operands either.
1039a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1040a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1041a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1042a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1043a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
104448a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt   case ast_mod_assign: {
104548a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
104648a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      op[1] = this->subexpressions[1]->hir(instructions, state);
104748a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
104865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
104948a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
105048a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      assert(operations[this->oper] == ir_binop_mod);
105148a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
1052768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick      ir_rvalue *temp_rhs;
10531660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
10541660a2954797e056caba319c5d6c70b0d4be22feCarl Worth					op[0], op[1]);
105548a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
10563e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      result = do_assignment(instructions, state,
10578273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt			     op[0]->clone(ctx, NULL), temp_rhs,
105848a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt			     this->subexpressions[0]->get_location());
105948a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      type = result->type;
106065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
106148a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      break;
106248a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt   }
1063a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1064a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_ls_assign:
1065a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_rs_assign:
1066183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      _mesa_glsl_error(& loc, state,
1067183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt		       "FINISHME: implement bit-shift assignment operators");
1068183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      error_emitted = true;
1069251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
1070a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1071a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_and_assign:
1072a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_xor_assign:
1073a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_or_assign:
1074183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      _mesa_glsl_error(& loc, state,
1075183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt		       "FINISHME: implement logic assignment operators");
1076183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      error_emitted = true;
1077251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
1078a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
107996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick   case ast_conditional: {
108096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
108196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
108296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
108396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *
108496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    "The ternary selection operator (?:). It operates on three
108596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    expressions (exp1 ? exp2 : exp3). This operator evaluates the
108696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    first expression, which must result in a scalar Boolean."
108796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
108896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
108996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 YYLTYPE loc = this->subexpressions[0]->get_location();
109096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
109196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 _mesa_glsl_error(& loc, state, "?: condition must be scalar boolean");
109296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 error_emitted = true;
109396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      }
109496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
109596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* The :? operator is implemented by generating an anonymous temporary
109696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * followed by an if-statement.  The last instruction in each branch of
109796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * the if-statement assigns a value to the anonymous temporary.  This
109896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * temporary is the r-value of the expression.
109996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
11000ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      exec_list then_instructions;
11010ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      exec_list else_instructions;
110296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
11030ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      op[1] = this->subexpressions[1]->hir(&then_instructions, state);
11040ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      op[2] = this->subexpressions[2]->hir(&else_instructions, state);
110596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
110696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
110796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *
110896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     "The second and third expressions can be any type, as
110996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     long their types match, or there is a conversion in
111096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     Section 4.1.10 "Implicit Conversions" that can be applied
111196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     to one of the expressions to make their types match. This
111296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     resulting matching type is the type of the entire
111396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     expression."
111496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
1115bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      if ((!apply_implicit_conversion(op[1]->type, op[2], state)
1116bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick	   && !apply_implicit_conversion(op[2]->type, op[1], state))
1117db9be2e7aa3a56e43b725ad7725fe6b424e4933eIan Romanick	  || (op[1]->type != op[2]->type)) {
111896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 YYLTYPE loc = this->subexpressions[1]->get_location();
111996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
112096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
112196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick			  "operator must have matching types.");
112296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 error_emitted = true;
11230ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick	 type = glsl_type::error_type;
1124db9be2e7aa3a56e43b725ad7725fe6b424e4933eIan Romanick      } else {
11250ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick	 type = op[1]->type;
112696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      }
112796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
11287825d3d15710fdfcfc503754862963aac8065480Ian Romanick      ir_constant *cond_val = op[0]->constant_expression_value();
11297825d3d15710fdfcfc503754862963aac8065480Ian Romanick      ir_constant *then_val = op[1]->constant_expression_value();
11307825d3d15710fdfcfc503754862963aac8065480Ian Romanick      ir_constant *else_val = op[2]->constant_expression_value();
11317825d3d15710fdfcfc503754862963aac8065480Ian Romanick
11327825d3d15710fdfcfc503754862963aac8065480Ian Romanick      if (then_instructions.is_empty()
11337825d3d15710fdfcfc503754862963aac8065480Ian Romanick	  && else_instructions.is_empty()
11347825d3d15710fdfcfc503754862963aac8065480Ian Romanick	  && (cond_val != NULL) && (then_val != NULL) && (else_val != NULL)) {
11357825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 result = (cond_val->value.b[0]) ? then_val : else_val;
11367825d3d15710fdfcfc503754862963aac8065480Ian Romanick      } else {
11377e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick	 ir_variable *const tmp =
11387e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick	    new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary);
11390b9ae3befb0bf80e000b159fd44c961a144f9c36Ian Romanick	 instructions->push_tail(tmp);
11400ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
11411660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_if *const stmt = new(ctx) ir_if(op[0]);
11427825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 instructions->push_tail(stmt);
11430ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
11447825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 then_instructions.move_nodes_to(& stmt->then_instructions);
11451660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const then_deref =
11461660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_dereference_variable(tmp);
11477825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 ir_assignment *const then_assign =
11481660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(then_deref, op[1], NULL);
11497825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 stmt->then_instructions.push_tail(then_assign);
11500ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
11517825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 else_instructions.move_nodes_to(& stmt->else_instructions);
11521660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const else_deref =
11531660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_dereference_variable(tmp);
11547825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 ir_assignment *const else_assign =
11551660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(else_deref, op[2], NULL);
11567825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 stmt->else_instructions.push_tail(else_assign);
11570ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
11581660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 result = new(ctx) ir_dereference_variable(tmp);
11597825d3d15710fdfcfc503754862963aac8065480Ian Romanick      }
1160251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
116196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick   }
1162a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1163a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_pre_inc:
116476ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt   case ast_pre_dec: {
116576ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
116676ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
11671660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 op[1] = new(ctx) ir_constant(1.0f);
116876ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      else
11691660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 op[1] = new(ctx) ir_constant(1);
117076ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
1171a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      type = arithmetic_result_type(op[0], op[1], false, state, & loc);
117276ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
1173768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick      ir_rvalue *temp_rhs;
11741660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
11751660a2954797e056caba319c5d6c70b0d4be22feCarl Worth					op[0], op[1]);
117676ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
11773e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      result = do_assignment(instructions, state,
11788273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt			     op[0]->clone(ctx, NULL), temp_rhs,
117976ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt			     this->subexpressions[0]->get_location());
118076ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      type = result->type;
118176ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      error_emitted = op[0]->type->is_error();
118276ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      break;
118376ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt   }
1184a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1185a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_post_inc:
1186de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   case ast_post_dec: {
1187de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
1188de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
11891660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 op[1] = new(ctx) ir_constant(1.0f);
1190de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      else
11911660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 op[1] = new(ctx) ir_constant(1);
1192de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1193de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1194de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1195a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1196de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1197768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick      ir_rvalue *temp_rhs;
11981660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
11991660a2954797e056caba319c5d6c70b0d4be22feCarl Worth					op[0], op[1]);
1200de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1201de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      /* Get a temporary of a copy of the lvalue before it's modified.
1202de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt       * This may get thrown away later.
1203de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt       */
12048273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL));
1205de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
12063e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      (void)do_assignment(instructions, state,
12078273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt			  op[0]->clone(ctx, NULL), temp_rhs,
1208de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt			  this->subexpressions[0]->get_location());
1209de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1210de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      type = result->type;
1211de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      error_emitted = op[0]->type->is_error();
1212a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1213de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   }
1214a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1215a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_field_selection:
121618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = _mesa_ast_field_selection_to_hir(this, instructions, state);
1217a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = result->type;
1218a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1219a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
122027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick   case ast_array_index: {
122127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      YYLTYPE index_loc = subexpressions[1]->get_location();
122227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
122327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      op[0] = subexpressions[0]->hir(instructions, state);
122427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      op[1] = subexpressions[1]->hir(instructions, state);
122527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
122627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
122727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
1228a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick      ir_rvalue *const array = op[0];
1229b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
12301660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_dereference_array(op[0], op[1]);
1231b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
1232b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      /* Do not use op[0] after this point.  Use array.
1233b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick       */
1234b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      op[0] = NULL;
1235b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
123627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
123727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (error_emitted)
123827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 break;
123927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
124063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick      if (!array->type->is_array()
124163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	  && !array->type->is_matrix()
124263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	  && !array->type->is_vector()) {
124327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
124463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			  "cannot dereference non-array / non-matrix / "
124563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			  "non-vector");
124627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
124727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
124827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
124927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (!op[1]->type->is_integer()) {
125027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
125127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick			  "array index must be integer type");
125227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
125327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      } else if (!op[1]->type->is_scalar()) {
125427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
125527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick			  "array index must be scalar");
125627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
125727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
125827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
125927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      /* If the array index is a constant expression and the array has a
126027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * declared size, ensure that the access is in-bounds.  If the array
126127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * index is not a constant expression, ensure that the array has a
126227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * declared size.
126327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       */
126427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      ir_constant *const const_index = op[1]->constant_expression_value();
126527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (const_index != NULL) {
126627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 const int idx = const_index->value.i[0];
126763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 const char *type_name;
126863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 unsigned bound = 0;
126963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick
127063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (array->type->is_matrix()) {
127163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    type_name = "matrix";
127263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else if (array->type->is_vector()) {
127363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    type_name = "vector";
127463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else {
127563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    type_name = "array";
127663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 }
127727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
127827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
127927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *
128027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    "It is illegal to declare an array with a size, and then
128127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    later (in the same shader) index the same array with an
128227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    integral constant expression greater than or equal to the
128327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    declared size. It is also illegal to index an array with a
128427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    negative constant expression."
128527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  */
128663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (array->type->is_matrix()) {
128763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if (array->type->row_type()->vector_elements <= idx) {
128863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       bound = array->type->row_type()->vector_elements;
128963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    }
129063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else if (array->type->is_vector()) {
129163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if (array->type->vector_elements <= idx) {
129263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       bound = array->type->vector_elements;
129363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    }
129463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else {
129563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if ((array->type->array_size() > 0)
129663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick		&& (array->type->array_size() <= idx)) {
129763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       bound = array->type->array_size();
129863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    }
129927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 }
130027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
130163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (bound > 0) {
130263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    _mesa_glsl_error(& loc, state, "%s index must be < %u",
130363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			     type_name, bound);
130463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    error_emitted = true;
130563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else if (idx < 0) {
130663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    _mesa_glsl_error(& loc, state, "%s index must be >= 0",
130763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			     type_name);
130827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	    error_emitted = true;
130927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 }
1310b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
131163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (array->type->is_array()) {
1312a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	    /* If the array is a variable dereference, it dereferences the
1313a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     * whole array, by definition.  Use this to get the variable.
1314a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     *
1315a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     * FINISHME: Should some methods for getting / setting / testing
1316a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     * FINISHME: array access limits be added to ir_dereference?
1317a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     */
1318a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	    ir_variable *const v = array->whole_variable_referenced();
131963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if ((v != NULL) && (unsigned(idx) > v->max_array_access))
132063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       v->max_array_access = idx;
132163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 }
13222b7c42b40ae459f7b290eb134d6dabd075aab9f0Kenneth Graunke      } else if (array->type->array_size() == 0) {
13232b7c42b40ae459f7b290eb134d6dabd075aab9f0Kenneth Graunke	 _mesa_glsl_error(&loc, state, "unsized array index must be constant");
1324a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt      } else {
1325a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt	 if (array->type->is_array()) {
13265226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	    /* whole_variable_referenced can return NULL if the array is a
13275226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	     * member of a structure.  In this case it is safe to not update
13285226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	     * the max_array_access field because it is never used for fields
13295226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	     * of structures.
13305226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	     */
1331a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt	    ir_variable *v = array->whole_variable_referenced();
13325226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	    if (v != NULL)
13335226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	       v->max_array_access = array->type->array_size();
1334a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt	 }
133527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
133627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
133727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (error_emitted)
133827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 result->type = glsl_type::error_type;
133927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
134027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      type = result->type;
1341a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
134227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick   }
1343a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1344a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_function_call:
13457cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick      /* Should *NEVER* get here.  ast_function_call should always be handled
13467cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick       * by ast_function_expression::hir.
1347a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
13487cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick      assert(0);
1349a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1350a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1351a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_identifier: {
1352a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* ast_identifier can appear several places in a full abstract syntax
1353a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * tree.  This particular use must be at location specified in the grammar
1354a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * as 'variable_identifier'.
1355a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
13568bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      ir_variable *var =
13578bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick	 state->symbols->get_variable(this->primary_expression.identifier);
1358a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
13591660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_dereference_variable(var);
1360a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1361a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (var != NULL) {
1362a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 type = result->type;
1363a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
136471d0bbfcb2853f37b580ec7b705e55bb0eb426faIan Romanick	 _mesa_glsl_error(& loc, state, "`%s' undeclared",
136518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  this->primary_expression.identifier);
1366a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1367a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 error_emitted = true;
1368a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1369a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1370a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1371a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1372a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_int_constant:
13730471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::int_type;
13741660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(this->primary_expression.int_constant);
1375a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1376a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1377a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_uint_constant:
13780471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::uint_type;
13791660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(this->primary_expression.uint_constant);
1380a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1381a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1382a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_float_constant:
13830471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::float_type;
13841660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(this->primary_expression.float_constant);
1385a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1386a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1387a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bool_constant:
13880471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::bool_type;
13891660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant));
1390a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1391a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1392a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sequence: {
1393a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* It should not be possible to generate a sequence in the AST without
1394a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * any expressions in it.
1395a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1396304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      assert(!this->expressions.is_empty());
1397a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1398a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* The r-value of a sequence is the last expression in the sequence.  If
1399a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * the other expressions in the sequence do not have side-effects (and
1400a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * therefore add instructions to the instruction list), they get dropped
1401a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * on the floor.
1402a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
14032b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick      foreach_list_typed (ast_node, ast, link, &this->expressions)
1404304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick	 result = ast->hir(instructions, state);
1405a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1406a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = result->type;
1407a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1408a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Any errors should have already been emitted in the loop above.
1409a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1410a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      error_emitted = true;
1411a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1412a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1413a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1414a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1415cef3baecf636a30b62cd7a1e8de57c7650f7003eIan Romanick   if (type->is_error() && !error_emitted)
141671d0bbfcb2853f37b580ec7b705e55bb0eb426faIan Romanick      _mesa_glsl_error(& loc, state, "type mismatch");
1417a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1418a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return result;
1419a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1420a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1421a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1422fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
14230044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_expression_statement::hir(exec_list *instructions,
142418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			      struct _mesa_glsl_parse_state *state)
1425a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1426a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* It is possible to have expression statements that don't have an
1427a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * expression.  This is the solitary semicolon:
1428a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
1429a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * for (i = 0; i < 5; i++)
1430a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     ;
1431a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
1432a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * In this case the expression will be NULL.  Test for NULL and don't do
1433a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * anything in that case.
1434a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
143518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (expression != NULL)
143618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      expression->hir(instructions, state);
1437a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1438a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Statements do not have r-values.
1439a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1440a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1441a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1442a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1443a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1444fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
14450044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_compound_statement::hir(exec_list *instructions,
144618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			    struct _mesa_glsl_parse_state *state)
1447a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
144818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (new_scope)
14498bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      state->symbols->push_scope();
1450a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
14512b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_node, ast, link, &this->statements)
1452304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      ast->hir(instructions, state);
1453a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
145418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (new_scope)
14558bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      state->symbols->pop_scope();
1456a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1457a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Compound statements do not have r-values.
1458a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1459a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1460a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1461a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1462a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
146328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanickstatic const glsl_type *
146428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanickprocess_array_type(const glsl_type *base, ast_node *array_size,
146528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick		   struct _mesa_glsl_parse_state *state)
146628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick{
146728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   unsigned length = 0;
146828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
146928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   /* FINISHME: Reject delcarations of multidimensional arrays. */
147028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
147128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   if (array_size != NULL) {
147228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      exec_list dummy_instructions;
147328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
147428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      YYLTYPE loc = array_size->get_location();
147528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
147628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      /* FINISHME: Verify that the grammar forbids side-effects in array
147728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick       * FINISHME: sizes.   i.e., 'vec4 [x = 12] data'
147828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick       */
147928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      assert(dummy_instructions.is_empty());
148028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
148128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      if (ir != NULL) {
148228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 if (!ir->type->is_integer()) {
148328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    _mesa_glsl_error(& loc, state, "array size must be integer type");
148428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 } else if (!ir->type->is_scalar()) {
148528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    _mesa_glsl_error(& loc, state, "array size must be scalar type");
148628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 } else {
148728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    ir_constant *const size = ir->constant_expression_value();
148828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
148928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    if (size == NULL) {
149028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       _mesa_glsl_error(& loc, state, "array size must be a "
149128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick				"constant valued expression");
149228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    } else if (size->value.i[0] <= 0) {
149328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       _mesa_glsl_error(& loc, state, "array size must be > 0");
149428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    } else {
149528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       assert(size->type == ir->type);
149628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       length = size->value.u[0];
149728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    }
149828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 }
149928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      }
150028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   }
150128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
1502f38d15b80d4e4c8ecb7a76087cdc49835f0aa271Ian Romanick   return glsl_type::get_array_instance(base, length);
150328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick}
150428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
150528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
1506d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanickconst glsl_type *
1507d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanickast_type_specifier::glsl_type(const char **name,
1508d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick			      struct _mesa_glsl_parse_state *state) const
1509a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1510d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   const struct glsl_type *type;
1511a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
15123455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   if ((this->type_specifier == ast_struct) && (this->type_name == NULL)) {
1513a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* FINISHME: Handle annonymous structures. */
1514a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = NULL;
1515a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
1516d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick      type = state->symbols->get_type(this->type_name);
1517d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick      *name = this->type_name;
1518a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1519d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick      if (this->is_array) {
1520d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick	 type = process_array_type(type, this->array_size, state);
152128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      }
1522a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1523a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1524a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return type;
1525a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1526a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1527a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1528a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic void
1529a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickapply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
1530768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick				 ir_variable *var,
15312e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				 struct _mesa_glsl_parse_state *state,
15322e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				 YYLTYPE *loc)
1533a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1534a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->invariant)
1535a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->invariant = 1;
1536a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1537a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* FINISHME: Mark 'in' variables at global scope as read-only. */
1538a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->constant || qual->attribute || qual->uniform
1539a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       || (qual->varying && (state->target == fragment_shader)))
1540a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->read_only = 1;
1541a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1542a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->centroid)
1543a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->centroid = 1;
1544a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1545ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick   if (qual->attribute && state->target != vertex_shader) {
15462e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      var->type = glsl_type::error_type;
15472e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      _mesa_glsl_error(loc, state,
15482e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt		       "`attribute' variables may not be declared in the "
1549ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick		       "%s shader",
1550ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick		       _mesa_glsl_shader_target_name(state->target));
15512e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   }
15522e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt
155390b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt   /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
155490b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *
155590b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     "The varying qualifier can be used only with the data types
155690b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
155790b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     these."
155890b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    */
15590ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt   if (qual->varying) {
15600ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      const glsl_type *non_array_type;
15610ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt
15620ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      if (var->type && var->type->is_array())
15630ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 non_array_type = var->type->fields.array;
15640ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      else
15650ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 non_array_type = var->type;
15660ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt
15670ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      if (non_array_type && non_array_type->base_type != GLSL_TYPE_FLOAT) {
15680ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 var->type = glsl_type::error_type;
15690ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 _mesa_glsl_error(loc, state,
15700ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt			  "varying variables must be of base type float");
15710ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      }
157290b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt   }
157390b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt
15747e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick   /* If there is no qualifier that changes the mode of the variable, leave
15757e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick    * the setting alone.
15767e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick    */
1577a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->in && qual->out)
1578a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_inout;
1579a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else if (qual->attribute || qual->in
1580a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    || (qual->varying && (state->target == fragment_shader)))
1581a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_in;
15820b678234625fac67a89285ad2871dedc891fb1b1Ian Romanick   else if (qual->out || (qual->varying && (state->target == vertex_shader)))
1583a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_out;
1584a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else if (qual->uniform)
1585a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_uniform;
1586a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1587a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->flat)
1588a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->interpolation = ir_var_flat;
1589a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else if (qual->noperspective)
1590a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->interpolation = ir_var_noperspective;
1591a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else
1592a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->interpolation = ir_var_smooth;
15939d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick
15944a962170d7cf4243d6ae156fca20a6167388925dEric Anholt   var->pixel_center_integer = qual->pixel_center_integer;
15954a962170d7cf4243d6ae156fca20a6167388925dEric Anholt   var->origin_upper_left = qual->origin_upper_left;
15968d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick   if ((qual->origin_upper_left || qual->pixel_center_integer)
15978d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick       && (strcmp(var->name, "gl_FragCoord") != 0)) {
15988d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick      const char *const qual_string = (qual->origin_upper_left)
15998d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick	 ? "origin_upper_left" : "pixel_center_integer";
16008d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick
16018d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick      _mesa_glsl_error(loc, state,
16028d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick		       "layout qualifier `%s' can only be applied to "
16038d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick		       "fragment shader input `gl_FragCoord'",
16048d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick		       qual_string);
16058d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick   }
16068d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick
16079d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick   if (var->type->is_array() && (state->language_version >= 120)) {
16089d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick      var->array_lvalue = true;
16099d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick   }
1610a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1611a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1612a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1613fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
16140044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_declarator_list::hir(exec_list *instructions,
161518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			 struct _mesa_glsl_parse_state *state)
1616a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1617953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
1618a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const struct glsl_type *decl_type;
1619a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const char *type_name = NULL;
16208558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt   ir_rvalue *result = NULL;
1621c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick   YYLTYPE loc = this->get_location();
1622a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
16236f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
16246f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *
16256f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *     "To ensure that a particular output variable is invariant, it is
16266f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *     necessary to use the invariant qualifier. It can either be used to
16276f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *     qualify a previously declared variable as being invariant
16286f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *
16296f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *         invariant gl_Position; // make existing gl_Position be invariant"
16306f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *
16316f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    * In these cases the parser will set the 'invariant' flag in the declarator
16326f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    * list, and the type will be NULL.
16336f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    */
16346f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   if (this->invariant) {
16356f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      assert(this->type == NULL);
16366f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
16376f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      if (state->current_function != NULL) {
16386f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 _mesa_glsl_error(& loc, state,
16396f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			  "All uses of `invariant' keyword must be at global "
16406f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			  "scope\n");
16416f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      }
16426f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
16436f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
16446f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 assert(!decl->is_array);
16456f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 assert(decl->array_size == NULL);
16466f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 assert(decl->initializer == NULL);
16476f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
16486f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 ir_variable *const earlier =
16496f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    state->symbols->get_variable(decl->identifier);
16506f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 if (earlier == NULL) {
16516f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
16526f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "Undeclared variable `%s' cannot be marked "
16536f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "invariant\n", decl->identifier);
16546f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 } else if ((state->target == vertex_shader)
16556f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	       && (earlier->mode != ir_var_out)) {
16566f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
16576f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "`%s' cannot be marked invariant, vertex shader "
16586f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "outputs only\n", decl->identifier);
16596f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 } else if ((state->target == fragment_shader)
16606f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	       && (earlier->mode != ir_var_in)) {
16616f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
16626f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "`%s' cannot be marked invariant, fragment shader "
16636f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "inputs only\n", decl->identifier);
16646f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 } else {
16656f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    earlier->invariant = true;
16666f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 }
16676f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      }
16686f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
16696f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      /* Invariant redeclarations do not have r-values.
16706f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick       */
16716f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      return NULL;
16726f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   }
16736f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
16746f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   assert(this->type != NULL);
16756f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   assert(!this->invariant);
16766f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
16773455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* The type specifier may contain a structure definition.  Process that
16783455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * before any of the variable declarations.
16793455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
16803455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   (void) this->type->specifier->hir(instructions, state);
16813455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
1682d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   decl_type = this->type->specifier->glsl_type(& type_name, state);
1683304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick   if (this->declarations.is_empty()) {
16846f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      /* The only valid case where the declaration list can be empty is when
16856f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick       * the declaration is setting the default precision of a built-in type
16866f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick       * (e.g., 'precision highp vec4;').
1687c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick       */
1688c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick
16896f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      if (decl_type != NULL) {
1690c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick      } else {
1691c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick	    _mesa_glsl_error(& loc, state, "incomplete declaration");
1692c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick      }
1693c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick   }
1694a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
16952b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
1696a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      const struct glsl_type *var_type;
1697768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick      ir_variable *var;
1698a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1699a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* FINISHME: Emit a warning if a variable declaration shadows a
1700a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * FINISHME: declaration at a higher scope.
1701a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1702a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1703cec65a6b76290ee4da91691bd3ef01c3fb8a0c37Ian Romanick      if ((decl_type == NULL) || decl_type->is_void()) {
1704a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 if (type_name != NULL) {
1705a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    _mesa_glsl_error(& loc, state,
1706a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     "invalid type `%s' in declaration of `%s'",
1707a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     type_name, decl->identifier);
1708a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 } else {
1709a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    _mesa_glsl_error(& loc, state,
1710a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     "invalid type in declaration of `%s'",
1711a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     decl->identifier);
1712a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
1713a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 continue;
1714a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1715a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1716a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (decl->is_array) {
171728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 var_type = process_array_type(decl_type, decl->array_size, state);
1718a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
1719a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 var_type = decl_type;
1720a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1721a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
17227e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick      var = new(ctx) ir_variable(var_type, decl->identifier, ir_var_auto);
1723a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
17243f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
17253f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *
17263f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     "Global variables can only use the qualifiers const,
17273f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     attribute, uni form, or varying. Only one may be
17283f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     specified.
17293f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *
17303f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     Local variables can only use the qualifier const."
17313f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *
17323f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       * This is relaxed in GLSL 1.30.
17333f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       */
17343f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      if (state->language_version < 120) {
17353f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 if (this->type->qualifier.out) {
17363f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	    _mesa_glsl_error(& loc, state,
17373f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "`out' qualifier in declaration of `%s' "
17383f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "only valid for function parameters in GLSL 1.10.",
17393f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     decl->identifier);
17403f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 }
17413f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 if (this->type->qualifier.in) {
17423f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	    _mesa_glsl_error(& loc, state,
17433f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "`in' qualifier in declaration of `%s' "
17443f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "only valid for function parameters in GLSL 1.10.",
17453f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     decl->identifier);
17463f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 }
17473f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 /* FINISHME: Test for other invalid qualifiers. */
17483f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      }
17493f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt
17502e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
17512e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				       & loc);
1752a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
17536f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      if (this->type->qualifier.invariant) {
1754046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	 if ((state->target == vertex_shader) && !(var->mode == ir_var_out ||
1755046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt						   var->mode == ir_var_inout)) {
1756046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	    /* FINISHME: Note that this doesn't work for invariant on
1757046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	     * a function signature outval
1758046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	     */
17596f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
17606f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "`%s' cannot be marked invariant, vertex shader "
17616f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "outputs only\n", var->name);
1762046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	 } else if ((state->target == fragment_shader) &&
1763046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt		    !(var->mode == ir_var_in || var->mode == ir_var_inout)) {
1764046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	    /* FINISHME: Note that this doesn't work for invariant on
1765046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	     * a function signature inval
1766046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	     */
17676f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
17686f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "`%s' cannot be marked invariant, fragment shader "
17696f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "inputs only\n", var->name);
17706f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 }
17716f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      }
17726f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
1773e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick      if (state->current_function != NULL) {
1774b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 const char *mode = NULL;
1775e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 const char *extra = "";
1776b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick
1777e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 /* There is no need to check for 'inout' here because the parser will
1778e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	  * only allow that in function parameter lists.
1779e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	  */
1780e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	 if (this->type->qualifier.attribute) {
1781b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	    mode = "attribute";
1782b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 } else if (this->type->qualifier.uniform) {
1783b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	    mode = "uniform";
1784b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 } else if (this->type->qualifier.varying) {
1785b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	    mode = "varying";
1786e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 } else if (this->type->qualifier.in) {
1787e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    mode = "in";
1788e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    extra = " or in function parameter list";
1789e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 } else if (this->type->qualifier.out) {
1790e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    mode = "out";
1791e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    extra = " or in function parameter list";
1792b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 }
1793b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick
1794b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 if (mode) {
1795e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	    _mesa_glsl_error(& loc, state,
1796b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick			     "%s variable `%s' must be declared at "
1797e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick			     "global scope%s",
1798e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick			     mode, var->name, extra);
1799e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	 }
1800e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick      } else if (var->mode == ir_var_in) {
1801fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	 if (state->target == vertex_shader) {
1802fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    bool error_emitted = false;
1803fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
1804fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1805fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
1806fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    "Vertex shader inputs can only be float, floating-point
1807fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    vectors, matrices, signed and unsigned integers and integer
1808fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    vectors. Vertex shader inputs can also form arrays of these
1809fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    types, but not structures."
1810fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
18112d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
18122d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *
18132d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *    "Vertex shader inputs can only be float, floating-point
18142d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *    vectors, matrices, signed and unsigned integers and integer
18152d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *    vectors. They cannot be arrays or structures."
18162d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *
1817fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
1818fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
1819fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    "The attribute qualifier can be used only with float,
1820fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    floating-point vectors, and matrices. Attribute variables
1821fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    cannot be declared as arrays or structures."
1822fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     */
1823fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    const glsl_type *check_type = var->type->is_array()
1824fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       ? var->type->fields.array : var->type;
1825fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
1826fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    switch (check_type->base_type) {
1827fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_FLOAT:
1828fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       break;
1829fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_UINT:
1830fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_INT:
1831fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       if (state->language_version > 120)
1832fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick		  break;
1833fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       /* FALLTHROUGH */
1834fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    default:
1835fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       _mesa_glsl_error(& loc, state,
1836fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"vertex shader input / attribute cannot have "
1837fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"type %s`%s'",
1838fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				var->type->is_array() ? "array of " : "",
1839fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				check_type->name);
1840fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       error_emitted = true;
1841fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    }
1842fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
18432d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	    if (!error_emitted && (state->language_version <= 130)
1844fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick		&& var->type->is_array()) {
1845fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       _mesa_glsl_error(& loc, state,
1846fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"vertex shader input / attribute cannot have "
1847fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"array type");
1848fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       error_emitted = true;
1849fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    }
1850fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	 }
1851fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick      }
1852fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
1853e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick      /* Process the initializer and add its instructions to a temporary
1854e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       * list.  This list will be added to the instruction stream (below) after
1855e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       * the declaration is added.  This is done because in some cases (such as
1856e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       * redeclarations) the declaration may not actually be added to the
1857e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       * instruction stream.
1858e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       */
1859fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt      exec_list initializer_instructions;
186066faec4895b7bb59a614087a200c05157191b4aeIan Romanick      if (decl->initializer != NULL) {
186143de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 YYLTYPE initializer_loc = decl->initializer->get_location();
186243de17282017bdf187d6e646de3262cc64b7f46bIan Romanick
186366faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
186466faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *
186566faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *    "All uniform variables are read-only and are initialized either
186666faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *    directly by an application via API commands, or indirectly by
186766faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *    OpenGL."
186866faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  */
186966faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 if ((state->language_version <= 110)
187066faec4895b7bb59a614087a200c05157191b4aeIan Romanick	     && (var->mode == ir_var_uniform)) {
187143de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	    _mesa_glsl_error(& initializer_loc, state,
187243de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     "cannot initialize uniforms in GLSL 1.10");
187343de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 }
187443de17282017bdf187d6e646de3262cc64b7f46bIan Romanick
187543de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 if (var->type->is_sampler()) {
187643de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	    _mesa_glsl_error(& initializer_loc, state,
187743de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     "cannot initialize samplers");
187843de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 }
187919360152f5bd8cff93359dbfe5a50a90b699c118Ian Romanick
188043de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 if ((var->mode == ir_var_in) && (state->current_function == NULL)) {
188143de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	    _mesa_glsl_error(& initializer_loc, state,
188243de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     "cannot initialize %s shader input / %s",
1883ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick			     _mesa_glsl_shader_target_name(state->target),
188443de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     (state->target == vertex_shader)
188543de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     ? "attribute" : "varying");
188666faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 }
188766faec4895b7bb59a614087a200c05157191b4aeIan Romanick
18881660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const lhs = new(ctx) ir_dereference_variable(var);
1889fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt	 ir_rvalue *rhs = decl->initializer->hir(&initializer_instructions,
1890e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick						 state);
189119360152f5bd8cff93359dbfe5a50a90b699c118Ian Romanick
1892ce030884064046925a655413097dd8257e9392ddIan Romanick	 /* Calculate the constant value if this is a const or uniform
1893307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	  * declaration.
189466faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  */
1895ce030884064046925a655413097dd8257e9392ddIan Romanick	 if (this->type->qualifier.constant || this->type->qualifier.uniform) {
1896e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke	    ir_rvalue *new_rhs = validate_assignment(state, var->type, rhs);
1897e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke	    if (new_rhs != NULL) {
1898e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke	       rhs = new_rhs;
1899e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt
1900e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt	       ir_constant *constant_value = rhs->constant_expression_value();
1901e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt	       if (!constant_value) {
1902e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt		  _mesa_glsl_error(& initializer_loc, state,
1903e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt				   "initializer of %s variable `%s' must be a "
1904e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt				   "constant expression",
1905e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt				   (this->type->qualifier.constant)
1906e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt				   ? "const" : "uniform",
1907e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt				   decl->identifier);
1908e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt		  if (var->type->is_numeric()) {
1909e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt		     /* Reduce cascading errors. */
1910e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt		     var->constant_value = ir_constant::zero(ctx, var->type);
1911e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt		  }
1912e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt	       } else {
1913e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt		  rhs = constant_value;
1914e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt		  var->constant_value = constant_value;
1915e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt	       }
1916e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke	    } else {
1917e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke	       _mesa_glsl_error(&initializer_loc, state,
1918e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke			        "initializer of type %s cannot be assigned to "
1919e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke				"variable of type %s",
1920e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke				rhs->type->name, var->type->name);
1921e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt	       if (var->type->is_numeric()) {
1922e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt		  /* Reduce cascading errors. */
1923e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt		  var->constant_value = ir_constant::zero(ctx, var->type);
1924e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt	       }
1925307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	    }
1926307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	 }
192766faec4895b7bb59a614087a200c05157191b4aeIan Romanick
1928307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	 if (rhs && !rhs->type->is_error()) {
1929ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt	    bool temp = var->read_only;
1930ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt	    if (this->type->qualifier.constant)
1931ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt	       var->read_only = false;
1932ce030884064046925a655413097dd8257e9392ddIan Romanick
1933ce030884064046925a655413097dd8257e9392ddIan Romanick	    /* Never emit code to initialize a uniform.
1934ce030884064046925a655413097dd8257e9392ddIan Romanick	     */
1935ce030884064046925a655413097dd8257e9392ddIan Romanick	    if (!this->type->qualifier.uniform)
1936fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt	       result = do_assignment(&initializer_instructions, state,
1937fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt				      lhs, rhs,
1938ce030884064046925a655413097dd8257e9392ddIan Romanick				      this->get_location());
1939ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt	    var->read_only = temp;
194066faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 }
194166faec4895b7bb59a614087a200c05157191b4aeIan Romanick      }
194217d86f4371da413176ba365ca26a58bac172d365Ian Romanick
19430ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt      /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
19440ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *
19450ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *     "It is an error to write to a const variable outside of
19460ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *      its declaration, so they must be initialized when
19470ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *      declared."
19480ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       */
19490ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt      if (this->type->qualifier.constant && decl->initializer == NULL) {
19500ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt	 _mesa_glsl_error(& loc, state,
19510ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt			  "const declaration of `%s' must be initialized");
19520ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt      }
19530ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt
19545d25746640ee27882b69a962459727cf924443dbKenneth Graunke      /* Check if this declaration is actually a re-declaration, either to
19555d25746640ee27882b69a962459727cf924443dbKenneth Graunke       * resize an array or add qualifiers to an existing variable.
19565466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       *
1957a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke       * This is allowed for variables in the current scope, or when at
1958a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke       * global scope (for built-ins in the implicit outer scope).
19595466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       */
19605d25746640ee27882b69a962459727cf924443dbKenneth Graunke      ir_variable *earlier = state->symbols->get_variable(decl->identifier);
1961a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke      if (earlier != NULL && (state->current_function == NULL ||
1962a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke	  state->symbols->name_declared_this_scope(decl->identifier))) {
19635466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
19645d25746640ee27882b69a962459727cf924443dbKenneth Graunke	 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
19655d25746640ee27882b69a962459727cf924443dbKenneth Graunke	  *
19665d25746640ee27882b69a962459727cf924443dbKenneth Graunke	  * "It is legal to declare an array without a size and then
19675d25746640ee27882b69a962459727cf924443dbKenneth Graunke	  *  later re-declare the same name as an array of the same
19685d25746640ee27882b69a962459727cf924443dbKenneth Graunke	  *  type and specify a size."
19695d25746640ee27882b69a962459727cf924443dbKenneth Graunke	  */
19705d25746640ee27882b69a962459727cf924443dbKenneth Graunke	 if ((earlier->type->array_size() == 0)
19715466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	     && var->type->is_array()
19725466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	     && (var->type->element_type() == earlier->type->element_type())) {
19735466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	    /* FINISHME: This doesn't match the qualifiers on the two
19745466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	     * FINISHME: declarations.  It's not 100% clear whether this is
19755466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	     * FINISHME: required or not.
19765466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	     */
19775466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
1978cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	    /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
1979cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	     *
1980cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	     *     "The size [of gl_TexCoord] can be at most
1981cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	     *     gl_MaxTextureCoords."
1982cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	     */
1983127308b4be077e5bdf60f76320307550921e86bbIan Romanick	    const unsigned size = unsigned(var->type->array_size());
1984cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	    if ((strcmp("gl_TexCoord", var->name) == 0)
1985127308b4be077e5bdf60f76320307550921e86bbIan Romanick		&& (size > state->Const.MaxTextureCoords)) {
1986cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	       YYLTYPE loc = this->get_location();
1987cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick
1988cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	       _mesa_glsl_error(& loc, state, "`gl_TexCoord' array size cannot "
1989cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick				"be larger than gl_MaxTextureCoords (%u)\n",
1990127308b4be077e5bdf60f76320307550921e86bbIan Romanick				state->Const.MaxTextureCoords);
199112873fa4e332959295154edfe957c0af79af5e74Ian Romanick	    } else if ((size > 0) && (size <= earlier->max_array_access)) {
19925466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	       YYLTYPE loc = this->get_location();
19935466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
19945466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	       _mesa_glsl_error(& loc, state, "array size must be > %u due to "
19955466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick				"previous access",
19965466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick				earlier->max_array_access);
19975466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	    }
19985466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
19995466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	    earlier->type = var->type;
20005466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	    delete var;
20015466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	    var = NULL;
20025d25746640ee27882b69a962459727cf924443dbKenneth Graunke	 } else if (state->extensions->ARB_fragment_coord_conventions
20035d25746640ee27882b69a962459727cf924443dbKenneth Graunke		    && strcmp(var->name, "gl_FragCoord") == 0
20045d25746640ee27882b69a962459727cf924443dbKenneth Graunke		    && earlier->type == var->type
20055d25746640ee27882b69a962459727cf924443dbKenneth Graunke		    && earlier->mode == var->mode) {
20064a962170d7cf4243d6ae156fca20a6167388925dEric Anholt	    /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
20074a962170d7cf4243d6ae156fca20a6167388925dEric Anholt	     * qualifiers.
20084a962170d7cf4243d6ae156fca20a6167388925dEric Anholt	     */
20094a962170d7cf4243d6ae156fca20a6167388925dEric Anholt	    earlier->origin_upper_left = var->origin_upper_left;
20104a962170d7cf4243d6ae156fca20a6167388925dEric Anholt	    earlier->pixel_center_integer = var->pixel_center_integer;
20115466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	 } else {
20125466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	    YYLTYPE loc = this->get_location();
20135d25746640ee27882b69a962459727cf924443dbKenneth Graunke	    _mesa_glsl_error(&loc, state, "`%s' redeclared", decl->identifier);
20145466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	 }
20155466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
20165466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	 continue;
20175466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick      }
20185466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
20195d25746640ee27882b69a962459727cf924443dbKenneth Graunke      /* By now, we know it's a new variable declaration (we didn't hit the
20205d25746640ee27882b69a962459727cf924443dbKenneth Graunke       * above "continue").
20215d25746640ee27882b69a962459727cf924443dbKenneth Graunke       *
20225d25746640ee27882b69a962459727cf924443dbKenneth Graunke       * From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
20235466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       *
20245466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       *   "Identifiers starting with "gl_" are reserved for use by
20255466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       *   OpenGL, and may not be declared in a shader as either a
20265466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       *   variable or a function."
20275466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       */
2028de3b40d8cdc42cc1cd71dd65c90d6d569d922fc6Ian Romanick      if (strncmp(decl->identifier, "gl_", 3) == 0)
20295466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	 _mesa_glsl_error(& loc, state,
20305466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick			  "identifier `%s' uses reserved `gl_' prefix",
20315466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick			  decl->identifier);
20325466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
20335d25746640ee27882b69a962459727cf924443dbKenneth Graunke      /* Add the variable to the symbol table.  Note that the initializer's
20345d25746640ee27882b69a962459727cf924443dbKenneth Graunke       * IR was already processed earlier (though it hasn't been emitted yet),
20355d25746640ee27882b69a962459727cf924443dbKenneth Graunke       * without the variable in scope.
20365d25746640ee27882b69a962459727cf924443dbKenneth Graunke       *
20375d25746640ee27882b69a962459727cf924443dbKenneth Graunke       * This differs from most C-like languages, but it follows the GLSL
20385d25746640ee27882b69a962459727cf924443dbKenneth Graunke       * specification.  From page 28 (page 34 of the PDF) of the GLSL 1.50
20395d25746640ee27882b69a962459727cf924443dbKenneth Graunke       * spec:
20405d25746640ee27882b69a962459727cf924443dbKenneth Graunke       *
20415d25746640ee27882b69a962459727cf924443dbKenneth Graunke       *     "Within a declaration, the scope of a name starts immediately
20425d25746640ee27882b69a962459727cf924443dbKenneth Graunke       *     after the initializer if present or immediately after the name
20435d25746640ee27882b69a962459727cf924443dbKenneth Graunke       *     being declared if not."
20445d25746640ee27882b69a962459727cf924443dbKenneth Graunke       */
20455d25746640ee27882b69a962459727cf924443dbKenneth Graunke      if (!state->symbols->add_variable(var->name, var)) {
20465d25746640ee27882b69a962459727cf924443dbKenneth Graunke	 YYLTYPE loc = this->get_location();
20475d25746640ee27882b69a962459727cf924443dbKenneth Graunke	 _mesa_glsl_error(&loc, state, "name `%s' already taken in the "
20485d25746640ee27882b69a962459727cf924443dbKenneth Graunke			  "current scope", decl->identifier);
20495d25746640ee27882b69a962459727cf924443dbKenneth Graunke	 continue;
20505d25746640ee27882b69a962459727cf924443dbKenneth Graunke      }
20515d25746640ee27882b69a962459727cf924443dbKenneth Graunke
20528048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt      /* Push the variable declaration to the top.  It means that all
20538048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt       * the variable declarations will appear in a funny
20548048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt       * last-to-first order, but otherwise we run into trouble if a
20558048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt       * function is prototyped, a global var is decled, then the
20568048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt       * function is defined with usage of the global var.  See
20578048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt       * glslparsertest's CorrectModule.frag.
20588048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt       */
20598048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt      instructions->push_head(var);
2060fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt      instructions->append_list(&initializer_instructions);
2061a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2062a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
20638558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt
20648558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt   /* Generally, variable declarations do not have r-values.  However,
20658558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * one is used for the declaration in
20668558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    *
20678558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * while (bool b = some_condition()) {
20688558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    *   ...
20698558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * }
20708558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    *
20718558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * so we return the rvalue from the last seen declaration here.
2072a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
20738558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt   return result;
2074a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
2075a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2076a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2077fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
20780044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_parameter_declarator::hir(exec_list *instructions,
207918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			      struct _mesa_glsl_parse_state *state)
2080a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
2081953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
2082a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const struct glsl_type *type;
2083a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const char *name = NULL;
20842e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   YYLTYPE loc = this->get_location();
2085a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2086d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   type = this->type->specifier->glsl_type(& name, state);
2087a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2088a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (type == NULL) {
2089a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (name != NULL) {
2090a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 _mesa_glsl_error(& loc, state,
2091a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			  "invalid type `%s' in declaration of `%s'",
209218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  name, this->identifier);
2093a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
2094a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 _mesa_glsl_error(& loc, state,
2095a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			  "invalid type in declaration of `%s'",
209618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  this->identifier);
2097a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
2098a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
20990471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::error_type;
2100a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2101a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2102068c80cfe0a280490353b6b007165d717c672eedEric Anholt   /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
2103068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *
2104068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    "Functions that accept no input arguments need not use void in the
2105068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    argument list because prototypes (or definitions) are required and
2106068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    therefore there is no ambiguity when an empty argument list "( )" is
2107068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    declared. The idiom "(void)" as a parameter list is provided for
2108068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    convenience."
2109068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *
2110068c80cfe0a280490353b6b007165d717c672eedEric Anholt    * Placing this check here prevents a void parameter being set up
2111068c80cfe0a280490353b6b007165d717c672eedEric Anholt    * for a function, which avoids tripping up checks for main taking
2112068c80cfe0a280490353b6b007165d717c672eedEric Anholt    * parameters and lookups of an unnamed symbol.
2113068c80cfe0a280490353b6b007165d717c672eedEric Anholt    */
2114cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   if (type->is_void()) {
2115cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      if (this->identifier != NULL)
2116cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick	 _mesa_glsl_error(& loc, state,
2117cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick			  "named parameter cannot have type `void'");
2118cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
2119cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      is_void = true;
2120068c80cfe0a280490353b6b007165d717c672eedEric Anholt      return NULL;
2121cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   }
2122068c80cfe0a280490353b6b007165d717c672eedEric Anholt
212345d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   if (formal_parameter && (this->identifier == NULL)) {
212445d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick      _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
212545d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick      return NULL;
212645d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   }
212745d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick
2128e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   /* This only handles "vec4 foo[..]".  The earlier specifier->glsl_type(...)
2129e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke    * call already handled the "vec4[..] foo" case.
2130e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke    */
2131e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   if (this->is_array) {
2132e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke      type = process_array_type(type, this->array_size, state);
2133e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   }
2134e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke
2135e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   if (type->array_size() == 0) {
2136e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke      _mesa_glsl_error(&loc, state, "arrays passed as parameters must have "
2137e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke		       "a declared size.");
2138e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke      type = glsl_type::error_type;
2139e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   }
2140e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke
2141cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   is_void = false;
21427e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick   ir_variable *var = new(ctx) ir_variable(type, this->identifier, ir_var_in);
2143a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2144cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick   /* Apply any specified qualifiers to the parameter declaration.  Note that
2145cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick    * for function parameters the default mode is 'in'.
2146cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick    */
21472e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc);
2148a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
21490044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick   instructions->push_tail(var);
2150a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2151a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Parameter declarations do not have r-values.
2152a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
2153a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
2154a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
2155a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2156a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
215745d8a70c12ee6ea956baaf898324a828496382f6Ian Romanickvoid
2158304ea90233baeac6801a98e981658cb7a2d2501cIan Romanickast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters,
215945d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					    bool formal,
216045d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					    exec_list *ir_parameters,
216145d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					    _mesa_glsl_parse_state *state)
2162a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
2163cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   ast_parameter_declarator *void_param = NULL;
2164cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   unsigned count = 0;
2165a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
21662b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) {
216745d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick      param->formal_parameter = formal;
2168068c80cfe0a280490353b6b007165d717c672eedEric Anholt      param->hir(ir_parameters, state);
2169cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
2170cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      if (param->is_void)
2171cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick	 void_param = param;
2172cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
2173cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      count++;
2174cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   }
2175cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
2176cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   if ((void_param != NULL) && (count > 1)) {
2177cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      YYLTYPE loc = void_param->get_location();
2178cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
2179cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      _mesa_glsl_error(& loc, state,
2180cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick		       "`void' parameter must be only parameter");
2181a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2182a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
2183a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2184a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2185fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
218692318a947958892497722772b03c643ebc943294Ian Romanickast_function::hir(exec_list *instructions,
218792318a947958892497722772b03c643ebc943294Ian Romanick		  struct _mesa_glsl_parse_state *state)
2188a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
2189953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
219018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   ir_function *f = NULL;
219192318a947958892497722772b03c643ebc943294Ian Romanick   ir_function_signature *sig = NULL;
219292318a947958892497722772b03c643ebc943294Ian Romanick   exec_list hir_parameters;
2193a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2194ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   const char *const name = identifier;
2195a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
219663b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick   /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
219763b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *
219863b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *   "Function declarations (prototypes) cannot occur inside of functions;
219963b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *   they must be at global scope, or for the built-in functions, outside
220063b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *   the global scope."
220163b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *
220263b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
220363b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *
220463b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *   "User defined functions may only be defined within the global scope."
220563b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *
220663b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    * Note that this language does not appear in GLSL 1.10.
220763b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    */
220863b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick   if ((state->current_function != NULL) && (state->language_version != 110)) {
220963b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick      YYLTYPE loc = this->get_location();
221063b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick      _mesa_glsl_error(&loc, state,
221163b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick		       "declaration of function `%s' not allowed within "
221263b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick		       "function body", name);
221363b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick   }
221463b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick
2215edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke   /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
2216edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    *
2217edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    *   "Identifiers starting with "gl_" are reserved for use by
2218edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    *   OpenGL, and may not be declared in a shader as either a
2219edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    *   variable or a function."
2220edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    */
2221edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke   if (strncmp(name, "gl_", 3) == 0) {
2222edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke      YYLTYPE loc = this->get_location();
2223edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke      _mesa_glsl_error(&loc, state,
2224edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke		       "identifier `%s' uses reserved `gl_' prefix", name);
2225edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke   }
2226edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke
2227a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Convert the list of function parameters to HIR now so that they can be
2228a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * used below to compare this function's signature with previously seen
2229a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * signatures for functions with the same name.
2230a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
223145d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   ast_parameter_declarator::parameters_to_hir(& this->parameters,
223245d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					       is_definition,
223345d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					       & hir_parameters, state);
2234a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2235e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick   const char *return_type_name;
2236e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick   const glsl_type *return_type =
223792318a947958892497722772b03c643ebc943294Ian Romanick      this->return_type->specifier->glsl_type(& return_type_name, state);
2238e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick
223976e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt   if (!return_type) {
224076e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt      YYLTYPE loc = this->get_location();
224176e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt      _mesa_glsl_error(&loc, state,
224276e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt		       "function `%s' has undeclared return type `%s'",
224376e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt		       name, return_type_name);
224476e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt      return_type = glsl_type::error_type;
224576e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt   }
2246e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick
2247ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
2248ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke    * "No qualifier is allowed on the return type of a function."
2249ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke    */
2250ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   if (this->return_type->has_qualifiers()) {
2251ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke      YYLTYPE loc = this->get_location();
2252ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke      _mesa_glsl_error(& loc, state,
2253ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke		       "function `%s' return type has qualifiers", name);
2254ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   }
2255ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke
2256a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Verify that this function's signature either doesn't match a previously
2257a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * seen signature for a function with the same name, or, if a match is found,
2258a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * that the previously seen signature does not have an associated definition.
2259a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
2260ac2376e6f51677ab321930b0200a79d1683cfbbaKenneth Graunke   f = state->symbols->get_function(name, false);
2261a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke   if (f != NULL && !f->is_builtin) {
2262202604e8160157e4e80b3458175e0170d168e557Ian Romanick      sig = f->exact_matching_signature(&hir_parameters);
22630d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke      if (sig != NULL) {
22640d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 const char *badvar = sig->qualifiers_match(&hir_parameters);
22650d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 if (badvar != NULL) {
22660d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    YYLTYPE loc = this->get_location();
2267abd40b15210c17b2a3ba8fcffc868fda203efa01Kenneth Graunke
22680d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
22690d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke			     "qualifiers don't match prototype", name, badvar);
22700d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 }
22711e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt
22720d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 if (sig->return_type != return_type) {
22730d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    YYLTYPE loc = this->get_location();
227460be7626b829af7e1d07330b9a88468924ba350eEric Anholt
22750d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
22760d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke			     "match prototype", name);
22770d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 }
2278a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
22790d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 if (is_definition && sig->is_defined) {
22800d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    YYLTYPE loc = this->get_location();
2281a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
22820d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
2283a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
2284a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
2285a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
22861660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      f = new(ctx) ir_function(name);
2287e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke      if (!state->symbols->add_function(f->name, f)) {
2288e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke	 /* This function name shadows a non-function use of the same name. */
2289e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke	 YYLTYPE loc = this->get_location();
2290e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke
2291e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke	 _mesa_glsl_error(&loc, state, "function name `%s' conflicts with "
2292e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke			  "non-function", name);
2293e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke	 return NULL;
2294e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke      }
22959fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke
22969fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke      /* Emit the new function header */
229763b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick      if (state->current_function == NULL)
229863b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	 instructions->push_tail(f);
229963b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick      else {
230063b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	 /* IR invariants disallow function declarations or definitions nested
230163b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	  * within other function definitions.  Insert the new ir_function
230263b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	  * block in the instruction sequence before the ir_function block
230363b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	  * containing the current ir_function_signature.
230463b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	  *
230563b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	  * This can only happen in a GLSL 1.10 shader.  In all other GLSL
230663b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	  * versions this nesting is disallowed.  There is a check for this at
230763b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	  * the top of this function.
230863b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	  */
230963b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	 ir_function *const curr =
231063b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	    const_cast<ir_function *>(state->current_function->function());
231163b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick
231263b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	 curr->insert_before(f);
231363b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick      }
2314a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2315a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2316ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   /* Verify the return type of main() */
2317ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   if (strcmp(name, "main") == 0) {
231825711a85c22bed305c9b52b89feb9c600d1892dfIan Romanick      if (! return_type->is_void()) {
2319ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt	 YYLTYPE loc = this->get_location();
2320ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt
2321ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt	 _mesa_glsl_error(& loc, state, "main() must return void");
2322ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt      }
2323174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt
232492318a947958892497722772b03c643ebc943294Ian Romanick      if (!hir_parameters.is_empty()) {
2325174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt	 YYLTYPE loc = this->get_location();
2326174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt
2327174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt	 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
2328174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt      }
2329ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   }
2330a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2331a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Finish storing the information about this new function in its signature.
2332a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
233392318a947958892497722772b03c643ebc943294Ian Romanick   if (sig == NULL) {
23341660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      sig = new(ctx) ir_function_signature(return_type);
233592318a947958892497722772b03c643ebc943294Ian Romanick      f->add_signature(sig);
2336a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2337a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2338bff6013d469b3d4e54cdc5731801c56994a523ecKenneth Graunke   sig->replace_parameters(&hir_parameters);
233992318a947958892497722772b03c643ebc943294Ian Romanick   signature = sig;
234092318a947958892497722772b03c643ebc943294Ian Romanick
234192318a947958892497722772b03c643ebc943294Ian Romanick   /* Function declarations (prototypes) do not have r-values.
234292318a947958892497722772b03c643ebc943294Ian Romanick    */
234392318a947958892497722772b03c643ebc943294Ian Romanick   return NULL;
234492318a947958892497722772b03c643ebc943294Ian Romanick}
234592318a947958892497722772b03c643ebc943294Ian Romanick
234692318a947958892497722772b03c643ebc943294Ian Romanick
234792318a947958892497722772b03c643ebc943294Ian Romanickir_rvalue *
234892318a947958892497722772b03c643ebc943294Ian Romanickast_function_definition::hir(exec_list *instructions,
234992318a947958892497722772b03c643ebc943294Ian Romanick			     struct _mesa_glsl_parse_state *state)
235092318a947958892497722772b03c643ebc943294Ian Romanick{
235192318a947958892497722772b03c643ebc943294Ian Romanick   prototype->is_definition = true;
235292318a947958892497722772b03c643ebc943294Ian Romanick   prototype->hir(instructions, state);
2353e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick
235492318a947958892497722772b03c643ebc943294Ian Romanick   ir_function_signature *signature = prototype->signature;
2355826a39cb14244820e8539a2328bb52447348f184Kenneth Graunke   if (signature == NULL)
2356826a39cb14244820e8539a2328bb52447348f184Kenneth Graunke      return NULL;
2357a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
235841ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   assert(state->current_function == NULL);
235941ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = signature;
23606de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke   state->found_return = false;
236141ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick
2362e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick   /* Duplicate parameters declared in the prototype as concrete variables.
2363e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick    * Add these to the symbol table.
2364a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
23658bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick   state->symbols->push_scope();
2366e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick   foreach_iter(exec_list_iterator, iter, signature->parameters) {
2367fbc7c0b8f2e161bce1c048c63d2d5cfcdeb096f1Eric Anholt      ir_variable *const var = ((ir_instruction *) iter.get())->as_variable();
2368e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick
2369fbc7c0b8f2e161bce1c048c63d2d5cfcdeb096f1Eric Anholt      assert(var != NULL);
2370a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
23713359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      /* The only way a parameter would "exist" is if two parameters have
23723359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick       * the same name.
23733359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick       */
23743359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      if (state->symbols->name_declared_this_scope(var->name)) {
23753359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 YYLTYPE loc = this->get_location();
23763359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick
23773359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
23783359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      } else {
23793359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 state->symbols->add_variable(var->name, var);
23803359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      }
2381a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2382a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
23839fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke   /* Convert the body of the function to HIR. */
2384894ea972a4defdaafeaa3a248c113b06c7ae0c7eEric Anholt   this->body->hir(&signature->body, state);
23859fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke   signature->is_defined = true;
2386a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
23878bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick   state->symbols->pop_scope();
2388a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
238941ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   assert(state->current_function == signature);
239041ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = NULL;
2391a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
23926de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke   if (!signature->return_type->is_void() && !state->found_return) {
23936de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke      YYLTYPE loc = this->get_location();
23946de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke      _mesa_glsl_error(& loc, state, "function `%s' has non-void return type "
23956de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke		       "%s, but no return statement",
23966de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke		       signature->function_name(),
23976de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke		       signature->return_type->name);
23986de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke   }
23996de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke
2400a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Function definitions do not have r-values.
2401a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
2402a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
2403a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
240416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
240516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
2406fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
240716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanickast_jump_statement::hir(exec_list *instructions,
240816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick			struct _mesa_glsl_parse_state *state)
240916a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick{
2410953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
241116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
2412c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   switch (mode) {
2413c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_return: {
241416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      ir_return *inst;
2415aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt      assert(state->current_function);
241616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
241716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      if (opt_return_value) {
2418ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	 if (state->current_function->return_type->base_type ==
2419ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	     GLSL_TYPE_VOID) {
2420ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	    YYLTYPE loc = this->get_location();
2421ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt
2422ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	    _mesa_glsl_error(& loc, state,
2423ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt			     "`return` with a value, in function `%s' "
2424ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt			     "returning void",
2425f96c52ba2e290e3ba5f14cd7f87ba5b4382a1785Kenneth Graunke			     state->current_function->function_name());
2426ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	 }
242716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
242816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 ir_expression *const ret = (ir_expression *)
242916a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	    opt_return_value->hir(instructions, state);
243016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 assert(ret != NULL);
243116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
243218707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	 /* Implicit conversions are not allowed for return values. */
243318707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	 if (state->current_function->return_type != ret->type) {
243418707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	    YYLTYPE loc = this->get_location();
243518707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke
243618707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	    _mesa_glsl_error(& loc, state,
243718707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     "`return' with wrong type %s, in function `%s' "
243818707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     "returning %s",
243918707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     ret->type->name,
244018707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     state->current_function->function_name(),
244118707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     state->current_function->return_type->name);
244218707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	 }
244316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
24441660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 inst = new(ctx) ir_return(ret);
244516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      } else {
2446aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	 if (state->current_function->return_type->base_type !=
2447aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	     GLSL_TYPE_VOID) {
2448aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	    YYLTYPE loc = this->get_location();
2449aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt
2450aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	    _mesa_glsl_error(& loc, state,
2451aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt			     "`return' with no value, in function %s returning "
2452aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt			     "non-void",
2453f96c52ba2e290e3ba5f14cd7f87ba5b4382a1785Kenneth Graunke			     state->current_function->function_name());
2454aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	 }
24551660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 inst = new(ctx) ir_return;
245616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      }
245716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
24586de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke      state->found_return = true;
245916a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      instructions->push_tail(inst);
2460c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick      break;
246116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   }
246216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
2463c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_discard:
2464b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt      if (state->target != fragment_shader) {
2465b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt	 YYLTYPE loc = this->get_location();
2466b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt
2467b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt	 _mesa_glsl_error(& loc, state,
2468b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt			  "`discard' may only appear in a fragment shader");
2469b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt      }
247077049a702ad54e09c4102fe8c964e069944f83e5Kenneth Graunke      instructions->push_tail(new(ctx) ir_discard);
2471c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick      break;
2472c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick
2473c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_break:
2474c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_continue:
24754cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      /* FINISHME: Handle switch-statements.  They cannot contain 'continue',
24764cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick       * FINISHME: and they use a different IR instruction for 'break'.
24774cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick       */
24784cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      /* FINISHME: Correctly handle the nesting.  If a switch-statement is
24794cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick       * FINISHME: inside a loop, a 'continue' is valid and will bind to the
24804cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick       * FINISHME: loop.
24814cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick       */
24824cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      if (state->loop_or_switch_nesting == NULL) {
24834cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 YYLTYPE loc = this->get_location();
24844cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
24854cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 _mesa_glsl_error(& loc, state,
24864cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick			  "`%s' may only appear in a loop",
24874cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick			  (mode == ast_break) ? "break" : "continue");
24884cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      } else {
24894cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 ir_loop *const loop = state->loop_or_switch_nesting->as_loop();
24904cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
24912d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	 /* Inline the for loop expression again, since we don't know
24922d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	  * where near the end of the loop body the normal copy of it
24932d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	  * is going to be placed.
24942d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	  */
24952d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	 if (mode == ast_continue &&
24962d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	     state->loop_or_switch_nesting_ast->rest_expression) {
24972d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	    state->loop_or_switch_nesting_ast->rest_expression->hir(instructions,
24982d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt								    state);
24992d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	 }
25002d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt
25014cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 if (loop != NULL) {
25024cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	    ir_loop_jump *const jump =
25031660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	       new(ctx) ir_loop_jump((mode == ast_break)
25041660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				     ? ir_loop_jump::jump_break
25051660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				     : ir_loop_jump::jump_continue);
25064cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	    instructions->push_tail(jump);
25074cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 }
25084cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      }
25094cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
2510c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick      break;
2511b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt   }
2512b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt
251316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   /* Jump instructions do not have r-values.
251416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick    */
251516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   return NULL;
251616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick}
25173c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
25183c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
25193c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanickir_rvalue *
25203c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanickast_selection_statement::hir(exec_list *instructions,
25213c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick			     struct _mesa_glsl_parse_state *state)
25223c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick{
2523953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
25241660a2954797e056caba319c5d6c70b0d4be22feCarl Worth
25253c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   ir_rvalue *const condition = this->condition->hir(instructions, state);
25263c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
25273c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
25283c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *
25293c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    "Any expression whose type evaluates to a Boolean can be used as the
25303c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    conditional expression bool-expression. Vector types are not accepted
25313c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    as the expression to if."
25323c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *
25333c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    * The checks are separated so that higher quality diagnostics can be
25343c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    * generated for cases where both rules are violated.
25353c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    */
25363c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
25373c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      YYLTYPE loc = this->condition->get_location();
25383c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
25393c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
25403c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick		       "boolean");
25413c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   }
25423c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
25431660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   ir_if *const stmt = new(ctx) ir_if(condition);
25443c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
2545665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke   if (then_statement != NULL) {
2546665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke      state->symbols->push_scope();
25474f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick      then_statement->hir(& stmt->then_instructions, state);
2548665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke      state->symbols->pop_scope();
2549665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke   }
25503c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
2551665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke   if (else_statement != NULL) {
2552665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke      state->symbols->push_scope();
25534f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick      else_statement->hir(& stmt->else_instructions, state);
2554665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke      state->symbols->pop_scope();
2555665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke   }
25563c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
25573c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   instructions->push_tail(stmt);
25583c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
25593c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   /* if-statements do not have r-values.
25603c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    */
25613c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   return NULL;
25623c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick}
25639e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
25649e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
25658c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickvoid
25668c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickast_iteration_statement::condition_to_hir(ir_loop *stmt,
25678c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick					  struct _mesa_glsl_parse_state *state)
25689e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick{
2569953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
25701660a2954797e056caba319c5d6c70b0d4be22feCarl Worth
25719e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   if (condition != NULL) {
25729e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      ir_rvalue *const cond =
25739e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 condition->hir(& stmt->body_instructions, state);
25749e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
25759e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      if ((cond == NULL)
25769e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	  || !cond->type->is_boolean() || !cond->type->is_scalar()) {
25779e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 YYLTYPE loc = condition->get_location();
25789e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
25799e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 _mesa_glsl_error(& loc, state,
25809e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick			  "loop condition must be scalar boolean");
25819e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      } else {
25829e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 /* As the first code in the loop body, generate a block that looks
25839e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	  * like 'if (!condition) break;' as the loop termination condition.
25849e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	  */
25859e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 ir_rvalue *const not_cond =
25861660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_expression(ir_unop_logic_not, glsl_type::bool_type, cond,
25871660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				   NULL);
25889e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
25891660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_if *const if_stmt = new(ctx) ir_if(not_cond);
25909e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
25919e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 ir_jump *const break_stmt =
25921660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
25939e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
25949e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 if_stmt->then_instructions.push_tail(break_stmt);
25959e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 stmt->body_instructions.push_tail(if_stmt);
25969e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      }
25979e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   }
25988c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick}
25998c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
26008c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
26018c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickir_rvalue *
26028c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickast_iteration_statement::hir(exec_list *instructions,
26038c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick			     struct _mesa_glsl_parse_state *state)
26048c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick{
2605953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
26061660a2954797e056caba319c5d6c70b0d4be22feCarl Worth
2607484606610e36ec7598f7733e9787d888b6a63d64Ian Romanick   /* For-loops and while-loops start a new scope, but do-while loops do not.
26088c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick    */
2609484606610e36ec7598f7733e9787d888b6a63d64Ian Romanick   if (mode != ast_do_while)
26108c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick      state->symbols->push_scope();
26118c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
26128c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   if (init_statement != NULL)
26138c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick      init_statement->hir(instructions, state);
26148c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
26151660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   ir_loop *const stmt = new(ctx) ir_loop();
26168c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   instructions->push_tail(stmt);
26178c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
26188c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   /* Track the current loop and / or switch-statement nesting.
26198c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick    */
26208c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   ir_instruction *const nesting = state->loop_or_switch_nesting;
26212d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt   ast_iteration_statement *nesting_ast = state->loop_or_switch_nesting_ast;
26222d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt
26238c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   state->loop_or_switch_nesting = stmt;
26242d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt   state->loop_or_switch_nesting_ast = this;
26258c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
26268c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   if (mode != ast_do_while)
26278c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick      condition_to_hir(stmt, state);
26289e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
26294f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick   if (body != NULL)
26304f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick      body->hir(& stmt->body_instructions, state);
26319e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
26329e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   if (rest_expression != NULL)
26339e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      rest_expression->hir(& stmt->body_instructions, state);
26349e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
26358c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   if (mode == ast_do_while)
26368c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick      condition_to_hir(stmt, state);
26378c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
2638484606610e36ec7598f7733e9787d888b6a63d64Ian Romanick   if (mode != ast_do_while)
26399e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      state->symbols->pop_scope();
26409e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
2641e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick   /* Restore previous nesting before returning.
2642e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick    */
2643e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick   state->loop_or_switch_nesting = nesting;
26442d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt   state->loop_or_switch_nesting_ast = nesting_ast;
2645e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick
26469e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   /* Loops do not have r-values.
26479e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick    */
26489e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   return NULL;
26499e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick}
26503455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
26513455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
26523455ce614424a5a23a23037e23d0454e476bceeaIan Romanickir_rvalue *
26533455ce614424a5a23a23037e23d0454e476bceeaIan Romanickast_type_specifier::hir(exec_list *instructions,
26543455ce614424a5a23a23037e23d0454e476bceeaIan Romanick			  struct _mesa_glsl_parse_state *state)
26553455ce614424a5a23a23037e23d0454e476bceeaIan Romanick{
26563455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   if (this->structure != NULL)
26573455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      return this->structure->hir(instructions, state);
265885ba37b97df8edd18b757bc475b12b66f4b117edIan Romanick
265985ba37b97df8edd18b757bc475b12b66f4b117edIan Romanick   return NULL;
26603455ce614424a5a23a23037e23d0454e476bceeaIan Romanick}
26613455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
26623455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
26633455ce614424a5a23a23037e23d0454e476bceeaIan Romanickir_rvalue *
26643455ce614424a5a23a23037e23d0454e476bceeaIan Romanickast_struct_specifier::hir(exec_list *instructions,
26653455ce614424a5a23a23037e23d0454e476bceeaIan Romanick			  struct _mesa_glsl_parse_state *state)
26663455ce614424a5a23a23037e23d0454e476bceeaIan Romanick{
26673455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   unsigned decl_count = 0;
26683455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
26693455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* Make an initial pass over the list of structure fields to determine how
26703455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * many there are.  Each element in this list is an ast_declarator_list.
26713455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * This means that we actually need to count the number of elements in the
26723455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * 'declarations' list in each of the elements.
26733455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
26742b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_declarator_list, decl_list, link,
26752b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick		       &this->declarations) {
2676304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      foreach_list_const (decl_ptr, & decl_list->declarations) {
26773455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 decl_count++;
26783455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      }
26793455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   }
26803455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
26813455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
26823455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* Allocate storage for the structure fields and process the field
26833455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * declarations.  As the declarations are processed, try to also convert
26843455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * the types to HIR.  This ensures that structure definitions embedded in
26853455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * other structure definitions are processed.
26863455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
268721b0dbd79937e9d6787f045af7d60d4b6c649ec8Eric Anholt   glsl_struct_field *const fields = talloc_array(state, glsl_struct_field,
268821b0dbd79937e9d6787f045af7d60d4b6c649ec8Eric Anholt						  decl_count);
26893455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
26903455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   unsigned i = 0;
26912b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_declarator_list, decl_list, link,
26922b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick		       &this->declarations) {
26933455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      const char *type_name;
26943455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
26953455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      decl_list->type->specifier->hir(instructions, state);
26963455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
26973455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      const glsl_type *decl_type =
26983455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 decl_list->type->specifier->glsl_type(& type_name, state);
26993455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
27002b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick      foreach_list_typed (ast_declaration, decl, link,
27012b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick			  &decl_list->declarations) {
27023455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 const struct glsl_type *const field_type =
27033455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	    (decl->is_array)
27043455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	    ? process_array_type(decl_type, decl->array_size, state)
27053455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	    : decl_type;
27063455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
270773986a7a262807ab2cfd6d46ae17cfc7a30cdfecIan Romanick	 fields[i].type = (field_type != NULL)
270873986a7a262807ab2cfd6d46ae17cfc7a30cdfecIan Romanick	    ? field_type : glsl_type::error_type;
27093455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 fields[i].name = decl->identifier;
27103455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 i++;
27113455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      }
27123455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   }
27133455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
27143455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   assert(i == decl_count);
27153455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
27161d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick   const char *name;
27171d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick   if (this->name == NULL) {
27181d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick      static unsigned anon_count = 1;
27191d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick      char buf[32];
27203455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
27211d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick      snprintf(buf, sizeof(buf), "#anon_struct_%04x", anon_count);
27221d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick      anon_count++;
27231d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick
27241d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick      name = strdup(buf);
27251d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick   } else {
27261d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick      name = this->name;
27271d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick   }
27281d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick
272949e3577b91f44013746f7a3db411e7041b7d899eIan Romanick   const glsl_type *t =
273049e3577b91f44013746f7a3db411e7041b7d899eIan Romanick      glsl_type::get_record_instance(fields, decl_count, name);
27311d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick
2732ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick   YYLTYPE loc = this->get_location();
2733a789ca649cb143c0c5bf3209ff1bde398fbd777eIan Romanick   if (!state->symbols->add_type(name, t)) {
2734ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick      _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
2735ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick   } else {
2736a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick
2737a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick      const glsl_type **s = (const glsl_type **)
2738a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick	 realloc(state->user_structures,
2739a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick		 sizeof(state->user_structures[0]) *
2740a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick		 (state->num_user_structures + 1));
2741a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick      if (s != NULL) {
2742a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick	 s[state->num_user_structures] = t;
2743a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick	 state->user_structures = s;
2744a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick	 state->num_user_structures++;
2745a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick      }
2746ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick   }
27473455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
27483455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* Structure type definitions do not have r-values.
27493455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
27503455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   return NULL;
27513455ce614424a5a23a23037e23d0454e476bceeaIan Romanick}
2752