daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
Nicolas Capens | 6ed8d8a | 2014-06-11 11:25:20 -0400 | [diff] [blame] | 2 | // Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // |
| 8 | // Build the intermediate representation. |
| 9 | // |
| 10 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 11 | #include <float.h> |
alokp@chromium.org | 1bcc3fd | 2010-05-19 17:08:44 +0000 | [diff] [blame] | 12 | #include <limits.h> |
alokp@chromium.org | 32cfaf4 | 2010-08-23 21:01:13 +0000 | [diff] [blame] | 13 | #include <algorithm> |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 14 | |
Jamie Madill | b1a85f4 | 2014-08-19 15:23:24 -0400 | [diff] [blame] | 15 | #include "compiler/translator/Intermediate.h" |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 16 | #include "compiler/translator/SymbolTable.h" |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 17 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 18 | //////////////////////////////////////////////////////////////////////////// |
| 19 | // |
| 20 | // First set of functions are to help build the intermediate representation. |
| 21 | // These functions are not member functions of the nodes. |
| 22 | // They are called from parser productions. |
| 23 | // |
| 24 | ///////////////////////////////////////////////////////////////////////////// |
| 25 | |
| 26 | // |
| 27 | // Add a terminal node for an identifier in an expression. |
| 28 | // |
| 29 | // Returns the added node. |
| 30 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 31 | TIntermSymbol *TIntermediate::addSymbol( |
| 32 | int id, const TString &name, const TType &type, const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 33 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 34 | TIntermSymbol *node = new TIntermSymbol(id, name, type); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 35 | node->setLine(line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 36 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 37 | return node; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | // |
| 41 | // Connect two nodes with a new parent that does a binary operation on the nodes. |
| 42 | // |
| 43 | // Returns the added node. |
| 44 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 45 | TIntermTyped *TIntermediate::addBinaryMath( |
| 46 | TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 47 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 48 | switch (op) |
| 49 | { |
| 50 | case EOpEqual: |
| 51 | case EOpNotEqual: |
| 52 | if (left->isArray()) |
| 53 | return NULL; |
| 54 | break; |
| 55 | case EOpLessThan: |
| 56 | case EOpGreaterThan: |
| 57 | case EOpLessThanEqual: |
| 58 | case EOpGreaterThanEqual: |
| 59 | if (left->isMatrix() || left->isArray() || left->isVector() || |
| 60 | left->getBasicType() == EbtStruct) |
| 61 | { |
| 62 | return NULL; |
| 63 | } |
| 64 | break; |
| 65 | case EOpLogicalOr: |
| 66 | case EOpLogicalXor: |
| 67 | case EOpLogicalAnd: |
| 68 | if (left->getBasicType() != EbtBool || |
| 69 | left->isMatrix() || left->isArray() || left->isVector()) |
| 70 | { |
| 71 | return NULL; |
| 72 | } |
| 73 | break; |
| 74 | case EOpAdd: |
| 75 | case EOpSub: |
| 76 | case EOpDiv: |
| 77 | case EOpMul: |
| 78 | if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool) |
Gregoire Payen de La Garanderie | be954a2 | 2014-12-23 00:05:28 +0000 | [diff] [blame] | 79 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 80 | return NULL; |
Gregoire Payen de La Garanderie | be954a2 | 2014-12-23 00:05:28 +0000 | [diff] [blame] | 81 | } |
| 82 | break; |
| 83 | case EOpMod: |
Olli Etuaho | d68157f | 2015-01-16 12:24:17 +0200 | [diff] [blame] | 84 | // Note that this is only for the % operator, not for mod() |
Gregoire Payen de La Garanderie | be954a2 | 2014-12-23 00:05:28 +0000 | [diff] [blame] | 85 | if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat) |
| 86 | { |
| 87 | return NULL; |
| 88 | } |
| 89 | break; |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 90 | default: |
| 91 | break; |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 92 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 93 | |
Nicolas Capens | 6ed8d8a | 2014-06-11 11:25:20 -0400 | [diff] [blame] | 94 | if (left->getBasicType() != right->getBasicType()) |
| 95 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 96 | return NULL; |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 97 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 98 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 99 | // |
| 100 | // Need a new node holding things together then. Make |
| 101 | // one and promote it to the right type. |
| 102 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 103 | TIntermBinary *node = new TIntermBinary(op); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 104 | node->setLine(line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 105 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 106 | node->setLeft(left); |
| 107 | node->setRight(right); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 108 | if (!node->promote(mInfoSink)) |
| 109 | return NULL; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 110 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 111 | // |
| 112 | // See if we can fold constants. |
| 113 | // |
alokp@chromium.org | 8f0f24a | 2010-09-01 21:06:24 +0000 | [diff] [blame] | 114 | TIntermConstantUnion *leftTempConstant = left->getAsConstantUnion(); |
| 115 | TIntermConstantUnion *rightTempConstant = right->getAsConstantUnion(); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 116 | if (leftTempConstant && rightTempConstant) |
| 117 | { |
| 118 | TIntermTyped *typedReturnNode = |
| 119 | leftTempConstant->fold(node->getOp(), rightTempConstant, mInfoSink); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 120 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 121 | if (typedReturnNode) |
| 122 | return typedReturnNode; |
| 123 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 124 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 125 | return node; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | // |
| 129 | // Connect two nodes through an assignment. |
| 130 | // |
| 131 | // Returns the added node. |
| 132 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 133 | TIntermTyped *TIntermediate::addAssign( |
| 134 | TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 135 | { |
Nicolas Capens | 6ed8d8a | 2014-06-11 11:25:20 -0400 | [diff] [blame] | 136 | if (left->getType().getStruct() || right->getType().getStruct()) |
| 137 | { |
| 138 | if (left->getType() != right->getType()) |
| 139 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 140 | return NULL; |
Nicolas Capens | 6ed8d8a | 2014-06-11 11:25:20 -0400 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 144 | TIntermBinary *node = new TIntermBinary(op); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 145 | node->setLine(line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 146 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 147 | node->setLeft(left); |
Nicolas Capens | 6ed8d8a | 2014-06-11 11:25:20 -0400 | [diff] [blame] | 148 | node->setRight(right); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 149 | if (!node->promote(mInfoSink)) |
| 150 | return NULL; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 151 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 152 | return node; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | // |
| 156 | // Connect two nodes through an index operator, where the left node is the base |
| 157 | // of an array or struct, and the right node is a direct or indirect offset. |
| 158 | // |
| 159 | // Returns the added node. |
| 160 | // The caller should set the type of the returned node. |
| 161 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 162 | TIntermTyped *TIntermediate::addIndex( |
| 163 | TOperator op, TIntermTyped *base, TIntermTyped *index, const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 164 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 165 | TIntermBinary *node = new TIntermBinary(op); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 166 | node->setLine(line); |
| 167 | node->setLeft(base); |
| 168 | node->setRight(index); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 169 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 170 | // caller should set the type |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 171 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 172 | return node; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | // |
| 176 | // Add one node as the parent of another that it operates on. |
| 177 | // |
| 178 | // Returns the added node. |
| 179 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 180 | TIntermTyped *TIntermediate::addUnaryMath( |
| 181 | TOperator op, TIntermNode *childNode, const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 182 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 183 | TIntermUnary *node; |
| 184 | TIntermTyped *child = childNode->getAsTyped(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 185 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 186 | if (child == NULL) |
| 187 | { |
| 188 | mInfoSink.info.message(EPrefixInternalError, line, |
| 189 | "Bad type in AddUnaryMath"); |
| 190 | return NULL; |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 191 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 192 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 193 | switch (op) |
| 194 | { |
| 195 | case EOpLogicalNot: |
| 196 | if (child->getType().getBasicType() != EbtBool || |
| 197 | child->getType().isMatrix() || |
| 198 | child->getType().isArray() || |
| 199 | child->getType().isVector()) |
| 200 | { |
| 201 | return NULL; |
| 202 | } |
| 203 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 204 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 205 | case EOpPostIncrement: |
| 206 | case EOpPreIncrement: |
| 207 | case EOpPostDecrement: |
| 208 | case EOpPreDecrement: |
| 209 | case EOpNegative: |
Zhenyao Mo | de1e00e | 2014-10-09 16:55:32 -0700 | [diff] [blame] | 210 | case EOpPositive: |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 211 | if (child->getType().getBasicType() == EbtStruct || |
| 212 | child->getType().isArray()) |
| 213 | { |
| 214 | return NULL; |
| 215 | } |
| 216 | default: |
| 217 | break; |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 218 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 219 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 220 | TIntermConstantUnion *childTempConstant = 0; |
| 221 | if (child->getAsConstantUnion()) |
| 222 | childTempConstant = child->getAsConstantUnion(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 223 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 224 | // |
| 225 | // Make a new node for the operator. |
| 226 | // |
| 227 | node = new TIntermUnary(op); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 228 | node->setLine(line); |
| 229 | node->setOperand(child); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 230 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 231 | if (!node->promote(mInfoSink)) |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 232 | return 0; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 233 | |
Olli Etuaho | 7700ff6 | 2015-01-15 12:16:29 +0200 | [diff] [blame^] | 234 | switch (op) |
| 235 | { |
| 236 | case EOpPackSnorm2x16: |
| 237 | case EOpPackUnorm2x16: |
| 238 | case EOpPackHalf2x16: |
| 239 | case EOpUnpackSnorm2x16: |
| 240 | case EOpUnpackUnorm2x16: |
| 241 | node->getTypePointer()->setPrecision(EbpHigh); |
| 242 | break; |
| 243 | case EOpUnpackHalf2x16: |
| 244 | node->getTypePointer()->setPrecision(EbpMedium); |
| 245 | break; |
| 246 | default: |
| 247 | break; |
| 248 | } |
| 249 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 250 | if (childTempConstant) |
| 251 | { |
| 252 | TIntermTyped *newChild = childTempConstant->fold(op, 0, mInfoSink); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 253 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 254 | if (newChild) |
| 255 | return newChild; |
| 256 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 257 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 258 | return node; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | // |
| 262 | // This is the safe way to change the operator on an aggregate, as it |
| 263 | // does lots of error checking and fixing. Especially for establishing |
| 264 | // a function call's operation on it's set of parameters. Sequences |
| 265 | // of instructions are also aggregates, but they just direnctly set |
| 266 | // their operator to EOpSequence. |
| 267 | // |
| 268 | // Returns an aggregate node, which could be the one passed in if |
daniel@transgaming.com | 978702d | 2012-04-04 15:05:58 +0000 | [diff] [blame] | 269 | // it was already an aggregate but no operator was set. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 270 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 271 | TIntermAggregate *TIntermediate::setAggregateOperator( |
| 272 | TIntermNode *node, TOperator op, const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 273 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 274 | TIntermAggregate *aggNode; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 275 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 276 | // |
| 277 | // Make sure we have an aggregate. If not turn it into one. |
| 278 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 279 | if (node) |
| 280 | { |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 281 | aggNode = node->getAsAggregate(); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 282 | if (aggNode == NULL || aggNode->getOp() != EOpNull) |
| 283 | { |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 284 | // |
| 285 | // Make an aggregate containing this node. |
| 286 | // |
| 287 | aggNode = new TIntermAggregate(); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 288 | aggNode->getSequence()->push_back(node); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 289 | } |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 290 | } |
| 291 | else |
| 292 | { |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 293 | aggNode = new TIntermAggregate(); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 294 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 295 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 296 | // |
| 297 | // Set the operator. |
| 298 | // |
alokp@chromium.org | 58e5429 | 2010-08-24 21:40:03 +0000 | [diff] [blame] | 299 | aggNode->setOp(op); |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 300 | aggNode->setLine(line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 301 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 302 | return aggNode; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 306 | // Safe way to combine two nodes into an aggregate. Works with null pointers, |
| 307 | // a node that's not a aggregate yet, etc. |
| 308 | // |
| 309 | // Returns the resulting aggregate, unless 0 was passed in for |
| 310 | // both existing nodes. |
| 311 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 312 | TIntermAggregate *TIntermediate::growAggregate( |
| 313 | TIntermNode *left, TIntermNode *right, const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 314 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 315 | if (left == NULL && right == NULL) |
| 316 | return NULL; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 317 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 318 | TIntermAggregate *aggNode = NULL; |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 319 | if (left) |
| 320 | aggNode = left->getAsAggregate(); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 321 | if (!aggNode || aggNode->getOp() != EOpNull) |
| 322 | { |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 323 | aggNode = new TIntermAggregate; |
| 324 | if (left) |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 325 | aggNode->getSequence()->push_back(left); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 326 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 327 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 328 | if (right) |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 329 | aggNode->getSequence()->push_back(right); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 330 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 331 | aggNode->setLine(line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 332 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 333 | return aggNode; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | // |
| 337 | // Turn an existing node into an aggregate. |
| 338 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 339 | // Returns an aggregate, unless NULL was passed in for the existing node. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 340 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 341 | TIntermAggregate *TIntermediate::makeAggregate( |
| 342 | TIntermNode *node, const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 343 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 344 | if (node == NULL) |
| 345 | return NULL; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 346 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 347 | TIntermAggregate *aggNode = new TIntermAggregate; |
| 348 | aggNode->getSequence()->push_back(node); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 349 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 350 | aggNode->setLine(line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 351 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 352 | return aggNode; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | // |
| 356 | // For "if" test nodes. There are three children; a condition, |
| 357 | // a true path, and a false path. The two paths are in the |
| 358 | // nodePair. |
| 359 | // |
| 360 | // Returns the selection node created. |
| 361 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 362 | TIntermNode *TIntermediate::addSelection( |
| 363 | TIntermTyped *cond, TIntermNodePair nodePair, const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 364 | { |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 365 | // |
| 366 | // For compile time constant selections, prune the code and |
| 367 | // test now. |
| 368 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 369 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 370 | if (cond->getAsTyped() && cond->getAsTyped()->getAsConstantUnion()) |
| 371 | { |
shannon.woods%transgaming.com@gtempaccount.com | c0d0c22 | 2013-04-13 03:29:36 +0000 | [diff] [blame] | 372 | if (cond->getAsConstantUnion()->getBConst(0) == true) |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 373 | { |
| 374 | return nodePair.node1 ? setAggregateOperator( |
| 375 | nodePair.node1, EOpSequence, nodePair.node1->getLine()) : NULL; |
| 376 | } |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 377 | else |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 378 | { |
| 379 | return nodePair.node2 ? setAggregateOperator( |
| 380 | nodePair.node2, EOpSequence, nodePair.node2->getLine()) : NULL; |
| 381 | } |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 382 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 383 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 384 | TIntermSelection *node = new TIntermSelection( |
| 385 | cond, nodePair.node1, nodePair.node2); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 386 | node->setLine(line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 387 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 388 | return node; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 391 | TIntermTyped *TIntermediate::addComma( |
| 392 | TIntermTyped *left, TIntermTyped *right, const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 393 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 394 | if (left->getType().getQualifier() == EvqConst && |
| 395 | right->getType().getQualifier() == EvqConst) |
| 396 | { |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 397 | return right; |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 398 | } |
| 399 | else |
| 400 | { |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 401 | TIntermTyped *commaAggregate = growAggregate(left, right, line); |
alokp@chromium.org | 58e5429 | 2010-08-24 21:40:03 +0000 | [diff] [blame] | 402 | commaAggregate->getAsAggregate()->setOp(EOpComma); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 403 | commaAggregate->setType(right->getType()); |
alokp@chromium.org | 58e5429 | 2010-08-24 21:40:03 +0000 | [diff] [blame] | 404 | commaAggregate->getTypePointer()->setQualifier(EvqTemporary); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 405 | return commaAggregate; |
| 406 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | // |
| 410 | // For "?:" test nodes. There are three children; a condition, |
| 411 | // a true path, and a false path. The two paths are specified |
| 412 | // as separate parameters. |
| 413 | // |
| 414 | // Returns the selection node created, or 0 if one could not be. |
| 415 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 416 | TIntermTyped *TIntermediate::addSelection( |
| 417 | TIntermTyped *cond, TIntermTyped *trueBlock, TIntermTyped *falseBlock, |
| 418 | const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 419 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 420 | if (!cond || !trueBlock || !falseBlock || |
| 421 | trueBlock->getType() != falseBlock->getType()) |
Nicolas Capens | 6ed8d8a | 2014-06-11 11:25:20 -0400 | [diff] [blame] | 422 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 423 | return NULL; |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 424 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 425 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 426 | // |
| 427 | // See if all the operands are constant, then fold it otherwise not. |
| 428 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 429 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 430 | if (cond->getAsConstantUnion() && |
| 431 | trueBlock->getAsConstantUnion() && |
| 432 | falseBlock->getAsConstantUnion()) |
| 433 | { |
shannon.woods%transgaming.com@gtempaccount.com | c0d0c22 | 2013-04-13 03:29:36 +0000 | [diff] [blame] | 434 | if (cond->getAsConstantUnion()->getBConst(0)) |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 435 | return trueBlock; |
| 436 | else |
| 437 | return falseBlock; |
| 438 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 439 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 440 | // |
| 441 | // Make a selection node. |
| 442 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 443 | TIntermSelection *node = new TIntermSelection( |
| 444 | cond, trueBlock, falseBlock, trueBlock->getType()); |
daniel@transgaming.com | 43affc5 | 2012-03-28 14:55:39 +0000 | [diff] [blame] | 445 | node->getTypePointer()->setQualifier(EvqTemporary); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 446 | node->setLine(line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 447 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 448 | return node; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | // |
| 452 | // Constant terminal nodes. Has a union that contains bool, float or int constants |
| 453 | // |
| 454 | // Returns the constant union node created. |
| 455 | // |
| 456 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 457 | TIntermConstantUnion *TIntermediate::addConstantUnion( |
| 458 | ConstantUnion *unionArrayPointer, const TType &t, const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 459 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 460 | TIntermConstantUnion *node = new TIntermConstantUnion(unionArrayPointer, t); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 461 | node->setLine(line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 462 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 463 | return node; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 466 | TIntermTyped *TIntermediate::addSwizzle( |
| 467 | TVectorFields &fields, const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 468 | { |
| 469 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 470 | TIntermAggregate *node = new TIntermAggregate(EOpSequence); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 471 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 472 | node->setLine(line); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 473 | TIntermConstantUnion *constIntNode; |
| 474 | TIntermSequence *sequenceVector = node->getSequence(); |
| 475 | ConstantUnion *unionArray; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 476 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 477 | for (int i = 0; i < fields.num; i++) |
| 478 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 479 | unionArray = new ConstantUnion[1]; |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 480 | unionArray->setIConst(fields.offsets[i]); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 481 | constIntNode = addConstantUnion( |
| 482 | unionArray, TType(EbtInt, EbpUndefined, EvqConst), line); |
| 483 | sequenceVector->push_back(constIntNode); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 484 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 485 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 486 | return node; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | // |
| 490 | // Create loop nodes. |
| 491 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 492 | TIntermNode *TIntermediate::addLoop( |
| 493 | TLoopType type, TIntermNode *init, TIntermTyped *cond, TIntermTyped *expr, |
| 494 | TIntermNode *body, const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 495 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 496 | TIntermNode *node = new TIntermLoop(type, init, cond, expr, body); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 497 | node->setLine(line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 498 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 499 | return node; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | // |
| 503 | // Add branches. |
| 504 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 505 | TIntermBranch* TIntermediate::addBranch( |
| 506 | TOperator branchOp, const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 507 | { |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 508 | return addBranch(branchOp, 0, line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 511 | TIntermBranch* TIntermediate::addBranch( |
| 512 | TOperator branchOp, TIntermTyped *expression, const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 513 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 514 | TIntermBranch *node = new TIntermBranch(branchOp, expression); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 515 | node->setLine(line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 516 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 517 | return node; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | // |
| 521 | // This is to be executed once the final root is put on top by the parsing |
| 522 | // process. |
| 523 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 524 | bool TIntermediate::postProcess(TIntermNode *root) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 525 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 526 | if (root == NULL) |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 527 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 528 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 529 | // |
| 530 | // First, finish off the top level sequence, if any |
| 531 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 532 | TIntermAggregate *aggRoot = root->getAsAggregate(); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 533 | if (aggRoot && aggRoot->getOp() == EOpNull) |
alokp@chromium.org | 58e5429 | 2010-08-24 21:40:03 +0000 | [diff] [blame] | 534 | aggRoot->setOp(EOpSequence); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 535 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 536 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 537 | } |