ast_to_hir.cpp revision 90b7825b0e92375dbe721d2dca1a4a3f1093f4ab
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:
1230104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      assert(!"FINISHME: Convert bool to float.");
1240104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   default:
1250104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      assert(0);
1260104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   }
1270104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1280104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   return true;
1290104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick}
1300104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1310104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
132a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
133bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickarithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
134a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick		       bool multiply,
135a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
136a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
137bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   const glsl_type *const type_a = value_a->type;
138bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   const glsl_type *const type_b = value_b->type;
1390104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
140a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
141a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
142a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The arithmetic binary operators add (+), subtract (-),
143a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    multiply (*), and divide (/) operate on integer and
144a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    floating-point scalars, vectors, and matrices."
145a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
14660b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   if (!type_a->is_numeric() || !type_b->is_numeric()) {
147a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state,
148a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "Operands to arithmetic operators must be numeric");
1490471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
150a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
151a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
152a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
153a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "If one operand is floating-point based and the other is
154a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    not, then the conversions from Section 4.1.10 "Implicit
155a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    Conversions" are applied to the non-floating-point-based operand."
156a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1570104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   if (!apply_implicit_conversion(type_a, value_b, state)
1580104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick       && !apply_implicit_conversion(type_b, value_a, state)) {
159a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state,
160a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "Could not implicitly convert operands to "
161a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "arithmetic operator");
1620104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return glsl_type::error_type;
163a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
164a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
165a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "If the operands are integer types, they must both be signed or
166a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    both be unsigned."
167a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
168a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * From this rule and the preceeding conversion it can be inferred that
169a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
17060b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick    * The is_numeric check above already filtered out the case where either
17160b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick    * type is not one of these, so now the base types need only be tested for
17260b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick    * equality.
173a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
174a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (type_a->base_type != type_b->base_type) {
175a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state,
176a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "base type mismatch for arithmetic operator");
1770471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
178a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
179a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
180a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "All arithmetic binary operators result in the same fundamental type
181a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    (signed integer, unsigned integer, or floating-point) as the
182a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    operands they operate on, after operand type conversion. After
183a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    conversion, the following cases are valid
184a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
185a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    * The two operands are scalars. In this case the operation is
186a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      applied, resulting in a scalar."
187a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
188cb36f8aaeeb09660843316270a781948f773d90bIan Romanick   if (type_a->is_scalar() && type_b->is_scalar())
189a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return type_a;
190a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
191a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*   "* One operand is a scalar, and the other is a vector or matrix.
192a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      In this case, the scalar operation is applied independently to each
193a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      component of the vector or matrix, resulting in the same size
194a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      vector or matrix."
195a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
196cb36f8aaeeb09660843316270a781948f773d90bIan Romanick   if (type_a->is_scalar()) {
197cb36f8aaeeb09660843316270a781948f773d90bIan Romanick      if (!type_b->is_scalar())
198a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 return type_b;
199cb36f8aaeeb09660843316270a781948f773d90bIan Romanick   } else if (type_b->is_scalar()) {
200a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return type_a;
201a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
202a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
203a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* All of the combinations of <scalar, scalar>, <vector, scalar>,
204a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
205a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * handled.
206a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
20760b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   assert(!type_a->is_scalar());
20860b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   assert(!type_b->is_scalar());
209a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
210a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*   "* The two operands are vectors of the same size. In this case, the
211a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operation is done component-wise resulting in the same size
212a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      vector."
213a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
214a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick   if (type_a->is_vector() && type_b->is_vector()) {
215a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      if (type_a == type_b) {
216a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 return type_a;
217a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      } else {
218a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 _mesa_glsl_error(loc, state,
219a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt			  "vector size mismatch for arithmetic operator");
220a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 return glsl_type::error_type;
221a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      }
222a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
223a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
224a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* All of the combinations of <scalar, scalar>, <vector, scalar>,
225a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
226a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * <vector, vector> have been handled.  At least one of the operands must
227a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * be matrix.  Further, since there are no integer matrix types, the base
228a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * type of both operands must be float.
229a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
23060b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   assert(type_a->is_matrix() || type_b->is_matrix());
231a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   assert(type_a->base_type == GLSL_TYPE_FLOAT);
232a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   assert(type_b->base_type == GLSL_TYPE_FLOAT);
233a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
234a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*   "* The operator is add (+), subtract (-), or divide (/), and the
235a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operands are matrices with the same number of rows and the same
236a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      number of columns. In this case, the operation is done component-
237a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      wise resulting in the same size matrix."
238a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    * The operator is multiply (*), where both operands are matrices or
239a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      one operand is a vector and the other a matrix. A right vector
240a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operand is treated as a column vector and a left vector operand as a
241a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      row vector. In all these cases, it is required that the number of
242a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      columns of the left operand is equal to the number of rows of the
243a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      right operand. Then, the multiply (*) operation does a linear
244a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      algebraic multiply, yielding an object that has the same number of
245a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      rows as the left operand and the same number of columns as the right
246a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operand. Section 5.10 "Vector and Matrix Operations" explains in
247a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      more detail how vectors and matrices are operated on."
248a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
249a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (! multiply) {
250a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      if (type_a == type_b)
251a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 return type_a;
252a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
253fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick      if (type_a->is_matrix() && type_b->is_matrix()) {
254c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 /* Matrix multiply.  The columns of A must match the rows of B.  Given
255c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * the other previously tested constraints, this means the vector type
256c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * of a row from A must be the same as the vector type of a column from
257c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * B.
258c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  */
259c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 if (type_a->row_type() == type_b->column_type()) {
260c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	    /* The resulting matrix has the number of columns of matrix B and
261c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     * the number of rows of matrix A.  We get the row count of A by
262c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     * looking at the size of a vector that makes up a column.  The
263c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     * transpose (size of a row) is done for B.
264c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     */
265a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	    const glsl_type *const type =
266c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	       glsl_type::get_instance(type_a->base_type,
267c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick				       type_a->column_type()->vector_elements,
268c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick				       type_b->row_type()->vector_elements);
269a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	    assert(type != glsl_type::error_type);
270a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt
271a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	    return type;
272a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
273fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick      } else if (type_a->is_matrix()) {
274a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 /* A is a matrix and B is a column vector.  Columns of A must match
275c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * rows of B.  Given the other previously tested constraints, this
276c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * means the vector type of a row from A must be the same as the
277c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * vector the type of B.
278a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  */
279c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 if (type_a->row_type() == type_b)
280a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    return type_b;
281a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
282fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick	 assert(type_b->is_matrix());
283a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
284c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 /* A is a row vector and B is a matrix.  Columns of A must match rows
285c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * of B.  Given the other previously tested constraints, this means
286c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * the type of A must be the same as the vector type of a column from
287c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * B.
288a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  */
289c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 if (type_a == type_b->column_type())
290a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    return type_a;
291a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
292a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt
293a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication");
294a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      return glsl_type::error_type;
295a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
296a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
297a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
298a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "All other cases are illegal."
299a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
300a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt   _mesa_glsl_error(loc, state, "type mismatch");
3010471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::error_type;
302a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
303a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
304a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
305a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
30665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholtunary_arithmetic_result_type(const struct glsl_type *type,
30765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt			     struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
308a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
309a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 57:
310a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
311a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The arithmetic unary operators negate (-), post- and pre-increment
312a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     and decrement (-- and ++) operate on integer or floating-point
313a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     values (including vectors and matrices). All unary operators work
314a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     component-wise on their operands. These result with the same type
315a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     they operated on."
316a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
31765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   if (!type->is_numeric()) {
31865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state,
31965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "Operands to arithmetic operators must be numeric");
3200471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
32165e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   }
322a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
323a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return type;
324a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
325a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
326a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
327a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
328a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickmodulus_result_type(const struct glsl_type *type_a,
32965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		    const struct glsl_type *type_b,
33065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		    struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
331a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
332a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
333a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The operator modulus (%) operates on signed or unsigned integers or
334a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    integer vectors. The operand types must both be signed or both be
335a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    unsigned."
336a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
33740176e249f72b6090204611873b19aed3da67c71Ian Romanick   if (!type_a->is_integer() || !type_b->is_integer()
338a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       || (type_a->base_type != type_b->base_type)) {
33965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state, "type mismatch");
3400471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
341a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
342a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
343a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The operands cannot be vectors of differing size. If one operand is
344a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    a scalar and the other vector, then the scalar is applied component-
345a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    wise to the vector, resulting in the same type as the vector. If both
346a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    are vectors of the same size, the result is computed component-wise."
347a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
348a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick   if (type_a->is_vector()) {
349a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick      if (!type_b->is_vector()
350a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  || (type_a->vector_elements == type_b->vector_elements))
351a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 return type_a;
352a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else
353a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return type_b;
354a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
355a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The operator modulus (%) is not defined for any other data types
356a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    (non-integer types)."
357a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
35865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   _mesa_glsl_error(loc, state, "type mismatch");
3590471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::error_type;
360a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
361a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
362a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
363a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
364bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickrelational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
36565e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
366a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
367bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   const glsl_type *const type_a = value_a->type;
368bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   const glsl_type *const type_b = value_b->type;
3690150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick
370a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
371a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The relational operators greater than (>), less than (<), greater
372a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    than or equal (>=), and less than or equal (<=) operate only on
373a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    scalar integer and scalar floating-point expressions."
374a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
375a6d653dcbbee3158f9ea7b284bdeb1a8432f0fcbIan Romanick   if (!type_a->is_numeric()
376a6d653dcbbee3158f9ea7b284bdeb1a8432f0fcbIan Romanick       || !type_b->is_numeric()
377cb36f8aaeeb09660843316270a781948f773d90bIan Romanick       || !type_a->is_scalar()
37865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt       || !type_b->is_scalar()) {
37965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state,
38065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "Operands to relational operators must be scalar and "
38165e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "numeric");
3820471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
38365e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   }
384a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
385a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "Either the operands' types must match, or the conversions from
386a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    Section 4.1.10 "Implicit Conversions" will be applied to the integer
387a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    operand, after which the types must match."
388a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
3890150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick   if (!apply_implicit_conversion(type_a, value_b, state)
3900150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick       && !apply_implicit_conversion(type_b, value_a, state)) {
39165e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state,
39265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "Could not implicitly convert operands to "
39365e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "relational operator");
3940150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick      return glsl_type::error_type;
395a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
396a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
39765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   if (type_a->base_type != type_b->base_type) {
39865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state, "base type mismatch");
3990471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
40065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   }
401a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
402a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The result is scalar Boolean."
403a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
4040471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::bool_type;
405a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
406a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
407a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
4080bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick/**
4090bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Validates that a value can be assigned to a location with a specified type
4100bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
4110bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Validates that \c rhs can be assigned to some location.  If the types are
4120bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * not an exact match but an automatic conversion is possible, \c rhs will be
4130bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * converted.
4140bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
4150bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \return
4160bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
4170bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Otherwise the actual RHS to be assigned will be returned.  This may be
4180bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \c rhs, or it may be \c rhs after some type conversion.
4190bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
4200bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \note
4210bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * In addition to being used for assignments, this function is used to
4220bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * type-check return values.
4230bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick */
424fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
425fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkevalidate_assignment(const glsl_type *lhs_type, ir_rvalue *rhs)
4260bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick{
4270bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   const glsl_type *const rhs_type = rhs->type;
4280bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
4290bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   /* If there is already some error in the RHS, just return it.  Anything
4300bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    * else will lead to an avalanche of error message back to the user.
4310bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    */
4320bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   if (rhs_type->is_error())
4330bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick      return rhs;
4340bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
4350bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   /* FINISHME: For GLSL 1.10, check that the types are not arrays. */
4360bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
4370bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   /* If the types are identical, the assignment can trivially proceed.
4380bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    */
4390bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   if (rhs_type == lhs_type)
4400bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick      return rhs;
4410bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
4420bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   /* FINISHME: Check for and apply automatic conversions. */
4430bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   return NULL;
4440bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick}
4450bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
44610a685218610e737e23d2d8a243ed6ff6613becdEric Anholtir_rvalue *
44710a685218610e737e23d2d8a243ed6ff6613becdEric Anholtdo_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
44810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	      ir_rvalue *lhs, ir_rvalue *rhs,
44910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	      YYLTYPE lhs_loc)
45010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt{
45110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
45210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
45310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   if (!error_emitted) {
45410a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
45510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      if (!lhs->is_lvalue()) {
45610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	 _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
45710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	 error_emitted = true;
45810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      }
45910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   }
46010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
46110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   ir_rvalue *new_rhs = validate_assignment(lhs->type, rhs);
46210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   if (new_rhs == NULL) {
46310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      _mesa_glsl_error(& lhs_loc, state, "type mismatch");
46410a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   } else {
46510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      rhs = new_rhs;
46610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   }
46710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
46810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   ir_instruction *tmp = new ir_assignment(lhs, rhs, NULL);
46910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   instructions->push_tail(tmp);
47010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
47110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   return rhs;
47210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt}
4730bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
4745185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick
4755185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick/**
4765185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick * Generate a new temporary and add its declaration to the instruction stream
4775185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick */
4785185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanickstatic ir_variable *
4795185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanickgenerate_temporary(const glsl_type *type, exec_list *instructions,
4805185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick		   struct _mesa_glsl_parse_state *state)
4815185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick{
4825185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick   char *name = (char *) malloc(sizeof(char) * 13);
4835185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick
4845185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick   snprintf(name, 13, "tmp_%08X", state->temp_index);
4855185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick   state->temp_index++;
4865185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick
4875185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick   ir_variable *const var = new ir_variable(type, name);
4885185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick   instructions->push_tail(var);
4895185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick
4905185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick   return var;
4915185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick}
4925185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick
4935185a5f7d5654c9202c226015c4daeee43d9b897Ian Romanick
494de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholtstatic ir_rvalue *
495de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholtget_lvalue_copy(exec_list *instructions, struct _mesa_glsl_parse_state *state,
496de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt		ir_rvalue *lvalue, YYLTYPE loc)
497de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt{
498de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   ir_variable *var;
499de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   ir_rvalue *var_deref;
500de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
501de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   /* FINISHME: Give unique names to the temporaries. */
502de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   var = new ir_variable(lvalue->type, "_internal_tmp");
503de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   var->mode = ir_var_auto;
504de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
505de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   var_deref = new ir_dereference(var);
506de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   do_assignment(instructions, state, var_deref, lvalue, loc);
507de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
508de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   /* Once we've created this temporary, mark it read only so it's no
509de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt    * longer considered an lvalue.
510de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt    */
511de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   var->read_only = true;
512de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
513de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   return var_deref;
514de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt}
515de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
516de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
517fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
5180044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_node::hir(exec_list *instructions,
51918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick	      struct _mesa_glsl_parse_state *state)
52018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick{
52118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   (void) instructions;
52218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   (void) state;
52318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
52418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   return NULL;
52518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick}
52618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
52718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
528fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
5290044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_expression::hir(exec_list *instructions,
53018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick		    struct _mesa_glsl_parse_state *state)
531a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
532a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   static const int operations[AST_NUM_OPERATORS] = {
533a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_assign doesn't convert to ir_expression. */
534a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_plus doesn't convert to ir_expression. */
535a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_neg,
536a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_add,
537a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_sub,
538a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mul,
539a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_div,
540a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mod,
541a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lshift,
542a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_rshift,
543a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_less,
544a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_greater,
545a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lequal,
546a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_gequal,
547a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_equal,
548a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_nequal,
549a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_and,
550a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_xor,
551a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_or,
552a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_bit_not,
553a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_and,
554a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_xor,
555a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_or,
556a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_logic_not,
557a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
558a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Note: The following block of expression types actually convert
559a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * to multiple IR instructions.
560a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
561a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mul,     /* ast_mul_assign */
562a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_div,     /* ast_div_assign */
563a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mod,     /* ast_mod_assign */
564a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_add,     /* ast_add_assign */
565a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_sub,     /* ast_sub_assign */
566a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lshift,  /* ast_ls_assign */
567a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_rshift,  /* ast_rs_assign */
568a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_and, /* ast_and_assign */
569a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_xor, /* ast_xor_assign */
570a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_or,  /* ast_or_assign */
571a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
572a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_conditional doesn't convert to ir_expression. */
573de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_add,     /* ast_pre_inc. */
574de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_sub,     /* ast_pre_dec. */
575de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_add,     /* ast_post_inc. */
576de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_sub,     /* ast_post_dec. */
577a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_field_selection doesn't conv to ir_expression. */
578a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_array_index doesn't convert to ir_expression. */
579a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_function_call doesn't conv to ir_expression. */
580a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_identifier doesn't convert to ir_expression. */
581a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_int_constant doesn't convert to ir_expression. */
582a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_uint_constant doesn't conv to ir_expression. */
583a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_float_constant doesn't conv to ir_expression. */
584a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_bool_constant doesn't conv to ir_expression. */
585a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_sequence doesn't convert to ir_expression. */
586a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   };
587fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke   ir_rvalue *result = NULL;
588fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke   ir_rvalue *op[2];
589a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   struct simple_node op_list;
5900471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   const struct glsl_type *type = glsl_type::error_type;
591a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   bool error_emitted = false;
592a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   YYLTYPE loc;
593a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
59418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   loc = this->get_location();
595a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   make_empty_list(& op_list);
596a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
59718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   switch (this->oper) {
5986652af36fe8994b1621d882fcc230d320908a2a3Ian Romanick   case ast_assign: {
59918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
60018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
601a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
60210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      result = do_assignment(instructions, state, op[0], op[1],
60310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt			     this->subexpressions[0]->get_location());
60410a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      error_emitted = result->type->is_error();
60510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      type = result->type;
606a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
6076652af36fe8994b1621d882fcc230d320908a2a3Ian Romanick   }
608a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
609a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_plus:
61018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
611a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
612a43817a483a8c4a480ef4e6dfda2cef899300eb0Ian Romanick      error_emitted = op[0]->type->is_error();
613a43817a483a8c4a480ef4e6dfda2cef899300eb0Ian Romanick      if (type->is_error())
614a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 op[0]->type = type;
615a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
616a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      result = op[0];
617a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
618a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
619a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_neg:
62018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
621a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
62265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = unary_arithmetic_result_type(op[0]->type, state, & loc);
623a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
62465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
625a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
62618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_expression(operations[this->oper], type,
627a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick				 op[0], NULL);
628a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
629a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
630a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_add:
631a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sub:
632a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mul:
633a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_div:
63418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
63518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
636a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
637bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      type = arithmetic_result_type(op[0], op[1],
63818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick				    (this->oper == ast_mul),
639a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt				    state, & loc);
640a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      error_emitted = type->is_error();
641a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
64218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_expression(operations[this->oper], type,
643a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick				 op[0], op[1]);
644a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
645a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
646a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mod:
64718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
64818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
649a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
65065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
651a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
65218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      assert(operations[this->oper] == ir_binop_mod);
653a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
65418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_expression(operations[this->oper], type,
655a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick				 op[0], op[1]);
65665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
657a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
658a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
659a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_lshift:
660a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_rshift:
661183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      _mesa_glsl_error(& loc, state, "FINISHME: implement bit-shift operators");
662183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      error_emitted = true;
663a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
664a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
665a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_less:
666a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_greater:
667a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_lequal:
668a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_gequal:
66918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
67018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
671a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
67265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = relational_result_type(op[0], op[1], state, & loc);
673a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
674a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* The relational operators must either generate an error or result
675a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * in a scalar boolean.  See page 57 of the GLSL 1.50 spec.
676a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
677a43817a483a8c4a480ef4e6dfda2cef899300eb0Ian Romanick      assert(type->is_error()
678a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	     || ((type->base_type == GLSL_TYPE_BOOL)
679cb36f8aaeeb09660843316270a781948f773d90bIan Romanick		 && type->is_scalar()));
680a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
68118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_expression(operations[this->oper], type,
682a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick				 op[0], op[1]);
68365e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
684a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
685a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
686a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_nequal:
687a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_equal:
6886e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
6896e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
6906e659caaa946339a2de3890a8bed091ccb65102aIan Romanick
6916e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
6926e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *
6936e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    "The equality operators equal (==), and not equal (!=)
6946e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    operate on all types. They result in a scalar Boolean. If
6956e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    the operand types do not match, then there must be a
6966e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    conversion from Section 4.1.10 "Implicit Conversions"
6976e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    applied to one operand that can make them match, in which
6986e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    case this conversion is done."
6996e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       */
700bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      if ((!apply_implicit_conversion(op[0]->type, op[1], state)
701bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick	   && !apply_implicit_conversion(op[1]->type, op[0], state))
702212b0327b47033442842a7be3d7fb10e08e2bf66Ian Romanick	  || (op[0]->type != op[1]->type)) {
7036e659caaa946339a2de3890a8bed091ccb65102aIan Romanick	 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
7046e659caaa946339a2de3890a8bed091ccb65102aIan Romanick			  "type", (this->oper == ast_equal) ? "==" : "!=");
7056e659caaa946339a2de3890a8bed091ccb65102aIan Romanick	 error_emitted = true;
706a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick      } else if ((state->language_version <= 110)
707a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick		 && (op[0]->type->is_array() || op[1]->type->is_array())) {
708a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick	 _mesa_glsl_error(& loc, state, "array comparisons forbidden in "
709a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick			  "GLSL 1.10");
710a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick	 error_emitted = true;
7116e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      }
7126e659caaa946339a2de3890a8bed091ccb65102aIan Romanick
7136e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      result = new ir_expression(operations[this->oper], glsl_type::bool_type,
7146e659caaa946339a2de3890a8bed091ccb65102aIan Romanick				 op[0], op[1]);
7156e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      type = glsl_type::bool_type;
7166e659caaa946339a2de3890a8bed091ccb65102aIan Romanick
7176e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      assert(result->type == glsl_type::bool_type);
718a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
719a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
720a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_and:
721a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_xor:
722a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_or:
723a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_not:
724183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      _mesa_glsl_error(& loc, state, "FINISHME: implement bit-wise operators");
725183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      error_emitted = true;
726a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
727a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
728a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_logic_and:
729a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_logic_xor:
730a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_logic_or:
731b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
732b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      op[1] = this->subexpressions[1]->hir(instructions, state);
733b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
734b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
735b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt	 YYLTYPE loc = this->subexpressions[0]->get_location();
736b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
737b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt	 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
738b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt			  operator_string(this->oper));
739ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt	 error_emitted = true;
740b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      }
741b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
742b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
743b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt	 YYLTYPE loc = this->subexpressions[1]->get_location();
744b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
745b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt	 _mesa_glsl_error(& loc, state, "RHS of `%s' must be scalar boolean",
746b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt			  operator_string(this->oper));
747ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt	 error_emitted = true;
748b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      }
749b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
750b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      result = new ir_expression(operations[this->oper], glsl_type::bool_type,
751b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt				 op[0], op[1]);
752ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt      type = glsl_type::bool_type;
753a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
754a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
755a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt   case ast_logic_not:
756a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
757a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
758a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
759a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt	 YYLTYPE loc = this->subexpressions[0]->get_location();
760a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
761a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt	 _mesa_glsl_error(& loc, state,
762a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt			  "operand of `!' must be scalar boolean");
763ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt	 error_emitted = true;
764a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      }
765a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
766a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      result = new ir_expression(operations[this->oper], glsl_type::bool_type,
767a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt				 op[0], NULL);
768ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt      type = glsl_type::bool_type;
769a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      break;
770a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
771a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mul_assign:
772a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_div_assign:
773a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_add_assign:
774a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sub_assign: {
77518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
77618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
777a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
778bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      type = arithmetic_result_type(op[0], op[1],
77918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick				    (this->oper == ast_mul_assign),
780a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt				    state, & loc);
781a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
782fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke      ir_rvalue *temp_rhs = new ir_expression(operations[this->oper], type,
783fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke				              op[0], op[1]);
784a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
78510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      result = do_assignment(instructions, state, op[0], temp_rhs,
78610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt			     this->subexpressions[0]->get_location());
78710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      type = result->type;
78810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      error_emitted = (op[0]->type->is_error());
789a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
790a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* GLSL 1.10 does not allow array assignment.  However, we don't have to
791a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * explicitly test for this because none of the binary expression
792a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * operators allow array operands either.
793a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
794a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
795a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
796a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
797a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
79848a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt   case ast_mod_assign: {
79948a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
80048a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      op[1] = this->subexpressions[1]->hir(instructions, state);
80148a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
80265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
80348a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
80448a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      assert(operations[this->oper] == ir_binop_mod);
80548a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
80648a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      struct ir_rvalue *temp_rhs;
80748a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      temp_rhs = new ir_expression(operations[this->oper], type,
80848a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt				   op[0], op[1]);
80948a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
81048a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      result = do_assignment(instructions, state, op[0], temp_rhs,
81148a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt			     this->subexpressions[0]->get_location());
81248a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      type = result->type;
81365e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
81448a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      break;
81548a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt   }
816a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
817a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_ls_assign:
818a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_rs_assign:
819183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      _mesa_glsl_error(& loc, state,
820183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt		       "FINISHME: implement bit-shift assignment operators");
821183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      error_emitted = true;
822251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
823a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
824a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_and_assign:
825a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_xor_assign:
826a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_or_assign:
827183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      _mesa_glsl_error(& loc, state,
828183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt		       "FINISHME: implement logic assignment operators");
829183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      error_emitted = true;
830251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
831a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
83296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick   case ast_conditional: {
83396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
83496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
83596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
83696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *
83796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    "The ternary selection operator (?:). It operates on three
83896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    expressions (exp1 ? exp2 : exp3). This operator evaluates the
83996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    first expression, which must result in a scalar Boolean."
84096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
84196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
84296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 YYLTYPE loc = this->subexpressions[0]->get_location();
84396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
84496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 _mesa_glsl_error(& loc, state, "?: condition must be scalar boolean");
84596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 error_emitted = true;
84696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      }
84796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
84896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* The :? operator is implemented by generating an anonymous temporary
84996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * followed by an if-statement.  The last instruction in each branch of
85096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * the if-statement assigns a value to the anonymous temporary.  This
85196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * temporary is the r-value of the expression.
85296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
85396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      ir_variable *const tmp = generate_temporary(glsl_type::error_type,
85496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick						  instructions, state);
85596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
85696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      ir_if *const stmt = new ir_if(op[0]);
85796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      instructions->push_tail(stmt);
85896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
85996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      op[1] = this->subexpressions[1]->hir(& stmt->then_instructions, state);
86096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      ir_dereference *const then_deref = new ir_dereference(tmp);
86196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      ir_assignment *const then_assign =
86296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 new ir_assignment(then_deref, op[1], NULL);
86396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      stmt->then_instructions.push_tail(then_assign);
86496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
86596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      op[2] = this->subexpressions[2]->hir(& stmt->else_instructions, state);
86696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      ir_dereference *const else_deref = new ir_dereference(tmp);
86796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      ir_assignment *const else_assign =
86896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 new ir_assignment(else_deref, op[2], NULL);
86996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      stmt->else_instructions.push_tail(else_assign);
87096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
87196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
87296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *
87396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     "The second and third expressions can be any type, as
87496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     long their types match, or there is a conversion in
87596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     Section 4.1.10 "Implicit Conversions" that can be applied
87696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     to one of the expressions to make their types match. This
87796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     resulting matching type is the type of the entire
87896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     expression."
87996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
880bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      if ((!apply_implicit_conversion(op[1]->type, op[2], state)
881bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick	   && !apply_implicit_conversion(op[2]->type, op[1], state))
882db9be2e7aa3a56e43b725ad7725fe6b424e4933eIan Romanick	  || (op[1]->type != op[2]->type)) {
88396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 YYLTYPE loc = this->subexpressions[1]->get_location();
88496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
88596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
88696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick			  "operator must have matching types.");
88796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 error_emitted = true;
888db9be2e7aa3a56e43b725ad7725fe6b424e4933eIan Romanick      } else {
889db9be2e7aa3a56e43b725ad7725fe6b424e4933eIan Romanick	 tmp->type = op[1]->type;
89096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      }
89196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
89296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      result = new ir_dereference(tmp);
89396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      type = tmp->type;
894251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
89596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick   }
896a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
897a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_pre_inc:
89876ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt   case ast_pre_dec: {
89976ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
90076ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
90176ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt	 op[1] = new ir_constant(1.0f);
90276ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      else
90376ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt	 op[1] = new ir_constant(1);
90476ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
905a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      type = arithmetic_result_type(op[0], op[1], false, state, & loc);
90676ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
90776ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      struct ir_rvalue *temp_rhs;
90876ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      temp_rhs = new ir_expression(operations[this->oper], type,
90976ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt				   op[0], op[1]);
91076ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
91176ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      result = do_assignment(instructions, state, op[0], temp_rhs,
91276ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt			     this->subexpressions[0]->get_location());
91376ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      type = result->type;
91476ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      error_emitted = op[0]->type->is_error();
91576ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      break;
91676ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt   }
917a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
918a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_post_inc:
919de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   case ast_post_dec: {
920de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
921de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
922de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt	 op[1] = new ir_constant(1.0f);
923de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      else
924de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt	 op[1] = new ir_constant(1);
925de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
926de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
927de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
928a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      type = arithmetic_result_type(op[0], op[1], false, state, & loc);
929de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
930de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      struct ir_rvalue *temp_rhs;
931de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      temp_rhs = new ir_expression(operations[this->oper], type,
932de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt				   op[0], op[1]);
933de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
934de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      /* Get a temporary of a copy of the lvalue before it's modified.
935de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt       * This may get thrown away later.
936de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt       */
937de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      result = get_lvalue_copy(instructions, state, op[0],
938de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt			       this->subexpressions[0]->get_location());
939de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
940de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      (void)do_assignment(instructions, state, op[0], temp_rhs,
941de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt			  this->subexpressions[0]->get_location());
942de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
943de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      type = result->type;
944de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      error_emitted = op[0]->type->is_error();
945a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
946de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   }
947a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
948a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_field_selection:
94918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = _mesa_ast_field_selection_to_hir(this, instructions, state);
950a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = result->type;
951a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
952a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
95327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick   case ast_array_index: {
95427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      YYLTYPE index_loc = subexpressions[1]->get_location();
95527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
95627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      op[0] = subexpressions[0]->hir(instructions, state);
95727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      op[1] = subexpressions[1]->hir(instructions, state);
95827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
95927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
96027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
961b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      ir_dereference *const lhs = op[0]->as_dereference();
962b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      ir_instruction *array;
963b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      if ((lhs != NULL)
964b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	  && (lhs->mode == ir_dereference::ir_reference_variable)) {
965b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	 result = new ir_dereference(lhs->var, op[1]);
966b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
967b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	 delete op[0];
968b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	 array = lhs->var;
969b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      } else {
970b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	 result = new ir_dereference(op[0], op[1]);
971b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	 array = op[0];
972b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      }
973b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
974b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      /* Do not use op[0] after this point.  Use array.
975b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick       */
976b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      op[0] = NULL;
977b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
97827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
97927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (error_emitted)
98027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 break;
98127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
98227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      /* FINISHME: Handle vectors and matrices accessed with []. */
983b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      if (!array->type->is_array()) {
98427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
98527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick			  "cannot dereference non-array");
98627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
98727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
98827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
98927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (!op[1]->type->is_integer()) {
99027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
99127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick			  "array index must be integer type");
99227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
99327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      } else if (!op[1]->type->is_scalar()) {
99427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
99527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick			  "array index must be scalar");
99627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
99727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
99827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
99927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      /* If the array index is a constant expression and the array has a
100027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * declared size, ensure that the access is in-bounds.  If the array
100127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * index is not a constant expression, ensure that the array has a
100227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * declared size.
100327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       */
100427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      ir_constant *const const_index = op[1]->constant_expression_value();
100527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (const_index != NULL) {
100627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 const int idx = const_index->value.i[0];
100727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
100827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
100927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *
101027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    "It is illegal to declare an array with a size, and then
101127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    later (in the same shader) index the same array with an
101227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    integral constant expression greater than or equal to the
101327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    declared size. It is also illegal to index an array with a
101427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    negative constant expression."
101527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  */
1016b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	 if ((array->type->array_size() > 0)
1017b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	     && (array->type->array_size() <= idx)) {
101827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	    _mesa_glsl_error(& loc, state,
101927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick			     "array index must be < %u",
1020b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick			     array->type->array_size());
102127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	    error_emitted = true;
102227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 }
102327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
102427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 if (idx < 0) {
102527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	    _mesa_glsl_error(& loc, state,
102627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick			     "array index must be >= 0");
102727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	    error_emitted = true;
102827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 }
1029b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
1030b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	 ir_variable *const v = array->as_variable();
1031b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	 if ((v != NULL) && (unsigned(idx) > v->max_array_access))
1032b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	    v->max_array_access = idx;
103327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
103427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
103527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (error_emitted)
103627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 result->type = glsl_type::error_type;
103727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
103827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      type = result->type;
1039a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
104027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick   }
1041a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1042a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_function_call:
10437cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick      /* Should *NEVER* get here.  ast_function_call should always be handled
10447cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick       * by ast_function_expression::hir.
1045a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
10467cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick      assert(0);
1047a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1048a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1049a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_identifier: {
1050a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* ast_identifier can appear several places in a full abstract syntax
1051a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * tree.  This particular use must be at location specified in the grammar
1052a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * as 'variable_identifier'.
1053a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
10548bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      ir_variable *var =
10558bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick	 state->symbols->get_variable(this->primary_expression.identifier);
1056a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1057a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      result = new ir_dereference(var);
1058a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1059a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (var != NULL) {
1060a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 type = result->type;
1061a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
106271d0bbfcb2853f37b580ec7b705e55bb0eb426faIan Romanick	 _mesa_glsl_error(& loc, state, "`%s' undeclared",
106318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  this->primary_expression.identifier);
1064a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1065a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 error_emitted = true;
1066a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1067a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1068a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1069a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1070a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_int_constant:
10710471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::int_type;
107218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_constant(type, & this->primary_expression);
1073a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1074a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1075a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_uint_constant:
10760471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::uint_type;
107718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_constant(type, & this->primary_expression);
1078a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1079a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1080a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_float_constant:
10810471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::float_type;
108218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_constant(type, & this->primary_expression);
1083a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1084a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1085a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bool_constant:
10860471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::bool_type;
108718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = new ir_constant(type, & this->primary_expression);
1088a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1089a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1090a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sequence: {
1091a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      struct simple_node *ptr;
1092a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1093a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* It should not be possible to generate a sequence in the AST without
1094a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * any expressions in it.
1095a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
109618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      assert(!is_empty_list(&this->expressions));
1097a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1098a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* The r-value of a sequence is the last expression in the sequence.  If
1099a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * the other expressions in the sequence do not have side-effects (and
1100a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * therefore add instructions to the instruction list), they get dropped
1101a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * on the floor.
1102a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
110318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      foreach (ptr, &this->expressions)
110418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick	 result = ((ast_node *)ptr)->hir(instructions, state);
1105a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1106a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = result->type;
1107a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1108a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Any errors should have already been emitted in the loop above.
1109a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1110a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      error_emitted = true;
1111a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1112a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1113a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1114a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1115cef3baecf636a30b62cd7a1e8de57c7650f7003eIan Romanick   if (type->is_error() && !error_emitted)
111671d0bbfcb2853f37b580ec7b705e55bb0eb426faIan Romanick      _mesa_glsl_error(& loc, state, "type mismatch");
1117a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1118a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return result;
1119a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1120a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1121a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1122fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
11230044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_expression_statement::hir(exec_list *instructions,
112418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			      struct _mesa_glsl_parse_state *state)
1125a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1126a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* It is possible to have expression statements that don't have an
1127a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * expression.  This is the solitary semicolon:
1128a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
1129a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * for (i = 0; i < 5; i++)
1130a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     ;
1131a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
1132a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * In this case the expression will be NULL.  Test for NULL and don't do
1133a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * anything in that case.
1134a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
113518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (expression != NULL)
113618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      expression->hir(instructions, state);
1137a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1138a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Statements do not have r-values.
1139a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1140a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1141a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1142a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1143a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1144fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
11450044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_compound_statement::hir(exec_list *instructions,
114618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			    struct _mesa_glsl_parse_state *state)
1147a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1148a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   struct simple_node *ptr;
1149a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1150a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
115118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (new_scope)
11528bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      state->symbols->push_scope();
1153a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
115418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   foreach (ptr, &statements)
115518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      ((ast_node *)ptr)->hir(instructions, state);
1156a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
115718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (new_scope)
11588bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      state->symbols->pop_scope();
1159a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1160a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Compound statements do not have r-values.
1161a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1162a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1163a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1164a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1165a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
116628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanickstatic const glsl_type *
116728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanickprocess_array_type(const glsl_type *base, ast_node *array_size,
116828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick		   struct _mesa_glsl_parse_state *state)
116928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick{
117028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   unsigned length = 0;
117128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
117228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   /* FINISHME: Reject delcarations of multidimensional arrays. */
117328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
117428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   if (array_size != NULL) {
117528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      exec_list dummy_instructions;
117628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
117728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      YYLTYPE loc = array_size->get_location();
117828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
117928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      /* FINISHME: Verify that the grammar forbids side-effects in array
118028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick       * FINISHME: sizes.   i.e., 'vec4 [x = 12] data'
118128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick       */
118228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      assert(dummy_instructions.is_empty());
118328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
118428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      if (ir != NULL) {
118528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 if (!ir->type->is_integer()) {
118628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    _mesa_glsl_error(& loc, state, "array size must be integer type");
118728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 } else if (!ir->type->is_scalar()) {
118828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    _mesa_glsl_error(& loc, state, "array size must be scalar type");
118928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 } else {
119028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    ir_constant *const size = ir->constant_expression_value();
119128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
119228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    if (size == NULL) {
119328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       _mesa_glsl_error(& loc, state, "array size must be a "
119428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick				"constant valued expression");
119528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    } else if (size->value.i[0] <= 0) {
119628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       _mesa_glsl_error(& loc, state, "array size must be > 0");
119728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    } else {
119828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       assert(size->type == ir->type);
119928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       length = size->value.u[0];
120028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    }
120128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 }
120228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      }
120328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   }
120428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
120528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   return glsl_type::get_array_instance(base, length);
120628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick}
120728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
120828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
1209d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanickconst glsl_type *
1210d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanickast_type_specifier::glsl_type(const char **name,
1211d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick			      struct _mesa_glsl_parse_state *state) const
1212a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1213d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   const struct glsl_type *type;
1214a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1215d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   if (this->type_specifier == ast_struct) {
1216a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* FINISHME: Handle annonymous structures. */
1217a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = NULL;
1218a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
1219d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick      type = state->symbols->get_type(this->type_name);
1220d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick      *name = this->type_name;
1221a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1222d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick      if (this->is_array) {
1223d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick	 type = process_array_type(type, this->array_size, state);
122428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      }
1225a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1226a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1227a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return type;
1228a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1229a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1230a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1231a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic void
1232a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickapply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
1233a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick				 struct ir_variable *var,
12342e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				 struct _mesa_glsl_parse_state *state,
12352e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				 YYLTYPE *loc)
1236a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1237a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->invariant)
1238a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->invariant = 1;
1239a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1240a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* FINISHME: Mark 'in' variables at global scope as read-only. */
1241a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->constant || qual->attribute || qual->uniform
1242a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       || (qual->varying && (state->target == fragment_shader)))
1243a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->read_only = 1;
1244a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1245a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->centroid)
1246a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->centroid = 1;
1247a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
12482e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   if (qual->attribute && state->target == fragment_shader) {
12492e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      var->type = glsl_type::error_type;
12502e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      _mesa_glsl_error(loc, state,
12512e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt		       "`attribute' variables may not be declared in the "
12522e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt		       "fragment shader");
12532e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   }
12542e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt
125590b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt   /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
125690b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *
125790b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     "The varying qualifier can be used only with the data types
125890b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
125990b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     these."
126090b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    */
126190b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt   if (qual->varying && var->type->base_type != GLSL_TYPE_FLOAT) {
126290b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt      var->type = glsl_type::error_type;
126390b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt      _mesa_glsl_error(loc, state,
126490b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt		       "varying variables must be of base type float");
126590b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt   }
126690b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt
1267a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->in && qual->out)
1268a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_inout;
1269a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else if (qual->attribute || qual->in
1270a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    || (qual->varying && (state->target == fragment_shader)))
1271a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_in;
12720b678234625fac67a89285ad2871dedc891fb1b1Ian Romanick   else if (qual->out || (qual->varying && (state->target == vertex_shader)))
1273a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_out;
1274a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else if (qual->uniform)
1275a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_uniform;
1276a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else
1277a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_auto;
1278a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1279a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->flat)
1280a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->interpolation = ir_var_flat;
1281a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else if (qual->noperspective)
1282a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->interpolation = ir_var_noperspective;
1283a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else
1284a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->interpolation = ir_var_smooth;
1285a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1286a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1287a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1288fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
12890044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_declarator_list::hir(exec_list *instructions,
129018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			 struct _mesa_glsl_parse_state *state)
1291a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1292a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   struct simple_node *ptr;
1293a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const struct glsl_type *decl_type;
1294a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const char *type_name = NULL;
1295a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1296a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1297a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* FINISHME: Handle vertex shader "invariant" declarations that do not
1298a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * FINISHME: include a type.  These re-declare built-in variables to be
1299a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * FINISHME: invariant.
1300a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1301a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1302d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   decl_type = this->type->specifier->glsl_type(& type_name, state);
1303a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
130418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   foreach (ptr, &this->declarations) {
1305a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      struct ast_declaration *const decl = (struct ast_declaration * )ptr;
1306a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      const struct glsl_type *var_type;
1307a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      struct ir_variable *var;
13082e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      YYLTYPE loc = this->get_location();
1309a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1310a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* FINISHME: Emit a warning if a variable declaration shadows a
1311a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * FINISHME: declaration at a higher scope.
1312a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1313a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1314cec65a6b76290ee4da91691bd3ef01c3fb8a0c37Ian Romanick      if ((decl_type == NULL) || decl_type->is_void()) {
1315a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 if (type_name != NULL) {
1316a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    _mesa_glsl_error(& loc, state,
1317a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     "invalid type `%s' in declaration of `%s'",
1318a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     type_name, decl->identifier);
1319a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 } else {
1320a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    _mesa_glsl_error(& loc, state,
1321a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     "invalid type in declaration of `%s'",
1322a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     decl->identifier);
1323a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
1324a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 continue;
1325a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1326a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1327a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (decl->is_array) {
132828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 var_type = process_array_type(decl_type, decl->array_size, state);
1329a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
1330a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 var_type = decl_type;
1331a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1332a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1333a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var = new ir_variable(var_type, decl->identifier);
1334a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
13352e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
13362e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				       & loc);
1337a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1338a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Attempt to add the variable to the symbol table.  If this fails, it
1339a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick       * means the variable has already been declared at this scope.  Arrays
1340a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick       * fudge this rule a little bit.
1341a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick       *
1342a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick       * From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
1343a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick       *
1344a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick       *    "It is legal to declare an array without a size and then
1345a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick       *    later re-declare the same name as an array of the same
1346a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick       *    type and specify a size."
1347a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
13483359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      if (state->symbols->name_declared_this_scope(decl->identifier)) {
1349a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	 ir_variable *const earlier =
1350a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	    state->symbols->get_variable(decl->identifier);
1351a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick
1352a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	 if ((earlier != NULL)
1353a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	     && (earlier->type->array_size() == 0)
1354a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	     && var->type->is_array()
1355a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	     && (var->type->element_type() == earlier->type->element_type())) {
1356a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	    /* FINISHME: This doesn't match the qualifiers on the two
1357a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	     * FINISHME: declarations.  It's not 100% clear whether this is
1358a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	     * FINISHME: required or not.
1359a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	     */
1360b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
1361b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	    if (var->type->array_size() <= earlier->max_array_access) {
1362b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	       YYLTYPE loc = this->get_location();
1363b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
1364b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	       _mesa_glsl_error(& loc, state, "array size must be > %u due to "
1365b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick				"previous access",
1366b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick				earlier->max_array_access);
1367b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick	    }
1368b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
1369a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	    earlier->type = var->type;
1370a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	    delete var;
1371a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	    var = NULL;
1372a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	 } else {
1373a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	    YYLTYPE loc = this->get_location();
1374a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick
1375a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	    _mesa_glsl_error(& loc, state, "`%s' redeclared",
1376a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick			     decl->identifier);
1377a4f308f0663208eec07cc320ff4a67589d4b5a7fIan Romanick	 }
1378a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1379a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 continue;
1380a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1381a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1382b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt      /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
1383b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt       *
1384b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt       *   "Identifiers starting with "gl_" are reserved for use by
1385b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt       *   OpenGL, and may not be declared in a shader as either a
1386b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt       *   variable or a function."
1387b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt       */
1388b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt      if (strncmp(decl->identifier, "gl_", 3) == 0) {
1389b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt	 /* FINISHME: This should only trigger if we're not redefining
1390b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt	  * FINISHME: a builtin (to add a qualifier, for example).
1391b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt	  */
1392b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt	 _mesa_glsl_error(& loc, state,
1393b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt			  "identifier `%s' uses reserved `gl_' prefix",
1394b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt			  decl->identifier);
1395b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt      }
1396b97ee2e260efb68849ee13402072b7d7185746d5Eric Anholt
13970044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick      instructions->push_tail(var);
1398a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1399e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick      if (state->current_function != NULL) {
1400b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 const char *mode = NULL;
1401e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 const char *extra = "";
1402b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick
1403e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 /* There is no need to check for 'inout' here because the parser will
1404e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	  * only allow that in function parameter lists.
1405e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	  */
1406e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	 if (this->type->qualifier.attribute) {
1407b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	    mode = "attribute";
1408b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 } else if (this->type->qualifier.uniform) {
1409b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	    mode = "uniform";
1410b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 } else if (this->type->qualifier.varying) {
1411b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	    mode = "varying";
1412e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 } else if (this->type->qualifier.in) {
1413e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    mode = "in";
1414e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    extra = " or in function parameter list";
1415e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 } else if (this->type->qualifier.out) {
1416e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    mode = "out";
1417e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    extra = " or in function parameter list";
1418b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 }
1419b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick
1420b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 if (mode) {
1421e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	    _mesa_glsl_error(& loc, state,
1422b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick			     "%s variable `%s' must be declared at "
1423e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick			     "global scope%s",
1424e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick			     mode, var->name, extra);
1425e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	 }
1426e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick      } else if (var->mode == ir_var_in) {
1427fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	 if (state->target == vertex_shader) {
1428fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    bool error_emitted = false;
1429fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
1430fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1431fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
1432fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    "Vertex shader inputs can only be float, floating-point
1433fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    vectors, matrices, signed and unsigned integers and integer
1434fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    vectors. Vertex shader inputs can also form arrays of these
1435fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    types, but not structures."
1436fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
14372d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
14382d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *
14392d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *    "Vertex shader inputs can only be float, floating-point
14402d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *    vectors, matrices, signed and unsigned integers and integer
14412d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *    vectors. They cannot be arrays or structures."
14422d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *
1443fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
1444fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
1445fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    "The attribute qualifier can be used only with float,
1446fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    floating-point vectors, and matrices. Attribute variables
1447fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    cannot be declared as arrays or structures."
1448fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     */
1449fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    const glsl_type *check_type = var->type->is_array()
1450fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       ? var->type->fields.array : var->type;
1451fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
1452fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    switch (check_type->base_type) {
1453fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_FLOAT:
1454fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       break;
1455fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_UINT:
1456fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_INT:
1457fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       if (state->language_version > 120)
1458fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick		  break;
1459fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       /* FALLTHROUGH */
1460fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    default:
1461fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       _mesa_glsl_error(& loc, state,
1462fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"vertex shader input / attribute cannot have "
1463fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"type %s`%s'",
1464fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				var->type->is_array() ? "array of " : "",
1465fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				check_type->name);
1466fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       error_emitted = true;
1467fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    }
1468fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
14692d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	    if (!error_emitted && (state->language_version <= 130)
1470fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick		&& var->type->is_array()) {
1471fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       _mesa_glsl_error(& loc, state,
1472fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"vertex shader input / attribute cannot have "
1473fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"array type");
1474fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       error_emitted = true;
1475fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    }
1476fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	 }
1477fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick      }
1478fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
147966faec4895b7bb59a614087a200c05157191b4aeIan Romanick      if (decl->initializer != NULL) {
148043de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 YYLTYPE initializer_loc = decl->initializer->get_location();
148143de17282017bdf187d6e646de3262cc64b7f46bIan Romanick
148266faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
148366faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *
148466faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *    "All uniform variables are read-only and are initialized either
148566faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *    directly by an application via API commands, or indirectly by
148666faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *    OpenGL."
148766faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  */
148866faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 if ((state->language_version <= 110)
148966faec4895b7bb59a614087a200c05157191b4aeIan Romanick	     && (var->mode == ir_var_uniform)) {
149043de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	    _mesa_glsl_error(& initializer_loc, state,
149143de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     "cannot initialize uniforms in GLSL 1.10");
149243de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 }
149343de17282017bdf187d6e646de3262cc64b7f46bIan Romanick
149443de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 if (var->type->is_sampler()) {
149543de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	    _mesa_glsl_error(& initializer_loc, state,
149643de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     "cannot initialize samplers");
149743de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 }
149819360152f5bd8cff93359dbfe5a50a90b699c118Ian Romanick
149943de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 if ((var->mode == ir_var_in) && (state->current_function == NULL)) {
150043de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	    _mesa_glsl_error(& initializer_loc, state,
150143de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     "cannot initialize %s shader input / %s",
150243de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     (state->target == vertex_shader)
150343de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     ? "vertex" : "fragment",
150443de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     (state->target == vertex_shader)
150543de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     ? "attribute" : "varying");
150666faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 }
150766faec4895b7bb59a614087a200c05157191b4aeIan Romanick
150866faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 ir_dereference *const lhs = new ir_dereference(var);
1509307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	 ir_rvalue *rhs = decl->initializer->hir(instructions, state);
151019360152f5bd8cff93359dbfe5a50a90b699c118Ian Romanick
1511307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	 /* Calculate the constant value if this is a const
1512307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	  * declaration.
151366faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  */
1514307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	 if (this->type->qualifier.constant) {
1515307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	    rhs = rhs->constant_expression_value();
1516307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	    if (!rhs) {
1517307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	       _mesa_glsl_error(& initializer_loc, state,
1518307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt				"initializer of const variable `%s' must be a "
1519307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt				"constant expression",
1520307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt				decl->identifier);
1521307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	    }
1522307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	 }
152366faec4895b7bb59a614087a200c05157191b4aeIan Romanick
1524307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	 if (rhs && !rhs->type->is_error()) {
1525ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt	    bool temp = var->read_only;
1526ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt	    if (this->type->qualifier.constant)
1527ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt	       var->read_only = false;
152866faec4895b7bb59a614087a200c05157191b4aeIan Romanick	    (void) do_assignment(instructions, state, lhs, rhs,
152966faec4895b7bb59a614087a200c05157191b4aeIan Romanick				 this->get_location());
1530ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt	    var->read_only = temp;
153166faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 }
153266faec4895b7bb59a614087a200c05157191b4aeIan Romanick      }
153317d86f4371da413176ba365ca26a58bac172d365Ian Romanick
15340ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt      /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
15350ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *
15360ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *     "It is an error to write to a const variable outside of
15370ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *      its declaration, so they must be initialized when
15380ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *      declared."
15390ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       */
15400ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt      if (this->type->qualifier.constant && decl->initializer == NULL) {
15410ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt	 _mesa_glsl_error(& loc, state,
15420ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt			  "const declaration of `%s' must be initialized");
15430ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt      }
15440ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt
154517d86f4371da413176ba365ca26a58bac172d365Ian Romanick      /* Add the vairable to the symbol table after processing the initializer.
154617d86f4371da413176ba365ca26a58bac172d365Ian Romanick       * This differs from most C-like languages, but it follows the GLSL
154717d86f4371da413176ba365ca26a58bac172d365Ian Romanick       * specification.  From page 28 (page 34 of the PDF) of the GLSL 1.50
154817d86f4371da413176ba365ca26a58bac172d365Ian Romanick       * spec:
154917d86f4371da413176ba365ca26a58bac172d365Ian Romanick       *
155017d86f4371da413176ba365ca26a58bac172d365Ian Romanick       *     "Within a declaration, the scope of a name starts immediately
155117d86f4371da413176ba365ca26a58bac172d365Ian Romanick       *     after the initializer if present or immediately after the name
155217d86f4371da413176ba365ca26a58bac172d365Ian Romanick       *     being declared if not."
155317d86f4371da413176ba365ca26a58bac172d365Ian Romanick       */
155417d86f4371da413176ba365ca26a58bac172d365Ian Romanick      const bool added_variable =
155517d86f4371da413176ba365ca26a58bac172d365Ian Romanick	 state->symbols->add_variable(decl->identifier, var);
155617d86f4371da413176ba365ca26a58bac172d365Ian Romanick      assert(added_variable);
1557a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1558a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1559a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Variable declarations do not have r-values.
1560a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1561a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1562a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1563a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1564a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1565fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
15660044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_parameter_declarator::hir(exec_list *instructions,
156718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			      struct _mesa_glsl_parse_state *state)
1568a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1569a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const struct glsl_type *type;
1570a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const char *name = NULL;
15712e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   YYLTYPE loc = this->get_location();
1572a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1573d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   type = this->type->specifier->glsl_type(& name, state);
1574a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1575a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (type == NULL) {
1576a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (name != NULL) {
1577a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 _mesa_glsl_error(& loc, state,
1578a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			  "invalid type `%s' in declaration of `%s'",
157918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  name, this->identifier);
1580a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
1581a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 _mesa_glsl_error(& loc, state,
1582a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			  "invalid type in declaration of `%s'",
158318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  this->identifier);
1584a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1585a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
15860471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::error_type;
1587a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1588a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1589068c80cfe0a280490353b6b007165d717c672eedEric Anholt   /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
1590068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *
1591068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    "Functions that accept no input arguments need not use void in the
1592068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    argument list because prototypes (or definitions) are required and
1593068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    therefore there is no ambiguity when an empty argument list "( )" is
1594068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    declared. The idiom "(void)" as a parameter list is provided for
1595068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    convenience."
1596068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *
1597068c80cfe0a280490353b6b007165d717c672eedEric Anholt    * Placing this check here prevents a void parameter being set up
1598068c80cfe0a280490353b6b007165d717c672eedEric Anholt    * for a function, which avoids tripping up checks for main taking
1599068c80cfe0a280490353b6b007165d717c672eedEric Anholt    * parameters and lookups of an unnamed symbol.
1600068c80cfe0a280490353b6b007165d717c672eedEric Anholt    */
1601068c80cfe0a280490353b6b007165d717c672eedEric Anholt   if (type->is_void() && (this->identifier == NULL))
1602068c80cfe0a280490353b6b007165d717c672eedEric Anholt      return NULL;
1603068c80cfe0a280490353b6b007165d717c672eedEric Anholt
160418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   ir_variable *var = new ir_variable(type, this->identifier);
1605a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1606a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* FINISHME: Handle array declarations.  Note that this requires
1607a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * FINISHME: complete handling of constant expressions.
1608a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1609a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1610cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick   /* Apply any specified qualifiers to the parameter declaration.  Note that
1611cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick    * for function parameters the default mode is 'in'.
1612cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick    */
16132e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc);
1614cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick   if (var->mode == ir_var_auto)
1615cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick      var->mode = ir_var_in;
1616a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
16170044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick   instructions->push_tail(var);
1618a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1619a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Parameter declarations do not have r-values.
1620a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1621a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1622a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1623a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1624a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1625a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic void
1626a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickast_function_parameters_to_hir(struct simple_node *ast_parameters,
16270044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick			       exec_list *ir_parameters,
1628a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			       struct _mesa_glsl_parse_state *state)
1629a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1630a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   struct simple_node *ptr;
1631a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1632a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   foreach (ptr, ast_parameters) {
1633068c80cfe0a280490353b6b007165d717c672eedEric Anholt      ast_node *param = (ast_node *)ptr;
1634068c80cfe0a280490353b6b007165d717c672eedEric Anholt      param->hir(ir_parameters, state);
1635068c80cfe0a280490353b6b007165d717c672eedEric Anholt
1636a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1637a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1638a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1639a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1640a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic bool
16410044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickparameter_lists_match(exec_list *list_a, exec_list *list_b)
1642a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
16430044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick   exec_list_iterator iter_a = list_a->iterator();
16440044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick   exec_list_iterator iter_b = list_b->iterator();
1645a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
16460044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick   while (iter_a.has_next()) {
1647a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* If all of the parameters from the other parameter list have been
1648a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * exhausted, the lists have different length and, by definition,
1649a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * do not match.
1650a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
16510044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick      if (!iter_b.has_next())
1652a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 return false;
1653a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1654a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* If the types of the parameters do not match, the parameters lists
1655a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * are different.
1656a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1657a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* FINISHME */
1658a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1659a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
16600044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick      iter_a.next();
16610044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick      iter_b.next();
1662a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1663a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1664a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return true;
1665a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1666a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1667a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1668fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
166992318a947958892497722772b03c643ebc943294Ian Romanickast_function::hir(exec_list *instructions,
167092318a947958892497722772b03c643ebc943294Ian Romanick		  struct _mesa_glsl_parse_state *state)
1671a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
167218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   ir_function *f = NULL;
167392318a947958892497722772b03c643ebc943294Ian Romanick   ir_function_signature *sig = NULL;
167492318a947958892497722772b03c643ebc943294Ian Romanick   exec_list hir_parameters;
1675a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1676a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
167792318a947958892497722772b03c643ebc943294Ian Romanick   /* The prototype part of a function does not generate anything in the IR
167892318a947958892497722772b03c643ebc943294Ian Romanick    * instruction stream.
167992318a947958892497722772b03c643ebc943294Ian Romanick    */
168092318a947958892497722772b03c643ebc943294Ian Romanick   (void) instructions;
168192318a947958892497722772b03c643ebc943294Ian Romanick
1682a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Convert the list of function parameters to HIR now so that they can be
1683a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * used below to compare this function's signature with previously seen
1684a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * signatures for functions with the same name.
1685a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
168692318a947958892497722772b03c643ebc943294Ian Romanick   ast_function_parameters_to_hir(& this->parameters, & hir_parameters, state);
1687a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1688e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick   const char *return_type_name;
1689e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick   const glsl_type *return_type =
169092318a947958892497722772b03c643ebc943294Ian Romanick      this->return_type->specifier->glsl_type(& return_type_name, state);
1691e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick
1692e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick   assert(return_type != NULL);
1693e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick
1694a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Verify that this function's signature either doesn't match a previously
1695a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * seen signature for a function with the same name, or, if a match is found,
1696a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * that the previously seen signature does not have an associated definition.
1697a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
169892318a947958892497722772b03c643ebc943294Ian Romanick   const char *const name = identifier;
16993359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick   f = state->symbols->get_function(name);
1700a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (f != NULL) {
170195cd6cc195f4652378d7ecf614c6e1c568311a04Ian Romanick      foreach_iter(exec_list_iterator, iter, *f) {
170292318a947958892497722772b03c643ebc943294Ian Romanick	 sig = (struct ir_function_signature *) iter.get();
1703a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1704a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 /* Compare the parameter list of the function being defined to the
1705a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  * existing function.  If the parameter lists match, then the return
1706a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  * type must also match and the existing function must not have a
1707a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  * definition.
1708a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  */
170992318a947958892497722772b03c643ebc943294Ian Romanick	 if (parameter_lists_match(& hir_parameters, & sig->parameters)) {
1710a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    /* FINISHME: Compare return types. */
1711a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
171292318a947958892497722772b03c643ebc943294Ian Romanick	    if (is_definition && (sig->definition != NULL)) {
171318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick	       YYLTYPE loc = this->get_location();
1714a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
17153359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	       _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
171692318a947958892497722772b03c643ebc943294Ian Romanick	       sig = NULL;
1717a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	       break;
1718a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    }
1719a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
1720a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
172192318a947958892497722772b03c643ebc943294Ian Romanick	 sig = NULL;
1722a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1723a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
17243359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick   } else if (state->symbols->name_declared_this_scope(name)) {
17253359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      /* This function name shadows a non-function use of the same name.
17263359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick       */
17273359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      YYLTYPE loc = this->get_location();
17283359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick
17293359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      _mesa_glsl_error(& loc, state, "function name `%s' conflicts with "
17303359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick		       "non-function", name);
173192318a947958892497722772b03c643ebc943294Ian Romanick      sig = NULL;
1732a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
1733882dad75408fc4071a9dd700309f9e54f6ad2650Ian Romanick      f = new ir_function(name);
17348bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      state->symbols->add_function(f->name, f);
1735a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1736a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1737ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   /* Verify the return type of main() */
1738ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   if (strcmp(name, "main") == 0) {
173925711a85c22bed305c9b52b89feb9c600d1892dfIan Romanick      if (! return_type->is_void()) {
1740ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt	 YYLTYPE loc = this->get_location();
1741ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt
1742ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt	 _mesa_glsl_error(& loc, state, "main() must return void");
1743ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt      }
1744174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt
174592318a947958892497722772b03c643ebc943294Ian Romanick      if (!hir_parameters.is_empty()) {
1746174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt	 YYLTYPE loc = this->get_location();
1747174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt
1748174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt	 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
1749174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt      }
1750ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   }
1751a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1752a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Finish storing the information about this new function in its signature.
1753a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
175492318a947958892497722772b03c643ebc943294Ian Romanick   if (sig == NULL) {
175592318a947958892497722772b03c643ebc943294Ian Romanick      sig = new ir_function_signature(return_type);
175692318a947958892497722772b03c643ebc943294Ian Romanick      f->add_signature(sig);
175792318a947958892497722772b03c643ebc943294Ian Romanick   } else if (is_definition) {
1758a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Destroy all of the previous parameter information.  The previous
1759a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * parameter information comes from the function prototype, and it can
1760a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * either include invalid parameter names or may not have names at all.
1761a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
176292318a947958892497722772b03c643ebc943294Ian Romanick      foreach_iter(exec_list_iterator, iter, sig->parameters) {
176344e1dfa2df4de3e2de963f0505cdadade6fe8180Kenneth Graunke	 assert(((ir_instruction *) iter.get())->as_variable() != NULL);
1764a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
17650044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick	 iter.remove();
17660044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick	 delete iter.get();
1767a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1768a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1769a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
177092318a947958892497722772b03c643ebc943294Ian Romanick   hir_parameters.move_nodes_to(& sig->parameters);
177192318a947958892497722772b03c643ebc943294Ian Romanick   signature = sig;
177292318a947958892497722772b03c643ebc943294Ian Romanick
177392318a947958892497722772b03c643ebc943294Ian Romanick   /* Function declarations (prototypes) do not have r-values.
177492318a947958892497722772b03c643ebc943294Ian Romanick    */
177592318a947958892497722772b03c643ebc943294Ian Romanick   return NULL;
177692318a947958892497722772b03c643ebc943294Ian Romanick}
177792318a947958892497722772b03c643ebc943294Ian Romanick
177892318a947958892497722772b03c643ebc943294Ian Romanick
177992318a947958892497722772b03c643ebc943294Ian Romanickir_rvalue *
178092318a947958892497722772b03c643ebc943294Ian Romanickast_function_definition::hir(exec_list *instructions,
178192318a947958892497722772b03c643ebc943294Ian Romanick			     struct _mesa_glsl_parse_state *state)
178292318a947958892497722772b03c643ebc943294Ian Romanick{
178392318a947958892497722772b03c643ebc943294Ian Romanick   prototype->is_definition = true;
178492318a947958892497722772b03c643ebc943294Ian Romanick   prototype->hir(instructions, state);
1785e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick
178692318a947958892497722772b03c643ebc943294Ian Romanick   ir_function_signature *signature = prototype->signature;
1787a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
178841ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   assert(state->current_function == NULL);
178941ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = signature;
179041ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick
179192318a947958892497722772b03c643ebc943294Ian Romanick   ir_label *label = new ir_label(signature->function_name());
1792a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (signature->definition == NULL) {
1793a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      signature->definition = label;
1794a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
17950044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick   instructions->push_tail(label);
1796a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1797e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick   /* Duplicate parameters declared in the prototype as concrete variables.
1798e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick    * Add these to the symbol table.
1799a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
18008bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick   state->symbols->push_scope();
1801e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick   foreach_iter(exec_list_iterator, iter, signature->parameters) {
1802e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick      ir_variable *const proto = ((ir_instruction *) iter.get())->as_variable();
1803e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick
1804e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick      assert(proto != NULL);
1805a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1806e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick      ir_variable *const var = proto->clone();
1807a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
18080044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick      instructions->push_tail(var);
1809a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
18103359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      /* The only way a parameter would "exist" is if two parameters have
18113359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick       * the same name.
18123359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick       */
18133359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      if (state->symbols->name_declared_this_scope(var->name)) {
18143359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 YYLTYPE loc = this->get_location();
18153359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick
18163359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
18173359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      } else {
18183359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 state->symbols->add_variable(var->name, var);
18193359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      }
1820a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1821a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1822a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Convert the body of the function to HIR, and append the resulting
1823a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * instructions to the list that currently consists of the function label
1824a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * and the function parameters.
1825a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
182618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   this->body->hir(instructions, state);
1827a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
18288bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick   state->symbols->pop_scope();
1829a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
183041ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   assert(state->current_function == signature);
183141ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = NULL;
1832a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1833a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Function definitions do not have r-values.
1834a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1835a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1836a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
183716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
183816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
1839fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
184016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanickast_jump_statement::hir(exec_list *instructions,
184116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick			struct _mesa_glsl_parse_state *state)
184216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick{
184316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
184416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   if (mode == ast_return) {
184516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      ir_return *inst;
1846aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt      assert(state->current_function);
184716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
184816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      if (opt_return_value) {
1849ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	 if (state->current_function->return_type->base_type ==
1850ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	     GLSL_TYPE_VOID) {
1851ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	    YYLTYPE loc = this->get_location();
1852ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt
1853ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	    _mesa_glsl_error(& loc, state,
1854ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt			     "`return` with a value, in function `%s' "
1855ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt			     "returning void",
1856ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt			     state->current_function->definition->label);
1857ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	 }
185816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
185916a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 ir_expression *const ret = (ir_expression *)
186016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	    opt_return_value->hir(instructions, state);
186116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 assert(ret != NULL);
186216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
186316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 /* FINISHME: Make sure the type of the return value matches the return
186416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	  * FINISHME: type of the enclosing function.
186516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	  */
186616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
186716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 inst = new ir_return(ret);
186816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      } else {
1869aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	 if (state->current_function->return_type->base_type !=
1870aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	     GLSL_TYPE_VOID) {
1871aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	    YYLTYPE loc = this->get_location();
1872aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt
1873aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	    _mesa_glsl_error(& loc, state,
1874aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt			     "`return' with no value, in function %s returning "
1875aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt			     "non-void",
1876aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt			     state->current_function->definition->label);
1877aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	 }
187816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 inst = new ir_return;
187916a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      }
188016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
188116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      instructions->push_tail(inst);
188216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   }
188316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
1884b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt   if (mode == ast_discard) {
1885b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt      /* FINISHME: discard support */
1886b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt      if (state->target != fragment_shader) {
1887b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt	 YYLTYPE loc = this->get_location();
1888b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt
1889b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt	 _mesa_glsl_error(& loc, state,
1890b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt			  "`discard' may only appear in a fragment shader");
1891b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt      }
1892b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt   }
1893b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt
189416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   /* Jump instructions do not have r-values.
189516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick    */
189616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   return NULL;
189716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick}
18983c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
18993c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
19003c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanickir_rvalue *
19013c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanickast_selection_statement::hir(exec_list *instructions,
19023c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick			     struct _mesa_glsl_parse_state *state)
19033c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick{
19043c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   ir_rvalue *const condition = this->condition->hir(instructions, state);
19053c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
19063c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
19073c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *
19083c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    "Any expression whose type evaluates to a Boolean can be used as the
19093c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    conditional expression bool-expression. Vector types are not accepted
19103c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    as the expression to if."
19113c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *
19123c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    * The checks are separated so that higher quality diagnostics can be
19133c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    * generated for cases where both rules are violated.
19143c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    */
19153c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
19163c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      YYLTYPE loc = this->condition->get_location();
19173c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
19183c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
19193c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick		       "boolean");
19203c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   }
19213c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
19223c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   ir_if *const stmt = new ir_if(condition);
19233c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
19243c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   if (then_statement != NULL) {
19253c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      ast_node *node = (ast_node *) then_statement;
19263c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      do {
19273c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick	 node->hir(& stmt->then_instructions, state);
19283c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick	 node = (ast_node *) node->next;
19293c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      } while (node != then_statement);
19303c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   }
19313c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
19323c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   if (else_statement != NULL) {
19333c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      ast_node *node = (ast_node *) else_statement;
19343c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      do {
19353c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick	 node->hir(& stmt->else_instructions, state);
19363c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick	 node = (ast_node *) node->next;
19373c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      } while (node != else_statement);
19383c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   }
19393c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
19403c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   instructions->push_tail(stmt);
19413c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
19423c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   /* if-statements do not have r-values.
19433c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    */
19443c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   return NULL;
19453c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick}
1946