ast_to_hir.cpp revision 6fae1e4c4d33769e2f255d50907b5aa0ab80edd4
1a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick/* 2a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * Copyright © 2010 Intel Corporation 3a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * 4a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * Permission is hereby granted, free of charge, to any person obtaining a 5a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * copy of this software and associated documentation files (the "Software"), 6a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * to deal in the Software without restriction, including without limitation 7a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * and/or sell copies of the Software, and to permit persons to whom the 9a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * Software is furnished to do so, subject to the following conditions: 10a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * 11a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * The above copyright notice and this permission notice (including the next 12a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * paragraph) shall be included in all copies or substantial portions of the 13a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * Software. 14a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * 15a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * DEALINGS IN THE SOFTWARE. 22a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 23a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 24a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick/** 25a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * \file ast_to_hir.c 26a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * Convert abstract syntax to to high-level intermediate reprensentation (HIR). 27a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * 28a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * During the conversion to HIR, the majority of the symantic checking is 29a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * preformed on the program. This includes: 30a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * 31a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * * Symbol table management 32a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * * Type checking 33a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * * Function binding 34a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * 35a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * The majority of this work could be done during parsing, and the parser could 36a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * probably generate HIR directly. However, this results in frequent changes 37a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * to the parser code. Since we do not assume that every system this complier 38a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * is built on will have Flex and Bison installed, we have to store the code 39a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * generated by these tools in our version control system. In other parts of 40a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * the system we've seen problems where a parser was changed but the generated 41a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * code was not committed, merge conflicts where created because two developers 42a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * had slightly different versions of Bison installed, etc. 43a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * 44a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * I have also noticed that running Bison generated parsers in GDB is very 45a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * irritating. When you get a segfault on '$$ = $1->foo', you can't very 46a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * well 'print $1' in GDB. 47a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * 48a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * As a result, my preference is to put as little C code as possible in the 49a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * parser (and lexer) sources. 50a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 51ac95f2f8c88d39aaa878f61172d9748af13e2c80Eric Anholt 52bfd7c9ac228c7ed8aec04c3b3aa33f40ee00b035Chia-I Wu#include "main/core.h" /* for struct gl_extensions */ 538bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick#include "glsl_symbol_table.h" 54a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick#include "glsl_parser_extras.h" 55a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick#include "ast.h" 56a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick#include "glsl_types.h" 57a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick#include "ir.h" 58a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 59d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanickvoid 60d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick_mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state) 61d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick{ 62adfb0cd7401251bef0c854ac945fce78f0ed11dbIan Romanick _mesa_glsl_initialize_variables(instructions, state); 63c22c40015db32b68b33c4944b9d94bf499135ec5Eric Anholt _mesa_glsl_initialize_functions(instructions, state); 64adfb0cd7401251bef0c854ac945fce78f0ed11dbIan Romanick 65814c89abdbcd5b841b98746af921796df0362238Kenneth Graunke state->symbols->language_version = state->language_version; 66814c89abdbcd5b841b98746af921796df0362238Kenneth Graunke 6741ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick state->current_function = NULL; 6841ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick 69a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke /* Section 4.2 of the GLSL 1.20 specification states: 70a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke * "The built-in functions are scoped in a scope outside the global scope 71a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke * users declare global variables in. That is, a shader's global scope, 72a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke * available for user-defined functions and global variables, is nested 73a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke * inside the scope containing the built-in functions." 74a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke * 75a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke * Since built-in functions like ftransform() access built-in variables, 76a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke * it follows that those must be in the outer scope as well. 77a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke * 78a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke * We push scope here to create this nesting effect...but don't pop. 79a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke * This way, a shader's globals are still in the symbol table for use 80a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke * by the linker. 81a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke */ 82a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke state->symbols->push_scope(); 83a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke 842b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick foreach_list_typed (ast_node, ast, link, & state->translation_unit) 85304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick ast->hir(instructions, state); 86d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick} 87d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick 88d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick 890104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick/** 900104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * If a conversion is available, convert one operand to a different type 910104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * 920104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * The \c from \c ir_rvalue is converted "in place". 930104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * 940104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param to Type that the operand it to be converted to 950104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param from Operand that is being converted 960104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param state GLSL compiler state 970104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * 980104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \return 990104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * If a conversion is possible (or unnecessary), \c true is returned. 1000104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * Otherwise \c false is returned. 1010104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick */ 102f32d3df8ab2b7c6c746f46870edc4b284cea50caKenneth Graunkebool 103bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickapply_implicit_conversion(const glsl_type *to, ir_rvalue * &from, 1040104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick struct _mesa_glsl_parse_state *state) 1050104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick{ 106953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke void *ctx = state; 107bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick if (to->base_type == from->type->base_type) 1080104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick return true; 1090104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick 1100104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick /* This conversion was added in GLSL 1.20. If the compilation mode is 1110104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * GLSL 1.10, the conversion is skipped. 1120104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick */ 1130104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick if (state->language_version < 120) 1140104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick return false; 1150104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick 1160104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec: 1170104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * 1180104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * "There are no implicit array or structure conversions. For 1190104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * example, an array of int cannot be implicitly converted to an 1200104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * array of float. There are no implicit conversions between 1210104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * signed and unsigned integers." 1220104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick */ 1230104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick /* FINISHME: The above comment is partially a lie. There is int/uint 1240104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * FINISHME: conversion for immediate constants. 1250104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick */ 126bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick if (!to->is_float() || !from->type->is_numeric()) 1270104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick return false; 1280104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick 129506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke /* Convert to a floating point type with the same number of components 130506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke * as the original type - i.e. int to float, not int to vec4. 131506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke */ 132506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke to = glsl_type::get_instance(GLSL_TYPE_FLOAT, from->type->vector_elements, 133506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke from->type->matrix_columns); 134506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke 135bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick switch (from->type->base_type) { 1360104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick case GLSL_TYPE_INT: 1371660a2954797e056caba319c5d6c70b0d4be22feCarl Worth from = new(ctx) ir_expression(ir_unop_i2f, to, from, NULL); 1380104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick break; 1390104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick case GLSL_TYPE_UINT: 1401660a2954797e056caba319c5d6c70b0d4be22feCarl Worth from = new(ctx) ir_expression(ir_unop_u2f, to, from, NULL); 1410104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick break; 1420104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick case GLSL_TYPE_BOOL: 1431660a2954797e056caba319c5d6c70b0d4be22feCarl Worth from = new(ctx) ir_expression(ir_unop_b2f, to, from, NULL); 144dc58b3f8ccd817fdee390a3df5b8e0fb29d5397cEric Anholt break; 1450104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick default: 1460104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick assert(0); 1470104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick } 1480104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick 1490104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick return true; 1500104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick} 1510104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick 1520104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick 153a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type * 154bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickarithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b, 155a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick bool multiply, 156a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt struct _mesa_glsl_parse_state *state, YYLTYPE *loc) 157a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{ 158336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt const glsl_type *type_a = value_a->type; 159336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt const glsl_type *type_b = value_b->type; 1600104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick 161a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* From GLSL 1.50 spec, page 56: 162a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * 163a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * "The arithmetic binary operators add (+), subtract (-), 164a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * multiply (*), and divide (/) operate on integer and 165a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * floating-point scalars, vectors, and matrices." 166a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 16760b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick if (!type_a->is_numeric() || !type_b->is_numeric()) { 168a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt _mesa_glsl_error(loc, state, 169a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt "Operands to arithmetic operators must be numeric"); 1700471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick return glsl_type::error_type; 171a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 172a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 173a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 174a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* "If one operand is floating-point based and the other is 175a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * not, then the conversions from Section 4.1.10 "Implicit 176a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * Conversions" are applied to the non-floating-point-based operand." 177a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 1780104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick if (!apply_implicit_conversion(type_a, value_b, state) 1790104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick && !apply_implicit_conversion(type_b, value_a, state)) { 180a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt _mesa_glsl_error(loc, state, 181a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt "Could not implicitly convert operands to " 182a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt "arithmetic operator"); 1830104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick return glsl_type::error_type; 184a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 185336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt type_a = value_a->type; 186336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt type_b = value_b->type; 187336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt 188a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* "If the operands are integer types, they must both be signed or 189a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * both be unsigned." 190a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * 191a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * From this rule and the preceeding conversion it can be inferred that 192a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT. 19360b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick * The is_numeric check above already filtered out the case where either 19460b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick * type is not one of these, so now the base types need only be tested for 19560b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick * equality. 196a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 197a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick if (type_a->base_type != type_b->base_type) { 198a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt _mesa_glsl_error(loc, state, 199a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt "base type mismatch for arithmetic operator"); 2000471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick return glsl_type::error_type; 201a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 202a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 203a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* "All arithmetic binary operators result in the same fundamental type 204a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * (signed integer, unsigned integer, or floating-point) as the 205a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * operands they operate on, after operand type conversion. After 206a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * conversion, the following cases are valid 207a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * 208a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * * The two operands are scalars. In this case the operation is 209a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * applied, resulting in a scalar." 210a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 211cb36f8aaeeb09660843316270a781948f773d90bIan Romanick if (type_a->is_scalar() && type_b->is_scalar()) 212a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick return type_a; 213a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 214a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* "* One operand is a scalar, and the other is a vector or matrix. 215a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * In this case, the scalar operation is applied independently to each 216a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * component of the vector or matrix, resulting in the same size 217a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * vector or matrix." 218a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 219cb36f8aaeeb09660843316270a781948f773d90bIan Romanick if (type_a->is_scalar()) { 220cb36f8aaeeb09660843316270a781948f773d90bIan Romanick if (!type_b->is_scalar()) 221a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick return type_b; 222cb36f8aaeeb09660843316270a781948f773d90bIan Romanick } else if (type_b->is_scalar()) { 223a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick return type_a; 224a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 225a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 226a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* All of the combinations of <scalar, scalar>, <vector, scalar>, 227a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been 228a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * handled. 229a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 23060b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick assert(!type_a->is_scalar()); 23160b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick assert(!type_b->is_scalar()); 232a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 233a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* "* The two operands are vectors of the same size. In this case, the 234a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * operation is done component-wise resulting in the same size 235a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * vector." 236a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 237a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick if (type_a->is_vector() && type_b->is_vector()) { 238a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt if (type_a == type_b) { 239a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt return type_a; 240a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt } else { 241a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt _mesa_glsl_error(loc, state, 242a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt "vector size mismatch for arithmetic operator"); 243a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt return glsl_type::error_type; 244a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt } 245a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 246a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 247a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* All of the combinations of <scalar, scalar>, <vector, scalar>, 248a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and 249a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * <vector, vector> have been handled. At least one of the operands must 250a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * be matrix. Further, since there are no integer matrix types, the base 251a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * type of both operands must be float. 252a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 25360b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick assert(type_a->is_matrix() || type_b->is_matrix()); 254a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick assert(type_a->base_type == GLSL_TYPE_FLOAT); 255a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick assert(type_b->base_type == GLSL_TYPE_FLOAT); 256a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 257a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* "* The operator is add (+), subtract (-), or divide (/), and the 258a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * operands are matrices with the same number of rows and the same 259a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * number of columns. In this case, the operation is done component- 260a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * wise resulting in the same size matrix." 261a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * * The operator is multiply (*), where both operands are matrices or 262a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * one operand is a vector and the other a matrix. A right vector 263a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * operand is treated as a column vector and a left vector operand as a 264a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * row vector. In all these cases, it is required that the number of 265a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * columns of the left operand is equal to the number of rows of the 266a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * right operand. Then, the multiply (*) operation does a linear 267a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * algebraic multiply, yielding an object that has the same number of 268a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * rows as the left operand and the same number of columns as the right 269a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * operand. Section 5.10 "Vector and Matrix Operations" explains in 270a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * more detail how vectors and matrices are operated on." 271a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 272a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick if (! multiply) { 273a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt if (type_a == type_b) 274a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt return type_a; 275a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } else { 276fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick if (type_a->is_matrix() && type_b->is_matrix()) { 277c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick /* Matrix multiply. The columns of A must match the rows of B. Given 278c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick * the other previously tested constraints, this means the vector type 279c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick * of a row from A must be the same as the vector type of a column from 280c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick * B. 281c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick */ 282c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick if (type_a->row_type() == type_b->column_type()) { 283c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick /* The resulting matrix has the number of columns of matrix B and 284c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick * the number of rows of matrix A. We get the row count of A by 285c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick * looking at the size of a vector that makes up a column. The 286c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick * transpose (size of a row) is done for B. 287c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick */ 288a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt const glsl_type *const type = 289c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick glsl_type::get_instance(type_a->base_type, 290c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick type_a->column_type()->vector_elements, 291c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick type_b->row_type()->vector_elements); 292a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt assert(type != glsl_type::error_type); 293a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt 294a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt return type; 295a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 296fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick } else if (type_a->is_matrix()) { 297a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* A is a matrix and B is a column vector. Columns of A must match 298c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick * rows of B. Given the other previously tested constraints, this 299c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick * means the vector type of a row from A must be the same as the 300c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick * vector the type of B. 301a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 30247c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth if (type_a->row_type() == type_b) { 30347c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth /* The resulting vector has a number of elements equal to 30447c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth * the number of rows of matrix A. */ 30547c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth const glsl_type *const type = 30647c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth glsl_type::get_instance(type_a->base_type, 30747c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth type_a->column_type()->vector_elements, 30847c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth 1); 30947c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth assert(type != glsl_type::error_type); 31047c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth 31147c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth return type; 31247c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth } 313a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } else { 314fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick assert(type_b->is_matrix()); 315a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 316c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick /* A is a row vector and B is a matrix. Columns of A must match rows 317c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick * of B. Given the other previously tested constraints, this means 318c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick * the type of A must be the same as the vector type of a column from 319c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick * B. 320a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 32147c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth if (type_a == type_b->column_type()) { 32247c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth /* The resulting vector has a number of elements equal to 32347c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth * the number of columns of matrix B. */ 32447c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth const glsl_type *const type = 32547c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth glsl_type::get_instance(type_a->base_type, 32647c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth type_b->row_type()->vector_elements, 32747c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth 1); 32847c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth assert(type != glsl_type::error_type); 32947c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth 33047c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth return type; 33147c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth } 332a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 333a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt 334a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication"); 335a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt return glsl_type::error_type; 336a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 337a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 338a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 339a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* "All other cases are illegal." 340a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 341a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt _mesa_glsl_error(loc, state, "type mismatch"); 3420471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick return glsl_type::error_type; 343a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick} 344a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 345a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 346a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type * 34765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholtunary_arithmetic_result_type(const struct glsl_type *type, 34865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt struct _mesa_glsl_parse_state *state, YYLTYPE *loc) 349a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{ 350a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* From GLSL 1.50 spec, page 57: 351a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * 352a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * "The arithmetic unary operators negate (-), post- and pre-increment 353a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * and decrement (-- and ++) operate on integer or floating-point 354a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * values (including vectors and matrices). All unary operators work 355a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * component-wise on their operands. These result with the same type 356a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * they operated on." 357a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 35865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt if (!type->is_numeric()) { 35965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt _mesa_glsl_error(loc, state, 36065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt "Operands to arithmetic operators must be numeric"); 3610471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick return glsl_type::error_type; 36265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt } 363a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 364a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick return type; 365a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick} 366a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 367cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace/** 368cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * \brief Return the result type of a bit-logic operation. 369cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * 370cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * If the given types to the bit-logic operator are invalid, return 371cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * glsl_type::error_type. 372cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * 373cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * \param type_a Type of LHS of bit-logic op 374cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * \param type_b Type of RHS of bit-logic op 375cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace */ 376cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versacestatic const struct glsl_type * 377cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versacebit_logic_result_type(const struct glsl_type *type_a, 378cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace const struct glsl_type *type_b, 379cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace ast_operators op, 380cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace struct _mesa_glsl_parse_state *state, YYLTYPE *loc) 381cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace{ 382cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace if (state->language_version < 130) { 383cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace _mesa_glsl_error(loc, state, "bit operations require GLSL 1.30"); 384cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace return glsl_type::error_type; 385cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace } 386cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace 387cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace /* From page 50 (page 56 of PDF) of GLSL 1.30 spec: 388cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * 389cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * "The bitwise operators and (&), exclusive-or (^), and inclusive-or 390cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * (|). The operands must be of type signed or unsigned integers or 391cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * integer vectors." 392cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace */ 393cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace if (!type_a->is_integer()) { 394cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace _mesa_glsl_error(loc, state, "LHS of `%s' must be an integer", 395cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace ast_expression::operator_string(op)); 396cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace return glsl_type::error_type; 397cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace } 398cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace if (!type_b->is_integer()) { 399cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace _mesa_glsl_error(loc, state, "RHS of `%s' must be an integer", 400cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace ast_expression::operator_string(op)); 401cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace return glsl_type::error_type; 402cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace } 403cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace 404cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace /* "The fundamental types of the operands (signed or unsigned) must 405cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * match," 406cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace */ 407cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace if (type_a->base_type != type_b->base_type) { 408cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace _mesa_glsl_error(loc, state, "operands of `%s' must have the same " 409cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace "base type", ast_expression::operator_string(op)); 410cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace return glsl_type::error_type; 411cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace } 412cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace 413cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace /* "The operands cannot be vectors of differing size." */ 414cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace if (type_a->is_vector() && 415cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace type_b->is_vector() && 416cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace type_a->vector_elements != type_b->vector_elements) { 417cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace _mesa_glsl_error(loc, state, "operands of `%s' cannot be vectors of " 418cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace "different sizes", ast_expression::operator_string(op)); 419cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace return glsl_type::error_type; 420cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace } 421cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace 422cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace /* "If one operand is a scalar and the other a vector, the scalar is 423cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * applied component-wise to the vector, resulting in the same type as 424cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * the vector. The fundamental types of the operands [...] will be the 425cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * resulting fundamental type." 426cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace */ 427cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace if (type_a->is_scalar()) 428cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace return type_b; 429cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace else 430cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace return type_a; 431cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace} 432a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 433a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type * 434a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickmodulus_result_type(const struct glsl_type *type_a, 43565e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt const struct glsl_type *type_b, 43665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt struct _mesa_glsl_parse_state *state, YYLTYPE *loc) 437a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{ 438a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* From GLSL 1.50 spec, page 56: 439a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * "The operator modulus (%) operates on signed or unsigned integers or 440a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * integer vectors. The operand types must both be signed or both be 441a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * unsigned." 442a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 44340176e249f72b6090204611873b19aed3da67c71Ian Romanick if (!type_a->is_integer() || !type_b->is_integer() 444a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick || (type_a->base_type != type_b->base_type)) { 44565e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt _mesa_glsl_error(loc, state, "type mismatch"); 4460471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick return glsl_type::error_type; 447a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 448a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 449a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* "The operands cannot be vectors of differing size. If one operand is 450a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * a scalar and the other vector, then the scalar is applied component- 451a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * wise to the vector, resulting in the same type as the vector. If both 452a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * are vectors of the same size, the result is computed component-wise." 453a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 454a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick if (type_a->is_vector()) { 455a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick if (!type_b->is_vector() 456a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick || (type_a->vector_elements == type_b->vector_elements)) 457a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick return type_a; 458a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } else 459a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick return type_b; 460a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 461a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* "The operator modulus (%) is not defined for any other data types 462a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * (non-integer types)." 463a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 46465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt _mesa_glsl_error(loc, state, "type mismatch"); 4650471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick return glsl_type::error_type; 466a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick} 467a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 468a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 469a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type * 470bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickrelational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b, 47165e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt struct _mesa_glsl_parse_state *state, YYLTYPE *loc) 472a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{ 473336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt const glsl_type *type_a = value_a->type; 474336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt const glsl_type *type_b = value_b->type; 4750150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick 476a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* From GLSL 1.50 spec, page 56: 477a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * "The relational operators greater than (>), less than (<), greater 478a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * than or equal (>=), and less than or equal (<=) operate only on 479a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * scalar integer and scalar floating-point expressions." 480a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 481a6d653dcbbee3158f9ea7b284bdeb1a8432f0fcbIan Romanick if (!type_a->is_numeric() 482a6d653dcbbee3158f9ea7b284bdeb1a8432f0fcbIan Romanick || !type_b->is_numeric() 483cb36f8aaeeb09660843316270a781948f773d90bIan Romanick || !type_a->is_scalar() 48465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt || !type_b->is_scalar()) { 48565e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt _mesa_glsl_error(loc, state, 48665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt "Operands to relational operators must be scalar and " 48765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt "numeric"); 4880471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick return glsl_type::error_type; 48965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt } 490a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 491a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* "Either the operands' types must match, or the conversions from 492a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * Section 4.1.10 "Implicit Conversions" will be applied to the integer 493a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * operand, after which the types must match." 494a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 4950150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick if (!apply_implicit_conversion(type_a, value_b, state) 4960150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick && !apply_implicit_conversion(type_b, value_a, state)) { 49765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt _mesa_glsl_error(loc, state, 49865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt "Could not implicitly convert operands to " 49965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt "relational operator"); 5000150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick return glsl_type::error_type; 501a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 502336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt type_a = value_a->type; 503336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt type_b = value_b->type; 504a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 50565e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt if (type_a->base_type != type_b->base_type) { 50665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt _mesa_glsl_error(loc, state, "base type mismatch"); 5070471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick return glsl_type::error_type; 50865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt } 509a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 510a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* "The result is scalar Boolean." 511a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 5120471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick return glsl_type::bool_type; 513a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick} 514a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 515c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace/** 516c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * \brief Return the result type of a bit-shift operation. 517c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * 518c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * If the given types to the bit-shift operator are invalid, return 519c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * glsl_type::error_type. 520c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * 521c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * \param type_a Type of LHS of bit-shift op 522c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * \param type_b Type of RHS of bit-shift op 523c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace */ 524c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versacestatic const struct glsl_type * 525c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versaceshift_result_type(const struct glsl_type *type_a, 526c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace const struct glsl_type *type_b, 527c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace ast_operators op, 528c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace struct _mesa_glsl_parse_state *state, YYLTYPE *loc) 529c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace{ 530c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace if (state->language_version < 130) { 531c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace _mesa_glsl_error(loc, state, "bit operations require GLSL 1.30"); 532c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace return glsl_type::error_type; 533c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace } 534c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace 535c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec: 536c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * 537c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * "The shift operators (<<) and (>>). For both operators, the operands 538c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * must be signed or unsigned integers or integer vectors. One operand 539c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * can be signed while the other is unsigned." 540c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace */ 541c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace if (!type_a->is_integer()) { 542c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace _mesa_glsl_error(loc, state, "LHS of operator %s must be an integer or " 543c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace "integer vector", ast_expression::operator_string(op)); 544c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace return glsl_type::error_type; 545c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace 546c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace } 547c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace if (!type_b->is_integer()) { 548c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace _mesa_glsl_error(loc, state, "RHS of operator %s must be an integer or " 549c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace "integer vector", ast_expression::operator_string(op)); 550c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace return glsl_type::error_type; 551c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace } 552c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace 553c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace /* "If the first operand is a scalar, the second operand has to be 554c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * a scalar as well." 555c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace */ 556c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace if (type_a->is_scalar() && !type_b->is_scalar()) { 557c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace _mesa_glsl_error(loc, state, "If the first operand of %s is scalar, the " 558c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace "second must be scalar as well", 559c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace ast_expression::operator_string(op)); 560c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace return glsl_type::error_type; 561c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace } 562c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace 563c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace /* If both operands are vectors, check that they have same number of 564c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * elements. 565c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace */ 566c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace if (type_a->is_vector() && 567c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace type_b->is_vector() && 568c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace type_a->vector_elements != type_b->vector_elements) { 569c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace _mesa_glsl_error(loc, state, "Vector operands to operator %s must " 570c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace "have same number of elements", 571c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace ast_expression::operator_string(op)); 572c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace return glsl_type::error_type; 573c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace } 574c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace 575c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace /* "In all cases, the resulting type will be the same type as the left 576c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * operand." 577c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace */ 578c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace return type_a; 579c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace} 580a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 5810bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick/** 5820bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Validates that a value can be assigned to a location with a specified type 5830bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * 5840bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Validates that \c rhs can be assigned to some location. If the types are 5850bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * not an exact match but an automatic conversion is possible, \c rhs will be 5860bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * converted. 5870bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * 5880bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \return 5890bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type. 5900bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Otherwise the actual RHS to be assigned will be returned. This may be 5910bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \c rhs, or it may be \c rhs after some type conversion. 5920bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * 5930bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \note 5940bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * In addition to being used for assignments, this function is used to 5950bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * type-check return values. 5960bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick */ 597fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue * 598336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholtvalidate_assignment(struct _mesa_glsl_parse_state *state, 599336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt const glsl_type *lhs_type, ir_rvalue *rhs) 6000bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick{ 601336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt const glsl_type *rhs_type = rhs->type; 6020bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick 6030bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick /* If there is already some error in the RHS, just return it. Anything 6040bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * else will lead to an avalanche of error message back to the user. 6050bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick */ 6060bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick if (rhs_type->is_error()) 6070bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick return rhs; 6080bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick 6090bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick /* If the types are identical, the assignment can trivially proceed. 6100bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick */ 6110bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick if (rhs_type == lhs_type) 6120bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick return rhs; 6130bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick 6140157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick /* If the array element types are the same and the size of the LHS is zero, 6150157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick * the assignment is okay. 6160157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick * 6170157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick * Note: Whole-array assignments are not permitted in GLSL 1.10, but this 6180157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick * is handled by ir_dereference::is_lvalue. 6190157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick */ 6200157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick if (lhs_type->is_array() && rhs->type->is_array() 6210157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick && (lhs_type->element_type() == rhs->type->element_type()) 6220157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick && (lhs_type->array_size() == 0)) { 6230157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick return rhs; 6240157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick } 6250157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick 626336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt /* Check for implicit conversion in GLSL 1.20 */ 627336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt if (apply_implicit_conversion(lhs_type, rhs, state)) { 628336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt rhs_type = rhs->type; 629336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt if (rhs_type == lhs_type) 630336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt return rhs; 631336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt } 632336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt 6330bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick return NULL; 6340bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick} 6350bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick 63610a685218610e737e23d2d8a243ed6ff6613becdEric Anholtir_rvalue * 63710a685218610e737e23d2d8a243ed6ff6613becdEric Anholtdo_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state, 63810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt ir_rvalue *lhs, ir_rvalue *rhs, 63910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt YYLTYPE lhs_loc) 64010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt{ 641953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke void *ctx = state; 64210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt bool error_emitted = (lhs->type->is_error() || rhs->type->is_error()); 64310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt 64410a685218610e737e23d2d8a243ed6ff6613becdEric Anholt if (!error_emitted) { 64510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt if (!lhs->is_lvalue()) { 64610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment"); 64710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt error_emitted = true; 64810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt } 64910eaa8bcbce1cd6d2e120e913f7abafde9675215Kenneth Graunke 65010eaa8bcbce1cd6d2e120e913f7abafde9675215Kenneth Graunke if (state->es_shader && lhs->type->is_array()) { 65110eaa8bcbce1cd6d2e120e913f7abafde9675215Kenneth Graunke _mesa_glsl_error(&lhs_loc, state, "whole array assignment is not " 65210eaa8bcbce1cd6d2e120e913f7abafde9675215Kenneth Graunke "allowed in GLSL ES 1.00."); 65310eaa8bcbce1cd6d2e120e913f7abafde9675215Kenneth Graunke error_emitted = true; 65410eaa8bcbce1cd6d2e120e913f7abafde9675215Kenneth Graunke } 65510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt } 65610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt 657336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt ir_rvalue *new_rhs = validate_assignment(state, lhs->type, rhs); 65810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt if (new_rhs == NULL) { 65910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt _mesa_glsl_error(& lhs_loc, state, "type mismatch"); 66010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt } else { 66110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt rhs = new_rhs; 6620157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick 6630157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick /* If the LHS array was not declared with a size, it takes it size from 6640157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick * the RHS. If the LHS is an l-value and a whole array, it must be a 6650157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick * dereference of a variable. Any other case would require that the LHS 6660157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick * is either not an l-value or not a whole array. 6670157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick */ 6680157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick if (lhs->type->array_size() == 0) { 6690157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick ir_dereference *const d = lhs->as_dereference(); 6700157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick 6710157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick assert(d != NULL); 6720157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick 67336ea28646c666ac2af9b43c47e65f9f53ffcc390Ian Romanick ir_variable *const var = d->variable_referenced(); 6740157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick 6750157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick assert(var != NULL); 6760157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick 67763f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick if (var->max_array_access >= unsigned(rhs->type->array_size())) { 67863f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick /* FINISHME: This should actually log the location of the RHS. */ 67963f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to " 68063f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick "previous access", 68163f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick var->max_array_access); 68263f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick } 68363f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick 684f38d15b80d4e4c8ecb7a76087cdc49835f0aa271Ian Romanick var->type = glsl_type::get_array_instance(lhs->type->element_type(), 6850157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick rhs->type->array_size()); 6869703ed05e684f4269cd8af27c94e9b6bf8781d85Eric Anholt d->type = var->type; 6870157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick } 68810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt } 68910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt 6902731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt /* Most callers of do_assignment (assign, add_assign, pre_inc/dec, 6912731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt * but not post_inc) need the converted assigned value as an rvalue 6922731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt * to handle things like: 6932731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt * 6942731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt * i = j += 1; 6952731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt * 6962731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt * So we always just store the computed value being assigned to a 6972731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt * temporary and return a deref of that temporary. If the rvalue 6982731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt * ends up not being used, the temp will get copy-propagated out. 6992731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt */ 7007e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp", 7017e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick ir_var_temporary); 702e33c10328caec29616a5433b1d1df9088f3a84dfEric Anholt ir_dereference_variable *deref_var = new(ctx) ir_dereference_variable(var); 703ae805922b7e3cdaf3aee26c3b799fe3608669bbaEric Anholt instructions->push_tail(var); 704e33c10328caec29616a5433b1d1df9088f3a84dfEric Anholt instructions->push_tail(new(ctx) ir_assignment(deref_var, 7051660a2954797e056caba319c5d6c70b0d4be22feCarl Worth rhs, 7061660a2954797e056caba319c5d6c70b0d4be22feCarl Worth NULL)); 707e33c10328caec29616a5433b1d1df9088f3a84dfEric Anholt deref_var = new(ctx) ir_dereference_variable(var); 7082731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt 7098e9ce2eb56a087c2544112700ae1abe3f96648ddIan Romanick if (!error_emitted) 7108e9ce2eb56a087c2544112700ae1abe3f96648ddIan Romanick instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var, NULL)); 71110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt 7121660a2954797e056caba319c5d6c70b0d4be22feCarl Worth return new(ctx) ir_dereference_variable(var); 71310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt} 7140bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick 715de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholtstatic ir_rvalue * 716959a9ecdd8fbc3375e4149f2b44d253622ff12eeEric Anholtget_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue) 717de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt{ 7181660a2954797e056caba319c5d6c70b0d4be22feCarl Worth void *ctx = talloc_parent(lvalue); 719de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt ir_variable *var; 720de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt 7217e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp", 7227e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick ir_var_temporary); 72343b5b03d67ce890e867c81d4a5cfc4871d711d43Eric Anholt instructions->push_tail(var); 724de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt var->mode = ir_var_auto; 725de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt 7261660a2954797e056caba319c5d6c70b0d4be22feCarl Worth instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var), 7271660a2954797e056caba319c5d6c70b0d4be22feCarl Worth lvalue, NULL)); 728de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt 729de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt /* Once we've created this temporary, mark it read only so it's no 730de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt * longer considered an lvalue. 731de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt */ 732de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt var->read_only = true; 733de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt 7341660a2954797e056caba319c5d6c70b0d4be22feCarl Worth return new(ctx) ir_dereference_variable(var); 735de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt} 736de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt 737de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt 738fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue * 7390044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_node::hir(exec_list *instructions, 74018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick struct _mesa_glsl_parse_state *state) 74118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick{ 74218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick (void) instructions; 74318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick (void) state; 74418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick 74518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick return NULL; 74618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick} 74718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick 748b4f585665c31b1f80d909e38b3b2a9fab0c03076Eric Anholtstatic void 749b4f585665c31b1f80d909e38b3b2a9fab0c03076Eric Anholtmark_whole_array_access(ir_rvalue *access) 750b4f585665c31b1f80d909e38b3b2a9fab0c03076Eric Anholt{ 751b4f585665c31b1f80d909e38b3b2a9fab0c03076Eric Anholt ir_dereference_variable *deref = access->as_dereference_variable(); 752b4f585665c31b1f80d909e38b3b2a9fab0c03076Eric Anholt 753b4f585665c31b1f80d909e38b3b2a9fab0c03076Eric Anholt if (deref) { 754b4f585665c31b1f80d909e38b3b2a9fab0c03076Eric Anholt deref->var->max_array_access = deref->type->length - 1; 755b4f585665c31b1f80d909e38b3b2a9fab0c03076Eric Anholt } 756b4f585665c31b1f80d909e38b3b2a9fab0c03076Eric Anholt} 757b4f585665c31b1f80d909e38b3b2a9fab0c03076Eric Anholt 758ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholtstatic ir_rvalue * 759ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholtdo_comparison(void *mem_ctx, int operation, ir_rvalue *op0, ir_rvalue *op1) 760ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt{ 761ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt int join_op; 762ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt 763ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt if (operation == ir_binop_all_equal) 764ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt join_op = ir_binop_logic_and; 765ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt else 766ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt join_op = ir_binop_logic_or; 767ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt 768ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt switch (op0->type->base_type) { 769ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt case GLSL_TYPE_FLOAT: 770ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt case GLSL_TYPE_UINT: 771ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt case GLSL_TYPE_INT: 772ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt case GLSL_TYPE_BOOL: 773ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt return new(mem_ctx) ir_expression(operation, op0, op1); 774ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt 775ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt case GLSL_TYPE_ARRAY: { 776ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt ir_rvalue *last = NULL; 777ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt 778ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt for (unsigned int i = 0; i < op0->type->length; i++) { 779ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt ir_rvalue *e0, *e1, *result; 780ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt 781ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt e0 = new(mem_ctx) ir_dereference_array(op0->clone(mem_ctx, NULL), 782ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt new(mem_ctx) ir_constant(i)); 783ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt e1 = new(mem_ctx) ir_dereference_array(op1->clone(mem_ctx, NULL), 784ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt new(mem_ctx) ir_constant(i)); 785ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt result = do_comparison(mem_ctx, operation, e0, e1); 786ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt 787ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt if (last) { 788ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt last = new(mem_ctx) ir_expression(join_op, last, result); 789ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt } else { 790ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt last = result; 791ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt } 792ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt } 793b4f585665c31b1f80d909e38b3b2a9fab0c03076Eric Anholt 794b4f585665c31b1f80d909e38b3b2a9fab0c03076Eric Anholt mark_whole_array_access(op0); 795b4f585665c31b1f80d909e38b3b2a9fab0c03076Eric Anholt mark_whole_array_access(op1); 796b4f585665c31b1f80d909e38b3b2a9fab0c03076Eric Anholt 797ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt return last; 798ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt } 799ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt 800ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt case GLSL_TYPE_STRUCT: { 801ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt ir_rvalue *last = NULL; 802ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt 803ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt for (unsigned int i = 0; i < op0->type->length; i++) { 804ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt ir_rvalue *e0, *e1, *result; 805ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt const char *field_name = op0->type->fields.structure[i].name; 806ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt 807ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt e0 = new(mem_ctx) ir_dereference_record(op0->clone(mem_ctx, NULL), 808ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt field_name); 809ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt e1 = new(mem_ctx) ir_dereference_record(op1->clone(mem_ctx, NULL), 810ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt field_name); 811ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt result = do_comparison(mem_ctx, operation, e0, e1); 812ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt 813ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt if (last) { 814ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt last = new(mem_ctx) ir_expression(join_op, last, result); 815ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt } else { 816ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt last = result; 817ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt } 818ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt } 819ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt return last; 820ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt } 821ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt 822ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt case GLSL_TYPE_ERROR: 823ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt case GLSL_TYPE_VOID: 824ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt case GLSL_TYPE_SAMPLER: 825ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt /* I assume a comparison of a struct containing a sampler just 826ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt * ignores the sampler present in the type. 827ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt */ 828ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt return new(mem_ctx) ir_constant(true); 829ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt } 830d56c97413ee65e40e3544b89ffca450df9ba1c06Eric Anholt 831d56c97413ee65e40e3544b89ffca450df9ba1c06Eric Anholt return NULL; 832ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt} 83318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick 834fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue * 8350044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_expression::hir(exec_list *instructions, 83618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick struct _mesa_glsl_parse_state *state) 837a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{ 838953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke void *ctx = state; 839a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick static const int operations[AST_NUM_OPERATORS] = { 840a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick -1, /* ast_assign doesn't convert to ir_expression. */ 841a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick -1, /* ast_plus doesn't convert to ir_expression. */ 842a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_unop_neg, 843a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_add, 844a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_sub, 845a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_mul, 846a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_div, 847a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_mod, 848a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_lshift, 849a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_rshift, 850a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_less, 851a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_greater, 852a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_lequal, 853a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_gequal, 8544dfb89904c0a3d2166e9a3fc0253a254680e91bcLuca Barbieri ir_binop_all_equal, 8554dfb89904c0a3d2166e9a3fc0253a254680e91bcLuca Barbieri ir_binop_any_nequal, 856a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_bit_and, 857a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_bit_xor, 858a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_bit_or, 859a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_unop_bit_not, 860a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_logic_and, 861a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_logic_xor, 862a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_logic_or, 863a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_unop_logic_not, 864a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 865a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* Note: The following block of expression types actually convert 866a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * to multiple IR instructions. 867a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 868a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_mul, /* ast_mul_assign */ 869a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_div, /* ast_div_assign */ 870a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_mod, /* ast_mod_assign */ 871a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_add, /* ast_add_assign */ 872a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_sub, /* ast_sub_assign */ 873a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_lshift, /* ast_ls_assign */ 874a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_rshift, /* ast_rs_assign */ 875a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_bit_and, /* ast_and_assign */ 876a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_bit_xor, /* ast_xor_assign */ 877a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_bit_or, /* ast_or_assign */ 878a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 879a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick -1, /* ast_conditional doesn't convert to ir_expression. */ 880de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt ir_binop_add, /* ast_pre_inc. */ 881de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt ir_binop_sub, /* ast_pre_dec. */ 882de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt ir_binop_add, /* ast_post_inc. */ 883de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt ir_binop_sub, /* ast_post_dec. */ 884a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick -1, /* ast_field_selection doesn't conv to ir_expression. */ 885a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick -1, /* ast_array_index doesn't convert to ir_expression. */ 886a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick -1, /* ast_function_call doesn't conv to ir_expression. */ 887a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick -1, /* ast_identifier doesn't convert to ir_expression. */ 888a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick -1, /* ast_int_constant doesn't convert to ir_expression. */ 889a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick -1, /* ast_uint_constant doesn't conv to ir_expression. */ 890a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick -1, /* ast_float_constant doesn't conv to ir_expression. */ 891a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick -1, /* ast_bool_constant doesn't conv to ir_expression. */ 892a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick -1, /* ast_sequence doesn't convert to ir_expression. */ 893a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick }; 894fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke ir_rvalue *result = NULL; 8951c325af4d6b907e0a47ab7f868a2a78f054f153fAras Pranckevicius ir_rvalue *op[3]; 8960471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick const struct glsl_type *type = glsl_type::error_type; 897a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick bool error_emitted = false; 898a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick YYLTYPE loc; 899a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 90018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick loc = this->get_location(); 901a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 90218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick switch (this->oper) { 9036652af36fe8994b1621d882fcc230d320908a2a3Ian Romanick case ast_assign: { 90418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick op[0] = this->subexpressions[0]->hir(instructions, state); 90518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick op[1] = this->subexpressions[1]->hir(instructions, state); 906a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 90710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt result = do_assignment(instructions, state, op[0], op[1], 90810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt this->subexpressions[0]->get_location()); 90910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt error_emitted = result->type->is_error(); 91010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt type = result->type; 911a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 9126652af36fe8994b1621d882fcc230d320908a2a3Ian Romanick } 913a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 914a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_plus: 91518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick op[0] = this->subexpressions[0]->hir(instructions, state); 916a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 917c24bcad9f88379ffba9e2f0ff92f22cdf60c2927Carl Worth type = unary_arithmetic_result_type(op[0]->type, state, & loc); 918c24bcad9f88379ffba9e2f0ff92f22cdf60c2927Carl Worth 919c24bcad9f88379ffba9e2f0ff92f22cdf60c2927Carl Worth error_emitted = type->is_error(); 920a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 921a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick result = op[0]; 922a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 923a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 924a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_neg: 92518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick op[0] = this->subexpressions[0]->hir(instructions, state); 926a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 92765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt type = unary_arithmetic_result_type(op[0]->type, state, & loc); 928a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 92965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt error_emitted = type->is_error(); 930a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 9311660a2954797e056caba319c5d6c70b0d4be22feCarl Worth result = new(ctx) ir_expression(operations[this->oper], type, 9321660a2954797e056caba319c5d6c70b0d4be22feCarl Worth op[0], NULL); 933a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 934a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 935a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_add: 936a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_sub: 937a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_mul: 938a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_div: 93918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick op[0] = this->subexpressions[0]->hir(instructions, state); 94018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick op[1] = this->subexpressions[1]->hir(instructions, state); 941a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 942bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick type = arithmetic_result_type(op[0], op[1], 94318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick (this->oper == ast_mul), 944a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt state, & loc); 945a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt error_emitted = type->is_error(); 946a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 9471660a2954797e056caba319c5d6c70b0d4be22feCarl Worth result = new(ctx) ir_expression(operations[this->oper], type, 9481660a2954797e056caba319c5d6c70b0d4be22feCarl Worth op[0], op[1]); 949a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 950a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 951a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_mod: 95218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick op[0] = this->subexpressions[0]->hir(instructions, state); 95318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick op[1] = this->subexpressions[1]->hir(instructions, state); 954a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 95565e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt type = modulus_result_type(op[0]->type, op[1]->type, state, & loc); 956a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 95718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick assert(operations[this->oper] == ir_binop_mod); 958a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 9591660a2954797e056caba319c5d6c70b0d4be22feCarl Worth result = new(ctx) ir_expression(operations[this->oper], type, 9601660a2954797e056caba319c5d6c70b0d4be22feCarl Worth op[0], op[1]); 96165e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt error_emitted = type->is_error(); 962a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 963a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 964a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_lshift: 965a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_rshift: 9665c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace if (state->language_version < 130) { 9675c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace _mesa_glsl_error(&loc, state, "operator %s requires GLSL 1.30", 9685c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace operator_string(this->oper)); 9695c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace error_emitted = true; 9705c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace } 9715c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace 9725c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace op[0] = this->subexpressions[0]->hir(instructions, state); 9735c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace op[1] = this->subexpressions[1]->hir(instructions, state); 974c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace type = shift_result_type(op[0]->type, op[1]->type, this->oper, state, 975c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace &loc); 9765c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace result = new(ctx) ir_expression(operations[this->oper], type, 9775c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace op[0], op[1]); 9785c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); 9795c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace break; 980a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 981a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_less: 982a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_greater: 983a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_lequal: 984a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_gequal: 98518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick op[0] = this->subexpressions[0]->hir(instructions, state); 98618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick op[1] = this->subexpressions[1]->hir(instructions, state); 987a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 98865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt type = relational_result_type(op[0], op[1], state, & loc); 989a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 990a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* The relational operators must either generate an error or result 991a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * in a scalar boolean. See page 57 of the GLSL 1.50 spec. 992a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 993a43817a483a8c4a480ef4e6dfda2cef899300eb0Ian Romanick assert(type->is_error() 994a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick || ((type->base_type == GLSL_TYPE_BOOL) 995cb36f8aaeeb09660843316270a781948f773d90bIan Romanick && type->is_scalar())); 996a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 9971660a2954797e056caba319c5d6c70b0d4be22feCarl Worth result = new(ctx) ir_expression(operations[this->oper], type, 9981660a2954797e056caba319c5d6c70b0d4be22feCarl Worth op[0], op[1]); 99965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt error_emitted = type->is_error(); 1000a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1001a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1002a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_nequal: 1003a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_equal: 10046e659caaa946339a2de3890a8bed091ccb65102aIan Romanick op[0] = this->subexpressions[0]->hir(instructions, state); 10056e659caaa946339a2de3890a8bed091ccb65102aIan Romanick op[1] = this->subexpressions[1]->hir(instructions, state); 10066e659caaa946339a2de3890a8bed091ccb65102aIan Romanick 10076e659caaa946339a2de3890a8bed091ccb65102aIan Romanick /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec: 10086e659caaa946339a2de3890a8bed091ccb65102aIan Romanick * 10096e659caaa946339a2de3890a8bed091ccb65102aIan Romanick * "The equality operators equal (==), and not equal (!=) 10106e659caaa946339a2de3890a8bed091ccb65102aIan Romanick * operate on all types. They result in a scalar Boolean. If 10116e659caaa946339a2de3890a8bed091ccb65102aIan Romanick * the operand types do not match, then there must be a 10126e659caaa946339a2de3890a8bed091ccb65102aIan Romanick * conversion from Section 4.1.10 "Implicit Conversions" 10136e659caaa946339a2de3890a8bed091ccb65102aIan Romanick * applied to one operand that can make them match, in which 10146e659caaa946339a2de3890a8bed091ccb65102aIan Romanick * case this conversion is done." 10156e659caaa946339a2de3890a8bed091ccb65102aIan Romanick */ 1016bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick if ((!apply_implicit_conversion(op[0]->type, op[1], state) 1017bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick && !apply_implicit_conversion(op[1]->type, op[0], state)) 1018212b0327b47033442842a7be3d7fb10e08e2bf66Ian Romanick || (op[0]->type != op[1]->type)) { 10196e659caaa946339a2de3890a8bed091ccb65102aIan Romanick _mesa_glsl_error(& loc, state, "operands of `%s' must have the same " 10206e659caaa946339a2de3890a8bed091ccb65102aIan Romanick "type", (this->oper == ast_equal) ? "==" : "!="); 10216e659caaa946339a2de3890a8bed091ccb65102aIan Romanick error_emitted = true; 1022a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick } else if ((state->language_version <= 110) 1023a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick && (op[0]->type->is_array() || op[1]->type->is_array())) { 1024a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick _mesa_glsl_error(& loc, state, "array comparisons forbidden in " 1025a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick "GLSL 1.10"); 1026a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick error_emitted = true; 10276e659caaa946339a2de3890a8bed091ccb65102aIan Romanick } 10286e659caaa946339a2de3890a8bed091ccb65102aIan Romanick 1029ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt result = do_comparison(ctx, operations[this->oper], op[0], op[1]); 10306e659caaa946339a2de3890a8bed091ccb65102aIan Romanick type = glsl_type::bool_type; 10316e659caaa946339a2de3890a8bed091ccb65102aIan Romanick 10326e659caaa946339a2de3890a8bed091ccb65102aIan Romanick assert(result->type == glsl_type::bool_type); 1033a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1034a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1035a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_bit_and: 1036a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_bit_xor: 1037a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_bit_or: 10381eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke op[0] = this->subexpressions[0]->hir(instructions, state); 10391eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke op[1] = this->subexpressions[1]->hir(instructions, state); 1040cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper, 1041cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace state, &loc); 10421eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke result = new(ctx) ir_expression(operations[this->oper], type, 10431eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke op[0], op[1]); 10441eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); 10451eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke break; 10461eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke 1047a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_bit_not: 10481eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke op[0] = this->subexpressions[0]->hir(instructions, state); 10491eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke 10501eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke if (state->language_version < 130) { 10511eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke _mesa_glsl_error(&loc, state, "bit-wise operations require GLSL 1.30"); 10521eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke error_emitted = true; 10531eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke } 10541eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke 10551eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke if (!op[0]->type->is_integer()) { 10561eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke _mesa_glsl_error(&loc, state, "operand of `~' must be an integer"); 10571eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke error_emitted = true; 10581eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke } 10591eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke 10601eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke type = op[0]->type; 10611eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke result = new(ctx) ir_expression(ir_unop_bit_not, type, op[0], NULL); 1062a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1063a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 10644950a68bf22ede6f4f368c9783e5401816159574Eric Anholt case ast_logic_and: { 1065b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt op[0] = this->subexpressions[0]->hir(instructions, state); 1066b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt 1067b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) { 1068b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt YYLTYPE loc = this->subexpressions[0]->get_location(); 1069b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt 1070b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean", 1071b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt operator_string(this->oper)); 1072ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt error_emitted = true; 1073b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt } 1074b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt 107544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt ir_constant *op0_const = op[0]->constant_expression_value(); 107644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt if (op0_const) { 107744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt if (op0_const->value.b[0]) { 107844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt op[1] = this->subexpressions[1]->hir(instructions, state); 107944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt 108044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) { 108144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt YYLTYPE loc = this->subexpressions[1]->get_location(); 108244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt 108344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt _mesa_glsl_error(& loc, state, 108444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt "RHS of `%s' must be scalar boolean", 108544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt operator_string(this->oper)); 108644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt error_emitted = true; 108744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt } 108844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt result = op[1]; 108944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt } else { 109044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt result = op0_const; 109144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt } 109244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt type = glsl_type::bool_type; 109344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt } else { 109481d664f099a5fd5fac777480532fb4307d591451Ian Romanick ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type, 10957e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick "and_tmp", 10967e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick ir_var_temporary); 109781d664f099a5fd5fac777480532fb4307d591451Ian Romanick instructions->push_tail(tmp); 109881d664f099a5fd5fac777480532fb4307d591451Ian Romanick 10991660a2954797e056caba319c5d6c70b0d4be22feCarl Worth ir_if *const stmt = new(ctx) ir_if(op[0]); 110044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt instructions->push_tail(stmt); 11014950a68bf22ede6f4f368c9783e5401816159574Eric Anholt 110244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt op[1] = this->subexpressions[1]->hir(&stmt->then_instructions, state); 11034950a68bf22ede6f4f368c9783e5401816159574Eric Anholt 110444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) { 110544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt YYLTYPE loc = this->subexpressions[1]->get_location(); 11064950a68bf22ede6f4f368c9783e5401816159574Eric Anholt 110744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt _mesa_glsl_error(& loc, state, 110844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt "RHS of `%s' must be scalar boolean", 110944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt operator_string(this->oper)); 111044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt error_emitted = true; 111144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt } 1112b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt 11131660a2954797e056caba319c5d6c70b0d4be22feCarl Worth ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp); 111444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt ir_assignment *const then_assign = 11151660a2954797e056caba319c5d6c70b0d4be22feCarl Worth new(ctx) ir_assignment(then_deref, op[1], NULL); 111644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt stmt->then_instructions.push_tail(then_assign); 11174950a68bf22ede6f4f368c9783e5401816159574Eric Anholt 11181660a2954797e056caba319c5d6c70b0d4be22feCarl Worth ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp); 111944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt ir_assignment *const else_assign = 11201660a2954797e056caba319c5d6c70b0d4be22feCarl Worth new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false), NULL); 112144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt stmt->else_instructions.push_tail(else_assign); 11224950a68bf22ede6f4f368c9783e5401816159574Eric Anholt 11231660a2954797e056caba319c5d6c70b0d4be22feCarl Worth result = new(ctx) ir_dereference_variable(tmp); 112444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt type = tmp->type; 112544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt } 11264950a68bf22ede6f4f368c9783e5401816159574Eric Anholt break; 11274950a68bf22ede6f4f368c9783e5401816159574Eric Anholt } 11284950a68bf22ede6f4f368c9783e5401816159574Eric Anholt 11294950a68bf22ede6f4f368c9783e5401816159574Eric Anholt case ast_logic_or: { 11304950a68bf22ede6f4f368c9783e5401816159574Eric Anholt op[0] = this->subexpressions[0]->hir(instructions, state); 11314950a68bf22ede6f4f368c9783e5401816159574Eric Anholt 11324950a68bf22ede6f4f368c9783e5401816159574Eric Anholt if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) { 11334950a68bf22ede6f4f368c9783e5401816159574Eric Anholt YYLTYPE loc = this->subexpressions[0]->get_location(); 11344950a68bf22ede6f4f368c9783e5401816159574Eric Anholt 11354950a68bf22ede6f4f368c9783e5401816159574Eric Anholt _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean", 11364950a68bf22ede6f4f368c9783e5401816159574Eric Anholt operator_string(this->oper)); 11374950a68bf22ede6f4f368c9783e5401816159574Eric Anholt error_emitted = true; 11384950a68bf22ede6f4f368c9783e5401816159574Eric Anholt } 11394950a68bf22ede6f4f368c9783e5401816159574Eric Anholt 114044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt ir_constant *op0_const = op[0]->constant_expression_value(); 114144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt if (op0_const) { 114244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt if (op0_const->value.b[0]) { 114344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt result = op0_const; 114444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt } else { 114544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt op[1] = this->subexpressions[1]->hir(instructions, state); 114644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt 114744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) { 114844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt YYLTYPE loc = this->subexpressions[1]->get_location(); 114944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt 115044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt _mesa_glsl_error(& loc, state, 115144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt "RHS of `%s' must be scalar boolean", 115244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt operator_string(this->oper)); 115344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt error_emitted = true; 115444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt } 115544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt result = op[1]; 115644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt } 115744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt type = glsl_type::bool_type; 115844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt } else { 1159dfd30ca6a95a7d95835dad78ffe1fba4d1f4ef69Kenneth Graunke ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type, 11607e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick "or_tmp", 11617e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick ir_var_temporary); 11620b9ae3befb0bf80e000b159fd44c961a144f9c36Ian Romanick instructions->push_tail(tmp); 11634950a68bf22ede6f4f368c9783e5401816159574Eric Anholt 116481d664f099a5fd5fac777480532fb4307d591451Ian Romanick ir_if *const stmt = new(ctx) ir_if(op[0]); 116581d664f099a5fd5fac777480532fb4307d591451Ian Romanick instructions->push_tail(stmt); 116681d664f099a5fd5fac777480532fb4307d591451Ian Romanick 1167a0879b9dd438d78635f047cdd5ed4c72bc831b60Eric Anholt op[1] = this->subexpressions[1]->hir(&stmt->else_instructions, state); 11684950a68bf22ede6f4f368c9783e5401816159574Eric Anholt 116944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) { 117044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt YYLTYPE loc = this->subexpressions[1]->get_location(); 11714950a68bf22ede6f4f368c9783e5401816159574Eric Anholt 117244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt _mesa_glsl_error(& loc, state, "RHS of `%s' must be scalar boolean", 117344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt operator_string(this->oper)); 117444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt error_emitted = true; 117544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt } 11764950a68bf22ede6f4f368c9783e5401816159574Eric Anholt 11771660a2954797e056caba319c5d6c70b0d4be22feCarl Worth ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp); 117844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt ir_assignment *const then_assign = 11791660a2954797e056caba319c5d6c70b0d4be22feCarl Worth new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true), NULL); 118044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt stmt->then_instructions.push_tail(then_assign); 11814950a68bf22ede6f4f368c9783e5401816159574Eric Anholt 11821660a2954797e056caba319c5d6c70b0d4be22feCarl Worth ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp); 118344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt ir_assignment *const else_assign = 11841660a2954797e056caba319c5d6c70b0d4be22feCarl Worth new(ctx) ir_assignment(else_deref, op[1], NULL); 118544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt stmt->else_instructions.push_tail(else_assign); 11864950a68bf22ede6f4f368c9783e5401816159574Eric Anholt 11871660a2954797e056caba319c5d6c70b0d4be22feCarl Worth result = new(ctx) ir_dereference_variable(tmp); 118844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt type = tmp->type; 118944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt } 11904950a68bf22ede6f4f368c9783e5401816159574Eric Anholt break; 11914950a68bf22ede6f4f368c9783e5401816159574Eric Anholt } 11924950a68bf22ede6f4f368c9783e5401816159574Eric Anholt 11934950a68bf22ede6f4f368c9783e5401816159574Eric Anholt case ast_logic_xor: 11944950a68bf22ede6f4f368c9783e5401816159574Eric Anholt op[0] = this->subexpressions[0]->hir(instructions, state); 11954950a68bf22ede6f4f368c9783e5401816159574Eric Anholt op[1] = this->subexpressions[1]->hir(instructions, state); 11964950a68bf22ede6f4f368c9783e5401816159574Eric Anholt 11974950a68bf22ede6f4f368c9783e5401816159574Eric Anholt 11981660a2954797e056caba319c5d6c70b0d4be22feCarl Worth result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type, 11991660a2954797e056caba319c5d6c70b0d4be22feCarl Worth op[0], op[1]); 1200ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt type = glsl_type::bool_type; 1201a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1202a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1203a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt case ast_logic_not: 1204a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt op[0] = this->subexpressions[0]->hir(instructions, state); 1205a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt 1206a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) { 1207a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt YYLTYPE loc = this->subexpressions[0]->get_location(); 1208a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt 1209a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt _mesa_glsl_error(& loc, state, 1210a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt "operand of `!' must be scalar boolean"); 1211ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt error_emitted = true; 1212a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt } 1213a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt 12141660a2954797e056caba319c5d6c70b0d4be22feCarl Worth result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type, 12151660a2954797e056caba319c5d6c70b0d4be22feCarl Worth op[0], NULL); 1216ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt type = glsl_type::bool_type; 1217a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt break; 1218a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt 1219a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_mul_assign: 1220a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_div_assign: 1221a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_add_assign: 1222a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_sub_assign: { 122318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick op[0] = this->subexpressions[0]->hir(instructions, state); 122418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick op[1] = this->subexpressions[1]->hir(instructions, state); 1225a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1226bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick type = arithmetic_result_type(op[0], op[1], 122718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick (this->oper == ast_mul_assign), 1228a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt state, & loc); 1229a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 12301660a2954797e056caba319c5d6c70b0d4be22feCarl Worth ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type, 12311660a2954797e056caba319c5d6c70b0d4be22feCarl Worth op[0], op[1]); 1232a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 12333e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt result = do_assignment(instructions, state, 12348273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt op[0]->clone(ctx, NULL), temp_rhs, 123510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt this->subexpressions[0]->get_location()); 123610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt type = result->type; 123710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt error_emitted = (op[0]->type->is_error()); 1238a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1239a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* GLSL 1.10 does not allow array assignment. However, we don't have to 1240a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * explicitly test for this because none of the binary expression 1241a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * operators allow array operands either. 1242a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 1243a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1244a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1245a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 1246a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 124748a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt case ast_mod_assign: { 124848a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt op[0] = this->subexpressions[0]->hir(instructions, state); 124948a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt op[1] = this->subexpressions[1]->hir(instructions, state); 125048a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt 125165e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt type = modulus_result_type(op[0]->type, op[1]->type, state, & loc); 125248a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt 125348a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt assert(operations[this->oper] == ir_binop_mod); 125448a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt 1255768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick ir_rvalue *temp_rhs; 12561660a2954797e056caba319c5d6c70b0d4be22feCarl Worth temp_rhs = new(ctx) ir_expression(operations[this->oper], type, 12571660a2954797e056caba319c5d6c70b0d4be22feCarl Worth op[0], op[1]); 125848a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt 12593e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt result = do_assignment(instructions, state, 12608273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt op[0]->clone(ctx, NULL), temp_rhs, 126148a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt this->subexpressions[0]->get_location()); 126248a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt type = result->type; 126365e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt error_emitted = type->is_error(); 126448a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt break; 126548a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt } 1266a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1267a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_ls_assign: 1268338ed6ec297d76746b6466c26c307722af965e60Chad Versace case ast_rs_assign: { 1269338ed6ec297d76746b6466c26c307722af965e60Chad Versace op[0] = this->subexpressions[0]->hir(instructions, state); 1270338ed6ec297d76746b6466c26c307722af965e60Chad Versace op[1] = this->subexpressions[1]->hir(instructions, state); 1271338ed6ec297d76746b6466c26c307722af965e60Chad Versace type = shift_result_type(op[0]->type, op[1]->type, this->oper, state, 1272338ed6ec297d76746b6466c26c307722af965e60Chad Versace &loc); 1273338ed6ec297d76746b6466c26c307722af965e60Chad Versace ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], 1274338ed6ec297d76746b6466c26c307722af965e60Chad Versace type, op[0], op[1]); 1275338ed6ec297d76746b6466c26c307722af965e60Chad Versace result = do_assignment(instructions, state, op[0]->clone(ctx, NULL), 1276338ed6ec297d76746b6466c26c307722af965e60Chad Versace temp_rhs, 1277338ed6ec297d76746b6466c26c307722af965e60Chad Versace this->subexpressions[0]->get_location()); 1278338ed6ec297d76746b6466c26c307722af965e60Chad Versace error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); 1279251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick break; 1280338ed6ec297d76746b6466c26c307722af965e60Chad Versace } 1281a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1282a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_and_assign: 1283a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_xor_assign: 1284d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace case ast_or_assign: { 1285d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace op[0] = this->subexpressions[0]->hir(instructions, state); 1286d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace op[1] = this->subexpressions[1]->hir(instructions, state); 1287d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper, 1288d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace state, &loc); 1289d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], 1290d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace type, op[0], op[1]); 1291d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace result = do_assignment(instructions, state, op[0]->clone(ctx, NULL), 1292d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace temp_rhs, 1293d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace this->subexpressions[0]->get_location()); 1294d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); 1295251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick break; 1296d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace } 1297a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 129896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick case ast_conditional: { 129996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick op[0] = this->subexpressions[0]->hir(instructions, state); 130096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick 130196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec: 130296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick * 130396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick * "The ternary selection operator (?:). It operates on three 130496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick * expressions (exp1 ? exp2 : exp3). This operator evaluates the 130596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick * first expression, which must result in a scalar Boolean." 130696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick */ 130796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) { 130896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick YYLTYPE loc = this->subexpressions[0]->get_location(); 130996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick 131096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick _mesa_glsl_error(& loc, state, "?: condition must be scalar boolean"); 131196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick error_emitted = true; 131296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick } 131396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick 131496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick /* The :? operator is implemented by generating an anonymous temporary 131596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick * followed by an if-statement. The last instruction in each branch of 131696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick * the if-statement assigns a value to the anonymous temporary. This 131796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick * temporary is the r-value of the expression. 131896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick */ 13190ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick exec_list then_instructions; 13200ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick exec_list else_instructions; 132196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick 13220ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick op[1] = this->subexpressions[1]->hir(&then_instructions, state); 13230ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick op[2] = this->subexpressions[2]->hir(&else_instructions, state); 132496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick 132596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec: 132696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick * 132796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick * "The second and third expressions can be any type, as 132896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick * long their types match, or there is a conversion in 132996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick * Section 4.1.10 "Implicit Conversions" that can be applied 133096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick * to one of the expressions to make their types match. This 133196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick * resulting matching type is the type of the entire 133296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick * expression." 133396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick */ 1334bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick if ((!apply_implicit_conversion(op[1]->type, op[2], state) 1335bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick && !apply_implicit_conversion(op[2]->type, op[1], state)) 1336db9be2e7aa3a56e43b725ad7725fe6b424e4933eIan Romanick || (op[1]->type != op[2]->type)) { 133796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick YYLTYPE loc = this->subexpressions[1]->get_location(); 133896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick 133996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick _mesa_glsl_error(& loc, state, "Second and third operands of ?: " 134096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick "operator must have matching types."); 134196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick error_emitted = true; 13420ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick type = glsl_type::error_type; 1343db9be2e7aa3a56e43b725ad7725fe6b424e4933eIan Romanick } else { 13440ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick type = op[1]->type; 134596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick } 134696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick 1347f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec: 1348f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick * 1349f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick * "The second and third expressions must be the same type, but can 1350f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick * be of any type other than an array." 1351f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick */ 1352f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick if ((state->language_version <= 110) && type->is_array()) { 1353f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick _mesa_glsl_error(& loc, state, "Second and third operands of ?: " 1354f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick "operator must not be arrays."); 1355f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick error_emitted = true; 1356f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick } 1357f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick 13587825d3d15710fdfcfc503754862963aac8065480Ian Romanick ir_constant *cond_val = op[0]->constant_expression_value(); 13597825d3d15710fdfcfc503754862963aac8065480Ian Romanick ir_constant *then_val = op[1]->constant_expression_value(); 13607825d3d15710fdfcfc503754862963aac8065480Ian Romanick ir_constant *else_val = op[2]->constant_expression_value(); 13617825d3d15710fdfcfc503754862963aac8065480Ian Romanick 13627825d3d15710fdfcfc503754862963aac8065480Ian Romanick if (then_instructions.is_empty() 13637825d3d15710fdfcfc503754862963aac8065480Ian Romanick && else_instructions.is_empty() 13647825d3d15710fdfcfc503754862963aac8065480Ian Romanick && (cond_val != NULL) && (then_val != NULL) && (else_val != NULL)) { 13657825d3d15710fdfcfc503754862963aac8065480Ian Romanick result = (cond_val->value.b[0]) ? then_val : else_val; 13667825d3d15710fdfcfc503754862963aac8065480Ian Romanick } else { 13677e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick ir_variable *const tmp = 13687e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary); 13690b9ae3befb0bf80e000b159fd44c961a144f9c36Ian Romanick instructions->push_tail(tmp); 13700ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick 13711660a2954797e056caba319c5d6c70b0d4be22feCarl Worth ir_if *const stmt = new(ctx) ir_if(op[0]); 13727825d3d15710fdfcfc503754862963aac8065480Ian Romanick instructions->push_tail(stmt); 13730ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick 13747825d3d15710fdfcfc503754862963aac8065480Ian Romanick then_instructions.move_nodes_to(& stmt->then_instructions); 13751660a2954797e056caba319c5d6c70b0d4be22feCarl Worth ir_dereference *const then_deref = 13761660a2954797e056caba319c5d6c70b0d4be22feCarl Worth new(ctx) ir_dereference_variable(tmp); 13777825d3d15710fdfcfc503754862963aac8065480Ian Romanick ir_assignment *const then_assign = 13781660a2954797e056caba319c5d6c70b0d4be22feCarl Worth new(ctx) ir_assignment(then_deref, op[1], NULL); 13797825d3d15710fdfcfc503754862963aac8065480Ian Romanick stmt->then_instructions.push_tail(then_assign); 13800ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick 13817825d3d15710fdfcfc503754862963aac8065480Ian Romanick else_instructions.move_nodes_to(& stmt->else_instructions); 13821660a2954797e056caba319c5d6c70b0d4be22feCarl Worth ir_dereference *const else_deref = 13831660a2954797e056caba319c5d6c70b0d4be22feCarl Worth new(ctx) ir_dereference_variable(tmp); 13847825d3d15710fdfcfc503754862963aac8065480Ian Romanick ir_assignment *const else_assign = 13851660a2954797e056caba319c5d6c70b0d4be22feCarl Worth new(ctx) ir_assignment(else_deref, op[2], NULL); 13867825d3d15710fdfcfc503754862963aac8065480Ian Romanick stmt->else_instructions.push_tail(else_assign); 13870ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick 13881660a2954797e056caba319c5d6c70b0d4be22feCarl Worth result = new(ctx) ir_dereference_variable(tmp); 13897825d3d15710fdfcfc503754862963aac8065480Ian Romanick } 1390251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick break; 139196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick } 1392a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1393a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_pre_inc: 139476ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt case ast_pre_dec: { 139576ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt op[0] = this->subexpressions[0]->hir(instructions, state); 139676ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt if (op[0]->type->base_type == GLSL_TYPE_FLOAT) 13971660a2954797e056caba319c5d6c70b0d4be22feCarl Worth op[1] = new(ctx) ir_constant(1.0f); 139876ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt else 13991660a2954797e056caba319c5d6c70b0d4be22feCarl Worth op[1] = new(ctx) ir_constant(1); 140076ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt 1401a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt type = arithmetic_result_type(op[0], op[1], false, state, & loc); 140276ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt 1403768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick ir_rvalue *temp_rhs; 14041660a2954797e056caba319c5d6c70b0d4be22feCarl Worth temp_rhs = new(ctx) ir_expression(operations[this->oper], type, 14051660a2954797e056caba319c5d6c70b0d4be22feCarl Worth op[0], op[1]); 140676ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt 14073e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt result = do_assignment(instructions, state, 14088273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt op[0]->clone(ctx, NULL), temp_rhs, 140976ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt this->subexpressions[0]->get_location()); 141076ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt type = result->type; 141176ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt error_emitted = op[0]->type->is_error(); 141276ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt break; 141376ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt } 1414a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1415a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_post_inc: 1416de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt case ast_post_dec: { 1417de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt op[0] = this->subexpressions[0]->hir(instructions, state); 1418de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt if (op[0]->type->base_type == GLSL_TYPE_FLOAT) 14191660a2954797e056caba319c5d6c70b0d4be22feCarl Worth op[1] = new(ctx) ir_constant(1.0f); 1420de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt else 14211660a2954797e056caba319c5d6c70b0d4be22feCarl Worth op[1] = new(ctx) ir_constant(1); 1422de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt 1423de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); 1424de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt 1425a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt type = arithmetic_result_type(op[0], op[1], false, state, & loc); 1426de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt 1427768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick ir_rvalue *temp_rhs; 14281660a2954797e056caba319c5d6c70b0d4be22feCarl Worth temp_rhs = new(ctx) ir_expression(operations[this->oper], type, 14291660a2954797e056caba319c5d6c70b0d4be22feCarl Worth op[0], op[1]); 1430de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt 1431de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt /* Get a temporary of a copy of the lvalue before it's modified. 1432de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt * This may get thrown away later. 1433de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt */ 14348273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL)); 1435de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt 14363e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt (void)do_assignment(instructions, state, 14378273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt op[0]->clone(ctx, NULL), temp_rhs, 1438de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt this->subexpressions[0]->get_location()); 1439de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt 1440de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt type = result->type; 1441de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt error_emitted = op[0]->type->is_error(); 1442a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1443de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt } 1444a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1445a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_field_selection: 144618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick result = _mesa_ast_field_selection_to_hir(this, instructions, state); 1447a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick type = result->type; 1448a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1449a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 145027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick case ast_array_index: { 145127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick YYLTYPE index_loc = subexpressions[1]->get_location(); 145227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick 145327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick op[0] = subexpressions[0]->hir(instructions, state); 145427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick op[1] = subexpressions[1]->hir(instructions, state); 145527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick 145627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); 145727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick 1458a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick ir_rvalue *const array = op[0]; 1459b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick 14601660a2954797e056caba319c5d6c70b0d4be22feCarl Worth result = new(ctx) ir_dereference_array(op[0], op[1]); 1461b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick 1462b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick /* Do not use op[0] after this point. Use array. 1463b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick */ 1464b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick op[0] = NULL; 1465b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick 146627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick 146727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick if (error_emitted) 146827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick break; 146927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick 147063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick if (!array->type->is_array() 147163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick && !array->type->is_matrix() 147263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick && !array->type->is_vector()) { 147327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick _mesa_glsl_error(& index_loc, state, 147463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick "cannot dereference non-array / non-matrix / " 147563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick "non-vector"); 147627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick error_emitted = true; 147727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick } 147827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick 147927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick if (!op[1]->type->is_integer()) { 148027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick _mesa_glsl_error(& index_loc, state, 148127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick "array index must be integer type"); 148227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick error_emitted = true; 148327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick } else if (!op[1]->type->is_scalar()) { 148427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick _mesa_glsl_error(& index_loc, state, 148527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick "array index must be scalar"); 148627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick error_emitted = true; 148727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick } 148827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick 148927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick /* If the array index is a constant expression and the array has a 149027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick * declared size, ensure that the access is in-bounds. If the array 149127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick * index is not a constant expression, ensure that the array has a 149227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick * declared size. 149327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick */ 149427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick ir_constant *const const_index = op[1]->constant_expression_value(); 149527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick if (const_index != NULL) { 149627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick const int idx = const_index->value.i[0]; 149763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick const char *type_name; 149863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick unsigned bound = 0; 149963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick 150063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick if (array->type->is_matrix()) { 150163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick type_name = "matrix"; 150263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick } else if (array->type->is_vector()) { 150363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick type_name = "vector"; 150463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick } else { 150563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick type_name = "array"; 150663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick } 150727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick 150827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec: 150927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick * 151027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick * "It is illegal to declare an array with a size, and then 151127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick * later (in the same shader) index the same array with an 151227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick * integral constant expression greater than or equal to the 151327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick * declared size. It is also illegal to index an array with a 151427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick * negative constant expression." 151527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick */ 151663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick if (array->type->is_matrix()) { 151763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick if (array->type->row_type()->vector_elements <= idx) { 151863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick bound = array->type->row_type()->vector_elements; 151963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick } 152063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick } else if (array->type->is_vector()) { 152163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick if (array->type->vector_elements <= idx) { 152263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick bound = array->type->vector_elements; 152363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick } 152463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick } else { 152563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick if ((array->type->array_size() > 0) 152663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick && (array->type->array_size() <= idx)) { 152763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick bound = array->type->array_size(); 152863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick } 152927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick } 153027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick 153163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick if (bound > 0) { 153263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick _mesa_glsl_error(& loc, state, "%s index must be < %u", 153363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick type_name, bound); 153463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick error_emitted = true; 153563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick } else if (idx < 0) { 153663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick _mesa_glsl_error(& loc, state, "%s index must be >= 0", 153763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick type_name); 153827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick error_emitted = true; 153927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick } 1540b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick 154163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick if (array->type->is_array()) { 1542a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick /* If the array is a variable dereference, it dereferences the 1543a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick * whole array, by definition. Use this to get the variable. 1544a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick * 1545a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick * FINISHME: Should some methods for getting / setting / testing 1546a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick * FINISHME: array access limits be added to ir_dereference? 1547a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick */ 1548a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick ir_variable *const v = array->whole_variable_referenced(); 154963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick if ((v != NULL) && (unsigned(idx) > v->max_array_access)) 155063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick v->max_array_access = idx; 155163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick } 15522b7c42b40ae459f7b290eb134d6dabd075aab9f0Kenneth Graunke } else if (array->type->array_size() == 0) { 15532b7c42b40ae459f7b290eb134d6dabd075aab9f0Kenneth Graunke _mesa_glsl_error(&loc, state, "unsized array index must be constant"); 1554a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt } else { 1555a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt if (array->type->is_array()) { 15565226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius /* whole_variable_referenced can return NULL if the array is a 15575226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius * member of a structure. In this case it is safe to not update 15585226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius * the max_array_access field because it is never used for fields 15595226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius * of structures. 15605226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius */ 1561a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt ir_variable *v = array->whole_variable_referenced(); 15625226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius if (v != NULL) 15635226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius v->max_array_access = array->type->array_size(); 1564a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt } 156527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick } 156627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick 156727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick if (error_emitted) 156827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick result->type = glsl_type::error_type; 156927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick 157027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick type = result->type; 1571a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 157227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick } 1573a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1574a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_function_call: 15757cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick /* Should *NEVER* get here. ast_function_call should always be handled 15767cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick * by ast_function_expression::hir. 1577a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 15787cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick assert(0); 1579a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1580a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1581a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_identifier: { 1582a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* ast_identifier can appear several places in a full abstract syntax 1583a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * tree. This particular use must be at location specified in the grammar 1584a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * as 'variable_identifier'. 1585a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 15868bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick ir_variable *var = 15878bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick state->symbols->get_variable(this->primary_expression.identifier); 1588a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 15891660a2954797e056caba319c5d6c70b0d4be22feCarl Worth result = new(ctx) ir_dereference_variable(var); 1590a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1591a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick if (var != NULL) { 1592a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick type = result->type; 1593a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } else { 159471d0bbfcb2853f37b580ec7b705e55bb0eb426faIan Romanick _mesa_glsl_error(& loc, state, "`%s' undeclared", 159518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick this->primary_expression.identifier); 1596a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1597a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick error_emitted = true; 1598a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 1599a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1600a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 1601a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1602a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_int_constant: 16030471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick type = glsl_type::int_type; 16041660a2954797e056caba319c5d6c70b0d4be22feCarl Worth result = new(ctx) ir_constant(this->primary_expression.int_constant); 1605a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1606a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1607a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_uint_constant: 16080471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick type = glsl_type::uint_type; 16091660a2954797e056caba319c5d6c70b0d4be22feCarl Worth result = new(ctx) ir_constant(this->primary_expression.uint_constant); 1610a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1611a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1612a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_float_constant: 16130471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick type = glsl_type::float_type; 16141660a2954797e056caba319c5d6c70b0d4be22feCarl Worth result = new(ctx) ir_constant(this->primary_expression.float_constant); 1615a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1616a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1617a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_bool_constant: 16180471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick type = glsl_type::bool_type; 16191660a2954797e056caba319c5d6c70b0d4be22feCarl Worth result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant)); 1620a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1621a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1622a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_sequence: { 1623a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* It should not be possible to generate a sequence in the AST without 1624a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * any expressions in it. 1625a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 1626304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick assert(!this->expressions.is_empty()); 1627a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1628a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* The r-value of a sequence is the last expression in the sequence. If 1629a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * the other expressions in the sequence do not have side-effects (and 1630a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * therefore add instructions to the instruction list), they get dropped 1631a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * on the floor. 1632a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 16332b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick foreach_list_typed (ast_node, ast, link, &this->expressions) 1634304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick result = ast->hir(instructions, state); 1635a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1636a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick type = result->type; 1637a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1638a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* Any errors should have already been emitted in the loop above. 1639a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 1640a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick error_emitted = true; 1641a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1642a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 1643a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 1644a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1645cef3baecf636a30b62cd7a1e8de57c7650f7003eIan Romanick if (type->is_error() && !error_emitted) 164671d0bbfcb2853f37b580ec7b705e55bb0eb426faIan Romanick _mesa_glsl_error(& loc, state, "type mismatch"); 1647a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1648a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick return result; 1649a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick} 1650a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1651a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1652fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue * 16530044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_expression_statement::hir(exec_list *instructions, 165418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick struct _mesa_glsl_parse_state *state) 1655a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{ 1656a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* It is possible to have expression statements that don't have an 1657a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * expression. This is the solitary semicolon: 1658a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * 1659a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * for (i = 0; i < 5; i++) 1660a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * ; 1661a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * 1662a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * In this case the expression will be NULL. Test for NULL and don't do 1663a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * anything in that case. 1664a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 166518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick if (expression != NULL) 166618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick expression->hir(instructions, state); 1667a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1668a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* Statements do not have r-values. 1669a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 1670a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick return NULL; 1671a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick} 1672a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1673a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1674fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue * 16750044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_compound_statement::hir(exec_list *instructions, 167618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick struct _mesa_glsl_parse_state *state) 1677a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{ 167818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick if (new_scope) 16798bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick state->symbols->push_scope(); 1680a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 16812b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick foreach_list_typed (ast_node, ast, link, &this->statements) 1682304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick ast->hir(instructions, state); 1683a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 168418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick if (new_scope) 16858bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick state->symbols->pop_scope(); 1686a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1687a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* Compound statements do not have r-values. 1688a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 1689a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick return NULL; 1690a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick} 1691a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1692a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 169328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanickstatic const glsl_type * 1694d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunkeprocess_array_type(YYLTYPE *loc, const glsl_type *base, ast_node *array_size, 169528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick struct _mesa_glsl_parse_state *state) 169628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick{ 169728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick unsigned length = 0; 169828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick 169928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick /* FINISHME: Reject delcarations of multidimensional arrays. */ 170028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick 170128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick if (array_size != NULL) { 170228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick exec_list dummy_instructions; 170328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick ir_rvalue *const ir = array_size->hir(& dummy_instructions, state); 170428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick YYLTYPE loc = array_size->get_location(); 170528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick 170628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick /* FINISHME: Verify that the grammar forbids side-effects in array 170728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick * FINISHME: sizes. i.e., 'vec4 [x = 12] data' 170828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick */ 170928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick assert(dummy_instructions.is_empty()); 171028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick 171128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick if (ir != NULL) { 171228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick if (!ir->type->is_integer()) { 171328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick _mesa_glsl_error(& loc, state, "array size must be integer type"); 171428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick } else if (!ir->type->is_scalar()) { 171528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick _mesa_glsl_error(& loc, state, "array size must be scalar type"); 171628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick } else { 171728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick ir_constant *const size = ir->constant_expression_value(); 171828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick 171928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick if (size == NULL) { 172028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick _mesa_glsl_error(& loc, state, "array size must be a " 172128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick "constant valued expression"); 172228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick } else if (size->value.i[0] <= 0) { 172328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick _mesa_glsl_error(& loc, state, "array size must be > 0"); 172428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick } else { 172528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick assert(size->type == ir->type); 172628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick length = size->value.u[0]; 172728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick } 172828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick } 172928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick } 1730d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke } else if (state->es_shader) { 1731d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke /* Section 10.17 of the GLSL ES 1.00 specification states that unsized 1732d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke * array declarations have been removed from the language. 1733d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke */ 1734d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke _mesa_glsl_error(loc, state, "unsized array declarations are not " 1735d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke "allowed in GLSL ES 1.00."); 173628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick } 173728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick 1738f38d15b80d4e4c8ecb7a76087cdc49835f0aa271Ian Romanick return glsl_type::get_array_instance(base, length); 173928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick} 174028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick 174128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick 1742d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanickconst glsl_type * 1743d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanickast_type_specifier::glsl_type(const char **name, 1744d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick struct _mesa_glsl_parse_state *state) const 1745a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{ 1746d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick const struct glsl_type *type; 1747a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1748ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke type = state->symbols->get_type(this->type_name); 1749ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke *name = this->type_name; 1750a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1751ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke if (this->is_array) { 1752ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke YYLTYPE loc = this->get_location(); 1753ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke type = process_array_type(&loc, type, this->array_size, state); 1754a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 1755a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1756a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick return type; 1757a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick} 1758a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1759a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1760a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic void 1761a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickapply_type_qualifier_to_variable(const struct ast_type_qualifier *qual, 1762768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick ir_variable *var, 17632e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt struct _mesa_glsl_parse_state *state, 17642e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt YYLTYPE *loc) 1765a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{ 1766e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick if (qual->flags.q.invariant) 1767a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick var->invariant = 1; 1768a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1769a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* FINISHME: Mark 'in' variables at global scope as read-only. */ 1770e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick if (qual->flags.q.constant || qual->flags.q.attribute 1771e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick || qual->flags.q.uniform 1772e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick || (qual->flags.q.varying && (state->target == fragment_shader))) 1773a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick var->read_only = 1; 1774a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1775e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick if (qual->flags.q.centroid) 1776a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick var->centroid = 1; 1777a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1778e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick if (qual->flags.q.attribute && state->target != vertex_shader) { 17792e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt var->type = glsl_type::error_type; 17802e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt _mesa_glsl_error(loc, state, 17812e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt "`attribute' variables may not be declared in the " 1782ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick "%s shader", 1783ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick _mesa_glsl_shader_target_name(state->target)); 17842e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt } 17852e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt 178690b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec: 178790b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt * 178890b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt * "The varying qualifier can be used only with the data types 178990b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of 179090b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt * these." 179190b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt */ 1792e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick if (qual->flags.q.varying) { 17930ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt const glsl_type *non_array_type; 17940ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt 17950ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt if (var->type && var->type->is_array()) 17960ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt non_array_type = var->type->fields.array; 17970ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt else 17980ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt non_array_type = var->type; 17990ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt 18000ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt if (non_array_type && non_array_type->base_type != GLSL_TYPE_FLOAT) { 18010ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt var->type = glsl_type::error_type; 18020ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt _mesa_glsl_error(loc, state, 18030ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt "varying variables must be of base type float"); 18040ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt } 180590b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt } 180690b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt 18077e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick /* If there is no qualifier that changes the mode of the variable, leave 18087e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick * the setting alone. 18097e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick */ 1810e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick if (qual->flags.q.in && qual->flags.q.out) 1811a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick var->mode = ir_var_inout; 1812e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick else if (qual->flags.q.attribute || qual->flags.q.in 1813e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick || (qual->flags.q.varying && (state->target == fragment_shader))) 1814a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick var->mode = ir_var_in; 1815e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick else if (qual->flags.q.out 1816e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick || (qual->flags.q.varying && (state->target == vertex_shader))) 1817a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick var->mode = ir_var_out; 1818e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick else if (qual->flags.q.uniform) 1819a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick var->mode = ir_var_uniform; 1820a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1821e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick if (qual->flags.q.flat) 1822a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick var->interpolation = ir_var_flat; 1823e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick else if (qual->flags.q.noperspective) 1824a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick var->interpolation = ir_var_noperspective; 1825a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick else 1826a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick var->interpolation = ir_var_smooth; 18279d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick 1828e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick var->pixel_center_integer = qual->flags.q.pixel_center_integer; 1829e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick var->origin_upper_left = qual->flags.q.origin_upper_left; 1830e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick if ((qual->flags.q.origin_upper_left || qual->flags.q.pixel_center_integer) 18318d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick && (strcmp(var->name, "gl_FragCoord") != 0)) { 1832e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick const char *const qual_string = (qual->flags.q.origin_upper_left) 18338d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick ? "origin_upper_left" : "pixel_center_integer"; 18348d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick 18358d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick _mesa_glsl_error(loc, state, 18368d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick "layout qualifier `%s' can only be applied to " 18378d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick "fragment shader input `gl_FragCoord'", 18388d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick qual_string); 18398d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick } 18408d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick 1841eee68d3631813580a14fa51fda6f0c959279256cIan Romanick if (qual->flags.q.explicit_location) { 1842eee68d3631813580a14fa51fda6f0c959279256cIan Romanick const bool global_scope = (state->current_function == NULL); 1843eee68d3631813580a14fa51fda6f0c959279256cIan Romanick bool fail = false; 1844eee68d3631813580a14fa51fda6f0c959279256cIan Romanick const char *string = ""; 1845eee68d3631813580a14fa51fda6f0c959279256cIan Romanick 1846eee68d3631813580a14fa51fda6f0c959279256cIan Romanick /* In the vertex shader only shader inputs can be given explicit 1847eee68d3631813580a14fa51fda6f0c959279256cIan Romanick * locations. 1848eee68d3631813580a14fa51fda6f0c959279256cIan Romanick * 1849eee68d3631813580a14fa51fda6f0c959279256cIan Romanick * In the fragment shader only shader outputs can be given explicit 1850eee68d3631813580a14fa51fda6f0c959279256cIan Romanick * locations. 1851eee68d3631813580a14fa51fda6f0c959279256cIan Romanick */ 1852eee68d3631813580a14fa51fda6f0c959279256cIan Romanick switch (state->target) { 1853eee68d3631813580a14fa51fda6f0c959279256cIan Romanick case vertex_shader: 1854eee68d3631813580a14fa51fda6f0c959279256cIan Romanick if (!global_scope || (var->mode != ir_var_in)) { 1855eee68d3631813580a14fa51fda6f0c959279256cIan Romanick fail = true; 1856eee68d3631813580a14fa51fda6f0c959279256cIan Romanick string = "input"; 1857eee68d3631813580a14fa51fda6f0c959279256cIan Romanick } 1858eee68d3631813580a14fa51fda6f0c959279256cIan Romanick break; 1859eee68d3631813580a14fa51fda6f0c959279256cIan Romanick 1860eee68d3631813580a14fa51fda6f0c959279256cIan Romanick case geometry_shader: 1861eee68d3631813580a14fa51fda6f0c959279256cIan Romanick _mesa_glsl_error(loc, state, 1862eee68d3631813580a14fa51fda6f0c959279256cIan Romanick "geometry shader variables cannot be given " 1863eee68d3631813580a14fa51fda6f0c959279256cIan Romanick "explicit locations\n"); 1864eee68d3631813580a14fa51fda6f0c959279256cIan Romanick break; 1865eee68d3631813580a14fa51fda6f0c959279256cIan Romanick 1866eee68d3631813580a14fa51fda6f0c959279256cIan Romanick case fragment_shader: 1867eee68d3631813580a14fa51fda6f0c959279256cIan Romanick if (!global_scope || (var->mode != ir_var_in)) { 1868eee68d3631813580a14fa51fda6f0c959279256cIan Romanick fail = true; 1869eee68d3631813580a14fa51fda6f0c959279256cIan Romanick string = "output"; 1870eee68d3631813580a14fa51fda6f0c959279256cIan Romanick } 1871eee68d3631813580a14fa51fda6f0c959279256cIan Romanick break; 1872a75da2c0e85eb6b8279ec895c3f74cc4aefc0257Kenneth Graunke }; 1873eee68d3631813580a14fa51fda6f0c959279256cIan Romanick 1874eee68d3631813580a14fa51fda6f0c959279256cIan Romanick if (fail) { 1875eee68d3631813580a14fa51fda6f0c959279256cIan Romanick _mesa_glsl_error(loc, state, 1876eee68d3631813580a14fa51fda6f0c959279256cIan Romanick "only %s shader %s variables can be given an " 1877eee68d3631813580a14fa51fda6f0c959279256cIan Romanick "explicit location\n", 1878eee68d3631813580a14fa51fda6f0c959279256cIan Romanick _mesa_glsl_shader_target_name(state->target), 1879eee68d3631813580a14fa51fda6f0c959279256cIan Romanick string); 1880eee68d3631813580a14fa51fda6f0c959279256cIan Romanick } else { 1881eee68d3631813580a14fa51fda6f0c959279256cIan Romanick var->explicit_location = true; 188268a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick 188368a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick /* This bit of silliness is needed because invalid explicit locations 188468a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick * are supposed to be flagged during linking. Small negative values 188568a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias 188668a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS). 188768a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick * The linker needs to be able to differentiate these cases. This 188868a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick * ensures that negative values stay negative. 188968a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick */ 189068a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick if (qual->location >= 0) { 189168a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick var->location = (state->target == vertex_shader) 189268a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick ? (qual->location + VERT_ATTRIB_GENERIC0) 189368a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick : (qual->location + FRAG_RESULT_DATA0); 189468a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick } else { 189568a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick var->location = qual->location; 189668a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick } 1897eee68d3631813580a14fa51fda6f0c959279256cIan Romanick } 1898eee68d3631813580a14fa51fda6f0c959279256cIan Romanick } 1899eee68d3631813580a14fa51fda6f0c959279256cIan Romanick 190010eaa8bcbce1cd6d2e120e913f7abafde9675215Kenneth Graunke if (var->type->is_array() && state->language_version != 110) { 19019d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick var->array_lvalue = true; 19029d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick } 1903a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick} 1904a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1905a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1906fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue * 19070044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_declarator_list::hir(exec_list *instructions, 190818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick struct _mesa_glsl_parse_state *state) 1909a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{ 1910953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke void *ctx = state; 1911a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick const struct glsl_type *decl_type; 1912a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick const char *type_name = NULL; 19138558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt ir_rvalue *result = NULL; 1914c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick YYLTYPE loc = this->get_location(); 1915a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 19166f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec: 19176f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick * 19186f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick * "To ensure that a particular output variable is invariant, it is 19196f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick * necessary to use the invariant qualifier. It can either be used to 19206f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick * qualify a previously declared variable as being invariant 19216f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick * 19226f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick * invariant gl_Position; // make existing gl_Position be invariant" 19236f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick * 19246f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick * In these cases the parser will set the 'invariant' flag in the declarator 19256f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick * list, and the type will be NULL. 19266f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick */ 19276f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick if (this->invariant) { 19286f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick assert(this->type == NULL); 19296f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick 19306f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick if (state->current_function != NULL) { 19316f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick _mesa_glsl_error(& loc, state, 19326f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick "All uses of `invariant' keyword must be at global " 19336f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick "scope\n"); 19346f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick } 19356f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick 19366f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick foreach_list_typed (ast_declaration, decl, link, &this->declarations) { 19376f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick assert(!decl->is_array); 19386f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick assert(decl->array_size == NULL); 19396f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick assert(decl->initializer == NULL); 19406f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick 19416f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick ir_variable *const earlier = 19426f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick state->symbols->get_variable(decl->identifier); 19436f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick if (earlier == NULL) { 19446f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick _mesa_glsl_error(& loc, state, 19456f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick "Undeclared variable `%s' cannot be marked " 19466f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick "invariant\n", decl->identifier); 19476f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick } else if ((state->target == vertex_shader) 19486f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick && (earlier->mode != ir_var_out)) { 19496f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick _mesa_glsl_error(& loc, state, 19506f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick "`%s' cannot be marked invariant, vertex shader " 19516f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick "outputs only\n", decl->identifier); 19526f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick } else if ((state->target == fragment_shader) 19536f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick && (earlier->mode != ir_var_in)) { 19546f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick _mesa_glsl_error(& loc, state, 19556f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick "`%s' cannot be marked invariant, fragment shader " 19566f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick "inputs only\n", decl->identifier); 19576f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick } else { 19586f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick earlier->invariant = true; 19596f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick } 19606f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick } 19616f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick 19626f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick /* Invariant redeclarations do not have r-values. 19636f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick */ 19646f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick return NULL; 19656f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick } 19666f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick 19676f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick assert(this->type != NULL); 19686f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick assert(!this->invariant); 19696f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick 19703455ce614424a5a23a23037e23d0454e476bceeaIan Romanick /* The type specifier may contain a structure definition. Process that 19713455ce614424a5a23a23037e23d0454e476bceeaIan Romanick * before any of the variable declarations. 19723455ce614424a5a23a23037e23d0454e476bceeaIan Romanick */ 19733455ce614424a5a23a23037e23d0454e476bceeaIan Romanick (void) this->type->specifier->hir(instructions, state); 19743455ce614424a5a23a23037e23d0454e476bceeaIan Romanick 1975d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick decl_type = this->type->specifier->glsl_type(& type_name, state); 1976304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick if (this->declarations.is_empty()) { 19776f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick /* The only valid case where the declaration list can be empty is when 19786f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick * the declaration is setting the default precision of a built-in type 19796f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick * (e.g., 'precision highp vec4;'). 1980c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick */ 1981c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick 19826f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick if (decl_type != NULL) { 1983c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick } else { 1984c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick _mesa_glsl_error(& loc, state, "incomplete declaration"); 1985c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick } 1986c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick } 1987a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 19882b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick foreach_list_typed (ast_declaration, decl, link, &this->declarations) { 1989a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick const struct glsl_type *var_type; 1990768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick ir_variable *var; 1991a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1992a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* FINISHME: Emit a warning if a variable declaration shadows a 1993a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * FINISHME: declaration at a higher scope. 1994a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 1995a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1996cec65a6b76290ee4da91691bd3ef01c3fb8a0c37Ian Romanick if ((decl_type == NULL) || decl_type->is_void()) { 1997a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick if (type_name != NULL) { 1998a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick _mesa_glsl_error(& loc, state, 1999a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick "invalid type `%s' in declaration of `%s'", 2000a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick type_name, decl->identifier); 2001a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } else { 2002a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick _mesa_glsl_error(& loc, state, 2003a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick "invalid type in declaration of `%s'", 2004a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick decl->identifier); 2005a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 2006a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick continue; 2007a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 2008a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 2009a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick if (decl->is_array) { 2010d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke var_type = process_array_type(&loc, decl_type, decl->array_size, 2011d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke state); 2012a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } else { 2013a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick var_type = decl_type; 2014a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 2015a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 20167e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick var = new(ctx) ir_variable(var_type, decl->identifier, ir_var_auto); 2017a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 20183f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification; 20193f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt * 20203f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt * "Global variables can only use the qualifiers const, 20213f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt * attribute, uni form, or varying. Only one may be 20223f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt * specified. 20233f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt * 20243f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt * Local variables can only use the qualifier const." 20253f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt * 20263f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt * This is relaxed in GLSL 1.30. 20273f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt */ 20283f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt if (state->language_version < 120) { 2029e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick if (this->type->qualifier.flags.q.out) { 20303f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt _mesa_glsl_error(& loc, state, 20313f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt "`out' qualifier in declaration of `%s' " 20323f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt "only valid for function parameters in GLSL 1.10.", 20333f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt decl->identifier); 20343f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt } 2035e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick if (this->type->qualifier.flags.q.in) { 20363f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt _mesa_glsl_error(& loc, state, 20373f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt "`in' qualifier in declaration of `%s' " 20383f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt "only valid for function parameters in GLSL 1.10.", 20393f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt decl->identifier); 20403f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt } 20413f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt /* FINISHME: Test for other invalid qualifiers. */ 20423f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt } 20433f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt 20442e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt apply_type_qualifier_to_variable(& this->type->qualifier, var, state, 20452e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt & loc); 2046a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 2047e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick if (this->type->qualifier.flags.q.invariant) { 2048046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt if ((state->target == vertex_shader) && !(var->mode == ir_var_out || 2049046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt var->mode == ir_var_inout)) { 2050046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt /* FINISHME: Note that this doesn't work for invariant on 2051046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt * a function signature outval 2052046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt */ 20536f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick _mesa_glsl_error(& loc, state, 20546f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick "`%s' cannot be marked invariant, vertex shader " 20556f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick "outputs only\n", var->name); 2056046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt } else if ((state->target == fragment_shader) && 2057046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt !(var->mode == ir_var_in || var->mode == ir_var_inout)) { 2058046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt /* FINISHME: Note that this doesn't work for invariant on 2059046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt * a function signature inval 2060046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt */ 20616f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick _mesa_glsl_error(& loc, state, 20626f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick "`%s' cannot be marked invariant, fragment shader " 20636f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick "inputs only\n", var->name); 20646f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick } 20656f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick } 20666f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick 2067e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick if (state->current_function != NULL) { 2068b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick const char *mode = NULL; 2069e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick const char *extra = ""; 2070b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick 2071e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick /* There is no need to check for 'inout' here because the parser will 2072e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick * only allow that in function parameter lists. 2073e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick */ 2074e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick if (this->type->qualifier.flags.q.attribute) { 2075b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick mode = "attribute"; 2076e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick } else if (this->type->qualifier.flags.q.uniform) { 2077b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick mode = "uniform"; 2078e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick } else if (this->type->qualifier.flags.q.varying) { 2079b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick mode = "varying"; 2080e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick } else if (this->type->qualifier.flags.q.in) { 2081e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick mode = "in"; 2082e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick extra = " or in function parameter list"; 2083e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick } else if (this->type->qualifier.flags.q.out) { 2084e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick mode = "out"; 2085e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick extra = " or in function parameter list"; 2086b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick } 2087b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick 2088b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick if (mode) { 2089e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick _mesa_glsl_error(& loc, state, 2090b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick "%s variable `%s' must be declared at " 2091e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick "global scope%s", 2092e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick mode, var->name, extra); 2093e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick } 2094e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick } else if (var->mode == ir_var_in) { 2095fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick if (state->target == vertex_shader) { 2096fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick bool error_emitted = false; 2097fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick 2098fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec: 2099fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick * 2100fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick * "Vertex shader inputs can only be float, floating-point 2101fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick * vectors, matrices, signed and unsigned integers and integer 2102fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick * vectors. Vertex shader inputs can also form arrays of these 2103fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick * types, but not structures." 2104fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick * 21052d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec: 21062d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick * 21072d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick * "Vertex shader inputs can only be float, floating-point 21082d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick * vectors, matrices, signed and unsigned integers and integer 21092d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick * vectors. They cannot be arrays or structures." 21102d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick * 2111fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec: 2112fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick * 2113fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick * "The attribute qualifier can be used only with float, 2114fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick * floating-point vectors, and matrices. Attribute variables 2115fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick * cannot be declared as arrays or structures." 2116fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick */ 2117fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick const glsl_type *check_type = var->type->is_array() 2118fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick ? var->type->fields.array : var->type; 2119fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick 2120fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick switch (check_type->base_type) { 2121fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick case GLSL_TYPE_FLOAT: 2122fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick break; 2123fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick case GLSL_TYPE_UINT: 2124fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick case GLSL_TYPE_INT: 2125fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick if (state->language_version > 120) 2126fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick break; 2127fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick /* FALLTHROUGH */ 2128fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick default: 2129fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick _mesa_glsl_error(& loc, state, 2130fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick "vertex shader input / attribute cannot have " 2131fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick "type %s`%s'", 2132fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick var->type->is_array() ? "array of " : "", 2133fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick check_type->name); 2134fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick error_emitted = true; 2135fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick } 2136fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick 21372d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick if (!error_emitted && (state->language_version <= 130) 2138fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick && var->type->is_array()) { 2139fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick _mesa_glsl_error(& loc, state, 2140fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick "vertex shader input / attribute cannot have " 2141fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick "array type"); 2142fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick error_emitted = true; 2143fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick } 2144fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick } 2145fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick } 2146fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick 2147e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick /* Process the initializer and add its instructions to a temporary 2148e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick * list. This list will be added to the instruction stream (below) after 2149e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick * the declaration is added. This is done because in some cases (such as 2150e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick * redeclarations) the declaration may not actually be added to the 2151e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick * instruction stream. 2152e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick */ 2153fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt exec_list initializer_instructions; 215466faec4895b7bb59a614087a200c05157191b4aeIan Romanick if (decl->initializer != NULL) { 215543de17282017bdf187d6e646de3262cc64b7f46bIan Romanick YYLTYPE initializer_loc = decl->initializer->get_location(); 215643de17282017bdf187d6e646de3262cc64b7f46bIan Romanick 215766faec4895b7bb59a614087a200c05157191b4aeIan Romanick /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec: 215866faec4895b7bb59a614087a200c05157191b4aeIan Romanick * 215966faec4895b7bb59a614087a200c05157191b4aeIan Romanick * "All uniform variables are read-only and are initialized either 216066faec4895b7bb59a614087a200c05157191b4aeIan Romanick * directly by an application via API commands, or indirectly by 216166faec4895b7bb59a614087a200c05157191b4aeIan Romanick * OpenGL." 216266faec4895b7bb59a614087a200c05157191b4aeIan Romanick */ 216366faec4895b7bb59a614087a200c05157191b4aeIan Romanick if ((state->language_version <= 110) 216466faec4895b7bb59a614087a200c05157191b4aeIan Romanick && (var->mode == ir_var_uniform)) { 216543de17282017bdf187d6e646de3262cc64b7f46bIan Romanick _mesa_glsl_error(& initializer_loc, state, 216643de17282017bdf187d6e646de3262cc64b7f46bIan Romanick "cannot initialize uniforms in GLSL 1.10"); 216743de17282017bdf187d6e646de3262cc64b7f46bIan Romanick } 216843de17282017bdf187d6e646de3262cc64b7f46bIan Romanick 216943de17282017bdf187d6e646de3262cc64b7f46bIan Romanick if (var->type->is_sampler()) { 217043de17282017bdf187d6e646de3262cc64b7f46bIan Romanick _mesa_glsl_error(& initializer_loc, state, 217143de17282017bdf187d6e646de3262cc64b7f46bIan Romanick "cannot initialize samplers"); 217243de17282017bdf187d6e646de3262cc64b7f46bIan Romanick } 217319360152f5bd8cff93359dbfe5a50a90b699c118Ian Romanick 217443de17282017bdf187d6e646de3262cc64b7f46bIan Romanick if ((var->mode == ir_var_in) && (state->current_function == NULL)) { 217543de17282017bdf187d6e646de3262cc64b7f46bIan Romanick _mesa_glsl_error(& initializer_loc, state, 217643de17282017bdf187d6e646de3262cc64b7f46bIan Romanick "cannot initialize %s shader input / %s", 2177ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick _mesa_glsl_shader_target_name(state->target), 217843de17282017bdf187d6e646de3262cc64b7f46bIan Romanick (state->target == vertex_shader) 217943de17282017bdf187d6e646de3262cc64b7f46bIan Romanick ? "attribute" : "varying"); 218066faec4895b7bb59a614087a200c05157191b4aeIan Romanick } 218166faec4895b7bb59a614087a200c05157191b4aeIan Romanick 21821660a2954797e056caba319c5d6c70b0d4be22feCarl Worth ir_dereference *const lhs = new(ctx) ir_dereference_variable(var); 2183fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt ir_rvalue *rhs = decl->initializer->hir(&initializer_instructions, 2184e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick state); 218519360152f5bd8cff93359dbfe5a50a90b699c118Ian Romanick 2186ce030884064046925a655413097dd8257e9392ddIan Romanick /* Calculate the constant value if this is a const or uniform 2187307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt * declaration. 218866faec4895b7bb59a614087a200c05157191b4aeIan Romanick */ 2189e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick if (this->type->qualifier.flags.q.constant 2190e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick || this->type->qualifier.flags.q.uniform) { 2191e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke ir_rvalue *new_rhs = validate_assignment(state, var->type, rhs); 2192e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke if (new_rhs != NULL) { 2193e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke rhs = new_rhs; 2194e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt 2195e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt ir_constant *constant_value = rhs->constant_expression_value(); 2196e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt if (!constant_value) { 2197e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt _mesa_glsl_error(& initializer_loc, state, 2198e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt "initializer of %s variable `%s' must be a " 2199e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt "constant expression", 2200e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick (this->type->qualifier.flags.q.constant) 2201e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt ? "const" : "uniform", 2202e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt decl->identifier); 2203e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt if (var->type->is_numeric()) { 2204e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt /* Reduce cascading errors. */ 2205e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt var->constant_value = ir_constant::zero(ctx, var->type); 2206e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt } 2207e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt } else { 2208e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt rhs = constant_value; 2209e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt var->constant_value = constant_value; 2210e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt } 2211e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke } else { 2212e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke _mesa_glsl_error(&initializer_loc, state, 2213e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke "initializer of type %s cannot be assigned to " 2214e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke "variable of type %s", 2215e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke rhs->type->name, var->type->name); 2216e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt if (var->type->is_numeric()) { 2217e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt /* Reduce cascading errors. */ 2218e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt var->constant_value = ir_constant::zero(ctx, var->type); 2219e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt } 2220307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt } 2221307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt } 222266faec4895b7bb59a614087a200c05157191b4aeIan Romanick 2223307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt if (rhs && !rhs->type->is_error()) { 2224ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt bool temp = var->read_only; 2225e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick if (this->type->qualifier.flags.q.constant) 2226ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt var->read_only = false; 2227ce030884064046925a655413097dd8257e9392ddIan Romanick 2228ce030884064046925a655413097dd8257e9392ddIan Romanick /* Never emit code to initialize a uniform. 2229ce030884064046925a655413097dd8257e9392ddIan Romanick */ 2230e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick if (!this->type->qualifier.flags.q.uniform) 2231fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt result = do_assignment(&initializer_instructions, state, 2232fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt lhs, rhs, 2233ce030884064046925a655413097dd8257e9392ddIan Romanick this->get_location()); 2234ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt var->read_only = temp; 223566faec4895b7bb59a614087a200c05157191b4aeIan Romanick } 223666faec4895b7bb59a614087a200c05157191b4aeIan Romanick } 223717d86f4371da413176ba365ca26a58bac172d365Ian Romanick 22380ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec: 22390ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt * 22400ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt * "It is an error to write to a const variable outside of 22410ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt * its declaration, so they must be initialized when 22420ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt * declared." 22430ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt */ 2244e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick if (this->type->qualifier.flags.q.constant && decl->initializer == NULL) { 22450ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt _mesa_glsl_error(& loc, state, 22460ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt "const declaration of `%s' must be initialized"); 22470ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt } 22480ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt 22495d25746640ee27882b69a962459727cf924443dbKenneth Graunke /* Check if this declaration is actually a re-declaration, either to 22505d25746640ee27882b69a962459727cf924443dbKenneth Graunke * resize an array or add qualifiers to an existing variable. 22515466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick * 2252a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke * This is allowed for variables in the current scope, or when at 2253a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke * global scope (for built-ins in the implicit outer scope). 22545466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick */ 22555d25746640ee27882b69a962459727cf924443dbKenneth Graunke ir_variable *earlier = state->symbols->get_variable(decl->identifier); 2256a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke if (earlier != NULL && (state->current_function == NULL || 2257a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke state->symbols->name_declared_this_scope(decl->identifier))) { 22585466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick 22595d25746640ee27882b69a962459727cf924443dbKenneth Graunke /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec, 22605d25746640ee27882b69a962459727cf924443dbKenneth Graunke * 22615d25746640ee27882b69a962459727cf924443dbKenneth Graunke * "It is legal to declare an array without a size and then 22625d25746640ee27882b69a962459727cf924443dbKenneth Graunke * later re-declare the same name as an array of the same 22635d25746640ee27882b69a962459727cf924443dbKenneth Graunke * type and specify a size." 22645d25746640ee27882b69a962459727cf924443dbKenneth Graunke */ 22655d25746640ee27882b69a962459727cf924443dbKenneth Graunke if ((earlier->type->array_size() == 0) 22665466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick && var->type->is_array() 22675466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick && (var->type->element_type() == earlier->type->element_type())) { 22685466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick /* FINISHME: This doesn't match the qualifiers on the two 22695466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick * FINISHME: declarations. It's not 100% clear whether this is 22705466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick * FINISHME: required or not. 22715466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick */ 22725466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick 2273cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec: 2274cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick * 2275cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick * "The size [of gl_TexCoord] can be at most 2276cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick * gl_MaxTextureCoords." 2277cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick */ 2278127308b4be077e5bdf60f76320307550921e86bbIan Romanick const unsigned size = unsigned(var->type->array_size()); 2279cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick if ((strcmp("gl_TexCoord", var->name) == 0) 2280127308b4be077e5bdf60f76320307550921e86bbIan Romanick && (size > state->Const.MaxTextureCoords)) { 2281cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick YYLTYPE loc = this->get_location(); 2282cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick 2283cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick _mesa_glsl_error(& loc, state, "`gl_TexCoord' array size cannot " 2284cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick "be larger than gl_MaxTextureCoords (%u)\n", 2285127308b4be077e5bdf60f76320307550921e86bbIan Romanick state->Const.MaxTextureCoords); 228612873fa4e332959295154edfe957c0af79af5e74Ian Romanick } else if ((size > 0) && (size <= earlier->max_array_access)) { 22875466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick YYLTYPE loc = this->get_location(); 22885466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick 22895466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick _mesa_glsl_error(& loc, state, "array size must be > %u due to " 22905466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick "previous access", 22915466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick earlier->max_array_access); 22925466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick } 22935466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick 22945466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick earlier->type = var->type; 22955466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick delete var; 22965466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick var = NULL; 22976e006273840282e06a08655553821ef8176b2d9cChad Versace } else if (state->ARB_fragment_coord_conventions_enable 22985d25746640ee27882b69a962459727cf924443dbKenneth Graunke && strcmp(var->name, "gl_FragCoord") == 0 22995d25746640ee27882b69a962459727cf924443dbKenneth Graunke && earlier->type == var->type 23005d25746640ee27882b69a962459727cf924443dbKenneth Graunke && earlier->mode == var->mode) { 23014a962170d7cf4243d6ae156fca20a6167388925dEric Anholt /* Allow redeclaration of gl_FragCoord for ARB_fcc layout 23024a962170d7cf4243d6ae156fca20a6167388925dEric Anholt * qualifiers. 23034a962170d7cf4243d6ae156fca20a6167388925dEric Anholt */ 23044a962170d7cf4243d6ae156fca20a6167388925dEric Anholt earlier->origin_upper_left = var->origin_upper_left; 23054a962170d7cf4243d6ae156fca20a6167388925dEric Anholt earlier->pixel_center_integer = var->pixel_center_integer; 23065466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick } else { 23075466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick YYLTYPE loc = this->get_location(); 23085d25746640ee27882b69a962459727cf924443dbKenneth Graunke _mesa_glsl_error(&loc, state, "`%s' redeclared", decl->identifier); 23095466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick } 23105466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick 23115466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick continue; 23125466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick } 23135466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick 23145d25746640ee27882b69a962459727cf924443dbKenneth Graunke /* By now, we know it's a new variable declaration (we didn't hit the 23155d25746640ee27882b69a962459727cf924443dbKenneth Graunke * above "continue"). 23165d25746640ee27882b69a962459727cf924443dbKenneth Graunke * 23175d25746640ee27882b69a962459727cf924443dbKenneth Graunke * From page 15 (page 21 of the PDF) of the GLSL 1.10 spec, 23185466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick * 23195466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick * "Identifiers starting with "gl_" are reserved for use by 23205466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick * OpenGL, and may not be declared in a shader as either a 23215466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick * variable or a function." 23225466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick */ 2323de3b40d8cdc42cc1cd71dd65c90d6d569d922fc6Ian Romanick if (strncmp(decl->identifier, "gl_", 3) == 0) 23245466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick _mesa_glsl_error(& loc, state, 23255466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick "identifier `%s' uses reserved `gl_' prefix", 23265466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick decl->identifier); 23275466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick 23285d25746640ee27882b69a962459727cf924443dbKenneth Graunke /* Add the variable to the symbol table. Note that the initializer's 23295d25746640ee27882b69a962459727cf924443dbKenneth Graunke * IR was already processed earlier (though it hasn't been emitted yet), 23305d25746640ee27882b69a962459727cf924443dbKenneth Graunke * without the variable in scope. 23315d25746640ee27882b69a962459727cf924443dbKenneth Graunke * 23325d25746640ee27882b69a962459727cf924443dbKenneth Graunke * This differs from most C-like languages, but it follows the GLSL 23335d25746640ee27882b69a962459727cf924443dbKenneth Graunke * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50 23345d25746640ee27882b69a962459727cf924443dbKenneth Graunke * spec: 23355d25746640ee27882b69a962459727cf924443dbKenneth Graunke * 23365d25746640ee27882b69a962459727cf924443dbKenneth Graunke * "Within a declaration, the scope of a name starts immediately 23375d25746640ee27882b69a962459727cf924443dbKenneth Graunke * after the initializer if present or immediately after the name 23385d25746640ee27882b69a962459727cf924443dbKenneth Graunke * being declared if not." 23395d25746640ee27882b69a962459727cf924443dbKenneth Graunke */ 2340001eee52d461233b1e1d6ed3577965e9bcb209e8Eric Anholt if (!state->symbols->add_variable(var)) { 23415d25746640ee27882b69a962459727cf924443dbKenneth Graunke YYLTYPE loc = this->get_location(); 23425d25746640ee27882b69a962459727cf924443dbKenneth Graunke _mesa_glsl_error(&loc, state, "name `%s' already taken in the " 23435d25746640ee27882b69a962459727cf924443dbKenneth Graunke "current scope", decl->identifier); 23445d25746640ee27882b69a962459727cf924443dbKenneth Graunke continue; 23455d25746640ee27882b69a962459727cf924443dbKenneth Graunke } 23465d25746640ee27882b69a962459727cf924443dbKenneth Graunke 23478048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt /* Push the variable declaration to the top. It means that all 23488048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt * the variable declarations will appear in a funny 23498048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt * last-to-first order, but otherwise we run into trouble if a 23508048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt * function is prototyped, a global var is decled, then the 23518048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt * function is defined with usage of the global var. See 23528048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt * glslparsertest's CorrectModule.frag. 23538048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt */ 23548048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt instructions->push_head(var); 2355fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt instructions->append_list(&initializer_instructions); 2356a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 2357a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 23588558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt 23598558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt /* Generally, variable declarations do not have r-values. However, 23608558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt * one is used for the declaration in 23618558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt * 23628558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt * while (bool b = some_condition()) { 23638558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt * ... 23648558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt * } 23658558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt * 23668558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt * so we return the rvalue from the last seen declaration here. 2367a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 23688558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt return result; 2369a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick} 2370a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 2371a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 2372fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue * 23730044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_parameter_declarator::hir(exec_list *instructions, 237418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick struct _mesa_glsl_parse_state *state) 2375a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{ 2376953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke void *ctx = state; 2377a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick const struct glsl_type *type; 2378a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick const char *name = NULL; 23792e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt YYLTYPE loc = this->get_location(); 2380a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 2381d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick type = this->type->specifier->glsl_type(& name, state); 2382a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 2383a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick if (type == NULL) { 2384a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick if (name != NULL) { 2385a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick _mesa_glsl_error(& loc, state, 2386a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick "invalid type `%s' in declaration of `%s'", 238718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick name, this->identifier); 2388a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } else { 2389a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick _mesa_glsl_error(& loc, state, 2390a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick "invalid type in declaration of `%s'", 239118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick this->identifier); 2392a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 2393a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 23940471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick type = glsl_type::error_type; 2395a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 2396a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 2397068c80cfe0a280490353b6b007165d717c672eedEric Anholt /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec: 2398068c80cfe0a280490353b6b007165d717c672eedEric Anholt * 2399068c80cfe0a280490353b6b007165d717c672eedEric Anholt * "Functions that accept no input arguments need not use void in the 2400068c80cfe0a280490353b6b007165d717c672eedEric Anholt * argument list because prototypes (or definitions) are required and 2401068c80cfe0a280490353b6b007165d717c672eedEric Anholt * therefore there is no ambiguity when an empty argument list "( )" is 2402068c80cfe0a280490353b6b007165d717c672eedEric Anholt * declared. The idiom "(void)" as a parameter list is provided for 2403068c80cfe0a280490353b6b007165d717c672eedEric Anholt * convenience." 2404068c80cfe0a280490353b6b007165d717c672eedEric Anholt * 2405068c80cfe0a280490353b6b007165d717c672eedEric Anholt * Placing this check here prevents a void parameter being set up 2406068c80cfe0a280490353b6b007165d717c672eedEric Anholt * for a function, which avoids tripping up checks for main taking 2407068c80cfe0a280490353b6b007165d717c672eedEric Anholt * parameters and lookups of an unnamed symbol. 2408068c80cfe0a280490353b6b007165d717c672eedEric Anholt */ 2409cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick if (type->is_void()) { 2410cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick if (this->identifier != NULL) 2411cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick _mesa_glsl_error(& loc, state, 2412cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick "named parameter cannot have type `void'"); 2413cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick 2414cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick is_void = true; 2415068c80cfe0a280490353b6b007165d717c672eedEric Anholt return NULL; 2416cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick } 2417068c80cfe0a280490353b6b007165d717c672eedEric Anholt 241845d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick if (formal_parameter && (this->identifier == NULL)) { 241945d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick _mesa_glsl_error(& loc, state, "formal parameter lacks a name"); 242045d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick return NULL; 242145d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick } 242245d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick 2423e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...) 2424e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke * call already handled the "vec4[..] foo" case. 2425e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke */ 2426e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke if (this->is_array) { 2427d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke type = process_array_type(&loc, type, this->array_size, state); 2428e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke } 2429e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke 2430e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke if (type->array_size() == 0) { 2431e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke _mesa_glsl_error(&loc, state, "arrays passed as parameters must have " 2432e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke "a declared size."); 2433e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke type = glsl_type::error_type; 2434e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke } 2435e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke 2436cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick is_void = false; 24377e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick ir_variable *var = new(ctx) ir_variable(type, this->identifier, ir_var_in); 2438a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 2439cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick /* Apply any specified qualifiers to the parameter declaration. Note that 2440cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick * for function parameters the default mode is 'in'. 2441cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick */ 24422e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc); 2443a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 24440044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick instructions->push_tail(var); 2445a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 2446a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* Parameter declarations do not have r-values. 2447a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 2448a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick return NULL; 2449a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick} 2450a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 2451a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 245245d8a70c12ee6ea956baaf898324a828496382f6Ian Romanickvoid 2453304ea90233baeac6801a98e981658cb7a2d2501cIan Romanickast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters, 245445d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick bool formal, 245545d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick exec_list *ir_parameters, 245645d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick _mesa_glsl_parse_state *state) 2457a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{ 2458cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick ast_parameter_declarator *void_param = NULL; 2459cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick unsigned count = 0; 2460a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 24612b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) { 246245d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick param->formal_parameter = formal; 2463068c80cfe0a280490353b6b007165d717c672eedEric Anholt param->hir(ir_parameters, state); 2464cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick 2465cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick if (param->is_void) 2466cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick void_param = param; 2467cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick 2468cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick count++; 2469cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick } 2470cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick 2471cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick if ((void_param != NULL) && (count > 1)) { 2472cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick YYLTYPE loc = void_param->get_location(); 2473cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick 2474cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick _mesa_glsl_error(& loc, state, 2475cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick "`void' parameter must be only parameter"); 2476a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 2477a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick} 2478a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 2479a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 24806fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunkevoid 24816fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunkeemit_function(_mesa_glsl_parse_state *state, exec_list *instructions, 24826fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke ir_function *f) 24836fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke{ 24846fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke /* Emit the new function header */ 24856fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke if (state->current_function == NULL) { 24866fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke instructions->push_tail(f); 24876fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke } else { 24886fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke /* IR invariants disallow function declarations or definitions nested 24896fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke * within other function definitions. Insert the new ir_function 24906fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke * block in the instruction sequence before the ir_function block 24916fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke * containing the current ir_function_signature. 24926fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke */ 24936fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke ir_function *const curr = 24946fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke const_cast<ir_function *>(state->current_function->function()); 24956fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke 24966fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke curr->insert_before(f); 24976fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke } 24986fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke} 24996fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke 25006fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke 2501fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue * 250292318a947958892497722772b03c643ebc943294Ian Romanickast_function::hir(exec_list *instructions, 250392318a947958892497722772b03c643ebc943294Ian Romanick struct _mesa_glsl_parse_state *state) 2504a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{ 2505953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke void *ctx = state; 250618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick ir_function *f = NULL; 250792318a947958892497722772b03c643ebc943294Ian Romanick ir_function_signature *sig = NULL; 250892318a947958892497722772b03c643ebc943294Ian Romanick exec_list hir_parameters; 2509a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 2510ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke const char *const name = identifier; 2511a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 251263b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec, 251363b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick * 251463b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick * "Function declarations (prototypes) cannot occur inside of functions; 251563b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick * they must be at global scope, or for the built-in functions, outside 251663b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick * the global scope." 251763b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick * 251863b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec, 251963b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick * 252063b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick * "User defined functions may only be defined within the global scope." 252163b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick * 252263b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick * Note that this language does not appear in GLSL 1.10. 252363b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick */ 252463b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick if ((state->current_function != NULL) && (state->language_version != 110)) { 252563b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick YYLTYPE loc = this->get_location(); 252663b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick _mesa_glsl_error(&loc, state, 252763b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick "declaration of function `%s' not allowed within " 252863b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick "function body", name); 252963b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick } 253063b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick 2531edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec, 2532edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke * 2533edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke * "Identifiers starting with "gl_" are reserved for use by 2534edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke * OpenGL, and may not be declared in a shader as either a 2535edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke * variable or a function." 2536edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke */ 2537edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke if (strncmp(name, "gl_", 3) == 0) { 2538edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke YYLTYPE loc = this->get_location(); 2539edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke _mesa_glsl_error(&loc, state, 2540edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke "identifier `%s' uses reserved `gl_' prefix", name); 2541edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke } 2542edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke 2543a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* Convert the list of function parameters to HIR now so that they can be 2544a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * used below to compare this function's signature with previously seen 2545a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * signatures for functions with the same name. 2546a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 254745d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick ast_parameter_declarator::parameters_to_hir(& this->parameters, 254845d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick is_definition, 254945d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick & hir_parameters, state); 2550a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 2551e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick const char *return_type_name; 2552e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick const glsl_type *return_type = 255392318a947958892497722772b03c643ebc943294Ian Romanick this->return_type->specifier->glsl_type(& return_type_name, state); 2554e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick 255576e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt if (!return_type) { 255676e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt YYLTYPE loc = this->get_location(); 255776e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt _mesa_glsl_error(&loc, state, 255876e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt "function `%s' has undeclared return type `%s'", 255976e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt name, return_type_name); 256076e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt return_type = glsl_type::error_type; 256176e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt } 2562e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick 2563ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec: 2564ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke * "No qualifier is allowed on the return type of a function." 2565ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke */ 2566ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke if (this->return_type->has_qualifiers()) { 2567ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke YYLTYPE loc = this->get_location(); 2568ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke _mesa_glsl_error(& loc, state, 2569ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke "function `%s' return type has qualifiers", name); 2570ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke } 2571ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke 2572a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* Verify that this function's signature either doesn't match a previously 2573a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * seen signature for a function with the same name, or, if a match is found, 2574a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * that the previously seen signature does not have an associated definition. 2575a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 2576e466b182bbf21f62fe6542091f4af3275555db80Ian Romanick f = state->symbols->get_function(name); 257781f03393982c29f8f4165b5629c8e8fb708b97a3Kenneth Graunke if (f != NULL && (state->es_shader || f->has_user_signature())) { 2578202604e8160157e4e80b3458175e0170d168e557Ian Romanick sig = f->exact_matching_signature(&hir_parameters); 25790d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke if (sig != NULL) { 25800d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke const char *badvar = sig->qualifiers_match(&hir_parameters); 25810d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke if (badvar != NULL) { 25820d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke YYLTYPE loc = this->get_location(); 2583abd40b15210c17b2a3ba8fcffc868fda203efa01Kenneth Graunke 25840d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' " 25850d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke "qualifiers don't match prototype", name, badvar); 25860d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke } 25871e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt 25880d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke if (sig->return_type != return_type) { 25890d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke YYLTYPE loc = this->get_location(); 259060be7626b829af7e1d07330b9a88468924ba350eEric Anholt 25910d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke _mesa_glsl_error(&loc, state, "function `%s' return type doesn't " 25920d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke "match prototype", name); 25930d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke } 2594a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 25950d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke if (is_definition && sig->is_defined) { 25960d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke YYLTYPE loc = this->get_location(); 2597a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 25980d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke _mesa_glsl_error(& loc, state, "function `%s' redefined", name); 2599a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 2600a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 2601a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } else { 26021660a2954797e056caba319c5d6c70b0d4be22feCarl Worth f = new(ctx) ir_function(name); 2603e8f5ebf313da3ce33ccbbcf9b72946853035fbddEric Anholt if (!state->symbols->add_function(f)) { 2604e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke /* This function name shadows a non-function use of the same name. */ 2605e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke YYLTYPE loc = this->get_location(); 2606e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke 2607e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke _mesa_glsl_error(&loc, state, "function name `%s' conflicts with " 2608e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke "non-function", name); 2609e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke return NULL; 2610e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke } 26119fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke 26126fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke emit_function(state, instructions, f); 2613a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 2614a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 2615ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt /* Verify the return type of main() */ 2616ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt if (strcmp(name, "main") == 0) { 261725711a85c22bed305c9b52b89feb9c600d1892dfIan Romanick if (! return_type->is_void()) { 2618ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt YYLTYPE loc = this->get_location(); 2619ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt 2620ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt _mesa_glsl_error(& loc, state, "main() must return void"); 2621ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt } 2622174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt 262392318a947958892497722772b03c643ebc943294Ian Romanick if (!hir_parameters.is_empty()) { 2624174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt YYLTYPE loc = this->get_location(); 2625174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt 2626174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt _mesa_glsl_error(& loc, state, "main() must not take any parameters"); 2627174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt } 2628ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt } 2629a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 2630a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* Finish storing the information about this new function in its signature. 2631a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 263292318a947958892497722772b03c643ebc943294Ian Romanick if (sig == NULL) { 26331660a2954797e056caba319c5d6c70b0d4be22feCarl Worth sig = new(ctx) ir_function_signature(return_type); 263492318a947958892497722772b03c643ebc943294Ian Romanick f->add_signature(sig); 2635a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 2636a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 2637bff6013d469b3d4e54cdc5731801c56994a523ecKenneth Graunke sig->replace_parameters(&hir_parameters); 263892318a947958892497722772b03c643ebc943294Ian Romanick signature = sig; 263992318a947958892497722772b03c643ebc943294Ian Romanick 264092318a947958892497722772b03c643ebc943294Ian Romanick /* Function declarations (prototypes) do not have r-values. 264192318a947958892497722772b03c643ebc943294Ian Romanick */ 264292318a947958892497722772b03c643ebc943294Ian Romanick return NULL; 264392318a947958892497722772b03c643ebc943294Ian Romanick} 264492318a947958892497722772b03c643ebc943294Ian Romanick 264592318a947958892497722772b03c643ebc943294Ian Romanick 264692318a947958892497722772b03c643ebc943294Ian Romanickir_rvalue * 264792318a947958892497722772b03c643ebc943294Ian Romanickast_function_definition::hir(exec_list *instructions, 264892318a947958892497722772b03c643ebc943294Ian Romanick struct _mesa_glsl_parse_state *state) 264992318a947958892497722772b03c643ebc943294Ian Romanick{ 265092318a947958892497722772b03c643ebc943294Ian Romanick prototype->is_definition = true; 265192318a947958892497722772b03c643ebc943294Ian Romanick prototype->hir(instructions, state); 2652e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick 265392318a947958892497722772b03c643ebc943294Ian Romanick ir_function_signature *signature = prototype->signature; 2654826a39cb14244820e8539a2328bb52447348f184Kenneth Graunke if (signature == NULL) 2655826a39cb14244820e8539a2328bb52447348f184Kenneth Graunke return NULL; 2656a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 265741ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick assert(state->current_function == NULL); 265841ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick state->current_function = signature; 26596de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke state->found_return = false; 266041ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick 2661e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick /* Duplicate parameters declared in the prototype as concrete variables. 2662e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick * Add these to the symbol table. 2663a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 26648bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick state->symbols->push_scope(); 2665e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick foreach_iter(exec_list_iterator, iter, signature->parameters) { 2666fbc7c0b8f2e161bce1c048c63d2d5cfcdeb096f1Eric Anholt ir_variable *const var = ((ir_instruction *) iter.get())->as_variable(); 2667e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick 2668fbc7c0b8f2e161bce1c048c63d2d5cfcdeb096f1Eric Anholt assert(var != NULL); 2669a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 26703359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick /* The only way a parameter would "exist" is if two parameters have 26713359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick * the same name. 26723359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick */ 26733359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick if (state->symbols->name_declared_this_scope(var->name)) { 26743359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick YYLTYPE loc = this->get_location(); 26753359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick 26763359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name); 26773359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick } else { 2678001eee52d461233b1e1d6ed3577965e9bcb209e8Eric Anholt state->symbols->add_variable(var); 26793359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick } 2680a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 2681a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 26829fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke /* Convert the body of the function to HIR. */ 2683894ea972a4defdaafeaa3a248c113b06c7ae0c7eEric Anholt this->body->hir(&signature->body, state); 26849fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke signature->is_defined = true; 2685a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 26868bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick state->symbols->pop_scope(); 2687a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 268841ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick assert(state->current_function == signature); 268941ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick state->current_function = NULL; 2690a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 26916de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke if (!signature->return_type->is_void() && !state->found_return) { 26926de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke YYLTYPE loc = this->get_location(); 26936de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke _mesa_glsl_error(& loc, state, "function `%s' has non-void return type " 26946de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke "%s, but no return statement", 26956de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke signature->function_name(), 26966de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke signature->return_type->name); 26976de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke } 26986de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke 2699a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* Function definitions do not have r-values. 2700a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 2701a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick return NULL; 2702a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick} 270316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick 270416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick 2705fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue * 270616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanickast_jump_statement::hir(exec_list *instructions, 270716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick struct _mesa_glsl_parse_state *state) 270816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick{ 2709953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke void *ctx = state; 271016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick 2711c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick switch (mode) { 2712c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick case ast_return: { 271316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick ir_return *inst; 2714aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt assert(state->current_function); 271516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick 271616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick if (opt_return_value) { 2717ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt if (state->current_function->return_type->base_type == 2718ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt GLSL_TYPE_VOID) { 2719ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt YYLTYPE loc = this->get_location(); 2720ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt 2721ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt _mesa_glsl_error(& loc, state, 2722ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt "`return` with a value, in function `%s' " 2723ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt "returning void", 2724f96c52ba2e290e3ba5f14cd7f87ba5b4382a1785Kenneth Graunke state->current_function->function_name()); 2725ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt } 272616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick 2727b4cdba687c098eea2ecc61349a4ea02a8769909eChad Versace ir_rvalue *const ret = opt_return_value->hir(instructions, state); 272816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick assert(ret != NULL); 272916a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick 273018707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke /* Implicit conversions are not allowed for return values. */ 273118707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke if (state->current_function->return_type != ret->type) { 273218707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke YYLTYPE loc = this->get_location(); 273318707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke 273418707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke _mesa_glsl_error(& loc, state, 273518707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke "`return' with wrong type %s, in function `%s' " 273618707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke "returning %s", 273718707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke ret->type->name, 273818707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke state->current_function->function_name(), 273918707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke state->current_function->return_type->name); 274018707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke } 274116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick 27421660a2954797e056caba319c5d6c70b0d4be22feCarl Worth inst = new(ctx) ir_return(ret); 274316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick } else { 2744aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt if (state->current_function->return_type->base_type != 2745aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt GLSL_TYPE_VOID) { 2746aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt YYLTYPE loc = this->get_location(); 2747aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt 2748aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt _mesa_glsl_error(& loc, state, 2749aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt "`return' with no value, in function %s returning " 2750aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt "non-void", 2751f96c52ba2e290e3ba5f14cd7f87ba5b4382a1785Kenneth Graunke state->current_function->function_name()); 2752aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt } 27531660a2954797e056caba319c5d6c70b0d4be22feCarl Worth inst = new(ctx) ir_return; 275416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick } 275516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick 27566de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke state->found_return = true; 275716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick instructions->push_tail(inst); 2758c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick break; 275916a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick } 276016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick 2761c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick case ast_discard: 2762b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt if (state->target != fragment_shader) { 2763b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt YYLTYPE loc = this->get_location(); 2764b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt 2765b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt _mesa_glsl_error(& loc, state, 2766b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt "`discard' may only appear in a fragment shader"); 2767b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt } 276877049a702ad54e09c4102fe8c964e069944f83e5Kenneth Graunke instructions->push_tail(new(ctx) ir_discard); 2769c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick break; 2770c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick 2771c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick case ast_break: 2772c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick case ast_continue: 27734cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick /* FINISHME: Handle switch-statements. They cannot contain 'continue', 27744cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick * FINISHME: and they use a different IR instruction for 'break'. 27754cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick */ 27764cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick /* FINISHME: Correctly handle the nesting. If a switch-statement is 27774cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick * FINISHME: inside a loop, a 'continue' is valid and will bind to the 27784cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick * FINISHME: loop. 27794cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick */ 27804cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick if (state->loop_or_switch_nesting == NULL) { 27814cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick YYLTYPE loc = this->get_location(); 27824cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick 27834cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick _mesa_glsl_error(& loc, state, 27844cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick "`%s' may only appear in a loop", 27854cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick (mode == ast_break) ? "break" : "continue"); 27864cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick } else { 27874cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick ir_loop *const loop = state->loop_or_switch_nesting->as_loop(); 27884cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick 27892d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt /* Inline the for loop expression again, since we don't know 27902d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt * where near the end of the loop body the normal copy of it 27912d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt * is going to be placed. 27922d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt */ 27932d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt if (mode == ast_continue && 27942d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt state->loop_or_switch_nesting_ast->rest_expression) { 27952d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt state->loop_or_switch_nesting_ast->rest_expression->hir(instructions, 27962d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt state); 27972d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt } 27982d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt 27994cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick if (loop != NULL) { 28004cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick ir_loop_jump *const jump = 28011660a2954797e056caba319c5d6c70b0d4be22feCarl Worth new(ctx) ir_loop_jump((mode == ast_break) 28021660a2954797e056caba319c5d6c70b0d4be22feCarl Worth ? ir_loop_jump::jump_break 28031660a2954797e056caba319c5d6c70b0d4be22feCarl Worth : ir_loop_jump::jump_continue); 28044cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick instructions->push_tail(jump); 28054cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick } 28064cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick } 28074cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick 2808c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick break; 2809b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt } 2810b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt 281116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick /* Jump instructions do not have r-values. 281216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick */ 281316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick return NULL; 281416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick} 28153c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick 28163c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick 28173c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanickir_rvalue * 28183c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanickast_selection_statement::hir(exec_list *instructions, 28193c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick struct _mesa_glsl_parse_state *state) 28203c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick{ 2821953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke void *ctx = state; 28221660a2954797e056caba319c5d6c70b0d4be22feCarl Worth 28233c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick ir_rvalue *const condition = this->condition->hir(instructions, state); 28243c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick 28253c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec: 28263c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick * 28273c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick * "Any expression whose type evaluates to a Boolean can be used as the 28283c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick * conditional expression bool-expression. Vector types are not accepted 28293c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick * as the expression to if." 28303c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick * 28313c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick * The checks are separated so that higher quality diagnostics can be 28323c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick * generated for cases where both rules are violated. 28333c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick */ 28343c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick if (!condition->type->is_boolean() || !condition->type->is_scalar()) { 28353c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick YYLTYPE loc = this->condition->get_location(); 28363c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick 28373c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick _mesa_glsl_error(& loc, state, "if-statement condition must be scalar " 28383c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick "boolean"); 28393c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick } 28403c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick 28411660a2954797e056caba319c5d6c70b0d4be22feCarl Worth ir_if *const stmt = new(ctx) ir_if(condition); 28423c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick 2843665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke if (then_statement != NULL) { 2844665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke state->symbols->push_scope(); 28454f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick then_statement->hir(& stmt->then_instructions, state); 2846665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke state->symbols->pop_scope(); 2847665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke } 28483c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick 2849665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke if (else_statement != NULL) { 2850665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke state->symbols->push_scope(); 28514f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick else_statement->hir(& stmt->else_instructions, state); 2852665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke state->symbols->pop_scope(); 2853665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke } 28543c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick 28553c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick instructions->push_tail(stmt); 28563c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick 28573c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick /* if-statements do not have r-values. 28583c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick */ 28593c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick return NULL; 28603c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick} 28619e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick 28629e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick 28638c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickvoid 28648c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickast_iteration_statement::condition_to_hir(ir_loop *stmt, 28658c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick struct _mesa_glsl_parse_state *state) 28669e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick{ 2867953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke void *ctx = state; 28681660a2954797e056caba319c5d6c70b0d4be22feCarl Worth 28699e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick if (condition != NULL) { 28709e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick ir_rvalue *const cond = 28719e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick condition->hir(& stmt->body_instructions, state); 28729e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick 28739e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick if ((cond == NULL) 28749e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick || !cond->type->is_boolean() || !cond->type->is_scalar()) { 28759e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick YYLTYPE loc = condition->get_location(); 28769e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick 28779e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick _mesa_glsl_error(& loc, state, 28789e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick "loop condition must be scalar boolean"); 28799e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick } else { 28809e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick /* As the first code in the loop body, generate a block that looks 28819e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick * like 'if (!condition) break;' as the loop termination condition. 28829e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick */ 28839e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick ir_rvalue *const not_cond = 28841660a2954797e056caba319c5d6c70b0d4be22feCarl Worth new(ctx) ir_expression(ir_unop_logic_not, glsl_type::bool_type, cond, 28851660a2954797e056caba319c5d6c70b0d4be22feCarl Worth NULL); 28869e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick 28871660a2954797e056caba319c5d6c70b0d4be22feCarl Worth ir_if *const if_stmt = new(ctx) ir_if(not_cond); 28889e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick 28899e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick ir_jump *const break_stmt = 28901660a2954797e056caba319c5d6c70b0d4be22feCarl Worth new(ctx) ir_loop_jump(ir_loop_jump::jump_break); 28919e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick 28929e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick if_stmt->then_instructions.push_tail(break_stmt); 28939e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick stmt->body_instructions.push_tail(if_stmt); 28949e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick } 28959e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick } 28968c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick} 28978c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick 28988c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick 28998c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickir_rvalue * 29008c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickast_iteration_statement::hir(exec_list *instructions, 29018c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick struct _mesa_glsl_parse_state *state) 29028c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick{ 2903953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke void *ctx = state; 29041660a2954797e056caba319c5d6c70b0d4be22feCarl Worth 2905484606610e36ec7598f7733e9787d888b6a63d64Ian Romanick /* For-loops and while-loops start a new scope, but do-while loops do not. 29068c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick */ 2907484606610e36ec7598f7733e9787d888b6a63d64Ian Romanick if (mode != ast_do_while) 29088c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick state->symbols->push_scope(); 29098c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick 29108c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick if (init_statement != NULL) 29118c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick init_statement->hir(instructions, state); 29128c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick 29131660a2954797e056caba319c5d6c70b0d4be22feCarl Worth ir_loop *const stmt = new(ctx) ir_loop(); 29148c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick instructions->push_tail(stmt); 29158c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick 29168c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick /* Track the current loop and / or switch-statement nesting. 29178c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick */ 29188c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick ir_instruction *const nesting = state->loop_or_switch_nesting; 29192d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt ast_iteration_statement *nesting_ast = state->loop_or_switch_nesting_ast; 29202d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt 29218c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick state->loop_or_switch_nesting = stmt; 29222d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt state->loop_or_switch_nesting_ast = this; 29238c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick 29248c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick if (mode != ast_do_while) 29258c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick condition_to_hir(stmt, state); 29269e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick 29274f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick if (body != NULL) 29284f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick body->hir(& stmt->body_instructions, state); 29299e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick 29309e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick if (rest_expression != NULL) 29319e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick rest_expression->hir(& stmt->body_instructions, state); 29329e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick 29338c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick if (mode == ast_do_while) 29348c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick condition_to_hir(stmt, state); 29358c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick 2936484606610e36ec7598f7733e9787d888b6a63d64Ian Romanick if (mode != ast_do_while) 29379e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick state->symbols->pop_scope(); 29389e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick 2939e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick /* Restore previous nesting before returning. 2940e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick */ 2941e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick state->loop_or_switch_nesting = nesting; 29422d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt state->loop_or_switch_nesting_ast = nesting_ast; 2943e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick 29449e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick /* Loops do not have r-values. 29459e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick */ 29469e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick return NULL; 29479e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick} 29483455ce614424a5a23a23037e23d0454e476bceeaIan Romanick 29493455ce614424a5a23a23037e23d0454e476bceeaIan Romanick 29503455ce614424a5a23a23037e23d0454e476bceeaIan Romanickir_rvalue * 29513455ce614424a5a23a23037e23d0454e476bceeaIan Romanickast_type_specifier::hir(exec_list *instructions, 29523455ce614424a5a23a23037e23d0454e476bceeaIan Romanick struct _mesa_glsl_parse_state *state) 29533455ce614424a5a23a23037e23d0454e476bceeaIan Romanick{ 29543455ce614424a5a23a23037e23d0454e476bceeaIan Romanick if (this->structure != NULL) 29553455ce614424a5a23a23037e23d0454e476bceeaIan Romanick return this->structure->hir(instructions, state); 295685ba37b97df8edd18b757bc475b12b66f4b117edIan Romanick 295785ba37b97df8edd18b757bc475b12b66f4b117edIan Romanick return NULL; 29583455ce614424a5a23a23037e23d0454e476bceeaIan Romanick} 29593455ce614424a5a23a23037e23d0454e476bceeaIan Romanick 29603455ce614424a5a23a23037e23d0454e476bceeaIan Romanick 29613455ce614424a5a23a23037e23d0454e476bceeaIan Romanickir_rvalue * 29623455ce614424a5a23a23037e23d0454e476bceeaIan Romanickast_struct_specifier::hir(exec_list *instructions, 29633455ce614424a5a23a23037e23d0454e476bceeaIan Romanick struct _mesa_glsl_parse_state *state) 29643455ce614424a5a23a23037e23d0454e476bceeaIan Romanick{ 29653455ce614424a5a23a23037e23d0454e476bceeaIan Romanick unsigned decl_count = 0; 29663455ce614424a5a23a23037e23d0454e476bceeaIan Romanick 29673455ce614424a5a23a23037e23d0454e476bceeaIan Romanick /* Make an initial pass over the list of structure fields to determine how 29683455ce614424a5a23a23037e23d0454e476bceeaIan Romanick * many there are. Each element in this list is an ast_declarator_list. 29693455ce614424a5a23a23037e23d0454e476bceeaIan Romanick * This means that we actually need to count the number of elements in the 29703455ce614424a5a23a23037e23d0454e476bceeaIan Romanick * 'declarations' list in each of the elements. 29713455ce614424a5a23a23037e23d0454e476bceeaIan Romanick */ 29722b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick foreach_list_typed (ast_declarator_list, decl_list, link, 29732b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick &this->declarations) { 2974304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick foreach_list_const (decl_ptr, & decl_list->declarations) { 29753455ce614424a5a23a23037e23d0454e476bceeaIan Romanick decl_count++; 29763455ce614424a5a23a23037e23d0454e476bceeaIan Romanick } 29773455ce614424a5a23a23037e23d0454e476bceeaIan Romanick } 29783455ce614424a5a23a23037e23d0454e476bceeaIan Romanick 29793455ce614424a5a23a23037e23d0454e476bceeaIan Romanick /* Allocate storage for the structure fields and process the field 29803455ce614424a5a23a23037e23d0454e476bceeaIan Romanick * declarations. As the declarations are processed, try to also convert 29813455ce614424a5a23a23037e23d0454e476bceeaIan Romanick * the types to HIR. This ensures that structure definitions embedded in 29823455ce614424a5a23a23037e23d0454e476bceeaIan Romanick * other structure definitions are processed. 29833455ce614424a5a23a23037e23d0454e476bceeaIan Romanick */ 298421b0dbd79937e9d6787f045af7d60d4b6c649ec8Eric Anholt glsl_struct_field *const fields = talloc_array(state, glsl_struct_field, 298521b0dbd79937e9d6787f045af7d60d4b6c649ec8Eric Anholt decl_count); 29863455ce614424a5a23a23037e23d0454e476bceeaIan Romanick 29873455ce614424a5a23a23037e23d0454e476bceeaIan Romanick unsigned i = 0; 29882b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick foreach_list_typed (ast_declarator_list, decl_list, link, 29892b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick &this->declarations) { 29903455ce614424a5a23a23037e23d0454e476bceeaIan Romanick const char *type_name; 29913455ce614424a5a23a23037e23d0454e476bceeaIan Romanick 29923455ce614424a5a23a23037e23d0454e476bceeaIan Romanick decl_list->type->specifier->hir(instructions, state); 29933455ce614424a5a23a23037e23d0454e476bceeaIan Romanick 2994c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke /* Section 10.9 of the GLSL ES 1.00 specification states that 2995c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke * embedded structure definitions have been removed from the language. 2996c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke */ 2997c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke if (state->es_shader && decl_list->type->specifier->structure != NULL) { 2998c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke YYLTYPE loc = this->get_location(); 2999c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke _mesa_glsl_error(&loc, state, "Embedded structure definitions are " 3000c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke "not allowed in GLSL ES 1.00."); 3001c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke } 3002c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke 30033455ce614424a5a23a23037e23d0454e476bceeaIan Romanick const glsl_type *decl_type = 30043455ce614424a5a23a23037e23d0454e476bceeaIan Romanick decl_list->type->specifier->glsl_type(& type_name, state); 30053455ce614424a5a23a23037e23d0454e476bceeaIan Romanick 30062b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick foreach_list_typed (ast_declaration, decl, link, 30072b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick &decl_list->declarations) { 3008d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke const struct glsl_type *field_type = decl_type; 3009d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke if (decl->is_array) { 3010d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke YYLTYPE loc = decl->get_location(); 3011d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke field_type = process_array_type(&loc, decl_type, decl->array_size, 3012d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke state); 3013d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke } 301473986a7a262807ab2cfd6d46ae17cfc7a30cdfecIan Romanick fields[i].type = (field_type != NULL) 301573986a7a262807ab2cfd6d46ae17cfc7a30cdfecIan Romanick ? field_type : glsl_type::error_type; 30163455ce614424a5a23a23037e23d0454e476bceeaIan Romanick fields[i].name = decl->identifier; 30173455ce614424a5a23a23037e23d0454e476bceeaIan Romanick i++; 30183455ce614424a5a23a23037e23d0454e476bceeaIan Romanick } 30193455ce614424a5a23a23037e23d0454e476bceeaIan Romanick } 30203455ce614424a5a23a23037e23d0454e476bceeaIan Romanick 30213455ce614424a5a23a23037e23d0454e476bceeaIan Romanick assert(i == decl_count); 30223455ce614424a5a23a23037e23d0454e476bceeaIan Romanick 302349e3577b91f44013746f7a3db411e7041b7d899eIan Romanick const glsl_type *t = 3024ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke glsl_type::get_record_instance(fields, decl_count, this->name); 30251d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick 3026ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick YYLTYPE loc = this->get_location(); 3027a789ca649cb143c0c5bf3209ff1bde398fbd777eIan Romanick if (!state->symbols->add_type(name, t)) { 3028ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name); 3029ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick } else { 3030a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick 3031a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick const glsl_type **s = (const glsl_type **) 3032a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick realloc(state->user_structures, 3033a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick sizeof(state->user_structures[0]) * 3034a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick (state->num_user_structures + 1)); 3035a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick if (s != NULL) { 3036a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick s[state->num_user_structures] = t; 3037a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick state->user_structures = s; 3038a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick state->num_user_structures++; 3039a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick } 3040ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick } 30413455ce614424a5a23a23037e23d0454e476bceeaIan Romanick 30423455ce614424a5a23a23037e23d0454e476bceeaIan Romanick /* Structure type definitions do not have r-values. 30433455ce614424a5a23a23037e23d0454e476bceeaIan Romanick */ 30443455ce614424a5a23a23037e23d0454e476bceeaIan Romanick return NULL; 30453455ce614424a5a23a23037e23d0454e476bceeaIan Romanick} 3046