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 |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 37 | #include <math.h> |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 38 | #include "ir.h" |
| 39 | #include "ir_visitor.h" |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 40 | #include "glsl_types.h" |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Visitor class for evaluating constant expressions |
| 44 | */ |
| 45 | class ir_constant_visitor : public ir_visitor { |
| 46 | public: |
| 47 | ir_constant_visitor() |
| 48 | : value(NULL) |
| 49 | { |
| 50 | /* empty */ |
| 51 | } |
| 52 | |
| 53 | virtual ~ir_constant_visitor() |
| 54 | { |
| 55 | /* empty */ |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * \name Visit methods |
| 60 | * |
| 61 | * As typical for the visitor pattern, there must be one \c visit method for |
| 62 | * each concrete subclass of \c ir_instruction. Virtual base classes within |
| 63 | * the hierarchy should not have \c visit methods. |
| 64 | */ |
| 65 | /*@{*/ |
| 66 | virtual void visit(ir_variable *); |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 67 | virtual void visit(ir_function_signature *); |
| 68 | virtual void visit(ir_function *); |
| 69 | virtual void visit(ir_expression *); |
Kenneth Graunke | 26d74cd | 2010-05-26 17:42:03 -0700 | [diff] [blame^] | 70 | virtual void visit(ir_texture *); |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 71 | virtual void visit(ir_swizzle *); |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 72 | virtual void visit(ir_dereference_variable *); |
| 73 | virtual void visit(ir_dereference_array *); |
| 74 | virtual void visit(ir_dereference_record *); |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 75 | virtual void visit(ir_assignment *); |
| 76 | virtual void visit(ir_constant *); |
| 77 | virtual void visit(ir_call *); |
| 78 | virtual void visit(ir_return *); |
| 79 | virtual void visit(ir_if *); |
Ian Romanick | fad607a | 2010-04-05 16:16:07 -0700 | [diff] [blame] | 80 | virtual void visit(ir_loop *); |
Ian Romanick | f8e31e0 | 2010-04-05 16:28:15 -0700 | [diff] [blame] | 81 | virtual void visit(ir_loop_jump *); |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 82 | /*@}*/ |
| 83 | |
| 84 | /** |
| 85 | * Value of the constant expression. |
| 86 | * |
| 87 | * \note |
| 88 | * This field will be \c NULL if the expression is not constant valued. |
| 89 | */ |
| 90 | /* FINIHSME: This cannot hold values for constant arrays or structures. */ |
| 91 | ir_constant *value; |
| 92 | }; |
| 93 | |
| 94 | |
| 95 | ir_constant * |
| 96 | ir_instruction::constant_expression_value() |
| 97 | { |
| 98 | ir_constant_visitor visitor; |
| 99 | |
| 100 | this->accept(& visitor); |
| 101 | return visitor.value; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | void |
| 106 | ir_constant_visitor::visit(ir_variable *ir) |
| 107 | { |
| 108 | (void) ir; |
| 109 | value = NULL; |
| 110 | } |
| 111 | |
| 112 | |
| 113 | void |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 114 | ir_constant_visitor::visit(ir_function_signature *ir) |
| 115 | { |
| 116 | (void) ir; |
| 117 | value = NULL; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | void |
| 122 | ir_constant_visitor::visit(ir_function *ir) |
| 123 | { |
| 124 | (void) ir; |
| 125 | value = NULL; |
| 126 | } |
| 127 | |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 128 | void |
| 129 | ir_constant_visitor::visit(ir_expression *ir) |
| 130 | { |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 131 | value = NULL; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 132 | ir_constant *op[2]; |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 133 | unsigned int operand, c; |
| 134 | unsigned u[16]; |
| 135 | int i[16]; |
| 136 | float f[16]; |
| 137 | bool b[16]; |
| 138 | const glsl_type *type = NULL; |
Eric Anholt | 160d092 | 2010-04-01 18:07:08 -1000 | [diff] [blame] | 139 | |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 140 | for (operand = 0; operand < ir->get_num_operands(); operand++) { |
| 141 | op[operand] = ir->operands[operand]->constant_expression_value(); |
| 142 | if (!op[operand]) |
Eric Anholt | 160d092 | 2010-04-01 18:07:08 -1000 | [diff] [blame] | 143 | return; |
| 144 | } |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 145 | |
| 146 | switch (ir->operation) { |
Eric Anholt | 528bb85 | 2010-03-31 21:09:02 -1000 | [diff] [blame] | 147 | case ir_unop_logic_not: |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 148 | type = ir->operands[0]->type; |
| 149 | assert(type->base_type == GLSL_TYPE_BOOL); |
| 150 | for (c = 0; c < ir->operands[0]->type->components(); c++) |
| 151 | b[c] = !op[0]->value.b[c]; |
Eric Anholt | 528bb85 | 2010-03-31 21:09:02 -1000 | [diff] [blame] | 152 | break; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 153 | |
| 154 | case ir_unop_f2i: |
| 155 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 156 | type = ir->type; |
| 157 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 158 | i[c] = op[0]->value.f[c]; |
| 159 | } |
| 160 | break; |
| 161 | case ir_unop_i2f: |
| 162 | assert(op[0]->type->base_type == GLSL_TYPE_UINT || |
| 163 | op[0]->type->base_type == GLSL_TYPE_INT); |
| 164 | type = ir->type; |
| 165 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 166 | if (op[0]->type->base_type == GLSL_TYPE_INT) |
| 167 | f[c] = op[0]->value.i[c]; |
| 168 | else |
| 169 | f[c] = op[0]->value.u[c]; |
| 170 | } |
| 171 | break; |
| 172 | |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 173 | case ir_unop_neg: |
| 174 | type = ir->type; |
| 175 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 176 | switch (type->base_type) { |
| 177 | case GLSL_TYPE_UINT: |
| 178 | u[c] = -op[0]->value.u[c]; |
| 179 | break; |
| 180 | case GLSL_TYPE_INT: |
| 181 | i[c] = -op[0]->value.i[c]; |
| 182 | break; |
| 183 | case GLSL_TYPE_FLOAT: |
| 184 | f[c] = -op[0]->value.f[c]; |
| 185 | break; |
| 186 | default: |
| 187 | assert(0); |
| 188 | } |
| 189 | } |
| 190 | break; |
| 191 | |
| 192 | case ir_unop_abs: |
| 193 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 194 | type = ir->type; |
| 195 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 196 | switch (type->base_type) { |
| 197 | case GLSL_TYPE_UINT: |
| 198 | u[c] = op[0]->value.u[c]; |
| 199 | break; |
| 200 | case GLSL_TYPE_INT: |
| 201 | i[c] = op[0]->value.i[c]; |
| 202 | if (i[c] < 0) |
| 203 | i[c] = -i[c]; |
| 204 | break; |
| 205 | case GLSL_TYPE_FLOAT: |
| 206 | f[c] = fabs(op[0]->value.f[c]); |
| 207 | break; |
| 208 | default: |
| 209 | assert(0); |
| 210 | } |
| 211 | } |
| 212 | break; |
| 213 | |
| 214 | case ir_unop_rcp: |
| 215 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 216 | type = ir->type; |
| 217 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 218 | switch (type->base_type) { |
| 219 | case GLSL_TYPE_UINT: |
| 220 | if (op[0]->value.u[c] != 0.0) |
| 221 | u[c] = 1 / op[0]->value.u[c]; |
| 222 | break; |
| 223 | case GLSL_TYPE_INT: |
| 224 | if (op[0]->value.i[c] != 0.0) |
| 225 | i[c] = 1 / op[0]->value.i[c]; |
| 226 | break; |
| 227 | case GLSL_TYPE_FLOAT: |
| 228 | if (op[0]->value.f[c] != 0.0) |
| 229 | f[c] = 1.0 / op[0]->value.f[c]; |
| 230 | break; |
| 231 | default: |
| 232 | assert(0); |
| 233 | } |
| 234 | } |
| 235 | break; |
| 236 | |
| 237 | case ir_unop_rsq: |
| 238 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 239 | type = ir->type; |
| 240 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 241 | f[c] = 1.0 / sqrtf(op[0]->value.f[c]); |
| 242 | } |
| 243 | break; |
| 244 | |
| 245 | case ir_unop_sqrt: |
| 246 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 247 | type = ir->type; |
| 248 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 249 | f[c] = sqrtf(op[0]->value.f[c]); |
| 250 | } |
| 251 | break; |
| 252 | |
| 253 | case ir_unop_exp: |
| 254 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 255 | type = ir->type; |
| 256 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 257 | f[c] = expf(op[0]->value.f[c]); |
| 258 | } |
| 259 | break; |
| 260 | |
| 261 | case ir_unop_log: |
| 262 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 263 | type = ir->type; |
| 264 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 265 | f[c] = logf(op[0]->value.f[c]); |
| 266 | } |
| 267 | break; |
| 268 | |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 269 | case ir_binop_add: |
| 270 | if (ir->operands[0]->type == ir->operands[1]->type) { |
| 271 | type = ir->operands[0]->type; |
| 272 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 273 | switch (ir->operands[0]->type->base_type) { |
| 274 | case GLSL_TYPE_UINT: |
| 275 | u[c] = op[0]->value.u[c] + op[1]->value.u[c]; |
| 276 | break; |
| 277 | case GLSL_TYPE_INT: |
| 278 | i[c] = op[0]->value.i[c] + op[1]->value.i[c]; |
| 279 | break; |
| 280 | case GLSL_TYPE_FLOAT: |
| 281 | f[c] = op[0]->value.f[c] + op[1]->value.f[c]; |
| 282 | break; |
| 283 | default: |
| 284 | assert(0); |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | break; |
| 289 | case ir_binop_sub: |
| 290 | if (ir->operands[0]->type == ir->operands[1]->type) { |
| 291 | type = ir->operands[0]->type; |
| 292 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 293 | switch (ir->operands[0]->type->base_type) { |
| 294 | case GLSL_TYPE_UINT: |
| 295 | u[c] = op[0]->value.u[c] - op[1]->value.u[c]; |
| 296 | break; |
| 297 | case GLSL_TYPE_INT: |
| 298 | i[c] = op[0]->value.i[c] - op[1]->value.i[c]; |
| 299 | break; |
| 300 | case GLSL_TYPE_FLOAT: |
| 301 | f[c] = op[0]->value.f[c] - op[1]->value.f[c]; |
| 302 | break; |
| 303 | default: |
| 304 | assert(0); |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | break; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 309 | case ir_binop_mul: |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 310 | if (ir->operands[0]->type == ir->operands[1]->type && |
| 311 | !ir->operands[0]->type->is_matrix()) { |
| 312 | type = ir->operands[0]->type; |
| 313 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 314 | switch (ir->operands[0]->type->base_type) { |
| 315 | case GLSL_TYPE_UINT: |
| 316 | u[c] = op[0]->value.u[c] * op[1]->value.u[c]; |
| 317 | break; |
| 318 | case GLSL_TYPE_INT: |
| 319 | i[c] = op[0]->value.i[c] * op[1]->value.i[c]; |
| 320 | break; |
| 321 | case GLSL_TYPE_FLOAT: |
| 322 | f[c] = op[0]->value.f[c] * op[1]->value.f[c]; |
| 323 | break; |
| 324 | default: |
| 325 | assert(0); |
| 326 | } |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | break; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 330 | case ir_binop_div: |
| 331 | if (ir->operands[0]->type == ir->operands[1]->type) { |
| 332 | type = ir->operands[0]->type; |
| 333 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 334 | switch (ir->operands[0]->type->base_type) { |
| 335 | case GLSL_TYPE_UINT: |
| 336 | u[c] = op[0]->value.u[c] / op[1]->value.u[c]; |
| 337 | break; |
| 338 | case GLSL_TYPE_INT: |
| 339 | i[c] = op[0]->value.i[c] / op[1]->value.i[c]; |
| 340 | break; |
| 341 | case GLSL_TYPE_FLOAT: |
| 342 | f[c] = op[0]->value.f[c] / op[1]->value.f[c]; |
| 343 | break; |
| 344 | default: |
| 345 | assert(0); |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | break; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 350 | case ir_binop_logic_and: |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 351 | type = ir->operands[0]->type; |
| 352 | assert(type->base_type == GLSL_TYPE_BOOL); |
| 353 | for (c = 0; c < ir->operands[0]->type->components(); c++) |
| 354 | b[c] = op[0]->value.b[c] && op[1]->value.b[c]; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 355 | break; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 356 | case ir_binop_logic_xor: |
| 357 | type = ir->operands[0]->type; |
| 358 | assert(type->base_type == GLSL_TYPE_BOOL); |
| 359 | for (c = 0; c < ir->operands[0]->type->components(); c++) |
| 360 | b[c] = op[0]->value.b[c] ^ op[1]->value.b[c]; |
| 361 | break; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 362 | case ir_binop_logic_or: |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 363 | type = ir->operands[0]->type; |
| 364 | assert(type->base_type == GLSL_TYPE_BOOL); |
| 365 | for (c = 0; c < ir->operands[0]->type->components(); c++) |
| 366 | b[c] = op[0]->value.b[c] || op[1]->value.b[c]; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 367 | break; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 368 | |
| 369 | case ir_binop_less: |
| 370 | type = glsl_type::bool_type; |
| 371 | switch (ir->operands[0]->type->base_type) { |
| 372 | case GLSL_TYPE_UINT: |
| 373 | b[0] = op[0]->value.u[0] < op[1]->value.u[0]; |
| 374 | break; |
| 375 | case GLSL_TYPE_INT: |
| 376 | b[0] = op[0]->value.i[0] < op[1]->value.i[0]; |
| 377 | break; |
| 378 | case GLSL_TYPE_FLOAT: |
| 379 | b[0] = op[0]->value.f[0] < op[1]->value.f[0]; |
| 380 | break; |
| 381 | default: |
| 382 | assert(0); |
| 383 | } |
| 384 | break; |
| 385 | case ir_binop_greater: |
| 386 | type = glsl_type::bool_type; |
| 387 | switch (ir->operands[0]->type->base_type) { |
| 388 | case GLSL_TYPE_UINT: |
| 389 | b[0] = op[0]->value.u[0] > op[1]->value.u[0]; |
| 390 | break; |
| 391 | case GLSL_TYPE_INT: |
| 392 | b[0] = op[0]->value.i[0] > op[1]->value.i[0]; |
| 393 | break; |
| 394 | case GLSL_TYPE_FLOAT: |
| 395 | b[0] = op[0]->value.f[0] > op[1]->value.f[0]; |
| 396 | break; |
| 397 | default: |
| 398 | assert(0); |
| 399 | } |
| 400 | break; |
| 401 | case ir_binop_lequal: |
| 402 | type = glsl_type::bool_type; |
| 403 | switch (ir->operands[0]->type->base_type) { |
| 404 | case GLSL_TYPE_UINT: |
| 405 | b[0] = op[0]->value.u[0] <= op[1]->value.u[0]; |
| 406 | break; |
| 407 | case GLSL_TYPE_INT: |
| 408 | b[0] = op[0]->value.i[0] <= op[1]->value.i[0]; |
| 409 | break; |
| 410 | case GLSL_TYPE_FLOAT: |
| 411 | b[0] = op[0]->value.f[0] <= op[1]->value.f[0]; |
| 412 | break; |
| 413 | default: |
| 414 | assert(0); |
| 415 | } |
| 416 | break; |
| 417 | case ir_binop_gequal: |
| 418 | type = glsl_type::bool_type; |
| 419 | switch (ir->operands[0]->type->base_type) { |
| 420 | case GLSL_TYPE_UINT: |
| 421 | b[0] = op[0]->value.u[0] >= op[1]->value.u[0]; |
| 422 | break; |
| 423 | case GLSL_TYPE_INT: |
| 424 | b[0] = op[0]->value.i[0] >= op[1]->value.i[0]; |
| 425 | break; |
| 426 | case GLSL_TYPE_FLOAT: |
| 427 | b[0] = op[0]->value.f[0] >= op[1]->value.f[0]; |
| 428 | break; |
| 429 | default: |
| 430 | assert(0); |
| 431 | } |
| 432 | break; |
| 433 | |
Eric Anholt | ec1949e | 2010-04-06 10:02:27 -0700 | [diff] [blame] | 434 | case ir_binop_equal: |
| 435 | if (ir->operands[0]->type == ir->operands[1]->type) { |
| 436 | type = glsl_type::bool_type; |
| 437 | b[0] = true; |
| 438 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 439 | switch (ir->operands[0]->type->base_type) { |
| 440 | case GLSL_TYPE_UINT: |
| 441 | b[0] = b[0] && op[0]->value.u[c] == op[1]->value.u[c]; |
| 442 | break; |
| 443 | case GLSL_TYPE_INT: |
| 444 | b[0] = b[0] && op[0]->value.i[c] == op[1]->value.i[c]; |
| 445 | break; |
| 446 | case GLSL_TYPE_FLOAT: |
| 447 | b[0] = b[0] && op[0]->value.f[c] == op[1]->value.f[c]; |
| 448 | break; |
Ian Romanick | 77cce64 | 2010-04-07 16:48:42 -0700 | [diff] [blame] | 449 | case GLSL_TYPE_BOOL: |
| 450 | b[0] = b[0] && op[0]->value.b[c] == op[1]->value.b[c]; |
| 451 | break; |
Eric Anholt | ec1949e | 2010-04-06 10:02:27 -0700 | [diff] [blame] | 452 | default: |
| 453 | assert(0); |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | break; |
| 458 | case ir_binop_nequal: |
| 459 | if (ir->operands[0]->type == ir->operands[1]->type) { |
| 460 | type = glsl_type::bool_type; |
| 461 | b[0] = false; |
| 462 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 463 | switch (ir->operands[0]->type->base_type) { |
| 464 | case GLSL_TYPE_UINT: |
| 465 | b[0] = b[0] || op[0]->value.u[c] != op[1]->value.u[c]; |
| 466 | break; |
| 467 | case GLSL_TYPE_INT: |
| 468 | b[0] = b[0] || op[0]->value.i[c] != op[1]->value.i[c]; |
| 469 | break; |
| 470 | case GLSL_TYPE_FLOAT: |
| 471 | b[0] = b[0] || op[0]->value.f[c] != op[1]->value.f[c]; |
| 472 | break; |
Ian Romanick | 77cce64 | 2010-04-07 16:48:42 -0700 | [diff] [blame] | 473 | case GLSL_TYPE_BOOL: |
| 474 | b[0] = b[0] || op[0]->value.b[c] != op[1]->value.b[c]; |
| 475 | break; |
Eric Anholt | ec1949e | 2010-04-06 10:02:27 -0700 | [diff] [blame] | 476 | default: |
| 477 | assert(0); |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | break; |
| 482 | |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 483 | default: |
| 484 | break; |
| 485 | } |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 486 | |
| 487 | if (type) { |
| 488 | switch (type->base_type) { |
| 489 | case GLSL_TYPE_UINT: |
| 490 | value = new ir_constant(type, u); |
| 491 | break; |
| 492 | case GLSL_TYPE_INT: |
| 493 | value = new ir_constant(type, i); |
| 494 | break; |
| 495 | case GLSL_TYPE_FLOAT: |
| 496 | value = new ir_constant(type, f); |
| 497 | break; |
| 498 | case GLSL_TYPE_BOOL: |
| 499 | value = new ir_constant(type, b); |
| 500 | break; |
| 501 | } |
| 502 | } |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | |
| 506 | void |
Kenneth Graunke | 26d74cd | 2010-05-26 17:42:03 -0700 | [diff] [blame^] | 507 | ir_constant_visitor::visit(ir_texture *ir) |
| 508 | { |
| 509 | // FINISHME: Do stuff with texture lookups |
| 510 | (void) ir; |
| 511 | value = NULL; |
| 512 | } |
| 513 | |
| 514 | |
| 515 | void |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 516 | ir_constant_visitor::visit(ir_swizzle *ir) |
| 517 | { |
| 518 | (void) ir; |
| 519 | value = NULL; |
| 520 | } |
| 521 | |
| 522 | |
| 523 | void |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 524 | ir_constant_visitor::visit(ir_dereference_variable *ir) |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 525 | { |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 526 | value = NULL; |
Eric Anholt | 326c676 | 2010-04-06 10:30:54 -0700 | [diff] [blame] | 527 | |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 528 | ir_variable *var = ir->variable_referenced(); |
| 529 | if (var && var->constant_value) |
| 530 | value = new ir_constant(ir->type, &var->constant_value->value); |
| 531 | } |
| 532 | |
| 533 | |
| 534 | void |
| 535 | ir_constant_visitor::visit(ir_dereference_array *ir) |
| 536 | { |
Ian Romanick | 36ea286 | 2010-05-19 13:52:29 +0200 | [diff] [blame] | 537 | (void) ir; |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 538 | value = NULL; |
| 539 | /* FINISHME: Other dereference modes. */ |
| 540 | } |
| 541 | |
| 542 | |
| 543 | void |
| 544 | ir_constant_visitor::visit(ir_dereference_record *ir) |
| 545 | { |
Ian Romanick | 36ea286 | 2010-05-19 13:52:29 +0200 | [diff] [blame] | 546 | (void) ir; |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 547 | value = NULL; |
Eric Anholt | 326c676 | 2010-04-06 10:30:54 -0700 | [diff] [blame] | 548 | /* FINISHME: Other dereference modes. */ |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | |
| 552 | void |
| 553 | ir_constant_visitor::visit(ir_assignment *ir) |
| 554 | { |
| 555 | (void) ir; |
| 556 | value = NULL; |
| 557 | } |
| 558 | |
| 559 | |
| 560 | void |
| 561 | ir_constant_visitor::visit(ir_constant *ir) |
| 562 | { |
| 563 | value = ir; |
| 564 | } |
| 565 | |
| 566 | |
| 567 | void |
| 568 | ir_constant_visitor::visit(ir_call *ir) |
| 569 | { |
| 570 | (void) ir; |
| 571 | value = NULL; |
| 572 | } |
| 573 | |
| 574 | |
| 575 | void |
| 576 | ir_constant_visitor::visit(ir_return *ir) |
| 577 | { |
| 578 | (void) ir; |
| 579 | value = NULL; |
| 580 | } |
| 581 | |
| 582 | |
| 583 | void |
| 584 | ir_constant_visitor::visit(ir_if *ir) |
| 585 | { |
| 586 | (void) ir; |
| 587 | value = NULL; |
| 588 | } |
Ian Romanick | fad607a | 2010-04-05 16:16:07 -0700 | [diff] [blame] | 589 | |
| 590 | |
| 591 | void |
| 592 | ir_constant_visitor::visit(ir_loop *ir) |
| 593 | { |
| 594 | (void) ir; |
| 595 | value = NULL; |
| 596 | } |
Ian Romanick | f8e31e0 | 2010-04-05 16:28:15 -0700 | [diff] [blame] | 597 | |
| 598 | |
| 599 | void |
| 600 | ir_constant_visitor::visit(ir_loop_jump *ir) |
| 601 | { |
| 602 | (void) ir; |
| 603 | value = NULL; |
| 604 | } |