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" |
| 57 | #include "ir.h" |
| 58 | |
Ian Romanick | d949a9a | 2010-03-10 09:55:22 -0800 | [diff] [blame] | 59 | void |
| 60 | _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state) |
| 61 | { |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 62 | _mesa_glsl_initialize_variables(instructions, state); |
| 63 | |
Kenneth Graunke | 814c89a | 2010-09-05 00:31:28 -0700 | [diff] [blame] | 64 | state->symbols->language_version = state->language_version; |
| 65 | |
Ian Romanick | 41ec6a4 | 2010-03-19 17:08:05 -0700 | [diff] [blame] | 66 | state->current_function = NULL; |
| 67 | |
Paul Berry | 0d81b0e | 2011-07-29 15:28:52 -0700 | [diff] [blame] | 68 | state->toplevel_ir = instructions; |
| 69 | |
Kenneth Graunke | a044285 | 2010-08-23 14:52:06 -0700 | [diff] [blame] | 70 | /* Section 4.2 of the GLSL 1.20 specification states: |
| 71 | * "The built-in functions are scoped in a scope outside the global scope |
| 72 | * users declare global variables in. That is, a shader's global scope, |
| 73 | * available for user-defined functions and global variables, is nested |
| 74 | * inside the scope containing the built-in functions." |
| 75 | * |
| 76 | * Since built-in functions like ftransform() access built-in variables, |
| 77 | * it follows that those must be in the outer scope as well. |
| 78 | * |
| 79 | * We push scope here to create this nesting effect...but don't pop. |
| 80 | * This way, a shader's globals are still in the symbol table for use |
| 81 | * by the linker. |
| 82 | */ |
| 83 | state->symbols->push_scope(); |
| 84 | |
Ian Romanick | 2b97dc6 | 2010-05-10 17:42:05 -0700 | [diff] [blame] | 85 | foreach_list_typed (ast_node, ast, link, & state->translation_unit) |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 86 | ast->hir(instructions, state); |
Ian Romanick | 02c5ae1 | 2011-07-11 10:46:01 -0700 | [diff] [blame] | 87 | |
| 88 | detect_recursion_unlinked(state, instructions); |
Paul Berry | 0d81b0e | 2011-07-29 15:28:52 -0700 | [diff] [blame] | 89 | |
| 90 | state->toplevel_ir = NULL; |
Ian Romanick | d949a9a | 2010-03-10 09:55:22 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 94 | /** |
| 95 | * If a conversion is available, convert one operand to a different type |
| 96 | * |
| 97 | * The \c from \c ir_rvalue is converted "in place". |
| 98 | * |
| 99 | * \param to Type that the operand it to be converted to |
| 100 | * \param from Operand that is being converted |
| 101 | * \param state GLSL compiler state |
| 102 | * |
| 103 | * \return |
| 104 | * If a conversion is possible (or unnecessary), \c true is returned. |
| 105 | * Otherwise \c false is returned. |
| 106 | */ |
Kenneth Graunke | f32d3df | 2010-09-01 20:03:17 -0700 | [diff] [blame] | 107 | bool |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 108 | apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from, |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 109 | struct _mesa_glsl_parse_state *state) |
| 110 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 111 | void *ctx = state; |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 112 | if (to->base_type == from->type->base_type) |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 113 | return true; |
| 114 | |
| 115 | /* This conversion was added in GLSL 1.20. If the compilation mode is |
| 116 | * GLSL 1.10, the conversion is skipped. |
| 117 | */ |
| 118 | if (state->language_version < 120) |
| 119 | return false; |
| 120 | |
| 121 | /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec: |
| 122 | * |
| 123 | * "There are no implicit array or structure conversions. For |
| 124 | * example, an array of int cannot be implicitly converted to an |
| 125 | * array of float. There are no implicit conversions between |
| 126 | * signed and unsigned integers." |
| 127 | */ |
| 128 | /* FINISHME: The above comment is partially a lie. There is int/uint |
| 129 | * FINISHME: conversion for immediate constants. |
| 130 | */ |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 131 | if (!to->is_float() || !from->type->is_numeric()) |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 132 | return false; |
| 133 | |
Kenneth Graunke | 506199b | 2010-06-29 15:59:27 -0700 | [diff] [blame] | 134 | /* Convert to a floating point type with the same number of components |
| 135 | * as the original type - i.e. int to float, not int to vec4. |
| 136 | */ |
| 137 | to = glsl_type::get_instance(GLSL_TYPE_FLOAT, from->type->vector_elements, |
| 138 | from->type->matrix_columns); |
| 139 | |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 140 | switch (from->type->base_type) { |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 141 | case GLSL_TYPE_INT: |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 142 | from = new(ctx) ir_expression(ir_unop_i2f, to, from, NULL); |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 143 | break; |
| 144 | case GLSL_TYPE_UINT: |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 145 | from = new(ctx) ir_expression(ir_unop_u2f, to, from, NULL); |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 146 | break; |
| 147 | case GLSL_TYPE_BOOL: |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 148 | from = new(ctx) ir_expression(ir_unop_b2f, to, from, NULL); |
Eric Anholt | dc58b3f | 2010-04-02 02:13:43 -1000 | [diff] [blame] | 149 | break; |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 150 | default: |
| 151 | assert(0); |
| 152 | } |
| 153 | |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 158 | static const struct glsl_type * |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 159 | arithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 160 | bool multiply, |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 161 | struct _mesa_glsl_parse_state *state, YYLTYPE *loc) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 162 | { |
Eric Anholt | 336b4ad | 2010-05-19 10:38:37 -0700 | [diff] [blame] | 163 | const glsl_type *type_a = value_a->type; |
| 164 | const glsl_type *type_b = value_b->type; |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 165 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 166 | /* From GLSL 1.50 spec, page 56: |
| 167 | * |
| 168 | * "The arithmetic binary operators add (+), subtract (-), |
| 169 | * multiply (*), and divide (/) operate on integer and |
| 170 | * floating-point scalars, vectors, and matrices." |
| 171 | */ |
Ian Romanick | 60b54d9 | 2010-03-24 17:08:13 -0700 | [diff] [blame] | 172 | if (!type_a->is_numeric() || !type_b->is_numeric()) { |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 173 | _mesa_glsl_error(loc, state, |
| 174 | "Operands to arithmetic operators must be numeric"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 175 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | |
| 179 | /* "If one operand is floating-point based and the other is |
| 180 | * not, then the conversions from Section 4.1.10 "Implicit |
| 181 | * Conversions" are applied to the non-floating-point-based operand." |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 182 | */ |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 183 | if (!apply_implicit_conversion(type_a, value_b, state) |
| 184 | && !apply_implicit_conversion(type_b, value_a, state)) { |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 185 | _mesa_glsl_error(loc, state, |
| 186 | "Could not implicitly convert operands to " |
| 187 | "arithmetic operator"); |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 188 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 189 | } |
Eric Anholt | 336b4ad | 2010-05-19 10:38:37 -0700 | [diff] [blame] | 190 | type_a = value_a->type; |
| 191 | type_b = value_b->type; |
| 192 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 193 | /* "If the operands are integer types, they must both be signed or |
| 194 | * both be unsigned." |
| 195 | * |
| 196 | * From this rule and the preceeding conversion it can be inferred that |
| 197 | * 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] | 198 | * The is_numeric check above already filtered out the case where either |
| 199 | * type is not one of these, so now the base types need only be tested for |
| 200 | * equality. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 201 | */ |
| 202 | if (type_a->base_type != type_b->base_type) { |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 203 | _mesa_glsl_error(loc, state, |
| 204 | "base type mismatch for arithmetic operator"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 205 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /* "All arithmetic binary operators result in the same fundamental type |
| 209 | * (signed integer, unsigned integer, or floating-point) as the |
| 210 | * operands they operate on, after operand type conversion. After |
| 211 | * conversion, the following cases are valid |
| 212 | * |
| 213 | * * The two operands are scalars. In this case the operation is |
| 214 | * applied, resulting in a scalar." |
| 215 | */ |
Ian Romanick | cb36f8a | 2010-03-09 15:51:22 -0800 | [diff] [blame] | 216 | if (type_a->is_scalar() && type_b->is_scalar()) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 217 | return type_a; |
| 218 | |
| 219 | /* "* One operand is a scalar, and the other is a vector or matrix. |
| 220 | * In this case, the scalar operation is applied independently to each |
| 221 | * component of the vector or matrix, resulting in the same size |
| 222 | * vector or matrix." |
| 223 | */ |
Ian Romanick | cb36f8a | 2010-03-09 15:51:22 -0800 | [diff] [blame] | 224 | if (type_a->is_scalar()) { |
| 225 | if (!type_b->is_scalar()) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 226 | return type_b; |
Ian Romanick | cb36f8a | 2010-03-09 15:51:22 -0800 | [diff] [blame] | 227 | } else if (type_b->is_scalar()) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 228 | return type_a; |
| 229 | } |
| 230 | |
| 231 | /* All of the combinations of <scalar, scalar>, <vector, scalar>, |
| 232 | * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been |
| 233 | * handled. |
| 234 | */ |
Ian Romanick | 60b54d9 | 2010-03-24 17:08:13 -0700 | [diff] [blame] | 235 | assert(!type_a->is_scalar()); |
| 236 | assert(!type_b->is_scalar()); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 237 | |
| 238 | /* "* The two operands are vectors of the same size. In this case, the |
| 239 | * operation is done component-wise resulting in the same size |
| 240 | * vector." |
| 241 | */ |
Ian Romanick | a2dd22f | 2010-03-09 15:55:16 -0800 | [diff] [blame] | 242 | if (type_a->is_vector() && type_b->is_vector()) { |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 243 | if (type_a == type_b) { |
| 244 | return type_a; |
| 245 | } else { |
| 246 | _mesa_glsl_error(loc, state, |
| 247 | "vector size mismatch for arithmetic operator"); |
| 248 | return glsl_type::error_type; |
| 249 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /* All of the combinations of <scalar, scalar>, <vector, scalar>, |
| 253 | * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and |
| 254 | * <vector, vector> have been handled. At least one of the operands must |
| 255 | * be matrix. Further, since there are no integer matrix types, the base |
| 256 | * type of both operands must be float. |
| 257 | */ |
Ian Romanick | 60b54d9 | 2010-03-24 17:08:13 -0700 | [diff] [blame] | 258 | assert(type_a->is_matrix() || type_b->is_matrix()); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 259 | assert(type_a->base_type == GLSL_TYPE_FLOAT); |
| 260 | assert(type_b->base_type == GLSL_TYPE_FLOAT); |
| 261 | |
| 262 | /* "* The operator is add (+), subtract (-), or divide (/), and the |
| 263 | * operands are matrices with the same number of rows and the same |
| 264 | * number of columns. In this case, the operation is done component- |
| 265 | * wise resulting in the same size matrix." |
| 266 | * * The operator is multiply (*), where both operands are matrices or |
| 267 | * one operand is a vector and the other a matrix. A right vector |
| 268 | * operand is treated as a column vector and a left vector operand as a |
| 269 | * row vector. In all these cases, it is required that the number of |
| 270 | * columns of the left operand is equal to the number of rows of the |
| 271 | * right operand. Then, the multiply (*) operation does a linear |
| 272 | * algebraic multiply, yielding an object that has the same number of |
| 273 | * rows as the left operand and the same number of columns as the right |
| 274 | * operand. Section 5.10 "Vector and Matrix Operations" explains in |
| 275 | * more detail how vectors and matrices are operated on." |
| 276 | */ |
| 277 | if (! multiply) { |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 278 | if (type_a == type_b) |
| 279 | return type_a; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 280 | } else { |
Ian Romanick | fce1150 | 2010-03-09 15:58:52 -0800 | [diff] [blame] | 281 | if (type_a->is_matrix() && type_b->is_matrix()) { |
Ian Romanick | c1bd3a1 | 2010-03-25 13:06:58 -0700 | [diff] [blame] | 282 | /* Matrix multiply. The columns of A must match the rows of B. Given |
| 283 | * the other previously tested constraints, this means the vector type |
| 284 | * of a row from A must be the same as the vector type of a column from |
| 285 | * B. |
| 286 | */ |
| 287 | if (type_a->row_type() == type_b->column_type()) { |
| 288 | /* The resulting matrix has the number of columns of matrix B and |
| 289 | * the number of rows of matrix A. We get the row count of A by |
| 290 | * looking at the size of a vector that makes up a column. The |
| 291 | * transpose (size of a row) is done for B. |
| 292 | */ |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 293 | const glsl_type *const type = |
Ian Romanick | c1bd3a1 | 2010-03-25 13:06:58 -0700 | [diff] [blame] | 294 | glsl_type::get_instance(type_a->base_type, |
| 295 | type_a->column_type()->vector_elements, |
| 296 | type_b->row_type()->vector_elements); |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 297 | assert(type != glsl_type::error_type); |
| 298 | |
| 299 | return type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 300 | } |
Ian Romanick | fce1150 | 2010-03-09 15:58:52 -0800 | [diff] [blame] | 301 | } else if (type_a->is_matrix()) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 302 | /* 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] | 303 | * rows of B. Given the other previously tested constraints, this |
| 304 | * means the vector type of a row from A must be the same as the |
| 305 | * vector the type of B. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 306 | */ |
Carl Worth | 47c90b1 | 2010-07-22 14:56:14 -0700 | [diff] [blame] | 307 | if (type_a->row_type() == type_b) { |
| 308 | /* The resulting vector has a number of elements equal to |
| 309 | * the number of rows of matrix A. */ |
| 310 | const glsl_type *const type = |
| 311 | glsl_type::get_instance(type_a->base_type, |
| 312 | type_a->column_type()->vector_elements, |
| 313 | 1); |
| 314 | assert(type != glsl_type::error_type); |
| 315 | |
| 316 | return type; |
| 317 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 318 | } else { |
Ian Romanick | fce1150 | 2010-03-09 15:58:52 -0800 | [diff] [blame] | 319 | assert(type_b->is_matrix()); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 320 | |
Ian Romanick | c1bd3a1 | 2010-03-25 13:06:58 -0700 | [diff] [blame] | 321 | /* A is a row vector and B is a matrix. Columns of A must match rows |
| 322 | * of B. Given the other previously tested constraints, this means |
| 323 | * the type of A must be the same as the vector type of a column from |
| 324 | * B. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 325 | */ |
Carl Worth | 47c90b1 | 2010-07-22 14:56:14 -0700 | [diff] [blame] | 326 | if (type_a == type_b->column_type()) { |
| 327 | /* The resulting vector has a number of elements equal to |
| 328 | * the number of columns of matrix B. */ |
| 329 | const glsl_type *const type = |
| 330 | glsl_type::get_instance(type_a->base_type, |
| 331 | type_b->row_type()->vector_elements, |
| 332 | 1); |
| 333 | assert(type != glsl_type::error_type); |
| 334 | |
| 335 | return type; |
| 336 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 337 | } |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 338 | |
| 339 | _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication"); |
| 340 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | |
| 344 | /* "All other cases are illegal." |
| 345 | */ |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 346 | _mesa_glsl_error(loc, state, "type mismatch"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 347 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | |
| 351 | static const struct glsl_type * |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 352 | unary_arithmetic_result_type(const struct glsl_type *type, |
| 353 | struct _mesa_glsl_parse_state *state, YYLTYPE *loc) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 354 | { |
| 355 | /* From GLSL 1.50 spec, page 57: |
| 356 | * |
| 357 | * "The arithmetic unary operators negate (-), post- and pre-increment |
| 358 | * and decrement (-- and ++) operate on integer or floating-point |
| 359 | * values (including vectors and matrices). All unary operators work |
| 360 | * component-wise on their operands. These result with the same type |
| 361 | * they operated on." |
| 362 | */ |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 363 | if (!type->is_numeric()) { |
| 364 | _mesa_glsl_error(loc, state, |
| 365 | "Operands to arithmetic operators must be numeric"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 366 | return glsl_type::error_type; |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 367 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 368 | |
| 369 | return type; |
| 370 | } |
| 371 | |
Chad Versace | cfdbf8b | 2010-10-15 11:28:05 -0700 | [diff] [blame] | 372 | /** |
| 373 | * \brief Return the result type of a bit-logic operation. |
| 374 | * |
| 375 | * If the given types to the bit-logic operator are invalid, return |
| 376 | * glsl_type::error_type. |
| 377 | * |
| 378 | * \param type_a Type of LHS of bit-logic op |
| 379 | * \param type_b Type of RHS of bit-logic op |
| 380 | */ |
| 381 | static const struct glsl_type * |
| 382 | bit_logic_result_type(const struct glsl_type *type_a, |
| 383 | const struct glsl_type *type_b, |
| 384 | ast_operators op, |
| 385 | struct _mesa_glsl_parse_state *state, YYLTYPE *loc) |
| 386 | { |
| 387 | if (state->language_version < 130) { |
| 388 | _mesa_glsl_error(loc, state, "bit operations require GLSL 1.30"); |
| 389 | return glsl_type::error_type; |
| 390 | } |
| 391 | |
| 392 | /* From page 50 (page 56 of PDF) of GLSL 1.30 spec: |
| 393 | * |
| 394 | * "The bitwise operators and (&), exclusive-or (^), and inclusive-or |
| 395 | * (|). The operands must be of type signed or unsigned integers or |
| 396 | * integer vectors." |
| 397 | */ |
| 398 | if (!type_a->is_integer()) { |
| 399 | _mesa_glsl_error(loc, state, "LHS of `%s' must be an integer", |
| 400 | ast_expression::operator_string(op)); |
| 401 | return glsl_type::error_type; |
| 402 | } |
| 403 | if (!type_b->is_integer()) { |
| 404 | _mesa_glsl_error(loc, state, "RHS of `%s' must be an integer", |
| 405 | ast_expression::operator_string(op)); |
| 406 | return glsl_type::error_type; |
| 407 | } |
| 408 | |
| 409 | /* "The fundamental types of the operands (signed or unsigned) must |
| 410 | * match," |
| 411 | */ |
| 412 | if (type_a->base_type != type_b->base_type) { |
| 413 | _mesa_glsl_error(loc, state, "operands of `%s' must have the same " |
| 414 | "base type", ast_expression::operator_string(op)); |
| 415 | return glsl_type::error_type; |
| 416 | } |
| 417 | |
| 418 | /* "The operands cannot be vectors of differing size." */ |
| 419 | if (type_a->is_vector() && |
| 420 | type_b->is_vector() && |
| 421 | type_a->vector_elements != type_b->vector_elements) { |
| 422 | _mesa_glsl_error(loc, state, "operands of `%s' cannot be vectors of " |
| 423 | "different sizes", ast_expression::operator_string(op)); |
| 424 | return glsl_type::error_type; |
| 425 | } |
| 426 | |
| 427 | /* "If one operand is a scalar and the other a vector, the scalar is |
| 428 | * applied component-wise to the vector, resulting in the same type as |
| 429 | * the vector. The fundamental types of the operands [...] will be the |
| 430 | * resulting fundamental type." |
| 431 | */ |
| 432 | if (type_a->is_scalar()) |
| 433 | return type_b; |
| 434 | else |
| 435 | return type_a; |
| 436 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 437 | |
| 438 | static const struct glsl_type * |
| 439 | modulus_result_type(const struct glsl_type *type_a, |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 440 | const struct glsl_type *type_b, |
| 441 | struct _mesa_glsl_parse_state *state, YYLTYPE *loc) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 442 | { |
Chad Versace | 82f994f | 2011-02-04 12:18:56 -0800 | [diff] [blame] | 443 | if (state->language_version < 130) { |
| 444 | _mesa_glsl_error(loc, state, |
| 445 | "operator '%%' is reserved in %s", |
| 446 | state->version_string); |
| 447 | return glsl_type::error_type; |
| 448 | } |
| 449 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 450 | /* From GLSL 1.50 spec, page 56: |
| 451 | * "The operator modulus (%) operates on signed or unsigned integers or |
| 452 | * integer vectors. The operand types must both be signed or both be |
| 453 | * unsigned." |
| 454 | */ |
Kenneth Graunke | 8eb9753 | 2011-06-14 22:21:41 -0700 | [diff] [blame] | 455 | if (!type_a->is_integer()) { |
| 456 | _mesa_glsl_error(loc, state, "LHS of operator %% must be an integer."); |
| 457 | return glsl_type::error_type; |
| 458 | } |
| 459 | if (!type_b->is_integer()) { |
| 460 | _mesa_glsl_error(loc, state, "RHS of operator %% must be an integer."); |
| 461 | return glsl_type::error_type; |
| 462 | } |
| 463 | if (type_a->base_type != type_b->base_type) { |
| 464 | _mesa_glsl_error(loc, state, |
| 465 | "operands of %% must have the same base type"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 466 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /* "The operands cannot be vectors of differing size. If one operand is |
| 470 | * a scalar and the other vector, then the scalar is applied component- |
| 471 | * wise to the vector, resulting in the same type as the vector. If both |
| 472 | * are vectors of the same size, the result is computed component-wise." |
| 473 | */ |
Ian Romanick | a2dd22f | 2010-03-09 15:55:16 -0800 | [diff] [blame] | 474 | if (type_a->is_vector()) { |
| 475 | if (!type_b->is_vector() |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 476 | || (type_a->vector_elements == type_b->vector_elements)) |
| 477 | return type_a; |
| 478 | } else |
| 479 | return type_b; |
| 480 | |
| 481 | /* "The operator modulus (%) is not defined for any other data types |
| 482 | * (non-integer types)." |
| 483 | */ |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 484 | _mesa_glsl_error(loc, state, "type mismatch"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 485 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | |
| 489 | static const struct glsl_type * |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 490 | relational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b, |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 491 | struct _mesa_glsl_parse_state *state, YYLTYPE *loc) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 492 | { |
Eric Anholt | 336b4ad | 2010-05-19 10:38:37 -0700 | [diff] [blame] | 493 | const glsl_type *type_a = value_a->type; |
| 494 | const glsl_type *type_b = value_b->type; |
Ian Romanick | 0150f5f | 2010-03-29 16:20:07 -0700 | [diff] [blame] | 495 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 496 | /* From GLSL 1.50 spec, page 56: |
| 497 | * "The relational operators greater than (>), less than (<), greater |
| 498 | * than or equal (>=), and less than or equal (<=) operate only on |
| 499 | * scalar integer and scalar floating-point expressions." |
| 500 | */ |
Ian Romanick | a6d653d | 2010-03-26 14:40:37 -0700 | [diff] [blame] | 501 | if (!type_a->is_numeric() |
| 502 | || !type_b->is_numeric() |
Ian Romanick | cb36f8a | 2010-03-09 15:51:22 -0800 | [diff] [blame] | 503 | || !type_a->is_scalar() |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 504 | || !type_b->is_scalar()) { |
| 505 | _mesa_glsl_error(loc, state, |
| 506 | "Operands to relational operators must be scalar and " |
| 507 | "numeric"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 508 | return glsl_type::error_type; |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 509 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 510 | |
| 511 | /* "Either the operands' types must match, or the conversions from |
| 512 | * Section 4.1.10 "Implicit Conversions" will be applied to the integer |
| 513 | * operand, after which the types must match." |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 514 | */ |
Ian Romanick | 0150f5f | 2010-03-29 16:20:07 -0700 | [diff] [blame] | 515 | if (!apply_implicit_conversion(type_a, value_b, state) |
| 516 | && !apply_implicit_conversion(type_b, value_a, state)) { |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 517 | _mesa_glsl_error(loc, state, |
| 518 | "Could not implicitly convert operands to " |
| 519 | "relational operator"); |
Ian Romanick | 0150f5f | 2010-03-29 16:20:07 -0700 | [diff] [blame] | 520 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 521 | } |
Eric Anholt | 336b4ad | 2010-05-19 10:38:37 -0700 | [diff] [blame] | 522 | type_a = value_a->type; |
| 523 | type_b = value_b->type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 524 | |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 525 | if (type_a->base_type != type_b->base_type) { |
| 526 | _mesa_glsl_error(loc, state, "base type mismatch"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 527 | return glsl_type::error_type; |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 528 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 529 | |
| 530 | /* "The result is scalar Boolean." |
| 531 | */ |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 532 | return glsl_type::bool_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 533 | } |
| 534 | |
Chad Versace | c0197ab | 2010-10-15 09:49:46 -0700 | [diff] [blame] | 535 | /** |
| 536 | * \brief Return the result type of a bit-shift operation. |
| 537 | * |
| 538 | * If the given types to the bit-shift operator are invalid, return |
| 539 | * glsl_type::error_type. |
| 540 | * |
| 541 | * \param type_a Type of LHS of bit-shift op |
| 542 | * \param type_b Type of RHS of bit-shift op |
| 543 | */ |
| 544 | static const struct glsl_type * |
| 545 | shift_result_type(const struct glsl_type *type_a, |
| 546 | const struct glsl_type *type_b, |
| 547 | ast_operators op, |
| 548 | struct _mesa_glsl_parse_state *state, YYLTYPE *loc) |
| 549 | { |
| 550 | if (state->language_version < 130) { |
| 551 | _mesa_glsl_error(loc, state, "bit operations require GLSL 1.30"); |
| 552 | return glsl_type::error_type; |
| 553 | } |
| 554 | |
| 555 | /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec: |
| 556 | * |
| 557 | * "The shift operators (<<) and (>>). For both operators, the operands |
| 558 | * must be signed or unsigned integers or integer vectors. One operand |
| 559 | * can be signed while the other is unsigned." |
| 560 | */ |
| 561 | if (!type_a->is_integer()) { |
| 562 | _mesa_glsl_error(loc, state, "LHS of operator %s must be an integer or " |
| 563 | "integer vector", ast_expression::operator_string(op)); |
| 564 | return glsl_type::error_type; |
| 565 | |
| 566 | } |
| 567 | if (!type_b->is_integer()) { |
| 568 | _mesa_glsl_error(loc, state, "RHS of operator %s must be an integer or " |
| 569 | "integer vector", ast_expression::operator_string(op)); |
| 570 | return glsl_type::error_type; |
| 571 | } |
| 572 | |
| 573 | /* "If the first operand is a scalar, the second operand has to be |
| 574 | * a scalar as well." |
| 575 | */ |
| 576 | if (type_a->is_scalar() && !type_b->is_scalar()) { |
| 577 | _mesa_glsl_error(loc, state, "If the first operand of %s is scalar, the " |
| 578 | "second must be scalar as well", |
| 579 | ast_expression::operator_string(op)); |
| 580 | return glsl_type::error_type; |
| 581 | } |
| 582 | |
| 583 | /* If both operands are vectors, check that they have same number of |
| 584 | * elements. |
| 585 | */ |
| 586 | if (type_a->is_vector() && |
| 587 | type_b->is_vector() && |
| 588 | type_a->vector_elements != type_b->vector_elements) { |
| 589 | _mesa_glsl_error(loc, state, "Vector operands to operator %s must " |
| 590 | "have same number of elements", |
| 591 | ast_expression::operator_string(op)); |
| 592 | return glsl_type::error_type; |
| 593 | } |
| 594 | |
| 595 | /* "In all cases, the resulting type will be the same type as the left |
| 596 | * operand." |
| 597 | */ |
| 598 | return type_a; |
| 599 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 600 | |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 601 | /** |
| 602 | * Validates that a value can be assigned to a location with a specified type |
| 603 | * |
| 604 | * Validates that \c rhs can be assigned to some location. If the types are |
| 605 | * not an exact match but an automatic conversion is possible, \c rhs will be |
| 606 | * converted. |
| 607 | * |
| 608 | * \return |
| 609 | * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type. |
| 610 | * Otherwise the actual RHS to be assigned will be returned. This may be |
| 611 | * \c rhs, or it may be \c rhs after some type conversion. |
| 612 | * |
| 613 | * \note |
| 614 | * In addition to being used for assignments, this function is used to |
| 615 | * type-check return values. |
| 616 | */ |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 617 | ir_rvalue * |
Eric Anholt | 336b4ad | 2010-05-19 10:38:37 -0700 | [diff] [blame] | 618 | validate_assignment(struct _mesa_glsl_parse_state *state, |
Ian Romanick | 85caea2 | 2011-03-15 16:33:27 -0700 | [diff] [blame] | 619 | const glsl_type *lhs_type, ir_rvalue *rhs, |
| 620 | bool is_initializer) |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 621 | { |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 622 | /* If there is already some error in the RHS, just return it. Anything |
| 623 | * else will lead to an avalanche of error message back to the user. |
| 624 | */ |
Ian Romanick | ec53010 | 2010-12-10 15:47:11 -0800 | [diff] [blame] | 625 | if (rhs->type->is_error()) |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 626 | return rhs; |
| 627 | |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 628 | /* If the types are identical, the assignment can trivially proceed. |
| 629 | */ |
Ian Romanick | ec53010 | 2010-12-10 15:47:11 -0800 | [diff] [blame] | 630 | if (rhs->type == lhs_type) |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 631 | return rhs; |
| 632 | |
Ian Romanick | 0157f41 | 2010-04-02 17:44:39 -0700 | [diff] [blame] | 633 | /* 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] | 634 | * the assignment is okay for initializers embedded in variable |
| 635 | * declarations. |
Ian Romanick | 0157f41 | 2010-04-02 17:44:39 -0700 | [diff] [blame] | 636 | * |
| 637 | * Note: Whole-array assignments are not permitted in GLSL 1.10, but this |
| 638 | * is handled by ir_dereference::is_lvalue. |
| 639 | */ |
Ian Romanick | 85caea2 | 2011-03-15 16:33:27 -0700 | [diff] [blame] | 640 | if (is_initializer && lhs_type->is_array() && rhs->type->is_array() |
Ian Romanick | 0157f41 | 2010-04-02 17:44:39 -0700 | [diff] [blame] | 641 | && (lhs_type->element_type() == rhs->type->element_type()) |
| 642 | && (lhs_type->array_size() == 0)) { |
| 643 | return rhs; |
| 644 | } |
| 645 | |
Eric Anholt | 336b4ad | 2010-05-19 10:38:37 -0700 | [diff] [blame] | 646 | /* Check for implicit conversion in GLSL 1.20 */ |
| 647 | if (apply_implicit_conversion(lhs_type, rhs, state)) { |
Ian Romanick | ec53010 | 2010-12-10 15:47:11 -0800 | [diff] [blame] | 648 | if (rhs->type == lhs_type) |
Eric Anholt | 336b4ad | 2010-05-19 10:38:37 -0700 | [diff] [blame] | 649 | return rhs; |
| 650 | } |
| 651 | |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 652 | return NULL; |
| 653 | } |
| 654 | |
Eric Anholt | a313c29 | 2011-08-05 21:40:50 -0700 | [diff] [blame] | 655 | static void |
| 656 | mark_whole_array_access(ir_rvalue *access) |
| 657 | { |
| 658 | ir_dereference_variable *deref = access->as_dereference_variable(); |
| 659 | |
| 660 | if (deref && deref->var) { |
| 661 | deref->var->max_array_access = deref->type->length - 1; |
| 662 | } |
| 663 | } |
| 664 | |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 665 | ir_rvalue * |
| 666 | do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state, |
Ian Romanick | 85caea2 | 2011-03-15 16:33:27 -0700 | [diff] [blame] | 667 | ir_rvalue *lhs, ir_rvalue *rhs, bool is_initializer, |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 668 | YYLTYPE lhs_loc) |
| 669 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 670 | void *ctx = state; |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 671 | bool error_emitted = (lhs->type->is_error() || rhs->type->is_error()); |
| 672 | |
| 673 | if (!error_emitted) { |
Chad Versace | b66be75 | 2011-01-21 13:44:08 -0800 | [diff] [blame] | 674 | if (lhs->variable_referenced() != NULL |
| 675 | && lhs->variable_referenced()->read_only) { |
| 676 | _mesa_glsl_error(&lhs_loc, state, |
| 677 | "assignment to read-only variable '%s'", |
| 678 | lhs->variable_referenced()->name); |
| 679 | error_emitted = true; |
| 680 | |
Eric Anholt | 525cec9 | 2011-09-07 12:03:36 -0700 | [diff] [blame] | 681 | } else if (state->language_version <= 110 && lhs->type->is_array()) { |
| 682 | /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec: |
| 683 | * |
| 684 | * "Other binary or unary expressions, non-dereferenced |
| 685 | * arrays, function names, swizzles with repeated fields, |
| 686 | * and constants cannot be l-values." |
| 687 | */ |
| 688 | _mesa_glsl_error(&lhs_loc, state, "whole array assignment is not " |
| 689 | "allowed in GLSL 1.10 or GLSL ES 1.00."); |
| 690 | error_emitted = true; |
Chad Versace | b66be75 | 2011-01-21 13:44:08 -0800 | [diff] [blame] | 691 | } else if (!lhs->is_lvalue()) { |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 692 | _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment"); |
| 693 | error_emitted = true; |
| 694 | } |
| 695 | } |
| 696 | |
Ian Romanick | 85caea2 | 2011-03-15 16:33:27 -0700 | [diff] [blame] | 697 | ir_rvalue *new_rhs = |
| 698 | validate_assignment(state, lhs->type, rhs, is_initializer); |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 699 | if (new_rhs == NULL) { |
| 700 | _mesa_glsl_error(& lhs_loc, state, "type mismatch"); |
| 701 | } else { |
| 702 | rhs = new_rhs; |
Ian Romanick | 0157f41 | 2010-04-02 17:44:39 -0700 | [diff] [blame] | 703 | |
| 704 | /* If the LHS array was not declared with a size, it takes it size from |
| 705 | * the RHS. If the LHS is an l-value and a whole array, it must be a |
| 706 | * dereference of a variable. Any other case would require that the LHS |
| 707 | * is either not an l-value or not a whole array. |
| 708 | */ |
| 709 | if (lhs->type->array_size() == 0) { |
| 710 | ir_dereference *const d = lhs->as_dereference(); |
| 711 | |
| 712 | assert(d != NULL); |
| 713 | |
Ian Romanick | 36ea286 | 2010-05-19 13:52:29 +0200 | [diff] [blame] | 714 | ir_variable *const var = d->variable_referenced(); |
Ian Romanick | 0157f41 | 2010-04-02 17:44:39 -0700 | [diff] [blame] | 715 | |
| 716 | assert(var != NULL); |
| 717 | |
Ian Romanick | 63f3942 | 2010-04-05 14:35:47 -0700 | [diff] [blame] | 718 | if (var->max_array_access >= unsigned(rhs->type->array_size())) { |
| 719 | /* FINISHME: This should actually log the location of the RHS. */ |
| 720 | _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to " |
| 721 | "previous access", |
| 722 | var->max_array_access); |
| 723 | } |
| 724 | |
Ian Romanick | f38d15b | 2010-07-20 15:33:40 -0700 | [diff] [blame] | 725 | var->type = glsl_type::get_array_instance(lhs->type->element_type(), |
Ian Romanick | 0157f41 | 2010-04-02 17:44:39 -0700 | [diff] [blame] | 726 | rhs->type->array_size()); |
Eric Anholt | 9703ed0 | 2010-07-22 15:50:37 -0700 | [diff] [blame] | 727 | d->type = var->type; |
Ian Romanick | 0157f41 | 2010-04-02 17:44:39 -0700 | [diff] [blame] | 728 | } |
Eric Anholt | 407a100 | 2011-09-07 11:53:20 -0700 | [diff] [blame] | 729 | mark_whole_array_access(rhs); |
Eric Anholt | a313c29 | 2011-08-05 21:40:50 -0700 | [diff] [blame] | 730 | mark_whole_array_access(lhs); |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 731 | } |
| 732 | |
Eric Anholt | 2731a73 | 2010-06-23 14:51:14 -0700 | [diff] [blame] | 733 | /* Most callers of do_assignment (assign, add_assign, pre_inc/dec, |
| 734 | * but not post_inc) need the converted assigned value as an rvalue |
| 735 | * to handle things like: |
| 736 | * |
| 737 | * i = j += 1; |
| 738 | * |
| 739 | * So we always just store the computed value being assigned to a |
| 740 | * temporary and return a deref of that temporary. If the rvalue |
| 741 | * ends up not being used, the temp will get copy-propagated out. |
| 742 | */ |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 743 | ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp", |
| 744 | ir_var_temporary); |
Eric Anholt | e33c103 | 2010-06-24 15:13:03 -0700 | [diff] [blame] | 745 | ir_dereference_variable *deref_var = new(ctx) ir_dereference_variable(var); |
Eric Anholt | ae80592 | 2010-06-24 09:06:12 -0700 | [diff] [blame] | 746 | instructions->push_tail(var); |
Eric Anholt | e33c103 | 2010-06-24 15:13:03 -0700 | [diff] [blame] | 747 | instructions->push_tail(new(ctx) ir_assignment(deref_var, |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 748 | rhs, |
| 749 | NULL)); |
Eric Anholt | e33c103 | 2010-06-24 15:13:03 -0700 | [diff] [blame] | 750 | deref_var = new(ctx) ir_dereference_variable(var); |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 751 | |
Ian Romanick | 8e9ce2e | 2010-08-03 15:02:35 -0700 | [diff] [blame] | 752 | if (!error_emitted) |
| 753 | instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var, NULL)); |
Eric Anholt | 2731a73 | 2010-06-23 14:51:14 -0700 | [diff] [blame] | 754 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 755 | return new(ctx) ir_dereference_variable(var); |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 756 | } |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 757 | |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 758 | static ir_rvalue * |
Eric Anholt | 959a9ec | 2010-06-23 14:43:50 -0700 | [diff] [blame] | 759 | get_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue) |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 760 | { |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 761 | void *ctx = ralloc_parent(lvalue); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 762 | ir_variable *var; |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 763 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 764 | var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp", |
| 765 | ir_var_temporary); |
Eric Anholt | 43b5b03 | 2010-07-07 14:04:30 -0700 | [diff] [blame] | 766 | instructions->push_tail(var); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 767 | var->mode = ir_var_auto; |
| 768 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 769 | instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var), |
| 770 | lvalue, NULL)); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 771 | |
| 772 | /* Once we've created this temporary, mark it read only so it's no |
| 773 | * longer considered an lvalue. |
| 774 | */ |
| 775 | var->read_only = true; |
| 776 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 777 | return new(ctx) ir_dereference_variable(var); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 781 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 782 | ast_node::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 783 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 784 | { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 785 | (void) instructions; |
| 786 | (void) state; |
| 787 | |
| 788 | return NULL; |
| 789 | } |
| 790 | |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 791 | static ir_rvalue * |
| 792 | do_comparison(void *mem_ctx, int operation, ir_rvalue *op0, ir_rvalue *op1) |
| 793 | { |
| 794 | int join_op; |
Ian Romanick | 6d36be5 | 2010-12-02 12:17:36 -0800 | [diff] [blame] | 795 | ir_rvalue *cmp = NULL; |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 796 | |
| 797 | if (operation == ir_binop_all_equal) |
| 798 | join_op = ir_binop_logic_and; |
| 799 | else |
| 800 | join_op = ir_binop_logic_or; |
| 801 | |
| 802 | switch (op0->type->base_type) { |
| 803 | case GLSL_TYPE_FLOAT: |
| 804 | case GLSL_TYPE_UINT: |
| 805 | case GLSL_TYPE_INT: |
| 806 | case GLSL_TYPE_BOOL: |
| 807 | return new(mem_ctx) ir_expression(operation, op0, op1); |
| 808 | |
| 809 | case GLSL_TYPE_ARRAY: { |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 810 | for (unsigned int i = 0; i < op0->type->length; i++) { |
| 811 | ir_rvalue *e0, *e1, *result; |
| 812 | |
| 813 | e0 = new(mem_ctx) ir_dereference_array(op0->clone(mem_ctx, NULL), |
| 814 | new(mem_ctx) ir_constant(i)); |
| 815 | e1 = new(mem_ctx) ir_dereference_array(op1->clone(mem_ctx, NULL), |
| 816 | new(mem_ctx) ir_constant(i)); |
| 817 | result = do_comparison(mem_ctx, operation, e0, e1); |
| 818 | |
Ian Romanick | 6d36be5 | 2010-12-02 12:17:36 -0800 | [diff] [blame] | 819 | if (cmp) { |
| 820 | cmp = new(mem_ctx) ir_expression(join_op, cmp, result); |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 821 | } else { |
Ian Romanick | 6d36be5 | 2010-12-02 12:17:36 -0800 | [diff] [blame] | 822 | cmp = result; |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 823 | } |
| 824 | } |
Eric Anholt | b4f5856 | 2010-12-01 15:55:53 -0800 | [diff] [blame] | 825 | |
| 826 | mark_whole_array_access(op0); |
| 827 | mark_whole_array_access(op1); |
Ian Romanick | 6d36be5 | 2010-12-02 12:17:36 -0800 | [diff] [blame] | 828 | break; |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | case GLSL_TYPE_STRUCT: { |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 832 | for (unsigned int i = 0; i < op0->type->length; i++) { |
| 833 | ir_rvalue *e0, *e1, *result; |
| 834 | const char *field_name = op0->type->fields.structure[i].name; |
| 835 | |
| 836 | e0 = new(mem_ctx) ir_dereference_record(op0->clone(mem_ctx, NULL), |
| 837 | field_name); |
| 838 | e1 = new(mem_ctx) ir_dereference_record(op1->clone(mem_ctx, NULL), |
| 839 | field_name); |
| 840 | result = do_comparison(mem_ctx, operation, e0, e1); |
| 841 | |
Ian Romanick | 6d36be5 | 2010-12-02 12:17:36 -0800 | [diff] [blame] | 842 | if (cmp) { |
| 843 | cmp = new(mem_ctx) ir_expression(join_op, cmp, result); |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 844 | } else { |
Ian Romanick | 6d36be5 | 2010-12-02 12:17:36 -0800 | [diff] [blame] | 845 | cmp = result; |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 846 | } |
| 847 | } |
Ian Romanick | 6d36be5 | 2010-12-02 12:17:36 -0800 | [diff] [blame] | 848 | break; |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | case GLSL_TYPE_ERROR: |
| 852 | case GLSL_TYPE_VOID: |
| 853 | case GLSL_TYPE_SAMPLER: |
| 854 | /* I assume a comparison of a struct containing a sampler just |
| 855 | * ignores the sampler present in the type. |
| 856 | */ |
Ian Romanick | 6d36be5 | 2010-12-02 12:17:36 -0800 | [diff] [blame] | 857 | break; |
| 858 | |
| 859 | default: |
| 860 | assert(!"Should not get here."); |
| 861 | break; |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 862 | } |
Eric Anholt | d56c974 | 2010-11-30 13:28:47 -0800 | [diff] [blame] | 863 | |
Ian Romanick | 6d36be5 | 2010-12-02 12:17:36 -0800 | [diff] [blame] | 864 | if (cmp == NULL) |
| 865 | cmp = new(mem_ctx) ir_constant(true); |
| 866 | |
| 867 | return cmp; |
Eric Anholt | ff79633 | 2010-11-30 11:23:28 -0800 | [diff] [blame] | 868 | } |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 869 | |
Eric Anholt | 0182270 | 2011-04-09 15:59:20 -1000 | [diff] [blame] | 870 | /* For logical operations, we want to ensure that the operands are |
| 871 | * scalar booleans. If it isn't, emit an error and return a constant |
| 872 | * boolean to avoid triggering cascading error messages. |
| 873 | */ |
| 874 | ir_rvalue * |
| 875 | get_scalar_boolean_operand(exec_list *instructions, |
| 876 | struct _mesa_glsl_parse_state *state, |
| 877 | ast_expression *parent_expr, |
| 878 | int operand, |
| 879 | const char *operand_name, |
| 880 | bool *error_emitted) |
| 881 | { |
| 882 | ast_expression *expr = parent_expr->subexpressions[operand]; |
| 883 | void *ctx = state; |
| 884 | ir_rvalue *val = expr->hir(instructions, state); |
| 885 | |
| 886 | if (val->type->is_boolean() && val->type->is_scalar()) |
| 887 | return val; |
| 888 | |
| 889 | if (!*error_emitted) { |
| 890 | YYLTYPE loc = expr->get_location(); |
| 891 | _mesa_glsl_error(&loc, state, "%s of `%s' must be scalar boolean", |
| 892 | operand_name, |
| 893 | parent_expr->operator_string(parent_expr->oper)); |
| 894 | *error_emitted = true; |
| 895 | } |
| 896 | |
| 897 | return new(ctx) ir_constant(true); |
| 898 | } |
| 899 | |
Paul Berry | 93b9758 | 2011-09-06 10:01:51 -0700 | [diff] [blame] | 900 | /** |
| 901 | * If name refers to a builtin array whose maximum allowed size is less than |
| 902 | * size, report an error and return true. Otherwise return false. |
| 903 | */ |
| 904 | static bool |
| 905 | check_builtin_array_max_size(const char *name, unsigned size, |
| 906 | YYLTYPE loc, struct _mesa_glsl_parse_state *state) |
| 907 | { |
| 908 | if ((strcmp("gl_TexCoord", name) == 0) |
| 909 | && (size > state->Const.MaxTextureCoords)) { |
| 910 | /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec: |
| 911 | * |
| 912 | * "The size [of gl_TexCoord] can be at most |
| 913 | * gl_MaxTextureCoords." |
| 914 | */ |
| 915 | _mesa_glsl_error(&loc, state, "`gl_TexCoord' array size cannot " |
| 916 | "be larger than gl_MaxTextureCoords (%u)\n", |
| 917 | state->Const.MaxTextureCoords); |
| 918 | return true; |
Paul Berry | 37bb1c4 | 2011-08-11 15:23:33 -0700 | [diff] [blame] | 919 | } else if (strcmp("gl_ClipDistance", name) == 0 |
| 920 | && size > state->Const.MaxClipPlanes) { |
| 921 | /* From section 7.1 (Vertex Shader Special Variables) of the |
| 922 | * GLSL 1.30 spec: |
| 923 | * |
| 924 | * "The gl_ClipDistance array is predeclared as unsized and |
| 925 | * must be sized by the shader either redeclaring it with a |
| 926 | * size or indexing it only with integral constant |
| 927 | * expressions. ... The size can be at most |
| 928 | * gl_MaxClipDistances." |
| 929 | */ |
| 930 | _mesa_glsl_error(&loc, state, "`gl_ClipDistance' array size cannot " |
| 931 | "be larger than gl_MaxClipDistances (%u)\n", |
| 932 | state->Const.MaxClipPlanes); |
| 933 | return true; |
Paul Berry | 93b9758 | 2011-09-06 10:01:51 -0700 | [diff] [blame] | 934 | } |
| 935 | return false; |
| 936 | } |
| 937 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 938 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 939 | ast_expression::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 940 | struct _mesa_glsl_parse_state *state) |
| 941 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 942 | void *ctx = state; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 943 | static const int operations[AST_NUM_OPERATORS] = { |
| 944 | -1, /* ast_assign doesn't convert to ir_expression. */ |
| 945 | -1, /* ast_plus doesn't convert to ir_expression. */ |
| 946 | ir_unop_neg, |
| 947 | ir_binop_add, |
| 948 | ir_binop_sub, |
| 949 | ir_binop_mul, |
| 950 | ir_binop_div, |
| 951 | ir_binop_mod, |
| 952 | ir_binop_lshift, |
| 953 | ir_binop_rshift, |
| 954 | ir_binop_less, |
| 955 | ir_binop_greater, |
| 956 | ir_binop_lequal, |
| 957 | ir_binop_gequal, |
Luca Barbieri | 4dfb899 | 2010-09-08 01:31:39 +0200 | [diff] [blame] | 958 | ir_binop_all_equal, |
| 959 | ir_binop_any_nequal, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 960 | ir_binop_bit_and, |
| 961 | ir_binop_bit_xor, |
| 962 | ir_binop_bit_or, |
| 963 | ir_unop_bit_not, |
| 964 | ir_binop_logic_and, |
| 965 | ir_binop_logic_xor, |
| 966 | ir_binop_logic_or, |
| 967 | ir_unop_logic_not, |
| 968 | |
| 969 | /* Note: The following block of expression types actually convert |
| 970 | * to multiple IR instructions. |
| 971 | */ |
| 972 | ir_binop_mul, /* ast_mul_assign */ |
| 973 | ir_binop_div, /* ast_div_assign */ |
| 974 | ir_binop_mod, /* ast_mod_assign */ |
| 975 | ir_binop_add, /* ast_add_assign */ |
| 976 | ir_binop_sub, /* ast_sub_assign */ |
| 977 | ir_binop_lshift, /* ast_ls_assign */ |
| 978 | ir_binop_rshift, /* ast_rs_assign */ |
| 979 | ir_binop_bit_and, /* ast_and_assign */ |
| 980 | ir_binop_bit_xor, /* ast_xor_assign */ |
| 981 | ir_binop_bit_or, /* ast_or_assign */ |
| 982 | |
| 983 | -1, /* ast_conditional doesn't convert to ir_expression. */ |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 984 | ir_binop_add, /* ast_pre_inc. */ |
| 985 | ir_binop_sub, /* ast_pre_dec. */ |
| 986 | ir_binop_add, /* ast_post_inc. */ |
| 987 | ir_binop_sub, /* ast_post_dec. */ |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 988 | -1, /* ast_field_selection doesn't conv to ir_expression. */ |
| 989 | -1, /* ast_array_index doesn't convert to ir_expression. */ |
| 990 | -1, /* ast_function_call doesn't conv to ir_expression. */ |
| 991 | -1, /* ast_identifier doesn't convert to ir_expression. */ |
| 992 | -1, /* ast_int_constant doesn't convert to ir_expression. */ |
| 993 | -1, /* ast_uint_constant doesn't conv to ir_expression. */ |
| 994 | -1, /* ast_float_constant doesn't conv to ir_expression. */ |
| 995 | -1, /* ast_bool_constant doesn't conv to ir_expression. */ |
| 996 | -1, /* ast_sequence doesn't convert to ir_expression. */ |
| 997 | }; |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 998 | ir_rvalue *result = NULL; |
Aras Pranckevicius | 1c325af | 2010-07-29 15:35:22 +0300 | [diff] [blame] | 999 | ir_rvalue *op[3]; |
Kenneth Graunke | 08ba977 | 2011-04-14 17:21:59 -0700 | [diff] [blame] | 1000 | const struct glsl_type *type; /* a temporary variable for switch cases */ |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1001 | bool error_emitted = false; |
| 1002 | YYLTYPE loc; |
| 1003 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1004 | loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1005 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1006 | switch (this->oper) { |
Ian Romanick | 6652af3 | 2010-03-09 16:38:02 -0800 | [diff] [blame] | 1007 | case ast_assign: { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1008 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1009 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1010 | |
Ian Romanick | 85caea2 | 2011-03-15 16:33:27 -0700 | [diff] [blame] | 1011 | result = do_assignment(instructions, state, op[0], op[1], false, |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 1012 | this->subexpressions[0]->get_location()); |
| 1013 | error_emitted = result->type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1014 | break; |
Ian Romanick | 6652af3 | 2010-03-09 16:38:02 -0800 | [diff] [blame] | 1015 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1016 | |
| 1017 | case ast_plus: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1018 | op[0] = this->subexpressions[0]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1019 | |
Carl Worth | c24bcad | 2010-07-21 11:23:51 -0700 | [diff] [blame] | 1020 | type = unary_arithmetic_result_type(op[0]->type, state, & loc); |
| 1021 | |
| 1022 | error_emitted = type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1023 | |
| 1024 | result = op[0]; |
| 1025 | break; |
| 1026 | |
| 1027 | case ast_neg: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1028 | op[0] = this->subexpressions[0]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1029 | |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 1030 | type = unary_arithmetic_result_type(op[0]->type, state, & loc); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1031 | |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 1032 | error_emitted = type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1033 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1034 | result = new(ctx) ir_expression(operations[this->oper], type, |
| 1035 | op[0], NULL); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1036 | break; |
| 1037 | |
| 1038 | case ast_add: |
| 1039 | case ast_sub: |
| 1040 | case ast_mul: |
| 1041 | case ast_div: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1042 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1043 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1044 | |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 1045 | type = arithmetic_result_type(op[0], op[1], |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1046 | (this->oper == ast_mul), |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 1047 | state, & loc); |
| 1048 | error_emitted = type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1049 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1050 | result = new(ctx) ir_expression(operations[this->oper], type, |
| 1051 | op[0], op[1]); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1052 | break; |
| 1053 | |
| 1054 | case ast_mod: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1055 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1056 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1057 | |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 1058 | type = modulus_result_type(op[0]->type, op[1]->type, state, & loc); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1059 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1060 | assert(operations[this->oper] == ir_binop_mod); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1061 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1062 | result = new(ctx) ir_expression(operations[this->oper], type, |
| 1063 | op[0], op[1]); |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 1064 | error_emitted = type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1065 | break; |
| 1066 | |
| 1067 | case ast_lshift: |
| 1068 | case ast_rshift: |
Chad Versace | 5c4c36f | 2010-10-08 16:22:28 -0700 | [diff] [blame] | 1069 | if (state->language_version < 130) { |
| 1070 | _mesa_glsl_error(&loc, state, "operator %s requires GLSL 1.30", |
| 1071 | operator_string(this->oper)); |
| 1072 | error_emitted = true; |
Chad Versace | 5c4c36f | 2010-10-08 16:22:28 -0700 | [diff] [blame] | 1073 | } |
| 1074 | |
Chad Versace | 5c4c36f | 2010-10-08 16:22:28 -0700 | [diff] [blame] | 1075 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1076 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Chad Versace | c0197ab | 2010-10-15 09:49:46 -0700 | [diff] [blame] | 1077 | type = shift_result_type(op[0]->type, op[1]->type, this->oper, state, |
| 1078 | &loc); |
Chad Versace | 5c4c36f | 2010-10-08 16:22:28 -0700 | [diff] [blame] | 1079 | result = new(ctx) ir_expression(operations[this->oper], type, |
| 1080 | op[0], op[1]); |
| 1081 | error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); |
| 1082 | break; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1083 | |
| 1084 | case ast_less: |
| 1085 | case ast_greater: |
| 1086 | case ast_lequal: |
| 1087 | case ast_gequal: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1088 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1089 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1090 | |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 1091 | type = relational_result_type(op[0], op[1], state, & loc); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1092 | |
| 1093 | /* The relational operators must either generate an error or result |
| 1094 | * in a scalar boolean. See page 57 of the GLSL 1.50 spec. |
| 1095 | */ |
Ian Romanick | a43817a | 2010-03-26 14:27:23 -0700 | [diff] [blame] | 1096 | assert(type->is_error() |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1097 | || ((type->base_type == GLSL_TYPE_BOOL) |
Ian Romanick | cb36f8a | 2010-03-09 15:51:22 -0800 | [diff] [blame] | 1098 | && type->is_scalar())); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1099 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1100 | result = new(ctx) ir_expression(operations[this->oper], type, |
| 1101 | op[0], op[1]); |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 1102 | error_emitted = type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1103 | break; |
| 1104 | |
| 1105 | case ast_nequal: |
| 1106 | case ast_equal: |
Ian Romanick | 6e659ca | 2010-03-29 15:11:05 -0700 | [diff] [blame] | 1107 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1108 | op[1] = this->subexpressions[1]->hir(instructions, state); |
| 1109 | |
| 1110 | /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec: |
| 1111 | * |
| 1112 | * "The equality operators equal (==), and not equal (!=) |
| 1113 | * operate on all types. They result in a scalar Boolean. If |
| 1114 | * the operand types do not match, then there must be a |
| 1115 | * conversion from Section 4.1.10 "Implicit Conversions" |
| 1116 | * applied to one operand that can make them match, in which |
| 1117 | * case this conversion is done." |
| 1118 | */ |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 1119 | if ((!apply_implicit_conversion(op[0]->type, op[1], state) |
| 1120 | && !apply_implicit_conversion(op[1]->type, op[0], state)) |
Ian Romanick | 212b032 | 2010-03-29 16:22:38 -0700 | [diff] [blame] | 1121 | || (op[0]->type != op[1]->type)) { |
Ian Romanick | 6e659ca | 2010-03-29 15:11:05 -0700 | [diff] [blame] | 1122 | _mesa_glsl_error(& loc, state, "operands of `%s' must have the same " |
| 1123 | "type", (this->oper == ast_equal) ? "==" : "!="); |
| 1124 | error_emitted = true; |
Ian Romanick | a80cbd6 | 2010-03-30 17:04:48 -0700 | [diff] [blame] | 1125 | } else if ((state->language_version <= 110) |
| 1126 | && (op[0]->type->is_array() || op[1]->type->is_array())) { |
| 1127 | _mesa_glsl_error(& loc, state, "array comparisons forbidden in " |
| 1128 | "GLSL 1.10"); |
| 1129 | error_emitted = true; |
Ian Romanick | 6e659ca | 2010-03-29 15:11:05 -0700 | [diff] [blame] | 1130 | } |
| 1131 | |
Eric Anholt | 175829f | 2011-04-09 12:54:34 -1000 | [diff] [blame] | 1132 | if (error_emitted) { |
| 1133 | result = new(ctx) ir_constant(false); |
| 1134 | } else { |
| 1135 | result = do_comparison(ctx, operations[this->oper], op[0], op[1]); |
| 1136 | assert(result->type == glsl_type::bool_type); |
Eric Anholt | 175829f | 2011-04-09 12:54:34 -1000 | [diff] [blame] | 1137 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1138 | break; |
| 1139 | |
| 1140 | case ast_bit_and: |
| 1141 | case ast_bit_xor: |
| 1142 | case ast_bit_or: |
Kenneth Graunke | 1eea963 | 2010-08-31 10:56:24 -0700 | [diff] [blame] | 1143 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1144 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Chad Versace | cfdbf8b | 2010-10-15 11:28:05 -0700 | [diff] [blame] | 1145 | type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper, |
| 1146 | state, &loc); |
Kenneth Graunke | 1eea963 | 2010-08-31 10:56:24 -0700 | [diff] [blame] | 1147 | result = new(ctx) ir_expression(operations[this->oper], type, |
| 1148 | op[0], op[1]); |
| 1149 | error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); |
| 1150 | break; |
| 1151 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1152 | case ast_bit_not: |
Kenneth Graunke | 1eea963 | 2010-08-31 10:56:24 -0700 | [diff] [blame] | 1153 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1154 | |
| 1155 | if (state->language_version < 130) { |
| 1156 | _mesa_glsl_error(&loc, state, "bit-wise operations require GLSL 1.30"); |
| 1157 | error_emitted = true; |
| 1158 | } |
| 1159 | |
| 1160 | if (!op[0]->type->is_integer()) { |
| 1161 | _mesa_glsl_error(&loc, state, "operand of `~' must be an integer"); |
| 1162 | error_emitted = true; |
| 1163 | } |
| 1164 | |
| 1165 | type = op[0]->type; |
| 1166 | 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] | 1167 | break; |
| 1168 | |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 1169 | case ast_logic_and: { |
Eric Anholt | 7ec0c97 | 2011-04-09 10:27:02 -1000 | [diff] [blame] | 1170 | exec_list rhs_instructions; |
Eric Anholt | 0182270 | 2011-04-09 15:59:20 -1000 | [diff] [blame] | 1171 | op[0] = get_scalar_boolean_operand(instructions, state, this, 0, |
| 1172 | "LHS", &error_emitted); |
Eric Anholt | 7ec0c97 | 2011-04-09 10:27:02 -1000 | [diff] [blame] | 1173 | op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1, |
| 1174 | "RHS", &error_emitted); |
Eric Anholt | b82c0c3 | 2010-03-31 09:11:39 -1000 | [diff] [blame] | 1175 | |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1176 | ir_constant *op0_const = op[0]->constant_expression_value(); |
| 1177 | if (op0_const) { |
| 1178 | if (op0_const->value.b[0]) { |
Eric Anholt | 7ec0c97 | 2011-04-09 10:27:02 -1000 | [diff] [blame] | 1179 | instructions->append_list(&rhs_instructions); |
| 1180 | result = op[1]; |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1181 | } else { |
| 1182 | result = op0_const; |
| 1183 | } |
| 1184 | type = glsl_type::bool_type; |
| 1185 | } else { |
Ian Romanick | 81d664f | 2010-07-12 15:18:55 -0700 | [diff] [blame] | 1186 | ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type, |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 1187 | "and_tmp", |
| 1188 | ir_var_temporary); |
Ian Romanick | 81d664f | 2010-07-12 15:18:55 -0700 | [diff] [blame] | 1189 | instructions->push_tail(tmp); |
| 1190 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1191 | ir_if *const stmt = new(ctx) ir_if(op[0]); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1192 | instructions->push_tail(stmt); |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 1193 | |
Eric Anholt | 7ec0c97 | 2011-04-09 10:27:02 -1000 | [diff] [blame] | 1194 | stmt->then_instructions.append_list(&rhs_instructions); |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1195 | ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1196 | ir_assignment *const then_assign = |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1197 | new(ctx) ir_assignment(then_deref, op[1], NULL); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1198 | stmt->then_instructions.push_tail(then_assign); |
| 1199 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1200 | ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1201 | ir_assignment *const else_assign = |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1202 | new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false), NULL); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1203 | stmt->else_instructions.push_tail(else_assign); |
| 1204 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1205 | result = new(ctx) ir_dereference_variable(tmp); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1206 | type = tmp->type; |
Eric Anholt | b82c0c3 | 2010-03-31 09:11:39 -1000 | [diff] [blame] | 1207 | } |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 1208 | break; |
| 1209 | } |
| 1210 | |
| 1211 | case ast_logic_or: { |
Eric Anholt | 9e04b19 | 2011-04-09 10:27:02 -1000 | [diff] [blame] | 1212 | exec_list rhs_instructions; |
Eric Anholt | 0182270 | 2011-04-09 15:59:20 -1000 | [diff] [blame] | 1213 | op[0] = get_scalar_boolean_operand(instructions, state, this, 0, |
| 1214 | "LHS", &error_emitted); |
Eric Anholt | 9e04b19 | 2011-04-09 10:27:02 -1000 | [diff] [blame] | 1215 | op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1, |
| 1216 | "RHS", &error_emitted); |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 1217 | |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1218 | ir_constant *op0_const = op[0]->constant_expression_value(); |
| 1219 | if (op0_const) { |
| 1220 | if (op0_const->value.b[0]) { |
| 1221 | result = op0_const; |
| 1222 | } else { |
Eric Anholt | 9e04b19 | 2011-04-09 10:27:02 -1000 | [diff] [blame] | 1223 | result = op[1]; |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1224 | } |
| 1225 | type = glsl_type::bool_type; |
| 1226 | } else { |
Kenneth Graunke | dfd30ca | 2010-07-08 12:40:52 -0700 | [diff] [blame] | 1227 | ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type, |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 1228 | "or_tmp", |
| 1229 | ir_var_temporary); |
Ian Romanick | 0b9ae3b | 2010-07-12 14:22:05 -0700 | [diff] [blame] | 1230 | instructions->push_tail(tmp); |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 1231 | |
Ian Romanick | 81d664f | 2010-07-12 15:18:55 -0700 | [diff] [blame] | 1232 | ir_if *const stmt = new(ctx) ir_if(op[0]); |
| 1233 | instructions->push_tail(stmt); |
| 1234 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1235 | ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1236 | ir_assignment *const then_assign = |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1237 | new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true), NULL); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1238 | stmt->then_instructions.push_tail(then_assign); |
| 1239 | |
Eric Anholt | 9e04b19 | 2011-04-09 10:27:02 -1000 | [diff] [blame] | 1240 | stmt->else_instructions.append_list(&rhs_instructions); |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1241 | ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1242 | ir_assignment *const else_assign = |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1243 | new(ctx) ir_assignment(else_deref, op[1], NULL); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1244 | stmt->else_instructions.push_tail(else_assign); |
| 1245 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1246 | result = new(ctx) ir_dereference_variable(tmp); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 1247 | type = tmp->type; |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 1248 | } |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 1249 | break; |
| 1250 | } |
| 1251 | |
| 1252 | case ast_logic_xor: |
Eric Anholt | 756c262 | 2011-04-09 14:57:17 -1000 | [diff] [blame] | 1253 | /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec: |
| 1254 | * |
| 1255 | * "The logical binary operators and (&&), or ( | | ), and |
| 1256 | * exclusive or (^^). They operate only on two Boolean |
| 1257 | * expressions and result in a Boolean expression." |
| 1258 | */ |
| 1259 | op[0] = get_scalar_boolean_operand(instructions, state, this, 0, "LHS", |
| 1260 | &error_emitted); |
| 1261 | op[1] = get_scalar_boolean_operand(instructions, state, this, 1, "RHS", |
| 1262 | &error_emitted); |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 1263 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1264 | result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type, |
| 1265 | op[0], op[1]); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1266 | break; |
| 1267 | |
Eric Anholt | a5827fe | 2010-03-31 16:50:55 -1000 | [diff] [blame] | 1268 | case ast_logic_not: |
Eric Anholt | 0182270 | 2011-04-09 15:59:20 -1000 | [diff] [blame] | 1269 | op[0] = get_scalar_boolean_operand(instructions, state, this, 0, |
| 1270 | "operand", &error_emitted); |
Eric Anholt | a5827fe | 2010-03-31 16:50:55 -1000 | [diff] [blame] | 1271 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1272 | result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type, |
| 1273 | op[0], NULL); |
Eric Anholt | a5827fe | 2010-03-31 16:50:55 -1000 | [diff] [blame] | 1274 | break; |
| 1275 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1276 | case ast_mul_assign: |
| 1277 | case ast_div_assign: |
| 1278 | case ast_add_assign: |
| 1279 | case ast_sub_assign: { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1280 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1281 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1282 | |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 1283 | type = arithmetic_result_type(op[0], op[1], |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1284 | (this->oper == ast_mul_assign), |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 1285 | state, & loc); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1286 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1287 | ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type, |
| 1288 | op[0], op[1]); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1289 | |
Eric Anholt | 3e24ef6 | 2010-06-23 12:40:17 -0700 | [diff] [blame] | 1290 | result = do_assignment(instructions, state, |
Ian Romanick | 85caea2 | 2011-03-15 16:33:27 -0700 | [diff] [blame] | 1291 | op[0]->clone(ctx, NULL), temp_rhs, false, |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 1292 | this->subexpressions[0]->get_location()); |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 1293 | error_emitted = (op[0]->type->is_error()); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1294 | |
| 1295 | /* GLSL 1.10 does not allow array assignment. However, we don't have to |
| 1296 | * explicitly test for this because none of the binary expression |
| 1297 | * operators allow array operands either. |
| 1298 | */ |
| 1299 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1300 | break; |
| 1301 | } |
| 1302 | |
Eric Anholt | 48a0e64 | 2010-03-26 11:57:46 -0700 | [diff] [blame] | 1303 | case ast_mod_assign: { |
| 1304 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1305 | op[1] = this->subexpressions[1]->hir(instructions, state); |
| 1306 | |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 1307 | type = modulus_result_type(op[0]->type, op[1]->type, state, & loc); |
Eric Anholt | 48a0e64 | 2010-03-26 11:57:46 -0700 | [diff] [blame] | 1308 | |
| 1309 | assert(operations[this->oper] == ir_binop_mod); |
| 1310 | |
Ian Romanick | 768b55a | 2010-08-13 16:46:43 -0700 | [diff] [blame] | 1311 | ir_rvalue *temp_rhs; |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1312 | temp_rhs = new(ctx) ir_expression(operations[this->oper], type, |
| 1313 | op[0], op[1]); |
Eric Anholt | 48a0e64 | 2010-03-26 11:57:46 -0700 | [diff] [blame] | 1314 | |
Eric Anholt | 3e24ef6 | 2010-06-23 12:40:17 -0700 | [diff] [blame] | 1315 | result = do_assignment(instructions, state, |
Ian Romanick | 85caea2 | 2011-03-15 16:33:27 -0700 | [diff] [blame] | 1316 | op[0]->clone(ctx, NULL), temp_rhs, false, |
Eric Anholt | 48a0e64 | 2010-03-26 11:57:46 -0700 | [diff] [blame] | 1317 | this->subexpressions[0]->get_location()); |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 1318 | error_emitted = type->is_error(); |
Eric Anholt | 48a0e64 | 2010-03-26 11:57:46 -0700 | [diff] [blame] | 1319 | break; |
| 1320 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1321 | |
| 1322 | case ast_ls_assign: |
Chad Versace | 338ed6e | 2010-10-15 10:05:50 -0700 | [diff] [blame] | 1323 | case ast_rs_assign: { |
| 1324 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1325 | op[1] = this->subexpressions[1]->hir(instructions, state); |
| 1326 | type = shift_result_type(op[0]->type, op[1]->type, this->oper, state, |
| 1327 | &loc); |
| 1328 | ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], |
| 1329 | type, op[0], op[1]); |
| 1330 | result = do_assignment(instructions, state, op[0]->clone(ctx, NULL), |
Ian Romanick | 85caea2 | 2011-03-15 16:33:27 -0700 | [diff] [blame] | 1331 | temp_rhs, false, |
Chad Versace | 338ed6e | 2010-10-15 10:05:50 -0700 | [diff] [blame] | 1332 | this->subexpressions[0]->get_location()); |
| 1333 | error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); |
Ian Romanick | 251eb75 | 2010-03-29 14:15:05 -0700 | [diff] [blame] | 1334 | break; |
Chad Versace | 338ed6e | 2010-10-15 10:05:50 -0700 | [diff] [blame] | 1335 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1336 | |
| 1337 | case ast_and_assign: |
| 1338 | case ast_xor_assign: |
Chad Versace | d03ac0f | 2010-10-15 12:08:28 -0700 | [diff] [blame] | 1339 | case ast_or_assign: { |
| 1340 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1341 | op[1] = this->subexpressions[1]->hir(instructions, state); |
| 1342 | type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper, |
| 1343 | state, &loc); |
| 1344 | ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], |
| 1345 | type, op[0], op[1]); |
| 1346 | result = do_assignment(instructions, state, op[0]->clone(ctx, NULL), |
Ian Romanick | 85caea2 | 2011-03-15 16:33:27 -0700 | [diff] [blame] | 1347 | temp_rhs, false, |
Chad Versace | d03ac0f | 2010-10-15 12:08:28 -0700 | [diff] [blame] | 1348 | this->subexpressions[0]->get_location()); |
| 1349 | error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); |
Ian Romanick | 251eb75 | 2010-03-29 14:15:05 -0700 | [diff] [blame] | 1350 | break; |
Chad Versace | d03ac0f | 2010-10-15 12:08:28 -0700 | [diff] [blame] | 1351 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1352 | |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 1353 | case ast_conditional: { |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 1354 | /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec: |
| 1355 | * |
| 1356 | * "The ternary selection operator (?:). It operates on three |
| 1357 | * expressions (exp1 ? exp2 : exp3). This operator evaluates the |
| 1358 | * first expression, which must result in a scalar Boolean." |
| 1359 | */ |
Eric Anholt | 0182270 | 2011-04-09 15:59:20 -1000 | [diff] [blame] | 1360 | op[0] = get_scalar_boolean_operand(instructions, state, this, 0, |
| 1361 | "condition", &error_emitted); |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 1362 | |
| 1363 | /* The :? operator is implemented by generating an anonymous temporary |
| 1364 | * followed by an if-statement. The last instruction in each branch of |
| 1365 | * the if-statement assigns a value to the anonymous temporary. This |
| 1366 | * temporary is the r-value of the expression. |
| 1367 | */ |
Ian Romanick | 0ad76c6 | 2010-06-11 12:56:26 -0700 | [diff] [blame] | 1368 | exec_list then_instructions; |
| 1369 | exec_list else_instructions; |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 1370 | |
Ian Romanick | 0ad76c6 | 2010-06-11 12:56:26 -0700 | [diff] [blame] | 1371 | op[1] = this->subexpressions[1]->hir(&then_instructions, state); |
| 1372 | op[2] = this->subexpressions[2]->hir(&else_instructions, state); |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 1373 | |
| 1374 | /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec: |
| 1375 | * |
| 1376 | * "The second and third expressions can be any type, as |
| 1377 | * long their types match, or there is a conversion in |
| 1378 | * Section 4.1.10 "Implicit Conversions" that can be applied |
| 1379 | * to one of the expressions to make their types match. This |
| 1380 | * resulting matching type is the type of the entire |
| 1381 | * expression." |
| 1382 | */ |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 1383 | if ((!apply_implicit_conversion(op[1]->type, op[2], state) |
| 1384 | && !apply_implicit_conversion(op[2]->type, op[1], state)) |
Ian Romanick | db9be2e | 2010-03-29 16:25:56 -0700 | [diff] [blame] | 1385 | || (op[1]->type != op[2]->type)) { |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 1386 | YYLTYPE loc = this->subexpressions[1]->get_location(); |
| 1387 | |
| 1388 | _mesa_glsl_error(& loc, state, "Second and third operands of ?: " |
| 1389 | "operator must have matching types."); |
| 1390 | error_emitted = true; |
Ian Romanick | 0ad76c6 | 2010-06-11 12:56:26 -0700 | [diff] [blame] | 1391 | type = glsl_type::error_type; |
Ian Romanick | db9be2e | 2010-03-29 16:25:56 -0700 | [diff] [blame] | 1392 | } else { |
Ian Romanick | 0ad76c6 | 2010-06-11 12:56:26 -0700 | [diff] [blame] | 1393 | type = op[1]->type; |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 1394 | } |
| 1395 | |
Ian Romanick | f09fabc | 2010-09-07 14:30:06 -0700 | [diff] [blame] | 1396 | /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec: |
| 1397 | * |
| 1398 | * "The second and third expressions must be the same type, but can |
| 1399 | * be of any type other than an array." |
| 1400 | */ |
| 1401 | if ((state->language_version <= 110) && type->is_array()) { |
| 1402 | _mesa_glsl_error(& loc, state, "Second and third operands of ?: " |
| 1403 | "operator must not be arrays."); |
| 1404 | error_emitted = true; |
| 1405 | } |
| 1406 | |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1407 | ir_constant *cond_val = op[0]->constant_expression_value(); |
| 1408 | ir_constant *then_val = op[1]->constant_expression_value(); |
| 1409 | ir_constant *else_val = op[2]->constant_expression_value(); |
Ian Romanick | 0ad76c6 | 2010-06-11 12:56:26 -0700 | [diff] [blame] | 1410 | |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1411 | if (then_instructions.is_empty() |
| 1412 | && else_instructions.is_empty() |
| 1413 | && (cond_val != NULL) && (then_val != NULL) && (else_val != NULL)) { |
| 1414 | result = (cond_val->value.b[0]) ? then_val : else_val; |
| 1415 | } else { |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 1416 | ir_variable *const tmp = |
| 1417 | new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary); |
Ian Romanick | 0b9ae3b | 2010-07-12 14:22:05 -0700 | [diff] [blame] | 1418 | instructions->push_tail(tmp); |
Ian Romanick | 0ad76c6 | 2010-06-11 12:56:26 -0700 | [diff] [blame] | 1419 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1420 | ir_if *const stmt = new(ctx) ir_if(op[0]); |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1421 | instructions->push_tail(stmt); |
Ian Romanick | 0ad76c6 | 2010-06-11 12:56:26 -0700 | [diff] [blame] | 1422 | |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1423 | then_instructions.move_nodes_to(& stmt->then_instructions); |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1424 | ir_dereference *const then_deref = |
| 1425 | new(ctx) ir_dereference_variable(tmp); |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1426 | ir_assignment *const then_assign = |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1427 | new(ctx) ir_assignment(then_deref, op[1], NULL); |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1428 | stmt->then_instructions.push_tail(then_assign); |
Ian Romanick | 0ad76c6 | 2010-06-11 12:56:26 -0700 | [diff] [blame] | 1429 | |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1430 | else_instructions.move_nodes_to(& stmt->else_instructions); |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1431 | ir_dereference *const else_deref = |
| 1432 | new(ctx) ir_dereference_variable(tmp); |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1433 | ir_assignment *const else_assign = |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1434 | new(ctx) ir_assignment(else_deref, op[2], NULL); |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1435 | stmt->else_instructions.push_tail(else_assign); |
| 1436 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1437 | result = new(ctx) ir_dereference_variable(tmp); |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1438 | } |
Ian Romanick | 251eb75 | 2010-03-29 14:15:05 -0700 | [diff] [blame] | 1439 | break; |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 1440 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1441 | |
| 1442 | case ast_pre_inc: |
Eric Anholt | 76ea56c | 2010-03-26 12:16:54 -0700 | [diff] [blame] | 1443 | case ast_pre_dec: { |
| 1444 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1445 | if (op[0]->type->base_type == GLSL_TYPE_FLOAT) |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1446 | op[1] = new(ctx) ir_constant(1.0f); |
Eric Anholt | 76ea56c | 2010-03-26 12:16:54 -0700 | [diff] [blame] | 1447 | else |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1448 | op[1] = new(ctx) ir_constant(1); |
Eric Anholt | 76ea56c | 2010-03-26 12:16:54 -0700 | [diff] [blame] | 1449 | |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 1450 | type = arithmetic_result_type(op[0], op[1], false, state, & loc); |
Eric Anholt | 76ea56c | 2010-03-26 12:16:54 -0700 | [diff] [blame] | 1451 | |
Ian Romanick | 768b55a | 2010-08-13 16:46:43 -0700 | [diff] [blame] | 1452 | ir_rvalue *temp_rhs; |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1453 | temp_rhs = new(ctx) ir_expression(operations[this->oper], type, |
| 1454 | op[0], op[1]); |
Eric Anholt | 76ea56c | 2010-03-26 12:16:54 -0700 | [diff] [blame] | 1455 | |
Eric Anholt | 3e24ef6 | 2010-06-23 12:40:17 -0700 | [diff] [blame] | 1456 | result = do_assignment(instructions, state, |
Ian Romanick | 85caea2 | 2011-03-15 16:33:27 -0700 | [diff] [blame] | 1457 | op[0]->clone(ctx, NULL), temp_rhs, false, |
Eric Anholt | 76ea56c | 2010-03-26 12:16:54 -0700 | [diff] [blame] | 1458 | this->subexpressions[0]->get_location()); |
Eric Anholt | 76ea56c | 2010-03-26 12:16:54 -0700 | [diff] [blame] | 1459 | error_emitted = op[0]->type->is_error(); |
| 1460 | break; |
| 1461 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1462 | |
| 1463 | case ast_post_inc: |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1464 | case ast_post_dec: { |
| 1465 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1466 | if (op[0]->type->base_type == GLSL_TYPE_FLOAT) |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1467 | op[1] = new(ctx) ir_constant(1.0f); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1468 | else |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1469 | op[1] = new(ctx) ir_constant(1); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1470 | |
| 1471 | error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); |
| 1472 | |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 1473 | type = arithmetic_result_type(op[0], op[1], false, state, & loc); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1474 | |
Ian Romanick | 768b55a | 2010-08-13 16:46:43 -0700 | [diff] [blame] | 1475 | ir_rvalue *temp_rhs; |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1476 | temp_rhs = new(ctx) ir_expression(operations[this->oper], type, |
| 1477 | op[0], op[1]); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1478 | |
| 1479 | /* Get a temporary of a copy of the lvalue before it's modified. |
| 1480 | * This may get thrown away later. |
| 1481 | */ |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 1482 | result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL)); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1483 | |
Eric Anholt | 3e24ef6 | 2010-06-23 12:40:17 -0700 | [diff] [blame] | 1484 | (void)do_assignment(instructions, state, |
Ian Romanick | 85caea2 | 2011-03-15 16:33:27 -0700 | [diff] [blame] | 1485 | op[0]->clone(ctx, NULL), temp_rhs, false, |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1486 | this->subexpressions[0]->get_location()); |
| 1487 | |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1488 | error_emitted = op[0]->type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1489 | break; |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1490 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1491 | |
| 1492 | case ast_field_selection: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1493 | result = _mesa_ast_field_selection_to_hir(this, instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1494 | break; |
| 1495 | |
Ian Romanick | 27e3cf8 | 2010-04-01 18:03:59 -0700 | [diff] [blame] | 1496 | case ast_array_index: { |
| 1497 | YYLTYPE index_loc = subexpressions[1]->get_location(); |
| 1498 | |
| 1499 | op[0] = subexpressions[0]->hir(instructions, state); |
| 1500 | op[1] = subexpressions[1]->hir(instructions, state); |
| 1501 | |
| 1502 | error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); |
| 1503 | |
Ian Romanick | a9159f9 | 2010-05-26 15:08:11 -0700 | [diff] [blame] | 1504 | ir_rvalue *const array = op[0]; |
Ian Romanick | b8a21cc | 2010-04-01 18:31:11 -0700 | [diff] [blame] | 1505 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1506 | result = new(ctx) ir_dereference_array(op[0], op[1]); |
Ian Romanick | b8a21cc | 2010-04-01 18:31:11 -0700 | [diff] [blame] | 1507 | |
| 1508 | /* Do not use op[0] after this point. Use array. |
| 1509 | */ |
| 1510 | op[0] = NULL; |
| 1511 | |
Ian Romanick | 27e3cf8 | 2010-04-01 18:03:59 -0700 | [diff] [blame] | 1512 | |
| 1513 | if (error_emitted) |
| 1514 | break; |
| 1515 | |
Ian Romanick | 63038e1 | 2010-04-05 13:16:00 -0700 | [diff] [blame] | 1516 | if (!array->type->is_array() |
| 1517 | && !array->type->is_matrix() |
| 1518 | && !array->type->is_vector()) { |
Ian Romanick | 27e3cf8 | 2010-04-01 18:03:59 -0700 | [diff] [blame] | 1519 | _mesa_glsl_error(& index_loc, state, |
Ian Romanick | 63038e1 | 2010-04-05 13:16:00 -0700 | [diff] [blame] | 1520 | "cannot dereference non-array / non-matrix / " |
| 1521 | "non-vector"); |
Ian Romanick | 27e3cf8 | 2010-04-01 18:03:59 -0700 | [diff] [blame] | 1522 | error_emitted = true; |
| 1523 | } |
| 1524 | |
| 1525 | if (!op[1]->type->is_integer()) { |
| 1526 | _mesa_glsl_error(& index_loc, state, |
| 1527 | "array index must be integer type"); |
| 1528 | error_emitted = true; |
| 1529 | } else if (!op[1]->type->is_scalar()) { |
| 1530 | _mesa_glsl_error(& index_loc, state, |
| 1531 | "array index must be scalar"); |
| 1532 | error_emitted = true; |
| 1533 | } |
| 1534 | |
| 1535 | /* If the array index is a constant expression and the array has a |
| 1536 | * declared size, ensure that the access is in-bounds. If the array |
| 1537 | * index is not a constant expression, ensure that the array has a |
| 1538 | * declared size. |
| 1539 | */ |
| 1540 | ir_constant *const const_index = op[1]->constant_expression_value(); |
| 1541 | if (const_index != NULL) { |
| 1542 | const int idx = const_index->value.i[0]; |
Ian Romanick | 63038e1 | 2010-04-05 13:16:00 -0700 | [diff] [blame] | 1543 | const char *type_name; |
| 1544 | unsigned bound = 0; |
| 1545 | |
| 1546 | if (array->type->is_matrix()) { |
| 1547 | type_name = "matrix"; |
| 1548 | } else if (array->type->is_vector()) { |
| 1549 | type_name = "vector"; |
| 1550 | } else { |
| 1551 | type_name = "array"; |
| 1552 | } |
Ian Romanick | 27e3cf8 | 2010-04-01 18:03:59 -0700 | [diff] [blame] | 1553 | |
| 1554 | /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec: |
| 1555 | * |
| 1556 | * "It is illegal to declare an array with a size, and then |
| 1557 | * later (in the same shader) index the same array with an |
| 1558 | * integral constant expression greater than or equal to the |
| 1559 | * declared size. It is also illegal to index an array with a |
| 1560 | * negative constant expression." |
| 1561 | */ |
Ian Romanick | 63038e1 | 2010-04-05 13:16:00 -0700 | [diff] [blame] | 1562 | if (array->type->is_matrix()) { |
| 1563 | if (array->type->row_type()->vector_elements <= idx) { |
| 1564 | bound = array->type->row_type()->vector_elements; |
| 1565 | } |
| 1566 | } else if (array->type->is_vector()) { |
| 1567 | if (array->type->vector_elements <= idx) { |
| 1568 | bound = array->type->vector_elements; |
| 1569 | } |
| 1570 | } else { |
| 1571 | if ((array->type->array_size() > 0) |
| 1572 | && (array->type->array_size() <= idx)) { |
| 1573 | bound = array->type->array_size(); |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | if (bound > 0) { |
| 1578 | _mesa_glsl_error(& loc, state, "%s index must be < %u", |
| 1579 | type_name, bound); |
| 1580 | error_emitted = true; |
| 1581 | } else if (idx < 0) { |
| 1582 | _mesa_glsl_error(& loc, state, "%s index must be >= 0", |
| 1583 | type_name); |
Ian Romanick | 27e3cf8 | 2010-04-01 18:03:59 -0700 | [diff] [blame] | 1584 | error_emitted = true; |
| 1585 | } |
| 1586 | |
Ian Romanick | 63038e1 | 2010-04-05 13:16:00 -0700 | [diff] [blame] | 1587 | if (array->type->is_array()) { |
Ian Romanick | a9159f9 | 2010-05-26 15:08:11 -0700 | [diff] [blame] | 1588 | /* If the array is a variable dereference, it dereferences the |
| 1589 | * whole array, by definition. Use this to get the variable. |
| 1590 | * |
| 1591 | * FINISHME: Should some methods for getting / setting / testing |
| 1592 | * FINISHME: array access limits be added to ir_dereference? |
| 1593 | */ |
| 1594 | ir_variable *const v = array->whole_variable_referenced(); |
Paul Berry | 93b9758 | 2011-09-06 10:01:51 -0700 | [diff] [blame] | 1595 | if ((v != NULL) && (unsigned(idx) > v->max_array_access)) { |
Ian Romanick | 63038e1 | 2010-04-05 13:16:00 -0700 | [diff] [blame] | 1596 | v->max_array_access = idx; |
Paul Berry | 93b9758 | 2011-09-06 10:01:51 -0700 | [diff] [blame] | 1597 | |
| 1598 | /* Check whether this access will, as a side effect, implicitly |
| 1599 | * cause the size of a built-in array to be too large. |
| 1600 | */ |
| 1601 | if (check_builtin_array_max_size(v->name, idx+1, loc, state)) |
| 1602 | error_emitted = true; |
| 1603 | } |
Ian Romanick | 27e3cf8 | 2010-04-01 18:03:59 -0700 | [diff] [blame] | 1604 | } |
Kenneth Graunke | 2b7c42b | 2010-07-16 18:28:44 -0700 | [diff] [blame] | 1605 | } else if (array->type->array_size() == 0) { |
| 1606 | _mesa_glsl_error(&loc, state, "unsized array index must be constant"); |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1607 | } else { |
| 1608 | if (array->type->is_array()) { |
Aras Pranckevicius | 5226f8c | 2010-08-25 22:42:13 +0300 | [diff] [blame] | 1609 | /* whole_variable_referenced can return NULL if the array is a |
| 1610 | * member of a structure. In this case it is safe to not update |
| 1611 | * the max_array_access field because it is never used for fields |
| 1612 | * of structures. |
| 1613 | */ |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1614 | ir_variable *v = array->whole_variable_referenced(); |
Aras Pranckevicius | 5226f8c | 2010-08-25 22:42:13 +0300 | [diff] [blame] | 1615 | if (v != NULL) |
Ian Romanick | 0d9d036 | 2011-03-24 16:50:23 -0700 | [diff] [blame] | 1616 | v->max_array_access = array->type->array_size() - 1; |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1617 | } |
Ian Romanick | 27e3cf8 | 2010-04-01 18:03:59 -0700 | [diff] [blame] | 1618 | } |
| 1619 | |
Ian Romanick | e942f32 | 2011-01-04 16:09:00 -0800 | [diff] [blame] | 1620 | /* From page 23 (29 of the PDF) of the GLSL 1.30 spec: |
| 1621 | * |
Chad Versace | f0f2ec4 | 2010-12-07 10:35:36 -0800 | [diff] [blame] | 1622 | * "Samplers aggregated into arrays within a shader (using square |
| 1623 | * brackets [ ]) can only be indexed with integral constant |
| 1624 | * expressions [...]." |
Ian Romanick | e942f32 | 2011-01-04 16:09:00 -0800 | [diff] [blame] | 1625 | * |
| 1626 | * This restriction was added in GLSL 1.30. Shaders using earlier version |
| 1627 | * of the language should not be rejected by the compiler front-end for |
| 1628 | * using this construct. This allows useful things such as using a loop |
| 1629 | * counter as the index to an array of samplers. If the loop in unrolled, |
| 1630 | * the code should compile correctly. Instead, emit a warning. |
Chad Versace | f0f2ec4 | 2010-12-07 10:35:36 -0800 | [diff] [blame] | 1631 | */ |
| 1632 | if (array->type->is_array() && |
| 1633 | array->type->element_type()->is_sampler() && |
| 1634 | const_index == NULL) { |
| 1635 | |
Ian Romanick | e942f32 | 2011-01-04 16:09:00 -0800 | [diff] [blame] | 1636 | if (state->language_version == 100) { |
| 1637 | _mesa_glsl_warning(&loc, state, |
| 1638 | "sampler arrays indexed with non-constant " |
| 1639 | "expressions is optional in GLSL ES 1.00"); |
| 1640 | } else if (state->language_version < 130) { |
| 1641 | _mesa_glsl_warning(&loc, state, |
| 1642 | "sampler arrays indexed with non-constant " |
| 1643 | "expressions is forbidden in GLSL 1.30 and " |
| 1644 | "later"); |
| 1645 | } else { |
| 1646 | _mesa_glsl_error(&loc, state, |
| 1647 | "sampler arrays indexed with non-constant " |
| 1648 | "expressions is forbidden in GLSL 1.30 and " |
| 1649 | "later"); |
| 1650 | error_emitted = true; |
| 1651 | } |
Chad Versace | f0f2ec4 | 2010-12-07 10:35:36 -0800 | [diff] [blame] | 1652 | } |
| 1653 | |
Ian Romanick | 27e3cf8 | 2010-04-01 18:03:59 -0700 | [diff] [blame] | 1654 | if (error_emitted) |
| 1655 | result->type = glsl_type::error_type; |
| 1656 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1657 | break; |
Ian Romanick | 27e3cf8 | 2010-04-01 18:03:59 -0700 | [diff] [blame] | 1658 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1659 | |
| 1660 | case ast_function_call: |
Ian Romanick | 7cfddf1 | 2010-03-10 13:26:52 -0800 | [diff] [blame] | 1661 | /* Should *NEVER* get here. ast_function_call should always be handled |
| 1662 | * by ast_function_expression::hir. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1663 | */ |
Ian Romanick | 7cfddf1 | 2010-03-10 13:26:52 -0800 | [diff] [blame] | 1664 | assert(0); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1665 | break; |
| 1666 | |
| 1667 | case ast_identifier: { |
| 1668 | /* ast_identifier can appear several places in a full abstract syntax |
| 1669 | * tree. This particular use must be at location specified in the grammar |
| 1670 | * as 'variable_identifier'. |
| 1671 | */ |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 1672 | ir_variable *var = |
| 1673 | state->symbols->get_variable(this->primary_expression.identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1674 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1675 | result = new(ctx) ir_dereference_variable(var); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1676 | |
| 1677 | if (var != NULL) { |
Ian Romanick | bd33055 | 2011-01-07 18:34:58 -0800 | [diff] [blame] | 1678 | var->used = true; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1679 | } else { |
Ian Romanick | 71d0bbf | 2010-03-23 13:21:19 -0700 | [diff] [blame] | 1680 | _mesa_glsl_error(& loc, state, "`%s' undeclared", |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1681 | this->primary_expression.identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1682 | |
| 1683 | error_emitted = true; |
| 1684 | } |
| 1685 | break; |
| 1686 | } |
| 1687 | |
| 1688 | case ast_int_constant: |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1689 | result = new(ctx) ir_constant(this->primary_expression.int_constant); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1690 | break; |
| 1691 | |
| 1692 | case ast_uint_constant: |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1693 | result = new(ctx) ir_constant(this->primary_expression.uint_constant); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1694 | break; |
| 1695 | |
| 1696 | case ast_float_constant: |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1697 | result = new(ctx) ir_constant(this->primary_expression.float_constant); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1698 | break; |
| 1699 | |
| 1700 | case ast_bool_constant: |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1701 | result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant)); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1702 | break; |
| 1703 | |
| 1704 | case ast_sequence: { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1705 | /* It should not be possible to generate a sequence in the AST without |
| 1706 | * any expressions in it. |
| 1707 | */ |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 1708 | assert(!this->expressions.is_empty()); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1709 | |
| 1710 | /* The r-value of a sequence is the last expression in the sequence. If |
| 1711 | * the other expressions in the sequence do not have side-effects (and |
| 1712 | * therefore add instructions to the instruction list), they get dropped |
| 1713 | * on the floor. |
| 1714 | */ |
Ian Romanick | 3d5cfcf | 2011-04-11 10:10:30 -0700 | [diff] [blame] | 1715 | exec_node *previous_tail_pred = NULL; |
| 1716 | YYLTYPE previous_operand_loc = loc; |
| 1717 | |
| 1718 | foreach_list_typed (ast_node, ast, link, &this->expressions) { |
| 1719 | /* If one of the operands of comma operator does not generate any |
| 1720 | * code, we want to emit a warning. At each pass through the loop |
| 1721 | * previous_tail_pred will point to the last instruction in the |
| 1722 | * stream *before* processing the previous operand. Naturally, |
| 1723 | * instructions->tail_pred will point to the last instruction in the |
| 1724 | * stream *after* processing the previous operand. If the two |
| 1725 | * pointers match, then the previous operand had no effect. |
| 1726 | * |
| 1727 | * The warning behavior here differs slightly from GCC. GCC will |
| 1728 | * only emit a warning if none of the left-hand operands have an |
| 1729 | * effect. However, it will emit a warning for each. I believe that |
| 1730 | * there are some cases in C (especially with GCC extensions) where |
| 1731 | * it is useful to have an intermediate step in a sequence have no |
| 1732 | * effect, but I don't think these cases exist in GLSL. Either way, |
| 1733 | * it would be a giant hassle to replicate that behavior. |
| 1734 | */ |
| 1735 | if (previous_tail_pred == instructions->tail_pred) { |
| 1736 | _mesa_glsl_warning(&previous_operand_loc, state, |
| 1737 | "left-hand operand of comma expression has " |
| 1738 | "no effect"); |
| 1739 | } |
| 1740 | |
| 1741 | /* tail_pred is directly accessed instead of using the get_tail() |
| 1742 | * method for performance reasons. get_tail() has extra code to |
| 1743 | * return NULL when the list is empty. We don't care about that |
| 1744 | * here, so using tail_pred directly is fine. |
| 1745 | */ |
| 1746 | previous_tail_pred = instructions->tail_pred; |
| 1747 | previous_operand_loc = ast->get_location(); |
| 1748 | |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 1749 | result = ast->hir(instructions, state); |
Ian Romanick | 3d5cfcf | 2011-04-11 10:10:30 -0700 | [diff] [blame] | 1750 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1751 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1752 | /* Any errors should have already been emitted in the loop above. |
| 1753 | */ |
| 1754 | error_emitted = true; |
| 1755 | break; |
| 1756 | } |
| 1757 | } |
Kenneth Graunke | 08ba977 | 2011-04-14 17:21:59 -0700 | [diff] [blame] | 1758 | type = NULL; /* use result->type, not type. */ |
| 1759 | assert(result != NULL); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1760 | |
Kenneth Graunke | 08ba977 | 2011-04-14 17:21:59 -0700 | [diff] [blame] | 1761 | if (result->type->is_error() && !error_emitted) |
Ian Romanick | 71d0bbf | 2010-03-23 13:21:19 -0700 | [diff] [blame] | 1762 | _mesa_glsl_error(& loc, state, "type mismatch"); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1763 | |
| 1764 | return result; |
| 1765 | } |
| 1766 | |
| 1767 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 1768 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1769 | ast_expression_statement::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1770 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1771 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1772 | /* It is possible to have expression statements that don't have an |
| 1773 | * expression. This is the solitary semicolon: |
| 1774 | * |
| 1775 | * for (i = 0; i < 5; i++) |
| 1776 | * ; |
| 1777 | * |
| 1778 | * In this case the expression will be NULL. Test for NULL and don't do |
| 1779 | * anything in that case. |
| 1780 | */ |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1781 | if (expression != NULL) |
| 1782 | expression->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1783 | |
| 1784 | /* Statements do not have r-values. |
| 1785 | */ |
| 1786 | return NULL; |
| 1787 | } |
| 1788 | |
| 1789 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 1790 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1791 | ast_compound_statement::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1792 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1793 | { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1794 | if (new_scope) |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 1795 | state->symbols->push_scope(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1796 | |
Ian Romanick | 2b97dc6 | 2010-05-10 17:42:05 -0700 | [diff] [blame] | 1797 | foreach_list_typed (ast_node, ast, link, &this->statements) |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 1798 | ast->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1799 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1800 | if (new_scope) |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 1801 | state->symbols->pop_scope(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1802 | |
| 1803 | /* Compound statements do not have r-values. |
| 1804 | */ |
| 1805 | return NULL; |
| 1806 | } |
| 1807 | |
| 1808 | |
Ian Romanick | 28009cd | 2010-03-30 16:59:27 -0700 | [diff] [blame] | 1809 | static const glsl_type * |
Kenneth Graunke | d8e34e2 | 2010-08-07 02:56:01 -0700 | [diff] [blame] | 1810 | 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] | 1811 | struct _mesa_glsl_parse_state *state) |
| 1812 | { |
| 1813 | unsigned length = 0; |
| 1814 | |
Ian Romanick | a04211e | 2011-10-24 11:45:50 -0700 | [diff] [blame] | 1815 | /* From page 19 (page 25) of the GLSL 1.20 spec: |
| 1816 | * |
| 1817 | * "Only one-dimensional arrays may be declared." |
| 1818 | */ |
| 1819 | if (base->is_array()) { |
| 1820 | _mesa_glsl_error(loc, state, |
| 1821 | "invalid array of `%s' (only one-dimensional arrays " |
| 1822 | "may be declared)", |
| 1823 | base->name); |
| 1824 | return glsl_type::error_type; |
| 1825 | } |
Ian Romanick | 28009cd | 2010-03-30 16:59:27 -0700 | [diff] [blame] | 1826 | |
| 1827 | if (array_size != NULL) { |
| 1828 | exec_list dummy_instructions; |
| 1829 | ir_rvalue *const ir = array_size->hir(& dummy_instructions, state); |
| 1830 | YYLTYPE loc = array_size->get_location(); |
| 1831 | |
Ian Romanick | 28009cd | 2010-03-30 16:59:27 -0700 | [diff] [blame] | 1832 | if (ir != NULL) { |
| 1833 | if (!ir->type->is_integer()) { |
| 1834 | _mesa_glsl_error(& loc, state, "array size must be integer type"); |
| 1835 | } else if (!ir->type->is_scalar()) { |
| 1836 | _mesa_glsl_error(& loc, state, "array size must be scalar type"); |
| 1837 | } else { |
| 1838 | ir_constant *const size = ir->constant_expression_value(); |
| 1839 | |
| 1840 | if (size == NULL) { |
| 1841 | _mesa_glsl_error(& loc, state, "array size must be a " |
| 1842 | "constant valued expression"); |
| 1843 | } else if (size->value.i[0] <= 0) { |
| 1844 | _mesa_glsl_error(& loc, state, "array size must be > 0"); |
| 1845 | } else { |
| 1846 | assert(size->type == ir->type); |
| 1847 | length = size->value.u[0]; |
Paul Berry | d4144a1 | 2011-08-01 15:23:07 -0700 | [diff] [blame] | 1848 | |
| 1849 | /* If the array size is const (and we've verified that |
| 1850 | * it is) then no instructions should have been emitted |
| 1851 | * when we converted it to HIR. If they were emitted, |
| 1852 | * then either the array size isn't const after all, or |
| 1853 | * we are emitting unnecessary instructions. |
| 1854 | */ |
| 1855 | assert(dummy_instructions.is_empty()); |
Ian Romanick | 28009cd | 2010-03-30 16:59:27 -0700 | [diff] [blame] | 1856 | } |
| 1857 | } |
| 1858 | } |
Kenneth Graunke | d8e34e2 | 2010-08-07 02:56:01 -0700 | [diff] [blame] | 1859 | } else if (state->es_shader) { |
| 1860 | /* Section 10.17 of the GLSL ES 1.00 specification states that unsized |
| 1861 | * array declarations have been removed from the language. |
| 1862 | */ |
| 1863 | _mesa_glsl_error(loc, state, "unsized array declarations are not " |
| 1864 | "allowed in GLSL ES 1.00."); |
Ian Romanick | 28009cd | 2010-03-30 16:59:27 -0700 | [diff] [blame] | 1865 | } |
| 1866 | |
Ian Romanick | f38d15b | 2010-07-20 15:33:40 -0700 | [diff] [blame] | 1867 | return glsl_type::get_array_instance(base, length); |
Ian Romanick | 28009cd | 2010-03-30 16:59:27 -0700 | [diff] [blame] | 1868 | } |
| 1869 | |
| 1870 | |
Ian Romanick | d612a12 | 2010-03-31 16:22:06 -0700 | [diff] [blame] | 1871 | const glsl_type * |
| 1872 | ast_type_specifier::glsl_type(const char **name, |
| 1873 | struct _mesa_glsl_parse_state *state) const |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1874 | { |
Ian Romanick | d612a12 | 2010-03-31 16:22:06 -0700 | [diff] [blame] | 1875 | const struct glsl_type *type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1876 | |
Kenneth Graunke | ca92ae2 | 2010-09-18 11:11:09 +0200 | [diff] [blame] | 1877 | type = state->symbols->get_type(this->type_name); |
| 1878 | *name = this->type_name; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1879 | |
Kenneth Graunke | ca92ae2 | 2010-09-18 11:11:09 +0200 | [diff] [blame] | 1880 | if (this->is_array) { |
| 1881 | YYLTYPE loc = this->get_location(); |
| 1882 | type = process_array_type(&loc, type, this->array_size, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1883 | } |
| 1884 | |
| 1885 | return type; |
| 1886 | } |
| 1887 | |
| 1888 | |
| 1889 | static void |
| 1890 | apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual, |
Ian Romanick | 768b55a | 2010-08-13 16:46:43 -0700 | [diff] [blame] | 1891 | ir_variable *var, |
Eric Anholt | 2e063f1 | 2010-03-28 00:56:22 -0700 | [diff] [blame] | 1892 | struct _mesa_glsl_parse_state *state, |
| 1893 | YYLTYPE *loc) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1894 | { |
Ian Romanick | bd33055 | 2011-01-07 18:34:58 -0800 | [diff] [blame] | 1895 | if (qual->flags.q.invariant) { |
| 1896 | if (var->used) { |
| 1897 | _mesa_glsl_error(loc, state, |
| 1898 | "variable `%s' may not be redeclared " |
| 1899 | "`invariant' after being used", |
| 1900 | var->name); |
| 1901 | } else { |
| 1902 | var->invariant = 1; |
| 1903 | } |
| 1904 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1905 | |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 1906 | if (qual->flags.q.constant || qual->flags.q.attribute |
| 1907 | || qual->flags.q.uniform |
| 1908 | || (qual->flags.q.varying && (state->target == fragment_shader))) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1909 | var->read_only = 1; |
| 1910 | |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 1911 | if (qual->flags.q.centroid) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1912 | var->centroid = 1; |
| 1913 | |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 1914 | if (qual->flags.q.attribute && state->target != vertex_shader) { |
Eric Anholt | 2e063f1 | 2010-03-28 00:56:22 -0700 | [diff] [blame] | 1915 | var->type = glsl_type::error_type; |
| 1916 | _mesa_glsl_error(loc, state, |
| 1917 | "`attribute' variables may not be declared in the " |
Ian Romanick | ae4c4c0 | 2010-04-07 16:41:40 -0700 | [diff] [blame] | 1918 | "%s shader", |
| 1919 | _mesa_glsl_shader_target_name(state->target)); |
Eric Anholt | 2e063f1 | 2010-03-28 00:56:22 -0700 | [diff] [blame] | 1920 | } |
| 1921 | |
Eric Anholt | 90b7825 | 2010-03-31 21:21:20 -1000 | [diff] [blame] | 1922 | /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec: |
| 1923 | * |
| 1924 | * "The varying qualifier can be used only with the data types |
| 1925 | * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of |
| 1926 | * these." |
| 1927 | */ |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 1928 | if (qual->flags.q.varying) { |
Eric Anholt | 0ca1719 | 2010-05-19 13:38:15 -0700 | [diff] [blame] | 1929 | const glsl_type *non_array_type; |
| 1930 | |
| 1931 | if (var->type && var->type->is_array()) |
| 1932 | non_array_type = var->type->fields.array; |
| 1933 | else |
| 1934 | non_array_type = var->type; |
| 1935 | |
| 1936 | if (non_array_type && non_array_type->base_type != GLSL_TYPE_FLOAT) { |
| 1937 | var->type = glsl_type::error_type; |
| 1938 | _mesa_glsl_error(loc, state, |
| 1939 | "varying variables must be of base type float"); |
| 1940 | } |
Eric Anholt | 90b7825 | 2010-03-31 21:21:20 -1000 | [diff] [blame] | 1941 | } |
| 1942 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 1943 | /* If there is no qualifier that changes the mode of the variable, leave |
| 1944 | * the setting alone. |
| 1945 | */ |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 1946 | if (qual->flags.q.in && qual->flags.q.out) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1947 | var->mode = ir_var_inout; |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 1948 | else if (qual->flags.q.attribute || qual->flags.q.in |
| 1949 | || (qual->flags.q.varying && (state->target == fragment_shader))) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1950 | var->mode = ir_var_in; |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 1951 | else if (qual->flags.q.out |
| 1952 | || (qual->flags.q.varying && (state->target == vertex_shader))) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1953 | var->mode = ir_var_out; |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 1954 | else if (qual->flags.q.uniform) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1955 | var->mode = ir_var_uniform; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1956 | |
Ian Romanick | 86b4398 | 2011-01-06 10:49:56 -0800 | [diff] [blame] | 1957 | if (state->all_invariant && (state->current_function == NULL)) { |
| 1958 | switch (state->target) { |
| 1959 | case vertex_shader: |
| 1960 | if (var->mode == ir_var_out) |
| 1961 | var->invariant = true; |
| 1962 | break; |
| 1963 | case geometry_shader: |
| 1964 | if ((var->mode == ir_var_in) || (var->mode == ir_var_out)) |
| 1965 | var->invariant = true; |
| 1966 | break; |
| 1967 | case fragment_shader: |
| 1968 | if (var->mode == ir_var_in) |
| 1969 | var->invariant = true; |
| 1970 | break; |
| 1971 | } |
| 1972 | } |
| 1973 | |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 1974 | if (qual->flags.q.flat) |
Paul Berry | cf45949 | 2011-10-25 18:06:37 -0700 | [diff] [blame] | 1975 | var->interpolation = INTERP_QUALIFIER_FLAT; |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 1976 | else if (qual->flags.q.noperspective) |
Paul Berry | cf45949 | 2011-10-25 18:06:37 -0700 | [diff] [blame] | 1977 | var->interpolation = INTERP_QUALIFIER_NOPERSPECTIVE; |
Paul Berry | c488150 | 2011-10-21 07:40:37 -0700 | [diff] [blame] | 1978 | else if (qual->flags.q.smooth) |
Paul Berry | cf45949 | 2011-10-25 18:06:37 -0700 | [diff] [blame] | 1979 | var->interpolation = INTERP_QUALIFIER_SMOOTH; |
Paul Berry | c488150 | 2011-10-21 07:40:37 -0700 | [diff] [blame] | 1980 | else |
| 1981 | var->interpolation = INTERP_QUALIFIER_NONE; |
Ian Romanick | 9d97537 | 2010-04-02 17:17:47 -0700 | [diff] [blame] | 1982 | |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 1983 | var->pixel_center_integer = qual->flags.q.pixel_center_integer; |
| 1984 | var->origin_upper_left = qual->flags.q.origin_upper_left; |
| 1985 | 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] | 1986 | && (strcmp(var->name, "gl_FragCoord") != 0)) { |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 1987 | const char *const qual_string = (qual->flags.q.origin_upper_left) |
Ian Romanick | 8d8469e | 2010-06-30 17:48:09 -0700 | [diff] [blame] | 1988 | ? "origin_upper_left" : "pixel_center_integer"; |
| 1989 | |
| 1990 | _mesa_glsl_error(loc, state, |
| 1991 | "layout qualifier `%s' can only be applied to " |
| 1992 | "fragment shader input `gl_FragCoord'", |
| 1993 | qual_string); |
| 1994 | } |
| 1995 | |
Ian Romanick | eee68d3 | 2010-10-07 15:13:38 -0700 | [diff] [blame] | 1996 | if (qual->flags.q.explicit_location) { |
| 1997 | const bool global_scope = (state->current_function == NULL); |
| 1998 | bool fail = false; |
| 1999 | const char *string = ""; |
| 2000 | |
| 2001 | /* In the vertex shader only shader inputs can be given explicit |
| 2002 | * locations. |
| 2003 | * |
| 2004 | * In the fragment shader only shader outputs can be given explicit |
| 2005 | * locations. |
| 2006 | */ |
| 2007 | switch (state->target) { |
| 2008 | case vertex_shader: |
| 2009 | if (!global_scope || (var->mode != ir_var_in)) { |
| 2010 | fail = true; |
| 2011 | string = "input"; |
| 2012 | } |
| 2013 | break; |
| 2014 | |
| 2015 | case geometry_shader: |
| 2016 | _mesa_glsl_error(loc, state, |
| 2017 | "geometry shader variables cannot be given " |
| 2018 | "explicit locations\n"); |
| 2019 | break; |
| 2020 | |
| 2021 | case fragment_shader: |
Paul Berry | b078aad | 2011-06-28 09:42:24 -0700 | [diff] [blame] | 2022 | if (!global_scope || (var->mode != ir_var_out)) { |
Ian Romanick | eee68d3 | 2010-10-07 15:13:38 -0700 | [diff] [blame] | 2023 | fail = true; |
| 2024 | string = "output"; |
| 2025 | } |
| 2026 | break; |
Kenneth Graunke | a75da2c | 2010-10-20 14:59:40 -0700 | [diff] [blame] | 2027 | }; |
Ian Romanick | eee68d3 | 2010-10-07 15:13:38 -0700 | [diff] [blame] | 2028 | |
| 2029 | if (fail) { |
| 2030 | _mesa_glsl_error(loc, state, |
| 2031 | "only %s shader %s variables can be given an " |
| 2032 | "explicit location\n", |
| 2033 | _mesa_glsl_shader_target_name(state->target), |
| 2034 | string); |
| 2035 | } else { |
| 2036 | var->explicit_location = true; |
Ian Romanick | 68a4fc9 | 2010-10-07 17:21:22 -0700 | [diff] [blame] | 2037 | |
| 2038 | /* This bit of silliness is needed because invalid explicit locations |
| 2039 | * are supposed to be flagged during linking. Small negative values |
| 2040 | * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias |
| 2041 | * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS). |
| 2042 | * The linker needs to be able to differentiate these cases. This |
| 2043 | * ensures that negative values stay negative. |
| 2044 | */ |
| 2045 | if (qual->location >= 0) { |
| 2046 | var->location = (state->target == vertex_shader) |
| 2047 | ? (qual->location + VERT_ATTRIB_GENERIC0) |
| 2048 | : (qual->location + FRAG_RESULT_DATA0); |
| 2049 | } else { |
| 2050 | var->location = qual->location; |
| 2051 | } |
Ian Romanick | eee68d3 | 2010-10-07 15:13:38 -0700 | [diff] [blame] | 2052 | } |
| 2053 | } |
| 2054 | |
Ian Romanick | 4bcff0c | 2011-01-07 16:53:59 -0800 | [diff] [blame] | 2055 | /* Does the declaration use the 'layout' keyword? |
| 2056 | */ |
| 2057 | const bool uses_layout = qual->flags.q.pixel_center_integer |
| 2058 | || qual->flags.q.origin_upper_left |
| 2059 | || qual->flags.q.explicit_location; |
| 2060 | |
| 2061 | /* Does the declaration use the deprecated 'attribute' or 'varying' |
| 2062 | * keywords? |
| 2063 | */ |
| 2064 | const bool uses_deprecated_qualifier = qual->flags.q.attribute |
| 2065 | || qual->flags.q.varying; |
| 2066 | |
| 2067 | /* Is the 'layout' keyword used with parameters that allow relaxed checking. |
| 2068 | * Many implementations of GL_ARB_fragment_coord_conventions_enable and some |
| 2069 | * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable |
| 2070 | * allowed the layout qualifier to be used with 'varying' and 'attribute'. |
| 2071 | * These extensions and all following extensions that add the 'layout' |
| 2072 | * keyword have been modified to require the use of 'in' or 'out'. |
| 2073 | * |
| 2074 | * The following extension do not allow the deprecated keywords: |
| 2075 | * |
| 2076 | * GL_AMD_conservative_depth |
| 2077 | * GL_ARB_gpu_shader5 |
| 2078 | * GL_ARB_separate_shader_objects |
| 2079 | * GL_ARB_tesselation_shader |
| 2080 | * GL_ARB_transform_feedback3 |
| 2081 | * GL_ARB_uniform_buffer_object |
| 2082 | * |
| 2083 | * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5 |
| 2084 | * allow layout with the deprecated keywords. |
| 2085 | */ |
| 2086 | const bool relaxed_layout_qualifier_checking = |
| 2087 | state->ARB_fragment_coord_conventions_enable; |
| 2088 | |
| 2089 | if (uses_layout && uses_deprecated_qualifier) { |
| 2090 | if (relaxed_layout_qualifier_checking) { |
| 2091 | _mesa_glsl_warning(loc, state, |
| 2092 | "`layout' qualifier may not be used with " |
| 2093 | "`attribute' or `varying'"); |
| 2094 | } else { |
| 2095 | _mesa_glsl_error(loc, state, |
| 2096 | "`layout' qualifier may not be used with " |
| 2097 | "`attribute' or `varying'"); |
| 2098 | } |
| 2099 | } |
| 2100 | |
Chad Versace | bc04d24 | 2011-01-27 01:40:26 -0800 | [diff] [blame] | 2101 | /* Layout qualifiers for gl_FragDepth, which are enabled by extension |
| 2102 | * AMD_conservative_depth. |
| 2103 | */ |
| 2104 | int depth_layout_count = qual->flags.q.depth_any |
| 2105 | + qual->flags.q.depth_greater |
| 2106 | + qual->flags.q.depth_less |
| 2107 | + qual->flags.q.depth_unchanged; |
| 2108 | if (depth_layout_count > 0 |
| 2109 | && !state->AMD_conservative_depth_enable) { |
| 2110 | _mesa_glsl_error(loc, state, |
| 2111 | "extension GL_AMD_conservative_depth must be enabled " |
| 2112 | "to use depth layout qualifiers"); |
| 2113 | } else if (depth_layout_count > 0 |
| 2114 | && strcmp(var->name, "gl_FragDepth") != 0) { |
| 2115 | _mesa_glsl_error(loc, state, |
| 2116 | "depth layout qualifiers can be applied only to " |
| 2117 | "gl_FragDepth"); |
| 2118 | } else if (depth_layout_count > 1 |
| 2119 | && strcmp(var->name, "gl_FragDepth") == 0) { |
| 2120 | _mesa_glsl_error(loc, state, |
| 2121 | "at most one depth layout qualifier can be applied to " |
| 2122 | "gl_FragDepth"); |
| 2123 | } |
| 2124 | if (qual->flags.q.depth_any) |
| 2125 | var->depth_layout = ir_depth_layout_any; |
| 2126 | else if (qual->flags.q.depth_greater) |
| 2127 | var->depth_layout = ir_depth_layout_greater; |
| 2128 | else if (qual->flags.q.depth_less) |
| 2129 | var->depth_layout = ir_depth_layout_less; |
| 2130 | else if (qual->flags.q.depth_unchanged) |
| 2131 | var->depth_layout = ir_depth_layout_unchanged; |
| 2132 | else |
| 2133 | var->depth_layout = ir_depth_layout_none; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2134 | } |
| 2135 | |
Ian Romanick | 8e6cb9f | 2011-03-04 15:28:40 -0800 | [diff] [blame] | 2136 | /** |
| 2137 | * Get the variable that is being redeclared by this declaration |
| 2138 | * |
| 2139 | * Semantic checks to verify the validity of the redeclaration are also |
| 2140 | * performed. If semantic checks fail, compilation error will be emitted via |
| 2141 | * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned. |
| 2142 | * |
| 2143 | * \returns |
| 2144 | * A pointer to an existing variable in the current scope if the declaration |
| 2145 | * is a redeclaration, \c NULL otherwise. |
| 2146 | */ |
| 2147 | ir_variable * |
| 2148 | get_variable_being_redeclared(ir_variable *var, ast_declaration *decl, |
| 2149 | struct _mesa_glsl_parse_state *state) |
| 2150 | { |
| 2151 | /* Check if this declaration is actually a re-declaration, either to |
| 2152 | * resize an array or add qualifiers to an existing variable. |
| 2153 | * |
| 2154 | * This is allowed for variables in the current scope, or when at |
| 2155 | * global scope (for built-ins in the implicit outer scope). |
| 2156 | */ |
| 2157 | ir_variable *earlier = state->symbols->get_variable(decl->identifier); |
| 2158 | if (earlier == NULL || |
| 2159 | (state->current_function != NULL && |
| 2160 | !state->symbols->name_declared_this_scope(decl->identifier))) { |
| 2161 | return NULL; |
| 2162 | } |
| 2163 | |
| 2164 | |
| 2165 | YYLTYPE loc = decl->get_location(); |
| 2166 | |
| 2167 | /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec, |
| 2168 | * |
| 2169 | * "It is legal to declare an array without a size and then |
| 2170 | * later re-declare the same name as an array of the same |
| 2171 | * type and specify a size." |
| 2172 | */ |
| 2173 | if ((earlier->type->array_size() == 0) |
| 2174 | && var->type->is_array() |
| 2175 | && (var->type->element_type() == earlier->type->element_type())) { |
| 2176 | /* FINISHME: This doesn't match the qualifiers on the two |
| 2177 | * FINISHME: declarations. It's not 100% clear whether this is |
| 2178 | * FINISHME: required or not. |
| 2179 | */ |
| 2180 | |
Ian Romanick | 8e6cb9f | 2011-03-04 15:28:40 -0800 | [diff] [blame] | 2181 | const unsigned size = unsigned(var->type->array_size()); |
Paul Berry | 93b9758 | 2011-09-06 10:01:51 -0700 | [diff] [blame] | 2182 | check_builtin_array_max_size(var->name, size, loc, state); |
| 2183 | if ((size > 0) && (size <= earlier->max_array_access)) { |
Ian Romanick | 8e6cb9f | 2011-03-04 15:28:40 -0800 | [diff] [blame] | 2184 | _mesa_glsl_error(& loc, state, "array size must be > %u due to " |
| 2185 | "previous access", |
| 2186 | earlier->max_array_access); |
| 2187 | } |
| 2188 | |
| 2189 | earlier->type = var->type; |
| 2190 | delete var; |
| 2191 | var = NULL; |
| 2192 | } else if (state->ARB_fragment_coord_conventions_enable |
| 2193 | && strcmp(var->name, "gl_FragCoord") == 0 |
| 2194 | && earlier->type == var->type |
| 2195 | && earlier->mode == var->mode) { |
| 2196 | /* Allow redeclaration of gl_FragCoord for ARB_fcc layout |
| 2197 | * qualifiers. |
| 2198 | */ |
| 2199 | earlier->origin_upper_left = var->origin_upper_left; |
| 2200 | earlier->pixel_center_integer = var->pixel_center_integer; |
| 2201 | |
| 2202 | /* According to section 4.3.7 of the GLSL 1.30 spec, |
| 2203 | * the following built-in varaibles can be redeclared with an |
| 2204 | * interpolation qualifier: |
| 2205 | * * gl_FrontColor |
| 2206 | * * gl_BackColor |
| 2207 | * * gl_FrontSecondaryColor |
| 2208 | * * gl_BackSecondaryColor |
| 2209 | * * gl_Color |
| 2210 | * * gl_SecondaryColor |
| 2211 | */ |
| 2212 | } else if (state->language_version >= 130 |
| 2213 | && (strcmp(var->name, "gl_FrontColor") == 0 |
| 2214 | || strcmp(var->name, "gl_BackColor") == 0 |
| 2215 | || strcmp(var->name, "gl_FrontSecondaryColor") == 0 |
| 2216 | || strcmp(var->name, "gl_BackSecondaryColor") == 0 |
| 2217 | || strcmp(var->name, "gl_Color") == 0 |
| 2218 | || strcmp(var->name, "gl_SecondaryColor") == 0) |
| 2219 | && earlier->type == var->type |
| 2220 | && earlier->mode == var->mode) { |
| 2221 | earlier->interpolation = var->interpolation; |
| 2222 | |
| 2223 | /* Layout qualifiers for gl_FragDepth. */ |
| 2224 | } else if (state->AMD_conservative_depth_enable |
| 2225 | && strcmp(var->name, "gl_FragDepth") == 0 |
| 2226 | && earlier->type == var->type |
| 2227 | && earlier->mode == var->mode) { |
| 2228 | |
| 2229 | /** From the AMD_conservative_depth spec: |
| 2230 | * Within any shader, the first redeclarations of gl_FragDepth |
| 2231 | * must appear before any use of gl_FragDepth. |
| 2232 | */ |
| 2233 | if (earlier->used) { |
| 2234 | _mesa_glsl_error(&loc, state, |
| 2235 | "the first redeclaration of gl_FragDepth " |
| 2236 | "must appear before any use of gl_FragDepth"); |
| 2237 | } |
| 2238 | |
| 2239 | /* Prevent inconsistent redeclaration of depth layout qualifier. */ |
| 2240 | if (earlier->depth_layout != ir_depth_layout_none |
| 2241 | && earlier->depth_layout != var->depth_layout) { |
| 2242 | _mesa_glsl_error(&loc, state, |
| 2243 | "gl_FragDepth: depth layout is declared here " |
| 2244 | "as '%s, but it was previously declared as " |
| 2245 | "'%s'", |
| 2246 | depth_layout_string(var->depth_layout), |
| 2247 | depth_layout_string(earlier->depth_layout)); |
| 2248 | } |
| 2249 | |
| 2250 | earlier->depth_layout = var->depth_layout; |
| 2251 | |
| 2252 | } else { |
| 2253 | _mesa_glsl_error(&loc, state, "`%s' redeclared", decl->identifier); |
| 2254 | } |
| 2255 | |
| 2256 | return earlier; |
| 2257 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2258 | |
Ian Romanick | 0292ffb | 2011-03-04 15:29:33 -0800 | [diff] [blame] | 2259 | /** |
| 2260 | * Generate the IR for an initializer in a variable declaration |
| 2261 | */ |
| 2262 | ir_rvalue * |
| 2263 | process_initializer(ir_variable *var, ast_declaration *decl, |
| 2264 | ast_fully_specified_type *type, |
| 2265 | exec_list *initializer_instructions, |
| 2266 | struct _mesa_glsl_parse_state *state) |
| 2267 | { |
| 2268 | ir_rvalue *result = NULL; |
| 2269 | |
| 2270 | YYLTYPE initializer_loc = decl->initializer->get_location(); |
| 2271 | |
| 2272 | /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec: |
| 2273 | * |
| 2274 | * "All uniform variables are read-only and are initialized either |
| 2275 | * directly by an application via API commands, or indirectly by |
| 2276 | * OpenGL." |
| 2277 | */ |
| 2278 | if ((state->language_version <= 110) |
| 2279 | && (var->mode == ir_var_uniform)) { |
| 2280 | _mesa_glsl_error(& initializer_loc, state, |
| 2281 | "cannot initialize uniforms in GLSL 1.10"); |
| 2282 | } |
| 2283 | |
| 2284 | if (var->type->is_sampler()) { |
| 2285 | _mesa_glsl_error(& initializer_loc, state, |
| 2286 | "cannot initialize samplers"); |
| 2287 | } |
| 2288 | |
| 2289 | if ((var->mode == ir_var_in) && (state->current_function == NULL)) { |
| 2290 | _mesa_glsl_error(& initializer_loc, state, |
| 2291 | "cannot initialize %s shader input / %s", |
| 2292 | _mesa_glsl_shader_target_name(state->target), |
| 2293 | (state->target == vertex_shader) |
| 2294 | ? "attribute" : "varying"); |
| 2295 | } |
| 2296 | |
| 2297 | ir_dereference *const lhs = new(state) ir_dereference_variable(var); |
| 2298 | ir_rvalue *rhs = decl->initializer->hir(initializer_instructions, |
| 2299 | state); |
| 2300 | |
| 2301 | /* Calculate the constant value if this is a const or uniform |
| 2302 | * declaration. |
| 2303 | */ |
| 2304 | if (type->qualifier.flags.q.constant |
| 2305 | || type->qualifier.flags.q.uniform) { |
Ian Romanick | 85caea2 | 2011-03-15 16:33:27 -0700 | [diff] [blame] | 2306 | ir_rvalue *new_rhs = validate_assignment(state, var->type, rhs, true); |
Ian Romanick | 0292ffb | 2011-03-04 15:29:33 -0800 | [diff] [blame] | 2307 | if (new_rhs != NULL) { |
| 2308 | rhs = new_rhs; |
| 2309 | |
| 2310 | ir_constant *constant_value = rhs->constant_expression_value(); |
| 2311 | if (!constant_value) { |
| 2312 | _mesa_glsl_error(& initializer_loc, state, |
| 2313 | "initializer of %s variable `%s' must be a " |
| 2314 | "constant expression", |
| 2315 | (type->qualifier.flags.q.constant) |
| 2316 | ? "const" : "uniform", |
| 2317 | decl->identifier); |
| 2318 | if (var->type->is_numeric()) { |
| 2319 | /* Reduce cascading errors. */ |
| 2320 | var->constant_value = ir_constant::zero(state, var->type); |
| 2321 | } |
| 2322 | } else { |
| 2323 | rhs = constant_value; |
| 2324 | var->constant_value = constant_value; |
| 2325 | } |
| 2326 | } else { |
| 2327 | _mesa_glsl_error(&initializer_loc, state, |
| 2328 | "initializer of type %s cannot be assigned to " |
| 2329 | "variable of type %s", |
| 2330 | rhs->type->name, var->type->name); |
| 2331 | if (var->type->is_numeric()) { |
| 2332 | /* Reduce cascading errors. */ |
| 2333 | var->constant_value = ir_constant::zero(state, var->type); |
| 2334 | } |
| 2335 | } |
| 2336 | } |
| 2337 | |
| 2338 | if (rhs && !rhs->type->is_error()) { |
| 2339 | bool temp = var->read_only; |
| 2340 | if (type->qualifier.flags.q.constant) |
| 2341 | var->read_only = false; |
| 2342 | |
| 2343 | /* Never emit code to initialize a uniform. |
| 2344 | */ |
| 2345 | const glsl_type *initializer_type; |
| 2346 | if (!type->qualifier.flags.q.uniform) { |
| 2347 | result = do_assignment(initializer_instructions, state, |
Ian Romanick | 85caea2 | 2011-03-15 16:33:27 -0700 | [diff] [blame] | 2348 | lhs, rhs, true, |
Ian Romanick | 0292ffb | 2011-03-04 15:29:33 -0800 | [diff] [blame] | 2349 | type->get_location()); |
| 2350 | initializer_type = result->type; |
| 2351 | } else |
| 2352 | initializer_type = rhs->type; |
| 2353 | |
| 2354 | /* If the declared variable is an unsized array, it must inherrit |
| 2355 | * its full type from the initializer. A declaration such as |
| 2356 | * |
| 2357 | * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0); |
| 2358 | * |
| 2359 | * becomes |
| 2360 | * |
| 2361 | * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0); |
| 2362 | * |
| 2363 | * The assignment generated in the if-statement (below) will also |
| 2364 | * automatically handle this case for non-uniforms. |
| 2365 | * |
| 2366 | * If the declared variable is not an array, the types must |
| 2367 | * already match exactly. As a result, the type assignment |
| 2368 | * here can be done unconditionally. For non-uniforms the call |
| 2369 | * to do_assignment can change the type of the initializer (via |
| 2370 | * the implicit conversion rules). For uniforms the initializer |
| 2371 | * must be a constant expression, and the type of that expression |
| 2372 | * was validated above. |
| 2373 | */ |
| 2374 | var->type = initializer_type; |
| 2375 | |
| 2376 | var->read_only = temp; |
| 2377 | } |
| 2378 | |
| 2379 | return result; |
| 2380 | } |
| 2381 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 2382 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 2383 | ast_declarator_list::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 2384 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2385 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 2386 | void *ctx = state; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2387 | const struct glsl_type *decl_type; |
| 2388 | const char *type_name = NULL; |
Eric Anholt | 8558459 | 2010-04-14 15:38:52 -0700 | [diff] [blame] | 2389 | ir_rvalue *result = NULL; |
Ian Romanick | c824e35 | 2010-04-23 15:55:19 -0700 | [diff] [blame] | 2390 | YYLTYPE loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2391 | |
Ian Romanick | 6f0823d | 2010-07-01 20:39:08 -0700 | [diff] [blame] | 2392 | /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec: |
| 2393 | * |
| 2394 | * "To ensure that a particular output variable is invariant, it is |
| 2395 | * necessary to use the invariant qualifier. It can either be used to |
| 2396 | * qualify a previously declared variable as being invariant |
| 2397 | * |
| 2398 | * invariant gl_Position; // make existing gl_Position be invariant" |
| 2399 | * |
| 2400 | * In these cases the parser will set the 'invariant' flag in the declarator |
| 2401 | * list, and the type will be NULL. |
| 2402 | */ |
| 2403 | if (this->invariant) { |
| 2404 | assert(this->type == NULL); |
| 2405 | |
| 2406 | if (state->current_function != NULL) { |
| 2407 | _mesa_glsl_error(& loc, state, |
| 2408 | "All uses of `invariant' keyword must be at global " |
| 2409 | "scope\n"); |
| 2410 | } |
| 2411 | |
| 2412 | foreach_list_typed (ast_declaration, decl, link, &this->declarations) { |
| 2413 | assert(!decl->is_array); |
| 2414 | assert(decl->array_size == NULL); |
| 2415 | assert(decl->initializer == NULL); |
| 2416 | |
| 2417 | ir_variable *const earlier = |
| 2418 | state->symbols->get_variable(decl->identifier); |
| 2419 | if (earlier == NULL) { |
| 2420 | _mesa_glsl_error(& loc, state, |
| 2421 | "Undeclared variable `%s' cannot be marked " |
| 2422 | "invariant\n", decl->identifier); |
| 2423 | } else if ((state->target == vertex_shader) |
| 2424 | && (earlier->mode != ir_var_out)) { |
| 2425 | _mesa_glsl_error(& loc, state, |
| 2426 | "`%s' cannot be marked invariant, vertex shader " |
| 2427 | "outputs only\n", decl->identifier); |
| 2428 | } else if ((state->target == fragment_shader) |
| 2429 | && (earlier->mode != ir_var_in)) { |
| 2430 | _mesa_glsl_error(& loc, state, |
| 2431 | "`%s' cannot be marked invariant, fragment shader " |
| 2432 | "inputs only\n", decl->identifier); |
Ian Romanick | bd33055 | 2011-01-07 18:34:58 -0800 | [diff] [blame] | 2433 | } else if (earlier->used) { |
| 2434 | _mesa_glsl_error(& loc, state, |
| 2435 | "variable `%s' may not be redeclared " |
| 2436 | "`invariant' after being used", |
| 2437 | earlier->name); |
Ian Romanick | 6f0823d | 2010-07-01 20:39:08 -0700 | [diff] [blame] | 2438 | } else { |
| 2439 | earlier->invariant = true; |
| 2440 | } |
| 2441 | } |
| 2442 | |
| 2443 | /* Invariant redeclarations do not have r-values. |
| 2444 | */ |
| 2445 | return NULL; |
| 2446 | } |
| 2447 | |
| 2448 | assert(this->type != NULL); |
| 2449 | assert(!this->invariant); |
| 2450 | |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 2451 | /* The type specifier may contain a structure definition. Process that |
| 2452 | * before any of the variable declarations. |
| 2453 | */ |
| 2454 | (void) this->type->specifier->hir(instructions, state); |
| 2455 | |
Ian Romanick | d612a12 | 2010-03-31 16:22:06 -0700 | [diff] [blame] | 2456 | decl_type = this->type->specifier->glsl_type(& type_name, state); |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 2457 | if (this->declarations.is_empty()) { |
Ian Romanick | f5ba4d0 | 2011-10-25 17:49:07 -0700 | [diff] [blame^] | 2458 | /* If there is no structure involved in the program text, there are two |
| 2459 | * possible scenarios: |
| 2460 | * |
| 2461 | * - The program text contained something like 'vec4;'. This is an |
| 2462 | * empty declaration. It is valid but weird. Emit a warning. |
| 2463 | * |
| 2464 | * - The program text contained something like 'S;' and 'S' is not the |
| 2465 | * name of a known structure type. This is both invalid and weird. |
| 2466 | * Emit an error. |
| 2467 | * |
| 2468 | * Note that if decl_type is NULL and there is a structure involved, |
| 2469 | * there must have been some sort of error with the structure. In this |
| 2470 | * case we assume that an error was already generated on this line of |
| 2471 | * code for the structure. There is no need to generate an additional, |
| 2472 | * confusing error. |
| 2473 | */ |
| 2474 | assert(this->type->specifier->structure == NULL || decl_type != NULL |
| 2475 | || state->error); |
| 2476 | if (this->type->specifier->structure == NULL) { |
| 2477 | if (decl_type != NULL) { |
Chia-I Wu | 547212d | 2011-08-04 00:39:07 +0900 | [diff] [blame] | 2478 | _mesa_glsl_warning(&loc, state, "empty declaration"); |
Ian Romanick | f5ba4d0 | 2011-10-25 17:49:07 -0700 | [diff] [blame^] | 2479 | } else { |
| 2480 | _mesa_glsl_error(&loc, state, |
| 2481 | "invalid type `%s' in empty declaration", |
| 2482 | type_name); |
Chia-I Wu | 547212d | 2011-08-04 00:39:07 +0900 | [diff] [blame] | 2483 | } |
Ian Romanick | c824e35 | 2010-04-23 15:55:19 -0700 | [diff] [blame] | 2484 | } |
| 2485 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2486 | |
Ian Romanick | 2b97dc6 | 2010-05-10 17:42:05 -0700 | [diff] [blame] | 2487 | foreach_list_typed (ast_declaration, decl, link, &this->declarations) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2488 | const struct glsl_type *var_type; |
Ian Romanick | 768b55a | 2010-08-13 16:46:43 -0700 | [diff] [blame] | 2489 | ir_variable *var; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2490 | |
| 2491 | /* FINISHME: Emit a warning if a variable declaration shadows a |
| 2492 | * FINISHME: declaration at a higher scope. |
| 2493 | */ |
| 2494 | |
Ian Romanick | cec65a6 | 2010-03-23 12:28:44 -0700 | [diff] [blame] | 2495 | if ((decl_type == NULL) || decl_type->is_void()) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2496 | if (type_name != NULL) { |
| 2497 | _mesa_glsl_error(& loc, state, |
| 2498 | "invalid type `%s' in declaration of `%s'", |
| 2499 | type_name, decl->identifier); |
| 2500 | } else { |
| 2501 | _mesa_glsl_error(& loc, state, |
| 2502 | "invalid type in declaration of `%s'", |
| 2503 | decl->identifier); |
| 2504 | } |
| 2505 | continue; |
| 2506 | } |
| 2507 | |
| 2508 | if (decl->is_array) { |
Kenneth Graunke | d8e34e2 | 2010-08-07 02:56:01 -0700 | [diff] [blame] | 2509 | var_type = process_array_type(&loc, decl_type, decl->array_size, |
| 2510 | state); |
Ian Romanick | a04211e | 2011-10-24 11:45:50 -0700 | [diff] [blame] | 2511 | if (var_type->is_error()) |
| 2512 | continue; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2513 | } else { |
| 2514 | var_type = decl_type; |
| 2515 | } |
| 2516 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 2517 | var = new(ctx) ir_variable(var_type, decl->identifier, ir_var_auto); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2518 | |
Eric Anholt | 3f15150 | 2010-04-02 01:53:57 -1000 | [diff] [blame] | 2519 | /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification; |
| 2520 | * |
| 2521 | * "Global variables can only use the qualifiers const, |
| 2522 | * attribute, uni form, or varying. Only one may be |
| 2523 | * specified. |
| 2524 | * |
| 2525 | * Local variables can only use the qualifier const." |
| 2526 | * |
Ian Romanick | 82c4b4f | 2011-01-07 16:53:07 -0800 | [diff] [blame] | 2527 | * This is relaxed in GLSL 1.30. It is also relaxed by any extension |
| 2528 | * that adds the 'layout' keyword. |
Eric Anholt | 3f15150 | 2010-04-02 01:53:57 -1000 | [diff] [blame] | 2529 | */ |
Ian Romanick | 82c4b4f | 2011-01-07 16:53:07 -0800 | [diff] [blame] | 2530 | if ((state->language_version < 130) |
| 2531 | && !state->ARB_explicit_attrib_location_enable |
| 2532 | && !state->ARB_fragment_coord_conventions_enable) { |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 2533 | if (this->type->qualifier.flags.q.out) { |
Eric Anholt | 3f15150 | 2010-04-02 01:53:57 -1000 | [diff] [blame] | 2534 | _mesa_glsl_error(& loc, state, |
| 2535 | "`out' qualifier in declaration of `%s' " |
Ian Romanick | 469ea69 | 2011-01-07 16:05:59 -0800 | [diff] [blame] | 2536 | "only valid for function parameters in %s.", |
| 2537 | decl->identifier, state->version_string); |
Eric Anholt | 3f15150 | 2010-04-02 01:53:57 -1000 | [diff] [blame] | 2538 | } |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 2539 | if (this->type->qualifier.flags.q.in) { |
Eric Anholt | 3f15150 | 2010-04-02 01:53:57 -1000 | [diff] [blame] | 2540 | _mesa_glsl_error(& loc, state, |
| 2541 | "`in' qualifier in declaration of `%s' " |
Ian Romanick | 469ea69 | 2011-01-07 16:05:59 -0800 | [diff] [blame] | 2542 | "only valid for function parameters in %s.", |
| 2543 | decl->identifier, state->version_string); |
Eric Anholt | 3f15150 | 2010-04-02 01:53:57 -1000 | [diff] [blame] | 2544 | } |
| 2545 | /* FINISHME: Test for other invalid qualifiers. */ |
| 2546 | } |
| 2547 | |
Eric Anholt | 2e063f1 | 2010-03-28 00:56:22 -0700 | [diff] [blame] | 2548 | apply_type_qualifier_to_variable(& this->type->qualifier, var, state, |
| 2549 | & loc); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2550 | |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 2551 | if (this->type->qualifier.flags.q.invariant) { |
Eric Anholt | 046bef2 | 2010-08-04 20:33:57 -0700 | [diff] [blame] | 2552 | if ((state->target == vertex_shader) && !(var->mode == ir_var_out || |
| 2553 | var->mode == ir_var_inout)) { |
| 2554 | /* FINISHME: Note that this doesn't work for invariant on |
| 2555 | * a function signature outval |
| 2556 | */ |
Ian Romanick | 6f0823d | 2010-07-01 20:39:08 -0700 | [diff] [blame] | 2557 | _mesa_glsl_error(& loc, state, |
| 2558 | "`%s' cannot be marked invariant, vertex shader " |
| 2559 | "outputs only\n", var->name); |
Eric Anholt | 046bef2 | 2010-08-04 20:33:57 -0700 | [diff] [blame] | 2560 | } else if ((state->target == fragment_shader) && |
| 2561 | !(var->mode == ir_var_in || var->mode == ir_var_inout)) { |
| 2562 | /* FINISHME: Note that this doesn't work for invariant on |
| 2563 | * a function signature inval |
| 2564 | */ |
Ian Romanick | 6f0823d | 2010-07-01 20:39:08 -0700 | [diff] [blame] | 2565 | _mesa_glsl_error(& loc, state, |
| 2566 | "`%s' cannot be marked invariant, fragment shader " |
| 2567 | "inputs only\n", var->name); |
| 2568 | } |
| 2569 | } |
| 2570 | |
Ian Romanick | e1c1a3f | 2010-03-31 12:26:03 -0700 | [diff] [blame] | 2571 | if (state->current_function != NULL) { |
Ian Romanick | b168e53 | 2010-03-31 12:31:18 -0700 | [diff] [blame] | 2572 | const char *mode = NULL; |
Ian Romanick | e080006 | 2010-03-31 13:15:23 -0700 | [diff] [blame] | 2573 | const char *extra = ""; |
Ian Romanick | b168e53 | 2010-03-31 12:31:18 -0700 | [diff] [blame] | 2574 | |
Ian Romanick | e080006 | 2010-03-31 13:15:23 -0700 | [diff] [blame] | 2575 | /* There is no need to check for 'inout' here because the parser will |
| 2576 | * only allow that in function parameter lists. |
Ian Romanick | e1c1a3f | 2010-03-31 12:26:03 -0700 | [diff] [blame] | 2577 | */ |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 2578 | if (this->type->qualifier.flags.q.attribute) { |
Ian Romanick | b168e53 | 2010-03-31 12:31:18 -0700 | [diff] [blame] | 2579 | mode = "attribute"; |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 2580 | } else if (this->type->qualifier.flags.q.uniform) { |
Ian Romanick | b168e53 | 2010-03-31 12:31:18 -0700 | [diff] [blame] | 2581 | mode = "uniform"; |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 2582 | } else if (this->type->qualifier.flags.q.varying) { |
Ian Romanick | b168e53 | 2010-03-31 12:31:18 -0700 | [diff] [blame] | 2583 | mode = "varying"; |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 2584 | } else if (this->type->qualifier.flags.q.in) { |
Ian Romanick | e080006 | 2010-03-31 13:15:23 -0700 | [diff] [blame] | 2585 | mode = "in"; |
| 2586 | extra = " or in function parameter list"; |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 2587 | } else if (this->type->qualifier.flags.q.out) { |
Ian Romanick | e080006 | 2010-03-31 13:15:23 -0700 | [diff] [blame] | 2588 | mode = "out"; |
| 2589 | extra = " or in function parameter list"; |
Ian Romanick | b168e53 | 2010-03-31 12:31:18 -0700 | [diff] [blame] | 2590 | } |
| 2591 | |
| 2592 | if (mode) { |
Ian Romanick | e1c1a3f | 2010-03-31 12:26:03 -0700 | [diff] [blame] | 2593 | _mesa_glsl_error(& loc, state, |
Ian Romanick | b168e53 | 2010-03-31 12:31:18 -0700 | [diff] [blame] | 2594 | "%s variable `%s' must be declared at " |
Ian Romanick | e080006 | 2010-03-31 13:15:23 -0700 | [diff] [blame] | 2595 | "global scope%s", |
| 2596 | mode, var->name, extra); |
Ian Romanick | e1c1a3f | 2010-03-31 12:26:03 -0700 | [diff] [blame] | 2597 | } |
| 2598 | } else if (var->mode == ir_var_in) { |
Chad Versace | 01a584d | 2011-01-20 14:12:16 -0800 | [diff] [blame] | 2599 | var->read_only = true; |
| 2600 | |
Ian Romanick | fb9f5b0 | 2010-03-29 17:16:35 -0700 | [diff] [blame] | 2601 | if (state->target == vertex_shader) { |
| 2602 | bool error_emitted = false; |
| 2603 | |
| 2604 | /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec: |
| 2605 | * |
| 2606 | * "Vertex shader inputs can only be float, floating-point |
| 2607 | * vectors, matrices, signed and unsigned integers and integer |
| 2608 | * vectors. Vertex shader inputs can also form arrays of these |
| 2609 | * types, but not structures." |
| 2610 | * |
Ian Romanick | 2d81620 | 2010-03-29 17:40:11 -0700 | [diff] [blame] | 2611 | * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec: |
| 2612 | * |
| 2613 | * "Vertex shader inputs can only be float, floating-point |
| 2614 | * vectors, matrices, signed and unsigned integers and integer |
| 2615 | * vectors. They cannot be arrays or structures." |
| 2616 | * |
Ian Romanick | fb9f5b0 | 2010-03-29 17:16:35 -0700 | [diff] [blame] | 2617 | * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec: |
| 2618 | * |
| 2619 | * "The attribute qualifier can be used only with float, |
| 2620 | * floating-point vectors, and matrices. Attribute variables |
| 2621 | * cannot be declared as arrays or structures." |
| 2622 | */ |
| 2623 | const glsl_type *check_type = var->type->is_array() |
| 2624 | ? var->type->fields.array : var->type; |
| 2625 | |
| 2626 | switch (check_type->base_type) { |
| 2627 | case GLSL_TYPE_FLOAT: |
| 2628 | break; |
| 2629 | case GLSL_TYPE_UINT: |
| 2630 | case GLSL_TYPE_INT: |
| 2631 | if (state->language_version > 120) |
| 2632 | break; |
| 2633 | /* FALLTHROUGH */ |
| 2634 | default: |
| 2635 | _mesa_glsl_error(& loc, state, |
| 2636 | "vertex shader input / attribute cannot have " |
| 2637 | "type %s`%s'", |
| 2638 | var->type->is_array() ? "array of " : "", |
| 2639 | check_type->name); |
| 2640 | error_emitted = true; |
| 2641 | } |
| 2642 | |
Ian Romanick | 2d81620 | 2010-03-29 17:40:11 -0700 | [diff] [blame] | 2643 | if (!error_emitted && (state->language_version <= 130) |
Ian Romanick | fb9f5b0 | 2010-03-29 17:16:35 -0700 | [diff] [blame] | 2644 | && var->type->is_array()) { |
| 2645 | _mesa_glsl_error(& loc, state, |
| 2646 | "vertex shader input / attribute cannot have " |
| 2647 | "array type"); |
| 2648 | error_emitted = true; |
| 2649 | } |
| 2650 | } |
| 2651 | } |
| 2652 | |
Chad Versace | 68d06b1 | 2010-12-16 11:06:19 -0800 | [diff] [blame] | 2653 | /* Integer vertex outputs must be qualified with 'flat'. |
| 2654 | * |
| 2655 | * From section 4.3.6 of the GLSL 1.30 spec: |
| 2656 | * "If a vertex output is a signed or unsigned integer or integer |
| 2657 | * vector, then it must be qualified with the interpolation qualifier |
| 2658 | * flat." |
| 2659 | */ |
| 2660 | if (state->language_version >= 130 |
| 2661 | && state->target == vertex_shader |
| 2662 | && state->current_function == NULL |
| 2663 | && var->type->is_integer() |
| 2664 | && var->mode == ir_var_out |
Paul Berry | cf45949 | 2011-10-25 18:06:37 -0700 | [diff] [blame] | 2665 | && var->interpolation != INTERP_QUALIFIER_FLAT) { |
Chad Versace | 68d06b1 | 2010-12-16 11:06:19 -0800 | [diff] [blame] | 2666 | |
| 2667 | _mesa_glsl_error(&loc, state, "If a vertex output is an integer, " |
| 2668 | "then it must be qualified with 'flat'"); |
| 2669 | } |
| 2670 | |
| 2671 | |
Chad Versace | 605aacc | 2011-01-11 17:21:18 -0800 | [diff] [blame] | 2672 | /* Interpolation qualifiers cannot be applied to 'centroid' and |
| 2673 | * 'centroid varying'. |
| 2674 | * |
| 2675 | * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec: |
| 2676 | * "interpolation qualifiers may only precede the qualifiers in, |
| 2677 | * centroid in, out, or centroid out in a declaration. They do not apply |
| 2678 | * to the deprecated storage qualifiers varying or centroid varying." |
| 2679 | */ |
| 2680 | if (state->language_version >= 130 |
| 2681 | && this->type->qualifier.has_interpolation() |
| 2682 | && this->type->qualifier.flags.q.varying) { |
| 2683 | |
| 2684 | const char *i = this->type->qualifier.interpolation_string(); |
| 2685 | assert(i != NULL); |
| 2686 | const char *s; |
| 2687 | if (this->type->qualifier.flags.q.centroid) |
| 2688 | s = "centroid varying"; |
| 2689 | else |
| 2690 | s = "varying"; |
| 2691 | |
| 2692 | _mesa_glsl_error(&loc, state, |
| 2693 | "qualifier '%s' cannot be applied to the " |
| 2694 | "deprecated storage qualifier '%s'", i, s); |
| 2695 | } |
| 2696 | |
| 2697 | |
Chad Versace | 8faaa4a | 2011-01-11 18:13:26 -0800 | [diff] [blame] | 2698 | /* Interpolation qualifiers can only apply to vertex shader outputs and |
| 2699 | * fragment shader inputs. |
| 2700 | * |
| 2701 | * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec: |
| 2702 | * "Outputs from a vertex shader (out) and inputs to a fragment |
| 2703 | * shader (in) can be further qualified with one or more of these |
| 2704 | * interpolation qualifiers" |
| 2705 | */ |
| 2706 | if (state->language_version >= 130 |
| 2707 | && this->type->qualifier.has_interpolation()) { |
| 2708 | |
| 2709 | const char *i = this->type->qualifier.interpolation_string(); |
| 2710 | assert(i != NULL); |
| 2711 | |
| 2712 | switch (state->target) { |
| 2713 | case vertex_shader: |
| 2714 | if (this->type->qualifier.flags.q.in) { |
| 2715 | _mesa_glsl_error(&loc, state, |
| 2716 | "qualifier '%s' cannot be applied to vertex " |
| 2717 | "shader inputs", i); |
| 2718 | } |
| 2719 | break; |
| 2720 | case fragment_shader: |
| 2721 | if (this->type->qualifier.flags.q.out) { |
| 2722 | _mesa_glsl_error(&loc, state, |
| 2723 | "qualifier '%s' cannot be applied to fragment " |
| 2724 | "shader outputs", i); |
| 2725 | } |
| 2726 | break; |
| 2727 | default: |
| 2728 | assert(0); |
| 2729 | } |
| 2730 | } |
| 2731 | |
| 2732 | |
Chad Versace | 1eb0f17 | 2011-01-11 18:24:17 -0800 | [diff] [blame] | 2733 | /* From section 4.3.4 of the GLSL 1.30 spec: |
| 2734 | * "It is an error to use centroid in in a vertex shader." |
| 2735 | */ |
| 2736 | if (state->language_version >= 130 |
| 2737 | && this->type->qualifier.flags.q.centroid |
| 2738 | && this->type->qualifier.flags.q.in |
| 2739 | && state->target == vertex_shader) { |
| 2740 | |
| 2741 | _mesa_glsl_error(&loc, state, |
| 2742 | "'centroid in' cannot be used in a vertex shader"); |
| 2743 | } |
| 2744 | |
| 2745 | |
Chad Versace | 889e1a5 | 2011-01-16 22:38:45 -0800 | [diff] [blame] | 2746 | /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30. |
| 2747 | */ |
| 2748 | if (this->type->specifier->precision != ast_precision_none |
| 2749 | && state->language_version != 100 |
| 2750 | && state->language_version < 130) { |
| 2751 | |
| 2752 | _mesa_glsl_error(&loc, state, |
| 2753 | "precision qualifiers are supported only in GLSL ES " |
| 2754 | "1.00, and GLSL 1.30 and later"); |
| 2755 | } |
| 2756 | |
| 2757 | |
Chad Versace | 45e8e6c | 2011-01-17 15:28:39 -0800 | [diff] [blame] | 2758 | /* Precision qualifiers only apply to floating point and integer types. |
Chad Versace | 889e1a5 | 2011-01-16 22:38:45 -0800 | [diff] [blame] | 2759 | * |
| 2760 | * From section 4.5.2 of the GLSL 1.30 spec: |
| 2761 | * "Any floating point or any integer declaration can have the type |
| 2762 | * preceded by one of these precision qualifiers [...] Literal |
| 2763 | * constants do not have precision qualifiers. Neither do Boolean |
| 2764 | * variables. |
Kenneth Graunke | 8752824 | 2011-03-26 23:37:09 -0700 | [diff] [blame] | 2765 | * |
| 2766 | * In GLSL ES, sampler types are also allowed. |
| 2767 | * |
| 2768 | * From page 87 of the GLSL ES spec: |
| 2769 | * "RESOLUTION: Allow sampler types to take a precision qualifier." |
Chad Versace | 889e1a5 | 2011-01-16 22:38:45 -0800 | [diff] [blame] | 2770 | */ |
| 2771 | if (this->type->specifier->precision != ast_precision_none |
Chad Versace | 45e8e6c | 2011-01-17 15:28:39 -0800 | [diff] [blame] | 2772 | && !var->type->is_float() |
| 2773 | && !var->type->is_integer() |
Kenneth Graunke | 8752824 | 2011-03-26 23:37:09 -0700 | [diff] [blame] | 2774 | && !(var->type->is_sampler() && state->es_shader) |
Chad Versace | 45e8e6c | 2011-01-17 15:28:39 -0800 | [diff] [blame] | 2775 | && !(var->type->is_array() |
| 2776 | && (var->type->fields.array->is_float() |
| 2777 | || var->type->fields.array->is_integer()))) { |
Chad Versace | 889e1a5 | 2011-01-16 22:38:45 -0800 | [diff] [blame] | 2778 | |
| 2779 | _mesa_glsl_error(&loc, state, |
Kenneth Graunke | 8752824 | 2011-03-26 23:37:09 -0700 | [diff] [blame] | 2780 | "precision qualifiers apply only to floating point" |
| 2781 | "%s types", state->es_shader ? ", integer, and sampler" |
| 2782 | : "and integer"); |
Chad Versace | 889e1a5 | 2011-01-16 22:38:45 -0800 | [diff] [blame] | 2783 | } |
| 2784 | |
Paul Berry | f072210 | 2011-07-12 12:03:02 -0700 | [diff] [blame] | 2785 | /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec: |
| 2786 | * |
| 2787 | * "[Sampler types] can only be declared as function |
| 2788 | * parameters or uniform variables (see Section 4.3.5 |
| 2789 | * "Uniform")". |
| 2790 | */ |
| 2791 | if (var_type->contains_sampler() && |
| 2792 | !this->type->qualifier.flags.q.uniform) { |
| 2793 | _mesa_glsl_error(&loc, state, "samplers must be declared uniform"); |
| 2794 | } |
| 2795 | |
Ian Romanick | e78e0fa | 2010-07-07 12:13:34 -0700 | [diff] [blame] | 2796 | /* Process the initializer and add its instructions to a temporary |
| 2797 | * list. This list will be added to the instruction stream (below) after |
| 2798 | * the declaration is added. This is done because in some cases (such as |
| 2799 | * redeclarations) the declaration may not actually be added to the |
| 2800 | * instruction stream. |
| 2801 | */ |
Eric Anholt | fa33d0b | 2010-07-29 13:50:17 -0700 | [diff] [blame] | 2802 | exec_list initializer_instructions; |
Ian Romanick | 09a4ba0 | 2011-03-04 16:15:20 -0800 | [diff] [blame] | 2803 | ir_variable *earlier = get_variable_being_redeclared(var, decl, state); |
| 2804 | |
Ian Romanick | 66faec4 | 2010-03-27 18:56:53 -0700 | [diff] [blame] | 2805 | if (decl->initializer != NULL) { |
Ian Romanick | 09a4ba0 | 2011-03-04 16:15:20 -0800 | [diff] [blame] | 2806 | result = process_initializer((earlier == NULL) ? var : earlier, |
| 2807 | decl, this->type, |
Ian Romanick | 0292ffb | 2011-03-04 15:29:33 -0800 | [diff] [blame] | 2808 | &initializer_instructions, state); |
Ian Romanick | 1936015 | 2010-03-26 18:05:27 -0700 | [diff] [blame] | 2809 | } |
Ian Romanick | 17d86f4 | 2010-03-29 12:59:02 -0700 | [diff] [blame] | 2810 | |
Eric Anholt | 0ed6125 | 2010-03-31 09:29:33 -1000 | [diff] [blame] | 2811 | /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec: |
| 2812 | * |
| 2813 | * "It is an error to write to a const variable outside of |
| 2814 | * its declaration, so they must be initialized when |
| 2815 | * declared." |
| 2816 | */ |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 2817 | if (this->type->qualifier.flags.q.constant && decl->initializer == NULL) { |
Eric Anholt | 0ed6125 | 2010-03-31 09:29:33 -1000 | [diff] [blame] | 2818 | _mesa_glsl_error(& loc, state, |
Chad Versace | 46f7105 | 2011-01-18 15:15:19 -0800 | [diff] [blame] | 2819 | "const declaration of `%s' must be initialized", |
| 2820 | decl->identifier); |
Eric Anholt | 0ed6125 | 2010-03-31 09:29:33 -1000 | [diff] [blame] | 2821 | } |
| 2822 | |
Ian Romanick | 09a4ba0 | 2011-03-04 16:15:20 -0800 | [diff] [blame] | 2823 | /* If the declaration is not a redeclaration, there are a few additional |
| 2824 | * semantic checks that must be applied. In addition, variable that was |
| 2825 | * created for the declaration should be added to the IR stream. |
| 2826 | */ |
| 2827 | if (earlier == NULL) { |
| 2828 | /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec, |
| 2829 | * |
| 2830 | * "Identifiers starting with "gl_" are reserved for use by |
| 2831 | * OpenGL, and may not be declared in a shader as either a |
| 2832 | * variable or a function." |
| 2833 | */ |
| 2834 | if (strncmp(decl->identifier, "gl_", 3) == 0) |
| 2835 | _mesa_glsl_error(& loc, state, |
| 2836 | "identifier `%s' uses reserved `gl_' prefix", |
| 2837 | decl->identifier); |
Jason Wood | c475a54 | 2011-10-06 22:37:48 -0700 | [diff] [blame] | 2838 | else if (strstr(decl->identifier, "__")) { |
Eric Anholt | 684b701 | 2011-10-03 16:27:59 -0700 | [diff] [blame] | 2839 | /* From page 14 (page 20 of the PDF) of the GLSL 1.10 |
| 2840 | * spec: |
| 2841 | * |
| 2842 | * "In addition, all identifiers containing two |
| 2843 | * consecutive underscores (__) are reserved as |
| 2844 | * possible future keywords." |
| 2845 | */ |
| 2846 | _mesa_glsl_error(& loc, state, |
| 2847 | "identifier `%s' uses reserved `__' string", |
| 2848 | decl->identifier); |
| 2849 | } |
Ian Romanick | 09a4ba0 | 2011-03-04 16:15:20 -0800 | [diff] [blame] | 2850 | |
| 2851 | /* Add the variable to the symbol table. Note that the initializer's |
| 2852 | * IR was already processed earlier (though it hasn't been emitted |
| 2853 | * yet), without the variable in scope. |
| 2854 | * |
| 2855 | * This differs from most C-like languages, but it follows the GLSL |
| 2856 | * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50 |
| 2857 | * spec: |
| 2858 | * |
| 2859 | * "Within a declaration, the scope of a name starts immediately |
| 2860 | * after the initializer if present or immediately after the name |
| 2861 | * being declared if not." |
| 2862 | */ |
| 2863 | if (!state->symbols->add_variable(var)) { |
| 2864 | YYLTYPE loc = this->get_location(); |
| 2865 | _mesa_glsl_error(&loc, state, "name `%s' already taken in the " |
| 2866 | "current scope", decl->identifier); |
| 2867 | continue; |
| 2868 | } |
| 2869 | |
| 2870 | /* Push the variable declaration to the top. It means that all the |
| 2871 | * variable declarations will appear in a funny last-to-first order, |
| 2872 | * but otherwise we run into trouble if a function is prototyped, a |
| 2873 | * global var is decled, then the function is defined with usage of |
| 2874 | * the global var. See glslparsertest's CorrectModule.frag. |
| 2875 | */ |
| 2876 | instructions->push_head(var); |
Ian Romanick | 5466b63 | 2010-07-01 12:46:55 -0700 | [diff] [blame] | 2877 | } |
| 2878 | |
Eric Anholt | fa33d0b | 2010-07-29 13:50:17 -0700 | [diff] [blame] | 2879 | instructions->append_list(&initializer_instructions); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2880 | } |
| 2881 | |
Eric Anholt | 8558459 | 2010-04-14 15:38:52 -0700 | [diff] [blame] | 2882 | |
| 2883 | /* Generally, variable declarations do not have r-values. However, |
| 2884 | * one is used for the declaration in |
| 2885 | * |
| 2886 | * while (bool b = some_condition()) { |
| 2887 | * ... |
| 2888 | * } |
| 2889 | * |
| 2890 | * so we return the rvalue from the last seen declaration here. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2891 | */ |
Eric Anholt | 8558459 | 2010-04-14 15:38:52 -0700 | [diff] [blame] | 2892 | return result; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2893 | } |
| 2894 | |
| 2895 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 2896 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 2897 | ast_parameter_declarator::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 2898 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2899 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 2900 | void *ctx = state; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2901 | const struct glsl_type *type; |
| 2902 | const char *name = NULL; |
Eric Anholt | 2e063f1 | 2010-03-28 00:56:22 -0700 | [diff] [blame] | 2903 | YYLTYPE loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2904 | |
Ian Romanick | d612a12 | 2010-03-31 16:22:06 -0700 | [diff] [blame] | 2905 | type = this->type->specifier->glsl_type(& name, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2906 | |
| 2907 | if (type == NULL) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2908 | if (name != NULL) { |
| 2909 | _mesa_glsl_error(& loc, state, |
| 2910 | "invalid type `%s' in declaration of `%s'", |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 2911 | name, this->identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2912 | } else { |
| 2913 | _mesa_glsl_error(& loc, state, |
| 2914 | "invalid type in declaration of `%s'", |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 2915 | this->identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2916 | } |
| 2917 | |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 2918 | type = glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2919 | } |
| 2920 | |
Eric Anholt | 068c80c | 2010-03-31 09:56:36 -1000 | [diff] [blame] | 2921 | /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec: |
| 2922 | * |
| 2923 | * "Functions that accept no input arguments need not use void in the |
| 2924 | * argument list because prototypes (or definitions) are required and |
| 2925 | * therefore there is no ambiguity when an empty argument list "( )" is |
| 2926 | * declared. The idiom "(void)" as a parameter list is provided for |
| 2927 | * convenience." |
| 2928 | * |
| 2929 | * Placing this check here prevents a void parameter being set up |
| 2930 | * for a function, which avoids tripping up checks for main taking |
| 2931 | * parameters and lookups of an unnamed symbol. |
| 2932 | */ |
Ian Romanick | cf37c9e | 2010-04-02 15:30:45 -0700 | [diff] [blame] | 2933 | if (type->is_void()) { |
| 2934 | if (this->identifier != NULL) |
| 2935 | _mesa_glsl_error(& loc, state, |
| 2936 | "named parameter cannot have type `void'"); |
| 2937 | |
| 2938 | is_void = true; |
Eric Anholt | 068c80c | 2010-03-31 09:56:36 -1000 | [diff] [blame] | 2939 | return NULL; |
Ian Romanick | cf37c9e | 2010-04-02 15:30:45 -0700 | [diff] [blame] | 2940 | } |
Eric Anholt | 068c80c | 2010-03-31 09:56:36 -1000 | [diff] [blame] | 2941 | |
Ian Romanick | 45d8a70 | 2010-04-02 15:09:33 -0700 | [diff] [blame] | 2942 | if (formal_parameter && (this->identifier == NULL)) { |
| 2943 | _mesa_glsl_error(& loc, state, "formal parameter lacks a name"); |
| 2944 | return NULL; |
| 2945 | } |
| 2946 | |
Kenneth Graunke | e511a35 | 2010-08-21 15:30:34 -0700 | [diff] [blame] | 2947 | /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...) |
| 2948 | * call already handled the "vec4[..] foo" case. |
| 2949 | */ |
| 2950 | if (this->is_array) { |
Kenneth Graunke | d8e34e2 | 2010-08-07 02:56:01 -0700 | [diff] [blame] | 2951 | type = process_array_type(&loc, type, this->array_size, state); |
Kenneth Graunke | e511a35 | 2010-08-21 15:30:34 -0700 | [diff] [blame] | 2952 | } |
| 2953 | |
Ian Romanick | a04211e | 2011-10-24 11:45:50 -0700 | [diff] [blame] | 2954 | if (!type->is_error() && type->array_size() == 0) { |
Kenneth Graunke | e511a35 | 2010-08-21 15:30:34 -0700 | [diff] [blame] | 2955 | _mesa_glsl_error(&loc, state, "arrays passed as parameters must have " |
| 2956 | "a declared size."); |
| 2957 | type = glsl_type::error_type; |
| 2958 | } |
| 2959 | |
Ian Romanick | cf37c9e | 2010-04-02 15:30:45 -0700 | [diff] [blame] | 2960 | is_void = false; |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 2961 | ir_variable *var = new(ctx) ir_variable(type, this->identifier, ir_var_in); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2962 | |
Ian Romanick | cdb8d54 | 2010-03-11 14:48:51 -0800 | [diff] [blame] | 2963 | /* Apply any specified qualifiers to the parameter declaration. Note that |
| 2964 | * for function parameters the default mode is 'in'. |
| 2965 | */ |
Eric Anholt | 2e063f1 | 2010-03-28 00:56:22 -0700 | [diff] [blame] | 2966 | apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2967 | |
Paul Berry | f072210 | 2011-07-12 12:03:02 -0700 | [diff] [blame] | 2968 | /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec: |
| 2969 | * |
| 2970 | * "Samplers cannot be treated as l-values; hence cannot be used |
| 2971 | * as out or inout function parameters, nor can they be assigned |
| 2972 | * into." |
| 2973 | */ |
| 2974 | if ((var->mode == ir_var_inout || var->mode == ir_var_out) |
| 2975 | && type->contains_sampler()) { |
| 2976 | _mesa_glsl_error(&loc, state, "out and inout parameters cannot contain samplers"); |
| 2977 | type = glsl_type::error_type; |
| 2978 | } |
| 2979 | |
Paul Berry | 00792e3 | 2011-09-10 07:48:46 -0700 | [diff] [blame] | 2980 | /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec: |
| 2981 | * |
| 2982 | * "When calling a function, expressions that do not evaluate to |
| 2983 | * l-values cannot be passed to parameters declared as out or inout." |
| 2984 | * |
| 2985 | * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec: |
| 2986 | * |
| 2987 | * "Other binary or unary expressions, non-dereferenced arrays, |
| 2988 | * function names, swizzles with repeated fields, and constants |
| 2989 | * cannot be l-values." |
| 2990 | * |
| 2991 | * So for GLSL 1.10, passing an array as an out or inout parameter is not |
| 2992 | * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES. |
| 2993 | */ |
| 2994 | if ((var->mode == ir_var_inout || var->mode == ir_var_out) |
| 2995 | && type->is_array() && state->language_version == 110) { |
| 2996 | _mesa_glsl_error(&loc, state, "Arrays cannot be out or inout parameters in GLSL 1.10"); |
| 2997 | type = glsl_type::error_type; |
| 2998 | } |
| 2999 | |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 3000 | instructions->push_tail(var); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3001 | |
| 3002 | /* Parameter declarations do not have r-values. |
| 3003 | */ |
| 3004 | return NULL; |
| 3005 | } |
| 3006 | |
| 3007 | |
Ian Romanick | 45d8a70 | 2010-04-02 15:09:33 -0700 | [diff] [blame] | 3008 | void |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 3009 | ast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters, |
Ian Romanick | 45d8a70 | 2010-04-02 15:09:33 -0700 | [diff] [blame] | 3010 | bool formal, |
| 3011 | exec_list *ir_parameters, |
| 3012 | _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3013 | { |
Ian Romanick | cf37c9e | 2010-04-02 15:30:45 -0700 | [diff] [blame] | 3014 | ast_parameter_declarator *void_param = NULL; |
| 3015 | unsigned count = 0; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3016 | |
Ian Romanick | 2b97dc6 | 2010-05-10 17:42:05 -0700 | [diff] [blame] | 3017 | foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) { |
Ian Romanick | 45d8a70 | 2010-04-02 15:09:33 -0700 | [diff] [blame] | 3018 | param->formal_parameter = formal; |
Eric Anholt | 068c80c | 2010-03-31 09:56:36 -1000 | [diff] [blame] | 3019 | param->hir(ir_parameters, state); |
Ian Romanick | cf37c9e | 2010-04-02 15:30:45 -0700 | [diff] [blame] | 3020 | |
| 3021 | if (param->is_void) |
| 3022 | void_param = param; |
| 3023 | |
| 3024 | count++; |
| 3025 | } |
| 3026 | |
| 3027 | if ((void_param != NULL) && (count > 1)) { |
| 3028 | YYLTYPE loc = void_param->get_location(); |
| 3029 | |
| 3030 | _mesa_glsl_error(& loc, state, |
| 3031 | "`void' parameter must be only parameter"); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3032 | } |
| 3033 | } |
| 3034 | |
| 3035 | |
Kenneth Graunke | 6fae1e4 | 2010-12-06 10:54:05 -0800 | [diff] [blame] | 3036 | void |
Paul Berry | 0d81b0e | 2011-07-29 15:28:52 -0700 | [diff] [blame] | 3037 | emit_function(_mesa_glsl_parse_state *state, ir_function *f) |
Kenneth Graunke | 6fae1e4 | 2010-12-06 10:54:05 -0800 | [diff] [blame] | 3038 | { |
Paul Berry | 0d81b0e | 2011-07-29 15:28:52 -0700 | [diff] [blame] | 3039 | /* IR invariants disallow function declarations or definitions |
| 3040 | * nested within other function definitions. But there is no |
| 3041 | * requirement about the relative order of function declarations |
| 3042 | * and definitions with respect to one another. So simply insert |
| 3043 | * the new ir_function block at the end of the toplevel instruction |
| 3044 | * list. |
| 3045 | */ |
| 3046 | state->toplevel_ir->push_tail(f); |
Kenneth Graunke | 6fae1e4 | 2010-12-06 10:54:05 -0800 | [diff] [blame] | 3047 | } |
| 3048 | |
| 3049 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 3050 | ir_rvalue * |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 3051 | ast_function::hir(exec_list *instructions, |
| 3052 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3053 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 3054 | void *ctx = state; |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 3055 | ir_function *f = NULL; |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 3056 | ir_function_signature *sig = NULL; |
| 3057 | exec_list hir_parameters; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3058 | |
Kenneth Graunke | ac04c25 | 2010-06-29 00:48:10 -0700 | [diff] [blame] | 3059 | const char *const name = identifier; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3060 | |
Ian Romanick | 9a3bd5e | 2011-08-29 14:56:29 -0700 | [diff] [blame] | 3061 | /* New functions are always added to the top-level IR instruction stream, |
| 3062 | * so this instruction list pointer is ignored. See also emit_function |
| 3063 | * (called below). |
| 3064 | */ |
| 3065 | (void) instructions; |
| 3066 | |
Ian Romanick | 63b80f8 | 2010-09-01 06:34:58 -0700 | [diff] [blame] | 3067 | /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec, |
| 3068 | * |
| 3069 | * "Function declarations (prototypes) cannot occur inside of functions; |
| 3070 | * they must be at global scope, or for the built-in functions, outside |
| 3071 | * the global scope." |
| 3072 | * |
| 3073 | * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec, |
| 3074 | * |
| 3075 | * "User defined functions may only be defined within the global scope." |
| 3076 | * |
| 3077 | * Note that this language does not appear in GLSL 1.10. |
| 3078 | */ |
| 3079 | if ((state->current_function != NULL) && (state->language_version != 110)) { |
| 3080 | YYLTYPE loc = this->get_location(); |
| 3081 | _mesa_glsl_error(&loc, state, |
| 3082 | "declaration of function `%s' not allowed within " |
| 3083 | "function body", name); |
| 3084 | } |
| 3085 | |
Kenneth Graunke | edd180f | 2010-08-20 02:14:35 -0700 | [diff] [blame] | 3086 | /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec, |
| 3087 | * |
| 3088 | * "Identifiers starting with "gl_" are reserved for use by |
| 3089 | * OpenGL, and may not be declared in a shader as either a |
| 3090 | * variable or a function." |
| 3091 | */ |
| 3092 | if (strncmp(name, "gl_", 3) == 0) { |
| 3093 | YYLTYPE loc = this->get_location(); |
| 3094 | _mesa_glsl_error(&loc, state, |
| 3095 | "identifier `%s' uses reserved `gl_' prefix", name); |
| 3096 | } |
| 3097 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3098 | /* Convert the list of function parameters to HIR now so that they can be |
| 3099 | * used below to compare this function's signature with previously seen |
| 3100 | * signatures for functions with the same name. |
| 3101 | */ |
Ian Romanick | 45d8a70 | 2010-04-02 15:09:33 -0700 | [diff] [blame] | 3102 | ast_parameter_declarator::parameters_to_hir(& this->parameters, |
| 3103 | is_definition, |
| 3104 | & hir_parameters, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3105 | |
Ian Romanick | e39cc69 | 2010-03-23 12:19:13 -0700 | [diff] [blame] | 3106 | const char *return_type_name; |
| 3107 | const glsl_type *return_type = |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 3108 | this->return_type->specifier->glsl_type(& return_type_name, state); |
Ian Romanick | e39cc69 | 2010-03-23 12:19:13 -0700 | [diff] [blame] | 3109 | |
Eric Anholt | 76e96d7 | 2010-08-23 13:26:52 -0700 | [diff] [blame] | 3110 | if (!return_type) { |
| 3111 | YYLTYPE loc = this->get_location(); |
| 3112 | _mesa_glsl_error(&loc, state, |
| 3113 | "function `%s' has undeclared return type `%s'", |
| 3114 | name, return_type_name); |
| 3115 | return_type = glsl_type::error_type; |
| 3116 | } |
Ian Romanick | e39cc69 | 2010-03-23 12:19:13 -0700 | [diff] [blame] | 3117 | |
Kenneth Graunke | ac04c25 | 2010-06-29 00:48:10 -0700 | [diff] [blame] | 3118 | /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec: |
| 3119 | * "No qualifier is allowed on the return type of a function." |
| 3120 | */ |
| 3121 | if (this->return_type->has_qualifiers()) { |
| 3122 | YYLTYPE loc = this->get_location(); |
| 3123 | _mesa_glsl_error(& loc, state, |
| 3124 | "function `%s' return type has qualifiers", name); |
| 3125 | } |
| 3126 | |
Paul Berry | f072210 | 2011-07-12 12:03:02 -0700 | [diff] [blame] | 3127 | /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec: |
| 3128 | * |
| 3129 | * "[Sampler types] can only be declared as function parameters |
| 3130 | * or uniform variables (see Section 4.3.5 "Uniform")". |
| 3131 | */ |
| 3132 | if (return_type->contains_sampler()) { |
| 3133 | YYLTYPE loc = this->get_location(); |
| 3134 | _mesa_glsl_error(&loc, state, |
| 3135 | "function `%s' return type can't contain a sampler", |
| 3136 | name); |
| 3137 | } |
| 3138 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3139 | /* Verify that this function's signature either doesn't match a previously |
| 3140 | * seen signature for a function with the same name, or, if a match is found, |
| 3141 | * that the previously seen signature does not have an associated definition. |
| 3142 | */ |
Ian Romanick | e466b18 | 2010-09-01 14:16:53 -0700 | [diff] [blame] | 3143 | f = state->symbols->get_function(name); |
Kenneth Graunke | 81f0339 | 2010-09-16 02:52:25 -0700 | [diff] [blame] | 3144 | if (f != NULL && (state->es_shader || f->has_user_signature())) { |
Ian Romanick | 202604e | 2010-08-11 16:58:25 -0700 | [diff] [blame] | 3145 | sig = f->exact_matching_signature(&hir_parameters); |
Kenneth Graunke | 0d605cb | 2010-04-28 12:04:23 -0700 | [diff] [blame] | 3146 | if (sig != NULL) { |
| 3147 | const char *badvar = sig->qualifiers_match(&hir_parameters); |
| 3148 | if (badvar != NULL) { |
| 3149 | YYLTYPE loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3150 | |
Kenneth Graunke | 0d605cb | 2010-04-28 12:04:23 -0700 | [diff] [blame] | 3151 | _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' " |
| 3152 | "qualifiers don't match prototype", name, badvar); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3153 | } |
| 3154 | |
Kenneth Graunke | 0d605cb | 2010-04-28 12:04:23 -0700 | [diff] [blame] | 3155 | if (sig->return_type != return_type) { |
| 3156 | YYLTYPE loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3157 | |
Kenneth Graunke | 0d605cb | 2010-04-28 12:04:23 -0700 | [diff] [blame] | 3158 | _mesa_glsl_error(&loc, state, "function `%s' return type doesn't " |
| 3159 | "match prototype", name); |
| 3160 | } |
| 3161 | |
| 3162 | if (is_definition && sig->is_defined) { |
| 3163 | YYLTYPE loc = this->get_location(); |
| 3164 | |
| 3165 | _mesa_glsl_error(& loc, state, "function `%s' redefined", name); |
Kenneth Graunke | 0d605cb | 2010-04-28 12:04:23 -0700 | [diff] [blame] | 3166 | } |
| 3167 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3168 | } else { |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 3169 | f = new(ctx) ir_function(name); |
Eric Anholt | e8f5ebf | 2010-11-05 06:08:45 -0700 | [diff] [blame] | 3170 | if (!state->symbols->add_function(f)) { |
Kenneth Graunke | e095913 | 2010-08-25 16:37:46 -0700 | [diff] [blame] | 3171 | /* This function name shadows a non-function use of the same name. */ |
| 3172 | YYLTYPE loc = this->get_location(); |
| 3173 | |
| 3174 | _mesa_glsl_error(&loc, state, "function name `%s' conflicts with " |
| 3175 | "non-function", name); |
| 3176 | return NULL; |
| 3177 | } |
Kenneth Graunke | 9fa99f3 | 2010-04-21 12:30:22 -0700 | [diff] [blame] | 3178 | |
Paul Berry | 0d81b0e | 2011-07-29 15:28:52 -0700 | [diff] [blame] | 3179 | emit_function(state, f); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3180 | } |
| 3181 | |
Eric Anholt | ab372da | 2010-03-28 01:24:55 -0700 | [diff] [blame] | 3182 | /* Verify the return type of main() */ |
| 3183 | if (strcmp(name, "main") == 0) { |
Ian Romanick | 25711a8 | 2010-03-31 17:39:10 -0700 | [diff] [blame] | 3184 | if (! return_type->is_void()) { |
Eric Anholt | ab372da | 2010-03-28 01:24:55 -0700 | [diff] [blame] | 3185 | YYLTYPE loc = this->get_location(); |
| 3186 | |
| 3187 | _mesa_glsl_error(& loc, state, "main() must return void"); |
| 3188 | } |
Eric Anholt | 174cc03 | 2010-03-30 23:37:51 -1000 | [diff] [blame] | 3189 | |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 3190 | if (!hir_parameters.is_empty()) { |
Eric Anholt | 174cc03 | 2010-03-30 23:37:51 -1000 | [diff] [blame] | 3191 | YYLTYPE loc = this->get_location(); |
| 3192 | |
| 3193 | _mesa_glsl_error(& loc, state, "main() must not take any parameters"); |
| 3194 | } |
Eric Anholt | ab372da | 2010-03-28 01:24:55 -0700 | [diff] [blame] | 3195 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3196 | |
| 3197 | /* Finish storing the information about this new function in its signature. |
| 3198 | */ |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 3199 | if (sig == NULL) { |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 3200 | sig = new(ctx) ir_function_signature(return_type); |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 3201 | f->add_signature(sig); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3202 | } |
| 3203 | |
Kenneth Graunke | bff6013 | 2010-04-28 12:44:24 -0700 | [diff] [blame] | 3204 | sig->replace_parameters(&hir_parameters); |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 3205 | signature = sig; |
Ian Romanick | e29a585 | 2010-03-31 17:54:26 -0700 | [diff] [blame] | 3206 | |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 3207 | /* Function declarations (prototypes) do not have r-values. |
| 3208 | */ |
| 3209 | return NULL; |
| 3210 | } |
| 3211 | |
| 3212 | |
| 3213 | ir_rvalue * |
| 3214 | ast_function_definition::hir(exec_list *instructions, |
| 3215 | struct _mesa_glsl_parse_state *state) |
| 3216 | { |
| 3217 | prototype->is_definition = true; |
| 3218 | prototype->hir(instructions, state); |
| 3219 | |
| 3220 | ir_function_signature *signature = prototype->signature; |
Kenneth Graunke | 826a39c | 2010-08-20 02:04:52 -0700 | [diff] [blame] | 3221 | if (signature == NULL) |
| 3222 | return NULL; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3223 | |
Ian Romanick | 41ec6a4 | 2010-03-19 17:08:05 -0700 | [diff] [blame] | 3224 | assert(state->current_function == NULL); |
| 3225 | state->current_function = signature; |
Kenneth Graunke | 6de8256 | 2010-06-29 09:59:40 -0700 | [diff] [blame] | 3226 | state->found_return = false; |
Ian Romanick | 41ec6a4 | 2010-03-19 17:08:05 -0700 | [diff] [blame] | 3227 | |
Ian Romanick | e29a585 | 2010-03-31 17:54:26 -0700 | [diff] [blame] | 3228 | /* Duplicate parameters declared in the prototype as concrete variables. |
| 3229 | * Add these to the symbol table. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3230 | */ |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 3231 | state->symbols->push_scope(); |
Ian Romanick | e29a585 | 2010-03-31 17:54:26 -0700 | [diff] [blame] | 3232 | foreach_iter(exec_list_iterator, iter, signature->parameters) { |
Eric Anholt | fbc7c0b | 2010-04-07 14:32:53 -0700 | [diff] [blame] | 3233 | ir_variable *const var = ((ir_instruction *) iter.get())->as_variable(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3234 | |
Eric Anholt | fbc7c0b | 2010-04-07 14:32:53 -0700 | [diff] [blame] | 3235 | assert(var != NULL); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3236 | |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 3237 | /* The only way a parameter would "exist" is if two parameters have |
| 3238 | * the same name. |
| 3239 | */ |
| 3240 | if (state->symbols->name_declared_this_scope(var->name)) { |
| 3241 | YYLTYPE loc = this->get_location(); |
| 3242 | |
| 3243 | _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name); |
| 3244 | } else { |
Eric Anholt | 001eee5 | 2010-11-05 06:11:24 -0700 | [diff] [blame] | 3245 | state->symbols->add_variable(var); |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 3246 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3247 | } |
| 3248 | |
Kenneth Graunke | 9fa99f3 | 2010-04-21 12:30:22 -0700 | [diff] [blame] | 3249 | /* Convert the body of the function to HIR. */ |
Eric Anholt | 894ea97 | 2010-04-07 13:19:11 -0700 | [diff] [blame] | 3250 | this->body->hir(&signature->body, state); |
Kenneth Graunke | 9fa99f3 | 2010-04-21 12:30:22 -0700 | [diff] [blame] | 3251 | signature->is_defined = true; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3252 | |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 3253 | state->symbols->pop_scope(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3254 | |
Ian Romanick | 41ec6a4 | 2010-03-19 17:08:05 -0700 | [diff] [blame] | 3255 | assert(state->current_function == signature); |
| 3256 | state->current_function = NULL; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3257 | |
Kenneth Graunke | 6de8256 | 2010-06-29 09:59:40 -0700 | [diff] [blame] | 3258 | if (!signature->return_type->is_void() && !state->found_return) { |
| 3259 | YYLTYPE loc = this->get_location(); |
| 3260 | _mesa_glsl_error(& loc, state, "function `%s' has non-void return type " |
| 3261 | "%s, but no return statement", |
| 3262 | signature->function_name(), |
| 3263 | signature->return_type->name); |
| 3264 | } |
| 3265 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 3266 | /* Function definitions do not have r-values. |
| 3267 | */ |
| 3268 | return NULL; |
| 3269 | } |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 3270 | |
| 3271 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 3272 | ir_rvalue * |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 3273 | ast_jump_statement::hir(exec_list *instructions, |
| 3274 | struct _mesa_glsl_parse_state *state) |
| 3275 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 3276 | void *ctx = state; |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 3277 | |
Ian Romanick | c0e76d8 | 2010-04-05 16:53:19 -0700 | [diff] [blame] | 3278 | switch (mode) { |
| 3279 | case ast_return: { |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 3280 | ir_return *inst; |
Eric Anholt | aad7c77 | 2010-03-30 23:28:20 -1000 | [diff] [blame] | 3281 | assert(state->current_function); |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 3282 | |
| 3283 | if (opt_return_value) { |
Chad Versace | b4cdba6 | 2010-11-17 10:28:01 -0800 | [diff] [blame] | 3284 | ir_rvalue *const ret = opt_return_value->hir(instructions, state); |
Ian Romanick | 2db46fe | 2011-01-22 17:47:05 -0800 | [diff] [blame] | 3285 | |
| 3286 | /* The value of the return type can be NULL if the shader says |
| 3287 | * 'return foo();' and foo() is a function that returns void. |
| 3288 | * |
| 3289 | * NOTE: The GLSL spec doesn't say that this is an error. The type |
| 3290 | * of the return value is void. If the return type of the function is |
| 3291 | * also void, then this should compile without error. Seriously. |
| 3292 | */ |
| 3293 | const glsl_type *const ret_type = |
| 3294 | (ret == NULL) ? glsl_type::void_type : ret->type; |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 3295 | |
Kenneth Graunke | 18707eb | 2010-06-28 23:38:04 -0700 | [diff] [blame] | 3296 | /* Implicit conversions are not allowed for return values. */ |
Ian Romanick | 2db46fe | 2011-01-22 17:47:05 -0800 | [diff] [blame] | 3297 | if (state->current_function->return_type != ret_type) { |
Kenneth Graunke | 18707eb | 2010-06-28 23:38:04 -0700 | [diff] [blame] | 3298 | YYLTYPE loc = this->get_location(); |
| 3299 | |
| 3300 | _mesa_glsl_error(& loc, state, |
| 3301 | "`return' with wrong type %s, in function `%s' " |
| 3302 | "returning %s", |
Ian Romanick | 2db46fe | 2011-01-22 17:47:05 -0800 | [diff] [blame] | 3303 | ret_type->name, |
Kenneth Graunke | 18707eb | 2010-06-28 23:38:04 -0700 | [diff] [blame] | 3304 | state->current_function->function_name(), |
| 3305 | state->current_function->return_type->name); |
| 3306 | } |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 3307 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 3308 | inst = new(ctx) ir_return(ret); |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 3309 | } else { |
Eric Anholt | aad7c77 | 2010-03-30 23:28:20 -1000 | [diff] [blame] | 3310 | if (state->current_function->return_type->base_type != |
| 3311 | GLSL_TYPE_VOID) { |
| 3312 | YYLTYPE loc = this->get_location(); |
| 3313 | |
| 3314 | _mesa_glsl_error(& loc, state, |
| 3315 | "`return' with no value, in function %s returning " |
| 3316 | "non-void", |
Kenneth Graunke | f96c52b | 2010-04-21 15:17:26 -0700 | [diff] [blame] | 3317 | state->current_function->function_name()); |
Eric Anholt | aad7c77 | 2010-03-30 23:28:20 -1000 | [diff] [blame] | 3318 | } |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 3319 | inst = new(ctx) ir_return; |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 3320 | } |
| 3321 | |
Kenneth Graunke | 6de8256 | 2010-06-29 09:59:40 -0700 | [diff] [blame] | 3322 | state->found_return = true; |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 3323 | instructions->push_tail(inst); |
Ian Romanick | c0e76d8 | 2010-04-05 16:53:19 -0700 | [diff] [blame] | 3324 | break; |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 3325 | } |
| 3326 | |
Ian Romanick | c0e76d8 | 2010-04-05 16:53:19 -0700 | [diff] [blame] | 3327 | case ast_discard: |
Eric Anholt | b980207 | 2010-03-30 23:40:14 -1000 | [diff] [blame] | 3328 | if (state->target != fragment_shader) { |
| 3329 | YYLTYPE loc = this->get_location(); |
| 3330 | |
| 3331 | _mesa_glsl_error(& loc, state, |
| 3332 | "`discard' may only appear in a fragment shader"); |
| 3333 | } |
Kenneth Graunke | 77049a7 | 2010-06-30 14:11:00 -0700 | [diff] [blame] | 3334 | instructions->push_tail(new(ctx) ir_discard); |
Ian Romanick | c0e76d8 | 2010-04-05 16:53:19 -0700 | [diff] [blame] | 3335 | break; |
| 3336 | |
| 3337 | case ast_break: |
| 3338 | case ast_continue: |
Ian Romanick | 4cf20cd | 2010-04-05 17:13:47 -0700 | [diff] [blame] | 3339 | /* FINISHME: Handle switch-statements. They cannot contain 'continue', |
| 3340 | * FINISHME: and they use a different IR instruction for 'break'. |
| 3341 | */ |
| 3342 | /* FINISHME: Correctly handle the nesting. If a switch-statement is |
| 3343 | * FINISHME: inside a loop, a 'continue' is valid and will bind to the |
| 3344 | * FINISHME: loop. |
| 3345 | */ |
| 3346 | if (state->loop_or_switch_nesting == NULL) { |
| 3347 | YYLTYPE loc = this->get_location(); |
| 3348 | |
| 3349 | _mesa_glsl_error(& loc, state, |
| 3350 | "`%s' may only appear in a loop", |
| 3351 | (mode == ast_break) ? "break" : "continue"); |
| 3352 | } else { |
| 3353 | ir_loop *const loop = state->loop_or_switch_nesting->as_loop(); |
| 3354 | |
Eric Anholt | 2d1ed7b | 2010-07-22 12:55:16 -0700 | [diff] [blame] | 3355 | /* Inline the for loop expression again, since we don't know |
| 3356 | * where near the end of the loop body the normal copy of it |
| 3357 | * is going to be placed. |
| 3358 | */ |
| 3359 | if (mode == ast_continue && |
| 3360 | state->loop_or_switch_nesting_ast->rest_expression) { |
| 3361 | state->loop_or_switch_nesting_ast->rest_expression->hir(instructions, |
| 3362 | state); |
| 3363 | } |
| 3364 | |
Ian Romanick | 4cf20cd | 2010-04-05 17:13:47 -0700 | [diff] [blame] | 3365 | if (loop != NULL) { |
| 3366 | ir_loop_jump *const jump = |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 3367 | new(ctx) ir_loop_jump((mode == ast_break) |
| 3368 | ? ir_loop_jump::jump_break |
| 3369 | : ir_loop_jump::jump_continue); |
Ian Romanick | 4cf20cd | 2010-04-05 17:13:47 -0700 | [diff] [blame] | 3370 | instructions->push_tail(jump); |
| 3371 | } |
| 3372 | } |
| 3373 | |
Ian Romanick | c0e76d8 | 2010-04-05 16:53:19 -0700 | [diff] [blame] | 3374 | break; |
Eric Anholt | b980207 | 2010-03-30 23:40:14 -1000 | [diff] [blame] | 3375 | } |
| 3376 | |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 3377 | /* Jump instructions do not have r-values. |
| 3378 | */ |
| 3379 | return NULL; |
| 3380 | } |
Ian Romanick | 3c6fea3 | 2010-03-29 14:11:25 -0700 | [diff] [blame] | 3381 | |
| 3382 | |
| 3383 | ir_rvalue * |
| 3384 | ast_selection_statement::hir(exec_list *instructions, |
| 3385 | struct _mesa_glsl_parse_state *state) |
| 3386 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 3387 | void *ctx = state; |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 3388 | |
Ian Romanick | 3c6fea3 | 2010-03-29 14:11:25 -0700 | [diff] [blame] | 3389 | ir_rvalue *const condition = this->condition->hir(instructions, state); |
Ian Romanick | 3c6fea3 | 2010-03-29 14:11:25 -0700 | [diff] [blame] | 3390 | |
| 3391 | /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec: |
| 3392 | * |
| 3393 | * "Any expression whose type evaluates to a Boolean can be used as the |
| 3394 | * conditional expression bool-expression. Vector types are not accepted |
| 3395 | * as the expression to if." |
| 3396 | * |
| 3397 | * The checks are separated so that higher quality diagnostics can be |
| 3398 | * generated for cases where both rules are violated. |
| 3399 | */ |
| 3400 | if (!condition->type->is_boolean() || !condition->type->is_scalar()) { |
| 3401 | YYLTYPE loc = this->condition->get_location(); |
| 3402 | |
| 3403 | _mesa_glsl_error(& loc, state, "if-statement condition must be scalar " |
| 3404 | "boolean"); |
| 3405 | } |
| 3406 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 3407 | ir_if *const stmt = new(ctx) ir_if(condition); |
Ian Romanick | 3c6fea3 | 2010-03-29 14:11:25 -0700 | [diff] [blame] | 3408 | |
Kenneth Graunke | 665d75c | 2010-08-18 13:54:50 -0700 | [diff] [blame] | 3409 | if (then_statement != NULL) { |
| 3410 | state->symbols->push_scope(); |
Ian Romanick | 4f9d72f | 2010-05-10 11:10:26 -0700 | [diff] [blame] | 3411 | then_statement->hir(& stmt->then_instructions, state); |
Kenneth Graunke | 665d75c | 2010-08-18 13:54:50 -0700 | [diff] [blame] | 3412 | state->symbols->pop_scope(); |
| 3413 | } |
Ian Romanick | 3c6fea3 | 2010-03-29 14:11:25 -0700 | [diff] [blame] | 3414 | |
Kenneth Graunke | 665d75c | 2010-08-18 13:54:50 -0700 | [diff] [blame] | 3415 | if (else_statement != NULL) { |
| 3416 | state->symbols->push_scope(); |
Ian Romanick | 4f9d72f | 2010-05-10 11:10:26 -0700 | [diff] [blame] | 3417 | else_statement->hir(& stmt->else_instructions, state); |
Kenneth Graunke | 665d75c | 2010-08-18 13:54:50 -0700 | [diff] [blame] | 3418 | state->symbols->pop_scope(); |
| 3419 | } |
Ian Romanick | 3c6fea3 | 2010-03-29 14:11:25 -0700 | [diff] [blame] | 3420 | |
| 3421 | instructions->push_tail(stmt); |
| 3422 | |
| 3423 | /* if-statements do not have r-values. |
| 3424 | */ |
| 3425 | return NULL; |
| 3426 | } |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 3427 | |
| 3428 | |
Ian Romanick | 8c46ed2 | 2010-04-05 18:07:27 -0700 | [diff] [blame] | 3429 | void |
| 3430 | ast_iteration_statement::condition_to_hir(ir_loop *stmt, |
| 3431 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 3432 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 3433 | void *ctx = state; |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 3434 | |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 3435 | if (condition != NULL) { |
| 3436 | ir_rvalue *const cond = |
| 3437 | condition->hir(& stmt->body_instructions, state); |
| 3438 | |
| 3439 | if ((cond == NULL) |
| 3440 | || !cond->type->is_boolean() || !cond->type->is_scalar()) { |
| 3441 | YYLTYPE loc = condition->get_location(); |
| 3442 | |
| 3443 | _mesa_glsl_error(& loc, state, |
| 3444 | "loop condition must be scalar boolean"); |
| 3445 | } else { |
| 3446 | /* As the first code in the loop body, generate a block that looks |
| 3447 | * like 'if (!condition) break;' as the loop termination condition. |
| 3448 | */ |
| 3449 | ir_rvalue *const not_cond = |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 3450 | new(ctx) ir_expression(ir_unop_logic_not, glsl_type::bool_type, cond, |
| 3451 | NULL); |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 3452 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 3453 | ir_if *const if_stmt = new(ctx) ir_if(not_cond); |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 3454 | |
| 3455 | ir_jump *const break_stmt = |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 3456 | new(ctx) ir_loop_jump(ir_loop_jump::jump_break); |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 3457 | |
| 3458 | if_stmt->then_instructions.push_tail(break_stmt); |
| 3459 | stmt->body_instructions.push_tail(if_stmt); |
| 3460 | } |
| 3461 | } |
Ian Romanick | 8c46ed2 | 2010-04-05 18:07:27 -0700 | [diff] [blame] | 3462 | } |
| 3463 | |
| 3464 | |
| 3465 | ir_rvalue * |
| 3466 | ast_iteration_statement::hir(exec_list *instructions, |
| 3467 | struct _mesa_glsl_parse_state *state) |
| 3468 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 3469 | void *ctx = state; |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 3470 | |
Ian Romanick | 4846066 | 2010-04-16 16:42:43 -0700 | [diff] [blame] | 3471 | /* For-loops and while-loops start a new scope, but do-while loops do not. |
Ian Romanick | 8c46ed2 | 2010-04-05 18:07:27 -0700 | [diff] [blame] | 3472 | */ |
Ian Romanick | 4846066 | 2010-04-16 16:42:43 -0700 | [diff] [blame] | 3473 | if (mode != ast_do_while) |
Ian Romanick | 8c46ed2 | 2010-04-05 18:07:27 -0700 | [diff] [blame] | 3474 | state->symbols->push_scope(); |
| 3475 | |
| 3476 | if (init_statement != NULL) |
| 3477 | init_statement->hir(instructions, state); |
| 3478 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 3479 | ir_loop *const stmt = new(ctx) ir_loop(); |
Ian Romanick | 8c46ed2 | 2010-04-05 18:07:27 -0700 | [diff] [blame] | 3480 | instructions->push_tail(stmt); |
| 3481 | |
| 3482 | /* Track the current loop and / or switch-statement nesting. |
| 3483 | */ |
| 3484 | ir_instruction *const nesting = state->loop_or_switch_nesting; |
Eric Anholt | 2d1ed7b | 2010-07-22 12:55:16 -0700 | [diff] [blame] | 3485 | ast_iteration_statement *nesting_ast = state->loop_or_switch_nesting_ast; |
| 3486 | |
Ian Romanick | 8c46ed2 | 2010-04-05 18:07:27 -0700 | [diff] [blame] | 3487 | state->loop_or_switch_nesting = stmt; |
Eric Anholt | 2d1ed7b | 2010-07-22 12:55:16 -0700 | [diff] [blame] | 3488 | state->loop_or_switch_nesting_ast = this; |
Ian Romanick | 8c46ed2 | 2010-04-05 18:07:27 -0700 | [diff] [blame] | 3489 | |
| 3490 | if (mode != ast_do_while) |
| 3491 | condition_to_hir(stmt, state); |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 3492 | |
Ian Romanick | 4f9d72f | 2010-05-10 11:10:26 -0700 | [diff] [blame] | 3493 | if (body != NULL) |
| 3494 | body->hir(& stmt->body_instructions, state); |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 3495 | |
| 3496 | if (rest_expression != NULL) |
| 3497 | rest_expression->hir(& stmt->body_instructions, state); |
| 3498 | |
Ian Romanick | 8c46ed2 | 2010-04-05 18:07:27 -0700 | [diff] [blame] | 3499 | if (mode == ast_do_while) |
| 3500 | condition_to_hir(stmt, state); |
| 3501 | |
Ian Romanick | 4846066 | 2010-04-16 16:42:43 -0700 | [diff] [blame] | 3502 | if (mode != ast_do_while) |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 3503 | state->symbols->pop_scope(); |
| 3504 | |
Ian Romanick | e9d0f26 | 2010-04-05 17:01:53 -0700 | [diff] [blame] | 3505 | /* Restore previous nesting before returning. |
| 3506 | */ |
| 3507 | state->loop_or_switch_nesting = nesting; |
Eric Anholt | 2d1ed7b | 2010-07-22 12:55:16 -0700 | [diff] [blame] | 3508 | state->loop_or_switch_nesting_ast = nesting_ast; |
Ian Romanick | e9d0f26 | 2010-04-05 17:01:53 -0700 | [diff] [blame] | 3509 | |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 3510 | /* Loops do not have r-values. |
| 3511 | */ |
| 3512 | return NULL; |
| 3513 | } |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 3514 | |
| 3515 | |
| 3516 | ir_rvalue * |
| 3517 | ast_type_specifier::hir(exec_list *instructions, |
| 3518 | struct _mesa_glsl_parse_state *state) |
| 3519 | { |
Chad Versace | 08a286c | 2011-01-16 21:44:57 -0800 | [diff] [blame] | 3520 | if (!this->is_precision_statement && this->structure == NULL) |
| 3521 | return NULL; |
| 3522 | |
| 3523 | YYLTYPE loc = this->get_location(); |
| 3524 | |
| 3525 | if (this->precision != ast_precision_none |
| 3526 | && state->language_version != 100 |
| 3527 | && state->language_version < 130) { |
| 3528 | _mesa_glsl_error(&loc, state, |
| 3529 | "precision qualifiers exist only in " |
| 3530 | "GLSL ES 1.00, and GLSL 1.30 and later"); |
| 3531 | return NULL; |
| 3532 | } |
| 3533 | if (this->precision != ast_precision_none |
| 3534 | && this->structure != NULL) { |
| 3535 | _mesa_glsl_error(&loc, state, |
| 3536 | "precision qualifiers do not apply to structures"); |
| 3537 | return NULL; |
| 3538 | } |
| 3539 | |
| 3540 | /* If this is a precision statement, check that the type to which it is |
| 3541 | * applied is either float or int. |
| 3542 | * |
| 3543 | * From section 4.5.3 of the GLSL 1.30 spec: |
| 3544 | * "The precision statement |
| 3545 | * precision precision-qualifier type; |
| 3546 | * can be used to establish a default precision qualifier. The type |
| 3547 | * field can be either int or float [...]. Any other types or |
| 3548 | * qualifiers will result in an error. |
| 3549 | */ |
| 3550 | if (this->is_precision_statement) { |
| 3551 | assert(this->precision != ast_precision_none); |
| 3552 | assert(this->structure == NULL); /* The check for structures was |
| 3553 | * performed above. */ |
| 3554 | if (this->is_array) { |
| 3555 | _mesa_glsl_error(&loc, state, |
| 3556 | "default precision statements do not apply to " |
| 3557 | "arrays"); |
| 3558 | return NULL; |
| 3559 | } |
| 3560 | if (this->type_specifier != ast_float |
| 3561 | && this->type_specifier != ast_int) { |
| 3562 | _mesa_glsl_error(&loc, state, |
| 3563 | "default precision statements apply only to types " |
| 3564 | "float and int"); |
| 3565 | return NULL; |
| 3566 | } |
| 3567 | |
| 3568 | /* FINISHME: Translate precision statements into IR. */ |
| 3569 | return NULL; |
| 3570 | } |
| 3571 | |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 3572 | if (this->structure != NULL) |
| 3573 | return this->structure->hir(instructions, state); |
Ian Romanick | 85ba37b | 2010-04-21 14:33:34 -0700 | [diff] [blame] | 3574 | |
| 3575 | return NULL; |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 3576 | } |
| 3577 | |
| 3578 | |
| 3579 | ir_rvalue * |
| 3580 | ast_struct_specifier::hir(exec_list *instructions, |
| 3581 | struct _mesa_glsl_parse_state *state) |
| 3582 | { |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 3583 | unsigned decl_count = 0; |
| 3584 | |
| 3585 | /* Make an initial pass over the list of structure fields to determine how |
| 3586 | * many there are. Each element in this list is an ast_declarator_list. |
| 3587 | * This means that we actually need to count the number of elements in the |
| 3588 | * 'declarations' list in each of the elements. |
| 3589 | */ |
Ian Romanick | 2b97dc6 | 2010-05-10 17:42:05 -0700 | [diff] [blame] | 3590 | foreach_list_typed (ast_declarator_list, decl_list, link, |
| 3591 | &this->declarations) { |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 3592 | foreach_list_const (decl_ptr, & decl_list->declarations) { |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 3593 | decl_count++; |
| 3594 | } |
| 3595 | } |
| 3596 | |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 3597 | /* Allocate storage for the structure fields and process the field |
| 3598 | * declarations. As the declarations are processed, try to also convert |
| 3599 | * the types to HIR. This ensures that structure definitions embedded in |
| 3600 | * other structure definitions are processed. |
| 3601 | */ |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 3602 | glsl_struct_field *const fields = ralloc_array(state, glsl_struct_field, |
Eric Anholt | 21b0dbd | 2010-07-20 16:47:25 -0700 | [diff] [blame] | 3603 | decl_count); |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 3604 | |
| 3605 | unsigned i = 0; |
Ian Romanick | 2b97dc6 | 2010-05-10 17:42:05 -0700 | [diff] [blame] | 3606 | foreach_list_typed (ast_declarator_list, decl_list, link, |
| 3607 | &this->declarations) { |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 3608 | const char *type_name; |
| 3609 | |
| 3610 | decl_list->type->specifier->hir(instructions, state); |
| 3611 | |
Kenneth Graunke | c98deb1 | 2010-08-16 14:02:25 -0700 | [diff] [blame] | 3612 | /* Section 10.9 of the GLSL ES 1.00 specification states that |
| 3613 | * embedded structure definitions have been removed from the language. |
| 3614 | */ |
| 3615 | if (state->es_shader && decl_list->type->specifier->structure != NULL) { |
| 3616 | YYLTYPE loc = this->get_location(); |
| 3617 | _mesa_glsl_error(&loc, state, "Embedded structure definitions are " |
| 3618 | "not allowed in GLSL ES 1.00."); |
| 3619 | } |
| 3620 | |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 3621 | const glsl_type *decl_type = |
| 3622 | decl_list->type->specifier->glsl_type(& type_name, state); |
| 3623 | |
Ian Romanick | 2b97dc6 | 2010-05-10 17:42:05 -0700 | [diff] [blame] | 3624 | foreach_list_typed (ast_declaration, decl, link, |
| 3625 | &decl_list->declarations) { |
Kenneth Graunke | d8e34e2 | 2010-08-07 02:56:01 -0700 | [diff] [blame] | 3626 | const struct glsl_type *field_type = decl_type; |
| 3627 | if (decl->is_array) { |
| 3628 | YYLTYPE loc = decl->get_location(); |
| 3629 | field_type = process_array_type(&loc, decl_type, decl->array_size, |
| 3630 | state); |
| 3631 | } |
Ian Romanick | 73986a7 | 2010-04-20 16:49:03 -0700 | [diff] [blame] | 3632 | fields[i].type = (field_type != NULL) |
| 3633 | ? field_type : glsl_type::error_type; |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 3634 | fields[i].name = decl->identifier; |
| 3635 | i++; |
| 3636 | } |
| 3637 | } |
| 3638 | |
| 3639 | assert(i == decl_count); |
| 3640 | |
Ian Romanick | 49e3577 | 2010-06-28 11:54:57 -0700 | [diff] [blame] | 3641 | const glsl_type *t = |
Kenneth Graunke | ca92ae2 | 2010-09-18 11:11:09 +0200 | [diff] [blame] | 3642 | glsl_type::get_record_instance(fields, decl_count, this->name); |
Ian Romanick | 1d28b61 | 2010-04-20 16:48:24 -0700 | [diff] [blame] | 3643 | |
Ian Romanick | ab89927 | 2010-04-23 13:24:08 -0700 | [diff] [blame] | 3644 | YYLTYPE loc = this->get_location(); |
Ian Romanick | a789ca6 | 2010-09-01 14:08:08 -0700 | [diff] [blame] | 3645 | if (!state->symbols->add_type(name, t)) { |
Ian Romanick | ab89927 | 2010-04-23 13:24:08 -0700 | [diff] [blame] | 3646 | _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name); |
| 3647 | } else { |
Kenneth Graunke | eb63934 | 2011-02-27 01:17:29 -0800 | [diff] [blame] | 3648 | const glsl_type **s = reralloc(state, state->user_structures, |
| 3649 | const glsl_type *, |
| 3650 | state->num_user_structures + 1); |
Ian Romanick | a2c6df5 | 2010-04-28 13:14:53 -0700 | [diff] [blame] | 3651 | if (s != NULL) { |
| 3652 | s[state->num_user_structures] = t; |
| 3653 | state->user_structures = s; |
| 3654 | state->num_user_structures++; |
| 3655 | } |
Ian Romanick | ab89927 | 2010-04-23 13:24:08 -0700 | [diff] [blame] | 3656 | } |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 3657 | |
| 3658 | /* Structure type definitions do not have r-values. |
| 3659 | */ |
| 3660 | return NULL; |
| 3661 | } |