Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2010 Intel Corporation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | * DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * \file ast_to_hir.c |
| 26 | * Convert abstract syntax to to high-level intermediate reprensentation (HIR). |
| 27 | * |
| 28 | * During the conversion to HIR, the majority of the symantic checking is |
| 29 | * preformed on the program. This includes: |
| 30 | * |
| 31 | * * Symbol table management |
| 32 | * * Type checking |
| 33 | * * Function binding |
| 34 | * |
| 35 | * The majority of this work could be done during parsing, and the parser could |
| 36 | * probably generate HIR directly. However, this results in frequent changes |
| 37 | * to the parser code. Since we do not assume that every system this complier |
| 38 | * is built on will have Flex and Bison installed, we have to store the code |
| 39 | * generated by these tools in our version control system. In other parts of |
| 40 | * the system we've seen problems where a parser was changed but the generated |
| 41 | * code was not committed, merge conflicts where created because two developers |
| 42 | * had slightly different versions of Bison installed, etc. |
| 43 | * |
| 44 | * I have also noticed that running Bison generated parsers in GDB is very |
| 45 | * irritating. When you get a segfault on '$$ = $1->foo', you can't very |
| 46 | * well 'print $1' in GDB. |
| 47 | * |
| 48 | * As a result, my preference is to put as little C code as possible in the |
| 49 | * parser (and lexer) sources. |
| 50 | */ |
Eric Anholt | ac95f2f | 2010-06-22 10:38:52 -0700 | [diff] [blame] | 51 | |
Chia-I Wu | bfd7c9a | 2010-08-23 17:51:42 +0800 | [diff] [blame] | 52 | #include "main/core.h" /* for struct gl_extensions */ |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 53 | #include "glsl_symbol_table.h" |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 54 | #include "glsl_parser_extras.h" |
| 55 | #include "ast.h" |
| 56 | #include "glsl_types.h" |
Eric Anholt | 1406321 | 2012-01-30 08:50:14 -0800 | [diff] [blame] | 57 | #include "program/hash_table.h" |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 58 | #include "ir.h" |
| 59 | |
Eric Anholt | 4b2a4cb | 2012-03-29 17:29:20 -0700 | [diff] [blame] | 60 | static void |
| 61 | detect_conflicting_assignments(struct _mesa_glsl_parse_state *state, |
| 62 | exec_list *instructions); |
| 63 | |
Ian Romanick | d949a9a | 2010-03-10 09:55:22 -0800 | [diff] [blame] | 64 | void |
| 65 | _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state) |
| 66 | { |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 67 | _mesa_glsl_initialize_variables(instructions, state); |
| 68 | |
Paul Berry | 53e572f | 2012-08-01 17:44:02 -0700 | [diff] [blame] | 69 | state->symbols->separate_function_namespace = state->language_version == 110; |
Kenneth Graunke | 814c89a | 2010-09-05 00:31:28 -0700 | [diff] [blame] | 70 | |
Ian Romanick | 41ec6a4 | 2010-03-19 17:08:05 -0700 | [diff] [blame] | 71 | state->current_function = NULL; |
| 72 | |
Paul Berry | 0d81b0e | 2011-07-29 15:28:52 -0700 | [diff] [blame] | 73 | state->toplevel_ir = instructions; |
| 74 | |
Kenneth Graunke | a044285 | 2010-08-23 14:52:06 -0700 | [diff] [blame] | 75 | /* Section 4.2 of the GLSL 1.20 specification states: |
| 76 | * "The built-in functions are scoped in a scope outside the global scope |
| 77 | * users declare global variables in. That is, a shader's global scope, |
| 78 | * available for user-defined functions and global variables, is nested |
| 79 | * inside the scope containing the built-in functions." |
| 80 | * |
| 81 | * Since built-in functions like ftransform() access built-in variables, |
| 82 | * it follows that those must be in the outer scope as well. |
| 83 | * |
| 84 | * We push scope here to create this nesting effect...but don't pop. |
| 85 | * This way, a shader's globals are still in the symbol table for use |
| 86 | * by the linker. |
| 87 | */ |
| 88 | state->symbols->push_scope(); |
| 89 | |
Ian Romanick | 2b97dc6 | 2010-05-10 17:42:05 -0700 | [diff] [blame] | 90 | foreach_list_typed (ast_node, ast, link, & state->translation_unit) |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 91 | ast->hir(instructions, state); |
Ian Romanick | 02c5ae1 | 2011-07-11 10:46:01 -0700 | [diff] [blame] | 92 | |
| 93 | detect_recursion_unlinked(state, instructions); |
Eric Anholt | 4b2a4cb | 2012-03-29 17:29:20 -0700 | [diff] [blame] | 94 | detect_conflicting_assignments(state, instructions); |
Paul Berry | 0d81b0e | 2011-07-29 15:28:52 -0700 | [diff] [blame] | 95 | |
| 96 | state->toplevel_ir = NULL; |
Ian Romanick | c170c90 | 2013-06-07 17:05:22 -0700 | [diff] [blame] | 97 | |
| 98 | /* Move all of the variable declarations to the front of the IR list, and |
| 99 | * reverse the order. This has the (intended!) side effect that vertex |
| 100 | * shader inputs and fragment shader outputs will appear in the IR in the |
| 101 | * same order that they appeared in the shader code. This results in the |
| 102 | * locations being assigned in the declared order. Many (arguably buggy) |
| 103 | * applications depend on this behavior, and it matches what nearly all |
| 104 | * other drivers do. |
| 105 | */ |
| 106 | foreach_list_safe(node, instructions) { |
| 107 | ir_variable *const var = ((ir_instruction *) node)->as_variable(); |
| 108 | |
| 109 | if (var == NULL) |
| 110 | continue; |
| 111 | |
| 112 | var->remove(); |
| 113 | instructions->push_head(var); |
| 114 | } |
Ian Romanick | d949a9a | 2010-03-10 09:55:22 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 118 | /** |
| 119 | * If a conversion is available, convert one operand to a different type |
| 120 | * |
| 121 | * The \c from \c ir_rvalue is converted "in place". |
| 122 | * |
| 123 | * \param to Type that the operand it to be converted to |
| 124 | * \param from Operand that is being converted |
| 125 | * \param state GLSL compiler state |
| 126 | * |
| 127 | * \return |
| 128 | * If a conversion is possible (or unnecessary), \c true is returned. |
| 129 | * Otherwise \c false is returned. |
| 130 | */ |
Kenneth Graunke | f32d3df | 2010-09-01 20:03:17 -0700 | [diff] [blame] | 131 | bool |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 132 | apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from, |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 133 | struct _mesa_glsl_parse_state *state) |
| 134 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 135 | void *ctx = state; |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 136 | if (to->base_type == from->type->base_type) |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 137 | return true; |
| 138 | |
| 139 | /* This conversion was added in GLSL 1.20. If the compilation mode is |
| 140 | * GLSL 1.10, the conversion is skipped. |
| 141 | */ |
Paul Berry | e3ded7f | 2012-08-01 14:50:05 -0700 | [diff] [blame] | 142 | if (!state->is_version(120, 0)) |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 143 | return false; |
| 144 | |
| 145 | /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec: |
| 146 | * |
| 147 | * "There are no implicit array or structure conversions. For |
| 148 | * example, an array of int cannot be implicitly converted to an |
| 149 | * array of float. There are no implicit conversions between |
| 150 | * signed and unsigned integers." |
| 151 | */ |
| 152 | /* FINISHME: The above comment is partially a lie. There is int/uint |
| 153 | * FINISHME: conversion for immediate constants. |
| 154 | */ |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 155 | if (!to->is_float() || !from->type->is_numeric()) |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 156 | return false; |
| 157 | |
Kenneth Graunke | 506199b | 2010-06-29 15:59:27 -0700 | [diff] [blame] | 158 | /* Convert to a floating point type with the same number of components |
| 159 | * as the original type - i.e. int to float, not int to vec4. |
| 160 | */ |
| 161 | to = glsl_type::get_instance(GLSL_TYPE_FLOAT, from->type->vector_elements, |
| 162 | from->type->matrix_columns); |
| 163 | |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 164 | switch (from->type->base_type) { |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 165 | case GLSL_TYPE_INT: |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 166 | from = new(ctx) ir_expression(ir_unop_i2f, to, from, NULL); |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 167 | break; |
| 168 | case GLSL_TYPE_UINT: |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 169 | from = new(ctx) ir_expression(ir_unop_u2f, to, from, NULL); |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 170 | break; |
| 171 | case GLSL_TYPE_BOOL: |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 172 | from = new(ctx) ir_expression(ir_unop_b2f, to, from, NULL); |
Eric Anholt | dc58b3f | 2010-04-02 02:13:43 -1000 | [diff] [blame] | 173 | break; |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 174 | default: |
| 175 | assert(0); |
| 176 | } |
| 177 | |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 182 | static const struct glsl_type * |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 183 | arithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 184 | bool multiply, |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 185 | struct _mesa_glsl_parse_state *state, YYLTYPE *loc) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 186 | { |
Eric Anholt | 336b4ad | 2010-05-19 10:38:37 -0700 | [diff] [blame] | 187 | const glsl_type *type_a = value_a->type; |
| 188 | const glsl_type *type_b = value_b->type; |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 189 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 190 | /* From GLSL 1.50 spec, page 56: |
| 191 | * |
| 192 | * "The arithmetic binary operators add (+), subtract (-), |
| 193 | * multiply (*), and divide (/) operate on integer and |
| 194 | * floating-point scalars, vectors, and matrices." |
| 195 | */ |
Ian Romanick | 60b54d9 | 2010-03-24 17:08:13 -0700 | [diff] [blame] | 196 | if (!type_a->is_numeric() || !type_b->is_numeric()) { |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 197 | _mesa_glsl_error(loc, state, |
| 198 | "Operands to arithmetic operators must be numeric"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 199 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | |
| 203 | /* "If one operand is floating-point based and the other is |
| 204 | * not, then the conversions from Section 4.1.10 "Implicit |
| 205 | * Conversions" are applied to the non-floating-point-based operand." |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 206 | */ |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 207 | if (!apply_implicit_conversion(type_a, value_b, state) |
| 208 | && !apply_implicit_conversion(type_b, value_a, state)) { |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 209 | _mesa_glsl_error(loc, state, |
| 210 | "Could not implicitly convert operands to " |
| 211 | "arithmetic operator"); |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 212 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 213 | } |
Eric Anholt | 336b4ad | 2010-05-19 10:38:37 -0700 | [diff] [blame] | 214 | type_a = value_a->type; |
| 215 | type_b = value_b->type; |
| 216 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 217 | /* "If the operands are integer types, they must both be signed or |
| 218 | * both be unsigned." |
| 219 | * |
| 220 | * From this rule and the preceeding conversion it can be inferred that |
| 221 | * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT. |
Ian Romanick | 60b54d9 | 2010-03-24 17:08:13 -0700 | [diff] [blame] | 222 | * The is_numeric check above already filtered out the case where either |
| 223 | * type is not one of these, so now the base types need only be tested for |
| 224 | * equality. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 225 | */ |
| 226 | if (type_a->base_type != type_b->base_type) { |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 227 | _mesa_glsl_error(loc, state, |
| 228 | "base type mismatch for arithmetic operator"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 229 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /* "All arithmetic binary operators result in the same fundamental type |
| 233 | * (signed integer, unsigned integer, or floating-point) as the |
| 234 | * operands they operate on, after operand type conversion. After |
| 235 | * conversion, the following cases are valid |
| 236 | * |
| 237 | * * The two operands are scalars. In this case the operation is |
| 238 | * applied, resulting in a scalar." |
| 239 | */ |
Ian Romanick | cb36f8a | 2010-03-09 15:51:22 -0800 | [diff] [blame] | 240 | if (type_a->is_scalar() && type_b->is_scalar()) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 241 | return type_a; |
| 242 | |
| 243 | /* "* One operand is a scalar, and the other is a vector or matrix. |
| 244 | * In this case, the scalar operation is applied independently to each |
| 245 | * component of the vector or matrix, resulting in the same size |
| 246 | * vector or matrix." |
| 247 | */ |
Ian Romanick | cb36f8a | 2010-03-09 15:51:22 -0800 | [diff] [blame] | 248 | if (type_a->is_scalar()) { |
| 249 | if (!type_b->is_scalar()) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 250 | return type_b; |
Ian Romanick | cb36f8a | 2010-03-09 15:51:22 -0800 | [diff] [blame] | 251 | } else if (type_b->is_scalar()) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 252 | return type_a; |
| 253 | } |
| 254 | |
| 255 | /* All of the combinations of <scalar, scalar>, <vector, scalar>, |
| 256 | * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been |
| 257 | * handled. |
| 258 | */ |
Ian Romanick | 60b54d9 | 2010-03-24 17:08:13 -0700 | [diff] [blame] | 259 | assert(!type_a->is_scalar()); |
| 260 | assert(!type_b->is_scalar()); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 261 | |
| 262 | /* "* The two operands are vectors of the same size. In this case, the |
| 263 | * operation is done component-wise resulting in the same size |
| 264 | * vector." |
| 265 | */ |
Ian Romanick | a2dd22f | 2010-03-09 15:55:16 -0800 | [diff] [blame] | 266 | if (type_a->is_vector() && type_b->is_vector()) { |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 267 | if (type_a == type_b) { |
| 268 | return type_a; |
| 269 | } else { |
| 270 | _mesa_glsl_error(loc, state, |
| 271 | "vector size mismatch for arithmetic operator"); |
| 272 | return glsl_type::error_type; |
| 273 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /* All of the combinations of <scalar, scalar>, <vector, scalar>, |
| 277 | * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and |
| 278 | * <vector, vector> have been handled. At least one of the operands must |
| 279 | * be matrix. Further, since there are no integer matrix types, the base |
| 280 | * type of both operands must be float. |
| 281 | */ |
Ian Romanick | 60b54d9 | 2010-03-24 17:08:13 -0700 | [diff] [blame] | 282 | assert(type_a->is_matrix() || type_b->is_matrix()); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 283 | assert(type_a->base_type == GLSL_TYPE_FLOAT); |
| 284 | assert(type_b->base_type == GLSL_TYPE_FLOAT); |
| 285 | |
| 286 | /* "* The operator is add (+), subtract (-), or divide (/), and the |
| 287 | * operands are matrices with the same number of rows and the same |
| 288 | * number of columns. In this case, the operation is done component- |
| 289 | * wise resulting in the same size matrix." |
| 290 | * * The operator is multiply (*), where both operands are matrices or |
| 291 | * one operand is a vector and the other a matrix. A right vector |
| 292 | * operand is treated as a column vector and a left vector operand as a |
| 293 | * row vector. In all these cases, it is required that the number of |
| 294 | * columns of the left operand is equal to the number of rows of the |
| 295 | * right operand. Then, the multiply (*) operation does a linear |
| 296 | * algebraic multiply, yielding an object that has the same number of |
| 297 | * rows as the left operand and the same number of columns as the right |
| 298 | * operand. Section 5.10 "Vector and Matrix Operations" explains in |
| 299 | * more detail how vectors and matrices are operated on." |
| 300 | */ |
| 301 | if (! multiply) { |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 302 | if (type_a == type_b) |
| 303 | return type_a; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 304 | } else { |
Ian Romanick | fce1150 | 2010-03-09 15:58:52 -0800 | [diff] [blame] | 305 | if (type_a->is_matrix() && type_b->is_matrix()) { |
Ian Romanick | c1bd3a1 | 2010-03-25 13:06:58 -0700 | [diff] [blame] | 306 | /* Matrix multiply. The columns of A must match the rows of B. Given |
| 307 | * the other previously tested constraints, this means the vector type |
| 308 | * of a row from A must be the same as the vector type of a column from |
| 309 | * B. |
| 310 | */ |
| 311 | if (type_a->row_type() == type_b->column_type()) { |
| 312 | /* The resulting matrix has the number of columns of matrix B and |
| 313 | * the number of rows of matrix A. We get the row count of A by |
| 314 | * looking at the size of a vector that makes up a column. The |
| 315 | * transpose (size of a row) is done for B. |
| 316 | */ |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 317 | const glsl_type *const type = |
Ian Romanick | c1bd3a1 | 2010-03-25 13:06:58 -0700 | [diff] [blame] | 318 | glsl_type::get_instance(type_a->base_type, |
| 319 | type_a->column_type()->vector_elements, |
| 320 | type_b->row_type()->vector_elements); |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 321 | assert(type != glsl_type::error_type); |
| 322 | |
| 323 | return type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 324 | } |
Ian Romanick | fce1150 | 2010-03-09 15:58:52 -0800 | [diff] [blame] | 325 | } else if (type_a->is_matrix()) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 326 | /* A is a matrix and B is a column vector. Columns of A must match |
Ian Romanick | c1bd3a1 | 2010-03-25 13:06:58 -0700 | [diff] [blame] | 327 | * rows of B. Given the other previously tested constraints, this |
| 328 | * means the vector type of a row from A must be the same as the |
| 329 | * vector the type of B. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 330 | */ |
Carl Worth | 47c90b1 | 2010-07-22 14:56:14 -0700 | [diff] [blame] | 331 | if (type_a->row_type() == type_b) { |
| 332 | /* The resulting vector has a number of elements equal to |
| 333 | * the number of rows of matrix A. */ |
| 334 | const glsl_type *const type = |
| 335 | glsl_type::get_instance(type_a->base_type, |
| 336 | type_a->column_type()->vector_elements, |
| 337 | 1); |
| 338 | assert(type != glsl_type::error_type); |
| 339 | |
| 340 | return type; |
| 341 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 342 | } else { |
Ian Romanick | fce1150 | 2010-03-09 15:58:52 -0800 | [diff] [blame] | 343 | assert(type_b->is_matrix()); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 344 | |
Ian Romanick | c1bd3a1 | 2010-03-25 13:06:58 -0700 | [diff] [blame] | 345 | /* A is a row vector and B is a matrix. Columns of A must match rows |
| 346 | * of B. Given the other previously tested constraints, this means |
| 347 | * the type of A must be the same as the vector type of a column from |
| 348 | * B. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 349 | */ |
Carl Worth | 47c90b1 | 2010-07-22 14:56:14 -0700 | [diff] [blame] | 350 | if (type_a == type_b->column_type()) { |
| 351 | /* The resulting vector has a number of elements equal to |
| 352 | * the number of columns of matrix B. */ |
| 353 | const glsl_type *const type = |
| 354 | glsl_type::get_instance(type_a->base_type, |
| 355 | type_b->row_type()->vector_elements, |
| 356 | 1); |
| 357 | assert(type != glsl_type::error_type); |
| 358 | |
| 359 | return type; |
| 360 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 361 | } |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 362 | |
| 363 | _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication"); |
| 364 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | |
| 368 | /* "All other cases are illegal." |
| 369 | */ |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 370 | _mesa_glsl_error(loc, state, "type mismatch"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 371 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | |
| 375 | static const struct glsl_type * |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 376 | unary_arithmetic_result_type(const struct glsl_type *type, |
| 377 | struct _mesa_glsl_parse_state *state, YYLTYPE *loc) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 378 | { |
| 379 | /* From GLSL 1.50 spec, page 57: |
| 380 | * |
| 381 | * "The arithmetic unary operators negate (-), post- and pre-increment |
| 382 | * and decrement (-- and ++) operate on integer or floating-point |
| 383 | * values (including vectors and matrices). All unary operators work |
| 384 | * component-wise on their operands. These result with the same type |
| 385 | * they operated on." |
| 386 | */ |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 387 | if (!type->is_numeric()) { |
| 388 | _mesa_glsl_error(loc, state, |
| 389 | "Operands to arithmetic operators must be numeric"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 390 | return glsl_type::error_type; |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 391 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 392 | |
| 393 | return type; |
| 394 | } |
| 395 | |
Chad Versace | cfdbf8b | 2010-10-15 11:28:05 -0700 | [diff] [blame] | 396 | /** |
| 397 | * \brief Return the result type of a bit-logic operation. |
| 398 | * |
| 399 | * If the given types to the bit-logic operator are invalid, return |
| 400 | * glsl_type::error_type. |
| 401 | * |
| 402 | * \param type_a Type of LHS of bit-logic op |
| 403 | * \param type_b Type of RHS of bit-logic op |
| 404 | */ |
| 405 | static const struct glsl_type * |
| 406 | bit_logic_result_type(const struct glsl_type *type_a, |
| 407 | const struct glsl_type *type_b, |
| 408 | ast_operators op, |
| 409 | struct _mesa_glsl_parse_state *state, YYLTYPE *loc) |
| 410 | { |
Paul Berry | 0d9bba6 | 2012-08-05 09:57:01 -0700 | [diff] [blame] | 411 | if (!state->check_bitwise_operations_allowed(loc)) { |
Chad Versace | cfdbf8b | 2010-10-15 11:28:05 -0700 | [diff] [blame] | 412 | return glsl_type::error_type; |
| 413 | } |
| 414 | |
| 415 | /* From page 50 (page 56 of PDF) of GLSL 1.30 spec: |
| 416 | * |
| 417 | * "The bitwise operators and (&), exclusive-or (^), and inclusive-or |
| 418 | * (|). The operands must be of type signed or unsigned integers or |
| 419 | * integer vectors." |
| 420 | */ |
| 421 | if (!type_a->is_integer()) { |
| 422 | _mesa_glsl_error(loc, state, "LHS of `%s' must be an integer", |
| 423 | ast_expression::operator_string(op)); |
| 424 | return glsl_type::error_type; |
| 425 | } |
| 426 | if (!type_b->is_integer()) { |
| 427 | _mesa_glsl_error(loc, state, "RHS of `%s' must be an integer", |
| 428 | ast_expression::operator_string(op)); |
| 429 | return glsl_type::error_type; |
| 430 | } |
| 431 | |
| 432 | /* "The fundamental types of the operands (signed or unsigned) must |
| 433 | * match," |
| 434 | */ |
| 435 | if (type_a->base_type != type_b->base_type) { |
| 436 | _mesa_glsl_error(loc, state, "operands of `%s' must have the same " |
| 437 | "base type", ast_expression::operator_string(op)); |
| 438 | return glsl_type::error_type; |
| 439 | } |
| 440 | |
| 441 | /* "The operands cannot be vectors of differing size." */ |
| 442 | if (type_a->is_vector() && |
| 443 | type_b->is_vector() && |
| 444 | type_a->vector_elements != type_b->vector_elements) { |
| 445 | _mesa_glsl_error(loc, state, "operands of `%s' cannot be vectors of " |
| 446 | "different sizes", ast_expression::operator_string(op)); |
| 447 | return glsl_type::error_type; |
| 448 | } |
| 449 | |
| 450 | /* "If one operand is a scalar and the other a vector, the scalar is |
| 451 | * applied component-wise to the vector, resulting in the same type as |
| 452 | * the vector. The fundamental types of the operands [...] will be the |
| 453 | * resulting fundamental type." |
| 454 | */ |
| 455 | if (type_a->is_scalar()) |
| 456 | return type_b; |
| 457 | else |
| 458 | return type_a; |
| 459 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 460 | |
| 461 | static const struct glsl_type * |
| 462 | modulus_result_type(const struct glsl_type *type_a, |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 463 | const struct glsl_type *type_b, |
| 464 | struct _mesa_glsl_parse_state *state, YYLTYPE *loc) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 465 | { |
Paul Berry | d4a2474 | 2012-08-02 08:18:12 -0700 | [diff] [blame] | 466 | if (!state->check_version(130, 300, loc, "operator '%%' is reserved")) { |
Chad Versace | 82f994f | 2011-02-04 12:18:56 -0800 | [diff] [blame] | 467 | return glsl_type::error_type; |
| 468 | } |
| 469 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 470 | /* From GLSL 1.50 spec, page 56: |
| 471 | * "The operator modulus (%) operates on signed or unsigned integers or |
| 472 | * integer vectors. The operand types must both be signed or both be |
| 473 | * unsigned." |
| 474 | */ |
Kenneth Graunke | 8eb9753 | 2011-06-14 22:21:41 -0700 | [diff] [blame] | 475 | if (!type_a->is_integer()) { |
| 476 | _mesa_glsl_error(loc, state, "LHS of operator %% must be an integer."); |
| 477 | return glsl_type::error_type; |
| 478 | } |
| 479 | if (!type_b->is_integer()) { |
| 480 | _mesa_glsl_error(loc, state, "RHS of operator %% must be an integer."); |
| 481 | return glsl_type::error_type; |
| 482 | } |
| 483 | if (type_a->base_type != type_b->base_type) { |
| 484 | _mesa_glsl_error(loc, state, |
| 485 | "operands of %% must have the same base type"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 486 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | /* "The operands cannot be vectors of differing size. If one operand is |
| 490 | * a scalar and the other vector, then the scalar is applied component- |
| 491 | * wise to the vector, resulting in the same type as the vector. If both |
| 492 | * are vectors of the same size, the result is computed component-wise." |
| 493 | */ |
Ian Romanick | a2dd22f | 2010-03-09 15:55:16 -0800 | [diff] [blame] | 494 | if (type_a->is_vector()) { |
| 495 | if (!type_b->is_vector() |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 496 | || (type_a->vector_elements == type_b->vector_elements)) |
| 497 | return type_a; |
| 498 | } else |
| 499 | return type_b; |
| 500 | |
| 501 | /* "The operator modulus (%) is not defined for any other data types |
| 502 | * (non-integer types)." |
| 503 | */ |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 504 | _mesa_glsl_error(loc, state, "type mismatch"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 505 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | |
| 509 | static const struct glsl_type * |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 510 | relational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b, |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 511 | struct _mesa_glsl_parse_state *state, YYLTYPE *loc) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 512 | { |
Eric Anholt | 336b4ad | 2010-05-19 10:38:37 -0700 | [diff] [blame] | 513 | const glsl_type *type_a = value_a->type; |
| 514 | const glsl_type *type_b = value_b->type; |
Ian Romanick | 0150f5f | 2010-03-29 16:20:07 -0700 | [diff] [blame] | 515 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 516 | /* From GLSL 1.50 spec, page 56: |
| 517 | * "The relational operators greater than (>), less than (<), greater |
| 518 | * than or equal (>=), and less than or equal (<=) operate only on |
| 519 | * scalar integer and scalar floating-point expressions." |
| 520 | */ |
Ian Romanick | a6d653d | 2010-03-26 14:40:37 -0700 | [diff] [blame] | 521 | if (!type_a->is_numeric() |
| 522 | || !type_b->is_numeric() |
Ian Romanick | cb36f8a | 2010-03-09 15:51:22 -0800 | [diff] [blame] | 523 | || !type_a->is_scalar() |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 524 | || !type_b->is_scalar()) { |
| 525 | _mesa_glsl_error(loc, state, |
| 526 | "Operands to relational operators must be scalar and " |
| 527 | "numeric"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 528 | return glsl_type::error_type; |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 529 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 530 | |
| 531 | /* "Either the operands' types must match, or the conversions from |
| 532 | * Section 4.1.10 "Implicit Conversions" will be applied to the integer |
| 533 | * operand, after which the types must match." |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 534 | */ |
Ian Romanick | 0150f5f | 2010-03-29 16:20:07 -0700 | [diff] [blame] | 535 | if (!apply_implicit_conversion(type_a, value_b, state) |
| 536 | && !apply_implicit_conversion(type_b, value_a, state)) { |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 537 | _mesa_glsl_error(loc, state, |
| 538 | "Could not implicitly convert operands to " |
| 539 | "relational operator"); |
Ian Romanick | 0150f5f | 2010-03-29 16:20:07 -0700 | [diff] [blame] | 540 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 541 | } |
Eric Anholt | 336b4ad | 2010-05-19 10:38:37 -0700 | [diff] [blame] | 542 | type_a = value_a->type; |
| 543 | type_b = value_b->type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 544 | |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 545 | if (type_a->base_type != type_b->base_type) { |
| 546 | _mesa_glsl_error(loc, state, "base type mismatch"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 547 | return glsl_type::error_type; |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 548 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 549 | |
| 550 | /* "The result is scalar Boolean." |
| 551 | */ |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 552 | return glsl_type::bool_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 553 | } |
| 554 | |
Chad Versace | c0197ab | 2010-10-15 09:49:46 -0700 | [diff] [blame] | 555 | /** |
| 556 | * \brief Return the result type of a bit-shift operation. |
| 557 | * |
| 558 | * If the given types to the bit-shift operator are invalid, return |
| 559 | * glsl_type::error_type. |
| 560 | * |
| 561 | * \param type_a Type of LHS of bit-shift op |
| 562 | * \param type_b Type of RHS of bit-shift op |
| 563 | */ |
| 564 | static const struct glsl_type * |
| 565 | shift_result_type(const struct glsl_type *type_a, |
| 566 | const struct glsl_type *type_b, |
| 567 | ast_operators op, |
| 568 | struct _mesa_glsl_parse_state *state, YYLTYPE *loc) |
| 569 | { |
Paul Berry | 0d9bba6 | 2012-08-05 09:57:01 -0700 | [diff] [blame] | 570 | if (!state->check_bitwise_operations_allowed(loc)) { |
Chad Versace | c0197ab | 2010-10-15 09:49:46 -0700 | [diff] [blame] | 571 | return glsl_type::error_type; |
| 572 | } |
| 573 | |
| 574 | /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec: |
| 575 | * |
| 576 | * "The shift operators (<<) and (>>). For both operators, the operands |
| 577 | * must be signed or unsigned integers or integer vectors. One operand |
| 578 | * can be signed while the other is unsigned." |
| 579 | */ |
| 580 | if (!type_a->is_integer()) { |
| 581 | _mesa_glsl_error(loc, state, "LHS of operator %s must be an integer or " |
| 582 | "integer vector", ast_expression::operator_string(op)); |
| 583 | return glsl_type::error_type; |
| 584 | |
| 585 | } |
| 586 | if (!type_b->is_integer()) { |
| 587 | _mesa_glsl_error(loc, state, "RHS of operator %s must be an integer or " |
| 588 | "integer vector", ast_expression::operator_string(op)); |
| 589 | return glsl_type::error_type; |
| 590 | } |
| 591 | |
| 592 | /* "If the first operand is a scalar, the second operand has to be |
| 593 | * a scalar as well." |
| 594 | */ |
| 595 | if (type_a->is_scalar() && !type_b->is_scalar()) { |
| 596 | _mesa_glsl_error(loc, state, "If the first operand of %s is scalar, the " |
| 597 | "second must be scalar as well", |
| 598 | ast_expression::operator_string(op)); |
| 599 | return glsl_type::error_type; |
| 600 | } |
| 601 | |
| 602 | /* If both operands are vectors, check that they have same number of |
| 603 | * elements. |
| 604 | */ |
| 605 | if (type_a->is_vector() && |
| 606 | type_b->is_vector() && |
| 607 | type_a->vector_elements != type_b->vector_elements) { |
| 608 | _mesa_glsl_error(loc, state, "Vector operands to operator %s must " |
| 609 | "have same number of elements", |
| 610 | ast_expression::operator_string(op)); |
| 611 | return glsl_type::error_type; |
| 612 | } |
| 613 | |
| 614 | /* "In all cases, the resulting type will be the same type as the left |
| 615 | * operand." |
| 616 | */ |
| 617 | return type_a; |
| 618 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 619 | |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 620 | /** |
| 621 | * Validates that a value can be assigned to a location with a specified type |
| 622 | * |
| 623 | * Validates that \c rhs can be assigned to some location. If the types are |
| 624 | * not an exact match but an automatic conversion is possible, \c rhs will be |
| 625 | * converted. |
| 626 | * |
| 627 | * \return |
| 628 | * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type. |
| 629 | * Otherwise the actual RHS to be assigned will be returned. This may be |
| 630 | * \c rhs, or it may be \c rhs after some type conversion. |
| 631 | * |
| 632 | * \note |
| 633 | * In addition to being used for assignments, this function is used to |
| 634 | * type-check return values. |
| 635 | */ |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 636 | ir_rvalue * |
Eric Anholt | 336b4ad | 2010-05-19 10:38:37 -0700 | [diff] [blame] | 637 | validate_assignment(struct _mesa_glsl_parse_state *state, |
Ian Romanick | 85caea2 | 2011-03-15 16:33:27 -0700 | [diff] [blame] | 638 | const glsl_type *lhs_type, ir_rvalue *rhs, |
| 639 | bool is_initializer) |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 640 | { |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 641 | /* If there is already some error in the RHS, just return it. Anything |
| 642 | * else will lead to an avalanche of error message back to the user. |
| 643 | */ |
Ian Romanick | ec53010 | 2010-12-10 15:47:11 -0800 | [diff] [blame] | 644 | if (rhs->type->is_error()) |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 645 | return rhs; |
| 646 | |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 647 | /* If the types are identical, the assignment can trivially proceed. |
| 648 | */ |
Ian Romanick | ec53010 | 2010-12-10 15:47:11 -0800 | [diff] [blame] | 649 | if (rhs->type == lhs_type) |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 650 | return rhs; |
| 651 | |
Ian Romanick | 0157f41 | 2010-04-02 17:44:39 -0700 | [diff] [blame] | 652 | /* If the array element types are the same and the size of the LHS is zero, |
Ian Romanick | 85caea2 | 2011-03-15 16:33:27 -0700 | [diff] [blame] | 653 | * the assignment is okay for initializers embedded in variable |
| 654 | * declarations. |
Ian Romanick | 0157f41 | 2010-04-02 17:44:39 -0700 | [diff] [blame] | 655 | * |
| 656 | * Note: Whole-array assignments are not permitted in GLSL 1.10, but this |
| 657 | * is handled by ir_dereference::is_lvalue. |
| 658 | */ |
Ian Romanick | 85caea2 | 2011-03-15 16:33:27 -0700 | [diff] [blame] | 659 | if (is_initializer && lhs_type->is_array() && rhs->type->is_array() |
Ian Romanick | 0157f41 | 2010-04-02 17:44:39 -0700 | [diff] [blame] | 660 | && (lhs_type->element_type() == rhs->type->element_type()) |
| 661 | && (lhs_type->array_size() == 0)) { |
| 662 | return rhs; |
| 663 | } |
| 664 | |
Eric Anholt | 336b4ad | 2010-05-19 10:38:37 -0700 | [diff] [blame] | 665 | /* Check for implicit conversion in GLSL 1.20 */ |
| 666 | if (apply_implicit_conversion(lhs_type, rhs, state)) { |
Ian Romanick | ec53010 | 2010-12-10 15:47:11 -0800 | [diff] [blame] | 667 | if (rhs->type == lhs_type) |
Eric Anholt | 336b4ad | 2010-05-19 10:38:37 -0700 | [diff] [blame] | 668 | return rhs; |
| 669 | } |
| 670 | |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 671 | return NULL; |
| 672 | } |
| 673 | |
Eric Anholt | a313c29 | 2011-08-05 21:40:50 -0700 | [diff] [blame] | 674 | static void |
| 675 | mark_whole_array_access(ir_rvalue *access) |
| 676 | { |
| 677 | ir_dereference_variable *deref = access->as_dereference_variable(); |
| 678 | |
| 679 | if (deref && deref->var) { |
| 680 | deref->var->max_array_access = deref->type->length - 1; |
| 681 | } |
| 682 | } |
| 683 | |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 684 | ir_rvalue * |
| 685 | do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state, |
Ian Romanick | e9015e9 | 2011-12-23 09:56:29 -0800 | [diff] [blame] | 686 | const char *non_lvalue_description, |
Ian Romanick | 85caea2 | 2011-03-15 16:33:27 -0700 | [diff] [blame] | 687 | ir_rvalue *lhs, ir_rvalue *rhs, bool is_initializer, |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 688 | YYLTYPE lhs_loc) |
| 689 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 690 | void *ctx = state; |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 691 | bool error_emitted = (lhs->type->is_error() || rhs->type->is_error()); |
| 692 | |
Ian Romanick | 89704eb | 2013-03-15 18:06:11 -0700 | [diff] [blame] | 693 | /* If the assignment LHS comes back as an ir_binop_vector_extract |
| 694 | * expression, move it to the RHS as an ir_triop_vector_insert. |
| 695 | */ |
| 696 | if (lhs->ir_type == ir_type_expression) { |
| 697 | ir_expression *const expr = lhs->as_expression(); |
| 698 | |
| 699 | if (unlikely(expr->operation == ir_binop_vector_extract)) { |
| 700 | ir_rvalue *new_rhs = |
| 701 | validate_assignment(state, lhs->type, rhs, is_initializer); |
| 702 | |
| 703 | if (new_rhs == NULL) { |
| 704 | _mesa_glsl_error(& lhs_loc, state, "type mismatch"); |
| 705 | return lhs; |
| 706 | } else { |
| 707 | rhs = new(ctx) ir_expression(ir_triop_vector_insert, |
| 708 | expr->operands[0]->type, |
| 709 | expr->operands[0], |
| 710 | new_rhs, |
| 711 | expr->operands[1]); |
| 712 | lhs = expr->operands[0]->clone(ctx, NULL); |
| 713 | } |
| 714 | } |
| 715 | } |
| 716 | |
Eric Anholt | f2475ca | 2012-03-29 17:02:15 -0700 | [diff] [blame] | 717 | ir_variable *lhs_var = lhs->variable_referenced(); |
| 718 | if (lhs_var) |
| 719 | lhs_var->assigned = true; |
| 720 | |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 721 | if (!error_emitted) { |
Ian Romanick | e9015e9 | 2011-12-23 09:56:29 -0800 | [diff] [blame] | 722 | if (non_lvalue_description != NULL) { |
| 723 | _mesa_glsl_error(&lhs_loc, state, |
| 724 | "assignment to %s", |
| 725 | non_lvalue_description); |
| 726 | error_emitted = true; |
| 727 | } else if (lhs->variable_referenced() != NULL |
| 728 | && lhs->variable_referenced()->read_only) { |
Chad Versace | b66be75 | 2011-01-21 13:44:08 -0800 | [diff] [blame] | 729 | _mesa_glsl_error(&lhs_loc, state, |
| 730 | "assignment to read-only variable '%s'", |
| 731 | lhs->variable_referenced()->name); |
| 732 | error_emitted = true; |
| 733 | |
Paul Berry | 0d9bba6 | 2012-08-05 09:57:01 -0700 | [diff] [blame] | 734 | } else if (lhs->type->is_array() && |
Paul Berry | d4a2474 | 2012-08-02 08:18:12 -0700 | [diff] [blame] | 735 | !state->check_version(120, 300, &lhs_loc, |
Paul Berry | 0d9bba6 | 2012-08-05 09:57:01 -0700 | [diff] [blame] | 736 | "whole array assignment forbidden")) { |
Eric Anholt | 525cec9 | 2011-09-07 12:03:36 -0700 | [diff] [blame] | 737 | /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec: |
| 738 | * |
| 739 | * "Other binary or unary expressions, non-dereferenced |
| 740 | * arrays, function names, swizzles with repeated fields, |
| 741 | * and constants cannot be l-values." |
Paul Berry | d4a2474 | 2012-08-02 08:18:12 -0700 | [diff] [blame] | 742 | * |
| 743 | * The restriction on arrays is lifted in GLSL 1.20 and GLSL ES 3.00. |
Eric Anholt | 525cec9 | 2011-09-07 12:03:36 -0700 | [diff] [blame] | 744 | */ |
Eric Anholt | 525cec9 | 2011-09-07 12:03:36 -0700 | [diff] [blame] | 745 | error_emitted = true; |
Chad Versace | b66be75 | 2011-01-21 13:44:08 -0800 | [diff] [blame] | 746 | } else if (!lhs->is_lvalue()) { |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 747 | _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment"); |
| 748 | error_emitted = true; |
| 749 | } |
| 750 | } |
| 751 | |
Ian Romanick | 85caea2 | 2011-03-15 16:33:27 -0700 | [diff] [blame] | 752 | ir_rvalue *new_rhs = |
| 753 | validate_assignment(state, lhs->type, rhs, is_initializer); |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 754 | if (new_rhs == NULL) { |
| 755 | _mesa_glsl_error(& lhs_loc, state, "type mismatch"); |
| 756 | } else { |
| 757 | rhs = new_rhs; |
Ian Romanick | 0157f41 | 2010-04-02 17:44:39 -0700 | [diff] [blame] | 758 | |
| 759 | /* If the LHS array was not declared with a size, it takes it size from |
| 760 | * the RHS. If the LHS is an l-value and a whole array, it must be a |
| 761 | * dereference of a variable. Any other case would require that the LHS |
| 762 | * is either not an l-value or not a whole array. |
| 763 | */ |
| 764 | if (lhs->type->array_size() == 0) { |
| 765 | ir_dereference *const d = lhs->as_dereference(); |
| 766 | |
| 767 | assert(d != NULL); |
| 768 | |
Ian Romanick | 36ea286 | 2010-05-19 13:52:29 +0200 | [diff] [blame] | 769 | ir_variable *const var = d->variable_referenced(); |
Ian Romanick | 0157f41 | 2010-04-02 17:44:39 -0700 | [diff] [blame] | 770 | |
| 771 | assert(var != NULL); |
| 772 | |
Ian Romanick | 63f3942 | 2010-04-05 14:35:47 -0700 | [diff] [blame] | 773 | if (var->max_array_access >= unsigned(rhs->type->array_size())) { |
| 774 | /* FINISHME: This should actually log the location of the RHS. */ |
| 775 | _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to " |
| 776 | "previous access", |
| 777 | var->max_array_access); |
| 778 | } |
| 779 | |
Ian Romanick | f38d15b | 2010-07-20 15:33:40 -0700 | [diff] [blame] | 780 | var->type = glsl_type::get_array_instance(lhs->type->element_type(), |
Ian Romanick | 0157f41 | 2010-04-02 17:44:39 -0700 | [diff] [blame] | 781 | rhs->type->array_size()); |
Eric Anholt | 9703ed0 | 2010-07-22 15:50:37 -0700 | [diff] [blame] | 782 | d->type = var->type; |
Ian Romanick | 0157f41 | 2010-04-02 17:44:39 -0700 | [diff] [blame] | 783 | } |
Eric Anholt | 407a100 | 2011-09-07 11:53:20 -0700 | [diff] [blame] | 784 | mark_whole_array_access(rhs); |
Eric Anholt | a313c29 | 2011-08-05 21:40:50 -0700 | [diff] [blame] | 785 | mark_whole_array_access(lhs); |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 786 | } |
| 787 | |
Eric Anholt | 2731a73 | 2010-06-23 14:51:14 -0700 | [diff] [blame] | 788 | /* Most callers of do_assignment (assign, add_assign, pre_inc/dec, |
| 789 | * but not post_inc) need the converted assigned value as an rvalue |
| 790 | * to handle things like: |
| 791 | * |
| 792 | * i = j += 1; |
| 793 | * |
| 794 | * So we always just store the computed value being assigned to a |
| 795 | * temporary and return a deref of that temporary. If the rvalue |
| 796 | * ends up not being used, the temp will get copy-propagated out. |
| 797 | */ |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 798 | ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp", |
| 799 | ir_var_temporary); |
Eric Anholt | e33c103 | 2010-06-24 15:13:03 -0700 | [diff] [blame] | 800 | ir_dereference_variable *deref_var = new(ctx) ir_dereference_variable(var); |
Eric Anholt | ae80592 | 2010-06-24 09:06:12 -0700 | [diff] [blame] | 801 | instructions->push_tail(var); |
Eric Anholt | aa5ec13 | 2012-05-14 09:14:54 -0700 | [diff] [blame] | 802 | instructions->push_tail(new(ctx) ir_assignment(deref_var, rhs)); |
Eric Anholt | e33c103 | 2010-06-24 15:13:03 -0700 | [diff] [blame] | 803 | deref_var = new(ctx) ir_dereference_variable(var); |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 804 | |
Ian Romanick | 8e9ce2e | 2010-08-03 15:02:35 -0700 | [diff] [blame] | 805 | if (!error_emitted) |
Eric Anholt | aa5ec13 | 2012-05-14 09:14:54 -0700 | [diff] [blame] | 806 | instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var)); |
Eric Anholt | 2731a73 | 2010-06-23 14:51:14 -0700 | [diff] [blame] | 807 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 808 | return new(ctx) ir_dereference_variable(var); |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 809 | } |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 810 | |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 811 | static ir_rvalue * |
Eric Anholt | 959a9ec | 2010-06-23 14:43:50 -0700 | [diff] [blame] | 812 | get_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue) |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 813 | { |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 814 | void *ctx = ralloc_parent(lvalue); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 815 | ir_variable *var; |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 816 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 817 | var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp", |
| 818 | ir_var_temporary); |
Eric Anholt | 43b5b03 | 2010-07-07 14:04:30 -0700 | [diff] [blame] | 819 | instructions->push_tail(var); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 820 | var->mode = ir_var_auto; |
| 821 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 822 | instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var), |
Eric Anholt | aa5ec13 | 2012-05-14 09:14:54 -0700 | [diff] [blame] | 823 | lvalue)); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 824 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 825 | return new(ctx) ir_dereference_variable(var); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 829 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 830 | ast_node::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 831 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 832 | { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 833 | (void) instructions; |
| 834 | (void) state; |
| 835 | |
| 836 | return NULL; |
| 837 | } |
| 838 | |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 839 | static ir_rvalue * |
| 840 | do_comparison(void *mem_ctx, int operation, ir_rvalue *op0, ir_rvalue *op1) |
| 841 | { |
| 842 | int join_op; |
Ian Romanick | 6d36be5 | 2010-12-02 12:17:36 -0800 | [diff] [blame] | 843 | ir_rvalue *cmp = NULL; |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 844 | |
| 845 | if (operation == ir_binop_all_equal) |
| 846 | join_op = ir_binop_logic_and; |
| 847 | else |
| 848 | join_op = ir_binop_logic_or; |
| 849 | |
| 850 | switch (op0->type->base_type) { |
| 851 | case GLSL_TYPE_FLOAT: |
| 852 | case GLSL_TYPE_UINT: |
| 853 | case GLSL_TYPE_INT: |
| 854 | case GLSL_TYPE_BOOL: |
| 855 | return new(mem_ctx) ir_expression(operation, op0, op1); |
| 856 | |
| 857 | case GLSL_TYPE_ARRAY: { |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 858 | for (unsigned int i = 0; i < op0->type->length; i++) { |
| 859 | ir_rvalue *e0, *e1, *result; |
| 860 | |
| 861 | e0 = new(mem_ctx) ir_dereference_array(op0->clone(mem_ctx, NULL), |
| 862 | new(mem_ctx) ir_constant(i)); |
| 863 | e1 = new(mem_ctx) ir_dereference_array(op1->clone(mem_ctx, NULL), |
| 864 | new(mem_ctx) ir_constant(i)); |
| 865 | result = do_comparison(mem_ctx, operation, e0, e1); |
| 866 | |
Ian Romanick | 6d36be5 | 2010-12-02 12:17:36 -0800 | [diff] [blame] | 867 | if (cmp) { |
| 868 | cmp = new(mem_ctx) ir_expression(join_op, cmp, result); |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 869 | } else { |
Ian Romanick | 6d36be5 | 2010-12-02 12:17:36 -0800 | [diff] [blame] | 870 | cmp = result; |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 871 | } |
| 872 | } |
Eric Anholt | b4f5856 | 2010-12-01 15:55:53 -0800 | [diff] [blame] | 873 | |
| 874 | mark_whole_array_access(op0); |
| 875 | mark_whole_array_access(op1); |
Ian Romanick | 6d36be5 | 2010-12-02 12:17:36 -0800 | [diff] [blame] | 876 | break; |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | case GLSL_TYPE_STRUCT: { |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 880 | for (unsigned int i = 0; i < op0->type->length; i++) { |
| 881 | ir_rvalue *e0, *e1, *result; |
| 882 | const char *field_name = op0->type->fields.structure[i].name; |
| 883 | |
| 884 | e0 = new(mem_ctx) ir_dereference_record(op0->clone(mem_ctx, NULL), |
| 885 | field_name); |
| 886 | e1 = new(mem_ctx) ir_dereference_record(op1->clone(mem_ctx, NULL), |
| 887 | field_name); |
| 888 | result = do_comparison(mem_ctx, operation, e0, e1); |
| 889 | |
Ian Romanick | 6d36be5 | 2010-12-02 12:17:36 -0800 | [diff] [blame] | 890 | if (cmp) { |
| 891 | cmp = new(mem_ctx) ir_expression(join_op, cmp, result); |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 892 | } else { |
Ian Romanick | 6d36be5 | 2010-12-02 12:17:36 -0800 | [diff] [blame] | 893 | cmp = result; |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 894 | } |
| 895 | } |
Ian Romanick | 6d36be5 | 2010-12-02 12:17:36 -0800 | [diff] [blame] | 896 | break; |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | case GLSL_TYPE_ERROR: |
| 900 | case GLSL_TYPE_VOID: |
| 901 | case GLSL_TYPE_SAMPLER: |
Ian Romanick | 491364e | 2012-12-11 12:11:16 -0800 | [diff] [blame] | 902 | case GLSL_TYPE_INTERFACE: |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 903 | /* I assume a comparison of a struct containing a sampler just |
| 904 | * ignores the sampler present in the type. |
| 905 | */ |
Ian Romanick | 6d36be5 | 2010-12-02 12:17:36 -0800 | [diff] [blame] | 906 | break; |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 907 | } |
Eric Anholt | d56c974 | 2010-11-30 13:28:47 -0800 | [diff] [blame] | 908 | |
Ian Romanick | 6d36be5 | 2010-12-02 12:17:36 -0800 | [diff] [blame] | 909 | if (cmp == NULL) |
| 910 | cmp = new(mem_ctx) ir_constant(true); |
| 911 | |
| 912 | return cmp; |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 913 | } |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 914 | |
Eric Anholt | 0182270 | 2011-04-09 15:59:20 -1000 | [diff] [blame] | 915 | /* For logical operations, we want to ensure that the operands are |
| 916 | * scalar booleans. If it isn't, emit an error and return a constant |
| 917 | * boolean to avoid triggering cascading error messages. |
| 918 | */ |
| 919 | ir_rvalue * |
| 920 | get_scalar_boolean_operand(exec_list *instructions, |
| 921 | struct _mesa_glsl_parse_state *state, |
| 922 | ast_expression *parent_expr, |
| 923 | int operand, |
| 924 | const char *operand_name, |
| 925 | bool *error_emitted) |
| 926 | { |
| 927 | ast_expression *expr = parent_expr->subexpressions[operand]; |
| 928 | void *ctx = state; |
| 929 | ir_rvalue *val = expr->hir(instructions, state); |
| 930 | |
| 931 | if (val->type->is_boolean() && val->type->is_scalar()) |
| 932 | return val; |
| 933 | |
| 934 | if (!*error_emitted) { |
| 935 | YYLTYPE loc = expr->get_location(); |
| 936 | _mesa_glsl_error(&loc, state, "%s of `%s' must be scalar boolean", |
| 937 | operand_name, |
| 938 | parent_expr->operator_string(parent_expr->oper)); |
| 939 | *error_emitted = true; |
| 940 | } |
| 941 | |
| 942 | return new(ctx) ir_constant(true); |
| 943 | } |
| 944 | |
Paul Berry | 93b9758 | 2011-09-06 10:01:51 -0700 | [diff] [blame] | 945 | /** |
| 946 | * If name refers to a builtin array whose maximum allowed size is less than |
| 947 | * size, report an error and return true. Otherwise return false. |
| 948 | */ |
Ian Romanick | 2c333a8 | 2013-03-15 14:33:01 -0700 | [diff] [blame] | 949 | void |
Paul Berry | 93b9758 | 2011-09-06 10:01:51 -0700 | [diff] [blame] | 950 | check_builtin_array_max_size(const char *name, unsigned size, |
| 951 | YYLTYPE loc, struct _mesa_glsl_parse_state *state) |
| 952 | { |
| 953 | if ((strcmp("gl_TexCoord", name) == 0) |
| 954 | && (size > state->Const.MaxTextureCoords)) { |
| 955 | /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec: |
| 956 | * |
| 957 | * "The size [of gl_TexCoord] can be at most |
| 958 | * gl_MaxTextureCoords." |
| 959 | */ |
| 960 | _mesa_glsl_error(&loc, state, "`gl_TexCoord' array size cannot " |
| 961 | "be larger than gl_MaxTextureCoords (%u)\n", |
| 962 | state->Const.MaxTextureCoords); |
Paul Berry | 37bb1c4 | 2011-08-11 15:23:33 -0700 | [diff] [blame] | 963 | } else if (strcmp("gl_ClipDistance", name) == 0 |
| 964 | && size > state->Const.MaxClipPlanes) { |
| 965 | /* From section 7.1 (Vertex Shader Special Variables) of the |
| 966 | * GLSL 1.30 spec: |
| 967 | * |
| 968 | * "The gl_ClipDistance array is predeclared as unsized and |
| 969 | * must be sized by the shader either redeclaring it with a |
| 970 | * size or indexing it only with integral constant |
| 971 | * expressions. ... The size can be at most |
| 972 | * gl_MaxClipDistances." |
| 973 | */ |
| 974 | _mesa_glsl_error(&loc, state, "`gl_ClipDistance' array size cannot " |
| 975 | "be larger than gl_MaxClipDistances (%u)\n", |
| 976 | state->Const.MaxClipPlanes); |
Paul Berry | 93b9758 | 2011-09-06 10:01:51 -0700 | [diff] [blame] | 977 | } |
Paul Berry | 93b9758 | 2011-09-06 10:01:51 -0700 | [diff] [blame] | 978 | } |
| 979 | |
Paul Berry | 9abda92 | 2011-10-31 18:22:48 -0700 | [diff] [blame] | 980 | /** |
| 981 | * Create the constant 1, of a which is appropriate for incrementing and |
| 982 | * decrementing values of the given GLSL type. For example, if type is vec4, |
| 983 | * this creates a constant value of 1.0 having type float. |
| 984 | * |
| 985 | * If the given type is invalid for increment and decrement operators, return |
| 986 | * a floating point 1--the error will be detected later. |
| 987 | */ |
| 988 | static ir_rvalue * |
| 989 | constant_one_for_inc_dec(void *ctx, const glsl_type *type) |
| 990 | { |
| 991 | switch (type->base_type) { |
| 992 | case GLSL_TYPE_UINT: |
| 993 | return new(ctx) ir_constant((unsigned) 1); |
| 994 | case GLSL_TYPE_INT: |
| 995 | return new(ctx) ir_constant(1); |
| 996 | default: |
| 997 | case GLSL_TYPE_FLOAT: |
| 998 | return new(ctx) ir_constant(1.0f); |
| 999 | } |
| 1000 | } |
| 1001 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 1002 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1003 | ast_expression::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1004 | struct _mesa_glsl_parse_state *state) |
| 1005 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 1006 | void *ctx = state; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1007 | static const int operations[AST_NUM_OPERATORS] = { |
| 1008 | -1, /* ast_assign doesn't convert to ir_expression. */ |
| 1009 | -1, /* ast_plus doesn't convert to ir_expression. */ |
| 1010 | ir_unop_neg, |
| 1011 | ir_binop_add, |
| 1012 | ir_binop_sub, |
| 1013 | ir_binop_mul, |
| 1014 | ir_binop_div, |
| 1015 | ir_binop_mod, |
| 1016 | ir_binop_lshift, |
| 1017 | ir_binop_rshift, |
| 1018 | ir_binop_less, |
| 1019 | ir_binop_greater, |
| 1020 | ir_binop_lequal, |
| 1021 | ir_binop_gequal, |
Luca Barbieri | 4dfb899 | 2010-09-08 01:31:39 +0200 | [diff] [blame] | 1022 | ir_binop_all_equal, |
| 1023 | ir_binop_any_nequal, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1024 | ir_binop_bit_and, |
| 1025 | ir_binop_bit_xor, |
| 1026 | ir_binop_bit_or, |
| 1027 | ir_unop_bit_not, |
| 1028 | ir_binop_logic_and, |
| 1029 | ir_binop_logic_xor, |
| 1030 | ir_binop_logic_or, |
| 1031 | ir_unop_logic_not, |
| 1032 | |
| 1033 | /* Note: The following block of expression types actually convert |
| 1034 | * to multiple IR instructions. |
| 1035 | */ |
| 1036 | ir_binop_mul, /* ast_mul_assign */ |
| 1037 | ir_binop_div, /* ast_div_assign */ |
| 1038 | ir_binop_mod, /* ast_mod_assign */ |
| 1039 | ir_binop_add, /* ast_add_assign */ |
| 1040 | ir_binop_sub, /* ast_sub_assign */ |
| 1041 | ir_binop_lshift, /* ast_ls_assign */ |
| 1042 | ir_binop_rshift, /* ast_rs_assign */ |
| 1043 | ir_binop_bit_and, /* ast_and_assign */ |
| 1044 | ir_binop_bit_xor, /* ast_xor_assign */ |
| 1045 | ir_binop_bit_or, /* ast_or_assign */ |
| 1046 | |
| 1047 | -1, /* ast_conditional doesn't convert to ir_expression. */ |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1048 | ir_binop_add, /* ast_pre_inc. */ |
| 1049 | ir_binop_sub, /* ast_pre_dec. */ |
| 1050 | ir_binop_add, /* ast_post_inc. */ |
| 1051 | ir_binop_sub, /* ast_post_dec. */ |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1052 | -1, /* ast_field_selection doesn't conv to ir_expression. */ |
| 1053 | -1, /* ast_array_index doesn't convert to ir_expression. */ |
| 1054 | -1, /* ast_function_call doesn't conv to ir_expression. */ |
| 1055 | -1, /* ast_identifier doesn't convert to ir_expression. */ |
| 1056 | -1, /* ast_int_constant doesn't convert to ir_expression. */ |
| 1057 | -1, /* ast_uint_constant doesn't conv to ir_expression. */ |
| 1058 | -1, /* ast_float_constant doesn't conv to ir_expression. */ |
| 1059 | -1, /* ast_bool_constant doesn't conv to ir_expression. */ |
| 1060 | -1, /* ast_sequence doesn't convert to ir_expression. */ |
| 1061 | }; |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 1062 | ir_rvalue *result = NULL; |
Aras Pranckevicius | 1c325af | 2010-07-29 15:35:22 +0300 | [diff] [blame] | 1063 | ir_rvalue *op[3]; |
Kenneth Graunke | 08ba977 | 2011-04-14 17:21:59 -0700 | [diff] [blame] | 1064 | const struct glsl_type *type; /* a temporary variable for switch cases */ |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1065 | bool error_emitted = false; |
| 1066 | YYLTYPE loc; |
| 1067 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1068 | loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1069 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1070 | switch (this->oper) { |
Matt Turner | ae79e86 | 2013-06-29 19:27:50 -0700 | [diff] [blame] | 1071 | case ast_aggregate: |
| 1072 | assert(!"ast_aggregate: Should never get here."); |
| 1073 | break; |
| 1074 | |
Ian Romanick | 6652af3 | 2010-03-09 16:38:02 -0800 | [diff] [blame] | 1075 | case ast_assign: { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1076 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1077 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1078 | |
Ian Romanick | e9015e9 | 2011-12-23 09:56:29 -0800 | [diff] [blame] | 1079 | result = do_assignment(instructions, state, |
| 1080 | this->subexpressions[0]->non_lvalue_description, |
| 1081 | op[0], op[1], false, |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 1082 | this->subexpressions[0]->get_location()); |
| 1083 | error_emitted = result->type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1084 | break; |
Ian Romanick | 6652af3 | 2010-03-09 16:38:02 -0800 | [diff] [blame] | 1085 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1086 | |
| 1087 | case ast_plus: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1088 | op[0] = this->subexpressions[0]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1089 | |
Carl Worth | c24bcad | 2010-07-21 11:23:51 -0700 | [diff] [blame] | 1090 | type = unary_arithmetic_result_type(op[0]->type, state, & loc); |
| 1091 | |
| 1092 | error_emitted = type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1093 | |
| 1094 | result = op[0]; |
| 1095 | break; |
| 1096 | |
| 1097 | case ast_neg: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1098 | op[0] = this->subexpressions[0]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1099 | |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 1100 | type = unary_arithmetic_result_type(op[0]->type, state, & loc); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1101 | |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 1102 | error_emitted = type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1103 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1104 | result = new(ctx) ir_expression(operations[this->oper], type, |
| 1105 | op[0], NULL); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1106 | break; |
| 1107 | |
| 1108 | case ast_add: |
| 1109 | case ast_sub: |
| 1110 | case ast_mul: |
| 1111 | case ast_div: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1112 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1113 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1114 | |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 1115 | type = arithmetic_result_type(op[0], op[1], |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1116 | (this->oper == ast_mul), |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 1117 | state, & loc); |
| 1118 | error_emitted = type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1119 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1120 | result = new(ctx) ir_expression(operations[this->oper], type, |
| 1121 | op[0], op[1]); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1122 | break; |
| 1123 | |
| 1124 | case ast_mod: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1125 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1126 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1127 | |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 1128 | type = modulus_result_type(op[0]->type, op[1]->type, state, & loc); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1129 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1130 | assert(operations[this->oper] == ir_binop_mod); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1131 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1132 | result = new(ctx) ir_expression(operations[this->oper], type, |
| 1133 | op[0], op[1]); |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 1134 | error_emitted = type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1135 | break; |
| 1136 | |
| 1137 | case ast_lshift: |
| 1138 | case ast_rshift: |
Paul Berry | 0d9bba6 | 2012-08-05 09:57:01 -0700 | [diff] [blame] | 1139 | if (!state->check_bitwise_operations_allowed(&loc)) { |
Chad Versace | 5c4c36f | 2010-10-08 16:22:28 -0700 | [diff] [blame] | 1140 | error_emitted = true; |
Chad Versace | 5c4c36f | 2010-10-08 16:22:28 -0700 | [diff] [blame] | 1141 | } |
| 1142 | |
Chad Versace | 5c4c36f | 2010-10-08 16:22:28 -0700 | [diff] [blame] | 1143 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1144 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Chad Versace | c0197ab | 2010-10-15 09:49:46 -0700 | [diff] [blame] | 1145 | type = shift_result_type(op[0]->type, op[1]->type, this->oper, state, |
| 1146 | &loc); |
Chad Versace | 5c4c36f | 2010-10-08 16:22:28 -0700 | [diff] [blame] | 1147 | result = new(ctx) ir_expression(operations[this->oper], type, |
| 1148 | op[0], op[1]); |
| 1149 | error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); |
| 1150 | break; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1151 | |
| 1152 | case ast_less: |
| 1153 | case ast_greater: |
| 1154 | case ast_lequal: |
| 1155 | case ast_gequal: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1156 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1157 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1158 | |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 1159 | type = relational_result_type(op[0], op[1], state, & loc); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1160 | |
| 1161 | /* The relational operators must either generate an error or result |
| 1162 | * in a scalar boolean. See page 57 of the GLSL 1.50 spec. |
| 1163 | */ |
Ian Romanick | a43817a | 2010-03-26 14:27:23 -0700 | [diff] [blame] | 1164 | assert(type->is_error() |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1165 | || ((type->base_type == GLSL_TYPE_BOOL) |
Ian Romanick | cb36f8a | 2010-03-09 15:51:22 -0800 | [diff] [blame] | 1166 | && type->is_scalar())); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1167 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1168 | result = new(ctx) ir_expression(operations[this->oper], type, |
| 1169 | op[0], op[1]); |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 1170 | error_emitted = type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1171 | break; |
| 1172 | |
| 1173 | case ast_nequal: |
| 1174 | case ast_equal: |
Ian Romanick | 6e659ca | 2010-03-29 15:11:05 -0700 | [diff] [blame] | 1175 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1176 | op[1] = this->subexpressions[1]->hir(instructions, state); |
| 1177 | |
| 1178 | /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec: |
| 1179 | * |
| 1180 | * "The equality operators equal (==), and not equal (!=) |
| 1181 | * operate on all types. They result in a scalar Boolean. If |
| 1182 | * the operand types do not match, then there must be a |
| 1183 | * conversion from Section 4.1.10 "Implicit Conversions" |
| 1184 | * applied to one operand that can make them match, in which |
| 1185 | * case this conversion is done." |
| 1186 | */ |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 1187 | if ((!apply_implicit_conversion(op[0]->type, op[1], state) |
| 1188 | && !apply_implicit_conversion(op[1]->type, op[0], state)) |
Ian Romanick | 212b032 | 2010-03-29 16:22:38 -0700 | [diff] [blame] | 1189 | || (op[0]->type != op[1]->type)) { |
Ian Romanick | 6e659ca | 2010-03-29 15:11:05 -0700 | [diff] [blame] | 1190 | _mesa_glsl_error(& loc, state, "operands of `%s' must have the same " |
| 1191 | "type", (this->oper == ast_equal) ? "==" : "!="); |
| 1192 | error_emitted = true; |
Paul Berry | 0d9bba6 | 2012-08-05 09:57:01 -0700 | [diff] [blame] | 1193 | } else if ((op[0]->type->is_array() || op[1]->type->is_array()) && |
Paul Berry | d4a2474 | 2012-08-02 08:18:12 -0700 | [diff] [blame] | 1194 | !state->check_version(120, 300, &loc, |
Paul Berry | 0d9bba6 | 2012-08-05 09:57:01 -0700 | [diff] [blame] | 1195 | "array comparisons forbidden")) { |
Ian Romanick | a80cbd6 | 2010-03-30 17:04:48 -0700 | [diff] [blame] | 1196 | error_emitted = true; |
Ian Romanick | 6e659ca | 2010-03-29 15:11:05 -0700 | [diff] [blame] | 1197 | } |
| 1198 | |
Eric Anholt | 175829f | 2011-04-09 12:54:34 -1000 | [diff] [blame] | 1199 | if (error_emitted) { |
| 1200 | result = new(ctx) ir_constant(false); |
| 1201 | } else { |
| 1202 | result = do_comparison(ctx, operations[this->oper], op[0], op[1]); |
| 1203 | assert(result->type == glsl_type::bool_type); |
Eric Anholt | 175829f | 2011-04-09 12:54:34 -1000 | [diff] [blame] | 1204 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1205 | break; |
| 1206 | |
| 1207 | case ast_bit_and: |
| 1208 | case ast_bit_xor: |
| 1209 | case ast_bit_or: |
Kenneth Graunke | 1eea963 | 2010-08-31 10:56:24 -0700 | [diff] [blame] | 1210 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1211 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Chad Versace | cfdbf8b | 2010-10-15 11:28:05 -0700 | [diff] [blame] | 1212 | type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper, |
| 1213 | state, &loc); |
Kenneth Graunke | 1eea963 | 2010-08-31 10:56:24 -0700 | [diff] [blame] | 1214 | result = new(ctx) ir_expression(operations[this->oper], type, |
| 1215 | op[0], op[1]); |
| 1216 | error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); |
| 1217 | break; |
| 1218 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1219 | case ast_bit_not: |
Kenneth Graunke | 1eea963 | 2010-08-31 10:56:24 -0700 | [diff] [blame] | 1220 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1221 | |
Paul Berry | 0d9bba6 | 2012-08-05 09:57:01 -0700 | [diff] [blame] | 1222 | if (!state->check_bitwise_operations_allowed(&loc)) { |
Kenneth Graunke | 1eea963 | 2010-08-31 10:56:24 -0700 | [diff] [blame] | 1223 | error_emitted = true; |
| 1224 | } |
| 1225 | |
| 1226 | if (!op[0]->type->is_integer()) { |
| 1227 | _mesa_glsl_error(&loc, state, "operand of `~' must be an integer"); |
| 1228 | error_emitted = true; |
| 1229 | } |
| 1230 | |
Ian Romanick | 3946448 | 2011-12-23 17:16:43 -0800 | [diff] [blame] | 1231 | type = error_emitted ? glsl_type::error_type : op[0]->type; |
Kenneth Graunke | 1eea963 | 2010-08-31 10:56:24 -0700 | [diff] [blame] | 1232 | result = new(ctx) ir_expression(ir_unop_bit_not, type, op[0], NULL); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1233 | break; |
| 1234 | |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 1235 | case ast_logic_and: { |
Eric Anholt | 7ec0c97 | 2011-04-09 10:27:02 -1000 | [diff] [blame] | 1236 | exec_list rhs_instructions; |
Eric Anholt | 0182270 | 2011-04-09 15:59:20 -1000 | [diff] [blame] | 1237 | op[0] = get_scalar_boolean_operand(instructions, state, this, 0, |
| 1238 | "LHS", &error_emitted); |
Eric Anholt | 7ec0c97 | 2011-04-09 10:27:02 -1000 | [diff] [blame] | 1239 | op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1, |
| 1240 | "RHS", &error_emitted); |
Eric Anholt | b82c0c3 | 2010-03-31 09:11:39 -1000 | [diff] [blame] | 1241 | |
Eric Anholt | ead3589 | 2012-02-07 22:59:24 -0800 | [diff] [blame] | 1242 | if (rhs_instructions.is_empty()) { |
| 1243 | result = new(ctx) ir_expression(ir_binop_logic_and, op[0], op[1]); |
| 1244 | type = result->type; |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1245 | } else { |
Ian Romanick | 81d664f | 2010-07-12 15:18:55 -0700 | [diff] [blame] | 1246 | ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type, |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 1247 | "and_tmp", |
| 1248 | ir_var_temporary); |
Ian Romanick | 81d664f | 2010-07-12 15:18:55 -0700 | [diff] [blame] | 1249 | instructions->push_tail(tmp); |
| 1250 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1251 | ir_if *const stmt = new(ctx) ir_if(op[0]); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1252 | instructions->push_tail(stmt); |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 1253 | |
Eric Anholt | 7ec0c97 | 2011-04-09 10:27:02 -1000 | [diff] [blame] | 1254 | stmt->then_instructions.append_list(&rhs_instructions); |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1255 | ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1256 | ir_assignment *const then_assign = |
Eric Anholt | aa5ec13 | 2012-05-14 09:14:54 -0700 | [diff] [blame] | 1257 | new(ctx) ir_assignment(then_deref, op[1]); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1258 | stmt->then_instructions.push_tail(then_assign); |
| 1259 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1260 | ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1261 | ir_assignment *const else_assign = |
Eric Anholt | aa5ec13 | 2012-05-14 09:14:54 -0700 | [diff] [blame] | 1262 | new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false)); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1263 | stmt->else_instructions.push_tail(else_assign); |
| 1264 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1265 | result = new(ctx) ir_dereference_variable(tmp); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1266 | type = tmp->type; |
Eric Anholt | b82c0c3 | 2010-03-31 09:11:39 -1000 | [diff] [blame] | 1267 | } |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 1268 | break; |
| 1269 | } |
| 1270 | |
| 1271 | case ast_logic_or: { |
Eric Anholt | 9e04b19 | 2011-04-09 10:27:02 -1000 | [diff] [blame] | 1272 | exec_list rhs_instructions; |
Eric Anholt | 0182270 | 2011-04-09 15:59:20 -1000 | [diff] [blame] | 1273 | op[0] = get_scalar_boolean_operand(instructions, state, this, 0, |
| 1274 | "LHS", &error_emitted); |
Eric Anholt | 9e04b19 | 2011-04-09 10:27:02 -1000 | [diff] [blame] | 1275 | op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1, |
| 1276 | "RHS", &error_emitted); |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 1277 | |
Eric Anholt | ead3589 | 2012-02-07 22:59:24 -0800 | [diff] [blame] | 1278 | if (rhs_instructions.is_empty()) { |
| 1279 | result = new(ctx) ir_expression(ir_binop_logic_or, op[0], op[1]); |
| 1280 | type = result->type; |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1281 | } else { |
Kenneth Graunke | dfd30ca | 2010-07-08 12:40:52 -0700 | [diff] [blame] | 1282 | ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type, |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 1283 | "or_tmp", |
| 1284 | ir_var_temporary); |
Ian Romanick | 0b9ae3b | 2010-07-12 14:22:05 -0700 | [diff] [blame] | 1285 | instructions->push_tail(tmp); |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 1286 | |
Ian Romanick | 81d664f | 2010-07-12 15:18:55 -0700 | [diff] [blame] | 1287 | ir_if *const stmt = new(ctx) ir_if(op[0]); |
| 1288 | instructions->push_tail(stmt); |
| 1289 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1290 | ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1291 | ir_assignment *const then_assign = |
Eric Anholt | aa5ec13 | 2012-05-14 09:14:54 -0700 | [diff] [blame] | 1292 | new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true)); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1293 | stmt->then_instructions.push_tail(then_assign); |
| 1294 | |
Eric Anholt | 9e04b19 | 2011-04-09 10:27:02 -1000 | [diff] [blame] | 1295 | stmt->else_instructions.append_list(&rhs_instructions); |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1296 | ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1297 | ir_assignment *const else_assign = |
Eric Anholt | aa5ec13 | 2012-05-14 09:14:54 -0700 | [diff] [blame] | 1298 | new(ctx) ir_assignment(else_deref, op[1]); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1299 | stmt->else_instructions.push_tail(else_assign); |
| 1300 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1301 | result = new(ctx) ir_dereference_variable(tmp); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1302 | type = tmp->type; |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 1303 | } |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 1304 | break; |
| 1305 | } |
| 1306 | |
| 1307 | case ast_logic_xor: |
Eric Anholt | 756c262 | 2011-04-09 14:57:17 -1000 | [diff] [blame] | 1308 | /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec: |
| 1309 | * |
| 1310 | * "The logical binary operators and (&&), or ( | | ), and |
| 1311 | * exclusive or (^^). They operate only on two Boolean |
| 1312 | * expressions and result in a Boolean expression." |
| 1313 | */ |
| 1314 | op[0] = get_scalar_boolean_operand(instructions, state, this, 0, "LHS", |
| 1315 | &error_emitted); |
| 1316 | op[1] = get_scalar_boolean_operand(instructions, state, this, 1, "RHS", |
| 1317 | &error_emitted); |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 1318 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1319 | result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type, |
| 1320 | op[0], op[1]); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1321 | break; |
| 1322 | |
Eric Anholt | a5827fe | 2010-03-31 16:50:55 -1000 | [diff] [blame] | 1323 | case ast_logic_not: |
Eric Anholt | 0182270 | 2011-04-09 15:59:20 -1000 | [diff] [blame] | 1324 | op[0] = get_scalar_boolean_operand(instructions, state, this, 0, |
| 1325 | "operand", &error_emitted); |
Eric Anholt | a5827fe | 2010-03-31 16:50:55 -1000 | [diff] [blame] | 1326 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1327 | result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type, |
| 1328 | op[0], NULL); |
Eric Anholt | a5827fe | 2010-03-31 16:50:55 -1000 | [diff] [blame] | 1329 | break; |
| 1330 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1331 | case ast_mul_assign: |
| 1332 | case ast_div_assign: |
| 1333 | case ast_add_assign: |
| 1334 | case ast_sub_assign: { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1335 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1336 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1337 | |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 1338 | type = arithmetic_result_type(op[0], op[1], |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1339 | (this->oper == ast_mul_assign), |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 1340 | state, & loc); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1341 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1342 | ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type, |
| 1343 | op[0], op[1]); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1344 | |
Eric Anholt | 3e24ef6 | 2010-06-23 12:40:17 -0700 | [diff] [blame] | 1345 | result = do_assignment(instructions, state, |
Ian Romanick | e9015e9 | 2011-12-23 09:56:29 -0800 | [diff] [blame] | 1346 | this->subexpressions[0]->non_lvalue_description, |
Ian Romanick | 85caea2 | 2011-03-15 16:33:27 -0700 | [diff] [blame] | 1347 | op[0]->clone(ctx, NULL), temp_rhs, false, |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 1348 | this->subexpressions[0]->get_location()); |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 1349 | error_emitted = (op[0]->type->is_error()); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1350 | |
| 1351 | /* GLSL 1.10 does not allow array assignment. However, we don't have to |
| 1352 | * explicitly test for this because none of the binary expression |
| 1353 | * operators allow array operands either. |
| 1354 | */ |
| 1355 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1356 | break; |
| 1357 | } |
| 1358 | |
Eric Anholt | 48a0e64 | 2010-03-26 11:57:46 -0700 | [diff] [blame] | 1359 | case ast_mod_assign: { |
| 1360 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1361 | op[1] = this->subexpressions[1]->hir(instructions, state); |
| 1362 | |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 1363 | type = modulus_result_type(op[0]->type, op[1]->type, state, & loc); |
Eric Anholt | 48a0e64 | 2010-03-26 11:57:46 -0700 | [diff] [blame] | 1364 | |
| 1365 | assert(operations[this->oper] == ir_binop_mod); |
| 1366 | |
Ian Romanick | 768b55a | 2010-08-13 16:46:43 -0700 | [diff] [blame] | 1367 | ir_rvalue *temp_rhs; |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1368 | temp_rhs = new(ctx) ir_expression(operations[this->oper], type, |
| 1369 | op[0], op[1]); |
Eric Anholt | 48a0e64 | 2010-03-26 11:57:46 -0700 | [diff] [blame] | 1370 | |
Eric Anholt | 3e24ef6 | 2010-06-23 12:40:17 -0700 | [diff] [blame] | 1371 | result = do_assignment(instructions, state, |
Ian Romanick | e9015e9 | 2011-12-23 09:56:29 -0800 | [diff] [blame] | 1372 | this->subexpressions[0]->non_lvalue_description, |
Ian Romanick | 85caea2 | 2011-03-15 16:33:27 -0700 | [diff] [blame] | 1373 | op[0]->clone(ctx, NULL), temp_rhs, false, |
Eric Anholt | 48a0e64 | 2010-03-26 11:57:46 -0700 | [diff] [blame] | 1374 | this->subexpressions[0]->get_location()); |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 1375 | error_emitted = type->is_error(); |
Eric Anholt | 48a0e64 | 2010-03-26 11:57:46 -0700 | [diff] [blame] | 1376 | break; |
| 1377 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1378 | |
| 1379 | case ast_ls_assign: |
Chad Versace | 338ed6e | 2010-10-15 10:05:50 -0700 | [diff] [blame] | 1380 | case ast_rs_assign: { |
| 1381 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1382 | op[1] = this->subexpressions[1]->hir(instructions, state); |
| 1383 | type = shift_result_type(op[0]->type, op[1]->type, this->oper, state, |
| 1384 | &loc); |
| 1385 | ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], |
| 1386 | type, op[0], op[1]); |
Ian Romanick | e9015e9 | 2011-12-23 09:56:29 -0800 | [diff] [blame] | 1387 | result = do_assignment(instructions, state, |
| 1388 | this->subexpressions[0]->non_lvalue_description, |
| 1389 | op[0]->clone(ctx, NULL), temp_rhs, false, |
Chad Versace | 338ed6e | 2010-10-15 10:05:50 -0700 | [diff] [blame] | 1390 | this->subexpressions[0]->get_location()); |
| 1391 | error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); |
Ian Romanick | 251eb75 | 2010-03-29 14:15:05 -0700 | [diff] [blame] | 1392 | break; |
Chad Versace | 338ed6e | 2010-10-15 10:05:50 -0700 | [diff] [blame] | 1393 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1394 | |
| 1395 | case ast_and_assign: |
| 1396 | case ast_xor_assign: |
Chad Versace | d03ac0f | 2010-10-15 12:08:28 -0700 | [diff] [blame] | 1397 | case ast_or_assign: { |
| 1398 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1399 | op[1] = this->subexpressions[1]->hir(instructions, state); |
| 1400 | type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper, |
| 1401 | state, &loc); |
| 1402 | ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], |
| 1403 | type, op[0], op[1]); |
Ian Romanick | e9015e9 | 2011-12-23 09:56:29 -0800 | [diff] [blame] | 1404 | result = do_assignment(instructions, state, |
| 1405 | this->subexpressions[0]->non_lvalue_description, |
| 1406 | op[0]->clone(ctx, NULL), temp_rhs, false, |
Chad Versace | d03ac0f | 2010-10-15 12:08:28 -0700 | [diff] [blame] | 1407 | this->subexpressions[0]->get_location()); |
| 1408 | error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); |
Ian Romanick | 251eb75 | 2010-03-29 14:15:05 -0700 | [diff] [blame] | 1409 | break; |
Chad Versace | d03ac0f | 2010-10-15 12:08:28 -0700 | [diff] [blame] | 1410 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1411 | |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 1412 | case ast_conditional: { |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 1413 | /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec: |
| 1414 | * |
| 1415 | * "The ternary selection operator (?:). It operates on three |
| 1416 | * expressions (exp1 ? exp2 : exp3). This operator evaluates the |
| 1417 | * first expression, which must result in a scalar Boolean." |
| 1418 | */ |
Eric Anholt | 0182270 | 2011-04-09 15:59:20 -1000 | [diff] [blame] | 1419 | op[0] = get_scalar_boolean_operand(instructions, state, this, 0, |
| 1420 | "condition", &error_emitted); |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 1421 | |
| 1422 | /* The :? operator is implemented by generating an anonymous temporary |
| 1423 | * followed by an if-statement. The last instruction in each branch of |
| 1424 | * the if-statement assigns a value to the anonymous temporary. This |
| 1425 | * temporary is the r-value of the expression. |
| 1426 | */ |
Ian Romanick | 0ad76c6 | 2010-06-11 12:56:26 -0700 | [diff] [blame] | 1427 | exec_list then_instructions; |
| 1428 | exec_list else_instructions; |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 1429 | |
Ian Romanick | 0ad76c6 | 2010-06-11 12:56:26 -0700 | [diff] [blame] | 1430 | op[1] = this->subexpressions[1]->hir(&then_instructions, state); |
| 1431 | op[2] = this->subexpressions[2]->hir(&else_instructions, state); |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 1432 | |
| 1433 | /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec: |
| 1434 | * |
| 1435 | * "The second and third expressions can be any type, as |
| 1436 | * long their types match, or there is a conversion in |
| 1437 | * Section 4.1.10 "Implicit Conversions" that can be applied |
| 1438 | * to one of the expressions to make their types match. This |
| 1439 | * resulting matching type is the type of the entire |
| 1440 | * expression." |
| 1441 | */ |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 1442 | if ((!apply_implicit_conversion(op[1]->type, op[2], state) |
| 1443 | && !apply_implicit_conversion(op[2]->type, op[1], state)) |
Ian Romanick | db9be2e | 2010-03-29 16:25:56 -0700 | [diff] [blame] | 1444 | || (op[1]->type != op[2]->type)) { |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 1445 | YYLTYPE loc = this->subexpressions[1]->get_location(); |
| 1446 | |
| 1447 | _mesa_glsl_error(& loc, state, "Second and third operands of ?: " |
| 1448 | "operator must have matching types."); |
| 1449 | error_emitted = true; |
Ian Romanick | 0ad76c6 | 2010-06-11 12:56:26 -0700 | [diff] [blame] | 1450 | type = glsl_type::error_type; |
Ian Romanick | db9be2e | 2010-03-29 16:25:56 -0700 | [diff] [blame] | 1451 | } else { |
Ian Romanick | 0ad76c6 | 2010-06-11 12:56:26 -0700 | [diff] [blame] | 1452 | type = op[1]->type; |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 1453 | } |
| 1454 | |
Ian Romanick | f09fabc | 2010-09-07 14:30:06 -0700 | [diff] [blame] | 1455 | /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec: |
| 1456 | * |
| 1457 | * "The second and third expressions must be the same type, but can |
| 1458 | * be of any type other than an array." |
| 1459 | */ |
Paul Berry | 0d9bba6 | 2012-08-05 09:57:01 -0700 | [diff] [blame] | 1460 | if (type->is_array() && |
Paul Berry | d4a2474 | 2012-08-02 08:18:12 -0700 | [diff] [blame] | 1461 | !state->check_version(120, 300, &loc, |
Paul Berry | 0d9bba6 | 2012-08-05 09:57:01 -0700 | [diff] [blame] | 1462 | "Second and third operands of ?: operator " |
| 1463 | "cannot be arrays")) { |
Ian Romanick | f09fabc | 2010-09-07 14:30:06 -0700 | [diff] [blame] | 1464 | error_emitted = true; |
| 1465 | } |
| 1466 | |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1467 | ir_constant *cond_val = op[0]->constant_expression_value(); |
| 1468 | ir_constant *then_val = op[1]->constant_expression_value(); |
| 1469 | ir_constant *else_val = op[2]->constant_expression_value(); |
Ian Romanick | 0ad76c6 | 2010-06-11 12:56:26 -0700 | [diff] [blame] | 1470 | |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1471 | if (then_instructions.is_empty() |
| 1472 | && else_instructions.is_empty() |
| 1473 | && (cond_val != NULL) && (then_val != NULL) && (else_val != NULL)) { |
| 1474 | result = (cond_val->value.b[0]) ? then_val : else_val; |
| 1475 | } else { |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 1476 | ir_variable *const tmp = |
| 1477 | new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary); |
Ian Romanick | 0b9ae3b | 2010-07-12 14:22:05 -0700 | [diff] [blame] | 1478 | instructions->push_tail(tmp); |
Ian Romanick | 0ad76c6 | 2010-06-11 12:56:26 -0700 | [diff] [blame] | 1479 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1480 | ir_if *const stmt = new(ctx) ir_if(op[0]); |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1481 | instructions->push_tail(stmt); |
Ian Romanick | 0ad76c6 | 2010-06-11 12:56:26 -0700 | [diff] [blame] | 1482 | |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1483 | then_instructions.move_nodes_to(& stmt->then_instructions); |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1484 | ir_dereference *const then_deref = |
| 1485 | new(ctx) ir_dereference_variable(tmp); |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1486 | ir_assignment *const then_assign = |
Eric Anholt | aa5ec13 | 2012-05-14 09:14:54 -0700 | [diff] [blame] | 1487 | new(ctx) ir_assignment(then_deref, op[1]); |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1488 | stmt->then_instructions.push_tail(then_assign); |
Ian Romanick | 0ad76c6 | 2010-06-11 12:56:26 -0700 | [diff] [blame] | 1489 | |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1490 | else_instructions.move_nodes_to(& stmt->else_instructions); |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1491 | ir_dereference *const else_deref = |
| 1492 | new(ctx) ir_dereference_variable(tmp); |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1493 | ir_assignment *const else_assign = |
Eric Anholt | aa5ec13 | 2012-05-14 09:14:54 -0700 | [diff] [blame] | 1494 | new(ctx) ir_assignment(else_deref, op[2]); |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1495 | stmt->else_instructions.push_tail(else_assign); |
| 1496 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1497 | result = new(ctx) ir_dereference_variable(tmp); |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1498 | } |
Ian Romanick | 251eb75 | 2010-03-29 14:15:05 -0700 | [diff] [blame] | 1499 | break; |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 1500 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1501 | |
| 1502 | case ast_pre_inc: |
Eric Anholt | 76ea56c | 2010-03-26 12:16:54 -0700 | [diff] [blame] | 1503 | case ast_pre_dec: { |
Ian Romanick | fa0a9ac | 2011-12-23 09:56:03 -0800 | [diff] [blame] | 1504 | this->non_lvalue_description = (this->oper == ast_pre_inc) |
| 1505 | ? "pre-increment operation" : "pre-decrement operation"; |
| 1506 | |
Eric Anholt | 76ea56c | 2010-03-26 12:16:54 -0700 | [diff] [blame] | 1507 | op[0] = this->subexpressions[0]->hir(instructions, state); |
Paul Berry | 9abda92 | 2011-10-31 18:22:48 -0700 | [diff] [blame] | 1508 | op[1] = constant_one_for_inc_dec(ctx, op[0]->type); |
Eric Anholt | 76ea56c | 2010-03-26 12:16:54 -0700 | [diff] [blame] | 1509 | |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 1510 | type = arithmetic_result_type(op[0], op[1], false, state, & loc); |
Eric Anholt | 76ea56c | 2010-03-26 12:16:54 -0700 | [diff] [blame] | 1511 | |
Ian Romanick | 768b55a | 2010-08-13 16:46:43 -0700 | [diff] [blame] | 1512 | ir_rvalue *temp_rhs; |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1513 | temp_rhs = new(ctx) ir_expression(operations[this->oper], type, |
| 1514 | op[0], op[1]); |
Eric Anholt | 76ea56c | 2010-03-26 12:16:54 -0700 | [diff] [blame] | 1515 | |
Eric Anholt | 3e24ef6 | 2010-06-23 12:40:17 -0700 | [diff] [blame] | 1516 | result = do_assignment(instructions, state, |
Ian Romanick | e9015e9 | 2011-12-23 09:56:29 -0800 | [diff] [blame] | 1517 | this->subexpressions[0]->non_lvalue_description, |
Ian Romanick | 85caea2 | 2011-03-15 16:33:27 -0700 | [diff] [blame] | 1518 | op[0]->clone(ctx, NULL), temp_rhs, false, |
Eric Anholt | 76ea56c | 2010-03-26 12:16:54 -0700 | [diff] [blame] | 1519 | this->subexpressions[0]->get_location()); |
Eric Anholt | 76ea56c | 2010-03-26 12:16:54 -0700 | [diff] [blame] | 1520 | error_emitted = op[0]->type->is_error(); |
| 1521 | break; |
| 1522 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1523 | |
| 1524 | case ast_post_inc: |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1525 | case ast_post_dec: { |
Ian Romanick | fa0a9ac | 2011-12-23 09:56:03 -0800 | [diff] [blame] | 1526 | this->non_lvalue_description = (this->oper == ast_post_inc) |
| 1527 | ? "post-increment operation" : "post-decrement operation"; |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1528 | op[0] = this->subexpressions[0]->hir(instructions, state); |
Paul Berry | 9abda92 | 2011-10-31 18:22:48 -0700 | [diff] [blame] | 1529 | op[1] = constant_one_for_inc_dec(ctx, op[0]->type); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1530 | |
| 1531 | error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); |
| 1532 | |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 1533 | type = arithmetic_result_type(op[0], op[1], false, state, & loc); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1534 | |
Ian Romanick | 768b55a | 2010-08-13 16:46:43 -0700 | [diff] [blame] | 1535 | ir_rvalue *temp_rhs; |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1536 | temp_rhs = new(ctx) ir_expression(operations[this->oper], type, |
| 1537 | op[0], op[1]); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1538 | |
| 1539 | /* Get a temporary of a copy of the lvalue before it's modified. |
| 1540 | * This may get thrown away later. |
| 1541 | */ |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 1542 | result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL)); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1543 | |
Eric Anholt | 3e24ef6 | 2010-06-23 12:40:17 -0700 | [diff] [blame] | 1544 | (void)do_assignment(instructions, state, |
Ian Romanick | e9015e9 | 2011-12-23 09:56:29 -0800 | [diff] [blame] | 1545 | this->subexpressions[0]->non_lvalue_description, |
Ian Romanick | 85caea2 | 2011-03-15 16:33:27 -0700 | [diff] [blame] | 1546 | op[0]->clone(ctx, NULL), temp_rhs, false, |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1547 | this->subexpressions[0]->get_location()); |
| 1548 | |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1549 | error_emitted = op[0]->type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1550 | break; |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1551 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1552 | |
| 1553 | case ast_field_selection: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1554 | result = _mesa_ast_field_selection_to_hir(this, instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1555 | break; |
| 1556 | |
Ian Romanick | 27e3cf8 | 2010-04-01 18:03:59 -0700 | [diff] [blame] | 1557 | case ast_array_index: { |
| 1558 | YYLTYPE index_loc = subexpressions[1]->get_location(); |
| 1559 | |
| 1560 | op[0] = subexpressions[0]->hir(instructions, state); |
| 1561 | op[1] = subexpressions[1]->hir(instructions, state); |
| 1562 | |
Ian Romanick | 46934ad | 2013-03-15 14:10:12 -0700 | [diff] [blame] | 1563 | result = _mesa_ast_array_index_to_hir(ctx, state, op[0], op[1], |
Ian Romanick | 58d93e3 | 2013-03-15 15:23:19 -0700 | [diff] [blame] | 1564 | loc, index_loc); |
Ian Romanick | b8a21cc | 2010-04-01 18:31:11 -0700 | [diff] [blame] | 1565 | |
Ian Romanick | 46934ad | 2013-03-15 14:10:12 -0700 | [diff] [blame] | 1566 | if (result->type->is_error()) |
Ian Romanick | 27e3cf8 | 2010-04-01 18:03:59 -0700 | [diff] [blame] | 1567 | error_emitted = true; |
Ian Romanick | 27e3cf8 | 2010-04-01 18:03:59 -0700 | [diff] [blame] | 1568 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1569 | break; |
Ian Romanick | 27e3cf8 | 2010-04-01 18:03:59 -0700 | [diff] [blame] | 1570 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1571 | |
| 1572 | case ast_function_call: |
Ian Romanick | 7cfddf1 | 2010-03-10 13:26:52 -0800 | [diff] [blame] | 1573 | /* Should *NEVER* get here. ast_function_call should always be handled |
| 1574 | * by ast_function_expression::hir. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1575 | */ |
Ian Romanick | 7cfddf1 | 2010-03-10 13:26:52 -0800 | [diff] [blame] | 1576 | assert(0); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1577 | break; |
| 1578 | |
| 1579 | case ast_identifier: { |
| 1580 | /* ast_identifier can appear several places in a full abstract syntax |
| 1581 | * tree. This particular use must be at location specified in the grammar |
| 1582 | * as 'variable_identifier'. |
| 1583 | */ |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 1584 | ir_variable *var = |
| 1585 | state->symbols->get_variable(this->primary_expression.identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1586 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1587 | if (var != NULL) { |
Ian Romanick | bd33055 | 2011-01-07 18:34:58 -0800 | [diff] [blame] | 1588 | var->used = true; |
Kenneth Graunke | dca19a7 | 2012-03-13 14:59:42 -0700 | [diff] [blame] | 1589 | result = new(ctx) ir_dereference_variable(var); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1590 | } else { |
Ian Romanick | 71d0bbf | 2010-03-23 13:21:19 -0700 | [diff] [blame] | 1591 | _mesa_glsl_error(& loc, state, "`%s' undeclared", |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1592 | this->primary_expression.identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1593 | |
Kenneth Graunke | 807e967 | 2011-09-22 15:04:56 -0700 | [diff] [blame] | 1594 | result = ir_rvalue::error_value(ctx); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1595 | error_emitted = true; |
| 1596 | } |
| 1597 | break; |
| 1598 | } |
| 1599 | |
| 1600 | case ast_int_constant: |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1601 | result = new(ctx) ir_constant(this->primary_expression.int_constant); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1602 | break; |
| 1603 | |
| 1604 | case ast_uint_constant: |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1605 | result = new(ctx) ir_constant(this->primary_expression.uint_constant); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1606 | break; |
| 1607 | |
| 1608 | case ast_float_constant: |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1609 | result = new(ctx) ir_constant(this->primary_expression.float_constant); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1610 | break; |
| 1611 | |
| 1612 | case ast_bool_constant: |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1613 | result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant)); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1614 | break; |
| 1615 | |
| 1616 | case ast_sequence: { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1617 | /* It should not be possible to generate a sequence in the AST without |
| 1618 | * any expressions in it. |
| 1619 | */ |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 1620 | assert(!this->expressions.is_empty()); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1621 | |
| 1622 | /* The r-value of a sequence is the last expression in the sequence. If |
| 1623 | * the other expressions in the sequence do not have side-effects (and |
| 1624 | * therefore add instructions to the instruction list), they get dropped |
| 1625 | * on the floor. |
| 1626 | */ |
Ian Romanick | 3d5cfcf | 2011-04-11 10:10:30 -0700 | [diff] [blame] | 1627 | exec_node *previous_tail_pred = NULL; |
| 1628 | YYLTYPE previous_operand_loc = loc; |
| 1629 | |
| 1630 | foreach_list_typed (ast_node, ast, link, &this->expressions) { |
| 1631 | /* If one of the operands of comma operator does not generate any |
| 1632 | * code, we want to emit a warning. At each pass through the loop |
| 1633 | * previous_tail_pred will point to the last instruction in the |
| 1634 | * stream *before* processing the previous operand. Naturally, |
| 1635 | * instructions->tail_pred will point to the last instruction in the |
| 1636 | * stream *after* processing the previous operand. If the two |
| 1637 | * pointers match, then the previous operand had no effect. |
| 1638 | * |
| 1639 | * The warning behavior here differs slightly from GCC. GCC will |
| 1640 | * only emit a warning if none of the left-hand operands have an |
| 1641 | * effect. However, it will emit a warning for each. I believe that |
| 1642 | * there are some cases in C (especially with GCC extensions) where |
| 1643 | * it is useful to have an intermediate step in a sequence have no |
| 1644 | * effect, but I don't think these cases exist in GLSL. Either way, |
| 1645 | * it would be a giant hassle to replicate that behavior. |
| 1646 | */ |
| 1647 | if (previous_tail_pred == instructions->tail_pred) { |
| 1648 | _mesa_glsl_warning(&previous_operand_loc, state, |
| 1649 | "left-hand operand of comma expression has " |
| 1650 | "no effect"); |
| 1651 | } |
| 1652 | |
| 1653 | /* tail_pred is directly accessed instead of using the get_tail() |
| 1654 | * method for performance reasons. get_tail() has extra code to |
| 1655 | * return NULL when the list is empty. We don't care about that |
| 1656 | * here, so using tail_pred directly is fine. |
| 1657 | */ |
| 1658 | previous_tail_pred = instructions->tail_pred; |
| 1659 | previous_operand_loc = ast->get_location(); |
| 1660 | |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 1661 | result = ast->hir(instructions, state); |
Ian Romanick | 3d5cfcf | 2011-04-11 10:10:30 -0700 | [diff] [blame] | 1662 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1663 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1664 | /* Any errors should have already been emitted in the loop above. |
| 1665 | */ |
| 1666 | error_emitted = true; |
| 1667 | break; |
| 1668 | } |
| 1669 | } |
Kenneth Graunke | 08ba977 | 2011-04-14 17:21:59 -0700 | [diff] [blame] | 1670 | type = NULL; /* use result->type, not type. */ |
| 1671 | assert(result != NULL); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1672 | |
Kenneth Graunke | 08ba977 | 2011-04-14 17:21:59 -0700 | [diff] [blame] | 1673 | if (result->type->is_error() && !error_emitted) |
Ian Romanick | 71d0bbf | 2010-03-23 13:21:19 -0700 | [diff] [blame] | 1674 | _mesa_glsl_error(& loc, state, "type mismatch"); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1675 | |
| 1676 | return result; |
| 1677 | } |
| 1678 | |
| 1679 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 1680 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1681 | ast_expression_statement::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1682 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1683 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1684 | /* It is possible to have expression statements that don't have an |
| 1685 | * expression. This is the solitary semicolon: |
| 1686 | * |
| 1687 | * for (i = 0; i < 5; i++) |
| 1688 | * ; |
| 1689 | * |
| 1690 | * In this case the expression will be NULL. Test for NULL and don't do |
| 1691 | * anything in that case. |
| 1692 | */ |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1693 | if (expression != NULL) |
| 1694 | expression->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1695 | |
| 1696 | /* Statements do not have r-values. |
| 1697 | */ |
| 1698 | return NULL; |
| 1699 | } |
| 1700 | |
| 1701 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 1702 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1703 | ast_compound_statement::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1704 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1705 | { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1706 | if (new_scope) |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 1707 | state->symbols->push_scope(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1708 | |
Ian Romanick | 2b97dc6 | 2010-05-10 17:42:05 -0700 | [diff] [blame] | 1709 | foreach_list_typed (ast_node, ast, link, &this->statements) |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 1710 | ast->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1711 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1712 | if (new_scope) |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 1713 | state->symbols->pop_scope(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1714 | |
| 1715 | /* Compound statements do not have r-values. |
| 1716 | */ |
| 1717 | return NULL; |
| 1718 | } |
| 1719 | |
| 1720 | |
Ian Romanick | 28009cd | 2010-03-30 16:59:27 -0700 | [diff] [blame] | 1721 | static const glsl_type * |
Kenneth Graunke | d8e34e2 | 2010-08-07 02:56:01 -0700 | [diff] [blame] | 1722 | process_array_type(YYLTYPE *loc, const glsl_type *base, ast_node *array_size, |
Ian Romanick | 28009cd | 2010-03-30 16:59:27 -0700 | [diff] [blame] | 1723 | struct _mesa_glsl_parse_state *state) |
| 1724 | { |
| 1725 | unsigned length = 0; |
| 1726 | |
Ian Romanick | ee55b84 | 2013-04-08 16:53:46 -0700 | [diff] [blame] | 1727 | if (base == NULL) |
| 1728 | return glsl_type::error_type; |
| 1729 | |
Ian Romanick | a04211e | 2011-10-24 11:45:50 -0700 | [diff] [blame] | 1730 | /* From page 19 (page 25) of the GLSL 1.20 spec: |
| 1731 | * |
| 1732 | * "Only one-dimensional arrays may be declared." |
| 1733 | */ |
| 1734 | if (base->is_array()) { |
| 1735 | _mesa_glsl_error(loc, state, |
| 1736 | "invalid array of `%s' (only one-dimensional arrays " |
| 1737 | "may be declared)", |
| 1738 | base->name); |
| 1739 | return glsl_type::error_type; |
| 1740 | } |
Ian Romanick | 28009cd | 2010-03-30 16:59:27 -0700 | [diff] [blame] | 1741 | |
| 1742 | if (array_size != NULL) { |
| 1743 | exec_list dummy_instructions; |
| 1744 | ir_rvalue *const ir = array_size->hir(& dummy_instructions, state); |
| 1745 | YYLTYPE loc = array_size->get_location(); |
| 1746 | |
Ian Romanick | 28009cd | 2010-03-30 16:59:27 -0700 | [diff] [blame] | 1747 | if (ir != NULL) { |
| 1748 | if (!ir->type->is_integer()) { |
| 1749 | _mesa_glsl_error(& loc, state, "array size must be integer type"); |
| 1750 | } else if (!ir->type->is_scalar()) { |
| 1751 | _mesa_glsl_error(& loc, state, "array size must be scalar type"); |
| 1752 | } else { |
| 1753 | ir_constant *const size = ir->constant_expression_value(); |
| 1754 | |
| 1755 | if (size == NULL) { |
| 1756 | _mesa_glsl_error(& loc, state, "array size must be a " |
| 1757 | "constant valued expression"); |
| 1758 | } else if (size->value.i[0] <= 0) { |
| 1759 | _mesa_glsl_error(& loc, state, "array size must be > 0"); |
| 1760 | } else { |
| 1761 | assert(size->type == ir->type); |
| 1762 | length = size->value.u[0]; |
Paul Berry | d4144a1 | 2011-08-01 15:23:07 -0700 | [diff] [blame] | 1763 | |
| 1764 | /* If the array size is const (and we've verified that |
| 1765 | * it is) then no instructions should have been emitted |
| 1766 | * when we converted it to HIR. If they were emitted, |
| 1767 | * then either the array size isn't const after all, or |
| 1768 | * we are emitting unnecessary instructions. |
| 1769 | */ |
| 1770 | assert(dummy_instructions.is_empty()); |
Ian Romanick | 28009cd | 2010-03-30 16:59:27 -0700 | [diff] [blame] | 1771 | } |
| 1772 | } |
| 1773 | } |
Kenneth Graunke | d8e34e2 | 2010-08-07 02:56:01 -0700 | [diff] [blame] | 1774 | } else if (state->es_shader) { |
| 1775 | /* Section 10.17 of the GLSL ES 1.00 specification states that unsized |
| 1776 | * array declarations have been removed from the language. |
| 1777 | */ |
| 1778 | _mesa_glsl_error(loc, state, "unsized array declarations are not " |
| 1779 | "allowed in GLSL ES 1.00."); |
Ian Romanick | 28009cd | 2010-03-30 16:59:27 -0700 | [diff] [blame] | 1780 | } |
| 1781 | |
Ian Romanick | ee55b84 | 2013-04-08 16:53:46 -0700 | [diff] [blame] | 1782 | const glsl_type *array_type = glsl_type::get_array_instance(base, length); |
| 1783 | return array_type != NULL ? array_type : glsl_type::error_type; |
Ian Romanick | 28009cd | 2010-03-30 16:59:27 -0700 | [diff] [blame] | 1784 | } |
| 1785 | |
| 1786 | |
Ian Romanick | d612a12 | 2010-03-31 16:22:06 -0700 | [diff] [blame] | 1787 | const glsl_type * |
| 1788 | ast_type_specifier::glsl_type(const char **name, |
| 1789 | struct _mesa_glsl_parse_state *state) const |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1790 | { |
Ian Romanick | d612a12 | 2010-03-31 16:22:06 -0700 | [diff] [blame] | 1791 | const struct glsl_type *type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1792 | |
Kenneth Graunke | ca92ae2 | 2010-09-18 11:11:09 +0200 | [diff] [blame] | 1793 | type = state->symbols->get_type(this->type_name); |
| 1794 | *name = this->type_name; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1795 | |
Kenneth Graunke | ca92ae2 | 2010-09-18 11:11:09 +0200 | [diff] [blame] | 1796 | if (this->is_array) { |
| 1797 | YYLTYPE loc = this->get_location(); |
| 1798 | type = process_array_type(&loc, type, this->array_size, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1799 | } |
| 1800 | |
| 1801 | return type; |
| 1802 | } |
| 1803 | |
| 1804 | |
Paul Berry | 1872055 | 2012-12-18 15:24:39 -0800 | [diff] [blame] | 1805 | /** |
| 1806 | * Determine whether a toplevel variable declaration declares a varying. This |
| 1807 | * function operates by examining the variable's mode and the shader target, |
| 1808 | * so it correctly identifies linkage variables regardless of whether they are |
| 1809 | * declared using the deprecated "varying" syntax or the new "in/out" syntax. |
| 1810 | * |
| 1811 | * Passing a non-toplevel variable declaration (e.g. a function parameter) to |
| 1812 | * this function will produce undefined results. |
| 1813 | */ |
| 1814 | static bool |
| 1815 | is_varying_var(ir_variable *var, _mesa_glsl_parser_targets target) |
| 1816 | { |
| 1817 | switch (target) { |
| 1818 | case vertex_shader: |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 1819 | return var->mode == ir_var_shader_out; |
Paul Berry | 1872055 | 2012-12-18 15:24:39 -0800 | [diff] [blame] | 1820 | case fragment_shader: |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 1821 | return var->mode == ir_var_shader_in; |
Paul Berry | 1872055 | 2012-12-18 15:24:39 -0800 | [diff] [blame] | 1822 | default: |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 1823 | return var->mode == ir_var_shader_out || var->mode == ir_var_shader_in; |
Paul Berry | 1872055 | 2012-12-18 15:24:39 -0800 | [diff] [blame] | 1824 | } |
| 1825 | } |
| 1826 | |
| 1827 | |
Ian Romanick | bb47a4d | 2013-01-16 12:01:50 -0800 | [diff] [blame] | 1828 | /** |
| 1829 | * Matrix layout qualifiers are only allowed on certain types |
| 1830 | */ |
| 1831 | static void |
| 1832 | validate_matrix_layout_for_type(struct _mesa_glsl_parse_state *state, |
| 1833 | YYLTYPE *loc, |
| 1834 | const glsl_type *type) |
| 1835 | { |
| 1836 | if (!type->is_matrix() && !type->is_record()) { |
| 1837 | _mesa_glsl_error(loc, state, |
| 1838 | "uniform block layout qualifiers row_major and " |
| 1839 | "column_major can only be applied to matrix and " |
| 1840 | "structure types"); |
| 1841 | } else if (type->is_record()) { |
| 1842 | /* We allow 'layout(row_major)' on structure types because it's the only |
| 1843 | * way to get row-major layouts on matrices contained in structures. |
| 1844 | */ |
| 1845 | _mesa_glsl_warning(loc, state, |
| 1846 | "uniform block layout qualifiers row_major and " |
| 1847 | "column_major applied to structure types is not " |
| 1848 | "strictly conformant and my be rejected by other " |
| 1849 | "compilers"); |
| 1850 | } |
| 1851 | } |
| 1852 | |
Kenneth Graunke | 5e5e120 | 2013-07-16 12:03:28 -0700 | [diff] [blame] | 1853 | static bool |
| 1854 | validate_binding_qualifier(struct _mesa_glsl_parse_state *state, |
| 1855 | YYLTYPE *loc, |
| 1856 | ir_variable *var, |
| 1857 | const ast_type_qualifier *qual) |
| 1858 | { |
| 1859 | if (var->mode != ir_var_uniform) { |
| 1860 | _mesa_glsl_error(loc, state, |
| 1861 | "the \"binding\" qualifier only applies to uniforms.\n"); |
| 1862 | return false; |
| 1863 | } |
| 1864 | |
| 1865 | if (qual->binding < 0) { |
| 1866 | _mesa_glsl_error(loc, state, "binding values must be >= 0.\n"); |
| 1867 | return false; |
| 1868 | } |
| 1869 | |
| 1870 | const struct gl_context *const ctx = state->ctx; |
| 1871 | unsigned elements = var->type->is_array() ? var->type->length : 1; |
| 1872 | unsigned max_index = qual->binding + elements - 1; |
| 1873 | |
| 1874 | if (var->type->is_interface()) { |
| 1875 | /* UBOs. From page 60 of the GLSL 4.20 specification: |
| 1876 | * "If the binding point for any uniform block instance is less than zero, |
| 1877 | * or greater than or equal to the implementation-dependent maximum |
| 1878 | * number of uniform buffer bindings, a compilation error will occur. |
| 1879 | * When the binding identifier is used with a uniform block instanced as |
| 1880 | * an array of size N, all elements of the array from binding through |
| 1881 | * binding + N – 1 must be within this range." |
| 1882 | * |
| 1883 | * The implementation-dependent maximum is GL_MAX_UNIFORM_BUFFER_BINDINGS. |
| 1884 | */ |
| 1885 | if (max_index >= ctx->Const.MaxUniformBufferBindings) { |
| 1886 | _mesa_glsl_error(loc, state, "layout(binding = %d) for %d UBOs exceeds " |
| 1887 | "the maximum number of UBO binding points (%d).\n", |
| 1888 | qual->binding, elements, |
| 1889 | ctx->Const.MaxUniformBufferBindings); |
| 1890 | return false; |
| 1891 | } |
| 1892 | } else if (var->type->is_sampler() || |
| 1893 | (var->type->is_array() && var->type->fields.array->is_sampler())) { |
| 1894 | /* Samplers. From page 63 of the GLSL 4.20 specification: |
| 1895 | * "If the binding is less than zero, or greater than or equal to the |
| 1896 | * implementation-dependent maximum supported number of units, a |
| 1897 | * compilation error will occur. When the binding identifier is used |
| 1898 | * with an array of size N, all elements of the array from binding |
| 1899 | * through binding + N - 1 must be within this range." |
| 1900 | */ |
| 1901 | unsigned limit; |
| 1902 | switch (state->target) { |
| 1903 | case vertex_shader: |
| 1904 | limit = ctx->Const.VertexProgram.MaxTextureImageUnits; |
| 1905 | break; |
| 1906 | case geometry_shader: |
| 1907 | limit = ctx->Const.GeometryProgram.MaxTextureImageUnits; |
| 1908 | break; |
| 1909 | case fragment_shader: |
| 1910 | limit = ctx->Const.FragmentProgram.MaxTextureImageUnits; |
| 1911 | break; |
| 1912 | } |
| 1913 | |
| 1914 | if (max_index >= limit) { |
| 1915 | _mesa_glsl_error(loc, state, "layout(binding = %d) for %d samplers " |
| 1916 | "exceeds the maximum number of texture image units " |
| 1917 | "(%d).\n", qual->binding, elements, limit); |
| 1918 | |
| 1919 | return false; |
| 1920 | } |
| 1921 | } else { |
| 1922 | _mesa_glsl_error(loc, state, |
| 1923 | "the \"binding\" qualifier only applies to uniform " |
| 1924 | "blocks, samplers, or arrays of samplers.\n"); |
| 1925 | return false; |
| 1926 | } |
| 1927 | |
| 1928 | return true; |
| 1929 | } |
| 1930 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1931 | static void |
| 1932 | apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual, |
Ian Romanick | 768b55a | 2010-08-13 16:46:43 -0700 | [diff] [blame] | 1933 | ir_variable *var, |
Eric Anholt | 2e063f1 | 2010-03-28 00:56:22 -0700 | [diff] [blame] | 1934 | struct _mesa_glsl_parse_state *state, |
Eric Anholt | f7561e8 | 2012-04-26 18:19:39 -0700 | [diff] [blame] | 1935 | YYLTYPE *loc, |
Paul Berry | c33be48 | 2012-12-18 14:49:34 -0800 | [diff] [blame] | 1936 | bool ubo_qualifiers_valid, |
| 1937 | bool is_parameter) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1938 | { |
Ian Romanick | bd33055 | 2011-01-07 18:34:58 -0800 | [diff] [blame] | 1939 | if (qual->flags.q.invariant) { |
| 1940 | if (var->used) { |
| 1941 | _mesa_glsl_error(loc, state, |
| 1942 | "variable `%s' may not be redeclared " |
| 1943 | "`invariant' after being used", |
| 1944 | var->name); |
| 1945 | } else { |
| 1946 | var->invariant = 1; |
| 1947 | } |
| 1948 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1949 | |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 1950 | if (qual->flags.q.constant || qual->flags.q.attribute |
| 1951 | || qual->flags.q.uniform |
| 1952 | || (qual->flags.q.varying && (state->target == fragment_shader))) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1953 | var->read_only = 1; |
| 1954 | |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 1955 | if (qual->flags.q.centroid) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1956 | var->centroid = 1; |
| 1957 | |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 1958 | if (qual->flags.q.attribute && state->target != vertex_shader) { |
Eric Anholt | 2e063f1 | 2010-03-28 00:56:22 -0700 | [diff] [blame] | 1959 | var->type = glsl_type::error_type; |
| 1960 | _mesa_glsl_error(loc, state, |
| 1961 | "`attribute' variables may not be declared in the " |
Ian Romanick | ae4c4c0 | 2010-04-07 16:41:40 -0700 | [diff] [blame] | 1962 | "%s shader", |
| 1963 | _mesa_glsl_shader_target_name(state->target)); |
Eric Anholt | 2e063f1 | 2010-03-28 00:56:22 -0700 | [diff] [blame] | 1964 | } |
| 1965 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 1966 | /* If there is no qualifier that changes the mode of the variable, leave |
| 1967 | * the setting alone. |
| 1968 | */ |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 1969 | if (qual->flags.q.in && qual->flags.q.out) |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 1970 | var->mode = ir_var_function_inout; |
| 1971 | else if (qual->flags.q.in) |
| 1972 | var->mode = is_parameter ? ir_var_function_in : ir_var_shader_in; |
| 1973 | else if (qual->flags.q.attribute |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 1974 | || (qual->flags.q.varying && (state->target == fragment_shader))) |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 1975 | var->mode = ir_var_shader_in; |
| 1976 | else if (qual->flags.q.out) |
| 1977 | var->mode = is_parameter ? ir_var_function_out : ir_var_shader_out; |
| 1978 | else if (qual->flags.q.varying && (state->target == vertex_shader)) |
| 1979 | var->mode = ir_var_shader_out; |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 1980 | else if (qual->flags.q.uniform) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1981 | var->mode = ir_var_uniform; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1982 | |
Paul Berry | 1872055 | 2012-12-18 15:24:39 -0800 | [diff] [blame] | 1983 | if (!is_parameter && is_varying_var(var, state->target)) { |
| 1984 | /* This variable is being used to link data between shader stages (in |
| 1985 | * pre-glsl-1.30 parlance, it's a "varying"). Check that it has a type |
| 1986 | * that is allowed for such purposes. |
| 1987 | * |
| 1988 | * From page 25 (page 31 of the PDF) of the GLSL 1.10 spec: |
| 1989 | * |
| 1990 | * "The varying qualifier can be used only with the data types |
| 1991 | * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of |
| 1992 | * these." |
| 1993 | * |
| 1994 | * This was relaxed in GLSL version 1.30 and GLSL ES version 3.00. From |
| 1995 | * page 31 (page 37 of the PDF) of the GLSL 1.30 spec: |
| 1996 | * |
| 1997 | * "Fragment inputs can only be signed and unsigned integers and |
| 1998 | * integer vectors, float, floating-point vectors, matrices, or |
| 1999 | * arrays of these. Structures cannot be input. |
| 2000 | * |
| 2001 | * Similar text exists in the section on vertex shader outputs. |
| 2002 | * |
| 2003 | * Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES |
Paul Berry | c6a50dd | 2013-01-04 10:43:01 -0800 | [diff] [blame] | 2004 | * 3.00 spec allows structs as well. Varying structs are also allowed |
| 2005 | * in GLSL 1.50. |
Paul Berry | 1872055 | 2012-12-18 15:24:39 -0800 | [diff] [blame] | 2006 | */ |
| 2007 | switch (var->type->get_scalar_type()->base_type) { |
| 2008 | case GLSL_TYPE_FLOAT: |
| 2009 | /* Ok in all GLSL versions */ |
| 2010 | break; |
| 2011 | case GLSL_TYPE_UINT: |
| 2012 | case GLSL_TYPE_INT: |
| 2013 | if (state->is_version(130, 300)) |
| 2014 | break; |
| 2015 | _mesa_glsl_error(loc, state, |
| 2016 | "varying variables must be of base type float in %s", |
| 2017 | state->get_version_string()); |
| 2018 | break; |
| 2019 | case GLSL_TYPE_STRUCT: |
Paul Berry | c6a50dd | 2013-01-04 10:43:01 -0800 | [diff] [blame] | 2020 | if (state->is_version(150, 300)) |
| 2021 | break; |
Paul Berry | 1872055 | 2012-12-18 15:24:39 -0800 | [diff] [blame] | 2022 | _mesa_glsl_error(loc, state, |
| 2023 | "varying variables may not be of type struct"); |
| 2024 | break; |
| 2025 | default: |
| 2026 | _mesa_glsl_error(loc, state, "illegal type for a varying variable"); |
| 2027 | break; |
| 2028 | } |
| 2029 | } |
| 2030 | |
Ian Romanick | 86b4398 | 2011-01-06 10:49:56 -0800 | [diff] [blame] | 2031 | if (state->all_invariant && (state->current_function == NULL)) { |
| 2032 | switch (state->target) { |
| 2033 | case vertex_shader: |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 2034 | if (var->mode == ir_var_shader_out) |
Ian Romanick | 86b4398 | 2011-01-06 10:49:56 -0800 | [diff] [blame] | 2035 | var->invariant = true; |
| 2036 | break; |
| 2037 | case geometry_shader: |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 2038 | if ((var->mode == ir_var_shader_in) |
| 2039 | || (var->mode == ir_var_shader_out)) |
Ian Romanick | 86b4398 | 2011-01-06 10:49:56 -0800 | [diff] [blame] | 2040 | var->invariant = true; |
| 2041 | break; |
| 2042 | case fragment_shader: |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 2043 | if (var->mode == ir_var_shader_in) |
Ian Romanick | 86b4398 | 2011-01-06 10:49:56 -0800 | [diff] [blame] | 2044 | var->invariant = true; |
| 2045 | break; |
| 2046 | } |
| 2047 | } |
| 2048 | |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 2049 | if (qual->flags.q.flat) |
Paul Berry | cf45949 | 2011-10-25 18:06:37 -0700 | [diff] [blame] | 2050 | var->interpolation = INTERP_QUALIFIER_FLAT; |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 2051 | else if (qual->flags.q.noperspective) |
Paul Berry | cf45949 | 2011-10-25 18:06:37 -0700 | [diff] [blame] | 2052 | var->interpolation = INTERP_QUALIFIER_NOPERSPECTIVE; |
Paul Berry | c488150 | 2011-10-21 07:40:37 -0700 | [diff] [blame] | 2053 | else if (qual->flags.q.smooth) |
Paul Berry | cf45949 | 2011-10-25 18:06:37 -0700 | [diff] [blame] | 2054 | var->interpolation = INTERP_QUALIFIER_SMOOTH; |
Paul Berry | c488150 | 2011-10-21 07:40:37 -0700 | [diff] [blame] | 2055 | else |
| 2056 | var->interpolation = INTERP_QUALIFIER_NONE; |
Ian Romanick | 9d97537 | 2010-04-02 17:17:47 -0700 | [diff] [blame] | 2057 | |
Eric Anholt | 916e206 | 2012-01-09 16:40:20 -0800 | [diff] [blame] | 2058 | if (var->interpolation != INTERP_QUALIFIER_NONE && |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 2059 | !(state->target == vertex_shader && var->mode == ir_var_shader_out) && |
| 2060 | !(state->target == fragment_shader && var->mode == ir_var_shader_in)) { |
Eric Anholt | 916e206 | 2012-01-09 16:40:20 -0800 | [diff] [blame] | 2061 | _mesa_glsl_error(loc, state, |
| 2062 | "interpolation qualifier `%s' can only be applied to " |
| 2063 | "vertex shader outputs and fragment shader inputs.", |
Kenneth Graunke | d43f418 | 2012-06-06 01:52:47 -0700 | [diff] [blame] | 2064 | var->interpolation_string()); |
Eric Anholt | 916e206 | 2012-01-09 16:40:20 -0800 | [diff] [blame] | 2065 | } |
| 2066 | |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 2067 | var->pixel_center_integer = qual->flags.q.pixel_center_integer; |
| 2068 | var->origin_upper_left = qual->flags.q.origin_upper_left; |
| 2069 | if ((qual->flags.q.origin_upper_left || qual->flags.q.pixel_center_integer) |
Ian Romanick | 8d8469e | 2010-06-30 17:48:09 -0700 | [diff] [blame] | 2070 | && (strcmp(var->name, "gl_FragCoord") != 0)) { |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 2071 | const char *const qual_string = (qual->flags.q.origin_upper_left) |
Ian Romanick | 8d8469e | 2010-06-30 17:48:09 -0700 | [diff] [blame] | 2072 | ? "origin_upper_left" : "pixel_center_integer"; |
| 2073 | |
| 2074 | _mesa_glsl_error(loc, state, |
| 2075 | "layout qualifier `%s' can only be applied to " |
| 2076 | "fragment shader input `gl_FragCoord'", |
| 2077 | qual_string); |
| 2078 | } |
| 2079 | |
Ian Romanick | eee68d3 | 2010-10-07 15:13:38 -0700 | [diff] [blame] | 2080 | if (qual->flags.q.explicit_location) { |
| 2081 | const bool global_scope = (state->current_function == NULL); |
| 2082 | bool fail = false; |
| 2083 | const char *string = ""; |
| 2084 | |
| 2085 | /* In the vertex shader only shader inputs can be given explicit |
| 2086 | * locations. |
| 2087 | * |
| 2088 | * In the fragment shader only shader outputs can be given explicit |
| 2089 | * locations. |
| 2090 | */ |
| 2091 | switch (state->target) { |
| 2092 | case vertex_shader: |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 2093 | if (!global_scope || (var->mode != ir_var_shader_in)) { |
Ian Romanick | eee68d3 | 2010-10-07 15:13:38 -0700 | [diff] [blame] | 2094 | fail = true; |
| 2095 | string = "input"; |
| 2096 | } |
| 2097 | break; |
| 2098 | |
| 2099 | case geometry_shader: |
| 2100 | _mesa_glsl_error(loc, state, |
| 2101 | "geometry shader variables cannot be given " |
| 2102 | "explicit locations\n"); |
| 2103 | break; |
| 2104 | |
| 2105 | case fragment_shader: |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 2106 | if (!global_scope || (var->mode != ir_var_shader_out)) { |
Ian Romanick | eee68d3 | 2010-10-07 15:13:38 -0700 | [diff] [blame] | 2107 | fail = true; |
| 2108 | string = "output"; |
| 2109 | } |
| 2110 | break; |
Kenneth Graunke | a75da2c | 2010-10-20 14:59:40 -0700 | [diff] [blame] | 2111 | }; |
Ian Romanick | eee68d3 | 2010-10-07 15:13:38 -0700 | [diff] [blame] | 2112 | |
| 2113 | if (fail) { |
| 2114 | _mesa_glsl_error(loc, state, |
| 2115 | "only %s shader %s variables can be given an " |
| 2116 | "explicit location\n", |
| 2117 | _mesa_glsl_shader_target_name(state->target), |
| 2118 | string); |
| 2119 | } else { |
| 2120 | var->explicit_location = true; |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 2121 | |
| 2122 | /* This bit of silliness is needed because invalid explicit locations |
| 2123 | * are supposed to be flagged during linking. Small negative values |
| 2124 | * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias |
| 2125 | * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS). |
| 2126 | * The linker needs to be able to differentiate these cases. This |
| 2127 | * ensures that negative values stay negative. |
| 2128 | */ |
| 2129 | if (qual->location >= 0) { |
| 2130 | var->location = (state->target == vertex_shader) |
| 2131 | ? (qual->location + VERT_ATTRIB_GENERIC0) |
| 2132 | : (qual->location + FRAG_RESULT_DATA0); |
| 2133 | } else { |
| 2134 | var->location = qual->location; |
| 2135 | } |
Kenneth Graunke | 354f2cb | 2012-08-31 16:04:19 -0700 | [diff] [blame] | 2136 | |
Dave Airlie | 1256a5d | 2012-03-24 13:33:41 +0000 | [diff] [blame] | 2137 | if (qual->flags.q.explicit_index) { |
Kenneth Graunke | 354f2cb | 2012-08-31 16:04:19 -0700 | [diff] [blame] | 2138 | /* From the GLSL 4.30 specification, section 4.4.2 (Output |
| 2139 | * Layout Qualifiers): |
| 2140 | * |
| 2141 | * "It is also a compile-time error if a fragment shader |
| 2142 | * sets a layout index to less than 0 or greater than 1." |
| 2143 | * |
| 2144 | * Older specifications don't mandate a behavior; we take |
| 2145 | * this as a clarification and always generate the error. |
| 2146 | */ |
| 2147 | if (qual->index < 0 || qual->index > 1) { |
| 2148 | _mesa_glsl_error(loc, state, |
| 2149 | "explicit index may only be 0 or 1\n"); |
| 2150 | } else { |
| 2151 | var->explicit_index = true; |
| 2152 | var->index = qual->index; |
| 2153 | } |
Dave Airlie | 1256a5d | 2012-03-24 13:33:41 +0000 | [diff] [blame] | 2154 | } |
Ian Romanick | eee68d3 | 2010-10-07 15:13:38 -0700 | [diff] [blame] | 2155 | } |
Dave Airlie | 1256a5d | 2012-03-24 13:33:41 +0000 | [diff] [blame] | 2156 | } else if (qual->flags.q.explicit_index) { |
| 2157 | _mesa_glsl_error(loc, state, |
| 2158 | "explicit index requires explicit location\n"); |
Ian Romanick | eee68d3 | 2010-10-07 15:13:38 -0700 | [diff] [blame] | 2159 | } |
| 2160 | |
Kenneth Graunke | d4375fc | 2013-07-18 15:11:57 -0700 | [diff] [blame^] | 2161 | if (qual->flags.q.explicit_binding && |
| 2162 | validate_binding_qualifier(state, loc, var, qual)) { |
| 2163 | var->explicit_binding = true; |
| 2164 | var->binding = qual->binding; |
| 2165 | } |
Kenneth Graunke | 5e5e120 | 2013-07-16 12:03:28 -0700 | [diff] [blame] | 2166 | |
Ian Romanick | 4bcff0c | 2011-01-07 16:53:59 -0800 | [diff] [blame] | 2167 | /* Does the declaration use the deprecated 'attribute' or 'varying' |
| 2168 | * keywords? |
| 2169 | */ |
| 2170 | const bool uses_deprecated_qualifier = qual->flags.q.attribute |
| 2171 | || qual->flags.q.varying; |
| 2172 | |
| 2173 | /* Is the 'layout' keyword used with parameters that allow relaxed checking. |
| 2174 | * Many implementations of GL_ARB_fragment_coord_conventions_enable and some |
| 2175 | * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable |
| 2176 | * allowed the layout qualifier to be used with 'varying' and 'attribute'. |
| 2177 | * These extensions and all following extensions that add the 'layout' |
| 2178 | * keyword have been modified to require the use of 'in' or 'out'. |
| 2179 | * |
| 2180 | * The following extension do not allow the deprecated keywords: |
| 2181 | * |
| 2182 | * GL_AMD_conservative_depth |
Marek Olšák | 6b43d6f | 2011-11-19 16:41:08 +0100 | [diff] [blame] | 2183 | * GL_ARB_conservative_depth |
Ian Romanick | 4bcff0c | 2011-01-07 16:53:59 -0800 | [diff] [blame] | 2184 | * GL_ARB_gpu_shader5 |
| 2185 | * GL_ARB_separate_shader_objects |
| 2186 | * GL_ARB_tesselation_shader |
| 2187 | * GL_ARB_transform_feedback3 |
| 2188 | * GL_ARB_uniform_buffer_object |
| 2189 | * |
| 2190 | * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5 |
| 2191 | * allow layout with the deprecated keywords. |
| 2192 | */ |
| 2193 | const bool relaxed_layout_qualifier_checking = |
| 2194 | state->ARB_fragment_coord_conventions_enable; |
| 2195 | |
Kenneth Graunke | 56bcde3 | 2013-07-16 11:39:01 -0700 | [diff] [blame] | 2196 | if (qual->has_layout() && uses_deprecated_qualifier) { |
Ian Romanick | 4bcff0c | 2011-01-07 16:53:59 -0800 | [diff] [blame] | 2197 | if (relaxed_layout_qualifier_checking) { |
| 2198 | _mesa_glsl_warning(loc, state, |
| 2199 | "`layout' qualifier may not be used with " |
| 2200 | "`attribute' or `varying'"); |
| 2201 | } else { |
| 2202 | _mesa_glsl_error(loc, state, |
| 2203 | "`layout' qualifier may not be used with " |
| 2204 | "`attribute' or `varying'"); |
| 2205 | } |
| 2206 | } |
| 2207 | |
Chad Versace | bc04d24 | 2011-01-27 01:40:26 -0800 | [diff] [blame] | 2208 | /* Layout qualifiers for gl_FragDepth, which are enabled by extension |
| 2209 | * AMD_conservative_depth. |
| 2210 | */ |
| 2211 | int depth_layout_count = qual->flags.q.depth_any |
| 2212 | + qual->flags.q.depth_greater |
| 2213 | + qual->flags.q.depth_less |
| 2214 | + qual->flags.q.depth_unchanged; |
| 2215 | if (depth_layout_count > 0 |
Marek Olšák | 6b43d6f | 2011-11-19 16:41:08 +0100 | [diff] [blame] | 2216 | && !state->AMD_conservative_depth_enable |
| 2217 | && !state->ARB_conservative_depth_enable) { |
Chad Versace | bc04d24 | 2011-01-27 01:40:26 -0800 | [diff] [blame] | 2218 | _mesa_glsl_error(loc, state, |
Marek Olšák | 6b43d6f | 2011-11-19 16:41:08 +0100 | [diff] [blame] | 2219 | "extension GL_AMD_conservative_depth or " |
| 2220 | "GL_ARB_conservative_depth must be enabled " |
Chad Versace | bc04d24 | 2011-01-27 01:40:26 -0800 | [diff] [blame] | 2221 | "to use depth layout qualifiers"); |
| 2222 | } else if (depth_layout_count > 0 |
| 2223 | && strcmp(var->name, "gl_FragDepth") != 0) { |
| 2224 | _mesa_glsl_error(loc, state, |
| 2225 | "depth layout qualifiers can be applied only to " |
| 2226 | "gl_FragDepth"); |
| 2227 | } else if (depth_layout_count > 1 |
| 2228 | && strcmp(var->name, "gl_FragDepth") == 0) { |
| 2229 | _mesa_glsl_error(loc, state, |
| 2230 | "at most one depth layout qualifier can be applied to " |
| 2231 | "gl_FragDepth"); |
| 2232 | } |
| 2233 | if (qual->flags.q.depth_any) |
| 2234 | var->depth_layout = ir_depth_layout_any; |
| 2235 | else if (qual->flags.q.depth_greater) |
| 2236 | var->depth_layout = ir_depth_layout_greater; |
| 2237 | else if (qual->flags.q.depth_less) |
| 2238 | var->depth_layout = ir_depth_layout_less; |
| 2239 | else if (qual->flags.q.depth_unchanged) |
| 2240 | var->depth_layout = ir_depth_layout_unchanged; |
| 2241 | else |
| 2242 | var->depth_layout = ir_depth_layout_none; |
Eric Anholt | f7561e8 | 2012-04-26 18:19:39 -0700 | [diff] [blame] | 2243 | |
| 2244 | if (qual->flags.q.std140 || |
| 2245 | qual->flags.q.packed || |
| 2246 | qual->flags.q.shared) { |
| 2247 | _mesa_glsl_error(loc, state, |
| 2248 | "uniform block layout qualifiers std140, packed, and " |
| 2249 | "shared can only be applied to uniform blocks, not " |
| 2250 | "members"); |
| 2251 | } |
| 2252 | |
Ian Romanick | bb47a4d | 2013-01-16 12:01:50 -0800 | [diff] [blame] | 2253 | if (qual->flags.q.row_major || qual->flags.q.column_major) { |
| 2254 | if (!ubo_qualifiers_valid) { |
| 2255 | _mesa_glsl_error(loc, state, |
| 2256 | "uniform block layout qualifiers row_major and " |
| 2257 | "column_major can only be applied to uniform block " |
| 2258 | "members"); |
| 2259 | } else |
| 2260 | validate_matrix_layout_for_type(state, loc, var->type); |
Eric Anholt | f7561e8 | 2012-04-26 18:19:39 -0700 | [diff] [blame] | 2261 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2262 | } |
| 2263 | |
Ian Romanick | 8e6cb9f | 2011-03-04 15:28:40 -0800 | [diff] [blame] | 2264 | /** |
| 2265 | * Get the variable that is being redeclared by this declaration |
| 2266 | * |
| 2267 | * Semantic checks to verify the validity of the redeclaration are also |
| 2268 | * performed. If semantic checks fail, compilation error will be emitted via |
| 2269 | * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned. |
| 2270 | * |
| 2271 | * \returns |
| 2272 | * A pointer to an existing variable in the current scope if the declaration |
| 2273 | * is a redeclaration, \c NULL otherwise. |
| 2274 | */ |
| 2275 | ir_variable * |
| 2276 | get_variable_being_redeclared(ir_variable *var, ast_declaration *decl, |
| 2277 | struct _mesa_glsl_parse_state *state) |
| 2278 | { |
| 2279 | /* Check if this declaration is actually a re-declaration, either to |
| 2280 | * resize an array or add qualifiers to an existing variable. |
| 2281 | * |
| 2282 | * This is allowed for variables in the current scope, or when at |
| 2283 | * global scope (for built-ins in the implicit outer scope). |
| 2284 | */ |
| 2285 | ir_variable *earlier = state->symbols->get_variable(decl->identifier); |
| 2286 | if (earlier == NULL || |
| 2287 | (state->current_function != NULL && |
| 2288 | !state->symbols->name_declared_this_scope(decl->identifier))) { |
| 2289 | return NULL; |
| 2290 | } |
| 2291 | |
| 2292 | |
| 2293 | YYLTYPE loc = decl->get_location(); |
| 2294 | |
| 2295 | /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec, |
| 2296 | * |
| 2297 | * "It is legal to declare an array without a size and then |
| 2298 | * later re-declare the same name as an array of the same |
| 2299 | * type and specify a size." |
| 2300 | */ |
| 2301 | if ((earlier->type->array_size() == 0) |
| 2302 | && var->type->is_array() |
| 2303 | && (var->type->element_type() == earlier->type->element_type())) { |
| 2304 | /* FINISHME: This doesn't match the qualifiers on the two |
| 2305 | * FINISHME: declarations. It's not 100% clear whether this is |
| 2306 | * FINISHME: required or not. |
| 2307 | */ |
| 2308 | |
Ian Romanick | 8e6cb9f | 2011-03-04 15:28:40 -0800 | [diff] [blame] | 2309 | const unsigned size = unsigned(var->type->array_size()); |
Paul Berry | 93b9758 | 2011-09-06 10:01:51 -0700 | [diff] [blame] | 2310 | check_builtin_array_max_size(var->name, size, loc, state); |
| 2311 | if ((size > 0) && (size <= earlier->max_array_access)) { |
Ian Romanick | 8e6cb9f | 2011-03-04 15:28:40 -0800 | [diff] [blame] | 2312 | _mesa_glsl_error(& loc, state, "array size must be > %u due to " |
| 2313 | "previous access", |
| 2314 | earlier->max_array_access); |
| 2315 | } |
| 2316 | |
| 2317 | earlier->type = var->type; |
| 2318 | delete var; |
| 2319 | var = NULL; |
| 2320 | } else if (state->ARB_fragment_coord_conventions_enable |
| 2321 | && strcmp(var->name, "gl_FragCoord") == 0 |
| 2322 | && earlier->type == var->type |
| 2323 | && earlier->mode == var->mode) { |
| 2324 | /* Allow redeclaration of gl_FragCoord for ARB_fcc layout |
| 2325 | * qualifiers. |
| 2326 | */ |
| 2327 | earlier->origin_upper_left = var->origin_upper_left; |
| 2328 | earlier->pixel_center_integer = var->pixel_center_integer; |
| 2329 | |
| 2330 | /* According to section 4.3.7 of the GLSL 1.30 spec, |
| 2331 | * the following built-in varaibles can be redeclared with an |
| 2332 | * interpolation qualifier: |
| 2333 | * * gl_FrontColor |
| 2334 | * * gl_BackColor |
| 2335 | * * gl_FrontSecondaryColor |
| 2336 | * * gl_BackSecondaryColor |
| 2337 | * * gl_Color |
| 2338 | * * gl_SecondaryColor |
| 2339 | */ |
Paul Berry | e3ded7f | 2012-08-01 14:50:05 -0700 | [diff] [blame] | 2340 | } else if (state->is_version(130, 0) |
Ian Romanick | 8e6cb9f | 2011-03-04 15:28:40 -0800 | [diff] [blame] | 2341 | && (strcmp(var->name, "gl_FrontColor") == 0 |
| 2342 | || strcmp(var->name, "gl_BackColor") == 0 |
| 2343 | || strcmp(var->name, "gl_FrontSecondaryColor") == 0 |
| 2344 | || strcmp(var->name, "gl_BackSecondaryColor") == 0 |
| 2345 | || strcmp(var->name, "gl_Color") == 0 |
| 2346 | || strcmp(var->name, "gl_SecondaryColor") == 0) |
| 2347 | && earlier->type == var->type |
| 2348 | && earlier->mode == var->mode) { |
| 2349 | earlier->interpolation = var->interpolation; |
| 2350 | |
| 2351 | /* Layout qualifiers for gl_FragDepth. */ |
Marek Olšák | 6b43d6f | 2011-11-19 16:41:08 +0100 | [diff] [blame] | 2352 | } else if ((state->AMD_conservative_depth_enable || |
| 2353 | state->ARB_conservative_depth_enable) |
Ian Romanick | 8e6cb9f | 2011-03-04 15:28:40 -0800 | [diff] [blame] | 2354 | && strcmp(var->name, "gl_FragDepth") == 0 |
| 2355 | && earlier->type == var->type |
| 2356 | && earlier->mode == var->mode) { |
| 2357 | |
| 2358 | /** From the AMD_conservative_depth spec: |
| 2359 | * Within any shader, the first redeclarations of gl_FragDepth |
| 2360 | * must appear before any use of gl_FragDepth. |
| 2361 | */ |
| 2362 | if (earlier->used) { |
| 2363 | _mesa_glsl_error(&loc, state, |
| 2364 | "the first redeclaration of gl_FragDepth " |
| 2365 | "must appear before any use of gl_FragDepth"); |
| 2366 | } |
| 2367 | |
| 2368 | /* Prevent inconsistent redeclaration of depth layout qualifier. */ |
| 2369 | if (earlier->depth_layout != ir_depth_layout_none |
| 2370 | && earlier->depth_layout != var->depth_layout) { |
| 2371 | _mesa_glsl_error(&loc, state, |
| 2372 | "gl_FragDepth: depth layout is declared here " |
| 2373 | "as '%s, but it was previously declared as " |
| 2374 | "'%s'", |
| 2375 | depth_layout_string(var->depth_layout), |
| 2376 | depth_layout_string(earlier->depth_layout)); |
| 2377 | } |
| 2378 | |
| 2379 | earlier->depth_layout = var->depth_layout; |
| 2380 | |
| 2381 | } else { |
| 2382 | _mesa_glsl_error(&loc, state, "`%s' redeclared", decl->identifier); |
| 2383 | } |
| 2384 | |
| 2385 | return earlier; |
| 2386 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2387 | |
Ian Romanick | 0292ffb | 2011-03-04 15:29:33 -0800 | [diff] [blame] | 2388 | /** |
| 2389 | * Generate the IR for an initializer in a variable declaration |
| 2390 | */ |
| 2391 | ir_rvalue * |
| 2392 | process_initializer(ir_variable *var, ast_declaration *decl, |
| 2393 | ast_fully_specified_type *type, |
| 2394 | exec_list *initializer_instructions, |
| 2395 | struct _mesa_glsl_parse_state *state) |
| 2396 | { |
| 2397 | ir_rvalue *result = NULL; |
| 2398 | |
| 2399 | YYLTYPE initializer_loc = decl->initializer->get_location(); |
| 2400 | |
| 2401 | /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec: |
| 2402 | * |
| 2403 | * "All uniform variables are read-only and are initialized either |
| 2404 | * directly by an application via API commands, or indirectly by |
| 2405 | * OpenGL." |
| 2406 | */ |
Paul Berry | 0d9bba6 | 2012-08-05 09:57:01 -0700 | [diff] [blame] | 2407 | if (var->mode == ir_var_uniform) { |
| 2408 | state->check_version(120, 0, &initializer_loc, |
| 2409 | "cannot initialize uniforms"); |
Ian Romanick | 0292ffb | 2011-03-04 15:29:33 -0800 | [diff] [blame] | 2410 | } |
| 2411 | |
| 2412 | if (var->type->is_sampler()) { |
| 2413 | _mesa_glsl_error(& initializer_loc, state, |
| 2414 | "cannot initialize samplers"); |
| 2415 | } |
| 2416 | |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 2417 | if ((var->mode == ir_var_shader_in) && (state->current_function == NULL)) { |
Ian Romanick | 0292ffb | 2011-03-04 15:29:33 -0800 | [diff] [blame] | 2418 | _mesa_glsl_error(& initializer_loc, state, |
| 2419 | "cannot initialize %s shader input / %s", |
| 2420 | _mesa_glsl_shader_target_name(state->target), |
| 2421 | (state->target == vertex_shader) |
| 2422 | ? "attribute" : "varying"); |
| 2423 | } |
| 2424 | |
| 2425 | ir_dereference *const lhs = new(state) ir_dereference_variable(var); |
| 2426 | ir_rvalue *rhs = decl->initializer->hir(initializer_instructions, |
| 2427 | state); |
| 2428 | |
| 2429 | /* Calculate the constant value if this is a const or uniform |
| 2430 | * declaration. |
| 2431 | */ |
| 2432 | if (type->qualifier.flags.q.constant |
| 2433 | || type->qualifier.flags.q.uniform) { |
Ian Romanick | 85caea2 | 2011-03-15 16:33:27 -0700 | [diff] [blame] | 2434 | ir_rvalue *new_rhs = validate_assignment(state, var->type, rhs, true); |
Ian Romanick | 0292ffb | 2011-03-04 15:29:33 -0800 | [diff] [blame] | 2435 | if (new_rhs != NULL) { |
| 2436 | rhs = new_rhs; |
| 2437 | |
| 2438 | ir_constant *constant_value = rhs->constant_expression_value(); |
| 2439 | if (!constant_value) { |
Matt Turner | ba7b60d | 2013-05-23 11:48:40 -0700 | [diff] [blame] | 2440 | /* If ARB_shading_language_420pack is enabled, initializers of |
| 2441 | * const-qualified local variables do not have to be constant |
| 2442 | * expressions. Const-qualified global variables must still be |
| 2443 | * initialized with constant expressions. |
| 2444 | */ |
| 2445 | if (!state->ARB_shading_language_420pack_enable |
| 2446 | || state->current_function == NULL) { |
| 2447 | _mesa_glsl_error(& initializer_loc, state, |
| 2448 | "initializer of %s variable `%s' must be a " |
| 2449 | "constant expression", |
| 2450 | (type->qualifier.flags.q.constant) |
| 2451 | ? "const" : "uniform", |
| 2452 | decl->identifier); |
| 2453 | if (var->type->is_numeric()) { |
| 2454 | /* Reduce cascading errors. */ |
| 2455 | var->constant_value = ir_constant::zero(state, var->type); |
| 2456 | } |
| 2457 | } |
| 2458 | } else { |
Ian Romanick | 0292ffb | 2011-03-04 15:29:33 -0800 | [diff] [blame] | 2459 | rhs = constant_value; |
| 2460 | var->constant_value = constant_value; |
| 2461 | } |
| 2462 | } else { |
| 2463 | _mesa_glsl_error(&initializer_loc, state, |
| 2464 | "initializer of type %s cannot be assigned to " |
| 2465 | "variable of type %s", |
| 2466 | rhs->type->name, var->type->name); |
| 2467 | if (var->type->is_numeric()) { |
| 2468 | /* Reduce cascading errors. */ |
| 2469 | var->constant_value = ir_constant::zero(state, var->type); |
| 2470 | } |
| 2471 | } |
| 2472 | } |
| 2473 | |
| 2474 | if (rhs && !rhs->type->is_error()) { |
| 2475 | bool temp = var->read_only; |
| 2476 | if (type->qualifier.flags.q.constant) |
| 2477 | var->read_only = false; |
| 2478 | |
| 2479 | /* Never emit code to initialize a uniform. |
| 2480 | */ |
| 2481 | const glsl_type *initializer_type; |
| 2482 | if (!type->qualifier.flags.q.uniform) { |
| 2483 | result = do_assignment(initializer_instructions, state, |
Ian Romanick | e9015e9 | 2011-12-23 09:56:29 -0800 | [diff] [blame] | 2484 | NULL, |
Ian Romanick | 85caea2 | 2011-03-15 16:33:27 -0700 | [diff] [blame] | 2485 | lhs, rhs, true, |
Ian Romanick | 0292ffb | 2011-03-04 15:29:33 -0800 | [diff] [blame] | 2486 | type->get_location()); |
| 2487 | initializer_type = result->type; |
| 2488 | } else |
| 2489 | initializer_type = rhs->type; |
| 2490 | |
Ian Romanick | f37b1ad | 2011-10-31 14:31:07 -0700 | [diff] [blame] | 2491 | var->constant_initializer = rhs->constant_expression_value(); |
| 2492 | var->has_initializer = true; |
| 2493 | |
Ian Romanick | 0292ffb | 2011-03-04 15:29:33 -0800 | [diff] [blame] | 2494 | /* If the declared variable is an unsized array, it must inherrit |
| 2495 | * its full type from the initializer. A declaration such as |
| 2496 | * |
| 2497 | * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0); |
| 2498 | * |
| 2499 | * becomes |
| 2500 | * |
| 2501 | * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0); |
| 2502 | * |
| 2503 | * The assignment generated in the if-statement (below) will also |
| 2504 | * automatically handle this case for non-uniforms. |
| 2505 | * |
| 2506 | * If the declared variable is not an array, the types must |
| 2507 | * already match exactly. As a result, the type assignment |
| 2508 | * here can be done unconditionally. For non-uniforms the call |
| 2509 | * to do_assignment can change the type of the initializer (via |
| 2510 | * the implicit conversion rules). For uniforms the initializer |
| 2511 | * must be a constant expression, and the type of that expression |
| 2512 | * was validated above. |
| 2513 | */ |
| 2514 | var->type = initializer_type; |
| 2515 | |
| 2516 | var->read_only = temp; |
| 2517 | } |
| 2518 | |
| 2519 | return result; |
| 2520 | } |
| 2521 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 2522 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 2523 | ast_declarator_list::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 2524 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2525 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 2526 | void *ctx = state; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2527 | const struct glsl_type *decl_type; |
| 2528 | const char *type_name = NULL; |
Eric Anholt | 8558459 | 2010-04-14 15:38:52 -0700 | [diff] [blame] | 2529 | ir_rvalue *result = NULL; |
Ian Romanick | c824e35 | 2010-04-23 15:55:19 -0700 | [diff] [blame] | 2530 | YYLTYPE loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2531 | |
Ian Romanick | 6f0823d | 2010-07-01 20:39:08 -0700 | [diff] [blame] | 2532 | /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec: |
| 2533 | * |
| 2534 | * "To ensure that a particular output variable is invariant, it is |
| 2535 | * necessary to use the invariant qualifier. It can either be used to |
| 2536 | * qualify a previously declared variable as being invariant |
| 2537 | * |
| 2538 | * invariant gl_Position; // make existing gl_Position be invariant" |
| 2539 | * |
| 2540 | * In these cases the parser will set the 'invariant' flag in the declarator |
| 2541 | * list, and the type will be NULL. |
| 2542 | */ |
| 2543 | if (this->invariant) { |
| 2544 | assert(this->type == NULL); |
| 2545 | |
| 2546 | if (state->current_function != NULL) { |
| 2547 | _mesa_glsl_error(& loc, state, |
| 2548 | "All uses of `invariant' keyword must be at global " |
| 2549 | "scope\n"); |
| 2550 | } |
| 2551 | |
| 2552 | foreach_list_typed (ast_declaration, decl, link, &this->declarations) { |
| 2553 | assert(!decl->is_array); |
| 2554 | assert(decl->array_size == NULL); |
| 2555 | assert(decl->initializer == NULL); |
| 2556 | |
| 2557 | ir_variable *const earlier = |
| 2558 | state->symbols->get_variable(decl->identifier); |
| 2559 | if (earlier == NULL) { |
| 2560 | _mesa_glsl_error(& loc, state, |
| 2561 | "Undeclared variable `%s' cannot be marked " |
| 2562 | "invariant\n", decl->identifier); |
| 2563 | } else if ((state->target == vertex_shader) |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 2564 | && (earlier->mode != ir_var_shader_out)) { |
Ian Romanick | 6f0823d | 2010-07-01 20:39:08 -0700 | [diff] [blame] | 2565 | _mesa_glsl_error(& loc, state, |
| 2566 | "`%s' cannot be marked invariant, vertex shader " |
| 2567 | "outputs only\n", decl->identifier); |
| 2568 | } else if ((state->target == fragment_shader) |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 2569 | && (earlier->mode != ir_var_shader_in)) { |
Ian Romanick | 6f0823d | 2010-07-01 20:39:08 -0700 | [diff] [blame] | 2570 | _mesa_glsl_error(& loc, state, |
| 2571 | "`%s' cannot be marked invariant, fragment shader " |
| 2572 | "inputs only\n", decl->identifier); |
Ian Romanick | bd33055 | 2011-01-07 18:34:58 -0800 | [diff] [blame] | 2573 | } else if (earlier->used) { |
| 2574 | _mesa_glsl_error(& loc, state, |
| 2575 | "variable `%s' may not be redeclared " |
| 2576 | "`invariant' after being used", |
| 2577 | earlier->name); |
Ian Romanick | 6f0823d | 2010-07-01 20:39:08 -0700 | [diff] [blame] | 2578 | } else { |
| 2579 | earlier->invariant = true; |
| 2580 | } |
| 2581 | } |
| 2582 | |
| 2583 | /* Invariant redeclarations do not have r-values. |
| 2584 | */ |
| 2585 | return NULL; |
| 2586 | } |
| 2587 | |
| 2588 | assert(this->type != NULL); |
| 2589 | assert(!this->invariant); |
| 2590 | |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 2591 | /* The type specifier may contain a structure definition. Process that |
| 2592 | * before any of the variable declarations. |
| 2593 | */ |
| 2594 | (void) this->type->specifier->hir(instructions, state); |
| 2595 | |
Ian Romanick | d612a12 | 2010-03-31 16:22:06 -0700 | [diff] [blame] | 2596 | decl_type = this->type->specifier->glsl_type(& type_name, state); |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 2597 | if (this->declarations.is_empty()) { |
Ian Romanick | f5ba4d0 | 2011-10-25 17:49:07 -0700 | [diff] [blame] | 2598 | /* If there is no structure involved in the program text, there are two |
| 2599 | * possible scenarios: |
| 2600 | * |
| 2601 | * - The program text contained something like 'vec4;'. This is an |
| 2602 | * empty declaration. It is valid but weird. Emit a warning. |
| 2603 | * |
| 2604 | * - The program text contained something like 'S;' and 'S' is not the |
| 2605 | * name of a known structure type. This is both invalid and weird. |
| 2606 | * Emit an error. |
| 2607 | * |
| 2608 | * Note that if decl_type is NULL and there is a structure involved, |
| 2609 | * there must have been some sort of error with the structure. In this |
| 2610 | * case we assume that an error was already generated on this line of |
| 2611 | * code for the structure. There is no need to generate an additional, |
| 2612 | * confusing error. |
| 2613 | */ |
| 2614 | assert(this->type->specifier->structure == NULL || decl_type != NULL |
| 2615 | || state->error); |
| 2616 | if (this->type->specifier->structure == NULL) { |
| 2617 | if (decl_type != NULL) { |
Chia-I Wu | 547212d | 2011-08-04 00:39:07 +0900 | [diff] [blame] | 2618 | _mesa_glsl_warning(&loc, state, "empty declaration"); |
Ian Romanick | f5ba4d0 | 2011-10-25 17:49:07 -0700 | [diff] [blame] | 2619 | } else { |
| 2620 | _mesa_glsl_error(&loc, state, |
| 2621 | "invalid type `%s' in empty declaration", |
| 2622 | type_name); |
Chia-I Wu | 547212d | 2011-08-04 00:39:07 +0900 | [diff] [blame] | 2623 | } |
Ian Romanick | c824e35 | 2010-04-23 15:55:19 -0700 | [diff] [blame] | 2624 | } |
Kenneth Graunke | 6eec502 | 2013-07-15 15:58:29 -0700 | [diff] [blame] | 2625 | |
| 2626 | if (this->type->qualifier.precision != ast_precision_none && |
| 2627 | this->type->specifier->structure != NULL) { |
| 2628 | _mesa_glsl_error(&loc, state, "Precision qualifiers can't be applied " |
| 2629 | "to structures.\n"); |
| 2630 | } |
Ian Romanick | c824e35 | 2010-04-23 15:55:19 -0700 | [diff] [blame] | 2631 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2632 | |
Ian Romanick | 2b97dc6 | 2010-05-10 17:42:05 -0700 | [diff] [blame] | 2633 | foreach_list_typed (ast_declaration, decl, link, &this->declarations) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2634 | const struct glsl_type *var_type; |
Ian Romanick | 768b55a | 2010-08-13 16:46:43 -0700 | [diff] [blame] | 2635 | ir_variable *var; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2636 | |
| 2637 | /* FINISHME: Emit a warning if a variable declaration shadows a |
| 2638 | * FINISHME: declaration at a higher scope. |
| 2639 | */ |
| 2640 | |
Ian Romanick | cec65a6 | 2010-03-23 12:28:44 -0700 | [diff] [blame] | 2641 | if ((decl_type == NULL) || decl_type->is_void()) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2642 | if (type_name != NULL) { |
| 2643 | _mesa_glsl_error(& loc, state, |
| 2644 | "invalid type `%s' in declaration of `%s'", |
| 2645 | type_name, decl->identifier); |
| 2646 | } else { |
| 2647 | _mesa_glsl_error(& loc, state, |
| 2648 | "invalid type in declaration of `%s'", |
| 2649 | decl->identifier); |
| 2650 | } |
| 2651 | continue; |
| 2652 | } |
| 2653 | |
| 2654 | if (decl->is_array) { |
Kenneth Graunke | d8e34e2 | 2010-08-07 02:56:01 -0700 | [diff] [blame] | 2655 | var_type = process_array_type(&loc, decl_type, decl->array_size, |
| 2656 | state); |
Ian Romanick | a04211e | 2011-10-24 11:45:50 -0700 | [diff] [blame] | 2657 | if (var_type->is_error()) |
| 2658 | continue; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2659 | } else { |
| 2660 | var_type = decl_type; |
| 2661 | } |
| 2662 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 2663 | var = new(ctx) ir_variable(var_type, decl->identifier, ir_var_auto); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2664 | |
Eric Anholt | 3f15150 | 2010-04-02 01:53:57 -1000 | [diff] [blame] | 2665 | /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification; |
| 2666 | * |
| 2667 | * "Global variables can only use the qualifiers const, |
| 2668 | * attribute, uni form, or varying. Only one may be |
| 2669 | * specified. |
| 2670 | * |
| 2671 | * Local variables can only use the qualifier const." |
| 2672 | * |
Paul Berry | d4a2474 | 2012-08-02 08:18:12 -0700 | [diff] [blame] | 2673 | * This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by |
| 2674 | * any extension that adds the 'layout' keyword. |
Eric Anholt | 3f15150 | 2010-04-02 01:53:57 -1000 | [diff] [blame] | 2675 | */ |
Paul Berry | d4a2474 | 2012-08-02 08:18:12 -0700 | [diff] [blame] | 2676 | if (!state->is_version(130, 300) |
Ian Romanick | 82c4b4f | 2011-01-07 16:53:07 -0800 | [diff] [blame] | 2677 | && !state->ARB_explicit_attrib_location_enable |
| 2678 | && !state->ARB_fragment_coord_conventions_enable) { |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 2679 | if (this->type->qualifier.flags.q.out) { |
Eric Anholt | 3f15150 | 2010-04-02 01:53:57 -1000 | [diff] [blame] | 2680 | _mesa_glsl_error(& loc, state, |
| 2681 | "`out' qualifier in declaration of `%s' " |
Ian Romanick | 469ea69 | 2011-01-07 16:05:59 -0800 | [diff] [blame] | 2682 | "only valid for function parameters in %s.", |
Paul Berry | dc9f9d8 | 2012-08-02 06:45:30 -0700 | [diff] [blame] | 2683 | decl->identifier, state->get_version_string()); |
Eric Anholt | 3f15150 | 2010-04-02 01:53:57 -1000 | [diff] [blame] | 2684 | } |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 2685 | if (this->type->qualifier.flags.q.in) { |
Eric Anholt | 3f15150 | 2010-04-02 01:53:57 -1000 | [diff] [blame] | 2686 | _mesa_glsl_error(& loc, state, |
| 2687 | "`in' qualifier in declaration of `%s' " |
Ian Romanick | 469ea69 | 2011-01-07 16:05:59 -0800 | [diff] [blame] | 2688 | "only valid for function parameters in %s.", |
Paul Berry | dc9f9d8 | 2012-08-02 06:45:30 -0700 | [diff] [blame] | 2689 | decl->identifier, state->get_version_string()); |
Eric Anholt | 3f15150 | 2010-04-02 01:53:57 -1000 | [diff] [blame] | 2690 | } |
| 2691 | /* FINISHME: Test for other invalid qualifiers. */ |
| 2692 | } |
| 2693 | |
Eric Anholt | 2e063f1 | 2010-03-28 00:56:22 -0700 | [diff] [blame] | 2694 | apply_type_qualifier_to_variable(& this->type->qualifier, var, state, |
Paul Berry | c33be48 | 2012-12-18 14:49:34 -0800 | [diff] [blame] | 2695 | & loc, this->ubo_qualifiers_valid, false); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2696 | |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 2697 | if (this->type->qualifier.flags.q.invariant) { |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 2698 | if ((state->target == vertex_shader) && |
| 2699 | var->mode != ir_var_shader_out) { |
Ian Romanick | 6f0823d | 2010-07-01 20:39:08 -0700 | [diff] [blame] | 2700 | _mesa_glsl_error(& loc, state, |
| 2701 | "`%s' cannot be marked invariant, vertex shader " |
| 2702 | "outputs only\n", var->name); |
Eric Anholt | 046bef2 | 2010-08-04 20:33:57 -0700 | [diff] [blame] | 2703 | } else if ((state->target == fragment_shader) && |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 2704 | var->mode != ir_var_shader_in) { |
Eric Anholt | 046bef2 | 2010-08-04 20:33:57 -0700 | [diff] [blame] | 2705 | /* FINISHME: Note that this doesn't work for invariant on |
| 2706 | * a function signature inval |
| 2707 | */ |
Ian Romanick | 6f0823d | 2010-07-01 20:39:08 -0700 | [diff] [blame] | 2708 | _mesa_glsl_error(& loc, state, |
| 2709 | "`%s' cannot be marked invariant, fragment shader " |
| 2710 | "inputs only\n", var->name); |
| 2711 | } |
| 2712 | } |
| 2713 | |
Ian Romanick | e1c1a3f | 2010-03-31 12:26:03 -0700 | [diff] [blame] | 2714 | if (state->current_function != NULL) { |
Ian Romanick | b168e53 | 2010-03-31 12:31:18 -0700 | [diff] [blame] | 2715 | const char *mode = NULL; |
Ian Romanick | e080006 | 2010-03-31 13:15:23 -0700 | [diff] [blame] | 2716 | const char *extra = ""; |
Ian Romanick | b168e53 | 2010-03-31 12:31:18 -0700 | [diff] [blame] | 2717 | |
Ian Romanick | e080006 | 2010-03-31 13:15:23 -0700 | [diff] [blame] | 2718 | /* There is no need to check for 'inout' here because the parser will |
| 2719 | * only allow that in function parameter lists. |
Ian Romanick | e1c1a3f | 2010-03-31 12:26:03 -0700 | [diff] [blame] | 2720 | */ |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 2721 | if (this->type->qualifier.flags.q.attribute) { |
Ian Romanick | b168e53 | 2010-03-31 12:31:18 -0700 | [diff] [blame] | 2722 | mode = "attribute"; |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 2723 | } else if (this->type->qualifier.flags.q.uniform) { |
Ian Romanick | b168e53 | 2010-03-31 12:31:18 -0700 | [diff] [blame] | 2724 | mode = "uniform"; |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 2725 | } else if (this->type->qualifier.flags.q.varying) { |
Ian Romanick | b168e53 | 2010-03-31 12:31:18 -0700 | [diff] [blame] | 2726 | mode = "varying"; |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 2727 | } else if (this->type->qualifier.flags.q.in) { |
Ian Romanick | e080006 | 2010-03-31 13:15:23 -0700 | [diff] [blame] | 2728 | mode = "in"; |
| 2729 | extra = " or in function parameter list"; |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 2730 | } else if (this->type->qualifier.flags.q.out) { |
Ian Romanick | e080006 | 2010-03-31 13:15:23 -0700 | [diff] [blame] | 2731 | mode = "out"; |
| 2732 | extra = " or in function parameter list"; |
Ian Romanick | b168e53 | 2010-03-31 12:31:18 -0700 | [diff] [blame] | 2733 | } |
| 2734 | |
| 2735 | if (mode) { |
Ian Romanick | e1c1a3f | 2010-03-31 12:26:03 -0700 | [diff] [blame] | 2736 | _mesa_glsl_error(& loc, state, |
Ian Romanick | b168e53 | 2010-03-31 12:31:18 -0700 | [diff] [blame] | 2737 | "%s variable `%s' must be declared at " |
Ian Romanick | e080006 | 2010-03-31 13:15:23 -0700 | [diff] [blame] | 2738 | "global scope%s", |
| 2739 | mode, var->name, extra); |
Ian Romanick | e1c1a3f | 2010-03-31 12:26:03 -0700 | [diff] [blame] | 2740 | } |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 2741 | } else if (var->mode == ir_var_shader_in) { |
Chad Versace | 01a584d | 2011-01-20 14:12:16 -0800 | [diff] [blame] | 2742 | var->read_only = true; |
| 2743 | |
Ian Romanick | fb9f5b0 | 2010-03-29 17:16:35 -0700 | [diff] [blame] | 2744 | if (state->target == vertex_shader) { |
| 2745 | bool error_emitted = false; |
| 2746 | |
| 2747 | /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec: |
| 2748 | * |
| 2749 | * "Vertex shader inputs can only be float, floating-point |
| 2750 | * vectors, matrices, signed and unsigned integers and integer |
| 2751 | * vectors. Vertex shader inputs can also form arrays of these |
| 2752 | * types, but not structures." |
| 2753 | * |
Ian Romanick | 2d81620 | 2010-03-29 17:40:11 -0700 | [diff] [blame] | 2754 | * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec: |
| 2755 | * |
| 2756 | * "Vertex shader inputs can only be float, floating-point |
| 2757 | * vectors, matrices, signed and unsigned integers and integer |
| 2758 | * vectors. They cannot be arrays or structures." |
| 2759 | * |
Ian Romanick | fb9f5b0 | 2010-03-29 17:16:35 -0700 | [diff] [blame] | 2760 | * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec: |
| 2761 | * |
| 2762 | * "The attribute qualifier can be used only with float, |
| 2763 | * floating-point vectors, and matrices. Attribute variables |
| 2764 | * cannot be declared as arrays or structures." |
Paul Berry | d4a2474 | 2012-08-02 08:18:12 -0700 | [diff] [blame] | 2765 | * |
| 2766 | * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec: |
| 2767 | * |
| 2768 | * "Vertex shader inputs can only be float, floating-point |
| 2769 | * vectors, matrices, signed and unsigned integers and integer |
| 2770 | * vectors. Vertex shader inputs cannot be arrays or |
| 2771 | * structures." |
Ian Romanick | fb9f5b0 | 2010-03-29 17:16:35 -0700 | [diff] [blame] | 2772 | */ |
| 2773 | const glsl_type *check_type = var->type->is_array() |
| 2774 | ? var->type->fields.array : var->type; |
| 2775 | |
| 2776 | switch (check_type->base_type) { |
| 2777 | case GLSL_TYPE_FLOAT: |
| 2778 | break; |
| 2779 | case GLSL_TYPE_UINT: |
| 2780 | case GLSL_TYPE_INT: |
Paul Berry | d4a2474 | 2012-08-02 08:18:12 -0700 | [diff] [blame] | 2781 | if (state->is_version(120, 300)) |
Ian Romanick | fb9f5b0 | 2010-03-29 17:16:35 -0700 | [diff] [blame] | 2782 | break; |
| 2783 | /* FALLTHROUGH */ |
| 2784 | default: |
| 2785 | _mesa_glsl_error(& loc, state, |
| 2786 | "vertex shader input / attribute cannot have " |
| 2787 | "type %s`%s'", |
| 2788 | var->type->is_array() ? "array of " : "", |
| 2789 | check_type->name); |
| 2790 | error_emitted = true; |
| 2791 | } |
| 2792 | |
Paul Berry | 0d9bba6 | 2012-08-05 09:57:01 -0700 | [diff] [blame] | 2793 | if (!error_emitted && var->type->is_array() && |
Paul Berry | b2265db | 2013-07-11 15:40:11 -0700 | [diff] [blame] | 2794 | !state->check_version(150, 0, &loc, |
Paul Berry | 0d9bba6 | 2012-08-05 09:57:01 -0700 | [diff] [blame] | 2795 | "vertex shader input / attribute " |
| 2796 | "cannot have array type")) { |
Ian Romanick | fb9f5b0 | 2010-03-29 17:16:35 -0700 | [diff] [blame] | 2797 | error_emitted = true; |
| 2798 | } |
| 2799 | } |
| 2800 | } |
| 2801 | |
Paul Berry | dfb57e7 | 2013-02-08 16:46:20 -0800 | [diff] [blame] | 2802 | /* Integer fragment inputs must be qualified with 'flat'. In GLSL ES, |
| 2803 | * so must integer vertex outputs. |
Chad Versace | 68d06b1 | 2010-12-16 11:06:19 -0800 | [diff] [blame] | 2804 | * |
Paul Berry | dfb57e7 | 2013-02-08 16:46:20 -0800 | [diff] [blame] | 2805 | * From section 4.3.4 ("Inputs") of the GLSL 1.50 spec: |
| 2806 | * "Fragment shader inputs that are signed or unsigned integers or |
| 2807 | * integer vectors must be qualified with the interpolation qualifier |
Chad Versace | 68d06b1 | 2010-12-16 11:06:19 -0800 | [diff] [blame] | 2808 | * flat." |
Paul Berry | d4a2474 | 2012-08-02 08:18:12 -0700 | [diff] [blame] | 2809 | * |
Paul Berry | dfb57e7 | 2013-02-08 16:46:20 -0800 | [diff] [blame] | 2810 | * From section 4.3.4 ("Input Variables") of the GLSL 3.00 ES spec: |
Paul Berry | 93c9134 | 2013-02-06 16:09:39 -0800 | [diff] [blame] | 2811 | * "Fragment shader inputs that are, or contain, signed or unsigned |
| 2812 | * integers or integer vectors must be qualified with the |
| 2813 | * interpolation qualifier flat." |
Paul Berry | d4a2474 | 2012-08-02 08:18:12 -0700 | [diff] [blame] | 2814 | * |
Paul Berry | dfb57e7 | 2013-02-08 16:46:20 -0800 | [diff] [blame] | 2815 | * From section 4.3.6 ("Output Variables") of the GLSL 3.00 ES spec: |
| 2816 | * "Vertex shader outputs that are, or contain, signed or unsigned |
| 2817 | * integers or integer vectors must be qualified with the |
| 2818 | * interpolation qualifier flat." |
| 2819 | * |
| 2820 | * Note that prior to GLSL 1.50, this requirement applied to vertex |
| 2821 | * outputs rather than fragment inputs. That creates problems in the |
| 2822 | * presence of geometry shaders, so we adopt the GLSL 1.50 rule for all |
| 2823 | * desktop GL shaders. For GLSL ES shaders, we follow the spec and |
| 2824 | * apply the restriction to both vertex outputs and fragment inputs. |
| 2825 | * |
| 2826 | * Note also that the desktop GLSL specs are missing the text "or |
| 2827 | * contain"; this is presumably an oversight, since there is no |
| 2828 | * reasonable way to interpolate a fragment shader input that contains |
| 2829 | * an integer. |
Chad Versace | 68d06b1 | 2010-12-16 11:06:19 -0800 | [diff] [blame] | 2830 | */ |
Paul Berry | dfb57e7 | 2013-02-08 16:46:20 -0800 | [diff] [blame] | 2831 | if (state->is_version(130, 300) && |
| 2832 | var->type->contains_integer() && |
| 2833 | var->interpolation != INTERP_QUALIFIER_FLAT && |
| 2834 | ((state->target == fragment_shader && var->mode == ir_var_shader_in) |
| 2835 | || (state->target == vertex_shader && var->mode == ir_var_shader_out |
| 2836 | && state->es_shader))) { |
| 2837 | const char *var_type = (state->target == vertex_shader) ? |
| 2838 | "vertex output" : "fragment input"; |
| 2839 | _mesa_glsl_error(&loc, state, "If a %s is (or contains) " |
| 2840 | "an integer, then it must be qualified with 'flat'", |
| 2841 | var_type); |
Chad Versace | 68d06b1 | 2010-12-16 11:06:19 -0800 | [diff] [blame] | 2842 | } |
| 2843 | |
| 2844 | |
Chad Versace | 605aacc | 2011-01-11 17:21:18 -0800 | [diff] [blame] | 2845 | /* Interpolation qualifiers cannot be applied to 'centroid' and |
| 2846 | * 'centroid varying'. |
| 2847 | * |
| 2848 | * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec: |
| 2849 | * "interpolation qualifiers may only precede the qualifiers in, |
| 2850 | * centroid in, out, or centroid out in a declaration. They do not apply |
| 2851 | * to the deprecated storage qualifiers varying or centroid varying." |
Paul Berry | d4a2474 | 2012-08-02 08:18:12 -0700 | [diff] [blame] | 2852 | * |
| 2853 | * These deprecated storage qualifiers do not exist in GLSL ES 3.00. |
Chad Versace | 605aacc | 2011-01-11 17:21:18 -0800 | [diff] [blame] | 2854 | */ |
Paul Berry | e3ded7f | 2012-08-01 14:50:05 -0700 | [diff] [blame] | 2855 | if (state->is_version(130, 0) |
Chad Versace | 605aacc | 2011-01-11 17:21:18 -0800 | [diff] [blame] | 2856 | && this->type->qualifier.has_interpolation() |
| 2857 | && this->type->qualifier.flags.q.varying) { |
| 2858 | |
| 2859 | const char *i = this->type->qualifier.interpolation_string(); |
| 2860 | assert(i != NULL); |
| 2861 | const char *s; |
| 2862 | if (this->type->qualifier.flags.q.centroid) |
| 2863 | s = "centroid varying"; |
| 2864 | else |
| 2865 | s = "varying"; |
| 2866 | |
| 2867 | _mesa_glsl_error(&loc, state, |
| 2868 | "qualifier '%s' cannot be applied to the " |
| 2869 | "deprecated storage qualifier '%s'", i, s); |
| 2870 | } |
| 2871 | |
| 2872 | |
Chad Versace | 8faaa4a | 2011-01-11 18:13:26 -0800 | [diff] [blame] | 2873 | /* Interpolation qualifiers can only apply to vertex shader outputs and |
| 2874 | * fragment shader inputs. |
| 2875 | * |
| 2876 | * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec: |
| 2877 | * "Outputs from a vertex shader (out) and inputs to a fragment |
| 2878 | * shader (in) can be further qualified with one or more of these |
| 2879 | * interpolation qualifiers" |
Paul Berry | d4a2474 | 2012-08-02 08:18:12 -0700 | [diff] [blame] | 2880 | * |
| 2881 | * From page 31 (page 37 of the PDF) of the GLSL ES 3.00 spec: |
| 2882 | * "These interpolation qualifiers may only precede the qualifiers |
| 2883 | * in, centroid in, out, or centroid out in a declaration. They do |
| 2884 | * not apply to inputs into a vertex shader or outputs from a |
| 2885 | * fragment shader." |
Chad Versace | 8faaa4a | 2011-01-11 18:13:26 -0800 | [diff] [blame] | 2886 | */ |
Paul Berry | d4a2474 | 2012-08-02 08:18:12 -0700 | [diff] [blame] | 2887 | if (state->is_version(130, 300) |
Chad Versace | 8faaa4a | 2011-01-11 18:13:26 -0800 | [diff] [blame] | 2888 | && this->type->qualifier.has_interpolation()) { |
| 2889 | |
| 2890 | const char *i = this->type->qualifier.interpolation_string(); |
| 2891 | assert(i != NULL); |
| 2892 | |
| 2893 | switch (state->target) { |
| 2894 | case vertex_shader: |
| 2895 | if (this->type->qualifier.flags.q.in) { |
| 2896 | _mesa_glsl_error(&loc, state, |
| 2897 | "qualifier '%s' cannot be applied to vertex " |
| 2898 | "shader inputs", i); |
| 2899 | } |
| 2900 | break; |
| 2901 | case fragment_shader: |
| 2902 | if (this->type->qualifier.flags.q.out) { |
| 2903 | _mesa_glsl_error(&loc, state, |
| 2904 | "qualifier '%s' cannot be applied to fragment " |
| 2905 | "shader outputs", i); |
| 2906 | } |
| 2907 | break; |
| 2908 | default: |
| 2909 | assert(0); |
| 2910 | } |
| 2911 | } |
| 2912 | |
| 2913 | |
Chad Versace | 1eb0f17 | 2011-01-11 18:24:17 -0800 | [diff] [blame] | 2914 | /* From section 4.3.4 of the GLSL 1.30 spec: |
| 2915 | * "It is an error to use centroid in in a vertex shader." |
Paul Berry | d4a2474 | 2012-08-02 08:18:12 -0700 | [diff] [blame] | 2916 | * |
| 2917 | * From section 4.3.4 of the GLSL ES 3.00 spec: |
| 2918 | * "It is an error to use centroid in or interpolation qualifiers in |
| 2919 | * a vertex shader input." |
Chad Versace | 1eb0f17 | 2011-01-11 18:24:17 -0800 | [diff] [blame] | 2920 | */ |
Paul Berry | d4a2474 | 2012-08-02 08:18:12 -0700 | [diff] [blame] | 2921 | if (state->is_version(130, 300) |
Chad Versace | 1eb0f17 | 2011-01-11 18:24:17 -0800 | [diff] [blame] | 2922 | && this->type->qualifier.flags.q.centroid |
| 2923 | && this->type->qualifier.flags.q.in |
| 2924 | && state->target == vertex_shader) { |
| 2925 | |
| 2926 | _mesa_glsl_error(&loc, state, |
| 2927 | "'centroid in' cannot be used in a vertex shader"); |
| 2928 | } |
| 2929 | |
| 2930 | |
Chad Versace | 889e1a5 | 2011-01-16 22:38:45 -0800 | [diff] [blame] | 2931 | /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30. |
| 2932 | */ |
Kenneth Graunke | 6eec502 | 2013-07-15 15:58:29 -0700 | [diff] [blame] | 2933 | if (this->type->qualifier.precision != ast_precision_none) { |
Paul Berry | 0d9bba6 | 2012-08-05 09:57:01 -0700 | [diff] [blame] | 2934 | state->check_precision_qualifiers_allowed(&loc); |
Chad Versace | 889e1a5 | 2011-01-16 22:38:45 -0800 | [diff] [blame] | 2935 | } |
| 2936 | |
| 2937 | |
Chad Versace | 45e8e6c | 2011-01-17 15:28:39 -0800 | [diff] [blame] | 2938 | /* Precision qualifiers only apply to floating point and integer types. |
Chad Versace | 889e1a5 | 2011-01-16 22:38:45 -0800 | [diff] [blame] | 2939 | * |
| 2940 | * From section 4.5.2 of the GLSL 1.30 spec: |
| 2941 | * "Any floating point or any integer declaration can have the type |
| 2942 | * preceded by one of these precision qualifiers [...] Literal |
| 2943 | * constants do not have precision qualifiers. Neither do Boolean |
| 2944 | * variables. |
Kenneth Graunke | 8752824 | 2011-03-26 23:37:09 -0700 | [diff] [blame] | 2945 | * |
| 2946 | * In GLSL ES, sampler types are also allowed. |
| 2947 | * |
| 2948 | * From page 87 of the GLSL ES spec: |
| 2949 | * "RESOLUTION: Allow sampler types to take a precision qualifier." |
Chad Versace | 889e1a5 | 2011-01-16 22:38:45 -0800 | [diff] [blame] | 2950 | */ |
Kenneth Graunke | 6eec502 | 2013-07-15 15:58:29 -0700 | [diff] [blame] | 2951 | if (this->type->qualifier.precision != ast_precision_none |
Chad Versace | 45e8e6c | 2011-01-17 15:28:39 -0800 | [diff] [blame] | 2952 | && !var->type->is_float() |
| 2953 | && !var->type->is_integer() |
Kenneth Graunke | 6eec502 | 2013-07-15 15:58:29 -0700 | [diff] [blame] | 2954 | && !var->type->is_record() |
Kenneth Graunke | 8752824 | 2011-03-26 23:37:09 -0700 | [diff] [blame] | 2955 | && !(var->type->is_sampler() && state->es_shader) |
Chad Versace | 45e8e6c | 2011-01-17 15:28:39 -0800 | [diff] [blame] | 2956 | && !(var->type->is_array() |
| 2957 | && (var->type->fields.array->is_float() |
| 2958 | || var->type->fields.array->is_integer()))) { |
Chad Versace | 889e1a5 | 2011-01-16 22:38:45 -0800 | [diff] [blame] | 2959 | |
| 2960 | _mesa_glsl_error(&loc, state, |
Kenneth Graunke | 8752824 | 2011-03-26 23:37:09 -0700 | [diff] [blame] | 2961 | "precision qualifiers apply only to floating point" |
| 2962 | "%s types", state->es_shader ? ", integer, and sampler" |
| 2963 | : "and integer"); |
Chad Versace | 889e1a5 | 2011-01-16 22:38:45 -0800 | [diff] [blame] | 2964 | } |
| 2965 | |
Paul Berry | f072210 | 2011-07-12 12:03:02 -0700 | [diff] [blame] | 2966 | /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec: |
| 2967 | * |
| 2968 | * "[Sampler types] can only be declared as function |
| 2969 | * parameters or uniform variables (see Section 4.3.5 |
| 2970 | * "Uniform")". |
| 2971 | */ |
| 2972 | if (var_type->contains_sampler() && |
| 2973 | !this->type->qualifier.flags.q.uniform) { |
| 2974 | _mesa_glsl_error(&loc, state, "samplers must be declared uniform"); |
| 2975 | } |
| 2976 | |
Ian Romanick | e78e0fa | 2010-07-07 12:13:34 -0700 | [diff] [blame] | 2977 | /* Process the initializer and add its instructions to a temporary |
| 2978 | * list. This list will be added to the instruction stream (below) after |
| 2979 | * the declaration is added. This is done because in some cases (such as |
| 2980 | * redeclarations) the declaration may not actually be added to the |
| 2981 | * instruction stream. |
| 2982 | */ |
Eric Anholt | fa33d0b | 2010-07-29 13:50:17 -0700 | [diff] [blame] | 2983 | exec_list initializer_instructions; |
Ian Romanick | 09a4ba0 | 2011-03-04 16:15:20 -0800 | [diff] [blame] | 2984 | ir_variable *earlier = get_variable_being_redeclared(var, decl, state); |
| 2985 | |
Ian Romanick | 66faec4 | 2010-03-27 18:56:53 -0700 | [diff] [blame] | 2986 | if (decl->initializer != NULL) { |
Ian Romanick | 09a4ba0 | 2011-03-04 16:15:20 -0800 | [diff] [blame] | 2987 | result = process_initializer((earlier == NULL) ? var : earlier, |
| 2988 | decl, this->type, |
Ian Romanick | 0292ffb | 2011-03-04 15:29:33 -0800 | [diff] [blame] | 2989 | &initializer_instructions, state); |
Ian Romanick | 1936015 | 2010-03-26 18:05:27 -0700 | [diff] [blame] | 2990 | } |
Ian Romanick | 17d86f4 | 2010-03-29 12:59:02 -0700 | [diff] [blame] | 2991 | |
Eric Anholt | 0ed6125 | 2010-03-31 09:29:33 -1000 | [diff] [blame] | 2992 | /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec: |
| 2993 | * |
| 2994 | * "It is an error to write to a const variable outside of |
| 2995 | * its declaration, so they must be initialized when |
| 2996 | * declared." |
| 2997 | */ |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 2998 | if (this->type->qualifier.flags.q.constant && decl->initializer == NULL) { |
Eric Anholt | 0ed6125 | 2010-03-31 09:29:33 -1000 | [diff] [blame] | 2999 | _mesa_glsl_error(& loc, state, |
Chad Versace | 46f7105 | 2011-01-18 15:15:19 -0800 | [diff] [blame] | 3000 | "const declaration of `%s' must be initialized", |
| 3001 | decl->identifier); |
Eric Anholt | 0ed6125 | 2010-03-31 09:29:33 -1000 | [diff] [blame] | 3002 | } |
| 3003 | |
Ian Romanick | 09a4ba0 | 2011-03-04 16:15:20 -0800 | [diff] [blame] | 3004 | /* If the declaration is not a redeclaration, there are a few additional |
| 3005 | * semantic checks that must be applied. In addition, variable that was |
| 3006 | * created for the declaration should be added to the IR stream. |
| 3007 | */ |
| 3008 | if (earlier == NULL) { |
| 3009 | /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec, |
| 3010 | * |
| 3011 | * "Identifiers starting with "gl_" are reserved for use by |
| 3012 | * OpenGL, and may not be declared in a shader as either a |
| 3013 | * variable or a function." |
| 3014 | */ |
| 3015 | if (strncmp(decl->identifier, "gl_", 3) == 0) |
| 3016 | _mesa_glsl_error(& loc, state, |
| 3017 | "identifier `%s' uses reserved `gl_' prefix", |
| 3018 | decl->identifier); |
Jason Wood | c475a54 | 2011-10-06 22:37:48 -0700 | [diff] [blame] | 3019 | else if (strstr(decl->identifier, "__")) { |
Eric Anholt | 684b701 | 2011-10-03 16:27:59 -0700 | [diff] [blame] | 3020 | /* From page 14 (page 20 of the PDF) of the GLSL 1.10 |
| 3021 | * spec: |
| 3022 | * |
| 3023 | * "In addition, all identifiers containing two |
| 3024 | * consecutive underscores (__) are reserved as |
| 3025 | * possible future keywords." |
| 3026 | */ |
| 3027 | _mesa_glsl_error(& loc, state, |
| 3028 | "identifier `%s' uses reserved `__' string", |
| 3029 | decl->identifier); |
| 3030 | } |
Ian Romanick | 09a4ba0 | 2011-03-04 16:15:20 -0800 | [diff] [blame] | 3031 | |
| 3032 | /* Add the variable to the symbol table. Note that the initializer's |
| 3033 | * IR was already processed earlier (though it hasn't been emitted |
| 3034 | * yet), without the variable in scope. |
| 3035 | * |
| 3036 | * This differs from most C-like languages, but it follows the GLSL |
| 3037 | * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50 |
| 3038 | * spec: |
| 3039 | * |
| 3040 | * "Within a declaration, the scope of a name starts immediately |
| 3041 | * after the initializer if present or immediately after the name |
| 3042 | * being declared if not." |
| 3043 | */ |
| 3044 | if (!state->symbols->add_variable(var)) { |
| 3045 | YYLTYPE loc = this->get_location(); |
| 3046 | _mesa_glsl_error(&loc, state, "name `%s' already taken in the " |
| 3047 | "current scope", decl->identifier); |
| 3048 | continue; |
| 3049 | } |
| 3050 | |
| 3051 | /* Push the variable declaration to the top. It means that all the |
| 3052 | * variable declarations will appear in a funny last-to-first order, |
| 3053 | * but otherwise we run into trouble if a function is prototyped, a |
| 3054 | * global var is decled, then the function is defined with usage of |
| 3055 | * the global var. See glslparsertest's CorrectModule.frag. |
| 3056 | */ |
| 3057 | instructions->push_head(var); |
Ian Romanick | 5466b63 | 2010-07-01 12:46:55 -0700 | [diff] [blame] | 3058 | } |
| 3059 | |
Eric Anholt | fa33d0b | 2010-07-29 13:50:17 -0700 | [diff] [blame] | 3060 | instructions->append_list(&initializer_instructions); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3061 | } |
| 3062 | |
Eric Anholt | 8558459 | 2010-04-14 15:38:52 -0700 | [diff] [blame] | 3063 | |
| 3064 | /* Generally, variable declarations do not have r-values. However, |
| 3065 | * one is used for the declaration in |
| 3066 | * |
| 3067 | * while (bool b = some_condition()) { |
| 3068 | * ... |
| 3069 | * } |
| 3070 | * |
| 3071 | * so we return the rvalue from the last seen declaration here. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3072 | */ |
Eric Anholt | 8558459 | 2010-04-14 15:38:52 -0700 | [diff] [blame] | 3073 | return result; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3074 | } |
| 3075 | |
| 3076 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 3077 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 3078 | ast_parameter_declarator::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 3079 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3080 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 3081 | void *ctx = state; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3082 | const struct glsl_type *type; |
| 3083 | const char *name = NULL; |
Eric Anholt | 2e063f1 | 2010-03-28 00:56:22 -0700 | [diff] [blame] | 3084 | YYLTYPE loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3085 | |
Ian Romanick | d612a12 | 2010-03-31 16:22:06 -0700 | [diff] [blame] | 3086 | type = this->type->specifier->glsl_type(& name, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3087 | |
| 3088 | if (type == NULL) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3089 | if (name != NULL) { |
| 3090 | _mesa_glsl_error(& loc, state, |
| 3091 | "invalid type `%s' in declaration of `%s'", |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 3092 | name, this->identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3093 | } else { |
| 3094 | _mesa_glsl_error(& loc, state, |
| 3095 | "invalid type in declaration of `%s'", |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 3096 | this->identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3097 | } |
| 3098 | |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 3099 | type = glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3100 | } |
| 3101 | |
Eric Anholt | 068c80c | 2010-03-31 09:56:36 -1000 | [diff] [blame] | 3102 | /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec: |
| 3103 | * |
| 3104 | * "Functions that accept no input arguments need not use void in the |
| 3105 | * argument list because prototypes (or definitions) are required and |
| 3106 | * therefore there is no ambiguity when an empty argument list "( )" is |
| 3107 | * declared. The idiom "(void)" as a parameter list is provided for |
| 3108 | * convenience." |
| 3109 | * |
| 3110 | * Placing this check here prevents a void parameter being set up |
| 3111 | * for a function, which avoids tripping up checks for main taking |
| 3112 | * parameters and lookups of an unnamed symbol. |
| 3113 | */ |
Ian Romanick | cf37c9e | 2010-04-02 15:30:45 -0700 | [diff] [blame] | 3114 | if (type->is_void()) { |
| 3115 | if (this->identifier != NULL) |
| 3116 | _mesa_glsl_error(& loc, state, |
| 3117 | "named parameter cannot have type `void'"); |
| 3118 | |
| 3119 | is_void = true; |
Eric Anholt | 068c80c | 2010-03-31 09:56:36 -1000 | [diff] [blame] | 3120 | return NULL; |
Ian Romanick | cf37c9e | 2010-04-02 15:30:45 -0700 | [diff] [blame] | 3121 | } |
Eric Anholt | 068c80c | 2010-03-31 09:56:36 -1000 | [diff] [blame] | 3122 | |
Ian Romanick | 45d8a70 | 2010-04-02 15:09:33 -0700 | [diff] [blame] | 3123 | if (formal_parameter && (this->identifier == NULL)) { |
| 3124 | _mesa_glsl_error(& loc, state, "formal parameter lacks a name"); |
| 3125 | return NULL; |
| 3126 | } |
| 3127 | |
Kenneth Graunke | e511a35 | 2010-08-21 15:30:34 -0700 | [diff] [blame] | 3128 | /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...) |
| 3129 | * call already handled the "vec4[..] foo" case. |
| 3130 | */ |
| 3131 | if (this->is_array) { |
Kenneth Graunke | d8e34e2 | 2010-08-07 02:56:01 -0700 | [diff] [blame] | 3132 | type = process_array_type(&loc, type, this->array_size, state); |
Kenneth Graunke | e511a35 | 2010-08-21 15:30:34 -0700 | [diff] [blame] | 3133 | } |
| 3134 | |
Ian Romanick | a04211e | 2011-10-24 11:45:50 -0700 | [diff] [blame] | 3135 | if (!type->is_error() && type->array_size() == 0) { |
Kenneth Graunke | e511a35 | 2010-08-21 15:30:34 -0700 | [diff] [blame] | 3136 | _mesa_glsl_error(&loc, state, "arrays passed as parameters must have " |
| 3137 | "a declared size."); |
| 3138 | type = glsl_type::error_type; |
| 3139 | } |
| 3140 | |
Ian Romanick | cf37c9e | 2010-04-02 15:30:45 -0700 | [diff] [blame] | 3141 | is_void = false; |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 3142 | ir_variable *var = new(ctx) |
| 3143 | ir_variable(type, this->identifier, ir_var_function_in); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3144 | |
Ian Romanick | cdb8d54 | 2010-03-11 14:48:51 -0800 | [diff] [blame] | 3145 | /* Apply any specified qualifiers to the parameter declaration. Note that |
| 3146 | * for function parameters the default mode is 'in'. |
| 3147 | */ |
Eric Anholt | f7561e8 | 2012-04-26 18:19:39 -0700 | [diff] [blame] | 3148 | apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc, |
Paul Berry | c33be48 | 2012-12-18 14:49:34 -0800 | [diff] [blame] | 3149 | false, true); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3150 | |
Paul Berry | f072210 | 2011-07-12 12:03:02 -0700 | [diff] [blame] | 3151 | /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec: |
| 3152 | * |
| 3153 | * "Samplers cannot be treated as l-values; hence cannot be used |
| 3154 | * as out or inout function parameters, nor can they be assigned |
| 3155 | * into." |
| 3156 | */ |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 3157 | if ((var->mode == ir_var_function_inout || var->mode == ir_var_function_out) |
Paul Berry | f072210 | 2011-07-12 12:03:02 -0700 | [diff] [blame] | 3158 | && type->contains_sampler()) { |
| 3159 | _mesa_glsl_error(&loc, state, "out and inout parameters cannot contain samplers"); |
| 3160 | type = glsl_type::error_type; |
| 3161 | } |
| 3162 | |
Paul Berry | 00792e3 | 2011-09-10 07:48:46 -0700 | [diff] [blame] | 3163 | /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec: |
| 3164 | * |
| 3165 | * "When calling a function, expressions that do not evaluate to |
| 3166 | * l-values cannot be passed to parameters declared as out or inout." |
| 3167 | * |
| 3168 | * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec: |
| 3169 | * |
| 3170 | * "Other binary or unary expressions, non-dereferenced arrays, |
| 3171 | * function names, swizzles with repeated fields, and constants |
| 3172 | * cannot be l-values." |
| 3173 | * |
| 3174 | * So for GLSL 1.10, passing an array as an out or inout parameter is not |
| 3175 | * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES. |
| 3176 | */ |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 3177 | if ((var->mode == ir_var_function_inout || var->mode == ir_var_function_out) |
Paul Berry | 0d9bba6 | 2012-08-05 09:57:01 -0700 | [diff] [blame] | 3178 | && type->is_array() |
| 3179 | && !state->check_version(120, 100, &loc, |
| 3180 | "Arrays cannot be out or inout parameters")) { |
Paul Berry | 00792e3 | 2011-09-10 07:48:46 -0700 | [diff] [blame] | 3181 | type = glsl_type::error_type; |
| 3182 | } |
| 3183 | |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 3184 | instructions->push_tail(var); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3185 | |
| 3186 | /* Parameter declarations do not have r-values. |
| 3187 | */ |
| 3188 | return NULL; |
| 3189 | } |
| 3190 | |
| 3191 | |
Ian Romanick | 45d8a70 | 2010-04-02 15:09:33 -0700 | [diff] [blame] | 3192 | void |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 3193 | ast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters, |
Ian Romanick | 45d8a70 | 2010-04-02 15:09:33 -0700 | [diff] [blame] | 3194 | bool formal, |
| 3195 | exec_list *ir_parameters, |
| 3196 | _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3197 | { |
Ian Romanick | cf37c9e | 2010-04-02 15:30:45 -0700 | [diff] [blame] | 3198 | ast_parameter_declarator *void_param = NULL; |
| 3199 | unsigned count = 0; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3200 | |
Ian Romanick | 2b97dc6 | 2010-05-10 17:42:05 -0700 | [diff] [blame] | 3201 | foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) { |
Ian Romanick | 45d8a70 | 2010-04-02 15:09:33 -0700 | [diff] [blame] | 3202 | param->formal_parameter = formal; |
Eric Anholt | 068c80c | 2010-03-31 09:56:36 -1000 | [diff] [blame] | 3203 | param->hir(ir_parameters, state); |
Ian Romanick | cf37c9e | 2010-04-02 15:30:45 -0700 | [diff] [blame] | 3204 | |
| 3205 | if (param->is_void) |
| 3206 | void_param = param; |
| 3207 | |
| 3208 | count++; |
| 3209 | } |
| 3210 | |
| 3211 | if ((void_param != NULL) && (count > 1)) { |
| 3212 | YYLTYPE loc = void_param->get_location(); |
| 3213 | |
| 3214 | _mesa_glsl_error(& loc, state, |
| 3215 | "`void' parameter must be only parameter"); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3216 | } |
| 3217 | } |
| 3218 | |
| 3219 | |
Kenneth Graunke | 6fae1e4 | 2010-12-06 10:54:05 -0800 | [diff] [blame] | 3220 | void |
Paul Berry | 0d81b0e | 2011-07-29 15:28:52 -0700 | [diff] [blame] | 3221 | emit_function(_mesa_glsl_parse_state *state, ir_function *f) |
Kenneth Graunke | 6fae1e4 | 2010-12-06 10:54:05 -0800 | [diff] [blame] | 3222 | { |
Paul Berry | 0d81b0e | 2011-07-29 15:28:52 -0700 | [diff] [blame] | 3223 | /* IR invariants disallow function declarations or definitions |
| 3224 | * nested within other function definitions. But there is no |
| 3225 | * requirement about the relative order of function declarations |
| 3226 | * and definitions with respect to one another. So simply insert |
| 3227 | * the new ir_function block at the end of the toplevel instruction |
| 3228 | * list. |
| 3229 | */ |
| 3230 | state->toplevel_ir->push_tail(f); |
Kenneth Graunke | 6fae1e4 | 2010-12-06 10:54:05 -0800 | [diff] [blame] | 3231 | } |
| 3232 | |
| 3233 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 3234 | ir_rvalue * |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 3235 | ast_function::hir(exec_list *instructions, |
| 3236 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3237 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 3238 | void *ctx = state; |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 3239 | ir_function *f = NULL; |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 3240 | ir_function_signature *sig = NULL; |
| 3241 | exec_list hir_parameters; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3242 | |
Kenneth Graunke | ac04c25 | 2010-06-29 00:48:10 -0700 | [diff] [blame] | 3243 | const char *const name = identifier; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3244 | |
Ian Romanick | 9a3bd5e | 2011-08-29 14:56:29 -0700 | [diff] [blame] | 3245 | /* New functions are always added to the top-level IR instruction stream, |
| 3246 | * so this instruction list pointer is ignored. See also emit_function |
| 3247 | * (called below). |
| 3248 | */ |
| 3249 | (void) instructions; |
| 3250 | |
Ian Romanick | 63b80f8 | 2010-09-01 06:34:58 -0700 | [diff] [blame] | 3251 | /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec, |
| 3252 | * |
| 3253 | * "Function declarations (prototypes) cannot occur inside of functions; |
| 3254 | * they must be at global scope, or for the built-in functions, outside |
| 3255 | * the global scope." |
| 3256 | * |
| 3257 | * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec, |
| 3258 | * |
| 3259 | * "User defined functions may only be defined within the global scope." |
| 3260 | * |
| 3261 | * Note that this language does not appear in GLSL 1.10. |
| 3262 | */ |
Paul Berry | e3ded7f | 2012-08-01 14:50:05 -0700 | [diff] [blame] | 3263 | if ((state->current_function != NULL) && |
| 3264 | state->is_version(120, 100)) { |
Ian Romanick | 63b80f8 | 2010-09-01 06:34:58 -0700 | [diff] [blame] | 3265 | YYLTYPE loc = this->get_location(); |
| 3266 | _mesa_glsl_error(&loc, state, |
| 3267 | "declaration of function `%s' not allowed within " |
| 3268 | "function body", name); |
| 3269 | } |
| 3270 | |
Kenneth Graunke | edd180f | 2010-08-20 02:14:35 -0700 | [diff] [blame] | 3271 | /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec, |
| 3272 | * |
| 3273 | * "Identifiers starting with "gl_" are reserved for use by |
| 3274 | * OpenGL, and may not be declared in a shader as either a |
| 3275 | * variable or a function." |
| 3276 | */ |
| 3277 | if (strncmp(name, "gl_", 3) == 0) { |
| 3278 | YYLTYPE loc = this->get_location(); |
| 3279 | _mesa_glsl_error(&loc, state, |
| 3280 | "identifier `%s' uses reserved `gl_' prefix", name); |
| 3281 | } |
| 3282 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3283 | /* Convert the list of function parameters to HIR now so that they can be |
| 3284 | * used below to compare this function's signature with previously seen |
| 3285 | * signatures for functions with the same name. |
| 3286 | */ |
Ian Romanick | 45d8a70 | 2010-04-02 15:09:33 -0700 | [diff] [blame] | 3287 | ast_parameter_declarator::parameters_to_hir(& this->parameters, |
| 3288 | is_definition, |
| 3289 | & hir_parameters, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3290 | |
Ian Romanick | e39cc69 | 2010-03-23 12:19:13 -0700 | [diff] [blame] | 3291 | const char *return_type_name; |
| 3292 | const glsl_type *return_type = |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 3293 | this->return_type->specifier->glsl_type(& return_type_name, state); |
Ian Romanick | e39cc69 | 2010-03-23 12:19:13 -0700 | [diff] [blame] | 3294 | |
Eric Anholt | 76e96d7 | 2010-08-23 13:26:52 -0700 | [diff] [blame] | 3295 | if (!return_type) { |
| 3296 | YYLTYPE loc = this->get_location(); |
| 3297 | _mesa_glsl_error(&loc, state, |
| 3298 | "function `%s' has undeclared return type `%s'", |
| 3299 | name, return_type_name); |
| 3300 | return_type = glsl_type::error_type; |
| 3301 | } |
Ian Romanick | e39cc69 | 2010-03-23 12:19:13 -0700 | [diff] [blame] | 3302 | |
Kenneth Graunke | ac04c25 | 2010-06-29 00:48:10 -0700 | [diff] [blame] | 3303 | /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec: |
| 3304 | * "No qualifier is allowed on the return type of a function." |
| 3305 | */ |
| 3306 | if (this->return_type->has_qualifiers()) { |
| 3307 | YYLTYPE loc = this->get_location(); |
| 3308 | _mesa_glsl_error(& loc, state, |
| 3309 | "function `%s' return type has qualifiers", name); |
| 3310 | } |
| 3311 | |
Paul Berry | f072210 | 2011-07-12 12:03:02 -0700 | [diff] [blame] | 3312 | /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec: |
| 3313 | * |
| 3314 | * "[Sampler types] can only be declared as function parameters |
| 3315 | * or uniform variables (see Section 4.3.5 "Uniform")". |
| 3316 | */ |
| 3317 | if (return_type->contains_sampler()) { |
| 3318 | YYLTYPE loc = this->get_location(); |
| 3319 | _mesa_glsl_error(&loc, state, |
| 3320 | "function `%s' return type can't contain a sampler", |
| 3321 | name); |
| 3322 | } |
| 3323 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3324 | /* Verify that this function's signature either doesn't match a previously |
| 3325 | * seen signature for a function with the same name, or, if a match is found, |
| 3326 | * that the previously seen signature does not have an associated definition. |
| 3327 | */ |
Ian Romanick | e466b18 | 2010-09-01 14:16:53 -0700 | [diff] [blame] | 3328 | f = state->symbols->get_function(name); |
Kenneth Graunke | 81f0339 | 2010-09-16 02:52:25 -0700 | [diff] [blame] | 3329 | if (f != NULL && (state->es_shader || f->has_user_signature())) { |
Ian Romanick | 202604e | 2010-08-11 16:58:25 -0700 | [diff] [blame] | 3330 | sig = f->exact_matching_signature(&hir_parameters); |
Kenneth Graunke | 0d605cb | 2010-04-28 12:04:23 -0700 | [diff] [blame] | 3331 | if (sig != NULL) { |
| 3332 | const char *badvar = sig->qualifiers_match(&hir_parameters); |
| 3333 | if (badvar != NULL) { |
| 3334 | YYLTYPE loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3335 | |
Kenneth Graunke | 0d605cb | 2010-04-28 12:04:23 -0700 | [diff] [blame] | 3336 | _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' " |
| 3337 | "qualifiers don't match prototype", name, badvar); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3338 | } |
| 3339 | |
Kenneth Graunke | 0d605cb | 2010-04-28 12:04:23 -0700 | [diff] [blame] | 3340 | if (sig->return_type != return_type) { |
| 3341 | YYLTYPE loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3342 | |
Kenneth Graunke | 0d605cb | 2010-04-28 12:04:23 -0700 | [diff] [blame] | 3343 | _mesa_glsl_error(&loc, state, "function `%s' return type doesn't " |
| 3344 | "match prototype", name); |
| 3345 | } |
| 3346 | |
Kenneth Graunke | 6c5cf8b | 2013-04-30 00:58:09 -0700 | [diff] [blame] | 3347 | if (sig->is_defined) { |
| 3348 | if (is_definition) { |
| 3349 | YYLTYPE loc = this->get_location(); |
| 3350 | _mesa_glsl_error(& loc, state, "function `%s' redefined", name); |
| 3351 | } else { |
| 3352 | /* We just encountered a prototype that exactly matches a |
| 3353 | * function that's already been defined. This is redundant, |
| 3354 | * and we should ignore it. |
| 3355 | */ |
| 3356 | return NULL; |
| 3357 | } |
Kenneth Graunke | 0d605cb | 2010-04-28 12:04:23 -0700 | [diff] [blame] | 3358 | } |
| 3359 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3360 | } else { |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 3361 | f = new(ctx) ir_function(name); |
Eric Anholt | e8f5ebf | 2010-11-05 06:08:45 -0700 | [diff] [blame] | 3362 | if (!state->symbols->add_function(f)) { |
Kenneth Graunke | e095913 | 2010-08-25 16:37:46 -0700 | [diff] [blame] | 3363 | /* This function name shadows a non-function use of the same name. */ |
| 3364 | YYLTYPE loc = this->get_location(); |
| 3365 | |
| 3366 | _mesa_glsl_error(&loc, state, "function name `%s' conflicts with " |
| 3367 | "non-function", name); |
| 3368 | return NULL; |
| 3369 | } |
Kenneth Graunke | 9fa99f3 | 2010-04-21 12:30:22 -0700 | [diff] [blame] | 3370 | |
Paul Berry | 0d81b0e | 2011-07-29 15:28:52 -0700 | [diff] [blame] | 3371 | emit_function(state, f); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3372 | } |
| 3373 | |
Eric Anholt | ab372da | 2010-03-28 01:24:55 -0700 | [diff] [blame] | 3374 | /* Verify the return type of main() */ |
| 3375 | if (strcmp(name, "main") == 0) { |
Ian Romanick | 25711a8 | 2010-03-31 17:39:10 -0700 | [diff] [blame] | 3376 | if (! return_type->is_void()) { |
Eric Anholt | ab372da | 2010-03-28 01:24:55 -0700 | [diff] [blame] | 3377 | YYLTYPE loc = this->get_location(); |
| 3378 | |
| 3379 | _mesa_glsl_error(& loc, state, "main() must return void"); |
| 3380 | } |
Eric Anholt | 174cc03 | 2010-03-30 23:37:51 -1000 | [diff] [blame] | 3381 | |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 3382 | if (!hir_parameters.is_empty()) { |
Eric Anholt | 174cc03 | 2010-03-30 23:37:51 -1000 | [diff] [blame] | 3383 | YYLTYPE loc = this->get_location(); |
| 3384 | |
| 3385 | _mesa_glsl_error(& loc, state, "main() must not take any parameters"); |
| 3386 | } |
Eric Anholt | ab372da | 2010-03-28 01:24:55 -0700 | [diff] [blame] | 3387 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3388 | |
| 3389 | /* Finish storing the information about this new function in its signature. |
| 3390 | */ |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 3391 | if (sig == NULL) { |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 3392 | sig = new(ctx) ir_function_signature(return_type); |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 3393 | f->add_signature(sig); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3394 | } |
| 3395 | |
Kenneth Graunke | bff6013 | 2010-04-28 12:44:24 -0700 | [diff] [blame] | 3396 | sig->replace_parameters(&hir_parameters); |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 3397 | signature = sig; |
Ian Romanick | e29a585 | 2010-03-31 17:54:26 -0700 | [diff] [blame] | 3398 | |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 3399 | /* Function declarations (prototypes) do not have r-values. |
| 3400 | */ |
| 3401 | return NULL; |
| 3402 | } |
| 3403 | |
| 3404 | |
| 3405 | ir_rvalue * |
| 3406 | ast_function_definition::hir(exec_list *instructions, |
| 3407 | struct _mesa_glsl_parse_state *state) |
| 3408 | { |
| 3409 | prototype->is_definition = true; |
| 3410 | prototype->hir(instructions, state); |
| 3411 | |
| 3412 | ir_function_signature *signature = prototype->signature; |
Kenneth Graunke | 826a39c | 2010-08-20 02:04:52 -0700 | [diff] [blame] | 3413 | if (signature == NULL) |
| 3414 | return NULL; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3415 | |
Ian Romanick | 41ec6a4 | 2010-03-19 17:08:05 -0700 | [diff] [blame] | 3416 | assert(state->current_function == NULL); |
| 3417 | state->current_function = signature; |
Kenneth Graunke | 6de8256 | 2010-06-29 09:59:40 -0700 | [diff] [blame] | 3418 | state->found_return = false; |
Ian Romanick | 41ec6a4 | 2010-03-19 17:08:05 -0700 | [diff] [blame] | 3419 | |
Ian Romanick | e29a585 | 2010-03-31 17:54:26 -0700 | [diff] [blame] | 3420 | /* Duplicate parameters declared in the prototype as concrete variables. |
| 3421 | * Add these to the symbol table. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3422 | */ |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 3423 | state->symbols->push_scope(); |
Ian Romanick | e29a585 | 2010-03-31 17:54:26 -0700 | [diff] [blame] | 3424 | foreach_iter(exec_list_iterator, iter, signature->parameters) { |
Eric Anholt | fbc7c0b | 2010-04-07 14:32:53 -0700 | [diff] [blame] | 3425 | ir_variable *const var = ((ir_instruction *) iter.get())->as_variable(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3426 | |
Eric Anholt | fbc7c0b | 2010-04-07 14:32:53 -0700 | [diff] [blame] | 3427 | assert(var != NULL); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3428 | |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 3429 | /* The only way a parameter would "exist" is if two parameters have |
| 3430 | * the same name. |
| 3431 | */ |
| 3432 | if (state->symbols->name_declared_this_scope(var->name)) { |
| 3433 | YYLTYPE loc = this->get_location(); |
| 3434 | |
| 3435 | _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name); |
| 3436 | } else { |
Eric Anholt | 001eee5 | 2010-11-05 06:11:24 -0700 | [diff] [blame] | 3437 | state->symbols->add_variable(var); |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 3438 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3439 | } |
| 3440 | |
Kenneth Graunke | 9fa99f3 | 2010-04-21 12:30:22 -0700 | [diff] [blame] | 3441 | /* Convert the body of the function to HIR. */ |
Eric Anholt | 894ea97 | 2010-04-07 13:19:11 -0700 | [diff] [blame] | 3442 | this->body->hir(&signature->body, state); |
Kenneth Graunke | 9fa99f3 | 2010-04-21 12:30:22 -0700 | [diff] [blame] | 3443 | signature->is_defined = true; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3444 | |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 3445 | state->symbols->pop_scope(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3446 | |
Ian Romanick | 41ec6a4 | 2010-03-19 17:08:05 -0700 | [diff] [blame] | 3447 | assert(state->current_function == signature); |
| 3448 | state->current_function = NULL; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3449 | |
Kenneth Graunke | 6de8256 | 2010-06-29 09:59:40 -0700 | [diff] [blame] | 3450 | if (!signature->return_type->is_void() && !state->found_return) { |
| 3451 | YYLTYPE loc = this->get_location(); |
| 3452 | _mesa_glsl_error(& loc, state, "function `%s' has non-void return type " |
| 3453 | "%s, but no return statement", |
| 3454 | signature->function_name(), |
| 3455 | signature->return_type->name); |
| 3456 | } |
| 3457 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3458 | /* Function definitions do not have r-values. |
| 3459 | */ |
| 3460 | return NULL; |
| 3461 | } |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 3462 | |
| 3463 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 3464 | ir_rvalue * |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 3465 | ast_jump_statement::hir(exec_list *instructions, |
| 3466 | struct _mesa_glsl_parse_state *state) |
| 3467 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 3468 | void *ctx = state; |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 3469 | |
Ian Romanick | c0e76d8 | 2010-04-05 16:53:19 -0700 | [diff] [blame] | 3470 | switch (mode) { |
| 3471 | case ast_return: { |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 3472 | ir_return *inst; |
Eric Anholt | aad7c77 | 2010-03-30 23:28:20 -1000 | [diff] [blame] | 3473 | assert(state->current_function); |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 3474 | |
| 3475 | if (opt_return_value) { |
Matt Turner | 1a1b03e | 2013-05-22 12:14:32 -0700 | [diff] [blame] | 3476 | ir_rvalue *ret = opt_return_value->hir(instructions, state); |
Ian Romanick | 2db46fe | 2011-01-22 17:47:05 -0800 | [diff] [blame] | 3477 | |
| 3478 | /* The value of the return type can be NULL if the shader says |
| 3479 | * 'return foo();' and foo() is a function that returns void. |
| 3480 | * |
| 3481 | * NOTE: The GLSL spec doesn't say that this is an error. The type |
| 3482 | * of the return value is void. If the return type of the function is |
| 3483 | * also void, then this should compile without error. Seriously. |
| 3484 | */ |
| 3485 | const glsl_type *const ret_type = |
| 3486 | (ret == NULL) ? glsl_type::void_type : ret->type; |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 3487 | |
Matt Turner | 1a1b03e | 2013-05-22 12:14:32 -0700 | [diff] [blame] | 3488 | /* Implicit conversions are not allowed for return values prior to |
| 3489 | * ARB_shading_language_420pack. |
| 3490 | */ |
| 3491 | if (state->current_function->return_type != ret_type) { |
Kenneth Graunke | 18707eb | 2010-06-28 23:38:04 -0700 | [diff] [blame] | 3492 | YYLTYPE loc = this->get_location(); |
| 3493 | |
Matt Turner | 1a1b03e | 2013-05-22 12:14:32 -0700 | [diff] [blame] | 3494 | if (state->ARB_shading_language_420pack_enable) { |
| 3495 | if (!apply_implicit_conversion(state->current_function->return_type, |
| 3496 | ret, state)) { |
| 3497 | _mesa_glsl_error(& loc, state, |
| 3498 | "Could not implicitly convert return value " |
| 3499 | "to %s, in function `%s'", |
| 3500 | state->current_function->return_type->name, |
| 3501 | state->current_function->function_name()); |
| 3502 | } |
| 3503 | } else { |
| 3504 | _mesa_glsl_error(& loc, state, |
| 3505 | "`return' with wrong type %s, in function `%s' " |
| 3506 | "returning %s", |
| 3507 | ret_type->name, |
| 3508 | state->current_function->function_name(), |
| 3509 | state->current_function->return_type->name); |
| 3510 | } |
Matt Turner | fcaa48d | 2013-05-22 14:57:04 -0700 | [diff] [blame] | 3511 | } else if (state->current_function->return_type->base_type == |
| 3512 | GLSL_TYPE_VOID) { |
| 3513 | YYLTYPE loc = this->get_location(); |
| 3514 | |
| 3515 | /* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20 |
| 3516 | * specs add a clarification: |
| 3517 | * |
| 3518 | * "A void function can only use return without a return argument, even if |
| 3519 | * the return argument has void type. Return statements only accept values: |
| 3520 | * |
| 3521 | * void func1() { } |
| 3522 | * void func2() { return func1(); } // illegal return statement" |
| 3523 | */ |
| 3524 | _mesa_glsl_error(& loc, state, |
| 3525 | "void functions can only use `return' without a " |
| 3526 | "return argument"); |
| 3527 | } |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 3528 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 3529 | inst = new(ctx) ir_return(ret); |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 3530 | } else { |
Eric Anholt | aad7c77 | 2010-03-30 23:28:20 -1000 | [diff] [blame] | 3531 | if (state->current_function->return_type->base_type != |
| 3532 | GLSL_TYPE_VOID) { |
| 3533 | YYLTYPE loc = this->get_location(); |
| 3534 | |
| 3535 | _mesa_glsl_error(& loc, state, |
| 3536 | "`return' with no value, in function %s returning " |
| 3537 | "non-void", |
Kenneth Graunke | f96c52b | 2010-04-21 15:17:26 -0700 | [diff] [blame] | 3538 | state->current_function->function_name()); |
Eric Anholt | aad7c77 | 2010-03-30 23:28:20 -1000 | [diff] [blame] | 3539 | } |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 3540 | inst = new(ctx) ir_return; |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 3541 | } |
| 3542 | |
Kenneth Graunke | 6de8256 | 2010-06-29 09:59:40 -0700 | [diff] [blame] | 3543 | state->found_return = true; |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 3544 | instructions->push_tail(inst); |
Ian Romanick | c0e76d8 | 2010-04-05 16:53:19 -0700 | [diff] [blame] | 3545 | break; |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 3546 | } |
| 3547 | |
Ian Romanick | c0e76d8 | 2010-04-05 16:53:19 -0700 | [diff] [blame] | 3548 | case ast_discard: |
Eric Anholt | b980207 | 2010-03-30 23:40:14 -1000 | [diff] [blame] | 3549 | if (state->target != fragment_shader) { |
| 3550 | YYLTYPE loc = this->get_location(); |
| 3551 | |
| 3552 | _mesa_glsl_error(& loc, state, |
| 3553 | "`discard' may only appear in a fragment shader"); |
| 3554 | } |
Kenneth Graunke | 77049a7 | 2010-06-30 14:11:00 -0700 | [diff] [blame] | 3555 | instructions->push_tail(new(ctx) ir_discard); |
Ian Romanick | c0e76d8 | 2010-04-05 16:53:19 -0700 | [diff] [blame] | 3556 | break; |
| 3557 | |
| 3558 | case ast_break: |
| 3559 | case ast_continue: |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 3560 | if (mode == ast_continue && |
| 3561 | state->loop_nesting_ast == NULL) { |
Ian Romanick | 4cf20cd | 2010-04-05 17:13:47 -0700 | [diff] [blame] | 3562 | YYLTYPE loc = this->get_location(); |
| 3563 | |
| 3564 | _mesa_glsl_error(& loc, state, |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 3565 | "continue may only appear in a loop"); |
| 3566 | } else if (mode == ast_break && |
| 3567 | state->loop_nesting_ast == NULL && |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 3568 | state->switch_state.switch_nesting_ast == NULL) { |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 3569 | YYLTYPE loc = this->get_location(); |
Ian Romanick | 4cf20cd | 2010-04-05 17:13:47 -0700 | [diff] [blame] | 3570 | |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 3571 | _mesa_glsl_error(& loc, state, |
| 3572 | "break may only appear in a loop or a switch"); |
| 3573 | } else { |
| 3574 | /* For a loop, inline the for loop expression again, |
| 3575 | * since we don't know where near the end of |
| 3576 | * the loop body the normal copy of it |
Eric Anholt | 2d1ed7b | 2010-07-22 12:55:16 -0700 | [diff] [blame] | 3577 | * is going to be placed. |
| 3578 | */ |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 3579 | if (state->loop_nesting_ast != NULL && |
| 3580 | mode == ast_continue && |
| 3581 | state->loop_nesting_ast->rest_expression) { |
| 3582 | state->loop_nesting_ast->rest_expression->hir(instructions, |
| 3583 | state); |
Eric Anholt | 2d1ed7b | 2010-07-22 12:55:16 -0700 | [diff] [blame] | 3584 | } |
| 3585 | |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 3586 | if (state->switch_state.is_switch_innermost && |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 3587 | mode == ast_break) { |
| 3588 | /* Force break out of switch by setting is_break switch state. |
| 3589 | */ |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 3590 | ir_variable *const is_break_var = state->switch_state.is_break_var; |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 3591 | ir_dereference_variable *const deref_is_break_var = |
| 3592 | new(ctx) ir_dereference_variable(is_break_var); |
| 3593 | ir_constant *const true_val = new(ctx) ir_constant(true); |
| 3594 | ir_assignment *const set_break_var = |
Eric Anholt | aa5ec13 | 2012-05-14 09:14:54 -0700 | [diff] [blame] | 3595 | new(ctx) ir_assignment(deref_is_break_var, true_val); |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 3596 | |
| 3597 | instructions->push_tail(set_break_var); |
| 3598 | } |
| 3599 | else { |
| 3600 | ir_loop_jump *const jump = |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 3601 | new(ctx) ir_loop_jump((mode == ast_break) |
| 3602 | ? ir_loop_jump::jump_break |
| 3603 | : ir_loop_jump::jump_continue); |
Ian Romanick | 4cf20cd | 2010-04-05 17:13:47 -0700 | [diff] [blame] | 3604 | instructions->push_tail(jump); |
| 3605 | } |
| 3606 | } |
| 3607 | |
Ian Romanick | c0e76d8 | 2010-04-05 16:53:19 -0700 | [diff] [blame] | 3608 | break; |
Eric Anholt | b980207 | 2010-03-30 23:40:14 -1000 | [diff] [blame] | 3609 | } |
| 3610 | |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 3611 | /* Jump instructions do not have r-values. |
| 3612 | */ |
| 3613 | return NULL; |
| 3614 | } |
Ian Romanick | 3c6fea3 | 2010-03-29 14:11:25 -0700 | [diff] [blame] | 3615 | |
| 3616 | |
| 3617 | ir_rvalue * |
| 3618 | ast_selection_statement::hir(exec_list *instructions, |
| 3619 | struct _mesa_glsl_parse_state *state) |
| 3620 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 3621 | void *ctx = state; |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 3622 | |
Ian Romanick | 3c6fea3 | 2010-03-29 14:11:25 -0700 | [diff] [blame] | 3623 | ir_rvalue *const condition = this->condition->hir(instructions, state); |
Ian Romanick | 3c6fea3 | 2010-03-29 14:11:25 -0700 | [diff] [blame] | 3624 | |
| 3625 | /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec: |
| 3626 | * |
| 3627 | * "Any expression whose type evaluates to a Boolean can be used as the |
| 3628 | * conditional expression bool-expression. Vector types are not accepted |
| 3629 | * as the expression to if." |
| 3630 | * |
| 3631 | * The checks are separated so that higher quality diagnostics can be |
| 3632 | * generated for cases where both rules are violated. |
| 3633 | */ |
| 3634 | if (!condition->type->is_boolean() || !condition->type->is_scalar()) { |
| 3635 | YYLTYPE loc = this->condition->get_location(); |
| 3636 | |
| 3637 | _mesa_glsl_error(& loc, state, "if-statement condition must be scalar " |
| 3638 | "boolean"); |
| 3639 | } |
| 3640 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 3641 | ir_if *const stmt = new(ctx) ir_if(condition); |
Ian Romanick | 3c6fea3 | 2010-03-29 14:11:25 -0700 | [diff] [blame] | 3642 | |
Kenneth Graunke | 665d75c | 2010-08-18 13:54:50 -0700 | [diff] [blame] | 3643 | if (then_statement != NULL) { |
| 3644 | state->symbols->push_scope(); |
Ian Romanick | 4f9d72f | 2010-05-10 11:10:26 -0700 | [diff] [blame] | 3645 | then_statement->hir(& stmt->then_instructions, state); |
Kenneth Graunke | 665d75c | 2010-08-18 13:54:50 -0700 | [diff] [blame] | 3646 | state->symbols->pop_scope(); |
| 3647 | } |
Ian Romanick | 3c6fea3 | 2010-03-29 14:11:25 -0700 | [diff] [blame] | 3648 | |
Kenneth Graunke | 665d75c | 2010-08-18 13:54:50 -0700 | [diff] [blame] | 3649 | if (else_statement != NULL) { |
| 3650 | state->symbols->push_scope(); |
Ian Romanick | 4f9d72f | 2010-05-10 11:10:26 -0700 | [diff] [blame] | 3651 | else_statement->hir(& stmt->else_instructions, state); |
Kenneth Graunke | 665d75c | 2010-08-18 13:54:50 -0700 | [diff] [blame] | 3652 | state->symbols->pop_scope(); |
| 3653 | } |
Ian Romanick | 3c6fea3 | 2010-03-29 14:11:25 -0700 | [diff] [blame] | 3654 | |
| 3655 | instructions->push_tail(stmt); |
| 3656 | |
| 3657 | /* if-statements do not have r-values. |
| 3658 | */ |
| 3659 | return NULL; |
| 3660 | } |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 3661 | |
| 3662 | |
Dan McCabe | 85beb39 | 2011-11-07 15:11:04 -0800 | [diff] [blame] | 3663 | ir_rvalue * |
| 3664 | ast_switch_statement::hir(exec_list *instructions, |
| 3665 | struct _mesa_glsl_parse_state *state) |
| 3666 | { |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 3667 | void *ctx = state; |
| 3668 | |
| 3669 | ir_rvalue *const test_expression = |
| 3670 | this->test_expression->hir(instructions, state); |
| 3671 | |
| 3672 | /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec: |
| 3673 | * |
| 3674 | * "The type of init-expression in a switch statement must be a |
| 3675 | * scalar integer." |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 3676 | */ |
Eric Anholt | bbbc7c7 | 2012-05-14 08:45:59 -0700 | [diff] [blame] | 3677 | if (!test_expression->type->is_scalar() || |
| 3678 | !test_expression->type->is_integer()) { |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 3679 | YYLTYPE loc = this->test_expression->get_location(); |
| 3680 | |
| 3681 | _mesa_glsl_error(& loc, |
| 3682 | state, |
| 3683 | "switch-statement expression must be scalar " |
| 3684 | "integer"); |
| 3685 | } |
| 3686 | |
| 3687 | /* Track the switch-statement nesting in a stack-like manner. |
| 3688 | */ |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 3689 | struct glsl_switch_state saved = state->switch_state; |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 3690 | |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 3691 | state->switch_state.is_switch_innermost = true; |
| 3692 | state->switch_state.switch_nesting_ast = this; |
Eric Anholt | 1406321 | 2012-01-30 08:50:14 -0800 | [diff] [blame] | 3693 | state->switch_state.labels_ht = hash_table_ctor(0, hash_table_pointer_hash, |
| 3694 | hash_table_pointer_compare); |
Eric Anholt | 57e4437 | 2012-01-30 09:50:35 -0800 | [diff] [blame] | 3695 | state->switch_state.previous_default = NULL; |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 3696 | |
| 3697 | /* Initalize is_fallthru state to false. |
| 3698 | */ |
| 3699 | ir_rvalue *const is_fallthru_val = new (ctx) ir_constant(false); |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 3700 | state->switch_state.is_fallthru_var = |
| 3701 | new(ctx) ir_variable(glsl_type::bool_type, |
| 3702 | "switch_is_fallthru_tmp", |
| 3703 | ir_var_temporary); |
| 3704 | instructions->push_tail(state->switch_state.is_fallthru_var); |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 3705 | |
| 3706 | ir_dereference_variable *deref_is_fallthru_var = |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 3707 | new(ctx) ir_dereference_variable(state->switch_state.is_fallthru_var); |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 3708 | instructions->push_tail(new(ctx) ir_assignment(deref_is_fallthru_var, |
Eric Anholt | aa5ec13 | 2012-05-14 09:14:54 -0700 | [diff] [blame] | 3709 | is_fallthru_val)); |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 3710 | |
| 3711 | /* Initalize is_break state to false. |
| 3712 | */ |
| 3713 | ir_rvalue *const is_break_val = new (ctx) ir_constant(false); |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 3714 | state->switch_state.is_break_var = new(ctx) ir_variable(glsl_type::bool_type, |
| 3715 | "switch_is_break_tmp", |
| 3716 | ir_var_temporary); |
| 3717 | instructions->push_tail(state->switch_state.is_break_var); |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 3718 | |
| 3719 | ir_dereference_variable *deref_is_break_var = |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 3720 | new(ctx) ir_dereference_variable(state->switch_state.is_break_var); |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 3721 | instructions->push_tail(new(ctx) ir_assignment(deref_is_break_var, |
Eric Anholt | aa5ec13 | 2012-05-14 09:14:54 -0700 | [diff] [blame] | 3722 | is_break_val)); |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 3723 | |
| 3724 | /* Cache test expression. |
| 3725 | */ |
| 3726 | test_to_hir(instructions, state); |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3727 | |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 3728 | /* Emit code for body of switch stmt. |
| 3729 | */ |
| 3730 | body->hir(instructions, state); |
| 3731 | |
Eric Anholt | 1406321 | 2012-01-30 08:50:14 -0800 | [diff] [blame] | 3732 | hash_table_dtor(state->switch_state.labels_ht); |
| 3733 | |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 3734 | state->switch_state = saved; |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 3735 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3736 | /* Switch statements do not have r-values. */ |
| 3737 | return NULL; |
| 3738 | } |
Dan McCabe | 85beb39 | 2011-11-07 15:11:04 -0800 | [diff] [blame] | 3739 | |
| 3740 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3741 | void |
| 3742 | ast_switch_statement::test_to_hir(exec_list *instructions, |
| 3743 | struct _mesa_glsl_parse_state *state) |
| 3744 | { |
| 3745 | void *ctx = state; |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 3746 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3747 | /* Cache value of test expression. */ |
| 3748 | ir_rvalue *const test_val = |
| 3749 | test_expression->hir(instructions, |
| 3750 | state); |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 3751 | |
Eric Anholt | 9c4e9ce | 2012-05-14 08:51:03 -0700 | [diff] [blame] | 3752 | state->switch_state.test_var = new(ctx) ir_variable(test_val->type, |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3753 | "switch_test_tmp", |
| 3754 | ir_var_temporary); |
| 3755 | ir_dereference_variable *deref_test_var = |
| 3756 | new(ctx) ir_dereference_variable(state->switch_state.test_var); |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 3757 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3758 | instructions->push_tail(state->switch_state.test_var); |
Eric Anholt | aa5ec13 | 2012-05-14 09:14:54 -0700 | [diff] [blame] | 3759 | instructions->push_tail(new(ctx) ir_assignment(deref_test_var, test_val)); |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3760 | } |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 3761 | |
| 3762 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3763 | ir_rvalue * |
| 3764 | ast_switch_body::hir(exec_list *instructions, |
| 3765 | struct _mesa_glsl_parse_state *state) |
| 3766 | { |
| 3767 | if (stmts != NULL) |
| 3768 | stmts->hir(instructions, state); |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 3769 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3770 | /* Switch bodies do not have r-values. */ |
| 3771 | return NULL; |
| 3772 | } |
| 3773 | |
| 3774 | ir_rvalue * |
| 3775 | ast_case_statement_list::hir(exec_list *instructions, |
| 3776 | struct _mesa_glsl_parse_state *state) |
| 3777 | { |
| 3778 | foreach_list_typed (ast_case_statement, case_stmt, link, & this->cases) |
| 3779 | case_stmt->hir(instructions, state); |
| 3780 | |
| 3781 | /* Case statements do not have r-values. */ |
| 3782 | return NULL; |
| 3783 | } |
| 3784 | |
| 3785 | ir_rvalue * |
| 3786 | ast_case_statement::hir(exec_list *instructions, |
| 3787 | struct _mesa_glsl_parse_state *state) |
| 3788 | { |
| 3789 | labels->hir(instructions, state); |
| 3790 | |
| 3791 | /* Conditionally set fallthru state based on break state. */ |
| 3792 | ir_constant *const false_val = new(state) ir_constant(false); |
| 3793 | ir_dereference_variable *const deref_is_fallthru_var = |
| 3794 | new(state) ir_dereference_variable(state->switch_state.is_fallthru_var); |
| 3795 | ir_dereference_variable *const deref_is_break_var = |
| 3796 | new(state) ir_dereference_variable(state->switch_state.is_break_var); |
| 3797 | ir_assignment *const reset_fallthru_on_break = |
| 3798 | new(state) ir_assignment(deref_is_fallthru_var, |
| 3799 | false_val, |
| 3800 | deref_is_break_var); |
| 3801 | instructions->push_tail(reset_fallthru_on_break); |
| 3802 | |
| 3803 | /* Guard case statements depending on fallthru state. */ |
| 3804 | ir_dereference_variable *const deref_fallthru_guard = |
| 3805 | new(state) ir_dereference_variable(state->switch_state.is_fallthru_var); |
| 3806 | ir_if *const test_fallthru = new(state) ir_if(deref_fallthru_guard); |
| 3807 | |
| 3808 | foreach_list_typed (ast_node, stmt, link, & this->stmts) |
| 3809 | stmt->hir(& test_fallthru->then_instructions, state); |
| 3810 | |
| 3811 | instructions->push_tail(test_fallthru); |
| 3812 | |
| 3813 | /* Case statements do not have r-values. */ |
| 3814 | return NULL; |
| 3815 | } |
Dan McCabe | 85beb39 | 2011-11-07 15:11:04 -0800 | [diff] [blame] | 3816 | |
| 3817 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3818 | ir_rvalue * |
| 3819 | ast_case_label_list::hir(exec_list *instructions, |
| 3820 | struct _mesa_glsl_parse_state *state) |
| 3821 | { |
| 3822 | foreach_list_typed (ast_case_label, label, link, & this->labels) |
| 3823 | label->hir(instructions, state); |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 3824 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3825 | /* Case labels do not have r-values. */ |
| 3826 | return NULL; |
| 3827 | } |
Dan McCabe | 85beb39 | 2011-11-07 15:11:04 -0800 | [diff] [blame] | 3828 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3829 | ir_rvalue * |
| 3830 | ast_case_label::hir(exec_list *instructions, |
| 3831 | struct _mesa_glsl_parse_state *state) |
| 3832 | { |
| 3833 | void *ctx = state; |
Dan McCabe | 85beb39 | 2011-11-07 15:11:04 -0800 | [diff] [blame] | 3834 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3835 | ir_dereference_variable *deref_fallthru_var = |
| 3836 | new(ctx) ir_dereference_variable(state->switch_state.is_fallthru_var); |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 3837 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3838 | ir_rvalue *const true_val = new(ctx) ir_constant(true); |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 3839 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3840 | /* If not default case, ... */ |
| 3841 | if (this->test_value != NULL) { |
| 3842 | /* Conditionally set fallthru state based on |
| 3843 | * comparison of cached test expression value to case label. |
| 3844 | */ |
| 3845 | ir_rvalue *const label_rval = this->test_value->hir(instructions, state); |
| 3846 | ir_constant *label_const = label_rval->constant_expression_value(); |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 3847 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3848 | if (!label_const) { |
| 3849 | YYLTYPE loc = this->test_value->get_location(); |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 3850 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3851 | _mesa_glsl_error(& loc, state, |
| 3852 | "switch statement case label must be a " |
| 3853 | "constant expression"); |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 3854 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3855 | /* Stuff a dummy value in to allow processing to continue. */ |
| 3856 | label_const = new(ctx) ir_constant(0); |
| 3857 | } else { |
| 3858 | ast_expression *previous_label = (ast_expression *) |
| 3859 | hash_table_find(state->switch_state.labels_ht, |
| 3860 | (void *)(uintptr_t)label_const->value.u[0]); |
Dan McCabe | 85beb39 | 2011-11-07 15:11:04 -0800 | [diff] [blame] | 3861 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3862 | if (previous_label) { |
| 3863 | YYLTYPE loc = this->test_value->get_location(); |
| 3864 | _mesa_glsl_error(& loc, state, |
| 3865 | "duplicate case value"); |
Dan McCabe | 85beb39 | 2011-11-07 15:11:04 -0800 | [diff] [blame] | 3866 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3867 | loc = previous_label->get_location(); |
| 3868 | _mesa_glsl_error(& loc, state, |
| 3869 | "this is the previous case label"); |
| 3870 | } else { |
| 3871 | hash_table_insert(state->switch_state.labels_ht, |
| 3872 | this->test_value, |
Eric Anholt | 1406321 | 2012-01-30 08:50:14 -0800 | [diff] [blame] | 3873 | (void *)(uintptr_t)label_const->value.u[0]); |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3874 | } |
| 3875 | } |
Eric Anholt | 1406321 | 2012-01-30 08:50:14 -0800 | [diff] [blame] | 3876 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3877 | ir_dereference_variable *deref_test_var = |
| 3878 | new(ctx) ir_dereference_variable(state->switch_state.test_var); |
Eric Anholt | 1406321 | 2012-01-30 08:50:14 -0800 | [diff] [blame] | 3879 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3880 | ir_rvalue *const test_cond = new(ctx) ir_expression(ir_binop_all_equal, |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3881 | label_const, |
| 3882 | deref_test_var); |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 3883 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3884 | ir_assignment *set_fallthru_on_test = |
| 3885 | new(ctx) ir_assignment(deref_fallthru_var, |
| 3886 | true_val, |
| 3887 | test_cond); |
Dan McCabe | 5c02e2e | 2011-11-07 16:17:58 -0800 | [diff] [blame] | 3888 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3889 | instructions->push_tail(set_fallthru_on_test); |
| 3890 | } else { /* default case */ |
| 3891 | if (state->switch_state.previous_default) { |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3892 | YYLTYPE loc = this->get_location(); |
| 3893 | _mesa_glsl_error(& loc, state, |
| 3894 | "multiple default labels in one switch"); |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 3895 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3896 | loc = state->switch_state.previous_default->get_location(); |
| 3897 | _mesa_glsl_error(& loc, state, |
| 3898 | "this is the first default label"); |
| 3899 | } |
| 3900 | state->switch_state.previous_default = this; |
Eric Anholt | 57e4437 | 2012-01-30 09:50:35 -0800 | [diff] [blame] | 3901 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3902 | /* Set falltrhu state. */ |
| 3903 | ir_assignment *set_fallthru = |
Eric Anholt | aa5ec13 | 2012-05-14 09:14:54 -0700 | [diff] [blame] | 3904 | new(ctx) ir_assignment(deref_fallthru_var, true_val); |
Eric Anholt | 57e4437 | 2012-01-30 09:50:35 -0800 | [diff] [blame] | 3905 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3906 | instructions->push_tail(set_fallthru); |
| 3907 | } |
Eric Anholt | 57e4437 | 2012-01-30 09:50:35 -0800 | [diff] [blame] | 3908 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3909 | /* Case statements do not have r-values. */ |
| 3910 | return NULL; |
| 3911 | } |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 3912 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3913 | void |
| 3914 | ast_iteration_statement::condition_to_hir(ir_loop *stmt, |
| 3915 | struct _mesa_glsl_parse_state *state) |
| 3916 | { |
| 3917 | void *ctx = state; |
Eric Anholt | 22d81f1 | 2012-01-28 11:26:02 -0800 | [diff] [blame] | 3918 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3919 | if (condition != NULL) { |
| 3920 | ir_rvalue *const cond = |
| 3921 | condition->hir(& stmt->body_instructions, state); |
| 3922 | |
| 3923 | if ((cond == NULL) |
| 3924 | || !cond->type->is_boolean() || !cond->type->is_scalar()) { |
| 3925 | YYLTYPE loc = condition->get_location(); |
| 3926 | |
| 3927 | _mesa_glsl_error(& loc, state, |
| 3928 | "loop condition must be scalar boolean"); |
| 3929 | } else { |
| 3930 | /* As the first code in the loop body, generate a block that looks |
| 3931 | * like 'if (!condition) break;' as the loop termination condition. |
| 3932 | */ |
| 3933 | ir_rvalue *const not_cond = |
Eric Anholt | 5d6ea16 | 2012-05-14 08:39:54 -0700 | [diff] [blame] | 3934 | new(ctx) ir_expression(ir_unop_logic_not, cond); |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3935 | |
| 3936 | ir_if *const if_stmt = new(ctx) ir_if(not_cond); |
| 3937 | |
| 3938 | ir_jump *const break_stmt = |
| 3939 | new(ctx) ir_loop_jump(ir_loop_jump::jump_break); |
| 3940 | |
| 3941 | if_stmt->then_instructions.push_tail(break_stmt); |
| 3942 | stmt->body_instructions.push_tail(if_stmt); |
| 3943 | } |
| 3944 | } |
| 3945 | } |
Dan McCabe | 85beb39 | 2011-11-07 15:11:04 -0800 | [diff] [blame] | 3946 | |
| 3947 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3948 | ir_rvalue * |
| 3949 | ast_iteration_statement::hir(exec_list *instructions, |
| 3950 | struct _mesa_glsl_parse_state *state) |
| 3951 | { |
| 3952 | void *ctx = state; |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 3953 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3954 | /* For-loops and while-loops start a new scope, but do-while loops do not. |
| 3955 | */ |
| 3956 | if (mode != ast_do_while) |
| 3957 | state->symbols->push_scope(); |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 3958 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3959 | if (init_statement != NULL) |
| 3960 | init_statement->hir(instructions, state); |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 3961 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3962 | ir_loop *const stmt = new(ctx) ir_loop(); |
| 3963 | instructions->push_tail(stmt); |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 3964 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3965 | /* Track the current loop nesting. */ |
| 3966 | ast_iteration_statement *nesting_ast = state->loop_nesting_ast; |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 3967 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3968 | state->loop_nesting_ast = this; |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 3969 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3970 | /* Likewise, indicate that following code is closest to a loop, |
| 3971 | * NOT closest to a switch. |
| 3972 | */ |
| 3973 | bool saved_is_switch_innermost = state->switch_state.is_switch_innermost; |
| 3974 | state->switch_state.is_switch_innermost = false; |
Ian Romanick | 8c46ed2 | 2010-04-05 18:07:27 -0700 | [diff] [blame] | 3975 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3976 | if (mode != ast_do_while) |
| 3977 | condition_to_hir(stmt, state); |
Ian Romanick | 8c46ed2 | 2010-04-05 18:07:27 -0700 | [diff] [blame] | 3978 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3979 | if (body != NULL) |
| 3980 | body->hir(& stmt->body_instructions, state); |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 3981 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3982 | if (rest_expression != NULL) |
| 3983 | rest_expression->hir(& stmt->body_instructions, state); |
Ian Romanick | 8c46ed2 | 2010-04-05 18:07:27 -0700 | [diff] [blame] | 3984 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3985 | if (mode == ast_do_while) |
| 3986 | condition_to_hir(stmt, state); |
Ian Romanick | 8c46ed2 | 2010-04-05 18:07:27 -0700 | [diff] [blame] | 3987 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3988 | if (mode != ast_do_while) |
| 3989 | state->symbols->pop_scope(); |
Ian Romanick | 8c46ed2 | 2010-04-05 18:07:27 -0700 | [diff] [blame] | 3990 | |
Eric Anholt | 5462f36 | 2012-05-14 08:37:50 -0700 | [diff] [blame] | 3991 | /* Restore previous nesting before returning. */ |
| 3992 | state->loop_nesting_ast = nesting_ast; |
| 3993 | state->switch_state.is_switch_innermost = saved_is_switch_innermost; |
Ian Romanick | e9d0f26 | 2010-04-05 17:01:53 -0700 | [diff] [blame] | 3994 | |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 3995 | /* Loops do not have r-values. |
| 3996 | */ |
| 3997 | return NULL; |
| 3998 | } |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 3999 | |
| 4000 | |
Paul Berry | d5948f2 | 2013-02-12 12:36:41 -0800 | [diff] [blame] | 4001 | /** |
| 4002 | * Determine if the given type is valid for establishing a default precision |
| 4003 | * qualifier. |
| 4004 | * |
| 4005 | * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"): |
| 4006 | * |
| 4007 | * "The precision statement |
| 4008 | * |
| 4009 | * precision precision-qualifier type; |
| 4010 | * |
| 4011 | * can be used to establish a default precision qualifier. The type field |
| 4012 | * can be either int or float or any of the sampler types, and the |
| 4013 | * precision-qualifier can be lowp, mediump, or highp." |
| 4014 | * |
| 4015 | * GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision |
| 4016 | * qualifiers on sampler types, but this seems like an oversight (since the |
| 4017 | * intention of including these in GLSL 1.30 is to allow compatibility with ES |
| 4018 | * shaders). So we allow int, float, and all sampler types regardless of GLSL |
| 4019 | * version. |
| 4020 | */ |
| 4021 | static bool |
| 4022 | is_valid_default_precision_type(const struct _mesa_glsl_parse_state *state, |
| 4023 | const char *type_name) |
| 4024 | { |
| 4025 | const struct glsl_type *type = state->symbols->get_type(type_name); |
| 4026 | if (type == NULL) |
| 4027 | return false; |
| 4028 | |
| 4029 | switch (type->base_type) { |
| 4030 | case GLSL_TYPE_INT: |
| 4031 | case GLSL_TYPE_FLOAT: |
| 4032 | /* "int" and "float" are valid, but vectors and matrices are not. */ |
| 4033 | return type->vector_elements == 1 && type->matrix_columns == 1; |
| 4034 | case GLSL_TYPE_SAMPLER: |
| 4035 | return true; |
| 4036 | default: |
| 4037 | return false; |
| 4038 | } |
| 4039 | } |
| 4040 | |
| 4041 | |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 4042 | ir_rvalue * |
| 4043 | ast_type_specifier::hir(exec_list *instructions, |
| 4044 | struct _mesa_glsl_parse_state *state) |
| 4045 | { |
Kenneth Graunke | 308d4c7 | 2013-07-15 15:39:35 -0700 | [diff] [blame] | 4046 | if (this->default_precision == ast_precision_none && this->structure == NULL) |
Chad Versace | 08a286c | 2011-01-16 21:44:57 -0800 | [diff] [blame] | 4047 | return NULL; |
| 4048 | |
| 4049 | YYLTYPE loc = this->get_location(); |
| 4050 | |
Chad Versace | 08a286c | 2011-01-16 21:44:57 -0800 | [diff] [blame] | 4051 | /* If this is a precision statement, check that the type to which it is |
| 4052 | * applied is either float or int. |
| 4053 | * |
| 4054 | * From section 4.5.3 of the GLSL 1.30 spec: |
| 4055 | * "The precision statement |
| 4056 | * precision precision-qualifier type; |
| 4057 | * can be used to establish a default precision qualifier. The type |
| 4058 | * field can be either int or float [...]. Any other types or |
| 4059 | * qualifiers will result in an error. |
| 4060 | */ |
Kenneth Graunke | 308d4c7 | 2013-07-15 15:39:35 -0700 | [diff] [blame] | 4061 | if (this->default_precision != ast_precision_none) { |
Kenneth Graunke | 6eec502 | 2013-07-15 15:58:29 -0700 | [diff] [blame] | 4062 | if (!state->check_precision_qualifiers_allowed(&loc)) |
| 4063 | return NULL; |
| 4064 | |
| 4065 | if (this->structure != NULL) { |
| 4066 | _mesa_glsl_error(&loc, state, |
| 4067 | "precision qualifiers do not apply to structures"); |
| 4068 | return NULL; |
| 4069 | } |
| 4070 | |
Chad Versace | 08a286c | 2011-01-16 21:44:57 -0800 | [diff] [blame] | 4071 | if (this->is_array) { |
| 4072 | _mesa_glsl_error(&loc, state, |
| 4073 | "default precision statements do not apply to " |
| 4074 | "arrays"); |
| 4075 | return NULL; |
| 4076 | } |
Paul Berry | d5948f2 | 2013-02-12 12:36:41 -0800 | [diff] [blame] | 4077 | if (!is_valid_default_precision_type(state, this->type_name)) { |
Chad Versace | 08a286c | 2011-01-16 21:44:57 -0800 | [diff] [blame] | 4078 | _mesa_glsl_error(&loc, state, |
| 4079 | "default precision statements apply only to types " |
Paul Berry | d5948f2 | 2013-02-12 12:36:41 -0800 | [diff] [blame] | 4080 | "float, int, and sampler types"); |
Chad Versace | 08a286c | 2011-01-16 21:44:57 -0800 | [diff] [blame] | 4081 | return NULL; |
| 4082 | } |
| 4083 | |
| 4084 | /* FINISHME: Translate precision statements into IR. */ |
| 4085 | return NULL; |
| 4086 | } |
| 4087 | |
Matt Turner | 1b0d6ae | 2013-06-29 19:29:16 -0700 | [diff] [blame] | 4088 | /* _mesa_ast_set_aggregate_type() sets the <structure> field so that |
| 4089 | * process_record_constructor() can do type-checking on C-style initializer |
| 4090 | * expressions of structs, but ast_struct_specifier should only be translated |
| 4091 | * to HIR if it is declaring the type of a structure. |
| 4092 | * |
| 4093 | * The ->is_declaration field is false for initializers of variables |
| 4094 | * declared separately from the struct's type definition. |
| 4095 | * |
| 4096 | * struct S { ... }; (is_declaration = true) |
| 4097 | * struct T { ... } t = { ... }; (is_declaration = true) |
| 4098 | * S s = { ... }; (is_declaration = false) |
| 4099 | */ |
| 4100 | if (this->structure != NULL && this->structure->is_declaration) |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 4101 | return this->structure->hir(instructions, state); |
Ian Romanick | 85ba37b | 2010-04-21 14:33:34 -0700 | [diff] [blame] | 4102 | |
| 4103 | return NULL; |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 4104 | } |
| 4105 | |
| 4106 | |
Ian Romanick | 51f740c | 2012-12-08 17:38:30 -0800 | [diff] [blame] | 4107 | /** |
| 4108 | * Process a structure or interface block tree into an array of structure fields |
| 4109 | * |
| 4110 | * After parsing, where there are some syntax differnces, structures and |
| 4111 | * interface blocks are almost identical. They are similar enough that the |
| 4112 | * AST for each can be processed the same way into a set of |
| 4113 | * \c glsl_struct_field to describe the members. |
| 4114 | * |
| 4115 | * \return |
| 4116 | * The number of fields processed. A pointer to the array structure fields is |
| 4117 | * stored in \c *fields_ret. |
| 4118 | */ |
| 4119 | unsigned |
| 4120 | ast_process_structure_or_interface_block(exec_list *instructions, |
| 4121 | struct _mesa_glsl_parse_state *state, |
| 4122 | exec_list *declarations, |
| 4123 | YYLTYPE &loc, |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 4124 | glsl_struct_field **fields_ret, |
| 4125 | bool is_interface, |
| 4126 | bool block_row_major) |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 4127 | { |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 4128 | unsigned decl_count = 0; |
| 4129 | |
Ian Romanick | 51f740c | 2012-12-08 17:38:30 -0800 | [diff] [blame] | 4130 | /* Make an initial pass over the list of fields to determine how |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 4131 | * many there are. Each element in this list is an ast_declarator_list. |
| 4132 | * This means that we actually need to count the number of elements in the |
| 4133 | * 'declarations' list in each of the elements. |
| 4134 | */ |
Ian Romanick | 51f740c | 2012-12-08 17:38:30 -0800 | [diff] [blame] | 4135 | foreach_list_typed (ast_declarator_list, decl_list, link, declarations) { |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 4136 | foreach_list_const (decl_ptr, & decl_list->declarations) { |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 4137 | decl_count++; |
| 4138 | } |
| 4139 | } |
| 4140 | |
Ian Romanick | 51f740c | 2012-12-08 17:38:30 -0800 | [diff] [blame] | 4141 | /* Allocate storage for the fields and process the field |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 4142 | * declarations. As the declarations are processed, try to also convert |
| 4143 | * the types to HIR. This ensures that structure definitions embedded in |
Ian Romanick | 51f740c | 2012-12-08 17:38:30 -0800 | [diff] [blame] | 4144 | * other structure definitions or in interface blocks are processed. |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 4145 | */ |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 4146 | glsl_struct_field *const fields = ralloc_array(state, glsl_struct_field, |
Eric Anholt | 21b0dbd | 2010-07-20 16:47:25 -0700 | [diff] [blame] | 4147 | decl_count); |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 4148 | |
| 4149 | unsigned i = 0; |
Ian Romanick | 51f740c | 2012-12-08 17:38:30 -0800 | [diff] [blame] | 4150 | foreach_list_typed (ast_declarator_list, decl_list, link, declarations) { |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 4151 | const char *type_name; |
| 4152 | |
| 4153 | decl_list->type->specifier->hir(instructions, state); |
| 4154 | |
Kenneth Graunke | c98deb1 | 2010-08-16 14:02:25 -0700 | [diff] [blame] | 4155 | /* Section 10.9 of the GLSL ES 1.00 specification states that |
| 4156 | * embedded structure definitions have been removed from the language. |
| 4157 | */ |
| 4158 | if (state->es_shader && decl_list->type->specifier->structure != NULL) { |
Kenneth Graunke | c98deb1 | 2010-08-16 14:02:25 -0700 | [diff] [blame] | 4159 | _mesa_glsl_error(&loc, state, "Embedded structure definitions are " |
| 4160 | "not allowed in GLSL ES 1.00."); |
| 4161 | } |
| 4162 | |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 4163 | const glsl_type *decl_type = |
| 4164 | decl_list->type->specifier->glsl_type(& type_name, state); |
| 4165 | |
Ian Romanick | 2b97dc6 | 2010-05-10 17:42:05 -0700 | [diff] [blame] | 4166 | foreach_list_typed (ast_declaration, decl, link, |
| 4167 | &decl_list->declarations) { |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 4168 | /* From the GL_ARB_uniform_buffer_object spec: |
| 4169 | * |
| 4170 | * "Sampler types are not allowed inside of uniform |
| 4171 | * blocks. All other types, arrays, and structures |
| 4172 | * allowed for uniforms are allowed within a uniform |
| 4173 | * block." |
Ian Romanick | 278c9af | 2013-04-08 16:37:04 -0700 | [diff] [blame] | 4174 | * |
| 4175 | * It should be impossible for decl_type to be NULL here. Cases that |
| 4176 | * might naturally lead to decl_type being NULL, especially for the |
| 4177 | * is_interface case, will have resulted in compilation having |
| 4178 | * already halted due to a syntax error. |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 4179 | */ |
Ian Romanick | 278c9af | 2013-04-08 16:37:04 -0700 | [diff] [blame] | 4180 | const struct glsl_type *field_type = |
| 4181 | decl_type != NULL ? decl_type : glsl_type::error_type; |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 4182 | |
| 4183 | if (is_interface && field_type->contains_sampler()) { |
| 4184 | YYLTYPE loc = decl_list->get_location(); |
| 4185 | _mesa_glsl_error(&loc, state, |
| 4186 | "Uniform in non-default uniform block contains sampler\n"); |
| 4187 | } |
| 4188 | |
| 4189 | const struct ast_type_qualifier *const qual = |
| 4190 | & decl_list->type->qualifier; |
| 4191 | if (qual->flags.q.std140 || |
| 4192 | qual->flags.q.packed || |
| 4193 | qual->flags.q.shared) { |
| 4194 | _mesa_glsl_error(&loc, state, |
| 4195 | "uniform block layout qualifiers std140, packed, and " |
| 4196 | "shared can only be applied to uniform blocks, not " |
| 4197 | "members"); |
| 4198 | } |
| 4199 | |
Kenneth Graunke | d8e34e2 | 2010-08-07 02:56:01 -0700 | [diff] [blame] | 4200 | if (decl->is_array) { |
Kenneth Graunke | d8e34e2 | 2010-08-07 02:56:01 -0700 | [diff] [blame] | 4201 | field_type = process_array_type(&loc, decl_type, decl->array_size, |
| 4202 | state); |
| 4203 | } |
Ian Romanick | 278c9af | 2013-04-08 16:37:04 -0700 | [diff] [blame] | 4204 | fields[i].type = field_type; |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 4205 | fields[i].name = decl->identifier; |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 4206 | |
| 4207 | if (qual->flags.q.row_major || qual->flags.q.column_major) { |
Jordan Justen | cb29a70 | 2013-03-23 17:16:28 -0700 | [diff] [blame] | 4208 | if (!qual->flags.q.uniform) { |
| 4209 | _mesa_glsl_error(&loc, state, |
| 4210 | "row_major and column_major can only be " |
| 4211 | "applied to uniform interface blocks."); |
| 4212 | } else if (!field_type->is_matrix() && !field_type->is_record()) { |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 4213 | _mesa_glsl_error(&loc, state, |
| 4214 | "uniform block layout qualifiers row_major and " |
| 4215 | "column_major can only be applied to matrix and " |
| 4216 | "structure types"); |
| 4217 | } else |
| 4218 | validate_matrix_layout_for_type(state, &loc, field_type); |
| 4219 | } |
| 4220 | |
Jordan Justen | 067cc08 | 2013-03-23 17:14:37 -0700 | [diff] [blame] | 4221 | if (qual->flags.q.uniform && qual->has_interpolation()) { |
| 4222 | _mesa_glsl_error(&loc, state, |
| 4223 | "interpolation qualifiers cannot be used " |
| 4224 | "with uniform interface blocks"); |
| 4225 | } |
| 4226 | |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 4227 | if (field_type->is_matrix() || |
| 4228 | (field_type->is_array() && field_type->fields.array->is_matrix())) { |
| 4229 | fields[i].row_major = block_row_major; |
| 4230 | if (qual->flags.q.row_major) |
| 4231 | fields[i].row_major = true; |
| 4232 | else if (qual->flags.q.column_major) |
| 4233 | fields[i].row_major = false; |
| 4234 | } |
| 4235 | |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 4236 | i++; |
| 4237 | } |
| 4238 | } |
| 4239 | |
| 4240 | assert(i == decl_count); |
| 4241 | |
Ian Romanick | 51f740c | 2012-12-08 17:38:30 -0800 | [diff] [blame] | 4242 | *fields_ret = fields; |
| 4243 | return decl_count; |
| 4244 | } |
| 4245 | |
| 4246 | |
| 4247 | ir_rvalue * |
| 4248 | ast_struct_specifier::hir(exec_list *instructions, |
| 4249 | struct _mesa_glsl_parse_state *state) |
| 4250 | { |
| 4251 | YYLTYPE loc = this->get_location(); |
| 4252 | glsl_struct_field *fields; |
| 4253 | unsigned decl_count = |
| 4254 | ast_process_structure_or_interface_block(instructions, |
| 4255 | state, |
| 4256 | &this->declarations, |
| 4257 | loc, |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 4258 | &fields, |
| 4259 | false, |
| 4260 | false); |
Ian Romanick | 51f740c | 2012-12-08 17:38:30 -0800 | [diff] [blame] | 4261 | |
Ian Romanick | 49e3577 | 2010-06-28 11:54:57 -0700 | [diff] [blame] | 4262 | const glsl_type *t = |
Kenneth Graunke | ca92ae2 | 2010-09-18 11:11:09 +0200 | [diff] [blame] | 4263 | glsl_type::get_record_instance(fields, decl_count, this->name); |
Ian Romanick | 1d28b61 | 2010-04-20 16:48:24 -0700 | [diff] [blame] | 4264 | |
Ian Romanick | a789ca6 | 2010-09-01 14:08:08 -0700 | [diff] [blame] | 4265 | if (!state->symbols->add_type(name, t)) { |
Ian Romanick | ab89927 | 2010-04-23 13:24:08 -0700 | [diff] [blame] | 4266 | _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name); |
| 4267 | } else { |
Kenneth Graunke | eb63934 | 2011-02-27 01:17:29 -0800 | [diff] [blame] | 4268 | const glsl_type **s = reralloc(state, state->user_structures, |
| 4269 | const glsl_type *, |
| 4270 | state->num_user_structures + 1); |
Ian Romanick | a2c6df5 | 2010-04-28 13:14:53 -0700 | [diff] [blame] | 4271 | if (s != NULL) { |
| 4272 | s[state->num_user_structures] = t; |
| 4273 | state->user_structures = s; |
| 4274 | state->num_user_structures++; |
| 4275 | } |
Ian Romanick | ab89927 | 2010-04-23 13:24:08 -0700 | [diff] [blame] | 4276 | } |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 4277 | |
| 4278 | /* Structure type definitions do not have r-values. |
| 4279 | */ |
| 4280 | return NULL; |
| 4281 | } |
Eric Anholt | 4b2a4cb | 2012-03-29 17:29:20 -0700 | [diff] [blame] | 4282 | |
Eric Anholt | 2d03f48 | 2012-04-18 13:35:56 -0700 | [diff] [blame] | 4283 | ir_rvalue * |
Jordan Justen | c9f5854 | 2013-03-09 10:40:41 -0800 | [diff] [blame] | 4284 | ast_interface_block::hir(exec_list *instructions, |
| 4285 | struct _mesa_glsl_parse_state *state) |
Eric Anholt | 2d03f48 | 2012-04-18 13:35:56 -0700 | [diff] [blame] | 4286 | { |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 4287 | YYLTYPE loc = this->get_location(); |
| 4288 | |
Jordan Justen | c9f5854 | 2013-03-09 10:40:41 -0800 | [diff] [blame] | 4289 | /* The ast_interface_block has a list of ast_declarator_lists. We |
Eric Anholt | 2d03f48 | 2012-04-18 13:35:56 -0700 | [diff] [blame] | 4290 | * need to turn those into ir_variables with an association |
| 4291 | * with this uniform block. |
| 4292 | */ |
Ian Romanick | 514f8c7 | 2013-01-22 01:09:16 -0500 | [diff] [blame] | 4293 | enum glsl_interface_packing packing; |
Ian Romanick | 9a971ab | 2012-12-13 02:13:30 -0800 | [diff] [blame] | 4294 | if (this->layout.flags.q.shared) { |
Ian Romanick | 514f8c7 | 2013-01-22 01:09:16 -0500 | [diff] [blame] | 4295 | packing = GLSL_INTERFACE_PACKING_SHARED; |
Ian Romanick | 9a971ab | 2012-12-13 02:13:30 -0800 | [diff] [blame] | 4296 | } else if (this->layout.flags.q.packed) { |
Ian Romanick | 514f8c7 | 2013-01-22 01:09:16 -0500 | [diff] [blame] | 4297 | packing = GLSL_INTERFACE_PACKING_PACKED; |
Ian Romanick | 9a971ab | 2012-12-13 02:13:30 -0800 | [diff] [blame] | 4298 | } else { |
| 4299 | /* The default layout is std140. |
| 4300 | */ |
Ian Romanick | 514f8c7 | 2013-01-22 01:09:16 -0500 | [diff] [blame] | 4301 | packing = GLSL_INTERFACE_PACKING_STD140; |
Ian Romanick | 9a971ab | 2012-12-13 02:13:30 -0800 | [diff] [blame] | 4302 | } |
| 4303 | |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 4304 | bool block_row_major = this->layout.flags.q.row_major; |
| 4305 | exec_list declared_variables; |
| 4306 | glsl_struct_field *fields; |
| 4307 | unsigned int num_variables = |
| 4308 | ast_process_structure_or_interface_block(&declared_variables, |
| 4309 | state, |
| 4310 | &this->declarations, |
| 4311 | loc, |
| 4312 | &fields, |
| 4313 | true, |
| 4314 | block_row_major); |
| 4315 | |
Jordan Justen | b24eeb0 | 2013-03-09 16:34:55 -0800 | [diff] [blame] | 4316 | ir_variable_mode var_mode; |
| 4317 | const char *iface_type_name; |
| 4318 | if (this->layout.flags.q.in) { |
| 4319 | var_mode = ir_var_shader_in; |
| 4320 | iface_type_name = "in"; |
| 4321 | } else if (this->layout.flags.q.out) { |
| 4322 | var_mode = ir_var_shader_out; |
| 4323 | iface_type_name = "out"; |
| 4324 | } else if (this->layout.flags.q.uniform) { |
| 4325 | var_mode = ir_var_uniform; |
| 4326 | iface_type_name = "uniform"; |
| 4327 | } else { |
Emil Velikov | 4df6823 | 2013-07-08 18:29:17 +0100 | [diff] [blame] | 4328 | var_mode = ir_var_auto; |
| 4329 | iface_type_name = "UNKNOWN"; |
Jordan Justen | b24eeb0 | 2013-03-09 16:34:55 -0800 | [diff] [blame] | 4330 | assert(!"interface block layout qualifier not found!"); |
| 4331 | } |
| 4332 | |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 4333 | const glsl_type *block_type = |
| 4334 | glsl_type::get_interface_instance(fields, |
| 4335 | num_variables, |
Ian Romanick | 514f8c7 | 2013-01-22 01:09:16 -0500 | [diff] [blame] | 4336 | packing, |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 4337 | this->block_name); |
| 4338 | |
Jordan Justen | b24eeb0 | 2013-03-09 16:34:55 -0800 | [diff] [blame] | 4339 | if (!state->symbols->add_interface(block_type->name, block_type, var_mode)) { |
Ian Romanick | 5383661 | 2013-01-21 23:01:33 -0500 | [diff] [blame] | 4340 | YYLTYPE loc = this->get_location(); |
Jordan Justen | b24eeb0 | 2013-03-09 16:34:55 -0800 | [diff] [blame] | 4341 | _mesa_glsl_error(&loc, state, "Interface block `%s' with type `%s' " |
| 4342 | "already taken in the current scope.\n", |
| 4343 | this->block_name, iface_type_name); |
Ian Romanick | 5383661 | 2013-01-21 23:01:33 -0500 | [diff] [blame] | 4344 | } |
| 4345 | |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 4346 | /* Since interface blocks cannot contain statements, it should be |
| 4347 | * impossible for the block to generate any instructions. |
| 4348 | */ |
| 4349 | assert(declared_variables.is_empty()); |
| 4350 | |
| 4351 | /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec |
| 4352 | * says: |
| 4353 | * |
| 4354 | * "If an instance name (instance-name) is used, then it puts all the |
| 4355 | * members inside a scope within its own name space, accessed with the |
| 4356 | * field selector ( . ) operator (analogously to structures)." |
| 4357 | */ |
| 4358 | if (this->instance_name) { |
Ian Romanick | 25e75b0 | 2013-01-21 23:06:45 -0500 | [diff] [blame] | 4359 | ir_variable *var; |
| 4360 | |
| 4361 | if (this->array_size != NULL) { |
| 4362 | const glsl_type *block_array_type = |
| 4363 | process_array_type(&loc, block_type, this->array_size, state); |
| 4364 | |
| 4365 | var = new(state) ir_variable(block_array_type, |
| 4366 | this->instance_name, |
Jordan Justen | b24eeb0 | 2013-03-09 16:34:55 -0800 | [diff] [blame] | 4367 | var_mode); |
Ian Romanick | 25e75b0 | 2013-01-21 23:06:45 -0500 | [diff] [blame] | 4368 | } else { |
| 4369 | var = new(state) ir_variable(block_type, |
| 4370 | this->instance_name, |
Jordan Justen | b24eeb0 | 2013-03-09 16:34:55 -0800 | [diff] [blame] | 4371 | var_mode); |
Ian Romanick | 25e75b0 | 2013-01-21 23:06:45 -0500 | [diff] [blame] | 4372 | } |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 4373 | |
Ian Romanick | 7a7b44b | 2013-01-21 21:51:15 -0500 | [diff] [blame] | 4374 | var->interface_type = block_type; |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 4375 | state->symbols->add_variable(var); |
| 4376 | instructions->push_tail(var); |
| 4377 | } else { |
Ian Romanick | 25e75b0 | 2013-01-21 23:06:45 -0500 | [diff] [blame] | 4378 | /* In order to have an array size, the block must also be declared with |
| 4379 | * an instane name. |
| 4380 | */ |
| 4381 | assert(this->array_size == NULL); |
| 4382 | |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 4383 | for (unsigned i = 0; i < num_variables; i++) { |
| 4384 | ir_variable *var = |
| 4385 | new(state) ir_variable(fields[i].type, |
| 4386 | ralloc_strdup(state, fields[i].name), |
Jordan Justen | b24eeb0 | 2013-03-09 16:34:55 -0800 | [diff] [blame] | 4387 | var_mode); |
Ian Romanick | 7a7b44b | 2013-01-21 21:51:15 -0500 | [diff] [blame] | 4388 | var->interface_type = block_type; |
Ian Romanick | 17e6f19 | 2012-12-11 12:14:03 -0800 | [diff] [blame] | 4389 | |
| 4390 | state->symbols->add_variable(var); |
| 4391 | instructions->push_tail(var); |
Eric Anholt | b3c093c | 2012-04-26 18:21:43 -0700 | [diff] [blame] | 4392 | } |
| 4393 | } |
| 4394 | |
Eric Anholt | 2d03f48 | 2012-04-18 13:35:56 -0700 | [diff] [blame] | 4395 | return NULL; |
| 4396 | } |
| 4397 | |
Eric Anholt | 4b2a4cb | 2012-03-29 17:29:20 -0700 | [diff] [blame] | 4398 | static void |
| 4399 | detect_conflicting_assignments(struct _mesa_glsl_parse_state *state, |
| 4400 | exec_list *instructions) |
| 4401 | { |
| 4402 | bool gl_FragColor_assigned = false; |
| 4403 | bool gl_FragData_assigned = false; |
| 4404 | bool user_defined_fs_output_assigned = false; |
| 4405 | ir_variable *user_defined_fs_output = NULL; |
| 4406 | |
| 4407 | /* It would be nice to have proper location information. */ |
| 4408 | YYLTYPE loc; |
| 4409 | memset(&loc, 0, sizeof(loc)); |
| 4410 | |
| 4411 | foreach_list(node, instructions) { |
| 4412 | ir_variable *var = ((ir_instruction *)node)->as_variable(); |
| 4413 | |
Eric Anholt | b2ee5a0 | 2012-04-23 16:10:12 -0700 | [diff] [blame] | 4414 | if (!var || !var->assigned) |
Eric Anholt | 4b2a4cb | 2012-03-29 17:29:20 -0700 | [diff] [blame] | 4415 | continue; |
| 4416 | |
| 4417 | if (strcmp(var->name, "gl_FragColor") == 0) |
Eric Anholt | b2ee5a0 | 2012-04-23 16:10:12 -0700 | [diff] [blame] | 4418 | gl_FragColor_assigned = true; |
Eric Anholt | 4b2a4cb | 2012-03-29 17:29:20 -0700 | [diff] [blame] | 4419 | else if (strcmp(var->name, "gl_FragData") == 0) |
Eric Anholt | b2ee5a0 | 2012-04-23 16:10:12 -0700 | [diff] [blame] | 4420 | gl_FragData_assigned = true; |
Eric Anholt | 4b2a4cb | 2012-03-29 17:29:20 -0700 | [diff] [blame] | 4421 | else if (strncmp(var->name, "gl_", 3) != 0) { |
| 4422 | if (state->target == fragment_shader && |
Paul Berry | 42a29d8 | 2013-01-11 14:39:32 -0800 | [diff] [blame] | 4423 | var->mode == ir_var_shader_out) { |
Eric Anholt | 4b2a4cb | 2012-03-29 17:29:20 -0700 | [diff] [blame] | 4424 | user_defined_fs_output_assigned = true; |
| 4425 | user_defined_fs_output = var; |
| 4426 | } |
| 4427 | } |
| 4428 | } |
| 4429 | |
| 4430 | /* From the GLSL 1.30 spec: |
| 4431 | * |
| 4432 | * "If a shader statically assigns a value to gl_FragColor, it |
| 4433 | * may not assign a value to any element of gl_FragData. If a |
| 4434 | * shader statically writes a value to any element of |
| 4435 | * gl_FragData, it may not assign a value to |
| 4436 | * gl_FragColor. That is, a shader may assign values to either |
| 4437 | * gl_FragColor or gl_FragData, but not both. Multiple shaders |
| 4438 | * linked together must also consistently write just one of |
| 4439 | * these variables. Similarly, if user declared output |
| 4440 | * variables are in use (statically assigned to), then the |
| 4441 | * built-in variables gl_FragColor and gl_FragData may not be |
| 4442 | * assigned to. These incorrect usages all generate compile |
| 4443 | * time errors." |
| 4444 | */ |
| 4445 | if (gl_FragColor_assigned && gl_FragData_assigned) { |
| 4446 | _mesa_glsl_error(&loc, state, "fragment shader writes to both " |
| 4447 | "`gl_FragColor' and `gl_FragData'\n"); |
| 4448 | } else if (gl_FragColor_assigned && user_defined_fs_output_assigned) { |
| 4449 | _mesa_glsl_error(&loc, state, "fragment shader writes to both " |
| 4450 | "`gl_FragColor' and `%s'\n", |
| 4451 | user_defined_fs_output->name); |
| 4452 | } else if (gl_FragData_assigned && user_defined_fs_output_assigned) { |
| 4453 | _mesa_glsl_error(&loc, state, "fragment shader writes to both " |
| 4454 | "`gl_FragData' and `%s'\n", |
| 4455 | user_defined_fs_output->name); |
| 4456 | } |
| 4457 | } |