ast_to_hir.cpp revision c824e35dd092a9cc0dbfd36d90fcdf1488c8942d
1a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick/*
2a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * Copyright © 2010 Intel Corporation
3a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *
4a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * Permission is hereby granted, free of charge, to any person obtaining a
5a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * copy of this software and associated documentation files (the "Software"),
6a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * to deal in the Software without restriction, including without limitation
7a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * and/or sell copies of the Software, and to permit persons to whom the
9a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * Software is furnished to do so, subject to the following conditions:
10a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *
11a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * The above copyright notice and this permission notice (including the next
12a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * paragraph) shall be included in all copies or substantial portions of the
13a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * Software.
14a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *
15a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * DEALINGS IN THE SOFTWARE.
22a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */
23a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
24a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick/**
25a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * \file ast_to_hir.c
26a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * Convert abstract syntax to to high-level intermediate reprensentation (HIR).
27a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *
28a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * During the conversion to HIR, the majority of the symantic checking is
29a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * preformed on the program.  This includes:
30a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *
31a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *    * Symbol table management
32a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *    * Type checking
33a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *    * Function binding
34a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *
35a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * The majority of this work could be done during parsing, and the parser could
36a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * probably generate HIR directly.  However, this results in frequent changes
37a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * to the parser code.  Since we do not assume that every system this complier
38a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * is built on will have Flex and Bison installed, we have to store the code
39a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * generated by these tools in our version control system.  In other parts of
40a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * the system we've seen problems where a parser was changed but the generated
41a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * code was not committed, merge conflicts where created because two developers
42a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * had slightly different versions of Bison installed, etc.
43a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *
44a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * I have also noticed that running Bison generated parsers in GDB is very
45a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * irritating.  When you get a segfault on '$$ = $1->foo', you can't very
46a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * well 'print $1' in GDB.
47a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *
48a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * As a result, my preference is to put as little C code as possible in the
49a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * parser (and lexer) sources.
50a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */
51a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick#include <stdio.h>
52a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick#include "main/imports.h"
538bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick#include "glsl_symbol_table.h"
54a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick#include "glsl_parser_extras.h"
55a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick#include "ast.h"
56a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick#include "glsl_types.h"
57a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick#include "ir.h"
58a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
59d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanickvoid
60d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick_mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
61d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick{
62d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick   struct simple_node *ptr;
63d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick
64adfb0cd7401251bef0c854ac945fce78f0ed11dbIan Romanick   _mesa_glsl_initialize_variables(instructions, state);
65a4e92c4b26578614d76ce71b53194ea5c0f58d6cIan Romanick   _mesa_glsl_initialize_constructors(instructions, state);
66c22c40015db32b68b33c4944b9d94bf499135ec5Eric Anholt   _mesa_glsl_initialize_functions(instructions, state);
67adfb0cd7401251bef0c854ac945fce78f0ed11dbIan Romanick
6841ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = NULL;
6941ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick
70d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick   foreach (ptr, & state->translation_unit) {
71d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick      ((ast_node *)ptr)->hir(instructions, state);
72d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick   }
73d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick}
74d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick
75d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick
760104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick/**
770104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * If a conversion is available, convert one operand to a different type
780104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick *
790104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * The \c from \c ir_rvalue is converted "in place".
800104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick *
810104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param to     Type that the operand it to be converted to
820104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param from   Operand that is being converted
830104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param state  GLSL compiler state
840104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick *
850104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \return
860104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * If a conversion is possible (or unnecessary), \c true is returned.
870104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * Otherwise \c false is returned.
880104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick */
890104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanickstatic bool
90bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickapply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
910104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick			  struct _mesa_glsl_parse_state *state)
920104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick{
93bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   if (to->base_type == from->type->base_type)
940104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return true;
950104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
960104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   /* This conversion was added in GLSL 1.20.  If the compilation mode is
970104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    * GLSL 1.10, the conversion is skipped.
980104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    */
990104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   if (state->language_version < 120)
1000104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return false;
1010104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1020104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
1030104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *
1040104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    "There are no implicit array or structure conversions. For
1050104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    example, an array of int cannot be implicitly converted to an
1060104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    array of float. There are no implicit conversions between
1070104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    signed and unsigned integers."
1080104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    */
1090104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   /* FINISHME: The above comment is partially a lie.  There is int/uint
1100104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    * FINISHME: conversion for immediate constants.
1110104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    */
112bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   if (!to->is_float() || !from->type->is_numeric())
1130104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return false;
1140104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
115bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   switch (from->type->base_type) {
1160104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   case GLSL_TYPE_INT:
117bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      from = new ir_expression(ir_unop_i2f, to, from, NULL);
1180104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      break;
1190104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   case GLSL_TYPE_UINT:
120bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      from = new ir_expression(ir_unop_u2f, to, from, NULL);
1210104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      break;
1220104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   case GLSL_TYPE_BOOL:
123dc58b3f8ccd817fdee390a3df5b8e0fb29d5397cEric Anholt      from = new ir_expression(ir_unop_b2f, to, from, NULL);
124dc58b3f8ccd817fdee390a3df5b8e0fb29d5397cEric Anholt      break;
1250104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   default:
1260104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      assert(0);
1270104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   }
1280104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1290104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   return true;
1300104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick}
1310104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1320104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
133a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
134bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickarithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
135a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick		       bool multiply,
136a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
137a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
138bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   const glsl_type *const type_a = value_a->type;
139bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   const glsl_type *const type_b = value_b->type;
1400104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
141a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
142a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
143a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The arithmetic binary operators add (+), subtract (-),
144a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    multiply (*), and divide (/) operate on integer and
145a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    floating-point scalars, vectors, and matrices."
146a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
14760b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   if (!type_a->is_numeric() || !type_b->is_numeric()) {
148a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state,
149a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "Operands to arithmetic operators must be numeric");
1500471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
151a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
152a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
153a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
154a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "If one operand is floating-point based and the other is
155a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    not, then the conversions from Section 4.1.10 "Implicit
156a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    Conversions" are applied to the non-floating-point-based operand."
157a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1580104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   if (!apply_implicit_conversion(type_a, value_b, state)
1590104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick       && !apply_implicit_conversion(type_b, value_a, state)) {
160a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state,
161a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "Could not implicitly convert operands to "
162a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "arithmetic operator");
1630104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return glsl_type::error_type;
164a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
165a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
166a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "If the operands are integer types, they must both be signed or
167a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    both be unsigned."
168a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
169a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * From this rule and the preceeding conversion it can be inferred that
170a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
17160b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick    * The is_numeric check above already filtered out the case where either
17260b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick    * type is not one of these, so now the base types need only be tested for
17360b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick    * equality.
174a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
175a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (type_a->base_type != type_b->base_type) {
176a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state,
177a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "base type mismatch for arithmetic operator");
1780471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
179a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
180a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
181a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "All arithmetic binary operators result in the same fundamental type
182a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    (signed integer, unsigned integer, or floating-point) as the
183a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    operands they operate on, after operand type conversion. After
184a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    conversion, the following cases are valid
185a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
186a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    * The two operands are scalars. In this case the operation is
187a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      applied, resulting in a scalar."
188a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
189cb36f8aaeeb09660843316270a781948f773d90bIan Romanick   if (type_a->is_scalar() && type_b->is_scalar())
190a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return type_a;
191a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
192a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*   "* One operand is a scalar, and the other is a vector or matrix.
193a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      In this case, the scalar operation is applied independently to each
194a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      component of the vector or matrix, resulting in the same size
195a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      vector or matrix."
196a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
197cb36f8aaeeb09660843316270a781948f773d90bIan Romanick   if (type_a->is_scalar()) {
198cb36f8aaeeb09660843316270a781948f773d90bIan Romanick      if (!type_b->is_scalar())
199a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 return type_b;
200cb36f8aaeeb09660843316270a781948f773d90bIan Romanick   } else if (type_b->is_scalar()) {
201a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return type_a;
202a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
203a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
204a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* All of the combinations of <scalar, scalar>, <vector, scalar>,
205a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
206a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * handled.
207a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
20860b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   assert(!type_a->is_scalar());
20960b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   assert(!type_b->is_scalar());
210a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
211a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*   "* The two operands are vectors of the same size. In this case, the
212a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operation is done component-wise resulting in the same size
213a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      vector."
214a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
215a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick   if (type_a->is_vector() && type_b->is_vector()) {
216a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      if (type_a == type_b) {
217a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 return type_a;
218a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      } else {
219a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 _mesa_glsl_error(loc, state,
220a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt			  "vector size mismatch for arithmetic operator");
221a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 return glsl_type::error_type;
222a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      }
223a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
224a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
225a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* All of the combinations of <scalar, scalar>, <vector, scalar>,
226a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
227a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * <vector, vector> have been handled.  At least one of the operands must
228a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * be matrix.  Further, since there are no integer matrix types, the base
229a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * type of both operands must be float.
230a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
23160b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   assert(type_a->is_matrix() || type_b->is_matrix());
232a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   assert(type_a->base_type == GLSL_TYPE_FLOAT);
233a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   assert(type_b->base_type == GLSL_TYPE_FLOAT);
234a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
235a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*   "* The operator is add (+), subtract (-), or divide (/), and the
236a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operands are matrices with the same number of rows and the same
237a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      number of columns. In this case, the operation is done component-
238a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      wise resulting in the same size matrix."
239a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    * The operator is multiply (*), where both operands are matrices or
240a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      one operand is a vector and the other a matrix. A right vector
241a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operand is treated as a column vector and a left vector operand as a
242a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      row vector. In all these cases, it is required that the number of
243a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      columns of the left operand is equal to the number of rows of the
244a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      right operand. Then, the multiply (*) operation does a linear
245a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      algebraic multiply, yielding an object that has the same number of
246a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      rows as the left operand and the same number of columns as the right
247a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operand. Section 5.10 "Vector and Matrix Operations" explains in
248a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      more detail how vectors and matrices are operated on."
249a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
250a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (! multiply) {
251a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      if (type_a == type_b)
252a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 return type_a;
253a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
254fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick      if (type_a->is_matrix() && type_b->is_matrix()) {
255c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 /* Matrix multiply.  The columns of A must match the rows of B.  Given
256c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * the other previously tested constraints, this means the vector type
257c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * of a row from A must be the same as the vector type of a column from
258c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * B.
259c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  */
260c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 if (type_a->row_type() == type_b->column_type()) {
261c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	    /* The resulting matrix has the number of columns of matrix B and
262c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     * the number of rows of matrix A.  We get the row count of A by
263c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     * looking at the size of a vector that makes up a column.  The
264c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     * transpose (size of a row) is done for B.
265c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     */
266a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	    const glsl_type *const type =
267c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	       glsl_type::get_instance(type_a->base_type,
268c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick				       type_a->column_type()->vector_elements,
269c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick				       type_b->row_type()->vector_elements);
270a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	    assert(type != glsl_type::error_type);
271a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt
272a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	    return type;
273a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
274fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick      } else if (type_a->is_matrix()) {
275a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 /* A is a matrix and B is a column vector.  Columns of A must match
276c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * rows of B.  Given the other previously tested constraints, this
277c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * means the vector type of a row from A must be the same as the
278c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * vector the type of B.
279a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  */
280c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 if (type_a->row_type() == type_b)
281a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    return type_b;
282a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
283fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick	 assert(type_b->is_matrix());
284a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
285c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 /* A is a row vector and B is a matrix.  Columns of A must match rows
286c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * of B.  Given the other previously tested constraints, this means
287c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * the type of A must be the same as the vector type of a column from
288c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * B.
289a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  */
290c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 if (type_a == type_b->column_type())
291a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    return type_a;
292a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
293a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt
294a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication");
295a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      return glsl_type::error_type;
296a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
297a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
298a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
299a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "All other cases are illegal."
300a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
301a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt   _mesa_glsl_error(loc, state, "type mismatch");
3020471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::error_type;
303a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
304a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
305a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
306a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
30765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholtunary_arithmetic_result_type(const struct glsl_type *type,
30865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt			     struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
309a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
310a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 57:
311a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
312a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The arithmetic unary operators negate (-), post- and pre-increment
313a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     and decrement (-- and ++) operate on integer or floating-point
314a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     values (including vectors and matrices). All unary operators work
315a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     component-wise on their operands. These result with the same type
316a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     they operated on."
317a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
31865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   if (!type->is_numeric()) {
31965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state,
32065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "Operands to arithmetic operators must be numeric");
3210471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
32265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   }
323a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
324a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return type;
325a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
326a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
327a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
328a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
329a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickmodulus_result_type(const struct glsl_type *type_a,
33065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		    const struct glsl_type *type_b,
33165e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		    struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
332a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
333a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
334a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The operator modulus (%) operates on signed or unsigned integers or
335a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    integer vectors. The operand types must both be signed or both be
336a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    unsigned."
337a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
33840176e249f72b6090204611873b19aed3da67c71Ian Romanick   if (!type_a->is_integer() || !type_b->is_integer()
339a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       || (type_a->base_type != type_b->base_type)) {
34065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state, "type mismatch");
3410471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
342a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
343a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
344a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The operands cannot be vectors of differing size. If one operand is
345a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    a scalar and the other vector, then the scalar is applied component-
346a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    wise to the vector, resulting in the same type as the vector. If both
347a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    are vectors of the same size, the result is computed component-wise."
348a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
349a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick   if (type_a->is_vector()) {
350a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick      if (!type_b->is_vector()
351a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  || (type_a->vector_elements == type_b->vector_elements))
352a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 return type_a;
353a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else
354a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return type_b;
355a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
356a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The operator modulus (%) is not defined for any other data types
357a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    (non-integer types)."
358a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
35965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   _mesa_glsl_error(loc, state, "type mismatch");
3600471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::error_type;
361a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
362a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
363a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
364a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
365bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickrelational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
36665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
367a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
368bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   const glsl_type *const type_a = value_a->type;
369bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   const glsl_type *const type_b = value_b->type;
3700150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick
371a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
372a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The relational operators greater than (>), less than (<), greater
373a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    than or equal (>=), and less than or equal (<=) operate only on
374a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    scalar integer and scalar floating-point expressions."
375a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
376a6d653dcbbee3158f9ea7b284bdeb1a8432f0fcbIan Romanick   if (!type_a->is_numeric()
377a6d653dcbbee3158f9ea7b284bdeb1a8432f0fcbIan Romanick       || !type_b->is_numeric()
378cb36f8aaeeb09660843316270a781948f773d90bIan Romanick       || !type_a->is_scalar()
37965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt       || !type_b->is_scalar()) {
38065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state,
38165e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "Operands to relational operators must be scalar and "
38265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "numeric");
3830471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
38465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   }
385a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
386a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "Either the operands' types must match, or the conversions from
387a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    Section 4.1.10 "Implicit Conversions" will be applied to the integer
388a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    operand, after which the types must match."
389a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
3900150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick   if (!apply_implicit_conversion(type_a, value_b, state)
3910150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick       && !apply_implicit_conversion(type_b, value_a, state)) {
39265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state,
39365e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "Could not implicitly convert operands to "
39465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "relational operator");
3950150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick      return glsl_type::error_type;
396a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
397a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
39865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   if (type_a->base_type != type_b->base_type) {
39965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state, "base type mismatch");
4000471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
40165e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   }
402a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
403a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The result is scalar Boolean."
404a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
4050471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::bool_type;
406a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
407a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
408a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
4090bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick/**
4100bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Validates that a value can be assigned to a location with a specified type
4110bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
4120bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Validates that \c rhs can be assigned to some location.  If the types are
4130bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * not an exact match but an automatic conversion is possible, \c rhs will be
4140bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * converted.
4150bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
4160bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \return
4170bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
4180bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Otherwise the actual RHS to be assigned will be returned.  This may be
4190bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \c rhs, or it may be \c rhs after some type conversion.
4200bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
4210bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \note
4220bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * In addition to being used for assignments, this function is used to
4230bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * type-check return values.
4240bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick */
425fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
426fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkevalidate_assignment(const glsl_type *lhs_type, ir_rvalue *rhs)
4270bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick{
4280bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   const glsl_type *const rhs_type = rhs->type;
4290bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
4300bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   /* If there is already some error in the RHS, just return it.  Anything
4310bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    * else will lead to an avalanche of error message back to the user.
4320bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    */
4330bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   if (rhs_type->is_error())
4340bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick      return rhs;
4350bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
4360bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   /* If the types are identical, the assignment can trivially proceed.
4370bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    */
4380bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   if (rhs_type == lhs_type)
4390bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick      return rhs;
4400bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
4410157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick   /* If the array element types are the same and the size of the LHS is zero,
4420157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    * the assignment is okay.
4430157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    *
4440157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
4450157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    * is handled by ir_dereference::is_lvalue.
4460157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    */
4470157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick   if (lhs_type->is_array() && rhs->type->is_array()
4480157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       && (lhs_type->element_type() == rhs->type->element_type())
4490157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       && (lhs_type->array_size() == 0)) {
4500157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      return rhs;
4510157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick   }
4520157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
4530bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   /* FINISHME: Check for and apply automatic conversions. */
4540bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   return NULL;
4550bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick}
4560bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
45710a685218610e737e23d2d8a243ed6ff6613becdEric Anholtir_rvalue *
45810a685218610e737e23d2d8a243ed6ff6613becdEric Anholtdo_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
45910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	      ir_rvalue *lhs, ir_rvalue *rhs,
46010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	      YYLTYPE lhs_loc)
46110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt{
46210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
46310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
46410a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   if (!error_emitted) {
46510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
46610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      if (!lhs->is_lvalue()) {
46710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	 _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
46810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	 error_emitted = true;
46910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      }
47010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   }
47110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
47210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   ir_rvalue *new_rhs = validate_assignment(lhs->type, rhs);
47310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   if (new_rhs == NULL) {
47410a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      _mesa_glsl_error(& lhs_loc, state, "type mismatch");
47510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   } else {
47610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      rhs = new_rhs;
4770157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
4780157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      /* If the LHS array was not declared with a size, it takes it size from
4790157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       * the RHS.  If the LHS is an l-value and a whole array, it must be a
4800157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       * dereference of a variable.  Any other case would require that the LHS
4810157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       * is either not an l-value or not a whole array.
4820157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       */
4830157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      if (lhs->type->array_size() == 0) {
4840157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 ir_dereference *const d = lhs->as_dereference();
4850157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
4860157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 assert(d != NULL);
4870157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
4880157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 ir_variable *const var = d->var->as_variable();
4890157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
4900157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 assert(var != NULL);
4910157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
49263f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	 if (var->max_array_access >= unsigned(rhs->type->array_size())) {
49363f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	    /* FINISHME: This should actually log the location of the RHS. */
49463f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	    _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
49563f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick			     "previous access",
49663f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick			     var->max_array_access);
49763f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	 }
49863f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick
4990157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 var->type = glsl_type::get_array_instance(lhs->type->element_type(),
5000157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick						   rhs->type->array_size());
5010157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      }
50210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   }
50310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
50410a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   ir_instruction *tmp = new ir_assignment(lhs, rhs, NULL);
50510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   instructions->push_tail(tmp);
50610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
50710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   return rhs;
50810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt}
5090bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
5105185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick
5115185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick/**
5125185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick * Generate a new temporary and add its declaration to the instruction stream
5135185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick */
5145185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanickstatic ir_variable *
5155185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanickgenerate_temporary(const glsl_type *type, exec_list *instructions,
5165185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick		   struct _mesa_glsl_parse_state *state)
5175185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick{
5185185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick   char *name = (char *) malloc(sizeof(char) * 13);
5195185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick
5205185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick   snprintf(name, 13, "tmp_%08X", state->temp_index);
5215185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick   state->temp_index++;
5225185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick
5235185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick   ir_variable *const var = new ir_variable(type, name);
5245185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick   instructions->push_tail(var);
5255185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick
5265185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick   return var;
5275185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick}
5285185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick
5295185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick
530de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholtstatic ir_rvalue *
531de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholtget_lvalue_copy(exec_list *instructions, struct _mesa_glsl_parse_state *state,
532de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt		ir_rvalue *lvalue, YYLTYPE loc)
533de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt{
534de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   ir_variable *var;
535de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   ir_rvalue *var_deref;
536de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
537de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   /* FINISHME: Give unique names to the temporaries. */
538de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   var = new ir_variable(lvalue->type, "_internal_tmp");
539de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   var->mode = ir_var_auto;
540de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
541de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   var_deref = new ir_dereference(var);
542de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   do_assignment(instructions, state, var_deref, lvalue, loc);
543de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
544de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   /* Once we've created this temporary, mark it read only so it's no
545de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt    * longer considered an lvalue.
546de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt    */
547de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   var->read_only = true;
548de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
549de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   return var_deref;
550de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt}
551de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
552de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
553fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
5540044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_node::hir(exec_list *instructions,
55518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick	      struct _mesa_glsl_parse_state *state)
55618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick{
55718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   (void) instructions;
55818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   (void) state;
55918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
56018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   return NULL;
56118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick}
56218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
56318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
564fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
5650044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_expression::hir(exec_list *instructions,
56618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick		    struct _mesa_glsl_parse_state *state)
567a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
568a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   static const int operations[AST_NUM_OPERATORS] = {
569a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_assign doesn't convert to ir_expression. */
570a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_plus doesn't convert to ir_expression. */
571a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_neg,
572a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_add,
573a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_sub,
574a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mul,
575a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_div,
576a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mod,
577a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lshift,
578a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_rshift,
579a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_less,
580a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_greater,
581a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lequal,
582a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_gequal,
583a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_equal,
584a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_nequal,
585a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_and,
586a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_xor,
587a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_or,
588a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_bit_not,
589a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_and,
590a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_xor,
591a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_or,
592a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_logic_not,
593a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
594a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Note: The following block of expression types actually convert
595a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * to multiple IR instructions.
596a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
597a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mul,     /* ast_mul_assign */
598a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_div,     /* ast_div_assign */
599a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mod,     /* ast_mod_assign */
600a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_add,     /* ast_add_assign */
601a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_sub,     /* ast_sub_assign */
602a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lshift,  /* ast_ls_assign */
603a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_rshift,  /* ast_rs_assign */
604a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_and, /* ast_and_assign */
605a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_xor, /* ast_xor_assign */
606a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_or,  /* ast_or_assign */
607a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
608a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_conditional doesn't convert to ir_expression. */
609de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_add,     /* ast_pre_inc. */
610de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_sub,     /* ast_pre_dec. */
611de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_add,     /* ast_post_inc. */
612de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_sub,     /* ast_post_dec. */
613a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_field_selection doesn't conv to ir_expression. */
614a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_array_index doesn't convert to ir_expression. */
615a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_function_call doesn't conv to ir_expression. */
616a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_identifier doesn't convert to ir_expression. */
617a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_int_constant doesn't convert to ir_expression. */
618a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_uint_constant doesn't conv to ir_expression. */
619a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_float_constant doesn't conv to ir_expression. */
620a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_bool_constant doesn't conv to ir_expression. */
621a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_sequence doesn't convert to ir_expression. */
622a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   };
623fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke   ir_rvalue *result = NULL;
624fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke   ir_rvalue *op[2];
625a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   struct simple_node op_list;
6260471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   const struct glsl_type *type = glsl_type::error_type;
627a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   bool error_emitted = false;
628a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   YYLTYPE loc;
629a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
63018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   loc = this->get_location();
631a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   make_empty_list(& op_list);
632a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
63318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   switch (this->oper) {
6346652af36fe8994b1621d882fcc230d320908a2a3Ian Romanick   case ast_assign: {
63518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
63618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
637a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
63810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      result = do_assignment(instructions, state, op[0], op[1],
63910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt			     this->subexpressions[0]->get_location());
64010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      error_emitted = result->type->is_error();
64110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      type = result->type;
642a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
6436652af36fe8994b1621d882fcc230d320908a2a3Ian Romanick   }
644a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
645a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_plus:
64618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
647a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
648a43817a483a8c4a480ef4e6dfda2cef899300eb0Ian Romanick      error_emitted = op[0]->type->is_error();
649a43817a483a8c4a480ef4e6dfda2cef899300eb0Ian Romanick      if (type->is_error())
650a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 op[0]->type = type;
651a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
652a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      result = op[0];
653a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
654a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
655a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_neg:
65618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
657a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
65865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = unary_arithmetic_result_type(op[0]->type, state, & loc);
659a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
66065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
661a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
66218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_expression(operations[this->oper], type,
663a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick				 op[0], NULL);
664a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
665a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
666a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_add:
667a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sub:
668a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mul:
669a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_div:
67018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
67118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
672a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
673bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      type = arithmetic_result_type(op[0], op[1],
67418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick				    (this->oper == ast_mul),
675a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt				    state, & loc);
676a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      error_emitted = type->is_error();
677a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
67818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_expression(operations[this->oper], type,
679a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick				 op[0], op[1]);
680a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
681a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
682a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mod:
68318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
68418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
685a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
68665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
687a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
68818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      assert(operations[this->oper] == ir_binop_mod);
689a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
69018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_expression(operations[this->oper], type,
691a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick				 op[0], op[1]);
69265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
693a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
694a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
695a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_lshift:
696a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_rshift:
697183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      _mesa_glsl_error(& loc, state, "FINISHME: implement bit-shift operators");
698183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      error_emitted = true;
699a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
700a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
701a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_less:
702a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_greater:
703a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_lequal:
704a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_gequal:
70518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
70618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
707a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
70865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = relational_result_type(op[0], op[1], state, & loc);
709a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
710a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* The relational operators must either generate an error or result
711a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * in a scalar boolean.  See page 57 of the GLSL 1.50 spec.
712a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
713a43817a483a8c4a480ef4e6dfda2cef899300eb0Ian Romanick      assert(type->is_error()
714a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	     || ((type->base_type == GLSL_TYPE_BOOL)
715cb36f8aaeeb09660843316270a781948f773d90bIan Romanick		 && type->is_scalar()));
716a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
71718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_expression(operations[this->oper], type,
718a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick				 op[0], op[1]);
71965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
720a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
721a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
722a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_nequal:
723a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_equal:
7246e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
7256e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
7266e659caaa946339a2de3890a8bed091ccb65102aIan Romanick
7276e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
7286e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *
7296e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    "The equality operators equal (==), and not equal (!=)
7306e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    operate on all types. They result in a scalar Boolean. If
7316e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    the operand types do not match, then there must be a
7326e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    conversion from Section 4.1.10 "Implicit Conversions"
7336e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    applied to one operand that can make them match, in which
7346e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    case this conversion is done."
7356e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       */
736bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      if ((!apply_implicit_conversion(op[0]->type, op[1], state)
737bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick	   && !apply_implicit_conversion(op[1]->type, op[0], state))
738212b0327b47033442842a7be3d7fb10e08e2bf66Ian Romanick	  || (op[0]->type != op[1]->type)) {
7396e659caaa946339a2de3890a8bed091ccb65102aIan Romanick	 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
7406e659caaa946339a2de3890a8bed091ccb65102aIan Romanick			  "type", (this->oper == ast_equal) ? "==" : "!=");
7416e659caaa946339a2de3890a8bed091ccb65102aIan Romanick	 error_emitted = true;
742a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick      } else if ((state->language_version <= 110)
743a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick		 && (op[0]->type->is_array() || op[1]->type->is_array())) {
744a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick	 _mesa_glsl_error(& loc, state, "array comparisons forbidden in "
745a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick			  "GLSL 1.10");
746a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick	 error_emitted = true;
7476e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      }
7486e659caaa946339a2de3890a8bed091ccb65102aIan Romanick
7496e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      result = new ir_expression(operations[this->oper], glsl_type::bool_type,
7506e659caaa946339a2de3890a8bed091ccb65102aIan Romanick				 op[0], op[1]);
7516e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      type = glsl_type::bool_type;
7526e659caaa946339a2de3890a8bed091ccb65102aIan Romanick
7536e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      assert(result->type == glsl_type::bool_type);
754a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
755a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
756a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_and:
757a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_xor:
758a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_or:
759a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_not:
760183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      _mesa_glsl_error(& loc, state, "FINISHME: implement bit-wise operators");
761183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      error_emitted = true;
762a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
763a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
7644950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   case ast_logic_and: {
765b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
766b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
767b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
768b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt	 YYLTYPE loc = this->subexpressions[0]->get_location();
769b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
770b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt	 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
771b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt			  operator_string(this->oper));
772ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt	 error_emitted = true;
773b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      }
774b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
77544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      ir_constant *op0_const = op[0]->constant_expression_value();
77644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      if (op0_const) {
77744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 if (op0_const->value.b[0]) {
77844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    op[1] = this->subexpressions[1]->hir(instructions, state);
77944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt
78044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
78144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       YYLTYPE loc = this->subexpressions[1]->get_location();
78244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt
78344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       _mesa_glsl_error(& loc, state,
78444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt				"RHS of `%s' must be scalar boolean",
78544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt				operator_string(this->oper));
78644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       error_emitted = true;
78744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    }
78844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    result = op[1];
78944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 } else {
79044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    result = op0_const;
79144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 }
79244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 type = glsl_type::bool_type;
79344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      } else {
79444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_if *const stmt = new ir_if(op[0]);
79544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 instructions->push_tail(stmt);
7964950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
79744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 op[1] = this->subexpressions[1]->hir(&stmt->then_instructions, state);
7984950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
79944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
80044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    YYLTYPE loc = this->subexpressions[1]->get_location();
8014950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
80244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    _mesa_glsl_error(& loc, state,
80344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt			     "RHS of `%s' must be scalar boolean",
80444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt			     operator_string(this->oper));
80544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    error_emitted = true;
80644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 }
807b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
80844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_variable *const tmp = generate_temporary(glsl_type::bool_type,
80944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt						     instructions, state);
810b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
81144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_dereference *const then_deref = new ir_dereference(tmp);
81244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const then_assign =
81344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    new ir_assignment(then_deref, op[1], NULL);
81444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->then_instructions.push_tail(then_assign);
8154950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
81644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_dereference *const else_deref = new ir_dereference(tmp);
81744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const else_assign =
81844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    new ir_assignment(else_deref, new ir_constant(false), NULL);
81944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->else_instructions.push_tail(else_assign);
8204950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
82144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 result = new ir_dereference(tmp);
82244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 type = tmp->type;
82344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      }
8244950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      break;
8254950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   }
8264950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
8274950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   case ast_logic_or: {
8284950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
8294950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
8304950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
8314950a68bf22ede6f4f368c9783e5401816159574Eric Anholt	 YYLTYPE loc = this->subexpressions[0]->get_location();
8324950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
8334950a68bf22ede6f4f368c9783e5401816159574Eric Anholt	 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
8344950a68bf22ede6f4f368c9783e5401816159574Eric Anholt			  operator_string(this->oper));
8354950a68bf22ede6f4f368c9783e5401816159574Eric Anholt	 error_emitted = true;
8364950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      }
8374950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
83844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      ir_constant *op0_const = op[0]->constant_expression_value();
83944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      if (op0_const) {
84044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 if (op0_const->value.b[0]) {
84144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    result = op0_const;
84244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 } else {
84344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    op[1] = this->subexpressions[1]->hir(instructions, state);
84444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt
84544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
84644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       YYLTYPE loc = this->subexpressions[1]->get_location();
84744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt
84844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       _mesa_glsl_error(& loc, state,
84944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt				"RHS of `%s' must be scalar boolean",
85044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt				operator_string(this->oper));
85144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       error_emitted = true;
85244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    }
85344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    result = op[1];
85444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 }
85544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 type = glsl_type::bool_type;
85644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      } else {
85744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_if *const stmt = new ir_if(op[0]);
85844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 instructions->push_tail(stmt);
8594950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
86044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_variable *const tmp = generate_temporary(glsl_type::bool_type,
86144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt						     instructions, state);
8624950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
86344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 op[1] = this->subexpressions[1]->hir(&stmt->then_instructions, state);
8644950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
86544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
86644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    YYLTYPE loc = this->subexpressions[1]->get_location();
8674950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
86844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    _mesa_glsl_error(& loc, state, "RHS of `%s' must be scalar boolean",
86944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt			     operator_string(this->oper));
87044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    error_emitted = true;
87144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 }
8724950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
87344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_dereference *const then_deref = new ir_dereference(tmp);
87444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const then_assign =
87544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    new ir_assignment(then_deref, new ir_constant(true), NULL);
87644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->then_instructions.push_tail(then_assign);
8774950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
87844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_dereference *const else_deref = new ir_dereference(tmp);
87944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const else_assign =
88044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    new ir_assignment(else_deref, op[1], NULL);
88144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->else_instructions.push_tail(else_assign);
8824950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
88344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 result = new ir_dereference(tmp);
88444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 type = tmp->type;
88544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      }
8864950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      break;
8874950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   }
8884950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
8894950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   case ast_logic_xor:
8904950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
8914950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      op[1] = this->subexpressions[1]->hir(instructions, state);
8924950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
8934950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
894b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      result = new ir_expression(operations[this->oper], glsl_type::bool_type,
895b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt				 op[0], op[1]);
896ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt      type = glsl_type::bool_type;
897a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
898a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
899a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt   case ast_logic_not:
900a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
901a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
902a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
903a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt	 YYLTYPE loc = this->subexpressions[0]->get_location();
904a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
905a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt	 _mesa_glsl_error(& loc, state,
906a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt			  "operand of `!' must be scalar boolean");
907ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt	 error_emitted = true;
908a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      }
909a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
910a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      result = new ir_expression(operations[this->oper], glsl_type::bool_type,
911a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt				 op[0], NULL);
912ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt      type = glsl_type::bool_type;
913a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      break;
914a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
915a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mul_assign:
916a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_div_assign:
917a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_add_assign:
918a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sub_assign: {
91918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
92018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
921a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
922bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      type = arithmetic_result_type(op[0], op[1],
92318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick				    (this->oper == ast_mul_assign),
924a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt				    state, & loc);
925a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
926fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke      ir_rvalue *temp_rhs = new ir_expression(operations[this->oper], type,
927fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke				              op[0], op[1]);
928a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
92910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      result = do_assignment(instructions, state, op[0], temp_rhs,
93010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt			     this->subexpressions[0]->get_location());
93110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      type = result->type;
93210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      error_emitted = (op[0]->type->is_error());
933a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
934a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* GLSL 1.10 does not allow array assignment.  However, we don't have to
935a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * explicitly test for this because none of the binary expression
936a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * operators allow array operands either.
937a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
938a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
939a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
940a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
941a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
94248a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt   case ast_mod_assign: {
94348a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
94448a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      op[1] = this->subexpressions[1]->hir(instructions, state);
94548a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
94665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
94748a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
94848a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      assert(operations[this->oper] == ir_binop_mod);
94948a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
95048a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      struct ir_rvalue *temp_rhs;
95148a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      temp_rhs = new ir_expression(operations[this->oper], type,
95248a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt				   op[0], op[1]);
95348a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
95448a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      result = do_assignment(instructions, state, op[0], temp_rhs,
95548a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt			     this->subexpressions[0]->get_location());
95648a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      type = result->type;
95765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
95848a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      break;
95948a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt   }
960a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
961a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_ls_assign:
962a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_rs_assign:
963183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      _mesa_glsl_error(& loc, state,
964183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt		       "FINISHME: implement bit-shift assignment operators");
965183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      error_emitted = true;
966251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
967a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
968a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_and_assign:
969a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_xor_assign:
970a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_or_assign:
971183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      _mesa_glsl_error(& loc, state,
972183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt		       "FINISHME: implement logic assignment operators");
973183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      error_emitted = true;
974251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
975a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
97696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick   case ast_conditional: {
97796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
97896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
97996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
98096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *
98196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    "The ternary selection operator (?:). It operates on three
98296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    expressions (exp1 ? exp2 : exp3). This operator evaluates the
98396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    first expression, which must result in a scalar Boolean."
98496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
98596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
98696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 YYLTYPE loc = this->subexpressions[0]->get_location();
98796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
98896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 _mesa_glsl_error(& loc, state, "?: condition must be scalar boolean");
98996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 error_emitted = true;
99096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      }
99196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
99296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* The :? operator is implemented by generating an anonymous temporary
99396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * followed by an if-statement.  The last instruction in each branch of
99496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * the if-statement assigns a value to the anonymous temporary.  This
99596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * temporary is the r-value of the expression.
99696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
99796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      ir_variable *const tmp = generate_temporary(glsl_type::error_type,
99896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick						  instructions, state);
99996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
100096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      ir_if *const stmt = new ir_if(op[0]);
100196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      instructions->push_tail(stmt);
100296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
100396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      op[1] = this->subexpressions[1]->hir(& stmt->then_instructions, state);
100496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      ir_dereference *const then_deref = new ir_dereference(tmp);
100596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      ir_assignment *const then_assign =
100696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 new ir_assignment(then_deref, op[1], NULL);
100796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      stmt->then_instructions.push_tail(then_assign);
100896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
100996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      op[2] = this->subexpressions[2]->hir(& stmt->else_instructions, state);
101096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      ir_dereference *const else_deref = new ir_dereference(tmp);
101196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      ir_assignment *const else_assign =
101296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 new ir_assignment(else_deref, op[2], NULL);
101396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      stmt->else_instructions.push_tail(else_assign);
101496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
101596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
101696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *
101796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     "The second and third expressions can be any type, as
101896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     long their types match, or there is a conversion in
101996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     Section 4.1.10 "Implicit Conversions" that can be applied
102096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     to one of the expressions to make their types match. This
102196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     resulting matching type is the type of the entire
102296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     expression."
102396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
1024bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      if ((!apply_implicit_conversion(op[1]->type, op[2], state)
1025bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick	   && !apply_implicit_conversion(op[2]->type, op[1], state))
1026db9be2e7aa3a56e43b725ad7725fe6b424e4933eIan Romanick	  || (op[1]->type != op[2]->type)) {
102796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 YYLTYPE loc = this->subexpressions[1]->get_location();
102896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
102996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
103096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick			  "operator must have matching types.");
103196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 error_emitted = true;
1032db9be2e7aa3a56e43b725ad7725fe6b424e4933eIan Romanick      } else {
1033db9be2e7aa3a56e43b725ad7725fe6b424e4933eIan Romanick	 tmp->type = op[1]->type;
103496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      }
103596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
103696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      result = new ir_dereference(tmp);
103796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      type = tmp->type;
1038251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
103996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick   }
1040a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1041a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_pre_inc:
104276ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt   case ast_pre_dec: {
104376ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
104476ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
104576ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt	 op[1] = new ir_constant(1.0f);
104676ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      else
104776ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt	 op[1] = new ir_constant(1);
104876ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
1049a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      type = arithmetic_result_type(op[0], op[1], false, state, & loc);
105076ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
105176ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      struct ir_rvalue *temp_rhs;
105276ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      temp_rhs = new ir_expression(operations[this->oper], type,
105376ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt				   op[0], op[1]);
105476ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
105576ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      result = do_assignment(instructions, state, op[0], temp_rhs,
105676ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt			     this->subexpressions[0]->get_location());
105776ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      type = result->type;
105876ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      error_emitted = op[0]->type->is_error();
105976ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      break;
106076ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt   }
1061a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1062a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_post_inc:
1063de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   case ast_post_dec: {
1064de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
1065de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
1066de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt	 op[1] = new ir_constant(1.0f);
1067de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      else
1068de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt	 op[1] = new ir_constant(1);
1069de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1070de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1071de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1072a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1073de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1074de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      struct ir_rvalue *temp_rhs;
1075de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      temp_rhs = new ir_expression(operations[this->oper], type,
1076de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt				   op[0], op[1]);
1077de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1078de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      /* Get a temporary of a copy of the lvalue before it's modified.
1079de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt       * This may get thrown away later.
1080de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt       */
1081de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      result = get_lvalue_copy(instructions, state, op[0],
1082de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt			       this->subexpressions[0]->get_location());
1083de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1084de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      (void)do_assignment(instructions, state, op[0], temp_rhs,
1085de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt			  this->subexpressions[0]->get_location());
1086de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1087de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      type = result->type;
1088de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      error_emitted = op[0]->type->is_error();
1089a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1090de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   }
1091a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1092a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_field_selection:
109318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = _mesa_ast_field_selection_to_hir(this, instructions, state);
1094a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = result->type;
1095a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1096a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
109727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick   case ast_array_index: {
109827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      YYLTYPE index_loc = subexpressions[1]->get_location();
109927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
110027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      op[0] = subexpressions[0]->hir(instructions, state);
110127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      op[1] = subexpressions[1]->hir(instructions, state);
110227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
110327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
110427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
1105b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      ir_dereference *const lhs = op[0]->as_dereference();
1106b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      ir_instruction *array;
1107b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      if ((lhs != NULL)
1108b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	  && (lhs->mode == ir_dereference::ir_reference_variable)) {
1109b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	 result = new ir_dereference(lhs->var, op[1]);
1110b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
1111b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	 delete op[0];
1112b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	 array = lhs->var;
1113b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      } else {
1114b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	 result = new ir_dereference(op[0], op[1]);
1115b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	 array = op[0];
1116b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      }
1117b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
1118b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      /* Do not use op[0] after this point.  Use array.
1119b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick       */
1120b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      op[0] = NULL;
1121b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
112227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
112327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (error_emitted)
112427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 break;
112527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
112663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick      if (!array->type->is_array()
112763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	  && !array->type->is_matrix()
112863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	  && !array->type->is_vector()) {
112927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
113063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			  "cannot dereference non-array / non-matrix / "
113163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			  "non-vector");
113227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
113327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
113427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
113527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (!op[1]->type->is_integer()) {
113627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
113727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick			  "array index must be integer type");
113827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
113927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      } else if (!op[1]->type->is_scalar()) {
114027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
114127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick			  "array index must be scalar");
114227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
114327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
114427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
114527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      /* If the array index is a constant expression and the array has a
114627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * declared size, ensure that the access is in-bounds.  If the array
114727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * index is not a constant expression, ensure that the array has a
114827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * declared size.
114927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       */
115027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      ir_constant *const const_index = op[1]->constant_expression_value();
115127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (const_index != NULL) {
115227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 const int idx = const_index->value.i[0];
115363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 const char *type_name;
115463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 unsigned bound = 0;
115563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick
115663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (array->type->is_matrix()) {
115763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    type_name = "matrix";
115863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else if (array->type->is_vector()) {
115963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    type_name = "vector";
116063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else {
116163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    type_name = "array";
116263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 }
116327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
116427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
116527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *
116627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    "It is illegal to declare an array with a size, and then
116727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    later (in the same shader) index the same array with an
116827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    integral constant expression greater than or equal to the
116927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    declared size. It is also illegal to index an array with a
117027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    negative constant expression."
117127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  */
117263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (array->type->is_matrix()) {
117363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if (array->type->row_type()->vector_elements <= idx) {
117463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       bound = array->type->row_type()->vector_elements;
117563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    }
117663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else if (array->type->is_vector()) {
117763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if (array->type->vector_elements <= idx) {
117863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       bound = array->type->vector_elements;
117963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    }
118063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else {
118163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if ((array->type->array_size() > 0)
118263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick		&& (array->type->array_size() <= idx)) {
118363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       bound = array->type->array_size();
118463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    }
118527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 }
118627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
118763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (bound > 0) {
118863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    _mesa_glsl_error(& loc, state, "%s index must be < %u",
118963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			     type_name, bound);
119063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    error_emitted = true;
119163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else if (idx < 0) {
119263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    _mesa_glsl_error(& loc, state, "%s index must be >= 0",
119363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			     type_name);
119427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	    error_emitted = true;
119527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 }
1196b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
119763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (array->type->is_array()) {
119863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    ir_variable *const v = array->as_variable();
119963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if ((v != NULL) && (unsigned(idx) > v->max_array_access))
120063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       v->max_array_access = idx;
120163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 }
120227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
120327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
120427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (error_emitted)
120527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 result->type = glsl_type::error_type;
120627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
120727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      type = result->type;
1208a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
120927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick   }
1210a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1211a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_function_call:
12127cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick      /* Should *NEVER* get here.  ast_function_call should always be handled
12137cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick       * by ast_function_expression::hir.
1214a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
12157cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick      assert(0);
1216a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1217a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1218a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_identifier: {
1219a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* ast_identifier can appear several places in a full abstract syntax
1220a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * tree.  This particular use must be at location specified in the grammar
1221a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * as 'variable_identifier'.
1222a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
12238bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      ir_variable *var =
12248bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick	 state->symbols->get_variable(this->primary_expression.identifier);
1225a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1226a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      result = new ir_dereference(var);
1227a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1228a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (var != NULL) {
1229a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 type = result->type;
1230a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
123171d0bbfcb2853f37b580ec7b705e55bb0eb426faIan Romanick	 _mesa_glsl_error(& loc, state, "`%s' undeclared",
123218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  this->primary_expression.identifier);
1233a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1234a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 error_emitted = true;
1235a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1236a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1237a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1238a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1239a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_int_constant:
12400471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::int_type;
124118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_constant(type, & this->primary_expression);
1242a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1243a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1244a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_uint_constant:
12450471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::uint_type;
124618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_constant(type, & this->primary_expression);
1247a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1248a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1249a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_float_constant:
12500471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::float_type;
125118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_constant(type, & this->primary_expression);
1252a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1253a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1254a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bool_constant:
12550471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::bool_type;
125618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_constant(type, & this->primary_expression);
1257a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1258a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1259a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sequence: {
1260a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      struct simple_node *ptr;
1261a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1262a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* It should not be possible to generate a sequence in the AST without
1263a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * any expressions in it.
1264a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
126518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      assert(!is_empty_list(&this->expressions));
1266a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1267a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* The r-value of a sequence is the last expression in the sequence.  If
1268a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * the other expressions in the sequence do not have side-effects (and
1269a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * therefore add instructions to the instruction list), they get dropped
1270a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * on the floor.
1271a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
127218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      foreach (ptr, &this->expressions)
127318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick	 result = ((ast_node *)ptr)->hir(instructions, state);
1274a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1275a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = result->type;
1276a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1277a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Any errors should have already been emitted in the loop above.
1278a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1279a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      error_emitted = true;
1280a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1281a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1282a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1283a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1284cef3baecf636a30b62cd7a1e8de57c7650f7003eIan Romanick   if (type->is_error() && !error_emitted)
128571d0bbfcb2853f37b580ec7b705e55bb0eb426faIan Romanick      _mesa_glsl_error(& loc, state, "type mismatch");
1286a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1287a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return result;
1288a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1289a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1290a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1291fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
12920044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_expression_statement::hir(exec_list *instructions,
129318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			      struct _mesa_glsl_parse_state *state)
1294a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1295a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* It is possible to have expression statements that don't have an
1296a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * expression.  This is the solitary semicolon:
1297a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
1298a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * for (i = 0; i < 5; i++)
1299a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     ;
1300a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
1301a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * In this case the expression will be NULL.  Test for NULL and don't do
1302a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * anything in that case.
1303a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
130418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (expression != NULL)
130518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      expression->hir(instructions, state);
1306a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1307a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Statements do not have r-values.
1308a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1309a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1310a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1311a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1312a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1313fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
13140044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_compound_statement::hir(exec_list *instructions,
131518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			    struct _mesa_glsl_parse_state *state)
1316a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1317a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   struct simple_node *ptr;
1318a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1319a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
132018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (new_scope)
13218bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      state->symbols->push_scope();
1322a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
132318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   foreach (ptr, &statements)
132418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      ((ast_node *)ptr)->hir(instructions, state);
1325a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
132618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (new_scope)
13278bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      state->symbols->pop_scope();
1328a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1329a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Compound statements do not have r-values.
1330a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1331a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1332a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1333a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1334a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
133528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanickstatic const glsl_type *
133628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanickprocess_array_type(const glsl_type *base, ast_node *array_size,
133728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick		   struct _mesa_glsl_parse_state *state)
133828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick{
133928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   unsigned length = 0;
134028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
134128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   /* FINISHME: Reject delcarations of multidimensional arrays. */
134228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
134328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   if (array_size != NULL) {
134428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      exec_list dummy_instructions;
134528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
134628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      YYLTYPE loc = array_size->get_location();
134728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
134828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      /* FINISHME: Verify that the grammar forbids side-effects in array
134928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick       * FINISHME: sizes.   i.e., 'vec4 [x = 12] data'
135028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick       */
135128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      assert(dummy_instructions.is_empty());
135228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
135328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      if (ir != NULL) {
135428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 if (!ir->type->is_integer()) {
135528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    _mesa_glsl_error(& loc, state, "array size must be integer type");
135628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 } else if (!ir->type->is_scalar()) {
135728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    _mesa_glsl_error(& loc, state, "array size must be scalar type");
135828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 } else {
135928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    ir_constant *const size = ir->constant_expression_value();
136028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
136128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    if (size == NULL) {
136228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       _mesa_glsl_error(& loc, state, "array size must be a "
136328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick				"constant valued expression");
136428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    } else if (size->value.i[0] <= 0) {
136528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       _mesa_glsl_error(& loc, state, "array size must be > 0");
136628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    } else {
136728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       assert(size->type == ir->type);
136828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       length = size->value.u[0];
136928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    }
137028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 }
137128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      }
137228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   }
137328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
137428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   return glsl_type::get_array_instance(base, length);
137528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick}
137628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
137728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
1378d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanickconst glsl_type *
1379d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanickast_type_specifier::glsl_type(const char **name,
1380d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick			      struct _mesa_glsl_parse_state *state) const
1381a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1382d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   const struct glsl_type *type;
1383a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1384d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   if ((this->type_specifier == ast_struct) && (this->type_name == NULL)) {
1385a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* FINISHME: Handle annonymous structures. */
1386a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = NULL;
1387a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
1388d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick      type = state->symbols->get_type(this->type_name);
1389d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick      *name = this->type_name;
1390a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1391d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick      if (this->is_array) {
1392d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick	 type = process_array_type(type, this->array_size, state);
139328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      }
1394a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1395a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1396a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return type;
1397a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1398a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1399a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1400a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic void
1401a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickapply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
1402a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick				 struct ir_variable *var,
14032e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				 struct _mesa_glsl_parse_state *state,
14042e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				 YYLTYPE *loc)
1405a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1406a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->invariant)
1407a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->invariant = 1;
1408a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1409a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* FINISHME: Mark 'in' variables at global scope as read-only. */
1410a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->constant || qual->attribute || qual->uniform
1411a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       || (qual->varying && (state->target == fragment_shader)))
1412a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->read_only = 1;
1413a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1414a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->centroid)
1415a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->centroid = 1;
1416a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1417ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick   if (qual->attribute && state->target != vertex_shader) {
14182e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      var->type = glsl_type::error_type;
14192e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      _mesa_glsl_error(loc, state,
14202e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt		       "`attribute' variables may not be declared in the "
1421ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick		       "%s shader",
1422ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick		       _mesa_glsl_shader_target_name(state->target));
14232e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   }
14242e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt
142590b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt   /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
142690b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *
142790b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     "The varying qualifier can be used only with the data types
142890b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
142990b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     these."
143090b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    */
143190b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt   if (qual->varying && var->type->base_type != GLSL_TYPE_FLOAT) {
143290b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt      var->type = glsl_type::error_type;
143390b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt      _mesa_glsl_error(loc, state,
143490b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt		       "varying variables must be of base type float");
143590b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt   }
143690b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt
1437a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->in && qual->out)
1438a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_inout;
1439a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else if (qual->attribute || qual->in
1440a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    || (qual->varying && (state->target == fragment_shader)))
1441a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_in;
14420b678234625fac67a89285ad2871dedc891fb1b1Ian Romanick   else if (qual->out || (qual->varying && (state->target == vertex_shader)))
1443a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_out;
1444a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else if (qual->uniform)
1445a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_uniform;
1446a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else
1447a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_auto;
1448a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
144971df19f5ef6e78beb5160801f81468184b75447eEric Anholt   if (qual->uniform)
145071df19f5ef6e78beb5160801f81468184b75447eEric Anholt      var->shader_in = true;
145171df19f5ef6e78beb5160801f81468184b75447eEric Anholt   if (qual->varying) {
145271df19f5ef6e78beb5160801f81468184b75447eEric Anholt      if (qual->in)
145371df19f5ef6e78beb5160801f81468184b75447eEric Anholt	 var->shader_in = true;
145471df19f5ef6e78beb5160801f81468184b75447eEric Anholt      if (qual->out)
145571df19f5ef6e78beb5160801f81468184b75447eEric Anholt	 var->shader_out = true;
145671df19f5ef6e78beb5160801f81468184b75447eEric Anholt   }
145771df19f5ef6e78beb5160801f81468184b75447eEric Anholt
1458a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->flat)
1459a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->interpolation = ir_var_flat;
1460a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else if (qual->noperspective)
1461a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->interpolation = ir_var_noperspective;
1462a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else
1463a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->interpolation = ir_var_smooth;
14649d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick
14659d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick   if (var->type->is_array() && (state->language_version >= 120)) {
14669d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick      var->array_lvalue = true;
14679d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick   }
1468a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1469a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1470a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1471fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
14720044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_declarator_list::hir(exec_list *instructions,
147318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			 struct _mesa_glsl_parse_state *state)
1474a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1475a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   struct simple_node *ptr;
1476a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const struct glsl_type *decl_type;
1477a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const char *type_name = NULL;
14788558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt   ir_rvalue *result = NULL;
1479a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   YYLTYPE loc = this->get_location();
1480a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1481a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* The type specifier may contain a structure definition.  Process that
1482a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * before any of the variable declarations.
1483a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1484a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   (void) this->type->specifier->hir(instructions, state);
1485d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick
1486a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* FINISHME: Handle vertex shader "invariant" declarations that do not
148718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick    * FINISHME: include a type.  These re-declare built-in variables to be
1488a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * FINISHME: invariant.
1489a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1490a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
14912e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   decl_type = this->type->specifier->glsl_type(& type_name, state);
1492a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (is_empty_list(&this->declarations)) {
1493a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* There are only two valid cases where the declaration list can be
1494a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * empty.
1495a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       *
1496a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * 1. The declaration is setting the default precision of a built-in
1497cec65a6b76290ee4da91691bd3ef01c3fb8a0c37Ian Romanick       *    type (e.g., 'precision highp vec4;').
1498a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       *
1499a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * 2. Adding 'invariant' to an existing vertex shader output.
1500a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1501a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1502a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (this->type->qualifier.invariant) {
1503a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else if (decl_type != NULL) {
1504a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
1505a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    _mesa_glsl_error(& loc, state, "incomplete declaration");
1506a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1507a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1508a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1509a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   foreach (ptr, &this->declarations) {
1510a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      struct ast_declaration *const decl = (struct ast_declaration * )ptr;
151128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      const struct glsl_type *var_type;
1512a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      struct ir_variable *var;
1513a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1514a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* FINISHME: Emit a warning if a variable declaration shadows a
1515a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * FINISHME: declaration at a higher scope.
1516a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1517a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
15183f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      if ((decl_type == NULL) || decl_type->is_void()) {
15193f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 if (type_name != NULL) {
15203f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	    _mesa_glsl_error(& loc, state,
15213f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "invalid type `%s' in declaration of `%s'",
15223f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     type_name, decl->identifier);
15233f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 } else {
15243f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	    _mesa_glsl_error(& loc, state,
15253f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "invalid type in declaration of `%s'",
15263f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     decl->identifier);
15273f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 }
15283f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 continue;
15293f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      }
15303f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt
15313f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      if (decl->is_array) {
15323f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 var_type = process_array_type(decl_type, decl->array_size, state);
15333f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      } else {
15343f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 var_type = decl_type;
15353f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      }
15363f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt
15373f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      var = new ir_variable(var_type, decl->identifier);
15383f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt
15393f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
15403f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *
15413f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     "Global variables can only use the qualifiers const,
15423f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     attribute, uni form, or varying. Only one may be
15433f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     specified.
15442e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt       *
15452e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt       *     Local variables can only use the qualifier const."
1546a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       *
1547a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * This is relaxed in GLSL 1.30.
1548a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick       */
1549a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick      if (state->language_version < 120) {
1550a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	 if (this->type->qualifier.out) {
1551a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	    _mesa_glsl_error(& loc, state,
1552a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick			     "`out' qualifier in declaration of `%s' "
1553a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick			     "only valid for function parameters in GLSL 1.10.",
1554a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick			     decl->identifier);
1555a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	 }
1556a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 if (this->type->qualifier.in) {
15573359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	    _mesa_glsl_error(& loc, state,
1558a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick			     "`in' qualifier in declaration of `%s' "
1559a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick			     "only valid for function parameters in GLSL 1.10.",
1560a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick			     decl->identifier);
1561a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	 }
1562a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	 /* FINISHME: Test for other invalid qualifiers. */
1563a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick      }
1564a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick
1565a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick      apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
1566a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick				       & loc);
1567a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick
1568a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick      /* Attempt to add the variable to the symbol table.  If this fails, it
1569b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick       * means the variable has already been declared at this scope.  Arrays
1570894ea972a4defdaafeaa3a248c113b06c7ae0c7eEric Anholt       * fudge this rule a little bit.
1571b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick       *
1572b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick       * From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
1573b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick       *
1574b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick       *    "It is legal to declare an array without a size and then
1575b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick       *    later re-declare the same name as an array of the same
1576b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick       *    type and specify a size."
1577b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick       */
1578a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick      if (state->symbols->name_declared_this_scope(decl->identifier)) {
1579a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	 ir_variable *const earlier =
1580a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	    state->symbols->get_variable(decl->identifier);
1581a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick
1582a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	 if ((earlier != NULL)
1583a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	     && (earlier->type->array_size() == 0)
1584a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	     && var->type->is_array()
1585a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	     && (var->type->element_type() == earlier->type->element_type())) {
1586a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	    /* FINISHME: This doesn't match the qualifiers on the two
1587a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	     * FINISHME: declarations.  It's not 100% clear whether this is
1588a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	     * FINISHME: required or not.
1589a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	     */
1590a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1591b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt	    if (var->type->array_size() <= (int)earlier->max_array_access) {
1592b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt	       YYLTYPE loc = this->get_location();
1593b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt
1594b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt	       _mesa_glsl_error(& loc, state, "array size must be > %u due to "
1595b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt				"previous access",
1596b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt				earlier->max_array_access);
1597b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt	    }
1598b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt
1599b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt	    earlier->type = var->type;
1600b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt	    delete var;
1601b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt	    var = NULL;
1602b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt	 } else {
1603b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt	    YYLTYPE loc = this->get_location();
1604b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt
1605b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt	    _mesa_glsl_error(& loc, state, "`%s' redeclared",
16060044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick			     decl->identifier);
1607a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
1608e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick
1609b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 continue;
1610e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick      }
1611b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick
1612e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick      /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
1613e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick       *
1614e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick       *   "Identifiers starting with "gl_" are reserved for use by
1615e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick       *   OpenGL, and may not be declared in a shader as either a
1616b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick       *   variable or a function."
1617b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick       */
1618b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick      if (strncmp(decl->identifier, "gl_", 3) == 0) {
1619b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 /* FINISHME: This should only trigger if we're not redefining
1620b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	  * FINISHME: a builtin (to add a qualifier, for example).
1621e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	  */
1622e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 _mesa_glsl_error(& loc, state,
1623e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick			  "identifier `%s' uses reserved `gl_' prefix",
1624e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick			  decl->identifier);
1625e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick      }
1626e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick
1627b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick      instructions->push_tail(var);
1628b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick
1629b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick      if (state->current_function != NULL) {
1630e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	 const char *mode = NULL;
1631b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 const char *extra = "";
1632e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick
1633e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 /* There is no need to check for 'inout' here because the parser will
1634e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	  * only allow that in function parameter lists.
1635e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	  */
1636fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	 if (this->type->qualifier.attribute) {
1637fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    mode = "attribute";
1638fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	 } else if (this->type->qualifier.uniform) {
1639fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    mode = "uniform";
1640fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	 } else if (this->type->qualifier.varying) {
1641fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    mode = "varying";
1642fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	 } else if (this->type->qualifier.in) {
1643fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    mode = "in";
1644fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    extra = " or in function parameter list";
1645fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	 } else if (this->type->qualifier.out) {
16462d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	    mode = "out";
16472d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	    extra = " or in function parameter list";
16482d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	 }
16492d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick
16502d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	 if (mode) {
16512d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	    _mesa_glsl_error(& loc, state,
1652fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick			     "%s variable `%s' must be declared at "
1653fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick			     "global scope%s",
1654fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick			     mode, var->name, extra);
1655fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	 }
1656fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick      } else if (var->mode == ir_var_in) {
1657fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	 if (state->target == vertex_shader) {
1658fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    bool error_emitted = false;
1659fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
1660fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1661fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
1662fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    "Vertex shader inputs can only be float, floating-point
1663fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    vectors, matrices, signed and unsigned integers and integer
1664fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    vectors. Vertex shader inputs can also form arrays of these
1665fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    types, but not structures."
1666fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
1667fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
1668fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
1669fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    "Vertex shader inputs can only be float, floating-point
1670fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    vectors, matrices, signed and unsigned integers and integer
1671fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    vectors. They cannot be arrays or structures."
1672fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
1673fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
1674fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
1675fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    "The attribute qualifier can be used only with float,
1676fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    floating-point vectors, and matrices. Attribute variables
1677fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    cannot be declared as arrays or structures."
16782d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     */
1679fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    const glsl_type *check_type = var->type->is_array()
1680fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       ? var->type->fields.array : var->type;
1681fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
1682fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    switch (check_type->base_type) {
1683fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_FLOAT:
1684fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       break;
1685fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_UINT:
1686fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_INT:
1687fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       if (state->language_version > 120)
168866faec4895b7bb59a614087a200c05157191b4aeIan Romanick		  break;
168943de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	       /* FALLTHROUGH */
169043de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	    default:
169166faec4895b7bb59a614087a200c05157191b4aeIan Romanick	       _mesa_glsl_error(& loc, state,
169266faec4895b7bb59a614087a200c05157191b4aeIan Romanick				"vertex shader input / attribute cannot have "
169366faec4895b7bb59a614087a200c05157191b4aeIan Romanick				"type %s`%s'",
169466faec4895b7bb59a614087a200c05157191b4aeIan Romanick				var->type->is_array() ? "array of " : "",
169566faec4895b7bb59a614087a200c05157191b4aeIan Romanick				check_type->name);
169666faec4895b7bb59a614087a200c05157191b4aeIan Romanick	       error_emitted = true;
169766faec4895b7bb59a614087a200c05157191b4aeIan Romanick	    }
169866faec4895b7bb59a614087a200c05157191b4aeIan Romanick
169943de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	    if (!error_emitted && (state->language_version <= 130)
170043de17282017bdf187d6e646de3262cc64b7f46bIan Romanick		&& var->type->is_array()) {
170143de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	       _mesa_glsl_error(& loc, state,
170243de17282017bdf187d6e646de3262cc64b7f46bIan Romanick				"vertex shader input / attribute cannot have "
170343de17282017bdf187d6e646de3262cc64b7f46bIan Romanick				"array type");
170443de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	       error_emitted = true;
170543de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	    }
170643de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 }
170719360152f5bd8cff93359dbfe5a50a90b699c118Ian Romanick      }
170843de17282017bdf187d6e646de3262cc64b7f46bIan Romanick
170943de17282017bdf187d6e646de3262cc64b7f46bIan Romanick      if (decl->initializer != NULL) {
171043de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 YYLTYPE initializer_loc = decl->initializer->get_location();
1711ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick
171243de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
171343de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	  *
171466faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *    "All uniform variables are read-only and are initialized either
171566faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *    directly by an application via API commands, or indirectly by
171666faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *    OpenGL."
1717307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	  */
171819360152f5bd8cff93359dbfe5a50a90b699c118Ian Romanick	 if ((state->language_version <= 110)
1719307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	     && (var->mode == ir_var_uniform)) {
1720307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	    _mesa_glsl_error(& initializer_loc, state,
172166faec4895b7bb59a614087a200c05157191b4aeIan Romanick			     "cannot initialize uniforms in GLSL 1.10");
1722307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	 }
1723326c676236e6a3c90db63e4d0c893aa4f9c21876Eric Anholt
1724326c676236e6a3c90db63e4d0c893aa4f9c21876Eric Anholt	 if (var->type->is_sampler()) {
1725307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	    _mesa_glsl_error(& initializer_loc, state,
1726307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt			     "cannot initialize samplers");
1727307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	 }
1728307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt
1729326c676236e6a3c90db63e4d0c893aa4f9c21876Eric Anholt	 if ((var->mode == ir_var_in) && (state->current_function == NULL)) {
1730326c676236e6a3c90db63e4d0c893aa4f9c21876Eric Anholt	    _mesa_glsl_error(& initializer_loc, state,
1731326c676236e6a3c90db63e4d0c893aa4f9c21876Eric Anholt			     "cannot initialize %s shader input / %s",
1732307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt			     _mesa_glsl_shader_target_name(state->target),
1733307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt			     (state->target == vertex_shader)
173466faec4895b7bb59a614087a200c05157191b4aeIan Romanick			     ? "attribute" : "varying");
1735307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	 }
1736ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt
1737ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt	 ir_dereference *const lhs = new ir_dereference(var);
1738ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt	 ir_rvalue *rhs = decl->initializer->hir(instructions, state);
17398558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt
17408558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt	 /* Calculate the constant value if this is a const
1741ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt	  * declaration.
174266faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  */
174366faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 if (this->type->qualifier.constant) {
174417d86f4371da413176ba365ca26a58bac172d365Ian Romanick	    ir_constant *constant_value = rhs->constant_expression_value();
17450ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt	    if (!constant_value) {
17460ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt	       _mesa_glsl_error(& initializer_loc, state,
17470ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt				"initializer of const variable `%s' must be a "
17480ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt				"constant expression",
17490ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt				decl->identifier);
17500ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt	    } else {
17510ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt	       rhs = constant_value;
17520ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt	       var->constant_value = constant_value;
17530ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt	    }
17540ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt	 }
17550ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt
175617d86f4371da413176ba365ca26a58bac172d365Ian Romanick	 if (rhs && !rhs->type->is_error()) {
175717d86f4371da413176ba365ca26a58bac172d365Ian Romanick	    bool temp = var->read_only;
175817d86f4371da413176ba365ca26a58bac172d365Ian Romanick	    if (this->type->qualifier.constant)
175917d86f4371da413176ba365ca26a58bac172d365Ian Romanick	       var->read_only = false;
176017d86f4371da413176ba365ca26a58bac172d365Ian Romanick	    result = do_assignment(instructions, state, lhs, rhs,
176117d86f4371da413176ba365ca26a58bac172d365Ian Romanick				   this->get_location());
176217d86f4371da413176ba365ca26a58bac172d365Ian Romanick	    var->read_only = temp;
176317d86f4371da413176ba365ca26a58bac172d365Ian Romanick	 }
176417d86f4371da413176ba365ca26a58bac172d365Ian Romanick      }
176517d86f4371da413176ba365ca26a58bac172d365Ian Romanick
176617d86f4371da413176ba365ca26a58bac172d365Ian Romanick      /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
176717d86f4371da413176ba365ca26a58bac172d365Ian Romanick       *
1768a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       *     "It is an error to write to a const variable outside of
1769a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       *      its declaration, so they must be initialized when
17708558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt       *      declared."
17718558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt       */
17728558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt      if (this->type->qualifier.constant && decl->initializer == NULL) {
17738558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt	 _mesa_glsl_error(& loc, state,
17748558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt			  "const declaration of `%s' must be initialized");
17758558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt      }
17768558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt
17778558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt      /* Add the vairable to the symbol table after processing the initializer.
17788558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt       * This differs from most C-like languages, but it follows the GLSL
1779a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * specification.  From page 28 (page 34 of the PDF) of the GLSL 1.50
17808558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt       * spec:
1781a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       *
1782a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       *     "Within a declaration, the scope of a name starts immediately
1783a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       *     after the initializer if present or immediately after the name
1784fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke       *     being declared if not."
17850044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick       */
178618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      const bool added_variable =
1787a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 state->symbols->add_variable(decl->identifier, var);
1788a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      assert(added_variable);
1789a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
17902e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt
1791a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1792d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   /* Generally, variable declarations do not have r-values.  However,
1793a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * one is used for the declaration in
1794a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
1795a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * while (bool b = some_condition()) {
1796a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *   ...
1797a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * }
179818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick    *
1799a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * so we return the rvalue from the last seen declaration here.
1800a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1801a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return result;
180218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick}
1803a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1804a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
18050471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanickir_rvalue *
1806a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickast_parameter_declarator::hir(exec_list *instructions,
1807a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			      struct _mesa_glsl_parse_state *state)
1808068c80cfe0a280490353b6b007165d717c672eedEric Anholt{
1809068c80cfe0a280490353b6b007165d717c672eedEric Anholt   const struct glsl_type *type;
1810068c80cfe0a280490353b6b007165d717c672eedEric Anholt   const char *name = NULL;
1811068c80cfe0a280490353b6b007165d717c672eedEric Anholt   YYLTYPE loc = this->get_location();
1812068c80cfe0a280490353b6b007165d717c672eedEric Anholt
1813068c80cfe0a280490353b6b007165d717c672eedEric Anholt   type = this->type->specifier->glsl_type(& name, state);
1814068c80cfe0a280490353b6b007165d717c672eedEric Anholt
1815068c80cfe0a280490353b6b007165d717c672eedEric Anholt   if (type == NULL) {
1816068c80cfe0a280490353b6b007165d717c672eedEric Anholt      if (name != NULL) {
1817068c80cfe0a280490353b6b007165d717c672eedEric Anholt	 _mesa_glsl_error(& loc, state,
1818068c80cfe0a280490353b6b007165d717c672eedEric Anholt			  "invalid type `%s' in declaration of `%s'",
1819068c80cfe0a280490353b6b007165d717c672eedEric Anholt			  name, this->identifier);
1820cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      } else {
1821cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick	 _mesa_glsl_error(& loc, state,
1822cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick			  "invalid type in declaration of `%s'",
1823cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick			  this->identifier);
1824cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      }
1825cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
1826068c80cfe0a280490353b6b007165d717c672eedEric Anholt      type = glsl_type::error_type;
1827cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   }
1828068c80cfe0a280490353b6b007165d717c672eedEric Anholt
182945d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
183045d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick    *
183145d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick    *    "Functions that accept no input arguments need not use void in the
183245d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick    *    argument list because prototypes (or definitions) are required and
183345d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick    *    therefore there is no ambiguity when an empty argument list "( )" is
1834cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick    *    declared. The idiom "(void)" as a parameter list is provided for
183518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick    *    convenience."
1836a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
1837a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * Placing this check here prevents a void parameter being set up
1838a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * for a function, which avoids tripping up checks for main taking
1839a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * parameters and lookups of an unnamed symbol.
1840a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1841cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick   if (type->is_void()) {
1842cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick      if (this->identifier != NULL)
1843cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick	 _mesa_glsl_error(& loc, state,
18442e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt			  "named parameter cannot have type `void'");
1845cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick
1846cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick      is_void = true;
1847a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return NULL;
18480044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick   }
1849a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1850a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (formal_parameter && (this->identifier == NULL)) {
1851a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
1852a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return NULL;
1853a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1854a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1855a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   is_void = false;
185645d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   ir_variable *var = new ir_variable(type, this->identifier);
185745d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick
185845d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   /* FINISHME: Handle array declarations.  Note that this requires
185945d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick    * FINISHME: complete handling of constant expressions.
186045d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick    */
1861a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1862a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Apply any specified qualifiers to the parameter declaration.  Note that
1863cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick    * for function parameters the default mode is 'in'.
1864cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick    */
1865a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc);
1866a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (var->mode == ir_var_auto)
186745d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick      var->mode = ir_var_in;
186845d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick
1869068c80cfe0a280490353b6b007165d717c672eedEric Anholt   instructions->push_tail(var);
1870cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
1871cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   /* Parameter declarations do not have r-values.
1872cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick    */
1873cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   return NULL;
1874cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick}
1875cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
1876cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
1877cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanickvoid
1878cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanickast_parameter_declarator::parameters_to_hir(struct simple_node *ast_parameters,
1879cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick					    bool formal,
1880cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick					    exec_list *ir_parameters,
1881cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick					    _mesa_glsl_parse_state *state)
1882a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1883a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   struct simple_node *ptr;
1884a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   ast_parameter_declarator *void_param = NULL;
1885a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   unsigned count = 0;
1886a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
18870044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick   foreach (ptr, ast_parameters) {
1888a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ast_parameter_declarator *param = (ast_parameter_declarator *)ptr;
18890044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick      param->formal_parameter = formal;
18900044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick      param->hir(ir_parameters, state);
1891a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
189267a092ae09dbb2dd820aab5aa7742d3f884d6cd4Kenneth Graunke      if (param->is_void)
18935150c567a0bf082d93f25ba7e29d5573c9dffb8bEric Anholt	 void_param = param;
18945150c567a0bf082d93f25ba7e29d5573c9dffb8bEric Anholt
18955150c567a0bf082d93f25ba7e29d5573c9dffb8bEric Anholt      count++;
1896a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1897a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1898a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if ((void_param != NULL) && (count > 1)) {
18995150c567a0bf082d93f25ba7e29d5573c9dffb8bEric Anholt      YYLTYPE loc = void_param->get_location();
19005150c567a0bf082d93f25ba7e29d5573c9dffb8bEric Anholt
1901a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      _mesa_glsl_error(& loc, state,
19020044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick		       "`void' parameter must be only parameter");
19030044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick   }
1904a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1905a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
190667a092ae09dbb2dd820aab5aa7742d3f884d6cd4Kenneth Graunke
190767a092ae09dbb2dd820aab5aa7742d3f884d6cd4Kenneth Graunkeir_rvalue *
190867a092ae09dbb2dd820aab5aa7742d3f884d6cd4Kenneth Graunkeast_function::hir(exec_list *instructions,
190967a092ae09dbb2dd820aab5aa7742d3f884d6cd4Kenneth Graunke		  struct _mesa_glsl_parse_state *state)
191067a092ae09dbb2dd820aab5aa7742d3f884d6cd4Kenneth Graunke{
191167a092ae09dbb2dd820aab5aa7742d3f884d6cd4Kenneth Graunke   ir_function *f = NULL;
1912a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   ir_function_signature *sig = NULL;
1913a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   exec_list hir_parameters;
1914a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1915a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1916fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke   /* Convert the list of function parameters to HIR now so that they can be
191792318a947958892497722772b03c643ebc943294Ian Romanick    * used below to compare this function's signature with previously seen
191892318a947958892497722772b03c643ebc943294Ian Romanick    * signatures for functions with the same name.
1919a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
192018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   ast_parameter_declarator::parameters_to_hir(& this->parameters,
192192318a947958892497722772b03c643ebc943294Ian Romanick					       is_definition,
192292318a947958892497722772b03c643ebc943294Ian Romanick					       & hir_parameters, state);
1923a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1924a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const char *return_type_name;
1925a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const glsl_type *return_type =
1926a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      this->return_type->specifier->glsl_type(& return_type_name, state);
1927a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1928a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   assert(return_type != NULL);
192945d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick
193045d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   /* Verify that this function's signature either doesn't match a previously
193145d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick    * seen signature for a function with the same name, or, if a match is found,
1932a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * that the previously seen signature does not have an associated definition.
1933e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick    */
1934e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick   const char *const name = identifier;
193592318a947958892497722772b03c643ebc943294Ian Romanick   f = state->symbols->get_function(name);
1936e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick   if (f != NULL) {
1937e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick      ir_function_signature *sig = f->exact_matching_signature(&hir_parameters);
1938e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick      if (sig != NULL) {
1939a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 const char *badvar = sig->qualifiers_match(&hir_parameters);
1940a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 if (badvar != NULL) {
1941a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    YYLTYPE loc = this->get_location();
1942a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
194392318a947958892497722772b03c643ebc943294Ian Romanick	    _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
19443359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick			     "qualifiers don't match prototype", name, badvar);
1945a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
194695cd6cc195f4652378d7ecf614c6e1c568311a04Ian Romanick
194792318a947958892497722772b03c643ebc943294Ian Romanick	 if (sig->return_type != return_type) {
1948a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    YYLTYPE loc = this->get_location();
1949a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1950a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
1951a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     "match prototype", name);
1952a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
1953a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
195492318a947958892497722772b03c643ebc943294Ian Romanick	 if (is_definition && sig->is_defined) {
19551e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt	    YYLTYPE loc = this->get_location();
19561e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt
19571e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt	    _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
19581e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt	    sig = NULL;
19591e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt	 }
19601e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt      }
19611e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt   } else if (state->symbols->name_declared_this_scope(name)) {
19621e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt      /* This function name shadows a non-function use of the same name.
19631e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt       */
19641e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt      YYLTYPE loc = this->get_location();
19651e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt
19661e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt      _mesa_glsl_error(& loc, state, "function name `%s' conflicts with "
19671e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt		       "non-function", name);
19681e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt      sig = NULL;
19691e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt   } else {
19701e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt      f = new ir_function(name);
19711e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt      state->symbols->add_function(f->name, f);
19721e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt
19731e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt      /* Emit the new function header */
19741e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt      instructions->push_tail(f);
19751e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt   }
19761e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt
19771e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt   /* Verify the return type of main() */
197860be7626b829af7e1d07330b9a88468924ba350eEric Anholt   if (strcmp(name, "main") == 0) {
197960be7626b829af7e1d07330b9a88468924ba350eEric Anholt      if (! return_type->is_void()) {
198060be7626b829af7e1d07330b9a88468924ba350eEric Anholt	 YYLTYPE loc = this->get_location();
198160be7626b829af7e1d07330b9a88468924ba350eEric Anholt
198260be7626b829af7e1d07330b9a88468924ba350eEric Anholt	 _mesa_glsl_error(& loc, state, "main() must return void");
198360be7626b829af7e1d07330b9a88468924ba350eEric Anholt      }
198460be7626b829af7e1d07330b9a88468924ba350eEric Anholt
198560be7626b829af7e1d07330b9a88468924ba350eEric Anholt      if (!hir_parameters.is_empty()) {
1986a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 YYLTYPE loc = this->get_location();
19879fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke
198818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick	 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
1989a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
19903359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick   }
199192318a947958892497722772b03c643ebc943294Ian Romanick
1992a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Finish storing the information about this new function in its signature.
1993a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1994a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (sig == NULL) {
1995a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      sig = new ir_function_signature(return_type);
199692318a947958892497722772b03c643ebc943294Ian Romanick      f->add_signature(sig);
1997a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1998a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
19993359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick   sig->replace_parameters(&hir_parameters);
20003359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick   signature = sig;
20013359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick
20023359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick   /* Function declarations (prototypes) do not have r-values.
20033359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick    */
20043359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick   return NULL;
20053359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick}
200692318a947958892497722772b03c643ebc943294Ian Romanick
2007a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2008882dad75408fc4071a9dd700309f9e54f6ad2650Ian Romanickir_rvalue *
20098bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanickast_function_definition::hir(exec_list *instructions,
20109fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke			     struct _mesa_glsl_parse_state *state)
20119fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke{
20129fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke   prototype->is_definition = true;
2013a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   prototype->hir(instructions, state);
2014a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2015ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   ir_function_signature *signature = prototype->signature;
2016ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt
201725711a85c22bed305c9b52b89feb9c600d1892dfIan Romanick   assert(state->current_function == NULL);
2018ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   state->current_function = signature;
2019ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt
2020ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   /* Duplicate parameters declared in the prototype as concrete variables.
2021ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt    * Add these to the symbol table.
2022174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt    */
202392318a947958892497722772b03c643ebc943294Ian Romanick   state->symbols->push_scope();
2024174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt   foreach_iter(exec_list_iterator, iter, signature->parameters) {
2025174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt      ir_variable *const var = ((ir_instruction *) iter.get())->as_variable();
2026174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt
2027174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt      assert(var != NULL);
2028ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt
2029a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* The only way a parameter would "exist" is if two parameters have
2030a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * the same name.
2031a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
203292318a947958892497722772b03c643ebc943294Ian Romanick      if (state->symbols->name_declared_this_scope(var->name)) {
203392318a947958892497722772b03c643ebc943294Ian Romanick	 YYLTYPE loc = this->get_location();
203492318a947958892497722772b03c643ebc943294Ian Romanick
203592318a947958892497722772b03c643ebc943294Ian Romanick	 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
2036a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
2037a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 state->symbols->add_variable(var->name, var);
2038a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
2039a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
204092318a947958892497722772b03c643ebc943294Ian Romanick
204144e1dfa2df4de3e2de963f0505cdadade6fe8180Kenneth Graunke   /* Convert the body of the function to HIR. */
2042a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   this->body->hir(&signature->body, state);
20430044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick   signature->is_defined = true;
20440044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick
2045a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   state->symbols->pop_scope();
2046a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2047a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   assert(state->current_function == signature);
204892318a947958892497722772b03c643ebc943294Ian Romanick   state->current_function = NULL;
204992318a947958892497722772b03c643ebc943294Ian Romanick
205092318a947958892497722772b03c643ebc943294Ian Romanick   /* Function definitions do not have r-values.
205192318a947958892497722772b03c643ebc943294Ian Romanick    */
205292318a947958892497722772b03c643ebc943294Ian Romanick   return NULL;
205392318a947958892497722772b03c643ebc943294Ian Romanick}
205492318a947958892497722772b03c643ebc943294Ian Romanick
205592318a947958892497722772b03c643ebc943294Ian Romanick
205692318a947958892497722772b03c643ebc943294Ian Romanickir_rvalue *
205792318a947958892497722772b03c643ebc943294Ian Romanickast_jump_statement::hir(exec_list *instructions,
205892318a947958892497722772b03c643ebc943294Ian Romanick			struct _mesa_glsl_parse_state *state)
205992318a947958892497722772b03c643ebc943294Ian Romanick{
206092318a947958892497722772b03c643ebc943294Ian Romanick
206192318a947958892497722772b03c643ebc943294Ian Romanick   switch (mode) {
206292318a947958892497722772b03c643ebc943294Ian Romanick   case ast_return: {
2063e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick      ir_return *inst;
206492318a947958892497722772b03c643ebc943294Ian Romanick      assert(state->current_function);
2065a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
206641ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick      if (opt_return_value) {
206741ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick	 if (state->current_function->return_type->base_type ==
206841ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick	     GLSL_TYPE_VOID) {
2069e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick	    YYLTYPE loc = this->get_location();
2070e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick
2071a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    _mesa_glsl_error(& loc, state,
20728bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick			     "`return` with a value, in function `%s' "
2073e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick			     "returning void",
2074fbc7c0b8f2e161bce1c048c63d2d5cfcdeb096f1Eric Anholt			     state->current_function->function_name());
2075e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick	 }
2076fbc7c0b8f2e161bce1c048c63d2d5cfcdeb096f1Eric Anholt
2077a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 ir_expression *const ret = (ir_expression *)
20783359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	    opt_return_value->hir(instructions, state);
20793359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 assert(ret != NULL);
20803359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick
20813359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 /* FINISHME: Make sure the type of the return value matches the return
20823359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	  * FINISHME: type of the enclosing function.
20833359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	  */
20843359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick
20853359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 inst = new ir_return(ret);
20863359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      } else {
20873359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 if (state->current_function->return_type->base_type !=
2088a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	     GLSL_TYPE_VOID) {
2089a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    YYLTYPE loc = this->get_location();
20909fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke
2091894ea972a4defdaafeaa3a248c113b06c7ae0c7eEric Anholt	    _mesa_glsl_error(& loc, state,
20929fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke			     "`return' with no value, in function %s returning "
2093a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     "non-void",
20948bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick			     state->current_function->function_name());
2095a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
209641ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick	 inst = new ir_return;
209741ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick      }
2098a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2099a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      instructions->push_tail(inst);
2100a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
2101a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2102a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
210316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   case ast_discard:
210416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      /* FINISHME: discard support */
2105fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke      if (state->target != fragment_shader) {
210616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 YYLTYPE loc = this->get_location();
210716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
210816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 _mesa_glsl_error(& loc, state,
210916a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick			  "`discard' may only appear in a fragment shader");
2110c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick      }
2111c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick      break;
211216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
2113aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt   case ast_break:
211416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   case ast_continue:
211516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      /* FINISHME: Handle switch-statements.  They cannot contain 'continue',
2116ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt       * FINISHME: and they use a different IR instruction for 'break'.
2117ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt       */
2118ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt      /* FINISHME: Correctly handle the nesting.  If a switch-statement is
2119ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt       * FINISHME: inside a loop, a 'continue' is valid and will bind to the
2120ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt       * FINISHME: loop.
2121ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt       */
2122ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt      if (state->loop_or_switch_nesting == NULL) {
2123f96c52ba2e290e3ba5f14cd7f87ba5b4382a1785Kenneth Graunke	 YYLTYPE loc = this->get_location();
2124ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt
212516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 _mesa_glsl_error(& loc, state,
212616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick			  "`%s' may only appear in a loop",
212716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick			  (mode == ast_break) ? "break" : "continue");
212816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      } else {
212916a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 ir_loop *const loop = state->loop_or_switch_nesting->as_loop();
213016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
213116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 if (loop != NULL) {
213216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	    ir_loop_jump *const jump =
213316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	       new ir_loop_jump(loop,
213416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick				(mode == ast_break)
213516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick				? ir_loop_jump::jump_break
2136aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt				: ir_loop_jump::jump_continue);
2137aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	    instructions->push_tail(jump);
2138aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	 }
2139aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt      }
2140aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt
2141aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt      break;
2142aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt   }
2143f96c52ba2e290e3ba5f14cd7f87ba5b4382a1785Kenneth Graunke
2144aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt   /* Jump instructions do not have r-values.
214516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick    */
214616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   return NULL;
214716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick}
214816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
2149c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick
215016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanickir_rvalue *
215116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanickast_selection_statement::hir(exec_list *instructions,
2152c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick			     struct _mesa_glsl_parse_state *state)
2153b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt{
2154b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt   ir_rvalue *const condition = this->condition->hir(instructions, state);
2155b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt
2156b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt   /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
2157b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt    *
2158b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt    *    "Any expression whose type evaluates to a Boolean can be used as the
2159b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt    *    conditional expression bool-expression. Vector types are not accepted
2160c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick    *    as the expression to if."
2161c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick    *
2162c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick    * The checks are separated so that higher quality diagnostics can be
2163c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick    * generated for cases where both rules are violated.
21644cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick    */
21654cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick   if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
21664cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      YYLTYPE loc = this->condition->get_location();
21674cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
21684cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
21694cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick		       "boolean");
21704cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick   }
21714cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
21724cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick   ir_if *const stmt = new ir_if(condition);
21734cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
21744cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick   if (then_statement != NULL) {
21754cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      ast_node *node = (ast_node *) then_statement;
21764cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      do {
21774cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 node->hir(& stmt->then_instructions, state);
21784cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 node = (ast_node *) node->next;
21794cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      } while (node != then_statement);
21804cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick   }
21814cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
21824cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick   if (else_statement != NULL) {
21834cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      ast_node *node = (ast_node *) else_statement;
21844cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      do {
21854cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 node->hir(& stmt->else_instructions, state);
21864cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 node = (ast_node *) node->next;
21874cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      } while (node != else_statement);
21884cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick   }
21894cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
2190c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   instructions->push_tail(stmt);
2191b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt
2192b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt   /* if-statements do not have r-values.
219316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick    */
219416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   return NULL;
219516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick}
219616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
21973c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
21983c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanickvoid
21993c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanickast_iteration_statement::condition_to_hir(ir_loop *stmt,
22003c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick					  struct _mesa_glsl_parse_state *state)
22013c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick{
22023c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   if (condition != NULL) {
22033c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      ir_rvalue *const cond =
22043c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick	 condition->hir(& stmt->body_instructions, state);
22053c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
22063c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      if ((cond == NULL)
22073c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick	  || !cond->type->is_boolean() || !cond->type->is_scalar()) {
22083c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick	 YYLTYPE loc = condition->get_location();
22093c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
22103c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick	 _mesa_glsl_error(& loc, state,
22113c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick			  "loop condition must be scalar boolean");
22123c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      } else {
22133c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick	 /* As the first code in the loop body, generate a block that looks
22143c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick	  * like 'if (!condition) break;' as the loop termination condition.
22153c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick	  */
22163c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick	 ir_rvalue *const not_cond =
22173c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick	    new ir_expression(ir_unop_logic_not, glsl_type::bool_type, cond,
22183c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick			      NULL);
22193c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
22203c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick	 ir_if *const if_stmt = new ir_if(not_cond);
22213c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
22223c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick	 ir_jump *const break_stmt =
22233c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick	    new ir_loop_jump(stmt, ir_loop_jump::jump_break);
22243c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
22253c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick	 if_stmt->then_instructions.push_tail(break_stmt);
22263c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick	 stmt->body_instructions.push_tail(if_stmt);
22273c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      }
22283c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   }
22293c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick}
22303c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
22313c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
22323c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanickir_rvalue *
22333c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanickast_iteration_statement::hir(exec_list *instructions,
22343c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick			     struct _mesa_glsl_parse_state *state)
22353c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick{
22363c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   /* For-loops and while-loops start a new scope, but do-while loops do not.
22373c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    */
22383c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   if (mode != ast_do_while)
22393c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      state->symbols->push_scope();
22403c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
22413c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   if (init_statement != NULL)
22423c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      init_statement->hir(instructions, state);
22433c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
22443c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   ir_loop *const stmt = new ir_loop();
22459e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   instructions->push_tail(stmt);
22469e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
22478c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   /* Track the current loop and / or switch-statement nesting.
22488c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick    */
22498c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   ir_instruction *const nesting = state->loop_or_switch_nesting;
22509e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   state->loop_or_switch_nesting = stmt;
22519e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
22529e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   if (mode != ast_do_while)
22539e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      condition_to_hir(stmt, state);
22549e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
22559e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   if (body != NULL) {
22569e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      ast_node *node = (ast_node *) body;
22579e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      do {
22589e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 node->hir(& stmt->body_instructions, state);
22599e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 node = (ast_node *) node->next;
22609e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      } while (node != body);
22619e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   }
22629e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
22639e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   if (rest_expression != NULL)
22649e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      rest_expression->hir(& stmt->body_instructions, state);
22659e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
22669e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   if (mode == ast_do_while)
22679e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      condition_to_hir(stmt, state);
22689e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
22699e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   if (mode != ast_do_while)
22709e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      state->symbols->pop_scope();
22719e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
22729e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   /* Restore previous nesting before returning.
22739e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick    */
22749e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   state->loop_or_switch_nesting = nesting;
22759e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
22769e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   /* Loops do not have r-values.
22779e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick    */
22788c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   return NULL;
22798c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick}
22808c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
22818c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
22828c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickir_rvalue *
22838c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickast_type_specifier::hir(exec_list *instructions,
22848c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick			  struct _mesa_glsl_parse_state *state)
2285484606610e36ec7598f7733e9787d888b6a63d64Ian Romanick{
22868c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   if (this->structure != NULL)
2287484606610e36ec7598f7733e9787d888b6a63d64Ian Romanick      return this->structure->hir(instructions, state);
22888c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
22898c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   return NULL;
22908c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick}
22918c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
22928c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
22938c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickir_rvalue *
22948c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickast_struct_specifier::hir(exec_list *instructions,
22958c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick			  struct _mesa_glsl_parse_state *state)
22968c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick{
22978c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   simple_node *ptr;
22988c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   unsigned decl_count = 0;
22998c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
23008c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   /* Make an initial pass over the list of structure fields to determine how
23018c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick    * many there are.  Each element in this list is an ast_declarator_list.
23028c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick    * This means that we actually need to count the number of elements in the
23039e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick    * 'declarations' list in each of the elements.
23049e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick    */
23059e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   foreach (ptr, & this->declarations) {
23069e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      ast_declarator_list *decl_list = (ast_declarator_list *) ptr;
23079e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      simple_node *decl_ptr;
23089e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
23099e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      foreach (decl_ptr, & decl_list->declarations) {
23109e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 decl_count++;
23119e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      }
23129e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   }
23139e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
23149e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
23158c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   /* Allocate storage for the structure fields and process the field
23168c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick    * declarations.  As the declarations are processed, try to also convert
23178c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick    * the types to HIR.  This ensures that structure definitions embedded in
2318484606610e36ec7598f7733e9787d888b6a63d64Ian Romanick    * other structure definitions are processed.
23199e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick    */
23209e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   glsl_struct_field *const fields = (glsl_struct_field *)
2321e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick      malloc(sizeof(*fields) * decl_count);
2322e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick
2323e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick   unsigned i = 0;
2324e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick   foreach (ptr, & this->declarations) {
23259e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      ast_declarator_list *decl_list = (ast_declarator_list *) ptr;
23269e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      simple_node *decl_ptr;
23279e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      const char *type_name;
23289e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
2329      decl_list->type->specifier->hir(instructions, state);
2330
2331      const glsl_type *decl_type =
2332	 decl_list->type->specifier->glsl_type(& type_name, state);
2333
2334      foreach (decl_ptr, & decl_list->declarations) {
2335	 ast_declaration *const decl = (ast_declaration *) decl_ptr;
2336	 const struct glsl_type *const field_type =
2337	    (decl->is_array)
2338	    ? process_array_type(decl_type, decl->array_size, state)
2339	    : decl_type;
2340
2341	 fields[i].type = (field_type != NULL)
2342	    ? field_type : glsl_type::error_type;
2343	 fields[i].name = decl->identifier;
2344	 i++;
2345      }
2346   }
2347
2348   assert(i == decl_count);
2349
2350   const char *name;
2351   if (this->name == NULL) {
2352      static unsigned anon_count = 1;
2353      char buf[32];
2354
2355      snprintf(buf, sizeof(buf), "#anon_struct_%04x", anon_count);
2356      anon_count++;
2357
2358      name = strdup(buf);
2359   } else {
2360      name = this->name;
2361   }
2362
2363   glsl_type *t = new glsl_type(fields, decl_count, name);
2364
2365   YYLTYPE loc = this->get_location();
2366   if (!state->symbols->add_type(name, t)) {
2367      _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
2368   } else {
2369      /* This logic is a bit tricky.  It is an error to declare a structure at
2370       * global scope if there is also a function with the same name.
2371       */
2372      if ((state->current_function == NULL)
2373	  && (state->symbols->get_function(name) != NULL)) {
2374	 _mesa_glsl_error(& loc, state, "name `%s' previously defined", name);
2375      } else {
2376	 t->generate_constructor(state->symbols);
2377      }
2378   }
2379
2380   /* Structure type definitions do not have r-values.
2381    */
2382   return NULL;
2383}
2384