ast_to_hir.cpp revision 81d664f099a5fd5fac777480532fb4307d591451
18d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/* 28d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Copyright © 2010 Intel Corporation 38d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * 48d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Permission is hereby granted, free of charge, to any person obtaining a 58d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * copy of this software and associated documentation files (the "Software"), 68d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * to deal in the Software without restriction, including without limitation 78d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * the rights to use, copy, modify, merge, publish, distribute, sublicense, 88d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * and/or sell copies of the Software, and to permit persons to whom the 98d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Software is furnished to do so, subject to the following conditions: 108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * 118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * The above copyright notice and this permission notice (including the next 128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * paragraph) shall be included in all copies or substantial portions of the 138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Software. 148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * 158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * DEALINGS IN THE SOFTWARE. 228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/** 258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * \file ast_to_hir.c 268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Convert abstract syntax to to high-level intermediate reprensentation (HIR). 278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * 288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * During the conversion to HIR, the majority of the symantic checking is 298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * preformed on the program. This includes: 308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * 318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * * Symbol table management 328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * * Type checking 338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * * Function binding 348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * 358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * The majority of this work could be done during parsing, and the parser could 368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * probably generate HIR directly. However, this results in frequent changes 378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * to the parser code. Since we do not assume that every system this complier 388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * is built on will have Flex and Bison installed, we have to store the code 398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * generated by these tools in our version control system. In other parts of 408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * the system we've seen problems where a parser was changed but the generated 418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * code was not committed, merge conflicts where created because two developers 428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * had slightly different versions of Bison installed, etc. 438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * 448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * I have also noticed that running Bison generated parsers in GDB is very 458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * irritating. When you get a segfault on '$$ = $1->foo', you can't very 468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * well 'print $1' in GDB. 478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * 488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * As a result, my preference is to put as little C code as possible in the 498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * parser (and lexer) sources. 508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "main/imports.h" 538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "glsl_symbol_table.h" 548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "glsl_parser_extras.h" 558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "ast.h" 568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "glsl_types.h" 578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "ir.h" 588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtvoid 608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt_mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state) 618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt _mesa_glsl_initialize_variables(instructions, state); 638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt _mesa_glsl_initialize_functions(instructions, state); 648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt state->current_function = NULL; 668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt foreach_list_typed (ast_node, ast, link, & state->translation_unit) 688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ast->hir(instructions, state); 698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/** 738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * If a conversion is available, convert one operand to a different type 748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * 758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * The \c from \c ir_rvalue is converted "in place". 768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * 778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * \param to Type that the operand it to be converted to 788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * \param from Operand that is being converted 798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * \param state GLSL compiler state 808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * 818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * \return 828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * If a conversion is possible (or unnecessary), \c true is returned. 838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Otherwise \c false is returned. 848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic bool 868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtapply_implicit_conversion(const glsl_type *to, ir_rvalue * &from, 878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt struct _mesa_glsl_parse_state *state) 888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt void *ctx = state; 908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (to->base_type == from->type->base_type) 918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return true; 928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* This conversion was added in GLSL 1.20. If the compilation mode is 948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * GLSL 1.10, the conversion is skipped. 958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (state->language_version < 120) 978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return false; 988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec: 1008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * 1018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * "There are no implicit array or structure conversions. For 1028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * example, an array of int cannot be implicitly converted to an 1038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * array of float. There are no implicit conversions between 1048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * signed and unsigned integers." 1058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 1068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* FINISHME: The above comment is partially a lie. There is int/uint 1078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * FINISHME: conversion for immediate constants. 1088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 1098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (!to->is_float() || !from->type->is_numeric()) 1108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return false; 1118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* Convert to a floating point type with the same number of components 1138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * as the original type - i.e. int to float, not int to vec4. 1148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 1158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt to = glsl_type::get_instance(GLSL_TYPE_FLOAT, from->type->vector_elements, 1168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt from->type->matrix_columns); 1178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt switch (from->type->base_type) { 1198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt case GLSL_TYPE_INT: 1208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt from = new(ctx) ir_expression(ir_unop_i2f, to, from, NULL); 1218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt break; 1228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt case GLSL_TYPE_UINT: 1238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt from = new(ctx) ir_expression(ir_unop_u2f, to, from, NULL); 1248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt break; 1258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt case GLSL_TYPE_BOOL: 1268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt from = new(ctx) ir_expression(ir_unop_b2f, to, from, NULL); 1278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt break; 1288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt default: 1298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt assert(0); 1308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 1318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return true; 1338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 1348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic const struct glsl_type * 1378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtarithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b, 1388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt bool multiply, 1398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt struct _mesa_glsl_parse_state *state, YYLTYPE *loc) 1408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 1418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt const glsl_type *type_a = value_a->type; 1428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt const glsl_type *type_b = value_b->type; 1438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* From GLSL 1.50 spec, page 56: 1458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * 1468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * "The arithmetic binary operators add (+), subtract (-), 1478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * multiply (*), and divide (/) operate on integer and 1488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * floating-point scalars, vectors, and matrices." 1498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 1508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (!type_a->is_numeric() || !type_b->is_numeric()) { 1518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt _mesa_glsl_error(loc, state, 1528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt "Operands to arithmetic operators must be numeric"); 1538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return glsl_type::error_type; 1548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 1558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* "If one operand is floating-point based and the other is 1588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * not, then the conversions from Section 4.1.10 "Implicit 1598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Conversions" are applied to the non-floating-point-based operand." 1608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 1618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (!apply_implicit_conversion(type_a, value_b, state) 1628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt && !apply_implicit_conversion(type_b, value_a, state)) { 1638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt _mesa_glsl_error(loc, state, 1648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt "Could not implicitly convert operands to " 1658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt "arithmetic operator"); 1668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return glsl_type::error_type; 1678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 1688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt type_a = value_a->type; 1698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt type_b = value_b->type; 1708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* "If the operands are integer types, they must both be signed or 1728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * both be unsigned." 1738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * 1748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * From this rule and the preceeding conversion it can be inferred that 1758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT. 1768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * The is_numeric check above already filtered out the case where either 1778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * type is not one of these, so now the base types need only be tested for 1788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * equality. 1798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 1808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (type_a->base_type != type_b->base_type) { 1818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt _mesa_glsl_error(loc, state, 1828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt "base type mismatch for arithmetic operator"); 1838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return glsl_type::error_type; 1848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 1858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* "All arithmetic binary operators result in the same fundamental type 1878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * (signed integer, unsigned integer, or floating-point) as the 1888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * operands they operate on, after operand type conversion. After 1898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * conversion, the following cases are valid 1908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * 1918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * * The two operands are scalars. In this case the operation is 1928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * applied, resulting in a scalar." 1938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 1948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (type_a->is_scalar() && type_b->is_scalar()) 1958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return type_a; 1968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 1978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* "* One operand is a scalar, and the other is a vector or matrix. 1988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * In this case, the scalar operation is applied independently to each 1998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * component of the vector or matrix, resulting in the same size 2008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * vector or matrix." 2018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 2028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (type_a->is_scalar()) { 2038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (!type_b->is_scalar()) 2048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return type_b; 2058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } else if (type_b->is_scalar()) { 2068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return type_a; 2078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 2088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* All of the combinations of <scalar, scalar>, <vector, scalar>, 2108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been 2118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * handled. 2128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 2138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt assert(!type_a->is_scalar()); 2148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt assert(!type_b->is_scalar()); 2158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* "* The two operands are vectors of the same size. In this case, the 2178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * operation is done component-wise resulting in the same size 2188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * vector." 2198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 2208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (type_a->is_vector() && type_b->is_vector()) { 2218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (type_a == type_b) { 2228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return type_a; 2238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } else { 2248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt _mesa_glsl_error(loc, state, 2258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt "vector size mismatch for arithmetic operator"); 2268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return glsl_type::error_type; 2278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 2288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 2298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* All of the combinations of <scalar, scalar>, <vector, scalar>, 2318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and 2328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * <vector, vector> have been handled. At least one of the operands must 2338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * be matrix. Further, since there are no integer matrix types, the base 2348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * type of both operands must be float. 2358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 2368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt assert(type_a->is_matrix() || type_b->is_matrix()); 2378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt assert(type_a->base_type == GLSL_TYPE_FLOAT); 2388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt assert(type_b->base_type == GLSL_TYPE_FLOAT); 2398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* "* The operator is add (+), subtract (-), or divide (/), and the 2418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * operands are matrices with the same number of rows and the same 2428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * number of columns. In this case, the operation is done component- 2438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wise resulting in the same size matrix." 2448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * * The operator is multiply (*), where both operands are matrices or 2458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * one operand is a vector and the other a matrix. A right vector 2468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * operand is treated as a column vector and a left vector operand as a 2478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * row vector. In all these cases, it is required that the number of 2488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * columns of the left operand is equal to the number of rows of the 2498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * right operand. Then, the multiply (*) operation does a linear 2508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * algebraic multiply, yielding an object that has the same number of 2518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * rows as the left operand and the same number of columns as the right 2528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * operand. Section 5.10 "Vector and Matrix Operations" explains in 2538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * more detail how vectors and matrices are operated on." 2548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 2558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (! multiply) { 2568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (type_a == type_b) 2578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return type_a; 2588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } else { 2598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (type_a->is_matrix() && type_b->is_matrix()) { 2608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* Matrix multiply. The columns of A must match the rows of B. Given 2618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * the other previously tested constraints, this means the vector type 2628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * of a row from A must be the same as the vector type of a column from 2638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * B. 2648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 2658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (type_a->row_type() == type_b->column_type()) { 2668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* The resulting matrix has the number of columns of matrix B and 2678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * the number of rows of matrix A. We get the row count of A by 2688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * looking at the size of a vector that makes up a column. The 2698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * transpose (size of a row) is done for B. 2708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 2718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt const glsl_type *const type = 2728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt glsl_type::get_instance(type_a->base_type, 2738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt type_a->column_type()->vector_elements, 2748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt type_b->row_type()->vector_elements); 2758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt assert(type != glsl_type::error_type); 2768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return type; 2788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 2798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } else if (type_a->is_matrix()) { 2808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* A is a matrix and B is a column vector. Columns of A must match 2818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * rows of B. Given the other previously tested constraints, this 2828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * means the vector type of a row from A must be the same as the 2838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * vector the type of B. 2848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 2858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (type_a->row_type() == type_b) 2868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return type_b; 2878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } else { 2888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt assert(type_b->is_matrix()); 2898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* A is a row vector and B is a matrix. Columns of A must match rows 2918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * of B. Given the other previously tested constraints, this means 2928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * the type of A must be the same as the vector type of a column from 2938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * B. 2948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 2958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (type_a == type_b->column_type()) 2968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return type_a; 2978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 2988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 2998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication"); 3008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return glsl_type::error_type; 3018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 3028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 3038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 3048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* "All other cases are illegal." 3058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 3068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt _mesa_glsl_error(loc, state, "type mismatch"); 3078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return glsl_type::error_type; 3088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 3098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 3108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 3118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic const struct glsl_type * 3128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtunary_arithmetic_result_type(const struct glsl_type *type, 3138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt struct _mesa_glsl_parse_state *state, YYLTYPE *loc) 3148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 3158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* From GLSL 1.50 spec, page 57: 3168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * 3178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * "The arithmetic unary operators negate (-), post- and pre-increment 3188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * and decrement (-- and ++) operate on integer or floating-point 3198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * values (including vectors and matrices). All unary operators work 3208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * component-wise on their operands. These result with the same type 3218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * they operated on." 3228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 3238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (!type->is_numeric()) { 3248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt _mesa_glsl_error(loc, state, 3258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt "Operands to arithmetic operators must be numeric"); 3268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return glsl_type::error_type; 3278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 3288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 3298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return type; 3308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 3318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 3328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 3338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic const struct glsl_type * 3348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtmodulus_result_type(const struct glsl_type *type_a, 3358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt const struct glsl_type *type_b, 3368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt struct _mesa_glsl_parse_state *state, YYLTYPE *loc) 3378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 3388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* From GLSL 1.50 spec, page 56: 3398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * "The operator modulus (%) operates on signed or unsigned integers or 3408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * integer vectors. The operand types must both be signed or both be 3418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * unsigned." 3428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 3438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (!type_a->is_integer() || !type_b->is_integer() 3448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt || (type_a->base_type != type_b->base_type)) { 3458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt _mesa_glsl_error(loc, state, "type mismatch"); 3468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return glsl_type::error_type; 3478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 3488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 3498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* "The operands cannot be vectors of differing size. If one operand is 3508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * a scalar and the other vector, then the scalar is applied component- 3518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * wise to the vector, resulting in the same type as the vector. If both 3528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * are vectors of the same size, the result is computed component-wise." 3538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 3548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (type_a->is_vector()) { 3558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (!type_b->is_vector() 3568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt || (type_a->vector_elements == type_b->vector_elements)) 3578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return type_a; 3588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } else 3598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return type_b; 3608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 3618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* "The operator modulus (%) is not defined for any other data types 3628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * (non-integer types)." 3638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 3648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt _mesa_glsl_error(loc, state, "type mismatch"); 3658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return glsl_type::error_type; 3668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 3678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 3688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 3698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic const struct glsl_type * 3708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtrelational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b, 3718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt struct _mesa_glsl_parse_state *state, YYLTYPE *loc) 3728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 3738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt const glsl_type *type_a = value_a->type; 3748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt const glsl_type *type_b = value_b->type; 3758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 3768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* From GLSL 1.50 spec, page 56: 3778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * "The relational operators greater than (>), less than (<), greater 3788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * than or equal (>=), and less than or equal (<=) operate only on 3798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * scalar integer and scalar floating-point expressions." 3808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 3818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (!type_a->is_numeric() 3828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt || !type_b->is_numeric() 3838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt || !type_a->is_scalar() 3848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt || !type_b->is_scalar()) { 3858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt _mesa_glsl_error(loc, state, 3868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt "Operands to relational operators must be scalar and " 3878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt "numeric"); 3888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return glsl_type::error_type; 3898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 3908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 3918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* "Either the operands' types must match, or the conversions from 3928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Section 4.1.10 "Implicit Conversions" will be applied to the integer 3938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * operand, after which the types must match." 3948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 3958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (!apply_implicit_conversion(type_a, value_b, state) 3968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt && !apply_implicit_conversion(type_b, value_a, state)) { 3978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt _mesa_glsl_error(loc, state, 3988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt "Could not implicitly convert operands to " 3998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt "relational operator"); 4008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return glsl_type::error_type; 4018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 4028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt type_a = value_a->type; 4038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt type_b = value_b->type; 4048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 4058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (type_a->base_type != type_b->base_type) { 4068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt _mesa_glsl_error(loc, state, "base type mismatch"); 4078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return glsl_type::error_type; 4088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 4098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 4108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* "The result is scalar Boolean." 4118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 4128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return glsl_type::bool_type; 4138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 4148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 4158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 4168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/** 4178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Validates that a value can be assigned to a location with a specified type 4188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * 4198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Validates that \c rhs can be assigned to some location. If the types are 4208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * not an exact match but an automatic conversion is possible, \c rhs will be 4218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * converted. 4228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * 4238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * \return 4248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type. 4258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Otherwise the actual RHS to be assigned will be returned. This may be 4268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * \c rhs, or it may be \c rhs after some type conversion. 4278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * 4288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * \note 4298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * In addition to being used for assignments, this function is used to 4308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * type-check return values. 4318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 4328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtir_rvalue * 4338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtvalidate_assignment(struct _mesa_glsl_parse_state *state, 4348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt const glsl_type *lhs_type, ir_rvalue *rhs) 4358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 4368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt const glsl_type *rhs_type = rhs->type; 4378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 4388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* If there is already some error in the RHS, just return it. Anything 4398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * else will lead to an avalanche of error message back to the user. 4408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 4418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (rhs_type->is_error()) 4428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return rhs; 4438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 4448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* If the types are identical, the assignment can trivially proceed. 4458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 4468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (rhs_type == lhs_type) 4478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return rhs; 4488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 4498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* If the array element types are the same and the size of the LHS is zero, 4508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * the assignment is okay. 4518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * 4528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Note: Whole-array assignments are not permitted in GLSL 1.10, but this 4538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * is handled by ir_dereference::is_lvalue. 4548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 4558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (lhs_type->is_array() && rhs->type->is_array() 4568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt && (lhs_type->element_type() == rhs->type->element_type()) 4578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt && (lhs_type->array_size() == 0)) { 4588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return rhs; 4598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 4608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 4618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* Check for implicit conversion in GLSL 1.20 */ 4628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (apply_implicit_conversion(lhs_type, rhs, state)) { 4638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt rhs_type = rhs->type; 4648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (rhs_type == lhs_type) 4658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return rhs; 4668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 4678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 4688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return NULL; 4698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 4708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 4718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtir_rvalue * 4728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtdo_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state, 4738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ir_rvalue *lhs, ir_rvalue *rhs, 4748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt YYLTYPE lhs_loc) 4758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 4768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt void *ctx = state; 4778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt bool error_emitted = (lhs->type->is_error() || rhs->type->is_error()); 4788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 4798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (!error_emitted) { 4808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */ 4818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (!lhs->is_lvalue()) { 4828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment"); 4838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt error_emitted = true; 4848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 4858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 4868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 4878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ir_rvalue *new_rhs = validate_assignment(state, lhs->type, rhs); 4888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (new_rhs == NULL) { 4898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt _mesa_glsl_error(& lhs_loc, state, "type mismatch"); 4908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } else { 4918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt rhs = new_rhs; 4928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 4938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* If the LHS array was not declared with a size, it takes it size from 4948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * the RHS. If the LHS is an l-value and a whole array, it must be a 4958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * dereference of a variable. Any other case would require that the LHS 4968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * is either not an l-value or not a whole array. 4978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 4988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (lhs->type->array_size() == 0) { 4998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ir_dereference *const d = lhs->as_dereference(); 5008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 5018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt assert(d != NULL); 5028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 5038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ir_variable *const var = d->variable_referenced(); 5048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 5058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt assert(var != NULL); 5068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 5078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt if (var->max_array_access >= unsigned(rhs->type->array_size())) { 5088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* FINISHME: This should actually log the location of the RHS. */ 5098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to " 5108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt "previous access", 5118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt var->max_array_access); 5128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 5138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 5148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt var->type = glsl_type::get_array_instance(state, 5158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt lhs->type->element_type(), 5168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt rhs->type->array_size()); 5178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 5188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt } 5198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 5208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* Most callers of do_assignment (assign, add_assign, pre_inc/dec, 5218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * but not post_inc) need the converted assigned value as an rvalue 5228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * to handle things like: 5238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * 5248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * i = j += 1; 5258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * 5268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * So we always just store the computed value being assigned to a 5278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * temporary and return a deref of that temporary. If the rvalue 5288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * ends up not being used, the temp will get copy-propagated out. 5298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 5308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp"); 5318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ir_dereference_variable *deref_var = new(ctx) ir_dereference_variable(var); 5328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt instructions->push_tail(var); 5338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt instructions->push_tail(new(ctx) ir_assignment(deref_var, 5348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt rhs, 5358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt NULL)); 5368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt deref_var = new(ctx) ir_dereference_variable(var); 5378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 5388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt instructions->push_tail(new(ctx) ir_assignment(lhs, 5398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt deref_var, 5408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt NULL)); 5418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 5428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return new(ctx) ir_dereference_variable(var); 5438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 5448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 5458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic ir_rvalue * 5468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtget_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue) 5478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 5488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt void *ctx = talloc_parent(lvalue); 5498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt ir_variable *var; 5508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 5518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* FINISHME: Give unique names to the temporaries. */ 5528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp"); 5538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt instructions->push_tail(var); 5548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt var->mode = ir_var_auto; 5558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 5568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var), 5578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt lvalue, NULL)); 5588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 5598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt /* Once we've created this temporary, mark it read only so it's no 5608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * longer considered an lvalue. 5618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */ 5628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt var->read_only = true; 5638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 5648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return new(ctx) ir_dereference_variable(var); 5658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 5668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 5678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 5688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtir_rvalue * 5698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtast_node::hir(exec_list *instructions, 5708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt struct _mesa_glsl_parse_state *state) 5718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 5728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt (void) instructions; 5738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt (void) state; 5748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 5758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt return NULL; 5768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt} 5778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 5788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt 5798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtir_rvalue * 5808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtast_expression::hir(exec_list *instructions, 5818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt struct _mesa_glsl_parse_state *state) 5828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{ 583 void *ctx = state; 584 static const int operations[AST_NUM_OPERATORS] = { 585 -1, /* ast_assign doesn't convert to ir_expression. */ 586 -1, /* ast_plus doesn't convert to ir_expression. */ 587 ir_unop_neg, 588 ir_binop_add, 589 ir_binop_sub, 590 ir_binop_mul, 591 ir_binop_div, 592 ir_binop_mod, 593 ir_binop_lshift, 594 ir_binop_rshift, 595 ir_binop_less, 596 ir_binop_greater, 597 ir_binop_lequal, 598 ir_binop_gequal, 599 ir_binop_equal, 600 ir_binop_nequal, 601 ir_binop_bit_and, 602 ir_binop_bit_xor, 603 ir_binop_bit_or, 604 ir_unop_bit_not, 605 ir_binop_logic_and, 606 ir_binop_logic_xor, 607 ir_binop_logic_or, 608 ir_unop_logic_not, 609 610 /* Note: The following block of expression types actually convert 611 * to multiple IR instructions. 612 */ 613 ir_binop_mul, /* ast_mul_assign */ 614 ir_binop_div, /* ast_div_assign */ 615 ir_binop_mod, /* ast_mod_assign */ 616 ir_binop_add, /* ast_add_assign */ 617 ir_binop_sub, /* ast_sub_assign */ 618 ir_binop_lshift, /* ast_ls_assign */ 619 ir_binop_rshift, /* ast_rs_assign */ 620 ir_binop_bit_and, /* ast_and_assign */ 621 ir_binop_bit_xor, /* ast_xor_assign */ 622 ir_binop_bit_or, /* ast_or_assign */ 623 624 -1, /* ast_conditional doesn't convert to ir_expression. */ 625 ir_binop_add, /* ast_pre_inc. */ 626 ir_binop_sub, /* ast_pre_dec. */ 627 ir_binop_add, /* ast_post_inc. */ 628 ir_binop_sub, /* ast_post_dec. */ 629 -1, /* ast_field_selection doesn't conv to ir_expression. */ 630 -1, /* ast_array_index doesn't convert to ir_expression. */ 631 -1, /* ast_function_call doesn't conv to ir_expression. */ 632 -1, /* ast_identifier doesn't convert to ir_expression. */ 633 -1, /* ast_int_constant doesn't convert to ir_expression. */ 634 -1, /* ast_uint_constant doesn't conv to ir_expression. */ 635 -1, /* ast_float_constant doesn't conv to ir_expression. */ 636 -1, /* ast_bool_constant doesn't conv to ir_expression. */ 637 -1, /* ast_sequence doesn't convert to ir_expression. */ 638 }; 639 ir_rvalue *result = NULL; 640 ir_rvalue *op[2]; 641 const struct glsl_type *type = glsl_type::error_type; 642 bool error_emitted = false; 643 YYLTYPE loc; 644 645 loc = this->get_location(); 646 647 switch (this->oper) { 648 case ast_assign: { 649 op[0] = this->subexpressions[0]->hir(instructions, state); 650 op[1] = this->subexpressions[1]->hir(instructions, state); 651 652 result = do_assignment(instructions, state, op[0], op[1], 653 this->subexpressions[0]->get_location()); 654 error_emitted = result->type->is_error(); 655 type = result->type; 656 break; 657 } 658 659 case ast_plus: 660 op[0] = this->subexpressions[0]->hir(instructions, state); 661 662 error_emitted = op[0]->type->is_error(); 663 if (type->is_error()) 664 op[0]->type = type; 665 666 result = op[0]; 667 break; 668 669 case ast_neg: 670 op[0] = this->subexpressions[0]->hir(instructions, state); 671 672 type = unary_arithmetic_result_type(op[0]->type, state, & loc); 673 674 error_emitted = type->is_error(); 675 676 result = new(ctx) ir_expression(operations[this->oper], type, 677 op[0], NULL); 678 break; 679 680 case ast_add: 681 case ast_sub: 682 case ast_mul: 683 case ast_div: 684 op[0] = this->subexpressions[0]->hir(instructions, state); 685 op[1] = this->subexpressions[1]->hir(instructions, state); 686 687 type = arithmetic_result_type(op[0], op[1], 688 (this->oper == ast_mul), 689 state, & loc); 690 error_emitted = type->is_error(); 691 692 result = new(ctx) ir_expression(operations[this->oper], type, 693 op[0], op[1]); 694 break; 695 696 case ast_mod: 697 op[0] = this->subexpressions[0]->hir(instructions, state); 698 op[1] = this->subexpressions[1]->hir(instructions, state); 699 700 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc); 701 702 assert(operations[this->oper] == ir_binop_mod); 703 704 result = new(ctx) ir_expression(operations[this->oper], type, 705 op[0], op[1]); 706 error_emitted = type->is_error(); 707 break; 708 709 case ast_lshift: 710 case ast_rshift: 711 _mesa_glsl_error(& loc, state, "FINISHME: implement bit-shift operators"); 712 error_emitted = true; 713 break; 714 715 case ast_less: 716 case ast_greater: 717 case ast_lequal: 718 case ast_gequal: 719 op[0] = this->subexpressions[0]->hir(instructions, state); 720 op[1] = this->subexpressions[1]->hir(instructions, state); 721 722 type = relational_result_type(op[0], op[1], state, & loc); 723 724 /* The relational operators must either generate an error or result 725 * in a scalar boolean. See page 57 of the GLSL 1.50 spec. 726 */ 727 assert(type->is_error() 728 || ((type->base_type == GLSL_TYPE_BOOL) 729 && type->is_scalar())); 730 731 result = new(ctx) ir_expression(operations[this->oper], type, 732 op[0], op[1]); 733 error_emitted = type->is_error(); 734 break; 735 736 case ast_nequal: 737 case ast_equal: 738 op[0] = this->subexpressions[0]->hir(instructions, state); 739 op[1] = this->subexpressions[1]->hir(instructions, state); 740 741 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec: 742 * 743 * "The equality operators equal (==), and not equal (!=) 744 * operate on all types. They result in a scalar Boolean. If 745 * the operand types do not match, then there must be a 746 * conversion from Section 4.1.10 "Implicit Conversions" 747 * applied to one operand that can make them match, in which 748 * case this conversion is done." 749 */ 750 if ((!apply_implicit_conversion(op[0]->type, op[1], state) 751 && !apply_implicit_conversion(op[1]->type, op[0], state)) 752 || (op[0]->type != op[1]->type)) { 753 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same " 754 "type", (this->oper == ast_equal) ? "==" : "!="); 755 error_emitted = true; 756 } else if ((state->language_version <= 110) 757 && (op[0]->type->is_array() || op[1]->type->is_array())) { 758 _mesa_glsl_error(& loc, state, "array comparisons forbidden in " 759 "GLSL 1.10"); 760 error_emitted = true; 761 } 762 763 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type, 764 op[0], op[1]); 765 type = glsl_type::bool_type; 766 767 assert(result->type == glsl_type::bool_type); 768 break; 769 770 case ast_bit_and: 771 case ast_bit_xor: 772 case ast_bit_or: 773 case ast_bit_not: 774 _mesa_glsl_error(& loc, state, "FINISHME: implement bit-wise operators"); 775 error_emitted = true; 776 break; 777 778 case ast_logic_and: { 779 op[0] = this->subexpressions[0]->hir(instructions, state); 780 781 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) { 782 YYLTYPE loc = this->subexpressions[0]->get_location(); 783 784 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean", 785 operator_string(this->oper)); 786 error_emitted = true; 787 } 788 789 ir_constant *op0_const = op[0]->constant_expression_value(); 790 if (op0_const) { 791 if (op0_const->value.b[0]) { 792 op[1] = this->subexpressions[1]->hir(instructions, state); 793 794 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) { 795 YYLTYPE loc = this->subexpressions[1]->get_location(); 796 797 _mesa_glsl_error(& loc, state, 798 "RHS of `%s' must be scalar boolean", 799 operator_string(this->oper)); 800 error_emitted = true; 801 } 802 result = op[1]; 803 } else { 804 result = op0_const; 805 } 806 type = glsl_type::bool_type; 807 } else { 808 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type, 809 "and_tmp"); 810 instructions->push_tail(tmp); 811 812 ir_if *const stmt = new(ctx) ir_if(op[0]); 813 instructions->push_tail(stmt); 814 815 op[1] = this->subexpressions[1]->hir(&stmt->then_instructions, state); 816 817 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) { 818 YYLTYPE loc = this->subexpressions[1]->get_location(); 819 820 _mesa_glsl_error(& loc, state, 821 "RHS of `%s' must be scalar boolean", 822 operator_string(this->oper)); 823 error_emitted = true; 824 } 825 826 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp); 827 ir_assignment *const then_assign = 828 new(ctx) ir_assignment(then_deref, op[1], NULL); 829 stmt->then_instructions.push_tail(then_assign); 830 831 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp); 832 ir_assignment *const else_assign = 833 new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false), NULL); 834 stmt->else_instructions.push_tail(else_assign); 835 836 result = new(ctx) ir_dereference_variable(tmp); 837 type = tmp->type; 838 } 839 break; 840 } 841 842 case ast_logic_or: { 843 op[0] = this->subexpressions[0]->hir(instructions, state); 844 845 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) { 846 YYLTYPE loc = this->subexpressions[0]->get_location(); 847 848 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean", 849 operator_string(this->oper)); 850 error_emitted = true; 851 } 852 853 ir_constant *op0_const = op[0]->constant_expression_value(); 854 if (op0_const) { 855 if (op0_const->value.b[0]) { 856 result = op0_const; 857 } else { 858 op[1] = this->subexpressions[1]->hir(instructions, state); 859 860 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) { 861 YYLTYPE loc = this->subexpressions[1]->get_location(); 862 863 _mesa_glsl_error(& loc, state, 864 "RHS of `%s' must be scalar boolean", 865 operator_string(this->oper)); 866 error_emitted = true; 867 } 868 result = op[1]; 869 } 870 type = glsl_type::bool_type; 871 } else { 872 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type, 873 "or_tmp"); 874 instructions->push_tail(tmp); 875 876 ir_if *const stmt = new(ctx) ir_if(op[0]); 877 instructions->push_tail(stmt); 878 879 op[1] = this->subexpressions[1]->hir(&stmt->then_instructions, state); 880 881 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) { 882 YYLTYPE loc = this->subexpressions[1]->get_location(); 883 884 _mesa_glsl_error(& loc, state, "RHS of `%s' must be scalar boolean", 885 operator_string(this->oper)); 886 error_emitted = true; 887 } 888 889 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp); 890 ir_assignment *const then_assign = 891 new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true), NULL); 892 stmt->then_instructions.push_tail(then_assign); 893 894 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp); 895 ir_assignment *const else_assign = 896 new(ctx) ir_assignment(else_deref, op[1], NULL); 897 stmt->else_instructions.push_tail(else_assign); 898 899 result = new(ctx) ir_dereference_variable(tmp); 900 type = tmp->type; 901 } 902 break; 903 } 904 905 case ast_logic_xor: 906 op[0] = this->subexpressions[0]->hir(instructions, state); 907 op[1] = this->subexpressions[1]->hir(instructions, state); 908 909 910 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type, 911 op[0], op[1]); 912 type = glsl_type::bool_type; 913 break; 914 915 case ast_logic_not: 916 op[0] = this->subexpressions[0]->hir(instructions, state); 917 918 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) { 919 YYLTYPE loc = this->subexpressions[0]->get_location(); 920 921 _mesa_glsl_error(& loc, state, 922 "operand of `!' must be scalar boolean"); 923 error_emitted = true; 924 } 925 926 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type, 927 op[0], NULL); 928 type = glsl_type::bool_type; 929 break; 930 931 case ast_mul_assign: 932 case ast_div_assign: 933 case ast_add_assign: 934 case ast_sub_assign: { 935 op[0] = this->subexpressions[0]->hir(instructions, state); 936 op[1] = this->subexpressions[1]->hir(instructions, state); 937 938 type = arithmetic_result_type(op[0], op[1], 939 (this->oper == ast_mul_assign), 940 state, & loc); 941 942 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type, 943 op[0], op[1]); 944 945 result = do_assignment(instructions, state, 946 op[0]->clone(NULL), temp_rhs, 947 this->subexpressions[0]->get_location()); 948 type = result->type; 949 error_emitted = (op[0]->type->is_error()); 950 951 /* GLSL 1.10 does not allow array assignment. However, we don't have to 952 * explicitly test for this because none of the binary expression 953 * operators allow array operands either. 954 */ 955 956 break; 957 } 958 959 case ast_mod_assign: { 960 op[0] = this->subexpressions[0]->hir(instructions, state); 961 op[1] = this->subexpressions[1]->hir(instructions, state); 962 963 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc); 964 965 assert(operations[this->oper] == ir_binop_mod); 966 967 struct ir_rvalue *temp_rhs; 968 temp_rhs = new(ctx) ir_expression(operations[this->oper], type, 969 op[0], op[1]); 970 971 result = do_assignment(instructions, state, 972 op[0]->clone(NULL), temp_rhs, 973 this->subexpressions[0]->get_location()); 974 type = result->type; 975 error_emitted = type->is_error(); 976 break; 977 } 978 979 case ast_ls_assign: 980 case ast_rs_assign: 981 _mesa_glsl_error(& loc, state, 982 "FINISHME: implement bit-shift assignment operators"); 983 error_emitted = true; 984 break; 985 986 case ast_and_assign: 987 case ast_xor_assign: 988 case ast_or_assign: 989 _mesa_glsl_error(& loc, state, 990 "FINISHME: implement logic assignment operators"); 991 error_emitted = true; 992 break; 993 994 case ast_conditional: { 995 op[0] = this->subexpressions[0]->hir(instructions, state); 996 997 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec: 998 * 999 * "The ternary selection operator (?:). It operates on three 1000 * expressions (exp1 ? exp2 : exp3). This operator evaluates the 1001 * first expression, which must result in a scalar Boolean." 1002 */ 1003 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) { 1004 YYLTYPE loc = this->subexpressions[0]->get_location(); 1005 1006 _mesa_glsl_error(& loc, state, "?: condition must be scalar boolean"); 1007 error_emitted = true; 1008 } 1009 1010 /* The :? operator is implemented by generating an anonymous temporary 1011 * followed by an if-statement. The last instruction in each branch of 1012 * the if-statement assigns a value to the anonymous temporary. This 1013 * temporary is the r-value of the expression. 1014 */ 1015 exec_list then_instructions; 1016 exec_list else_instructions; 1017 1018 op[1] = this->subexpressions[1]->hir(&then_instructions, state); 1019 op[2] = this->subexpressions[2]->hir(&else_instructions, state); 1020 1021 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec: 1022 * 1023 * "The second and third expressions can be any type, as 1024 * long their types match, or there is a conversion in 1025 * Section 4.1.10 "Implicit Conversions" that can be applied 1026 * to one of the expressions to make their types match. This 1027 * resulting matching type is the type of the entire 1028 * expression." 1029 */ 1030 if ((!apply_implicit_conversion(op[1]->type, op[2], state) 1031 && !apply_implicit_conversion(op[2]->type, op[1], state)) 1032 || (op[1]->type != op[2]->type)) { 1033 YYLTYPE loc = this->subexpressions[1]->get_location(); 1034 1035 _mesa_glsl_error(& loc, state, "Second and third operands of ?: " 1036 "operator must have matching types."); 1037 error_emitted = true; 1038 type = glsl_type::error_type; 1039 } else { 1040 type = op[1]->type; 1041 } 1042 1043 ir_constant *cond_val = op[0]->constant_expression_value(); 1044 ir_constant *then_val = op[1]->constant_expression_value(); 1045 ir_constant *else_val = op[2]->constant_expression_value(); 1046 1047 if (then_instructions.is_empty() 1048 && else_instructions.is_empty() 1049 && (cond_val != NULL) && (then_val != NULL) && (else_val != NULL)) { 1050 result = (cond_val->value.b[0]) ? then_val : else_val; 1051 } else { 1052 ir_variable *const tmp = new(ctx) ir_variable(type, "conditional_tmp"); 1053 instructions->push_tail(tmp); 1054 1055 ir_if *const stmt = new(ctx) ir_if(op[0]); 1056 instructions->push_tail(stmt); 1057 1058 then_instructions.move_nodes_to(& stmt->then_instructions); 1059 ir_dereference *const then_deref = 1060 new(ctx) ir_dereference_variable(tmp); 1061 ir_assignment *const then_assign = 1062 new(ctx) ir_assignment(then_deref, op[1], NULL); 1063 stmt->then_instructions.push_tail(then_assign); 1064 1065 else_instructions.move_nodes_to(& stmt->else_instructions); 1066 ir_dereference *const else_deref = 1067 new(ctx) ir_dereference_variable(tmp); 1068 ir_assignment *const else_assign = 1069 new(ctx) ir_assignment(else_deref, op[2], NULL); 1070 stmt->else_instructions.push_tail(else_assign); 1071 1072 result = new(ctx) ir_dereference_variable(tmp); 1073 } 1074 break; 1075 } 1076 1077 case ast_pre_inc: 1078 case ast_pre_dec: { 1079 op[0] = this->subexpressions[0]->hir(instructions, state); 1080 if (op[0]->type->base_type == GLSL_TYPE_FLOAT) 1081 op[1] = new(ctx) ir_constant(1.0f); 1082 else 1083 op[1] = new(ctx) ir_constant(1); 1084 1085 type = arithmetic_result_type(op[0], op[1], false, state, & loc); 1086 1087 struct ir_rvalue *temp_rhs; 1088 temp_rhs = new(ctx) ir_expression(operations[this->oper], type, 1089 op[0], op[1]); 1090 1091 result = do_assignment(instructions, state, 1092 op[0]->clone(NULL), temp_rhs, 1093 this->subexpressions[0]->get_location()); 1094 type = result->type; 1095 error_emitted = op[0]->type->is_error(); 1096 break; 1097 } 1098 1099 case ast_post_inc: 1100 case ast_post_dec: { 1101 op[0] = this->subexpressions[0]->hir(instructions, state); 1102 if (op[0]->type->base_type == GLSL_TYPE_FLOAT) 1103 op[1] = new(ctx) ir_constant(1.0f); 1104 else 1105 op[1] = new(ctx) ir_constant(1); 1106 1107 error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); 1108 1109 type = arithmetic_result_type(op[0], op[1], false, state, & loc); 1110 1111 struct ir_rvalue *temp_rhs; 1112 temp_rhs = new(ctx) ir_expression(operations[this->oper], type, 1113 op[0], op[1]); 1114 1115 /* Get a temporary of a copy of the lvalue before it's modified. 1116 * This may get thrown away later. 1117 */ 1118 result = get_lvalue_copy(instructions, op[0]->clone(NULL)); 1119 1120 (void)do_assignment(instructions, state, 1121 op[0]->clone(NULL), temp_rhs, 1122 this->subexpressions[0]->get_location()); 1123 1124 type = result->type; 1125 error_emitted = op[0]->type->is_error(); 1126 break; 1127 } 1128 1129 case ast_field_selection: 1130 result = _mesa_ast_field_selection_to_hir(this, instructions, state); 1131 type = result->type; 1132 break; 1133 1134 case ast_array_index: { 1135 YYLTYPE index_loc = subexpressions[1]->get_location(); 1136 1137 op[0] = subexpressions[0]->hir(instructions, state); 1138 op[1] = subexpressions[1]->hir(instructions, state); 1139 1140 error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); 1141 1142 ir_rvalue *const array = op[0]; 1143 1144 result = new(ctx) ir_dereference_array(op[0], op[1]); 1145 1146 /* Do not use op[0] after this point. Use array. 1147 */ 1148 op[0] = NULL; 1149 1150 1151 if (error_emitted) 1152 break; 1153 1154 if (!array->type->is_array() 1155 && !array->type->is_matrix() 1156 && !array->type->is_vector()) { 1157 _mesa_glsl_error(& index_loc, state, 1158 "cannot dereference non-array / non-matrix / " 1159 "non-vector"); 1160 error_emitted = true; 1161 } 1162 1163 if (!op[1]->type->is_integer()) { 1164 _mesa_glsl_error(& index_loc, state, 1165 "array index must be integer type"); 1166 error_emitted = true; 1167 } else if (!op[1]->type->is_scalar()) { 1168 _mesa_glsl_error(& index_loc, state, 1169 "array index must be scalar"); 1170 error_emitted = true; 1171 } 1172 1173 /* If the array index is a constant expression and the array has a 1174 * declared size, ensure that the access is in-bounds. If the array 1175 * index is not a constant expression, ensure that the array has a 1176 * declared size. 1177 */ 1178 ir_constant *const const_index = op[1]->constant_expression_value(); 1179 if (const_index != NULL) { 1180 const int idx = const_index->value.i[0]; 1181 const char *type_name; 1182 unsigned bound = 0; 1183 1184 if (array->type->is_matrix()) { 1185 type_name = "matrix"; 1186 } else if (array->type->is_vector()) { 1187 type_name = "vector"; 1188 } else { 1189 type_name = "array"; 1190 } 1191 1192 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec: 1193 * 1194 * "It is illegal to declare an array with a size, and then 1195 * later (in the same shader) index the same array with an 1196 * integral constant expression greater than or equal to the 1197 * declared size. It is also illegal to index an array with a 1198 * negative constant expression." 1199 */ 1200 if (array->type->is_matrix()) { 1201 if (array->type->row_type()->vector_elements <= idx) { 1202 bound = array->type->row_type()->vector_elements; 1203 } 1204 } else if (array->type->is_vector()) { 1205 if (array->type->vector_elements <= idx) { 1206 bound = array->type->vector_elements; 1207 } 1208 } else { 1209 if ((array->type->array_size() > 0) 1210 && (array->type->array_size() <= idx)) { 1211 bound = array->type->array_size(); 1212 } 1213 } 1214 1215 if (bound > 0) { 1216 _mesa_glsl_error(& loc, state, "%s index must be < %u", 1217 type_name, bound); 1218 error_emitted = true; 1219 } else if (idx < 0) { 1220 _mesa_glsl_error(& loc, state, "%s index must be >= 0", 1221 type_name); 1222 error_emitted = true; 1223 } 1224 1225 if (array->type->is_array()) { 1226 /* If the array is a variable dereference, it dereferences the 1227 * whole array, by definition. Use this to get the variable. 1228 * 1229 * FINISHME: Should some methods for getting / setting / testing 1230 * FINISHME: array access limits be added to ir_dereference? 1231 */ 1232 ir_variable *const v = array->whole_variable_referenced(); 1233 if ((v != NULL) && (unsigned(idx) > v->max_array_access)) 1234 v->max_array_access = idx; 1235 } 1236 } 1237 1238 if (error_emitted) 1239 result->type = glsl_type::error_type; 1240 1241 type = result->type; 1242 break; 1243 } 1244 1245 case ast_function_call: 1246 /* Should *NEVER* get here. ast_function_call should always be handled 1247 * by ast_function_expression::hir. 1248 */ 1249 assert(0); 1250 break; 1251 1252 case ast_identifier: { 1253 /* ast_identifier can appear several places in a full abstract syntax 1254 * tree. This particular use must be at location specified in the grammar 1255 * as 'variable_identifier'. 1256 */ 1257 ir_variable *var = 1258 state->symbols->get_variable(this->primary_expression.identifier); 1259 1260 result = new(ctx) ir_dereference_variable(var); 1261 1262 if (var != NULL) { 1263 type = result->type; 1264 } else { 1265 _mesa_glsl_error(& loc, state, "`%s' undeclared", 1266 this->primary_expression.identifier); 1267 1268 error_emitted = true; 1269 } 1270 break; 1271 } 1272 1273 case ast_int_constant: 1274 type = glsl_type::int_type; 1275 result = new(ctx) ir_constant(this->primary_expression.int_constant); 1276 break; 1277 1278 case ast_uint_constant: 1279 type = glsl_type::uint_type; 1280 result = new(ctx) ir_constant(this->primary_expression.uint_constant); 1281 break; 1282 1283 case ast_float_constant: 1284 type = glsl_type::float_type; 1285 result = new(ctx) ir_constant(this->primary_expression.float_constant); 1286 break; 1287 1288 case ast_bool_constant: 1289 type = glsl_type::bool_type; 1290 result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant)); 1291 break; 1292 1293 case ast_sequence: { 1294 /* It should not be possible to generate a sequence in the AST without 1295 * any expressions in it. 1296 */ 1297 assert(!this->expressions.is_empty()); 1298 1299 /* The r-value of a sequence is the last expression in the sequence. If 1300 * the other expressions in the sequence do not have side-effects (and 1301 * therefore add instructions to the instruction list), they get dropped 1302 * on the floor. 1303 */ 1304 foreach_list_typed (ast_node, ast, link, &this->expressions) 1305 result = ast->hir(instructions, state); 1306 1307 type = result->type; 1308 1309 /* Any errors should have already been emitted in the loop above. 1310 */ 1311 error_emitted = true; 1312 break; 1313 } 1314 } 1315 1316 if (type->is_error() && !error_emitted) 1317 _mesa_glsl_error(& loc, state, "type mismatch"); 1318 1319 return result; 1320} 1321 1322 1323ir_rvalue * 1324ast_expression_statement::hir(exec_list *instructions, 1325 struct _mesa_glsl_parse_state *state) 1326{ 1327 /* It is possible to have expression statements that don't have an 1328 * expression. This is the solitary semicolon: 1329 * 1330 * for (i = 0; i < 5; i++) 1331 * ; 1332 * 1333 * In this case the expression will be NULL. Test for NULL and don't do 1334 * anything in that case. 1335 */ 1336 if (expression != NULL) 1337 expression->hir(instructions, state); 1338 1339 /* Statements do not have r-values. 1340 */ 1341 return NULL; 1342} 1343 1344 1345ir_rvalue * 1346ast_compound_statement::hir(exec_list *instructions, 1347 struct _mesa_glsl_parse_state *state) 1348{ 1349 if (new_scope) 1350 state->symbols->push_scope(); 1351 1352 foreach_list_typed (ast_node, ast, link, &this->statements) 1353 ast->hir(instructions, state); 1354 1355 if (new_scope) 1356 state->symbols->pop_scope(); 1357 1358 /* Compound statements do not have r-values. 1359 */ 1360 return NULL; 1361} 1362 1363 1364static const glsl_type * 1365process_array_type(const glsl_type *base, ast_node *array_size, 1366 struct _mesa_glsl_parse_state *state) 1367{ 1368 unsigned length = 0; 1369 1370 /* FINISHME: Reject delcarations of multidimensional arrays. */ 1371 1372 if (array_size != NULL) { 1373 exec_list dummy_instructions; 1374 ir_rvalue *const ir = array_size->hir(& dummy_instructions, state); 1375 YYLTYPE loc = array_size->get_location(); 1376 1377 /* FINISHME: Verify that the grammar forbids side-effects in array 1378 * FINISHME: sizes. i.e., 'vec4 [x = 12] data' 1379 */ 1380 assert(dummy_instructions.is_empty()); 1381 1382 if (ir != NULL) { 1383 if (!ir->type->is_integer()) { 1384 _mesa_glsl_error(& loc, state, "array size must be integer type"); 1385 } else if (!ir->type->is_scalar()) { 1386 _mesa_glsl_error(& loc, state, "array size must be scalar type"); 1387 } else { 1388 ir_constant *const size = ir->constant_expression_value(); 1389 1390 if (size == NULL) { 1391 _mesa_glsl_error(& loc, state, "array size must be a " 1392 "constant valued expression"); 1393 } else if (size->value.i[0] <= 0) { 1394 _mesa_glsl_error(& loc, state, "array size must be > 0"); 1395 } else { 1396 assert(size->type == ir->type); 1397 length = size->value.u[0]; 1398 } 1399 } 1400 } 1401 } 1402 1403 return glsl_type::get_array_instance(state, base, length); 1404} 1405 1406 1407const glsl_type * 1408ast_type_specifier::glsl_type(const char **name, 1409 struct _mesa_glsl_parse_state *state) const 1410{ 1411 const struct glsl_type *type; 1412 1413 if ((this->type_specifier == ast_struct) && (this->type_name == NULL)) { 1414 /* FINISHME: Handle annonymous structures. */ 1415 type = NULL; 1416 } else { 1417 type = state->symbols->get_type(this->type_name); 1418 *name = this->type_name; 1419 1420 if (this->is_array) { 1421 type = process_array_type(type, this->array_size, state); 1422 } 1423 } 1424 1425 return type; 1426} 1427 1428 1429static void 1430apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual, 1431 struct ir_variable *var, 1432 struct _mesa_glsl_parse_state *state, 1433 YYLTYPE *loc) 1434{ 1435 if (qual->invariant) 1436 var->invariant = 1; 1437 1438 /* FINISHME: Mark 'in' variables at global scope as read-only. */ 1439 if (qual->constant || qual->attribute || qual->uniform 1440 || (qual->varying && (state->target == fragment_shader))) 1441 var->read_only = 1; 1442 1443 if (qual->centroid) 1444 var->centroid = 1; 1445 1446 if (qual->attribute && state->target != vertex_shader) { 1447 var->type = glsl_type::error_type; 1448 _mesa_glsl_error(loc, state, 1449 "`attribute' variables may not be declared in the " 1450 "%s shader", 1451 _mesa_glsl_shader_target_name(state->target)); 1452 } 1453 1454 /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec: 1455 * 1456 * "The varying qualifier can be used only with the data types 1457 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of 1458 * these." 1459 */ 1460 if (qual->varying) { 1461 const glsl_type *non_array_type; 1462 1463 if (var->type && var->type->is_array()) 1464 non_array_type = var->type->fields.array; 1465 else 1466 non_array_type = var->type; 1467 1468 if (non_array_type && non_array_type->base_type != GLSL_TYPE_FLOAT) { 1469 var->type = glsl_type::error_type; 1470 _mesa_glsl_error(loc, state, 1471 "varying variables must be of base type float"); 1472 } 1473 } 1474 1475 if (qual->in && qual->out) 1476 var->mode = ir_var_inout; 1477 else if (qual->attribute || qual->in 1478 || (qual->varying && (state->target == fragment_shader))) 1479 var->mode = ir_var_in; 1480 else if (qual->out || (qual->varying && (state->target == vertex_shader))) 1481 var->mode = ir_var_out; 1482 else if (qual->uniform) 1483 var->mode = ir_var_uniform; 1484 else 1485 var->mode = ir_var_auto; 1486 1487 if (qual->uniform) 1488 var->shader_in = true; 1489 1490 /* Any 'in' or 'inout' variables at global scope must be marked as being 1491 * shader inputs. Likewise, any 'out' or 'inout' variables at global scope 1492 * must be marked as being shader outputs. 1493 */ 1494 if (state->current_function == NULL) { 1495 switch (var->mode) { 1496 case ir_var_in: 1497 case ir_var_uniform: 1498 var->shader_in = true; 1499 break; 1500 case ir_var_out: 1501 var->shader_out = true; 1502 break; 1503 case ir_var_inout: 1504 var->shader_in = true; 1505 var->shader_out = true; 1506 break; 1507 default: 1508 break; 1509 } 1510 } 1511 1512 if (qual->flat) 1513 var->interpolation = ir_var_flat; 1514 else if (qual->noperspective) 1515 var->interpolation = ir_var_noperspective; 1516 else 1517 var->interpolation = ir_var_smooth; 1518 1519 if (var->type->is_array() && (state->language_version >= 120)) { 1520 var->array_lvalue = true; 1521 } 1522} 1523 1524 1525ir_rvalue * 1526ast_declarator_list::hir(exec_list *instructions, 1527 struct _mesa_glsl_parse_state *state) 1528{ 1529 void *ctx = state; 1530 const struct glsl_type *decl_type; 1531 const char *type_name = NULL; 1532 ir_rvalue *result = NULL; 1533 YYLTYPE loc = this->get_location(); 1534 1535 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec: 1536 * 1537 * "To ensure that a particular output variable is invariant, it is 1538 * necessary to use the invariant qualifier. It can either be used to 1539 * qualify a previously declared variable as being invariant 1540 * 1541 * invariant gl_Position; // make existing gl_Position be invariant" 1542 * 1543 * In these cases the parser will set the 'invariant' flag in the declarator 1544 * list, and the type will be NULL. 1545 */ 1546 if (this->invariant) { 1547 assert(this->type == NULL); 1548 1549 if (state->current_function != NULL) { 1550 _mesa_glsl_error(& loc, state, 1551 "All uses of `invariant' keyword must be at global " 1552 "scope\n"); 1553 } 1554 1555 foreach_list_typed (ast_declaration, decl, link, &this->declarations) { 1556 assert(!decl->is_array); 1557 assert(decl->array_size == NULL); 1558 assert(decl->initializer == NULL); 1559 1560 ir_variable *const earlier = 1561 state->symbols->get_variable(decl->identifier); 1562 if (earlier == NULL) { 1563 _mesa_glsl_error(& loc, state, 1564 "Undeclared variable `%s' cannot be marked " 1565 "invariant\n", decl->identifier); 1566 } else if ((state->target == vertex_shader) 1567 && (earlier->mode != ir_var_out)) { 1568 _mesa_glsl_error(& loc, state, 1569 "`%s' cannot be marked invariant, vertex shader " 1570 "outputs only\n", decl->identifier); 1571 } else if ((state->target == fragment_shader) 1572 && (earlier->mode != ir_var_in)) { 1573 _mesa_glsl_error(& loc, state, 1574 "`%s' cannot be marked invariant, fragment shader " 1575 "inputs only\n", decl->identifier); 1576 } else { 1577 earlier->invariant = true; 1578 } 1579 } 1580 1581 /* Invariant redeclarations do not have r-values. 1582 */ 1583 return NULL; 1584 } 1585 1586 assert(this->type != NULL); 1587 assert(!this->invariant); 1588 1589 /* The type specifier may contain a structure definition. Process that 1590 * before any of the variable declarations. 1591 */ 1592 (void) this->type->specifier->hir(instructions, state); 1593 1594 decl_type = this->type->specifier->glsl_type(& type_name, state); 1595 if (this->declarations.is_empty()) { 1596 /* The only valid case where the declaration list can be empty is when 1597 * the declaration is setting the default precision of a built-in type 1598 * (e.g., 'precision highp vec4;'). 1599 */ 1600 1601 if (decl_type != NULL) { 1602 } else { 1603 _mesa_glsl_error(& loc, state, "incomplete declaration"); 1604 } 1605 } 1606 1607 foreach_list_typed (ast_declaration, decl, link, &this->declarations) { 1608 const struct glsl_type *var_type; 1609 struct ir_variable *var; 1610 1611 /* FINISHME: Emit a warning if a variable declaration shadows a 1612 * FINISHME: declaration at a higher scope. 1613 */ 1614 1615 if ((decl_type == NULL) || decl_type->is_void()) { 1616 if (type_name != NULL) { 1617 _mesa_glsl_error(& loc, state, 1618 "invalid type `%s' in declaration of `%s'", 1619 type_name, decl->identifier); 1620 } else { 1621 _mesa_glsl_error(& loc, state, 1622 "invalid type in declaration of `%s'", 1623 decl->identifier); 1624 } 1625 continue; 1626 } 1627 1628 if (decl->is_array) { 1629 var_type = process_array_type(decl_type, decl->array_size, state); 1630 } else { 1631 var_type = decl_type; 1632 } 1633 1634 var = new(ctx) ir_variable(var_type, decl->identifier); 1635 1636 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification; 1637 * 1638 * "Global variables can only use the qualifiers const, 1639 * attribute, uni form, or varying. Only one may be 1640 * specified. 1641 * 1642 * Local variables can only use the qualifier const." 1643 * 1644 * This is relaxed in GLSL 1.30. 1645 */ 1646 if (state->language_version < 120) { 1647 if (this->type->qualifier.out) { 1648 _mesa_glsl_error(& loc, state, 1649 "`out' qualifier in declaration of `%s' " 1650 "only valid for function parameters in GLSL 1.10.", 1651 decl->identifier); 1652 } 1653 if (this->type->qualifier.in) { 1654 _mesa_glsl_error(& loc, state, 1655 "`in' qualifier in declaration of `%s' " 1656 "only valid for function parameters in GLSL 1.10.", 1657 decl->identifier); 1658 } 1659 /* FINISHME: Test for other invalid qualifiers. */ 1660 } 1661 1662 apply_type_qualifier_to_variable(& this->type->qualifier, var, state, 1663 & loc); 1664 1665 if (this->type->qualifier.invariant) { 1666 if ((state->target == vertex_shader) && !var->shader_out) { 1667 _mesa_glsl_error(& loc, state, 1668 "`%s' cannot be marked invariant, vertex shader " 1669 "outputs only\n", var->name); 1670 } else if ((state->target == fragment_shader) && !var->shader_in) { 1671 _mesa_glsl_error(& loc, state, 1672 "`%s' cannot be marked invariant, fragment shader " 1673 "inputs only\n", var->name); 1674 } 1675 } 1676 1677 if (state->current_function != NULL) { 1678 const char *mode = NULL; 1679 const char *extra = ""; 1680 1681 /* There is no need to check for 'inout' here because the parser will 1682 * only allow that in function parameter lists. 1683 */ 1684 if (this->type->qualifier.attribute) { 1685 mode = "attribute"; 1686 } else if (this->type->qualifier.uniform) { 1687 mode = "uniform"; 1688 } else if (this->type->qualifier.varying) { 1689 mode = "varying"; 1690 } else if (this->type->qualifier.in) { 1691 mode = "in"; 1692 extra = " or in function parameter list"; 1693 } else if (this->type->qualifier.out) { 1694 mode = "out"; 1695 extra = " or in function parameter list"; 1696 } 1697 1698 if (mode) { 1699 _mesa_glsl_error(& loc, state, 1700 "%s variable `%s' must be declared at " 1701 "global scope%s", 1702 mode, var->name, extra); 1703 } 1704 } else if (var->mode == ir_var_in) { 1705 if (state->target == vertex_shader) { 1706 bool error_emitted = false; 1707 1708 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec: 1709 * 1710 * "Vertex shader inputs can only be float, floating-point 1711 * vectors, matrices, signed and unsigned integers and integer 1712 * vectors. Vertex shader inputs can also form arrays of these 1713 * types, but not structures." 1714 * 1715 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec: 1716 * 1717 * "Vertex shader inputs can only be float, floating-point 1718 * vectors, matrices, signed and unsigned integers and integer 1719 * vectors. They cannot be arrays or structures." 1720 * 1721 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec: 1722 * 1723 * "The attribute qualifier can be used only with float, 1724 * floating-point vectors, and matrices. Attribute variables 1725 * cannot be declared as arrays or structures." 1726 */ 1727 const glsl_type *check_type = var->type->is_array() 1728 ? var->type->fields.array : var->type; 1729 1730 switch (check_type->base_type) { 1731 case GLSL_TYPE_FLOAT: 1732 break; 1733 case GLSL_TYPE_UINT: 1734 case GLSL_TYPE_INT: 1735 if (state->language_version > 120) 1736 break; 1737 /* FALLTHROUGH */ 1738 default: 1739 _mesa_glsl_error(& loc, state, 1740 "vertex shader input / attribute cannot have " 1741 "type %s`%s'", 1742 var->type->is_array() ? "array of " : "", 1743 check_type->name); 1744 error_emitted = true; 1745 } 1746 1747 if (!error_emitted && (state->language_version <= 130) 1748 && var->type->is_array()) { 1749 _mesa_glsl_error(& loc, state, 1750 "vertex shader input / attribute cannot have " 1751 "array type"); 1752 error_emitted = true; 1753 } 1754 } 1755 } 1756 1757 /* Process the initializer and add its instructions to a temporary 1758 * list. This list will be added to the instruction stream (below) after 1759 * the declaration is added. This is done because in some cases (such as 1760 * redeclarations) the declaration may not actually be added to the 1761 * instruction stream. 1762 */ 1763 exec_list intializer_instructions; 1764 if (decl->initializer != NULL) { 1765 YYLTYPE initializer_loc = decl->initializer->get_location(); 1766 1767 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec: 1768 * 1769 * "All uniform variables are read-only and are initialized either 1770 * directly by an application via API commands, or indirectly by 1771 * OpenGL." 1772 */ 1773 if ((state->language_version <= 110) 1774 && (var->mode == ir_var_uniform)) { 1775 _mesa_glsl_error(& initializer_loc, state, 1776 "cannot initialize uniforms in GLSL 1.10"); 1777 } 1778 1779 if (var->type->is_sampler()) { 1780 _mesa_glsl_error(& initializer_loc, state, 1781 "cannot initialize samplers"); 1782 } 1783 1784 if ((var->mode == ir_var_in) && (state->current_function == NULL)) { 1785 _mesa_glsl_error(& initializer_loc, state, 1786 "cannot initialize %s shader input / %s", 1787 _mesa_glsl_shader_target_name(state->target), 1788 (state->target == vertex_shader) 1789 ? "attribute" : "varying"); 1790 } 1791 1792 ir_dereference *const lhs = new(ctx) ir_dereference_variable(var); 1793 ir_rvalue *rhs = decl->initializer->hir(&intializer_instructions, 1794 state); 1795 1796 /* Calculate the constant value if this is a const or uniform 1797 * declaration. 1798 */ 1799 if (this->type->qualifier.constant || this->type->qualifier.uniform) { 1800 ir_constant *constant_value = rhs->constant_expression_value(); 1801 if (!constant_value) { 1802 _mesa_glsl_error(& initializer_loc, state, 1803 "initializer of %s variable `%s' must be a " 1804 "constant expression", 1805 (this->type->qualifier.constant) 1806 ? "const" : "uniform", 1807 decl->identifier); 1808 } else { 1809 rhs = constant_value; 1810 var->constant_value = constant_value; 1811 } 1812 } 1813 1814 if (rhs && !rhs->type->is_error()) { 1815 bool temp = var->read_only; 1816 if (this->type->qualifier.constant) 1817 var->read_only = false; 1818 1819 /* Never emit code to initialize a uniform. 1820 */ 1821 if (!this->type->qualifier.uniform) 1822 result = do_assignment(&intializer_instructions, state, lhs, rhs, 1823 this->get_location()); 1824 var->read_only = temp; 1825 } 1826 } 1827 1828 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec: 1829 * 1830 * "It is an error to write to a const variable outside of 1831 * its declaration, so they must be initialized when 1832 * declared." 1833 */ 1834 if (this->type->qualifier.constant && decl->initializer == NULL) { 1835 _mesa_glsl_error(& loc, state, 1836 "const declaration of `%s' must be initialized"); 1837 } 1838 1839 /* Attempt to add the variable to the symbol table. If this fails, it 1840 * means the variable has already been declared at this scope. Arrays 1841 * fudge this rule a little bit. 1842 * 1843 * From page 24 (page 30 of the PDF) of the GLSL 1.50 spec, 1844 * 1845 * "It is legal to declare an array without a size and then 1846 * later re-declare the same name as an array of the same 1847 * type and specify a size." 1848 */ 1849 if (state->symbols->name_declared_this_scope(decl->identifier)) { 1850 ir_variable *const earlier = 1851 state->symbols->get_variable(decl->identifier); 1852 1853 if ((earlier != NULL) 1854 && (earlier->type->array_size() == 0) 1855 && var->type->is_array() 1856 && (var->type->element_type() == earlier->type->element_type())) { 1857 /* FINISHME: This doesn't match the qualifiers on the two 1858 * FINISHME: declarations. It's not 100% clear whether this is 1859 * FINISHME: required or not. 1860 */ 1861 1862 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec: 1863 * 1864 * "The size [of gl_TexCoord] can be at most 1865 * gl_MaxTextureCoords." 1866 */ 1867 const unsigned size = unsigned(var->type->array_size()); 1868 if ((strcmp("gl_TexCoord", var->name) == 0) 1869 && (size > state->Const.MaxTextureCoords)) { 1870 YYLTYPE loc = this->get_location(); 1871 1872 _mesa_glsl_error(& loc, state, "`gl_TexCoord' array size cannot " 1873 "be larger than gl_MaxTextureCoords (%u)\n", 1874 state->Const.MaxTextureCoords); 1875 } else if ((size > 0) && (size <= earlier->max_array_access)) { 1876 YYLTYPE loc = this->get_location(); 1877 1878 _mesa_glsl_error(& loc, state, "array size must be > %u due to " 1879 "previous access", 1880 earlier->max_array_access); 1881 } 1882 1883 earlier->type = var->type; 1884 delete var; 1885 var = NULL; 1886 } else { 1887 YYLTYPE loc = this->get_location(); 1888 1889 _mesa_glsl_error(& loc, state, "`%s' redeclared", 1890 decl->identifier); 1891 } 1892 1893 continue; 1894 } 1895 1896 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec, 1897 * 1898 * "Identifiers starting with "gl_" are reserved for use by 1899 * OpenGL, and may not be declared in a shader as either a 1900 * variable or a function." 1901 */ 1902 if (strncmp(decl->identifier, "gl_", 3) == 0) { 1903 /* FINISHME: This should only trigger if we're not redefining 1904 * FINISHME: a builtin (to add a qualifier, for example). 1905 */ 1906 _mesa_glsl_error(& loc, state, 1907 "identifier `%s' uses reserved `gl_' prefix", 1908 decl->identifier); 1909 } 1910 1911 instructions->push_tail(var); 1912 instructions->append_list(&intializer_instructions); 1913 1914 /* Add the variable to the symbol table after processing the initializer. 1915 * This differs from most C-like languages, but it follows the GLSL 1916 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50 1917 * spec: 1918 * 1919 * "Within a declaration, the scope of a name starts immediately 1920 * after the initializer if present or immediately after the name 1921 * being declared if not." 1922 */ 1923 const bool added_variable = 1924 state->symbols->add_variable(var->name, var); 1925 assert(added_variable); 1926 } 1927 1928 1929 /* Generally, variable declarations do not have r-values. However, 1930 * one is used for the declaration in 1931 * 1932 * while (bool b = some_condition()) { 1933 * ... 1934 * } 1935 * 1936 * so we return the rvalue from the last seen declaration here. 1937 */ 1938 return result; 1939} 1940 1941 1942ir_rvalue * 1943ast_parameter_declarator::hir(exec_list *instructions, 1944 struct _mesa_glsl_parse_state *state) 1945{ 1946 void *ctx = state; 1947 const struct glsl_type *type; 1948 const char *name = NULL; 1949 YYLTYPE loc = this->get_location(); 1950 1951 type = this->type->specifier->glsl_type(& name, state); 1952 1953 if (type == NULL) { 1954 if (name != NULL) { 1955 _mesa_glsl_error(& loc, state, 1956 "invalid type `%s' in declaration of `%s'", 1957 name, this->identifier); 1958 } else { 1959 _mesa_glsl_error(& loc, state, 1960 "invalid type in declaration of `%s'", 1961 this->identifier); 1962 } 1963 1964 type = glsl_type::error_type; 1965 } 1966 1967 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec: 1968 * 1969 * "Functions that accept no input arguments need not use void in the 1970 * argument list because prototypes (or definitions) are required and 1971 * therefore there is no ambiguity when an empty argument list "( )" is 1972 * declared. The idiom "(void)" as a parameter list is provided for 1973 * convenience." 1974 * 1975 * Placing this check here prevents a void parameter being set up 1976 * for a function, which avoids tripping up checks for main taking 1977 * parameters and lookups of an unnamed symbol. 1978 */ 1979 if (type->is_void()) { 1980 if (this->identifier != NULL) 1981 _mesa_glsl_error(& loc, state, 1982 "named parameter cannot have type `void'"); 1983 1984 is_void = true; 1985 return NULL; 1986 } 1987 1988 if (formal_parameter && (this->identifier == NULL)) { 1989 _mesa_glsl_error(& loc, state, "formal parameter lacks a name"); 1990 return NULL; 1991 } 1992 1993 is_void = false; 1994 ir_variable *var = new(ctx) ir_variable(type, this->identifier); 1995 1996 /* FINISHME: Handle array declarations. Note that this requires 1997 * FINISHME: complete handling of constant expressions. 1998 */ 1999 2000 /* Apply any specified qualifiers to the parameter declaration. Note that 2001 * for function parameters the default mode is 'in'. 2002 */ 2003 apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc); 2004 if (var->mode == ir_var_auto) 2005 var->mode = ir_var_in; 2006 2007 instructions->push_tail(var); 2008 2009 /* Parameter declarations do not have r-values. 2010 */ 2011 return NULL; 2012} 2013 2014 2015void 2016ast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters, 2017 bool formal, 2018 exec_list *ir_parameters, 2019 _mesa_glsl_parse_state *state) 2020{ 2021 ast_parameter_declarator *void_param = NULL; 2022 unsigned count = 0; 2023 2024 foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) { 2025 param->formal_parameter = formal; 2026 param->hir(ir_parameters, state); 2027 2028 if (param->is_void) 2029 void_param = param; 2030 2031 count++; 2032 } 2033 2034 if ((void_param != NULL) && (count > 1)) { 2035 YYLTYPE loc = void_param->get_location(); 2036 2037 _mesa_glsl_error(& loc, state, 2038 "`void' parameter must be only parameter"); 2039 } 2040} 2041 2042 2043ir_rvalue * 2044ast_function::hir(exec_list *instructions, 2045 struct _mesa_glsl_parse_state *state) 2046{ 2047 void *ctx = state; 2048 ir_function *f = NULL; 2049 ir_function_signature *sig = NULL; 2050 exec_list hir_parameters; 2051 2052 const char *const name = identifier; 2053 2054 /* Convert the list of function parameters to HIR now so that they can be 2055 * used below to compare this function's signature with previously seen 2056 * signatures for functions with the same name. 2057 */ 2058 ast_parameter_declarator::parameters_to_hir(& this->parameters, 2059 is_definition, 2060 & hir_parameters, state); 2061 2062 const char *return_type_name; 2063 const glsl_type *return_type = 2064 this->return_type->specifier->glsl_type(& return_type_name, state); 2065 2066 assert(return_type != NULL); 2067 2068 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec: 2069 * "No qualifier is allowed on the return type of a function." 2070 */ 2071 if (this->return_type->has_qualifiers()) { 2072 YYLTYPE loc = this->get_location(); 2073 _mesa_glsl_error(& loc, state, 2074 "function `%s' return type has qualifiers", name); 2075 } 2076 2077 /* Verify that this function's signature either doesn't match a previously 2078 * seen signature for a function with the same name, or, if a match is found, 2079 * that the previously seen signature does not have an associated definition. 2080 */ 2081 f = state->symbols->get_function(name); 2082 if (f != NULL) { 2083 ir_function_signature *sig = f->exact_matching_signature(&hir_parameters); 2084 if (sig != NULL) { 2085 const char *badvar = sig->qualifiers_match(&hir_parameters); 2086 if (badvar != NULL) { 2087 YYLTYPE loc = this->get_location(); 2088 2089 _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' " 2090 "qualifiers don't match prototype", name, badvar); 2091 } 2092 2093 if (sig->return_type != return_type) { 2094 YYLTYPE loc = this->get_location(); 2095 2096 _mesa_glsl_error(&loc, state, "function `%s' return type doesn't " 2097 "match prototype", name); 2098 } 2099 2100 if (is_definition && sig->is_defined) { 2101 YYLTYPE loc = this->get_location(); 2102 2103 _mesa_glsl_error(& loc, state, "function `%s' redefined", name); 2104 sig = NULL; 2105 } 2106 } 2107 } else if (state->symbols->name_declared_this_scope(name)) { 2108 /* This function name shadows a non-function use of the same name. 2109 */ 2110 YYLTYPE loc = this->get_location(); 2111 2112 _mesa_glsl_error(& loc, state, "function name `%s' conflicts with " 2113 "non-function", name); 2114 sig = NULL; 2115 } else { 2116 f = new(ctx) ir_function(name); 2117 state->symbols->add_function(f->name, f); 2118 2119 /* Emit the new function header */ 2120 instructions->push_tail(f); 2121 } 2122 2123 /* Verify the return type of main() */ 2124 if (strcmp(name, "main") == 0) { 2125 if (! return_type->is_void()) { 2126 YYLTYPE loc = this->get_location(); 2127 2128 _mesa_glsl_error(& loc, state, "main() must return void"); 2129 } 2130 2131 if (!hir_parameters.is_empty()) { 2132 YYLTYPE loc = this->get_location(); 2133 2134 _mesa_glsl_error(& loc, state, "main() must not take any parameters"); 2135 } 2136 } 2137 2138 /* Finish storing the information about this new function in its signature. 2139 */ 2140 if (sig == NULL) { 2141 sig = new(ctx) ir_function_signature(return_type); 2142 f->add_signature(sig); 2143 } 2144 2145 sig->replace_parameters(&hir_parameters); 2146 signature = sig; 2147 2148 /* Function declarations (prototypes) do not have r-values. 2149 */ 2150 return NULL; 2151} 2152 2153 2154ir_rvalue * 2155ast_function_definition::hir(exec_list *instructions, 2156 struct _mesa_glsl_parse_state *state) 2157{ 2158 prototype->is_definition = true; 2159 prototype->hir(instructions, state); 2160 2161 ir_function_signature *signature = prototype->signature; 2162 2163 assert(state->current_function == NULL); 2164 state->current_function = signature; 2165 state->found_return = false; 2166 2167 /* Duplicate parameters declared in the prototype as concrete variables. 2168 * Add these to the symbol table. 2169 */ 2170 state->symbols->push_scope(); 2171 foreach_iter(exec_list_iterator, iter, signature->parameters) { 2172 ir_variable *const var = ((ir_instruction *) iter.get())->as_variable(); 2173 2174 assert(var != NULL); 2175 2176 /* The only way a parameter would "exist" is if two parameters have 2177 * the same name. 2178 */ 2179 if (state->symbols->name_declared_this_scope(var->name)) { 2180 YYLTYPE loc = this->get_location(); 2181 2182 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name); 2183 } else { 2184 state->symbols->add_variable(var->name, var); 2185 } 2186 } 2187 2188 /* Convert the body of the function to HIR. */ 2189 this->body->hir(&signature->body, state); 2190 signature->is_defined = true; 2191 2192 state->symbols->pop_scope(); 2193 2194 assert(state->current_function == signature); 2195 state->current_function = NULL; 2196 2197 if (!signature->return_type->is_void() && !state->found_return) { 2198 YYLTYPE loc = this->get_location(); 2199 _mesa_glsl_error(& loc, state, "function `%s' has non-void return type " 2200 "%s, but no return statement", 2201 signature->function_name(), 2202 signature->return_type->name); 2203 } 2204 2205 /* Function definitions do not have r-values. 2206 */ 2207 return NULL; 2208} 2209 2210 2211ir_rvalue * 2212ast_jump_statement::hir(exec_list *instructions, 2213 struct _mesa_glsl_parse_state *state) 2214{ 2215 void *ctx = state; 2216 2217 switch (mode) { 2218 case ast_return: { 2219 ir_return *inst; 2220 assert(state->current_function); 2221 2222 if (opt_return_value) { 2223 if (state->current_function->return_type->base_type == 2224 GLSL_TYPE_VOID) { 2225 YYLTYPE loc = this->get_location(); 2226 2227 _mesa_glsl_error(& loc, state, 2228 "`return` with a value, in function `%s' " 2229 "returning void", 2230 state->current_function->function_name()); 2231 } 2232 2233 ir_expression *const ret = (ir_expression *) 2234 opt_return_value->hir(instructions, state); 2235 assert(ret != NULL); 2236 2237 /* Implicit conversions are not allowed for return values. */ 2238 if (state->current_function->return_type != ret->type) { 2239 YYLTYPE loc = this->get_location(); 2240 2241 _mesa_glsl_error(& loc, state, 2242 "`return' with wrong type %s, in function `%s' " 2243 "returning %s", 2244 ret->type->name, 2245 state->current_function->function_name(), 2246 state->current_function->return_type->name); 2247 } 2248 2249 inst = new(ctx) ir_return(ret); 2250 } else { 2251 if (state->current_function->return_type->base_type != 2252 GLSL_TYPE_VOID) { 2253 YYLTYPE loc = this->get_location(); 2254 2255 _mesa_glsl_error(& loc, state, 2256 "`return' with no value, in function %s returning " 2257 "non-void", 2258 state->current_function->function_name()); 2259 } 2260 inst = new(ctx) ir_return; 2261 } 2262 2263 state->found_return = true; 2264 instructions->push_tail(inst); 2265 break; 2266 } 2267 2268 case ast_discard: 2269 if (state->target != fragment_shader) { 2270 YYLTYPE loc = this->get_location(); 2271 2272 _mesa_glsl_error(& loc, state, 2273 "`discard' may only appear in a fragment shader"); 2274 } 2275 instructions->push_tail(new(ctx) ir_discard); 2276 break; 2277 2278 case ast_break: 2279 case ast_continue: 2280 /* FINISHME: Handle switch-statements. They cannot contain 'continue', 2281 * FINISHME: and they use a different IR instruction for 'break'. 2282 */ 2283 /* FINISHME: Correctly handle the nesting. If a switch-statement is 2284 * FINISHME: inside a loop, a 'continue' is valid and will bind to the 2285 * FINISHME: loop. 2286 */ 2287 if (state->loop_or_switch_nesting == NULL) { 2288 YYLTYPE loc = this->get_location(); 2289 2290 _mesa_glsl_error(& loc, state, 2291 "`%s' may only appear in a loop", 2292 (mode == ast_break) ? "break" : "continue"); 2293 } else { 2294 ir_loop *const loop = state->loop_or_switch_nesting->as_loop(); 2295 2296 if (loop != NULL) { 2297 ir_loop_jump *const jump = 2298 new(ctx) ir_loop_jump((mode == ast_break) 2299 ? ir_loop_jump::jump_break 2300 : ir_loop_jump::jump_continue); 2301 instructions->push_tail(jump); 2302 } 2303 } 2304 2305 break; 2306 } 2307 2308 /* Jump instructions do not have r-values. 2309 */ 2310 return NULL; 2311} 2312 2313 2314ir_rvalue * 2315ast_selection_statement::hir(exec_list *instructions, 2316 struct _mesa_glsl_parse_state *state) 2317{ 2318 void *ctx = state; 2319 2320 ir_rvalue *const condition = this->condition->hir(instructions, state); 2321 2322 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec: 2323 * 2324 * "Any expression whose type evaluates to a Boolean can be used as the 2325 * conditional expression bool-expression. Vector types are not accepted 2326 * as the expression to if." 2327 * 2328 * The checks are separated so that higher quality diagnostics can be 2329 * generated for cases where both rules are violated. 2330 */ 2331 if (!condition->type->is_boolean() || !condition->type->is_scalar()) { 2332 YYLTYPE loc = this->condition->get_location(); 2333 2334 _mesa_glsl_error(& loc, state, "if-statement condition must be scalar " 2335 "boolean"); 2336 } 2337 2338 ir_if *const stmt = new(ctx) ir_if(condition); 2339 2340 if (then_statement != NULL) 2341 then_statement->hir(& stmt->then_instructions, state); 2342 2343 if (else_statement != NULL) 2344 else_statement->hir(& stmt->else_instructions, state); 2345 2346 instructions->push_tail(stmt); 2347 2348 /* if-statements do not have r-values. 2349 */ 2350 return NULL; 2351} 2352 2353 2354void 2355ast_iteration_statement::condition_to_hir(ir_loop *stmt, 2356 struct _mesa_glsl_parse_state *state) 2357{ 2358 void *ctx = state; 2359 2360 if (condition != NULL) { 2361 ir_rvalue *const cond = 2362 condition->hir(& stmt->body_instructions, state); 2363 2364 if ((cond == NULL) 2365 || !cond->type->is_boolean() || !cond->type->is_scalar()) { 2366 YYLTYPE loc = condition->get_location(); 2367 2368 _mesa_glsl_error(& loc, state, 2369 "loop condition must be scalar boolean"); 2370 } else { 2371 /* As the first code in the loop body, generate a block that looks 2372 * like 'if (!condition) break;' as the loop termination condition. 2373 */ 2374 ir_rvalue *const not_cond = 2375 new(ctx) ir_expression(ir_unop_logic_not, glsl_type::bool_type, cond, 2376 NULL); 2377 2378 ir_if *const if_stmt = new(ctx) ir_if(not_cond); 2379 2380 ir_jump *const break_stmt = 2381 new(ctx) ir_loop_jump(ir_loop_jump::jump_break); 2382 2383 if_stmt->then_instructions.push_tail(break_stmt); 2384 stmt->body_instructions.push_tail(if_stmt); 2385 } 2386 } 2387} 2388 2389 2390ir_rvalue * 2391ast_iteration_statement::hir(exec_list *instructions, 2392 struct _mesa_glsl_parse_state *state) 2393{ 2394 void *ctx = state; 2395 2396 /* For-loops and while-loops start a new scope, but do-while loops do not. 2397 */ 2398 if (mode != ast_do_while) 2399 state->symbols->push_scope(); 2400 2401 if (init_statement != NULL) 2402 init_statement->hir(instructions, state); 2403 2404 ir_loop *const stmt = new(ctx) ir_loop(); 2405 instructions->push_tail(stmt); 2406 2407 /* Track the current loop and / or switch-statement nesting. 2408 */ 2409 ir_instruction *const nesting = state->loop_or_switch_nesting; 2410 state->loop_or_switch_nesting = stmt; 2411 2412 if (mode != ast_do_while) 2413 condition_to_hir(stmt, state); 2414 2415 if (body != NULL) 2416 body->hir(& stmt->body_instructions, state); 2417 2418 if (rest_expression != NULL) 2419 rest_expression->hir(& stmt->body_instructions, state); 2420 2421 if (mode == ast_do_while) 2422 condition_to_hir(stmt, state); 2423 2424 if (mode != ast_do_while) 2425 state->symbols->pop_scope(); 2426 2427 /* Restore previous nesting before returning. 2428 */ 2429 state->loop_or_switch_nesting = nesting; 2430 2431 /* Loops do not have r-values. 2432 */ 2433 return NULL; 2434} 2435 2436 2437ir_rvalue * 2438ast_type_specifier::hir(exec_list *instructions, 2439 struct _mesa_glsl_parse_state *state) 2440{ 2441 if (this->structure != NULL) 2442 return this->structure->hir(instructions, state); 2443 2444 return NULL; 2445} 2446 2447 2448ir_rvalue * 2449ast_struct_specifier::hir(exec_list *instructions, 2450 struct _mesa_glsl_parse_state *state) 2451{ 2452 unsigned decl_count = 0; 2453 2454 /* Make an initial pass over the list of structure fields to determine how 2455 * many there are. Each element in this list is an ast_declarator_list. 2456 * This means that we actually need to count the number of elements in the 2457 * 'declarations' list in each of the elements. 2458 */ 2459 foreach_list_typed (ast_declarator_list, decl_list, link, 2460 &this->declarations) { 2461 foreach_list_const (decl_ptr, & decl_list->declarations) { 2462 decl_count++; 2463 } 2464 } 2465 2466 2467 /* Allocate storage for the structure fields and process the field 2468 * declarations. As the declarations are processed, try to also convert 2469 * the types to HIR. This ensures that structure definitions embedded in 2470 * other structure definitions are processed. 2471 */ 2472 glsl_struct_field *const fields = (glsl_struct_field *) 2473 malloc(sizeof(*fields) * decl_count); 2474 2475 unsigned i = 0; 2476 foreach_list_typed (ast_declarator_list, decl_list, link, 2477 &this->declarations) { 2478 const char *type_name; 2479 2480 decl_list->type->specifier->hir(instructions, state); 2481 2482 const glsl_type *decl_type = 2483 decl_list->type->specifier->glsl_type(& type_name, state); 2484 2485 foreach_list_typed (ast_declaration, decl, link, 2486 &decl_list->declarations) { 2487 const struct glsl_type *const field_type = 2488 (decl->is_array) 2489 ? process_array_type(decl_type, decl->array_size, state) 2490 : decl_type; 2491 2492 fields[i].type = (field_type != NULL) 2493 ? field_type : glsl_type::error_type; 2494 fields[i].name = decl->identifier; 2495 i++; 2496 } 2497 } 2498 2499 assert(i == decl_count); 2500 2501 const char *name; 2502 if (this->name == NULL) { 2503 static unsigned anon_count = 1; 2504 char buf[32]; 2505 2506 snprintf(buf, sizeof(buf), "#anon_struct_%04x", anon_count); 2507 anon_count++; 2508 2509 name = strdup(buf); 2510 } else { 2511 name = this->name; 2512 } 2513 2514 const glsl_type *t = 2515 glsl_type::get_record_instance(fields, decl_count, name); 2516 2517 YYLTYPE loc = this->get_location(); 2518 if (!state->symbols->add_type(name, t)) { 2519 _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name); 2520 } else { 2521 /* This logic is a bit tricky. It is an error to declare a structure at 2522 * global scope if there is also a function with the same name. 2523 */ 2524 if ((state->current_function == NULL) 2525 && (state->symbols->get_function(name) != NULL)) { 2526 _mesa_glsl_error(& loc, state, "name `%s' previously defined", name); 2527 } else { 2528 t->generate_constructor(state->symbols); 2529 } 2530 2531 const glsl_type **s = (const glsl_type **) 2532 realloc(state->user_structures, 2533 sizeof(state->user_structures[0]) * 2534 (state->num_user_structures + 1)); 2535 if (s != NULL) { 2536 s[state->num_user_structures] = t; 2537 state->user_structures = s; 2538 state->num_user_structures++; 2539 } 2540 } 2541 2542 /* Structure type definitions do not have r-values. 2543 */ 2544 return NULL; 2545} 2546