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