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