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