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" |
| 39 | |
| 40 | /** |
| 41 | * Visitor class for evaluating constant expressions |
| 42 | */ |
| 43 | class ir_constant_visitor : public ir_visitor { |
| 44 | public: |
| 45 | ir_constant_visitor() |
| 46 | : value(NULL) |
| 47 | { |
| 48 | /* empty */ |
| 49 | } |
| 50 | |
| 51 | virtual ~ir_constant_visitor() |
| 52 | { |
| 53 | /* empty */ |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * \name Visit methods |
| 58 | * |
| 59 | * As typical for the visitor pattern, there must be one \c visit method for |
| 60 | * each concrete subclass of \c ir_instruction. Virtual base classes within |
| 61 | * the hierarchy should not have \c visit methods. |
| 62 | */ |
| 63 | /*@{*/ |
| 64 | virtual void visit(ir_variable *); |
| 65 | virtual void visit(ir_label *); |
| 66 | virtual void visit(ir_function_signature *); |
| 67 | virtual void visit(ir_function *); |
| 68 | virtual void visit(ir_expression *); |
| 69 | virtual void visit(ir_swizzle *); |
| 70 | virtual void visit(ir_dereference *); |
| 71 | virtual void visit(ir_assignment *); |
| 72 | virtual void visit(ir_constant *); |
| 73 | virtual void visit(ir_call *); |
| 74 | virtual void visit(ir_return *); |
| 75 | virtual void visit(ir_if *); |
| 76 | /*@}*/ |
| 77 | |
| 78 | /** |
| 79 | * Value of the constant expression. |
| 80 | * |
| 81 | * \note |
| 82 | * This field will be \c NULL if the expression is not constant valued. |
| 83 | */ |
| 84 | /* FINIHSME: This cannot hold values for constant arrays or structures. */ |
| 85 | ir_constant *value; |
| 86 | }; |
| 87 | |
| 88 | |
| 89 | ir_constant * |
| 90 | ir_instruction::constant_expression_value() |
| 91 | { |
| 92 | ir_constant_visitor visitor; |
| 93 | |
| 94 | this->accept(& visitor); |
| 95 | return visitor.value; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | void |
| 100 | ir_constant_visitor::visit(ir_variable *ir) |
| 101 | { |
| 102 | (void) ir; |
| 103 | value = NULL; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | void |
| 108 | ir_constant_visitor::visit(ir_label *ir) |
| 109 | { |
| 110 | (void) ir; |
| 111 | value = NULL; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | void |
| 116 | ir_constant_visitor::visit(ir_function_signature *ir) |
| 117 | { |
| 118 | (void) ir; |
| 119 | value = NULL; |
| 120 | } |
| 121 | |
| 122 | |
| 123 | void |
| 124 | ir_constant_visitor::visit(ir_function *ir) |
| 125 | { |
| 126 | (void) ir; |
| 127 | value = NULL; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | void |
| 132 | ir_constant_visitor::visit(ir_expression *ir) |
| 133 | { |
| 134 | (void) ir; |
| 135 | value = NULL; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | void |
| 140 | ir_constant_visitor::visit(ir_swizzle *ir) |
| 141 | { |
| 142 | (void) ir; |
| 143 | value = NULL; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | void |
| 148 | ir_constant_visitor::visit(ir_dereference *ir) |
| 149 | { |
| 150 | (void) ir; |
| 151 | value = NULL; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | void |
| 156 | ir_constant_visitor::visit(ir_assignment *ir) |
| 157 | { |
| 158 | (void) ir; |
| 159 | value = NULL; |
| 160 | } |
| 161 | |
| 162 | |
| 163 | void |
| 164 | ir_constant_visitor::visit(ir_constant *ir) |
| 165 | { |
| 166 | value = ir; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | void |
| 171 | ir_constant_visitor::visit(ir_call *ir) |
| 172 | { |
| 173 | (void) ir; |
| 174 | value = NULL; |
| 175 | } |
| 176 | |
| 177 | |
| 178 | void |
| 179 | ir_constant_visitor::visit(ir_return *ir) |
| 180 | { |
| 181 | (void) ir; |
| 182 | value = NULL; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | void |
| 187 | ir_constant_visitor::visit(ir_if *ir) |
| 188 | { |
| 189 | (void) ir; |
| 190 | value = NULL; |
| 191 | } |