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 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 65 | // This is the safe way to change the operator on an aggregate, as it |
| 66 | // does lots of error checking and fixing. Especially for establishing |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 67 | // a function call's operation on it's set of parameters. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 68 | // |
| 69 | // 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] | 70 | // it was already an aggregate but no operator was set. |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 71 | TIntermAggregate *TIntermediate::setAggregateOperator( |
| 72 | TIntermNode *node, TOperator op, const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 73 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 74 | TIntermAggregate *aggNode; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 75 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 76 | // |
| 77 | // Make sure we have an aggregate. If not turn it into one. |
| 78 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 79 | if (node) |
| 80 | { |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 81 | aggNode = node->getAsAggregate(); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 82 | if (aggNode == NULL || aggNode->getOp() != EOpNull) |
| 83 | { |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 84 | // |
| 85 | // Make an aggregate containing this node. |
| 86 | // |
| 87 | aggNode = new TIntermAggregate(); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 88 | aggNode->getSequence()->push_back(node); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 89 | } |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 90 | } |
| 91 | else |
| 92 | { |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 93 | aggNode = new TIntermAggregate(); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 94 | } |
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 | // |
| 97 | // Set the operator. |
| 98 | // |
alokp@chromium.org | 58e5429 | 2010-08-24 21:40:03 +0000 | [diff] [blame] | 99 | aggNode->setOp(op); |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 100 | aggNode->setLine(line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 101 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 102 | return aggNode; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 106 | // Safe way to combine two nodes into an aggregate. Works with null pointers, |
| 107 | // a node that's not a aggregate yet, etc. |
| 108 | // |
| 109 | // Returns the resulting aggregate, unless 0 was passed in for |
| 110 | // both existing nodes. |
| 111 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 112 | TIntermAggregate *TIntermediate::growAggregate( |
| 113 | TIntermNode *left, TIntermNode *right, const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 114 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 115 | if (left == NULL && right == NULL) |
| 116 | return NULL; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 117 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 118 | TIntermAggregate *aggNode = NULL; |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 119 | if (left) |
| 120 | aggNode = left->getAsAggregate(); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 121 | if (!aggNode || aggNode->getOp() != EOpNull) |
| 122 | { |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 123 | aggNode = new TIntermAggregate; |
| 124 | if (left) |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 125 | aggNode->getSequence()->push_back(left); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 126 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 127 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 128 | if (right) |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 129 | aggNode->getSequence()->push_back(right); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 130 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 131 | aggNode->setLine(line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 132 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 133 | return aggNode; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | // |
| 137 | // Turn an existing node into an aggregate. |
| 138 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 139 | // 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] | 140 | // |
Olli Etuaho | 32db19b | 2016-10-04 14:43:16 +0100 | [diff] [blame] | 141 | TIntermAggregate *TIntermediate::MakeAggregate(TIntermNode *node, const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 142 | { |
Olli Etuaho | 32db19b | 2016-10-04 14:43:16 +0100 | [diff] [blame] | 143 | if (node == nullptr) |
| 144 | return nullptr; |
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. |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 155 | // If the input node is a block node, return it. |
| 156 | // If the input node is not a block node, put it inside a block node and return that. |
| 157 | TIntermBlock *TIntermediate::EnsureBlock(TIntermNode *node) |
Olli Etuaho | 7d7f8c4 | 2015-05-19 18:38:49 +0300 | [diff] [blame] | 158 | { |
| 159 | if (node == nullptr) |
| 160 | return nullptr; |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 161 | TIntermBlock *blockNode = node->getAsBlock(); |
| 162 | if (blockNode != nullptr) |
| 163 | return blockNode; |
Olli Etuaho | 7d7f8c4 | 2015-05-19 18:38:49 +0300 | [diff] [blame] | 164 | |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 165 | blockNode = new TIntermBlock(); |
| 166 | blockNode->setLine(node->getLine()); |
| 167 | blockNode->getSequence()->push_back(node); |
| 168 | return blockNode; |
Olli Etuaho | 7d7f8c4 | 2015-05-19 18:38:49 +0300 | [diff] [blame] | 169 | } |
| 170 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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 | // |
Olli Etuaho | 5796127 | 2016-09-14 13:57:46 +0300 | [diff] [blame] | 175 | // Returns the node created. |
| 176 | TIntermNode *TIntermediate::addIfElse(TIntermTyped *cond, |
| 177 | TIntermNodePair nodePair, |
| 178 | const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 179 | { |
Olli Etuaho | 5796127 | 2016-09-14 13:57:46 +0300 | [diff] [blame] | 180 | // For compile time constant conditions, prune the code now. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 181 | |
Olli Etuaho | 7c3848e | 2015-11-04 13:19:17 +0200 | [diff] [blame] | 182 | if (cond->getAsConstantUnion()) |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 183 | { |
shannon.woods%transgaming.com@gtempaccount.com | c0d0c22 | 2013-04-13 03:29:36 +0000 | [diff] [blame] | 184 | if (cond->getAsConstantUnion()->getBConst(0) == true) |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 185 | { |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 186 | return EnsureBlock(nodePair.node1); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 187 | } |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 188 | else |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 189 | { |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 190 | return EnsureBlock(nodePair.node2); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 191 | } |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 192 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 193 | |
Olli Etuaho | 5796127 | 2016-09-14 13:57:46 +0300 | [diff] [blame] | 194 | TIntermIfElse *node = |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 195 | new TIntermIfElse(cond, EnsureBlock(nodePair.node1), EnsureBlock(nodePair.node2)); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 196 | node->setLine(line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 197 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 198 | return node; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Olli Etuaho | 4db7ded | 2016-10-13 12:23:11 +0100 | [diff] [blame^] | 201 | TIntermTyped *TIntermediate::AddComma(TIntermTyped *left, |
Olli Etuaho | 1520004 | 2015-11-04 16:56:31 +0200 | [diff] [blame] | 202 | TIntermTyped *right, |
| 203 | const TSourceLoc &line, |
| 204 | int shaderVersion) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 205 | { |
Olli Etuaho | 1520004 | 2015-11-04 16:56:31 +0200 | [diff] [blame] | 206 | TIntermTyped *commaNode = nullptr; |
| 207 | if (!left->hasSideEffects()) |
| 208 | { |
| 209 | commaNode = right; |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 210 | } |
| 211 | else |
| 212 | { |
Olli Etuaho | 4db7ded | 2016-10-13 12:23:11 +0100 | [diff] [blame^] | 213 | commaNode = new TIntermBinary(EOpComma, left, right); |
| 214 | commaNode->setLine(line); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 215 | } |
Olli Etuaho | 4db7ded | 2016-10-13 12:23:11 +0100 | [diff] [blame^] | 216 | TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(shaderVersion, left, right); |
Olli Etuaho | 1520004 | 2015-11-04 16:56:31 +0200 | [diff] [blame] | 217 | commaNode->getTypePointer()->setQualifier(resultQualifier); |
| 218 | return commaNode; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 219 | } |
| 220 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 221 | // For "?:" test nodes. There are three children; a condition, |
| 222 | // a true path, and a false path. The two paths are specified |
| 223 | // as separate parameters. |
| 224 | // |
Olli Etuaho | d0bad2c | 2016-09-09 18:01:16 +0300 | [diff] [blame] | 225 | // Returns the ternary node created, or one of trueExpression and falseExpression if the expression |
| 226 | // could be folded. |
| 227 | TIntermTyped *TIntermediate::AddTernarySelection(TIntermTyped *cond, |
| 228 | TIntermTyped *trueExpression, |
| 229 | TIntermTyped *falseExpression, |
| 230 | const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 231 | { |
Olli Etuaho | 7c3848e | 2015-11-04 13:19:17 +0200 | [diff] [blame] | 232 | // Note that the node resulting from here can be a constant union without being qualified as |
| 233 | // constant. |
| 234 | if (cond->getAsConstantUnion()) |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 235 | { |
Olli Etuaho | d0bad2c | 2016-09-09 18:01:16 +0300 | [diff] [blame] | 236 | TQualifier resultQualifier = |
| 237 | TIntermTernary::DetermineQualifier(cond, trueExpression, falseExpression); |
shannon.woods%transgaming.com@gtempaccount.com | c0d0c22 | 2013-04-13 03:29:36 +0000 | [diff] [blame] | 238 | if (cond->getAsConstantUnion()->getBConst(0)) |
Olli Etuaho | 7c3848e | 2015-11-04 13:19:17 +0200 | [diff] [blame] | 239 | { |
Olli Etuaho | d0bad2c | 2016-09-09 18:01:16 +0300 | [diff] [blame] | 240 | trueExpression->getTypePointer()->setQualifier(resultQualifier); |
| 241 | return trueExpression; |
Olli Etuaho | 7c3848e | 2015-11-04 13:19:17 +0200 | [diff] [blame] | 242 | } |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 243 | else |
Olli Etuaho | 7c3848e | 2015-11-04 13:19:17 +0200 | [diff] [blame] | 244 | { |
Olli Etuaho | d0bad2c | 2016-09-09 18:01:16 +0300 | [diff] [blame] | 245 | falseExpression->getTypePointer()->setQualifier(resultQualifier); |
| 246 | return falseExpression; |
Olli Etuaho | 7c3848e | 2015-11-04 13:19:17 +0200 | [diff] [blame] | 247 | } |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 248 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 249 | |
Olli Etuaho | d0bad2c | 2016-09-09 18:01:16 +0300 | [diff] [blame] | 250 | // Make a ternary node. |
| 251 | TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 252 | node->setLine(line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 253 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 254 | return node; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 257 | TIntermSwitch *TIntermediate::addSwitch(TIntermTyped *init, |
| 258 | TIntermBlock *statementList, |
| 259 | const TSourceLoc &line) |
Olli Etuaho | a3a3666 | 2015-02-17 13:46:51 +0200 | [diff] [blame] | 260 | { |
Olli Etuaho | 3c1dfb5 | 2015-02-20 11:34:03 +0200 | [diff] [blame] | 261 | TIntermSwitch *node = new TIntermSwitch(init, statementList); |
| 262 | node->setLine(line); |
| 263 | |
| 264 | return node; |
Olli Etuaho | a3a3666 | 2015-02-17 13:46:51 +0200 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | TIntermCase *TIntermediate::addCase( |
| 268 | TIntermTyped *condition, const TSourceLoc &line) |
| 269 | { |
Olli Etuaho | 3c1dfb5 | 2015-02-20 11:34:03 +0200 | [diff] [blame] | 270 | TIntermCase *node = new TIntermCase(condition); |
| 271 | node->setLine(line); |
| 272 | |
| 273 | return node; |
Olli Etuaho | a3a3666 | 2015-02-17 13:46:51 +0200 | [diff] [blame] | 274 | } |
| 275 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 276 | // |
| 277 | // Constant terminal nodes. Has a union that contains bool, float or int constants |
| 278 | // |
| 279 | // Returns the constant union node created. |
| 280 | // |
| 281 | |
Olli Etuaho | 5c0e023 | 2015-11-11 15:55:59 +0200 | [diff] [blame] | 282 | TIntermConstantUnion *TIntermediate::addConstantUnion(const TConstantUnion *constantUnion, |
| 283 | const TType &type, |
| 284 | const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 285 | { |
Jamie Madill | b11e248 | 2015-05-04 14:21:22 -0400 | [diff] [blame] | 286 | TIntermConstantUnion *node = new TIntermConstantUnion(constantUnion, type); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 287 | node->setLine(line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 288 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 289 | return node; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Olli Etuaho | b6fa043 | 2016-09-28 16:28:05 +0100 | [diff] [blame] | 292 | TIntermTyped *TIntermediate::AddSwizzle(TIntermTyped *baseExpression, |
| 293 | const TVectorFields &fields, |
| 294 | const TSourceLoc &dotLocation) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 295 | { |
Olli Etuaho | b6fa043 | 2016-09-28 16:28:05 +0100 | [diff] [blame] | 296 | TVector<int> fieldsVector; |
| 297 | for (int i = 0; i < fields.num; ++i) |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 298 | { |
Olli Etuaho | b6fa043 | 2016-09-28 16:28:05 +0100 | [diff] [blame] | 299 | fieldsVector.push_back(fields.offsets[i]); |
| 300 | } |
| 301 | TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldsVector); |
| 302 | node->setLine(dotLocation); |
| 303 | |
| 304 | TIntermTyped *folded = node->fold(); |
| 305 | if (folded) |
| 306 | { |
| 307 | return folded; |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 308 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 309 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 310 | return node; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | // |
| 314 | // Create loop nodes. |
| 315 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 316 | TIntermNode *TIntermediate::addLoop( |
| 317 | TLoopType type, TIntermNode *init, TIntermTyped *cond, TIntermTyped *expr, |
| 318 | TIntermNode *body, const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 319 | { |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 320 | TIntermNode *node = new TIntermLoop(type, init, cond, expr, EnsureBlock(body)); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 321 | node->setLine(line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 322 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 323 | return node; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | // |
| 327 | // Add branches. |
| 328 | // |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 329 | TIntermBranch* TIntermediate::addBranch( |
| 330 | TOperator branchOp, const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 331 | { |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 332 | return addBranch(branchOp, 0, line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 335 | TIntermBranch* TIntermediate::addBranch( |
| 336 | TOperator branchOp, TIntermTyped *expression, const TSourceLoc &line) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 337 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 338 | TIntermBranch *node = new TIntermBranch(branchOp, expression); |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 339 | node->setLine(line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 340 | |
alokp@chromium.org | 2cf1771 | 2010-03-30 20:33:18 +0000 | [diff] [blame] | 341 | return node; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Olli Etuaho | f119a26 | 2016-08-19 15:54:22 +0300 | [diff] [blame] | 344 | TIntermTyped *TIntermediate::foldAggregateBuiltIn(TIntermAggregate *aggregate, |
| 345 | TDiagnostics *diagnostics) |
Arun Patole | 274f070 | 2015-05-05 13:33:30 +0530 | [diff] [blame] | 346 | { |
Olli Etuaho | b43846e | 2015-06-02 18:18:57 +0300 | [diff] [blame] | 347 | switch (aggregate->getOp()) |
Arun Patole | 274f070 | 2015-05-05 13:33:30 +0530 | [diff] [blame] | 348 | { |
Olli Etuaho | 1d12278 | 2015-11-06 15:35:17 +0200 | [diff] [blame] | 349 | case EOpAtan: |
| 350 | case EOpPow: |
| 351 | case EOpMod: |
| 352 | case EOpMin: |
| 353 | case EOpMax: |
| 354 | case EOpClamp: |
| 355 | case EOpMix: |
| 356 | case EOpStep: |
| 357 | case EOpSmoothStep: |
| 358 | case EOpMul: |
| 359 | case EOpOuterProduct: |
| 360 | case EOpLessThan: |
| 361 | case EOpLessThanEqual: |
| 362 | case EOpGreaterThan: |
| 363 | case EOpGreaterThanEqual: |
| 364 | case EOpVectorEqual: |
| 365 | case EOpVectorNotEqual: |
| 366 | case EOpDistance: |
| 367 | case EOpDot: |
| 368 | case EOpCross: |
| 369 | case EOpFaceForward: |
| 370 | case EOpReflect: |
| 371 | case EOpRefract: |
Olli Etuaho | f119a26 | 2016-08-19 15:54:22 +0300 | [diff] [blame] | 372 | return aggregate->fold(diagnostics); |
Olli Etuaho | 1d12278 | 2015-11-06 15:35:17 +0200 | [diff] [blame] | 373 | default: |
| 374 | // TODO: Add support for folding array constructors |
| 375 | if (aggregate->isConstructor() && !aggregate->isArray()) |
| 376 | { |
Olli Etuaho | f119a26 | 2016-08-19 15:54:22 +0300 | [diff] [blame] | 377 | return aggregate->fold(diagnostics); |
Olli Etuaho | 1d12278 | 2015-11-06 15:35:17 +0200 | [diff] [blame] | 378 | } |
| 379 | // Constant folding not supported for the built-in. |
| 380 | return nullptr; |
Arun Patole | 274f070 | 2015-05-05 13:33:30 +0530 | [diff] [blame] | 381 | } |
Arun Patole | 274f070 | 2015-05-05 13:33:30 +0530 | [diff] [blame] | 382 | } |