blob: 81f8aa07f064d67eb7075671fd1c4c5d12823d78 [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 //
61 // See if we can fold constants.
62 //
alokp@chromium.org8f0f24a2010-09-01 21:06:24 +000063 TIntermConstantUnion *leftTempConstant = left->getAsConstantUnion();
64 TIntermConstantUnion *rightTempConstant = right->getAsConstantUnion();
Zhenyao Moe40d1e92014-07-16 17:40:36 -070065 if (leftTempConstant && rightTempConstant)
66 {
67 TIntermTyped *typedReturnNode =
68 leftTempConstant->fold(node->getOp(), rightTempConstant, mInfoSink);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000069
alokp@chromium.org2cf17712010-03-30 20:33:18 +000070 if (typedReturnNode)
71 return typedReturnNode;
72 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000073
alokp@chromium.org2cf17712010-03-30 20:33:18 +000074 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000075}
76
77//
78// Connect two nodes through an assignment.
79//
80// Returns the added node.
81//
Zhenyao Moe40d1e92014-07-16 17:40:36 -070082TIntermTyped *TIntermediate::addAssign(
83 TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000084{
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -040085 if (left->getType().getStruct() || right->getType().getStruct())
86 {
87 if (left->getType() != right->getType())
88 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -070089 return NULL;
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -040090 }
91 }
92
Zhenyao Moe40d1e92014-07-16 17:40:36 -070093 TIntermBinary *node = new TIntermBinary(op);
alokp@chromium.org2cf17712010-03-30 20:33:18 +000094 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095
alokp@chromium.org2cf17712010-03-30 20:33:18 +000096 node->setLeft(left);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -040097 node->setRight(right);
Zhenyao Moe40d1e92014-07-16 17:40:36 -070098 if (!node->promote(mInfoSink))
99 return NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000100
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000101 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102}
103
104//
105// Connect two nodes through an index operator, where the left node is the base
106// of an array or struct, and the right node is a direct or indirect offset.
107//
108// Returns the added node.
109// The caller should set the type of the returned node.
110//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700111TIntermTyped *TIntermediate::addIndex(
112 TOperator op, TIntermTyped *base, TIntermTyped *index, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000113{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700114 TIntermBinary *node = new TIntermBinary(op);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000115 node->setLine(line);
116 node->setLeft(base);
117 node->setRight(index);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000118
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000119 // caller should set the type
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000120
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000121 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000122}
123
124//
125// Add one node as the parent of another that it operates on.
126//
127// Returns the added node.
128//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700129TIntermTyped *TIntermediate::addUnaryMath(
Olli Etuahof6c694b2015-03-26 14:50:53 +0200130 TOperator op, TIntermTyped *child, const TSourceLoc &line, const TType *funcReturnType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000131{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000132 TIntermConstantUnion *childTempConstant = 0;
133 if (child->getAsConstantUnion())
134 childTempConstant = child->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000135
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000136 //
137 // Make a new node for the operator.
138 //
Olli Etuaho69c11b52015-03-26 12:59:00 +0200139 TIntermUnary *node = new TIntermUnary(op);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000140 node->setLine(line);
141 node->setOperand(child);
Olli Etuahof6c694b2015-03-26 14:50:53 +0200142 node->promote(funcReturnType);
Olli Etuaho7700ff62015-01-15 12:16:29 +0200143
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700144 if (childTempConstant)
145 {
Olli Etuaho2cb7b832015-04-23 20:27:44 +0300146 TIntermTyped *newChild = childTempConstant->fold(op, nullptr, mInfoSink);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000147
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000148 if (newChild)
149 return newChild;
150 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000151
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000152 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000153}
154
155//
156// This is the safe way to change the operator on an aggregate, as it
157// does lots of error checking and fixing. Especially for establishing
158// a function call's operation on it's set of parameters. Sequences
159// of instructions are also aggregates, but they just direnctly set
160// their operator to EOpSequence.
161//
162// Returns an aggregate node, which could be the one passed in if
daniel@transgaming.com978702d2012-04-04 15:05:58 +0000163// it was already an aggregate but no operator was set.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000164//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700165TIntermAggregate *TIntermediate::setAggregateOperator(
166 TIntermNode *node, TOperator op, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000167{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700168 TIntermAggregate *aggNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000169
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000170 //
171 // Make sure we have an aggregate. If not turn it into one.
172 //
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700173 if (node)
174 {
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000175 aggNode = node->getAsAggregate();
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700176 if (aggNode == NULL || aggNode->getOp() != EOpNull)
177 {
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000178 //
179 // Make an aggregate containing this node.
180 //
181 aggNode = new TIntermAggregate();
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700182 aggNode->getSequence()->push_back(node);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000183 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700184 }
185 else
186 {
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000187 aggNode = new TIntermAggregate();
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700188 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000189
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000190 //
191 // Set the operator.
192 //
alokp@chromium.org58e54292010-08-24 21:40:03 +0000193 aggNode->setOp(op);
Jamie Madill075edd82013-07-08 13:30:19 -0400194 aggNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000195
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000196 return aggNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000197}
198
199//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000200// Safe way to combine two nodes into an aggregate. Works with null pointers,
201// a node that's not a aggregate yet, etc.
202//
203// Returns the resulting aggregate, unless 0 was passed in for
204// both existing nodes.
205//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700206TIntermAggregate *TIntermediate::growAggregate(
207 TIntermNode *left, TIntermNode *right, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000208{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700209 if (left == NULL && right == NULL)
210 return NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000211
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700212 TIntermAggregate *aggNode = NULL;
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000213 if (left)
214 aggNode = left->getAsAggregate();
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700215 if (!aggNode || aggNode->getOp() != EOpNull)
216 {
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000217 aggNode = new TIntermAggregate;
218 if (left)
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700219 aggNode->getSequence()->push_back(left);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000220 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000221
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000222 if (right)
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700223 aggNode->getSequence()->push_back(right);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000224
Jamie Madill075edd82013-07-08 13:30:19 -0400225 aggNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000226
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000227 return aggNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000228}
229
230//
231// Turn an existing node into an aggregate.
232//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700233// Returns an aggregate, unless NULL was passed in for the existing node.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000234//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700235TIntermAggregate *TIntermediate::makeAggregate(
236 TIntermNode *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000237{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700238 if (node == NULL)
239 return NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000240
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700241 TIntermAggregate *aggNode = new TIntermAggregate;
242 aggNode->getSequence()->push_back(node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000243
Jamie Madill075edd82013-07-08 13:30:19 -0400244 aggNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000245
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000246 return aggNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000247}
248
249//
250// For "if" test nodes. There are three children; a condition,
251// a true path, and a false path. The two paths are in the
252// nodePair.
253//
254// Returns the selection node created.
255//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700256TIntermNode *TIntermediate::addSelection(
257 TIntermTyped *cond, TIntermNodePair nodePair, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000258{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000259 //
260 // For compile time constant selections, prune the code and
261 // test now.
262 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000263
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700264 if (cond->getAsTyped() && cond->getAsTyped()->getAsConstantUnion())
265 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +0000266 if (cond->getAsConstantUnion()->getBConst(0) == true)
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700267 {
268 return nodePair.node1 ? setAggregateOperator(
269 nodePair.node1, EOpSequence, nodePair.node1->getLine()) : NULL;
270 }
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000271 else
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700272 {
273 return nodePair.node2 ? setAggregateOperator(
274 nodePair.node2, EOpSequence, nodePair.node2->getLine()) : NULL;
275 }
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000276 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000277
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700278 TIntermSelection *node = new TIntermSelection(
279 cond, nodePair.node1, nodePair.node2);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000280 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000281
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000282 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000283}
284
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700285TIntermTyped *TIntermediate::addComma(
286 TIntermTyped *left, TIntermTyped *right, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000287{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700288 if (left->getType().getQualifier() == EvqConst &&
289 right->getType().getQualifier() == EvqConst)
290 {
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000291 return right;
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700292 }
293 else
294 {
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000295 TIntermTyped *commaAggregate = growAggregate(left, right, line);
alokp@chromium.org58e54292010-08-24 21:40:03 +0000296 commaAggregate->getAsAggregate()->setOp(EOpComma);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000297 commaAggregate->setType(right->getType());
alokp@chromium.org58e54292010-08-24 21:40:03 +0000298 commaAggregate->getTypePointer()->setQualifier(EvqTemporary);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000299 return commaAggregate;
300 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000301}
302
303//
304// For "?:" test nodes. There are three children; a condition,
305// a true path, and a false path. The two paths are specified
306// as separate parameters.
307//
Olli Etuaho52901742015-04-15 13:42:45 +0300308// 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 +0000309//
Olli Etuaho52901742015-04-15 13:42:45 +0300310TIntermTyped *TIntermediate::addSelection(TIntermTyped *cond, TIntermTyped *trueBlock, TIntermTyped *falseBlock,
311 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000312{
Olli Etuaho52901742015-04-15 13:42:45 +0300313 // Right now it's safe to fold ternary operators only when all operands
314 // are constant. If only the condition is constant, it's theoretically
315 // possible to fold the ternary operator, but that requires making sure
316 // that the node returned from here won't be treated as a constant
317 // expression in case the node that gets eliminated was not a constant
318 // expression.
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700319 if (cond->getAsConstantUnion() &&
320 trueBlock->getAsConstantUnion() &&
321 falseBlock->getAsConstantUnion())
322 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +0000323 if (cond->getAsConstantUnion()->getBConst(0))
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000324 return trueBlock;
325 else
326 return falseBlock;
327 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000328
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000329 //
330 // Make a selection node.
331 //
Olli Etuaho52901742015-04-15 13:42:45 +0300332 TIntermSelection *node = new TIntermSelection(cond, trueBlock, falseBlock, trueBlock->getType());
daniel@transgaming.com43affc52012-03-28 14:55:39 +0000333 node->getTypePointer()->setQualifier(EvqTemporary);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000334 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000335
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000336 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000337}
338
Olli Etuahoa3a36662015-02-17 13:46:51 +0200339TIntermSwitch *TIntermediate::addSwitch(
340 TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &line)
341{
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200342 TIntermSwitch *node = new TIntermSwitch(init, statementList);
343 node->setLine(line);
344
345 return node;
Olli Etuahoa3a36662015-02-17 13:46:51 +0200346}
347
348TIntermCase *TIntermediate::addCase(
349 TIntermTyped *condition, const TSourceLoc &line)
350{
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200351 TIntermCase *node = new TIntermCase(condition);
352 node->setLine(line);
353
354 return node;
Olli Etuahoa3a36662015-02-17 13:46:51 +0200355}
356
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000357//
358// Constant terminal nodes. Has a union that contains bool, float or int constants
359//
360// Returns the constant union node created.
361//
362
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700363TIntermConstantUnion *TIntermediate::addConstantUnion(
Jamie Madill6ba6ead2015-05-04 14:21:21 -0400364 TConstantUnion *unionArrayPointer, const TType &t, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000365{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700366 TIntermConstantUnion *node = new TIntermConstantUnion(unionArrayPointer, t);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000367 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000368
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000369 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000370}
371
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700372TIntermTyped *TIntermediate::addSwizzle(
373 TVectorFields &fields, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000374{
375
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700376 TIntermAggregate *node = new TIntermAggregate(EOpSequence);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000377
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000378 node->setLine(line);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700379 TIntermConstantUnion *constIntNode;
380 TIntermSequence *sequenceVector = node->getSequence();
Jamie Madill6ba6ead2015-05-04 14:21:21 -0400381 TConstantUnion *unionArray;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000382
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700383 for (int i = 0; i < fields.num; i++)
384 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -0400385 unionArray = new TConstantUnion[1];
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000386 unionArray->setIConst(fields.offsets[i]);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700387 constIntNode = addConstantUnion(
388 unionArray, TType(EbtInt, EbpUndefined, EvqConst), line);
389 sequenceVector->push_back(constIntNode);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000390 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000391
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000392 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000393}
394
395//
396// Create loop nodes.
397//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700398TIntermNode *TIntermediate::addLoop(
399 TLoopType type, TIntermNode *init, TIntermTyped *cond, TIntermTyped *expr,
400 TIntermNode *body, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000401{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700402 TIntermNode *node = new TIntermLoop(type, init, cond, expr, body);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000403 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000404
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000405 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000406}
407
408//
409// Add branches.
410//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700411TIntermBranch* TIntermediate::addBranch(
412 TOperator branchOp, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000413{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000414 return addBranch(branchOp, 0, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000415}
416
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700417TIntermBranch* TIntermediate::addBranch(
418 TOperator branchOp, TIntermTyped *expression, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000419{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700420 TIntermBranch *node = new TIntermBranch(branchOp, expression);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000421 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000422
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000423 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000424}
425
426//
427// This is to be executed once the final root is put on top by the parsing
428// process.
429//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700430bool TIntermediate::postProcess(TIntermNode *root)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000431{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700432 if (root == NULL)
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000433 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000434
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000435 //
436 // First, finish off the top level sequence, if any
437 //
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700438 TIntermAggregate *aggRoot = root->getAsAggregate();
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000439 if (aggRoot && aggRoot->getOp() == EOpNull)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000440 aggRoot->setOp(EOpSequence);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000441
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000442 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000443}