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 | |
| 362 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 363 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 364 | ast_node::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 365 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 366 | { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 367 | (void) instructions; |
| 368 | (void) state; |
| 369 | |
| 370 | return NULL; |
| 371 | } |
| 372 | |
| 373 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 374 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 375 | ast_expression::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 376 | struct _mesa_glsl_parse_state *state) |
| 377 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 378 | static const int operations[AST_NUM_OPERATORS] = { |
| 379 | -1, /* ast_assign doesn't convert to ir_expression. */ |
| 380 | -1, /* ast_plus doesn't convert to ir_expression. */ |
| 381 | ir_unop_neg, |
| 382 | ir_binop_add, |
| 383 | ir_binop_sub, |
| 384 | ir_binop_mul, |
| 385 | ir_binop_div, |
| 386 | ir_binop_mod, |
| 387 | ir_binop_lshift, |
| 388 | ir_binop_rshift, |
| 389 | ir_binop_less, |
| 390 | ir_binop_greater, |
| 391 | ir_binop_lequal, |
| 392 | ir_binop_gequal, |
| 393 | ir_binop_equal, |
| 394 | ir_binop_nequal, |
| 395 | ir_binop_bit_and, |
| 396 | ir_binop_bit_xor, |
| 397 | ir_binop_bit_or, |
| 398 | ir_unop_bit_not, |
| 399 | ir_binop_logic_and, |
| 400 | ir_binop_logic_xor, |
| 401 | ir_binop_logic_or, |
| 402 | ir_unop_logic_not, |
| 403 | |
| 404 | /* Note: The following block of expression types actually convert |
| 405 | * to multiple IR instructions. |
| 406 | */ |
| 407 | ir_binop_mul, /* ast_mul_assign */ |
| 408 | ir_binop_div, /* ast_div_assign */ |
| 409 | ir_binop_mod, /* ast_mod_assign */ |
| 410 | ir_binop_add, /* ast_add_assign */ |
| 411 | ir_binop_sub, /* ast_sub_assign */ |
| 412 | ir_binop_lshift, /* ast_ls_assign */ |
| 413 | ir_binop_rshift, /* ast_rs_assign */ |
| 414 | ir_binop_bit_and, /* ast_and_assign */ |
| 415 | ir_binop_bit_xor, /* ast_xor_assign */ |
| 416 | ir_binop_bit_or, /* ast_or_assign */ |
| 417 | |
| 418 | -1, /* ast_conditional doesn't convert to ir_expression. */ |
| 419 | -1, /* ast_pre_inc doesn't convert to ir_expression. */ |
| 420 | -1, /* ast_pre_dec doesn't convert to ir_expression. */ |
| 421 | -1, /* ast_post_inc doesn't convert to ir_expression. */ |
| 422 | -1, /* ast_post_dec doesn't convert to ir_expression. */ |
| 423 | -1, /* ast_field_selection doesn't conv to ir_expression. */ |
| 424 | -1, /* ast_array_index doesn't convert to ir_expression. */ |
| 425 | -1, /* ast_function_call doesn't conv to ir_expression. */ |
| 426 | -1, /* ast_identifier doesn't convert to ir_expression. */ |
| 427 | -1, /* ast_int_constant doesn't convert to ir_expression. */ |
| 428 | -1, /* ast_uint_constant doesn't conv to ir_expression. */ |
| 429 | -1, /* ast_float_constant doesn't conv to ir_expression. */ |
| 430 | -1, /* ast_bool_constant doesn't conv to ir_expression. */ |
| 431 | -1, /* ast_sequence doesn't convert to ir_expression. */ |
| 432 | }; |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 433 | ir_rvalue *result = NULL; |
| 434 | ir_rvalue *op[2]; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 435 | struct simple_node op_list; |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 436 | const struct glsl_type *type = glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 437 | bool error_emitted = false; |
| 438 | YYLTYPE loc; |
| 439 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 440 | loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 441 | make_empty_list(& op_list); |
| 442 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 443 | switch (this->oper) { |
Ian Romanick | 6652af3 | 2010-03-09 16:38:02 -0800 | [diff] [blame] | 444 | case ast_assign: { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 445 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 446 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 447 | |
Ian Romanick | a43817a | 2010-03-26 14:27:23 -0700 | [diff] [blame] | 448 | error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 449 | |
| 450 | type = op[0]->type; |
| 451 | if (!error_emitted) { |
| 452 | YYLTYPE loc; |
| 453 | |
| 454 | /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */ |
Kenneth Graunke | affc141 | 2010-03-26 01:20:08 -0700 | [diff] [blame] | 455 | if (!op[0]->is_lvalue()) { |
| 456 | _mesa_glsl_error(& loc, state, "non-lvalue in assignment"); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 457 | error_emitted = true; |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 458 | type = glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 462 | ir_instruction *rhs = validate_assignment(op[0]->type, op[1]); |
| 463 | if (rhs == NULL) { |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 464 | type = glsl_type::error_type; |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 465 | rhs = op[1]; |
| 466 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 467 | |
Ian Romanick | 6652af3 | 2010-03-09 16:38:02 -0800 | [diff] [blame] | 468 | ir_instruction *tmp = new ir_assignment(op[0], op[1], NULL); |
| 469 | instructions->push_tail(tmp); |
| 470 | |
| 471 | result = op[0]; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 472 | break; |
Ian Romanick | 6652af3 | 2010-03-09 16:38:02 -0800 | [diff] [blame] | 473 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 474 | |
| 475 | case ast_plus: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 476 | op[0] = this->subexpressions[0]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 477 | |
Ian Romanick | a43817a | 2010-03-26 14:27:23 -0700 | [diff] [blame] | 478 | error_emitted = op[0]->type->is_error(); |
| 479 | if (type->is_error()) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 480 | op[0]->type = type; |
| 481 | |
| 482 | result = op[0]; |
| 483 | break; |
| 484 | |
| 485 | case ast_neg: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 486 | op[0] = this->subexpressions[0]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 487 | |
| 488 | type = unary_arithmetic_result_type(op[0]->type); |
| 489 | |
Ian Romanick | a43817a | 2010-03-26 14:27:23 -0700 | [diff] [blame] | 490 | error_emitted = op[0]->type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 491 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 492 | result = new ir_expression(operations[this->oper], type, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 493 | op[0], NULL); |
| 494 | break; |
| 495 | |
| 496 | case ast_add: |
| 497 | case ast_sub: |
| 498 | case ast_mul: |
| 499 | case ast_div: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 500 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 501 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 502 | |
| 503 | type = arithmetic_result_type(op[0]->type, op[1]->type, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 504 | (this->oper == ast_mul), |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 505 | state); |
| 506 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 507 | result = new ir_expression(operations[this->oper], type, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 508 | op[0], op[1]); |
| 509 | break; |
| 510 | |
| 511 | case ast_mod: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 512 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 513 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 514 | |
Ian Romanick | a43817a | 2010-03-26 14:27:23 -0700 | [diff] [blame] | 515 | error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 516 | |
| 517 | type = modulus_result_type(op[0]->type, op[1]->type); |
| 518 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 519 | assert(operations[this->oper] == ir_binop_mod); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 520 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 521 | result = new ir_expression(operations[this->oper], type, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 522 | op[0], op[1]); |
| 523 | break; |
| 524 | |
| 525 | case ast_lshift: |
| 526 | case ast_rshift: |
| 527 | /* FINISHME: Implement bit-shift operators. */ |
| 528 | break; |
| 529 | |
| 530 | case ast_less: |
| 531 | case ast_greater: |
| 532 | case ast_lequal: |
| 533 | case ast_gequal: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 534 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 535 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 536 | |
Ian Romanick | a43817a | 2010-03-26 14:27:23 -0700 | [diff] [blame] | 537 | error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 538 | |
| 539 | type = relational_result_type(op[0]->type, op[1]->type, state); |
| 540 | |
| 541 | /* The relational operators must either generate an error or result |
| 542 | * in a scalar boolean. See page 57 of the GLSL 1.50 spec. |
| 543 | */ |
Ian Romanick | a43817a | 2010-03-26 14:27:23 -0700 | [diff] [blame] | 544 | assert(type->is_error() |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 545 | || ((type->base_type == GLSL_TYPE_BOOL) |
Ian Romanick | cb36f8a | 2010-03-09 15:51:22 -0800 | [diff] [blame] | 546 | && type->is_scalar())); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 547 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 548 | result = new ir_expression(operations[this->oper], type, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 549 | op[0], op[1]); |
| 550 | break; |
| 551 | |
| 552 | case ast_nequal: |
| 553 | case ast_equal: |
| 554 | /* FINISHME: Implement equality operators. */ |
| 555 | break; |
| 556 | |
| 557 | case ast_bit_and: |
| 558 | case ast_bit_xor: |
| 559 | case ast_bit_or: |
| 560 | case ast_bit_not: |
| 561 | /* FINISHME: Implement bit-wise operators. */ |
| 562 | break; |
| 563 | |
| 564 | case ast_logic_and: |
| 565 | case ast_logic_xor: |
| 566 | case ast_logic_or: |
| 567 | case ast_logic_not: |
| 568 | /* FINISHME: Implement logical operators. */ |
| 569 | break; |
| 570 | |
| 571 | case ast_mul_assign: |
| 572 | case ast_div_assign: |
| 573 | case ast_add_assign: |
| 574 | case ast_sub_assign: { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 575 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 576 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 577 | |
Ian Romanick | a43817a | 2010-03-26 14:27:23 -0700 | [diff] [blame] | 578 | error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 579 | |
| 580 | type = arithmetic_result_type(op[0]->type, op[1]->type, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 581 | (this->oper == ast_mul_assign), |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 582 | state); |
| 583 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 584 | ir_rvalue *temp_rhs = new ir_expression(operations[this->oper], type, |
| 585 | op[0], op[1]); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 586 | |
Eric Anholt | ece0a51 | 2010-03-25 17:02:15 -0700 | [diff] [blame] | 587 | /* FINISHME: This is copied from ast_assign above. It should |
| 588 | * FINISHME: probably be consolidated. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 589 | */ |
Ian Romanick | a43817a | 2010-03-26 14:27:23 -0700 | [diff] [blame] | 590 | error_emitted = op[0]->type->is_error() || temp_rhs->type->is_error(); |
Eric Anholt | ece0a51 | 2010-03-25 17:02:15 -0700 | [diff] [blame] | 591 | |
| 592 | type = op[0]->type; |
| 593 | if (!error_emitted) { |
| 594 | YYLTYPE loc; |
| 595 | |
Kenneth Graunke | affc141 | 2010-03-26 01:20:08 -0700 | [diff] [blame] | 596 | if (!op[0]->is_lvalue()) { |
| 597 | _mesa_glsl_error(& loc, state, "non-lvalue in assignment"); |
Eric Anholt | ece0a51 | 2010-03-25 17:02:15 -0700 | [diff] [blame] | 598 | error_emitted = true; |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 599 | type = glsl_type::error_type; |
Eric Anholt | ece0a51 | 2010-03-25 17:02:15 -0700 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 603 | ir_rvalue *rhs = validate_assignment(op[0]->type, temp_rhs); |
Eric Anholt | ece0a51 | 2010-03-25 17:02:15 -0700 | [diff] [blame] | 604 | if (rhs == NULL) { |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 605 | type = glsl_type::error_type; |
Eric Anholt | ece0a51 | 2010-03-25 17:02:15 -0700 | [diff] [blame] | 606 | rhs = temp_rhs; |
| 607 | } |
| 608 | |
| 609 | ir_instruction *tmp = new ir_assignment(op[0], rhs, NULL); |
| 610 | instructions->push_tail(tmp); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 611 | |
| 612 | /* GLSL 1.10 does not allow array assignment. However, we don't have to |
| 613 | * explicitly test for this because none of the binary expression |
| 614 | * operators allow array operands either. |
| 615 | */ |
| 616 | |
Eric Anholt | ece0a51 | 2010-03-25 17:02:15 -0700 | [diff] [blame] | 617 | result = op[0]; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 618 | break; |
| 619 | } |
| 620 | |
| 621 | case ast_mod_assign: |
| 622 | |
| 623 | case ast_ls_assign: |
| 624 | case ast_rs_assign: |
| 625 | |
| 626 | case ast_and_assign: |
| 627 | case ast_xor_assign: |
| 628 | case ast_or_assign: |
| 629 | |
| 630 | case ast_conditional: |
| 631 | |
| 632 | case ast_pre_inc: |
| 633 | case ast_pre_dec: |
| 634 | |
| 635 | case ast_post_inc: |
| 636 | case ast_post_dec: |
| 637 | break; |
| 638 | |
| 639 | case ast_field_selection: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 640 | result = _mesa_ast_field_selection_to_hir(this, instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 641 | type = result->type; |
| 642 | break; |
| 643 | |
| 644 | case ast_array_index: |
| 645 | break; |
| 646 | |
| 647 | case ast_function_call: |
Ian Romanick | 7cfddf1 | 2010-03-10 13:26:52 -0800 | [diff] [blame] | 648 | /* Should *NEVER* get here. ast_function_call should always be handled |
| 649 | * by ast_function_expression::hir. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 650 | */ |
Ian Romanick | 7cfddf1 | 2010-03-10 13:26:52 -0800 | [diff] [blame] | 651 | assert(0); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 652 | break; |
| 653 | |
| 654 | case ast_identifier: { |
| 655 | /* ast_identifier can appear several places in a full abstract syntax |
| 656 | * tree. This particular use must be at location specified in the grammar |
| 657 | * as 'variable_identifier'. |
| 658 | */ |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 659 | ir_variable *var = |
| 660 | state->symbols->get_variable(this->primary_expression.identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 661 | |
| 662 | result = new ir_dereference(var); |
| 663 | |
| 664 | if (var != NULL) { |
| 665 | type = result->type; |
| 666 | } else { |
Ian Romanick | 71d0bbf | 2010-03-23 13:21:19 -0700 | [diff] [blame] | 667 | _mesa_glsl_error(& loc, state, "`%s' undeclared", |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 668 | this->primary_expression.identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 669 | |
| 670 | error_emitted = true; |
| 671 | } |
| 672 | break; |
| 673 | } |
| 674 | |
| 675 | case ast_int_constant: |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 676 | type = glsl_type::int_type; |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 677 | result = new ir_constant(type, & this->primary_expression); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 678 | break; |
| 679 | |
| 680 | case ast_uint_constant: |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 681 | type = glsl_type::uint_type; |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 682 | result = new ir_constant(type, & this->primary_expression); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 683 | break; |
| 684 | |
| 685 | case ast_float_constant: |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 686 | type = glsl_type::float_type; |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 687 | result = new ir_constant(type, & this->primary_expression); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 688 | break; |
| 689 | |
| 690 | case ast_bool_constant: |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 691 | type = glsl_type::bool_type; |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 692 | result = new ir_constant(type, & this->primary_expression); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 693 | break; |
| 694 | |
| 695 | case ast_sequence: { |
| 696 | struct simple_node *ptr; |
| 697 | |
| 698 | /* It should not be possible to generate a sequence in the AST without |
| 699 | * any expressions in it. |
| 700 | */ |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 701 | assert(!is_empty_list(&this->expressions)); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 702 | |
| 703 | /* The r-value of a sequence is the last expression in the sequence. If |
| 704 | * the other expressions in the sequence do not have side-effects (and |
| 705 | * therefore add instructions to the instruction list), they get dropped |
| 706 | * on the floor. |
| 707 | */ |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 708 | foreach (ptr, &this->expressions) |
| 709 | result = ((ast_node *)ptr)->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 710 | |
| 711 | type = result->type; |
| 712 | |
| 713 | /* Any errors should have already been emitted in the loop above. |
| 714 | */ |
| 715 | error_emitted = true; |
| 716 | break; |
| 717 | } |
| 718 | } |
| 719 | |
Ian Romanick | cef3bae | 2010-03-26 14:41:32 -0700 | [diff] [blame^] | 720 | if (type->is_error() && !error_emitted) |
Ian Romanick | 71d0bbf | 2010-03-23 13:21:19 -0700 | [diff] [blame] | 721 | _mesa_glsl_error(& loc, state, "type mismatch"); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 722 | |
| 723 | return result; |
| 724 | } |
| 725 | |
| 726 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 727 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 728 | ast_expression_statement::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 729 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 730 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 731 | /* It is possible to have expression statements that don't have an |
| 732 | * expression. This is the solitary semicolon: |
| 733 | * |
| 734 | * for (i = 0; i < 5; i++) |
| 735 | * ; |
| 736 | * |
| 737 | * In this case the expression will be NULL. Test for NULL and don't do |
| 738 | * anything in that case. |
| 739 | */ |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 740 | if (expression != NULL) |
| 741 | expression->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 742 | |
| 743 | /* Statements do not have r-values. |
| 744 | */ |
| 745 | return NULL; |
| 746 | } |
| 747 | |
| 748 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 749 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 750 | ast_compound_statement::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 751 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 752 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 753 | struct simple_node *ptr; |
| 754 | |
| 755 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 756 | if (new_scope) |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 757 | state->symbols->push_scope(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 758 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 759 | foreach (ptr, &statements) |
| 760 | ((ast_node *)ptr)->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 761 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 762 | if (new_scope) |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 763 | state->symbols->pop_scope(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 764 | |
| 765 | /* Compound statements do not have r-values. |
| 766 | */ |
| 767 | return NULL; |
| 768 | } |
| 769 | |
| 770 | |
| 771 | static const struct glsl_type * |
| 772 | type_specifier_to_glsl_type(const struct ast_type_specifier *spec, |
| 773 | const char **name, |
| 774 | struct _mesa_glsl_parse_state *state) |
| 775 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 776 | struct glsl_type *type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 777 | |
| 778 | if (spec->type_specifier == ast_struct) { |
| 779 | /* FINISHME: Handle annonymous structures. */ |
| 780 | type = NULL; |
| 781 | } else { |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 782 | type = state->symbols->get_type(spec->type_name); |
Ian Romanick | 7f9d309 | 2010-03-15 14:15:15 -0700 | [diff] [blame] | 783 | *name = spec->type_name; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 784 | |
| 785 | /* FINISHME: Handle array declarations. Note that this requires complete |
Ian Romanick | f3f111e | 2010-03-23 13:04:19 -0700 | [diff] [blame] | 786 | * FINISHME: handling of constant expressions. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 787 | */ |
| 788 | } |
| 789 | |
| 790 | return type; |
| 791 | } |
| 792 | |
| 793 | |
| 794 | static void |
| 795 | apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual, |
| 796 | struct ir_variable *var, |
| 797 | struct _mesa_glsl_parse_state *state) |
| 798 | { |
| 799 | if (qual->invariant) |
| 800 | var->invariant = 1; |
| 801 | |
| 802 | /* FINISHME: Mark 'in' variables at global scope as read-only. */ |
| 803 | if (qual->constant || qual->attribute || qual->uniform |
| 804 | || (qual->varying && (state->target == fragment_shader))) |
| 805 | var->read_only = 1; |
| 806 | |
| 807 | if (qual->centroid) |
| 808 | var->centroid = 1; |
| 809 | |
| 810 | if (qual->in && qual->out) |
| 811 | var->mode = ir_var_inout; |
| 812 | else if (qual->attribute || qual->in |
| 813 | || (qual->varying && (state->target == fragment_shader))) |
| 814 | var->mode = ir_var_in; |
Ian Romanick | 0b67823 | 2010-03-10 00:28:59 -0800 | [diff] [blame] | 815 | else if (qual->out || (qual->varying && (state->target == vertex_shader))) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 816 | var->mode = ir_var_out; |
| 817 | else if (qual->uniform) |
| 818 | var->mode = ir_var_uniform; |
| 819 | else |
| 820 | var->mode = ir_var_auto; |
| 821 | |
| 822 | if (qual->flat) |
| 823 | var->interpolation = ir_var_flat; |
| 824 | else if (qual->noperspective) |
| 825 | var->interpolation = ir_var_noperspective; |
| 826 | else |
| 827 | var->interpolation = ir_var_smooth; |
| 828 | } |
| 829 | |
| 830 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 831 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 832 | ast_declarator_list::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 833 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 834 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 835 | struct simple_node *ptr; |
| 836 | const struct glsl_type *decl_type; |
| 837 | const char *type_name = NULL; |
| 838 | |
| 839 | |
| 840 | /* FINISHME: Handle vertex shader "invariant" declarations that do not |
| 841 | * FINISHME: include a type. These re-declare built-in variables to be |
| 842 | * FINISHME: invariant. |
| 843 | */ |
| 844 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 845 | decl_type = type_specifier_to_glsl_type(this->type->specifier, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 846 | & type_name, state); |
| 847 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 848 | foreach (ptr, &this->declarations) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 849 | struct ast_declaration *const decl = (struct ast_declaration * )ptr; |
| 850 | const struct glsl_type *var_type; |
| 851 | struct ir_variable *var; |
| 852 | |
| 853 | |
| 854 | /* FINISHME: Emit a warning if a variable declaration shadows a |
| 855 | * FINISHME: declaration at a higher scope. |
| 856 | */ |
| 857 | |
Ian Romanick | cec65a6 | 2010-03-23 12:28:44 -0700 | [diff] [blame] | 858 | if ((decl_type == NULL) || decl_type->is_void()) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 859 | YYLTYPE loc; |
| 860 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 861 | loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 862 | if (type_name != NULL) { |
| 863 | _mesa_glsl_error(& loc, state, |
| 864 | "invalid type `%s' in declaration of `%s'", |
| 865 | type_name, decl->identifier); |
| 866 | } else { |
| 867 | _mesa_glsl_error(& loc, state, |
| 868 | "invalid type in declaration of `%s'", |
| 869 | decl->identifier); |
| 870 | } |
| 871 | continue; |
| 872 | } |
| 873 | |
| 874 | if (decl->is_array) { |
| 875 | /* FINISHME: Handle array declarations. Note that this requires |
| 876 | * FINISHME: complete handling of constant expressions. |
| 877 | */ |
| 878 | |
| 879 | /* FINISHME: Reject delcarations of multidimensional arrays. */ |
| 880 | } else { |
| 881 | var_type = decl_type; |
| 882 | } |
| 883 | |
| 884 | var = new ir_variable(var_type, decl->identifier); |
| 885 | |
Ian Romanick | f3f111e | 2010-03-23 13:04:19 -0700 | [diff] [blame] | 886 | /* FINISHME: Variables that are attribute, uniform, varying, in, or |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 887 | * FINISHME: out varibles must be declared either at global scope or |
| 888 | * FINISHME: in a parameter list (in and out only). |
| 889 | */ |
| 890 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 891 | apply_type_qualifier_to_variable(& this->type->qualifier, var, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 892 | |
| 893 | /* Attempt to add the variable to the symbol table. If this fails, it |
| 894 | * means the variable has already been declared at this scope. |
| 895 | */ |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 896 | if (state->symbols->name_declared_this_scope(decl->identifier)) { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 897 | YYLTYPE loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 898 | |
| 899 | _mesa_glsl_error(& loc, state, "`%s' redeclared", |
| 900 | decl->identifier); |
| 901 | continue; |
| 902 | } |
| 903 | |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 904 | const bool added_variable = |
| 905 | state->symbols->add_variable(decl->identifier, var); |
| 906 | assert(added_variable); |
| 907 | |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 908 | instructions->push_tail(var); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 909 | |
| 910 | /* FINISHME: Process the declaration initializer. */ |
| 911 | } |
| 912 | |
| 913 | /* Variable declarations do not have r-values. |
| 914 | */ |
| 915 | return NULL; |
| 916 | } |
| 917 | |
| 918 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 919 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 920 | ast_parameter_declarator::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 921 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 922 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 923 | const struct glsl_type *type; |
| 924 | const char *name = NULL; |
| 925 | |
| 926 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 927 | type = type_specifier_to_glsl_type(this->type->specifier, & name, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 928 | |
| 929 | if (type == NULL) { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 930 | YYLTYPE loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 931 | if (name != NULL) { |
| 932 | _mesa_glsl_error(& loc, state, |
| 933 | "invalid type `%s' in declaration of `%s'", |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 934 | name, this->identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 935 | } else { |
| 936 | _mesa_glsl_error(& loc, state, |
| 937 | "invalid type in declaration of `%s'", |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 938 | this->identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 939 | } |
| 940 | |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 941 | type = glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 942 | } |
| 943 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 944 | ir_variable *var = new ir_variable(type, this->identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 945 | |
| 946 | /* FINISHME: Handle array declarations. Note that this requires |
| 947 | * FINISHME: complete handling of constant expressions. |
| 948 | */ |
| 949 | |
Ian Romanick | cdb8d54 | 2010-03-11 14:48:51 -0800 | [diff] [blame] | 950 | /* Apply any specified qualifiers to the parameter declaration. Note that |
| 951 | * for function parameters the default mode is 'in'. |
| 952 | */ |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 953 | apply_type_qualifier_to_variable(& this->type->qualifier, var, state); |
Ian Romanick | cdb8d54 | 2010-03-11 14:48:51 -0800 | [diff] [blame] | 954 | if (var->mode == ir_var_auto) |
| 955 | var->mode = ir_var_in; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 956 | |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 957 | instructions->push_tail(var); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 958 | |
| 959 | /* Parameter declarations do not have r-values. |
| 960 | */ |
| 961 | return NULL; |
| 962 | } |
| 963 | |
| 964 | |
| 965 | static void |
| 966 | ast_function_parameters_to_hir(struct simple_node *ast_parameters, |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 967 | exec_list *ir_parameters, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 968 | struct _mesa_glsl_parse_state *state) |
| 969 | { |
| 970 | struct simple_node *ptr; |
| 971 | |
| 972 | foreach (ptr, ast_parameters) { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 973 | ((ast_node *)ptr)->hir(ir_parameters, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 974 | } |
| 975 | } |
| 976 | |
| 977 | |
| 978 | static bool |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 979 | parameter_lists_match(exec_list *list_a, exec_list *list_b) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 980 | { |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 981 | exec_list_iterator iter_a = list_a->iterator(); |
| 982 | exec_list_iterator iter_b = list_b->iterator(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 983 | |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 984 | while (iter_a.has_next()) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 985 | /* If all of the parameters from the other parameter list have been |
| 986 | * exhausted, the lists have different length and, by definition, |
| 987 | * do not match. |
| 988 | */ |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 989 | if (!iter_b.has_next()) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 990 | return false; |
| 991 | |
| 992 | /* If the types of the parameters do not match, the parameters lists |
| 993 | * are different. |
| 994 | */ |
| 995 | /* FINISHME */ |
| 996 | |
| 997 | |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 998 | iter_a.next(); |
| 999 | iter_b.next(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | return true; |
| 1003 | } |
| 1004 | |
| 1005 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 1006 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1007 | ast_function_definition::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1008 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1009 | { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1010 | ir_label *label; |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1011 | ir_function_signature *signature = NULL; |
| 1012 | ir_function *f = NULL; |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1013 | exec_list parameters; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1014 | |
| 1015 | |
| 1016 | /* Convert the list of function parameters to HIR now so that they can be |
| 1017 | * used below to compare this function's signature with previously seen |
| 1018 | * signatures for functions with the same name. |
| 1019 | */ |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1020 | ast_function_parameters_to_hir(& this->prototype->parameters, & parameters, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1021 | state); |
| 1022 | |
Ian Romanick | e39cc69 | 2010-03-23 12:19:13 -0700 | [diff] [blame] | 1023 | const char *return_type_name; |
| 1024 | const glsl_type *return_type = |
| 1025 | type_specifier_to_glsl_type(this->prototype->return_type->specifier, |
| 1026 | & return_type_name, state); |
| 1027 | |
| 1028 | assert(return_type != NULL); |
| 1029 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1030 | |
| 1031 | /* Verify that this function's signature either doesn't match a previously |
| 1032 | * seen signature for a function with the same name, or, if a match is found, |
| 1033 | * that the previously seen signature does not have an associated definition. |
| 1034 | */ |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 1035 | const char *const name = this->prototype->identifier; |
| 1036 | f = state->symbols->get_function(name); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1037 | if (f != NULL) { |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1038 | foreach_iter(exec_list_iterator, iter, f->signatures) { |
| 1039 | signature = (struct ir_function_signature *) iter.get(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1040 | |
| 1041 | /* Compare the parameter list of the function being defined to the |
| 1042 | * existing function. If the parameter lists match, then the return |
| 1043 | * type must also match and the existing function must not have a |
| 1044 | * definition. |
| 1045 | */ |
| 1046 | if (parameter_lists_match(& parameters, & signature->parameters)) { |
| 1047 | /* FINISHME: Compare return types. */ |
| 1048 | |
| 1049 | if (signature->definition != NULL) { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1050 | YYLTYPE loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1051 | |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 1052 | _mesa_glsl_error(& loc, state, "function `%s' redefined", name); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1053 | signature = NULL; |
| 1054 | break; |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | signature = NULL; |
| 1059 | } |
| 1060 | |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 1061 | } else if (state->symbols->name_declared_this_scope(name)) { |
| 1062 | /* This function name shadows a non-function use of the same name. |
| 1063 | */ |
| 1064 | YYLTYPE loc = this->get_location(); |
| 1065 | |
| 1066 | _mesa_glsl_error(& loc, state, "function name `%s' conflicts with " |
| 1067 | "non-function", name); |
| 1068 | signature = NULL; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1069 | } else { |
Ian Romanick | 882dad7 | 2010-03-23 17:42:04 -0700 | [diff] [blame] | 1070 | f = new ir_function(name); |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 1071 | state->symbols->add_function(f->name, f); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | |
| 1075 | /* Finish storing the information about this new function in its signature. |
| 1076 | */ |
| 1077 | if (signature == NULL) { |
Ian Romanick | e39cc69 | 2010-03-23 12:19:13 -0700 | [diff] [blame] | 1078 | signature = new ir_function_signature(return_type); |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1079 | f->signatures.push_tail(signature); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1080 | } else { |
| 1081 | /* Destroy all of the previous parameter information. The previous |
| 1082 | * parameter information comes from the function prototype, and it can |
| 1083 | * either include invalid parameter names or may not have names at all. |
| 1084 | */ |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1085 | foreach_iter(exec_list_iterator, iter, signature->parameters) { |
Kenneth Graunke | 44e1dfa | 2010-03-25 23:30:28 -0700 | [diff] [blame] | 1086 | assert(((ir_instruction *) iter.get())->as_variable() != NULL); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1087 | |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1088 | iter.remove(); |
| 1089 | delete iter.get(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | |
Ian Romanick | 41ec6a4 | 2010-03-19 17:08:05 -0700 | [diff] [blame] | 1094 | assert(state->current_function == NULL); |
| 1095 | state->current_function = signature; |
| 1096 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1097 | ast_function_parameters_to_hir(& this->prototype->parameters, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1098 | & signature->parameters, |
| 1099 | state); |
| 1100 | /* FINISHME: Set signature->return_type */ |
| 1101 | |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 1102 | label = new ir_label(name); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1103 | if (signature->definition == NULL) { |
| 1104 | signature->definition = label; |
| 1105 | } |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1106 | instructions->push_tail(label); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1107 | |
| 1108 | /* Add the function parameters to the symbol table. During this step the |
| 1109 | * parameter declarations are also moved from the temporary "parameters" list |
| 1110 | * to the instruction list. There are other more efficient ways to do this, |
| 1111 | * but they involve ugly linked-list gymnastics. |
| 1112 | */ |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 1113 | state->symbols->push_scope(); |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1114 | foreach_iter(exec_list_iterator, iter, parameters) { |
| 1115 | ir_variable *const var = (ir_variable *) iter.get(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1116 | |
Kenneth Graunke | 44e1dfa | 2010-03-25 23:30:28 -0700 | [diff] [blame] | 1117 | assert(((ir_instruction *) var)->as_variable() != NULL); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1118 | |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1119 | iter.remove(); |
| 1120 | instructions->push_tail(var); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1121 | |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 1122 | /* The only way a parameter would "exist" is if two parameters have |
| 1123 | * the same name. |
| 1124 | */ |
| 1125 | if (state->symbols->name_declared_this_scope(var->name)) { |
| 1126 | YYLTYPE loc = this->get_location(); |
| 1127 | |
| 1128 | _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name); |
| 1129 | } else { |
| 1130 | state->symbols->add_variable(var->name, var); |
| 1131 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | /* Convert the body of the function to HIR, and append the resulting |
| 1135 | * instructions to the list that currently consists of the function label |
| 1136 | * and the function parameters. |
| 1137 | */ |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1138 | this->body->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1139 | |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 1140 | state->symbols->pop_scope(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1141 | |
Ian Romanick | 41ec6a4 | 2010-03-19 17:08:05 -0700 | [diff] [blame] | 1142 | assert(state->current_function == signature); |
| 1143 | state->current_function = NULL; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1144 | |
| 1145 | /* Function definitions do not have r-values. |
| 1146 | */ |
| 1147 | return NULL; |
| 1148 | } |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 1149 | |
| 1150 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 1151 | ir_rvalue * |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 1152 | ast_jump_statement::hir(exec_list *instructions, |
| 1153 | struct _mesa_glsl_parse_state *state) |
| 1154 | { |
| 1155 | |
| 1156 | if (mode == ast_return) { |
| 1157 | ir_return *inst; |
| 1158 | |
| 1159 | if (opt_return_value) { |
| 1160 | /* FINISHME: Make sure the enclosing function has a non-void return |
| 1161 | * FINISHME: type. |
| 1162 | */ |
| 1163 | |
| 1164 | ir_expression *const ret = (ir_expression *) |
| 1165 | opt_return_value->hir(instructions, state); |
| 1166 | assert(ret != NULL); |
| 1167 | |
| 1168 | /* FINISHME: Make sure the type of the return value matches the return |
| 1169 | * FINISHME: type of the enclosing function. |
| 1170 | */ |
| 1171 | |
| 1172 | inst = new ir_return(ret); |
| 1173 | } else { |
| 1174 | /* FINISHME: Make sure the enclosing function has a void return type. |
| 1175 | */ |
| 1176 | inst = new ir_return; |
| 1177 | } |
| 1178 | |
| 1179 | instructions->push_tail(inst); |
| 1180 | } |
| 1181 | |
| 1182 | /* Jump instructions do not have r-values. |
| 1183 | */ |
| 1184 | return NULL; |
| 1185 | } |