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