blob: 62c6f134bb248bace28d51b0391fada70e7d0109 [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 Etuahob1edc4f2015-11-02 17:20:03 +0200312 TQualifier resultQualifier = EvqTemporary;
313 if (cond->getQualifier() == EvqConst && trueBlock->getQualifier() == EvqConst &&
314 falseBlock->getQualifier() == EvqConst)
315 {
316 resultQualifier = EvqConst;
317 }
Olli Etuaho52901742015-04-15 13:42:45 +0300318 // Right now it's safe to fold ternary operators only when all operands
319 // are constant. If only the condition is constant, it's theoretically
320 // possible to fold the ternary operator, but that requires making sure
321 // that the node returned from here won't be treated as a constant
322 // expression in case the node that gets eliminated was not a constant
323 // expression.
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200324 if (resultQualifier == EvqConst && cond->getAsConstantUnion() &&
325 trueBlock->getAsConstantUnion() && falseBlock->getAsConstantUnion())
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700326 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +0000327 if (cond->getAsConstantUnion()->getBConst(0))
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000328 return trueBlock;
329 else
330 return falseBlock;
331 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000332
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000333 //
334 // Make a selection node.
335 //
Olli Etuaho52901742015-04-15 13:42:45 +0300336 TIntermSelection *node = new TIntermSelection(cond, trueBlock, falseBlock, trueBlock->getType());
Olli Etuahob1edc4f2015-11-02 17:20:03 +0200337 node->getTypePointer()->setQualifier(resultQualifier);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000338 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000339
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000340 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000341}
342
Olli Etuahoa3a36662015-02-17 13:46:51 +0200343TIntermSwitch *TIntermediate::addSwitch(
344 TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &line)
345{
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200346 TIntermSwitch *node = new TIntermSwitch(init, statementList);
347 node->setLine(line);
348
349 return node;
Olli Etuahoa3a36662015-02-17 13:46:51 +0200350}
351
352TIntermCase *TIntermediate::addCase(
353 TIntermTyped *condition, const TSourceLoc &line)
354{
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200355 TIntermCase *node = new TIntermCase(condition);
356 node->setLine(line);
357
358 return node;
Olli Etuahoa3a36662015-02-17 13:46:51 +0200359}
360
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000361//
362// Constant terminal nodes. Has a union that contains bool, float or int constants
363//
364// Returns the constant union node created.
365//
366
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700367TIntermConstantUnion *TIntermediate::addConstantUnion(
Jamie Madillb11e2482015-05-04 14:21:22 -0400368 TConstantUnion *constantUnion, const TType &type, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000369{
Jamie Madillb11e2482015-05-04 14:21:22 -0400370 TIntermConstantUnion *node = new TIntermConstantUnion(constantUnion, type);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000371 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000372
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000373 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000374}
375
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700376TIntermTyped *TIntermediate::addSwizzle(
377 TVectorFields &fields, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000378{
379
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700380 TIntermAggregate *node = new TIntermAggregate(EOpSequence);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000381
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000382 node->setLine(line);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700383 TIntermConstantUnion *constIntNode;
384 TIntermSequence *sequenceVector = node->getSequence();
Jamie Madill6ba6ead2015-05-04 14:21:21 -0400385 TConstantUnion *unionArray;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000386
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700387 for (int i = 0; i < fields.num; i++)
388 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -0400389 unionArray = new TConstantUnion[1];
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000390 unionArray->setIConst(fields.offsets[i]);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700391 constIntNode = addConstantUnion(
392 unionArray, TType(EbtInt, EbpUndefined, EvqConst), line);
393 sequenceVector->push_back(constIntNode);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000394 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000395
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000396 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000397}
398
399//
400// Create loop nodes.
401//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700402TIntermNode *TIntermediate::addLoop(
403 TLoopType type, TIntermNode *init, TIntermTyped *cond, TIntermTyped *expr,
404 TIntermNode *body, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000405{
Olli Etuaho7d7f8c42015-05-19 18:38:49 +0300406 TIntermNode *node = new TIntermLoop(type, init, cond, expr, ensureSequence(body));
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000407 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000408
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000409 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000410}
411
412//
413// Add branches.
414//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700415TIntermBranch* TIntermediate::addBranch(
416 TOperator branchOp, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000417{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000418 return addBranch(branchOp, 0, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000419}
420
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700421TIntermBranch* TIntermediate::addBranch(
422 TOperator branchOp, TIntermTyped *expression, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000423{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700424 TIntermBranch *node = new TIntermBranch(branchOp, expression);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000425 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000426
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000427 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000428}
429
430//
431// This is to be executed once the final root is put on top by the parsing
432// process.
433//
Olli Etuaho43613b02015-08-04 11:02:21 +0300434TIntermAggregate *TIntermediate::postProcess(TIntermNode *root)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000435{
Olli Etuaho43613b02015-08-04 11:02:21 +0300436 if (root == nullptr)
437 return nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000438
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000439 //
Olli Etuaho43613b02015-08-04 11:02:21 +0300440 // Finish off the top level sequence, if any
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000441 //
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700442 TIntermAggregate *aggRoot = root->getAsAggregate();
Olli Etuaho43613b02015-08-04 11:02:21 +0300443 if (aggRoot != nullptr && aggRoot->getOp() == EOpNull)
444 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000445 aggRoot->setOp(EOpSequence);
Olli Etuaho43613b02015-08-04 11:02:21 +0300446 }
447 else if (aggRoot == nullptr || aggRoot->getOp() != EOpSequence)
448 {
449 aggRoot = new TIntermAggregate(EOpSequence);
450 aggRoot->setLine(root->getLine());
451 aggRoot->getSequence()->push_back(root);
452 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000453
Olli Etuaho43613b02015-08-04 11:02:21 +0300454 return aggRoot;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000455}
Arun Patole274f0702015-05-05 13:33:30 +0530456
Olli Etuahob43846e2015-06-02 18:18:57 +0300457TIntermTyped *TIntermediate::foldAggregateBuiltIn(TIntermAggregate *aggregate)
Arun Patole274f0702015-05-05 13:33:30 +0530458{
Olli Etuahob43846e2015-06-02 18:18:57 +0300459 switch (aggregate->getOp())
Arun Patole274f0702015-05-05 13:33:30 +0530460 {
Arun Patolebf790422015-05-18 17:53:04 +0530461 case EOpAtan:
462 case EOpPow:
463 case EOpMod:
Arun Patole274f0702015-05-05 13:33:30 +0530464 case EOpMin:
465 case EOpMax:
466 case EOpClamp:
Arun Patolebf790422015-05-18 17:53:04 +0530467 case EOpMix:
468 case EOpStep:
469 case EOpSmoothStep:
Arun Patole7fa33552015-06-10 15:15:18 +0530470 case EOpMul:
471 case EOpOuterProduct:
Arun Patole9d0b1f92015-05-20 14:27:17 +0530472 case EOpLessThan:
473 case EOpLessThanEqual:
474 case EOpGreaterThan:
475 case EOpGreaterThanEqual:
476 case EOpVectorEqual:
477 case EOpVectorNotEqual:
Arun Patole1155ddd2015-06-05 18:04:36 +0530478 case EOpDistance:
479 case EOpDot:
480 case EOpCross:
481 case EOpFaceForward:
482 case EOpReflect:
483 case EOpRefract:
Olli Etuahob43846e2015-06-02 18:18:57 +0300484 return aggregate->fold(mInfoSink);
Arun Patole274f0702015-05-05 13:33:30 +0530485 default:
486 // Constant folding not supported for the built-in.
487 return nullptr;
488 }
489
490 return nullptr;
491}