Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2010 Intel Corporation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | * DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * \file ast_to_hir.c |
| 26 | * Convert abstract syntax to to high-level intermediate reprensentation (HIR). |
| 27 | * |
| 28 | * During the conversion to HIR, the majority of the symantic checking is |
| 29 | * preformed on the program. This includes: |
| 30 | * |
| 31 | * * Symbol table management |
| 32 | * * Type checking |
| 33 | * * Function binding |
| 34 | * |
| 35 | * The majority of this work could be done during parsing, and the parser could |
| 36 | * probably generate HIR directly. However, this results in frequent changes |
| 37 | * to the parser code. Since we do not assume that every system this complier |
| 38 | * is built on will have Flex and Bison installed, we have to store the code |
| 39 | * generated by these tools in our version control system. In other parts of |
| 40 | * the system we've seen problems where a parser was changed but the generated |
| 41 | * code was not committed, merge conflicts where created because two developers |
| 42 | * had slightly different versions of Bison installed, etc. |
| 43 | * |
| 44 | * I have also noticed that running Bison generated parsers in GDB is very |
| 45 | * irritating. When you get a segfault on '$$ = $1->foo', you can't very |
| 46 | * well 'print $1' in GDB. |
| 47 | * |
| 48 | * As a result, my preference is to put as little C code as possible in the |
| 49 | * parser (and lexer) sources. |
| 50 | */ |
| 51 | #include <stdio.h> |
| 52 | #include "main/imports.h" |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 53 | #include "glsl_symbol_table.h" |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 54 | #include "glsl_parser_extras.h" |
| 55 | #include "ast.h" |
| 56 | #include "glsl_types.h" |
| 57 | #include "ir.h" |
| 58 | |
Ian Romanick | d949a9a | 2010-03-10 09:55:22 -0800 | [diff] [blame] | 59 | void |
| 60 | _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state) |
| 61 | { |
| 62 | struct simple_node *ptr; |
| 63 | |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 64 | _mesa_glsl_initialize_variables(instructions, state); |
Ian Romanick | a4e92c4 | 2010-03-25 17:02:22 -0700 | [diff] [blame] | 65 | _mesa_glsl_initialize_constructors(instructions, state); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 66 | _mesa_glsl_initialize_functions(instructions, state); |
Ian Romanick | adfb0cd | 2010-03-10 10:43:16 -0800 | [diff] [blame] | 67 | |
Ian Romanick | 41ec6a4 | 2010-03-19 17:08:05 -0700 | [diff] [blame] | 68 | state->current_function = NULL; |
| 69 | |
Ian Romanick | d949a9a | 2010-03-10 09:55:22 -0800 | [diff] [blame] | 70 | foreach (ptr, & state->translation_unit) { |
| 71 | ((ast_node *)ptr)->hir(instructions, state); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 76 | /** |
| 77 | * If a conversion is available, convert one operand to a different type |
| 78 | * |
| 79 | * The \c from \c ir_rvalue is converted "in place". |
| 80 | * |
| 81 | * \param to Type that the operand it to be converted to |
| 82 | * \param from Operand that is being converted |
| 83 | * \param state GLSL compiler state |
| 84 | * |
| 85 | * \return |
| 86 | * If a conversion is possible (or unnecessary), \c true is returned. |
| 87 | * Otherwise \c false is returned. |
| 88 | */ |
| 89 | static bool |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 90 | apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from, |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 91 | struct _mesa_glsl_parse_state *state) |
| 92 | { |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 93 | if (to->base_type == from->type->base_type) |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 94 | return true; |
| 95 | |
| 96 | /* This conversion was added in GLSL 1.20. If the compilation mode is |
| 97 | * GLSL 1.10, the conversion is skipped. |
| 98 | */ |
| 99 | if (state->language_version < 120) |
| 100 | return false; |
| 101 | |
| 102 | /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec: |
| 103 | * |
| 104 | * "There are no implicit array or structure conversions. For |
| 105 | * example, an array of int cannot be implicitly converted to an |
| 106 | * array of float. There are no implicit conversions between |
| 107 | * signed and unsigned integers." |
| 108 | */ |
| 109 | /* FINISHME: The above comment is partially a lie. There is int/uint |
| 110 | * FINISHME: conversion for immediate constants. |
| 111 | */ |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 112 | if (!to->is_float() || !from->type->is_numeric()) |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 113 | return false; |
| 114 | |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 115 | switch (from->type->base_type) { |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 116 | case GLSL_TYPE_INT: |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 117 | from = new ir_expression(ir_unop_i2f, to, from, NULL); |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 118 | break; |
| 119 | case GLSL_TYPE_UINT: |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 120 | from = new ir_expression(ir_unop_u2f, to, from, NULL); |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 121 | break; |
| 122 | case GLSL_TYPE_BOOL: |
| 123 | assert(!"FINISHME: Convert bool to float."); |
| 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, |
| 135 | struct _mesa_glsl_parse_state *state) |
| 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()) { |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 147 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | |
| 151 | /* "If one operand is floating-point based and the other is |
| 152 | * not, then the conversions from Section 4.1.10 "Implicit |
| 153 | * Conversions" are applied to the non-floating-point-based operand." |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 154 | */ |
Ian Romanick | 0104536 | 2010-03-29 16:17:56 -0700 | [diff] [blame] | 155 | if (!apply_implicit_conversion(type_a, value_b, state) |
| 156 | && !apply_implicit_conversion(type_b, value_a, state)) { |
| 157 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /* "If the operands are integer types, they must both be signed or |
| 161 | * both be unsigned." |
| 162 | * |
| 163 | * From this rule and the preceeding conversion it can be inferred that |
| 164 | * 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] | 165 | * The is_numeric check above already filtered out the case where either |
| 166 | * type is not one of these, so now the base types need only be tested for |
| 167 | * equality. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 168 | */ |
| 169 | if (type_a->base_type != type_b->base_type) { |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 170 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /* "All arithmetic binary operators result in the same fundamental type |
| 174 | * (signed integer, unsigned integer, or floating-point) as the |
| 175 | * operands they operate on, after operand type conversion. After |
| 176 | * conversion, the following cases are valid |
| 177 | * |
| 178 | * * The two operands are scalars. In this case the operation is |
| 179 | * applied, resulting in a scalar." |
| 180 | */ |
Ian Romanick | cb36f8a | 2010-03-09 15:51:22 -0800 | [diff] [blame] | 181 | if (type_a->is_scalar() && type_b->is_scalar()) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 182 | return type_a; |
| 183 | |
| 184 | /* "* One operand is a scalar, and the other is a vector or matrix. |
| 185 | * In this case, the scalar operation is applied independently to each |
| 186 | * component of the vector or matrix, resulting in the same size |
| 187 | * vector or matrix." |
| 188 | */ |
Ian Romanick | cb36f8a | 2010-03-09 15:51:22 -0800 | [diff] [blame] | 189 | if (type_a->is_scalar()) { |
| 190 | if (!type_b->is_scalar()) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 191 | return type_b; |
Ian Romanick | cb36f8a | 2010-03-09 15:51:22 -0800 | [diff] [blame] | 192 | } else if (type_b->is_scalar()) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 193 | return type_a; |
| 194 | } |
| 195 | |
| 196 | /* All of the combinations of <scalar, scalar>, <vector, scalar>, |
| 197 | * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been |
| 198 | * handled. |
| 199 | */ |
Ian Romanick | 60b54d9 | 2010-03-24 17:08:13 -0700 | [diff] [blame] | 200 | assert(!type_a->is_scalar()); |
| 201 | assert(!type_b->is_scalar()); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 202 | |
| 203 | /* "* The two operands are vectors of the same size. In this case, the |
| 204 | * operation is done component-wise resulting in the same size |
| 205 | * vector." |
| 206 | */ |
Ian Romanick | a2dd22f | 2010-03-09 15:55:16 -0800 | [diff] [blame] | 207 | if (type_a->is_vector() && type_b->is_vector()) { |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 208 | return (type_a == type_b) ? type_a : glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /* All of the combinations of <scalar, scalar>, <vector, scalar>, |
| 212 | * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and |
| 213 | * <vector, vector> have been handled. At least one of the operands must |
| 214 | * be matrix. Further, since there are no integer matrix types, the base |
| 215 | * type of both operands must be float. |
| 216 | */ |
Ian Romanick | 60b54d9 | 2010-03-24 17:08:13 -0700 | [diff] [blame] | 217 | assert(type_a->is_matrix() || type_b->is_matrix()); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 218 | assert(type_a->base_type == GLSL_TYPE_FLOAT); |
| 219 | assert(type_b->base_type == GLSL_TYPE_FLOAT); |
| 220 | |
| 221 | /* "* The operator is add (+), subtract (-), or divide (/), and the |
| 222 | * operands are matrices with the same number of rows and the same |
| 223 | * number of columns. In this case, the operation is done component- |
| 224 | * wise resulting in the same size matrix." |
| 225 | * * The operator is multiply (*), where both operands are matrices or |
| 226 | * one operand is a vector and the other a matrix. A right vector |
| 227 | * operand is treated as a column vector and a left vector operand as a |
| 228 | * row vector. In all these cases, it is required that the number of |
| 229 | * columns of the left operand is equal to the number of rows of the |
| 230 | * right operand. Then, the multiply (*) operation does a linear |
| 231 | * algebraic multiply, yielding an object that has the same number of |
| 232 | * rows as the left operand and the same number of columns as the right |
| 233 | * operand. Section 5.10 "Vector and Matrix Operations" explains in |
| 234 | * more detail how vectors and matrices are operated on." |
| 235 | */ |
| 236 | if (! multiply) { |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 237 | return (type_a == type_b) ? type_a : glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 238 | } else { |
Ian Romanick | fce1150 | 2010-03-09 15:58:52 -0800 | [diff] [blame] | 239 | if (type_a->is_matrix() && type_b->is_matrix()) { |
Ian Romanick | c1bd3a1 | 2010-03-25 13:06:58 -0700 | [diff] [blame] | 240 | /* Matrix multiply. The columns of A must match the rows of B. Given |
| 241 | * the other previously tested constraints, this means the vector type |
| 242 | * of a row from A must be the same as the vector type of a column from |
| 243 | * B. |
| 244 | */ |
| 245 | if (type_a->row_type() == type_b->column_type()) { |
| 246 | /* The resulting matrix has the number of columns of matrix B and |
| 247 | * the number of rows of matrix A. We get the row count of A by |
| 248 | * looking at the size of a vector that makes up a column. The |
| 249 | * transpose (size of a row) is done for B. |
| 250 | */ |
| 251 | return |
| 252 | glsl_type::get_instance(type_a->base_type, |
| 253 | type_a->column_type()->vector_elements, |
| 254 | type_b->row_type()->vector_elements); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 255 | } |
Ian Romanick | fce1150 | 2010-03-09 15:58:52 -0800 | [diff] [blame] | 256 | } else if (type_a->is_matrix()) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 257 | /* 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] | 258 | * rows of B. Given the other previously tested constraints, this |
| 259 | * means the vector type of a row from A must be the same as the |
| 260 | * vector the type of B. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 261 | */ |
Ian Romanick | c1bd3a1 | 2010-03-25 13:06:58 -0700 | [diff] [blame] | 262 | if (type_a->row_type() == type_b) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 263 | return type_b; |
| 264 | } else { |
Ian Romanick | fce1150 | 2010-03-09 15:58:52 -0800 | [diff] [blame] | 265 | assert(type_b->is_matrix()); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 266 | |
Ian Romanick | c1bd3a1 | 2010-03-25 13:06:58 -0700 | [diff] [blame] | 267 | /* A is a row vector and B is a matrix. Columns of A must match rows |
| 268 | * of B. Given the other previously tested constraints, this means |
| 269 | * the type of A must be the same as the vector type of a column from |
| 270 | * B. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 271 | */ |
Ian Romanick | c1bd3a1 | 2010-03-25 13:06:58 -0700 | [diff] [blame] | 272 | if (type_a == type_b->column_type()) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 273 | return type_a; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | |
| 278 | /* "All other cases are illegal." |
| 279 | */ |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 280 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | |
| 284 | static const struct glsl_type * |
| 285 | unary_arithmetic_result_type(const struct glsl_type *type) |
| 286 | { |
| 287 | /* From GLSL 1.50 spec, page 57: |
| 288 | * |
| 289 | * "The arithmetic unary operators negate (-), post- and pre-increment |
| 290 | * and decrement (-- and ++) operate on integer or floating-point |
| 291 | * values (including vectors and matrices). All unary operators work |
| 292 | * component-wise on their operands. These result with the same type |
| 293 | * they operated on." |
| 294 | */ |
Ian Romanick | a6d653d | 2010-03-26 14:40:37 -0700 | [diff] [blame] | 295 | if (!type->is_numeric()) |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 296 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 297 | |
| 298 | return type; |
| 299 | } |
| 300 | |
| 301 | |
| 302 | static const struct glsl_type * |
| 303 | modulus_result_type(const struct glsl_type *type_a, |
| 304 | const struct glsl_type *type_b) |
| 305 | { |
| 306 | /* From GLSL 1.50 spec, page 56: |
| 307 | * "The operator modulus (%) operates on signed or unsigned integers or |
| 308 | * integer vectors. The operand types must both be signed or both be |
| 309 | * unsigned." |
| 310 | */ |
Ian Romanick | 40176e2 | 2010-03-26 14:38:37 -0700 | [diff] [blame] | 311 | if (!type_a->is_integer() || !type_b->is_integer() |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 312 | || (type_a->base_type != type_b->base_type)) { |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 313 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /* "The operands cannot be vectors of differing size. If one operand is |
| 317 | * a scalar and the other vector, then the scalar is applied component- |
| 318 | * wise to the vector, resulting in the same type as the vector. If both |
| 319 | * are vectors of the same size, the result is computed component-wise." |
| 320 | */ |
Ian Romanick | a2dd22f | 2010-03-09 15:55:16 -0800 | [diff] [blame] | 321 | if (type_a->is_vector()) { |
| 322 | if (!type_b->is_vector() |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 323 | || (type_a->vector_elements == type_b->vector_elements)) |
| 324 | return type_a; |
| 325 | } else |
| 326 | return type_b; |
| 327 | |
| 328 | /* "The operator modulus (%) is not defined for any other data types |
| 329 | * (non-integer types)." |
| 330 | */ |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 331 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | |
| 335 | static const struct glsl_type * |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 336 | relational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 337 | struct _mesa_glsl_parse_state *state) |
| 338 | { |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 339 | const glsl_type *const type_a = value_a->type; |
| 340 | const glsl_type *const type_b = value_b->type; |
Ian Romanick | 0150f5f | 2010-03-29 16:20:07 -0700 | [diff] [blame] | 341 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 342 | /* From GLSL 1.50 spec, page 56: |
| 343 | * "The relational operators greater than (>), less than (<), greater |
| 344 | * than or equal (>=), and less than or equal (<=) operate only on |
| 345 | * scalar integer and scalar floating-point expressions." |
| 346 | */ |
Ian Romanick | a6d653d | 2010-03-26 14:40:37 -0700 | [diff] [blame] | 347 | if (!type_a->is_numeric() |
| 348 | || !type_b->is_numeric() |
Ian Romanick | cb36f8a | 2010-03-09 15:51:22 -0800 | [diff] [blame] | 349 | || !type_a->is_scalar() |
| 350 | || !type_b->is_scalar()) |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 351 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 352 | |
| 353 | /* "Either the operands' types must match, or the conversions from |
| 354 | * Section 4.1.10 "Implicit Conversions" will be applied to the integer |
| 355 | * operand, after which the types must match." |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 356 | */ |
Ian Romanick | 0150f5f | 2010-03-29 16:20:07 -0700 | [diff] [blame] | 357 | if (!apply_implicit_conversion(type_a, value_b, state) |
| 358 | && !apply_implicit_conversion(type_b, value_a, state)) { |
| 359 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | if (type_a->base_type != type_b->base_type) |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 363 | return glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 364 | |
| 365 | /* "The result is scalar Boolean." |
| 366 | */ |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 367 | return glsl_type::bool_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 371 | /** |
| 372 | * Validates that a value can be assigned to a location with a specified type |
| 373 | * |
| 374 | * Validates that \c rhs can be assigned to some location. If the types are |
| 375 | * not an exact match but an automatic conversion is possible, \c rhs will be |
| 376 | * converted. |
| 377 | * |
| 378 | * \return |
| 379 | * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type. |
| 380 | * Otherwise the actual RHS to be assigned will be returned. This may be |
| 381 | * \c rhs, or it may be \c rhs after some type conversion. |
| 382 | * |
| 383 | * \note |
| 384 | * In addition to being used for assignments, this function is used to |
| 385 | * type-check return values. |
| 386 | */ |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 387 | ir_rvalue * |
| 388 | validate_assignment(const glsl_type *lhs_type, ir_rvalue *rhs) |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 389 | { |
| 390 | const glsl_type *const rhs_type = rhs->type; |
| 391 | |
| 392 | /* If there is already some error in the RHS, just return it. Anything |
| 393 | * else will lead to an avalanche of error message back to the user. |
| 394 | */ |
| 395 | if (rhs_type->is_error()) |
| 396 | return rhs; |
| 397 | |
| 398 | /* FINISHME: For GLSL 1.10, check that the types are not arrays. */ |
| 399 | |
| 400 | /* If the types are identical, the assignment can trivially proceed. |
| 401 | */ |
| 402 | if (rhs_type == lhs_type) |
| 403 | return rhs; |
| 404 | |
| 405 | /* FINISHME: Check for and apply automatic conversions. */ |
| 406 | return NULL; |
| 407 | } |
| 408 | |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 409 | ir_rvalue * |
| 410 | do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state, |
| 411 | ir_rvalue *lhs, ir_rvalue *rhs, |
| 412 | YYLTYPE lhs_loc) |
| 413 | { |
| 414 | bool error_emitted = (lhs->type->is_error() || rhs->type->is_error()); |
| 415 | |
| 416 | if (!error_emitted) { |
| 417 | /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */ |
| 418 | if (!lhs->is_lvalue()) { |
| 419 | _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment"); |
| 420 | error_emitted = true; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | ir_rvalue *new_rhs = validate_assignment(lhs->type, rhs); |
| 425 | if (new_rhs == NULL) { |
| 426 | _mesa_glsl_error(& lhs_loc, state, "type mismatch"); |
| 427 | } else { |
| 428 | rhs = new_rhs; |
| 429 | } |
| 430 | |
| 431 | ir_instruction *tmp = new ir_assignment(lhs, rhs, NULL); |
| 432 | instructions->push_tail(tmp); |
| 433 | |
| 434 | return rhs; |
| 435 | } |
Ian Romanick | 0bb1c3c | 2010-03-23 13:23:31 -0700 | [diff] [blame] | 436 | |
Ian Romanick | 5185a5f | 2010-03-29 15:20:42 -0700 | [diff] [blame] | 437 | |
| 438 | /** |
| 439 | * Generate a new temporary and add its declaration to the instruction stream |
| 440 | */ |
| 441 | static ir_variable * |
| 442 | generate_temporary(const glsl_type *type, exec_list *instructions, |
| 443 | struct _mesa_glsl_parse_state *state) |
| 444 | { |
| 445 | char *name = (char *) malloc(sizeof(char) * 13); |
| 446 | |
| 447 | snprintf(name, 13, "tmp_%08X", state->temp_index); |
| 448 | state->temp_index++; |
| 449 | |
| 450 | ir_variable *const var = new ir_variable(type, name); |
| 451 | instructions->push_tail(var); |
| 452 | |
| 453 | return var; |
| 454 | } |
| 455 | |
| 456 | |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 457 | static ir_rvalue * |
| 458 | get_lvalue_copy(exec_list *instructions, struct _mesa_glsl_parse_state *state, |
| 459 | ir_rvalue *lvalue, YYLTYPE loc) |
| 460 | { |
| 461 | ir_variable *var; |
| 462 | ir_rvalue *var_deref; |
| 463 | |
| 464 | /* FINISHME: Give unique names to the temporaries. */ |
| 465 | var = new ir_variable(lvalue->type, "_internal_tmp"); |
| 466 | var->mode = ir_var_auto; |
| 467 | |
| 468 | var_deref = new ir_dereference(var); |
| 469 | do_assignment(instructions, state, var_deref, lvalue, loc); |
| 470 | |
| 471 | /* Once we've created this temporary, mark it read only so it's no |
| 472 | * longer considered an lvalue. |
| 473 | */ |
| 474 | var->read_only = true; |
| 475 | |
| 476 | return var_deref; |
| 477 | } |
| 478 | |
| 479 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 480 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 481 | ast_node::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 482 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 483 | { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 484 | (void) instructions; |
| 485 | (void) state; |
| 486 | |
| 487 | return NULL; |
| 488 | } |
| 489 | |
| 490 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 491 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 492 | ast_expression::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 493 | struct _mesa_glsl_parse_state *state) |
| 494 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 495 | static const int operations[AST_NUM_OPERATORS] = { |
| 496 | -1, /* ast_assign doesn't convert to ir_expression. */ |
| 497 | -1, /* ast_plus doesn't convert to ir_expression. */ |
| 498 | ir_unop_neg, |
| 499 | ir_binop_add, |
| 500 | ir_binop_sub, |
| 501 | ir_binop_mul, |
| 502 | ir_binop_div, |
| 503 | ir_binop_mod, |
| 504 | ir_binop_lshift, |
| 505 | ir_binop_rshift, |
| 506 | ir_binop_less, |
| 507 | ir_binop_greater, |
| 508 | ir_binop_lequal, |
| 509 | ir_binop_gequal, |
| 510 | ir_binop_equal, |
| 511 | ir_binop_nequal, |
| 512 | ir_binop_bit_and, |
| 513 | ir_binop_bit_xor, |
| 514 | ir_binop_bit_or, |
| 515 | ir_unop_bit_not, |
| 516 | ir_binop_logic_and, |
| 517 | ir_binop_logic_xor, |
| 518 | ir_binop_logic_or, |
| 519 | ir_unop_logic_not, |
| 520 | |
| 521 | /* Note: The following block of expression types actually convert |
| 522 | * to multiple IR instructions. |
| 523 | */ |
| 524 | ir_binop_mul, /* ast_mul_assign */ |
| 525 | ir_binop_div, /* ast_div_assign */ |
| 526 | ir_binop_mod, /* ast_mod_assign */ |
| 527 | ir_binop_add, /* ast_add_assign */ |
| 528 | ir_binop_sub, /* ast_sub_assign */ |
| 529 | ir_binop_lshift, /* ast_ls_assign */ |
| 530 | ir_binop_rshift, /* ast_rs_assign */ |
| 531 | ir_binop_bit_and, /* ast_and_assign */ |
| 532 | ir_binop_bit_xor, /* ast_xor_assign */ |
| 533 | ir_binop_bit_or, /* ast_or_assign */ |
| 534 | |
| 535 | -1, /* ast_conditional doesn't convert to ir_expression. */ |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 536 | ir_binop_add, /* ast_pre_inc. */ |
| 537 | ir_binop_sub, /* ast_pre_dec. */ |
| 538 | ir_binop_add, /* ast_post_inc. */ |
| 539 | ir_binop_sub, /* ast_post_dec. */ |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 540 | -1, /* ast_field_selection doesn't conv to ir_expression. */ |
| 541 | -1, /* ast_array_index doesn't convert to ir_expression. */ |
| 542 | -1, /* ast_function_call doesn't conv to ir_expression. */ |
| 543 | -1, /* ast_identifier doesn't convert to ir_expression. */ |
| 544 | -1, /* ast_int_constant doesn't convert to ir_expression. */ |
| 545 | -1, /* ast_uint_constant doesn't conv to ir_expression. */ |
| 546 | -1, /* ast_float_constant doesn't conv to ir_expression. */ |
| 547 | -1, /* ast_bool_constant doesn't conv to ir_expression. */ |
| 548 | -1, /* ast_sequence doesn't convert to ir_expression. */ |
| 549 | }; |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 550 | ir_rvalue *result = NULL; |
| 551 | ir_rvalue *op[2]; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 552 | struct simple_node op_list; |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 553 | const struct glsl_type *type = glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 554 | bool error_emitted = false; |
| 555 | YYLTYPE loc; |
| 556 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 557 | loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 558 | make_empty_list(& op_list); |
| 559 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 560 | switch (this->oper) { |
Ian Romanick | 6652af3 | 2010-03-09 16:38:02 -0800 | [diff] [blame] | 561 | case ast_assign: { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 562 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 563 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 564 | |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 565 | result = do_assignment(instructions, state, op[0], op[1], |
| 566 | this->subexpressions[0]->get_location()); |
| 567 | error_emitted = result->type->is_error(); |
| 568 | type = result->type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 569 | break; |
Ian Romanick | 6652af3 | 2010-03-09 16:38:02 -0800 | [diff] [blame] | 570 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 571 | |
| 572 | case ast_plus: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 573 | op[0] = this->subexpressions[0]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 574 | |
Ian Romanick | a43817a | 2010-03-26 14:27:23 -0700 | [diff] [blame] | 575 | error_emitted = op[0]->type->is_error(); |
| 576 | if (type->is_error()) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 577 | op[0]->type = type; |
| 578 | |
| 579 | result = op[0]; |
| 580 | break; |
| 581 | |
| 582 | case ast_neg: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 583 | op[0] = this->subexpressions[0]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 584 | |
| 585 | type = unary_arithmetic_result_type(op[0]->type); |
| 586 | |
Ian Romanick | a43817a | 2010-03-26 14:27:23 -0700 | [diff] [blame] | 587 | error_emitted = op[0]->type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 588 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 589 | result = new ir_expression(operations[this->oper], type, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 590 | op[0], NULL); |
| 591 | break; |
| 592 | |
| 593 | case ast_add: |
| 594 | case ast_sub: |
| 595 | case ast_mul: |
| 596 | case ast_div: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 597 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 598 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 599 | |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 600 | type = arithmetic_result_type(op[0], op[1], |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 601 | (this->oper == ast_mul), |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 602 | state); |
| 603 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 604 | result = new ir_expression(operations[this->oper], type, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 605 | op[0], op[1]); |
| 606 | break; |
| 607 | |
| 608 | case ast_mod: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 609 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 610 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 611 | |
Ian Romanick | a43817a | 2010-03-26 14:27:23 -0700 | [diff] [blame] | 612 | error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 613 | |
| 614 | type = modulus_result_type(op[0]->type, op[1]->type); |
| 615 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 616 | assert(operations[this->oper] == ir_binop_mod); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 617 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 618 | result = new ir_expression(operations[this->oper], type, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 619 | op[0], op[1]); |
| 620 | break; |
| 621 | |
| 622 | case ast_lshift: |
| 623 | case ast_rshift: |
| 624 | /* FINISHME: Implement bit-shift operators. */ |
| 625 | break; |
| 626 | |
| 627 | case ast_less: |
| 628 | case ast_greater: |
| 629 | case ast_lequal: |
| 630 | case ast_gequal: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 631 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 632 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 633 | |
Ian Romanick | a43817a | 2010-03-26 14:27:23 -0700 | [diff] [blame] | 634 | error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 635 | |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 636 | type = relational_result_type(op[0], op[1], state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 637 | |
| 638 | /* The relational operators must either generate an error or result |
| 639 | * in a scalar boolean. See page 57 of the GLSL 1.50 spec. |
| 640 | */ |
Ian Romanick | a43817a | 2010-03-26 14:27:23 -0700 | [diff] [blame] | 641 | assert(type->is_error() |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 642 | || ((type->base_type == GLSL_TYPE_BOOL) |
Ian Romanick | cb36f8a | 2010-03-09 15:51:22 -0800 | [diff] [blame] | 643 | && type->is_scalar())); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 644 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 645 | result = new ir_expression(operations[this->oper], type, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 646 | op[0], op[1]); |
| 647 | break; |
| 648 | |
| 649 | case ast_nequal: |
| 650 | case ast_equal: |
Ian Romanick | 6e659ca | 2010-03-29 15:11:05 -0700 | [diff] [blame] | 651 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 652 | op[1] = this->subexpressions[1]->hir(instructions, state); |
| 653 | |
| 654 | /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec: |
| 655 | * |
| 656 | * "The equality operators equal (==), and not equal (!=) |
| 657 | * operate on all types. They result in a scalar Boolean. If |
| 658 | * the operand types do not match, then there must be a |
| 659 | * conversion from Section 4.1.10 "Implicit Conversions" |
| 660 | * applied to one operand that can make them match, in which |
| 661 | * case this conversion is done." |
| 662 | */ |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 663 | if ((!apply_implicit_conversion(op[0]->type, op[1], state) |
| 664 | && !apply_implicit_conversion(op[1]->type, op[0], state)) |
Ian Romanick | 212b032 | 2010-03-29 16:22:38 -0700 | [diff] [blame] | 665 | || (op[0]->type != op[1]->type)) { |
Ian Romanick | 6e659ca | 2010-03-29 15:11:05 -0700 | [diff] [blame] | 666 | _mesa_glsl_error(& loc, state, "operands of `%s' must have the same " |
| 667 | "type", (this->oper == ast_equal) ? "==" : "!="); |
| 668 | error_emitted = true; |
Ian Romanick | a80cbd6 | 2010-03-30 17:04:48 -0700 | [diff] [blame] | 669 | } else if ((state->language_version <= 110) |
| 670 | && (op[0]->type->is_array() || op[1]->type->is_array())) { |
| 671 | _mesa_glsl_error(& loc, state, "array comparisons forbidden in " |
| 672 | "GLSL 1.10"); |
| 673 | error_emitted = true; |
Ian Romanick | 6e659ca | 2010-03-29 15:11:05 -0700 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | result = new ir_expression(operations[this->oper], glsl_type::bool_type, |
| 677 | op[0], op[1]); |
| 678 | type = glsl_type::bool_type; |
| 679 | |
| 680 | assert(result->type == glsl_type::bool_type); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 681 | break; |
| 682 | |
| 683 | case ast_bit_and: |
| 684 | case ast_bit_xor: |
| 685 | case ast_bit_or: |
| 686 | case ast_bit_not: |
| 687 | /* FINISHME: Implement bit-wise operators. */ |
| 688 | break; |
| 689 | |
| 690 | case ast_logic_and: |
| 691 | case ast_logic_xor: |
| 692 | case ast_logic_or: |
| 693 | case ast_logic_not: |
Eric Anholt | b82c0c3 | 2010-03-31 09:11:39 -1000 | [diff] [blame] | 694 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 695 | op[1] = this->subexpressions[1]->hir(instructions, state); |
| 696 | |
| 697 | if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) { |
| 698 | YYLTYPE loc = this->subexpressions[0]->get_location(); |
| 699 | |
| 700 | _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean", |
| 701 | operator_string(this->oper)); |
| 702 | } |
| 703 | |
| 704 | if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) { |
| 705 | YYLTYPE loc = this->subexpressions[1]->get_location(); |
| 706 | |
| 707 | _mesa_glsl_error(& loc, state, "RHS of `%s' must be scalar boolean", |
| 708 | operator_string(this->oper)); |
| 709 | } |
| 710 | |
| 711 | result = new ir_expression(operations[this->oper], glsl_type::bool_type, |
| 712 | op[0], op[1]); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 713 | break; |
| 714 | |
| 715 | case ast_mul_assign: |
| 716 | case ast_div_assign: |
| 717 | case ast_add_assign: |
| 718 | case ast_sub_assign: { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 719 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 720 | op[1] = this->subexpressions[1]->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 721 | |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 722 | type = arithmetic_result_type(op[0], op[1], |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 723 | (this->oper == ast_mul_assign), |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 724 | state); |
| 725 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 726 | ir_rvalue *temp_rhs = new ir_expression(operations[this->oper], type, |
| 727 | op[0], op[1]); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 728 | |
Eric Anholt | 10a6852 | 2010-03-26 11:53:37 -0700 | [diff] [blame] | 729 | result = do_assignment(instructions, state, op[0], temp_rhs, |
| 730 | this->subexpressions[0]->get_location()); |
| 731 | type = result->type; |
| 732 | error_emitted = (op[0]->type->is_error()); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 733 | |
| 734 | /* GLSL 1.10 does not allow array assignment. However, we don't have to |
| 735 | * explicitly test for this because none of the binary expression |
| 736 | * operators allow array operands either. |
| 737 | */ |
| 738 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 739 | break; |
| 740 | } |
| 741 | |
Eric Anholt | 48a0e64 | 2010-03-26 11:57:46 -0700 | [diff] [blame] | 742 | case ast_mod_assign: { |
| 743 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 744 | op[1] = this->subexpressions[1]->hir(instructions, state); |
| 745 | |
| 746 | error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); |
| 747 | |
| 748 | type = modulus_result_type(op[0]->type, op[1]->type); |
| 749 | |
| 750 | assert(operations[this->oper] == ir_binop_mod); |
| 751 | |
| 752 | struct ir_rvalue *temp_rhs; |
| 753 | temp_rhs = new ir_expression(operations[this->oper], type, |
| 754 | op[0], op[1]); |
| 755 | |
| 756 | result = do_assignment(instructions, state, op[0], temp_rhs, |
| 757 | this->subexpressions[0]->get_location()); |
| 758 | type = result->type; |
| 759 | error_emitted = op[0]->type->is_error(); |
| 760 | break; |
| 761 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 762 | |
| 763 | case ast_ls_assign: |
| 764 | case ast_rs_assign: |
Ian Romanick | 251eb75 | 2010-03-29 14:15:05 -0700 | [diff] [blame] | 765 | break; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 766 | |
| 767 | case ast_and_assign: |
| 768 | case ast_xor_assign: |
| 769 | case ast_or_assign: |
Ian Romanick | 251eb75 | 2010-03-29 14:15:05 -0700 | [diff] [blame] | 770 | break; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 771 | |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 772 | case ast_conditional: { |
| 773 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 774 | |
| 775 | /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec: |
| 776 | * |
| 777 | * "The ternary selection operator (?:). It operates on three |
| 778 | * expressions (exp1 ? exp2 : exp3). This operator evaluates the |
| 779 | * first expression, which must result in a scalar Boolean." |
| 780 | */ |
| 781 | if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) { |
| 782 | YYLTYPE loc = this->subexpressions[0]->get_location(); |
| 783 | |
| 784 | _mesa_glsl_error(& loc, state, "?: condition must be scalar boolean"); |
| 785 | error_emitted = true; |
| 786 | } |
| 787 | |
| 788 | /* The :? operator is implemented by generating an anonymous temporary |
| 789 | * followed by an if-statement. The last instruction in each branch of |
| 790 | * the if-statement assigns a value to the anonymous temporary. This |
| 791 | * temporary is the r-value of the expression. |
| 792 | */ |
| 793 | ir_variable *const tmp = generate_temporary(glsl_type::error_type, |
| 794 | instructions, state); |
| 795 | |
| 796 | ir_if *const stmt = new ir_if(op[0]); |
| 797 | instructions->push_tail(stmt); |
| 798 | |
| 799 | op[1] = this->subexpressions[1]->hir(& stmt->then_instructions, state); |
| 800 | ir_dereference *const then_deref = new ir_dereference(tmp); |
| 801 | ir_assignment *const then_assign = |
| 802 | new ir_assignment(then_deref, op[1], NULL); |
| 803 | stmt->then_instructions.push_tail(then_assign); |
| 804 | |
| 805 | op[2] = this->subexpressions[2]->hir(& stmt->else_instructions, state); |
| 806 | ir_dereference *const else_deref = new ir_dereference(tmp); |
| 807 | ir_assignment *const else_assign = |
| 808 | new ir_assignment(else_deref, op[2], NULL); |
| 809 | stmt->else_instructions.push_tail(else_assign); |
| 810 | |
| 811 | /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec: |
| 812 | * |
| 813 | * "The second and third expressions can be any type, as |
| 814 | * long their types match, or there is a conversion in |
| 815 | * Section 4.1.10 "Implicit Conversions" that can be applied |
| 816 | * to one of the expressions to make their types match. This |
| 817 | * resulting matching type is the type of the entire |
| 818 | * expression." |
| 819 | */ |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 820 | if ((!apply_implicit_conversion(op[1]->type, op[2], state) |
| 821 | && !apply_implicit_conversion(op[2]->type, op[1], state)) |
Ian Romanick | db9be2e | 2010-03-29 16:25:56 -0700 | [diff] [blame] | 822 | || (op[1]->type != op[2]->type)) { |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 823 | YYLTYPE loc = this->subexpressions[1]->get_location(); |
| 824 | |
| 825 | _mesa_glsl_error(& loc, state, "Second and third operands of ?: " |
| 826 | "operator must have matching types."); |
| 827 | error_emitted = true; |
Ian Romanick | db9be2e | 2010-03-29 16:25:56 -0700 | [diff] [blame] | 828 | } else { |
| 829 | tmp->type = op[1]->type; |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 830 | } |
| 831 | |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 832 | result = new ir_dereference(tmp); |
| 833 | type = tmp->type; |
Ian Romanick | 251eb75 | 2010-03-29 14:15:05 -0700 | [diff] [blame] | 834 | break; |
Ian Romanick | 96f9cea | 2010-03-29 15:33:54 -0700 | [diff] [blame] | 835 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 836 | |
| 837 | case ast_pre_inc: |
Eric Anholt | 76ea56c | 2010-03-26 12:16:54 -0700 | [diff] [blame] | 838 | case ast_pre_dec: { |
| 839 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 840 | if (op[0]->type->base_type == GLSL_TYPE_FLOAT) |
| 841 | op[1] = new ir_constant(1.0f); |
| 842 | else |
| 843 | op[1] = new ir_constant(1); |
| 844 | |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 845 | type = arithmetic_result_type(op[0], op[1], false, state); |
Eric Anholt | 76ea56c | 2010-03-26 12:16:54 -0700 | [diff] [blame] | 846 | |
| 847 | struct ir_rvalue *temp_rhs; |
| 848 | temp_rhs = new ir_expression(operations[this->oper], type, |
| 849 | op[0], op[1]); |
| 850 | |
| 851 | result = do_assignment(instructions, state, op[0], temp_rhs, |
| 852 | this->subexpressions[0]->get_location()); |
| 853 | type = result->type; |
| 854 | error_emitted = op[0]->type->is_error(); |
| 855 | break; |
| 856 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 857 | |
| 858 | case ast_post_inc: |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 859 | case ast_post_dec: { |
| 860 | op[0] = this->subexpressions[0]->hir(instructions, state); |
| 861 | if (op[0]->type->base_type == GLSL_TYPE_FLOAT) |
| 862 | op[1] = new ir_constant(1.0f); |
| 863 | else |
| 864 | op[1] = new ir_constant(1); |
| 865 | |
| 866 | error_emitted = op[0]->type->is_error() || op[1]->type->is_error(); |
| 867 | |
Ian Romanick | bfb09c2 | 2010-03-29 16:32:55 -0700 | [diff] [blame] | 868 | type = arithmetic_result_type(op[0], op[1], false, state); |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 869 | |
| 870 | struct ir_rvalue *temp_rhs; |
| 871 | temp_rhs = new ir_expression(operations[this->oper], type, |
| 872 | op[0], op[1]); |
| 873 | |
| 874 | /* Get a temporary of a copy of the lvalue before it's modified. |
| 875 | * This may get thrown away later. |
| 876 | */ |
| 877 | result = get_lvalue_copy(instructions, state, op[0], |
| 878 | this->subexpressions[0]->get_location()); |
| 879 | |
| 880 | (void)do_assignment(instructions, state, op[0], temp_rhs, |
| 881 | this->subexpressions[0]->get_location()); |
| 882 | |
| 883 | type = result->type; |
| 884 | error_emitted = op[0]->type->is_error(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 885 | break; |
Eric Anholt | de38f0e | 2010-03-26 12:14:54 -0700 | [diff] [blame] | 886 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 887 | |
| 888 | case ast_field_selection: |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 889 | result = _mesa_ast_field_selection_to_hir(this, instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 890 | type = result->type; |
| 891 | break; |
| 892 | |
| 893 | case ast_array_index: |
| 894 | break; |
| 895 | |
| 896 | case ast_function_call: |
Ian Romanick | 7cfddf1 | 2010-03-10 13:26:52 -0800 | [diff] [blame] | 897 | /* Should *NEVER* get here. ast_function_call should always be handled |
| 898 | * by ast_function_expression::hir. |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 899 | */ |
Ian Romanick | 7cfddf1 | 2010-03-10 13:26:52 -0800 | [diff] [blame] | 900 | assert(0); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 901 | break; |
| 902 | |
| 903 | case ast_identifier: { |
| 904 | /* ast_identifier can appear several places in a full abstract syntax |
| 905 | * tree. This particular use must be at location specified in the grammar |
| 906 | * as 'variable_identifier'. |
| 907 | */ |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 908 | ir_variable *var = |
| 909 | state->symbols->get_variable(this->primary_expression.identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 910 | |
| 911 | result = new ir_dereference(var); |
| 912 | |
| 913 | if (var != NULL) { |
| 914 | type = result->type; |
| 915 | } else { |
Ian Romanick | 71d0bbf | 2010-03-23 13:21:19 -0700 | [diff] [blame] | 916 | _mesa_glsl_error(& loc, state, "`%s' undeclared", |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 917 | this->primary_expression.identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 918 | |
| 919 | error_emitted = true; |
| 920 | } |
| 921 | break; |
| 922 | } |
| 923 | |
| 924 | case ast_int_constant: |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 925 | type = glsl_type::int_type; |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 926 | result = new ir_constant(type, & this->primary_expression); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 927 | break; |
| 928 | |
| 929 | case ast_uint_constant: |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 930 | type = glsl_type::uint_type; |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 931 | result = new ir_constant(type, & this->primary_expression); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 932 | break; |
| 933 | |
| 934 | case ast_float_constant: |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 935 | type = glsl_type::float_type; |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 936 | result = new ir_constant(type, & this->primary_expression); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 937 | break; |
| 938 | |
| 939 | case ast_bool_constant: |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 940 | type = glsl_type::bool_type; |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 941 | result = new ir_constant(type, & this->primary_expression); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 942 | break; |
| 943 | |
| 944 | case ast_sequence: { |
| 945 | struct simple_node *ptr; |
| 946 | |
| 947 | /* It should not be possible to generate a sequence in the AST without |
| 948 | * any expressions in it. |
| 949 | */ |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 950 | assert(!is_empty_list(&this->expressions)); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 951 | |
| 952 | /* The r-value of a sequence is the last expression in the sequence. If |
| 953 | * the other expressions in the sequence do not have side-effects (and |
| 954 | * therefore add instructions to the instruction list), they get dropped |
| 955 | * on the floor. |
| 956 | */ |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 957 | foreach (ptr, &this->expressions) |
| 958 | result = ((ast_node *)ptr)->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 959 | |
| 960 | type = result->type; |
| 961 | |
| 962 | /* Any errors should have already been emitted in the loop above. |
| 963 | */ |
| 964 | error_emitted = true; |
| 965 | break; |
| 966 | } |
| 967 | } |
| 968 | |
Ian Romanick | cef3bae | 2010-03-26 14:41:32 -0700 | [diff] [blame] | 969 | if (type->is_error() && !error_emitted) |
Ian Romanick | 71d0bbf | 2010-03-23 13:21:19 -0700 | [diff] [blame] | 970 | _mesa_glsl_error(& loc, state, "type mismatch"); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 971 | |
| 972 | return result; |
| 973 | } |
| 974 | |
| 975 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 976 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 977 | ast_expression_statement::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 978 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 979 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 980 | /* It is possible to have expression statements that don't have an |
| 981 | * expression. This is the solitary semicolon: |
| 982 | * |
| 983 | * for (i = 0; i < 5; i++) |
| 984 | * ; |
| 985 | * |
| 986 | * In this case the expression will be NULL. Test for NULL and don't do |
| 987 | * anything in that case. |
| 988 | */ |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 989 | if (expression != NULL) |
| 990 | expression->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 991 | |
| 992 | /* Statements do not have r-values. |
| 993 | */ |
| 994 | return NULL; |
| 995 | } |
| 996 | |
| 997 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 998 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 999 | ast_compound_statement::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1000 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1001 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1002 | struct simple_node *ptr; |
| 1003 | |
| 1004 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1005 | if (new_scope) |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 1006 | state->symbols->push_scope(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1007 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1008 | foreach (ptr, &statements) |
| 1009 | ((ast_node *)ptr)->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1010 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1011 | if (new_scope) |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 1012 | state->symbols->pop_scope(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1013 | |
| 1014 | /* Compound statements do not have r-values. |
| 1015 | */ |
| 1016 | return NULL; |
| 1017 | } |
| 1018 | |
| 1019 | |
Ian Romanick | 28009cd | 2010-03-30 16:59:27 -0700 | [diff] [blame] | 1020 | static const glsl_type * |
| 1021 | process_array_type(const glsl_type *base, ast_node *array_size, |
| 1022 | struct _mesa_glsl_parse_state *state) |
| 1023 | { |
| 1024 | unsigned length = 0; |
| 1025 | |
| 1026 | /* FINISHME: Reject delcarations of multidimensional arrays. */ |
| 1027 | |
| 1028 | if (array_size != NULL) { |
| 1029 | exec_list dummy_instructions; |
| 1030 | ir_rvalue *const ir = array_size->hir(& dummy_instructions, state); |
| 1031 | YYLTYPE loc = array_size->get_location(); |
| 1032 | |
| 1033 | /* FINISHME: Verify that the grammar forbids side-effects in array |
| 1034 | * FINISHME: sizes. i.e., 'vec4 [x = 12] data' |
| 1035 | */ |
| 1036 | assert(dummy_instructions.is_empty()); |
| 1037 | |
| 1038 | if (ir != NULL) { |
| 1039 | if (!ir->type->is_integer()) { |
| 1040 | _mesa_glsl_error(& loc, state, "array size must be integer type"); |
| 1041 | } else if (!ir->type->is_scalar()) { |
| 1042 | _mesa_glsl_error(& loc, state, "array size must be scalar type"); |
| 1043 | } else { |
| 1044 | ir_constant *const size = ir->constant_expression_value(); |
| 1045 | |
| 1046 | if (size == NULL) { |
| 1047 | _mesa_glsl_error(& loc, state, "array size must be a " |
| 1048 | "constant valued expression"); |
| 1049 | } else if (size->value.i[0] <= 0) { |
| 1050 | _mesa_glsl_error(& loc, state, "array size must be > 0"); |
| 1051 | } else { |
| 1052 | assert(size->type == ir->type); |
| 1053 | length = size->value.u[0]; |
| 1054 | } |
| 1055 | } |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | return glsl_type::get_array_instance(base, length); |
| 1060 | } |
| 1061 | |
| 1062 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1063 | static const struct glsl_type * |
| 1064 | type_specifier_to_glsl_type(const struct ast_type_specifier *spec, |
| 1065 | const char **name, |
| 1066 | struct _mesa_glsl_parse_state *state) |
| 1067 | { |
Ian Romanick | 28009cd | 2010-03-30 16:59:27 -0700 | [diff] [blame] | 1068 | const glsl_type *type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1069 | |
| 1070 | if (spec->type_specifier == ast_struct) { |
| 1071 | /* FINISHME: Handle annonymous structures. */ |
| 1072 | type = NULL; |
| 1073 | } else { |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 1074 | type = state->symbols->get_type(spec->type_name); |
Ian Romanick | 7f9d309 | 2010-03-15 14:15:15 -0700 | [diff] [blame] | 1075 | *name = spec->type_name; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1076 | |
Ian Romanick | 28009cd | 2010-03-30 16:59:27 -0700 | [diff] [blame] | 1077 | if (spec->is_array) { |
| 1078 | type = process_array_type(type, spec->array_size, state); |
| 1079 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | return type; |
| 1083 | } |
| 1084 | |
| 1085 | |
| 1086 | static void |
| 1087 | apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual, |
| 1088 | struct ir_variable *var, |
Eric Anholt | 2e063f1 | 2010-03-28 00:56:22 -0700 | [diff] [blame] | 1089 | struct _mesa_glsl_parse_state *state, |
| 1090 | YYLTYPE *loc) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1091 | { |
| 1092 | if (qual->invariant) |
| 1093 | var->invariant = 1; |
| 1094 | |
| 1095 | /* FINISHME: Mark 'in' variables at global scope as read-only. */ |
| 1096 | if (qual->constant || qual->attribute || qual->uniform |
| 1097 | || (qual->varying && (state->target == fragment_shader))) |
| 1098 | var->read_only = 1; |
| 1099 | |
| 1100 | if (qual->centroid) |
| 1101 | var->centroid = 1; |
| 1102 | |
Eric Anholt | 2e063f1 | 2010-03-28 00:56:22 -0700 | [diff] [blame] | 1103 | if (qual->attribute && state->target == fragment_shader) { |
| 1104 | var->type = glsl_type::error_type; |
| 1105 | _mesa_glsl_error(loc, state, |
| 1106 | "`attribute' variables may not be declared in the " |
| 1107 | "fragment shader"); |
| 1108 | } |
| 1109 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1110 | if (qual->in && qual->out) |
| 1111 | var->mode = ir_var_inout; |
| 1112 | else if (qual->attribute || qual->in |
| 1113 | || (qual->varying && (state->target == fragment_shader))) |
| 1114 | var->mode = ir_var_in; |
Ian Romanick | 0b67823 | 2010-03-10 00:28:59 -0800 | [diff] [blame] | 1115 | else if (qual->out || (qual->varying && (state->target == vertex_shader))) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1116 | var->mode = ir_var_out; |
| 1117 | else if (qual->uniform) |
| 1118 | var->mode = ir_var_uniform; |
| 1119 | else |
| 1120 | var->mode = ir_var_auto; |
| 1121 | |
| 1122 | if (qual->flat) |
| 1123 | var->interpolation = ir_var_flat; |
| 1124 | else if (qual->noperspective) |
| 1125 | var->interpolation = ir_var_noperspective; |
| 1126 | else |
| 1127 | var->interpolation = ir_var_smooth; |
| 1128 | } |
| 1129 | |
| 1130 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 1131 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1132 | ast_declarator_list::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1133 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1134 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1135 | struct simple_node *ptr; |
| 1136 | const struct glsl_type *decl_type; |
| 1137 | const char *type_name = NULL; |
| 1138 | |
| 1139 | |
| 1140 | /* FINISHME: Handle vertex shader "invariant" declarations that do not |
| 1141 | * FINISHME: include a type. These re-declare built-in variables to be |
| 1142 | * FINISHME: invariant. |
| 1143 | */ |
| 1144 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1145 | decl_type = type_specifier_to_glsl_type(this->type->specifier, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1146 | & type_name, state); |
| 1147 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1148 | foreach (ptr, &this->declarations) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1149 | struct ast_declaration *const decl = (struct ast_declaration * )ptr; |
| 1150 | const struct glsl_type *var_type; |
| 1151 | struct ir_variable *var; |
Eric Anholt | 2e063f1 | 2010-03-28 00:56:22 -0700 | [diff] [blame] | 1152 | YYLTYPE loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1153 | |
| 1154 | /* FINISHME: Emit a warning if a variable declaration shadows a |
| 1155 | * FINISHME: declaration at a higher scope. |
| 1156 | */ |
| 1157 | |
Ian Romanick | cec65a6 | 2010-03-23 12:28:44 -0700 | [diff] [blame] | 1158 | if ((decl_type == NULL) || decl_type->is_void()) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1159 | if (type_name != NULL) { |
| 1160 | _mesa_glsl_error(& loc, state, |
| 1161 | "invalid type `%s' in declaration of `%s'", |
| 1162 | type_name, decl->identifier); |
| 1163 | } else { |
| 1164 | _mesa_glsl_error(& loc, state, |
| 1165 | "invalid type in declaration of `%s'", |
| 1166 | decl->identifier); |
| 1167 | } |
| 1168 | continue; |
| 1169 | } |
| 1170 | |
| 1171 | if (decl->is_array) { |
Ian Romanick | 28009cd | 2010-03-30 16:59:27 -0700 | [diff] [blame] | 1172 | var_type = process_array_type(decl_type, decl->array_size, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1173 | } else { |
| 1174 | var_type = decl_type; |
| 1175 | } |
| 1176 | |
| 1177 | var = new ir_variable(var_type, decl->identifier); |
| 1178 | |
Eric Anholt | 2e063f1 | 2010-03-28 00:56:22 -0700 | [diff] [blame] | 1179 | apply_type_qualifier_to_variable(& this->type->qualifier, var, state, |
| 1180 | & loc); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1181 | |
| 1182 | /* Attempt to add the variable to the symbol table. If this fails, it |
| 1183 | * means the variable has already been declared at this scope. |
| 1184 | */ |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 1185 | if (state->symbols->name_declared_this_scope(decl->identifier)) { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1186 | YYLTYPE loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1187 | |
| 1188 | _mesa_glsl_error(& loc, state, "`%s' redeclared", |
| 1189 | decl->identifier); |
| 1190 | continue; |
| 1191 | } |
| 1192 | |
Eric Anholt | b97ee2e | 2010-03-31 08:20:58 -1000 | [diff] [blame] | 1193 | /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec, |
| 1194 | * |
| 1195 | * "Identifiers starting with "gl_" are reserved for use by |
| 1196 | * OpenGL, and may not be declared in a shader as either a |
| 1197 | * variable or a function." |
| 1198 | */ |
| 1199 | if (strncmp(decl->identifier, "gl_", 3) == 0) { |
| 1200 | /* FINISHME: This should only trigger if we're not redefining |
| 1201 | * FINISHME: a builtin (to add a qualifier, for example). |
| 1202 | */ |
| 1203 | _mesa_glsl_error(& loc, state, |
| 1204 | "identifier `%s' uses reserved `gl_' prefix", |
| 1205 | decl->identifier); |
| 1206 | } |
| 1207 | |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1208 | instructions->push_tail(var); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1209 | |
Ian Romanick | e1c1a3f | 2010-03-31 12:26:03 -0700 | [diff] [blame] | 1210 | if (state->current_function != NULL) { |
Ian Romanick | b168e53 | 2010-03-31 12:31:18 -0700 | [diff] [blame^] | 1211 | const char *mode = NULL; |
| 1212 | |
| 1213 | /* FINISHME: Variables that are in or out must be declared either at |
| 1214 | * FINISHME: global scope or FINISHME: in a parameter list. |
Ian Romanick | e1c1a3f | 2010-03-31 12:26:03 -0700 | [diff] [blame] | 1215 | */ |
| 1216 | if (this->type->qualifier.attribute) { |
Ian Romanick | b168e53 | 2010-03-31 12:31:18 -0700 | [diff] [blame^] | 1217 | mode = "attribute"; |
| 1218 | } else if (this->type->qualifier.uniform) { |
| 1219 | mode = "uniform"; |
| 1220 | } else if (this->type->qualifier.varying) { |
| 1221 | mode = "varying"; |
| 1222 | } |
| 1223 | |
| 1224 | if (mode) { |
Ian Romanick | e1c1a3f | 2010-03-31 12:26:03 -0700 | [diff] [blame] | 1225 | _mesa_glsl_error(& loc, state, |
Ian Romanick | b168e53 | 2010-03-31 12:31:18 -0700 | [diff] [blame^] | 1226 | "%s variable `%s' must be declared at " |
Ian Romanick | e1c1a3f | 2010-03-31 12:26:03 -0700 | [diff] [blame] | 1227 | "global scope", |
| 1228 | var->name); |
| 1229 | } |
| 1230 | } else if (var->mode == ir_var_in) { |
Ian Romanick | fb9f5b0 | 2010-03-29 17:16:35 -0700 | [diff] [blame] | 1231 | if (state->target == vertex_shader) { |
| 1232 | bool error_emitted = false; |
| 1233 | |
| 1234 | /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec: |
| 1235 | * |
| 1236 | * "Vertex shader inputs can only be float, floating-point |
| 1237 | * vectors, matrices, signed and unsigned integers and integer |
| 1238 | * vectors. Vertex shader inputs can also form arrays of these |
| 1239 | * types, but not structures." |
| 1240 | * |
Ian Romanick | 2d81620 | 2010-03-29 17:40:11 -0700 | [diff] [blame] | 1241 | * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec: |
| 1242 | * |
| 1243 | * "Vertex shader inputs can only be float, floating-point |
| 1244 | * vectors, matrices, signed and unsigned integers and integer |
| 1245 | * vectors. They cannot be arrays or structures." |
| 1246 | * |
Ian Romanick | fb9f5b0 | 2010-03-29 17:16:35 -0700 | [diff] [blame] | 1247 | * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec: |
| 1248 | * |
| 1249 | * "The attribute qualifier can be used only with float, |
| 1250 | * floating-point vectors, and matrices. Attribute variables |
| 1251 | * cannot be declared as arrays or structures." |
| 1252 | */ |
| 1253 | const glsl_type *check_type = var->type->is_array() |
| 1254 | ? var->type->fields.array : var->type; |
| 1255 | |
| 1256 | switch (check_type->base_type) { |
| 1257 | case GLSL_TYPE_FLOAT: |
| 1258 | break; |
| 1259 | case GLSL_TYPE_UINT: |
| 1260 | case GLSL_TYPE_INT: |
| 1261 | if (state->language_version > 120) |
| 1262 | break; |
| 1263 | /* FALLTHROUGH */ |
| 1264 | default: |
| 1265 | _mesa_glsl_error(& loc, state, |
| 1266 | "vertex shader input / attribute cannot have " |
| 1267 | "type %s`%s'", |
| 1268 | var->type->is_array() ? "array of " : "", |
| 1269 | check_type->name); |
| 1270 | error_emitted = true; |
| 1271 | } |
| 1272 | |
Ian Romanick | 2d81620 | 2010-03-29 17:40:11 -0700 | [diff] [blame] | 1273 | if (!error_emitted && (state->language_version <= 130) |
Ian Romanick | fb9f5b0 | 2010-03-29 17:16:35 -0700 | [diff] [blame] | 1274 | && var->type->is_array()) { |
| 1275 | _mesa_glsl_error(& loc, state, |
| 1276 | "vertex shader input / attribute cannot have " |
| 1277 | "array type"); |
| 1278 | error_emitted = true; |
| 1279 | } |
| 1280 | } |
| 1281 | } |
| 1282 | |
Ian Romanick | 66faec4 | 2010-03-27 18:56:53 -0700 | [diff] [blame] | 1283 | if (decl->initializer != NULL) { |
Ian Romanick | 43de172 | 2010-03-28 17:03:16 -0700 | [diff] [blame] | 1284 | YYLTYPE initializer_loc = decl->initializer->get_location(); |
| 1285 | |
Ian Romanick | 66faec4 | 2010-03-27 18:56:53 -0700 | [diff] [blame] | 1286 | /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec: |
| 1287 | * |
| 1288 | * "All uniform variables are read-only and are initialized either |
| 1289 | * directly by an application via API commands, or indirectly by |
| 1290 | * OpenGL." |
| 1291 | */ |
| 1292 | if ((state->language_version <= 110) |
| 1293 | && (var->mode == ir_var_uniform)) { |
Ian Romanick | 43de172 | 2010-03-28 17:03:16 -0700 | [diff] [blame] | 1294 | _mesa_glsl_error(& initializer_loc, state, |
| 1295 | "cannot initialize uniforms in GLSL 1.10"); |
| 1296 | } |
Ian Romanick | 1936015 | 2010-03-26 18:05:27 -0700 | [diff] [blame] | 1297 | |
Ian Romanick | 43de172 | 2010-03-28 17:03:16 -0700 | [diff] [blame] | 1298 | if (var->type->is_sampler()) { |
| 1299 | _mesa_glsl_error(& initializer_loc, state, |
| 1300 | "cannot initialize samplers"); |
| 1301 | } |
| 1302 | |
| 1303 | if ((var->mode == ir_var_in) && (state->current_function == NULL)) { |
| 1304 | _mesa_glsl_error(& initializer_loc, state, |
| 1305 | "cannot initialize %s shader input / %s", |
| 1306 | (state->target == vertex_shader) |
| 1307 | ? "vertex" : "fragment", |
| 1308 | (state->target == vertex_shader) |
| 1309 | ? "attribute" : "varying"); |
Ian Romanick | 66faec4 | 2010-03-27 18:56:53 -0700 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | ir_dereference *const lhs = new ir_dereference(var); |
| 1313 | ir_rvalue *const rhs = decl->initializer->hir(instructions, state); |
| 1314 | |
| 1315 | /* FINISHME: If the declaration is either 'const' or 'uniform', the |
| 1316 | * FINISHME: initializer (rhs) must be a constant expression. |
| 1317 | */ |
| 1318 | |
| 1319 | if (!rhs->type->is_error()) { |
| 1320 | (void) do_assignment(instructions, state, lhs, rhs, |
| 1321 | this->get_location()); |
| 1322 | } |
Ian Romanick | 1936015 | 2010-03-26 18:05:27 -0700 | [diff] [blame] | 1323 | } |
Ian Romanick | 17d86f4 | 2010-03-29 12:59:02 -0700 | [diff] [blame] | 1324 | |
| 1325 | /* Add the vairable to the symbol table after processing the initializer. |
| 1326 | * This differs from most C-like languages, but it follows the GLSL |
| 1327 | * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50 |
| 1328 | * spec: |
| 1329 | * |
| 1330 | * "Within a declaration, the scope of a name starts immediately |
| 1331 | * after the initializer if present or immediately after the name |
| 1332 | * being declared if not." |
| 1333 | */ |
| 1334 | const bool added_variable = |
| 1335 | state->symbols->add_variable(decl->identifier, var); |
| 1336 | assert(added_variable); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1337 | } |
| 1338 | |
| 1339 | /* Variable declarations do not have r-values. |
| 1340 | */ |
| 1341 | return NULL; |
| 1342 | } |
| 1343 | |
| 1344 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 1345 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1346 | ast_parameter_declarator::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1347 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1348 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1349 | const struct glsl_type *type; |
| 1350 | const char *name = NULL; |
Eric Anholt | 2e063f1 | 2010-03-28 00:56:22 -0700 | [diff] [blame] | 1351 | YYLTYPE loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1352 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1353 | type = type_specifier_to_glsl_type(this->type->specifier, & name, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1354 | |
| 1355 | if (type == NULL) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1356 | if (name != NULL) { |
| 1357 | _mesa_glsl_error(& loc, state, |
| 1358 | "invalid type `%s' in declaration of `%s'", |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1359 | name, this->identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1360 | } else { |
| 1361 | _mesa_glsl_error(& loc, state, |
| 1362 | "invalid type in declaration of `%s'", |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1363 | this->identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1364 | } |
| 1365 | |
Ian Romanick | 0471e8b | 2010-03-26 14:33:41 -0700 | [diff] [blame] | 1366 | type = glsl_type::error_type; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1367 | } |
| 1368 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1369 | ir_variable *var = new ir_variable(type, this->identifier); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1370 | |
| 1371 | /* FINISHME: Handle array declarations. Note that this requires |
| 1372 | * FINISHME: complete handling of constant expressions. |
| 1373 | */ |
| 1374 | |
Ian Romanick | cdb8d54 | 2010-03-11 14:48:51 -0800 | [diff] [blame] | 1375 | /* Apply any specified qualifiers to the parameter declaration. Note that |
| 1376 | * for function parameters the default mode is 'in'. |
| 1377 | */ |
Eric Anholt | 2e063f1 | 2010-03-28 00:56:22 -0700 | [diff] [blame] | 1378 | apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc); |
Ian Romanick | cdb8d54 | 2010-03-11 14:48:51 -0800 | [diff] [blame] | 1379 | if (var->mode == ir_var_auto) |
| 1380 | var->mode = ir_var_in; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1381 | |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1382 | instructions->push_tail(var); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1383 | |
| 1384 | /* Parameter declarations do not have r-values. |
| 1385 | */ |
| 1386 | return NULL; |
| 1387 | } |
| 1388 | |
| 1389 | |
| 1390 | static void |
| 1391 | ast_function_parameters_to_hir(struct simple_node *ast_parameters, |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1392 | exec_list *ir_parameters, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1393 | struct _mesa_glsl_parse_state *state) |
| 1394 | { |
| 1395 | struct simple_node *ptr; |
| 1396 | |
| 1397 | foreach (ptr, ast_parameters) { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1398 | ((ast_node *)ptr)->hir(ir_parameters, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1399 | } |
| 1400 | } |
| 1401 | |
| 1402 | |
| 1403 | static bool |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1404 | parameter_lists_match(exec_list *list_a, exec_list *list_b) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1405 | { |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1406 | exec_list_iterator iter_a = list_a->iterator(); |
| 1407 | exec_list_iterator iter_b = list_b->iterator(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1408 | |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1409 | while (iter_a.has_next()) { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1410 | /* If all of the parameters from the other parameter list have been |
| 1411 | * exhausted, the lists have different length and, by definition, |
| 1412 | * do not match. |
| 1413 | */ |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1414 | if (!iter_b.has_next()) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1415 | return false; |
| 1416 | |
| 1417 | /* If the types of the parameters do not match, the parameters lists |
| 1418 | * are different. |
| 1419 | */ |
| 1420 | /* FINISHME */ |
| 1421 | |
| 1422 | |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1423 | iter_a.next(); |
| 1424 | iter_b.next(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | return true; |
| 1428 | } |
| 1429 | |
| 1430 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 1431 | ir_rvalue * |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1432 | ast_function_definition::hir(exec_list *instructions, |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1433 | struct _mesa_glsl_parse_state *state) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1434 | { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1435 | ir_label *label; |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1436 | ir_function_signature *signature = NULL; |
| 1437 | ir_function *f = NULL; |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1438 | exec_list parameters; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1439 | |
| 1440 | |
| 1441 | /* Convert the list of function parameters to HIR now so that they can be |
| 1442 | * used below to compare this function's signature with previously seen |
| 1443 | * signatures for functions with the same name. |
| 1444 | */ |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1445 | ast_function_parameters_to_hir(& this->prototype->parameters, & parameters, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1446 | state); |
| 1447 | |
Ian Romanick | e39cc69 | 2010-03-23 12:19:13 -0700 | [diff] [blame] | 1448 | const char *return_type_name; |
| 1449 | const glsl_type *return_type = |
| 1450 | type_specifier_to_glsl_type(this->prototype->return_type->specifier, |
| 1451 | & return_type_name, state); |
| 1452 | |
| 1453 | assert(return_type != NULL); |
| 1454 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1455 | /* Verify that this function's signature either doesn't match a previously |
| 1456 | * seen signature for a function with the same name, or, if a match is found, |
| 1457 | * that the previously seen signature does not have an associated definition. |
| 1458 | */ |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 1459 | const char *const name = this->prototype->identifier; |
| 1460 | f = state->symbols->get_function(name); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1461 | if (f != NULL) { |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1462 | foreach_iter(exec_list_iterator, iter, f->signatures) { |
| 1463 | signature = (struct ir_function_signature *) iter.get(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1464 | |
| 1465 | /* Compare the parameter list of the function being defined to the |
| 1466 | * existing function. If the parameter lists match, then the return |
| 1467 | * type must also match and the existing function must not have a |
| 1468 | * definition. |
| 1469 | */ |
| 1470 | if (parameter_lists_match(& parameters, & signature->parameters)) { |
| 1471 | /* FINISHME: Compare return types. */ |
| 1472 | |
| 1473 | if (signature->definition != NULL) { |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1474 | YYLTYPE loc = this->get_location(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1475 | |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 1476 | _mesa_glsl_error(& loc, state, "function `%s' redefined", name); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1477 | signature = NULL; |
| 1478 | break; |
| 1479 | } |
| 1480 | } |
| 1481 | |
| 1482 | signature = NULL; |
| 1483 | } |
| 1484 | |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 1485 | } else if (state->symbols->name_declared_this_scope(name)) { |
| 1486 | /* This function name shadows a non-function use of the same name. |
| 1487 | */ |
| 1488 | YYLTYPE loc = this->get_location(); |
| 1489 | |
| 1490 | _mesa_glsl_error(& loc, state, "function name `%s' conflicts with " |
| 1491 | "non-function", name); |
| 1492 | signature = NULL; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1493 | } else { |
Ian Romanick | 882dad7 | 2010-03-23 17:42:04 -0700 | [diff] [blame] | 1494 | f = new ir_function(name); |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 1495 | state->symbols->add_function(f->name, f); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1496 | } |
| 1497 | |
Eric Anholt | ab372da | 2010-03-28 01:24:55 -0700 | [diff] [blame] | 1498 | /* Verify the return type of main() */ |
| 1499 | if (strcmp(name, "main") == 0) { |
| 1500 | if (return_type != glsl_type::get_instance(GLSL_TYPE_VOID, 0, 0)) { |
| 1501 | YYLTYPE loc = this->get_location(); |
| 1502 | |
| 1503 | _mesa_glsl_error(& loc, state, "main() must return void"); |
| 1504 | } |
Eric Anholt | 174cc03 | 2010-03-30 23:37:51 -1000 | [diff] [blame] | 1505 | |
| 1506 | if (!parameters.is_empty()) { |
| 1507 | YYLTYPE loc = this->get_location(); |
| 1508 | |
| 1509 | _mesa_glsl_error(& loc, state, "main() must not take any parameters"); |
| 1510 | } |
Eric Anholt | ab372da | 2010-03-28 01:24:55 -0700 | [diff] [blame] | 1511 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1512 | |
| 1513 | /* Finish storing the information about this new function in its signature. |
| 1514 | */ |
| 1515 | if (signature == NULL) { |
Ian Romanick | e39cc69 | 2010-03-23 12:19:13 -0700 | [diff] [blame] | 1516 | signature = new ir_function_signature(return_type); |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1517 | f->signatures.push_tail(signature); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1518 | } else { |
| 1519 | /* Destroy all of the previous parameter information. The previous |
| 1520 | * parameter information comes from the function prototype, and it can |
| 1521 | * either include invalid parameter names or may not have names at all. |
| 1522 | */ |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1523 | foreach_iter(exec_list_iterator, iter, signature->parameters) { |
Kenneth Graunke | 44e1dfa | 2010-03-25 23:30:28 -0700 | [diff] [blame] | 1524 | assert(((ir_instruction *) iter.get())->as_variable() != NULL); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1525 | |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1526 | iter.remove(); |
| 1527 | delete iter.get(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1528 | } |
| 1529 | } |
| 1530 | |
| 1531 | |
Ian Romanick | 41ec6a4 | 2010-03-19 17:08:05 -0700 | [diff] [blame] | 1532 | assert(state->current_function == NULL); |
| 1533 | state->current_function = signature; |
| 1534 | |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1535 | ast_function_parameters_to_hir(& this->prototype->parameters, |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1536 | & signature->parameters, |
| 1537 | state); |
| 1538 | /* FINISHME: Set signature->return_type */ |
| 1539 | |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 1540 | label = new ir_label(name); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1541 | if (signature->definition == NULL) { |
| 1542 | signature->definition = label; |
| 1543 | } |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1544 | instructions->push_tail(label); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1545 | |
| 1546 | /* Add the function parameters to the symbol table. During this step the |
| 1547 | * parameter declarations are also moved from the temporary "parameters" list |
| 1548 | * to the instruction list. There are other more efficient ways to do this, |
| 1549 | * but they involve ugly linked-list gymnastics. |
| 1550 | */ |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 1551 | state->symbols->push_scope(); |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1552 | foreach_iter(exec_list_iterator, iter, parameters) { |
| 1553 | ir_variable *const var = (ir_variable *) iter.get(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1554 | |
Kenneth Graunke | 44e1dfa | 2010-03-25 23:30:28 -0700 | [diff] [blame] | 1555 | assert(((ir_instruction *) var)->as_variable() != NULL); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1556 | |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 1557 | iter.remove(); |
| 1558 | instructions->push_tail(var); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1559 | |
Ian Romanick | 3359e58 | 2010-03-19 15:38:52 -0700 | [diff] [blame] | 1560 | /* The only way a parameter would "exist" is if two parameters have |
| 1561 | * the same name. |
| 1562 | */ |
| 1563 | if (state->symbols->name_declared_this_scope(var->name)) { |
| 1564 | YYLTYPE loc = this->get_location(); |
| 1565 | |
| 1566 | _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name); |
| 1567 | } else { |
| 1568 | state->symbols->add_variable(var->name, var); |
| 1569 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | /* Convert the body of the function to HIR, and append the resulting |
| 1573 | * instructions to the list that currently consists of the function label |
| 1574 | * and the function parameters. |
| 1575 | */ |
Ian Romanick | 18238de | 2010-03-01 13:49:10 -0800 | [diff] [blame] | 1576 | this->body->hir(instructions, state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1577 | |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 1578 | state->symbols->pop_scope(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1579 | |
Ian Romanick | 41ec6a4 | 2010-03-19 17:08:05 -0700 | [diff] [blame] | 1580 | assert(state->current_function == signature); |
| 1581 | state->current_function = NULL; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1582 | |
| 1583 | /* Function definitions do not have r-values. |
| 1584 | */ |
| 1585 | return NULL; |
| 1586 | } |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 1587 | |
| 1588 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 1589 | ir_rvalue * |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 1590 | ast_jump_statement::hir(exec_list *instructions, |
| 1591 | struct _mesa_glsl_parse_state *state) |
| 1592 | { |
| 1593 | |
| 1594 | if (mode == ast_return) { |
| 1595 | ir_return *inst; |
Eric Anholt | aad7c77 | 2010-03-30 23:28:20 -1000 | [diff] [blame] | 1596 | assert(state->current_function); |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 1597 | |
| 1598 | if (opt_return_value) { |
Eric Anholt | ab79d4e | 2010-03-30 23:23:16 -1000 | [diff] [blame] | 1599 | if (state->current_function->return_type->base_type == |
| 1600 | GLSL_TYPE_VOID) { |
| 1601 | YYLTYPE loc = this->get_location(); |
| 1602 | |
| 1603 | _mesa_glsl_error(& loc, state, |
| 1604 | "`return` with a value, in function `%s' " |
| 1605 | "returning void", |
| 1606 | state->current_function->definition->label); |
| 1607 | } |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 1608 | |
| 1609 | ir_expression *const ret = (ir_expression *) |
| 1610 | opt_return_value->hir(instructions, state); |
| 1611 | assert(ret != NULL); |
| 1612 | |
| 1613 | /* FINISHME: Make sure the type of the return value matches the return |
| 1614 | * FINISHME: type of the enclosing function. |
| 1615 | */ |
| 1616 | |
| 1617 | inst = new ir_return(ret); |
| 1618 | } else { |
Eric Anholt | aad7c77 | 2010-03-30 23:28:20 -1000 | [diff] [blame] | 1619 | if (state->current_function->return_type->base_type != |
| 1620 | GLSL_TYPE_VOID) { |
| 1621 | YYLTYPE loc = this->get_location(); |
| 1622 | |
| 1623 | _mesa_glsl_error(& loc, state, |
| 1624 | "`return' with no value, in function %s returning " |
| 1625 | "non-void", |
| 1626 | state->current_function->definition->label); |
| 1627 | } |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 1628 | inst = new ir_return; |
| 1629 | } |
| 1630 | |
| 1631 | instructions->push_tail(inst); |
| 1632 | } |
| 1633 | |
Eric Anholt | b980207 | 2010-03-30 23:40:14 -1000 | [diff] [blame] | 1634 | if (mode == ast_discard) { |
| 1635 | /* FINISHME: discard support */ |
| 1636 | if (state->target != fragment_shader) { |
| 1637 | YYLTYPE loc = this->get_location(); |
| 1638 | |
| 1639 | _mesa_glsl_error(& loc, state, |
| 1640 | "`discard' may only appear in a fragment shader"); |
| 1641 | } |
| 1642 | } |
| 1643 | |
Ian Romanick | 16a246c | 2010-03-19 16:45:19 -0700 | [diff] [blame] | 1644 | /* Jump instructions do not have r-values. |
| 1645 | */ |
| 1646 | return NULL; |
| 1647 | } |
Ian Romanick | 3c6fea3 | 2010-03-29 14:11:25 -0700 | [diff] [blame] | 1648 | |
| 1649 | |
| 1650 | ir_rvalue * |
| 1651 | ast_selection_statement::hir(exec_list *instructions, |
| 1652 | struct _mesa_glsl_parse_state *state) |
| 1653 | { |
| 1654 | ir_rvalue *const condition = this->condition->hir(instructions, state); |
Ian Romanick | 3c6fea3 | 2010-03-29 14:11:25 -0700 | [diff] [blame] | 1655 | |
| 1656 | /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec: |
| 1657 | * |
| 1658 | * "Any expression whose type evaluates to a Boolean can be used as the |
| 1659 | * conditional expression bool-expression. Vector types are not accepted |
| 1660 | * as the expression to if." |
| 1661 | * |
| 1662 | * The checks are separated so that higher quality diagnostics can be |
| 1663 | * generated for cases where both rules are violated. |
| 1664 | */ |
| 1665 | if (!condition->type->is_boolean() || !condition->type->is_scalar()) { |
| 1666 | YYLTYPE loc = this->condition->get_location(); |
| 1667 | |
| 1668 | _mesa_glsl_error(& loc, state, "if-statement condition must be scalar " |
| 1669 | "boolean"); |
| 1670 | } |
| 1671 | |
| 1672 | ir_if *const stmt = new ir_if(condition); |
| 1673 | |
| 1674 | if (then_statement != NULL) { |
| 1675 | ast_node *node = (ast_node *) then_statement; |
| 1676 | do { |
| 1677 | node->hir(& stmt->then_instructions, state); |
| 1678 | node = (ast_node *) node->next; |
| 1679 | } while (node != then_statement); |
| 1680 | } |
| 1681 | |
| 1682 | if (else_statement != NULL) { |
| 1683 | ast_node *node = (ast_node *) else_statement; |
| 1684 | do { |
| 1685 | node->hir(& stmt->else_instructions, state); |
| 1686 | node = (ast_node *) node->next; |
| 1687 | } while (node != else_statement); |
| 1688 | } |
| 1689 | |
| 1690 | instructions->push_tail(stmt); |
| 1691 | |
| 1692 | /* if-statements do not have r-values. |
| 1693 | */ |
| 1694 | return NULL; |
| 1695 | } |