Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [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 ir_constant_expression.cpp |
| 26 | * Evaluate and process constant valued expressions |
| 27 | * |
| 28 | * In GLSL, constant valued expressions are used in several places. These |
| 29 | * must be processed and evaluated very early in the compilation process. |
| 30 | * |
| 31 | * * Sizes of arrays |
| 32 | * * Initializers for uniforms |
| 33 | * * Initializers for \c const variables |
| 34 | */ |
| 35 | |
| 36 | #define NULL 0 |
| 37 | #include "ir.h" |
| 38 | #include "ir_visitor.h" |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 39 | #include "glsl_types.h" |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Visitor class for evaluating constant expressions |
| 43 | */ |
| 44 | class ir_constant_visitor : public ir_visitor { |
| 45 | public: |
| 46 | ir_constant_visitor() |
| 47 | : value(NULL) |
| 48 | { |
| 49 | /* empty */ |
| 50 | } |
| 51 | |
| 52 | virtual ~ir_constant_visitor() |
| 53 | { |
| 54 | /* empty */ |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * \name Visit methods |
| 59 | * |
| 60 | * As typical for the visitor pattern, there must be one \c visit method for |
| 61 | * each concrete subclass of \c ir_instruction. Virtual base classes within |
| 62 | * the hierarchy should not have \c visit methods. |
| 63 | */ |
| 64 | /*@{*/ |
| 65 | virtual void visit(ir_variable *); |
| 66 | virtual void visit(ir_label *); |
| 67 | virtual void visit(ir_function_signature *); |
| 68 | virtual void visit(ir_function *); |
| 69 | virtual void visit(ir_expression *); |
| 70 | virtual void visit(ir_swizzle *); |
| 71 | virtual void visit(ir_dereference *); |
| 72 | virtual void visit(ir_assignment *); |
| 73 | virtual void visit(ir_constant *); |
| 74 | virtual void visit(ir_call *); |
| 75 | virtual void visit(ir_return *); |
| 76 | virtual void visit(ir_if *); |
| 77 | /*@}*/ |
| 78 | |
| 79 | /** |
| 80 | * Value of the constant expression. |
| 81 | * |
| 82 | * \note |
| 83 | * This field will be \c NULL if the expression is not constant valued. |
| 84 | */ |
| 85 | /* FINIHSME: This cannot hold values for constant arrays or structures. */ |
| 86 | ir_constant *value; |
| 87 | }; |
| 88 | |
| 89 | |
| 90 | ir_constant * |
| 91 | ir_instruction::constant_expression_value() |
| 92 | { |
| 93 | ir_constant_visitor visitor; |
| 94 | |
| 95 | this->accept(& visitor); |
| 96 | return visitor.value; |
| 97 | } |
| 98 | |
| 99 | |
| 100 | void |
| 101 | ir_constant_visitor::visit(ir_variable *ir) |
| 102 | { |
| 103 | (void) ir; |
| 104 | value = NULL; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | void |
| 109 | ir_constant_visitor::visit(ir_label *ir) |
| 110 | { |
| 111 | (void) ir; |
| 112 | value = NULL; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | void |
| 117 | ir_constant_visitor::visit(ir_function_signature *ir) |
| 118 | { |
| 119 | (void) ir; |
| 120 | value = NULL; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | void |
| 125 | ir_constant_visitor::visit(ir_function *ir) |
| 126 | { |
| 127 | (void) ir; |
| 128 | value = NULL; |
| 129 | } |
| 130 | |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 131 | void |
| 132 | ir_constant_visitor::visit(ir_expression *ir) |
| 133 | { |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 134 | value = NULL; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 135 | ir_constant *op[2]; |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 136 | unsigned int operand, c; |
| 137 | unsigned u[16]; |
| 138 | int i[16]; |
| 139 | float f[16]; |
| 140 | bool b[16]; |
| 141 | const glsl_type *type = NULL; |
Eric Anholt | 160d092 | 2010-04-01 18:07:08 -1000 | [diff] [blame] | 142 | |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 143 | for (operand = 0; operand < ir->get_num_operands(); operand++) { |
| 144 | op[operand] = ir->operands[operand]->constant_expression_value(); |
| 145 | if (!op[operand]) |
Eric Anholt | 160d092 | 2010-04-01 18:07:08 -1000 | [diff] [blame] | 146 | return; |
| 147 | } |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 148 | |
| 149 | switch (ir->operation) { |
Eric Anholt | 528bb85 | 2010-03-31 21:09:02 -1000 | [diff] [blame] | 150 | case ir_unop_logic_not: |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 151 | type = ir->operands[0]->type; |
| 152 | assert(type->base_type == GLSL_TYPE_BOOL); |
| 153 | for (c = 0; c < ir->operands[0]->type->components(); c++) |
| 154 | b[c] = !op[0]->value.b[c]; |
Eric Anholt | 528bb85 | 2010-03-31 21:09:02 -1000 | [diff] [blame] | 155 | break; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 156 | case ir_binop_add: |
| 157 | if (ir->operands[0]->type == ir->operands[1]->type) { |
| 158 | type = ir->operands[0]->type; |
| 159 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 160 | switch (ir->operands[0]->type->base_type) { |
| 161 | case GLSL_TYPE_UINT: |
| 162 | u[c] = op[0]->value.u[c] + op[1]->value.u[c]; |
| 163 | break; |
| 164 | case GLSL_TYPE_INT: |
| 165 | i[c] = op[0]->value.i[c] + op[1]->value.i[c]; |
| 166 | break; |
| 167 | case GLSL_TYPE_FLOAT: |
| 168 | f[c] = op[0]->value.f[c] + op[1]->value.f[c]; |
| 169 | break; |
| 170 | default: |
| 171 | assert(0); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | break; |
| 176 | case ir_binop_sub: |
| 177 | if (ir->operands[0]->type == ir->operands[1]->type) { |
| 178 | type = ir->operands[0]->type; |
| 179 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 180 | switch (ir->operands[0]->type->base_type) { |
| 181 | case GLSL_TYPE_UINT: |
| 182 | u[c] = op[0]->value.u[c] - op[1]->value.u[c]; |
| 183 | break; |
| 184 | case GLSL_TYPE_INT: |
| 185 | i[c] = op[0]->value.i[c] - op[1]->value.i[c]; |
| 186 | break; |
| 187 | case GLSL_TYPE_FLOAT: |
| 188 | f[c] = op[0]->value.f[c] - op[1]->value.f[c]; |
| 189 | break; |
| 190 | default: |
| 191 | assert(0); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | break; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 196 | case ir_binop_mul: |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 197 | if (ir->operands[0]->type == ir->operands[1]->type && |
| 198 | !ir->operands[0]->type->is_matrix()) { |
| 199 | type = ir->operands[0]->type; |
| 200 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 201 | switch (ir->operands[0]->type->base_type) { |
| 202 | case GLSL_TYPE_UINT: |
| 203 | u[c] = op[0]->value.u[c] * op[1]->value.u[c]; |
| 204 | break; |
| 205 | case GLSL_TYPE_INT: |
| 206 | i[c] = op[0]->value.i[c] * op[1]->value.i[c]; |
| 207 | break; |
| 208 | case GLSL_TYPE_FLOAT: |
| 209 | f[c] = op[0]->value.f[c] * op[1]->value.f[c]; |
| 210 | break; |
| 211 | default: |
| 212 | assert(0); |
| 213 | } |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | break; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 217 | case ir_binop_div: |
| 218 | if (ir->operands[0]->type == ir->operands[1]->type) { |
| 219 | type = ir->operands[0]->type; |
| 220 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 221 | switch (ir->operands[0]->type->base_type) { |
| 222 | case GLSL_TYPE_UINT: |
| 223 | u[c] = op[0]->value.u[c] / op[1]->value.u[c]; |
| 224 | break; |
| 225 | case GLSL_TYPE_INT: |
| 226 | i[c] = op[0]->value.i[c] / op[1]->value.i[c]; |
| 227 | break; |
| 228 | case GLSL_TYPE_FLOAT: |
| 229 | f[c] = op[0]->value.f[c] / op[1]->value.f[c]; |
| 230 | break; |
| 231 | default: |
| 232 | assert(0); |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | break; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 237 | case ir_binop_logic_and: |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 238 | type = ir->operands[0]->type; |
| 239 | assert(type->base_type == GLSL_TYPE_BOOL); |
| 240 | for (c = 0; c < ir->operands[0]->type->components(); c++) |
| 241 | b[c] = op[0]->value.b[c] && op[1]->value.b[c]; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 242 | break; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 243 | case ir_binop_logic_xor: |
| 244 | type = ir->operands[0]->type; |
| 245 | assert(type->base_type == GLSL_TYPE_BOOL); |
| 246 | for (c = 0; c < ir->operands[0]->type->components(); c++) |
| 247 | b[c] = op[0]->value.b[c] ^ op[1]->value.b[c]; |
| 248 | break; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 249 | case ir_binop_logic_or: |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 250 | type = ir->operands[0]->type; |
| 251 | assert(type->base_type == GLSL_TYPE_BOOL); |
| 252 | for (c = 0; c < ir->operands[0]->type->components(); c++) |
| 253 | b[c] = op[0]->value.b[c] || op[1]->value.b[c]; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 254 | break; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 255 | |
| 256 | case ir_binop_less: |
| 257 | type = glsl_type::bool_type; |
| 258 | switch (ir->operands[0]->type->base_type) { |
| 259 | case GLSL_TYPE_UINT: |
| 260 | b[0] = op[0]->value.u[0] < op[1]->value.u[0]; |
| 261 | break; |
| 262 | case GLSL_TYPE_INT: |
| 263 | b[0] = op[0]->value.i[0] < op[1]->value.i[0]; |
| 264 | break; |
| 265 | case GLSL_TYPE_FLOAT: |
| 266 | b[0] = op[0]->value.f[0] < op[1]->value.f[0]; |
| 267 | break; |
| 268 | default: |
| 269 | assert(0); |
| 270 | } |
| 271 | break; |
| 272 | case ir_binop_greater: |
| 273 | type = glsl_type::bool_type; |
| 274 | switch (ir->operands[0]->type->base_type) { |
| 275 | case GLSL_TYPE_UINT: |
| 276 | b[0] = op[0]->value.u[0] > op[1]->value.u[0]; |
| 277 | break; |
| 278 | case GLSL_TYPE_INT: |
| 279 | b[0] = op[0]->value.i[0] > op[1]->value.i[0]; |
| 280 | break; |
| 281 | case GLSL_TYPE_FLOAT: |
| 282 | b[0] = op[0]->value.f[0] > op[1]->value.f[0]; |
| 283 | break; |
| 284 | default: |
| 285 | assert(0); |
| 286 | } |
| 287 | break; |
| 288 | case ir_binop_lequal: |
| 289 | type = glsl_type::bool_type; |
| 290 | switch (ir->operands[0]->type->base_type) { |
| 291 | case GLSL_TYPE_UINT: |
| 292 | b[0] = op[0]->value.u[0] <= op[1]->value.u[0]; |
| 293 | break; |
| 294 | case GLSL_TYPE_INT: |
| 295 | b[0] = op[0]->value.i[0] <= op[1]->value.i[0]; |
| 296 | break; |
| 297 | case GLSL_TYPE_FLOAT: |
| 298 | b[0] = op[0]->value.f[0] <= op[1]->value.f[0]; |
| 299 | break; |
| 300 | default: |
| 301 | assert(0); |
| 302 | } |
| 303 | break; |
| 304 | case ir_binop_gequal: |
| 305 | type = glsl_type::bool_type; |
| 306 | switch (ir->operands[0]->type->base_type) { |
| 307 | case GLSL_TYPE_UINT: |
| 308 | b[0] = op[0]->value.u[0] >= op[1]->value.u[0]; |
| 309 | break; |
| 310 | case GLSL_TYPE_INT: |
| 311 | b[0] = op[0]->value.i[0] >= op[1]->value.i[0]; |
| 312 | break; |
| 313 | case GLSL_TYPE_FLOAT: |
| 314 | b[0] = op[0]->value.f[0] >= op[1]->value.f[0]; |
| 315 | break; |
| 316 | default: |
| 317 | assert(0); |
| 318 | } |
| 319 | break; |
| 320 | |
Eric Anholt | ec1949e | 2010-04-06 10:02:27 -0700 | [diff] [blame] | 321 | case ir_binop_equal: |
| 322 | if (ir->operands[0]->type == ir->operands[1]->type) { |
| 323 | type = glsl_type::bool_type; |
| 324 | b[0] = true; |
| 325 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 326 | switch (ir->operands[0]->type->base_type) { |
| 327 | case GLSL_TYPE_UINT: |
| 328 | b[0] = b[0] && op[0]->value.u[c] == op[1]->value.u[c]; |
| 329 | break; |
| 330 | case GLSL_TYPE_INT: |
| 331 | b[0] = b[0] && op[0]->value.i[c] == op[1]->value.i[c]; |
| 332 | break; |
| 333 | case GLSL_TYPE_FLOAT: |
| 334 | b[0] = b[0] && op[0]->value.f[c] == op[1]->value.f[c]; |
| 335 | break; |
| 336 | default: |
| 337 | assert(0); |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | break; |
| 342 | case ir_binop_nequal: |
| 343 | if (ir->operands[0]->type == ir->operands[1]->type) { |
| 344 | type = glsl_type::bool_type; |
| 345 | b[0] = false; |
| 346 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 347 | switch (ir->operands[0]->type->base_type) { |
| 348 | case GLSL_TYPE_UINT: |
| 349 | b[0] = b[0] || op[0]->value.u[c] != op[1]->value.u[c]; |
| 350 | break; |
| 351 | case GLSL_TYPE_INT: |
| 352 | b[0] = b[0] || op[0]->value.i[c] != op[1]->value.i[c]; |
| 353 | break; |
| 354 | case GLSL_TYPE_FLOAT: |
| 355 | b[0] = b[0] || op[0]->value.f[c] != op[1]->value.f[c]; |
| 356 | break; |
| 357 | default: |
| 358 | assert(0); |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | break; |
| 363 | |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 364 | default: |
| 365 | break; |
| 366 | } |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 367 | |
| 368 | if (type) { |
| 369 | switch (type->base_type) { |
| 370 | case GLSL_TYPE_UINT: |
| 371 | value = new ir_constant(type, u); |
| 372 | break; |
| 373 | case GLSL_TYPE_INT: |
| 374 | value = new ir_constant(type, i); |
| 375 | break; |
| 376 | case GLSL_TYPE_FLOAT: |
| 377 | value = new ir_constant(type, f); |
| 378 | break; |
| 379 | case GLSL_TYPE_BOOL: |
| 380 | value = new ir_constant(type, b); |
| 381 | break; |
| 382 | } |
| 383 | } |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | |
| 387 | void |
| 388 | ir_constant_visitor::visit(ir_swizzle *ir) |
| 389 | { |
| 390 | (void) ir; |
| 391 | value = NULL; |
| 392 | } |
| 393 | |
| 394 | |
| 395 | void |
| 396 | ir_constant_visitor::visit(ir_dereference *ir) |
| 397 | { |
| 398 | (void) ir; |
| 399 | value = NULL; |
| 400 | } |
| 401 | |
| 402 | |
| 403 | void |
| 404 | ir_constant_visitor::visit(ir_assignment *ir) |
| 405 | { |
| 406 | (void) ir; |
| 407 | value = NULL; |
| 408 | } |
| 409 | |
| 410 | |
| 411 | void |
| 412 | ir_constant_visitor::visit(ir_constant *ir) |
| 413 | { |
| 414 | value = ir; |
| 415 | } |
| 416 | |
| 417 | |
| 418 | void |
| 419 | ir_constant_visitor::visit(ir_call *ir) |
| 420 | { |
| 421 | (void) ir; |
| 422 | value = NULL; |
| 423 | } |
| 424 | |
| 425 | |
| 426 | void |
| 427 | ir_constant_visitor::visit(ir_return *ir) |
| 428 | { |
| 429 | (void) ir; |
| 430 | value = NULL; |
| 431 | } |
| 432 | |
| 433 | |
| 434 | void |
| 435 | ir_constant_visitor::visit(ir_if *ir) |
| 436 | { |
| 437 | (void) ir; |
| 438 | value = NULL; |
| 439 | } |