ast_to_hir.cpp revision 814c89abdbcd5b841b98746af921796df0362238
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
65814c89abdbcd5b841b98746af921796df0362238Kenneth Graunke   state->symbols->language_version = state->language_version;
66814c89abdbcd5b841b98746af921796df0362238Kenneth Graunke
6741ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = NULL;
6841ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick
69a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke   /* Section 4.2 of the GLSL 1.20 specification states:
70a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    * "The built-in functions are scoped in a scope outside the global scope
71a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    *  users declare global variables in.  That is, a shader's global scope,
72a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    *  available for user-defined functions and global variables, is nested
73a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    *  inside the scope containing the built-in functions."
74a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    *
75a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    * Since built-in functions like ftransform() access built-in variables,
76a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    * it follows that those must be in the outer scope as well.
77a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    *
78a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    * We push scope here to create this nesting effect...but don't pop.
79a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    * This way, a shader's globals are still in the symbol table for use
80a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    * by the linker.
81a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    */
82a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke   state->symbols->push_scope();
83a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke
842b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_node, ast, link, & state->translation_unit)
85304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      ast->hir(instructions, state);
86d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick}
87d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick
88d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick
890104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick/**
900104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * If a conversion is available, convert one operand to a different type
910104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick *
920104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * The \c from \c ir_rvalue is converted "in place".
930104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick *
940104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param to     Type that the operand it to be converted to
950104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param from   Operand that is being converted
960104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param state  GLSL compiler state
970104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick *
980104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \return
990104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * If a conversion is possible (or unnecessary), \c true is returned.
1000104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * Otherwise \c false is returned.
1010104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick */
102f32d3df8ab2b7c6c746f46870edc4b284cea50caKenneth Graunkebool
103bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickapply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
1040104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick			  struct _mesa_glsl_parse_state *state)
1050104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick{
106953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
107bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   if (to->base_type == from->type->base_type)
1080104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return true;
1090104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1100104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   /* This conversion was added in GLSL 1.20.  If the compilation mode is
1110104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    * GLSL 1.10, the conversion is skipped.
1120104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    */
1130104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   if (state->language_version < 120)
1140104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return false;
1150104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1160104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
1170104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *
1180104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    "There are no implicit array or structure conversions. For
1190104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    example, an array of int cannot be implicitly converted to an
1200104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    array of float. There are no implicit conversions between
1210104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    signed and unsigned integers."
1220104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    */
1230104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   /* FINISHME: The above comment is partially a lie.  There is int/uint
1240104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    * FINISHME: conversion for immediate constants.
1250104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    */
126bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   if (!to->is_float() || !from->type->is_numeric())
1270104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return false;
1280104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
129506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke   /* Convert to a floating point type with the same number of components
130506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke    * as the original type - i.e. int to float, not int to vec4.
131506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke    */
132506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke   to = glsl_type::get_instance(GLSL_TYPE_FLOAT, from->type->vector_elements,
133506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke			        from->type->matrix_columns);
134506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke
135bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   switch (from->type->base_type) {
1360104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   case GLSL_TYPE_INT:
1371660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      from = new(ctx) ir_expression(ir_unop_i2f, to, from, NULL);
1380104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      break;
1390104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   case GLSL_TYPE_UINT:
1401660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      from = new(ctx) ir_expression(ir_unop_u2f, to, from, NULL);
1410104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      break;
1420104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   case GLSL_TYPE_BOOL:
1431660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      from = new(ctx) ir_expression(ir_unop_b2f, to, from, NULL);
144dc58b3f8ccd817fdee390a3df5b8e0fb29d5397cEric Anholt      break;
1450104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   default:
1460104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      assert(0);
1470104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   }
1480104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1490104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   return true;
1500104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick}
1510104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1520104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
153a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
154bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickarithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
155a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick		       bool multiply,
156a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
157a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
158336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *type_a = value_a->type;
159336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *type_b = value_b->type;
1600104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
161a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
162a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
163a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The arithmetic binary operators add (+), subtract (-),
164a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    multiply (*), and divide (/) operate on integer and
165a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    floating-point scalars, vectors, and matrices."
166a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
16760b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   if (!type_a->is_numeric() || !type_b->is_numeric()) {
168a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state,
169a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "Operands to arithmetic operators must be numeric");
1700471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
171a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
172a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
173a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
174a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "If one operand is floating-point based and the other is
175a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    not, then the conversions from Section 4.1.10 "Implicit
176a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    Conversions" are applied to the non-floating-point-based operand."
177a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1780104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   if (!apply_implicit_conversion(type_a, value_b, state)
1790104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick       && !apply_implicit_conversion(type_b, value_a, state)) {
180a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state,
181a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "Could not implicitly convert operands to "
182a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "arithmetic operator");
1830104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return glsl_type::error_type;
184a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
185336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   type_a = value_a->type;
186336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   type_b = value_b->type;
187336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt
188a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "If the operands are integer types, they must both be signed or
189a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    both be unsigned."
190a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
191a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * From this rule and the preceeding conversion it can be inferred that
192a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
19360b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick    * The is_numeric check above already filtered out the case where either
19460b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick    * type is not one of these, so now the base types need only be tested for
19560b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick    * equality.
196a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
197a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (type_a->base_type != type_b->base_type) {
198a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state,
199a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "base type mismatch for arithmetic operator");
2000471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
201a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
202a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
203a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "All arithmetic binary operators result in the same fundamental type
204a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    (signed integer, unsigned integer, or floating-point) as the
205a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    operands they operate on, after operand type conversion. After
206a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    conversion, the following cases are valid
207a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
208a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    * The two operands are scalars. In this case the operation is
209a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      applied, resulting in a scalar."
210a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
211cb36f8aaeeb09660843316270a781948f773d90bIan Romanick   if (type_a->is_scalar() && type_b->is_scalar())
212a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return type_a;
213a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
214a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*   "* One operand is a scalar, and the other is a vector or matrix.
215a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      In this case, the scalar operation is applied independently to each
216a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      component of the vector or matrix, resulting in the same size
217a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      vector or matrix."
218a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
219cb36f8aaeeb09660843316270a781948f773d90bIan Romanick   if (type_a->is_scalar()) {
220cb36f8aaeeb09660843316270a781948f773d90bIan Romanick      if (!type_b->is_scalar())
221a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 return type_b;
222cb36f8aaeeb09660843316270a781948f773d90bIan Romanick   } else if (type_b->is_scalar()) {
223a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return type_a;
224a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
225a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
226a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* All of the combinations of <scalar, scalar>, <vector, scalar>,
227a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
228a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * handled.
229a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
23060b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   assert(!type_a->is_scalar());
23160b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   assert(!type_b->is_scalar());
232a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
233a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*   "* The two operands are vectors of the same size. In this case, the
234a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operation is done component-wise resulting in the same size
235a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      vector."
236a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
237a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick   if (type_a->is_vector() && type_b->is_vector()) {
238a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      if (type_a == type_b) {
239a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 return type_a;
240a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      } else {
241a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 _mesa_glsl_error(loc, state,
242a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt			  "vector size mismatch for arithmetic operator");
243a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 return glsl_type::error_type;
244a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      }
245a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
246a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
247a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* All of the combinations of <scalar, scalar>, <vector, scalar>,
248a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
249a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * <vector, vector> have been handled.  At least one of the operands must
250a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * be matrix.  Further, since there are no integer matrix types, the base
251a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * type of both operands must be float.
252a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
25360b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   assert(type_a->is_matrix() || type_b->is_matrix());
254a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   assert(type_a->base_type == GLSL_TYPE_FLOAT);
255a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   assert(type_b->base_type == GLSL_TYPE_FLOAT);
256a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
257a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*   "* The operator is add (+), subtract (-), or divide (/), and the
258a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operands are matrices with the same number of rows and the same
259a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      number of columns. In this case, the operation is done component-
260a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      wise resulting in the same size matrix."
261a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    * The operator is multiply (*), where both operands are matrices or
262a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      one operand is a vector and the other a matrix. A right vector
263a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operand is treated as a column vector and a left vector operand as a
264a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      row vector. In all these cases, it is required that the number of
265a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      columns of the left operand is equal to the number of rows of the
266a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      right operand. Then, the multiply (*) operation does a linear
267a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      algebraic multiply, yielding an object that has the same number of
268a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      rows as the left operand and the same number of columns as the right
269a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operand. Section 5.10 "Vector and Matrix Operations" explains in
270a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      more detail how vectors and matrices are operated on."
271a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
272a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (! multiply) {
273a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      if (type_a == type_b)
274a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 return type_a;
275a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
276fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick      if (type_a->is_matrix() && type_b->is_matrix()) {
277c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 /* Matrix multiply.  The columns of A must match the rows of B.  Given
278c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * the other previously tested constraints, this means the vector type
279c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * of a row from A must be the same as the vector type of a column from
280c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * B.
281c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  */
282c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 if (type_a->row_type() == type_b->column_type()) {
283c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	    /* The resulting matrix has the number of columns of matrix B and
284c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     * the number of rows of matrix A.  We get the row count of A by
285c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     * looking at the size of a vector that makes up a column.  The
286c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     * transpose (size of a row) is done for B.
287c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     */
288a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	    const glsl_type *const type =
289c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	       glsl_type::get_instance(type_a->base_type,
290c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick				       type_a->column_type()->vector_elements,
291c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick				       type_b->row_type()->vector_elements);
292a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	    assert(type != glsl_type::error_type);
293a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt
294a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	    return type;
295a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
296fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick      } else if (type_a->is_matrix()) {
297a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 /* A is a matrix and B is a column vector.  Columns of A must match
298c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * rows of B.  Given the other previously tested constraints, this
299c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * means the vector type of a row from A must be the same as the
300c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * vector the type of B.
301a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  */
30247c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	 if (type_a->row_type() == type_b) {
30347c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    /* The resulting vector has a number of elements equal to
30447c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	     * the number of rows of matrix A. */
30547c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    const glsl_type *const type =
30647c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	       glsl_type::get_instance(type_a->base_type,
30747c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth				       type_a->column_type()->vector_elements,
30847c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth				       1);
30947c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    assert(type != glsl_type::error_type);
31047c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth
31147c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    return type;
31247c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	 }
313a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
314fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick	 assert(type_b->is_matrix());
315a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
316c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 /* A is a row vector and B is a matrix.  Columns of A must match rows
317c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * of B.  Given the other previously tested constraints, this means
318c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * the type of A must be the same as the vector type of a column from
319c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * B.
320a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  */
32147c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	 if (type_a == type_b->column_type()) {
32247c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    /* The resulting vector has a number of elements equal to
32347c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	     * the number of columns of matrix B. */
32447c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    const glsl_type *const type =
32547c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	       glsl_type::get_instance(type_a->base_type,
32647c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth				       type_b->row_type()->vector_elements,
32747c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth				       1);
32847c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    assert(type != glsl_type::error_type);
32947c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth
33047c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    return type;
33147c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	 }
332a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
333a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt
334a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication");
335a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      return glsl_type::error_type;
336a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
337a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
338a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
339a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "All other cases are illegal."
340a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
341a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt   _mesa_glsl_error(loc, state, "type mismatch");
3420471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::error_type;
343a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
344a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
345a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
346a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
34765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholtunary_arithmetic_result_type(const struct glsl_type *type,
34865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt			     struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
349a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
350a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 57:
351a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
352a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The arithmetic unary operators negate (-), post- and pre-increment
353a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     and decrement (-- and ++) operate on integer or floating-point
354a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     values (including vectors and matrices). All unary operators work
355a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     component-wise on their operands. These result with the same type
356a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     they operated on."
357a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
35865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   if (!type->is_numeric()) {
35965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state,
36065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "Operands to arithmetic operators must be numeric");
3610471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
36265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   }
363a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
364a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return type;
365a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
366a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
367a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
368a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
369a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickmodulus_result_type(const struct glsl_type *type_a,
37065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		    const struct glsl_type *type_b,
37165e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		    struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
372a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
373a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
374a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The operator modulus (%) operates on signed or unsigned integers or
375a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    integer vectors. The operand types must both be signed or both be
376a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    unsigned."
377a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
37840176e249f72b6090204611873b19aed3da67c71Ian Romanick   if (!type_a->is_integer() || !type_b->is_integer()
379a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       || (type_a->base_type != type_b->base_type)) {
38065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state, "type mismatch");
3810471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
382a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
383a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
384a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The operands cannot be vectors of differing size. If one operand is
385a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    a scalar and the other vector, then the scalar is applied component-
386a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    wise to the vector, resulting in the same type as the vector. If both
387a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    are vectors of the same size, the result is computed component-wise."
388a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
389a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick   if (type_a->is_vector()) {
390a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick      if (!type_b->is_vector()
391a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  || (type_a->vector_elements == type_b->vector_elements))
392a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 return type_a;
393a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else
394a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return type_b;
395a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
396a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The operator modulus (%) is not defined for any other data types
397a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    (non-integer types)."
398a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
39965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   _mesa_glsl_error(loc, state, "type mismatch");
4000471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::error_type;
401a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
402a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
403a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
404a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
405bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickrelational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
40665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
407a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
408336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *type_a = value_a->type;
409336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *type_b = value_b->type;
4100150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick
411a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
412a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The relational operators greater than (>), less than (<), greater
413a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    than or equal (>=), and less than or equal (<=) operate only on
414a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    scalar integer and scalar floating-point expressions."
415a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
416a6d653dcbbee3158f9ea7b284bdeb1a8432f0fcbIan Romanick   if (!type_a->is_numeric()
417a6d653dcbbee3158f9ea7b284bdeb1a8432f0fcbIan Romanick       || !type_b->is_numeric()
418cb36f8aaeeb09660843316270a781948f773d90bIan Romanick       || !type_a->is_scalar()
41965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt       || !type_b->is_scalar()) {
42065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state,
42165e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "Operands to relational operators must be scalar and "
42265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "numeric");
4230471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
42465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   }
425a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
426a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "Either the operands' types must match, or the conversions from
427a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    Section 4.1.10 "Implicit Conversions" will be applied to the integer
428a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    operand, after which the types must match."
429a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
4300150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick   if (!apply_implicit_conversion(type_a, value_b, state)
4310150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick       && !apply_implicit_conversion(type_b, value_a, state)) {
43265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state,
43365e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "Could not implicitly convert operands to "
43465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "relational operator");
4350150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick      return glsl_type::error_type;
436a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
437336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   type_a = value_a->type;
438336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   type_b = value_b->type;
439a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
44065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   if (type_a->base_type != type_b->base_type) {
44165e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state, "base type mismatch");
4420471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
44365e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   }
444a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
445a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The result is scalar Boolean."
446a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
4470471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::bool_type;
448a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
449a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
450a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
4510bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick/**
4520bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Validates that a value can be assigned to a location with a specified type
4530bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
4540bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Validates that \c rhs can be assigned to some location.  If the types are
4550bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * not an exact match but an automatic conversion is possible, \c rhs will be
4560bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * converted.
4570bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
4580bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \return
4590bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
4600bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Otherwise the actual RHS to be assigned will be returned.  This may be
4610bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \c rhs, or it may be \c rhs after some type conversion.
4620bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
4630bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \note
4640bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * In addition to being used for assignments, this function is used to
4650bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * type-check return values.
4660bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick */
467fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
468336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholtvalidate_assignment(struct _mesa_glsl_parse_state *state,
469336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt		    const glsl_type *lhs_type, ir_rvalue *rhs)
4700bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick{
471336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *rhs_type = rhs->type;
4720bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
4730bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   /* If there is already some error in the RHS, just return it.  Anything
4740bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    * else will lead to an avalanche of error message back to the user.
4750bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    */
4760bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   if (rhs_type->is_error())
4770bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick      return rhs;
4780bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
4790bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   /* If the types are identical, the assignment can trivially proceed.
4800bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    */
4810bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   if (rhs_type == lhs_type)
4820bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick      return rhs;
4830bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
4840157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick   /* If the array element types are the same and the size of the LHS is zero,
4850157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    * the assignment is okay.
4860157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    *
4870157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
4880157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    * is handled by ir_dereference::is_lvalue.
4890157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    */
4900157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick   if (lhs_type->is_array() && rhs->type->is_array()
4910157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       && (lhs_type->element_type() == rhs->type->element_type())
4920157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       && (lhs_type->array_size() == 0)) {
4930157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      return rhs;
4940157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick   }
4950157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
496336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   /* Check for implicit conversion in GLSL 1.20 */
497336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   if (apply_implicit_conversion(lhs_type, rhs, state)) {
498336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt      rhs_type = rhs->type;
499336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt      if (rhs_type == lhs_type)
500336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt	 return rhs;
501336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   }
502336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt
5030bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   return NULL;
5040bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick}
5050bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
50610a685218610e737e23d2d8a243ed6ff6613becdEric Anholtir_rvalue *
50710a685218610e737e23d2d8a243ed6ff6613becdEric Anholtdo_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
50810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	      ir_rvalue *lhs, ir_rvalue *rhs,
50910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	      YYLTYPE lhs_loc)
51010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt{
511953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
51210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
51310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
51410a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   if (!error_emitted) {
51510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      if (!lhs->is_lvalue()) {
51610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	 _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
51710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	 error_emitted = true;
51810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      }
51910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   }
52010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
521336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   ir_rvalue *new_rhs = validate_assignment(state, lhs->type, rhs);
52210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   if (new_rhs == NULL) {
52310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      _mesa_glsl_error(& lhs_loc, state, "type mismatch");
52410a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   } else {
52510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      rhs = new_rhs;
5260157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
5270157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      /* If the LHS array was not declared with a size, it takes it size from
5280157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       * the RHS.  If the LHS is an l-value and a whole array, it must be a
5290157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       * dereference of a variable.  Any other case would require that the LHS
5300157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       * is either not an l-value or not a whole array.
5310157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       */
5320157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      if (lhs->type->array_size() == 0) {
5330157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 ir_dereference *const d = lhs->as_dereference();
5340157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
5350157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 assert(d != NULL);
5360157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
53736ea28646c666ac2af9b43c47e65f9f53ffcc390Ian Romanick	 ir_variable *const var = d->variable_referenced();
5380157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
5390157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 assert(var != NULL);
5400157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
54163f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	 if (var->max_array_access >= unsigned(rhs->type->array_size())) {
54263f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	    /* FINISHME: This should actually log the location of the RHS. */
54363f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	    _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
54463f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick			     "previous access",
54563f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick			     var->max_array_access);
54663f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	 }
54763f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick
548f38d15b80d4e4c8ecb7a76087cdc49835f0aa271Ian Romanick	 var->type = glsl_type::get_array_instance(lhs->type->element_type(),
5490157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick						   rhs->type->array_size());
5509703ed05e684f4269cd8af27c94e9b6bf8781d85Eric Anholt	 d->type = var->type;
5510157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      }
55210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   }
55310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
5542731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt   /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
5552731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * but not post_inc) need the converted assigned value as an rvalue
5562731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * to handle things like:
5572731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    *
5582731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * i = j += 1;
5592731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    *
5602731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * So we always just store the computed value being assigned to a
5612731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * temporary and return a deref of that temporary.  If the rvalue
5622731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * ends up not being used, the temp will get copy-propagated out.
5632731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    */
5647e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick   ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp",
5657e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick					   ir_var_temporary);
566e33c10328caec29616a5433b1d1df9088f3a84dfEric Anholt   ir_dereference_variable *deref_var = new(ctx) ir_dereference_variable(var);
567ae805922b7e3cdaf3aee26c3b799fe3608669bbaEric Anholt   instructions->push_tail(var);
568e33c10328caec29616a5433b1d1df9088f3a84dfEric Anholt   instructions->push_tail(new(ctx) ir_assignment(deref_var,
5691660a2954797e056caba319c5d6c70b0d4be22feCarl Worth						  rhs,
5701660a2954797e056caba319c5d6c70b0d4be22feCarl Worth						  NULL));
571e33c10328caec29616a5433b1d1df9088f3a84dfEric Anholt   deref_var = new(ctx) ir_dereference_variable(var);
5722731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt
5738e9ce2eb56a087c2544112700ae1abe3f96648ddIan Romanick   if (!error_emitted)
5748e9ce2eb56a087c2544112700ae1abe3f96648ddIan Romanick      instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var, NULL));
57510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
5761660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   return new(ctx) ir_dereference_variable(var);
57710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt}
5780bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
579de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholtstatic ir_rvalue *
580959a9ecdd8fbc3375e4149f2b44d253622ff12eeEric Anholtget_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue)
581de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt{
5821660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   void *ctx = talloc_parent(lvalue);
583de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   ir_variable *var;
584de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
5857e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick   var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp",
5867e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick			      ir_var_temporary);
58743b5b03d67ce890e867c81d4a5cfc4871d711d43Eric Anholt   instructions->push_tail(var);
588de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   var->mode = ir_var_auto;
589de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
5901660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
5911660a2954797e056caba319c5d6c70b0d4be22feCarl Worth						  lvalue, NULL));
592de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
593de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   /* Once we've created this temporary, mark it read only so it's no
594de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt    * longer considered an lvalue.
595de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt    */
596de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   var->read_only = true;
597de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
5981660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   return new(ctx) ir_dereference_variable(var);
599de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt}
600de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
601de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
602fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
6030044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_node::hir(exec_list *instructions,
60418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick	      struct _mesa_glsl_parse_state *state)
60518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick{
60618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   (void) instructions;
60718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   (void) state;
60818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
60918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   return NULL;
61018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick}
61118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
61218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
613fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
6140044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_expression::hir(exec_list *instructions,
61518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick		    struct _mesa_glsl_parse_state *state)
616a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
617953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
618a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   static const int operations[AST_NUM_OPERATORS] = {
619a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_assign doesn't convert to ir_expression. */
620a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_plus doesn't convert to ir_expression. */
621a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_neg,
622a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_add,
623a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_sub,
624a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mul,
625a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_div,
626a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mod,
627a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lshift,
628a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_rshift,
629a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_less,
630a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_greater,
631a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lequal,
632a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_gequal,
633a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_equal,
634a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_nequal,
635a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_and,
636a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_xor,
637a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_or,
638a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_bit_not,
639a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_and,
640a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_xor,
641a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_or,
642a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_logic_not,
643a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
644a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Note: The following block of expression types actually convert
645a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * to multiple IR instructions.
646a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
647a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mul,     /* ast_mul_assign */
648a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_div,     /* ast_div_assign */
649a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mod,     /* ast_mod_assign */
650a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_add,     /* ast_add_assign */
651a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_sub,     /* ast_sub_assign */
652a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lshift,  /* ast_ls_assign */
653a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_rshift,  /* ast_rs_assign */
654a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_and, /* ast_and_assign */
655a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_xor, /* ast_xor_assign */
656a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_or,  /* ast_or_assign */
657a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
658a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_conditional doesn't convert to ir_expression. */
659de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_add,     /* ast_pre_inc. */
660de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_sub,     /* ast_pre_dec. */
661de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_add,     /* ast_post_inc. */
662de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_sub,     /* ast_post_dec. */
663a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_field_selection doesn't conv to ir_expression. */
664a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_array_index doesn't convert to ir_expression. */
665a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_function_call doesn't conv to ir_expression. */
666a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_identifier doesn't convert to ir_expression. */
667a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_int_constant doesn't convert to ir_expression. */
668a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_uint_constant doesn't conv to ir_expression. */
669a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_float_constant doesn't conv to ir_expression. */
670a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_bool_constant doesn't conv to ir_expression. */
671a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_sequence doesn't convert to ir_expression. */
672a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   };
673fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke   ir_rvalue *result = NULL;
6741c325af4d6b907e0a47ab7f868a2a78f054f153fAras Pranckevicius   ir_rvalue *op[3];
6750471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   const struct glsl_type *type = glsl_type::error_type;
676a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   bool error_emitted = false;
677a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   YYLTYPE loc;
678a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
67918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   loc = this->get_location();
680a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
68118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   switch (this->oper) {
6826652af36fe8994b1621d882fcc230d320908a2a3Ian Romanick   case ast_assign: {
68318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
68418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
685a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
68610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      result = do_assignment(instructions, state, op[0], op[1],
68710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt			     this->subexpressions[0]->get_location());
68810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      error_emitted = result->type->is_error();
68910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      type = result->type;
690a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
6916652af36fe8994b1621d882fcc230d320908a2a3Ian Romanick   }
692a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
693a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_plus:
69418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
695a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
696c24bcad9f88379ffba9e2f0ff92f22cdf60c2927Carl Worth      type = unary_arithmetic_result_type(op[0]->type, state, & loc);
697c24bcad9f88379ffba9e2f0ff92f22cdf60c2927Carl Worth
698c24bcad9f88379ffba9e2f0ff92f22cdf60c2927Carl Worth      error_emitted = type->is_error();
699a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
700a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      result = op[0];
701a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
702a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
703a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_neg:
70418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
705a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
70665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = unary_arithmetic_result_type(op[0]->type, state, & loc);
707a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
70865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
709a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
7101660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
7111660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], NULL);
712a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
713a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
714a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_add:
715a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sub:
716a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mul:
717a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_div:
71818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
71918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
720a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
721bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      type = arithmetic_result_type(op[0], op[1],
72218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick				    (this->oper == ast_mul),
723a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt				    state, & loc);
724a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      error_emitted = type->is_error();
725a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
7261660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
7271660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
728a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
729a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
730a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mod:
73118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
73218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
733a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
73465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
735a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
73618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      assert(operations[this->oper] == ir_binop_mod);
737a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
7381660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
7391660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
74065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
741a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
742a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
743a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_lshift:
744a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_rshift:
745183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      _mesa_glsl_error(& loc, state, "FINISHME: implement bit-shift operators");
746183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      error_emitted = true;
747a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
748a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
749a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_less:
750a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_greater:
751a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_lequal:
752a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_gequal:
75318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
75418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
755a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
75665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = relational_result_type(op[0], op[1], state, & loc);
757a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
758a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* The relational operators must either generate an error or result
759a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * in a scalar boolean.  See page 57 of the GLSL 1.50 spec.
760a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
761a43817a483a8c4a480ef4e6dfda2cef899300eb0Ian Romanick      assert(type->is_error()
762a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	     || ((type->base_type == GLSL_TYPE_BOOL)
763cb36f8aaeeb09660843316270a781948f773d90bIan Romanick		 && type->is_scalar()));
764a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
7651660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
7661660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
76765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
768a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
769a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
770a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_nequal:
771a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_equal:
7726e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
7736e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
7746e659caaa946339a2de3890a8bed091ccb65102aIan Romanick
7756e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
7766e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *
7776e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    "The equality operators equal (==), and not equal (!=)
7786e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    operate on all types. They result in a scalar Boolean. If
7796e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    the operand types do not match, then there must be a
7806e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    conversion from Section 4.1.10 "Implicit Conversions"
7816e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    applied to one operand that can make them match, in which
7826e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    case this conversion is done."
7836e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       */
784bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      if ((!apply_implicit_conversion(op[0]->type, op[1], state)
785bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick	   && !apply_implicit_conversion(op[1]->type, op[0], state))
786212b0327b47033442842a7be3d7fb10e08e2bf66Ian Romanick	  || (op[0]->type != op[1]->type)) {
7876e659caaa946339a2de3890a8bed091ccb65102aIan Romanick	 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
7886e659caaa946339a2de3890a8bed091ccb65102aIan Romanick			  "type", (this->oper == ast_equal) ? "==" : "!=");
7896e659caaa946339a2de3890a8bed091ccb65102aIan Romanick	 error_emitted = true;
790a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick      } else if ((state->language_version <= 110)
791a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick		 && (op[0]->type->is_array() || op[1]->type->is_array())) {
792a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick	 _mesa_glsl_error(& loc, state, "array comparisons forbidden in "
793a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick			  "GLSL 1.10");
794a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick	 error_emitted = true;
7956e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      }
7966e659caaa946339a2de3890a8bed091ccb65102aIan Romanick
7971660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
7981660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
7996e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      type = glsl_type::bool_type;
8006e659caaa946339a2de3890a8bed091ccb65102aIan Romanick
8016e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      assert(result->type == glsl_type::bool_type);
802a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
803a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
804a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_and:
805a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_xor:
806a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_or:
8071eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      op[0] = this->subexpressions[0]->hir(instructions, state);
8081eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      op[1] = this->subexpressions[1]->hir(instructions, state);
8091eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
8101eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      if (state->language_version < 130) {
8111eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 _mesa_glsl_error(&loc, state, "bit-wise operations require GLSL 1.30");
8121eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 error_emitted = true;
8131eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      }
8141eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
8151eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      if (!op[0]->type->is_integer()) {
8161eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 _mesa_glsl_error(&loc, state, "LHS of `%s' must be an integer",
8171eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke			  operator_string(this->oper));
8181eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 error_emitted = true;
8191eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      }
8201eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
8211eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      if (!op[1]->type->is_integer()) {
8221eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 _mesa_glsl_error(&loc, state, "RHS of `%s' must be an integer",
8231eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke			  operator_string(this->oper));
8241eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 error_emitted = true;
8251eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      }
8261eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
8271eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      if (op[0]->type->base_type != op[1]->type->base_type) {
8281eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 _mesa_glsl_error(&loc, state, "operands of `%s' must have the same "
8291eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke			  "base type", operator_string(this->oper));
8301eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 error_emitted = true;
8311eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      }
8321eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
8331eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      if (op[0]->type->is_vector() && op[1]->type->is_vector()
8341eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	  && op[0]->type->vector_elements != op[1]->type->vector_elements) {
8351eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 _mesa_glsl_error(&loc, state, "operands of `%s' cannot be vectors of "
8361eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke			  "different sizes", operator_string(this->oper));
8371eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 error_emitted = true;
8381eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      }
8391eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
8401eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      type = op[0]->type->is_scalar() ? op[1]->type : op[0]->type;
8411eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      result = new(ctx) ir_expression(operations[this->oper], type,
8421eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke				      op[0], op[1]);
8431eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
8441eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      break;
8451eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
846a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_not:
8471eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      op[0] = this->subexpressions[0]->hir(instructions, state);
8481eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
8491eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      if (state->language_version < 130) {
8501eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 _mesa_glsl_error(&loc, state, "bit-wise operations require GLSL 1.30");
8511eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 error_emitted = true;
8521eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      }
8531eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
8541eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      if (!op[0]->type->is_integer()) {
8551eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 _mesa_glsl_error(&loc, state, "operand of `~' must be an integer");
8561eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 error_emitted = true;
8571eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      }
8581eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
8591eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      type = op[0]->type;
8601eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      result = new(ctx) ir_expression(ir_unop_bit_not, type, op[0], NULL);
861a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
862a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
8634950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   case ast_logic_and: {
864b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
865b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
866b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
867b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt	 YYLTYPE loc = this->subexpressions[0]->get_location();
868b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
869b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt	 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
870b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt			  operator_string(this->oper));
871ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt	 error_emitted = true;
872b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      }
873b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
87444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      ir_constant *op0_const = op[0]->constant_expression_value();
87544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      if (op0_const) {
87644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 if (op0_const->value.b[0]) {
87744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    op[1] = this->subexpressions[1]->hir(instructions, state);
87844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt
87944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
88044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       YYLTYPE loc = this->subexpressions[1]->get_location();
88144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt
88244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       _mesa_glsl_error(& loc, state,
88344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt				"RHS of `%s' must be scalar boolean",
88444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt				operator_string(this->oper));
88544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       error_emitted = true;
88644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    }
88744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    result = op[1];
88844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 } else {
88944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    result = op0_const;
89044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 }
89144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 type = glsl_type::bool_type;
89244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      } else {
89381d664f099a5fd5fac777480532fb4307d591451Ian Romanick	 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
8947e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick						       "and_tmp",
8957e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick						       ir_var_temporary);
89681d664f099a5fd5fac777480532fb4307d591451Ian Romanick	 instructions->push_tail(tmp);
89781d664f099a5fd5fac777480532fb4307d591451Ian Romanick
8981660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_if *const stmt = new(ctx) ir_if(op[0]);
89944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 instructions->push_tail(stmt);
9004950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
90144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 op[1] = this->subexpressions[1]->hir(&stmt->then_instructions, state);
9024950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
90344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
90444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    YYLTYPE loc = this->subexpressions[1]->get_location();
9054950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
90644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    _mesa_glsl_error(& loc, state,
90744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt			     "RHS of `%s' must be scalar boolean",
90844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt			     operator_string(this->oper));
90944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    error_emitted = true;
91044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 }
911b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
9121660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
91344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const then_assign =
9141660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(then_deref, op[1], NULL);
91544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->then_instructions.push_tail(then_assign);
9164950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9171660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
91844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const else_assign =
9191660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false), NULL);
92044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->else_instructions.push_tail(else_assign);
9214950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9221660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 result = new(ctx) ir_dereference_variable(tmp);
92344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 type = tmp->type;
92444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      }
9254950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      break;
9264950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   }
9274950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9284950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   case ast_logic_or: {
9294950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
9304950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9314950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
9324950a68bf22ede6f4f368c9783e5401816159574Eric Anholt	 YYLTYPE loc = this->subexpressions[0]->get_location();
9334950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9344950a68bf22ede6f4f368c9783e5401816159574Eric Anholt	 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
9354950a68bf22ede6f4f368c9783e5401816159574Eric Anholt			  operator_string(this->oper));
9364950a68bf22ede6f4f368c9783e5401816159574Eric Anholt	 error_emitted = true;
9374950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      }
9384950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
93944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      ir_constant *op0_const = op[0]->constant_expression_value();
94044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      if (op0_const) {
94144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 if (op0_const->value.b[0]) {
94244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    result = op0_const;
94344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 } else {
94444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    op[1] = this->subexpressions[1]->hir(instructions, state);
94544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt
94644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
94744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       YYLTYPE loc = this->subexpressions[1]->get_location();
94844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt
94944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       _mesa_glsl_error(& loc, state,
95044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt				"RHS of `%s' must be scalar boolean",
95144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt				operator_string(this->oper));
95244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       error_emitted = true;
95344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    }
95444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    result = op[1];
95544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 }
95644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 type = glsl_type::bool_type;
95744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      } else {
958dfd30ca6a95a7d95835dad78ffe1fba4d1f4ef69Kenneth Graunke	 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
9597e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick						       "or_tmp",
9607e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick						       ir_var_temporary);
9610b9ae3befb0bf80e000b159fd44c961a144f9c36Ian Romanick	 instructions->push_tail(tmp);
9624950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
96381d664f099a5fd5fac777480532fb4307d591451Ian Romanick	 ir_if *const stmt = new(ctx) ir_if(op[0]);
96481d664f099a5fd5fac777480532fb4307d591451Ian Romanick	 instructions->push_tail(stmt);
96581d664f099a5fd5fac777480532fb4307d591451Ian Romanick
966a0879b9dd438d78635f047cdd5ed4c72bc831b60Eric Anholt	 op[1] = this->subexpressions[1]->hir(&stmt->else_instructions, state);
9674950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
96844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
96944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    YYLTYPE loc = this->subexpressions[1]->get_location();
9704950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
97144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    _mesa_glsl_error(& loc, state, "RHS of `%s' must be scalar boolean",
97244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt			     operator_string(this->oper));
97344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    error_emitted = true;
97444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 }
9754950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9761660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
97744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const then_assign =
9781660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true), NULL);
97944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->then_instructions.push_tail(then_assign);
9804950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9811660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
98244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const else_assign =
9831660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(else_deref, op[1], NULL);
98444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->else_instructions.push_tail(else_assign);
9854950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9861660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 result = new(ctx) ir_dereference_variable(tmp);
98744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 type = tmp->type;
98844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      }
9894950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      break;
9904950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   }
9914950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9924950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   case ast_logic_xor:
9934950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
9944950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      op[1] = this->subexpressions[1]->hir(instructions, state);
9954950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9964950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9971660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
9981660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
999ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt      type = glsl_type::bool_type;
1000a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1001a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1002a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt   case ast_logic_not:
1003a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
1004a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
1005a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
1006a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt	 YYLTYPE loc = this->subexpressions[0]->get_location();
1007a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
1008a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt	 _mesa_glsl_error(& loc, state,
1009a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt			  "operand of `!' must be scalar boolean");
1010ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt	 error_emitted = true;
1011a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      }
1012a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
10131660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
10141660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], NULL);
1015ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt      type = glsl_type::bool_type;
1016a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      break;
1017a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
1018a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mul_assign:
1019a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_div_assign:
1020a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_add_assign:
1021a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sub_assign: {
102218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
102318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
1024a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1025bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      type = arithmetic_result_type(op[0], op[1],
102618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick				    (this->oper == ast_mul_assign),
1027a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt				    state, & loc);
1028a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
10291660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
10301660a2954797e056caba319c5d6c70b0d4be22feCarl Worth						   op[0], op[1]);
1031a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
10323e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      result = do_assignment(instructions, state,
10338273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt			     op[0]->clone(ctx, NULL), temp_rhs,
103410a685218610e737e23d2d8a243ed6ff6613becdEric Anholt			     this->subexpressions[0]->get_location());
103510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      type = result->type;
103610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      error_emitted = (op[0]->type->is_error());
1037a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1038a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* GLSL 1.10 does not allow array assignment.  However, we don't have to
1039a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * explicitly test for this because none of the binary expression
1040a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * operators allow array operands either.
1041a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1042a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1043a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1044a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1045a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
104648a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt   case ast_mod_assign: {
104748a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
104848a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      op[1] = this->subexpressions[1]->hir(instructions, state);
104948a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
105065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
105148a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
105248a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      assert(operations[this->oper] == ir_binop_mod);
105348a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
1054768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick      ir_rvalue *temp_rhs;
10551660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
10561660a2954797e056caba319c5d6c70b0d4be22feCarl Worth					op[0], op[1]);
105748a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
10583e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      result = do_assignment(instructions, state,
10598273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt			     op[0]->clone(ctx, NULL), temp_rhs,
106048a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt			     this->subexpressions[0]->get_location());
106148a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      type = result->type;
106265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
106348a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      break;
106448a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt   }
1065a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1066a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_ls_assign:
1067a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_rs_assign:
1068183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      _mesa_glsl_error(& loc, state,
1069183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt		       "FINISHME: implement bit-shift assignment operators");
1070183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      error_emitted = true;
1071251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
1072a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1073a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_and_assign:
1074a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_xor_assign:
1075a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_or_assign:
1076183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      _mesa_glsl_error(& loc, state,
1077183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt		       "FINISHME: implement logic assignment operators");
1078183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      error_emitted = true;
1079251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
1080a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
108196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick   case ast_conditional: {
108296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
108396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
108496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
108596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *
108696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    "The ternary selection operator (?:). It operates on three
108796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    expressions (exp1 ? exp2 : exp3). This operator evaluates the
108896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    first expression, which must result in a scalar Boolean."
108996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
109096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
109196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 YYLTYPE loc = this->subexpressions[0]->get_location();
109296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
109396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 _mesa_glsl_error(& loc, state, "?: condition must be scalar boolean");
109496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 error_emitted = true;
109596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      }
109696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
109796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* The :? operator is implemented by generating an anonymous temporary
109896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * followed by an if-statement.  The last instruction in each branch of
109996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * the if-statement assigns a value to the anonymous temporary.  This
110096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * temporary is the r-value of the expression.
110196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
11020ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      exec_list then_instructions;
11030ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      exec_list else_instructions;
110496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
11050ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      op[1] = this->subexpressions[1]->hir(&then_instructions, state);
11060ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      op[2] = this->subexpressions[2]->hir(&else_instructions, state);
110796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
110896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
110996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *
111096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     "The second and third expressions can be any type, as
111196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     long their types match, or there is a conversion in
111296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     Section 4.1.10 "Implicit Conversions" that can be applied
111396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     to one of the expressions to make their types match. This
111496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     resulting matching type is the type of the entire
111596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     expression."
111696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
1117bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      if ((!apply_implicit_conversion(op[1]->type, op[2], state)
1118bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick	   && !apply_implicit_conversion(op[2]->type, op[1], state))
1119db9be2e7aa3a56e43b725ad7725fe6b424e4933eIan Romanick	  || (op[1]->type != op[2]->type)) {
112096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 YYLTYPE loc = this->subexpressions[1]->get_location();
112196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
112296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
112396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick			  "operator must have matching types.");
112496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 error_emitted = true;
11250ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick	 type = glsl_type::error_type;
1126db9be2e7aa3a56e43b725ad7725fe6b424e4933eIan Romanick      } else {
11270ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick	 type = op[1]->type;
112896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      }
112996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
1130f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick      /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1131f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick       *
1132f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick       *    "The second and third expressions must be the same type, but can
1133f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick       *    be of any type other than an array."
1134f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick       */
1135f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick      if ((state->language_version <= 110) && type->is_array()) {
1136f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick	 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
1137f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick			  "operator must not be arrays.");
1138f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick	 error_emitted = true;
1139f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick      }
1140f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick
11417825d3d15710fdfcfc503754862963aac8065480Ian Romanick      ir_constant *cond_val = op[0]->constant_expression_value();
11427825d3d15710fdfcfc503754862963aac8065480Ian Romanick      ir_constant *then_val = op[1]->constant_expression_value();
11437825d3d15710fdfcfc503754862963aac8065480Ian Romanick      ir_constant *else_val = op[2]->constant_expression_value();
11447825d3d15710fdfcfc503754862963aac8065480Ian Romanick
11457825d3d15710fdfcfc503754862963aac8065480Ian Romanick      if (then_instructions.is_empty()
11467825d3d15710fdfcfc503754862963aac8065480Ian Romanick	  && else_instructions.is_empty()
11477825d3d15710fdfcfc503754862963aac8065480Ian Romanick	  && (cond_val != NULL) && (then_val != NULL) && (else_val != NULL)) {
11487825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 result = (cond_val->value.b[0]) ? then_val : else_val;
11497825d3d15710fdfcfc503754862963aac8065480Ian Romanick      } else {
11507e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick	 ir_variable *const tmp =
11517e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick	    new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary);
11520b9ae3befb0bf80e000b159fd44c961a144f9c36Ian Romanick	 instructions->push_tail(tmp);
11530ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
11541660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_if *const stmt = new(ctx) ir_if(op[0]);
11557825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 instructions->push_tail(stmt);
11560ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
11577825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 then_instructions.move_nodes_to(& stmt->then_instructions);
11581660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const then_deref =
11591660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_dereference_variable(tmp);
11607825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 ir_assignment *const then_assign =
11611660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(then_deref, op[1], NULL);
11627825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 stmt->then_instructions.push_tail(then_assign);
11630ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
11647825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 else_instructions.move_nodes_to(& stmt->else_instructions);
11651660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const else_deref =
11661660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_dereference_variable(tmp);
11677825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 ir_assignment *const else_assign =
11681660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(else_deref, op[2], NULL);
11697825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 stmt->else_instructions.push_tail(else_assign);
11700ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
11711660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 result = new(ctx) ir_dereference_variable(tmp);
11727825d3d15710fdfcfc503754862963aac8065480Ian Romanick      }
1173251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
117496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick   }
1175a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1176a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_pre_inc:
117776ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt   case ast_pre_dec: {
117876ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
117976ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
11801660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 op[1] = new(ctx) ir_constant(1.0f);
118176ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      else
11821660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 op[1] = new(ctx) ir_constant(1);
118376ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
1184a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      type = arithmetic_result_type(op[0], op[1], false, state, & loc);
118576ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
1186768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick      ir_rvalue *temp_rhs;
11871660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
11881660a2954797e056caba319c5d6c70b0d4be22feCarl Worth					op[0], op[1]);
118976ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
11903e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      result = do_assignment(instructions, state,
11918273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt			     op[0]->clone(ctx, NULL), temp_rhs,
119276ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt			     this->subexpressions[0]->get_location());
119376ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      type = result->type;
119476ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      error_emitted = op[0]->type->is_error();
119576ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      break;
119676ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt   }
1197a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1198a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_post_inc:
1199de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   case ast_post_dec: {
1200de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
1201de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
12021660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 op[1] = new(ctx) ir_constant(1.0f);
1203de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      else
12041660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 op[1] = new(ctx) ir_constant(1);
1205de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1206de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1207de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1208a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1209de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1210768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick      ir_rvalue *temp_rhs;
12111660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
12121660a2954797e056caba319c5d6c70b0d4be22feCarl Worth					op[0], op[1]);
1213de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1214de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      /* Get a temporary of a copy of the lvalue before it's modified.
1215de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt       * This may get thrown away later.
1216de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt       */
12178273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL));
1218de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
12193e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      (void)do_assignment(instructions, state,
12208273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt			  op[0]->clone(ctx, NULL), temp_rhs,
1221de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt			  this->subexpressions[0]->get_location());
1222de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1223de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      type = result->type;
1224de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      error_emitted = op[0]->type->is_error();
1225a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1226de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   }
1227a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1228a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_field_selection:
122918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = _mesa_ast_field_selection_to_hir(this, instructions, state);
1230a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = result->type;
1231a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1232a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
123327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick   case ast_array_index: {
123427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      YYLTYPE index_loc = subexpressions[1]->get_location();
123527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
123627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      op[0] = subexpressions[0]->hir(instructions, state);
123727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      op[1] = subexpressions[1]->hir(instructions, state);
123827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
123927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
124027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
1241a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick      ir_rvalue *const array = op[0];
1242b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
12431660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_dereference_array(op[0], op[1]);
1244b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
1245b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      /* Do not use op[0] after this point.  Use array.
1246b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick       */
1247b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      op[0] = NULL;
1248b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
124927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
125027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (error_emitted)
125127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 break;
125227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
125363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick      if (!array->type->is_array()
125463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	  && !array->type->is_matrix()
125563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	  && !array->type->is_vector()) {
125627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
125763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			  "cannot dereference non-array / non-matrix / "
125863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			  "non-vector");
125927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
126027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
126127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
126227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (!op[1]->type->is_integer()) {
126327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
126427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick			  "array index must be integer type");
126527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
126627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      } else if (!op[1]->type->is_scalar()) {
126727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
126827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick			  "array index must be scalar");
126927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
127027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
127127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
127227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      /* If the array index is a constant expression and the array has a
127327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * declared size, ensure that the access is in-bounds.  If the array
127427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * index is not a constant expression, ensure that the array has a
127527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * declared size.
127627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       */
127727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      ir_constant *const const_index = op[1]->constant_expression_value();
127827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (const_index != NULL) {
127927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 const int idx = const_index->value.i[0];
128063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 const char *type_name;
128163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 unsigned bound = 0;
128263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick
128363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (array->type->is_matrix()) {
128463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    type_name = "matrix";
128563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else if (array->type->is_vector()) {
128663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    type_name = "vector";
128763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else {
128863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    type_name = "array";
128963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 }
129027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
129127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
129227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *
129327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    "It is illegal to declare an array with a size, and then
129427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    later (in the same shader) index the same array with an
129527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    integral constant expression greater than or equal to the
129627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    declared size. It is also illegal to index an array with a
129727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    negative constant expression."
129827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  */
129963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (array->type->is_matrix()) {
130063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if (array->type->row_type()->vector_elements <= idx) {
130163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       bound = array->type->row_type()->vector_elements;
130263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    }
130363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else if (array->type->is_vector()) {
130463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if (array->type->vector_elements <= idx) {
130563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       bound = array->type->vector_elements;
130663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    }
130763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else {
130863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if ((array->type->array_size() > 0)
130963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick		&& (array->type->array_size() <= idx)) {
131063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       bound = array->type->array_size();
131163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    }
131227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 }
131327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
131463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (bound > 0) {
131563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    _mesa_glsl_error(& loc, state, "%s index must be < %u",
131663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			     type_name, bound);
131763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    error_emitted = true;
131863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else if (idx < 0) {
131963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    _mesa_glsl_error(& loc, state, "%s index must be >= 0",
132063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			     type_name);
132127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	    error_emitted = true;
132227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 }
1323b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
132463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (array->type->is_array()) {
1325a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	    /* If the array is a variable dereference, it dereferences the
1326a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     * whole array, by definition.  Use this to get the variable.
1327a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     *
1328a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     * FINISHME: Should some methods for getting / setting / testing
1329a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     * FINISHME: array access limits be added to ir_dereference?
1330a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     */
1331a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	    ir_variable *const v = array->whole_variable_referenced();
133263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if ((v != NULL) && (unsigned(idx) > v->max_array_access))
133363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       v->max_array_access = idx;
133463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 }
13352b7c42b40ae459f7b290eb134d6dabd075aab9f0Kenneth Graunke      } else if (array->type->array_size() == 0) {
13362b7c42b40ae459f7b290eb134d6dabd075aab9f0Kenneth Graunke	 _mesa_glsl_error(&loc, state, "unsized array index must be constant");
1337a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt      } else {
1338a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt	 if (array->type->is_array()) {
13395226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	    /* whole_variable_referenced can return NULL if the array is a
13405226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	     * member of a structure.  In this case it is safe to not update
13415226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	     * the max_array_access field because it is never used for fields
13425226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	     * of structures.
13435226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	     */
1344a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt	    ir_variable *v = array->whole_variable_referenced();
13455226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	    if (v != NULL)
13465226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	       v->max_array_access = array->type->array_size();
1347a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt	 }
134827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
134927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
135027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (error_emitted)
135127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 result->type = glsl_type::error_type;
135227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
135327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      type = result->type;
1354a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
135527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick   }
1356a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1357a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_function_call:
13587cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick      /* Should *NEVER* get here.  ast_function_call should always be handled
13597cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick       * by ast_function_expression::hir.
1360a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
13617cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick      assert(0);
1362a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1363a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1364a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_identifier: {
1365a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* ast_identifier can appear several places in a full abstract syntax
1366a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * tree.  This particular use must be at location specified in the grammar
1367a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * as 'variable_identifier'.
1368a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
13698bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      ir_variable *var =
13708bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick	 state->symbols->get_variable(this->primary_expression.identifier);
1371a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
13721660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_dereference_variable(var);
1373a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1374a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (var != NULL) {
1375a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 type = result->type;
1376a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
137771d0bbfcb2853f37b580ec7b705e55bb0eb426faIan Romanick	 _mesa_glsl_error(& loc, state, "`%s' undeclared",
137818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  this->primary_expression.identifier);
1379a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1380a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 error_emitted = true;
1381a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1382a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1383a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1384a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1385a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_int_constant:
13860471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::int_type;
13871660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(this->primary_expression.int_constant);
1388a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1389a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1390a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_uint_constant:
13910471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::uint_type;
13921660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(this->primary_expression.uint_constant);
1393a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1394a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1395a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_float_constant:
13960471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::float_type;
13971660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(this->primary_expression.float_constant);
1398a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1399a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1400a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bool_constant:
14010471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::bool_type;
14021660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant));
1403a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1404a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1405a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sequence: {
1406a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* It should not be possible to generate a sequence in the AST without
1407a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * any expressions in it.
1408a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1409304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      assert(!this->expressions.is_empty());
1410a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1411a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* The r-value of a sequence is the last expression in the sequence.  If
1412a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * the other expressions in the sequence do not have side-effects (and
1413a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * therefore add instructions to the instruction list), they get dropped
1414a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * on the floor.
1415a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
14162b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick      foreach_list_typed (ast_node, ast, link, &this->expressions)
1417304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick	 result = ast->hir(instructions, state);
1418a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1419a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = result->type;
1420a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1421a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Any errors should have already been emitted in the loop above.
1422a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1423a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      error_emitted = true;
1424a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1425a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1426a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1427a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1428cef3baecf636a30b62cd7a1e8de57c7650f7003eIan Romanick   if (type->is_error() && !error_emitted)
142971d0bbfcb2853f37b580ec7b705e55bb0eb426faIan Romanick      _mesa_glsl_error(& loc, state, "type mismatch");
1430a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1431a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return result;
1432a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1433a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1434a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1435fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
14360044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_expression_statement::hir(exec_list *instructions,
143718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			      struct _mesa_glsl_parse_state *state)
1438a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1439a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* It is possible to have expression statements that don't have an
1440a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * expression.  This is the solitary semicolon:
1441a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
1442a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * for (i = 0; i < 5; i++)
1443a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     ;
1444a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
1445a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * In this case the expression will be NULL.  Test for NULL and don't do
1446a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * anything in that case.
1447a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
144818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (expression != NULL)
144918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      expression->hir(instructions, state);
1450a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1451a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Statements do not have r-values.
1452a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1453a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1454a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1455a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1456a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1457fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
14580044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_compound_statement::hir(exec_list *instructions,
145918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			    struct _mesa_glsl_parse_state *state)
1460a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
146118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (new_scope)
14628bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      state->symbols->push_scope();
1463a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
14642b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_node, ast, link, &this->statements)
1465304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      ast->hir(instructions, state);
1466a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
146718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (new_scope)
14688bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      state->symbols->pop_scope();
1469a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1470a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Compound statements do not have r-values.
1471a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1472a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1473a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1474a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1475a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
147628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanickstatic const glsl_type *
147728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanickprocess_array_type(const glsl_type *base, ast_node *array_size,
147828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick		   struct _mesa_glsl_parse_state *state)
147928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick{
148028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   unsigned length = 0;
148128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
148228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   /* FINISHME: Reject delcarations of multidimensional arrays. */
148328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
148428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   if (array_size != NULL) {
148528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      exec_list dummy_instructions;
148628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
148728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      YYLTYPE loc = array_size->get_location();
148828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
148928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      /* FINISHME: Verify that the grammar forbids side-effects in array
149028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick       * FINISHME: sizes.   i.e., 'vec4 [x = 12] data'
149128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick       */
149228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      assert(dummy_instructions.is_empty());
149328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
149428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      if (ir != NULL) {
149528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 if (!ir->type->is_integer()) {
149628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    _mesa_glsl_error(& loc, state, "array size must be integer type");
149728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 } else if (!ir->type->is_scalar()) {
149828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    _mesa_glsl_error(& loc, state, "array size must be scalar type");
149928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 } else {
150028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    ir_constant *const size = ir->constant_expression_value();
150128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
150228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    if (size == NULL) {
150328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       _mesa_glsl_error(& loc, state, "array size must be a "
150428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick				"constant valued expression");
150528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    } else if (size->value.i[0] <= 0) {
150628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       _mesa_glsl_error(& loc, state, "array size must be > 0");
150728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    } else {
150828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       assert(size->type == ir->type);
150928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       length = size->value.u[0];
151028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    }
151128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 }
151228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      }
151328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   }
151428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
1515f38d15b80d4e4c8ecb7a76087cdc49835f0aa271Ian Romanick   return glsl_type::get_array_instance(base, length);
151628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick}
151728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
151828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
1519d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanickconst glsl_type *
1520d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanickast_type_specifier::glsl_type(const char **name,
1521d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick			      struct _mesa_glsl_parse_state *state) const
1522a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1523d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   const struct glsl_type *type;
1524a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
15253455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   if ((this->type_specifier == ast_struct) && (this->type_name == NULL)) {
1526a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* FINISHME: Handle annonymous structures. */
1527a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = NULL;
1528a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
1529d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick      type = state->symbols->get_type(this->type_name);
1530d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick      *name = this->type_name;
1531a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1532d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick      if (this->is_array) {
1533d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick	 type = process_array_type(type, this->array_size, state);
153428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      }
1535a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1536a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1537a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return type;
1538a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1539a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1540a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1541a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic void
1542a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickapply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
1543768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick				 ir_variable *var,
15442e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				 struct _mesa_glsl_parse_state *state,
15452e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				 YYLTYPE *loc)
1546a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1547a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->invariant)
1548a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->invariant = 1;
1549a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1550a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* FINISHME: Mark 'in' variables at global scope as read-only. */
1551a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->constant || qual->attribute || qual->uniform
1552a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       || (qual->varying && (state->target == fragment_shader)))
1553a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->read_only = 1;
1554a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1555a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->centroid)
1556a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->centroid = 1;
1557a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1558ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick   if (qual->attribute && state->target != vertex_shader) {
15592e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      var->type = glsl_type::error_type;
15602e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      _mesa_glsl_error(loc, state,
15612e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt		       "`attribute' variables may not be declared in the "
1562ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick		       "%s shader",
1563ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick		       _mesa_glsl_shader_target_name(state->target));
15642e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   }
15652e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt
156690b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt   /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
156790b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *
156890b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     "The varying qualifier can be used only with the data types
156990b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
157090b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     these."
157190b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    */
15720ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt   if (qual->varying) {
15730ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      const glsl_type *non_array_type;
15740ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt
15750ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      if (var->type && var->type->is_array())
15760ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 non_array_type = var->type->fields.array;
15770ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      else
15780ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 non_array_type = var->type;
15790ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt
15800ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      if (non_array_type && non_array_type->base_type != GLSL_TYPE_FLOAT) {
15810ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 var->type = glsl_type::error_type;
15820ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 _mesa_glsl_error(loc, state,
15830ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt			  "varying variables must be of base type float");
15840ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      }
158590b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt   }
158690b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt
15877e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick   /* If there is no qualifier that changes the mode of the variable, leave
15887e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick    * the setting alone.
15897e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick    */
1590a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->in && qual->out)
1591a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_inout;
1592a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else if (qual->attribute || qual->in
1593a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    || (qual->varying && (state->target == fragment_shader)))
1594a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_in;
15950b678234625fac67a89285ad2871dedc891fb1b1Ian Romanick   else if (qual->out || (qual->varying && (state->target == vertex_shader)))
1596a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_out;
1597a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else if (qual->uniform)
1598a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_uniform;
1599a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1600a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->flat)
1601a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->interpolation = ir_var_flat;
1602a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else if (qual->noperspective)
1603a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->interpolation = ir_var_noperspective;
1604a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else
1605a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->interpolation = ir_var_smooth;
16069d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick
16074a962170d7cf4243d6ae156fca20a6167388925dEric Anholt   var->pixel_center_integer = qual->pixel_center_integer;
16084a962170d7cf4243d6ae156fca20a6167388925dEric Anholt   var->origin_upper_left = qual->origin_upper_left;
16098d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick   if ((qual->origin_upper_left || qual->pixel_center_integer)
16108d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick       && (strcmp(var->name, "gl_FragCoord") != 0)) {
16118d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick      const char *const qual_string = (qual->origin_upper_left)
16128d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick	 ? "origin_upper_left" : "pixel_center_integer";
16138d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick
16148d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick      _mesa_glsl_error(loc, state,
16158d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick		       "layout qualifier `%s' can only be applied to "
16168d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick		       "fragment shader input `gl_FragCoord'",
16178d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick		       qual_string);
16188d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick   }
16198d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick
16209d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick   if (var->type->is_array() && (state->language_version >= 120)) {
16219d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick      var->array_lvalue = true;
16229d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick   }
1623a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1624a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1625a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1626fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
16270044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_declarator_list::hir(exec_list *instructions,
162818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			 struct _mesa_glsl_parse_state *state)
1629a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1630953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
1631a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const struct glsl_type *decl_type;
1632a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const char *type_name = NULL;
16338558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt   ir_rvalue *result = NULL;
1634c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick   YYLTYPE loc = this->get_location();
1635a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
16366f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
16376f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *
16386f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *     "To ensure that a particular output variable is invariant, it is
16396f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *     necessary to use the invariant qualifier. It can either be used to
16406f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *     qualify a previously declared variable as being invariant
16416f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *
16426f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *         invariant gl_Position; // make existing gl_Position be invariant"
16436f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *
16446f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    * In these cases the parser will set the 'invariant' flag in the declarator
16456f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    * list, and the type will be NULL.
16466f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    */
16476f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   if (this->invariant) {
16486f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      assert(this->type == NULL);
16496f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
16506f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      if (state->current_function != NULL) {
16516f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 _mesa_glsl_error(& loc, state,
16526f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			  "All uses of `invariant' keyword must be at global "
16536f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			  "scope\n");
16546f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      }
16556f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
16566f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
16576f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 assert(!decl->is_array);
16586f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 assert(decl->array_size == NULL);
16596f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 assert(decl->initializer == NULL);
16606f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
16616f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 ir_variable *const earlier =
16626f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    state->symbols->get_variable(decl->identifier);
16636f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 if (earlier == NULL) {
16646f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
16656f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "Undeclared variable `%s' cannot be marked "
16666f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "invariant\n", decl->identifier);
16676f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 } else if ((state->target == vertex_shader)
16686f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	       && (earlier->mode != ir_var_out)) {
16696f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
16706f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "`%s' cannot be marked invariant, vertex shader "
16716f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "outputs only\n", decl->identifier);
16726f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 } else if ((state->target == fragment_shader)
16736f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	       && (earlier->mode != ir_var_in)) {
16746f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
16756f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "`%s' cannot be marked invariant, fragment shader "
16766f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "inputs only\n", decl->identifier);
16776f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 } else {
16786f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    earlier->invariant = true;
16796f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 }
16806f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      }
16816f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
16826f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      /* Invariant redeclarations do not have r-values.
16836f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick       */
16846f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      return NULL;
16856f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   }
16866f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
16876f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   assert(this->type != NULL);
16886f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   assert(!this->invariant);
16896f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
16903455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* The type specifier may contain a structure definition.  Process that
16913455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * before any of the variable declarations.
16923455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
16933455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   (void) this->type->specifier->hir(instructions, state);
16943455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
1695d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   decl_type = this->type->specifier->glsl_type(& type_name, state);
1696304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick   if (this->declarations.is_empty()) {
16976f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      /* The only valid case where the declaration list can be empty is when
16986f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick       * the declaration is setting the default precision of a built-in type
16996f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick       * (e.g., 'precision highp vec4;').
1700c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick       */
1701c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick
17026f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      if (decl_type != NULL) {
1703c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick      } else {
1704c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick	    _mesa_glsl_error(& loc, state, "incomplete declaration");
1705c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick      }
1706c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick   }
1707a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
17082b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
1709a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      const struct glsl_type *var_type;
1710768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick      ir_variable *var;
1711a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1712a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* FINISHME: Emit a warning if a variable declaration shadows a
1713a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * FINISHME: declaration at a higher scope.
1714a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1715a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1716cec65a6b76290ee4da91691bd3ef01c3fb8a0c37Ian Romanick      if ((decl_type == NULL) || decl_type->is_void()) {
1717a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 if (type_name != NULL) {
1718a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    _mesa_glsl_error(& loc, state,
1719a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     "invalid type `%s' in declaration of `%s'",
1720a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     type_name, decl->identifier);
1721a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 } else {
1722a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    _mesa_glsl_error(& loc, state,
1723a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     "invalid type in declaration of `%s'",
1724a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     decl->identifier);
1725a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
1726a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 continue;
1727a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1728a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1729a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (decl->is_array) {
173028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 var_type = process_array_type(decl_type, decl->array_size, state);
1731a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
1732a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 var_type = decl_type;
1733a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1734a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
17357e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick      var = new(ctx) ir_variable(var_type, decl->identifier, ir_var_auto);
1736a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
17373f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
17383f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *
17393f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     "Global variables can only use the qualifiers const,
17403f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     attribute, uni form, or varying. Only one may be
17413f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     specified.
17423f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *
17433f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     Local variables can only use the qualifier const."
17443f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *
17453f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       * This is relaxed in GLSL 1.30.
17463f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       */
17473f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      if (state->language_version < 120) {
17483f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 if (this->type->qualifier.out) {
17493f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	    _mesa_glsl_error(& loc, state,
17503f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "`out' qualifier in declaration of `%s' "
17513f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "only valid for function parameters in GLSL 1.10.",
17523f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     decl->identifier);
17533f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 }
17543f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 if (this->type->qualifier.in) {
17553f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	    _mesa_glsl_error(& loc, state,
17563f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "`in' qualifier in declaration of `%s' "
17573f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "only valid for function parameters in GLSL 1.10.",
17583f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     decl->identifier);
17593f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 }
17603f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 /* FINISHME: Test for other invalid qualifiers. */
17613f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      }
17623f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt
17632e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
17642e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				       & loc);
1765a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
17666f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      if (this->type->qualifier.invariant) {
1767046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	 if ((state->target == vertex_shader) && !(var->mode == ir_var_out ||
1768046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt						   var->mode == ir_var_inout)) {
1769046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	    /* FINISHME: Note that this doesn't work for invariant on
1770046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	     * a function signature outval
1771046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	     */
17726f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
17736f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "`%s' cannot be marked invariant, vertex shader "
17746f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "outputs only\n", var->name);
1775046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	 } else if ((state->target == fragment_shader) &&
1776046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt		    !(var->mode == ir_var_in || var->mode == ir_var_inout)) {
1777046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	    /* FINISHME: Note that this doesn't work for invariant on
1778046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	     * a function signature inval
1779046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	     */
17806f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
17816f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "`%s' cannot be marked invariant, fragment shader "
17826f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "inputs only\n", var->name);
17836f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 }
17846f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      }
17856f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
1786e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick      if (state->current_function != NULL) {
1787b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 const char *mode = NULL;
1788e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 const char *extra = "";
1789b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick
1790e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 /* There is no need to check for 'inout' here because the parser will
1791e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	  * only allow that in function parameter lists.
1792e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	  */
1793e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	 if (this->type->qualifier.attribute) {
1794b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	    mode = "attribute";
1795b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 } else if (this->type->qualifier.uniform) {
1796b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	    mode = "uniform";
1797b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 } else if (this->type->qualifier.varying) {
1798b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	    mode = "varying";
1799e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 } else if (this->type->qualifier.in) {
1800e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    mode = "in";
1801e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    extra = " or in function parameter list";
1802e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 } else if (this->type->qualifier.out) {
1803e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    mode = "out";
1804e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    extra = " or in function parameter list";
1805b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 }
1806b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick
1807b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 if (mode) {
1808e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	    _mesa_glsl_error(& loc, state,
1809b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick			     "%s variable `%s' must be declared at "
1810e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick			     "global scope%s",
1811e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick			     mode, var->name, extra);
1812e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	 }
1813e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick      } else if (var->mode == ir_var_in) {
1814fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	 if (state->target == vertex_shader) {
1815fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    bool error_emitted = false;
1816fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
1817fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1818fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
1819fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    "Vertex shader inputs can only be float, floating-point
1820fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    vectors, matrices, signed and unsigned integers and integer
1821fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    vectors. Vertex shader inputs can also form arrays of these
1822fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    types, but not structures."
1823fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
18242d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
18252d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *
18262d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *    "Vertex shader inputs can only be float, floating-point
18272d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *    vectors, matrices, signed and unsigned integers and integer
18282d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *    vectors. They cannot be arrays or structures."
18292d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *
1830fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
1831fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
1832fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    "The attribute qualifier can be used only with float,
1833fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    floating-point vectors, and matrices. Attribute variables
1834fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    cannot be declared as arrays or structures."
1835fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     */
1836fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    const glsl_type *check_type = var->type->is_array()
1837fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       ? var->type->fields.array : var->type;
1838fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
1839fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    switch (check_type->base_type) {
1840fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_FLOAT:
1841fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       break;
1842fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_UINT:
1843fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_INT:
1844fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       if (state->language_version > 120)
1845fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick		  break;
1846fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       /* FALLTHROUGH */
1847fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    default:
1848fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       _mesa_glsl_error(& loc, state,
1849fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"vertex shader input / attribute cannot have "
1850fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"type %s`%s'",
1851fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				var->type->is_array() ? "array of " : "",
1852fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				check_type->name);
1853fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       error_emitted = true;
1854fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    }
1855fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
18562d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	    if (!error_emitted && (state->language_version <= 130)
1857fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick		&& var->type->is_array()) {
1858fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       _mesa_glsl_error(& loc, state,
1859fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"vertex shader input / attribute cannot have "
1860fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"array type");
1861fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       error_emitted = true;
1862fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    }
1863fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	 }
1864fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick      }
1865fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
1866e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick      /* Process the initializer and add its instructions to a temporary
1867e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       * list.  This list will be added to the instruction stream (below) after
1868e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       * the declaration is added.  This is done because in some cases (such as
1869e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       * redeclarations) the declaration may not actually be added to the
1870e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       * instruction stream.
1871e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       */
1872fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt      exec_list initializer_instructions;
187366faec4895b7bb59a614087a200c05157191b4aeIan Romanick      if (decl->initializer != NULL) {
187443de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 YYLTYPE initializer_loc = decl->initializer->get_location();
187543de17282017bdf187d6e646de3262cc64b7f46bIan Romanick
187666faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
187766faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *
187866faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *    "All uniform variables are read-only and are initialized either
187966faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *    directly by an application via API commands, or indirectly by
188066faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *    OpenGL."
188166faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  */
188266faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 if ((state->language_version <= 110)
188366faec4895b7bb59a614087a200c05157191b4aeIan Romanick	     && (var->mode == ir_var_uniform)) {
188443de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	    _mesa_glsl_error(& initializer_loc, state,
188543de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     "cannot initialize uniforms in GLSL 1.10");
188643de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 }
188743de17282017bdf187d6e646de3262cc64b7f46bIan Romanick
188843de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 if (var->type->is_sampler()) {
188943de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	    _mesa_glsl_error(& initializer_loc, state,
189043de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     "cannot initialize samplers");
189143de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 }
189219360152f5bd8cff93359dbfe5a50a90b699c118Ian Romanick
189343de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 if ((var->mode == ir_var_in) && (state->current_function == NULL)) {
189443de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	    _mesa_glsl_error(& initializer_loc, state,
189543de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     "cannot initialize %s shader input / %s",
1896ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick			     _mesa_glsl_shader_target_name(state->target),
189743de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     (state->target == vertex_shader)
189843de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     ? "attribute" : "varying");
189966faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 }
190066faec4895b7bb59a614087a200c05157191b4aeIan Romanick
19011660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const lhs = new(ctx) ir_dereference_variable(var);
1902fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt	 ir_rvalue *rhs = decl->initializer->hir(&initializer_instructions,
1903e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick						 state);
190419360152f5bd8cff93359dbfe5a50a90b699c118Ian Romanick
1905ce030884064046925a655413097dd8257e9392ddIan Romanick	 /* Calculate the constant value if this is a const or uniform
1906307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	  * declaration.
190766faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  */
1908ce030884064046925a655413097dd8257e9392ddIan Romanick	 if (this->type->qualifier.constant || this->type->qualifier.uniform) {
1909e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke	    ir_rvalue *new_rhs = validate_assignment(state, var->type, rhs);
1910e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke	    if (new_rhs != NULL) {
1911e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke	       rhs = new_rhs;
1912e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt
1913e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt	       ir_constant *constant_value = rhs->constant_expression_value();
1914e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt	       if (!constant_value) {
1915e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt		  _mesa_glsl_error(& initializer_loc, state,
1916e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt				   "initializer of %s variable `%s' must be a "
1917e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt				   "constant expression",
1918e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt				   (this->type->qualifier.constant)
1919e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt				   ? "const" : "uniform",
1920e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt				   decl->identifier);
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		  }
1925e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt	       } else {
1926e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt		  rhs = constant_value;
1927e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt		  var->constant_value = constant_value;
1928e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt	       }
1929e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke	    } else {
1930e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke	       _mesa_glsl_error(&initializer_loc, state,
1931e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke			        "initializer of type %s cannot be assigned to "
1932e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke				"variable of type %s",
1933e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke				rhs->type->name, var->type->name);
1934e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt	       if (var->type->is_numeric()) {
1935e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt		  /* Reduce cascading errors. */
1936e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt		  var->constant_value = ir_constant::zero(ctx, var->type);
1937e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt	       }
1938307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	    }
1939307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	 }
194066faec4895b7bb59a614087a200c05157191b4aeIan Romanick
1941307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	 if (rhs && !rhs->type->is_error()) {
1942ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt	    bool temp = var->read_only;
1943ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt	    if (this->type->qualifier.constant)
1944ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt	       var->read_only = false;
1945ce030884064046925a655413097dd8257e9392ddIan Romanick
1946ce030884064046925a655413097dd8257e9392ddIan Romanick	    /* Never emit code to initialize a uniform.
1947ce030884064046925a655413097dd8257e9392ddIan Romanick	     */
1948ce030884064046925a655413097dd8257e9392ddIan Romanick	    if (!this->type->qualifier.uniform)
1949fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt	       result = do_assignment(&initializer_instructions, state,
1950fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt				      lhs, rhs,
1951ce030884064046925a655413097dd8257e9392ddIan Romanick				      this->get_location());
1952ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt	    var->read_only = temp;
195366faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 }
195466faec4895b7bb59a614087a200c05157191b4aeIan Romanick      }
195517d86f4371da413176ba365ca26a58bac172d365Ian Romanick
19560ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt      /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
19570ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *
19580ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *     "It is an error to write to a const variable outside of
19590ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *      its declaration, so they must be initialized when
19600ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *      declared."
19610ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       */
19620ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt      if (this->type->qualifier.constant && decl->initializer == NULL) {
19630ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt	 _mesa_glsl_error(& loc, state,
19640ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt			  "const declaration of `%s' must be initialized");
19650ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt      }
19660ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt
19675d25746640ee27882b69a962459727cf924443dbKenneth Graunke      /* Check if this declaration is actually a re-declaration, either to
19685d25746640ee27882b69a962459727cf924443dbKenneth Graunke       * resize an array or add qualifiers to an existing variable.
19695466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       *
1970a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke       * This is allowed for variables in the current scope, or when at
1971a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke       * global scope (for built-ins in the implicit outer scope).
19725466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       */
19735d25746640ee27882b69a962459727cf924443dbKenneth Graunke      ir_variable *earlier = state->symbols->get_variable(decl->identifier);
1974a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke      if (earlier != NULL && (state->current_function == NULL ||
1975a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke	  state->symbols->name_declared_this_scope(decl->identifier))) {
19765466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
19775d25746640ee27882b69a962459727cf924443dbKenneth Graunke	 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
19785d25746640ee27882b69a962459727cf924443dbKenneth Graunke	  *
19795d25746640ee27882b69a962459727cf924443dbKenneth Graunke	  * "It is legal to declare an array without a size and then
19805d25746640ee27882b69a962459727cf924443dbKenneth Graunke	  *  later re-declare the same name as an array of the same
19815d25746640ee27882b69a962459727cf924443dbKenneth Graunke	  *  type and specify a size."
19825d25746640ee27882b69a962459727cf924443dbKenneth Graunke	  */
19835d25746640ee27882b69a962459727cf924443dbKenneth Graunke	 if ((earlier->type->array_size() == 0)
19845466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	     && var->type->is_array()
19855466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	     && (var->type->element_type() == earlier->type->element_type())) {
19865466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	    /* FINISHME: This doesn't match the qualifiers on the two
19875466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	     * FINISHME: declarations.  It's not 100% clear whether this is
19885466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	     * FINISHME: required or not.
19895466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	     */
19905466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
1991cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	    /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
1992cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	     *
1993cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	     *     "The size [of gl_TexCoord] can be at most
1994cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	     *     gl_MaxTextureCoords."
1995cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	     */
1996127308b4be077e5bdf60f76320307550921e86bbIan Romanick	    const unsigned size = unsigned(var->type->array_size());
1997cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	    if ((strcmp("gl_TexCoord", var->name) == 0)
1998127308b4be077e5bdf60f76320307550921e86bbIan Romanick		&& (size > state->Const.MaxTextureCoords)) {
1999cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	       YYLTYPE loc = this->get_location();
2000cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick
2001cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	       _mesa_glsl_error(& loc, state, "`gl_TexCoord' array size cannot "
2002cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick				"be larger than gl_MaxTextureCoords (%u)\n",
2003127308b4be077e5bdf60f76320307550921e86bbIan Romanick				state->Const.MaxTextureCoords);
200412873fa4e332959295154edfe957c0af79af5e74Ian Romanick	    } else if ((size > 0) && (size <= earlier->max_array_access)) {
20055466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	       YYLTYPE loc = this->get_location();
20065466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
20075466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	       _mesa_glsl_error(& loc, state, "array size must be > %u due to "
20085466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick				"previous access",
20095466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick				earlier->max_array_access);
20105466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	    }
20115466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
20125466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	    earlier->type = var->type;
20135466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	    delete var;
20145466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	    var = NULL;
20155d25746640ee27882b69a962459727cf924443dbKenneth Graunke	 } else if (state->extensions->ARB_fragment_coord_conventions
20165d25746640ee27882b69a962459727cf924443dbKenneth Graunke		    && strcmp(var->name, "gl_FragCoord") == 0
20175d25746640ee27882b69a962459727cf924443dbKenneth Graunke		    && earlier->type == var->type
20185d25746640ee27882b69a962459727cf924443dbKenneth Graunke		    && earlier->mode == var->mode) {
20194a962170d7cf4243d6ae156fca20a6167388925dEric Anholt	    /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
20204a962170d7cf4243d6ae156fca20a6167388925dEric Anholt	     * qualifiers.
20214a962170d7cf4243d6ae156fca20a6167388925dEric Anholt	     */
20224a962170d7cf4243d6ae156fca20a6167388925dEric Anholt	    earlier->origin_upper_left = var->origin_upper_left;
20234a962170d7cf4243d6ae156fca20a6167388925dEric Anholt	    earlier->pixel_center_integer = var->pixel_center_integer;
20245466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	 } else {
20255466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	    YYLTYPE loc = this->get_location();
20265d25746640ee27882b69a962459727cf924443dbKenneth Graunke	    _mesa_glsl_error(&loc, state, "`%s' redeclared", decl->identifier);
20275466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	 }
20285466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
20295466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	 continue;
20305466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick      }
20315466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
20325d25746640ee27882b69a962459727cf924443dbKenneth Graunke      /* By now, we know it's a new variable declaration (we didn't hit the
20335d25746640ee27882b69a962459727cf924443dbKenneth Graunke       * above "continue").
20345d25746640ee27882b69a962459727cf924443dbKenneth Graunke       *
20355d25746640ee27882b69a962459727cf924443dbKenneth Graunke       * From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
20365466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       *
20375466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       *   "Identifiers starting with "gl_" are reserved for use by
20385466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       *   OpenGL, and may not be declared in a shader as either a
20395466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       *   variable or a function."
20405466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       */
2041de3b40d8cdc42cc1cd71dd65c90d6d569d922fc6Ian Romanick      if (strncmp(decl->identifier, "gl_", 3) == 0)
20425466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	 _mesa_glsl_error(& loc, state,
20435466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick			  "identifier `%s' uses reserved `gl_' prefix",
20445466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick			  decl->identifier);
20455466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
20465d25746640ee27882b69a962459727cf924443dbKenneth Graunke      /* Add the variable to the symbol table.  Note that the initializer's
20475d25746640ee27882b69a962459727cf924443dbKenneth Graunke       * IR was already processed earlier (though it hasn't been emitted yet),
20485d25746640ee27882b69a962459727cf924443dbKenneth Graunke       * without the variable in scope.
20495d25746640ee27882b69a962459727cf924443dbKenneth Graunke       *
20505d25746640ee27882b69a962459727cf924443dbKenneth Graunke       * This differs from most C-like languages, but it follows the GLSL
20515d25746640ee27882b69a962459727cf924443dbKenneth Graunke       * specification.  From page 28 (page 34 of the PDF) of the GLSL 1.50
20525d25746640ee27882b69a962459727cf924443dbKenneth Graunke       * spec:
20535d25746640ee27882b69a962459727cf924443dbKenneth Graunke       *
20545d25746640ee27882b69a962459727cf924443dbKenneth Graunke       *     "Within a declaration, the scope of a name starts immediately
20555d25746640ee27882b69a962459727cf924443dbKenneth Graunke       *     after the initializer if present or immediately after the name
20565d25746640ee27882b69a962459727cf924443dbKenneth Graunke       *     being declared if not."
20575d25746640ee27882b69a962459727cf924443dbKenneth Graunke       */
20585d25746640ee27882b69a962459727cf924443dbKenneth Graunke      if (!state->symbols->add_variable(var->name, var)) {
20595d25746640ee27882b69a962459727cf924443dbKenneth Graunke	 YYLTYPE loc = this->get_location();
20605d25746640ee27882b69a962459727cf924443dbKenneth Graunke	 _mesa_glsl_error(&loc, state, "name `%s' already taken in the "
20615d25746640ee27882b69a962459727cf924443dbKenneth Graunke			  "current scope", decl->identifier);
20625d25746640ee27882b69a962459727cf924443dbKenneth Graunke	 continue;
20635d25746640ee27882b69a962459727cf924443dbKenneth Graunke      }
20645d25746640ee27882b69a962459727cf924443dbKenneth Graunke
20658048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt      /* Push the variable declaration to the top.  It means that all
20668048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt       * the variable declarations will appear in a funny
20678048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt       * last-to-first order, but otherwise we run into trouble if a
20688048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt       * function is prototyped, a global var is decled, then the
20698048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt       * function is defined with usage of the global var.  See
20708048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt       * glslparsertest's CorrectModule.frag.
20718048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt       */
20728048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt      instructions->push_head(var);
2073fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt      instructions->append_list(&initializer_instructions);
2074a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2075a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
20768558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt
20778558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt   /* Generally, variable declarations do not have r-values.  However,
20788558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * one is used for the declaration in
20798558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    *
20808558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * while (bool b = some_condition()) {
20818558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    *   ...
20828558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * }
20838558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    *
20848558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * so we return the rvalue from the last seen declaration here.
2085a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
20868558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt   return result;
2087a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
2088a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2089a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2090fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
20910044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_parameter_declarator::hir(exec_list *instructions,
209218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			      struct _mesa_glsl_parse_state *state)
2093a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
2094953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
2095a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const struct glsl_type *type;
2096a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const char *name = NULL;
20972e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   YYLTYPE loc = this->get_location();
2098a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2099d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   type = this->type->specifier->glsl_type(& name, state);
2100a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2101a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (type == NULL) {
2102a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (name != NULL) {
2103a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 _mesa_glsl_error(& loc, state,
2104a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			  "invalid type `%s' in declaration of `%s'",
210518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  name, this->identifier);
2106a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
2107a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 _mesa_glsl_error(& loc, state,
2108a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			  "invalid type in declaration of `%s'",
210918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  this->identifier);
2110a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
2111a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
21120471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::error_type;
2113a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2114a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2115068c80cfe0a280490353b6b007165d717c672eedEric Anholt   /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
2116068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *
2117068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    "Functions that accept no input arguments need not use void in the
2118068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    argument list because prototypes (or definitions) are required and
2119068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    therefore there is no ambiguity when an empty argument list "( )" is
2120068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    declared. The idiom "(void)" as a parameter list is provided for
2121068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    convenience."
2122068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *
2123068c80cfe0a280490353b6b007165d717c672eedEric Anholt    * Placing this check here prevents a void parameter being set up
2124068c80cfe0a280490353b6b007165d717c672eedEric Anholt    * for a function, which avoids tripping up checks for main taking
2125068c80cfe0a280490353b6b007165d717c672eedEric Anholt    * parameters and lookups of an unnamed symbol.
2126068c80cfe0a280490353b6b007165d717c672eedEric Anholt    */
2127cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   if (type->is_void()) {
2128cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      if (this->identifier != NULL)
2129cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick	 _mesa_glsl_error(& loc, state,
2130cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick			  "named parameter cannot have type `void'");
2131cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
2132cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      is_void = true;
2133068c80cfe0a280490353b6b007165d717c672eedEric Anholt      return NULL;
2134cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   }
2135068c80cfe0a280490353b6b007165d717c672eedEric Anholt
213645d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   if (formal_parameter && (this->identifier == NULL)) {
213745d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick      _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
213845d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick      return NULL;
213945d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   }
214045d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick
2141e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   /* This only handles "vec4 foo[..]".  The earlier specifier->glsl_type(...)
2142e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke    * call already handled the "vec4[..] foo" case.
2143e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke    */
2144e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   if (this->is_array) {
2145e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke      type = process_array_type(type, this->array_size, state);
2146e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   }
2147e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke
2148e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   if (type->array_size() == 0) {
2149e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke      _mesa_glsl_error(&loc, state, "arrays passed as parameters must have "
2150e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke		       "a declared size.");
2151e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke      type = glsl_type::error_type;
2152e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   }
2153e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke
2154cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   is_void = false;
21557e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick   ir_variable *var = new(ctx) ir_variable(type, this->identifier, ir_var_in);
2156a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2157cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick   /* Apply any specified qualifiers to the parameter declaration.  Note that
2158cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick    * for function parameters the default mode is 'in'.
2159cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick    */
21602e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc);
2161a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
21620044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick   instructions->push_tail(var);
2163a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2164a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Parameter declarations do not have r-values.
2165a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
2166a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
2167a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
2168a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2169a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
217045d8a70c12ee6ea956baaf898324a828496382f6Ian Romanickvoid
2171304ea90233baeac6801a98e981658cb7a2d2501cIan Romanickast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters,
217245d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					    bool formal,
217345d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					    exec_list *ir_parameters,
217445d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					    _mesa_glsl_parse_state *state)
2175a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
2176cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   ast_parameter_declarator *void_param = NULL;
2177cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   unsigned count = 0;
2178a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
21792b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) {
218045d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick      param->formal_parameter = formal;
2181068c80cfe0a280490353b6b007165d717c672eedEric Anholt      param->hir(ir_parameters, state);
2182cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
2183cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      if (param->is_void)
2184cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick	 void_param = param;
2185cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
2186cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      count++;
2187cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   }
2188cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
2189cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   if ((void_param != NULL) && (count > 1)) {
2190cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      YYLTYPE loc = void_param->get_location();
2191cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
2192cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      _mesa_glsl_error(& loc, state,
2193cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick		       "`void' parameter must be only parameter");
2194a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2195a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
2196a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2197a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2198fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
219992318a947958892497722772b03c643ebc943294Ian Romanickast_function::hir(exec_list *instructions,
220092318a947958892497722772b03c643ebc943294Ian Romanick		  struct _mesa_glsl_parse_state *state)
2201a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
2202953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
220318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   ir_function *f = NULL;
220492318a947958892497722772b03c643ebc943294Ian Romanick   ir_function_signature *sig = NULL;
220592318a947958892497722772b03c643ebc943294Ian Romanick   exec_list hir_parameters;
2206a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2207ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   const char *const name = identifier;
2208a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
220963b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick   /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
221063b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *
221163b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *   "Function declarations (prototypes) cannot occur inside of functions;
221263b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *   they must be at global scope, or for the built-in functions, outside
221363b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *   the global scope."
221463b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *
221563b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
221663b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *
221763b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *   "User defined functions may only be defined within the global scope."
221863b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *
221963b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    * Note that this language does not appear in GLSL 1.10.
222063b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    */
222163b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick   if ((state->current_function != NULL) && (state->language_version != 110)) {
222263b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick      YYLTYPE loc = this->get_location();
222363b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick      _mesa_glsl_error(&loc, state,
222463b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick		       "declaration of function `%s' not allowed within "
222563b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick		       "function body", name);
222663b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick   }
222763b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick
2228edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke   /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
2229edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    *
2230edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    *   "Identifiers starting with "gl_" are reserved for use by
2231edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    *   OpenGL, and may not be declared in a shader as either a
2232edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    *   variable or a function."
2233edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    */
2234edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke   if (strncmp(name, "gl_", 3) == 0) {
2235edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke      YYLTYPE loc = this->get_location();
2236edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke      _mesa_glsl_error(&loc, state,
2237edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke		       "identifier `%s' uses reserved `gl_' prefix", name);
2238edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke   }
2239edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke
2240a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Convert the list of function parameters to HIR now so that they can be
2241a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * used below to compare this function's signature with previously seen
2242a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * signatures for functions with the same name.
2243a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
224445d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   ast_parameter_declarator::parameters_to_hir(& this->parameters,
224545d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					       is_definition,
224645d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					       & hir_parameters, state);
2247a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2248e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick   const char *return_type_name;
2249e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick   const glsl_type *return_type =
225092318a947958892497722772b03c643ebc943294Ian Romanick      this->return_type->specifier->glsl_type(& return_type_name, state);
2251e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick
225276e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt   if (!return_type) {
225376e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt      YYLTYPE loc = this->get_location();
225476e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt      _mesa_glsl_error(&loc, state,
225576e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt		       "function `%s' has undeclared return type `%s'",
225676e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt		       name, return_type_name);
225776e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt      return_type = glsl_type::error_type;
225876e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt   }
2259e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick
2260ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
2261ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke    * "No qualifier is allowed on the return type of a function."
2262ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke    */
2263ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   if (this->return_type->has_qualifiers()) {
2264ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke      YYLTYPE loc = this->get_location();
2265ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke      _mesa_glsl_error(& loc, state,
2266ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke		       "function `%s' return type has qualifiers", name);
2267ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   }
2268ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke
2269a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Verify that this function's signature either doesn't match a previously
2270a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * seen signature for a function with the same name, or, if a match is found,
2271a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * that the previously seen signature does not have an associated definition.
2272a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
2273e466b182bbf21f62fe6542091f4af3275555db80Ian Romanick   f = state->symbols->get_function(name);
2274a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke   if (f != NULL && !f->is_builtin) {
2275202604e8160157e4e80b3458175e0170d168e557Ian Romanick      sig = f->exact_matching_signature(&hir_parameters);
22760d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke      if (sig != NULL) {
22770d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 const char *badvar = sig->qualifiers_match(&hir_parameters);
22780d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 if (badvar != NULL) {
22790d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    YYLTYPE loc = this->get_location();
2280abd40b15210c17b2a3ba8fcffc868fda203efa01Kenneth Graunke
22810d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
22820d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke			     "qualifiers don't match prototype", name, badvar);
22830d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 }
22841e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt
22850d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 if (sig->return_type != return_type) {
22860d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    YYLTYPE loc = this->get_location();
228760be7626b829af7e1d07330b9a88468924ba350eEric Anholt
22880d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
22890d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke			     "match prototype", name);
22900d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 }
2291a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
22920d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 if (is_definition && sig->is_defined) {
22930d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    YYLTYPE loc = this->get_location();
2294a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
22950d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
2296a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
2297a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
2298a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
22991660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      f = new(ctx) ir_function(name);
2300e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke      if (!state->symbols->add_function(f->name, f)) {
2301e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke	 /* This function name shadows a non-function use of the same name. */
2302e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke	 YYLTYPE loc = this->get_location();
2303e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke
2304e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke	 _mesa_glsl_error(&loc, state, "function name `%s' conflicts with "
2305e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke			  "non-function", name);
2306e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke	 return NULL;
2307e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke      }
23089fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke
23099fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke      /* Emit the new function header */
231063b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick      if (state->current_function == NULL)
231163b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	 instructions->push_tail(f);
231263b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick      else {
231363b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	 /* IR invariants disallow function declarations or definitions nested
231463b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	  * within other function definitions.  Insert the new ir_function
231563b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	  * block in the instruction sequence before the ir_function block
231663b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	  * containing the current ir_function_signature.
231763b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	  *
231863b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	  * This can only happen in a GLSL 1.10 shader.  In all other GLSL
231963b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	  * versions this nesting is disallowed.  There is a check for this at
232063b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	  * the top of this function.
232163b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	  */
232263b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	 ir_function *const curr =
232363b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	    const_cast<ir_function *>(state->current_function->function());
232463b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick
232563b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	 curr->insert_before(f);
232663b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick      }
2327a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2328a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2329ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   /* Verify the return type of main() */
2330ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   if (strcmp(name, "main") == 0) {
233125711a85c22bed305c9b52b89feb9c600d1892dfIan Romanick      if (! return_type->is_void()) {
2332ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt	 YYLTYPE loc = this->get_location();
2333ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt
2334ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt	 _mesa_glsl_error(& loc, state, "main() must return void");
2335ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt      }
2336174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt
233792318a947958892497722772b03c643ebc943294Ian Romanick      if (!hir_parameters.is_empty()) {
2338174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt	 YYLTYPE loc = this->get_location();
2339174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt
2340174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt	 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
2341174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt      }
2342ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   }
2343a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2344a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Finish storing the information about this new function in its signature.
2345a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
234692318a947958892497722772b03c643ebc943294Ian Romanick   if (sig == NULL) {
23471660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      sig = new(ctx) ir_function_signature(return_type);
234892318a947958892497722772b03c643ebc943294Ian Romanick      f->add_signature(sig);
2349a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2350a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2351bff6013d469b3d4e54cdc5731801c56994a523ecKenneth Graunke   sig->replace_parameters(&hir_parameters);
235292318a947958892497722772b03c643ebc943294Ian Romanick   signature = sig;
235392318a947958892497722772b03c643ebc943294Ian Romanick
235492318a947958892497722772b03c643ebc943294Ian Romanick   /* Function declarations (prototypes) do not have r-values.
235592318a947958892497722772b03c643ebc943294Ian Romanick    */
235692318a947958892497722772b03c643ebc943294Ian Romanick   return NULL;
235792318a947958892497722772b03c643ebc943294Ian Romanick}
235892318a947958892497722772b03c643ebc943294Ian Romanick
235992318a947958892497722772b03c643ebc943294Ian Romanick
236092318a947958892497722772b03c643ebc943294Ian Romanickir_rvalue *
236192318a947958892497722772b03c643ebc943294Ian Romanickast_function_definition::hir(exec_list *instructions,
236292318a947958892497722772b03c643ebc943294Ian Romanick			     struct _mesa_glsl_parse_state *state)
236392318a947958892497722772b03c643ebc943294Ian Romanick{
236492318a947958892497722772b03c643ebc943294Ian Romanick   prototype->is_definition = true;
236592318a947958892497722772b03c643ebc943294Ian Romanick   prototype->hir(instructions, state);
2366e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick
236792318a947958892497722772b03c643ebc943294Ian Romanick   ir_function_signature *signature = prototype->signature;
2368826a39cb14244820e8539a2328bb52447348f184Kenneth Graunke   if (signature == NULL)
2369826a39cb14244820e8539a2328bb52447348f184Kenneth Graunke      return NULL;
2370a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
237141ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   assert(state->current_function == NULL);
237241ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = signature;
23736de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke   state->found_return = false;
237441ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick
2375e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick   /* Duplicate parameters declared in the prototype as concrete variables.
2376e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick    * Add these to the symbol table.
2377a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
23788bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick   state->symbols->push_scope();
2379e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick   foreach_iter(exec_list_iterator, iter, signature->parameters) {
2380fbc7c0b8f2e161bce1c048c63d2d5cfcdeb096f1Eric Anholt      ir_variable *const var = ((ir_instruction *) iter.get())->as_variable();
2381e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick
2382fbc7c0b8f2e161bce1c048c63d2d5cfcdeb096f1Eric Anholt      assert(var != NULL);
2383a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
23843359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      /* The only way a parameter would "exist" is if two parameters have
23853359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick       * the same name.
23863359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick       */
23873359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      if (state->symbols->name_declared_this_scope(var->name)) {
23883359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 YYLTYPE loc = this->get_location();
23893359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick
23903359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
23913359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      } else {
23923359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 state->symbols->add_variable(var->name, var);
23933359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      }
2394a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2395a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
23969fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke   /* Convert the body of the function to HIR. */
2397894ea972a4defdaafeaa3a248c113b06c7ae0c7eEric Anholt   this->body->hir(&signature->body, state);
23989fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke   signature->is_defined = true;
2399a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
24008bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick   state->symbols->pop_scope();
2401a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
240241ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   assert(state->current_function == signature);
240341ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = NULL;
2404a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
24056de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke   if (!signature->return_type->is_void() && !state->found_return) {
24066de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke      YYLTYPE loc = this->get_location();
24076de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke      _mesa_glsl_error(& loc, state, "function `%s' has non-void return type "
24086de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke		       "%s, but no return statement",
24096de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke		       signature->function_name(),
24106de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke		       signature->return_type->name);
24116de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke   }
24126de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke
2413a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Function definitions do not have r-values.
2414a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
2415a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
2416a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
241716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
241816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
2419fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
242016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanickast_jump_statement::hir(exec_list *instructions,
242116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick			struct _mesa_glsl_parse_state *state)
242216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick{
2423953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
242416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
2425c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   switch (mode) {
2426c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_return: {
242716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      ir_return *inst;
2428aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt      assert(state->current_function);
242916a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
243016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      if (opt_return_value) {
2431ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	 if (state->current_function->return_type->base_type ==
2432ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	     GLSL_TYPE_VOID) {
2433ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	    YYLTYPE loc = this->get_location();
2434ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt
2435ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	    _mesa_glsl_error(& loc, state,
2436ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt			     "`return` with a value, in function `%s' "
2437ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt			     "returning void",
2438f96c52ba2e290e3ba5f14cd7f87ba5b4382a1785Kenneth Graunke			     state->current_function->function_name());
2439ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	 }
244016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
244116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 ir_expression *const ret = (ir_expression *)
244216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	    opt_return_value->hir(instructions, state);
244316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 assert(ret != NULL);
244416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
244518707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	 /* Implicit conversions are not allowed for return values. */
244618707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	 if (state->current_function->return_type != ret->type) {
244718707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	    YYLTYPE loc = this->get_location();
244818707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke
244918707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	    _mesa_glsl_error(& loc, state,
245018707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     "`return' with wrong type %s, in function `%s' "
245118707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     "returning %s",
245218707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     ret->type->name,
245318707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     state->current_function->function_name(),
245418707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     state->current_function->return_type->name);
245518707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	 }
245616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
24571660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 inst = new(ctx) ir_return(ret);
245816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      } else {
2459aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	 if (state->current_function->return_type->base_type !=
2460aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	     GLSL_TYPE_VOID) {
2461aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	    YYLTYPE loc = this->get_location();
2462aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt
2463aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	    _mesa_glsl_error(& loc, state,
2464aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt			     "`return' with no value, in function %s returning "
2465aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt			     "non-void",
2466f96c52ba2e290e3ba5f14cd7f87ba5b4382a1785Kenneth Graunke			     state->current_function->function_name());
2467aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	 }
24681660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 inst = new(ctx) ir_return;
246916a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      }
247016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
24716de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke      state->found_return = true;
247216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      instructions->push_tail(inst);
2473c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick      break;
247416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   }
247516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
2476c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_discard:
2477b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt      if (state->target != fragment_shader) {
2478b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt	 YYLTYPE loc = this->get_location();
2479b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt
2480b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt	 _mesa_glsl_error(& loc, state,
2481b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt			  "`discard' may only appear in a fragment shader");
2482b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt      }
248377049a702ad54e09c4102fe8c964e069944f83e5Kenneth Graunke      instructions->push_tail(new(ctx) ir_discard);
2484c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick      break;
2485c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick
2486c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_break:
2487c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_continue:
24884cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      /* FINISHME: Handle switch-statements.  They cannot contain 'continue',
24894cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick       * FINISHME: and they use a different IR instruction for 'break'.
24904cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick       */
24914cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      /* FINISHME: Correctly handle the nesting.  If a switch-statement is
24924cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick       * FINISHME: inside a loop, a 'continue' is valid and will bind to the
24934cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick       * FINISHME: loop.
24944cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick       */
24954cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      if (state->loop_or_switch_nesting == NULL) {
24964cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 YYLTYPE loc = this->get_location();
24974cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
24984cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 _mesa_glsl_error(& loc, state,
24994cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick			  "`%s' may only appear in a loop",
25004cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick			  (mode == ast_break) ? "break" : "continue");
25014cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      } else {
25024cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 ir_loop *const loop = state->loop_or_switch_nesting->as_loop();
25034cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
25042d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	 /* Inline the for loop expression again, since we don't know
25052d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	  * where near the end of the loop body the normal copy of it
25062d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	  * is going to be placed.
25072d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	  */
25082d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	 if (mode == ast_continue &&
25092d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	     state->loop_or_switch_nesting_ast->rest_expression) {
25102d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	    state->loop_or_switch_nesting_ast->rest_expression->hir(instructions,
25112d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt								    state);
25122d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	 }
25132d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt
25144cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 if (loop != NULL) {
25154cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	    ir_loop_jump *const jump =
25161660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	       new(ctx) ir_loop_jump((mode == ast_break)
25171660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				     ? ir_loop_jump::jump_break
25181660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				     : ir_loop_jump::jump_continue);
25194cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	    instructions->push_tail(jump);
25204cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 }
25214cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      }
25224cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
2523c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick      break;
2524b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt   }
2525b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt
252616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   /* Jump instructions do not have r-values.
252716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick    */
252816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   return NULL;
252916a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick}
25303c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
25313c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
25323c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanickir_rvalue *
25333c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanickast_selection_statement::hir(exec_list *instructions,
25343c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick			     struct _mesa_glsl_parse_state *state)
25353c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick{
2536953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
25371660a2954797e056caba319c5d6c70b0d4be22feCarl Worth
25383c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   ir_rvalue *const condition = this->condition->hir(instructions, state);
25393c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
25403c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
25413c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *
25423c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    "Any expression whose type evaluates to a Boolean can be used as the
25433c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    conditional expression bool-expression. Vector types are not accepted
25443c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    as the expression to if."
25453c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *
25463c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    * The checks are separated so that higher quality diagnostics can be
25473c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    * generated for cases where both rules are violated.
25483c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    */
25493c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
25503c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      YYLTYPE loc = this->condition->get_location();
25513c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
25523c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
25533c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick		       "boolean");
25543c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   }
25553c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
25561660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   ir_if *const stmt = new(ctx) ir_if(condition);
25573c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
2558665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke   if (then_statement != NULL) {
2559665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke      state->symbols->push_scope();
25604f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick      then_statement->hir(& stmt->then_instructions, state);
2561665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke      state->symbols->pop_scope();
2562665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke   }
25633c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
2564665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke   if (else_statement != NULL) {
2565665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke      state->symbols->push_scope();
25664f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick      else_statement->hir(& stmt->else_instructions, state);
2567665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke      state->symbols->pop_scope();
2568665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke   }
25693c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
25703c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   instructions->push_tail(stmt);
25713c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
25723c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   /* if-statements do not have r-values.
25733c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    */
25743c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   return NULL;
25753c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick}
25769e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
25779e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
25788c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickvoid
25798c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickast_iteration_statement::condition_to_hir(ir_loop *stmt,
25808c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick					  struct _mesa_glsl_parse_state *state)
25819e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick{
2582953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
25831660a2954797e056caba319c5d6c70b0d4be22feCarl Worth
25849e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   if (condition != NULL) {
25859e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      ir_rvalue *const cond =
25869e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 condition->hir(& stmt->body_instructions, state);
25879e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
25889e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      if ((cond == NULL)
25899e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	  || !cond->type->is_boolean() || !cond->type->is_scalar()) {
25909e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 YYLTYPE loc = condition->get_location();
25919e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
25929e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 _mesa_glsl_error(& loc, state,
25939e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick			  "loop condition must be scalar boolean");
25949e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      } else {
25959e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 /* As the first code in the loop body, generate a block that looks
25969e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	  * like 'if (!condition) break;' as the loop termination condition.
25979e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	  */
25989e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 ir_rvalue *const not_cond =
25991660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_expression(ir_unop_logic_not, glsl_type::bool_type, cond,
26001660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				   NULL);
26019e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
26021660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_if *const if_stmt = new(ctx) ir_if(not_cond);
26039e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
26049e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 ir_jump *const break_stmt =
26051660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
26069e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
26079e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 if_stmt->then_instructions.push_tail(break_stmt);
26089e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 stmt->body_instructions.push_tail(if_stmt);
26099e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      }
26109e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   }
26118c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick}
26128c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
26138c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
26148c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickir_rvalue *
26158c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickast_iteration_statement::hir(exec_list *instructions,
26168c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick			     struct _mesa_glsl_parse_state *state)
26178c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick{
2618953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
26191660a2954797e056caba319c5d6c70b0d4be22feCarl Worth
2620484606610e36ec7598f7733e9787d888b6a63d64Ian Romanick   /* For-loops and while-loops start a new scope, but do-while loops do not.
26218c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick    */
2622484606610e36ec7598f7733e9787d888b6a63d64Ian Romanick   if (mode != ast_do_while)
26238c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick      state->symbols->push_scope();
26248c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
26258c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   if (init_statement != NULL)
26268c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick      init_statement->hir(instructions, state);
26278c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
26281660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   ir_loop *const stmt = new(ctx) ir_loop();
26298c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   instructions->push_tail(stmt);
26308c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
26318c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   /* Track the current loop and / or switch-statement nesting.
26328c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick    */
26338c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   ir_instruction *const nesting = state->loop_or_switch_nesting;
26342d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt   ast_iteration_statement *nesting_ast = state->loop_or_switch_nesting_ast;
26352d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt
26368c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   state->loop_or_switch_nesting = stmt;
26372d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt   state->loop_or_switch_nesting_ast = this;
26388c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
26398c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   if (mode != ast_do_while)
26408c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick      condition_to_hir(stmt, state);
26419e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
26424f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick   if (body != NULL)
26434f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick      body->hir(& stmt->body_instructions, state);
26449e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
26459e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   if (rest_expression != NULL)
26469e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      rest_expression->hir(& stmt->body_instructions, state);
26479e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
26488c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   if (mode == ast_do_while)
26498c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick      condition_to_hir(stmt, state);
26508c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
2651484606610e36ec7598f7733e9787d888b6a63d64Ian Romanick   if (mode != ast_do_while)
26529e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      state->symbols->pop_scope();
26539e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
2654e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick   /* Restore previous nesting before returning.
2655e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick    */
2656e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick   state->loop_or_switch_nesting = nesting;
26572d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt   state->loop_or_switch_nesting_ast = nesting_ast;
2658e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick
26599e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   /* Loops do not have r-values.
26609e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick    */
26619e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   return NULL;
26629e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick}
26633455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
26643455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
26653455ce614424a5a23a23037e23d0454e476bceeaIan Romanickir_rvalue *
26663455ce614424a5a23a23037e23d0454e476bceeaIan Romanickast_type_specifier::hir(exec_list *instructions,
26673455ce614424a5a23a23037e23d0454e476bceeaIan Romanick			  struct _mesa_glsl_parse_state *state)
26683455ce614424a5a23a23037e23d0454e476bceeaIan Romanick{
26693455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   if (this->structure != NULL)
26703455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      return this->structure->hir(instructions, state);
267185ba37b97df8edd18b757bc475b12b66f4b117edIan Romanick
267285ba37b97df8edd18b757bc475b12b66f4b117edIan Romanick   return NULL;
26733455ce614424a5a23a23037e23d0454e476bceeaIan Romanick}
26743455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
26753455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
26763455ce614424a5a23a23037e23d0454e476bceeaIan Romanickir_rvalue *
26773455ce614424a5a23a23037e23d0454e476bceeaIan Romanickast_struct_specifier::hir(exec_list *instructions,
26783455ce614424a5a23a23037e23d0454e476bceeaIan Romanick			  struct _mesa_glsl_parse_state *state)
26793455ce614424a5a23a23037e23d0454e476bceeaIan Romanick{
26803455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   unsigned decl_count = 0;
26813455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
26823455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* Make an initial pass over the list of structure fields to determine how
26833455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * many there are.  Each element in this list is an ast_declarator_list.
26843455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * This means that we actually need to count the number of elements in the
26853455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * 'declarations' list in each of the elements.
26863455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
26872b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_declarator_list, decl_list, link,
26882b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick		       &this->declarations) {
2689304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      foreach_list_const (decl_ptr, & decl_list->declarations) {
26903455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 decl_count++;
26913455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      }
26923455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   }
26933455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
26943455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
26953455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* Allocate storage for the structure fields and process the field
26963455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * declarations.  As the declarations are processed, try to also convert
26973455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * the types to HIR.  This ensures that structure definitions embedded in
26983455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * other structure definitions are processed.
26993455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
270021b0dbd79937e9d6787f045af7d60d4b6c649ec8Eric Anholt   glsl_struct_field *const fields = talloc_array(state, glsl_struct_field,
270121b0dbd79937e9d6787f045af7d60d4b6c649ec8Eric Anholt						  decl_count);
27023455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
27033455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   unsigned i = 0;
27042b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_declarator_list, decl_list, link,
27052b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick		       &this->declarations) {
27063455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      const char *type_name;
27073455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
27083455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      decl_list->type->specifier->hir(instructions, state);
27093455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
27103455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      const glsl_type *decl_type =
27113455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 decl_list->type->specifier->glsl_type(& type_name, state);
27123455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
27132b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick      foreach_list_typed (ast_declaration, decl, link,
27142b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick			  &decl_list->declarations) {
27153455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 const struct glsl_type *const field_type =
27163455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	    (decl->is_array)
27173455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	    ? process_array_type(decl_type, decl->array_size, state)
27183455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	    : decl_type;
27193455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
272073986a7a262807ab2cfd6d46ae17cfc7a30cdfecIan Romanick	 fields[i].type = (field_type != NULL)
272173986a7a262807ab2cfd6d46ae17cfc7a30cdfecIan Romanick	    ? field_type : glsl_type::error_type;
27223455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 fields[i].name = decl->identifier;
27233455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 i++;
27243455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      }
27253455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   }
27263455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
27273455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   assert(i == decl_count);
27283455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
27291d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick   const char *name;
27301d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick   if (this->name == NULL) {
27311d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick      static unsigned anon_count = 1;
27321d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick      char buf[32];
27333455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
27341d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick      snprintf(buf, sizeof(buf), "#anon_struct_%04x", anon_count);
27351d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick      anon_count++;
27361d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick
27371d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick      name = strdup(buf);
27381d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick   } else {
27391d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick      name = this->name;
27401d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick   }
27411d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick
274249e3577b91f44013746f7a3db411e7041b7d899eIan Romanick   const glsl_type *t =
274349e3577b91f44013746f7a3db411e7041b7d899eIan Romanick      glsl_type::get_record_instance(fields, decl_count, name);
27441d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick
2745ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick   YYLTYPE loc = this->get_location();
2746a789ca649cb143c0c5bf3209ff1bde398fbd777eIan Romanick   if (!state->symbols->add_type(name, t)) {
2747ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick      _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
2748ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick   } else {
2749a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick
2750a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick      const glsl_type **s = (const glsl_type **)
2751a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick	 realloc(state->user_structures,
2752a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick		 sizeof(state->user_structures[0]) *
2753a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick		 (state->num_user_structures + 1));
2754a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick      if (s != NULL) {
2755a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick	 s[state->num_user_structures] = t;
2756a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick	 state->user_structures = s;
2757a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick	 state->num_user_structures++;
2758a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick      }
2759ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick   }
27603455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
27613455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* Structure type definitions do not have r-values.
27623455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
27633455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   return NULL;
27643455ce614424a5a23a23037e23d0454e476bceeaIan Romanick}
2765