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