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