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 | 160d092 | 2010-04-01 18:07:08 -1000 | [diff] [blame^] | 136 | unsigned int i; |
| 137 | |
| 138 | for (i = 0; i < ir->get_num_operands(); i++) { |
| 139 | op[i] = ir->operands[i]->constant_expression_value(); |
| 140 | if (!op[i]) |
| 141 | return; |
| 142 | } |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 143 | |
| 144 | switch (ir->operation) { |
Eric Anholt | 528bb85 | 2010-03-31 21:09:02 -1000 | [diff] [blame] | 145 | case ir_unop_logic_not: |
Eric Anholt | 160d092 | 2010-04-01 18:07:08 -1000 | [diff] [blame^] | 146 | value = new ir_constant(!op[0]->value.b[0]); |
| 147 | value->type = glsl_type::bool_type; |
Eric Anholt | 528bb85 | 2010-03-31 21:09:02 -1000 | [diff] [blame] | 148 | break; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 149 | case ir_binop_mul: |
Eric Anholt | 160d092 | 2010-04-01 18:07:08 -1000 | [diff] [blame^] | 150 | if (ir->operands[0]->type == ir->operands[1]->type) { |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 151 | if (ir->operands[1]->type == glsl_type::float_type) { |
| 152 | value = new ir_constant(op[0]->value.f[0] * op[1]->value.f[0]); |
| 153 | value->type = glsl_type::float_type; |
| 154 | } |
| 155 | } |
Eric Anholt | 160d092 | 2010-04-01 18:07:08 -1000 | [diff] [blame^] | 156 | if (value) |
| 157 | value->type = ir->operands[1]->type; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 158 | break; |
| 159 | case ir_binop_logic_and: |
Eric Anholt | 160d092 | 2010-04-01 18:07:08 -1000 | [diff] [blame^] | 160 | value = new ir_constant(op[0]->value.b[0] && op[1]->value.b[0]); |
| 161 | value->type = glsl_type::bool_type; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 162 | break; |
| 163 | case ir_binop_logic_or: |
Eric Anholt | 160d092 | 2010-04-01 18:07:08 -1000 | [diff] [blame^] | 164 | value = new ir_constant(op[0]->value.b[0] || op[1]->value.b[0]); |
| 165 | value->type = glsl_type::bool_type; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 166 | break; |
| 167 | default: |
| 168 | break; |
| 169 | } |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | |
| 173 | void |
| 174 | ir_constant_visitor::visit(ir_swizzle *ir) |
| 175 | { |
| 176 | (void) ir; |
| 177 | value = NULL; |
| 178 | } |
| 179 | |
| 180 | |
| 181 | void |
| 182 | ir_constant_visitor::visit(ir_dereference *ir) |
| 183 | { |
| 184 | (void) ir; |
| 185 | value = NULL; |
| 186 | } |
| 187 | |
| 188 | |
| 189 | void |
| 190 | ir_constant_visitor::visit(ir_assignment *ir) |
| 191 | { |
| 192 | (void) ir; |
| 193 | value = NULL; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | void |
| 198 | ir_constant_visitor::visit(ir_constant *ir) |
| 199 | { |
| 200 | value = ir; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | void |
| 205 | ir_constant_visitor::visit(ir_call *ir) |
| 206 | { |
| 207 | (void) ir; |
| 208 | value = NULL; |
| 209 | } |
| 210 | |
| 211 | |
| 212 | void |
| 213 | ir_constant_visitor::visit(ir_return *ir) |
| 214 | { |
| 215 | (void) ir; |
| 216 | value = NULL; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | void |
| 221 | ir_constant_visitor::visit(ir_if *ir) |
| 222 | { |
| 223 | (void) ir; |
| 224 | value = NULL; |
| 225 | } |