ast_to_hir.cpp revision b3c093c79c2ec49c36af37aa290d5ae452149f6e
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" 57140632190cf41e6a035ca199b181091d4ed46986Eric Anholt#include "program/hash_table.h" 58a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick#include "ir.h" 59a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 604b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholtstatic void 614b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholtdetect_conflicting_assignments(struct _mesa_glsl_parse_state *state, 624b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt exec_list *instructions); 634b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt 64d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanickvoid 65d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick_mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state) 66d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick{ 67adfb0cd7401251bef0c854ac945fce78f0ed11dbIan Romanick _mesa_glsl_initialize_variables(instructions, state); 68adfb0cd7401251bef0c854ac945fce78f0ed11dbIan Romanick 69814c89abdbcd5b841b98746af921796df0362238Kenneth Graunke state->symbols->language_version = state->language_version; 70814c89abdbcd5b841b98746af921796df0362238Kenneth Graunke 7141ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick state->current_function = NULL; 7241ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick 730d81b0e18494a80c4326fbc98837842959675869Paul Berry state->toplevel_ir = instructions; 740d81b0e18494a80c4326fbc98837842959675869Paul Berry 75a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke /* Section 4.2 of the GLSL 1.20 specification states: 76a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke * "The built-in functions are scoped in a scope outside the global scope 77a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke * users declare global variables in. That is, a shader's global scope, 78a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke * available for user-defined functions and global variables, is nested 79a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke * inside the scope containing the built-in functions." 80a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke * 81a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke * Since built-in functions like ftransform() access built-in variables, 82a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke * it follows that those must be in the outer scope as well. 83a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke * 84a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke * We push scope here to create this nesting effect...but don't pop. 85a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke * This way, a shader's globals are still in the symbol table for use 86a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke * by the linker. 87a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke */ 88a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke state->symbols->push_scope(); 89a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke 902b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick foreach_list_typed (ast_node, ast, link, & state->translation_unit) 91304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick ast->hir(instructions, state); 9202c5ae1b3fef75d5c0a715313a69e6b95ebd5b95Ian Romanick 9302c5ae1b3fef75d5c0a715313a69e6b95ebd5b95Ian Romanick detect_recursion_unlinked(state, instructions); 944b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt detect_conflicting_assignments(state, instructions); 950d81b0e18494a80c4326fbc98837842959675869Paul Berry 960d81b0e18494a80c4326fbc98837842959675869Paul Berry state->toplevel_ir = NULL; 97d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick} 98d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick 99d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick 1000104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick/** 1010104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * If a conversion is available, convert one operand to a different type 1020104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * 1030104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * The \c from \c ir_rvalue is converted "in place". 1040104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * 1050104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param to Type that the operand it to be converted to 1060104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param from Operand that is being converted 1070104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param state GLSL compiler state 1080104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * 1090104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \return 1100104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * If a conversion is possible (or unnecessary), \c true is returned. 1110104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * Otherwise \c false is returned. 1120104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick */ 113f32d3df8ab2b7c6c746f46870edc4b284cea50caKenneth Graunkebool 114bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickapply_implicit_conversion(const glsl_type *to, ir_rvalue * &from, 1150104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick struct _mesa_glsl_parse_state *state) 1160104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick{ 117953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke void *ctx = state; 118bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick if (to->base_type == from->type->base_type) 1190104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick return true; 1200104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick 1210104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick /* This conversion was added in GLSL 1.20. If the compilation mode is 1220104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * GLSL 1.10, the conversion is skipped. 1230104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick */ 1240104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick if (state->language_version < 120) 1250104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick return false; 1260104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick 1270104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec: 1280104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * 1290104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * "There are no implicit array or structure conversions. For 1300104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * example, an array of int cannot be implicitly converted to an 1310104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * array of float. There are no implicit conversions between 1320104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * signed and unsigned integers." 1330104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick */ 1340104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick /* FINISHME: The above comment is partially a lie. There is int/uint 1350104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * FINISHME: conversion for immediate constants. 1360104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick */ 137bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick if (!to->is_float() || !from->type->is_numeric()) 1380104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick return false; 1390104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick 140506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke /* Convert to a floating point type with the same number of components 141506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke * as the original type - i.e. int to float, not int to vec4. 142506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke */ 143506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke to = glsl_type::get_instance(GLSL_TYPE_FLOAT, from->type->vector_elements, 144506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke from->type->matrix_columns); 145506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke 146bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick switch (from->type->base_type) { 1470104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick case GLSL_TYPE_INT: 1481660a2954797e056caba319c5d6c70b0d4be22feCarl Worth from = new(ctx) ir_expression(ir_unop_i2f, to, from, NULL); 1490104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick break; 1500104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick case GLSL_TYPE_UINT: 1511660a2954797e056caba319c5d6c70b0d4be22feCarl Worth from = new(ctx) ir_expression(ir_unop_u2f, to, from, NULL); 1520104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick break; 1530104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick case GLSL_TYPE_BOOL: 1541660a2954797e056caba319c5d6c70b0d4be22feCarl Worth from = new(ctx) ir_expression(ir_unop_b2f, to, from, NULL); 155dc58b3f8ccd817fdee390a3df5b8e0fb29d5397cEric Anholt break; 1560104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick default: 1570104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick assert(0); 1580104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick } 1590104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick 1600104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick return true; 1610104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick} 1620104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick 1630104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick 164a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type * 165bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickarithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b, 166a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick bool multiply, 167a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt struct _mesa_glsl_parse_state *state, YYLTYPE *loc) 168a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{ 169336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt const glsl_type *type_a = value_a->type; 170336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt const glsl_type *type_b = value_b->type; 1710104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick 172a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* From GLSL 1.50 spec, page 56: 173a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * 174a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * "The arithmetic binary operators add (+), subtract (-), 175a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * multiply (*), and divide (/) operate on integer and 176a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * floating-point scalars, vectors, and matrices." 177a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 17860b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick if (!type_a->is_numeric() || !type_b->is_numeric()) { 179a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt _mesa_glsl_error(loc, state, 180a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt "Operands to arithmetic operators must be numeric"); 1810471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick return glsl_type::error_type; 182a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 183a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 184a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 185a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* "If one operand is floating-point based and the other is 186a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * not, then the conversions from Section 4.1.10 "Implicit 187a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * Conversions" are applied to the non-floating-point-based operand." 188a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 1890104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick if (!apply_implicit_conversion(type_a, value_b, state) 1900104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick && !apply_implicit_conversion(type_b, value_a, state)) { 191a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt _mesa_glsl_error(loc, state, 192a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt "Could not implicitly convert operands to " 193a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt "arithmetic operator"); 1940104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick return glsl_type::error_type; 195a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 196336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt type_a = value_a->type; 197336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt type_b = value_b->type; 198336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt 199a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* "If the operands are integer types, they must both be signed or 200a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * both be unsigned." 201a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * 202a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * From this rule and the preceeding conversion it can be inferred that 203a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT. 20460b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick * The is_numeric check above already filtered out the case where either 20560b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick * type is not one of these, so now the base types need only be tested for 20660b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick * equality. 207a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 208a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick if (type_a->base_type != type_b->base_type) { 209a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt _mesa_glsl_error(loc, state, 210a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt "base type mismatch for arithmetic operator"); 2110471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick return glsl_type::error_type; 212a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 213a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 214a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* "All arithmetic binary operators result in the same fundamental type 215a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * (signed integer, unsigned integer, or floating-point) as the 216a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * operands they operate on, after operand type conversion. After 217a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * conversion, the following cases are valid 218a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * 219a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * * The two operands are scalars. In this case the operation is 220a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * applied, resulting in a scalar." 221a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 222cb36f8aaeeb09660843316270a781948f773d90bIan Romanick if (type_a->is_scalar() && type_b->is_scalar()) 223a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick return type_a; 224a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 225a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* "* One operand is a scalar, and the other is a vector or matrix. 226a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * In this case, the scalar operation is applied independently to each 227a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * component of the vector or matrix, resulting in the same size 228a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * vector or matrix." 229a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 230cb36f8aaeeb09660843316270a781948f773d90bIan Romanick if (type_a->is_scalar()) { 231cb36f8aaeeb09660843316270a781948f773d90bIan Romanick if (!type_b->is_scalar()) 232a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick return type_b; 233cb36f8aaeeb09660843316270a781948f773d90bIan Romanick } else if (type_b->is_scalar()) { 234a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick return type_a; 235a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 236a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 237a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* All of the combinations of <scalar, scalar>, <vector, scalar>, 238a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been 239a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * handled. 240a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 24160b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick assert(!type_a->is_scalar()); 24260b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick assert(!type_b->is_scalar()); 243a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 244a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* "* The two operands are vectors of the same size. In this case, the 245a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * operation is done component-wise resulting in the same size 246a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * vector." 247a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 248a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick if (type_a->is_vector() && type_b->is_vector()) { 249a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt if (type_a == type_b) { 250a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt return type_a; 251a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt } else { 252a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt _mesa_glsl_error(loc, state, 253a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt "vector size mismatch for arithmetic operator"); 254a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt return glsl_type::error_type; 255a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt } 256a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 257a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 258a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* All of the combinations of <scalar, scalar>, <vector, scalar>, 259a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and 260a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * <vector, vector> have been handled. At least one of the operands must 261a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * be matrix. Further, since there are no integer matrix types, the base 262a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * type of both operands must be float. 263a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 26460b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick assert(type_a->is_matrix() || type_b->is_matrix()); 265a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick assert(type_a->base_type == GLSL_TYPE_FLOAT); 266a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick assert(type_b->base_type == GLSL_TYPE_FLOAT); 267a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 268a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* "* The operator is add (+), subtract (-), or divide (/), and the 269a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * operands are matrices with the same number of rows and the same 270a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * number of columns. In this case, the operation is done component- 271a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * wise resulting in the same size matrix." 272a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * * The operator is multiply (*), where both operands are matrices or 273a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * one operand is a vector and the other a matrix. A right vector 274a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * operand is treated as a column vector and a left vector operand as a 275a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * row vector. In all these cases, it is required that the number of 276a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * columns of the left operand is equal to the number of rows of the 277a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * right operand. Then, the multiply (*) operation does a linear 278a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * algebraic multiply, yielding an object that has the same number of 279a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * rows as the left operand and the same number of columns as the right 280a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * operand. Section 5.10 "Vector and Matrix Operations" explains in 281a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * more detail how vectors and matrices are operated on." 282a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 283a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick if (! multiply) { 284a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt if (type_a == type_b) 285a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt return type_a; 286a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } else { 287fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick if (type_a->is_matrix() && type_b->is_matrix()) { 288c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick /* Matrix multiply. The columns of A must match the rows of B. Given 289c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick * the other previously tested constraints, this means the vector type 290c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick * of a row from A must be the same as the vector type of a column from 291c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick * B. 292c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick */ 293c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick if (type_a->row_type() == type_b->column_type()) { 294c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick /* The resulting matrix has the number of columns of matrix B and 295c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick * the number of rows of matrix A. We get the row count of A by 296c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick * looking at the size of a vector that makes up a column. The 297c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick * transpose (size of a row) is done for B. 298c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick */ 299a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt const glsl_type *const type = 300c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick glsl_type::get_instance(type_a->base_type, 301c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick type_a->column_type()->vector_elements, 302c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick type_b->row_type()->vector_elements); 303a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt assert(type != glsl_type::error_type); 304a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt 305a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt return type; 306a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 307fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick } else if (type_a->is_matrix()) { 308a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* A is a matrix and B is a column vector. Columns of A must match 309c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick * rows of B. Given the other previously tested constraints, this 310c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick * means the vector type of a row from A must be the same as the 311c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick * vector the type of B. 312a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 31347c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth if (type_a->row_type() == type_b) { 31447c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth /* The resulting vector has a number of elements equal to 31547c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth * the number of rows of matrix A. */ 31647c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth const glsl_type *const type = 31747c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth glsl_type::get_instance(type_a->base_type, 31847c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth type_a->column_type()->vector_elements, 31947c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth 1); 32047c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth assert(type != glsl_type::error_type); 32147c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth 32247c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth return type; 32347c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth } 324a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } else { 325fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick assert(type_b->is_matrix()); 326a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 327c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick /* A is a row vector and B is a matrix. Columns of A must match rows 328c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick * of B. Given the other previously tested constraints, this means 329c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick * the type of A must be the same as the vector type of a column from 330c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick * B. 331a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 33247c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth if (type_a == type_b->column_type()) { 33347c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth /* The resulting vector has a number of elements equal to 33447c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth * the number of columns of matrix B. */ 33547c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth const glsl_type *const type = 33647c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth glsl_type::get_instance(type_a->base_type, 33747c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth type_b->row_type()->vector_elements, 33847c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth 1); 33947c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth assert(type != glsl_type::error_type); 34047c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth 34147c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth return type; 34247c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth } 343a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 344a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt 345a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication"); 346a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt return glsl_type::error_type; 347a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 348a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 349a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 350a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* "All other cases are illegal." 351a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 352a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt _mesa_glsl_error(loc, state, "type mismatch"); 3530471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick return glsl_type::error_type; 354a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick} 355a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 356a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 357a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type * 35865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholtunary_arithmetic_result_type(const struct glsl_type *type, 35965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt struct _mesa_glsl_parse_state *state, YYLTYPE *loc) 360a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{ 361a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* From GLSL 1.50 spec, page 57: 362a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * 363a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * "The arithmetic unary operators negate (-), post- and pre-increment 364a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * and decrement (-- and ++) operate on integer or floating-point 365a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * values (including vectors and matrices). All unary operators work 366a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * component-wise on their operands. These result with the same type 367a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * they operated on." 368a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 36965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt if (!type->is_numeric()) { 37065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt _mesa_glsl_error(loc, state, 37165e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt "Operands to arithmetic operators must be numeric"); 3720471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick return glsl_type::error_type; 37365e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt } 374a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 375a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick return type; 376a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick} 377a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 378cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace/** 379cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * \brief Return the result type of a bit-logic operation. 380cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * 381cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * If the given types to the bit-logic operator are invalid, return 382cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * glsl_type::error_type. 383cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * 384cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * \param type_a Type of LHS of bit-logic op 385cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * \param type_b Type of RHS of bit-logic op 386cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace */ 387cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versacestatic const struct glsl_type * 388cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versacebit_logic_result_type(const struct glsl_type *type_a, 389cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace const struct glsl_type *type_b, 390cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace ast_operators op, 391cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace struct _mesa_glsl_parse_state *state, YYLTYPE *loc) 392cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace{ 393cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace if (state->language_version < 130) { 394cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace _mesa_glsl_error(loc, state, "bit operations require GLSL 1.30"); 395cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace return glsl_type::error_type; 396cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace } 397cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace 398cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace /* From page 50 (page 56 of PDF) of GLSL 1.30 spec: 399cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * 400cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * "The bitwise operators and (&), exclusive-or (^), and inclusive-or 401cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * (|). The operands must be of type signed or unsigned integers or 402cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * integer vectors." 403cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace */ 404cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace if (!type_a->is_integer()) { 405cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace _mesa_glsl_error(loc, state, "LHS of `%s' must be an integer", 406cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace ast_expression::operator_string(op)); 407cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace return glsl_type::error_type; 408cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace } 409cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace if (!type_b->is_integer()) { 410cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace _mesa_glsl_error(loc, state, "RHS of `%s' must be an integer", 411cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace ast_expression::operator_string(op)); 412cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace return glsl_type::error_type; 413cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace } 414cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace 415cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace /* "The fundamental types of the operands (signed or unsigned) must 416cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * match," 417cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace */ 418cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace if (type_a->base_type != type_b->base_type) { 419cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace _mesa_glsl_error(loc, state, "operands of `%s' must have the same " 420cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace "base type", ast_expression::operator_string(op)); 421cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace return glsl_type::error_type; 422cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace } 423cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace 424cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace /* "The operands cannot be vectors of differing size." */ 425cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace if (type_a->is_vector() && 426cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace type_b->is_vector() && 427cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace type_a->vector_elements != type_b->vector_elements) { 428cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace _mesa_glsl_error(loc, state, "operands of `%s' cannot be vectors of " 429cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace "different sizes", ast_expression::operator_string(op)); 430cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace return glsl_type::error_type; 431cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace } 432cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace 433cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace /* "If one operand is a scalar and the other a vector, the scalar is 434cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * applied component-wise to the vector, resulting in the same type as 435cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * the vector. The fundamental types of the operands [...] will be the 436cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * resulting fundamental type." 437cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace */ 438cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace if (type_a->is_scalar()) 439cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace return type_b; 440cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace else 441cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace return type_a; 442cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace} 443a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 444a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type * 445a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickmodulus_result_type(const struct glsl_type *type_a, 44665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt const struct glsl_type *type_b, 44765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt struct _mesa_glsl_parse_state *state, YYLTYPE *loc) 448a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{ 44982f994f3860ca05ff5550f32844b0f523d40b9efChad Versace if (state->language_version < 130) { 45082f994f3860ca05ff5550f32844b0f523d40b9efChad Versace _mesa_glsl_error(loc, state, 45182f994f3860ca05ff5550f32844b0f523d40b9efChad Versace "operator '%%' is reserved in %s", 45282f994f3860ca05ff5550f32844b0f523d40b9efChad Versace state->version_string); 45382f994f3860ca05ff5550f32844b0f523d40b9efChad Versace return glsl_type::error_type; 45482f994f3860ca05ff5550f32844b0f523d40b9efChad Versace } 45582f994f3860ca05ff5550f32844b0f523d40b9efChad Versace 456a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* From GLSL 1.50 spec, page 56: 457a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * "The operator modulus (%) operates on signed or unsigned integers or 458a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * integer vectors. The operand types must both be signed or both be 459a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * unsigned." 460a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 4618eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke if (!type_a->is_integer()) { 4628eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke _mesa_glsl_error(loc, state, "LHS of operator %% must be an integer."); 4638eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke return glsl_type::error_type; 4648eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke } 4658eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke if (!type_b->is_integer()) { 4668eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke _mesa_glsl_error(loc, state, "RHS of operator %% must be an integer."); 4678eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke return glsl_type::error_type; 4688eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke } 4698eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke if (type_a->base_type != type_b->base_type) { 4708eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke _mesa_glsl_error(loc, state, 4718eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke "operands of %% must have the same base type"); 4720471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick return glsl_type::error_type; 473a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 474a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 475a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* "The operands cannot be vectors of differing size. If one operand is 476a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * a scalar and the other vector, then the scalar is applied component- 477a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * wise to the vector, resulting in the same type as the vector. If both 478a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * are vectors of the same size, the result is computed component-wise." 479a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 480a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick if (type_a->is_vector()) { 481a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick if (!type_b->is_vector() 482a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick || (type_a->vector_elements == type_b->vector_elements)) 483a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick return type_a; 484a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } else 485a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick return type_b; 486a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 487a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* "The operator modulus (%) is not defined for any other data types 488a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * (non-integer types)." 489a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 49065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt _mesa_glsl_error(loc, state, "type mismatch"); 4910471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick return glsl_type::error_type; 492a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick} 493a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 494a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 495a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type * 496bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickrelational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b, 49765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt struct _mesa_glsl_parse_state *state, YYLTYPE *loc) 498a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{ 499336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt const glsl_type *type_a = value_a->type; 500336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt const glsl_type *type_b = value_b->type; 5010150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick 502a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* From GLSL 1.50 spec, page 56: 503a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * "The relational operators greater than (>), less than (<), greater 504a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * than or equal (>=), and less than or equal (<=) operate only on 505a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * scalar integer and scalar floating-point expressions." 506a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 507a6d653dcbbee3158f9ea7b284bdeb1a8432f0fcbIan Romanick if (!type_a->is_numeric() 508a6d653dcbbee3158f9ea7b284bdeb1a8432f0fcbIan Romanick || !type_b->is_numeric() 509cb36f8aaeeb09660843316270a781948f773d90bIan Romanick || !type_a->is_scalar() 51065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt || !type_b->is_scalar()) { 51165e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt _mesa_glsl_error(loc, state, 51265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt "Operands to relational operators must be scalar and " 51365e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt "numeric"); 5140471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick return glsl_type::error_type; 51565e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt } 516a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 517a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* "Either the operands' types must match, or the conversions from 518a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * Section 4.1.10 "Implicit Conversions" will be applied to the integer 519a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * operand, after which the types must match." 520a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 5210150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick if (!apply_implicit_conversion(type_a, value_b, state) 5220150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick && !apply_implicit_conversion(type_b, value_a, state)) { 52365e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt _mesa_glsl_error(loc, state, 52465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt "Could not implicitly convert operands to " 52565e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt "relational operator"); 5260150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick return glsl_type::error_type; 527a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 528336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt type_a = value_a->type; 529336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt type_b = value_b->type; 530a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 53165e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt if (type_a->base_type != type_b->base_type) { 53265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt _mesa_glsl_error(loc, state, "base type mismatch"); 5330471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick return glsl_type::error_type; 53465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt } 535a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 536a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* "The result is scalar Boolean." 537a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 5380471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick return glsl_type::bool_type; 539a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick} 540a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 541c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace/** 542c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * \brief Return the result type of a bit-shift operation. 543c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * 544c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * If the given types to the bit-shift operator are invalid, return 545c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * glsl_type::error_type. 546c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * 547c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * \param type_a Type of LHS of bit-shift op 548c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * \param type_b Type of RHS of bit-shift op 549c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace */ 550c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versacestatic const struct glsl_type * 551c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versaceshift_result_type(const struct glsl_type *type_a, 552c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace const struct glsl_type *type_b, 553c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace ast_operators op, 554c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace struct _mesa_glsl_parse_state *state, YYLTYPE *loc) 555c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace{ 556c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace if (state->language_version < 130) { 557c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace _mesa_glsl_error(loc, state, "bit operations require GLSL 1.30"); 558c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace return glsl_type::error_type; 559c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace } 560c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace 561c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec: 562c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * 563c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * "The shift operators (<<) and (>>). For both operators, the operands 564c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * must be signed or unsigned integers or integer vectors. One operand 565c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * can be signed while the other is unsigned." 566c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace */ 567c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace if (!type_a->is_integer()) { 568c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace _mesa_glsl_error(loc, state, "LHS of operator %s must be an integer or " 569c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace "integer vector", ast_expression::operator_string(op)); 570c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace return glsl_type::error_type; 571c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace 572c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace } 573c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace if (!type_b->is_integer()) { 574c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace _mesa_glsl_error(loc, state, "RHS of operator %s must be an integer or " 575c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace "integer vector", ast_expression::operator_string(op)); 576c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace return glsl_type::error_type; 577c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace } 578c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace 579c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace /* "If the first operand is a scalar, the second operand has to be 580c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * a scalar as well." 581c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace */ 582c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace if (type_a->is_scalar() && !type_b->is_scalar()) { 583c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace _mesa_glsl_error(loc, state, "If the first operand of %s is scalar, the " 584c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace "second must be scalar as well", 585c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace ast_expression::operator_string(op)); 586c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace return glsl_type::error_type; 587c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace } 588c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace 589c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace /* If both operands are vectors, check that they have same number of 590c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * elements. 591c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace */ 592c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace if (type_a->is_vector() && 593c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace type_b->is_vector() && 594c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace type_a->vector_elements != type_b->vector_elements) { 595c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace _mesa_glsl_error(loc, state, "Vector operands to operator %s must " 596c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace "have same number of elements", 597c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace ast_expression::operator_string(op)); 598c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace return glsl_type::error_type; 599c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace } 600c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace 601c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace /* "In all cases, the resulting type will be the same type as the left 602c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * operand." 603c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace */ 604c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace return type_a; 605c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace} 606a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 6070bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick/** 6080bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Validates that a value can be assigned to a location with a specified type 6090bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * 6100bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Validates that \c rhs can be assigned to some location. If the types are 6110bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * not an exact match but an automatic conversion is possible, \c rhs will be 6120bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * converted. 6130bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * 6140bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \return 6150bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type. 6160bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Otherwise the actual RHS to be assigned will be returned. This may be 6170bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \c rhs, or it may be \c rhs after some type conversion. 6180bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * 6190bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \note 6200bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * In addition to being used for assignments, this function is used to 6210bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * type-check return values. 6220bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick */ 623fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue * 624336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholtvalidate_assignment(struct _mesa_glsl_parse_state *state, 62585caea29c18fad89050ac366c558afef568dcb3fIan Romanick const glsl_type *lhs_type, ir_rvalue *rhs, 62685caea29c18fad89050ac366c558afef568dcb3fIan Romanick bool is_initializer) 6270bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick{ 6280bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick /* If there is already some error in the RHS, just return it. Anything 6290bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * else will lead to an avalanche of error message back to the user. 6300bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick */ 631ec53010c4d02e11171d3c782a41b70cad76788e8Ian Romanick if (rhs->type->is_error()) 6320bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick return rhs; 6330bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick 6340bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick /* If the types are identical, the assignment can trivially proceed. 6350bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick */ 636ec53010c4d02e11171d3c782a41b70cad76788e8Ian Romanick if (rhs->type == lhs_type) 6370bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick return rhs; 6380bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick 6390157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick /* If the array element types are the same and the size of the LHS is zero, 64085caea29c18fad89050ac366c558afef568dcb3fIan Romanick * the assignment is okay for initializers embedded in variable 64185caea29c18fad89050ac366c558afef568dcb3fIan Romanick * declarations. 6420157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick * 6430157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick * Note: Whole-array assignments are not permitted in GLSL 1.10, but this 6440157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick * is handled by ir_dereference::is_lvalue. 6450157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick */ 64685caea29c18fad89050ac366c558afef568dcb3fIan Romanick if (is_initializer && lhs_type->is_array() && rhs->type->is_array() 6470157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick && (lhs_type->element_type() == rhs->type->element_type()) 6480157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick && (lhs_type->array_size() == 0)) { 6490157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick return rhs; 6500157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick } 6510157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick 652336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt /* Check for implicit conversion in GLSL 1.20 */ 653336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt if (apply_implicit_conversion(lhs_type, rhs, state)) { 654ec53010c4d02e11171d3c782a41b70cad76788e8Ian Romanick if (rhs->type == lhs_type) 655336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt return rhs; 656336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt } 657336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt 6580bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick return NULL; 6590bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick} 6600bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick 661a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholtstatic void 662a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholtmark_whole_array_access(ir_rvalue *access) 663a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholt{ 664a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholt ir_dereference_variable *deref = access->as_dereference_variable(); 665a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholt 666a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholt if (deref && deref->var) { 667a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholt deref->var->max_array_access = deref->type->length - 1; 668a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholt } 669a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholt} 670a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholt 67110a685218610e737e23d2d8a243ed6ff6613becdEric Anholtir_rvalue * 67210a685218610e737e23d2d8a243ed6ff6613becdEric Anholtdo_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state, 673e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick const char *non_lvalue_description, 67485caea29c18fad89050ac366c558afef568dcb3fIan Romanick ir_rvalue *lhs, ir_rvalue *rhs, bool is_initializer, 67510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt YYLTYPE lhs_loc) 67610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt{ 677953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke void *ctx = state; 67810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt bool error_emitted = (lhs->type->is_error() || rhs->type->is_error()); 67910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt 680f2475ca424f7e001be50f64dafa5700f6603d684Eric Anholt ir_variable *lhs_var = lhs->variable_referenced(); 681f2475ca424f7e001be50f64dafa5700f6603d684Eric Anholt if (lhs_var) 682f2475ca424f7e001be50f64dafa5700f6603d684Eric Anholt lhs_var->assigned = true; 683f2475ca424f7e001be50f64dafa5700f6603d684Eric Anholt 68410a685218610e737e23d2d8a243ed6ff6613becdEric Anholt if (!error_emitted) { 685e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick if (non_lvalue_description != NULL) { 686e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick _mesa_glsl_error(&lhs_loc, state, 687e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick "assignment to %s", 688e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick non_lvalue_description); 689e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick error_emitted = true; 690e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick } else if (lhs->variable_referenced() != NULL 691e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick && lhs->variable_referenced()->read_only) { 692b66be7518ad57368b31b5d70a2bb4c0fe66aa988Chad Versace _mesa_glsl_error(&lhs_loc, state, 693b66be7518ad57368b31b5d70a2bb4c0fe66aa988Chad Versace "assignment to read-only variable '%s'", 694b66be7518ad57368b31b5d70a2bb4c0fe66aa988Chad Versace lhs->variable_referenced()->name); 695b66be7518ad57368b31b5d70a2bb4c0fe66aa988Chad Versace error_emitted = true; 696b66be7518ad57368b31b5d70a2bb4c0fe66aa988Chad Versace 697525cec98a5c65c27c62fed0cff706bca50bf8c6eEric Anholt } else if (state->language_version <= 110 && lhs->type->is_array()) { 698525cec98a5c65c27c62fed0cff706bca50bf8c6eEric Anholt /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec: 699525cec98a5c65c27c62fed0cff706bca50bf8c6eEric Anholt * 700525cec98a5c65c27c62fed0cff706bca50bf8c6eEric Anholt * "Other binary or unary expressions, non-dereferenced 701525cec98a5c65c27c62fed0cff706bca50bf8c6eEric Anholt * arrays, function names, swizzles with repeated fields, 702525cec98a5c65c27c62fed0cff706bca50bf8c6eEric Anholt * and constants cannot be l-values." 703525cec98a5c65c27c62fed0cff706bca50bf8c6eEric Anholt */ 704525cec98a5c65c27c62fed0cff706bca50bf8c6eEric Anholt _mesa_glsl_error(&lhs_loc, state, "whole array assignment is not " 705525cec98a5c65c27c62fed0cff706bca50bf8c6eEric Anholt "allowed in GLSL 1.10 or GLSL ES 1.00."); 706525cec98a5c65c27c62fed0cff706bca50bf8c6eEric Anholt error_emitted = true; 707b66be7518ad57368b31b5d70a2bb4c0fe66aa988Chad Versace } else if (!lhs->is_lvalue()) { 70810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment"); 70910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt error_emitted = true; 71010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt } 71110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt } 71210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt 71385caea29c18fad89050ac366c558afef568dcb3fIan Romanick ir_rvalue *new_rhs = 71485caea29c18fad89050ac366c558afef568dcb3fIan Romanick validate_assignment(state, lhs->type, rhs, is_initializer); 71510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt if (new_rhs == NULL) { 71610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt _mesa_glsl_error(& lhs_loc, state, "type mismatch"); 71710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt } else { 71810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt rhs = new_rhs; 7190157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick 7200157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick /* If the LHS array was not declared with a size, it takes it size from 7210157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick * the RHS. If the LHS is an l-value and a whole array, it must be a 7220157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick * dereference of a variable. Any other case would require that the LHS 7230157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick * is either not an l-value or not a whole array. 7240157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick */ 7250157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick if (lhs->type->array_size() == 0) { 7260157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick ir_dereference *const d = lhs->as_dereference(); 7270157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick 7280157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick assert(d != NULL); 7290157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick 73036ea28646c666ac2af9b43c47e65f9f53ffcc390Ian Romanick ir_variable *const var = d->variable_referenced(); 7310157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick 7320157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick assert(var != NULL); 7330157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick 73463f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick if (var->max_array_access >= unsigned(rhs->type->array_size())) { 73563f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick /* FINISHME: This should actually log the location of the RHS. */ 73663f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to " 73763f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick "previous access", 73863f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick var->max_array_access); 73963f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick } 74063f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick 741f38d15b80d4e4c8ecb7a76087cdc49835f0aa271Ian Romanick var->type = glsl_type::get_array_instance(lhs->type->element_type(), 7420157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick rhs->type->array_size()); 7439703ed05e684f4269cd8af27c94e9b6bf8781d85Eric Anholt d->type = var->type; 7440157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick } 745407a1001aefcb15e8d066031417d91ea22f1daf1Eric Anholt mark_whole_array_access(rhs); 746a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholt mark_whole_array_access(lhs); 74710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt } 74810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt 7492731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt /* Most callers of do_assignment (assign, add_assign, pre_inc/dec, 7502731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt * but not post_inc) need the converted assigned value as an rvalue 7512731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt * to handle things like: 7522731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt * 7532731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt * i = j += 1; 7542731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt * 7552731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt * So we always just store the computed value being assigned to a 7562731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt * temporary and return a deref of that temporary. If the rvalue 7572731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt * ends up not being used, the temp will get copy-propagated out. 7582731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt */ 7597e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp", 7607e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick ir_var_temporary); 761e33c10328caec29616a5433b1d1df9088f3a84dfEric Anholt ir_dereference_variable *deref_var = new(ctx) ir_dereference_variable(var); 762ae805922b7e3cdaf3aee26c3b799fe3608669bbaEric Anholt instructions->push_tail(var); 763aa5ec137757323b424d0f2638c50c93b9b06ffffEric Anholt instructions->push_tail(new(ctx) ir_assignment(deref_var, rhs)); 764e33c10328caec29616a5433b1d1df9088f3a84dfEric Anholt deref_var = new(ctx) ir_dereference_variable(var); 7652731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt 7668e9ce2eb56a087c2544112700ae1abe3f96648ddIan Romanick if (!error_emitted) 767aa5ec137757323b424d0f2638c50c93b9b06ffffEric Anholt instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var)); 76810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt 7691660a2954797e056caba319c5d6c70b0d4be22feCarl Worth return new(ctx) ir_dereference_variable(var); 77010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt} 7710bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick 772de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholtstatic ir_rvalue * 773959a9ecdd8fbc3375e4149f2b44d253622ff12eeEric Anholtget_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue) 774de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt{ 775d3073f58c17d8675a2ecdd5dfa83e5520c78e1a8Kenneth Graunke void *ctx = ralloc_parent(lvalue); 776de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt ir_variable *var; 777de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt 7787e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp", 7797e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick ir_var_temporary); 78043b5b03d67ce890e867c81d4a5cfc4871d711d43Eric Anholt instructions->push_tail(var); 781de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt var->mode = ir_var_auto; 782de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt 7831660a2954797e056caba319c5d6c70b0d4be22feCarl Worth instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var), 784aa5ec137757323b424d0f2638c50c93b9b06ffffEric Anholt lvalue)); 785de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt 7861660a2954797e056caba319c5d6c70b0d4be22feCarl Worth return new(ctx) ir_dereference_variable(var); 787de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt} 788de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt 789de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt 790fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue * 7910044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_node::hir(exec_list *instructions, 79218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick struct _mesa_glsl_parse_state *state) 79318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick{ 79418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick (void) instructions; 79518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick (void) state; 79618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick 79718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick return NULL; 79818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick} 79918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick 800ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholtstatic ir_rvalue * 801ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholtdo_comparison(void *mem_ctx, int operation, ir_rvalue *op0, ir_rvalue *op1) 802ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt{ 803ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt int join_op; 8046d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick ir_rvalue *cmp = NULL; 805ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt 806ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt if (operation == ir_binop_all_equal) 807ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt join_op = ir_binop_logic_and; 808ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt else 809ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt join_op = ir_binop_logic_or; 810ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt 811ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt switch (op0->type->base_type) { 812ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt case GLSL_TYPE_FLOAT: 813ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt case GLSL_TYPE_UINT: 814ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt case GLSL_TYPE_INT: 815ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt case GLSL_TYPE_BOOL: 816ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt return new(mem_ctx) ir_expression(operation, op0, op1); 817ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt 818ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt case GLSL_TYPE_ARRAY: { 819ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt for (unsigned int i = 0; i < op0->type->length; i++) { 820ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt ir_rvalue *e0, *e1, *result; 821ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt 822ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt e0 = new(mem_ctx) ir_dereference_array(op0->clone(mem_ctx, NULL), 823ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt new(mem_ctx) ir_constant(i)); 824ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt e1 = new(mem_ctx) ir_dereference_array(op1->clone(mem_ctx, NULL), 825ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt new(mem_ctx) ir_constant(i)); 826ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt result = do_comparison(mem_ctx, operation, e0, e1); 827ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt 8286d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick if (cmp) { 8296d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick cmp = new(mem_ctx) ir_expression(join_op, cmp, result); 830ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt } else { 8316d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick cmp = result; 832ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt } 833ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt } 834b4f585665c31b1f80d909e38b3b2a9fab0c03076Eric Anholt 835b4f585665c31b1f80d909e38b3b2a9fab0c03076Eric Anholt mark_whole_array_access(op0); 836b4f585665c31b1f80d909e38b3b2a9fab0c03076Eric Anholt mark_whole_array_access(op1); 8376d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick break; 838ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt } 839ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt 840ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt case GLSL_TYPE_STRUCT: { 841ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt for (unsigned int i = 0; i < op0->type->length; i++) { 842ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt ir_rvalue *e0, *e1, *result; 843ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt const char *field_name = op0->type->fields.structure[i].name; 844ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt 845ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt e0 = new(mem_ctx) ir_dereference_record(op0->clone(mem_ctx, NULL), 846ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt field_name); 847ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt e1 = new(mem_ctx) ir_dereference_record(op1->clone(mem_ctx, NULL), 848ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt field_name); 849ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt result = do_comparison(mem_ctx, operation, e0, e1); 850ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt 8516d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick if (cmp) { 8526d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick cmp = new(mem_ctx) ir_expression(join_op, cmp, result); 853ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt } else { 8546d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick cmp = result; 855ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt } 856ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt } 8576d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick break; 858ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt } 859ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt 860ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt case GLSL_TYPE_ERROR: 861ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt case GLSL_TYPE_VOID: 862ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt case GLSL_TYPE_SAMPLER: 863ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt /* I assume a comparison of a struct containing a sampler just 864ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt * ignores the sampler present in the type. 865ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt */ 8666d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick break; 8676d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick 8686d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick default: 8696d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick assert(!"Should not get here."); 8706d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick break; 871ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt } 872d56c97413ee65e40e3544b89ffca450df9ba1c06Eric Anholt 8736d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick if (cmp == NULL) 8746d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick cmp = new(mem_ctx) ir_constant(true); 8756d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick 8766d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick return cmp; 877ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt} 87818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick 87901822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt/* For logical operations, we want to ensure that the operands are 88001822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt * scalar booleans. If it isn't, emit an error and return a constant 88101822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt * boolean to avoid triggering cascading error messages. 88201822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt */ 88301822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholtir_rvalue * 88401822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholtget_scalar_boolean_operand(exec_list *instructions, 88501822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt struct _mesa_glsl_parse_state *state, 88601822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt ast_expression *parent_expr, 88701822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt int operand, 88801822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt const char *operand_name, 88901822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt bool *error_emitted) 89001822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt{ 89101822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt ast_expression *expr = parent_expr->subexpressions[operand]; 89201822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt void *ctx = state; 89301822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt ir_rvalue *val = expr->hir(instructions, state); 89401822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt 89501822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt if (val->type->is_boolean() && val->type->is_scalar()) 89601822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt return val; 89701822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt 89801822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt if (!*error_emitted) { 89901822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt YYLTYPE loc = expr->get_location(); 90001822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt _mesa_glsl_error(&loc, state, "%s of `%s' must be scalar boolean", 90101822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt operand_name, 90201822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt parent_expr->operator_string(parent_expr->oper)); 90301822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt *error_emitted = true; 90401822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt } 90501822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt 90601822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt return new(ctx) ir_constant(true); 90701822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt} 90801822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt 90993b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry/** 91093b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry * If name refers to a builtin array whose maximum allowed size is less than 91193b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry * size, report an error and return true. Otherwise return false. 91293b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry */ 91393b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berrystatic bool 91493b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berrycheck_builtin_array_max_size(const char *name, unsigned size, 91593b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry YYLTYPE loc, struct _mesa_glsl_parse_state *state) 91693b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry{ 91793b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry if ((strcmp("gl_TexCoord", name) == 0) 91893b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry && (size > state->Const.MaxTextureCoords)) { 91993b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec: 92093b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry * 92193b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry * "The size [of gl_TexCoord] can be at most 92293b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry * gl_MaxTextureCoords." 92393b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry */ 92493b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry _mesa_glsl_error(&loc, state, "`gl_TexCoord' array size cannot " 92593b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry "be larger than gl_MaxTextureCoords (%u)\n", 92693b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry state->Const.MaxTextureCoords); 92793b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry return true; 92837bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry } else if (strcmp("gl_ClipDistance", name) == 0 92937bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry && size > state->Const.MaxClipPlanes) { 93037bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry /* From section 7.1 (Vertex Shader Special Variables) of the 93137bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry * GLSL 1.30 spec: 93237bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry * 93337bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry * "The gl_ClipDistance array is predeclared as unsized and 93437bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry * must be sized by the shader either redeclaring it with a 93537bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry * size or indexing it only with integral constant 93637bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry * expressions. ... The size can be at most 93737bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry * gl_MaxClipDistances." 93837bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry */ 93937bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry _mesa_glsl_error(&loc, state, "`gl_ClipDistance' array size cannot " 94037bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry "be larger than gl_MaxClipDistances (%u)\n", 94137bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry state->Const.MaxClipPlanes); 94237bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry return true; 94393b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry } 94493b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry return false; 94593b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry} 94693b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry 9479abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry/** 9489abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry * Create the constant 1, of a which is appropriate for incrementing and 9499abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry * decrementing values of the given GLSL type. For example, if type is vec4, 9509abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry * this creates a constant value of 1.0 having type float. 9519abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry * 9529abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry * If the given type is invalid for increment and decrement operators, return 9539abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry * a floating point 1--the error will be detected later. 9549abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry */ 9559abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berrystatic ir_rvalue * 9569abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berryconstant_one_for_inc_dec(void *ctx, const glsl_type *type) 9579abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry{ 9589abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry switch (type->base_type) { 9599abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry case GLSL_TYPE_UINT: 9609abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry return new(ctx) ir_constant((unsigned) 1); 9619abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry case GLSL_TYPE_INT: 9629abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry return new(ctx) ir_constant(1); 9639abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry default: 9649abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry case GLSL_TYPE_FLOAT: 9659abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry return new(ctx) ir_constant(1.0f); 9669abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry } 9679abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry} 9689abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry 969fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue * 9700044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_expression::hir(exec_list *instructions, 97118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick struct _mesa_glsl_parse_state *state) 972a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{ 973953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke void *ctx = state; 974a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick static const int operations[AST_NUM_OPERATORS] = { 975a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick -1, /* ast_assign doesn't convert to ir_expression. */ 976a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick -1, /* ast_plus doesn't convert to ir_expression. */ 977a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_unop_neg, 978a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_add, 979a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_sub, 980a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_mul, 981a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_div, 982a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_mod, 983a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_lshift, 984a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_rshift, 985a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_less, 986a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_greater, 987a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_lequal, 988a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_gequal, 9894dfb89904c0a3d2166e9a3fc0253a254680e91bcLuca Barbieri ir_binop_all_equal, 9904dfb89904c0a3d2166e9a3fc0253a254680e91bcLuca Barbieri ir_binop_any_nequal, 991a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_bit_and, 992a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_bit_xor, 993a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_bit_or, 994a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_unop_bit_not, 995a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_logic_and, 996a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_logic_xor, 997a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_logic_or, 998a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_unop_logic_not, 999a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1000a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* Note: The following block of expression types actually convert 1001a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * to multiple IR instructions. 1002a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 1003a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_mul, /* ast_mul_assign */ 1004a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_div, /* ast_div_assign */ 1005a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_mod, /* ast_mod_assign */ 1006a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_add, /* ast_add_assign */ 1007a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_sub, /* ast_sub_assign */ 1008a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_lshift, /* ast_ls_assign */ 1009a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_rshift, /* ast_rs_assign */ 1010a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_bit_and, /* ast_and_assign */ 1011a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_bit_xor, /* ast_xor_assign */ 1012a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick ir_binop_bit_or, /* ast_or_assign */ 1013a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1014a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick -1, /* ast_conditional doesn't convert to ir_expression. */ 1015de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt ir_binop_add, /* ast_pre_inc. */ 1016de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt ir_binop_sub, /* ast_pre_dec. */ 1017de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt ir_binop_add, /* ast_post_inc. */ 1018de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt ir_binop_sub, /* ast_post_dec. */ 1019a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick -1, /* ast_field_selection doesn't conv to ir_expression. */ 1020a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick -1, /* ast_array_index doesn't convert to ir_expression. */ 1021a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick -1, /* ast_function_call doesn't conv to ir_expression. */ 1022a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick -1, /* ast_identifier doesn't convert to ir_expression. */ 1023a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick -1, /* ast_int_constant doesn't convert to ir_expression. */ 1024a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick -1, /* ast_uint_constant doesn't conv to ir_expression. */ 1025a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick -1, /* ast_float_constant doesn't conv to ir_expression. */ 1026a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick -1, /* ast_bool_constant doesn't conv to ir_expression. */ 1027a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick -1, /* ast_sequence doesn't convert to ir_expression. */ 1028a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick }; 1029fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke ir_rvalue *result = NULL; 10301c325af4d6b907e0a47ab7f868a2a78f054f153fAras Pranckevicius ir_rvalue *op[3]; 103108ba9778dbf3326bdfc8ca8035b51412032748eaKenneth Graunke const struct glsl_type *type; /* a temporary variable for switch cases */ 1032a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick bool error_emitted = false; 1033a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick YYLTYPE loc; 1034a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 103518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick loc = this->get_location(); 1036a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 103718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick switch (this->oper) { 10386652af36fe8994b1621d882fcc230d320908a2a3Ian Romanick case ast_assign: { 103918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick op[0] = this->subexpressions[0]->hir(instructions, state); 104018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick op[1] = this->subexpressions[1]->hir(instructions, state); 1041a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1042e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick result = do_assignment(instructions, state, 1043e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick this->subexpressions[0]->non_lvalue_description, 1044e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick op[0], op[1], false, 104510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt this->subexpressions[0]->get_location()); 104610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt error_emitted = result->type->is_error(); 1047a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 10486652af36fe8994b1621d882fcc230d320908a2a3Ian Romanick } 1049a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1050a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_plus: 105118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick op[0] = this->subexpressions[0]->hir(instructions, state); 1052a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1053c24bcad9f88379ffba9e2f0ff92f22cdf60c2927Carl Worth type = unary_arithmetic_result_type(op[0]->type, state, & loc); 1054c24bcad9f88379ffba9e2f0ff92f22cdf60c2927Carl Worth 1055c24bcad9f88379ffba9e2f0ff92f22cdf60c2927Carl Worth error_emitted = type->is_error(); 1056a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1057a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick result = op[0]; 1058a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1059a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1060a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_neg: 106118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick op[0] = this->subexpressions[0]->hir(instructions, state); 1062a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 106365e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt type = unary_arithmetic_result_type(op[0]->type, state, & loc); 1064a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 106565e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt error_emitted = type->is_error(); 1066a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 10671660a2954797e056caba319c5d6c70b0d4be22feCarl Worth result = new(ctx) ir_expression(operations[this->oper], type, 10681660a2954797e056caba319c5d6c70b0d4be22feCarl Worth op[0], NULL); 1069a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1070a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1071a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_add: 1072a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_sub: 1073a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_mul: 1074a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_div: 107518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick op[0] = this->subexpressions[0]->hir(instructions, state); 107618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick op[1] = this->subexpressions[1]->hir(instructions, state); 1077a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1078bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick type = arithmetic_result_type(op[0], op[1], 107918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick (this->oper == ast_mul), 1080a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt state, & loc); 1081a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt error_emitted = type->is_error(); 1082a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 10831660a2954797e056caba319c5d6c70b0d4be22feCarl Worth result = new(ctx) ir_expression(operations[this->oper], type, 10841660a2954797e056caba319c5d6c70b0d4be22feCarl Worth op[0], op[1]); 1085a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1086a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1087a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_mod: 108818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick op[0] = this->subexpressions[0]->hir(instructions, state); 108918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick op[1] = this->subexpressions[1]->hir(instructions, state); 1090a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 109165e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt type = modulus_result_type(op[0]->type, op[1]->type, state, & loc); 1092a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 109318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick assert(operations[this->oper] == ir_binop_mod); 1094a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 10951660a2954797e056caba319c5d6c70b0d4be22feCarl Worth result = new(ctx) ir_expression(operations[this->oper], type, 10961660a2954797e056caba319c5d6c70b0d4be22feCarl Worth op[0], op[1]); 109765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt error_emitted = type->is_error(); 1098a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1099a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1100a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_lshift: 1101a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_rshift: 11025c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace if (state->language_version < 130) { 11035c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace _mesa_glsl_error(&loc, state, "operator %s requires GLSL 1.30", 11045c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace operator_string(this->oper)); 11055c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace error_emitted = true; 11065c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace } 11075c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace 11085c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace op[0] = this->subexpressions[0]->hir(instructions, state); 11095c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace op[1] = this->subexpressions[1]->hir(instructions, state); 1110c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace type = shift_result_type(op[0]->type, op[1]->type, this->oper, state, 1111c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace &loc); 11125c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace result = new(ctx) ir_expression(operations[this->oper], type, 11135c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace op[0], op[1]); 11145c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); 11155c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace break; 1116a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1117a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_less: 1118a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_greater: 1119a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_lequal: 1120a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_gequal: 112118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick op[0] = this->subexpressions[0]->hir(instructions, state); 112218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick op[1] = this->subexpressions[1]->hir(instructions, state); 1123a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 112465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt type = relational_result_type(op[0], op[1], state, & loc); 1125a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1126a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* The relational operators must either generate an error or result 1127a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * in a scalar boolean. See page 57 of the GLSL 1.50 spec. 1128a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 1129a43817a483a8c4a480ef4e6dfda2cef899300eb0Ian Romanick assert(type->is_error() 1130a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick || ((type->base_type == GLSL_TYPE_BOOL) 1131cb36f8aaeeb09660843316270a781948f773d90bIan Romanick && type->is_scalar())); 1132a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 11331660a2954797e056caba319c5d6c70b0d4be22feCarl Worth result = new(ctx) ir_expression(operations[this->oper], type, 11341660a2954797e056caba319c5d6c70b0d4be22feCarl Worth op[0], op[1]); 113565e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt error_emitted = type->is_error(); 1136a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1137a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1138a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_nequal: 1139a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_equal: 11406e659caaa946339a2de3890a8bed091ccb65102aIan Romanick op[0] = this->subexpressions[0]->hir(instructions, state); 11416e659caaa946339a2de3890a8bed091ccb65102aIan Romanick op[1] = this->subexpressions[1]->hir(instructions, state); 11426e659caaa946339a2de3890a8bed091ccb65102aIan Romanick 11436e659caaa946339a2de3890a8bed091ccb65102aIan Romanick /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec: 11446e659caaa946339a2de3890a8bed091ccb65102aIan Romanick * 11456e659caaa946339a2de3890a8bed091ccb65102aIan Romanick * "The equality operators equal (==), and not equal (!=) 11466e659caaa946339a2de3890a8bed091ccb65102aIan Romanick * operate on all types. They result in a scalar Boolean. If 11476e659caaa946339a2de3890a8bed091ccb65102aIan Romanick * the operand types do not match, then there must be a 11486e659caaa946339a2de3890a8bed091ccb65102aIan Romanick * conversion from Section 4.1.10 "Implicit Conversions" 11496e659caaa946339a2de3890a8bed091ccb65102aIan Romanick * applied to one operand that can make them match, in which 11506e659caaa946339a2de3890a8bed091ccb65102aIan Romanick * case this conversion is done." 11516e659caaa946339a2de3890a8bed091ccb65102aIan Romanick */ 1152bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick if ((!apply_implicit_conversion(op[0]->type, op[1], state) 1153bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick && !apply_implicit_conversion(op[1]->type, op[0], state)) 1154212b0327b47033442842a7be3d7fb10e08e2bf66Ian Romanick || (op[0]->type != op[1]->type)) { 11556e659caaa946339a2de3890a8bed091ccb65102aIan Romanick _mesa_glsl_error(& loc, state, "operands of `%s' must have the same " 11566e659caaa946339a2de3890a8bed091ccb65102aIan Romanick "type", (this->oper == ast_equal) ? "==" : "!="); 11576e659caaa946339a2de3890a8bed091ccb65102aIan Romanick error_emitted = true; 1158a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick } else if ((state->language_version <= 110) 1159a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick && (op[0]->type->is_array() || op[1]->type->is_array())) { 1160a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick _mesa_glsl_error(& loc, state, "array comparisons forbidden in " 1161a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick "GLSL 1.10"); 1162a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick error_emitted = true; 11636e659caaa946339a2de3890a8bed091ccb65102aIan Romanick } 11646e659caaa946339a2de3890a8bed091ccb65102aIan Romanick 1165175829f1a8ab0df7594131cc569462e45c1974ecEric Anholt if (error_emitted) { 1166175829f1a8ab0df7594131cc569462e45c1974ecEric Anholt result = new(ctx) ir_constant(false); 1167175829f1a8ab0df7594131cc569462e45c1974ecEric Anholt } else { 1168175829f1a8ab0df7594131cc569462e45c1974ecEric Anholt result = do_comparison(ctx, operations[this->oper], op[0], op[1]); 1169175829f1a8ab0df7594131cc569462e45c1974ecEric Anholt assert(result->type == glsl_type::bool_type); 1170175829f1a8ab0df7594131cc569462e45c1974ecEric Anholt } 1171a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1172a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1173a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_bit_and: 1174a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_bit_xor: 1175a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_bit_or: 11761eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke op[0] = this->subexpressions[0]->hir(instructions, state); 11771eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke op[1] = this->subexpressions[1]->hir(instructions, state); 1178cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper, 1179cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace state, &loc); 11801eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke result = new(ctx) ir_expression(operations[this->oper], type, 11811eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke op[0], op[1]); 11821eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); 11831eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke break; 11841eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke 1185a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_bit_not: 11861eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke op[0] = this->subexpressions[0]->hir(instructions, state); 11871eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke 11881eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke if (state->language_version < 130) { 11891eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke _mesa_glsl_error(&loc, state, "bit-wise operations require GLSL 1.30"); 11901eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke error_emitted = true; 11911eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke } 11921eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke 11931eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke if (!op[0]->type->is_integer()) { 11941eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke _mesa_glsl_error(&loc, state, "operand of `~' must be an integer"); 11951eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke error_emitted = true; 11961eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke } 11971eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke 119839464489510270bbe472d11f7614c04ce1b6ae33Ian Romanick type = error_emitted ? glsl_type::error_type : op[0]->type; 11991eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke result = new(ctx) ir_expression(ir_unop_bit_not, type, op[0], NULL); 1200a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1201a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 12024950a68bf22ede6f4f368c9783e5401816159574Eric Anholt case ast_logic_and: { 12037ec0c9789669ac88fcdd66c562e6d58281b477ceEric Anholt exec_list rhs_instructions; 120401822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt op[0] = get_scalar_boolean_operand(instructions, state, this, 0, 120501822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt "LHS", &error_emitted); 12067ec0c9789669ac88fcdd66c562e6d58281b477ceEric Anholt op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1, 12077ec0c9789669ac88fcdd66c562e6d58281b477ceEric Anholt "RHS", &error_emitted); 1208b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt 1209ead3589aa2810b66164178a1d55d2063cfa3b041Eric Anholt if (rhs_instructions.is_empty()) { 1210ead3589aa2810b66164178a1d55d2063cfa3b041Eric Anholt result = new(ctx) ir_expression(ir_binop_logic_and, op[0], op[1]); 1211ead3589aa2810b66164178a1d55d2063cfa3b041Eric Anholt type = result->type; 121244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt } else { 121381d664f099a5fd5fac777480532fb4307d591451Ian Romanick ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type, 12147e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick "and_tmp", 12157e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick ir_var_temporary); 121681d664f099a5fd5fac777480532fb4307d591451Ian Romanick instructions->push_tail(tmp); 121781d664f099a5fd5fac777480532fb4307d591451Ian Romanick 12181660a2954797e056caba319c5d6c70b0d4be22feCarl Worth ir_if *const stmt = new(ctx) ir_if(op[0]); 121944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt instructions->push_tail(stmt); 12204950a68bf22ede6f4f368c9783e5401816159574Eric Anholt 12217ec0c9789669ac88fcdd66c562e6d58281b477ceEric Anholt stmt->then_instructions.append_list(&rhs_instructions); 12221660a2954797e056caba319c5d6c70b0d4be22feCarl Worth ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp); 122344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt ir_assignment *const then_assign = 1224aa5ec137757323b424d0f2638c50c93b9b06ffffEric Anholt new(ctx) ir_assignment(then_deref, op[1]); 122544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt stmt->then_instructions.push_tail(then_assign); 12264950a68bf22ede6f4f368c9783e5401816159574Eric Anholt 12271660a2954797e056caba319c5d6c70b0d4be22feCarl Worth ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp); 122844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt ir_assignment *const else_assign = 1229aa5ec137757323b424d0f2638c50c93b9b06ffffEric Anholt new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false)); 123044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt stmt->else_instructions.push_tail(else_assign); 12314950a68bf22ede6f4f368c9783e5401816159574Eric Anholt 12321660a2954797e056caba319c5d6c70b0d4be22feCarl Worth result = new(ctx) ir_dereference_variable(tmp); 123344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt type = tmp->type; 123444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt } 12354950a68bf22ede6f4f368c9783e5401816159574Eric Anholt break; 12364950a68bf22ede6f4f368c9783e5401816159574Eric Anholt } 12374950a68bf22ede6f4f368c9783e5401816159574Eric Anholt 12384950a68bf22ede6f4f368c9783e5401816159574Eric Anholt case ast_logic_or: { 12399e04b190b5f59c5b375645f5756a6edd98a7f90cEric Anholt exec_list rhs_instructions; 124001822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt op[0] = get_scalar_boolean_operand(instructions, state, this, 0, 124101822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt "LHS", &error_emitted); 12429e04b190b5f59c5b375645f5756a6edd98a7f90cEric Anholt op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1, 12439e04b190b5f59c5b375645f5756a6edd98a7f90cEric Anholt "RHS", &error_emitted); 12444950a68bf22ede6f4f368c9783e5401816159574Eric Anholt 1245ead3589aa2810b66164178a1d55d2063cfa3b041Eric Anholt if (rhs_instructions.is_empty()) { 1246ead3589aa2810b66164178a1d55d2063cfa3b041Eric Anholt result = new(ctx) ir_expression(ir_binop_logic_or, op[0], op[1]); 1247ead3589aa2810b66164178a1d55d2063cfa3b041Eric Anholt type = result->type; 124844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt } else { 1249dfd30ca6a95a7d95835dad78ffe1fba4d1f4ef69Kenneth Graunke ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type, 12507e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick "or_tmp", 12517e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick ir_var_temporary); 12520b9ae3befb0bf80e000b159fd44c961a144f9c36Ian Romanick instructions->push_tail(tmp); 12534950a68bf22ede6f4f368c9783e5401816159574Eric Anholt 125481d664f099a5fd5fac777480532fb4307d591451Ian Romanick ir_if *const stmt = new(ctx) ir_if(op[0]); 125581d664f099a5fd5fac777480532fb4307d591451Ian Romanick instructions->push_tail(stmt); 125681d664f099a5fd5fac777480532fb4307d591451Ian Romanick 12571660a2954797e056caba319c5d6c70b0d4be22feCarl Worth ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp); 125844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt ir_assignment *const then_assign = 1259aa5ec137757323b424d0f2638c50c93b9b06ffffEric Anholt new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true)); 126044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt stmt->then_instructions.push_tail(then_assign); 12614950a68bf22ede6f4f368c9783e5401816159574Eric Anholt 12629e04b190b5f59c5b375645f5756a6edd98a7f90cEric Anholt stmt->else_instructions.append_list(&rhs_instructions); 12631660a2954797e056caba319c5d6c70b0d4be22feCarl Worth ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp); 126444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt ir_assignment *const else_assign = 1265aa5ec137757323b424d0f2638c50c93b9b06ffffEric Anholt new(ctx) ir_assignment(else_deref, op[1]); 126644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt stmt->else_instructions.push_tail(else_assign); 12674950a68bf22ede6f4f368c9783e5401816159574Eric Anholt 12681660a2954797e056caba319c5d6c70b0d4be22feCarl Worth result = new(ctx) ir_dereference_variable(tmp); 126944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt type = tmp->type; 127044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt } 12714950a68bf22ede6f4f368c9783e5401816159574Eric Anholt break; 12724950a68bf22ede6f4f368c9783e5401816159574Eric Anholt } 12734950a68bf22ede6f4f368c9783e5401816159574Eric Anholt 12744950a68bf22ede6f4f368c9783e5401816159574Eric Anholt case ast_logic_xor: 1275756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec: 1276756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt * 1277756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt * "The logical binary operators and (&&), or ( | | ), and 1278756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt * exclusive or (^^). They operate only on two Boolean 1279756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt * expressions and result in a Boolean expression." 1280756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt */ 1281756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt op[0] = get_scalar_boolean_operand(instructions, state, this, 0, "LHS", 1282756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt &error_emitted); 1283756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt op[1] = get_scalar_boolean_operand(instructions, state, this, 1, "RHS", 1284756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt &error_emitted); 12854950a68bf22ede6f4f368c9783e5401816159574Eric Anholt 12861660a2954797e056caba319c5d6c70b0d4be22feCarl Worth result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type, 12871660a2954797e056caba319c5d6c70b0d4be22feCarl Worth op[0], op[1]); 1288a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1289a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1290a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt case ast_logic_not: 129101822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt op[0] = get_scalar_boolean_operand(instructions, state, this, 0, 129201822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt "operand", &error_emitted); 1293a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt 12941660a2954797e056caba319c5d6c70b0d4be22feCarl Worth result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type, 12951660a2954797e056caba319c5d6c70b0d4be22feCarl Worth op[0], NULL); 1296a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt break; 1297a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt 1298a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_mul_assign: 1299a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_div_assign: 1300a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_add_assign: 1301a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_sub_assign: { 130218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick op[0] = this->subexpressions[0]->hir(instructions, state); 130318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick op[1] = this->subexpressions[1]->hir(instructions, state); 1304a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1305bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick type = arithmetic_result_type(op[0], op[1], 130618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick (this->oper == ast_mul_assign), 1307a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt state, & loc); 1308a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 13091660a2954797e056caba319c5d6c70b0d4be22feCarl Worth ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type, 13101660a2954797e056caba319c5d6c70b0d4be22feCarl Worth op[0], op[1]); 1311a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 13123e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt result = do_assignment(instructions, state, 1313e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick this->subexpressions[0]->non_lvalue_description, 131485caea29c18fad89050ac366c558afef568dcb3fIan Romanick op[0]->clone(ctx, NULL), temp_rhs, false, 131510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt this->subexpressions[0]->get_location()); 131610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt error_emitted = (op[0]->type->is_error()); 1317a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1318a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* GLSL 1.10 does not allow array assignment. However, we don't have to 1319a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * explicitly test for this because none of the binary expression 1320a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * operators allow array operands either. 1321a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 1322a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1323a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1324a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 1325a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 132648a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt case ast_mod_assign: { 132748a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt op[0] = this->subexpressions[0]->hir(instructions, state); 132848a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt op[1] = this->subexpressions[1]->hir(instructions, state); 132948a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt 133065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt type = modulus_result_type(op[0]->type, op[1]->type, state, & loc); 133148a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt 133248a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt assert(operations[this->oper] == ir_binop_mod); 133348a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt 1334768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick ir_rvalue *temp_rhs; 13351660a2954797e056caba319c5d6c70b0d4be22feCarl Worth temp_rhs = new(ctx) ir_expression(operations[this->oper], type, 13361660a2954797e056caba319c5d6c70b0d4be22feCarl Worth op[0], op[1]); 133748a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt 13383e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt result = do_assignment(instructions, state, 1339e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick this->subexpressions[0]->non_lvalue_description, 134085caea29c18fad89050ac366c558afef568dcb3fIan Romanick op[0]->clone(ctx, NULL), temp_rhs, false, 134148a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt this->subexpressions[0]->get_location()); 134265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt error_emitted = type->is_error(); 134348a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt break; 134448a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt } 1345a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1346a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_ls_assign: 1347338ed6ec297d76746b6466c26c307722af965e60Chad Versace case ast_rs_assign: { 1348338ed6ec297d76746b6466c26c307722af965e60Chad Versace op[0] = this->subexpressions[0]->hir(instructions, state); 1349338ed6ec297d76746b6466c26c307722af965e60Chad Versace op[1] = this->subexpressions[1]->hir(instructions, state); 1350338ed6ec297d76746b6466c26c307722af965e60Chad Versace type = shift_result_type(op[0]->type, op[1]->type, this->oper, state, 1351338ed6ec297d76746b6466c26c307722af965e60Chad Versace &loc); 1352338ed6ec297d76746b6466c26c307722af965e60Chad Versace ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], 1353338ed6ec297d76746b6466c26c307722af965e60Chad Versace type, op[0], op[1]); 1354e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick result = do_assignment(instructions, state, 1355e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick this->subexpressions[0]->non_lvalue_description, 1356e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick op[0]->clone(ctx, NULL), temp_rhs, false, 1357338ed6ec297d76746b6466c26c307722af965e60Chad Versace this->subexpressions[0]->get_location()); 1358338ed6ec297d76746b6466c26c307722af965e60Chad Versace error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); 1359251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick break; 1360338ed6ec297d76746b6466c26c307722af965e60Chad Versace } 1361a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1362a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_and_assign: 1363a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_xor_assign: 1364d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace case ast_or_assign: { 1365d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace op[0] = this->subexpressions[0]->hir(instructions, state); 1366d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace op[1] = this->subexpressions[1]->hir(instructions, state); 1367d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper, 1368d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace state, &loc); 1369d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], 1370d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace type, op[0], op[1]); 1371e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick result = do_assignment(instructions, state, 1372e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick this->subexpressions[0]->non_lvalue_description, 1373e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick op[0]->clone(ctx, NULL), temp_rhs, false, 1374d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace this->subexpressions[0]->get_location()); 1375d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); 1376251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick break; 1377d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace } 1378a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 137996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick case ast_conditional: { 138096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec: 138196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick * 138296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick * "The ternary selection operator (?:). It operates on three 138396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick * expressions (exp1 ? exp2 : exp3). This operator evaluates the 138496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick * first expression, which must result in a scalar Boolean." 138596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick */ 138601822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt op[0] = get_scalar_boolean_operand(instructions, state, this, 0, 138701822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt "condition", &error_emitted); 138896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick 138996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick /* The :? operator is implemented by generating an anonymous temporary 139096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick * followed by an if-statement. The last instruction in each branch of 139196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick * the if-statement assigns a value to the anonymous temporary. This 139296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick * temporary is the r-value of the expression. 139396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick */ 13940ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick exec_list then_instructions; 13950ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick exec_list else_instructions; 139696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick 13970ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick op[1] = this->subexpressions[1]->hir(&then_instructions, state); 13980ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick op[2] = this->subexpressions[2]->hir(&else_instructions, state); 139996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick 140096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec: 140196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick * 140296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick * "The second and third expressions can be any type, as 140396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick * long their types match, or there is a conversion in 140496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick * Section 4.1.10 "Implicit Conversions" that can be applied 140596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick * to one of the expressions to make their types match. This 140696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick * resulting matching type is the type of the entire 140796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick * expression." 140896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick */ 1409bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick if ((!apply_implicit_conversion(op[1]->type, op[2], state) 1410bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick && !apply_implicit_conversion(op[2]->type, op[1], state)) 1411db9be2e7aa3a56e43b725ad7725fe6b424e4933eIan Romanick || (op[1]->type != op[2]->type)) { 141296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick YYLTYPE loc = this->subexpressions[1]->get_location(); 141396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick 141496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick _mesa_glsl_error(& loc, state, "Second and third operands of ?: " 141596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick "operator must have matching types."); 141696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick error_emitted = true; 14170ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick type = glsl_type::error_type; 1418db9be2e7aa3a56e43b725ad7725fe6b424e4933eIan Romanick } else { 14190ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick type = op[1]->type; 142096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick } 142196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick 1422f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec: 1423f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick * 1424f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick * "The second and third expressions must be the same type, but can 1425f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick * be of any type other than an array." 1426f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick */ 1427f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick if ((state->language_version <= 110) && type->is_array()) { 1428f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick _mesa_glsl_error(& loc, state, "Second and third operands of ?: " 1429f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick "operator must not be arrays."); 1430f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick error_emitted = true; 1431f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick } 1432f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick 14337825d3d15710fdfcfc503754862963aac8065480Ian Romanick ir_constant *cond_val = op[0]->constant_expression_value(); 14347825d3d15710fdfcfc503754862963aac8065480Ian Romanick ir_constant *then_val = op[1]->constant_expression_value(); 14357825d3d15710fdfcfc503754862963aac8065480Ian Romanick ir_constant *else_val = op[2]->constant_expression_value(); 14367825d3d15710fdfcfc503754862963aac8065480Ian Romanick 14377825d3d15710fdfcfc503754862963aac8065480Ian Romanick if (then_instructions.is_empty() 14387825d3d15710fdfcfc503754862963aac8065480Ian Romanick && else_instructions.is_empty() 14397825d3d15710fdfcfc503754862963aac8065480Ian Romanick && (cond_val != NULL) && (then_val != NULL) && (else_val != NULL)) { 14407825d3d15710fdfcfc503754862963aac8065480Ian Romanick result = (cond_val->value.b[0]) ? then_val : else_val; 14417825d3d15710fdfcfc503754862963aac8065480Ian Romanick } else { 14427e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick ir_variable *const tmp = 14437e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary); 14440b9ae3befb0bf80e000b159fd44c961a144f9c36Ian Romanick instructions->push_tail(tmp); 14450ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick 14461660a2954797e056caba319c5d6c70b0d4be22feCarl Worth ir_if *const stmt = new(ctx) ir_if(op[0]); 14477825d3d15710fdfcfc503754862963aac8065480Ian Romanick instructions->push_tail(stmt); 14480ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick 14497825d3d15710fdfcfc503754862963aac8065480Ian Romanick then_instructions.move_nodes_to(& stmt->then_instructions); 14501660a2954797e056caba319c5d6c70b0d4be22feCarl Worth ir_dereference *const then_deref = 14511660a2954797e056caba319c5d6c70b0d4be22feCarl Worth new(ctx) ir_dereference_variable(tmp); 14527825d3d15710fdfcfc503754862963aac8065480Ian Romanick ir_assignment *const then_assign = 1453aa5ec137757323b424d0f2638c50c93b9b06ffffEric Anholt new(ctx) ir_assignment(then_deref, op[1]); 14547825d3d15710fdfcfc503754862963aac8065480Ian Romanick stmt->then_instructions.push_tail(then_assign); 14550ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick 14567825d3d15710fdfcfc503754862963aac8065480Ian Romanick else_instructions.move_nodes_to(& stmt->else_instructions); 14571660a2954797e056caba319c5d6c70b0d4be22feCarl Worth ir_dereference *const else_deref = 14581660a2954797e056caba319c5d6c70b0d4be22feCarl Worth new(ctx) ir_dereference_variable(tmp); 14597825d3d15710fdfcfc503754862963aac8065480Ian Romanick ir_assignment *const else_assign = 1460aa5ec137757323b424d0f2638c50c93b9b06ffffEric Anholt new(ctx) ir_assignment(else_deref, op[2]); 14617825d3d15710fdfcfc503754862963aac8065480Ian Romanick stmt->else_instructions.push_tail(else_assign); 14620ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick 14631660a2954797e056caba319c5d6c70b0d4be22feCarl Worth result = new(ctx) ir_dereference_variable(tmp); 14647825d3d15710fdfcfc503754862963aac8065480Ian Romanick } 1465251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick break; 146696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick } 1467a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1468a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_pre_inc: 146976ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt case ast_pre_dec: { 1470fa0a9ac5cdf49865cfc289c4326c73c9dd4a61c5Ian Romanick this->non_lvalue_description = (this->oper == ast_pre_inc) 1471fa0a9ac5cdf49865cfc289c4326c73c9dd4a61c5Ian Romanick ? "pre-increment operation" : "pre-decrement operation"; 1472fa0a9ac5cdf49865cfc289c4326c73c9dd4a61c5Ian Romanick 147376ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt op[0] = this->subexpressions[0]->hir(instructions, state); 14749abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry op[1] = constant_one_for_inc_dec(ctx, op[0]->type); 147576ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt 1476a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt type = arithmetic_result_type(op[0], op[1], false, state, & loc); 147776ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt 1478768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick ir_rvalue *temp_rhs; 14791660a2954797e056caba319c5d6c70b0d4be22feCarl Worth temp_rhs = new(ctx) ir_expression(operations[this->oper], type, 14801660a2954797e056caba319c5d6c70b0d4be22feCarl Worth op[0], op[1]); 148176ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt 14823e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt result = do_assignment(instructions, state, 1483e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick this->subexpressions[0]->non_lvalue_description, 148485caea29c18fad89050ac366c558afef568dcb3fIan Romanick op[0]->clone(ctx, NULL), temp_rhs, false, 148576ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt this->subexpressions[0]->get_location()); 148676ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt error_emitted = op[0]->type->is_error(); 148776ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt break; 148876ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt } 1489a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1490a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_post_inc: 1491de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt case ast_post_dec: { 1492fa0a9ac5cdf49865cfc289c4326c73c9dd4a61c5Ian Romanick this->non_lvalue_description = (this->oper == ast_post_inc) 1493fa0a9ac5cdf49865cfc289c4326c73c9dd4a61c5Ian Romanick ? "post-increment operation" : "post-decrement operation"; 1494de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt op[0] = this->subexpressions[0]->hir(instructions, state); 14959abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry op[1] = constant_one_for_inc_dec(ctx, op[0]->type); 1496de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt 1497de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); 1498de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt 1499a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt type = arithmetic_result_type(op[0], op[1], false, state, & loc); 1500de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt 1501768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick ir_rvalue *temp_rhs; 15021660a2954797e056caba319c5d6c70b0d4be22feCarl Worth temp_rhs = new(ctx) ir_expression(operations[this->oper], type, 15031660a2954797e056caba319c5d6c70b0d4be22feCarl Worth op[0], op[1]); 1504de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt 1505de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt /* Get a temporary of a copy of the lvalue before it's modified. 1506de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt * This may get thrown away later. 1507de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt */ 15088273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL)); 1509de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt 15103e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt (void)do_assignment(instructions, state, 1511e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick this->subexpressions[0]->non_lvalue_description, 151285caea29c18fad89050ac366c558afef568dcb3fIan Romanick op[0]->clone(ctx, NULL), temp_rhs, false, 1513de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt this->subexpressions[0]->get_location()); 1514de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt 1515de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt error_emitted = op[0]->type->is_error(); 1516a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1517de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt } 1518a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1519a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_field_selection: 152018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick result = _mesa_ast_field_selection_to_hir(this, instructions, state); 1521a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1522a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 152327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick case ast_array_index: { 152427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick YYLTYPE index_loc = subexpressions[1]->get_location(); 152527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick 152627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick op[0] = subexpressions[0]->hir(instructions, state); 152727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick op[1] = subexpressions[1]->hir(instructions, state); 152827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick 152927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); 153027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick 1531a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick ir_rvalue *const array = op[0]; 1532b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick 15331660a2954797e056caba319c5d6c70b0d4be22feCarl Worth result = new(ctx) ir_dereference_array(op[0], op[1]); 1534b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick 1535b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick /* Do not use op[0] after this point. Use array. 1536b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick */ 1537b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick op[0] = NULL; 1538b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick 153927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick 154027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick if (error_emitted) 154127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick break; 154227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick 154363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick if (!array->type->is_array() 154463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick && !array->type->is_matrix() 154563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick && !array->type->is_vector()) { 154627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick _mesa_glsl_error(& index_loc, state, 154763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick "cannot dereference non-array / non-matrix / " 154863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick "non-vector"); 154927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick error_emitted = true; 155027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick } 155127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick 155227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick if (!op[1]->type->is_integer()) { 155327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick _mesa_glsl_error(& index_loc, state, 155427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick "array index must be integer type"); 155527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick error_emitted = true; 155627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick } else if (!op[1]->type->is_scalar()) { 155727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick _mesa_glsl_error(& index_loc, state, 155827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick "array index must be scalar"); 155927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick error_emitted = true; 156027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick } 156127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick 156227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick /* If the array index is a constant expression and the array has a 156327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick * declared size, ensure that the access is in-bounds. If the array 156427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick * index is not a constant expression, ensure that the array has a 156527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick * declared size. 156627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick */ 156727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick ir_constant *const const_index = op[1]->constant_expression_value(); 156827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick if (const_index != NULL) { 156927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick const int idx = const_index->value.i[0]; 157063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick const char *type_name; 157163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick unsigned bound = 0; 157263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick 157363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick if (array->type->is_matrix()) { 157463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick type_name = "matrix"; 157563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick } else if (array->type->is_vector()) { 157663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick type_name = "vector"; 157763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick } else { 157863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick type_name = "array"; 157963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick } 158027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick 158127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec: 158227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick * 158327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick * "It is illegal to declare an array with a size, and then 158427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick * later (in the same shader) index the same array with an 158527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick * integral constant expression greater than or equal to the 158627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick * declared size. It is also illegal to index an array with a 158727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick * negative constant expression." 158827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick */ 158963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick if (array->type->is_matrix()) { 159063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick if (array->type->row_type()->vector_elements <= idx) { 159163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick bound = array->type->row_type()->vector_elements; 159263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick } 159363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick } else if (array->type->is_vector()) { 159463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick if (array->type->vector_elements <= idx) { 159563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick bound = array->type->vector_elements; 159663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick } 159763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick } else { 159863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick if ((array->type->array_size() > 0) 159963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick && (array->type->array_size() <= idx)) { 160063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick bound = array->type->array_size(); 160163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick } 160227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick } 160327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick 160463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick if (bound > 0) { 160563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick _mesa_glsl_error(& loc, state, "%s index must be < %u", 160663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick type_name, bound); 160763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick error_emitted = true; 160863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick } else if (idx < 0) { 160963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick _mesa_glsl_error(& loc, state, "%s index must be >= 0", 161063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick type_name); 161127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick error_emitted = true; 161227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick } 1613b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick 161463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick if (array->type->is_array()) { 1615a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick /* If the array is a variable dereference, it dereferences the 1616a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick * whole array, by definition. Use this to get the variable. 1617a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick * 1618a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick * FINISHME: Should some methods for getting / setting / testing 1619a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick * FINISHME: array access limits be added to ir_dereference? 1620a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick */ 1621a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick ir_variable *const v = array->whole_variable_referenced(); 162293b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry if ((v != NULL) && (unsigned(idx) > v->max_array_access)) { 162363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick v->max_array_access = idx; 162493b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry 162593b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry /* Check whether this access will, as a side effect, implicitly 162693b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry * cause the size of a built-in array to be too large. 162793b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry */ 162893b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry if (check_builtin_array_max_size(v->name, idx+1, loc, state)) 162993b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry error_emitted = true; 163093b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry } 163163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick } 16322b7c42b40ae459f7b290eb134d6dabd075aab9f0Kenneth Graunke } else if (array->type->array_size() == 0) { 16332b7c42b40ae459f7b290eb134d6dabd075aab9f0Kenneth Graunke _mesa_glsl_error(&loc, state, "unsized array index must be constant"); 1634a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt } else { 1635a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt if (array->type->is_array()) { 16365226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius /* whole_variable_referenced can return NULL if the array is a 16375226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius * member of a structure. In this case it is safe to not update 16385226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius * the max_array_access field because it is never used for fields 16395226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius * of structures. 16405226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius */ 1641a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt ir_variable *v = array->whole_variable_referenced(); 16425226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius if (v != NULL) 16430d9d036004f135c38990c60f46074b70cff6e663Ian Romanick v->max_array_access = array->type->array_size() - 1; 1644a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt } 164527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick } 164627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick 1647e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick /* From page 23 (29 of the PDF) of the GLSL 1.30 spec: 1648e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick * 1649f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace * "Samplers aggregated into arrays within a shader (using square 1650f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace * brackets [ ]) can only be indexed with integral constant 1651f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace * expressions [...]." 1652e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick * 1653e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick * This restriction was added in GLSL 1.30. Shaders using earlier version 1654e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick * of the language should not be rejected by the compiler front-end for 1655e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick * using this construct. This allows useful things such as using a loop 1656e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick * counter as the index to an array of samplers. If the loop in unrolled, 1657e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick * the code should compile correctly. Instead, emit a warning. 1658f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace */ 1659f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace if (array->type->is_array() && 1660f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace array->type->element_type()->is_sampler() && 1661f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace const_index == NULL) { 1662f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace 1663e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick if (state->language_version == 100) { 1664e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick _mesa_glsl_warning(&loc, state, 1665e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick "sampler arrays indexed with non-constant " 1666e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick "expressions is optional in GLSL ES 1.00"); 1667e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick } else if (state->language_version < 130) { 1668e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick _mesa_glsl_warning(&loc, state, 1669e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick "sampler arrays indexed with non-constant " 1670e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick "expressions is forbidden in GLSL 1.30 and " 1671e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick "later"); 1672e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick } else { 1673e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick _mesa_glsl_error(&loc, state, 1674e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick "sampler arrays indexed with non-constant " 1675e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick "expressions is forbidden in GLSL 1.30 and " 1676e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick "later"); 1677e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick error_emitted = true; 1678e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick } 1679f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace } 1680f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace 168127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick if (error_emitted) 168227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick result->type = glsl_type::error_type; 168327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick 1684a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 168527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick } 1686a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1687a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_function_call: 16887cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick /* Should *NEVER* get here. ast_function_call should always be handled 16897cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick * by ast_function_expression::hir. 1690a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 16917cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick assert(0); 1692a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1693a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1694a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_identifier: { 1695a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* ast_identifier can appear several places in a full abstract syntax 1696a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * tree. This particular use must be at location specified in the grammar 1697a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * as 'variable_identifier'. 1698a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 16998bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick ir_variable *var = 17008bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick state->symbols->get_variable(this->primary_expression.identifier); 1701a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1702a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick if (var != NULL) { 1703bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick var->used = true; 1704dca19a771156685895892740f687cee7cf84a8c9Kenneth Graunke result = new(ctx) ir_dereference_variable(var); 1705a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } else { 170671d0bbfcb2853f37b580ec7b705e55bb0eb426faIan Romanick _mesa_glsl_error(& loc, state, "`%s' undeclared", 170718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick this->primary_expression.identifier); 1708a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1709807e967c615dc80a264af5a89af7649f95481744Kenneth Graunke result = ir_rvalue::error_value(ctx); 1710a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick error_emitted = true; 1711a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 1712a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1713a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 1714a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1715a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_int_constant: 17161660a2954797e056caba319c5d6c70b0d4be22feCarl Worth result = new(ctx) ir_constant(this->primary_expression.int_constant); 1717a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1718a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1719a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_uint_constant: 17201660a2954797e056caba319c5d6c70b0d4be22feCarl Worth result = new(ctx) ir_constant(this->primary_expression.uint_constant); 1721a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1722a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1723a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_float_constant: 17241660a2954797e056caba319c5d6c70b0d4be22feCarl Worth result = new(ctx) ir_constant(this->primary_expression.float_constant); 1725a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1726a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1727a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_bool_constant: 17281660a2954797e056caba319c5d6c70b0d4be22feCarl Worth result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant)); 1729a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1730a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1731a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick case ast_sequence: { 1732a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* It should not be possible to generate a sequence in the AST without 1733a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * any expressions in it. 1734a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 1735304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick assert(!this->expressions.is_empty()); 1736a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1737a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* The r-value of a sequence is the last expression in the sequence. If 1738a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * the other expressions in the sequence do not have side-effects (and 1739a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * therefore add instructions to the instruction list), they get dropped 1740a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * on the floor. 1741a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 17423d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick exec_node *previous_tail_pred = NULL; 17433d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick YYLTYPE previous_operand_loc = loc; 17443d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick 17453d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick foreach_list_typed (ast_node, ast, link, &this->expressions) { 17463d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick /* If one of the operands of comma operator does not generate any 17473d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick * code, we want to emit a warning. At each pass through the loop 17483d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick * previous_tail_pred will point to the last instruction in the 17493d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick * stream *before* processing the previous operand. Naturally, 17503d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick * instructions->tail_pred will point to the last instruction in the 17513d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick * stream *after* processing the previous operand. If the two 17523d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick * pointers match, then the previous operand had no effect. 17533d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick * 17543d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick * The warning behavior here differs slightly from GCC. GCC will 17553d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick * only emit a warning if none of the left-hand operands have an 17563d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick * effect. However, it will emit a warning for each. I believe that 17573d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick * there are some cases in C (especially with GCC extensions) where 17583d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick * it is useful to have an intermediate step in a sequence have no 17593d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick * effect, but I don't think these cases exist in GLSL. Either way, 17603d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick * it would be a giant hassle to replicate that behavior. 17613d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick */ 17623d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick if (previous_tail_pred == instructions->tail_pred) { 17633d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick _mesa_glsl_warning(&previous_operand_loc, state, 17643d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick "left-hand operand of comma expression has " 17653d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick "no effect"); 17663d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick } 17673d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick 17683d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick /* tail_pred is directly accessed instead of using the get_tail() 17693d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick * method for performance reasons. get_tail() has extra code to 17703d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick * return NULL when the list is empty. We don't care about that 17713d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick * here, so using tail_pred directly is fine. 17723d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick */ 17733d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick previous_tail_pred = instructions->tail_pred; 17743d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick previous_operand_loc = ast->get_location(); 17753d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick 1776304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick result = ast->hir(instructions, state); 17773d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick } 1778a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1779a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* Any errors should have already been emitted in the loop above. 1780a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 1781a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick error_emitted = true; 1782a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick break; 1783a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 1784a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 178508ba9778dbf3326bdfc8ca8035b51412032748eaKenneth Graunke type = NULL; /* use result->type, not type. */ 178608ba9778dbf3326bdfc8ca8035b51412032748eaKenneth Graunke assert(result != NULL); 1787a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 178808ba9778dbf3326bdfc8ca8035b51412032748eaKenneth Graunke if (result->type->is_error() && !error_emitted) 178971d0bbfcb2853f37b580ec7b705e55bb0eb426faIan Romanick _mesa_glsl_error(& loc, state, "type mismatch"); 1790a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1791a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick return result; 1792a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick} 1793a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1794a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1795fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue * 17960044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_expression_statement::hir(exec_list *instructions, 179718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick struct _mesa_glsl_parse_state *state) 1798a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{ 1799a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* It is possible to have expression statements that don't have an 1800a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * expression. This is the solitary semicolon: 1801a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * 1802a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * for (i = 0; i < 5; i++) 1803a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * ; 1804a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * 1805a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * In this case the expression will be NULL. Test for NULL and don't do 1806a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * anything in that case. 1807a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 180818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick if (expression != NULL) 180918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick expression->hir(instructions, state); 1810a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1811a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* Statements do not have r-values. 1812a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 1813a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick return NULL; 1814a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick} 1815a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1816a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1817fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue * 18180044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_compound_statement::hir(exec_list *instructions, 181918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick struct _mesa_glsl_parse_state *state) 1820a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{ 182118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick if (new_scope) 18228bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick state->symbols->push_scope(); 1823a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 18242b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick foreach_list_typed (ast_node, ast, link, &this->statements) 1825304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick ast->hir(instructions, state); 1826a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 182718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick if (new_scope) 18288bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick state->symbols->pop_scope(); 1829a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1830a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* Compound statements do not have r-values. 1831a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 1832a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick return NULL; 1833a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick} 1834a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1835a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 183628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanickstatic const glsl_type * 1837d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunkeprocess_array_type(YYLTYPE *loc, const glsl_type *base, ast_node *array_size, 183828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick struct _mesa_glsl_parse_state *state) 183928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick{ 184028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick unsigned length = 0; 184128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick 1842a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick /* From page 19 (page 25) of the GLSL 1.20 spec: 1843a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick * 1844a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick * "Only one-dimensional arrays may be declared." 1845a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick */ 1846a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick if (base->is_array()) { 1847a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick _mesa_glsl_error(loc, state, 1848a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick "invalid array of `%s' (only one-dimensional arrays " 1849a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick "may be declared)", 1850a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick base->name); 1851a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick return glsl_type::error_type; 1852a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick } 185328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick 185428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick if (array_size != NULL) { 185528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick exec_list dummy_instructions; 185628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick ir_rvalue *const ir = array_size->hir(& dummy_instructions, state); 185728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick YYLTYPE loc = array_size->get_location(); 185828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick 185928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick if (ir != NULL) { 186028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick if (!ir->type->is_integer()) { 186128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick _mesa_glsl_error(& loc, state, "array size must be integer type"); 186228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick } else if (!ir->type->is_scalar()) { 186328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick _mesa_glsl_error(& loc, state, "array size must be scalar type"); 186428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick } else { 186528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick ir_constant *const size = ir->constant_expression_value(); 186628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick 186728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick if (size == NULL) { 186828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick _mesa_glsl_error(& loc, state, "array size must be a " 186928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick "constant valued expression"); 187028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick } else if (size->value.i[0] <= 0) { 187128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick _mesa_glsl_error(& loc, state, "array size must be > 0"); 187228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick } else { 187328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick assert(size->type == ir->type); 187428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick length = size->value.u[0]; 1875d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry 1876d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry /* If the array size is const (and we've verified that 1877d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry * it is) then no instructions should have been emitted 1878d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry * when we converted it to HIR. If they were emitted, 1879d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry * then either the array size isn't const after all, or 1880d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry * we are emitting unnecessary instructions. 1881d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry */ 1882d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry assert(dummy_instructions.is_empty()); 188328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick } 188428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick } 188528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick } 1886d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke } else if (state->es_shader) { 1887d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke /* Section 10.17 of the GLSL ES 1.00 specification states that unsized 1888d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke * array declarations have been removed from the language. 1889d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke */ 1890d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke _mesa_glsl_error(loc, state, "unsized array declarations are not " 1891d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke "allowed in GLSL ES 1.00."); 189228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick } 189328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick 1894f38d15b80d4e4c8ecb7a76087cdc49835f0aa271Ian Romanick return glsl_type::get_array_instance(base, length); 189528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick} 189628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick 189728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick 1898d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanickconst glsl_type * 1899d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanickast_type_specifier::glsl_type(const char **name, 1900d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick struct _mesa_glsl_parse_state *state) const 1901a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{ 1902d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick const struct glsl_type *type; 1903a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1904ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke type = state->symbols->get_type(this->type_name); 1905ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke *name = this->type_name; 1906a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1907ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke if (this->is_array) { 1908ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke YYLTYPE loc = this->get_location(); 1909ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke type = process_array_type(&loc, type, this->array_size, state); 1910a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 1911a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1912a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick return type; 1913a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick} 1914a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1915a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1916a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic void 1917a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickapply_type_qualifier_to_variable(const struct ast_type_qualifier *qual, 1918768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick ir_variable *var, 19192e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt struct _mesa_glsl_parse_state *state, 1920f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt YYLTYPE *loc, 1921f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt bool ubo_qualifiers_valid) 1922a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{ 1923bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick if (qual->flags.q.invariant) { 1924bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick if (var->used) { 1925bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick _mesa_glsl_error(loc, state, 1926bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick "variable `%s' may not be redeclared " 1927bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick "`invariant' after being used", 1928bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick var->name); 1929bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick } else { 1930bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick var->invariant = 1; 1931bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick } 1932bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick } 1933a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1934e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick if (qual->flags.q.constant || qual->flags.q.attribute 1935e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick || qual->flags.q.uniform 1936e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick || (qual->flags.q.varying && (state->target == fragment_shader))) 1937a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick var->read_only = 1; 1938a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1939e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick if (qual->flags.q.centroid) 1940a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick var->centroid = 1; 1941a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 1942e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick if (qual->flags.q.attribute && state->target != vertex_shader) { 19432e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt var->type = glsl_type::error_type; 19442e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt _mesa_glsl_error(loc, state, 19452e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt "`attribute' variables may not be declared in the " 1946ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick "%s shader", 1947ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick _mesa_glsl_shader_target_name(state->target)); 19482e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt } 19492e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt 195090b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec: 195190b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt * 195290b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt * "The varying qualifier can be used only with the data types 195390b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of 195490b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt * these." 195590b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt */ 1956e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick if (qual->flags.q.varying) { 19570ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt const glsl_type *non_array_type; 19580ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt 19590ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt if (var->type && var->type->is_array()) 19600ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt non_array_type = var->type->fields.array; 19610ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt else 19620ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt non_array_type = var->type; 19630ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt 19640ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt if (non_array_type && non_array_type->base_type != GLSL_TYPE_FLOAT) { 19650ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt var->type = glsl_type::error_type; 19660ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt _mesa_glsl_error(loc, state, 19670ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt "varying variables must be of base type float"); 19680ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt } 196990b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt } 197090b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt 19717e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick /* If there is no qualifier that changes the mode of the variable, leave 19727e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick * the setting alone. 19737e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick */ 1974e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick if (qual->flags.q.in && qual->flags.q.out) 1975a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick var->mode = ir_var_inout; 1976e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick else if (qual->flags.q.attribute || qual->flags.q.in 1977e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick || (qual->flags.q.varying && (state->target == fragment_shader))) 1978a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick var->mode = ir_var_in; 1979e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick else if (qual->flags.q.out 1980e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick || (qual->flags.q.varying && (state->target == vertex_shader))) 1981a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick var->mode = ir_var_out; 1982e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick else if (qual->flags.q.uniform) 1983a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick var->mode = ir_var_uniform; 1984a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 198586b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick if (state->all_invariant && (state->current_function == NULL)) { 198686b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick switch (state->target) { 198786b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick case vertex_shader: 198886b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick if (var->mode == ir_var_out) 198986b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick var->invariant = true; 199086b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick break; 199186b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick case geometry_shader: 199286b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick if ((var->mode == ir_var_in) || (var->mode == ir_var_out)) 199386b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick var->invariant = true; 199486b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick break; 199586b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick case fragment_shader: 199686b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick if (var->mode == ir_var_in) 199786b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick var->invariant = true; 199886b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick break; 199986b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick } 200086b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick } 200186b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick 2002e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick if (qual->flags.q.flat) 2003cf45949d6a896651a5f3864d3b195e26d59eee74Paul Berry var->interpolation = INTERP_QUALIFIER_FLAT; 2004e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick else if (qual->flags.q.noperspective) 2005cf45949d6a896651a5f3864d3b195e26d59eee74Paul Berry var->interpolation = INTERP_QUALIFIER_NOPERSPECTIVE; 2006c488150dea083a9677429b4185c6b20d7facd52bPaul Berry else if (qual->flags.q.smooth) 2007cf45949d6a896651a5f3864d3b195e26d59eee74Paul Berry var->interpolation = INTERP_QUALIFIER_SMOOTH; 2008c488150dea083a9677429b4185c6b20d7facd52bPaul Berry else 2009c488150dea083a9677429b4185c6b20d7facd52bPaul Berry var->interpolation = INTERP_QUALIFIER_NONE; 20109d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick 2011916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt if (var->interpolation != INTERP_QUALIFIER_NONE && 2012916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt !(state->target == vertex_shader && var->mode == ir_var_out) && 2013916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt !(state->target == fragment_shader && var->mode == ir_var_in)) { 2014916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt _mesa_glsl_error(loc, state, 2015916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt "interpolation qualifier `%s' can only be applied to " 2016916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt "vertex shader outputs and fragment shader inputs.", 2017d43f4181e1633d2677a8d828abd4ffc1a7c1e493Kenneth Graunke var->interpolation_string()); 2018916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt } 2019916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt 2020e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick var->pixel_center_integer = qual->flags.q.pixel_center_integer; 2021e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick var->origin_upper_left = qual->flags.q.origin_upper_left; 2022e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick if ((qual->flags.q.origin_upper_left || qual->flags.q.pixel_center_integer) 20238d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick && (strcmp(var->name, "gl_FragCoord") != 0)) { 2024e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick const char *const qual_string = (qual->flags.q.origin_upper_left) 20258d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick ? "origin_upper_left" : "pixel_center_integer"; 20268d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick 20278d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick _mesa_glsl_error(loc, state, 20288d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick "layout qualifier `%s' can only be applied to " 20298d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick "fragment shader input `gl_FragCoord'", 20308d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick qual_string); 20318d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick } 20328d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick 2033eee68d3631813580a14fa51fda6f0c959279256cIan Romanick if (qual->flags.q.explicit_location) { 2034eee68d3631813580a14fa51fda6f0c959279256cIan Romanick const bool global_scope = (state->current_function == NULL); 2035eee68d3631813580a14fa51fda6f0c959279256cIan Romanick bool fail = false; 2036eee68d3631813580a14fa51fda6f0c959279256cIan Romanick const char *string = ""; 2037eee68d3631813580a14fa51fda6f0c959279256cIan Romanick 2038eee68d3631813580a14fa51fda6f0c959279256cIan Romanick /* In the vertex shader only shader inputs can be given explicit 2039eee68d3631813580a14fa51fda6f0c959279256cIan Romanick * locations. 2040eee68d3631813580a14fa51fda6f0c959279256cIan Romanick * 2041eee68d3631813580a14fa51fda6f0c959279256cIan Romanick * In the fragment shader only shader outputs can be given explicit 2042eee68d3631813580a14fa51fda6f0c959279256cIan Romanick * locations. 2043eee68d3631813580a14fa51fda6f0c959279256cIan Romanick */ 2044eee68d3631813580a14fa51fda6f0c959279256cIan Romanick switch (state->target) { 2045eee68d3631813580a14fa51fda6f0c959279256cIan Romanick case vertex_shader: 2046eee68d3631813580a14fa51fda6f0c959279256cIan Romanick if (!global_scope || (var->mode != ir_var_in)) { 2047eee68d3631813580a14fa51fda6f0c959279256cIan Romanick fail = true; 2048eee68d3631813580a14fa51fda6f0c959279256cIan Romanick string = "input"; 2049eee68d3631813580a14fa51fda6f0c959279256cIan Romanick } 2050eee68d3631813580a14fa51fda6f0c959279256cIan Romanick break; 2051eee68d3631813580a14fa51fda6f0c959279256cIan Romanick 2052eee68d3631813580a14fa51fda6f0c959279256cIan Romanick case geometry_shader: 2053eee68d3631813580a14fa51fda6f0c959279256cIan Romanick _mesa_glsl_error(loc, state, 2054eee68d3631813580a14fa51fda6f0c959279256cIan Romanick "geometry shader variables cannot be given " 2055eee68d3631813580a14fa51fda6f0c959279256cIan Romanick "explicit locations\n"); 2056eee68d3631813580a14fa51fda6f0c959279256cIan Romanick break; 2057eee68d3631813580a14fa51fda6f0c959279256cIan Romanick 2058eee68d3631813580a14fa51fda6f0c959279256cIan Romanick case fragment_shader: 2059b078aad8ab22d840456688480a8c27d4664297cePaul Berry if (!global_scope || (var->mode != ir_var_out)) { 2060eee68d3631813580a14fa51fda6f0c959279256cIan Romanick fail = true; 2061eee68d3631813580a14fa51fda6f0c959279256cIan Romanick string = "output"; 2062eee68d3631813580a14fa51fda6f0c959279256cIan Romanick } 2063eee68d3631813580a14fa51fda6f0c959279256cIan Romanick break; 2064a75da2c0e85eb6b8279ec895c3f74cc4aefc0257Kenneth Graunke }; 2065eee68d3631813580a14fa51fda6f0c959279256cIan Romanick 2066eee68d3631813580a14fa51fda6f0c959279256cIan Romanick if (fail) { 2067eee68d3631813580a14fa51fda6f0c959279256cIan Romanick _mesa_glsl_error(loc, state, 2068eee68d3631813580a14fa51fda6f0c959279256cIan Romanick "only %s shader %s variables can be given an " 2069eee68d3631813580a14fa51fda6f0c959279256cIan Romanick "explicit location\n", 2070eee68d3631813580a14fa51fda6f0c959279256cIan Romanick _mesa_glsl_shader_target_name(state->target), 2071eee68d3631813580a14fa51fda6f0c959279256cIan Romanick string); 2072eee68d3631813580a14fa51fda6f0c959279256cIan Romanick } else { 2073eee68d3631813580a14fa51fda6f0c959279256cIan Romanick var->explicit_location = true; 207468a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick 207568a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick /* This bit of silliness is needed because invalid explicit locations 207668a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick * are supposed to be flagged during linking. Small negative values 207768a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias 207868a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS). 207968a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick * The linker needs to be able to differentiate these cases. This 208068a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick * ensures that negative values stay negative. 208168a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick */ 208268a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick if (qual->location >= 0) { 208368a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick var->location = (state->target == vertex_shader) 208468a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick ? (qual->location + VERT_ATTRIB_GENERIC0) 208568a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick : (qual->location + FRAG_RESULT_DATA0); 208668a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick } else { 208768a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick var->location = qual->location; 208868a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick } 20891256a5dcc86014d48bdc6fd10ea5a2fa11241667Dave Airlie if (qual->flags.q.explicit_index) { 20901256a5dcc86014d48bdc6fd10ea5a2fa11241667Dave Airlie var->explicit_index = true; 20911256a5dcc86014d48bdc6fd10ea5a2fa11241667Dave Airlie var->index = qual->index; 20921256a5dcc86014d48bdc6fd10ea5a2fa11241667Dave Airlie } 2093eee68d3631813580a14fa51fda6f0c959279256cIan Romanick } 20941256a5dcc86014d48bdc6fd10ea5a2fa11241667Dave Airlie } else if (qual->flags.q.explicit_index) { 20951256a5dcc86014d48bdc6fd10ea5a2fa11241667Dave Airlie _mesa_glsl_error(loc, state, 20961256a5dcc86014d48bdc6fd10ea5a2fa11241667Dave Airlie "explicit index requires explicit location\n"); 2097eee68d3631813580a14fa51fda6f0c959279256cIan Romanick } 2098eee68d3631813580a14fa51fda6f0c959279256cIan Romanick 20994bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick /* Does the declaration use the 'layout' keyword? 21004bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick */ 21014bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick const bool uses_layout = qual->flags.q.pixel_center_integer 21024bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick || qual->flags.q.origin_upper_left 21031256a5dcc86014d48bdc6fd10ea5a2fa11241667Dave Airlie || qual->flags.q.explicit_location; /* no need for index since it relies on location */ 21044bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick 21054bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick /* Does the declaration use the deprecated 'attribute' or 'varying' 21064bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick * keywords? 21074bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick */ 21084bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick const bool uses_deprecated_qualifier = qual->flags.q.attribute 21094bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick || qual->flags.q.varying; 21104bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick 21114bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick /* Is the 'layout' keyword used with parameters that allow relaxed checking. 21124bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick * Many implementations of GL_ARB_fragment_coord_conventions_enable and some 21134bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable 21144bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick * allowed the layout qualifier to be used with 'varying' and 'attribute'. 21154bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick * These extensions and all following extensions that add the 'layout' 21164bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick * keyword have been modified to require the use of 'in' or 'out'. 21174bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick * 21184bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick * The following extension do not allow the deprecated keywords: 21194bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick * 21204bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick * GL_AMD_conservative_depth 21216b43d6fdda13d73ca7b059f986ce2a2046bf03a0Marek Olšák * GL_ARB_conservative_depth 21224bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick * GL_ARB_gpu_shader5 21234bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick * GL_ARB_separate_shader_objects 21244bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick * GL_ARB_tesselation_shader 21254bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick * GL_ARB_transform_feedback3 21264bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick * GL_ARB_uniform_buffer_object 21274bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick * 21284bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5 21294bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick * allow layout with the deprecated keywords. 21304bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick */ 21314bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick const bool relaxed_layout_qualifier_checking = 21324bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick state->ARB_fragment_coord_conventions_enable; 21334bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick 21344bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick if (uses_layout && uses_deprecated_qualifier) { 21354bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick if (relaxed_layout_qualifier_checking) { 21364bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick _mesa_glsl_warning(loc, state, 21374bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick "`layout' qualifier may not be used with " 21384bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick "`attribute' or `varying'"); 21394bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick } else { 21404bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick _mesa_glsl_error(loc, state, 21414bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick "`layout' qualifier may not be used with " 21424bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick "`attribute' or `varying'"); 21434bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick } 21444bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick } 21454bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick 2146bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace /* Layout qualifiers for gl_FragDepth, which are enabled by extension 2147bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace * AMD_conservative_depth. 2148bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace */ 2149bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace int depth_layout_count = qual->flags.q.depth_any 2150bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace + qual->flags.q.depth_greater 2151bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace + qual->flags.q.depth_less 2152bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace + qual->flags.q.depth_unchanged; 2153bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace if (depth_layout_count > 0 21546b43d6fdda13d73ca7b059f986ce2a2046bf03a0Marek Olšák && !state->AMD_conservative_depth_enable 21556b43d6fdda13d73ca7b059f986ce2a2046bf03a0Marek Olšák && !state->ARB_conservative_depth_enable) { 2156bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace _mesa_glsl_error(loc, state, 21576b43d6fdda13d73ca7b059f986ce2a2046bf03a0Marek Olšák "extension GL_AMD_conservative_depth or " 21586b43d6fdda13d73ca7b059f986ce2a2046bf03a0Marek Olšák "GL_ARB_conservative_depth must be enabled " 2159bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace "to use depth layout qualifiers"); 2160bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace } else if (depth_layout_count > 0 2161bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace && strcmp(var->name, "gl_FragDepth") != 0) { 2162bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace _mesa_glsl_error(loc, state, 2163bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace "depth layout qualifiers can be applied only to " 2164bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace "gl_FragDepth"); 2165bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace } else if (depth_layout_count > 1 2166bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace && strcmp(var->name, "gl_FragDepth") == 0) { 2167bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace _mesa_glsl_error(loc, state, 2168bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace "at most one depth layout qualifier can be applied to " 2169bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace "gl_FragDepth"); 2170bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace } 2171bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace if (qual->flags.q.depth_any) 2172bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace var->depth_layout = ir_depth_layout_any; 2173bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace else if (qual->flags.q.depth_greater) 2174bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace var->depth_layout = ir_depth_layout_greater; 2175bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace else if (qual->flags.q.depth_less) 2176bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace var->depth_layout = ir_depth_layout_less; 2177bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace else if (qual->flags.q.depth_unchanged) 2178bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace var->depth_layout = ir_depth_layout_unchanged; 2179bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace else 2180bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace var->depth_layout = ir_depth_layout_none; 2181f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt 2182f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt if (qual->flags.q.std140 || 2183f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt qual->flags.q.packed || 2184f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt qual->flags.q.shared) { 2185f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt _mesa_glsl_error(loc, state, 2186f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt "uniform block layout qualifiers std140, packed, and " 2187f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt "shared can only be applied to uniform blocks, not " 2188f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt "members"); 2189f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt } 2190f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt 2191f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt if (!ubo_qualifiers_valid && 2192f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt (qual->flags.q.row_major || qual->flags.q.column_major)) { 2193f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt _mesa_glsl_error(loc, state, 2194f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt "uniform block layout qualifiers row_major and " 2195f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt "column_major can only be applied to uniform block " 2196f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt "members"); 2197f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt } 2198a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick} 2199a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 22008e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick/** 22018e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * Get the variable that is being redeclared by this declaration 22028e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * 22038e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * Semantic checks to verify the validity of the redeclaration are also 22048e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * performed. If semantic checks fail, compilation error will be emitted via 22058e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned. 22068e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * 22078e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * \returns 22088e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * A pointer to an existing variable in the current scope if the declaration 22098e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * is a redeclaration, \c NULL otherwise. 22108e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick */ 22118e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanickir_variable * 22128e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanickget_variable_being_redeclared(ir_variable *var, ast_declaration *decl, 22138e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick struct _mesa_glsl_parse_state *state) 22148e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick{ 22158e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick /* Check if this declaration is actually a re-declaration, either to 22168e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * resize an array or add qualifiers to an existing variable. 22178e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * 22188e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * This is allowed for variables in the current scope, or when at 22198e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * global scope (for built-ins in the implicit outer scope). 22208e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick */ 22218e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick ir_variable *earlier = state->symbols->get_variable(decl->identifier); 22228e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick if (earlier == NULL || 22238e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick (state->current_function != NULL && 22248e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick !state->symbols->name_declared_this_scope(decl->identifier))) { 22258e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick return NULL; 22268e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick } 22278e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick 22288e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick 22298e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick YYLTYPE loc = decl->get_location(); 22308e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick 22318e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec, 22328e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * 22338e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * "It is legal to declare an array without a size and then 22348e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * later re-declare the same name as an array of the same 22358e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * type and specify a size." 22368e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick */ 22378e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick if ((earlier->type->array_size() == 0) 22388e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick && var->type->is_array() 22398e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick && (var->type->element_type() == earlier->type->element_type())) { 22408e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick /* FINISHME: This doesn't match the qualifiers on the two 22418e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * FINISHME: declarations. It's not 100% clear whether this is 22428e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * FINISHME: required or not. 22438e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick */ 22448e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick 22458e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick const unsigned size = unsigned(var->type->array_size()); 224693b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry check_builtin_array_max_size(var->name, size, loc, state); 224793b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry if ((size > 0) && (size <= earlier->max_array_access)) { 22488e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick _mesa_glsl_error(& loc, state, "array size must be > %u due to " 22498e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick "previous access", 22508e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick earlier->max_array_access); 22518e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick } 22528e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick 22538e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick earlier->type = var->type; 22548e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick delete var; 22558e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick var = NULL; 22568e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick } else if (state->ARB_fragment_coord_conventions_enable 22578e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick && strcmp(var->name, "gl_FragCoord") == 0 22588e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick && earlier->type == var->type 22598e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick && earlier->mode == var->mode) { 22608e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick /* Allow redeclaration of gl_FragCoord for ARB_fcc layout 22618e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * qualifiers. 22628e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick */ 22638e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick earlier->origin_upper_left = var->origin_upper_left; 22648e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick earlier->pixel_center_integer = var->pixel_center_integer; 22658e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick 22668e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick /* According to section 4.3.7 of the GLSL 1.30 spec, 22678e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * the following built-in varaibles can be redeclared with an 22688e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * interpolation qualifier: 22698e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * * gl_FrontColor 22708e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * * gl_BackColor 22718e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * * gl_FrontSecondaryColor 22728e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * * gl_BackSecondaryColor 22738e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * * gl_Color 22748e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * * gl_SecondaryColor 22758e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick */ 22768e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick } else if (state->language_version >= 130 22778e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick && (strcmp(var->name, "gl_FrontColor") == 0 22788e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick || strcmp(var->name, "gl_BackColor") == 0 22798e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick || strcmp(var->name, "gl_FrontSecondaryColor") == 0 22808e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick || strcmp(var->name, "gl_BackSecondaryColor") == 0 22818e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick || strcmp(var->name, "gl_Color") == 0 22828e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick || strcmp(var->name, "gl_SecondaryColor") == 0) 22838e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick && earlier->type == var->type 22848e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick && earlier->mode == var->mode) { 22858e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick earlier->interpolation = var->interpolation; 22868e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick 22878e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick /* Layout qualifiers for gl_FragDepth. */ 22886b43d6fdda13d73ca7b059f986ce2a2046bf03a0Marek Olšák } else if ((state->AMD_conservative_depth_enable || 22896b43d6fdda13d73ca7b059f986ce2a2046bf03a0Marek Olšák state->ARB_conservative_depth_enable) 22908e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick && strcmp(var->name, "gl_FragDepth") == 0 22918e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick && earlier->type == var->type 22928e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick && earlier->mode == var->mode) { 22938e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick 22948e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick /** From the AMD_conservative_depth spec: 22958e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * Within any shader, the first redeclarations of gl_FragDepth 22968e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * must appear before any use of gl_FragDepth. 22978e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick */ 22988e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick if (earlier->used) { 22998e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick _mesa_glsl_error(&loc, state, 23008e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick "the first redeclaration of gl_FragDepth " 23018e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick "must appear before any use of gl_FragDepth"); 23028e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick } 23038e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick 23048e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick /* Prevent inconsistent redeclaration of depth layout qualifier. */ 23058e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick if (earlier->depth_layout != ir_depth_layout_none 23068e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick && earlier->depth_layout != var->depth_layout) { 23078e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick _mesa_glsl_error(&loc, state, 23088e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick "gl_FragDepth: depth layout is declared here " 23098e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick "as '%s, but it was previously declared as " 23108e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick "'%s'", 23118e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick depth_layout_string(var->depth_layout), 23128e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick depth_layout_string(earlier->depth_layout)); 23138e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick } 23148e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick 23158e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick earlier->depth_layout = var->depth_layout; 23168e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick 23178e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick } else { 23188e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick _mesa_glsl_error(&loc, state, "`%s' redeclared", decl->identifier); 23198e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick } 23208e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick 23218e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick return earlier; 23228e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick} 2323a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 23240292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick/** 23250292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick * Generate the IR for an initializer in a variable declaration 23260292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick */ 23270292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanickir_rvalue * 23280292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanickprocess_initializer(ir_variable *var, ast_declaration *decl, 23290292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick ast_fully_specified_type *type, 23300292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick exec_list *initializer_instructions, 23310292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick struct _mesa_glsl_parse_state *state) 23320292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick{ 23330292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick ir_rvalue *result = NULL; 23340292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick 23350292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick YYLTYPE initializer_loc = decl->initializer->get_location(); 23360292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick 23370292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec: 23380292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick * 23390292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick * "All uniform variables are read-only and are initialized either 23400292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick * directly by an application via API commands, or indirectly by 23410292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick * OpenGL." 23420292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick */ 23430292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick if ((state->language_version <= 110) 23440292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick && (var->mode == ir_var_uniform)) { 23450292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick _mesa_glsl_error(& initializer_loc, state, 23460292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick "cannot initialize uniforms in GLSL 1.10"); 23470292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick } 23480292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick 23490292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick if (var->type->is_sampler()) { 23500292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick _mesa_glsl_error(& initializer_loc, state, 23510292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick "cannot initialize samplers"); 23520292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick } 23530292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick 23540292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick if ((var->mode == ir_var_in) && (state->current_function == NULL)) { 23550292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick _mesa_glsl_error(& initializer_loc, state, 23560292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick "cannot initialize %s shader input / %s", 23570292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick _mesa_glsl_shader_target_name(state->target), 23580292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick (state->target == vertex_shader) 23590292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick ? "attribute" : "varying"); 23600292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick } 23610292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick 23620292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick ir_dereference *const lhs = new(state) ir_dereference_variable(var); 23630292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick ir_rvalue *rhs = decl->initializer->hir(initializer_instructions, 23640292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick state); 23650292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick 23660292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick /* Calculate the constant value if this is a const or uniform 23670292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick * declaration. 23680292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick */ 23690292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick if (type->qualifier.flags.q.constant 23700292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick || type->qualifier.flags.q.uniform) { 237185caea29c18fad89050ac366c558afef568dcb3fIan Romanick ir_rvalue *new_rhs = validate_assignment(state, var->type, rhs, true); 23720292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick if (new_rhs != NULL) { 23730292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick rhs = new_rhs; 23740292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick 23750292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick ir_constant *constant_value = rhs->constant_expression_value(); 23760292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick if (!constant_value) { 23770292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick _mesa_glsl_error(& initializer_loc, state, 23780292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick "initializer of %s variable `%s' must be a " 23790292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick "constant expression", 23800292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick (type->qualifier.flags.q.constant) 23810292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick ? "const" : "uniform", 23820292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick decl->identifier); 23830292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick if (var->type->is_numeric()) { 23840292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick /* Reduce cascading errors. */ 23850292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick var->constant_value = ir_constant::zero(state, var->type); 23860292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick } 23870292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick } else { 23880292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick rhs = constant_value; 23890292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick var->constant_value = constant_value; 23900292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick } 23910292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick } else { 23920292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick _mesa_glsl_error(&initializer_loc, state, 23930292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick "initializer of type %s cannot be assigned to " 23940292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick "variable of type %s", 23950292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick rhs->type->name, var->type->name); 23960292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick if (var->type->is_numeric()) { 23970292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick /* Reduce cascading errors. */ 23980292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick var->constant_value = ir_constant::zero(state, var->type); 23990292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick } 24000292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick } 24010292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick } 24020292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick 24030292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick if (rhs && !rhs->type->is_error()) { 24040292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick bool temp = var->read_only; 24050292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick if (type->qualifier.flags.q.constant) 24060292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick var->read_only = false; 24070292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick 24080292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick /* Never emit code to initialize a uniform. 24090292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick */ 24100292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick const glsl_type *initializer_type; 24110292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick if (!type->qualifier.flags.q.uniform) { 24120292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick result = do_assignment(initializer_instructions, state, 2413e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick NULL, 241485caea29c18fad89050ac366c558afef568dcb3fIan Romanick lhs, rhs, true, 24150292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick type->get_location()); 24160292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick initializer_type = result->type; 24170292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick } else 24180292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick initializer_type = rhs->type; 24190292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick 2420f37b1ad937dd2c420f4c9fd9aa5887942bd31f3fIan Romanick var->constant_initializer = rhs->constant_expression_value(); 2421f37b1ad937dd2c420f4c9fd9aa5887942bd31f3fIan Romanick var->has_initializer = true; 2422f37b1ad937dd2c420f4c9fd9aa5887942bd31f3fIan Romanick 24230292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick /* If the declared variable is an unsized array, it must inherrit 24240292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick * its full type from the initializer. A declaration such as 24250292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick * 24260292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0); 24270292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick * 24280292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick * becomes 24290292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick * 24300292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0); 24310292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick * 24320292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick * The assignment generated in the if-statement (below) will also 24330292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick * automatically handle this case for non-uniforms. 24340292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick * 24350292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick * If the declared variable is not an array, the types must 24360292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick * already match exactly. As a result, the type assignment 24370292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick * here can be done unconditionally. For non-uniforms the call 24380292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick * to do_assignment can change the type of the initializer (via 24390292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick * the implicit conversion rules). For uniforms the initializer 24400292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick * must be a constant expression, and the type of that expression 24410292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick * was validated above. 24420292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick */ 24430292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick var->type = initializer_type; 24440292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick 24450292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick var->read_only = temp; 24460292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick } 24470292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick 24480292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick return result; 24490292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick} 24500292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick 2451fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue * 24520044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_declarator_list::hir(exec_list *instructions, 245318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick struct _mesa_glsl_parse_state *state) 2454a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{ 2455953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke void *ctx = state; 2456a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick const struct glsl_type *decl_type; 2457a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick const char *type_name = NULL; 24588558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt ir_rvalue *result = NULL; 2459c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick YYLTYPE loc = this->get_location(); 2460a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 24616f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec: 24626f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick * 24636f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick * "To ensure that a particular output variable is invariant, it is 24646f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick * necessary to use the invariant qualifier. It can either be used to 24656f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick * qualify a previously declared variable as being invariant 24666f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick * 24676f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick * invariant gl_Position; // make existing gl_Position be invariant" 24686f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick * 24696f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick * In these cases the parser will set the 'invariant' flag in the declarator 24706f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick * list, and the type will be NULL. 24716f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick */ 24726f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick if (this->invariant) { 24736f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick assert(this->type == NULL); 24746f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick 24756f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick if (state->current_function != NULL) { 24766f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick _mesa_glsl_error(& loc, state, 24776f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick "All uses of `invariant' keyword must be at global " 24786f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick "scope\n"); 24796f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick } 24806f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick 24816f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick foreach_list_typed (ast_declaration, decl, link, &this->declarations) { 24826f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick assert(!decl->is_array); 24836f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick assert(decl->array_size == NULL); 24846f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick assert(decl->initializer == NULL); 24856f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick 24866f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick ir_variable *const earlier = 24876f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick state->symbols->get_variable(decl->identifier); 24886f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick if (earlier == NULL) { 24896f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick _mesa_glsl_error(& loc, state, 24906f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick "Undeclared variable `%s' cannot be marked " 24916f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick "invariant\n", decl->identifier); 24926f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick } else if ((state->target == vertex_shader) 24936f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick && (earlier->mode != ir_var_out)) { 24946f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick _mesa_glsl_error(& loc, state, 24956f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick "`%s' cannot be marked invariant, vertex shader " 24966f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick "outputs only\n", decl->identifier); 24976f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick } else if ((state->target == fragment_shader) 24986f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick && (earlier->mode != ir_var_in)) { 24996f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick _mesa_glsl_error(& loc, state, 25006f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick "`%s' cannot be marked invariant, fragment shader " 25016f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick "inputs only\n", decl->identifier); 2502bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick } else if (earlier->used) { 2503bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick _mesa_glsl_error(& loc, state, 2504bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick "variable `%s' may not be redeclared " 2505bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick "`invariant' after being used", 2506bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick earlier->name); 25076f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick } else { 25086f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick earlier->invariant = true; 25096f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick } 25106f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick } 25116f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick 25126f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick /* Invariant redeclarations do not have r-values. 25136f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick */ 25146f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick return NULL; 25156f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick } 25166f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick 25176f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick assert(this->type != NULL); 25186f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick assert(!this->invariant); 25196f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick 25203455ce614424a5a23a23037e23d0454e476bceeaIan Romanick /* The type specifier may contain a structure definition. Process that 25213455ce614424a5a23a23037e23d0454e476bceeaIan Romanick * before any of the variable declarations. 25223455ce614424a5a23a23037e23d0454e476bceeaIan Romanick */ 25233455ce614424a5a23a23037e23d0454e476bceeaIan Romanick (void) this->type->specifier->hir(instructions, state); 25243455ce614424a5a23a23037e23d0454e476bceeaIan Romanick 2525d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick decl_type = this->type->specifier->glsl_type(& type_name, state); 2526304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick if (this->declarations.is_empty()) { 2527f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick /* If there is no structure involved in the program text, there are two 2528f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick * possible scenarios: 2529f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick * 2530f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick * - The program text contained something like 'vec4;'. This is an 2531f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick * empty declaration. It is valid but weird. Emit a warning. 2532f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick * 2533f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick * - The program text contained something like 'S;' and 'S' is not the 2534f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick * name of a known structure type. This is both invalid and weird. 2535f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick * Emit an error. 2536f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick * 2537f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick * Note that if decl_type is NULL and there is a structure involved, 2538f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick * there must have been some sort of error with the structure. In this 2539f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick * case we assume that an error was already generated on this line of 2540f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick * code for the structure. There is no need to generate an additional, 2541f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick * confusing error. 2542f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick */ 2543f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick assert(this->type->specifier->structure == NULL || decl_type != NULL 2544f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick || state->error); 2545f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick if (this->type->specifier->structure == NULL) { 2546f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick if (decl_type != NULL) { 2547547212d963c70161915c46d64e8020617199fb8dChia-I Wu _mesa_glsl_warning(&loc, state, "empty declaration"); 2548f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick } else { 2549f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick _mesa_glsl_error(&loc, state, 2550f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick "invalid type `%s' in empty declaration", 2551f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick type_name); 2552547212d963c70161915c46d64e8020617199fb8dChia-I Wu } 2553c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick } 2554c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick } 2555a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 25562b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick foreach_list_typed (ast_declaration, decl, link, &this->declarations) { 2557a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick const struct glsl_type *var_type; 2558768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick ir_variable *var; 2559a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 2560a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* FINISHME: Emit a warning if a variable declaration shadows a 2561a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * FINISHME: declaration at a higher scope. 2562a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 2563a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 2564cec65a6b76290ee4da91691bd3ef01c3fb8a0c37Ian Romanick if ((decl_type == NULL) || decl_type->is_void()) { 2565a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick if (type_name != NULL) { 2566a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick _mesa_glsl_error(& loc, state, 2567a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick "invalid type `%s' in declaration of `%s'", 2568a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick type_name, decl->identifier); 2569a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } else { 2570a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick _mesa_glsl_error(& loc, state, 2571a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick "invalid type in declaration of `%s'", 2572a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick decl->identifier); 2573a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 2574a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick continue; 2575a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 2576a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 2577a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick if (decl->is_array) { 2578d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke var_type = process_array_type(&loc, decl_type, decl->array_size, 2579d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke state); 2580a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick if (var_type->is_error()) 2581a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick continue; 2582a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } else { 2583a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick var_type = decl_type; 2584a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 2585a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 25867e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick var = new(ctx) ir_variable(var_type, decl->identifier, ir_var_auto); 2587a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 25883f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification; 25893f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt * 25903f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt * "Global variables can only use the qualifiers const, 25913f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt * attribute, uni form, or varying. Only one may be 25923f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt * specified. 25933f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt * 25943f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt * Local variables can only use the qualifier const." 25953f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt * 259682c4b4f88af97395a3d1b01e1998ec828cd5d305Ian Romanick * This is relaxed in GLSL 1.30. It is also relaxed by any extension 259782c4b4f88af97395a3d1b01e1998ec828cd5d305Ian Romanick * that adds the 'layout' keyword. 25983f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt */ 259982c4b4f88af97395a3d1b01e1998ec828cd5d305Ian Romanick if ((state->language_version < 130) 260082c4b4f88af97395a3d1b01e1998ec828cd5d305Ian Romanick && !state->ARB_explicit_attrib_location_enable 260182c4b4f88af97395a3d1b01e1998ec828cd5d305Ian Romanick && !state->ARB_fragment_coord_conventions_enable) { 2602e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick if (this->type->qualifier.flags.q.out) { 26033f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt _mesa_glsl_error(& loc, state, 26043f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt "`out' qualifier in declaration of `%s' " 2605469ea695bbbc984ebec26b2413ab70d450a283caIan Romanick "only valid for function parameters in %s.", 2606469ea695bbbc984ebec26b2413ab70d450a283caIan Romanick decl->identifier, state->version_string); 26073f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt } 2608e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick if (this->type->qualifier.flags.q.in) { 26093f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt _mesa_glsl_error(& loc, state, 26103f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt "`in' qualifier in declaration of `%s' " 2611469ea695bbbc984ebec26b2413ab70d450a283caIan Romanick "only valid for function parameters in %s.", 2612469ea695bbbc984ebec26b2413ab70d450a283caIan Romanick decl->identifier, state->version_string); 26133f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt } 26143f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt /* FINISHME: Test for other invalid qualifiers. */ 26153f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt } 26163f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt 26172e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt apply_type_qualifier_to_variable(& this->type->qualifier, var, state, 2618f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt & loc, this->ubo_qualifiers_valid); 2619a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 2620e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick if (this->type->qualifier.flags.q.invariant) { 2621046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt if ((state->target == vertex_shader) && !(var->mode == ir_var_out || 2622046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt var->mode == ir_var_inout)) { 2623046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt /* FINISHME: Note that this doesn't work for invariant on 2624046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt * a function signature outval 2625046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt */ 26266f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick _mesa_glsl_error(& loc, state, 26276f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick "`%s' cannot be marked invariant, vertex shader " 26286f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick "outputs only\n", var->name); 2629046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt } else if ((state->target == fragment_shader) && 2630046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt !(var->mode == ir_var_in || var->mode == ir_var_inout)) { 2631046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt /* FINISHME: Note that this doesn't work for invariant on 2632046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt * a function signature inval 2633046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt */ 26346f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick _mesa_glsl_error(& loc, state, 26356f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick "`%s' cannot be marked invariant, fragment shader " 26366f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick "inputs only\n", var->name); 26376f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick } 26386f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick } 26396f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick 2640e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick if (state->current_function != NULL) { 2641b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick const char *mode = NULL; 2642e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick const char *extra = ""; 2643b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick 2644e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick /* There is no need to check for 'inout' here because the parser will 2645e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick * only allow that in function parameter lists. 2646e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick */ 2647e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick if (this->type->qualifier.flags.q.attribute) { 2648b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick mode = "attribute"; 2649e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick } else if (this->type->qualifier.flags.q.uniform) { 2650b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick mode = "uniform"; 2651e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick } else if (this->type->qualifier.flags.q.varying) { 2652b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick mode = "varying"; 2653e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick } else if (this->type->qualifier.flags.q.in) { 2654e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick mode = "in"; 2655e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick extra = " or in function parameter list"; 2656e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick } else if (this->type->qualifier.flags.q.out) { 2657e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick mode = "out"; 2658e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick extra = " or in function parameter list"; 2659b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick } 2660b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick 2661b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick if (mode) { 2662e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick _mesa_glsl_error(& loc, state, 2663b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick "%s variable `%s' must be declared at " 2664e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick "global scope%s", 2665e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick mode, var->name, extra); 2666e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick } 2667e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick } else if (var->mode == ir_var_in) { 266801a584d09350d2c726312e2c9e88c5dbc54bdb70Chad Versace var->read_only = true; 266901a584d09350d2c726312e2c9e88c5dbc54bdb70Chad Versace 2670fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick if (state->target == vertex_shader) { 2671fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick bool error_emitted = false; 2672fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick 2673fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec: 2674fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick * 2675fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick * "Vertex shader inputs can only be float, floating-point 2676fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick * vectors, matrices, signed and unsigned integers and integer 2677fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick * vectors. Vertex shader inputs can also form arrays of these 2678fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick * types, but not structures." 2679fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick * 26802d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec: 26812d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick * 26822d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick * "Vertex shader inputs can only be float, floating-point 26832d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick * vectors, matrices, signed and unsigned integers and integer 26842d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick * vectors. They cannot be arrays or structures." 26852d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick * 2686fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec: 2687fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick * 2688fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick * "The attribute qualifier can be used only with float, 2689fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick * floating-point vectors, and matrices. Attribute variables 2690fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick * cannot be declared as arrays or structures." 2691fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick */ 2692fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick const glsl_type *check_type = var->type->is_array() 2693fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick ? var->type->fields.array : var->type; 2694fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick 2695fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick switch (check_type->base_type) { 2696fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick case GLSL_TYPE_FLOAT: 2697fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick break; 2698fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick case GLSL_TYPE_UINT: 2699fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick case GLSL_TYPE_INT: 2700fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick if (state->language_version > 120) 2701fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick break; 2702fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick /* FALLTHROUGH */ 2703fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick default: 2704fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick _mesa_glsl_error(& loc, state, 2705fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick "vertex shader input / attribute cannot have " 2706fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick "type %s`%s'", 2707fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick var->type->is_array() ? "array of " : "", 2708fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick check_type->name); 2709fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick error_emitted = true; 2710fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick } 2711fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick 27122d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick if (!error_emitted && (state->language_version <= 130) 2713fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick && var->type->is_array()) { 2714fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick _mesa_glsl_error(& loc, state, 2715fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick "vertex shader input / attribute cannot have " 2716fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick "array type"); 2717fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick error_emitted = true; 2718fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick } 2719fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick } 2720fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick } 2721fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick 272268d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace /* Integer vertex outputs must be qualified with 'flat'. 272368d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace * 272468d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace * From section 4.3.6 of the GLSL 1.30 spec: 272568d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace * "If a vertex output is a signed or unsigned integer or integer 272668d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace * vector, then it must be qualified with the interpolation qualifier 272768d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace * flat." 272868d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace */ 272968d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace if (state->language_version >= 130 273068d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace && state->target == vertex_shader 273168d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace && state->current_function == NULL 273268d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace && var->type->is_integer() 273368d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace && var->mode == ir_var_out 2734cf45949d6a896651a5f3864d3b195e26d59eee74Paul Berry && var->interpolation != INTERP_QUALIFIER_FLAT) { 273568d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace 273668d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace _mesa_glsl_error(&loc, state, "If a vertex output is an integer, " 273768d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace "then it must be qualified with 'flat'"); 273868d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace } 273968d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace 274068d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace 2741605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace /* Interpolation qualifiers cannot be applied to 'centroid' and 2742605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace * 'centroid varying'. 2743605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace * 2744605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec: 2745605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace * "interpolation qualifiers may only precede the qualifiers in, 2746605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace * centroid in, out, or centroid out in a declaration. They do not apply 2747605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace * to the deprecated storage qualifiers varying or centroid varying." 2748605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace */ 2749605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace if (state->language_version >= 130 2750605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace && this->type->qualifier.has_interpolation() 2751605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace && this->type->qualifier.flags.q.varying) { 2752605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace 2753605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace const char *i = this->type->qualifier.interpolation_string(); 2754605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace assert(i != NULL); 2755605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace const char *s; 2756605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace if (this->type->qualifier.flags.q.centroid) 2757605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace s = "centroid varying"; 2758605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace else 2759605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace s = "varying"; 2760605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace 2761605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace _mesa_glsl_error(&loc, state, 2762605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace "qualifier '%s' cannot be applied to the " 2763605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace "deprecated storage qualifier '%s'", i, s); 2764605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace } 2765605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace 2766605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace 27678faaa4a672c1062e486eda2525287715b554342dChad Versace /* Interpolation qualifiers can only apply to vertex shader outputs and 27688faaa4a672c1062e486eda2525287715b554342dChad Versace * fragment shader inputs. 27698faaa4a672c1062e486eda2525287715b554342dChad Versace * 27708faaa4a672c1062e486eda2525287715b554342dChad Versace * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec: 27718faaa4a672c1062e486eda2525287715b554342dChad Versace * "Outputs from a vertex shader (out) and inputs to a fragment 27728faaa4a672c1062e486eda2525287715b554342dChad Versace * shader (in) can be further qualified with one or more of these 27738faaa4a672c1062e486eda2525287715b554342dChad Versace * interpolation qualifiers" 27748faaa4a672c1062e486eda2525287715b554342dChad Versace */ 27758faaa4a672c1062e486eda2525287715b554342dChad Versace if (state->language_version >= 130 27768faaa4a672c1062e486eda2525287715b554342dChad Versace && this->type->qualifier.has_interpolation()) { 27778faaa4a672c1062e486eda2525287715b554342dChad Versace 27788faaa4a672c1062e486eda2525287715b554342dChad Versace const char *i = this->type->qualifier.interpolation_string(); 27798faaa4a672c1062e486eda2525287715b554342dChad Versace assert(i != NULL); 27808faaa4a672c1062e486eda2525287715b554342dChad Versace 27818faaa4a672c1062e486eda2525287715b554342dChad Versace switch (state->target) { 27828faaa4a672c1062e486eda2525287715b554342dChad Versace case vertex_shader: 27838faaa4a672c1062e486eda2525287715b554342dChad Versace if (this->type->qualifier.flags.q.in) { 27848faaa4a672c1062e486eda2525287715b554342dChad Versace _mesa_glsl_error(&loc, state, 27858faaa4a672c1062e486eda2525287715b554342dChad Versace "qualifier '%s' cannot be applied to vertex " 27868faaa4a672c1062e486eda2525287715b554342dChad Versace "shader inputs", i); 27878faaa4a672c1062e486eda2525287715b554342dChad Versace } 27888faaa4a672c1062e486eda2525287715b554342dChad Versace break; 27898faaa4a672c1062e486eda2525287715b554342dChad Versace case fragment_shader: 27908faaa4a672c1062e486eda2525287715b554342dChad Versace if (this->type->qualifier.flags.q.out) { 27918faaa4a672c1062e486eda2525287715b554342dChad Versace _mesa_glsl_error(&loc, state, 27928faaa4a672c1062e486eda2525287715b554342dChad Versace "qualifier '%s' cannot be applied to fragment " 27938faaa4a672c1062e486eda2525287715b554342dChad Versace "shader outputs", i); 27948faaa4a672c1062e486eda2525287715b554342dChad Versace } 27958faaa4a672c1062e486eda2525287715b554342dChad Versace break; 27968faaa4a672c1062e486eda2525287715b554342dChad Versace default: 27978faaa4a672c1062e486eda2525287715b554342dChad Versace assert(0); 27988faaa4a672c1062e486eda2525287715b554342dChad Versace } 27998faaa4a672c1062e486eda2525287715b554342dChad Versace } 28008faaa4a672c1062e486eda2525287715b554342dChad Versace 28018faaa4a672c1062e486eda2525287715b554342dChad Versace 28021eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace /* From section 4.3.4 of the GLSL 1.30 spec: 28031eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace * "It is an error to use centroid in in a vertex shader." 28041eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace */ 28051eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace if (state->language_version >= 130 28061eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace && this->type->qualifier.flags.q.centroid 28071eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace && this->type->qualifier.flags.q.in 28081eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace && state->target == vertex_shader) { 28091eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace 28101eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace _mesa_glsl_error(&loc, state, 28111eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace "'centroid in' cannot be used in a vertex shader"); 28121eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace } 28131eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace 28141eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace 2815889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30. 2816889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace */ 2817889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace if (this->type->specifier->precision != ast_precision_none 2818889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace && state->language_version != 100 2819889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace && state->language_version < 130) { 2820889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace 2821889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace _mesa_glsl_error(&loc, state, 2822889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace "precision qualifiers are supported only in GLSL ES " 2823889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace "1.00, and GLSL 1.30 and later"); 2824889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace } 2825889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace 2826889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace 282745e8e6c6b1b7f3bc00a578fa6809c9bc719c171aChad Versace /* Precision qualifiers only apply to floating point and integer types. 2828889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace * 2829889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace * From section 4.5.2 of the GLSL 1.30 spec: 2830889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace * "Any floating point or any integer declaration can have the type 2831889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace * preceded by one of these precision qualifiers [...] Literal 2832889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace * constants do not have precision qualifiers. Neither do Boolean 2833889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace * variables. 28348752824f27c979986ae855667337e89637b005fbKenneth Graunke * 28358752824f27c979986ae855667337e89637b005fbKenneth Graunke * In GLSL ES, sampler types are also allowed. 28368752824f27c979986ae855667337e89637b005fbKenneth Graunke * 28378752824f27c979986ae855667337e89637b005fbKenneth Graunke * From page 87 of the GLSL ES spec: 28388752824f27c979986ae855667337e89637b005fbKenneth Graunke * "RESOLUTION: Allow sampler types to take a precision qualifier." 2839889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace */ 2840889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace if (this->type->specifier->precision != ast_precision_none 284145e8e6c6b1b7f3bc00a578fa6809c9bc719c171aChad Versace && !var->type->is_float() 284245e8e6c6b1b7f3bc00a578fa6809c9bc719c171aChad Versace && !var->type->is_integer() 28438752824f27c979986ae855667337e89637b005fbKenneth Graunke && !(var->type->is_sampler() && state->es_shader) 284445e8e6c6b1b7f3bc00a578fa6809c9bc719c171aChad Versace && !(var->type->is_array() 284545e8e6c6b1b7f3bc00a578fa6809c9bc719c171aChad Versace && (var->type->fields.array->is_float() 284645e8e6c6b1b7f3bc00a578fa6809c9bc719c171aChad Versace || var->type->fields.array->is_integer()))) { 2847889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace 2848889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace _mesa_glsl_error(&loc, state, 28498752824f27c979986ae855667337e89637b005fbKenneth Graunke "precision qualifiers apply only to floating point" 28508752824f27c979986ae855667337e89637b005fbKenneth Graunke "%s types", state->es_shader ? ", integer, and sampler" 28518752824f27c979986ae855667337e89637b005fbKenneth Graunke : "and integer"); 2852889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace } 2853889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace 2854f07221056e1822187546b76387714b3172f9b2c5Paul Berry /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec: 2855f07221056e1822187546b76387714b3172f9b2c5Paul Berry * 2856f07221056e1822187546b76387714b3172f9b2c5Paul Berry * "[Sampler types] can only be declared as function 2857f07221056e1822187546b76387714b3172f9b2c5Paul Berry * parameters or uniform variables (see Section 4.3.5 2858f07221056e1822187546b76387714b3172f9b2c5Paul Berry * "Uniform")". 2859f07221056e1822187546b76387714b3172f9b2c5Paul Berry */ 2860f07221056e1822187546b76387714b3172f9b2c5Paul Berry if (var_type->contains_sampler() && 2861f07221056e1822187546b76387714b3172f9b2c5Paul Berry !this->type->qualifier.flags.q.uniform) { 2862f07221056e1822187546b76387714b3172f9b2c5Paul Berry _mesa_glsl_error(&loc, state, "samplers must be declared uniform"); 2863f07221056e1822187546b76387714b3172f9b2c5Paul Berry } 2864f07221056e1822187546b76387714b3172f9b2c5Paul Berry 2865e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick /* Process the initializer and add its instructions to a temporary 2866e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick * list. This list will be added to the instruction stream (below) after 2867e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick * the declaration is added. This is done because in some cases (such as 2868e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick * redeclarations) the declaration may not actually be added to the 2869e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick * instruction stream. 2870e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick */ 2871fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt exec_list initializer_instructions; 287209a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick ir_variable *earlier = get_variable_being_redeclared(var, decl, state); 287309a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick 287466faec4895b7bb59a614087a200c05157191b4aeIan Romanick if (decl->initializer != NULL) { 287509a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick result = process_initializer((earlier == NULL) ? var : earlier, 287609a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick decl, this->type, 28770292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick &initializer_instructions, state); 287866faec4895b7bb59a614087a200c05157191b4aeIan Romanick } 287917d86f4371da413176ba365ca26a58bac172d365Ian Romanick 28800ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec: 28810ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt * 28820ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt * "It is an error to write to a const variable outside of 28830ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt * its declaration, so they must be initialized when 28840ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt * declared." 28850ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt */ 2886e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick if (this->type->qualifier.flags.q.constant && decl->initializer == NULL) { 28870ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt _mesa_glsl_error(& loc, state, 288846f7105df487c91569f7e4a8da74d673c12e5619Chad Versace "const declaration of `%s' must be initialized", 288946f7105df487c91569f7e4a8da74d673c12e5619Chad Versace decl->identifier); 28900ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt } 28910ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt 289209a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick /* If the declaration is not a redeclaration, there are a few additional 289309a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick * semantic checks that must be applied. In addition, variable that was 289409a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick * created for the declaration should be added to the IR stream. 28955466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick */ 289609a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick if (earlier == NULL) { 289709a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec, 289809a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick * 289909a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick * "Identifiers starting with "gl_" are reserved for use by 290009a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick * OpenGL, and may not be declared in a shader as either a 290109a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick * variable or a function." 290209a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick */ 290309a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick if (strncmp(decl->identifier, "gl_", 3) == 0) 290409a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick _mesa_glsl_error(& loc, state, 290509a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick "identifier `%s' uses reserved `gl_' prefix", 290609a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick decl->identifier); 2907c475a54578bf5473c6c62bc5468ef4fe555164d7Jason Wood else if (strstr(decl->identifier, "__")) { 2908684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt /* From page 14 (page 20 of the PDF) of the GLSL 1.10 2909684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt * spec: 2910684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt * 2911684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt * "In addition, all identifiers containing two 2912684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt * consecutive underscores (__) are reserved as 2913684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt * possible future keywords." 2914684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt */ 2915684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt _mesa_glsl_error(& loc, state, 2916684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt "identifier `%s' uses reserved `__' string", 2917684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt decl->identifier); 2918684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt } 29195466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick 292009a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick /* Add the variable to the symbol table. Note that the initializer's 292109a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick * IR was already processed earlier (though it hasn't been emitted 292209a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick * yet), without the variable in scope. 292309a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick * 292409a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick * This differs from most C-like languages, but it follows the GLSL 292509a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50 292609a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick * spec: 292709a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick * 292809a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick * "Within a declaration, the scope of a name starts immediately 292909a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick * after the initializer if present or immediately after the name 293009a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick * being declared if not." 293109a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick */ 293209a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick if (!state->symbols->add_variable(var)) { 293309a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick YYLTYPE loc = this->get_location(); 293409a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick _mesa_glsl_error(&loc, state, "name `%s' already taken in the " 293509a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick "current scope", decl->identifier); 293609a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick continue; 293709a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick } 293809a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick 293909a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick /* Push the variable declaration to the top. It means that all the 294009a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick * variable declarations will appear in a funny last-to-first order, 294109a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick * but otherwise we run into trouble if a function is prototyped, a 294209a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick * global var is decled, then the function is defined with usage of 294309a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick * the global var. See glslparsertest's CorrectModule.frag. 294409a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick */ 294509a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick instructions->push_head(var); 29465d25746640ee27882b69a962459727cf924443dbKenneth Graunke } 29475d25746640ee27882b69a962459727cf924443dbKenneth Graunke 2948fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt instructions->append_list(&initializer_instructions); 2949a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 2950a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 29518558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt 29528558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt /* Generally, variable declarations do not have r-values. However, 29538558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt * one is used for the declaration in 29548558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt * 29558558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt * while (bool b = some_condition()) { 29568558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt * ... 29578558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt * } 29588558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt * 29598558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt * so we return the rvalue from the last seen declaration here. 2960a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 29618558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt return result; 2962a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick} 2963a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 2964a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 2965fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue * 29660044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_parameter_declarator::hir(exec_list *instructions, 296718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick struct _mesa_glsl_parse_state *state) 2968a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{ 2969953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke void *ctx = state; 2970a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick const struct glsl_type *type; 2971a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick const char *name = NULL; 29722e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt YYLTYPE loc = this->get_location(); 2973a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 2974d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick type = this->type->specifier->glsl_type(& name, state); 2975a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 2976a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick if (type == NULL) { 2977a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick if (name != NULL) { 2978a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick _mesa_glsl_error(& loc, state, 2979a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick "invalid type `%s' in declaration of `%s'", 298018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick name, this->identifier); 2981a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } else { 2982a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick _mesa_glsl_error(& loc, state, 2983a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick "invalid type in declaration of `%s'", 298418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick this->identifier); 2985a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 2986a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 29870471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick type = glsl_type::error_type; 2988a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 2989a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 2990068c80cfe0a280490353b6b007165d717c672eedEric Anholt /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec: 2991068c80cfe0a280490353b6b007165d717c672eedEric Anholt * 2992068c80cfe0a280490353b6b007165d717c672eedEric Anholt * "Functions that accept no input arguments need not use void in the 2993068c80cfe0a280490353b6b007165d717c672eedEric Anholt * argument list because prototypes (or definitions) are required and 2994068c80cfe0a280490353b6b007165d717c672eedEric Anholt * therefore there is no ambiguity when an empty argument list "( )" is 2995068c80cfe0a280490353b6b007165d717c672eedEric Anholt * declared. The idiom "(void)" as a parameter list is provided for 2996068c80cfe0a280490353b6b007165d717c672eedEric Anholt * convenience." 2997068c80cfe0a280490353b6b007165d717c672eedEric Anholt * 2998068c80cfe0a280490353b6b007165d717c672eedEric Anholt * Placing this check here prevents a void parameter being set up 2999068c80cfe0a280490353b6b007165d717c672eedEric Anholt * for a function, which avoids tripping up checks for main taking 3000068c80cfe0a280490353b6b007165d717c672eedEric Anholt * parameters and lookups of an unnamed symbol. 3001068c80cfe0a280490353b6b007165d717c672eedEric Anholt */ 3002cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick if (type->is_void()) { 3003cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick if (this->identifier != NULL) 3004cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick _mesa_glsl_error(& loc, state, 3005cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick "named parameter cannot have type `void'"); 3006cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick 3007cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick is_void = true; 3008068c80cfe0a280490353b6b007165d717c672eedEric Anholt return NULL; 3009cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick } 3010068c80cfe0a280490353b6b007165d717c672eedEric Anholt 301145d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick if (formal_parameter && (this->identifier == NULL)) { 301245d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick _mesa_glsl_error(& loc, state, "formal parameter lacks a name"); 301345d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick return NULL; 301445d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick } 301545d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick 3016e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...) 3017e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke * call already handled the "vec4[..] foo" case. 3018e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke */ 3019e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke if (this->is_array) { 3020d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke type = process_array_type(&loc, type, this->array_size, state); 3021e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke } 3022e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke 3023a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick if (!type->is_error() && type->array_size() == 0) { 3024e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke _mesa_glsl_error(&loc, state, "arrays passed as parameters must have " 3025e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke "a declared size."); 3026e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke type = glsl_type::error_type; 3027e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke } 3028e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke 3029cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick is_void = false; 30307e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick ir_variable *var = new(ctx) ir_variable(type, this->identifier, ir_var_in); 3031a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 3032cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick /* Apply any specified qualifiers to the parameter declaration. Note that 3033cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick * for function parameters the default mode is 'in'. 3034cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick */ 3035f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc, 3036f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt false); 3037a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 3038f07221056e1822187546b76387714b3172f9b2c5Paul Berry /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec: 3039f07221056e1822187546b76387714b3172f9b2c5Paul Berry * 3040f07221056e1822187546b76387714b3172f9b2c5Paul Berry * "Samplers cannot be treated as l-values; hence cannot be used 3041f07221056e1822187546b76387714b3172f9b2c5Paul Berry * as out or inout function parameters, nor can they be assigned 3042f07221056e1822187546b76387714b3172f9b2c5Paul Berry * into." 3043f07221056e1822187546b76387714b3172f9b2c5Paul Berry */ 3044f07221056e1822187546b76387714b3172f9b2c5Paul Berry if ((var->mode == ir_var_inout || var->mode == ir_var_out) 3045f07221056e1822187546b76387714b3172f9b2c5Paul Berry && type->contains_sampler()) { 3046f07221056e1822187546b76387714b3172f9b2c5Paul Berry _mesa_glsl_error(&loc, state, "out and inout parameters cannot contain samplers"); 3047f07221056e1822187546b76387714b3172f9b2c5Paul Berry type = glsl_type::error_type; 3048f07221056e1822187546b76387714b3172f9b2c5Paul Berry } 3049f07221056e1822187546b76387714b3172f9b2c5Paul Berry 305000792e3586746c833ffc9bb65712e38038916e06Paul Berry /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec: 305100792e3586746c833ffc9bb65712e38038916e06Paul Berry * 305200792e3586746c833ffc9bb65712e38038916e06Paul Berry * "When calling a function, expressions that do not evaluate to 305300792e3586746c833ffc9bb65712e38038916e06Paul Berry * l-values cannot be passed to parameters declared as out or inout." 305400792e3586746c833ffc9bb65712e38038916e06Paul Berry * 305500792e3586746c833ffc9bb65712e38038916e06Paul Berry * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec: 305600792e3586746c833ffc9bb65712e38038916e06Paul Berry * 305700792e3586746c833ffc9bb65712e38038916e06Paul Berry * "Other binary or unary expressions, non-dereferenced arrays, 305800792e3586746c833ffc9bb65712e38038916e06Paul Berry * function names, swizzles with repeated fields, and constants 305900792e3586746c833ffc9bb65712e38038916e06Paul Berry * cannot be l-values." 306000792e3586746c833ffc9bb65712e38038916e06Paul Berry * 306100792e3586746c833ffc9bb65712e38038916e06Paul Berry * So for GLSL 1.10, passing an array as an out or inout parameter is not 306200792e3586746c833ffc9bb65712e38038916e06Paul Berry * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES. 306300792e3586746c833ffc9bb65712e38038916e06Paul Berry */ 306400792e3586746c833ffc9bb65712e38038916e06Paul Berry if ((var->mode == ir_var_inout || var->mode == ir_var_out) 306500792e3586746c833ffc9bb65712e38038916e06Paul Berry && type->is_array() && state->language_version == 110) { 306600792e3586746c833ffc9bb65712e38038916e06Paul Berry _mesa_glsl_error(&loc, state, "Arrays cannot be out or inout parameters in GLSL 1.10"); 306700792e3586746c833ffc9bb65712e38038916e06Paul Berry type = glsl_type::error_type; 306800792e3586746c833ffc9bb65712e38038916e06Paul Berry } 306900792e3586746c833ffc9bb65712e38038916e06Paul Berry 30700044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick instructions->push_tail(var); 3071a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 3072a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* Parameter declarations do not have r-values. 3073a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 3074a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick return NULL; 3075a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick} 3076a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 3077a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 307845d8a70c12ee6ea956baaf898324a828496382f6Ian Romanickvoid 3079304ea90233baeac6801a98e981658cb7a2d2501cIan Romanickast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters, 308045d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick bool formal, 308145d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick exec_list *ir_parameters, 308245d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick _mesa_glsl_parse_state *state) 3083a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{ 3084cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick ast_parameter_declarator *void_param = NULL; 3085cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick unsigned count = 0; 3086a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 30872b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) { 308845d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick param->formal_parameter = formal; 3089068c80cfe0a280490353b6b007165d717c672eedEric Anholt param->hir(ir_parameters, state); 3090cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick 3091cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick if (param->is_void) 3092cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick void_param = param; 3093cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick 3094cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick count++; 3095cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick } 3096cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick 3097cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick if ((void_param != NULL) && (count > 1)) { 3098cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick YYLTYPE loc = void_param->get_location(); 3099cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick 3100cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick _mesa_glsl_error(& loc, state, 3101cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick "`void' parameter must be only parameter"); 3102a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 3103a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick} 3104a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 3105a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 31066fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunkevoid 31070d81b0e18494a80c4326fbc98837842959675869Paul Berryemit_function(_mesa_glsl_parse_state *state, ir_function *f) 31086fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke{ 31090d81b0e18494a80c4326fbc98837842959675869Paul Berry /* IR invariants disallow function declarations or definitions 31100d81b0e18494a80c4326fbc98837842959675869Paul Berry * nested within other function definitions. But there is no 31110d81b0e18494a80c4326fbc98837842959675869Paul Berry * requirement about the relative order of function declarations 31120d81b0e18494a80c4326fbc98837842959675869Paul Berry * and definitions with respect to one another. So simply insert 31130d81b0e18494a80c4326fbc98837842959675869Paul Berry * the new ir_function block at the end of the toplevel instruction 31140d81b0e18494a80c4326fbc98837842959675869Paul Berry * list. 31150d81b0e18494a80c4326fbc98837842959675869Paul Berry */ 31160d81b0e18494a80c4326fbc98837842959675869Paul Berry state->toplevel_ir->push_tail(f); 31176fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke} 31186fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke 31196fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke 3120fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue * 312192318a947958892497722772b03c643ebc943294Ian Romanickast_function::hir(exec_list *instructions, 312292318a947958892497722772b03c643ebc943294Ian Romanick struct _mesa_glsl_parse_state *state) 3123a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{ 3124953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke void *ctx = state; 312518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick ir_function *f = NULL; 312692318a947958892497722772b03c643ebc943294Ian Romanick ir_function_signature *sig = NULL; 312792318a947958892497722772b03c643ebc943294Ian Romanick exec_list hir_parameters; 3128a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 3129ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke const char *const name = identifier; 3130a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 31319a3bd5e0452c9c791ba94155d3c9ddba42abd114Ian Romanick /* New functions are always added to the top-level IR instruction stream, 31329a3bd5e0452c9c791ba94155d3c9ddba42abd114Ian Romanick * so this instruction list pointer is ignored. See also emit_function 31339a3bd5e0452c9c791ba94155d3c9ddba42abd114Ian Romanick * (called below). 31349a3bd5e0452c9c791ba94155d3c9ddba42abd114Ian Romanick */ 31359a3bd5e0452c9c791ba94155d3c9ddba42abd114Ian Romanick (void) instructions; 31369a3bd5e0452c9c791ba94155d3c9ddba42abd114Ian Romanick 313763b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec, 313863b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick * 313963b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick * "Function declarations (prototypes) cannot occur inside of functions; 314063b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick * they must be at global scope, or for the built-in functions, outside 314163b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick * the global scope." 314263b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick * 314363b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec, 314463b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick * 314563b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick * "User defined functions may only be defined within the global scope." 314663b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick * 314763b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick * Note that this language does not appear in GLSL 1.10. 314863b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick */ 314963b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick if ((state->current_function != NULL) && (state->language_version != 110)) { 315063b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick YYLTYPE loc = this->get_location(); 315163b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick _mesa_glsl_error(&loc, state, 315263b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick "declaration of function `%s' not allowed within " 315363b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick "function body", name); 315463b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick } 315563b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick 3156edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec, 3157edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke * 3158edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke * "Identifiers starting with "gl_" are reserved for use by 3159edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke * OpenGL, and may not be declared in a shader as either a 3160edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke * variable or a function." 3161edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke */ 3162edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke if (strncmp(name, "gl_", 3) == 0) { 3163edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke YYLTYPE loc = this->get_location(); 3164edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke _mesa_glsl_error(&loc, state, 3165edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke "identifier `%s' uses reserved `gl_' prefix", name); 3166edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke } 3167edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke 3168a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* Convert the list of function parameters to HIR now so that they can be 3169a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * used below to compare this function's signature with previously seen 3170a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * signatures for functions with the same name. 3171a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 317245d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick ast_parameter_declarator::parameters_to_hir(& this->parameters, 317345d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick is_definition, 317445d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick & hir_parameters, state); 3175a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 3176e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick const char *return_type_name; 3177e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick const glsl_type *return_type = 317892318a947958892497722772b03c643ebc943294Ian Romanick this->return_type->specifier->glsl_type(& return_type_name, state); 3179e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick 318076e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt if (!return_type) { 318176e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt YYLTYPE loc = this->get_location(); 318276e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt _mesa_glsl_error(&loc, state, 318376e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt "function `%s' has undeclared return type `%s'", 318476e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt name, return_type_name); 318576e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt return_type = glsl_type::error_type; 318676e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt } 3187e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick 3188ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec: 3189ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke * "No qualifier is allowed on the return type of a function." 3190ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke */ 3191ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke if (this->return_type->has_qualifiers()) { 3192ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke YYLTYPE loc = this->get_location(); 3193ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke _mesa_glsl_error(& loc, state, 3194ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke "function `%s' return type has qualifiers", name); 3195ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke } 3196ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke 3197f07221056e1822187546b76387714b3172f9b2c5Paul Berry /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec: 3198f07221056e1822187546b76387714b3172f9b2c5Paul Berry * 3199f07221056e1822187546b76387714b3172f9b2c5Paul Berry * "[Sampler types] can only be declared as function parameters 3200f07221056e1822187546b76387714b3172f9b2c5Paul Berry * or uniform variables (see Section 4.3.5 "Uniform")". 3201f07221056e1822187546b76387714b3172f9b2c5Paul Berry */ 3202f07221056e1822187546b76387714b3172f9b2c5Paul Berry if (return_type->contains_sampler()) { 3203f07221056e1822187546b76387714b3172f9b2c5Paul Berry YYLTYPE loc = this->get_location(); 3204f07221056e1822187546b76387714b3172f9b2c5Paul Berry _mesa_glsl_error(&loc, state, 3205f07221056e1822187546b76387714b3172f9b2c5Paul Berry "function `%s' return type can't contain a sampler", 3206f07221056e1822187546b76387714b3172f9b2c5Paul Berry name); 3207f07221056e1822187546b76387714b3172f9b2c5Paul Berry } 3208f07221056e1822187546b76387714b3172f9b2c5Paul Berry 3209a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* Verify that this function's signature either doesn't match a previously 3210a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * seen signature for a function with the same name, or, if a match is found, 3211a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * that the previously seen signature does not have an associated definition. 3212a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 3213e466b182bbf21f62fe6542091f4af3275555db80Ian Romanick f = state->symbols->get_function(name); 321481f03393982c29f8f4165b5629c8e8fb708b97a3Kenneth Graunke if (f != NULL && (state->es_shader || f->has_user_signature())) { 3215202604e8160157e4e80b3458175e0170d168e557Ian Romanick sig = f->exact_matching_signature(&hir_parameters); 32160d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke if (sig != NULL) { 32170d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke const char *badvar = sig->qualifiers_match(&hir_parameters); 32180d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke if (badvar != NULL) { 32190d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke YYLTYPE loc = this->get_location(); 3220abd40b15210c17b2a3ba8fcffc868fda203efa01Kenneth Graunke 32210d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' " 32220d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke "qualifiers don't match prototype", name, badvar); 32230d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke } 32241e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt 32250d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke if (sig->return_type != return_type) { 32260d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke YYLTYPE loc = this->get_location(); 322760be7626b829af7e1d07330b9a88468924ba350eEric Anholt 32280d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke _mesa_glsl_error(&loc, state, "function `%s' return type doesn't " 32290d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke "match prototype", name); 32300d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke } 3231a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 32320d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke if (is_definition && sig->is_defined) { 32330d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke YYLTYPE loc = this->get_location(); 3234a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 32350d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke _mesa_glsl_error(& loc, state, "function `%s' redefined", name); 3236a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 3237a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 3238a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } else { 32391660a2954797e056caba319c5d6c70b0d4be22feCarl Worth f = new(ctx) ir_function(name); 3240e8f5ebf313da3ce33ccbbcf9b72946853035fbddEric Anholt if (!state->symbols->add_function(f)) { 3241e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke /* This function name shadows a non-function use of the same name. */ 3242e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke YYLTYPE loc = this->get_location(); 3243e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke 3244e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke _mesa_glsl_error(&loc, state, "function name `%s' conflicts with " 3245e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke "non-function", name); 3246e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke return NULL; 3247e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke } 32489fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke 32490d81b0e18494a80c4326fbc98837842959675869Paul Berry emit_function(state, f); 3250a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 3251a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 3252ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt /* Verify the return type of main() */ 3253ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt if (strcmp(name, "main") == 0) { 325425711a85c22bed305c9b52b89feb9c600d1892dfIan Romanick if (! return_type->is_void()) { 3255ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt YYLTYPE loc = this->get_location(); 3256ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt 3257ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt _mesa_glsl_error(& loc, state, "main() must return void"); 3258ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt } 3259174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt 326092318a947958892497722772b03c643ebc943294Ian Romanick if (!hir_parameters.is_empty()) { 3261174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt YYLTYPE loc = this->get_location(); 3262174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt 3263174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt _mesa_glsl_error(& loc, state, "main() must not take any parameters"); 3264174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt } 3265ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt } 3266a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 3267a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* Finish storing the information about this new function in its signature. 3268a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 326992318a947958892497722772b03c643ebc943294Ian Romanick if (sig == NULL) { 32701660a2954797e056caba319c5d6c70b0d4be22feCarl Worth sig = new(ctx) ir_function_signature(return_type); 327192318a947958892497722772b03c643ebc943294Ian Romanick f->add_signature(sig); 3272a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 3273a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 3274bff6013d469b3d4e54cdc5731801c56994a523ecKenneth Graunke sig->replace_parameters(&hir_parameters); 327592318a947958892497722772b03c643ebc943294Ian Romanick signature = sig; 327692318a947958892497722772b03c643ebc943294Ian Romanick 327792318a947958892497722772b03c643ebc943294Ian Romanick /* Function declarations (prototypes) do not have r-values. 327892318a947958892497722772b03c643ebc943294Ian Romanick */ 327992318a947958892497722772b03c643ebc943294Ian Romanick return NULL; 328092318a947958892497722772b03c643ebc943294Ian Romanick} 328192318a947958892497722772b03c643ebc943294Ian Romanick 328292318a947958892497722772b03c643ebc943294Ian Romanick 328392318a947958892497722772b03c643ebc943294Ian Romanickir_rvalue * 328492318a947958892497722772b03c643ebc943294Ian Romanickast_function_definition::hir(exec_list *instructions, 328592318a947958892497722772b03c643ebc943294Ian Romanick struct _mesa_glsl_parse_state *state) 328692318a947958892497722772b03c643ebc943294Ian Romanick{ 328792318a947958892497722772b03c643ebc943294Ian Romanick prototype->is_definition = true; 328892318a947958892497722772b03c643ebc943294Ian Romanick prototype->hir(instructions, state); 3289e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick 329092318a947958892497722772b03c643ebc943294Ian Romanick ir_function_signature *signature = prototype->signature; 3291826a39cb14244820e8539a2328bb52447348f184Kenneth Graunke if (signature == NULL) 3292826a39cb14244820e8539a2328bb52447348f184Kenneth Graunke return NULL; 3293a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 329441ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick assert(state->current_function == NULL); 329541ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick state->current_function = signature; 32966de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke state->found_return = false; 329741ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick 3298e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick /* Duplicate parameters declared in the prototype as concrete variables. 3299e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick * Add these to the symbol table. 3300a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 33018bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick state->symbols->push_scope(); 3302e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick foreach_iter(exec_list_iterator, iter, signature->parameters) { 3303fbc7c0b8f2e161bce1c048c63d2d5cfcdeb096f1Eric Anholt ir_variable *const var = ((ir_instruction *) iter.get())->as_variable(); 3304e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick 3305fbc7c0b8f2e161bce1c048c63d2d5cfcdeb096f1Eric Anholt assert(var != NULL); 3306a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 33073359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick /* The only way a parameter would "exist" is if two parameters have 33083359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick * the same name. 33093359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick */ 33103359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick if (state->symbols->name_declared_this_scope(var->name)) { 33113359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick YYLTYPE loc = this->get_location(); 33123359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick 33133359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name); 33143359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick } else { 3315001eee52d461233b1e1d6ed3577965e9bcb209e8Eric Anholt state->symbols->add_variable(var); 33163359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick } 3317a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick } 3318a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 33199fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke /* Convert the body of the function to HIR. */ 3320894ea972a4defdaafeaa3a248c113b06c7ae0c7eEric Anholt this->body->hir(&signature->body, state); 33219fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke signature->is_defined = true; 3322a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 33238bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick state->symbols->pop_scope(); 3324a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 332541ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick assert(state->current_function == signature); 332641ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick state->current_function = NULL; 3327a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick 33286de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke if (!signature->return_type->is_void() && !state->found_return) { 33296de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke YYLTYPE loc = this->get_location(); 33306de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke _mesa_glsl_error(& loc, state, "function `%s' has non-void return type " 33316de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke "%s, but no return statement", 33326de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke signature->function_name(), 33336de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke signature->return_type->name); 33346de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke } 33356de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke 3336a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick /* Function definitions do not have r-values. 3337a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */ 3338a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick return NULL; 3339a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick} 334016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick 334116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick 3342fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue * 334316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanickast_jump_statement::hir(exec_list *instructions, 334416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick struct _mesa_glsl_parse_state *state) 334516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick{ 3346953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke void *ctx = state; 334716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick 3348c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick switch (mode) { 3349c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick case ast_return: { 335016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick ir_return *inst; 3351aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt assert(state->current_function); 335216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick 335316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick if (opt_return_value) { 3354b4cdba687c098eea2ecc61349a4ea02a8769909eChad Versace ir_rvalue *const ret = opt_return_value->hir(instructions, state); 33552db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick 33562db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick /* The value of the return type can be NULL if the shader says 33572db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick * 'return foo();' and foo() is a function that returns void. 33582db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick * 33592db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick * NOTE: The GLSL spec doesn't say that this is an error. The type 33602db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick * of the return value is void. If the return type of the function is 33612db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick * also void, then this should compile without error. Seriously. 33622db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick */ 33632db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick const glsl_type *const ret_type = 33642db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick (ret == NULL) ? glsl_type::void_type : ret->type; 336516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick 336618707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke /* Implicit conversions are not allowed for return values. */ 33672db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick if (state->current_function->return_type != ret_type) { 336818707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke YYLTYPE loc = this->get_location(); 336918707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke 337018707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke _mesa_glsl_error(& loc, state, 337118707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke "`return' with wrong type %s, in function `%s' " 337218707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke "returning %s", 33732db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick ret_type->name, 337418707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke state->current_function->function_name(), 337518707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke state->current_function->return_type->name); 337618707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke } 337716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick 33781660a2954797e056caba319c5d6c70b0d4be22feCarl Worth inst = new(ctx) ir_return(ret); 337916a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick } else { 3380aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt if (state->current_function->return_type->base_type != 3381aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt GLSL_TYPE_VOID) { 3382aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt YYLTYPE loc = this->get_location(); 3383aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt 3384aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt _mesa_glsl_error(& loc, state, 3385aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt "`return' with no value, in function %s returning " 3386aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt "non-void", 3387f96c52ba2e290e3ba5f14cd7f87ba5b4382a1785Kenneth Graunke state->current_function->function_name()); 3388aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt } 33891660a2954797e056caba319c5d6c70b0d4be22feCarl Worth inst = new(ctx) ir_return; 339016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick } 339116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick 33926de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke state->found_return = true; 339316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick instructions->push_tail(inst); 3394c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick break; 339516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick } 339616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick 3397c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick case ast_discard: 3398b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt if (state->target != fragment_shader) { 3399b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt YYLTYPE loc = this->get_location(); 3400b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt 3401b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt _mesa_glsl_error(& loc, state, 3402b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt "`discard' may only appear in a fragment shader"); 3403b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt } 340477049a702ad54e09c4102fe8c964e069944f83e5Kenneth Graunke instructions->push_tail(new(ctx) ir_discard); 3405c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick break; 3406c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick 3407c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick case ast_break: 3408c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick case ast_continue: 34095c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe if (mode == ast_continue && 34105c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe state->loop_nesting_ast == NULL) { 34114cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick YYLTYPE loc = this->get_location(); 34124cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick 34134cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick _mesa_glsl_error(& loc, state, 34145c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe "continue may only appear in a loop"); 34155c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe } else if (mode == ast_break && 34165c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe state->loop_nesting_ast == NULL && 341722d81f154fed9e004cca91807808ae3b81b01cedEric Anholt state->switch_state.switch_nesting_ast == NULL) { 34185c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe YYLTYPE loc = this->get_location(); 34194cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick 34205c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe _mesa_glsl_error(& loc, state, 34215c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe "break may only appear in a loop or a switch"); 34225c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe } else { 34235c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe /* For a loop, inline the for loop expression again, 34245c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe * since we don't know where near the end of 34255c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe * the loop body the normal copy of it 34262d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt * is going to be placed. 34272d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt */ 34285c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe if (state->loop_nesting_ast != NULL && 34295c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe mode == ast_continue && 34305c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe state->loop_nesting_ast->rest_expression) { 34315c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe state->loop_nesting_ast->rest_expression->hir(instructions, 34325c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe state); 34332d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt } 34342d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt 343522d81f154fed9e004cca91807808ae3b81b01cedEric Anholt if (state->switch_state.is_switch_innermost && 34365c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe mode == ast_break) { 34375c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe /* Force break out of switch by setting is_break switch state. 34385c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe */ 343922d81f154fed9e004cca91807808ae3b81b01cedEric Anholt ir_variable *const is_break_var = state->switch_state.is_break_var; 34405c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe ir_dereference_variable *const deref_is_break_var = 34415c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe new(ctx) ir_dereference_variable(is_break_var); 34425c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe ir_constant *const true_val = new(ctx) ir_constant(true); 34435c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe ir_assignment *const set_break_var = 3444aa5ec137757323b424d0f2638c50c93b9b06ffffEric Anholt new(ctx) ir_assignment(deref_is_break_var, true_val); 34455c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe 34465c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe instructions->push_tail(set_break_var); 34475c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe } 34485c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe else { 34495c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe ir_loop_jump *const jump = 34501660a2954797e056caba319c5d6c70b0d4be22feCarl Worth new(ctx) ir_loop_jump((mode == ast_break) 34511660a2954797e056caba319c5d6c70b0d4be22feCarl Worth ? ir_loop_jump::jump_break 34521660a2954797e056caba319c5d6c70b0d4be22feCarl Worth : ir_loop_jump::jump_continue); 34534cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick instructions->push_tail(jump); 34544cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick } 34554cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick } 34564cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick 3457c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick break; 3458b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt } 3459b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt 346016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick /* Jump instructions do not have r-values. 346116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick */ 346216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick return NULL; 346316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick} 34643c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick 34653c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick 34663c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanickir_rvalue * 34673c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanickast_selection_statement::hir(exec_list *instructions, 34683c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick struct _mesa_glsl_parse_state *state) 34693c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick{ 3470953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke void *ctx = state; 34711660a2954797e056caba319c5d6c70b0d4be22feCarl Worth 34723c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick ir_rvalue *const condition = this->condition->hir(instructions, state); 34733c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick 34743c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec: 34753c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick * 34763c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick * "Any expression whose type evaluates to a Boolean can be used as the 34773c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick * conditional expression bool-expression. Vector types are not accepted 34783c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick * as the expression to if." 34793c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick * 34803c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick * The checks are separated so that higher quality diagnostics can be 34813c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick * generated for cases where both rules are violated. 34823c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick */ 34833c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick if (!condition->type->is_boolean() || !condition->type->is_scalar()) { 34843c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick YYLTYPE loc = this->condition->get_location(); 34853c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick 34863c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick _mesa_glsl_error(& loc, state, "if-statement condition must be scalar " 34873c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick "boolean"); 34883c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick } 34893c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick 34901660a2954797e056caba319c5d6c70b0d4be22feCarl Worth ir_if *const stmt = new(ctx) ir_if(condition); 34913c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick 3492665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke if (then_statement != NULL) { 3493665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke state->symbols->push_scope(); 34944f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick then_statement->hir(& stmt->then_instructions, state); 3495665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke state->symbols->pop_scope(); 3496665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke } 34973c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick 3498665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke if (else_statement != NULL) { 3499665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke state->symbols->push_scope(); 35004f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick else_statement->hir(& stmt->else_instructions, state); 3501665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke state->symbols->pop_scope(); 3502665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke } 35033c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick 35043c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick instructions->push_tail(stmt); 35053c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick 35063c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick /* if-statements do not have r-values. 35073c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick */ 35083c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick return NULL; 35093c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick} 35109e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick 35119e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick 351285beb39e14556cf02f58116fd287120cd1defbd5Dan McCabeir_rvalue * 351385beb39e14556cf02f58116fd287120cd1defbd5Dan McCabeast_switch_statement::hir(exec_list *instructions, 351485beb39e14556cf02f58116fd287120cd1defbd5Dan McCabe struct _mesa_glsl_parse_state *state) 351585beb39e14556cf02f58116fd287120cd1defbd5Dan McCabe{ 35165c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe void *ctx = state; 35175c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe 35185c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe ir_rvalue *const test_expression = 35195c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe this->test_expression->hir(instructions, state); 35205c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe 35215c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec: 35225c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe * 35235c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe * "The type of init-expression in a switch statement must be a 35245c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe * scalar integer." 35255c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe */ 3526bbbc7c7d566905920967f56648fc26abcb37f4a1Eric Anholt if (!test_expression->type->is_scalar() || 3527bbbc7c7d566905920967f56648fc26abcb37f4a1Eric Anholt !test_expression->type->is_integer()) { 35285c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe YYLTYPE loc = this->test_expression->get_location(); 35295c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe 35305c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe _mesa_glsl_error(& loc, 35315c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe state, 35325c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe "switch-statement expression must be scalar " 35335c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe "integer"); 35345c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe } 35355c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe 35365c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe /* Track the switch-statement nesting in a stack-like manner. 35375c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe */ 353822d81f154fed9e004cca91807808ae3b81b01cedEric Anholt struct glsl_switch_state saved = state->switch_state; 35395c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe 354022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt state->switch_state.is_switch_innermost = true; 354122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt state->switch_state.switch_nesting_ast = this; 3542140632190cf41e6a035ca199b181091d4ed46986Eric Anholt state->switch_state.labels_ht = hash_table_ctor(0, hash_table_pointer_hash, 3543140632190cf41e6a035ca199b181091d4ed46986Eric Anholt hash_table_pointer_compare); 354457e44371a5b6aa8122b6a482ed6bd33e797ea1d2Eric Anholt state->switch_state.previous_default = NULL; 35455c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe 35465c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe /* Initalize is_fallthru state to false. 35475c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe */ 35485c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe ir_rvalue *const is_fallthru_val = new (ctx) ir_constant(false); 354922d81f154fed9e004cca91807808ae3b81b01cedEric Anholt state->switch_state.is_fallthru_var = 355022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt new(ctx) ir_variable(glsl_type::bool_type, 355122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt "switch_is_fallthru_tmp", 355222d81f154fed9e004cca91807808ae3b81b01cedEric Anholt ir_var_temporary); 355322d81f154fed9e004cca91807808ae3b81b01cedEric Anholt instructions->push_tail(state->switch_state.is_fallthru_var); 35545c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe 35555c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe ir_dereference_variable *deref_is_fallthru_var = 355622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt new(ctx) ir_dereference_variable(state->switch_state.is_fallthru_var); 35575c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe instructions->push_tail(new(ctx) ir_assignment(deref_is_fallthru_var, 3558aa5ec137757323b424d0f2638c50c93b9b06ffffEric Anholt is_fallthru_val)); 35595c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe 35605c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe /* Initalize is_break state to false. 35615c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe */ 35625c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe ir_rvalue *const is_break_val = new (ctx) ir_constant(false); 356322d81f154fed9e004cca91807808ae3b81b01cedEric Anholt state->switch_state.is_break_var = new(ctx) ir_variable(glsl_type::bool_type, 356422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt "switch_is_break_tmp", 356522d81f154fed9e004cca91807808ae3b81b01cedEric Anholt ir_var_temporary); 356622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt instructions->push_tail(state->switch_state.is_break_var); 35675c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe 35685c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe ir_dereference_variable *deref_is_break_var = 356922d81f154fed9e004cca91807808ae3b81b01cedEric Anholt new(ctx) ir_dereference_variable(state->switch_state.is_break_var); 35705c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe instructions->push_tail(new(ctx) ir_assignment(deref_is_break_var, 3571aa5ec137757323b424d0f2638c50c93b9b06ffffEric Anholt is_break_val)); 35725c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe 35735c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe /* Cache test expression. 35745c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe */ 35755c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe test_to_hir(instructions, state); 35765462f3679ab7217d3a3be48365750801c7771237Eric Anholt 35775c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe /* Emit code for body of switch stmt. 35785c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe */ 35795c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe body->hir(instructions, state); 35805c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe 3581140632190cf41e6a035ca199b181091d4ed46986Eric Anholt hash_table_dtor(state->switch_state.labels_ht); 3582140632190cf41e6a035ca199b181091d4ed46986Eric Anholt 358322d81f154fed9e004cca91807808ae3b81b01cedEric Anholt state->switch_state = saved; 358485beb39e14556cf02f58116fd287120cd1defbd5Dan McCabe 35855462f3679ab7217d3a3be48365750801c7771237Eric Anholt /* Switch statements do not have r-values. */ 35865462f3679ab7217d3a3be48365750801c7771237Eric Anholt return NULL; 35875462f3679ab7217d3a3be48365750801c7771237Eric Anholt} 35885462f3679ab7217d3a3be48365750801c7771237Eric Anholt 35895462f3679ab7217d3a3be48365750801c7771237Eric Anholt 35905462f3679ab7217d3a3be48365750801c7771237Eric Anholtvoid 35915462f3679ab7217d3a3be48365750801c7771237Eric Anholtast_switch_statement::test_to_hir(exec_list *instructions, 35925462f3679ab7217d3a3be48365750801c7771237Eric Anholt struct _mesa_glsl_parse_state *state) 35935462f3679ab7217d3a3be48365750801c7771237Eric Anholt{ 35945462f3679ab7217d3a3be48365750801c7771237Eric Anholt void *ctx = state; 35955462f3679ab7217d3a3be48365750801c7771237Eric Anholt 35965462f3679ab7217d3a3be48365750801c7771237Eric Anholt /* Cache value of test expression. */ 35975462f3679ab7217d3a3be48365750801c7771237Eric Anholt ir_rvalue *const test_val = 35985462f3679ab7217d3a3be48365750801c7771237Eric Anholt test_expression->hir(instructions, 35995462f3679ab7217d3a3be48365750801c7771237Eric Anholt state); 36005462f3679ab7217d3a3be48365750801c7771237Eric Anholt 36019c4e9ce051bb43861adb4f8cd8e88a733c2f3ed1Eric Anholt state->switch_state.test_var = new(ctx) ir_variable(test_val->type, 36025462f3679ab7217d3a3be48365750801c7771237Eric Anholt "switch_test_tmp", 36035462f3679ab7217d3a3be48365750801c7771237Eric Anholt ir_var_temporary); 36045462f3679ab7217d3a3be48365750801c7771237Eric Anholt ir_dereference_variable *deref_test_var = 36055462f3679ab7217d3a3be48365750801c7771237Eric Anholt new(ctx) ir_dereference_variable(state->switch_state.test_var); 36065462f3679ab7217d3a3be48365750801c7771237Eric Anholt 36075462f3679ab7217d3a3be48365750801c7771237Eric Anholt instructions->push_tail(state->switch_state.test_var); 3608aa5ec137757323b424d0f2638c50c93b9b06ffffEric Anholt instructions->push_tail(new(ctx) ir_assignment(deref_test_var, test_val)); 36095462f3679ab7217d3a3be48365750801c7771237Eric Anholt} 36105462f3679ab7217d3a3be48365750801c7771237Eric Anholt 36115462f3679ab7217d3a3be48365750801c7771237Eric Anholt 36125462f3679ab7217d3a3be48365750801c7771237Eric Anholtir_rvalue * 36135462f3679ab7217d3a3be48365750801c7771237Eric Anholtast_switch_body::hir(exec_list *instructions, 36145462f3679ab7217d3a3be48365750801c7771237Eric Anholt struct _mesa_glsl_parse_state *state) 36155462f3679ab7217d3a3be48365750801c7771237Eric Anholt{ 36165462f3679ab7217d3a3be48365750801c7771237Eric Anholt if (stmts != NULL) 36175462f3679ab7217d3a3be48365750801c7771237Eric Anholt stmts->hir(instructions, state); 36185462f3679ab7217d3a3be48365750801c7771237Eric Anholt 36195462f3679ab7217d3a3be48365750801c7771237Eric Anholt /* Switch bodies do not have r-values. */ 36205462f3679ab7217d3a3be48365750801c7771237Eric Anholt return NULL; 36215462f3679ab7217d3a3be48365750801c7771237Eric Anholt} 36225462f3679ab7217d3a3be48365750801c7771237Eric Anholt 36235462f3679ab7217d3a3be48365750801c7771237Eric Anholtir_rvalue * 36245462f3679ab7217d3a3be48365750801c7771237Eric Anholtast_case_statement_list::hir(exec_list *instructions, 36255462f3679ab7217d3a3be48365750801c7771237Eric Anholt struct _mesa_glsl_parse_state *state) 36265462f3679ab7217d3a3be48365750801c7771237Eric Anholt{ 36275462f3679ab7217d3a3be48365750801c7771237Eric Anholt foreach_list_typed (ast_case_statement, case_stmt, link, & this->cases) 36285462f3679ab7217d3a3be48365750801c7771237Eric Anholt case_stmt->hir(instructions, state); 36295462f3679ab7217d3a3be48365750801c7771237Eric Anholt 36305462f3679ab7217d3a3be48365750801c7771237Eric Anholt /* Case statements do not have r-values. */ 36315462f3679ab7217d3a3be48365750801c7771237Eric Anholt return NULL; 36325462f3679ab7217d3a3be48365750801c7771237Eric Anholt} 36335462f3679ab7217d3a3be48365750801c7771237Eric Anholt 36345462f3679ab7217d3a3be48365750801c7771237Eric Anholtir_rvalue * 36355462f3679ab7217d3a3be48365750801c7771237Eric Anholtast_case_statement::hir(exec_list *instructions, 36365462f3679ab7217d3a3be48365750801c7771237Eric Anholt struct _mesa_glsl_parse_state *state) 36375462f3679ab7217d3a3be48365750801c7771237Eric Anholt{ 36385462f3679ab7217d3a3be48365750801c7771237Eric Anholt labels->hir(instructions, state); 36395462f3679ab7217d3a3be48365750801c7771237Eric Anholt 36405462f3679ab7217d3a3be48365750801c7771237Eric Anholt /* Conditionally set fallthru state based on break state. */ 36415462f3679ab7217d3a3be48365750801c7771237Eric Anholt ir_constant *const false_val = new(state) ir_constant(false); 36425462f3679ab7217d3a3be48365750801c7771237Eric Anholt ir_dereference_variable *const deref_is_fallthru_var = 36435462f3679ab7217d3a3be48365750801c7771237Eric Anholt new(state) ir_dereference_variable(state->switch_state.is_fallthru_var); 36445462f3679ab7217d3a3be48365750801c7771237Eric Anholt ir_dereference_variable *const deref_is_break_var = 36455462f3679ab7217d3a3be48365750801c7771237Eric Anholt new(state) ir_dereference_variable(state->switch_state.is_break_var); 36465462f3679ab7217d3a3be48365750801c7771237Eric Anholt ir_assignment *const reset_fallthru_on_break = 36475462f3679ab7217d3a3be48365750801c7771237Eric Anholt new(state) ir_assignment(deref_is_fallthru_var, 36485462f3679ab7217d3a3be48365750801c7771237Eric Anholt false_val, 36495462f3679ab7217d3a3be48365750801c7771237Eric Anholt deref_is_break_var); 36505462f3679ab7217d3a3be48365750801c7771237Eric Anholt instructions->push_tail(reset_fallthru_on_break); 36515462f3679ab7217d3a3be48365750801c7771237Eric Anholt 36525462f3679ab7217d3a3be48365750801c7771237Eric Anholt /* Guard case statements depending on fallthru state. */ 36535462f3679ab7217d3a3be48365750801c7771237Eric Anholt ir_dereference_variable *const deref_fallthru_guard = 36545462f3679ab7217d3a3be48365750801c7771237Eric Anholt new(state) ir_dereference_variable(state->switch_state.is_fallthru_var); 36555462f3679ab7217d3a3be48365750801c7771237Eric Anholt ir_if *const test_fallthru = new(state) ir_if(deref_fallthru_guard); 36565462f3679ab7217d3a3be48365750801c7771237Eric Anholt 36575462f3679ab7217d3a3be48365750801c7771237Eric Anholt foreach_list_typed (ast_node, stmt, link, & this->stmts) 36585462f3679ab7217d3a3be48365750801c7771237Eric Anholt stmt->hir(& test_fallthru->then_instructions, state); 36595462f3679ab7217d3a3be48365750801c7771237Eric Anholt 36605462f3679ab7217d3a3be48365750801c7771237Eric Anholt instructions->push_tail(test_fallthru); 36615462f3679ab7217d3a3be48365750801c7771237Eric Anholt 36625462f3679ab7217d3a3be48365750801c7771237Eric Anholt /* Case statements do not have r-values. */ 36635462f3679ab7217d3a3be48365750801c7771237Eric Anholt return NULL; 36645462f3679ab7217d3a3be48365750801c7771237Eric Anholt} 36655462f3679ab7217d3a3be48365750801c7771237Eric Anholt 36665462f3679ab7217d3a3be48365750801c7771237Eric Anholt 36675462f3679ab7217d3a3be48365750801c7771237Eric Anholtir_rvalue * 36685462f3679ab7217d3a3be48365750801c7771237Eric Anholtast_case_label_list::hir(exec_list *instructions, 36695462f3679ab7217d3a3be48365750801c7771237Eric Anholt struct _mesa_glsl_parse_state *state) 36705462f3679ab7217d3a3be48365750801c7771237Eric Anholt{ 36715462f3679ab7217d3a3be48365750801c7771237Eric Anholt foreach_list_typed (ast_case_label, label, link, & this->labels) 36725462f3679ab7217d3a3be48365750801c7771237Eric Anholt label->hir(instructions, state); 36735462f3679ab7217d3a3be48365750801c7771237Eric Anholt 36745462f3679ab7217d3a3be48365750801c7771237Eric Anholt /* Case labels do not have r-values. */ 36755462f3679ab7217d3a3be48365750801c7771237Eric Anholt return NULL; 36765462f3679ab7217d3a3be48365750801c7771237Eric Anholt} 36775462f3679ab7217d3a3be48365750801c7771237Eric Anholt 36785462f3679ab7217d3a3be48365750801c7771237Eric Anholtir_rvalue * 36795462f3679ab7217d3a3be48365750801c7771237Eric Anholtast_case_label::hir(exec_list *instructions, 36805462f3679ab7217d3a3be48365750801c7771237Eric Anholt struct _mesa_glsl_parse_state *state) 36815462f3679ab7217d3a3be48365750801c7771237Eric Anholt{ 36825462f3679ab7217d3a3be48365750801c7771237Eric Anholt void *ctx = state; 36835462f3679ab7217d3a3be48365750801c7771237Eric Anholt 36845462f3679ab7217d3a3be48365750801c7771237Eric Anholt ir_dereference_variable *deref_fallthru_var = 36855462f3679ab7217d3a3be48365750801c7771237Eric Anholt new(ctx) ir_dereference_variable(state->switch_state.is_fallthru_var); 36865462f3679ab7217d3a3be48365750801c7771237Eric Anholt 36875462f3679ab7217d3a3be48365750801c7771237Eric Anholt ir_rvalue *const true_val = new(ctx) ir_constant(true); 36885462f3679ab7217d3a3be48365750801c7771237Eric Anholt 36895462f3679ab7217d3a3be48365750801c7771237Eric Anholt /* If not default case, ... */ 36905462f3679ab7217d3a3be48365750801c7771237Eric Anholt if (this->test_value != NULL) { 36915462f3679ab7217d3a3be48365750801c7771237Eric Anholt /* Conditionally set fallthru state based on 36925462f3679ab7217d3a3be48365750801c7771237Eric Anholt * comparison of cached test expression value to case label. 36935462f3679ab7217d3a3be48365750801c7771237Eric Anholt */ 36945462f3679ab7217d3a3be48365750801c7771237Eric Anholt ir_rvalue *const label_rval = this->test_value->hir(instructions, state); 36955462f3679ab7217d3a3be48365750801c7771237Eric Anholt ir_constant *label_const = label_rval->constant_expression_value(); 36965462f3679ab7217d3a3be48365750801c7771237Eric Anholt 36975462f3679ab7217d3a3be48365750801c7771237Eric Anholt if (!label_const) { 36985462f3679ab7217d3a3be48365750801c7771237Eric Anholt YYLTYPE loc = this->test_value->get_location(); 36995462f3679ab7217d3a3be48365750801c7771237Eric Anholt 37005462f3679ab7217d3a3be48365750801c7771237Eric Anholt _mesa_glsl_error(& loc, state, 37015462f3679ab7217d3a3be48365750801c7771237Eric Anholt "switch statement case label must be a " 37025462f3679ab7217d3a3be48365750801c7771237Eric Anholt "constant expression"); 37035462f3679ab7217d3a3be48365750801c7771237Eric Anholt 37045462f3679ab7217d3a3be48365750801c7771237Eric Anholt /* Stuff a dummy value in to allow processing to continue. */ 37055462f3679ab7217d3a3be48365750801c7771237Eric Anholt label_const = new(ctx) ir_constant(0); 37065462f3679ab7217d3a3be48365750801c7771237Eric Anholt } else { 37075462f3679ab7217d3a3be48365750801c7771237Eric Anholt ast_expression *previous_label = (ast_expression *) 37085462f3679ab7217d3a3be48365750801c7771237Eric Anholt hash_table_find(state->switch_state.labels_ht, 37095462f3679ab7217d3a3be48365750801c7771237Eric Anholt (void *)(uintptr_t)label_const->value.u[0]); 37105462f3679ab7217d3a3be48365750801c7771237Eric Anholt 37115462f3679ab7217d3a3be48365750801c7771237Eric Anholt if (previous_label) { 37125462f3679ab7217d3a3be48365750801c7771237Eric Anholt YYLTYPE loc = this->test_value->get_location(); 37135462f3679ab7217d3a3be48365750801c7771237Eric Anholt _mesa_glsl_error(& loc, state, 37145462f3679ab7217d3a3be48365750801c7771237Eric Anholt "duplicate case value"); 37155462f3679ab7217d3a3be48365750801c7771237Eric Anholt 37165462f3679ab7217d3a3be48365750801c7771237Eric Anholt loc = previous_label->get_location(); 37175462f3679ab7217d3a3be48365750801c7771237Eric Anholt _mesa_glsl_error(& loc, state, 37185462f3679ab7217d3a3be48365750801c7771237Eric Anholt "this is the previous case label"); 37195462f3679ab7217d3a3be48365750801c7771237Eric Anholt } else { 37205462f3679ab7217d3a3be48365750801c7771237Eric Anholt hash_table_insert(state->switch_state.labels_ht, 37215462f3679ab7217d3a3be48365750801c7771237Eric Anholt this->test_value, 3722140632190cf41e6a035ca199b181091d4ed46986Eric Anholt (void *)(uintptr_t)label_const->value.u[0]); 37235462f3679ab7217d3a3be48365750801c7771237Eric Anholt } 37245462f3679ab7217d3a3be48365750801c7771237Eric Anholt } 37255462f3679ab7217d3a3be48365750801c7771237Eric Anholt 37265462f3679ab7217d3a3be48365750801c7771237Eric Anholt ir_dereference_variable *deref_test_var = 37275462f3679ab7217d3a3be48365750801c7771237Eric Anholt new(ctx) ir_dereference_variable(state->switch_state.test_var); 37285462f3679ab7217d3a3be48365750801c7771237Eric Anholt 37295462f3679ab7217d3a3be48365750801c7771237Eric Anholt ir_rvalue *const test_cond = new(ctx) ir_expression(ir_binop_all_equal, 37305462f3679ab7217d3a3be48365750801c7771237Eric Anholt label_const, 37315462f3679ab7217d3a3be48365750801c7771237Eric Anholt deref_test_var); 37325462f3679ab7217d3a3be48365750801c7771237Eric Anholt 37335462f3679ab7217d3a3be48365750801c7771237Eric Anholt ir_assignment *set_fallthru_on_test = 37345462f3679ab7217d3a3be48365750801c7771237Eric Anholt new(ctx) ir_assignment(deref_fallthru_var, 37355462f3679ab7217d3a3be48365750801c7771237Eric Anholt true_val, 37365462f3679ab7217d3a3be48365750801c7771237Eric Anholt test_cond); 37375462f3679ab7217d3a3be48365750801c7771237Eric Anholt 37385462f3679ab7217d3a3be48365750801c7771237Eric Anholt instructions->push_tail(set_fallthru_on_test); 37395462f3679ab7217d3a3be48365750801c7771237Eric Anholt } else { /* default case */ 37405462f3679ab7217d3a3be48365750801c7771237Eric Anholt if (state->switch_state.previous_default) { 37415462f3679ab7217d3a3be48365750801c7771237Eric Anholt YYLTYPE loc = this->get_location(); 37425462f3679ab7217d3a3be48365750801c7771237Eric Anholt _mesa_glsl_error(& loc, state, 37435462f3679ab7217d3a3be48365750801c7771237Eric Anholt "multiple default labels in one switch"); 37445462f3679ab7217d3a3be48365750801c7771237Eric Anholt 37455462f3679ab7217d3a3be48365750801c7771237Eric Anholt loc = state->switch_state.previous_default->get_location(); 37465462f3679ab7217d3a3be48365750801c7771237Eric Anholt _mesa_glsl_error(& loc, state, 37475462f3679ab7217d3a3be48365750801c7771237Eric Anholt "this is the first default label"); 37485462f3679ab7217d3a3be48365750801c7771237Eric Anholt } 37495462f3679ab7217d3a3be48365750801c7771237Eric Anholt state->switch_state.previous_default = this; 37505462f3679ab7217d3a3be48365750801c7771237Eric Anholt 37515462f3679ab7217d3a3be48365750801c7771237Eric Anholt /* Set falltrhu state. */ 37525462f3679ab7217d3a3be48365750801c7771237Eric Anholt ir_assignment *set_fallthru = 3753aa5ec137757323b424d0f2638c50c93b9b06ffffEric Anholt new(ctx) ir_assignment(deref_fallthru_var, true_val); 37545462f3679ab7217d3a3be48365750801c7771237Eric Anholt 37555462f3679ab7217d3a3be48365750801c7771237Eric Anholt instructions->push_tail(set_fallthru); 37565462f3679ab7217d3a3be48365750801c7771237Eric Anholt } 3757140632190cf41e6a035ca199b181091d4ed46986Eric Anholt 37585462f3679ab7217d3a3be48365750801c7771237Eric Anholt /* Case statements do not have r-values. */ 37595462f3679ab7217d3a3be48365750801c7771237Eric Anholt return NULL; 37605462f3679ab7217d3a3be48365750801c7771237Eric Anholt} 37615462f3679ab7217d3a3be48365750801c7771237Eric Anholt 37625462f3679ab7217d3a3be48365750801c7771237Eric Anholtvoid 37635462f3679ab7217d3a3be48365750801c7771237Eric Anholtast_iteration_statement::condition_to_hir(ir_loop *stmt, 37645462f3679ab7217d3a3be48365750801c7771237Eric Anholt struct _mesa_glsl_parse_state *state) 37655462f3679ab7217d3a3be48365750801c7771237Eric Anholt{ 37665462f3679ab7217d3a3be48365750801c7771237Eric Anholt void *ctx = state; 37675462f3679ab7217d3a3be48365750801c7771237Eric Anholt 37685462f3679ab7217d3a3be48365750801c7771237Eric Anholt if (condition != NULL) { 37695462f3679ab7217d3a3be48365750801c7771237Eric Anholt ir_rvalue *const cond = 37705462f3679ab7217d3a3be48365750801c7771237Eric Anholt condition->hir(& stmt->body_instructions, state); 37715462f3679ab7217d3a3be48365750801c7771237Eric Anholt 37725462f3679ab7217d3a3be48365750801c7771237Eric Anholt if ((cond == NULL) 37735462f3679ab7217d3a3be48365750801c7771237Eric Anholt || !cond->type->is_boolean() || !cond->type->is_scalar()) { 37745462f3679ab7217d3a3be48365750801c7771237Eric Anholt YYLTYPE loc = condition->get_location(); 37755462f3679ab7217d3a3be48365750801c7771237Eric Anholt 37765462f3679ab7217d3a3be48365750801c7771237Eric Anholt _mesa_glsl_error(& loc, state, 37775462f3679ab7217d3a3be48365750801c7771237Eric Anholt "loop condition must be scalar boolean"); 37785462f3679ab7217d3a3be48365750801c7771237Eric Anholt } else { 37795462f3679ab7217d3a3be48365750801c7771237Eric Anholt /* As the first code in the loop body, generate a block that looks 37805462f3679ab7217d3a3be48365750801c7771237Eric Anholt * like 'if (!condition) break;' as the loop termination condition. 37815462f3679ab7217d3a3be48365750801c7771237Eric Anholt */ 37825462f3679ab7217d3a3be48365750801c7771237Eric Anholt ir_rvalue *const not_cond = 37835d6ea16dfe99e1aba61c25a897b66951faab1a39Eric Anholt new(ctx) ir_expression(ir_unop_logic_not, cond); 37845462f3679ab7217d3a3be48365750801c7771237Eric Anholt 37855462f3679ab7217d3a3be48365750801c7771237Eric Anholt ir_if *const if_stmt = new(ctx) ir_if(not_cond); 37865462f3679ab7217d3a3be48365750801c7771237Eric Anholt 37875462f3679ab7217d3a3be48365750801c7771237Eric Anholt ir_jump *const break_stmt = 37885462f3679ab7217d3a3be48365750801c7771237Eric Anholt new(ctx) ir_loop_jump(ir_loop_jump::jump_break); 37895462f3679ab7217d3a3be48365750801c7771237Eric Anholt 37905462f3679ab7217d3a3be48365750801c7771237Eric Anholt if_stmt->then_instructions.push_tail(break_stmt); 37915462f3679ab7217d3a3be48365750801c7771237Eric Anholt stmt->body_instructions.push_tail(if_stmt); 37925462f3679ab7217d3a3be48365750801c7771237Eric Anholt } 37935462f3679ab7217d3a3be48365750801c7771237Eric Anholt } 37945462f3679ab7217d3a3be48365750801c7771237Eric Anholt} 37955462f3679ab7217d3a3be48365750801c7771237Eric Anholt 37965462f3679ab7217d3a3be48365750801c7771237Eric Anholt 37975462f3679ab7217d3a3be48365750801c7771237Eric Anholtir_rvalue * 37985462f3679ab7217d3a3be48365750801c7771237Eric Anholtast_iteration_statement::hir(exec_list *instructions, 37995462f3679ab7217d3a3be48365750801c7771237Eric Anholt struct _mesa_glsl_parse_state *state) 38005462f3679ab7217d3a3be48365750801c7771237Eric Anholt{ 38015462f3679ab7217d3a3be48365750801c7771237Eric Anholt void *ctx = state; 38025462f3679ab7217d3a3be48365750801c7771237Eric Anholt 38035462f3679ab7217d3a3be48365750801c7771237Eric Anholt /* For-loops and while-loops start a new scope, but do-while loops do not. 38045462f3679ab7217d3a3be48365750801c7771237Eric Anholt */ 38055462f3679ab7217d3a3be48365750801c7771237Eric Anholt if (mode != ast_do_while) 38065462f3679ab7217d3a3be48365750801c7771237Eric Anholt state->symbols->push_scope(); 38075462f3679ab7217d3a3be48365750801c7771237Eric Anholt 38085462f3679ab7217d3a3be48365750801c7771237Eric Anholt if (init_statement != NULL) 38095462f3679ab7217d3a3be48365750801c7771237Eric Anholt init_statement->hir(instructions, state); 38105462f3679ab7217d3a3be48365750801c7771237Eric Anholt 38115462f3679ab7217d3a3be48365750801c7771237Eric Anholt ir_loop *const stmt = new(ctx) ir_loop(); 38125462f3679ab7217d3a3be48365750801c7771237Eric Anholt instructions->push_tail(stmt); 38135462f3679ab7217d3a3be48365750801c7771237Eric Anholt 38145462f3679ab7217d3a3be48365750801c7771237Eric Anholt /* Track the current loop nesting. */ 38155462f3679ab7217d3a3be48365750801c7771237Eric Anholt ast_iteration_statement *nesting_ast = state->loop_nesting_ast; 38165462f3679ab7217d3a3be48365750801c7771237Eric Anholt 38175462f3679ab7217d3a3be48365750801c7771237Eric Anholt state->loop_nesting_ast = this; 38185462f3679ab7217d3a3be48365750801c7771237Eric Anholt 38195462f3679ab7217d3a3be48365750801c7771237Eric Anholt /* Likewise, indicate that following code is closest to a loop, 38205462f3679ab7217d3a3be48365750801c7771237Eric Anholt * NOT closest to a switch. 38215462f3679ab7217d3a3be48365750801c7771237Eric Anholt */ 38225462f3679ab7217d3a3be48365750801c7771237Eric Anholt bool saved_is_switch_innermost = state->switch_state.is_switch_innermost; 38235462f3679ab7217d3a3be48365750801c7771237Eric Anholt state->switch_state.is_switch_innermost = false; 38245462f3679ab7217d3a3be48365750801c7771237Eric Anholt 38255462f3679ab7217d3a3be48365750801c7771237Eric Anholt if (mode != ast_do_while) 38265462f3679ab7217d3a3be48365750801c7771237Eric Anholt condition_to_hir(stmt, state); 38275462f3679ab7217d3a3be48365750801c7771237Eric Anholt 38285462f3679ab7217d3a3be48365750801c7771237Eric Anholt if (body != NULL) 38295462f3679ab7217d3a3be48365750801c7771237Eric Anholt body->hir(& stmt->body_instructions, state); 38305462f3679ab7217d3a3be48365750801c7771237Eric Anholt 38315462f3679ab7217d3a3be48365750801c7771237Eric Anholt if (rest_expression != NULL) 38325462f3679ab7217d3a3be48365750801c7771237Eric Anholt rest_expression->hir(& stmt->body_instructions, state); 38335462f3679ab7217d3a3be48365750801c7771237Eric Anholt 38345462f3679ab7217d3a3be48365750801c7771237Eric Anholt if (mode == ast_do_while) 38355462f3679ab7217d3a3be48365750801c7771237Eric Anholt condition_to_hir(stmt, state); 38365462f3679ab7217d3a3be48365750801c7771237Eric Anholt 38375462f3679ab7217d3a3be48365750801c7771237Eric Anholt if (mode != ast_do_while) 38385462f3679ab7217d3a3be48365750801c7771237Eric Anholt state->symbols->pop_scope(); 383922d81f154fed9e004cca91807808ae3b81b01cedEric Anholt 38405462f3679ab7217d3a3be48365750801c7771237Eric Anholt /* Restore previous nesting before returning. */ 38415462f3679ab7217d3a3be48365750801c7771237Eric Anholt state->loop_nesting_ast = nesting_ast; 38425462f3679ab7217d3a3be48365750801c7771237Eric Anholt state->switch_state.is_switch_innermost = saved_is_switch_innermost; 3843e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick 38449e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick /* Loops do not have r-values. 38459e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick */ 38469e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick return NULL; 38479e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick} 38483455ce614424a5a23a23037e23d0454e476bceeaIan Romanick 38493455ce614424a5a23a23037e23d0454e476bceeaIan Romanick 38503455ce614424a5a23a23037e23d0454e476bceeaIan Romanickir_rvalue * 38513455ce614424a5a23a23037e23d0454e476bceeaIan Romanickast_type_specifier::hir(exec_list *instructions, 38523455ce614424a5a23a23037e23d0454e476bceeaIan Romanick struct _mesa_glsl_parse_state *state) 38533455ce614424a5a23a23037e23d0454e476bceeaIan Romanick{ 385408a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace if (!this->is_precision_statement && this->structure == NULL) 385508a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace return NULL; 385608a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace 385708a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace YYLTYPE loc = this->get_location(); 385808a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace 385908a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace if (this->precision != ast_precision_none 386008a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace && state->language_version != 100 386108a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace && state->language_version < 130) { 386208a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace _mesa_glsl_error(&loc, state, 386308a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace "precision qualifiers exist only in " 386408a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace "GLSL ES 1.00, and GLSL 1.30 and later"); 386508a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace return NULL; 386608a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace } 386708a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace if (this->precision != ast_precision_none 386808a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace && this->structure != NULL) { 386908a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace _mesa_glsl_error(&loc, state, 387008a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace "precision qualifiers do not apply to structures"); 387108a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace return NULL; 387208a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace } 387308a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace 387408a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace /* If this is a precision statement, check that the type to which it is 387508a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace * applied is either float or int. 387608a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace * 387708a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace * From section 4.5.3 of the GLSL 1.30 spec: 387808a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace * "The precision statement 387908a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace * precision precision-qualifier type; 388008a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace * can be used to establish a default precision qualifier. The type 388108a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace * field can be either int or float [...]. Any other types or 388208a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace * qualifiers will result in an error. 388308a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace */ 388408a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace if (this->is_precision_statement) { 388508a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace assert(this->precision != ast_precision_none); 388608a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace assert(this->structure == NULL); /* The check for structures was 388708a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace * performed above. */ 388808a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace if (this->is_array) { 388908a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace _mesa_glsl_error(&loc, state, 389008a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace "default precision statements do not apply to " 389108a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace "arrays"); 389208a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace return NULL; 389308a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace } 3894eb7a71dea78152142b456f29e4881c4d3aeb56b6Eric Anholt if (strcmp(this->type_name, "float") != 0 && 3895eb7a71dea78152142b456f29e4881c4d3aeb56b6Eric Anholt strcmp(this->type_name, "int") != 0) { 389608a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace _mesa_glsl_error(&loc, state, 389708a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace "default precision statements apply only to types " 389808a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace "float and int"); 389908a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace return NULL; 390008a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace } 390108a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace 390208a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace /* FINISHME: Translate precision statements into IR. */ 390308a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace return NULL; 390408a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace } 390508a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace 39063455ce614424a5a23a23037e23d0454e476bceeaIan Romanick if (this->structure != NULL) 39073455ce614424a5a23a23037e23d0454e476bceeaIan Romanick return this->structure->hir(instructions, state); 390885ba37b97df8edd18b757bc475b12b66f4b117edIan Romanick 390985ba37b97df8edd18b757bc475b12b66f4b117edIan Romanick return NULL; 39103455ce614424a5a23a23037e23d0454e476bceeaIan Romanick} 39113455ce614424a5a23a23037e23d0454e476bceeaIan Romanick 39123455ce614424a5a23a23037e23d0454e476bceeaIan Romanick 39133455ce614424a5a23a23037e23d0454e476bceeaIan Romanickir_rvalue * 39143455ce614424a5a23a23037e23d0454e476bceeaIan Romanickast_struct_specifier::hir(exec_list *instructions, 39153455ce614424a5a23a23037e23d0454e476bceeaIan Romanick struct _mesa_glsl_parse_state *state) 39163455ce614424a5a23a23037e23d0454e476bceeaIan Romanick{ 39173455ce614424a5a23a23037e23d0454e476bceeaIan Romanick unsigned decl_count = 0; 39183455ce614424a5a23a23037e23d0454e476bceeaIan Romanick 39193455ce614424a5a23a23037e23d0454e476bceeaIan Romanick /* Make an initial pass over the list of structure fields to determine how 39203455ce614424a5a23a23037e23d0454e476bceeaIan Romanick * many there are. Each element in this list is an ast_declarator_list. 39213455ce614424a5a23a23037e23d0454e476bceeaIan Romanick * This means that we actually need to count the number of elements in the 39223455ce614424a5a23a23037e23d0454e476bceeaIan Romanick * 'declarations' list in each of the elements. 39233455ce614424a5a23a23037e23d0454e476bceeaIan Romanick */ 39242b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick foreach_list_typed (ast_declarator_list, decl_list, link, 39252b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick &this->declarations) { 3926304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick foreach_list_const (decl_ptr, & decl_list->declarations) { 39273455ce614424a5a23a23037e23d0454e476bceeaIan Romanick decl_count++; 39283455ce614424a5a23a23037e23d0454e476bceeaIan Romanick } 39293455ce614424a5a23a23037e23d0454e476bceeaIan Romanick } 39303455ce614424a5a23a23037e23d0454e476bceeaIan Romanick 39313455ce614424a5a23a23037e23d0454e476bceeaIan Romanick /* Allocate storage for the structure fields and process the field 39323455ce614424a5a23a23037e23d0454e476bceeaIan Romanick * declarations. As the declarations are processed, try to also convert 39333455ce614424a5a23a23037e23d0454e476bceeaIan Romanick * the types to HIR. This ensures that structure definitions embedded in 39343455ce614424a5a23a23037e23d0454e476bceeaIan Romanick * other structure definitions are processed. 39353455ce614424a5a23a23037e23d0454e476bceeaIan Romanick */ 3936d3073f58c17d8675a2ecdd5dfa83e5520c78e1a8Kenneth Graunke glsl_struct_field *const fields = ralloc_array(state, glsl_struct_field, 393721b0dbd79937e9d6787f045af7d60d4b6c649ec8Eric Anholt decl_count); 39383455ce614424a5a23a23037e23d0454e476bceeaIan Romanick 39393455ce614424a5a23a23037e23d0454e476bceeaIan Romanick unsigned i = 0; 39402b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick foreach_list_typed (ast_declarator_list, decl_list, link, 39412b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick &this->declarations) { 39423455ce614424a5a23a23037e23d0454e476bceeaIan Romanick const char *type_name; 39433455ce614424a5a23a23037e23d0454e476bceeaIan Romanick 39443455ce614424a5a23a23037e23d0454e476bceeaIan Romanick decl_list->type->specifier->hir(instructions, state); 39453455ce614424a5a23a23037e23d0454e476bceeaIan Romanick 3946c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke /* Section 10.9 of the GLSL ES 1.00 specification states that 3947c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke * embedded structure definitions have been removed from the language. 3948c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke */ 3949c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke if (state->es_shader && decl_list->type->specifier->structure != NULL) { 3950c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke YYLTYPE loc = this->get_location(); 3951c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke _mesa_glsl_error(&loc, state, "Embedded structure definitions are " 3952c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke "not allowed in GLSL ES 1.00."); 3953c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke } 3954c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke 39553455ce614424a5a23a23037e23d0454e476bceeaIan Romanick const glsl_type *decl_type = 39563455ce614424a5a23a23037e23d0454e476bceeaIan Romanick decl_list->type->specifier->glsl_type(& type_name, state); 39573455ce614424a5a23a23037e23d0454e476bceeaIan Romanick 39582b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick foreach_list_typed (ast_declaration, decl, link, 39592b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick &decl_list->declarations) { 3960d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke const struct glsl_type *field_type = decl_type; 3961d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke if (decl->is_array) { 3962d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke YYLTYPE loc = decl->get_location(); 3963d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke field_type = process_array_type(&loc, decl_type, decl->array_size, 3964d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke state); 3965d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke } 396673986a7a262807ab2cfd6d46ae17cfc7a30cdfecIan Romanick fields[i].type = (field_type != NULL) 396773986a7a262807ab2cfd6d46ae17cfc7a30cdfecIan Romanick ? field_type : glsl_type::error_type; 39683455ce614424a5a23a23037e23d0454e476bceeaIan Romanick fields[i].name = decl->identifier; 39693455ce614424a5a23a23037e23d0454e476bceeaIan Romanick i++; 39703455ce614424a5a23a23037e23d0454e476bceeaIan Romanick } 39713455ce614424a5a23a23037e23d0454e476bceeaIan Romanick } 39723455ce614424a5a23a23037e23d0454e476bceeaIan Romanick 39733455ce614424a5a23a23037e23d0454e476bceeaIan Romanick assert(i == decl_count); 39743455ce614424a5a23a23037e23d0454e476bceeaIan Romanick 397549e3577b91f44013746f7a3db411e7041b7d899eIan Romanick const glsl_type *t = 3976ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke glsl_type::get_record_instance(fields, decl_count, this->name); 39771d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick 3978ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick YYLTYPE loc = this->get_location(); 3979a789ca649cb143c0c5bf3209ff1bde398fbd777eIan Romanick if (!state->symbols->add_type(name, t)) { 3980ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name); 3981ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick } else { 3982eb639349e289a6b8be06a54f5e9e0ce18c71d511Kenneth Graunke const glsl_type **s = reralloc(state, state->user_structures, 3983eb639349e289a6b8be06a54f5e9e0ce18c71d511Kenneth Graunke const glsl_type *, 3984eb639349e289a6b8be06a54f5e9e0ce18c71d511Kenneth Graunke state->num_user_structures + 1); 3985a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick if (s != NULL) { 3986a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick s[state->num_user_structures] = t; 3987a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick state->user_structures = s; 3988a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick state->num_user_structures++; 3989a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick } 3990ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick } 39913455ce614424a5a23a23037e23d0454e476bceeaIan Romanick 39923455ce614424a5a23a23037e23d0454e476bceeaIan Romanick /* Structure type definitions do not have r-values. 39933455ce614424a5a23a23037e23d0454e476bceeaIan Romanick */ 39943455ce614424a5a23a23037e23d0454e476bceeaIan Romanick return NULL; 39953455ce614424a5a23a23037e23d0454e476bceeaIan Romanick} 39964b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt 3997b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholtstatic struct gl_uniform_block * 3998b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholtget_next_uniform_block(struct _mesa_glsl_parse_state *state) 3999b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt{ 4000b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt if (state->num_uniform_blocks >= state->uniform_block_array_size) { 4001b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt state->uniform_block_array_size *= 2; 4002b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt if (state->uniform_block_array_size <= 4) 4003b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt state->uniform_block_array_size = 4; 4004b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt 4005b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt state->uniform_blocks = reralloc(state, 4006b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt state->uniform_blocks, 4007b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt struct gl_uniform_block, 4008b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt state->uniform_block_array_size); 4009b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt } 4010b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt 4011b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt memset(&state->uniform_blocks[state->num_uniform_blocks], 4012b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt 0, sizeof(*state->uniform_blocks)); 4013b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt return &state->uniform_blocks[state->num_uniform_blocks++]; 4014b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt} 4015b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt 40162d03f48a65a666ecdcfaffa3d39ad1b77f2b25b6Eric Anholtir_rvalue * 40172d03f48a65a666ecdcfaffa3d39ad1b77f2b25b6Eric Anholtast_uniform_block::hir(exec_list *instructions, 40182d03f48a65a666ecdcfaffa3d39ad1b77f2b25b6Eric Anholt struct _mesa_glsl_parse_state *state) 40192d03f48a65a666ecdcfaffa3d39ad1b77f2b25b6Eric Anholt{ 40202d03f48a65a666ecdcfaffa3d39ad1b77f2b25b6Eric Anholt /* The ast_uniform_block has a list of ast_declarator_lists. We 40212d03f48a65a666ecdcfaffa3d39ad1b77f2b25b6Eric Anholt * need to turn those into ir_variables with an association 40222d03f48a65a666ecdcfaffa3d39ad1b77f2b25b6Eric Anholt * with this uniform block. 40232d03f48a65a666ecdcfaffa3d39ad1b77f2b25b6Eric Anholt */ 4024b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt struct gl_uniform_block *ubo = get_next_uniform_block(state); 4025b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt ubo->Name = ralloc_strdup(state->uniform_blocks, this->block_name); 4026b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt 4027b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt unsigned int num_variables = 0; 4028b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt foreach_list_typed(ast_declarator_list, decl_list, link, &declarations) { 4029b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt foreach_list_const(node, &decl_list->declarations) { 4030b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt num_variables++; 4031b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt } 4032b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt } 4033b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt 4034b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt bool block_row_major = this->layout.flags.q.row_major; 4035b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt 4036b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt ubo->Uniforms = rzalloc_array(state->uniform_blocks, 4037b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt struct gl_uniform_buffer_variable, 4038b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt num_variables); 4039b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt 4040f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt foreach_list_typed(ast_declarator_list, decl_list, link, &declarations) { 4041f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt exec_list declared_variables; 4042f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt 4043f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt decl_list->hir(&declared_variables, state); 4044f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt 4045b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt foreach_list_const(node, &declared_variables) { 4046b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt struct ir_variable *var = (ir_variable *)node; 4047b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt 4048b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt struct gl_uniform_buffer_variable *ubo_var = 4049b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt &ubo->Uniforms[ubo->NumUniforms++]; 4050b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt 4051b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt var->uniform_block = ubo - state->uniform_blocks; 4052b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt 4053b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt ubo_var->Name = ralloc_strdup(state->uniform_blocks, var->name); 4054b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt ubo_var->Type = var->type; 4055b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt ubo_var->Buffer = ubo - state->uniform_blocks; 4056b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt ubo_var->Offset = 0; /* Assigned at link time. */ 4057b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt ubo_var->RowMajor = block_row_major; 4058b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt if (decl_list->type->qualifier.flags.q.row_major) 4059b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt ubo_var->RowMajor = true; 4060b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt else if (decl_list->type->qualifier.flags.q.column_major) 4061b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt ubo_var->RowMajor = false; 4062b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt 4063b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt /* From the GL_ARB_uniform_buffer_object spec: 4064b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt * 4065b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt * "Sampler types are not allowed inside of uniform 4066b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt * blocks. All other types, arrays, and structures 4067b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt * allowed for uniforms are allowed within a uniform 4068b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt * block." 4069b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt */ 4070b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt if (var->type->contains_sampler()) { 4071b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt YYLTYPE loc = decl_list->get_location(); 4072b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt _mesa_glsl_error(&loc, state, 4073b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt "Uniform in non-default uniform block contains sampler\n"); 4074b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt } 4075b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt } 4076b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt 4077f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt instructions->append_list(&declared_variables); 4078f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt } 4079f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt 40802d03f48a65a666ecdcfaffa3d39ad1b77f2b25b6Eric Anholt return NULL; 40812d03f48a65a666ecdcfaffa3d39ad1b77f2b25b6Eric Anholt} 40822d03f48a65a666ecdcfaffa3d39ad1b77f2b25b6Eric Anholt 40834b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholtstatic void 40844b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholtdetect_conflicting_assignments(struct _mesa_glsl_parse_state *state, 40854b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt exec_list *instructions) 40864b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt{ 40874b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt bool gl_FragColor_assigned = false; 40884b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt bool gl_FragData_assigned = false; 40894b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt bool user_defined_fs_output_assigned = false; 40904b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt ir_variable *user_defined_fs_output = NULL; 40914b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt 40924b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt /* It would be nice to have proper location information. */ 40934b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt YYLTYPE loc; 40944b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt memset(&loc, 0, sizeof(loc)); 40954b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt 40964b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt foreach_list(node, instructions) { 40974b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt ir_variable *var = ((ir_instruction *)node)->as_variable(); 40984b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt 4099b2ee5a08bae6cdbbdafc1f1d9d6f3afbad2f7944Eric Anholt if (!var || !var->assigned) 41004b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt continue; 41014b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt 41024b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt if (strcmp(var->name, "gl_FragColor") == 0) 4103b2ee5a08bae6cdbbdafc1f1d9d6f3afbad2f7944Eric Anholt gl_FragColor_assigned = true; 41044b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt else if (strcmp(var->name, "gl_FragData") == 0) 4105b2ee5a08bae6cdbbdafc1f1d9d6f3afbad2f7944Eric Anholt gl_FragData_assigned = true; 41064b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt else if (strncmp(var->name, "gl_", 3) != 0) { 41074b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt if (state->target == fragment_shader && 41084b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt (var->mode == ir_var_out || var->mode == ir_var_inout)) { 41094b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt user_defined_fs_output_assigned = true; 41104b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt user_defined_fs_output = var; 41114b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt } 41124b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt } 41134b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt } 41144b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt 41154b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt /* From the GLSL 1.30 spec: 41164b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt * 41174b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt * "If a shader statically assigns a value to gl_FragColor, it 41184b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt * may not assign a value to any element of gl_FragData. If a 41194b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt * shader statically writes a value to any element of 41204b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt * gl_FragData, it may not assign a value to 41214b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt * gl_FragColor. That is, a shader may assign values to either 41224b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt * gl_FragColor or gl_FragData, but not both. Multiple shaders 41234b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt * linked together must also consistently write just one of 41244b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt * these variables. Similarly, if user declared output 41254b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt * variables are in use (statically assigned to), then the 41264b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt * built-in variables gl_FragColor and gl_FragData may not be 41274b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt * assigned to. These incorrect usages all generate compile 41284b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt * time errors." 41294b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt */ 41304b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt if (gl_FragColor_assigned && gl_FragData_assigned) { 41314b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt _mesa_glsl_error(&loc, state, "fragment shader writes to both " 41324b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt "`gl_FragColor' and `gl_FragData'\n"); 41334b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt } else if (gl_FragColor_assigned && user_defined_fs_output_assigned) { 41344b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt _mesa_glsl_error(&loc, state, "fragment shader writes to both " 41354b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt "`gl_FragColor' and `%s'\n", 41364b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt user_defined_fs_output->name); 41374b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt } else if (gl_FragData_assigned && user_defined_fs_output_assigned) { 41384b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt _mesa_glsl_error(&loc, state, "fragment shader writes to both " 41394b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt "`gl_FragData' and `%s'\n", 41404b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt user_defined_fs_output->name); 41414b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt } 41424b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt} 4143