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 | */ |
| 51 | #include <stdio.h> |
| 52 | #include "main/imports.h" |
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 | { |
| 62 | struct simple_node *ptr; |
| 63 | |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 64 | _mesa_glsl_initialize_variables(instructions, state); |
Ian Romanick | a4e92c4 | 2010-03-25 17:02:22 -0700 | [diff] [blame] | 65 | _mesa_glsl_initialize_constructors(instructions, state); |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 66 | |
Ian Romanick | 41ec6a4 | 2010-03-19 17:08:05 -0700 | [diff] [blame] | 67 | state->current_function = NULL; |
| 68 | |
Ian Romanick | d949a9a | 2010-03-10 09:55:22 -0800 | [diff] [blame] | 69 | foreach (ptr, & state->translation_unit) { |
| 70 | ((ast_node *)ptr)->hir(instructions, state); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 75 | static const struct glsl_type * |
| 76 | arithmetic_result_type(const struct glsl_type *type_a, |
| 77 | const struct glsl_type *type_b, |
| 78 | bool multiply, |
| 79 | struct _mesa_glsl_parse_state *state) |
| 80 | { |
| 81 | /* From GLSL 1.50 spec, page 56: |
| 82 | * |
| 83 | * "The arithmetic binary operators add (+), subtract (-), |
| 84 | * multiply (*), and divide (/) operate on integer and |
| 85 | * floating-point scalars, vectors, and matrices." |
| 86 | */ |
Ian Romanick | 60b54d9 | 2010-03-24 17:08:13 -0700 | [diff] [blame] | 87 | if (!type_a->is_numeric() || !type_b->is_numeric()) { |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 88 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | |
| 92 | /* "If one operand is floating-point based and the other is |
| 93 | * not, then the conversions from Section 4.1.10 "Implicit |
| 94 | * Conversions" are applied to the non-floating-point-based operand." |
| 95 | * |
| 96 | * This conversion was added in GLSL 1.20. If the compilation mode is |
| 97 | * GLSL 1.10, the conversion is skipped. |
| 98 | */ |
| 99 | if (state->language_version >= 120) { |
| 100 | if ((type_a->base_type == GLSL_TYPE_FLOAT) |
| 101 | && (type_b->base_type != GLSL_TYPE_FLOAT)) { |
| 102 | } else if ((type_a->base_type != GLSL_TYPE_FLOAT) |
| 103 | && (type_b->base_type == GLSL_TYPE_FLOAT)) { |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /* "If the operands are integer types, they must both be signed or |
| 108 | * both be unsigned." |
| 109 | * |
| 110 | * From this rule and the preceeding conversion it can be inferred that |
| 111 | * 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] | 112 | * The is_numeric check above already filtered out the case where either |
| 113 | * type is not one of these, so now the base types need only be tested for |
| 114 | * equality. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 115 | */ |
| 116 | if (type_a->base_type != type_b->base_type) { |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 117 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /* "All arithmetic binary operators result in the same fundamental type |
| 121 | * (signed integer, unsigned integer, or floating-point) as the |
| 122 | * operands they operate on, after operand type conversion. After |
| 123 | * conversion, the following cases are valid |
| 124 | * |
| 125 | * * The two operands are scalars. In this case the operation is |
| 126 | * applied, resulting in a scalar." |
| 127 | */ |
Ian Romanick | cb36f8a | 2010-03-09 15:51:22 -0800 | [diff] [blame] | 128 | if (type_a->is_scalar() && type_b->is_scalar()) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 129 | return type_a; |
| 130 | |
| 131 | /* "* One operand is a scalar, and the other is a vector or matrix. |
| 132 | * In this case, the scalar operation is applied independently to each |
| 133 | * component of the vector or matrix, resulting in the same size |
| 134 | * vector or matrix." |
| 135 | */ |
Ian Romanick | cb36f8a | 2010-03-09 15:51:22 -0800 | [diff] [blame] | 136 | if (type_a->is_scalar()) { |
| 137 | if (!type_b->is_scalar()) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 138 | return type_b; |
Ian Romanick | cb36f8a | 2010-03-09 15:51:22 -0800 | [diff] [blame] | 139 | } else if (type_b->is_scalar()) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 140 | return type_a; |
| 141 | } |
| 142 | |
| 143 | /* All of the combinations of <scalar, scalar>, <vector, scalar>, |
| 144 | * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been |
| 145 | * handled. |
| 146 | */ |
Ian Romanick | 60b54d9 | 2010-03-24 17:08:13 -0700 | [diff] [blame] | 147 | assert(!type_a->is_scalar()); |
| 148 | assert(!type_b->is_scalar()); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 149 | |
| 150 | /* "* The two operands are vectors of the same size. In this case, the |
| 151 | * operation is done component-wise resulting in the same size |
| 152 | * vector." |
| 153 | */ |
Ian Romanick | a2dd22f | 2010-03-09 15:55:16 -0800 | [diff] [blame] | 154 | if (type_a->is_vector() && type_b->is_vector()) { |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 155 | return (type_a == type_b) ? type_a : glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /* All of the combinations of <scalar, scalar>, <vector, scalar>, |
| 159 | * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and |
| 160 | * <vector, vector> have been handled. At least one of the operands must |
| 161 | * be matrix. Further, since there are no integer matrix types, the base |
| 162 | * type of both operands must be float. |
| 163 | */ |
Ian Romanick | 60b54d9 | 2010-03-24 17:08:13 -0700 | [diff] [blame] | 164 | assert(type_a->is_matrix() || type_b->is_matrix()); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 165 | assert(type_a->base_type == GLSL_TYPE_FLOAT); |
| 166 | assert(type_b->base_type == GLSL_TYPE_FLOAT); |
| 167 | |
| 168 | /* "* The operator is add (+), subtract (-), or divide (/), and the |
| 169 | * operands are matrices with the same number of rows and the same |
| 170 | * number of columns. In this case, the operation is done component- |
| 171 | * wise resulting in the same size matrix." |
| 172 | * * The operator is multiply (*), where both operands are matrices or |
| 173 | * one operand is a vector and the other a matrix. A right vector |
| 174 | * operand is treated as a column vector and a left vector operand as a |
| 175 | * row vector. In all these cases, it is required that the number of |
| 176 | * columns of the left operand is equal to the number of rows of the |
| 177 | * right operand. Then, the multiply (*) operation does a linear |
| 178 | * algebraic multiply, yielding an object that has the same number of |
| 179 | * rows as the left operand and the same number of columns as the right |
| 180 | * operand. Section 5.10 "Vector and Matrix Operations" explains in |
| 181 | * more detail how vectors and matrices are operated on." |
| 182 | */ |
| 183 | if (! multiply) { |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 184 | return (type_a == type_b) ? type_a : glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 185 | } else { |
Ian Romanick | fce1150 | 2010-03-09 15:58:52 -0800 | [diff] [blame] | 186 | if (type_a->is_matrix() && type_b->is_matrix()) { |
Ian Romanick | c1bd3a1 | 2010-03-25 13:06:58 -0700 | [diff] [blame] | 187 | /* Matrix multiply. The columns of A must match the rows of B. Given |
| 188 | * the other previously tested constraints, this means the vector type |
| 189 | * of a row from A must be the same as the vector type of a column from |
| 190 | * B. |
| 191 | */ |
| 192 | if (type_a->row_type() == type_b->column_type()) { |
| 193 | /* The resulting matrix has the number of columns of matrix B and |
| 194 | * the number of rows of matrix A. We get the row count of A by |
| 195 | * looking at the size of a vector that makes up a column. The |
| 196 | * transpose (size of a row) is done for B. |
| 197 | */ |
| 198 | return |
| 199 | glsl_type::get_instance(type_a->base_type, |
| 200 | type_a->column_type()->vector_elements, |
| 201 | type_b->row_type()->vector_elements); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 202 | } |
Ian Romanick | fce1150 | 2010-03-09 15:58:52 -0800 | [diff] [blame] | 203 | } else if (type_a->is_matrix()) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 204 | /* 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] | 205 | * rows of B. Given the other previously tested constraints, this |
| 206 | * means the vector type of a row from A must be the same as the |
| 207 | * vector the type of B. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 208 | */ |
Ian Romanick | c1bd3a1 | 2010-03-25 13:06:58 -0700 | [diff] [blame] | 209 | if (type_a->row_type() == type_b) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 210 | return type_b; |
| 211 | } else { |
Ian Romanick | fce1150 | 2010-03-09 15:58:52 -0800 | [diff] [blame] | 212 | assert(type_b->is_matrix()); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 213 | |
Ian Romanick | c1bd3a1 | 2010-03-25 13:06:58 -0700 | [diff] [blame] | 214 | /* A is a row vector and B is a matrix. Columns of A must match rows |
| 215 | * of B. Given the other previously tested constraints, this means |
| 216 | * the type of A must be the same as the vector type of a column from |
| 217 | * B. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 218 | */ |
Ian Romanick | c1bd3a1 | 2010-03-25 13:06:58 -0700 | [diff] [blame] | 219 | if (type_a == type_b->column_type()) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 220 | return type_a; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | |
| 225 | /* "All other cases are illegal." |
| 226 | */ |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 227 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | |
| 231 | static const struct glsl_type * |
| 232 | unary_arithmetic_result_type(const struct glsl_type *type) |
| 233 | { |
| 234 | /* From GLSL 1.50 spec, page 57: |
| 235 | * |
| 236 | * "The arithmetic unary operators negate (-), post- and pre-increment |
| 237 | * and decrement (-- and ++) operate on integer or floating-point |
| 238 | * values (including vectors and matrices). All unary operators work |
| 239 | * component-wise on their operands. These result with the same type |
| 240 | * they operated on." |
| 241 | */ |
Ian Romanick | a6d653d | 2010-03-26 14:40:37 -0700 | [diff] [blame] | 242 | if (!type->is_numeric()) |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 243 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 244 | |
| 245 | return type; |
| 246 | } |
| 247 | |
| 248 | |
| 249 | static const struct glsl_type * |
| 250 | modulus_result_type(const struct glsl_type *type_a, |
| 251 | const struct glsl_type *type_b) |
| 252 | { |
| 253 | /* From GLSL 1.50 spec, page 56: |
| 254 | * "The operator modulus (%) operates on signed or unsigned integers or |
| 255 | * integer vectors. The operand types must both be signed or both be |
| 256 | * unsigned." |
| 257 | */ |
Ian Romanick | 40176e2 | 2010-03-26 14:38:37 -0700 | [diff] [blame] | 258 | if (!type_a->is_integer() || !type_b->is_integer() |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 259 | || (type_a->base_type != type_b->base_type)) { |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 260 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /* "The operands cannot be vectors of differing size. If one operand is |
| 264 | * a scalar and the other vector, then the scalar is applied component- |
| 265 | * wise to the vector, resulting in the same type as the vector. If both |
| 266 | * are vectors of the same size, the result is computed component-wise." |
| 267 | */ |
Ian Romanick | a2dd22f | 2010-03-09 15:55:16 -0800 | [diff] [blame] | 268 | if (type_a->is_vector()) { |
| 269 | if (!type_b->is_vector() |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 270 | || (type_a->vector_elements == type_b->vector_elements)) |
| 271 | return type_a; |
| 272 | } else |
| 273 | return type_b; |
| 274 | |
| 275 | /* "The operator modulus (%) is not defined for any other data types |
| 276 | * (non-integer types)." |
| 277 | */ |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 278 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | |
| 282 | static const struct glsl_type * |
| 283 | relational_result_type(const struct glsl_type *type_a, |
| 284 | const struct glsl_type *type_b, |
| 285 | struct _mesa_glsl_parse_state *state) |
| 286 | { |
| 287 | /* From GLSL 1.50 spec, page 56: |
| 288 | * "The relational operators greater than (>), less than (<), greater |
| 289 | * than or equal (>=), and less than or equal (<=) operate only on |
| 290 | * scalar integer and scalar floating-point expressions." |
| 291 | */ |
Ian Romanick | a6d653d | 2010-03-26 14:40:37 -0700 | [diff] [blame] | 292 | if (!type_a->is_numeric() |
| 293 | || !type_b->is_numeric() |
Ian Romanick | cb36f8a | 2010-03-09 15:51:22 -0800 | [diff] [blame] | 294 | || !type_a->is_scalar() |
| 295 | || !type_b->is_scalar()) |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 296 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 297 | |
| 298 | /* "Either the operands' types must match, or the conversions from |
| 299 | * Section 4.1.10 "Implicit Conversions" will be applied to the integer |
| 300 | * operand, after which the types must match." |
| 301 | * |
| 302 | * This conversion was added in GLSL 1.20. If the compilation mode is |
| 303 | * GLSL 1.10, the conversion is skipped. |
| 304 | */ |
| 305 | if (state->language_version >= 120) { |
| 306 | if ((type_a->base_type == GLSL_TYPE_FLOAT) |
| 307 | && (type_b->base_type != GLSL_TYPE_FLOAT)) { |
| 308 | /* FINISHME: Generate the implicit type conversion. */ |
| 309 | } else if ((type_a->base_type != GLSL_TYPE_FLOAT) |
| 310 | && (type_b->base_type == GLSL_TYPE_FLOAT)) { |
| 311 | /* FINISHME: Generate the implicit type conversion. */ |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | if (type_a->base_type != type_b->base_type) |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 316 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 317 | |
| 318 | /* "The result is scalar Boolean." |
| 319 | */ |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 320 | return glsl_type::bool_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 324 | /** |
| 325 | * Validates that a value can be assigned to a location with a specified type |
| 326 | * |
| 327 | * Validates that \c rhs can be assigned to some location. If the types are |
| 328 | * not an exact match but an automatic conversion is possible, \c rhs will be |
| 329 | * converted. |
| 330 | * |
| 331 | * \return |
| 332 | * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type. |
| 333 | * Otherwise the actual RHS to be assigned will be returned. This may be |
| 334 | * \c rhs, or it may be \c rhs after some type conversion. |
| 335 | * |
| 336 | * \note |
| 337 | * In addition to being used for assignments, this function is used to |
| 338 | * type-check return values. |
| 339 | */ |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 340 | ir_rvalue * |
| 341 | validate_assignment(const glsl_type *lhs_type, ir_rvalue *rhs) |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 342 | { |
| 343 | const glsl_type *const rhs_type = rhs->type; |
| 344 | |
| 345 | /* If there is already some error in the RHS, just return it. Anything |
| 346 | * else will lead to an avalanche of error message back to the user. |
| 347 | */ |
| 348 | if (rhs_type->is_error()) |
| 349 | return rhs; |
| 350 | |
| 351 | /* FINISHME: For GLSL 1.10, check that the types are not arrays. */ |
| 352 | |
| 353 | /* If the types are identical, the assignment can trivially proceed. |
| 354 | */ |
| 355 | if (rhs_type == lhs_type) |
| 356 | return rhs; |
| 357 | |
| 358 | /* FINISHME: Check for and apply automatic conversions. */ |
| 359 | return NULL; |
| 360 | } |
| 361 | |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 362 | ir_rvalue * |
| 363 | do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state, |
| 364 | ir_rvalue *lhs, ir_rvalue *rhs, |
| 365 | YYLTYPE lhs_loc) |
| 366 | { |
| 367 | bool error_emitted = (lhs->type->is_error() || rhs->type->is_error()); |
| 368 | |
| 369 | if (!error_emitted) { |
| 370 | /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */ |
| 371 | if (!lhs->is_lvalue()) { |
| 372 | _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment"); |
| 373 | error_emitted = true; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | ir_rvalue *new_rhs = validate_assignment(lhs->type, rhs); |
| 378 | if (new_rhs == NULL) { |
| 379 | _mesa_glsl_error(& lhs_loc, state, "type mismatch"); |
| 380 | } else { |
| 381 | rhs = new_rhs; |
| 382 | } |
| 383 | |
| 384 | ir_instruction *tmp = new ir_assignment(lhs, rhs, NULL); |
| 385 | instructions->push_tail(tmp); |
| 386 | |
| 387 | return rhs; |
| 388 | } |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 389 | |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 390 | static ir_rvalue * |
| 391 | get_lvalue_copy(exec_list *instructions, struct _mesa_glsl_parse_state *state, |
| 392 | ir_rvalue *lvalue, YYLTYPE loc) |
| 393 | { |
| 394 | ir_variable *var; |
| 395 | ir_rvalue *var_deref; |
| 396 | |
| 397 | /* FINISHME: Give unique names to the temporaries. */ |
| 398 | var = new ir_variable(lvalue->type, "_internal_tmp"); |
| 399 | var->mode = ir_var_auto; |
| 400 | |
| 401 | var_deref = new ir_dereference(var); |
| 402 | do_assignment(instructions, state, var_deref, lvalue, loc); |
| 403 | |
| 404 | /* Once we've created this temporary, mark it read only so it's no |
| 405 | * longer considered an lvalue. |
| 406 | */ |
| 407 | var->read_only = true; |
| 408 | |
| 409 | return var_deref; |
| 410 | } |
| 411 | |
| 412 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 413 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 414 | ast_node::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 415 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 416 | { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 417 | (void) instructions; |
| 418 | (void) state; |
| 419 | |
| 420 | return NULL; |
| 421 | } |
| 422 | |
| 423 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 424 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 425 | ast_expression::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 426 | struct _mesa_glsl_parse_state *state) |
| 427 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 428 | static const int operations[AST_NUM_OPERATORS] = { |
| 429 | -1, /* ast_assign doesn't convert to ir_expression. */ |
| 430 | -1, /* ast_plus doesn't convert to ir_expression. */ |
| 431 | ir_unop_neg, |
| 432 | ir_binop_add, |
| 433 | ir_binop_sub, |
| 434 | ir_binop_mul, |
| 435 | ir_binop_div, |
| 436 | ir_binop_mod, |
| 437 | ir_binop_lshift, |
| 438 | ir_binop_rshift, |
| 439 | ir_binop_less, |
| 440 | ir_binop_greater, |
| 441 | ir_binop_lequal, |
| 442 | ir_binop_gequal, |
| 443 | ir_binop_equal, |
| 444 | ir_binop_nequal, |
| 445 | ir_binop_bit_and, |
| 446 | ir_binop_bit_xor, |
| 447 | ir_binop_bit_or, |
| 448 | ir_unop_bit_not, |
| 449 | ir_binop_logic_and, |
| 450 | ir_binop_logic_xor, |
| 451 | ir_binop_logic_or, |
| 452 | ir_unop_logic_not, |
| 453 | |
| 454 | /* Note: The following block of expression types actually convert |
| 455 | * to multiple IR instructions. |
| 456 | */ |
| 457 | ir_binop_mul, /* ast_mul_assign */ |
| 458 | ir_binop_div, /* ast_div_assign */ |
| 459 | ir_binop_mod, /* ast_mod_assign */ |
| 460 | ir_binop_add, /* ast_add_assign */ |
| 461 | ir_binop_sub, /* ast_sub_assign */ |
| 462 | ir_binop_lshift, /* ast_ls_assign */ |
| 463 | ir_binop_rshift, /* ast_rs_assign */ |
| 464 | ir_binop_bit_and, /* ast_and_assign */ |
| 465 | ir_binop_bit_xor, /* ast_xor_assign */ |
| 466 | ir_binop_bit_or, /* ast_or_assign */ |
| 467 | |
| 468 | -1, /* ast_conditional doesn't convert to ir_expression. */ |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 469 | ir_binop_add, /* ast_pre_inc. */ |
| 470 | ir_binop_sub, /* ast_pre_dec. */ |
| 471 | ir_binop_add, /* ast_post_inc. */ |
| 472 | ir_binop_sub, /* ast_post_dec. */ |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 473 | -1, /* ast_field_selection doesn't conv to ir_expression. */ |
| 474 | -1, /* ast_array_index doesn't convert to ir_expression. */ |
| 475 | -1, /* ast_function_call doesn't conv to ir_expression. */ |
| 476 | -1, /* ast_identifier doesn't convert to ir_expression. */ |
| 477 | -1, /* ast_int_constant doesn't convert to ir_expression. */ |
| 478 | -1, /* ast_uint_constant doesn't conv to ir_expression. */ |
| 479 | -1, /* ast_float_constant doesn't conv to ir_expression. */ |
| 480 | -1, /* ast_bool_constant doesn't conv to ir_expression. */ |
| 481 | -1, /* ast_sequence doesn't convert to ir_expression. */ |
| 482 | }; |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 483 | ir_rvalue *result = NULL; |
| 484 | ir_rvalue *op[2]; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 485 | struct simple_node op_list; |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 486 | const struct glsl_type *type = glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 487 | bool error_emitted = false; |
| 488 | YYLTYPE loc; |
| 489 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 490 | loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 491 | make_empty_list(& op_list); |
| 492 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 493 | switch (this->oper) { |
Ian Romanick | 6652af3 | 2010-03-09 16:38:02 -0800 | [diff] [blame] | 494 | case ast_assign: { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 495 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 496 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 497 | |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 498 | result = do_assignment(instructions, state, op[0], op[1], |
| 499 | this->subexpressions[0]->get_location()); |
| 500 | error_emitted = result->type->is_error(); |
| 501 | type = result->type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 502 | break; |
Ian Romanick | 6652af3 | 2010-03-09 16:38:02 -0800 | [diff] [blame] | 503 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 504 | |
| 505 | case ast_plus: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 506 | op[0] = this->subexpressions[0]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 507 | |
Ian Romanick | a43817a | 2010-03-26 14:27:23 -0700 | [diff] [blame] | 508 | error_emitted = op[0]->type->is_error(); |
| 509 | if (type->is_error()) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 510 | op[0]->type = type; |
| 511 | |
| 512 | result = op[0]; |
| 513 | break; |
| 514 | |
| 515 | case ast_neg: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 516 | op[0] = this->subexpressions[0]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 517 | |
| 518 | type = unary_arithmetic_result_type(op[0]->type); |
| 519 | |
Ian Romanick | a43817a | 2010-03-26 14:27:23 -0700 | [diff] [blame] | 520 | error_emitted = op[0]->type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 521 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 522 | result = new ir_expression(operations[this->oper], type, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 523 | op[0], NULL); |
| 524 | break; |
| 525 | |
| 526 | case ast_add: |
| 527 | case ast_sub: |
| 528 | case ast_mul: |
| 529 | case ast_div: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 530 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 531 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 532 | |
| 533 | type = arithmetic_result_type(op[0]->type, op[1]->type, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 534 | (this->oper == ast_mul), |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 535 | state); |
| 536 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 537 | result = new ir_expression(operations[this->oper], type, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 538 | op[0], op[1]); |
| 539 | break; |
| 540 | |
| 541 | case ast_mod: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 542 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 543 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 544 | |
Ian Romanick | a43817a | 2010-03-26 14:27:23 -0700 | [diff] [blame] | 545 | error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 546 | |
| 547 | type = modulus_result_type(op[0]->type, op[1]->type); |
| 548 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 549 | assert(operations[this->oper] == ir_binop_mod); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 550 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 551 | result = new ir_expression(operations[this->oper], type, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 552 | op[0], op[1]); |
| 553 | break; |
| 554 | |
| 555 | case ast_lshift: |
| 556 | case ast_rshift: |
| 557 | /* FINISHME: Implement bit-shift operators. */ |
| 558 | break; |
| 559 | |
| 560 | case ast_less: |
| 561 | case ast_greater: |
| 562 | case ast_lequal: |
| 563 | case ast_gequal: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 564 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 565 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 566 | |
Ian Romanick | a43817a | 2010-03-26 14:27:23 -0700 | [diff] [blame] | 567 | error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 568 | |
| 569 | type = relational_result_type(op[0]->type, op[1]->type, state); |
| 570 | |
| 571 | /* The relational operators must either generate an error or result |
| 572 | * in a scalar boolean. See page 57 of the GLSL 1.50 spec. |
| 573 | */ |
Ian Romanick | a43817a | 2010-03-26 14:27:23 -0700 | [diff] [blame] | 574 | assert(type->is_error() |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 575 | || ((type->base_type == GLSL_TYPE_BOOL) |
Ian Romanick | cb36f8a | 2010-03-09 15:51:22 -0800 | [diff] [blame] | 576 | && type->is_scalar())); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 577 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 578 | result = new ir_expression(operations[this->oper], type, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 579 | op[0], op[1]); |
| 580 | break; |
| 581 | |
| 582 | case ast_nequal: |
| 583 | case ast_equal: |
| 584 | /* FINISHME: Implement equality operators. */ |
| 585 | break; |
| 586 | |
| 587 | case ast_bit_and: |
| 588 | case ast_bit_xor: |
| 589 | case ast_bit_or: |
| 590 | case ast_bit_not: |
| 591 | /* FINISHME: Implement bit-wise operators. */ |
| 592 | break; |
| 593 | |
| 594 | case ast_logic_and: |
| 595 | case ast_logic_xor: |
| 596 | case ast_logic_or: |
| 597 | case ast_logic_not: |
| 598 | /* FINISHME: Implement logical operators. */ |
| 599 | break; |
| 600 | |
| 601 | case ast_mul_assign: |
| 602 | case ast_div_assign: |
| 603 | case ast_add_assign: |
| 604 | case ast_sub_assign: { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 605 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 606 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 607 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 608 | type = arithmetic_result_type(op[0]->type, op[1]->type, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 609 | (this->oper == ast_mul_assign), |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 610 | state); |
| 611 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 612 | ir_rvalue *temp_rhs = new ir_expression(operations[this->oper], type, |
| 613 | op[0], op[1]); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 614 | |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 615 | result = do_assignment(instructions, state, op[0], temp_rhs, |
| 616 | this->subexpressions[0]->get_location()); |
| 617 | type = result->type; |
| 618 | error_emitted = (op[0]->type->is_error()); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 619 | |
| 620 | /* GLSL 1.10 does not allow array assignment. However, we don't have to |
| 621 | * explicitly test for this because none of the binary expression |
| 622 | * operators allow array operands either. |
| 623 | */ |
| 624 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 625 | break; |
| 626 | } |
| 627 | |
Eric Anholt | 48a0e64 | 2010-03-26 11:57:46 -0700 | [diff] [blame] | 628 | case ast_mod_assign: { |
| 629 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 630 | op[1] = this->subexpressions[1]->hir(instructions, state); |
| 631 | |
| 632 | error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); |
| 633 | |
| 634 | type = modulus_result_type(op[0]->type, op[1]->type); |
| 635 | |
| 636 | assert(operations[this->oper] == ir_binop_mod); |
| 637 | |
| 638 | struct ir_rvalue *temp_rhs; |
| 639 | temp_rhs = new ir_expression(operations[this->oper], type, |
| 640 | op[0], op[1]); |
| 641 | |
| 642 | result = do_assignment(instructions, state, op[0], temp_rhs, |
| 643 | this->subexpressions[0]->get_location()); |
| 644 | type = result->type; |
| 645 | error_emitted = op[0]->type->is_error(); |
| 646 | break; |
| 647 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 648 | |
| 649 | case ast_ls_assign: |
| 650 | case ast_rs_assign: |
| 651 | |
| 652 | case ast_and_assign: |
| 653 | case ast_xor_assign: |
| 654 | case ast_or_assign: |
| 655 | |
| 656 | case ast_conditional: |
| 657 | |
| 658 | case ast_pre_inc: |
Eric Anholt | 76ea56c | 2010-03-26 12:16:54 -0700 | [diff] [blame] | 659 | case ast_pre_dec: { |
| 660 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 661 | if (op[0]->type->base_type == GLSL_TYPE_FLOAT) |
| 662 | op[1] = new ir_constant(1.0f); |
| 663 | else |
| 664 | op[1] = new ir_constant(1); |
| 665 | |
| 666 | type = arithmetic_result_type(op[0]->type, op[1]->type, |
| 667 | false, state); |
| 668 | |
| 669 | struct ir_rvalue *temp_rhs; |
| 670 | temp_rhs = new ir_expression(operations[this->oper], type, |
| 671 | op[0], op[1]); |
| 672 | |
| 673 | result = do_assignment(instructions, state, op[0], temp_rhs, |
| 674 | this->subexpressions[0]->get_location()); |
| 675 | type = result->type; |
| 676 | error_emitted = op[0]->type->is_error(); |
| 677 | break; |
| 678 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 679 | |
| 680 | case ast_post_inc: |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 681 | case ast_post_dec: { |
| 682 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 683 | if (op[0]->type->base_type == GLSL_TYPE_FLOAT) |
| 684 | op[1] = new ir_constant(1.0f); |
| 685 | else |
| 686 | op[1] = new ir_constant(1); |
| 687 | |
| 688 | error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); |
| 689 | |
| 690 | type = arithmetic_result_type(op[0]->type, op[1]->type, |
| 691 | false, state); |
| 692 | |
| 693 | struct ir_rvalue *temp_rhs; |
| 694 | temp_rhs = new ir_expression(operations[this->oper], type, |
| 695 | op[0], op[1]); |
| 696 | |
| 697 | /* Get a temporary of a copy of the lvalue before it's modified. |
| 698 | * This may get thrown away later. |
| 699 | */ |
| 700 | result = get_lvalue_copy(instructions, state, op[0], |
| 701 | this->subexpressions[0]->get_location()); |
| 702 | |
| 703 | (void)do_assignment(instructions, state, op[0], temp_rhs, |
| 704 | this->subexpressions[0]->get_location()); |
| 705 | |
| 706 | type = result->type; |
| 707 | error_emitted = op[0]->type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 708 | break; |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 709 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 710 | |
| 711 | case ast_field_selection: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 712 | result = _mesa_ast_field_selection_to_hir(this, instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 713 | type = result->type; |
| 714 | break; |
| 715 | |
| 716 | case ast_array_index: |
| 717 | break; |
| 718 | |
| 719 | case ast_function_call: |
Ian Romanick | 7cfddf1 | 2010-03-10 13:26:52 -0800 | [diff] [blame] | 720 | /* Should *NEVER* get here. ast_function_call should always be handled |
| 721 | * by ast_function_expression::hir. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 722 | */ |
Ian Romanick | 7cfddf1 | 2010-03-10 13:26:52 -0800 | [diff] [blame] | 723 | assert(0); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 724 | break; |
| 725 | |
| 726 | case ast_identifier: { |
| 727 | /* ast_identifier can appear several places in a full abstract syntax |
| 728 | * tree. This particular use must be at location specified in the grammar |
| 729 | * as 'variable_identifier'. |
| 730 | */ |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 731 | ir_variable *var = |
| 732 | state->symbols->get_variable(this->primary_expression.identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 733 | |
| 734 | result = new ir_dereference(var); |
| 735 | |
| 736 | if (var != NULL) { |
| 737 | type = result->type; |
| 738 | } else { |
Ian Romanick | 71d0bbf | 2010-03-23 13:21:19 -0700 | [diff] [blame] | 739 | _mesa_glsl_error(& loc, state, "`%s' undeclared", |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 740 | this->primary_expression.identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 741 | |
| 742 | error_emitted = true; |
| 743 | } |
| 744 | break; |
| 745 | } |
| 746 | |
| 747 | case ast_int_constant: |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 748 | type = glsl_type::int_type; |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 749 | result = new ir_constant(type, & this->primary_expression); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 750 | break; |
| 751 | |
| 752 | case ast_uint_constant: |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 753 | type = glsl_type::uint_type; |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 754 | result = new ir_constant(type, & this->primary_expression); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 755 | break; |
| 756 | |
| 757 | case ast_float_constant: |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 758 | type = glsl_type::float_type; |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 759 | result = new ir_constant(type, & this->primary_expression); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 760 | break; |
| 761 | |
| 762 | case ast_bool_constant: |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 763 | type = glsl_type::bool_type; |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 764 | result = new ir_constant(type, & this->primary_expression); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 765 | break; |
| 766 | |
| 767 | case ast_sequence: { |
| 768 | struct simple_node *ptr; |
| 769 | |
| 770 | /* It should not be possible to generate a sequence in the AST without |
| 771 | * any expressions in it. |
| 772 | */ |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 773 | assert(!is_empty_list(&this->expressions)); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 774 | |
| 775 | /* The r-value of a sequence is the last expression in the sequence. If |
| 776 | * the other expressions in the sequence do not have side-effects (and |
| 777 | * therefore add instructions to the instruction list), they get dropped |
| 778 | * on the floor. |
| 779 | */ |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 780 | foreach (ptr, &this->expressions) |
| 781 | result = ((ast_node *)ptr)->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 782 | |
| 783 | type = result->type; |
| 784 | |
| 785 | /* Any errors should have already been emitted in the loop above. |
| 786 | */ |
| 787 | error_emitted = true; |
| 788 | break; |
| 789 | } |
| 790 | } |
| 791 | |
Ian Romanick | cef3bae | 2010-03-26 14:41:32 -0700 | [diff] [blame] | 792 | if (type->is_error() && !error_emitted) |
Ian Romanick | 71d0bbf | 2010-03-23 13:21:19 -0700 | [diff] [blame] | 793 | _mesa_glsl_error(& loc, state, "type mismatch"); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 794 | |
| 795 | return result; |
| 796 | } |
| 797 | |
| 798 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 799 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 800 | ast_expression_statement::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 801 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 802 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 803 | /* It is possible to have expression statements that don't have an |
| 804 | * expression. This is the solitary semicolon: |
| 805 | * |
| 806 | * for (i = 0; i < 5; i++) |
| 807 | * ; |
| 808 | * |
| 809 | * In this case the expression will be NULL. Test for NULL and don't do |
| 810 | * anything in that case. |
| 811 | */ |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 812 | if (expression != NULL) |
| 813 | expression->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 814 | |
| 815 | /* Statements do not have r-values. |
| 816 | */ |
| 817 | return NULL; |
| 818 | } |
| 819 | |
| 820 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 821 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 822 | ast_compound_statement::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 823 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 824 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 825 | struct simple_node *ptr; |
| 826 | |
| 827 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 828 | if (new_scope) |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 829 | state->symbols->push_scope(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 830 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 831 | foreach (ptr, &statements) |
| 832 | ((ast_node *)ptr)->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 833 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 834 | if (new_scope) |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 835 | state->symbols->pop_scope(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 836 | |
| 837 | /* Compound statements do not have r-values. |
| 838 | */ |
| 839 | return NULL; |
| 840 | } |
| 841 | |
| 842 | |
| 843 | static const struct glsl_type * |
| 844 | type_specifier_to_glsl_type(const struct ast_type_specifier *spec, |
| 845 | const char **name, |
| 846 | struct _mesa_glsl_parse_state *state) |
| 847 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 848 | struct glsl_type *type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 849 | |
| 850 | if (spec->type_specifier == ast_struct) { |
| 851 | /* FINISHME: Handle annonymous structures. */ |
| 852 | type = NULL; |
| 853 | } else { |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 854 | type = state->symbols->get_type(spec->type_name); |
Ian Romanick | 7f9d309 | 2010-03-15 14:15:15 -0700 | [diff] [blame] | 855 | *name = spec->type_name; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 856 | |
| 857 | /* FINISHME: Handle array declarations. Note that this requires complete |
Ian Romanick | f3f111e | 2010-03-23 13:04:19 -0700 | [diff] [blame] | 858 | * FINISHME: handling of constant expressions. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 859 | */ |
| 860 | } |
| 861 | |
| 862 | return type; |
| 863 | } |
| 864 | |
| 865 | |
| 866 | static void |
| 867 | apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual, |
| 868 | struct ir_variable *var, |
| 869 | struct _mesa_glsl_parse_state *state) |
| 870 | { |
| 871 | if (qual->invariant) |
| 872 | var->invariant = 1; |
| 873 | |
| 874 | /* FINISHME: Mark 'in' variables at global scope as read-only. */ |
| 875 | if (qual->constant || qual->attribute || qual->uniform |
| 876 | || (qual->varying && (state->target == fragment_shader))) |
| 877 | var->read_only = 1; |
| 878 | |
| 879 | if (qual->centroid) |
| 880 | var->centroid = 1; |
| 881 | |
| 882 | if (qual->in && qual->out) |
| 883 | var->mode = ir_var_inout; |
| 884 | else if (qual->attribute || qual->in |
| 885 | || (qual->varying && (state->target == fragment_shader))) |
| 886 | var->mode = ir_var_in; |
Ian Romanick | 0b67823 | 2010-03-10 00:28:59 -0800 | [diff] [blame] | 887 | else if (qual->out || (qual->varying && (state->target == vertex_shader))) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 888 | var->mode = ir_var_out; |
| 889 | else if (qual->uniform) |
| 890 | var->mode = ir_var_uniform; |
| 891 | else |
| 892 | var->mode = ir_var_auto; |
| 893 | |
| 894 | if (qual->flat) |
| 895 | var->interpolation = ir_var_flat; |
| 896 | else if (qual->noperspective) |
| 897 | var->interpolation = ir_var_noperspective; |
| 898 | else |
| 899 | var->interpolation = ir_var_smooth; |
| 900 | } |
| 901 | |
| 902 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 903 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 904 | ast_declarator_list::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 905 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 906 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 907 | struct simple_node *ptr; |
| 908 | const struct glsl_type *decl_type; |
| 909 | const char *type_name = NULL; |
| 910 | |
| 911 | |
| 912 | /* FINISHME: Handle vertex shader "invariant" declarations that do not |
| 913 | * FINISHME: include a type. These re-declare built-in variables to be |
| 914 | * FINISHME: invariant. |
| 915 | */ |
| 916 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 917 | decl_type = type_specifier_to_glsl_type(this->type->specifier, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 918 | & type_name, state); |
| 919 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 920 | foreach (ptr, &this->declarations) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 921 | struct ast_declaration *const decl = (struct ast_declaration * )ptr; |
| 922 | const struct glsl_type *var_type; |
| 923 | struct ir_variable *var; |
| 924 | |
| 925 | |
| 926 | /* FINISHME: Emit a warning if a variable declaration shadows a |
| 927 | * FINISHME: declaration at a higher scope. |
| 928 | */ |
| 929 | |
Ian Romanick | cec65a6 | 2010-03-23 12:28:44 -0700 | [diff] [blame] | 930 | if ((decl_type == NULL) || decl_type->is_void()) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 931 | YYLTYPE loc; |
| 932 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 933 | loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 934 | if (type_name != NULL) { |
| 935 | _mesa_glsl_error(& loc, state, |
| 936 | "invalid type `%s' in declaration of `%s'", |
| 937 | type_name, decl->identifier); |
| 938 | } else { |
| 939 | _mesa_glsl_error(& loc, state, |
| 940 | "invalid type in declaration of `%s'", |
| 941 | decl->identifier); |
| 942 | } |
| 943 | continue; |
| 944 | } |
| 945 | |
| 946 | if (decl->is_array) { |
| 947 | /* FINISHME: Handle array declarations. Note that this requires |
| 948 | * FINISHME: complete handling of constant expressions. |
| 949 | */ |
Eric Anholt | 8518e75 | 2010-03-26 16:37:22 -0700 | [diff] [blame] | 950 | var_type = glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 951 | |
| 952 | /* FINISHME: Reject delcarations of multidimensional arrays. */ |
| 953 | } else { |
| 954 | var_type = decl_type; |
| 955 | } |
| 956 | |
| 957 | var = new ir_variable(var_type, decl->identifier); |
| 958 | |
Ian Romanick | f3f111e | 2010-03-23 13:04:19 -0700 | [diff] [blame] | 959 | /* FINISHME: Variables that are attribute, uniform, varying, in, or |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 960 | * FINISHME: out varibles must be declared either at global scope or |
| 961 | * FINISHME: in a parameter list (in and out only). |
| 962 | */ |
| 963 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 964 | apply_type_qualifier_to_variable(& this->type->qualifier, var, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 965 | |
| 966 | /* Attempt to add the variable to the symbol table. If this fails, it |
| 967 | * means the variable has already been declared at this scope. |
| 968 | */ |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 969 | if (state->symbols->name_declared_this_scope(decl->identifier)) { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 970 | YYLTYPE loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 971 | |
| 972 | _mesa_glsl_error(& loc, state, "`%s' redeclared", |
| 973 | decl->identifier); |
| 974 | continue; |
| 975 | } |
| 976 | |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 977 | const bool added_variable = |
| 978 | state->symbols->add_variable(decl->identifier, var); |
| 979 | assert(added_variable); |
| 980 | |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 981 | instructions->push_tail(var); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 982 | |
Ian Romanick | 66faec4 | 2010-03-27 18:56:53 -0700 | [diff] [blame] | 983 | if (decl->initializer != NULL) { |
Ian Romanick | 43de172 | 2010-03-28 17:03:16 -0700 | [diff] [blame^] | 984 | YYLTYPE initializer_loc = decl->initializer->get_location(); |
| 985 | |
Ian Romanick | 66faec4 | 2010-03-27 18:56:53 -0700 | [diff] [blame] | 986 | /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec: |
| 987 | * |
| 988 | * "All uniform variables are read-only and are initialized either |
| 989 | * directly by an application via API commands, or indirectly by |
| 990 | * OpenGL." |
| 991 | */ |
| 992 | if ((state->language_version <= 110) |
| 993 | && (var->mode == ir_var_uniform)) { |
Ian Romanick | 43de172 | 2010-03-28 17:03:16 -0700 | [diff] [blame^] | 994 | _mesa_glsl_error(& initializer_loc, state, |
| 995 | "cannot initialize uniforms in GLSL 1.10"); |
| 996 | } |
Ian Romanick | 1936015 | 2010-03-26 18:05:27 -0700 | [diff] [blame] | 997 | |
Ian Romanick | 43de172 | 2010-03-28 17:03:16 -0700 | [diff] [blame^] | 998 | if (var->type->is_sampler()) { |
| 999 | _mesa_glsl_error(& initializer_loc, state, |
| 1000 | "cannot initialize samplers"); |
| 1001 | } |
| 1002 | |
| 1003 | if ((var->mode == ir_var_in) && (state->current_function == NULL)) { |
| 1004 | _mesa_glsl_error(& initializer_loc, state, |
| 1005 | "cannot initialize %s shader input / %s", |
| 1006 | (state->target == vertex_shader) |
| 1007 | ? "vertex" : "fragment", |
| 1008 | (state->target == vertex_shader) |
| 1009 | ? "attribute" : "varying"); |
Ian Romanick | 66faec4 | 2010-03-27 18:56:53 -0700 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | ir_dereference *const lhs = new ir_dereference(var); |
| 1013 | ir_rvalue *const rhs = decl->initializer->hir(instructions, state); |
| 1014 | |
| 1015 | /* FINISHME: If the declaration is either 'const' or 'uniform', the |
| 1016 | * FINISHME: initializer (rhs) must be a constant expression. |
| 1017 | */ |
| 1018 | |
| 1019 | if (!rhs->type->is_error()) { |
| 1020 | (void) do_assignment(instructions, state, lhs, rhs, |
| 1021 | this->get_location()); |
| 1022 | } |
Ian Romanick | 1936015 | 2010-03-26 18:05:27 -0700 | [diff] [blame] | 1023 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | /* Variable declarations do not have r-values. |
| 1027 | */ |
| 1028 | return NULL; |
| 1029 | } |
| 1030 | |
| 1031 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 1032 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1033 | ast_parameter_declarator::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1034 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1035 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1036 | const struct glsl_type *type; |
| 1037 | const char *name = NULL; |
| 1038 | |
| 1039 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1040 | type = type_specifier_to_glsl_type(this->type->specifier, & name, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1041 | |
| 1042 | if (type == NULL) { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1043 | YYLTYPE loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1044 | if (name != NULL) { |
| 1045 | _mesa_glsl_error(& loc, state, |
| 1046 | "invalid type `%s' in declaration of `%s'", |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1047 | name, this->identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1048 | } else { |
| 1049 | _mesa_glsl_error(& loc, state, |
| 1050 | "invalid type in declaration of `%s'", |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1051 | this->identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1052 | } |
| 1053 | |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 1054 | type = glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1055 | } |
| 1056 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1057 | ir_variable *var = new ir_variable(type, this->identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1058 | |
| 1059 | /* FINISHME: Handle array declarations. Note that this requires |
| 1060 | * FINISHME: complete handling of constant expressions. |
| 1061 | */ |
| 1062 | |
Ian Romanick | cdb8d54 | 2010-03-11 14:48:51 -0800 | [diff] [blame] | 1063 | /* Apply any specified qualifiers to the parameter declaration. Note that |
| 1064 | * for function parameters the default mode is 'in'. |
| 1065 | */ |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1066 | apply_type_qualifier_to_variable(& this->type->qualifier, var, state); |
Ian Romanick | cdb8d54 | 2010-03-11 14:48:51 -0800 | [diff] [blame] | 1067 | if (var->mode == ir_var_auto) |
| 1068 | var->mode = ir_var_in; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1069 | |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1070 | instructions->push_tail(var); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1071 | |
| 1072 | /* Parameter declarations do not have r-values. |
| 1073 | */ |
| 1074 | return NULL; |
| 1075 | } |
| 1076 | |
| 1077 | |
| 1078 | static void |
| 1079 | ast_function_parameters_to_hir(struct simple_node *ast_parameters, |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1080 | exec_list *ir_parameters, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1081 | struct _mesa_glsl_parse_state *state) |
| 1082 | { |
| 1083 | struct simple_node *ptr; |
| 1084 | |
| 1085 | foreach (ptr, ast_parameters) { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1086 | ((ast_node *)ptr)->hir(ir_parameters, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | |
| 1091 | static bool |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1092 | parameter_lists_match(exec_list *list_a, exec_list *list_b) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1093 | { |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1094 | exec_list_iterator iter_a = list_a->iterator(); |
| 1095 | exec_list_iterator iter_b = list_b->iterator(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1096 | |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1097 | while (iter_a.has_next()) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1098 | /* If all of the parameters from the other parameter list have been |
| 1099 | * exhausted, the lists have different length and, by definition, |
| 1100 | * do not match. |
| 1101 | */ |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1102 | if (!iter_b.has_next()) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1103 | return false; |
| 1104 | |
| 1105 | /* If the types of the parameters do not match, the parameters lists |
| 1106 | * are different. |
| 1107 | */ |
| 1108 | /* FINISHME */ |
| 1109 | |
| 1110 | |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1111 | iter_a.next(); |
| 1112 | iter_b.next(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | return true; |
| 1116 | } |
| 1117 | |
| 1118 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 1119 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1120 | ast_function_definition::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1121 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1122 | { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1123 | ir_label *label; |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1124 | ir_function_signature *signature = NULL; |
| 1125 | ir_function *f = NULL; |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1126 | exec_list parameters; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1127 | |
| 1128 | |
| 1129 | /* Convert the list of function parameters to HIR now so that they can be |
| 1130 | * used below to compare this function's signature with previously seen |
| 1131 | * signatures for functions with the same name. |
| 1132 | */ |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1133 | ast_function_parameters_to_hir(& this->prototype->parameters, & parameters, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1134 | state); |
| 1135 | |
Ian Romanick | e39cc69 | 2010-03-23 12:19:13 -0700 | [diff] [blame] | 1136 | const char *return_type_name; |
| 1137 | const glsl_type *return_type = |
| 1138 | type_specifier_to_glsl_type(this->prototype->return_type->specifier, |
| 1139 | & return_type_name, state); |
| 1140 | |
| 1141 | assert(return_type != NULL); |
| 1142 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1143 | |
| 1144 | /* Verify that this function's signature either doesn't match a previously |
| 1145 | * seen signature for a function with the same name, or, if a match is found, |
| 1146 | * that the previously seen signature does not have an associated definition. |
| 1147 | */ |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 1148 | const char *const name = this->prototype->identifier; |
| 1149 | f = state->symbols->get_function(name); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1150 | if (f != NULL) { |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1151 | foreach_iter(exec_list_iterator, iter, f->signatures) { |
| 1152 | signature = (struct ir_function_signature *) iter.get(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1153 | |
| 1154 | /* Compare the parameter list of the function being defined to the |
| 1155 | * existing function. If the parameter lists match, then the return |
| 1156 | * type must also match and the existing function must not have a |
| 1157 | * definition. |
| 1158 | */ |
| 1159 | if (parameter_lists_match(& parameters, & signature->parameters)) { |
| 1160 | /* FINISHME: Compare return types. */ |
| 1161 | |
| 1162 | if (signature->definition != NULL) { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1163 | YYLTYPE loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1164 | |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 1165 | _mesa_glsl_error(& loc, state, "function `%s' redefined", name); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1166 | signature = NULL; |
| 1167 | break; |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | signature = NULL; |
| 1172 | } |
| 1173 | |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 1174 | } else if (state->symbols->name_declared_this_scope(name)) { |
| 1175 | /* This function name shadows a non-function use of the same name. |
| 1176 | */ |
| 1177 | YYLTYPE loc = this->get_location(); |
| 1178 | |
| 1179 | _mesa_glsl_error(& loc, state, "function name `%s' conflicts with " |
| 1180 | "non-function", name); |
| 1181 | signature = NULL; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1182 | } else { |
Ian Romanick | 882dad7 | 2010-03-23 17:42:04 -0700 | [diff] [blame] | 1183 | f = new ir_function(name); |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 1184 | state->symbols->add_function(f->name, f); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | |
| 1188 | /* Finish storing the information about this new function in its signature. |
| 1189 | */ |
| 1190 | if (signature == NULL) { |
Ian Romanick | e39cc69 | 2010-03-23 12:19:13 -0700 | [diff] [blame] | 1191 | signature = new ir_function_signature(return_type); |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1192 | f->signatures.push_tail(signature); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1193 | } else { |
| 1194 | /* Destroy all of the previous parameter information. The previous |
| 1195 | * parameter information comes from the function prototype, and it can |
| 1196 | * either include invalid parameter names or may not have names at all. |
| 1197 | */ |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1198 | foreach_iter(exec_list_iterator, iter, signature->parameters) { |
Kenneth Graunke | 44e1dfa | 2010-03-25 23:30:28 -0700 | [diff] [blame] | 1199 | assert(((ir_instruction *) iter.get())->as_variable() != NULL); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1200 | |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1201 | iter.remove(); |
| 1202 | delete iter.get(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | |
Ian Romanick | 41ec6a4 | 2010-03-19 17:08:05 -0700 | [diff] [blame] | 1207 | assert(state->current_function == NULL); |
| 1208 | state->current_function = signature; |
| 1209 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1210 | ast_function_parameters_to_hir(& this->prototype->parameters, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1211 | & signature->parameters, |
| 1212 | state); |
| 1213 | /* FINISHME: Set signature->return_type */ |
| 1214 | |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 1215 | label = new ir_label(name); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1216 | if (signature->definition == NULL) { |
| 1217 | signature->definition = label; |
| 1218 | } |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1219 | instructions->push_tail(label); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1220 | |
| 1221 | /* Add the function parameters to the symbol table. During this step the |
| 1222 | * parameter declarations are also moved from the temporary "parameters" list |
| 1223 | * to the instruction list. There are other more efficient ways to do this, |
| 1224 | * but they involve ugly linked-list gymnastics. |
| 1225 | */ |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 1226 | state->symbols->push_scope(); |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1227 | foreach_iter(exec_list_iterator, iter, parameters) { |
| 1228 | ir_variable *const var = (ir_variable *) iter.get(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1229 | |
Kenneth Graunke | 44e1dfa | 2010-03-25 23:30:28 -0700 | [diff] [blame] | 1230 | assert(((ir_instruction *) var)->as_variable() != NULL); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1231 | |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1232 | iter.remove(); |
| 1233 | instructions->push_tail(var); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1234 | |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 1235 | /* The only way a parameter would "exist" is if two parameters have |
| 1236 | * the same name. |
| 1237 | */ |
| 1238 | if (state->symbols->name_declared_this_scope(var->name)) { |
| 1239 | YYLTYPE loc = this->get_location(); |
| 1240 | |
| 1241 | _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name); |
| 1242 | } else { |
| 1243 | state->symbols->add_variable(var->name, var); |
| 1244 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | /* Convert the body of the function to HIR, and append the resulting |
| 1248 | * instructions to the list that currently consists of the function label |
| 1249 | * and the function parameters. |
| 1250 | */ |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1251 | this->body->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1252 | |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 1253 | state->symbols->pop_scope(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1254 | |
Ian Romanick | 41ec6a4 | 2010-03-19 17:08:05 -0700 | [diff] [blame] | 1255 | assert(state->current_function == signature); |
| 1256 | state->current_function = NULL; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1257 | |
| 1258 | /* Function definitions do not have r-values. |
| 1259 | */ |
| 1260 | return NULL; |
| 1261 | } |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 1262 | |
| 1263 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 1264 | ir_rvalue * |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 1265 | ast_jump_statement::hir(exec_list *instructions, |
| 1266 | struct _mesa_glsl_parse_state *state) |
| 1267 | { |
| 1268 | |
| 1269 | if (mode == ast_return) { |
| 1270 | ir_return *inst; |
| 1271 | |
| 1272 | if (opt_return_value) { |
| 1273 | /* FINISHME: Make sure the enclosing function has a non-void return |
| 1274 | * FINISHME: type. |
| 1275 | */ |
| 1276 | |
| 1277 | ir_expression *const ret = (ir_expression *) |
| 1278 | opt_return_value->hir(instructions, state); |
| 1279 | assert(ret != NULL); |
| 1280 | |
| 1281 | /* FINISHME: Make sure the type of the return value matches the return |
| 1282 | * FINISHME: type of the enclosing function. |
| 1283 | */ |
| 1284 | |
| 1285 | inst = new ir_return(ret); |
| 1286 | } else { |
| 1287 | /* FINISHME: Make sure the enclosing function has a void return type. |
| 1288 | */ |
| 1289 | inst = new ir_return; |
| 1290 | } |
| 1291 | |
| 1292 | instructions->push_tail(inst); |
| 1293 | } |
| 1294 | |
| 1295 | /* Jump instructions do not have r-values. |
| 1296 | */ |
| 1297 | return NULL; |
| 1298 | } |