Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2010 Intel Corporation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | * DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * \file ast_to_hir.c |
| 26 | * Convert abstract syntax to to high-level intermediate reprensentation (HIR). |
| 27 | * |
| 28 | * During the conversion to HIR, the majority of the symantic checking is |
| 29 | * preformed on the program. This includes: |
| 30 | * |
| 31 | * * Symbol table management |
| 32 | * * Type checking |
| 33 | * * Function binding |
| 34 | * |
| 35 | * The majority of this work could be done during parsing, and the parser could |
| 36 | * probably generate HIR directly. However, this results in frequent changes |
| 37 | * to the parser code. Since we do not assume that every system this complier |
| 38 | * is built on will have Flex and Bison installed, we have to store the code |
| 39 | * generated by these tools in our version control system. In other parts of |
| 40 | * the system we've seen problems where a parser was changed but the generated |
| 41 | * code was not committed, merge conflicts where created because two developers |
| 42 | * had slightly different versions of Bison installed, etc. |
| 43 | * |
| 44 | * I have also noticed that running Bison generated parsers in GDB is very |
| 45 | * irritating. When you get a segfault on '$$ = $1->foo', you can't very |
| 46 | * well 'print $1' in GDB. |
| 47 | * |
| 48 | * As a result, my preference is to put as little C code as possible in the |
| 49 | * parser (and lexer) sources. |
| 50 | */ |
Eric Anholt | ac95f2f | 2010-06-22 10:38:52 -0700 | [diff] [blame] | 51 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 52 | #include "main/imports.h" |
Eric Anholt | 4a96217 | 2010-07-28 14:41:51 -0700 | [diff] [blame] | 53 | #include "main/extensions.h" |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 54 | #include "glsl_symbol_table.h" |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 55 | #include "glsl_parser_extras.h" |
| 56 | #include "ast.h" |
| 57 | #include "glsl_types.h" |
| 58 | #include "ir.h" |
| 59 | |
Ian Romanick | d949a9a | 2010-03-10 09:55:22 -0800 | [diff] [blame] | 60 | void |
| 61 | _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state) |
| 62 | { |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 63 | _mesa_glsl_initialize_variables(instructions, state); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 64 | _mesa_glsl_initialize_functions(instructions, state); |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 65 | |
Ian Romanick | 41ec6a4 | 2010-03-19 17:08:05 -0700 | [diff] [blame] | 66 | state->current_function = NULL; |
| 67 | |
Ian Romanick | 2b97dc6 | 2010-05-10 17:42:05 -0700 | [diff] [blame] | 68 | foreach_list_typed (ast_node, ast, link, & state->translation_unit) |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 69 | ast->hir(instructions, state); |
Ian Romanick | d949a9a | 2010-03-10 09:55:22 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 73 | /** |
| 74 | * If a conversion is available, convert one operand to a different type |
| 75 | * |
| 76 | * The \c from \c ir_rvalue is converted "in place". |
| 77 | * |
| 78 | * \param to Type that the operand it to be converted to |
| 79 | * \param from Operand that is being converted |
| 80 | * \param state GLSL compiler state |
| 81 | * |
| 82 | * \return |
| 83 | * If a conversion is possible (or unnecessary), \c true is returned. |
| 84 | * Otherwise \c false is returned. |
| 85 | */ |
| 86 | static bool |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 87 | apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from, |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 88 | struct _mesa_glsl_parse_state *state) |
| 89 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 90 | void *ctx = state; |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 91 | if (to->base_type == from->type->base_type) |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 92 | return true; |
| 93 | |
| 94 | /* This conversion was added in GLSL 1.20. If the compilation mode is |
| 95 | * GLSL 1.10, the conversion is skipped. |
| 96 | */ |
| 97 | if (state->language_version < 120) |
| 98 | return false; |
| 99 | |
| 100 | /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec: |
| 101 | * |
| 102 | * "There are no implicit array or structure conversions. For |
| 103 | * example, an array of int cannot be implicitly converted to an |
| 104 | * array of float. There are no implicit conversions between |
| 105 | * signed and unsigned integers." |
| 106 | */ |
| 107 | /* FINISHME: The above comment is partially a lie. There is int/uint |
| 108 | * FINISHME: conversion for immediate constants. |
| 109 | */ |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 110 | if (!to->is_float() || !from->type->is_numeric()) |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 111 | return false; |
| 112 | |
Kenneth Graunke | 506199b | 2010-06-29 15:59:27 -0700 | [diff] [blame] | 113 | /* Convert to a floating point type with the same number of components |
| 114 | * as the original type - i.e. int to float, not int to vec4. |
| 115 | */ |
| 116 | to = glsl_type::get_instance(GLSL_TYPE_FLOAT, from->type->vector_elements, |
| 117 | from->type->matrix_columns); |
| 118 | |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 119 | switch (from->type->base_type) { |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 120 | case GLSL_TYPE_INT: |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 121 | from = new(ctx) ir_expression(ir_unop_i2f, to, from, NULL); |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 122 | break; |
| 123 | case GLSL_TYPE_UINT: |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 124 | from = new(ctx) ir_expression(ir_unop_u2f, to, from, NULL); |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 125 | break; |
| 126 | case GLSL_TYPE_BOOL: |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 127 | from = new(ctx) ir_expression(ir_unop_b2f, to, from, NULL); |
Eric Anholt | dc58b3f | 2010-04-02 02:13:43 -1000 | [diff] [blame] | 128 | break; |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 129 | default: |
| 130 | assert(0); |
| 131 | } |
| 132 | |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 137 | static const struct glsl_type * |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 138 | arithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 139 | bool multiply, |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 140 | struct _mesa_glsl_parse_state *state, YYLTYPE *loc) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 141 | { |
Eric Anholt | 336b4ad | 2010-05-19 10:38:37 -0700 | [diff] [blame] | 142 | const glsl_type *type_a = value_a->type; |
| 143 | const glsl_type *type_b = value_b->type; |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 144 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 145 | /* From GLSL 1.50 spec, page 56: |
| 146 | * |
| 147 | * "The arithmetic binary operators add (+), subtract (-), |
| 148 | * multiply (*), and divide (/) operate on integer and |
| 149 | * floating-point scalars, vectors, and matrices." |
| 150 | */ |
Ian Romanick | 60b54d9 | 2010-03-24 17:08:13 -0700 | [diff] [blame] | 151 | if (!type_a->is_numeric() || !type_b->is_numeric()) { |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 152 | _mesa_glsl_error(loc, state, |
| 153 | "Operands to arithmetic operators must be numeric"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 154 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | |
| 158 | /* "If one operand is floating-point based and the other is |
| 159 | * not, then the conversions from Section 4.1.10 "Implicit |
| 160 | * Conversions" are applied to the non-floating-point-based operand." |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 161 | */ |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 162 | if (!apply_implicit_conversion(type_a, value_b, state) |
| 163 | && !apply_implicit_conversion(type_b, value_a, state)) { |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 164 | _mesa_glsl_error(loc, state, |
| 165 | "Could not implicitly convert operands to " |
| 166 | "arithmetic operator"); |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 167 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 168 | } |
Eric Anholt | 336b4ad | 2010-05-19 10:38:37 -0700 | [diff] [blame] | 169 | type_a = value_a->type; |
| 170 | type_b = value_b->type; |
| 171 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 172 | /* "If the operands are integer types, they must both be signed or |
| 173 | * both be unsigned." |
| 174 | * |
| 175 | * From this rule and the preceeding conversion it can be inferred that |
| 176 | * 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] | 177 | * The is_numeric check above already filtered out the case where either |
| 178 | * type is not one of these, so now the base types need only be tested for |
| 179 | * equality. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 180 | */ |
| 181 | if (type_a->base_type != type_b->base_type) { |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 182 | _mesa_glsl_error(loc, state, |
| 183 | "base type mismatch for arithmetic operator"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 184 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /* "All arithmetic binary operators result in the same fundamental type |
| 188 | * (signed integer, unsigned integer, or floating-point) as the |
| 189 | * operands they operate on, after operand type conversion. After |
| 190 | * conversion, the following cases are valid |
| 191 | * |
| 192 | * * The two operands are scalars. In this case the operation is |
| 193 | * applied, resulting in a scalar." |
| 194 | */ |
Ian Romanick | cb36f8a | 2010-03-09 15:51:22 -0800 | [diff] [blame] | 195 | if (type_a->is_scalar() && type_b->is_scalar()) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 196 | return type_a; |
| 197 | |
| 198 | /* "* One operand is a scalar, and the other is a vector or matrix. |
| 199 | * In this case, the scalar operation is applied independently to each |
| 200 | * component of the vector or matrix, resulting in the same size |
| 201 | * vector or matrix." |
| 202 | */ |
Ian Romanick | cb36f8a | 2010-03-09 15:51:22 -0800 | [diff] [blame] | 203 | if (type_a->is_scalar()) { |
| 204 | if (!type_b->is_scalar()) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 205 | return type_b; |
Ian Romanick | cb36f8a | 2010-03-09 15:51:22 -0800 | [diff] [blame] | 206 | } else if (type_b->is_scalar()) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 207 | return type_a; |
| 208 | } |
| 209 | |
| 210 | /* All of the combinations of <scalar, scalar>, <vector, scalar>, |
| 211 | * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been |
| 212 | * handled. |
| 213 | */ |
Ian Romanick | 60b54d9 | 2010-03-24 17:08:13 -0700 | [diff] [blame] | 214 | assert(!type_a->is_scalar()); |
| 215 | assert(!type_b->is_scalar()); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 216 | |
| 217 | /* "* The two operands are vectors of the same size. In this case, the |
| 218 | * operation is done component-wise resulting in the same size |
| 219 | * vector." |
| 220 | */ |
Ian Romanick | a2dd22f | 2010-03-09 15:55:16 -0800 | [diff] [blame] | 221 | if (type_a->is_vector() && type_b->is_vector()) { |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 222 | if (type_a == type_b) { |
| 223 | return type_a; |
| 224 | } else { |
| 225 | _mesa_glsl_error(loc, state, |
| 226 | "vector size mismatch for arithmetic operator"); |
| 227 | return glsl_type::error_type; |
| 228 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /* All of the combinations of <scalar, scalar>, <vector, scalar>, |
| 232 | * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and |
| 233 | * <vector, vector> have been handled. At least one of the operands must |
| 234 | * be matrix. Further, since there are no integer matrix types, the base |
| 235 | * type of both operands must be float. |
| 236 | */ |
Ian Romanick | 60b54d9 | 2010-03-24 17:08:13 -0700 | [diff] [blame] | 237 | assert(type_a->is_matrix() || type_b->is_matrix()); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 238 | assert(type_a->base_type == GLSL_TYPE_FLOAT); |
| 239 | assert(type_b->base_type == GLSL_TYPE_FLOAT); |
| 240 | |
| 241 | /* "* The operator is add (+), subtract (-), or divide (/), and the |
| 242 | * operands are matrices with the same number of rows and the same |
| 243 | * number of columns. In this case, the operation is done component- |
| 244 | * wise resulting in the same size matrix." |
| 245 | * * The operator is multiply (*), where both operands are matrices or |
| 246 | * one operand is a vector and the other a matrix. A right vector |
| 247 | * operand is treated as a column vector and a left vector operand as a |
| 248 | * row vector. In all these cases, it is required that the number of |
| 249 | * columns of the left operand is equal to the number of rows of the |
| 250 | * right operand. Then, the multiply (*) operation does a linear |
| 251 | * algebraic multiply, yielding an object that has the same number of |
| 252 | * rows as the left operand and the same number of columns as the right |
| 253 | * operand. Section 5.10 "Vector and Matrix Operations" explains in |
| 254 | * more detail how vectors and matrices are operated on." |
| 255 | */ |
| 256 | if (! multiply) { |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 257 | if (type_a == type_b) |
| 258 | return type_a; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 259 | } else { |
Ian Romanick | fce1150 | 2010-03-09 15:58:52 -0800 | [diff] [blame] | 260 | if (type_a->is_matrix() && type_b->is_matrix()) { |
Ian Romanick | c1bd3a1 | 2010-03-25 13:06:58 -0700 | [diff] [blame] | 261 | /* Matrix multiply. The columns of A must match the rows of B. Given |
| 262 | * the other previously tested constraints, this means the vector type |
| 263 | * of a row from A must be the same as the vector type of a column from |
| 264 | * B. |
| 265 | */ |
| 266 | if (type_a->row_type() == type_b->column_type()) { |
| 267 | /* The resulting matrix has the number of columns of matrix B and |
| 268 | * the number of rows of matrix A. We get the row count of A by |
| 269 | * looking at the size of a vector that makes up a column. The |
| 270 | * transpose (size of a row) is done for B. |
| 271 | */ |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 272 | const glsl_type *const type = |
Ian Romanick | c1bd3a1 | 2010-03-25 13:06:58 -0700 | [diff] [blame] | 273 | glsl_type::get_instance(type_a->base_type, |
| 274 | type_a->column_type()->vector_elements, |
| 275 | type_b->row_type()->vector_elements); |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 276 | assert(type != glsl_type::error_type); |
| 277 | |
| 278 | return type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 279 | } |
Ian Romanick | fce1150 | 2010-03-09 15:58:52 -0800 | [diff] [blame] | 280 | } else if (type_a->is_matrix()) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 281 | /* 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] | 282 | * rows of B. Given the other previously tested constraints, this |
| 283 | * means the vector type of a row from A must be the same as the |
| 284 | * vector the type of B. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 285 | */ |
Carl Worth | 47c90b1 | 2010-07-22 14:56:14 -0700 | [diff] [blame] | 286 | if (type_a->row_type() == type_b) { |
| 287 | /* The resulting vector has a number of elements equal to |
| 288 | * the number of rows of matrix A. */ |
| 289 | const glsl_type *const type = |
| 290 | glsl_type::get_instance(type_a->base_type, |
| 291 | type_a->column_type()->vector_elements, |
| 292 | 1); |
| 293 | assert(type != glsl_type::error_type); |
| 294 | |
| 295 | return type; |
| 296 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 297 | } else { |
Ian Romanick | fce1150 | 2010-03-09 15:58:52 -0800 | [diff] [blame] | 298 | assert(type_b->is_matrix()); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 299 | |
Ian Romanick | c1bd3a1 | 2010-03-25 13:06:58 -0700 | [diff] [blame] | 300 | /* A is a row vector and B is a matrix. Columns of A must match rows |
| 301 | * of B. Given the other previously tested constraints, this means |
| 302 | * the type of A must be the same as the vector type of a column from |
| 303 | * B. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 304 | */ |
Carl Worth | 47c90b1 | 2010-07-22 14:56:14 -0700 | [diff] [blame] | 305 | if (type_a == type_b->column_type()) { |
| 306 | /* The resulting vector has a number of elements equal to |
| 307 | * the number of columns of matrix B. */ |
| 308 | const glsl_type *const type = |
| 309 | glsl_type::get_instance(type_a->base_type, |
| 310 | type_b->row_type()->vector_elements, |
| 311 | 1); |
| 312 | assert(type != glsl_type::error_type); |
| 313 | |
| 314 | return type; |
| 315 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 316 | } |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 317 | |
| 318 | _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication"); |
| 319 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | |
| 323 | /* "All other cases are illegal." |
| 324 | */ |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 325 | _mesa_glsl_error(loc, state, "type mismatch"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 326 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | |
| 330 | static const struct glsl_type * |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 331 | unary_arithmetic_result_type(const struct glsl_type *type, |
| 332 | struct _mesa_glsl_parse_state *state, YYLTYPE *loc) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 333 | { |
| 334 | /* From GLSL 1.50 spec, page 57: |
| 335 | * |
| 336 | * "The arithmetic unary operators negate (-), post- and pre-increment |
| 337 | * and decrement (-- and ++) operate on integer or floating-point |
| 338 | * values (including vectors and matrices). All unary operators work |
| 339 | * component-wise on their operands. These result with the same type |
| 340 | * they operated on." |
| 341 | */ |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 342 | if (!type->is_numeric()) { |
| 343 | _mesa_glsl_error(loc, state, |
| 344 | "Operands to arithmetic operators must be numeric"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 345 | return glsl_type::error_type; |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 346 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 347 | |
| 348 | return type; |
| 349 | } |
| 350 | |
| 351 | |
| 352 | static const struct glsl_type * |
| 353 | modulus_result_type(const struct glsl_type *type_a, |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 354 | const struct glsl_type *type_b, |
| 355 | struct _mesa_glsl_parse_state *state, YYLTYPE *loc) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 356 | { |
| 357 | /* From GLSL 1.50 spec, page 56: |
| 358 | * "The operator modulus (%) operates on signed or unsigned integers or |
| 359 | * integer vectors. The operand types must both be signed or both be |
| 360 | * unsigned." |
| 361 | */ |
Ian Romanick | 40176e2 | 2010-03-26 14:38:37 -0700 | [diff] [blame] | 362 | if (!type_a->is_integer() || !type_b->is_integer() |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 363 | || (type_a->base_type != type_b->base_type)) { |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 364 | _mesa_glsl_error(loc, state, "type mismatch"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 365 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | /* "The operands cannot be vectors of differing size. If one operand is |
| 369 | * a scalar and the other vector, then the scalar is applied component- |
| 370 | * wise to the vector, resulting in the same type as the vector. If both |
| 371 | * are vectors of the same size, the result is computed component-wise." |
| 372 | */ |
Ian Romanick | a2dd22f | 2010-03-09 15:55:16 -0800 | [diff] [blame] | 373 | if (type_a->is_vector()) { |
| 374 | if (!type_b->is_vector() |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 375 | || (type_a->vector_elements == type_b->vector_elements)) |
| 376 | return type_a; |
| 377 | } else |
| 378 | return type_b; |
| 379 | |
| 380 | /* "The operator modulus (%) is not defined for any other data types |
| 381 | * (non-integer types)." |
| 382 | */ |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 383 | _mesa_glsl_error(loc, state, "type mismatch"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 384 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | |
| 388 | static const struct glsl_type * |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 389 | relational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b, |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 390 | struct _mesa_glsl_parse_state *state, YYLTYPE *loc) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 391 | { |
Eric Anholt | 336b4ad | 2010-05-19 10:38:37 -0700 | [diff] [blame] | 392 | const glsl_type *type_a = value_a->type; |
| 393 | const glsl_type *type_b = value_b->type; |
Ian Romanick | 0150f5f | 2010-03-29 16:20:07 -0700 | [diff] [blame] | 394 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 395 | /* From GLSL 1.50 spec, page 56: |
| 396 | * "The relational operators greater than (>), less than (<), greater |
| 397 | * than or equal (>=), and less than or equal (<=) operate only on |
| 398 | * scalar integer and scalar floating-point expressions." |
| 399 | */ |
Ian Romanick | a6d653d | 2010-03-26 14:40:37 -0700 | [diff] [blame] | 400 | if (!type_a->is_numeric() |
| 401 | || !type_b->is_numeric() |
Ian Romanick | cb36f8a | 2010-03-09 15:51:22 -0800 | [diff] [blame] | 402 | || !type_a->is_scalar() |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 403 | || !type_b->is_scalar()) { |
| 404 | _mesa_glsl_error(loc, state, |
| 405 | "Operands to relational operators must be scalar and " |
| 406 | "numeric"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 407 | return glsl_type::error_type; |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 408 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 409 | |
| 410 | /* "Either the operands' types must match, or the conversions from |
| 411 | * Section 4.1.10 "Implicit Conversions" will be applied to the integer |
| 412 | * operand, after which the types must match." |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 413 | */ |
Ian Romanick | 0150f5f | 2010-03-29 16:20:07 -0700 | [diff] [blame] | 414 | if (!apply_implicit_conversion(type_a, value_b, state) |
| 415 | && !apply_implicit_conversion(type_b, value_a, state)) { |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 416 | _mesa_glsl_error(loc, state, |
| 417 | "Could not implicitly convert operands to " |
| 418 | "relational operator"); |
Ian Romanick | 0150f5f | 2010-03-29 16:20:07 -0700 | [diff] [blame] | 419 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 420 | } |
Eric Anholt | 336b4ad | 2010-05-19 10:38:37 -0700 | [diff] [blame] | 421 | type_a = value_a->type; |
| 422 | type_b = value_b->type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 423 | |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 424 | if (type_a->base_type != type_b->base_type) { |
| 425 | _mesa_glsl_error(loc, state, "base type mismatch"); |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 426 | return glsl_type::error_type; |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 427 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 428 | |
| 429 | /* "The result is scalar Boolean." |
| 430 | */ |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 431 | return glsl_type::bool_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 435 | /** |
| 436 | * Validates that a value can be assigned to a location with a specified type |
| 437 | * |
| 438 | * Validates that \c rhs can be assigned to some location. If the types are |
| 439 | * not an exact match but an automatic conversion is possible, \c rhs will be |
| 440 | * converted. |
| 441 | * |
| 442 | * \return |
| 443 | * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type. |
| 444 | * Otherwise the actual RHS to be assigned will be returned. This may be |
| 445 | * \c rhs, or it may be \c rhs after some type conversion. |
| 446 | * |
| 447 | * \note |
| 448 | * In addition to being used for assignments, this function is used to |
| 449 | * type-check return values. |
| 450 | */ |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 451 | ir_rvalue * |
Eric Anholt | 336b4ad | 2010-05-19 10:38:37 -0700 | [diff] [blame] | 452 | validate_assignment(struct _mesa_glsl_parse_state *state, |
| 453 | const glsl_type *lhs_type, ir_rvalue *rhs) |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 454 | { |
Eric Anholt | 336b4ad | 2010-05-19 10:38:37 -0700 | [diff] [blame] | 455 | const glsl_type *rhs_type = rhs->type; |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 456 | |
| 457 | /* If there is already some error in the RHS, just return it. Anything |
| 458 | * else will lead to an avalanche of error message back to the user. |
| 459 | */ |
| 460 | if (rhs_type->is_error()) |
| 461 | return rhs; |
| 462 | |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 463 | /* If the types are identical, the assignment can trivially proceed. |
| 464 | */ |
| 465 | if (rhs_type == lhs_type) |
| 466 | return rhs; |
| 467 | |
Ian Romanick | 0157f41 | 2010-04-02 17:44:39 -0700 | [diff] [blame] | 468 | /* If the array element types are the same and the size of the LHS is zero, |
| 469 | * the assignment is okay. |
| 470 | * |
| 471 | * Note: Whole-array assignments are not permitted in GLSL 1.10, but this |
| 472 | * is handled by ir_dereference::is_lvalue. |
| 473 | */ |
| 474 | if (lhs_type->is_array() && rhs->type->is_array() |
| 475 | && (lhs_type->element_type() == rhs->type->element_type()) |
| 476 | && (lhs_type->array_size() == 0)) { |
| 477 | return rhs; |
| 478 | } |
| 479 | |
Eric Anholt | 336b4ad | 2010-05-19 10:38:37 -0700 | [diff] [blame] | 480 | /* Check for implicit conversion in GLSL 1.20 */ |
| 481 | if (apply_implicit_conversion(lhs_type, rhs, state)) { |
| 482 | rhs_type = rhs->type; |
| 483 | if (rhs_type == lhs_type) |
| 484 | return rhs; |
| 485 | } |
| 486 | |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 487 | return NULL; |
| 488 | } |
| 489 | |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 490 | ir_rvalue * |
| 491 | do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state, |
| 492 | ir_rvalue *lhs, ir_rvalue *rhs, |
| 493 | YYLTYPE lhs_loc) |
| 494 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 495 | void *ctx = state; |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 496 | bool error_emitted = (lhs->type->is_error() || rhs->type->is_error()); |
| 497 | |
| 498 | if (!error_emitted) { |
| 499 | /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */ |
| 500 | if (!lhs->is_lvalue()) { |
| 501 | _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment"); |
| 502 | error_emitted = true; |
| 503 | } |
| 504 | } |
| 505 | |
Eric Anholt | 336b4ad | 2010-05-19 10:38:37 -0700 | [diff] [blame] | 506 | ir_rvalue *new_rhs = validate_assignment(state, lhs->type, rhs); |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 507 | if (new_rhs == NULL) { |
| 508 | _mesa_glsl_error(& lhs_loc, state, "type mismatch"); |
| 509 | } else { |
| 510 | rhs = new_rhs; |
Ian Romanick | 0157f41 | 2010-04-02 17:44:39 -0700 | [diff] [blame] | 511 | |
| 512 | /* If the LHS array was not declared with a size, it takes it size from |
| 513 | * the RHS. If the LHS is an l-value and a whole array, it must be a |
| 514 | * dereference of a variable. Any other case would require that the LHS |
| 515 | * is either not an l-value or not a whole array. |
| 516 | */ |
| 517 | if (lhs->type->array_size() == 0) { |
| 518 | ir_dereference *const d = lhs->as_dereference(); |
| 519 | |
| 520 | assert(d != NULL); |
| 521 | |
Ian Romanick | 36ea286 | 2010-05-19 13:52:29 +0200 | [diff] [blame] | 522 | ir_variable *const var = d->variable_referenced(); |
Ian Romanick | 0157f41 | 2010-04-02 17:44:39 -0700 | [diff] [blame] | 523 | |
| 524 | assert(var != NULL); |
| 525 | |
Ian Romanick | 63f3942 | 2010-04-05 14:35:47 -0700 | [diff] [blame] | 526 | if (var->max_array_access >= unsigned(rhs->type->array_size())) { |
| 527 | /* FINISHME: This should actually log the location of the RHS. */ |
| 528 | _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to " |
| 529 | "previous access", |
| 530 | var->max_array_access); |
| 531 | } |
| 532 | |
Ian Romanick | f38d15b | 2010-07-20 15:33:40 -0700 | [diff] [blame] | 533 | var->type = glsl_type::get_array_instance(lhs->type->element_type(), |
Ian Romanick | 0157f41 | 2010-04-02 17:44:39 -0700 | [diff] [blame] | 534 | rhs->type->array_size()); |
Eric Anholt | 9703ed0 | 2010-07-22 15:50:37 -0700 | [diff] [blame] | 535 | d->type = var->type; |
Ian Romanick | 0157f41 | 2010-04-02 17:44:39 -0700 | [diff] [blame] | 536 | } |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Eric Anholt | 2731a73 | 2010-06-23 14:51:14 -0700 | [diff] [blame] | 539 | /* Most callers of do_assignment (assign, add_assign, pre_inc/dec, |
| 540 | * but not post_inc) need the converted assigned value as an rvalue |
| 541 | * to handle things like: |
| 542 | * |
| 543 | * i = j += 1; |
| 544 | * |
| 545 | * So we always just store the computed value being assigned to a |
| 546 | * temporary and return a deref of that temporary. If the rvalue |
| 547 | * ends up not being used, the temp will get copy-propagated out. |
| 548 | */ |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 549 | ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp", |
| 550 | ir_var_temporary); |
Eric Anholt | e33c103 | 2010-06-24 15:13:03 -0700 | [diff] [blame] | 551 | ir_dereference_variable *deref_var = new(ctx) ir_dereference_variable(var); |
Eric Anholt | ae80592 | 2010-06-24 09:06:12 -0700 | [diff] [blame] | 552 | instructions->push_tail(var); |
Eric Anholt | e33c103 | 2010-06-24 15:13:03 -0700 | [diff] [blame] | 553 | instructions->push_tail(new(ctx) ir_assignment(deref_var, |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 554 | rhs, |
| 555 | NULL)); |
Eric Anholt | e33c103 | 2010-06-24 15:13:03 -0700 | [diff] [blame] | 556 | deref_var = new(ctx) ir_dereference_variable(var); |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 557 | |
Ian Romanick | 8e9ce2e | 2010-08-03 15:02:35 -0700 | [diff] [blame] | 558 | if (!error_emitted) |
| 559 | instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var, NULL)); |
Eric Anholt | 2731a73 | 2010-06-23 14:51:14 -0700 | [diff] [blame] | 560 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 561 | return new(ctx) ir_dereference_variable(var); |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 562 | } |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 563 | |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 564 | static ir_rvalue * |
Eric Anholt | 959a9ec | 2010-06-23 14:43:50 -0700 | [diff] [blame] | 565 | get_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue) |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 566 | { |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 567 | void *ctx = talloc_parent(lvalue); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 568 | ir_variable *var; |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 569 | |
| 570 | /* FINISHME: Give unique names to the temporaries. */ |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 571 | var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp", |
| 572 | ir_var_temporary); |
Eric Anholt | 43b5b03 | 2010-07-07 14:04:30 -0700 | [diff] [blame] | 573 | instructions->push_tail(var); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 574 | var->mode = ir_var_auto; |
| 575 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 576 | instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var), |
| 577 | lvalue, NULL)); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 578 | |
| 579 | /* Once we've created this temporary, mark it read only so it's no |
| 580 | * longer considered an lvalue. |
| 581 | */ |
| 582 | var->read_only = true; |
| 583 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 584 | return new(ctx) ir_dereference_variable(var); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 588 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 589 | ast_node::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 590 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 591 | { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 592 | (void) instructions; |
| 593 | (void) state; |
| 594 | |
| 595 | return NULL; |
| 596 | } |
| 597 | |
| 598 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 599 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 600 | ast_expression::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 601 | struct _mesa_glsl_parse_state *state) |
| 602 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 603 | void *ctx = state; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 604 | static const int operations[AST_NUM_OPERATORS] = { |
| 605 | -1, /* ast_assign doesn't convert to ir_expression. */ |
| 606 | -1, /* ast_plus doesn't convert to ir_expression. */ |
| 607 | ir_unop_neg, |
| 608 | ir_binop_add, |
| 609 | ir_binop_sub, |
| 610 | ir_binop_mul, |
| 611 | ir_binop_div, |
| 612 | ir_binop_mod, |
| 613 | ir_binop_lshift, |
| 614 | ir_binop_rshift, |
| 615 | ir_binop_less, |
| 616 | ir_binop_greater, |
| 617 | ir_binop_lequal, |
| 618 | ir_binop_gequal, |
| 619 | ir_binop_equal, |
| 620 | ir_binop_nequal, |
| 621 | ir_binop_bit_and, |
| 622 | ir_binop_bit_xor, |
| 623 | ir_binop_bit_or, |
| 624 | ir_unop_bit_not, |
| 625 | ir_binop_logic_and, |
| 626 | ir_binop_logic_xor, |
| 627 | ir_binop_logic_or, |
| 628 | ir_unop_logic_not, |
| 629 | |
| 630 | /* Note: The following block of expression types actually convert |
| 631 | * to multiple IR instructions. |
| 632 | */ |
| 633 | ir_binop_mul, /* ast_mul_assign */ |
| 634 | ir_binop_div, /* ast_div_assign */ |
| 635 | ir_binop_mod, /* ast_mod_assign */ |
| 636 | ir_binop_add, /* ast_add_assign */ |
| 637 | ir_binop_sub, /* ast_sub_assign */ |
| 638 | ir_binop_lshift, /* ast_ls_assign */ |
| 639 | ir_binop_rshift, /* ast_rs_assign */ |
| 640 | ir_binop_bit_and, /* ast_and_assign */ |
| 641 | ir_binop_bit_xor, /* ast_xor_assign */ |
| 642 | ir_binop_bit_or, /* ast_or_assign */ |
| 643 | |
| 644 | -1, /* ast_conditional doesn't convert to ir_expression. */ |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 645 | ir_binop_add, /* ast_pre_inc. */ |
| 646 | ir_binop_sub, /* ast_pre_dec. */ |
| 647 | ir_binop_add, /* ast_post_inc. */ |
| 648 | ir_binop_sub, /* ast_post_dec. */ |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 649 | -1, /* ast_field_selection doesn't conv to ir_expression. */ |
| 650 | -1, /* ast_array_index doesn't convert to ir_expression. */ |
| 651 | -1, /* ast_function_call doesn't conv to ir_expression. */ |
| 652 | -1, /* ast_identifier doesn't convert to ir_expression. */ |
| 653 | -1, /* ast_int_constant doesn't convert to ir_expression. */ |
| 654 | -1, /* ast_uint_constant doesn't conv to ir_expression. */ |
| 655 | -1, /* ast_float_constant doesn't conv to ir_expression. */ |
| 656 | -1, /* ast_bool_constant doesn't conv to ir_expression. */ |
| 657 | -1, /* ast_sequence doesn't convert to ir_expression. */ |
| 658 | }; |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 659 | ir_rvalue *result = NULL; |
Aras Pranckevicius | 1c325af | 2010-07-29 15:35:22 +0300 | [diff] [blame] | 660 | ir_rvalue *op[3]; |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 661 | const struct glsl_type *type = glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 662 | bool error_emitted = false; |
| 663 | YYLTYPE loc; |
| 664 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 665 | loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 666 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 667 | switch (this->oper) { |
Ian Romanick | 6652af3 | 2010-03-09 16:38:02 -0800 | [diff] [blame] | 668 | case ast_assign: { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 669 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 670 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 671 | |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 672 | result = do_assignment(instructions, state, op[0], op[1], |
| 673 | this->subexpressions[0]->get_location()); |
| 674 | error_emitted = result->type->is_error(); |
| 675 | type = result->type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 676 | break; |
Ian Romanick | 6652af3 | 2010-03-09 16:38:02 -0800 | [diff] [blame] | 677 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 678 | |
| 679 | case ast_plus: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 680 | op[0] = this->subexpressions[0]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 681 | |
Carl Worth | c24bcad | 2010-07-21 11:23:51 -0700 | [diff] [blame] | 682 | type = unary_arithmetic_result_type(op[0]->type, state, & loc); |
| 683 | |
| 684 | error_emitted = type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 685 | |
| 686 | result = op[0]; |
| 687 | break; |
| 688 | |
| 689 | case ast_neg: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 690 | op[0] = this->subexpressions[0]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 691 | |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 692 | type = unary_arithmetic_result_type(op[0]->type, state, & loc); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 693 | |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 694 | error_emitted = type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 695 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 696 | result = new(ctx) ir_expression(operations[this->oper], type, |
| 697 | op[0], NULL); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 698 | break; |
| 699 | |
| 700 | case ast_add: |
| 701 | case ast_sub: |
| 702 | case ast_mul: |
| 703 | case ast_div: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 704 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 705 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 706 | |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 707 | type = arithmetic_result_type(op[0], op[1], |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 708 | (this->oper == ast_mul), |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 709 | state, & loc); |
| 710 | error_emitted = type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 711 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 712 | result = new(ctx) ir_expression(operations[this->oper], type, |
| 713 | op[0], op[1]); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 714 | break; |
| 715 | |
| 716 | case ast_mod: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 717 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 718 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 719 | |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 720 | type = modulus_result_type(op[0]->type, op[1]->type, state, & loc); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 721 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 722 | assert(operations[this->oper] == ir_binop_mod); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 723 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 724 | result = new(ctx) ir_expression(operations[this->oper], type, |
| 725 | op[0], op[1]); |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 726 | error_emitted = type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 727 | break; |
| 728 | |
| 729 | case ast_lshift: |
| 730 | case ast_rshift: |
Eric Anholt | 183d8c6 | 2010-03-31 16:53:47 -1000 | [diff] [blame] | 731 | _mesa_glsl_error(& loc, state, "FINISHME: implement bit-shift operators"); |
| 732 | error_emitted = true; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 733 | break; |
| 734 | |
| 735 | case ast_less: |
| 736 | case ast_greater: |
| 737 | case ast_lequal: |
| 738 | case ast_gequal: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 739 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 740 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 741 | |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 742 | type = relational_result_type(op[0], op[1], state, & loc); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 743 | |
| 744 | /* The relational operators must either generate an error or result |
| 745 | * in a scalar boolean. See page 57 of the GLSL 1.50 spec. |
| 746 | */ |
Ian Romanick | a43817a | 2010-03-26 14:27:23 -0700 | [diff] [blame] | 747 | assert(type->is_error() |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 748 | || ((type->base_type == GLSL_TYPE_BOOL) |
Ian Romanick | cb36f8a | 2010-03-09 15:51:22 -0800 | [diff] [blame] | 749 | && type->is_scalar())); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 750 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 751 | result = new(ctx) ir_expression(operations[this->oper], type, |
| 752 | op[0], op[1]); |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 753 | error_emitted = type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 754 | break; |
| 755 | |
| 756 | case ast_nequal: |
| 757 | case ast_equal: |
Ian Romanick | 6e659ca | 2010-03-29 15:11:05 -0700 | [diff] [blame] | 758 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 759 | op[1] = this->subexpressions[1]->hir(instructions, state); |
| 760 | |
| 761 | /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec: |
| 762 | * |
| 763 | * "The equality operators equal (==), and not equal (!=) |
| 764 | * operate on all types. They result in a scalar Boolean. If |
| 765 | * the operand types do not match, then there must be a |
| 766 | * conversion from Section 4.1.10 "Implicit Conversions" |
| 767 | * applied to one operand that can make them match, in which |
| 768 | * case this conversion is done." |
| 769 | */ |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 770 | if ((!apply_implicit_conversion(op[0]->type, op[1], state) |
| 771 | && !apply_implicit_conversion(op[1]->type, op[0], state)) |
Ian Romanick | 212b032 | 2010-03-29 16:22:38 -0700 | [diff] [blame] | 772 | || (op[0]->type != op[1]->type)) { |
Ian Romanick | 6e659ca | 2010-03-29 15:11:05 -0700 | [diff] [blame] | 773 | _mesa_glsl_error(& loc, state, "operands of `%s' must have the same " |
| 774 | "type", (this->oper == ast_equal) ? "==" : "!="); |
| 775 | error_emitted = true; |
Ian Romanick | a80cbd6 | 2010-03-30 17:04:48 -0700 | [diff] [blame] | 776 | } else if ((state->language_version <= 110) |
| 777 | && (op[0]->type->is_array() || op[1]->type->is_array())) { |
| 778 | _mesa_glsl_error(& loc, state, "array comparisons forbidden in " |
| 779 | "GLSL 1.10"); |
| 780 | error_emitted = true; |
Ian Romanick | 6e659ca | 2010-03-29 15:11:05 -0700 | [diff] [blame] | 781 | } |
| 782 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 783 | result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type, |
| 784 | op[0], op[1]); |
Ian Romanick | 6e659ca | 2010-03-29 15:11:05 -0700 | [diff] [blame] | 785 | type = glsl_type::bool_type; |
| 786 | |
| 787 | assert(result->type == glsl_type::bool_type); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 788 | break; |
| 789 | |
| 790 | case ast_bit_and: |
| 791 | case ast_bit_xor: |
| 792 | case ast_bit_or: |
| 793 | case ast_bit_not: |
Eric Anholt | 183d8c6 | 2010-03-31 16:53:47 -1000 | [diff] [blame] | 794 | _mesa_glsl_error(& loc, state, "FINISHME: implement bit-wise operators"); |
| 795 | error_emitted = true; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 796 | break; |
| 797 | |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 798 | case ast_logic_and: { |
Eric Anholt | b82c0c3 | 2010-03-31 09:11:39 -1000 | [diff] [blame] | 799 | op[0] = this->subexpressions[0]->hir(instructions, state); |
Eric Anholt | b82c0c3 | 2010-03-31 09:11:39 -1000 | [diff] [blame] | 800 | |
| 801 | if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) { |
| 802 | YYLTYPE loc = this->subexpressions[0]->get_location(); |
| 803 | |
| 804 | _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean", |
| 805 | operator_string(this->oper)); |
Eric Anholt | ebbf14b | 2010-03-31 17:05:32 -1000 | [diff] [blame] | 806 | error_emitted = true; |
Eric Anholt | b82c0c3 | 2010-03-31 09:11:39 -1000 | [diff] [blame] | 807 | } |
| 808 | |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 809 | ir_constant *op0_const = op[0]->constant_expression_value(); |
| 810 | if (op0_const) { |
| 811 | if (op0_const->value.b[0]) { |
| 812 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 813 | |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 814 | if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) { |
| 815 | YYLTYPE loc = this->subexpressions[1]->get_location(); |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 816 | |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 817 | _mesa_glsl_error(& loc, state, |
| 818 | "RHS of `%s' must be scalar boolean", |
| 819 | operator_string(this->oper)); |
| 820 | error_emitted = true; |
| 821 | } |
| 822 | result = op[1]; |
| 823 | } else { |
| 824 | result = op0_const; |
| 825 | } |
| 826 | type = glsl_type::bool_type; |
| 827 | } else { |
Ian Romanick | 81d664f | 2010-07-12 15:18:55 -0700 | [diff] [blame] | 828 | ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type, |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 829 | "and_tmp", |
| 830 | ir_var_temporary); |
Ian Romanick | 81d664f | 2010-07-12 15:18:55 -0700 | [diff] [blame] | 831 | instructions->push_tail(tmp); |
| 832 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 833 | ir_if *const stmt = new(ctx) ir_if(op[0]); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 834 | instructions->push_tail(stmt); |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 835 | |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 836 | op[1] = this->subexpressions[1]->hir(&stmt->then_instructions, state); |
Eric Anholt | b82c0c3 | 2010-03-31 09:11:39 -1000 | [diff] [blame] | 837 | |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 838 | if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) { |
| 839 | YYLTYPE loc = this->subexpressions[1]->get_location(); |
| 840 | |
| 841 | _mesa_glsl_error(& loc, state, |
| 842 | "RHS of `%s' must be scalar boolean", |
| 843 | operator_string(this->oper)); |
| 844 | error_emitted = true; |
| 845 | } |
| 846 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 847 | ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 848 | ir_assignment *const then_assign = |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 849 | new(ctx) ir_assignment(then_deref, op[1], NULL); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 850 | stmt->then_instructions.push_tail(then_assign); |
| 851 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 852 | ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 853 | ir_assignment *const else_assign = |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 854 | new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false), NULL); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 855 | stmt->else_instructions.push_tail(else_assign); |
| 856 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 857 | result = new(ctx) ir_dereference_variable(tmp); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 858 | type = tmp->type; |
Eric Anholt | b82c0c3 | 2010-03-31 09:11:39 -1000 | [diff] [blame] | 859 | } |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 860 | break; |
| 861 | } |
| 862 | |
| 863 | case ast_logic_or: { |
| 864 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 865 | |
| 866 | if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) { |
| 867 | YYLTYPE loc = this->subexpressions[0]->get_location(); |
| 868 | |
| 869 | _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean", |
| 870 | operator_string(this->oper)); |
| 871 | error_emitted = true; |
| 872 | } |
| 873 | |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 874 | ir_constant *op0_const = op[0]->constant_expression_value(); |
| 875 | if (op0_const) { |
| 876 | if (op0_const->value.b[0]) { |
| 877 | result = op0_const; |
| 878 | } else { |
| 879 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 880 | |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 881 | if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) { |
| 882 | YYLTYPE loc = this->subexpressions[1]->get_location(); |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 883 | |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 884 | _mesa_glsl_error(& loc, state, |
| 885 | "RHS of `%s' must be scalar boolean", |
| 886 | operator_string(this->oper)); |
| 887 | error_emitted = true; |
| 888 | } |
| 889 | result = op[1]; |
| 890 | } |
| 891 | type = glsl_type::bool_type; |
| 892 | } else { |
Kenneth Graunke | dfd30ca | 2010-07-08 12:40:52 -0700 | [diff] [blame] | 893 | ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type, |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 894 | "or_tmp", |
| 895 | ir_var_temporary); |
Ian Romanick | 0b9ae3b | 2010-07-12 14:22:05 -0700 | [diff] [blame] | 896 | instructions->push_tail(tmp); |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 897 | |
Ian Romanick | 81d664f | 2010-07-12 15:18:55 -0700 | [diff] [blame] | 898 | ir_if *const stmt = new(ctx) ir_if(op[0]); |
| 899 | instructions->push_tail(stmt); |
| 900 | |
Eric Anholt | a0879b9 | 2010-07-22 16:30:41 -0700 | [diff] [blame] | 901 | op[1] = this->subexpressions[1]->hir(&stmt->else_instructions, state); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 902 | |
| 903 | if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) { |
| 904 | YYLTYPE loc = this->subexpressions[1]->get_location(); |
| 905 | |
| 906 | _mesa_glsl_error(& loc, state, "RHS of `%s' must be scalar boolean", |
| 907 | operator_string(this->oper)); |
| 908 | error_emitted = true; |
| 909 | } |
| 910 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 911 | ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 912 | ir_assignment *const then_assign = |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 913 | new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true), NULL); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 914 | stmt->then_instructions.push_tail(then_assign); |
| 915 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 916 | ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 917 | ir_assignment *const else_assign = |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 918 | new(ctx) ir_assignment(else_deref, op[1], NULL); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 919 | stmt->else_instructions.push_tail(else_assign); |
| 920 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 921 | result = new(ctx) ir_dereference_variable(tmp); |
Eric Anholt | 44b694e | 2010-04-16 13:49:04 -0700 | [diff] [blame] | 922 | type = tmp->type; |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 923 | } |
Eric Anholt | 4950a68 | 2010-04-16 01:10:32 -0700 | [diff] [blame] | 924 | break; |
| 925 | } |
| 926 | |
| 927 | case ast_logic_xor: |
| 928 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 929 | op[1] = this->subexpressions[1]->hir(instructions, state); |
| 930 | |
| 931 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 932 | result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type, |
| 933 | op[0], op[1]); |
Eric Anholt | ebbf14b | 2010-03-31 17:05:32 -1000 | [diff] [blame] | 934 | type = glsl_type::bool_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 935 | break; |
| 936 | |
Eric Anholt | a5827fe | 2010-03-31 16:50:55 -1000 | [diff] [blame] | 937 | case ast_logic_not: |
| 938 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 939 | |
| 940 | if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) { |
| 941 | YYLTYPE loc = this->subexpressions[0]->get_location(); |
| 942 | |
| 943 | _mesa_glsl_error(& loc, state, |
| 944 | "operand of `!' must be scalar boolean"); |
Eric Anholt | ebbf14b | 2010-03-31 17:05:32 -1000 | [diff] [blame] | 945 | error_emitted = true; |
Eric Anholt | a5827fe | 2010-03-31 16:50:55 -1000 | [diff] [blame] | 946 | } |
| 947 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 948 | result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type, |
| 949 | op[0], NULL); |
Eric Anholt | ebbf14b | 2010-03-31 17:05:32 -1000 | [diff] [blame] | 950 | type = glsl_type::bool_type; |
Eric Anholt | a5827fe | 2010-03-31 16:50:55 -1000 | [diff] [blame] | 951 | break; |
| 952 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 953 | case ast_mul_assign: |
| 954 | case ast_div_assign: |
| 955 | case ast_add_assign: |
| 956 | case ast_sub_assign: { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 957 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 958 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 959 | |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 960 | type = arithmetic_result_type(op[0], op[1], |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 961 | (this->oper == ast_mul_assign), |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 962 | state, & loc); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 963 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 964 | ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type, |
| 965 | op[0], op[1]); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 966 | |
Eric Anholt | 3e24ef6 | 2010-06-23 12:40:17 -0700 | [diff] [blame] | 967 | result = do_assignment(instructions, state, |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 968 | op[0]->clone(ctx, NULL), temp_rhs, |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 969 | this->subexpressions[0]->get_location()); |
| 970 | type = result->type; |
| 971 | error_emitted = (op[0]->type->is_error()); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 972 | |
| 973 | /* GLSL 1.10 does not allow array assignment. However, we don't have to |
| 974 | * explicitly test for this because none of the binary expression |
| 975 | * operators allow array operands either. |
| 976 | */ |
| 977 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 978 | break; |
| 979 | } |
| 980 | |
Eric Anholt | 48a0e64 | 2010-03-26 11:57:46 -0700 | [diff] [blame] | 981 | case ast_mod_assign: { |
| 982 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 983 | op[1] = this->subexpressions[1]->hir(instructions, state); |
| 984 | |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 985 | type = modulus_result_type(op[0]->type, op[1]->type, state, & loc); |
Eric Anholt | 48a0e64 | 2010-03-26 11:57:46 -0700 | [diff] [blame] | 986 | |
| 987 | assert(operations[this->oper] == ir_binop_mod); |
| 988 | |
Ian Romanick | 768b55a | 2010-08-13 16:46:43 -0700 | [diff] [blame] | 989 | ir_rvalue *temp_rhs; |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 990 | temp_rhs = new(ctx) ir_expression(operations[this->oper], type, |
| 991 | op[0], op[1]); |
Eric Anholt | 48a0e64 | 2010-03-26 11:57:46 -0700 | [diff] [blame] | 992 | |
Eric Anholt | 3e24ef6 | 2010-06-23 12:40:17 -0700 | [diff] [blame] | 993 | result = do_assignment(instructions, state, |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 994 | op[0]->clone(ctx, NULL), temp_rhs, |
Eric Anholt | 48a0e64 | 2010-03-26 11:57:46 -0700 | [diff] [blame] | 995 | this->subexpressions[0]->get_location()); |
| 996 | type = result->type; |
Eric Anholt | 65e1a7ac | 2010-03-31 16:45:20 -1000 | [diff] [blame] | 997 | error_emitted = type->is_error(); |
Eric Anholt | 48a0e64 | 2010-03-26 11:57:46 -0700 | [diff] [blame] | 998 | break; |
| 999 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1000 | |
| 1001 | case ast_ls_assign: |
| 1002 | case ast_rs_assign: |
Eric Anholt | 183d8c6 | 2010-03-31 16:53:47 -1000 | [diff] [blame] | 1003 | _mesa_glsl_error(& loc, state, |
| 1004 | "FINISHME: implement bit-shift assignment operators"); |
| 1005 | error_emitted = true; |
Ian Romanick | 251eb75 | 2010-03-29 14:15:05 -0700 | [diff] [blame] | 1006 | break; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1007 | |
| 1008 | case ast_and_assign: |
| 1009 | case ast_xor_assign: |
| 1010 | case ast_or_assign: |
Eric Anholt | 183d8c6 | 2010-03-31 16:53:47 -1000 | [diff] [blame] | 1011 | _mesa_glsl_error(& loc, state, |
| 1012 | "FINISHME: implement logic assignment operators"); |
| 1013 | error_emitted = true; |
Ian Romanick | 251eb75 | 2010-03-29 14:15:05 -0700 | [diff] [blame] | 1014 | break; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1015 | |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 1016 | case ast_conditional: { |
| 1017 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1018 | |
| 1019 | /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec: |
| 1020 | * |
| 1021 | * "The ternary selection operator (?:). It operates on three |
| 1022 | * expressions (exp1 ? exp2 : exp3). This operator evaluates the |
| 1023 | * first expression, which must result in a scalar Boolean." |
| 1024 | */ |
| 1025 | if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) { |
| 1026 | YYLTYPE loc = this->subexpressions[0]->get_location(); |
| 1027 | |
| 1028 | _mesa_glsl_error(& loc, state, "?: condition must be scalar boolean"); |
| 1029 | error_emitted = true; |
| 1030 | } |
| 1031 | |
| 1032 | /* The :? operator is implemented by generating an anonymous temporary |
| 1033 | * followed by an if-statement. The last instruction in each branch of |
| 1034 | * the if-statement assigns a value to the anonymous temporary. This |
| 1035 | * temporary is the r-value of the expression. |
| 1036 | */ |
Ian Romanick | 0ad76c6 | 2010-06-11 12:56:26 -0700 | [diff] [blame] | 1037 | exec_list then_instructions; |
| 1038 | exec_list else_instructions; |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 1039 | |
Ian Romanick | 0ad76c6 | 2010-06-11 12:56:26 -0700 | [diff] [blame] | 1040 | op[1] = this->subexpressions[1]->hir(&then_instructions, state); |
| 1041 | op[2] = this->subexpressions[2]->hir(&else_instructions, state); |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 1042 | |
| 1043 | /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec: |
| 1044 | * |
| 1045 | * "The second and third expressions can be any type, as |
| 1046 | * long their types match, or there is a conversion in |
| 1047 | * Section 4.1.10 "Implicit Conversions" that can be applied |
| 1048 | * to one of the expressions to make their types match. This |
| 1049 | * resulting matching type is the type of the entire |
| 1050 | * expression." |
| 1051 | */ |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 1052 | if ((!apply_implicit_conversion(op[1]->type, op[2], state) |
| 1053 | && !apply_implicit_conversion(op[2]->type, op[1], state)) |
Ian Romanick | db9be2e | 2010-03-29 16:25:56 -0700 | [diff] [blame] | 1054 | || (op[1]->type != op[2]->type)) { |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 1055 | YYLTYPE loc = this->subexpressions[1]->get_location(); |
| 1056 | |
| 1057 | _mesa_glsl_error(& loc, state, "Second and third operands of ?: " |
| 1058 | "operator must have matching types."); |
| 1059 | error_emitted = true; |
Ian Romanick | 0ad76c6 | 2010-06-11 12:56:26 -0700 | [diff] [blame] | 1060 | type = glsl_type::error_type; |
Ian Romanick | db9be2e | 2010-03-29 16:25:56 -0700 | [diff] [blame] | 1061 | } else { |
Ian Romanick | 0ad76c6 | 2010-06-11 12:56:26 -0700 | [diff] [blame] | 1062 | type = op[1]->type; |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1065 | ir_constant *cond_val = op[0]->constant_expression_value(); |
| 1066 | ir_constant *then_val = op[1]->constant_expression_value(); |
| 1067 | ir_constant *else_val = op[2]->constant_expression_value(); |
Ian Romanick | 0ad76c6 | 2010-06-11 12:56:26 -0700 | [diff] [blame] | 1068 | |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1069 | if (then_instructions.is_empty() |
| 1070 | && else_instructions.is_empty() |
| 1071 | && (cond_val != NULL) && (then_val != NULL) && (else_val != NULL)) { |
| 1072 | result = (cond_val->value.b[0]) ? then_val : else_val; |
| 1073 | } else { |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 1074 | ir_variable *const tmp = |
| 1075 | new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary); |
Ian Romanick | 0b9ae3b | 2010-07-12 14:22:05 -0700 | [diff] [blame] | 1076 | instructions->push_tail(tmp); |
Ian Romanick | 0ad76c6 | 2010-06-11 12:56:26 -0700 | [diff] [blame] | 1077 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1078 | ir_if *const stmt = new(ctx) ir_if(op[0]); |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1079 | instructions->push_tail(stmt); |
Ian Romanick | 0ad76c6 | 2010-06-11 12:56:26 -0700 | [diff] [blame] | 1080 | |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1081 | then_instructions.move_nodes_to(& stmt->then_instructions); |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1082 | ir_dereference *const then_deref = |
| 1083 | new(ctx) ir_dereference_variable(tmp); |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1084 | ir_assignment *const then_assign = |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1085 | new(ctx) ir_assignment(then_deref, op[1], NULL); |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1086 | stmt->then_instructions.push_tail(then_assign); |
Ian Romanick | 0ad76c6 | 2010-06-11 12:56:26 -0700 | [diff] [blame] | 1087 | |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1088 | else_instructions.move_nodes_to(& stmt->else_instructions); |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1089 | ir_dereference *const else_deref = |
| 1090 | new(ctx) ir_dereference_variable(tmp); |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1091 | ir_assignment *const else_assign = |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1092 | new(ctx) ir_assignment(else_deref, op[2], NULL); |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1093 | stmt->else_instructions.push_tail(else_assign); |
| 1094 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1095 | result = new(ctx) ir_dereference_variable(tmp); |
Ian Romanick | 7825d3d | 2010-06-11 13:45:51 -0700 | [diff] [blame] | 1096 | } |
Ian Romanick | 251eb75 | 2010-03-29 14:15:05 -0700 | [diff] [blame] | 1097 | break; |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 1098 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1099 | |
| 1100 | case ast_pre_inc: |
Eric Anholt | 76ea56c | 2010-03-26 12:16:54 -0700 | [diff] [blame] | 1101 | case ast_pre_dec: { |
| 1102 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1103 | if (op[0]->type->base_type == GLSL_TYPE_FLOAT) |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1104 | op[1] = new(ctx) ir_constant(1.0f); |
Eric Anholt | 76ea56c | 2010-03-26 12:16:54 -0700 | [diff] [blame] | 1105 | else |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1106 | op[1] = new(ctx) ir_constant(1); |
Eric Anholt | 76ea56c | 2010-03-26 12:16:54 -0700 | [diff] [blame] | 1107 | |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 1108 | type = arithmetic_result_type(op[0], op[1], false, state, & loc); |
Eric Anholt | 76ea56c | 2010-03-26 12:16:54 -0700 | [diff] [blame] | 1109 | |
Ian Romanick | 768b55a | 2010-08-13 16:46:43 -0700 | [diff] [blame] | 1110 | ir_rvalue *temp_rhs; |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1111 | temp_rhs = new(ctx) ir_expression(operations[this->oper], type, |
| 1112 | op[0], op[1]); |
Eric Anholt | 76ea56c | 2010-03-26 12:16:54 -0700 | [diff] [blame] | 1113 | |
Eric Anholt | 3e24ef6 | 2010-06-23 12:40:17 -0700 | [diff] [blame] | 1114 | result = do_assignment(instructions, state, |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 1115 | op[0]->clone(ctx, NULL), temp_rhs, |
Eric Anholt | 76ea56c | 2010-03-26 12:16:54 -0700 | [diff] [blame] | 1116 | this->subexpressions[0]->get_location()); |
| 1117 | type = result->type; |
| 1118 | error_emitted = op[0]->type->is_error(); |
| 1119 | break; |
| 1120 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1121 | |
| 1122 | case ast_post_inc: |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1123 | case ast_post_dec: { |
| 1124 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 1125 | if (op[0]->type->base_type == GLSL_TYPE_FLOAT) |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1126 | op[1] = new(ctx) ir_constant(1.0f); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1127 | else |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1128 | op[1] = new(ctx) ir_constant(1); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1129 | |
| 1130 | error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); |
| 1131 | |
Eric Anholt | a13bb14 | 2010-03-31 16:38:11 -1000 | [diff] [blame] | 1132 | type = arithmetic_result_type(op[0], op[1], false, state, & loc); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1133 | |
Ian Romanick | 768b55a | 2010-08-13 16:46:43 -0700 | [diff] [blame] | 1134 | ir_rvalue *temp_rhs; |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1135 | temp_rhs = new(ctx) ir_expression(operations[this->oper], type, |
| 1136 | op[0], op[1]); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1137 | |
| 1138 | /* Get a temporary of a copy of the lvalue before it's modified. |
| 1139 | * This may get thrown away later. |
| 1140 | */ |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 1141 | result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL)); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1142 | |
Eric Anholt | 3e24ef6 | 2010-06-23 12:40:17 -0700 | [diff] [blame] | 1143 | (void)do_assignment(instructions, state, |
Eric Anholt | 8273bd4 | 2010-08-04 12:34:56 -0700 | [diff] [blame] | 1144 | op[0]->clone(ctx, NULL), temp_rhs, |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1145 | this->subexpressions[0]->get_location()); |
| 1146 | |
| 1147 | type = result->type; |
| 1148 | error_emitted = op[0]->type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1149 | break; |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 1150 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1151 | |
| 1152 | case ast_field_selection: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1153 | result = _mesa_ast_field_selection_to_hir(this, instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1154 | type = result->type; |
| 1155 | break; |
| 1156 | |
Ian Romanick | 27e3cf8 | 2010-04-01 18:03:59 -0700 | [diff] [blame] | 1157 | case ast_array_index: { |
| 1158 | YYLTYPE index_loc = subexpressions[1]->get_location(); |
| 1159 | |
| 1160 | op[0] = subexpressions[0]->hir(instructions, state); |
| 1161 | op[1] = subexpressions[1]->hir(instructions, state); |
| 1162 | |
| 1163 | error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); |
| 1164 | |
Ian Romanick | a9159f9 | 2010-05-26 15:08:11 -0700 | [diff] [blame] | 1165 | ir_rvalue *const array = op[0]; |
Ian Romanick | b8a21cc | 2010-04-01 18:31:11 -0700 | [diff] [blame] | 1166 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1167 | result = new(ctx) ir_dereference_array(op[0], op[1]); |
Ian Romanick | b8a21cc | 2010-04-01 18:31:11 -0700 | [diff] [blame] | 1168 | |
| 1169 | /* Do not use op[0] after this point. Use array. |
| 1170 | */ |
| 1171 | op[0] = NULL; |
| 1172 | |
Ian Romanick | 27e3cf8 | 2010-04-01 18:03:59 -0700 | [diff] [blame] | 1173 | |
| 1174 | if (error_emitted) |
| 1175 | break; |
| 1176 | |
Ian Romanick | 63038e1 | 2010-04-05 13:16:00 -0700 | [diff] [blame] | 1177 | if (!array->type->is_array() |
| 1178 | && !array->type->is_matrix() |
| 1179 | && !array->type->is_vector()) { |
Ian Romanick | 27e3cf8 | 2010-04-01 18:03:59 -0700 | [diff] [blame] | 1180 | _mesa_glsl_error(& index_loc, state, |
Ian Romanick | 63038e1 | 2010-04-05 13:16:00 -0700 | [diff] [blame] | 1181 | "cannot dereference non-array / non-matrix / " |
| 1182 | "non-vector"); |
Ian Romanick | 27e3cf8 | 2010-04-01 18:03:59 -0700 | [diff] [blame] | 1183 | error_emitted = true; |
| 1184 | } |
| 1185 | |
| 1186 | if (!op[1]->type->is_integer()) { |
| 1187 | _mesa_glsl_error(& index_loc, state, |
| 1188 | "array index must be integer type"); |
| 1189 | error_emitted = true; |
| 1190 | } else if (!op[1]->type->is_scalar()) { |
| 1191 | _mesa_glsl_error(& index_loc, state, |
| 1192 | "array index must be scalar"); |
| 1193 | error_emitted = true; |
| 1194 | } |
| 1195 | |
| 1196 | /* If the array index is a constant expression and the array has a |
| 1197 | * declared size, ensure that the access is in-bounds. If the array |
| 1198 | * index is not a constant expression, ensure that the array has a |
| 1199 | * declared size. |
| 1200 | */ |
| 1201 | ir_constant *const const_index = op[1]->constant_expression_value(); |
| 1202 | if (const_index != NULL) { |
| 1203 | const int idx = const_index->value.i[0]; |
Ian Romanick | 63038e1 | 2010-04-05 13:16:00 -0700 | [diff] [blame] | 1204 | const char *type_name; |
| 1205 | unsigned bound = 0; |
| 1206 | |
| 1207 | if (array->type->is_matrix()) { |
| 1208 | type_name = "matrix"; |
| 1209 | } else if (array->type->is_vector()) { |
| 1210 | type_name = "vector"; |
| 1211 | } else { |
| 1212 | type_name = "array"; |
| 1213 | } |
Ian Romanick | 27e3cf8 | 2010-04-01 18:03:59 -0700 | [diff] [blame] | 1214 | |
| 1215 | /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec: |
| 1216 | * |
| 1217 | * "It is illegal to declare an array with a size, and then |
| 1218 | * later (in the same shader) index the same array with an |
| 1219 | * integral constant expression greater than or equal to the |
| 1220 | * declared size. It is also illegal to index an array with a |
| 1221 | * negative constant expression." |
| 1222 | */ |
Ian Romanick | 63038e1 | 2010-04-05 13:16:00 -0700 | [diff] [blame] | 1223 | if (array->type->is_matrix()) { |
| 1224 | if (array->type->row_type()->vector_elements <= idx) { |
| 1225 | bound = array->type->row_type()->vector_elements; |
| 1226 | } |
| 1227 | } else if (array->type->is_vector()) { |
| 1228 | if (array->type->vector_elements <= idx) { |
| 1229 | bound = array->type->vector_elements; |
| 1230 | } |
| 1231 | } else { |
| 1232 | if ((array->type->array_size() > 0) |
| 1233 | && (array->type->array_size() <= idx)) { |
| 1234 | bound = array->type->array_size(); |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | if (bound > 0) { |
| 1239 | _mesa_glsl_error(& loc, state, "%s index must be < %u", |
| 1240 | type_name, bound); |
| 1241 | error_emitted = true; |
| 1242 | } else if (idx < 0) { |
| 1243 | _mesa_glsl_error(& loc, state, "%s index must be >= 0", |
| 1244 | type_name); |
Ian Romanick | 27e3cf8 | 2010-04-01 18:03:59 -0700 | [diff] [blame] | 1245 | error_emitted = true; |
| 1246 | } |
| 1247 | |
Ian Romanick | 63038e1 | 2010-04-05 13:16:00 -0700 | [diff] [blame] | 1248 | if (array->type->is_array()) { |
Ian Romanick | a9159f9 | 2010-05-26 15:08:11 -0700 | [diff] [blame] | 1249 | /* If the array is a variable dereference, it dereferences the |
| 1250 | * whole array, by definition. Use this to get the variable. |
| 1251 | * |
| 1252 | * FINISHME: Should some methods for getting / setting / testing |
| 1253 | * FINISHME: array access limits be added to ir_dereference? |
| 1254 | */ |
| 1255 | ir_variable *const v = array->whole_variable_referenced(); |
Ian Romanick | 63038e1 | 2010-04-05 13:16:00 -0700 | [diff] [blame] | 1256 | if ((v != NULL) && (unsigned(idx) > v->max_array_access)) |
| 1257 | v->max_array_access = idx; |
Ian Romanick | 27e3cf8 | 2010-04-01 18:03:59 -0700 | [diff] [blame] | 1258 | } |
Kenneth Graunke | 2b7c42b | 2010-07-16 18:28:44 -0700 | [diff] [blame] | 1259 | } else if (array->type->array_size() == 0) { |
| 1260 | _mesa_glsl_error(&loc, state, "unsized array index must be constant"); |
Eric Anholt | a721abf | 2010-08-23 10:32:01 -0700 | [diff] [blame] | 1261 | } else { |
| 1262 | if (array->type->is_array()) { |
| 1263 | ir_variable *v = array->whole_variable_referenced(); |
| 1264 | v->max_array_access = array->type->array_size(); |
| 1265 | } |
Ian Romanick | 27e3cf8 | 2010-04-01 18:03:59 -0700 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | if (error_emitted) |
| 1269 | result->type = glsl_type::error_type; |
| 1270 | |
| 1271 | type = result->type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1272 | break; |
Ian Romanick | 27e3cf8 | 2010-04-01 18:03:59 -0700 | [diff] [blame] | 1273 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1274 | |
| 1275 | case ast_function_call: |
Ian Romanick | 7cfddf1 | 2010-03-10 13:26:52 -0800 | [diff] [blame] | 1276 | /* Should *NEVER* get here. ast_function_call should always be handled |
| 1277 | * by ast_function_expression::hir. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1278 | */ |
Ian Romanick | 7cfddf1 | 2010-03-10 13:26:52 -0800 | [diff] [blame] | 1279 | assert(0); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1280 | break; |
| 1281 | |
| 1282 | case ast_identifier: { |
| 1283 | /* ast_identifier can appear several places in a full abstract syntax |
| 1284 | * tree. This particular use must be at location specified in the grammar |
| 1285 | * as 'variable_identifier'. |
| 1286 | */ |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 1287 | ir_variable *var = |
| 1288 | state->symbols->get_variable(this->primary_expression.identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1289 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1290 | result = new(ctx) ir_dereference_variable(var); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1291 | |
| 1292 | if (var != NULL) { |
| 1293 | type = result->type; |
| 1294 | } else { |
Ian Romanick | 71d0bbf | 2010-03-23 13:21:19 -0700 | [diff] [blame] | 1295 | _mesa_glsl_error(& loc, state, "`%s' undeclared", |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1296 | this->primary_expression.identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1297 | |
| 1298 | error_emitted = true; |
| 1299 | } |
| 1300 | break; |
| 1301 | } |
| 1302 | |
| 1303 | case ast_int_constant: |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 1304 | type = glsl_type::int_type; |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1305 | result = new(ctx) ir_constant(this->primary_expression.int_constant); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1306 | break; |
| 1307 | |
| 1308 | case ast_uint_constant: |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 1309 | type = glsl_type::uint_type; |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1310 | result = new(ctx) ir_constant(this->primary_expression.uint_constant); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1311 | break; |
| 1312 | |
| 1313 | case ast_float_constant: |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 1314 | type = glsl_type::float_type; |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1315 | result = new(ctx) ir_constant(this->primary_expression.float_constant); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1316 | break; |
| 1317 | |
| 1318 | case ast_bool_constant: |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 1319 | type = glsl_type::bool_type; |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1320 | result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant)); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1321 | break; |
| 1322 | |
| 1323 | case ast_sequence: { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1324 | /* It should not be possible to generate a sequence in the AST without |
| 1325 | * any expressions in it. |
| 1326 | */ |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 1327 | assert(!this->expressions.is_empty()); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1328 | |
| 1329 | /* The r-value of a sequence is the last expression in the sequence. If |
| 1330 | * the other expressions in the sequence do not have side-effects (and |
| 1331 | * therefore add instructions to the instruction list), they get dropped |
| 1332 | * on the floor. |
| 1333 | */ |
Ian Romanick | 2b97dc6 | 2010-05-10 17:42:05 -0700 | [diff] [blame] | 1334 | foreach_list_typed (ast_node, ast, link, &this->expressions) |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 1335 | result = ast->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1336 | |
| 1337 | type = result->type; |
| 1338 | |
| 1339 | /* Any errors should have already been emitted in the loop above. |
| 1340 | */ |
| 1341 | error_emitted = true; |
| 1342 | break; |
| 1343 | } |
| 1344 | } |
| 1345 | |
Ian Romanick | cef3bae | 2010-03-26 14:41:32 -0700 | [diff] [blame] | 1346 | if (type->is_error() && !error_emitted) |
Ian Romanick | 71d0bbf | 2010-03-23 13:21:19 -0700 | [diff] [blame] | 1347 | _mesa_glsl_error(& loc, state, "type mismatch"); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1348 | |
| 1349 | return result; |
| 1350 | } |
| 1351 | |
| 1352 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 1353 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1354 | ast_expression_statement::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1355 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1356 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1357 | /* It is possible to have expression statements that don't have an |
| 1358 | * expression. This is the solitary semicolon: |
| 1359 | * |
| 1360 | * for (i = 0; i < 5; i++) |
| 1361 | * ; |
| 1362 | * |
| 1363 | * In this case the expression will be NULL. Test for NULL and don't do |
| 1364 | * anything in that case. |
| 1365 | */ |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1366 | if (expression != NULL) |
| 1367 | expression->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1368 | |
| 1369 | /* Statements do not have r-values. |
| 1370 | */ |
| 1371 | return NULL; |
| 1372 | } |
| 1373 | |
| 1374 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 1375 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1376 | ast_compound_statement::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1377 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1378 | { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1379 | if (new_scope) |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 1380 | state->symbols->push_scope(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1381 | |
Ian Romanick | 2b97dc6 | 2010-05-10 17:42:05 -0700 | [diff] [blame] | 1382 | foreach_list_typed (ast_node, ast, link, &this->statements) |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 1383 | ast->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1384 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1385 | if (new_scope) |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 1386 | state->symbols->pop_scope(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1387 | |
| 1388 | /* Compound statements do not have r-values. |
| 1389 | */ |
| 1390 | return NULL; |
| 1391 | } |
| 1392 | |
| 1393 | |
Ian Romanick | 28009cd | 2010-03-30 16:59:27 -0700 | [diff] [blame] | 1394 | static const glsl_type * |
| 1395 | process_array_type(const glsl_type *base, ast_node *array_size, |
| 1396 | struct _mesa_glsl_parse_state *state) |
| 1397 | { |
| 1398 | unsigned length = 0; |
| 1399 | |
| 1400 | /* FINISHME: Reject delcarations of multidimensional arrays. */ |
| 1401 | |
| 1402 | if (array_size != NULL) { |
| 1403 | exec_list dummy_instructions; |
| 1404 | ir_rvalue *const ir = array_size->hir(& dummy_instructions, state); |
| 1405 | YYLTYPE loc = array_size->get_location(); |
| 1406 | |
| 1407 | /* FINISHME: Verify that the grammar forbids side-effects in array |
| 1408 | * FINISHME: sizes. i.e., 'vec4 [x = 12] data' |
| 1409 | */ |
| 1410 | assert(dummy_instructions.is_empty()); |
| 1411 | |
| 1412 | if (ir != NULL) { |
| 1413 | if (!ir->type->is_integer()) { |
| 1414 | _mesa_glsl_error(& loc, state, "array size must be integer type"); |
| 1415 | } else if (!ir->type->is_scalar()) { |
| 1416 | _mesa_glsl_error(& loc, state, "array size must be scalar type"); |
| 1417 | } else { |
| 1418 | ir_constant *const size = ir->constant_expression_value(); |
| 1419 | |
| 1420 | if (size == NULL) { |
| 1421 | _mesa_glsl_error(& loc, state, "array size must be a " |
| 1422 | "constant valued expression"); |
| 1423 | } else if (size->value.i[0] <= 0) { |
| 1424 | _mesa_glsl_error(& loc, state, "array size must be > 0"); |
| 1425 | } else { |
| 1426 | assert(size->type == ir->type); |
| 1427 | length = size->value.u[0]; |
| 1428 | } |
| 1429 | } |
| 1430 | } |
| 1431 | } |
| 1432 | |
Ian Romanick | f38d15b | 2010-07-20 15:33:40 -0700 | [diff] [blame] | 1433 | return glsl_type::get_array_instance(base, length); |
Ian Romanick | 28009cd | 2010-03-30 16:59:27 -0700 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | |
Ian Romanick | d612a12 | 2010-03-31 16:22:06 -0700 | [diff] [blame] | 1437 | const glsl_type * |
| 1438 | ast_type_specifier::glsl_type(const char **name, |
| 1439 | struct _mesa_glsl_parse_state *state) const |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1440 | { |
Ian Romanick | d612a12 | 2010-03-31 16:22:06 -0700 | [diff] [blame] | 1441 | const struct glsl_type *type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1442 | |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 1443 | if ((this->type_specifier == ast_struct) && (this->type_name == NULL)) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1444 | /* FINISHME: Handle annonymous structures. */ |
| 1445 | type = NULL; |
| 1446 | } else { |
Ian Romanick | d612a12 | 2010-03-31 16:22:06 -0700 | [diff] [blame] | 1447 | type = state->symbols->get_type(this->type_name); |
| 1448 | *name = this->type_name; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1449 | |
Ian Romanick | d612a12 | 2010-03-31 16:22:06 -0700 | [diff] [blame] | 1450 | if (this->is_array) { |
| 1451 | type = process_array_type(type, this->array_size, state); |
Ian Romanick | 28009cd | 2010-03-30 16:59:27 -0700 | [diff] [blame] | 1452 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1453 | } |
| 1454 | |
| 1455 | return type; |
| 1456 | } |
| 1457 | |
| 1458 | |
| 1459 | static void |
| 1460 | apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual, |
Ian Romanick | 768b55a | 2010-08-13 16:46:43 -0700 | [diff] [blame] | 1461 | ir_variable *var, |
Eric Anholt | 2e063f1 | 2010-03-28 00:56:22 -0700 | [diff] [blame] | 1462 | struct _mesa_glsl_parse_state *state, |
| 1463 | YYLTYPE *loc) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1464 | { |
| 1465 | if (qual->invariant) |
| 1466 | var->invariant = 1; |
| 1467 | |
| 1468 | /* FINISHME: Mark 'in' variables at global scope as read-only. */ |
| 1469 | if (qual->constant || qual->attribute || qual->uniform |
| 1470 | || (qual->varying && (state->target == fragment_shader))) |
| 1471 | var->read_only = 1; |
| 1472 | |
| 1473 | if (qual->centroid) |
| 1474 | var->centroid = 1; |
| 1475 | |
Ian Romanick | ae4c4c0 | 2010-04-07 16:41:40 -0700 | [diff] [blame] | 1476 | if (qual->attribute && state->target != vertex_shader) { |
Eric Anholt | 2e063f1 | 2010-03-28 00:56:22 -0700 | [diff] [blame] | 1477 | var->type = glsl_type::error_type; |
| 1478 | _mesa_glsl_error(loc, state, |
| 1479 | "`attribute' variables may not be declared in the " |
Ian Romanick | ae4c4c0 | 2010-04-07 16:41:40 -0700 | [diff] [blame] | 1480 | "%s shader", |
| 1481 | _mesa_glsl_shader_target_name(state->target)); |
Eric Anholt | 2e063f1 | 2010-03-28 00:56:22 -0700 | [diff] [blame] | 1482 | } |
| 1483 | |
Eric Anholt | 90b7825 | 2010-03-31 21:21:20 -1000 | [diff] [blame] | 1484 | /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec: |
| 1485 | * |
| 1486 | * "The varying qualifier can be used only with the data types |
| 1487 | * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of |
| 1488 | * these." |
| 1489 | */ |
Eric Anholt | 0ca1719 | 2010-05-19 13:38:15 -0700 | [diff] [blame] | 1490 | if (qual->varying) { |
| 1491 | const glsl_type *non_array_type; |
| 1492 | |
| 1493 | if (var->type && var->type->is_array()) |
| 1494 | non_array_type = var->type->fields.array; |
| 1495 | else |
| 1496 | non_array_type = var->type; |
| 1497 | |
| 1498 | if (non_array_type && non_array_type->base_type != GLSL_TYPE_FLOAT) { |
| 1499 | var->type = glsl_type::error_type; |
| 1500 | _mesa_glsl_error(loc, state, |
| 1501 | "varying variables must be of base type float"); |
| 1502 | } |
Eric Anholt | 90b7825 | 2010-03-31 21:21:20 -1000 | [diff] [blame] | 1503 | } |
| 1504 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 1505 | /* If there is no qualifier that changes the mode of the variable, leave |
| 1506 | * the setting alone. |
| 1507 | */ |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1508 | if (qual->in && qual->out) |
| 1509 | var->mode = ir_var_inout; |
| 1510 | else if (qual->attribute || qual->in |
| 1511 | || (qual->varying && (state->target == fragment_shader))) |
| 1512 | var->mode = ir_var_in; |
Ian Romanick | 0b67823 | 2010-03-10 00:28:59 -0800 | [diff] [blame] | 1513 | else if (qual->out || (qual->varying && (state->target == vertex_shader))) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1514 | var->mode = ir_var_out; |
| 1515 | else if (qual->uniform) |
| 1516 | var->mode = ir_var_uniform; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1517 | |
| 1518 | if (qual->flat) |
| 1519 | var->interpolation = ir_var_flat; |
| 1520 | else if (qual->noperspective) |
| 1521 | var->interpolation = ir_var_noperspective; |
| 1522 | else |
| 1523 | var->interpolation = ir_var_smooth; |
Ian Romanick | 9d97537 | 2010-04-02 17:17:47 -0700 | [diff] [blame] | 1524 | |
Eric Anholt | 4a96217 | 2010-07-28 14:41:51 -0700 | [diff] [blame] | 1525 | var->pixel_center_integer = qual->pixel_center_integer; |
| 1526 | var->origin_upper_left = qual->origin_upper_left; |
Ian Romanick | 8d8469e | 2010-06-30 17:48:09 -0700 | [diff] [blame] | 1527 | if ((qual->origin_upper_left || qual->pixel_center_integer) |
| 1528 | && (strcmp(var->name, "gl_FragCoord") != 0)) { |
| 1529 | const char *const qual_string = (qual->origin_upper_left) |
| 1530 | ? "origin_upper_left" : "pixel_center_integer"; |
| 1531 | |
| 1532 | _mesa_glsl_error(loc, state, |
| 1533 | "layout qualifier `%s' can only be applied to " |
| 1534 | "fragment shader input `gl_FragCoord'", |
| 1535 | qual_string); |
| 1536 | } |
| 1537 | |
Ian Romanick | 9d97537 | 2010-04-02 17:17:47 -0700 | [diff] [blame] | 1538 | if (var->type->is_array() && (state->language_version >= 120)) { |
| 1539 | var->array_lvalue = true; |
| 1540 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 1544 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1545 | ast_declarator_list::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1546 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1547 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 1548 | void *ctx = state; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1549 | const struct glsl_type *decl_type; |
| 1550 | const char *type_name = NULL; |
Eric Anholt | 8558459 | 2010-04-14 15:38:52 -0700 | [diff] [blame] | 1551 | ir_rvalue *result = NULL; |
Ian Romanick | c824e35 | 2010-04-23 15:55:19 -0700 | [diff] [blame] | 1552 | YYLTYPE loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1553 | |
Ian Romanick | 6f0823d | 2010-07-01 20:39:08 -0700 | [diff] [blame] | 1554 | /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec: |
| 1555 | * |
| 1556 | * "To ensure that a particular output variable is invariant, it is |
| 1557 | * necessary to use the invariant qualifier. It can either be used to |
| 1558 | * qualify a previously declared variable as being invariant |
| 1559 | * |
| 1560 | * invariant gl_Position; // make existing gl_Position be invariant" |
| 1561 | * |
| 1562 | * In these cases the parser will set the 'invariant' flag in the declarator |
| 1563 | * list, and the type will be NULL. |
| 1564 | */ |
| 1565 | if (this->invariant) { |
| 1566 | assert(this->type == NULL); |
| 1567 | |
| 1568 | if (state->current_function != NULL) { |
| 1569 | _mesa_glsl_error(& loc, state, |
| 1570 | "All uses of `invariant' keyword must be at global " |
| 1571 | "scope\n"); |
| 1572 | } |
| 1573 | |
| 1574 | foreach_list_typed (ast_declaration, decl, link, &this->declarations) { |
| 1575 | assert(!decl->is_array); |
| 1576 | assert(decl->array_size == NULL); |
| 1577 | assert(decl->initializer == NULL); |
| 1578 | |
| 1579 | ir_variable *const earlier = |
| 1580 | state->symbols->get_variable(decl->identifier); |
| 1581 | if (earlier == NULL) { |
| 1582 | _mesa_glsl_error(& loc, state, |
| 1583 | "Undeclared variable `%s' cannot be marked " |
| 1584 | "invariant\n", decl->identifier); |
| 1585 | } else if ((state->target == vertex_shader) |
| 1586 | && (earlier->mode != ir_var_out)) { |
| 1587 | _mesa_glsl_error(& loc, state, |
| 1588 | "`%s' cannot be marked invariant, vertex shader " |
| 1589 | "outputs only\n", decl->identifier); |
| 1590 | } else if ((state->target == fragment_shader) |
| 1591 | && (earlier->mode != ir_var_in)) { |
| 1592 | _mesa_glsl_error(& loc, state, |
| 1593 | "`%s' cannot be marked invariant, fragment shader " |
| 1594 | "inputs only\n", decl->identifier); |
| 1595 | } else { |
| 1596 | earlier->invariant = true; |
| 1597 | } |
| 1598 | } |
| 1599 | |
| 1600 | /* Invariant redeclarations do not have r-values. |
| 1601 | */ |
| 1602 | return NULL; |
| 1603 | } |
| 1604 | |
| 1605 | assert(this->type != NULL); |
| 1606 | assert(!this->invariant); |
| 1607 | |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 1608 | /* The type specifier may contain a structure definition. Process that |
| 1609 | * before any of the variable declarations. |
| 1610 | */ |
| 1611 | (void) this->type->specifier->hir(instructions, state); |
| 1612 | |
Ian Romanick | d612a12 | 2010-03-31 16:22:06 -0700 | [diff] [blame] | 1613 | decl_type = this->type->specifier->glsl_type(& type_name, state); |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 1614 | if (this->declarations.is_empty()) { |
Ian Romanick | 6f0823d | 2010-07-01 20:39:08 -0700 | [diff] [blame] | 1615 | /* The only valid case where the declaration list can be empty is when |
| 1616 | * the declaration is setting the default precision of a built-in type |
| 1617 | * (e.g., 'precision highp vec4;'). |
Ian Romanick | c824e35 | 2010-04-23 15:55:19 -0700 | [diff] [blame] | 1618 | */ |
| 1619 | |
Ian Romanick | 6f0823d | 2010-07-01 20:39:08 -0700 | [diff] [blame] | 1620 | if (decl_type != NULL) { |
Ian Romanick | c824e35 | 2010-04-23 15:55:19 -0700 | [diff] [blame] | 1621 | } else { |
| 1622 | _mesa_glsl_error(& loc, state, "incomplete declaration"); |
| 1623 | } |
| 1624 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1625 | |
Ian Romanick | 2b97dc6 | 2010-05-10 17:42:05 -0700 | [diff] [blame] | 1626 | foreach_list_typed (ast_declaration, decl, link, &this->declarations) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1627 | const struct glsl_type *var_type; |
Ian Romanick | 768b55a | 2010-08-13 16:46:43 -0700 | [diff] [blame] | 1628 | ir_variable *var; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1629 | |
| 1630 | /* FINISHME: Emit a warning if a variable declaration shadows a |
| 1631 | * FINISHME: declaration at a higher scope. |
| 1632 | */ |
| 1633 | |
Ian Romanick | cec65a6 | 2010-03-23 12:28:44 -0700 | [diff] [blame] | 1634 | if ((decl_type == NULL) || decl_type->is_void()) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1635 | if (type_name != NULL) { |
| 1636 | _mesa_glsl_error(& loc, state, |
| 1637 | "invalid type `%s' in declaration of `%s'", |
| 1638 | type_name, decl->identifier); |
| 1639 | } else { |
| 1640 | _mesa_glsl_error(& loc, state, |
| 1641 | "invalid type in declaration of `%s'", |
| 1642 | decl->identifier); |
| 1643 | } |
| 1644 | continue; |
| 1645 | } |
| 1646 | |
| 1647 | if (decl->is_array) { |
Ian Romanick | 28009cd | 2010-03-30 16:59:27 -0700 | [diff] [blame] | 1648 | var_type = process_array_type(decl_type, decl->array_size, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1649 | } else { |
| 1650 | var_type = decl_type; |
| 1651 | } |
| 1652 | |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 1653 | var = new(ctx) ir_variable(var_type, decl->identifier, ir_var_auto); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1654 | |
Eric Anholt | 3f15150 | 2010-04-02 01:53:57 -1000 | [diff] [blame] | 1655 | /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification; |
| 1656 | * |
| 1657 | * "Global variables can only use the qualifiers const, |
| 1658 | * attribute, uni form, or varying. Only one may be |
| 1659 | * specified. |
| 1660 | * |
| 1661 | * Local variables can only use the qualifier const." |
| 1662 | * |
| 1663 | * This is relaxed in GLSL 1.30. |
| 1664 | */ |
| 1665 | if (state->language_version < 120) { |
| 1666 | if (this->type->qualifier.out) { |
| 1667 | _mesa_glsl_error(& loc, state, |
| 1668 | "`out' qualifier in declaration of `%s' " |
| 1669 | "only valid for function parameters in GLSL 1.10.", |
| 1670 | decl->identifier); |
| 1671 | } |
| 1672 | if (this->type->qualifier.in) { |
| 1673 | _mesa_glsl_error(& loc, state, |
| 1674 | "`in' qualifier in declaration of `%s' " |
| 1675 | "only valid for function parameters in GLSL 1.10.", |
| 1676 | decl->identifier); |
| 1677 | } |
| 1678 | /* FINISHME: Test for other invalid qualifiers. */ |
| 1679 | } |
| 1680 | |
Eric Anholt | 2e063f1 | 2010-03-28 00:56:22 -0700 | [diff] [blame] | 1681 | apply_type_qualifier_to_variable(& this->type->qualifier, var, state, |
| 1682 | & loc); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1683 | |
Ian Romanick | 6f0823d | 2010-07-01 20:39:08 -0700 | [diff] [blame] | 1684 | if (this->type->qualifier.invariant) { |
Eric Anholt | 046bef2 | 2010-08-04 20:33:57 -0700 | [diff] [blame] | 1685 | if ((state->target == vertex_shader) && !(var->mode == ir_var_out || |
| 1686 | var->mode == ir_var_inout)) { |
| 1687 | /* FINISHME: Note that this doesn't work for invariant on |
| 1688 | * a function signature outval |
| 1689 | */ |
Ian Romanick | 6f0823d | 2010-07-01 20:39:08 -0700 | [diff] [blame] | 1690 | _mesa_glsl_error(& loc, state, |
| 1691 | "`%s' cannot be marked invariant, vertex shader " |
| 1692 | "outputs only\n", var->name); |
Eric Anholt | 046bef2 | 2010-08-04 20:33:57 -0700 | [diff] [blame] | 1693 | } else if ((state->target == fragment_shader) && |
| 1694 | !(var->mode == ir_var_in || var->mode == ir_var_inout)) { |
| 1695 | /* FINISHME: Note that this doesn't work for invariant on |
| 1696 | * a function signature inval |
| 1697 | */ |
Ian Romanick | 6f0823d | 2010-07-01 20:39:08 -0700 | [diff] [blame] | 1698 | _mesa_glsl_error(& loc, state, |
| 1699 | "`%s' cannot be marked invariant, fragment shader " |
| 1700 | "inputs only\n", var->name); |
| 1701 | } |
| 1702 | } |
| 1703 | |
Ian Romanick | e1c1a3f | 2010-03-31 12:26:03 -0700 | [diff] [blame] | 1704 | if (state->current_function != NULL) { |
Ian Romanick | b168e53 | 2010-03-31 12:31:18 -0700 | [diff] [blame] | 1705 | const char *mode = NULL; |
Ian Romanick | e080006 | 2010-03-31 13:15:23 -0700 | [diff] [blame] | 1706 | const char *extra = ""; |
Ian Romanick | b168e53 | 2010-03-31 12:31:18 -0700 | [diff] [blame] | 1707 | |
Ian Romanick | e080006 | 2010-03-31 13:15:23 -0700 | [diff] [blame] | 1708 | /* There is no need to check for 'inout' here because the parser will |
| 1709 | * only allow that in function parameter lists. |
Ian Romanick | e1c1a3f | 2010-03-31 12:26:03 -0700 | [diff] [blame] | 1710 | */ |
| 1711 | if (this->type->qualifier.attribute) { |
Ian Romanick | b168e53 | 2010-03-31 12:31:18 -0700 | [diff] [blame] | 1712 | mode = "attribute"; |
| 1713 | } else if (this->type->qualifier.uniform) { |
| 1714 | mode = "uniform"; |
| 1715 | } else if (this->type->qualifier.varying) { |
| 1716 | mode = "varying"; |
Ian Romanick | e080006 | 2010-03-31 13:15:23 -0700 | [diff] [blame] | 1717 | } else if (this->type->qualifier.in) { |
| 1718 | mode = "in"; |
| 1719 | extra = " or in function parameter list"; |
| 1720 | } else if (this->type->qualifier.out) { |
| 1721 | mode = "out"; |
| 1722 | extra = " or in function parameter list"; |
Ian Romanick | b168e53 | 2010-03-31 12:31:18 -0700 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | if (mode) { |
Ian Romanick | e1c1a3f | 2010-03-31 12:26:03 -0700 | [diff] [blame] | 1726 | _mesa_glsl_error(& loc, state, |
Ian Romanick | b168e53 | 2010-03-31 12:31:18 -0700 | [diff] [blame] | 1727 | "%s variable `%s' must be declared at " |
Ian Romanick | e080006 | 2010-03-31 13:15:23 -0700 | [diff] [blame] | 1728 | "global scope%s", |
| 1729 | mode, var->name, extra); |
Ian Romanick | e1c1a3f | 2010-03-31 12:26:03 -0700 | [diff] [blame] | 1730 | } |
| 1731 | } else if (var->mode == ir_var_in) { |
Ian Romanick | fb9f5b0 | 2010-03-29 17:16:35 -0700 | [diff] [blame] | 1732 | if (state->target == vertex_shader) { |
| 1733 | bool error_emitted = false; |
| 1734 | |
| 1735 | /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec: |
| 1736 | * |
| 1737 | * "Vertex shader inputs can only be float, floating-point |
| 1738 | * vectors, matrices, signed and unsigned integers and integer |
| 1739 | * vectors. Vertex shader inputs can also form arrays of these |
| 1740 | * types, but not structures." |
| 1741 | * |
Ian Romanick | 2d81620 | 2010-03-29 17:40:11 -0700 | [diff] [blame] | 1742 | * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec: |
| 1743 | * |
| 1744 | * "Vertex shader inputs can only be float, floating-point |
| 1745 | * vectors, matrices, signed and unsigned integers and integer |
| 1746 | * vectors. They cannot be arrays or structures." |
| 1747 | * |
Ian Romanick | fb9f5b0 | 2010-03-29 17:16:35 -0700 | [diff] [blame] | 1748 | * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec: |
| 1749 | * |
| 1750 | * "The attribute qualifier can be used only with float, |
| 1751 | * floating-point vectors, and matrices. Attribute variables |
| 1752 | * cannot be declared as arrays or structures." |
| 1753 | */ |
| 1754 | const glsl_type *check_type = var->type->is_array() |
| 1755 | ? var->type->fields.array : var->type; |
| 1756 | |
| 1757 | switch (check_type->base_type) { |
| 1758 | case GLSL_TYPE_FLOAT: |
| 1759 | break; |
| 1760 | case GLSL_TYPE_UINT: |
| 1761 | case GLSL_TYPE_INT: |
| 1762 | if (state->language_version > 120) |
| 1763 | break; |
| 1764 | /* FALLTHROUGH */ |
| 1765 | default: |
| 1766 | _mesa_glsl_error(& loc, state, |
| 1767 | "vertex shader input / attribute cannot have " |
| 1768 | "type %s`%s'", |
| 1769 | var->type->is_array() ? "array of " : "", |
| 1770 | check_type->name); |
| 1771 | error_emitted = true; |
| 1772 | } |
| 1773 | |
Ian Romanick | 2d81620 | 2010-03-29 17:40:11 -0700 | [diff] [blame] | 1774 | if (!error_emitted && (state->language_version <= 130) |
Ian Romanick | fb9f5b0 | 2010-03-29 17:16:35 -0700 | [diff] [blame] | 1775 | && var->type->is_array()) { |
| 1776 | _mesa_glsl_error(& loc, state, |
| 1777 | "vertex shader input / attribute cannot have " |
| 1778 | "array type"); |
| 1779 | error_emitted = true; |
| 1780 | } |
| 1781 | } |
| 1782 | } |
| 1783 | |
Ian Romanick | e78e0fa | 2010-07-07 12:13:34 -0700 | [diff] [blame] | 1784 | /* Process the initializer and add its instructions to a temporary |
| 1785 | * list. This list will be added to the instruction stream (below) after |
| 1786 | * the declaration is added. This is done because in some cases (such as |
| 1787 | * redeclarations) the declaration may not actually be added to the |
| 1788 | * instruction stream. |
| 1789 | */ |
Eric Anholt | fa33d0b | 2010-07-29 13:50:17 -0700 | [diff] [blame] | 1790 | exec_list initializer_instructions; |
Ian Romanick | 66faec4 | 2010-03-27 18:56:53 -0700 | [diff] [blame] | 1791 | if (decl->initializer != NULL) { |
Ian Romanick | 43de172 | 2010-03-28 17:03:16 -0700 | [diff] [blame] | 1792 | YYLTYPE initializer_loc = decl->initializer->get_location(); |
| 1793 | |
Ian Romanick | 66faec4 | 2010-03-27 18:56:53 -0700 | [diff] [blame] | 1794 | /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec: |
| 1795 | * |
| 1796 | * "All uniform variables are read-only and are initialized either |
| 1797 | * directly by an application via API commands, or indirectly by |
| 1798 | * OpenGL." |
| 1799 | */ |
| 1800 | if ((state->language_version <= 110) |
| 1801 | && (var->mode == ir_var_uniform)) { |
Ian Romanick | 43de172 | 2010-03-28 17:03:16 -0700 | [diff] [blame] | 1802 | _mesa_glsl_error(& initializer_loc, state, |
| 1803 | "cannot initialize uniforms in GLSL 1.10"); |
| 1804 | } |
Ian Romanick | 1936015 | 2010-03-26 18:05:27 -0700 | [diff] [blame] | 1805 | |
Ian Romanick | 43de172 | 2010-03-28 17:03:16 -0700 | [diff] [blame] | 1806 | if (var->type->is_sampler()) { |
| 1807 | _mesa_glsl_error(& initializer_loc, state, |
| 1808 | "cannot initialize samplers"); |
| 1809 | } |
| 1810 | |
| 1811 | if ((var->mode == ir_var_in) && (state->current_function == NULL)) { |
| 1812 | _mesa_glsl_error(& initializer_loc, state, |
| 1813 | "cannot initialize %s shader input / %s", |
Ian Romanick | ae4c4c0 | 2010-04-07 16:41:40 -0700 | [diff] [blame] | 1814 | _mesa_glsl_shader_target_name(state->target), |
Ian Romanick | 43de172 | 2010-03-28 17:03:16 -0700 | [diff] [blame] | 1815 | (state->target == vertex_shader) |
| 1816 | ? "attribute" : "varying"); |
Ian Romanick | 66faec4 | 2010-03-27 18:56:53 -0700 | [diff] [blame] | 1817 | } |
| 1818 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 1819 | ir_dereference *const lhs = new(ctx) ir_dereference_variable(var); |
Eric Anholt | fa33d0b | 2010-07-29 13:50:17 -0700 | [diff] [blame] | 1820 | ir_rvalue *rhs = decl->initializer->hir(&initializer_instructions, |
Ian Romanick | e78e0fa | 2010-07-07 12:13:34 -0700 | [diff] [blame] | 1821 | state); |
Ian Romanick | 66faec4 | 2010-03-27 18:56:53 -0700 | [diff] [blame] | 1822 | |
Ian Romanick | ce03088 | 2010-06-17 20:09:34 -0700 | [diff] [blame] | 1823 | /* Calculate the constant value if this is a const or uniform |
Eric Anholt | 307c71b | 2010-03-31 15:53:26 -1000 | [diff] [blame] | 1824 | * declaration. |
Ian Romanick | 66faec4 | 2010-03-27 18:56:53 -0700 | [diff] [blame] | 1825 | */ |
Ian Romanick | ce03088 | 2010-06-17 20:09:34 -0700 | [diff] [blame] | 1826 | if (this->type->qualifier.constant || this->type->qualifier.uniform) { |
Kenneth Graunke | e1d7185 | 2010-07-20 03:53:47 -0700 | [diff] [blame] | 1827 | ir_rvalue *new_rhs = validate_assignment(state, var->type, rhs); |
| 1828 | if (new_rhs != NULL) { |
| 1829 | rhs = new_rhs; |
Eric Anholt | e11757b | 2010-08-23 14:54:06 -0700 | [diff] [blame] | 1830 | |
| 1831 | ir_constant *constant_value = rhs->constant_expression_value(); |
| 1832 | if (!constant_value) { |
| 1833 | _mesa_glsl_error(& initializer_loc, state, |
| 1834 | "initializer of %s variable `%s' must be a " |
| 1835 | "constant expression", |
| 1836 | (this->type->qualifier.constant) |
| 1837 | ? "const" : "uniform", |
| 1838 | decl->identifier); |
| 1839 | if (var->type->is_numeric()) { |
| 1840 | /* Reduce cascading errors. */ |
| 1841 | var->constant_value = ir_constant::zero(ctx, var->type); |
| 1842 | } |
| 1843 | } else { |
| 1844 | rhs = constant_value; |
| 1845 | var->constant_value = constant_value; |
| 1846 | } |
Kenneth Graunke | e1d7185 | 2010-07-20 03:53:47 -0700 | [diff] [blame] | 1847 | } else { |
| 1848 | _mesa_glsl_error(&initializer_loc, state, |
| 1849 | "initializer of type %s cannot be assigned to " |
| 1850 | "variable of type %s", |
| 1851 | rhs->type->name, var->type->name); |
Eric Anholt | e11757b | 2010-08-23 14:54:06 -0700 | [diff] [blame] | 1852 | if (var->type->is_numeric()) { |
| 1853 | /* Reduce cascading errors. */ |
| 1854 | var->constant_value = ir_constant::zero(ctx, var->type); |
| 1855 | } |
Eric Anholt | 307c71b | 2010-03-31 15:53:26 -1000 | [diff] [blame] | 1856 | } |
| 1857 | } |
Ian Romanick | 66faec4 | 2010-03-27 18:56:53 -0700 | [diff] [blame] | 1858 | |
Eric Anholt | 307c71b | 2010-03-31 15:53:26 -1000 | [diff] [blame] | 1859 | if (rhs && !rhs->type->is_error()) { |
Eric Anholt | ac3af37 | 2010-03-31 15:44:38 -1000 | [diff] [blame] | 1860 | bool temp = var->read_only; |
| 1861 | if (this->type->qualifier.constant) |
| 1862 | var->read_only = false; |
Ian Romanick | ce03088 | 2010-06-17 20:09:34 -0700 | [diff] [blame] | 1863 | |
| 1864 | /* Never emit code to initialize a uniform. |
| 1865 | */ |
| 1866 | if (!this->type->qualifier.uniform) |
Eric Anholt | fa33d0b | 2010-07-29 13:50:17 -0700 | [diff] [blame] | 1867 | result = do_assignment(&initializer_instructions, state, |
| 1868 | lhs, rhs, |
Ian Romanick | ce03088 | 2010-06-17 20:09:34 -0700 | [diff] [blame] | 1869 | this->get_location()); |
Eric Anholt | ac3af37 | 2010-03-31 15:44:38 -1000 | [diff] [blame] | 1870 | var->read_only = temp; |
Ian Romanick | 66faec4 | 2010-03-27 18:56:53 -0700 | [diff] [blame] | 1871 | } |
Ian Romanick | 1936015 | 2010-03-26 18:05:27 -0700 | [diff] [blame] | 1872 | } |
Ian Romanick | 17d86f4 | 2010-03-29 12:59:02 -0700 | [diff] [blame] | 1873 | |
Eric Anholt | 0ed6125 | 2010-03-31 09:29:33 -1000 | [diff] [blame] | 1874 | /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec: |
| 1875 | * |
| 1876 | * "It is an error to write to a const variable outside of |
| 1877 | * its declaration, so they must be initialized when |
| 1878 | * declared." |
| 1879 | */ |
| 1880 | if (this->type->qualifier.constant && decl->initializer == NULL) { |
| 1881 | _mesa_glsl_error(& loc, state, |
| 1882 | "const declaration of `%s' must be initialized"); |
| 1883 | } |
| 1884 | |
Ian Romanick | 5466b63 | 2010-07-01 12:46:55 -0700 | [diff] [blame] | 1885 | /* Attempt to add the variable to the symbol table. If this fails, it |
| 1886 | * means the variable has already been declared at this scope. Arrays |
| 1887 | * fudge this rule a little bit. |
| 1888 | * |
| 1889 | * From page 24 (page 30 of the PDF) of the GLSL 1.50 spec, |
| 1890 | * |
| 1891 | * "It is legal to declare an array without a size and then |
| 1892 | * later re-declare the same name as an array of the same |
| 1893 | * type and specify a size." |
| 1894 | */ |
| 1895 | if (state->symbols->name_declared_this_scope(decl->identifier)) { |
| 1896 | ir_variable *const earlier = |
| 1897 | state->symbols->get_variable(decl->identifier); |
| 1898 | |
| 1899 | if ((earlier != NULL) |
| 1900 | && (earlier->type->array_size() == 0) |
| 1901 | && var->type->is_array() |
| 1902 | && (var->type->element_type() == earlier->type->element_type())) { |
| 1903 | /* FINISHME: This doesn't match the qualifiers on the two |
| 1904 | * FINISHME: declarations. It's not 100% clear whether this is |
| 1905 | * FINISHME: required or not. |
| 1906 | */ |
| 1907 | |
Ian Romanick | cd00d5b | 2010-07-01 13:17:54 -0700 | [diff] [blame] | 1908 | /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec: |
| 1909 | * |
| 1910 | * "The size [of gl_TexCoord] can be at most |
| 1911 | * gl_MaxTextureCoords." |
Ian Romanick | cd00d5b | 2010-07-01 13:17:54 -0700 | [diff] [blame] | 1912 | */ |
Ian Romanick | 127308b | 2010-07-01 13:30:50 -0700 | [diff] [blame] | 1913 | const unsigned size = unsigned(var->type->array_size()); |
Ian Romanick | cd00d5b | 2010-07-01 13:17:54 -0700 | [diff] [blame] | 1914 | if ((strcmp("gl_TexCoord", var->name) == 0) |
Ian Romanick | 127308b | 2010-07-01 13:30:50 -0700 | [diff] [blame] | 1915 | && (size > state->Const.MaxTextureCoords)) { |
Ian Romanick | cd00d5b | 2010-07-01 13:17:54 -0700 | [diff] [blame] | 1916 | YYLTYPE loc = this->get_location(); |
| 1917 | |
| 1918 | _mesa_glsl_error(& loc, state, "`gl_TexCoord' array size cannot " |
| 1919 | "be larger than gl_MaxTextureCoords (%u)\n", |
Ian Romanick | 127308b | 2010-07-01 13:30:50 -0700 | [diff] [blame] | 1920 | state->Const.MaxTextureCoords); |
Ian Romanick | 12873fa | 2010-07-01 14:10:19 -0700 | [diff] [blame] | 1921 | } else if ((size > 0) && (size <= earlier->max_array_access)) { |
Ian Romanick | 5466b63 | 2010-07-01 12:46:55 -0700 | [diff] [blame] | 1922 | YYLTYPE loc = this->get_location(); |
| 1923 | |
| 1924 | _mesa_glsl_error(& loc, state, "array size must be > %u due to " |
| 1925 | "previous access", |
| 1926 | earlier->max_array_access); |
| 1927 | } |
| 1928 | |
| 1929 | earlier->type = var->type; |
| 1930 | delete var; |
| 1931 | var = NULL; |
Eric Anholt | 4a96217 | 2010-07-28 14:41:51 -0700 | [diff] [blame] | 1932 | } else if (state->extensions->ARB_fragment_coord_conventions && |
| 1933 | (earlier != NULL) && |
| 1934 | (strcmp(var->name, "gl_FragCoord") == 0) && |
| 1935 | earlier->type == var->type && |
| 1936 | earlier->mode == var->mode) { |
| 1937 | /* Allow redeclaration of gl_FragCoord for ARB_fcc layout |
| 1938 | * qualifiers. |
| 1939 | */ |
| 1940 | earlier->origin_upper_left = var->origin_upper_left; |
| 1941 | earlier->pixel_center_integer = var->pixel_center_integer; |
Ian Romanick | 5466b63 | 2010-07-01 12:46:55 -0700 | [diff] [blame] | 1942 | } else { |
| 1943 | YYLTYPE loc = this->get_location(); |
| 1944 | |
| 1945 | _mesa_glsl_error(& loc, state, "`%s' redeclared", |
| 1946 | decl->identifier); |
| 1947 | } |
| 1948 | |
| 1949 | continue; |
| 1950 | } |
| 1951 | |
| 1952 | /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec, |
| 1953 | * |
| 1954 | * "Identifiers starting with "gl_" are reserved for use by |
| 1955 | * OpenGL, and may not be declared in a shader as either a |
| 1956 | * variable or a function." |
| 1957 | */ |
| 1958 | if (strncmp(decl->identifier, "gl_", 3) == 0) { |
| 1959 | /* FINISHME: This should only trigger if we're not redefining |
| 1960 | * FINISHME: a builtin (to add a qualifier, for example). |
| 1961 | */ |
| 1962 | _mesa_glsl_error(& loc, state, |
| 1963 | "identifier `%s' uses reserved `gl_' prefix", |
| 1964 | decl->identifier); |
| 1965 | } |
| 1966 | |
Eric Anholt | 8048226 | 2010-08-05 14:41:09 -0700 | [diff] [blame] | 1967 | /* Push the variable declaration to the top. It means that all |
| 1968 | * the variable declarations will appear in a funny |
| 1969 | * last-to-first order, but otherwise we run into trouble if a |
| 1970 | * function is prototyped, a global var is decled, then the |
| 1971 | * function is defined with usage of the global var. See |
| 1972 | * glslparsertest's CorrectModule.frag. |
| 1973 | */ |
| 1974 | instructions->push_head(var); |
Eric Anholt | fa33d0b | 2010-07-29 13:50:17 -0700 | [diff] [blame] | 1975 | instructions->append_list(&initializer_instructions); |
Ian Romanick | 2e85f99 | 2010-07-07 11:57:16 -0700 | [diff] [blame] | 1976 | |
Kenneth Graunke | c7f4ff1 | 2010-06-30 11:57:43 -0700 | [diff] [blame] | 1977 | /* Add the variable to the symbol table after processing the initializer. |
Ian Romanick | 17d86f4 | 2010-03-29 12:59:02 -0700 | [diff] [blame] | 1978 | * This differs from most C-like languages, but it follows the GLSL |
| 1979 | * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50 |
| 1980 | * spec: |
| 1981 | * |
| 1982 | * "Within a declaration, the scope of a name starts immediately |
| 1983 | * after the initializer if present or immediately after the name |
| 1984 | * being declared if not." |
| 1985 | */ |
| 1986 | const bool added_variable = |
Kenneth Graunke | c7f4ff1 | 2010-06-30 11:57:43 -0700 | [diff] [blame] | 1987 | state->symbols->add_variable(var->name, var); |
Ian Romanick | 17d86f4 | 2010-03-29 12:59:02 -0700 | [diff] [blame] | 1988 | assert(added_variable); |
Vinson Lee | 13b3d4c | 2010-08-21 16:21:41 -0700 | [diff] [blame] | 1989 | (void) added_variable; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1990 | } |
| 1991 | |
Eric Anholt | 8558459 | 2010-04-14 15:38:52 -0700 | [diff] [blame] | 1992 | |
| 1993 | /* Generally, variable declarations do not have r-values. However, |
| 1994 | * one is used for the declaration in |
| 1995 | * |
| 1996 | * while (bool b = some_condition()) { |
| 1997 | * ... |
| 1998 | * } |
| 1999 | * |
| 2000 | * so we return the rvalue from the last seen declaration here. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2001 | */ |
Eric Anholt | 8558459 | 2010-04-14 15:38:52 -0700 | [diff] [blame] | 2002 | return result; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2003 | } |
| 2004 | |
| 2005 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 2006 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 2007 | ast_parameter_declarator::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 2008 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2009 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 2010 | void *ctx = state; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2011 | const struct glsl_type *type; |
| 2012 | const char *name = NULL; |
Eric Anholt | 2e063f1 | 2010-03-28 00:56:22 -0700 | [diff] [blame] | 2013 | YYLTYPE loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2014 | |
Ian Romanick | d612a12 | 2010-03-31 16:22:06 -0700 | [diff] [blame] | 2015 | type = this->type->specifier->glsl_type(& name, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2016 | |
| 2017 | if (type == NULL) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2018 | if (name != NULL) { |
| 2019 | _mesa_glsl_error(& loc, state, |
| 2020 | "invalid type `%s' in declaration of `%s'", |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 2021 | name, this->identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2022 | } else { |
| 2023 | _mesa_glsl_error(& loc, state, |
| 2024 | "invalid type in declaration of `%s'", |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 2025 | this->identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2026 | } |
| 2027 | |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 2028 | type = glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2029 | } |
| 2030 | |
Eric Anholt | 068c80c | 2010-03-31 09:56:36 -1000 | [diff] [blame] | 2031 | /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec: |
| 2032 | * |
| 2033 | * "Functions that accept no input arguments need not use void in the |
| 2034 | * argument list because prototypes (or definitions) are required and |
| 2035 | * therefore there is no ambiguity when an empty argument list "( )" is |
| 2036 | * declared. The idiom "(void)" as a parameter list is provided for |
| 2037 | * convenience." |
| 2038 | * |
| 2039 | * Placing this check here prevents a void parameter being set up |
| 2040 | * for a function, which avoids tripping up checks for main taking |
| 2041 | * parameters and lookups of an unnamed symbol. |
| 2042 | */ |
Ian Romanick | cf37c9e | 2010-04-02 15:30:45 -0700 | [diff] [blame] | 2043 | if (type->is_void()) { |
| 2044 | if (this->identifier != NULL) |
| 2045 | _mesa_glsl_error(& loc, state, |
| 2046 | "named parameter cannot have type `void'"); |
| 2047 | |
| 2048 | is_void = true; |
Eric Anholt | 068c80c | 2010-03-31 09:56:36 -1000 | [diff] [blame] | 2049 | return NULL; |
Ian Romanick | cf37c9e | 2010-04-02 15:30:45 -0700 | [diff] [blame] | 2050 | } |
Eric Anholt | 068c80c | 2010-03-31 09:56:36 -1000 | [diff] [blame] | 2051 | |
Ian Romanick | 45d8a70 | 2010-04-02 15:09:33 -0700 | [diff] [blame] | 2052 | if (formal_parameter && (this->identifier == NULL)) { |
| 2053 | _mesa_glsl_error(& loc, state, "formal parameter lacks a name"); |
| 2054 | return NULL; |
| 2055 | } |
| 2056 | |
Kenneth Graunke | e511a35 | 2010-08-21 15:30:34 -0700 | [diff] [blame] | 2057 | /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...) |
| 2058 | * call already handled the "vec4[..] foo" case. |
| 2059 | */ |
| 2060 | if (this->is_array) { |
| 2061 | type = process_array_type(type, this->array_size, state); |
| 2062 | } |
| 2063 | |
| 2064 | if (type->array_size() == 0) { |
| 2065 | _mesa_glsl_error(&loc, state, "arrays passed as parameters must have " |
| 2066 | "a declared size."); |
| 2067 | type = glsl_type::error_type; |
| 2068 | } |
| 2069 | |
Ian Romanick | cf37c9e | 2010-04-02 15:30:45 -0700 | [diff] [blame] | 2070 | is_void = false; |
Ian Romanick | 7e2aa91 | 2010-07-19 17:12:42 -0700 | [diff] [blame] | 2071 | ir_variable *var = new(ctx) ir_variable(type, this->identifier, ir_var_in); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2072 | |
Ian Romanick | cdb8d54 | 2010-03-11 14:48:51 -0800 | [diff] [blame] | 2073 | /* Apply any specified qualifiers to the parameter declaration. Note that |
| 2074 | * for function parameters the default mode is 'in'. |
| 2075 | */ |
Eric Anholt | 2e063f1 | 2010-03-28 00:56:22 -0700 | [diff] [blame] | 2076 | apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2077 | |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 2078 | instructions->push_tail(var); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2079 | |
| 2080 | /* Parameter declarations do not have r-values. |
| 2081 | */ |
| 2082 | return NULL; |
| 2083 | } |
| 2084 | |
| 2085 | |
Ian Romanick | 45d8a70 | 2010-04-02 15:09:33 -0700 | [diff] [blame] | 2086 | void |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 2087 | ast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters, |
Ian Romanick | 45d8a70 | 2010-04-02 15:09:33 -0700 | [diff] [blame] | 2088 | bool formal, |
| 2089 | exec_list *ir_parameters, |
| 2090 | _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2091 | { |
Ian Romanick | cf37c9e | 2010-04-02 15:30:45 -0700 | [diff] [blame] | 2092 | ast_parameter_declarator *void_param = NULL; |
| 2093 | unsigned count = 0; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2094 | |
Ian Romanick | 2b97dc6 | 2010-05-10 17:42:05 -0700 | [diff] [blame] | 2095 | foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) { |
Ian Romanick | 45d8a70 | 2010-04-02 15:09:33 -0700 | [diff] [blame] | 2096 | param->formal_parameter = formal; |
Eric Anholt | 068c80c | 2010-03-31 09:56:36 -1000 | [diff] [blame] | 2097 | param->hir(ir_parameters, state); |
Ian Romanick | cf37c9e | 2010-04-02 15:30:45 -0700 | [diff] [blame] | 2098 | |
| 2099 | if (param->is_void) |
| 2100 | void_param = param; |
| 2101 | |
| 2102 | count++; |
| 2103 | } |
| 2104 | |
| 2105 | if ((void_param != NULL) && (count > 1)) { |
| 2106 | YYLTYPE loc = void_param->get_location(); |
| 2107 | |
| 2108 | _mesa_glsl_error(& loc, state, |
| 2109 | "`void' parameter must be only parameter"); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2110 | } |
| 2111 | } |
| 2112 | |
| 2113 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 2114 | ir_rvalue * |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 2115 | ast_function::hir(exec_list *instructions, |
| 2116 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2117 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 2118 | void *ctx = state; |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 2119 | ir_function *f = NULL; |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 2120 | ir_function_signature *sig = NULL; |
| 2121 | exec_list hir_parameters; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2122 | |
Kenneth Graunke | ac04c25 | 2010-06-29 00:48:10 -0700 | [diff] [blame] | 2123 | const char *const name = identifier; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2124 | |
Kenneth Graunke | edd180f | 2010-08-20 02:14:35 -0700 | [diff] [blame] | 2125 | /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec, |
| 2126 | * |
| 2127 | * "Identifiers starting with "gl_" are reserved for use by |
| 2128 | * OpenGL, and may not be declared in a shader as either a |
| 2129 | * variable or a function." |
| 2130 | */ |
| 2131 | if (strncmp(name, "gl_", 3) == 0) { |
| 2132 | YYLTYPE loc = this->get_location(); |
| 2133 | _mesa_glsl_error(&loc, state, |
| 2134 | "identifier `%s' uses reserved `gl_' prefix", name); |
| 2135 | } |
| 2136 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2137 | /* Convert the list of function parameters to HIR now so that they can be |
| 2138 | * used below to compare this function's signature with previously seen |
| 2139 | * signatures for functions with the same name. |
| 2140 | */ |
Ian Romanick | 45d8a70 | 2010-04-02 15:09:33 -0700 | [diff] [blame] | 2141 | ast_parameter_declarator::parameters_to_hir(& this->parameters, |
| 2142 | is_definition, |
| 2143 | & hir_parameters, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2144 | |
Ian Romanick | e39cc69 | 2010-03-23 12:19:13 -0700 | [diff] [blame] | 2145 | const char *return_type_name; |
| 2146 | const glsl_type *return_type = |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 2147 | this->return_type->specifier->glsl_type(& return_type_name, state); |
Ian Romanick | e39cc69 | 2010-03-23 12:19:13 -0700 | [diff] [blame] | 2148 | |
Eric Anholt | 76e96d7 | 2010-08-23 13:26:52 -0700 | [diff] [blame] | 2149 | if (!return_type) { |
| 2150 | YYLTYPE loc = this->get_location(); |
| 2151 | _mesa_glsl_error(&loc, state, |
| 2152 | "function `%s' has undeclared return type `%s'", |
| 2153 | name, return_type_name); |
| 2154 | return_type = glsl_type::error_type; |
| 2155 | } |
Ian Romanick | e39cc69 | 2010-03-23 12:19:13 -0700 | [diff] [blame] | 2156 | |
Kenneth Graunke | ac04c25 | 2010-06-29 00:48:10 -0700 | [diff] [blame] | 2157 | /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec: |
| 2158 | * "No qualifier is allowed on the return type of a function." |
| 2159 | */ |
| 2160 | if (this->return_type->has_qualifiers()) { |
| 2161 | YYLTYPE loc = this->get_location(); |
| 2162 | _mesa_glsl_error(& loc, state, |
| 2163 | "function `%s' return type has qualifiers", name); |
| 2164 | } |
| 2165 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2166 | /* Verify that this function's signature either doesn't match a previously |
| 2167 | * seen signature for a function with the same name, or, if a match is found, |
| 2168 | * that the previously seen signature does not have an associated definition. |
| 2169 | */ |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 2170 | f = state->symbols->get_function(name); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2171 | if (f != NULL) { |
Ian Romanick | 202604e | 2010-08-11 16:58:25 -0700 | [diff] [blame] | 2172 | sig = f->exact_matching_signature(&hir_parameters); |
Kenneth Graunke | 0d605cb | 2010-04-28 12:04:23 -0700 | [diff] [blame] | 2173 | if (sig != NULL) { |
| 2174 | const char *badvar = sig->qualifiers_match(&hir_parameters); |
| 2175 | if (badvar != NULL) { |
| 2176 | YYLTYPE loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2177 | |
Kenneth Graunke | 0d605cb | 2010-04-28 12:04:23 -0700 | [diff] [blame] | 2178 | _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' " |
| 2179 | "qualifiers don't match prototype", name, badvar); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2180 | } |
| 2181 | |
Kenneth Graunke | 0d605cb | 2010-04-28 12:04:23 -0700 | [diff] [blame] | 2182 | if (sig->return_type != return_type) { |
| 2183 | YYLTYPE loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2184 | |
Kenneth Graunke | 0d605cb | 2010-04-28 12:04:23 -0700 | [diff] [blame] | 2185 | _mesa_glsl_error(&loc, state, "function `%s' return type doesn't " |
| 2186 | "match prototype", name); |
| 2187 | } |
| 2188 | |
| 2189 | if (is_definition && sig->is_defined) { |
| 2190 | YYLTYPE loc = this->get_location(); |
| 2191 | |
| 2192 | _mesa_glsl_error(& loc, state, "function `%s' redefined", name); |
Kenneth Graunke | 0d605cb | 2010-04-28 12:04:23 -0700 | [diff] [blame] | 2193 | } |
| 2194 | } |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 2195 | } else if (state->symbols->name_declared_this_scope(name)) { |
| 2196 | /* This function name shadows a non-function use of the same name. |
| 2197 | */ |
| 2198 | YYLTYPE loc = this->get_location(); |
| 2199 | |
| 2200 | _mesa_glsl_error(& loc, state, "function name `%s' conflicts with " |
| 2201 | "non-function", name); |
Kenneth Graunke | 826a39c | 2010-08-20 02:04:52 -0700 | [diff] [blame] | 2202 | return NULL; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2203 | } else { |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 2204 | f = new(ctx) ir_function(name); |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 2205 | state->symbols->add_function(f->name, f); |
Kenneth Graunke | 9fa99f3 | 2010-04-21 12:30:22 -0700 | [diff] [blame] | 2206 | |
| 2207 | /* Emit the new function header */ |
| 2208 | instructions->push_tail(f); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2209 | } |
| 2210 | |
Eric Anholt | ab372da | 2010-03-28 01:24:55 -0700 | [diff] [blame] | 2211 | /* Verify the return type of main() */ |
| 2212 | if (strcmp(name, "main") == 0) { |
Ian Romanick | 25711a8 | 2010-03-31 17:39:10 -0700 | [diff] [blame] | 2213 | if (! return_type->is_void()) { |
Eric Anholt | ab372da | 2010-03-28 01:24:55 -0700 | [diff] [blame] | 2214 | YYLTYPE loc = this->get_location(); |
| 2215 | |
| 2216 | _mesa_glsl_error(& loc, state, "main() must return void"); |
| 2217 | } |
Eric Anholt | 174cc03 | 2010-03-30 23:37:51 -1000 | [diff] [blame] | 2218 | |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 2219 | if (!hir_parameters.is_empty()) { |
Eric Anholt | 174cc03 | 2010-03-30 23:37:51 -1000 | [diff] [blame] | 2220 | YYLTYPE loc = this->get_location(); |
| 2221 | |
| 2222 | _mesa_glsl_error(& loc, state, "main() must not take any parameters"); |
| 2223 | } |
Eric Anholt | ab372da | 2010-03-28 01:24:55 -0700 | [diff] [blame] | 2224 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2225 | |
| 2226 | /* Finish storing the information about this new function in its signature. |
| 2227 | */ |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 2228 | if (sig == NULL) { |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 2229 | sig = new(ctx) ir_function_signature(return_type); |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 2230 | f->add_signature(sig); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2231 | } |
| 2232 | |
Kenneth Graunke | bff6013 | 2010-04-28 12:44:24 -0700 | [diff] [blame] | 2233 | sig->replace_parameters(&hir_parameters); |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 2234 | signature = sig; |
Ian Romanick | e29a585 | 2010-03-31 17:54:26 -0700 | [diff] [blame] | 2235 | |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 2236 | /* Function declarations (prototypes) do not have r-values. |
| 2237 | */ |
| 2238 | return NULL; |
| 2239 | } |
| 2240 | |
| 2241 | |
| 2242 | ir_rvalue * |
| 2243 | ast_function_definition::hir(exec_list *instructions, |
| 2244 | struct _mesa_glsl_parse_state *state) |
| 2245 | { |
| 2246 | prototype->is_definition = true; |
| 2247 | prototype->hir(instructions, state); |
| 2248 | |
| 2249 | ir_function_signature *signature = prototype->signature; |
Kenneth Graunke | 826a39c | 2010-08-20 02:04:52 -0700 | [diff] [blame] | 2250 | if (signature == NULL) |
| 2251 | return NULL; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2252 | |
Ian Romanick | 41ec6a4 | 2010-03-19 17:08:05 -0700 | [diff] [blame] | 2253 | assert(state->current_function == NULL); |
| 2254 | state->current_function = signature; |
Kenneth Graunke | 6de8256 | 2010-06-29 09:59:40 -0700 | [diff] [blame] | 2255 | state->found_return = false; |
Ian Romanick | 41ec6a4 | 2010-03-19 17:08:05 -0700 | [diff] [blame] | 2256 | |
Ian Romanick | e29a585 | 2010-03-31 17:54:26 -0700 | [diff] [blame] | 2257 | /* Duplicate parameters declared in the prototype as concrete variables. |
| 2258 | * Add these to the symbol table. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2259 | */ |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 2260 | state->symbols->push_scope(); |
Ian Romanick | e29a585 | 2010-03-31 17:54:26 -0700 | [diff] [blame] | 2261 | foreach_iter(exec_list_iterator, iter, signature->parameters) { |
Eric Anholt | fbc7c0b | 2010-04-07 14:32:53 -0700 | [diff] [blame] | 2262 | ir_variable *const var = ((ir_instruction *) iter.get())->as_variable(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2263 | |
Eric Anholt | fbc7c0b | 2010-04-07 14:32:53 -0700 | [diff] [blame] | 2264 | assert(var != NULL); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2265 | |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 2266 | /* The only way a parameter would "exist" is if two parameters have |
| 2267 | * the same name. |
| 2268 | */ |
| 2269 | if (state->symbols->name_declared_this_scope(var->name)) { |
| 2270 | YYLTYPE loc = this->get_location(); |
| 2271 | |
| 2272 | _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name); |
| 2273 | } else { |
| 2274 | state->symbols->add_variable(var->name, var); |
| 2275 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2276 | } |
| 2277 | |
Kenneth Graunke | 9fa99f3 | 2010-04-21 12:30:22 -0700 | [diff] [blame] | 2278 | /* Convert the body of the function to HIR. */ |
Eric Anholt | 894ea97 | 2010-04-07 13:19:11 -0700 | [diff] [blame] | 2279 | this->body->hir(&signature->body, state); |
Kenneth Graunke | 9fa99f3 | 2010-04-21 12:30:22 -0700 | [diff] [blame] | 2280 | signature->is_defined = true; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2281 | |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 2282 | state->symbols->pop_scope(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2283 | |
Ian Romanick | 41ec6a4 | 2010-03-19 17:08:05 -0700 | [diff] [blame] | 2284 | assert(state->current_function == signature); |
| 2285 | state->current_function = NULL; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2286 | |
Kenneth Graunke | 6de8256 | 2010-06-29 09:59:40 -0700 | [diff] [blame] | 2287 | if (!signature->return_type->is_void() && !state->found_return) { |
| 2288 | YYLTYPE loc = this->get_location(); |
| 2289 | _mesa_glsl_error(& loc, state, "function `%s' has non-void return type " |
| 2290 | "%s, but no return statement", |
| 2291 | signature->function_name(), |
| 2292 | signature->return_type->name); |
| 2293 | } |
| 2294 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 2295 | /* Function definitions do not have r-values. |
| 2296 | */ |
| 2297 | return NULL; |
| 2298 | } |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 2299 | |
| 2300 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 2301 | ir_rvalue * |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 2302 | ast_jump_statement::hir(exec_list *instructions, |
| 2303 | struct _mesa_glsl_parse_state *state) |
| 2304 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 2305 | void *ctx = state; |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 2306 | |
Ian Romanick | c0e76d8 | 2010-04-05 16:53:19 -0700 | [diff] [blame] | 2307 | switch (mode) { |
| 2308 | case ast_return: { |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 2309 | ir_return *inst; |
Eric Anholt | aad7c77 | 2010-03-30 23:28:20 -1000 | [diff] [blame] | 2310 | assert(state->current_function); |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 2311 | |
| 2312 | if (opt_return_value) { |
Eric Anholt | ab79d4e | 2010-03-30 23:23:16 -1000 | [diff] [blame] | 2313 | if (state->current_function->return_type->base_type == |
| 2314 | GLSL_TYPE_VOID) { |
| 2315 | YYLTYPE loc = this->get_location(); |
| 2316 | |
| 2317 | _mesa_glsl_error(& loc, state, |
| 2318 | "`return` with a value, in function `%s' " |
| 2319 | "returning void", |
Kenneth Graunke | f96c52b | 2010-04-21 15:17:26 -0700 | [diff] [blame] | 2320 | state->current_function->function_name()); |
Eric Anholt | ab79d4e | 2010-03-30 23:23:16 -1000 | [diff] [blame] | 2321 | } |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 2322 | |
| 2323 | ir_expression *const ret = (ir_expression *) |
| 2324 | opt_return_value->hir(instructions, state); |
| 2325 | assert(ret != NULL); |
| 2326 | |
Kenneth Graunke | 18707eb | 2010-06-28 23:38:04 -0700 | [diff] [blame] | 2327 | /* Implicit conversions are not allowed for return values. */ |
| 2328 | if (state->current_function->return_type != ret->type) { |
| 2329 | YYLTYPE loc = this->get_location(); |
| 2330 | |
| 2331 | _mesa_glsl_error(& loc, state, |
| 2332 | "`return' with wrong type %s, in function `%s' " |
| 2333 | "returning %s", |
| 2334 | ret->type->name, |
| 2335 | state->current_function->function_name(), |
| 2336 | state->current_function->return_type->name); |
| 2337 | } |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 2338 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 2339 | inst = new(ctx) ir_return(ret); |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 2340 | } else { |
Eric Anholt | aad7c77 | 2010-03-30 23:28:20 -1000 | [diff] [blame] | 2341 | if (state->current_function->return_type->base_type != |
| 2342 | GLSL_TYPE_VOID) { |
| 2343 | YYLTYPE loc = this->get_location(); |
| 2344 | |
| 2345 | _mesa_glsl_error(& loc, state, |
| 2346 | "`return' with no value, in function %s returning " |
| 2347 | "non-void", |
Kenneth Graunke | f96c52b | 2010-04-21 15:17:26 -0700 | [diff] [blame] | 2348 | state->current_function->function_name()); |
Eric Anholt | aad7c77 | 2010-03-30 23:28:20 -1000 | [diff] [blame] | 2349 | } |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 2350 | inst = new(ctx) ir_return; |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 2351 | } |
| 2352 | |
Kenneth Graunke | 6de8256 | 2010-06-29 09:59:40 -0700 | [diff] [blame] | 2353 | state->found_return = true; |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 2354 | instructions->push_tail(inst); |
Ian Romanick | c0e76d8 | 2010-04-05 16:53:19 -0700 | [diff] [blame] | 2355 | break; |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 2356 | } |
| 2357 | |
Ian Romanick | c0e76d8 | 2010-04-05 16:53:19 -0700 | [diff] [blame] | 2358 | case ast_discard: |
Eric Anholt | b980207 | 2010-03-30 23:40:14 -1000 | [diff] [blame] | 2359 | if (state->target != fragment_shader) { |
| 2360 | YYLTYPE loc = this->get_location(); |
| 2361 | |
| 2362 | _mesa_glsl_error(& loc, state, |
| 2363 | "`discard' may only appear in a fragment shader"); |
| 2364 | } |
Kenneth Graunke | 77049a7 | 2010-06-30 14:11:00 -0700 | [diff] [blame] | 2365 | instructions->push_tail(new(ctx) ir_discard); |
Ian Romanick | c0e76d8 | 2010-04-05 16:53:19 -0700 | [diff] [blame] | 2366 | break; |
| 2367 | |
| 2368 | case ast_break: |
| 2369 | case ast_continue: |
Ian Romanick | 4cf20cd | 2010-04-05 17:13:47 -0700 | [diff] [blame] | 2370 | /* FINISHME: Handle switch-statements. They cannot contain 'continue', |
| 2371 | * FINISHME: and they use a different IR instruction for 'break'. |
| 2372 | */ |
| 2373 | /* FINISHME: Correctly handle the nesting. If a switch-statement is |
| 2374 | * FINISHME: inside a loop, a 'continue' is valid and will bind to the |
| 2375 | * FINISHME: loop. |
| 2376 | */ |
| 2377 | if (state->loop_or_switch_nesting == NULL) { |
| 2378 | YYLTYPE loc = this->get_location(); |
| 2379 | |
| 2380 | _mesa_glsl_error(& loc, state, |
| 2381 | "`%s' may only appear in a loop", |
| 2382 | (mode == ast_break) ? "break" : "continue"); |
| 2383 | } else { |
| 2384 | ir_loop *const loop = state->loop_or_switch_nesting->as_loop(); |
| 2385 | |
Eric Anholt | 2d1ed7b | 2010-07-22 12:55:16 -0700 | [diff] [blame] | 2386 | /* Inline the for loop expression again, since we don't know |
| 2387 | * where near the end of the loop body the normal copy of it |
| 2388 | * is going to be placed. |
| 2389 | */ |
| 2390 | if (mode == ast_continue && |
| 2391 | state->loop_or_switch_nesting_ast->rest_expression) { |
| 2392 | state->loop_or_switch_nesting_ast->rest_expression->hir(instructions, |
| 2393 | state); |
| 2394 | } |
| 2395 | |
Ian Romanick | 4cf20cd | 2010-04-05 17:13:47 -0700 | [diff] [blame] | 2396 | if (loop != NULL) { |
| 2397 | ir_loop_jump *const jump = |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 2398 | new(ctx) ir_loop_jump((mode == ast_break) |
| 2399 | ? ir_loop_jump::jump_break |
| 2400 | : ir_loop_jump::jump_continue); |
Ian Romanick | 4cf20cd | 2010-04-05 17:13:47 -0700 | [diff] [blame] | 2401 | instructions->push_tail(jump); |
| 2402 | } |
| 2403 | } |
| 2404 | |
Ian Romanick | c0e76d8 | 2010-04-05 16:53:19 -0700 | [diff] [blame] | 2405 | break; |
Eric Anholt | b980207 | 2010-03-30 23:40:14 -1000 | [diff] [blame] | 2406 | } |
| 2407 | |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 2408 | /* Jump instructions do not have r-values. |
| 2409 | */ |
| 2410 | return NULL; |
| 2411 | } |
Ian Romanick | 3c6fea3 | 2010-03-29 14:11:25 -0700 | [diff] [blame] | 2412 | |
| 2413 | |
| 2414 | ir_rvalue * |
| 2415 | ast_selection_statement::hir(exec_list *instructions, |
| 2416 | struct _mesa_glsl_parse_state *state) |
| 2417 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 2418 | void *ctx = state; |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 2419 | |
Ian Romanick | 3c6fea3 | 2010-03-29 14:11:25 -0700 | [diff] [blame] | 2420 | ir_rvalue *const condition = this->condition->hir(instructions, state); |
Ian Romanick | 3c6fea3 | 2010-03-29 14:11:25 -0700 | [diff] [blame] | 2421 | |
| 2422 | /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec: |
| 2423 | * |
| 2424 | * "Any expression whose type evaluates to a Boolean can be used as the |
| 2425 | * conditional expression bool-expression. Vector types are not accepted |
| 2426 | * as the expression to if." |
| 2427 | * |
| 2428 | * The checks are separated so that higher quality diagnostics can be |
| 2429 | * generated for cases where both rules are violated. |
| 2430 | */ |
| 2431 | if (!condition->type->is_boolean() || !condition->type->is_scalar()) { |
| 2432 | YYLTYPE loc = this->condition->get_location(); |
| 2433 | |
| 2434 | _mesa_glsl_error(& loc, state, "if-statement condition must be scalar " |
| 2435 | "boolean"); |
| 2436 | } |
| 2437 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 2438 | ir_if *const stmt = new(ctx) ir_if(condition); |
Ian Romanick | 3c6fea3 | 2010-03-29 14:11:25 -0700 | [diff] [blame] | 2439 | |
Kenneth Graunke | 665d75c | 2010-08-18 13:54:50 -0700 | [diff] [blame] | 2440 | if (then_statement != NULL) { |
| 2441 | state->symbols->push_scope(); |
Ian Romanick | 4f9d72f | 2010-05-10 11:10:26 -0700 | [diff] [blame] | 2442 | then_statement->hir(& stmt->then_instructions, state); |
Kenneth Graunke | 665d75c | 2010-08-18 13:54:50 -0700 | [diff] [blame] | 2443 | state->symbols->pop_scope(); |
| 2444 | } |
Ian Romanick | 3c6fea3 | 2010-03-29 14:11:25 -0700 | [diff] [blame] | 2445 | |
Kenneth Graunke | 665d75c | 2010-08-18 13:54:50 -0700 | [diff] [blame] | 2446 | if (else_statement != NULL) { |
| 2447 | state->symbols->push_scope(); |
Ian Romanick | 4f9d72f | 2010-05-10 11:10:26 -0700 | [diff] [blame] | 2448 | else_statement->hir(& stmt->else_instructions, state); |
Kenneth Graunke | 665d75c | 2010-08-18 13:54:50 -0700 | [diff] [blame] | 2449 | state->symbols->pop_scope(); |
| 2450 | } |
Ian Romanick | 3c6fea3 | 2010-03-29 14:11:25 -0700 | [diff] [blame] | 2451 | |
| 2452 | instructions->push_tail(stmt); |
| 2453 | |
| 2454 | /* if-statements do not have r-values. |
| 2455 | */ |
| 2456 | return NULL; |
| 2457 | } |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 2458 | |
| 2459 | |
Ian Romanick | 8c46ed2 | 2010-04-05 18:07:27 -0700 | [diff] [blame] | 2460 | void |
| 2461 | ast_iteration_statement::condition_to_hir(ir_loop *stmt, |
| 2462 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 2463 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 2464 | void *ctx = state; |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 2465 | |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 2466 | if (condition != NULL) { |
| 2467 | ir_rvalue *const cond = |
| 2468 | condition->hir(& stmt->body_instructions, state); |
| 2469 | |
| 2470 | if ((cond == NULL) |
| 2471 | || !cond->type->is_boolean() || !cond->type->is_scalar()) { |
| 2472 | YYLTYPE loc = condition->get_location(); |
| 2473 | |
| 2474 | _mesa_glsl_error(& loc, state, |
| 2475 | "loop condition must be scalar boolean"); |
| 2476 | } else { |
| 2477 | /* As the first code in the loop body, generate a block that looks |
| 2478 | * like 'if (!condition) break;' as the loop termination condition. |
| 2479 | */ |
| 2480 | ir_rvalue *const not_cond = |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 2481 | new(ctx) ir_expression(ir_unop_logic_not, glsl_type::bool_type, cond, |
| 2482 | NULL); |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 2483 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 2484 | ir_if *const if_stmt = new(ctx) ir_if(not_cond); |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 2485 | |
| 2486 | ir_jump *const break_stmt = |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 2487 | new(ctx) ir_loop_jump(ir_loop_jump::jump_break); |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 2488 | |
| 2489 | if_stmt->then_instructions.push_tail(break_stmt); |
| 2490 | stmt->body_instructions.push_tail(if_stmt); |
| 2491 | } |
| 2492 | } |
Ian Romanick | 8c46ed2 | 2010-04-05 18:07:27 -0700 | [diff] [blame] | 2493 | } |
| 2494 | |
| 2495 | |
| 2496 | ir_rvalue * |
| 2497 | ast_iteration_statement::hir(exec_list *instructions, |
| 2498 | struct _mesa_glsl_parse_state *state) |
| 2499 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 2500 | void *ctx = state; |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 2501 | |
Ian Romanick | 4846066 | 2010-04-16 16:42:43 -0700 | [diff] [blame] | 2502 | /* For-loops and while-loops start a new scope, but do-while loops do not. |
Ian Romanick | 8c46ed2 | 2010-04-05 18:07:27 -0700 | [diff] [blame] | 2503 | */ |
Ian Romanick | 4846066 | 2010-04-16 16:42:43 -0700 | [diff] [blame] | 2504 | if (mode != ast_do_while) |
Ian Romanick | 8c46ed2 | 2010-04-05 18:07:27 -0700 | [diff] [blame] | 2505 | state->symbols->push_scope(); |
| 2506 | |
| 2507 | if (init_statement != NULL) |
| 2508 | init_statement->hir(instructions, state); |
| 2509 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 2510 | ir_loop *const stmt = new(ctx) ir_loop(); |
Ian Romanick | 8c46ed2 | 2010-04-05 18:07:27 -0700 | [diff] [blame] | 2511 | instructions->push_tail(stmt); |
| 2512 | |
| 2513 | /* Track the current loop and / or switch-statement nesting. |
| 2514 | */ |
| 2515 | ir_instruction *const nesting = state->loop_or_switch_nesting; |
Eric Anholt | 2d1ed7b | 2010-07-22 12:55:16 -0700 | [diff] [blame] | 2516 | ast_iteration_statement *nesting_ast = state->loop_or_switch_nesting_ast; |
| 2517 | |
Ian Romanick | 8c46ed2 | 2010-04-05 18:07:27 -0700 | [diff] [blame] | 2518 | state->loop_or_switch_nesting = stmt; |
Eric Anholt | 2d1ed7b | 2010-07-22 12:55:16 -0700 | [diff] [blame] | 2519 | state->loop_or_switch_nesting_ast = this; |
Ian Romanick | 8c46ed2 | 2010-04-05 18:07:27 -0700 | [diff] [blame] | 2520 | |
| 2521 | if (mode != ast_do_while) |
| 2522 | condition_to_hir(stmt, state); |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 2523 | |
Ian Romanick | 4f9d72f | 2010-05-10 11:10:26 -0700 | [diff] [blame] | 2524 | if (body != NULL) |
| 2525 | body->hir(& stmt->body_instructions, state); |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 2526 | |
| 2527 | if (rest_expression != NULL) |
| 2528 | rest_expression->hir(& stmt->body_instructions, state); |
| 2529 | |
Ian Romanick | 8c46ed2 | 2010-04-05 18:07:27 -0700 | [diff] [blame] | 2530 | if (mode == ast_do_while) |
| 2531 | condition_to_hir(stmt, state); |
| 2532 | |
Ian Romanick | 4846066 | 2010-04-16 16:42:43 -0700 | [diff] [blame] | 2533 | if (mode != ast_do_while) |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 2534 | state->symbols->pop_scope(); |
| 2535 | |
Ian Romanick | e9d0f26 | 2010-04-05 17:01:53 -0700 | [diff] [blame] | 2536 | /* Restore previous nesting before returning. |
| 2537 | */ |
| 2538 | state->loop_or_switch_nesting = nesting; |
Eric Anholt | 2d1ed7b | 2010-07-22 12:55:16 -0700 | [diff] [blame] | 2539 | state->loop_or_switch_nesting_ast = nesting_ast; |
Ian Romanick | e9d0f26 | 2010-04-05 17:01:53 -0700 | [diff] [blame] | 2540 | |
Ian Romanick | 9e7d010 | 2010-04-05 16:37:49 -0700 | [diff] [blame] | 2541 | /* Loops do not have r-values. |
| 2542 | */ |
| 2543 | return NULL; |
| 2544 | } |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 2545 | |
| 2546 | |
| 2547 | ir_rvalue * |
| 2548 | ast_type_specifier::hir(exec_list *instructions, |
| 2549 | struct _mesa_glsl_parse_state *state) |
| 2550 | { |
| 2551 | if (this->structure != NULL) |
| 2552 | return this->structure->hir(instructions, state); |
Ian Romanick | 85ba37b | 2010-04-21 14:33:34 -0700 | [diff] [blame] | 2553 | |
| 2554 | return NULL; |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 2555 | } |
| 2556 | |
| 2557 | |
| 2558 | ir_rvalue * |
| 2559 | ast_struct_specifier::hir(exec_list *instructions, |
| 2560 | struct _mesa_glsl_parse_state *state) |
| 2561 | { |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 2562 | unsigned decl_count = 0; |
| 2563 | |
| 2564 | /* Make an initial pass over the list of structure fields to determine how |
| 2565 | * many there are. Each element in this list is an ast_declarator_list. |
| 2566 | * This means that we actually need to count the number of elements in the |
| 2567 | * 'declarations' list in each of the elements. |
| 2568 | */ |
Ian Romanick | 2b97dc6 | 2010-05-10 17:42:05 -0700 | [diff] [blame] | 2569 | foreach_list_typed (ast_declarator_list, decl_list, link, |
| 2570 | &this->declarations) { |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 2571 | foreach_list_const (decl_ptr, & decl_list->declarations) { |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 2572 | decl_count++; |
| 2573 | } |
| 2574 | } |
| 2575 | |
| 2576 | |
| 2577 | /* Allocate storage for the structure fields and process the field |
| 2578 | * declarations. As the declarations are processed, try to also convert |
| 2579 | * the types to HIR. This ensures that structure definitions embedded in |
| 2580 | * other structure definitions are processed. |
| 2581 | */ |
Eric Anholt | 21b0dbd | 2010-07-20 16:47:25 -0700 | [diff] [blame] | 2582 | glsl_struct_field *const fields = talloc_array(state, glsl_struct_field, |
| 2583 | decl_count); |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 2584 | |
| 2585 | unsigned i = 0; |
Ian Romanick | 2b97dc6 | 2010-05-10 17:42:05 -0700 | [diff] [blame] | 2586 | foreach_list_typed (ast_declarator_list, decl_list, link, |
| 2587 | &this->declarations) { |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 2588 | const char *type_name; |
| 2589 | |
| 2590 | decl_list->type->specifier->hir(instructions, state); |
| 2591 | |
| 2592 | const glsl_type *decl_type = |
| 2593 | decl_list->type->specifier->glsl_type(& type_name, state); |
| 2594 | |
Ian Romanick | 2b97dc6 | 2010-05-10 17:42:05 -0700 | [diff] [blame] | 2595 | foreach_list_typed (ast_declaration, decl, link, |
| 2596 | &decl_list->declarations) { |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 2597 | const struct glsl_type *const field_type = |
| 2598 | (decl->is_array) |
| 2599 | ? process_array_type(decl_type, decl->array_size, state) |
| 2600 | : decl_type; |
| 2601 | |
Ian Romanick | 73986a7 | 2010-04-20 16:49:03 -0700 | [diff] [blame] | 2602 | fields[i].type = (field_type != NULL) |
| 2603 | ? field_type : glsl_type::error_type; |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 2604 | fields[i].name = decl->identifier; |
| 2605 | i++; |
| 2606 | } |
| 2607 | } |
| 2608 | |
| 2609 | assert(i == decl_count); |
| 2610 | |
Ian Romanick | 1d28b61 | 2010-04-20 16:48:24 -0700 | [diff] [blame] | 2611 | const char *name; |
| 2612 | if (this->name == NULL) { |
| 2613 | static unsigned anon_count = 1; |
| 2614 | char buf[32]; |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 2615 | |
Ian Romanick | 1d28b61 | 2010-04-20 16:48:24 -0700 | [diff] [blame] | 2616 | snprintf(buf, sizeof(buf), "#anon_struct_%04x", anon_count); |
| 2617 | anon_count++; |
| 2618 | |
| 2619 | name = strdup(buf); |
| 2620 | } else { |
| 2621 | name = this->name; |
| 2622 | } |
| 2623 | |
Ian Romanick | 49e3577 | 2010-06-28 11:54:57 -0700 | [diff] [blame] | 2624 | const glsl_type *t = |
| 2625 | glsl_type::get_record_instance(fields, decl_count, name); |
Ian Romanick | 1d28b61 | 2010-04-20 16:48:24 -0700 | [diff] [blame] | 2626 | |
Ian Romanick | ab89927 | 2010-04-23 13:24:08 -0700 | [diff] [blame] | 2627 | YYLTYPE loc = this->get_location(); |
| 2628 | if (!state->symbols->add_type(name, t)) { |
| 2629 | _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name); |
| 2630 | } else { |
| 2631 | /* This logic is a bit tricky. It is an error to declare a structure at |
| 2632 | * global scope if there is also a function with the same name. |
| 2633 | */ |
| 2634 | if ((state->current_function == NULL) |
| 2635 | && (state->symbols->get_function(name) != NULL)) { |
| 2636 | _mesa_glsl_error(& loc, state, "name `%s' previously defined", name); |
| 2637 | } else { |
| 2638 | t->generate_constructor(state->symbols); |
| 2639 | } |
Ian Romanick | a2c6df5 | 2010-04-28 13:14:53 -0700 | [diff] [blame] | 2640 | |
| 2641 | const glsl_type **s = (const glsl_type **) |
| 2642 | realloc(state->user_structures, |
| 2643 | sizeof(state->user_structures[0]) * |
| 2644 | (state->num_user_structures + 1)); |
| 2645 | if (s != NULL) { |
| 2646 | s[state->num_user_structures] = t; |
| 2647 | state->user_structures = s; |
| 2648 | state->num_user_structures++; |
| 2649 | } |
Ian Romanick | ab89927 | 2010-04-23 13:24:08 -0700 | [diff] [blame] | 2650 | } |
Ian Romanick | 3455ce6 | 2010-04-19 15:13:15 -0700 | [diff] [blame] | 2651 | |
| 2652 | /* Structure type definitions do not have r-values. |
| 2653 | */ |
| 2654 | return NULL; |
| 2655 | } |