blob: cf0717f138539d5a339fdc9900f53cbac0f2175d [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// 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.com4f39fd92010-03-08 20:26:45 +000011#include <float.h>
alokp@chromium.org1bcc3fd2010-05-19 17:08:44 +000012#include <limits.h>
alokp@chromium.org32cfaf42010-08-23 21:01:13 +000013#include <algorithm>
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000014
Jamie Madillb1a85f42014-08-19 15:23:24 -040015#include "compiler/translator/Intermediate.h"
Geoff Lang17732822013-08-29 13:46:49 -040016#include "compiler/translator/SymbolTable.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000017
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000018////////////////////////////////////////////////////////////////////////////
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 Moe40d1e92014-07-16 17:40:36 -070031TIntermSymbol *TIntermediate::addSymbol(
32 int id, const TString &name, const TType &type, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000033{
Zhenyao Moe40d1e92014-07-16 17:40:36 -070034 TIntermSymbol *node = new TIntermSymbol(id, name, type);
alokp@chromium.org2cf17712010-03-30 20:33:18 +000035 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000036
alokp@chromium.org2cf17712010-03-30 20:33:18 +000037 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000038}
39
40//
41// Connect two nodes with a new parent that does a binary operation on the nodes.
42//
43// Returns the added node.
44//
Zhenyao Moe40d1e92014-07-16 17:40:36 -070045TIntermTyped *TIntermediate::addBinaryMath(
46 TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000047{
alokp@chromium.org2cf17712010-03-30 20:33:18 +000048 //
49 // Need a new node holding things together then. Make
50 // one and promote it to the right type.
51 //
Zhenyao Moe40d1e92014-07-16 17:40:36 -070052 TIntermBinary *node = new TIntermBinary(op);
alokp@chromium.org2cf17712010-03-30 20:33:18 +000053 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000054
alokp@chromium.org2cf17712010-03-30 20:33:18 +000055 node->setLeft(left);
56 node->setRight(right);
Zhenyao Moe40d1e92014-07-16 17:40:36 -070057 if (!node->promote(mInfoSink))
58 return NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000059
alokp@chromium.org2cf17712010-03-30 20:33:18 +000060 // See if we can fold constants.
Olli Etuaho2c4b7462015-06-08 11:30:31 +030061 TIntermTyped *foldedNode = node->fold(mInfoSink);
62 if (foldedNode)
63 return foldedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000064
alokp@chromium.org2cf17712010-03-30 20:33:18 +000065 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000066}
67
68//
69// Connect two nodes through an assignment.
70//
71// Returns the added node.
72//
Zhenyao Moe40d1e92014-07-16 17:40:36 -070073TIntermTyped *TIntermediate::addAssign(
74 TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000075{
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -040076 if (left->getType().getStruct() || right->getType().getStruct())
77 {
78 if (left->getType() != right->getType())
79 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -070080 return NULL;
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -040081 }
82 }
83
Zhenyao Moe40d1e92014-07-16 17:40:36 -070084 TIntermBinary *node = new TIntermBinary(op);
alokp@chromium.org2cf17712010-03-30 20:33:18 +000085 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000086
alokp@chromium.org2cf17712010-03-30 20:33:18 +000087 node->setLeft(left);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -040088 node->setRight(right);
Zhenyao Moe40d1e92014-07-16 17:40:36 -070089 if (!node->promote(mInfoSink))
90 return NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000091
alokp@chromium.org2cf17712010-03-30 20:33:18 +000092 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000093}
94
95//
96// Connect two nodes through an index operator, where the left node is the base
97// of an array or struct, and the right node is a direct or indirect offset.
98//
99// Returns the added node.
100// The caller should set the type of the returned node.
101//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700102TIntermTyped *TIntermediate::addIndex(
103 TOperator op, TIntermTyped *base, TIntermTyped *index, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000104{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700105 TIntermBinary *node = new TIntermBinary(op);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000106 node->setLine(line);
107 node->setLeft(base);
108 node->setRight(index);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000109
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000110 // caller should set the type
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000111
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000112 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000113}
114
115//
116// Add one node as the parent of another that it operates on.
117//
118// Returns the added node.
119//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700120TIntermTyped *TIntermediate::addUnaryMath(
Olli Etuahof6c694b2015-03-26 14:50:53 +0200121 TOperator op, TIntermTyped *child, const TSourceLoc &line, const TType *funcReturnType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000122{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000123 //
124 // Make a new node for the operator.
125 //
Olli Etuaho69c11b52015-03-26 12:59:00 +0200126 TIntermUnary *node = new TIntermUnary(op);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000127 node->setLine(line);
128 node->setOperand(child);
Olli Etuahof6c694b2015-03-26 14:50:53 +0200129 node->promote(funcReturnType);
Olli Etuaho7700ff62015-01-15 12:16:29 +0200130
Olli Etuaho95310b02015-06-02 17:43:38 +0300131 TIntermTyped *foldedNode = node->fold(mInfoSink);
132 if (foldedNode)
133 return foldedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000134
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000135 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000136}
137
138//
139// This is the safe way to change the operator on an aggregate, as it
140// does lots of error checking and fixing. Especially for establishing
141// a function call's operation on it's set of parameters. Sequences
142// of instructions are also aggregates, but they just direnctly set
143// their operator to EOpSequence.
144//
145// Returns an aggregate node, which could be the one passed in if
daniel@transgaming.com978702d2012-04-04 15:05:58 +0000146// it was already an aggregate but no operator was set.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000147//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700148TIntermAggregate *TIntermediate::setAggregateOperator(
149 TIntermNode *node, TOperator op, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000150{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700151 TIntermAggregate *aggNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000152
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000153 //
154 // Make sure we have an aggregate. If not turn it into one.
155 //
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700156 if (node)
157 {
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000158 aggNode = node->getAsAggregate();
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700159 if (aggNode == NULL || aggNode->getOp() != EOpNull)
160 {
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000161 //
162 // Make an aggregate containing this node.
163 //
164 aggNode = new TIntermAggregate();
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700165 aggNode->getSequence()->push_back(node);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000166 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700167 }
168 else
169 {
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000170 aggNode = new TIntermAggregate();
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700171 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000172
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000173 //
174 // Set the operator.
175 //
alokp@chromium.org58e54292010-08-24 21:40:03 +0000176 aggNode->setOp(op);
Jamie Madill075edd82013-07-08 13:30:19 -0400177 aggNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000178
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000179 return aggNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000180}
181
182//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000183// Safe way to combine two nodes into an aggregate. Works with null pointers,
184// a node that's not a aggregate yet, etc.
185//
186// Returns the resulting aggregate, unless 0 was passed in for
187// both existing nodes.
188//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700189TIntermAggregate *TIntermediate::growAggregate(
190 TIntermNode *left, TIntermNode *right, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000191{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700192 if (left == NULL && right == NULL)
193 return NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000194
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700195 TIntermAggregate *aggNode = NULL;
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000196 if (left)
197 aggNode = left->getAsAggregate();
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700198 if (!aggNode || aggNode->getOp() != EOpNull)
199 {
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000200 aggNode = new TIntermAggregate;
201 if (left)
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700202 aggNode->getSequence()->push_back(left);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000203 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000204
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000205 if (right)
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700206 aggNode->getSequence()->push_back(right);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000207
Jamie Madill075edd82013-07-08 13:30:19 -0400208 aggNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000209
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000210 return aggNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000211}
212
213//
214// Turn an existing node into an aggregate.
215//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700216// Returns an aggregate, unless NULL was passed in for the existing node.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000217//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700218TIntermAggregate *TIntermediate::makeAggregate(
219 TIntermNode *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000220{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700221 if (node == NULL)
222 return NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000223
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700224 TIntermAggregate *aggNode = new TIntermAggregate;
225 aggNode->getSequence()->push_back(node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000226
Jamie Madill075edd82013-07-08 13:30:19 -0400227 aggNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000228
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000229 return aggNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000230}
231
Olli Etuaho7d7f8c42015-05-19 18:38:49 +0300232// If the input node is nullptr, return nullptr.
233// If the input node is a sequence (block) node, return it.
234// If the input node is not a sequence node, put it inside a sequence node and return that.
235TIntermAggregate *TIntermediate::ensureSequence(TIntermNode *node)
236{
237 if (node == nullptr)
238 return nullptr;
239 TIntermAggregate *aggNode = node->getAsAggregate();
240 if (aggNode != nullptr && aggNode->getOp() == EOpSequence)
241 return aggNode;
242
243 aggNode = makeAggregate(node, node->getLine());
244 aggNode->setOp(EOpSequence);
245 return aggNode;
246}
247
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000248//
249// For "if" test nodes. There are three children; a condition,
250// a true path, and a false path. The two paths are in the
251// nodePair.
252//
253// Returns the selection node created.
254//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700255TIntermNode *TIntermediate::addSelection(
256 TIntermTyped *cond, TIntermNodePair nodePair, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000257{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000258 //
259 // For compile time constant selections, prune the code and
260 // test now.
261 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000262
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700263 if (cond->getAsTyped() && cond->getAsTyped()->getAsConstantUnion())
264 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +0000265 if (cond->getAsConstantUnion()->getBConst(0) == true)
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700266 {
267 return nodePair.node1 ? setAggregateOperator(
268 nodePair.node1, EOpSequence, nodePair.node1->getLine()) : NULL;
269 }
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000270 else
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700271 {
272 return nodePair.node2 ? setAggregateOperator(
273 nodePair.node2, EOpSequence, nodePair.node2->getLine()) : NULL;
274 }
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000275 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000276
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700277 TIntermSelection *node = new TIntermSelection(
Olli Etuaho7d7f8c42015-05-19 18:38:49 +0300278 cond, ensureSequence(nodePair.node1), ensureSequence(nodePair.node2));
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000279 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000280
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000281 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000282}
283
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700284TIntermTyped *TIntermediate::addComma(
285 TIntermTyped *left, TIntermTyped *right, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000286{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700287 if (left->getType().getQualifier() == EvqConst &&
288 right->getType().getQualifier() == EvqConst)
289 {
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000290 return right;
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700291 }
292 else
293 {
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000294 TIntermTyped *commaAggregate = growAggregate(left, right, line);
alokp@chromium.org58e54292010-08-24 21:40:03 +0000295 commaAggregate->getAsAggregate()->setOp(EOpComma);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000296 commaAggregate->setType(right->getType());
alokp@chromium.org58e54292010-08-24 21:40:03 +0000297 commaAggregate->getTypePointer()->setQualifier(EvqTemporary);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000298 return commaAggregate;
299 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000300}
301
302//
303// For "?:" test nodes. There are three children; a condition,
304// a true path, and a false path. The two paths are specified
305// as separate parameters.
306//
Olli Etuaho52901742015-04-15 13:42:45 +0300307// Returns the selection node created, or one of trueBlock and falseBlock if the expression could be folded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000308//
Olli Etuaho52901742015-04-15 13:42:45 +0300309TIntermTyped *TIntermediate::addSelection(TIntermTyped *cond, TIntermTyped *trueBlock, TIntermTyped *falseBlock,
310 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000311{
Olli Etuaho52901742015-04-15 13:42:45 +0300312 // Right now it's safe to fold ternary operators only when all operands
313 // are constant. If only the condition is constant, it's theoretically
314 // possible to fold the ternary operator, but that requires making sure
315 // that the node returned from here won't be treated as a constant
316 // expression in case the node that gets eliminated was not a constant
317 // expression.
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700318 if (cond->getAsConstantUnion() &&
319 trueBlock->getAsConstantUnion() &&
320 falseBlock->getAsConstantUnion())
321 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +0000322 if (cond->getAsConstantUnion()->getBConst(0))
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000323 return trueBlock;
324 else
325 return falseBlock;
326 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000327
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000328 //
329 // Make a selection node.
330 //
Olli Etuaho52901742015-04-15 13:42:45 +0300331 TIntermSelection *node = new TIntermSelection(cond, trueBlock, falseBlock, trueBlock->getType());
daniel@transgaming.com43affc52012-03-28 14:55:39 +0000332 node->getTypePointer()->setQualifier(EvqTemporary);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000333 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000334
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000335 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000336}
337
Olli Etuahoa3a36662015-02-17 13:46:51 +0200338TIntermSwitch *TIntermediate::addSwitch(
339 TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &line)
340{
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200341 TIntermSwitch *node = new TIntermSwitch(init, statementList);
342 node->setLine(line);
343
344 return node;
Olli Etuahoa3a36662015-02-17 13:46:51 +0200345}
346
347TIntermCase *TIntermediate::addCase(
348 TIntermTyped *condition, const TSourceLoc &line)
349{
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200350 TIntermCase *node = new TIntermCase(condition);
351 node->setLine(line);
352
353 return node;
Olli Etuahoa3a36662015-02-17 13:46:51 +0200354}
355
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000356//
357// Constant terminal nodes. Has a union that contains bool, float or int constants
358//
359// Returns the constant union node created.
360//
361
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700362TIntermConstantUnion *TIntermediate::addConstantUnion(
Jamie Madillb11e2482015-05-04 14:21:22 -0400363 TConstantUnion *constantUnion, const TType &type, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000364{
Jamie Madillb11e2482015-05-04 14:21:22 -0400365 TIntermConstantUnion *node = new TIntermConstantUnion(constantUnion, type);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000366 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000367
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000368 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000369}
370
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700371TIntermTyped *TIntermediate::addSwizzle(
372 TVectorFields &fields, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000373{
374
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700375 TIntermAggregate *node = new TIntermAggregate(EOpSequence);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000376
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000377 node->setLine(line);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700378 TIntermConstantUnion *constIntNode;
379 TIntermSequence *sequenceVector = node->getSequence();
Jamie Madill6ba6ead2015-05-04 14:21:21 -0400380 TConstantUnion *unionArray;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000381
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700382 for (int i = 0; i < fields.num; i++)
383 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -0400384 unionArray = new TConstantUnion[1];
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000385 unionArray->setIConst(fields.offsets[i]);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700386 constIntNode = addConstantUnion(
387 unionArray, TType(EbtInt, EbpUndefined, EvqConst), line);
388 sequenceVector->push_back(constIntNode);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000389 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000390
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000391 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000392}
393
394//
395// Create loop nodes.
396//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700397TIntermNode *TIntermediate::addLoop(
398 TLoopType type, TIntermNode *init, TIntermTyped *cond, TIntermTyped *expr,
399 TIntermNode *body, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000400{
Olli Etuaho7d7f8c42015-05-19 18:38:49 +0300401 TIntermNode *node = new TIntermLoop(type, init, cond, expr, ensureSequence(body));
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000402 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000403
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000404 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000405}
406
407//
408// Add branches.
409//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700410TIntermBranch* TIntermediate::addBranch(
411 TOperator branchOp, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000412{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000413 return addBranch(branchOp, 0, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000414}
415
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700416TIntermBranch* TIntermediate::addBranch(
417 TOperator branchOp, TIntermTyped *expression, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000418{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700419 TIntermBranch *node = new TIntermBranch(branchOp, expression);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000420 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000421
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000422 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000423}
424
425//
426// This is to be executed once the final root is put on top by the parsing
427// process.
428//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700429bool TIntermediate::postProcess(TIntermNode *root)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000430{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700431 if (root == NULL)
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000432 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000433
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000434 //
435 // First, finish off the top level sequence, if any
436 //
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700437 TIntermAggregate *aggRoot = root->getAsAggregate();
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000438 if (aggRoot && aggRoot->getOp() == EOpNull)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000439 aggRoot->setOp(EOpSequence);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000440
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000441 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000442}
Arun Patole274f0702015-05-05 13:33:30 +0530443
Olli Etuahob43846e2015-06-02 18:18:57 +0300444TIntermTyped *TIntermediate::foldAggregateBuiltIn(TIntermAggregate *aggregate)
Arun Patole274f0702015-05-05 13:33:30 +0530445{
Olli Etuahob43846e2015-06-02 18:18:57 +0300446 switch (aggregate->getOp())
Arun Patole274f0702015-05-05 13:33:30 +0530447 {
Arun Patolebf790422015-05-18 17:53:04 +0530448 case EOpAtan:
449 case EOpPow:
450 case EOpMod:
Arun Patole274f0702015-05-05 13:33:30 +0530451 case EOpMin:
452 case EOpMax:
453 case EOpClamp:
Arun Patolebf790422015-05-18 17:53:04 +0530454 case EOpMix:
455 case EOpStep:
456 case EOpSmoothStep:
Arun Patole9d0b1f92015-05-20 14:27:17 +0530457 case EOpLessThan:
458 case EOpLessThanEqual:
459 case EOpGreaterThan:
460 case EOpGreaterThanEqual:
461 case EOpVectorEqual:
462 case EOpVectorNotEqual:
Arun Patole1155ddd2015-06-05 18:04:36 +0530463 case EOpDistance:
464 case EOpDot:
465 case EOpCross:
466 case EOpFaceForward:
467 case EOpReflect:
468 case EOpRefract:
Olli Etuahob43846e2015-06-02 18:18:57 +0300469 return aggregate->fold(mInfoSink);
Arun Patole274f0702015-05-05 13:33:30 +0530470 default:
471 // Constant folding not supported for the built-in.
472 return nullptr;
473 }
474
475 return nullptr;
476}