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 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 41 | // Connect two nodes through an index operator, where the left node is the base |
| 42 | // of an array or struct, and the right node is a direct or indirect offset. |
| 43 | // |
| 44 | // Returns the added node. |
| 45 | // The caller should set the type of the returned node. |
| 46 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 47 | TIntermTyped *TIntermediate::addIndex( |
| 48 | TOperator op, TIntermTyped *base, TIntermTyped *index, const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 49 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 50 | TIntermBinary *node = new TIntermBinary(op); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 51 | node->setLine(line); |
| 52 | node->setLeft(base); |
| 53 | node->setRight(index); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 54 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 55 | // caller should set the type |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 56 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 57 | return node; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 61 | // This is the safe way to change the operator on an aggregate, as it |
| 62 | // does lots of error checking and fixing. Especially for establishing |
| 63 | // a function call's operation on it's set of parameters. Sequences |
| 64 | // of instructions are also aggregates, but they just direnctly set |
| 65 | // their operator to EOpSequence. |
| 66 | // |
| 67 | // 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] | 68 | // it was already an aggregate but no operator was set. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 69 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 70 | TIntermAggregate *TIntermediate::setAggregateOperator( |
| 71 | TIntermNode *node, TOperator op, const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 72 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 73 | TIntermAggregate *aggNode; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 74 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 75 | // |
| 76 | // Make sure we have an aggregate. If not turn it into one. |
| 77 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 78 | if (node) |
| 79 | { |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 80 | aggNode = node->getAsAggregate(); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 81 | if (aggNode == NULL || aggNode->getOp() != EOpNull) |
| 82 | { |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 83 | // |
| 84 | // Make an aggregate containing this node. |
| 85 | // |
| 86 | aggNode = new TIntermAggregate(); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 87 | aggNode->getSequence()->push_back(node); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 88 | } |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 89 | } |
| 90 | else |
| 91 | { |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 92 | aggNode = new TIntermAggregate(); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 93 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 94 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 95 | // |
| 96 | // Set the operator. |
| 97 | // |
alokp@chromium.org | 58e5429 | 2010-08-24 21:40:03 +0000 | [diff] [blame] | 98 | aggNode->setOp(op); |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 99 | aggNode->setLine(line); |
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 | return aggNode; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 105 | // Safe way to combine two nodes into an aggregate. Works with null pointers, |
| 106 | // a node that's not a aggregate yet, etc. |
| 107 | // |
| 108 | // Returns the resulting aggregate, unless 0 was passed in for |
| 109 | // both existing nodes. |
| 110 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 111 | TIntermAggregate *TIntermediate::growAggregate( |
| 112 | TIntermNode *left, TIntermNode *right, const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 113 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 114 | if (left == NULL && right == NULL) |
| 115 | return NULL; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 116 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 117 | TIntermAggregate *aggNode = NULL; |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 118 | if (left) |
| 119 | aggNode = left->getAsAggregate(); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 120 | if (!aggNode || aggNode->getOp() != EOpNull) |
| 121 | { |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 122 | aggNode = new TIntermAggregate; |
| 123 | if (left) |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 124 | aggNode->getSequence()->push_back(left); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 125 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 126 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 127 | if (right) |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 128 | aggNode->getSequence()->push_back(right); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 129 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 130 | aggNode->setLine(line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 131 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 132 | return aggNode; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // |
| 136 | // Turn an existing node into an aggregate. |
| 137 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 138 | // 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] | 139 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 140 | TIntermAggregate *TIntermediate::makeAggregate( |
| 141 | TIntermNode *node, const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 142 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 143 | if (node == NULL) |
| 144 | return NULL; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 145 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 146 | TIntermAggregate *aggNode = new TIntermAggregate; |
| 147 | aggNode->getSequence()->push_back(node); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 148 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 149 | aggNode->setLine(line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 150 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 151 | return aggNode; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Olli Etuaho | 7d7f8c4 | 2015-05-19 18:38:49 +0300 | [diff] [blame] | 154 | // If the input node is nullptr, return nullptr. |
| 155 | // If the input node is a sequence (block) node, return it. |
| 156 | // If the input node is not a sequence node, put it inside a sequence node and return that. |
| 157 | TIntermAggregate *TIntermediate::ensureSequence(TIntermNode *node) |
| 158 | { |
| 159 | if (node == nullptr) |
| 160 | return nullptr; |
| 161 | TIntermAggregate *aggNode = node->getAsAggregate(); |
| 162 | if (aggNode != nullptr && aggNode->getOp() == EOpSequence) |
| 163 | return aggNode; |
| 164 | |
| 165 | aggNode = makeAggregate(node, node->getLine()); |
| 166 | aggNode->setOp(EOpSequence); |
| 167 | return aggNode; |
| 168 | } |
| 169 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 170 | // |
| 171 | // For "if" test nodes. There are three children; a condition, |
| 172 | // a true path, and a false path. The two paths are in the |
| 173 | // nodePair. |
| 174 | // |
| 175 | // Returns the selection node created. |
| 176 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 177 | TIntermNode *TIntermediate::addSelection( |
| 178 | TIntermTyped *cond, TIntermNodePair nodePair, const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 179 | { |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 180 | // |
| 181 | // For compile time constant selections, prune the code and |
| 182 | // test now. |
| 183 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 184 | |
Olli Etuaho | 7c3848e | 2015-11-04 13:19:17 +0200 | [diff] [blame] | 185 | if (cond->getAsConstantUnion()) |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 186 | { |
shannon.woods%transgaming.com@gtempaccount.com | c0d0c22 | 2013-04-13 03:29:36 +0000 | [diff] [blame] | 187 | if (cond->getAsConstantUnion()->getBConst(0) == true) |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 188 | { |
| 189 | return nodePair.node1 ? setAggregateOperator( |
| 190 | nodePair.node1, EOpSequence, nodePair.node1->getLine()) : NULL; |
| 191 | } |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 192 | else |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 193 | { |
| 194 | return nodePair.node2 ? setAggregateOperator( |
| 195 | nodePair.node2, EOpSequence, nodePair.node2->getLine()) : NULL; |
| 196 | } |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 197 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 198 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 199 | TIntermSelection *node = new TIntermSelection( |
Olli Etuaho | 7d7f8c4 | 2015-05-19 18:38:49 +0300 | [diff] [blame] | 200 | cond, ensureSequence(nodePair.node1), ensureSequence(nodePair.node2)); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 201 | node->setLine(line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 202 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 203 | return node; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Olli Etuaho | 1520004 | 2015-11-04 16:56:31 +0200 | [diff] [blame] | 206 | TIntermTyped *TIntermediate::addComma(TIntermTyped *left, |
| 207 | TIntermTyped *right, |
| 208 | const TSourceLoc &line, |
| 209 | int shaderVersion) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 210 | { |
Olli Etuaho | 1520004 | 2015-11-04 16:56:31 +0200 | [diff] [blame] | 211 | TQualifier resultQualifier = EvqConst; |
| 212 | // ESSL3.00 section 12.43: The result of a sequence operator is not a constant-expression. |
| 213 | if (shaderVersion >= 300 || left->getQualifier() != EvqConst || |
| 214 | right->getQualifier() != EvqConst) |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 215 | { |
Olli Etuaho | 1520004 | 2015-11-04 16:56:31 +0200 | [diff] [blame] | 216 | resultQualifier = EvqTemporary; |
| 217 | } |
| 218 | |
| 219 | TIntermTyped *commaNode = nullptr; |
| 220 | if (!left->hasSideEffects()) |
| 221 | { |
| 222 | commaNode = right; |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 223 | } |
| 224 | else |
| 225 | { |
Olli Etuaho | 1520004 | 2015-11-04 16:56:31 +0200 | [diff] [blame] | 226 | commaNode = growAggregate(left, right, line); |
| 227 | commaNode->getAsAggregate()->setOp(EOpComma); |
| 228 | commaNode->setType(right->getType()); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 229 | } |
Olli Etuaho | 1520004 | 2015-11-04 16:56:31 +0200 | [diff] [blame] | 230 | commaNode->getTypePointer()->setQualifier(resultQualifier); |
| 231 | return commaNode; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | // |
| 235 | // For "?:" test nodes. There are three children; a condition, |
| 236 | // a true path, and a false path. The two paths are specified |
| 237 | // as separate parameters. |
| 238 | // |
Olli Etuaho | 5290174 | 2015-04-15 13:42:45 +0300 | [diff] [blame] | 239 | // Returns the selection node created, or one of trueBlock and falseBlock if the expression could be folded. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 240 | // |
Olli Etuaho | 5290174 | 2015-04-15 13:42:45 +0300 | [diff] [blame] | 241 | TIntermTyped *TIntermediate::addSelection(TIntermTyped *cond, TIntermTyped *trueBlock, TIntermTyped *falseBlock, |
| 242 | const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 243 | { |
Olli Etuaho | b1edc4f | 2015-11-02 17:20:03 +0200 | [diff] [blame] | 244 | TQualifier resultQualifier = EvqTemporary; |
| 245 | if (cond->getQualifier() == EvqConst && trueBlock->getQualifier() == EvqConst && |
| 246 | falseBlock->getQualifier() == EvqConst) |
| 247 | { |
| 248 | resultQualifier = EvqConst; |
| 249 | } |
Olli Etuaho | 7c3848e | 2015-11-04 13:19:17 +0200 | [diff] [blame] | 250 | // Note that the node resulting from here can be a constant union without being qualified as |
| 251 | // constant. |
| 252 | if (cond->getAsConstantUnion()) |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 253 | { |
shannon.woods%transgaming.com@gtempaccount.com | c0d0c22 | 2013-04-13 03:29:36 +0000 | [diff] [blame] | 254 | if (cond->getAsConstantUnion()->getBConst(0)) |
Olli Etuaho | 7c3848e | 2015-11-04 13:19:17 +0200 | [diff] [blame] | 255 | { |
| 256 | trueBlock->getTypePointer()->setQualifier(resultQualifier); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 257 | return trueBlock; |
Olli Etuaho | 7c3848e | 2015-11-04 13:19:17 +0200 | [diff] [blame] | 258 | } |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 259 | else |
Olli Etuaho | 7c3848e | 2015-11-04 13:19:17 +0200 | [diff] [blame] | 260 | { |
| 261 | falseBlock->getTypePointer()->setQualifier(resultQualifier); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 262 | return falseBlock; |
Olli Etuaho | 7c3848e | 2015-11-04 13:19:17 +0200 | [diff] [blame] | 263 | } |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 264 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 265 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 266 | // |
| 267 | // Make a selection node. |
| 268 | // |
Olli Etuaho | 5290174 | 2015-04-15 13:42:45 +0300 | [diff] [blame] | 269 | TIntermSelection *node = new TIntermSelection(cond, trueBlock, falseBlock, trueBlock->getType()); |
Olli Etuaho | b1edc4f | 2015-11-02 17:20:03 +0200 | [diff] [blame] | 270 | node->getTypePointer()->setQualifier(resultQualifier); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 271 | node->setLine(line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 272 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 273 | return node; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Olli Etuaho | a3a3666 | 2015-02-17 13:46:51 +0200 | [diff] [blame] | 276 | TIntermSwitch *TIntermediate::addSwitch( |
| 277 | TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &line) |
| 278 | { |
Olli Etuaho | 3c1dfb5 | 2015-02-20 11:34:03 +0200 | [diff] [blame] | 279 | TIntermSwitch *node = new TIntermSwitch(init, statementList); |
| 280 | node->setLine(line); |
| 281 | |
| 282 | return node; |
Olli Etuaho | a3a3666 | 2015-02-17 13:46:51 +0200 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | TIntermCase *TIntermediate::addCase( |
| 286 | TIntermTyped *condition, const TSourceLoc &line) |
| 287 | { |
Olli Etuaho | 3c1dfb5 | 2015-02-20 11:34:03 +0200 | [diff] [blame] | 288 | TIntermCase *node = new TIntermCase(condition); |
| 289 | node->setLine(line); |
| 290 | |
| 291 | return node; |
Olli Etuaho | a3a3666 | 2015-02-17 13:46:51 +0200 | [diff] [blame] | 292 | } |
| 293 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 294 | // |
| 295 | // Constant terminal nodes. Has a union that contains bool, float or int constants |
| 296 | // |
| 297 | // Returns the constant union node created. |
| 298 | // |
| 299 | |
Olli Etuaho | 5c0e023 | 2015-11-11 15:55:59 +0200 | [diff] [blame] | 300 | TIntermConstantUnion *TIntermediate::addConstantUnion(const TConstantUnion *constantUnion, |
| 301 | const TType &type, |
| 302 | const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 303 | { |
Jamie Madill | b11e248 | 2015-05-04 14:21:22 -0400 | [diff] [blame] | 304 | TIntermConstantUnion *node = new TIntermConstantUnion(constantUnion, type); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 305 | node->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 node; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 310 | TIntermTyped *TIntermediate::addSwizzle( |
| 311 | TVectorFields &fields, const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 312 | { |
| 313 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 314 | TIntermAggregate *node = new TIntermAggregate(EOpSequence); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 315 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 316 | node->setLine(line); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 317 | TIntermConstantUnion *constIntNode; |
| 318 | TIntermSequence *sequenceVector = node->getSequence(); |
Jamie Madill | 6ba6ead | 2015-05-04 14:21:21 -0400 | [diff] [blame] | 319 | TConstantUnion *unionArray; |
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 | for (int i = 0; i < fields.num; i++) |
| 322 | { |
Jamie Madill | 6ba6ead | 2015-05-04 14:21:21 -0400 | [diff] [blame] | 323 | unionArray = new TConstantUnion[1]; |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 324 | unionArray->setIConst(fields.offsets[i]); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 325 | constIntNode = addConstantUnion( |
| 326 | unionArray, TType(EbtInt, EbpUndefined, EvqConst), line); |
| 327 | sequenceVector->push_back(constIntNode); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 328 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 329 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 330 | return node; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | // |
| 334 | // Create loop nodes. |
| 335 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 336 | TIntermNode *TIntermediate::addLoop( |
| 337 | TLoopType type, TIntermNode *init, TIntermTyped *cond, TIntermTyped *expr, |
| 338 | TIntermNode *body, const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 339 | { |
Olli Etuaho | 7d7f8c4 | 2015-05-19 18:38:49 +0300 | [diff] [blame] | 340 | TIntermNode *node = new TIntermLoop(type, init, cond, expr, ensureSequence(body)); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 341 | node->setLine(line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 342 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 343 | return node; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | // |
| 347 | // Add branches. |
| 348 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 349 | TIntermBranch* TIntermediate::addBranch( |
| 350 | TOperator branchOp, const TSourceLoc &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 addBranch(branchOp, 0, line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 355 | TIntermBranch* TIntermediate::addBranch( |
| 356 | TOperator branchOp, TIntermTyped *expression, const TSourceLoc &line) |
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 | TIntermBranch *node = new TIntermBranch(branchOp, expression); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 359 | node->setLine(line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 360 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 361 | return node; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | // |
| 365 | // This is to be executed once the final root is put on top by the parsing |
| 366 | // process. |
| 367 | // |
Olli Etuaho | f119a26 | 2016-08-19 15:54:22 +0300 | [diff] [blame^] | 368 | TIntermAggregate *TIntermediate::PostProcess(TIntermNode *root) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 369 | { |
Olli Etuaho | 43613b0 | 2015-08-04 11:02:21 +0300 | [diff] [blame] | 370 | if (root == nullptr) |
| 371 | return nullptr; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 372 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 373 | // |
Olli Etuaho | 43613b0 | 2015-08-04 11:02:21 +0300 | [diff] [blame] | 374 | // Finish off the top level sequence, if any |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 375 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 376 | TIntermAggregate *aggRoot = root->getAsAggregate(); |
Olli Etuaho | 43613b0 | 2015-08-04 11:02:21 +0300 | [diff] [blame] | 377 | if (aggRoot != nullptr && aggRoot->getOp() == EOpNull) |
| 378 | { |
alokp@chromium.org | 58e5429 | 2010-08-24 21:40:03 +0000 | [diff] [blame] | 379 | aggRoot->setOp(EOpSequence); |
Olli Etuaho | 43613b0 | 2015-08-04 11:02:21 +0300 | [diff] [blame] | 380 | } |
| 381 | else if (aggRoot == nullptr || aggRoot->getOp() != EOpSequence) |
| 382 | { |
| 383 | aggRoot = new TIntermAggregate(EOpSequence); |
| 384 | aggRoot->setLine(root->getLine()); |
| 385 | aggRoot->getSequence()->push_back(root); |
| 386 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 387 | |
Olli Etuaho | 43613b0 | 2015-08-04 11:02:21 +0300 | [diff] [blame] | 388 | return aggRoot; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 389 | } |
Arun Patole | 274f070 | 2015-05-05 13:33:30 +0530 | [diff] [blame] | 390 | |
Olli Etuaho | f119a26 | 2016-08-19 15:54:22 +0300 | [diff] [blame^] | 391 | TIntermTyped *TIntermediate::foldAggregateBuiltIn(TIntermAggregate *aggregate, |
| 392 | TDiagnostics *diagnostics) |
Arun Patole | 274f070 | 2015-05-05 13:33:30 +0530 | [diff] [blame] | 393 | { |
Olli Etuaho | b43846e | 2015-06-02 18:18:57 +0300 | [diff] [blame] | 394 | switch (aggregate->getOp()) |
Arun Patole | 274f070 | 2015-05-05 13:33:30 +0530 | [diff] [blame] | 395 | { |
Olli Etuaho | 1d12278 | 2015-11-06 15:35:17 +0200 | [diff] [blame] | 396 | case EOpAtan: |
| 397 | case EOpPow: |
| 398 | case EOpMod: |
| 399 | case EOpMin: |
| 400 | case EOpMax: |
| 401 | case EOpClamp: |
| 402 | case EOpMix: |
| 403 | case EOpStep: |
| 404 | case EOpSmoothStep: |
| 405 | case EOpMul: |
| 406 | case EOpOuterProduct: |
| 407 | case EOpLessThan: |
| 408 | case EOpLessThanEqual: |
| 409 | case EOpGreaterThan: |
| 410 | case EOpGreaterThanEqual: |
| 411 | case EOpVectorEqual: |
| 412 | case EOpVectorNotEqual: |
| 413 | case EOpDistance: |
| 414 | case EOpDot: |
| 415 | case EOpCross: |
| 416 | case EOpFaceForward: |
| 417 | case EOpReflect: |
| 418 | case EOpRefract: |
Olli Etuaho | f119a26 | 2016-08-19 15:54:22 +0300 | [diff] [blame^] | 419 | return aggregate->fold(diagnostics); |
Olli Etuaho | 1d12278 | 2015-11-06 15:35:17 +0200 | [diff] [blame] | 420 | default: |
| 421 | // TODO: Add support for folding array constructors |
| 422 | if (aggregate->isConstructor() && !aggregate->isArray()) |
| 423 | { |
Olli Etuaho | f119a26 | 2016-08-19 15:54:22 +0300 | [diff] [blame^] | 424 | return aggregate->fold(diagnostics); |
Olli Etuaho | 1d12278 | 2015-11-06 15:35:17 +0200 | [diff] [blame] | 425 | } |
| 426 | // Constant folding not supported for the built-in. |
| 427 | return nullptr; |
Arun Patole | 274f070 | 2015-05-05 13:33:30 +0530 | [diff] [blame] | 428 | } |
Arun Patole | 274f070 | 2015-05-05 13:33:30 +0530 | [diff] [blame] | 429 | } |