blob: b72a2bfc12e5feb02ed758360f027aab6f11c0f2 [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 Etuaho69c11b52015-03-26 12:59:00 +0200130 TOperator op, TIntermTyped *child, const TSourceLoc &line)
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 Etuahodca3e792015-03-26 13:24:04 +0200142 node->promote();
Olli Etuaho7700ff62015-01-15 12:16:29 +0200143
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700144 if (childTempConstant)
145 {
146 TIntermTyped *newChild = childTempConstant->fold(op, 0, 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//
308// Returns the selection node created, or 0 if one could not be.
309//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700310TIntermTyped *TIntermediate::addSelection(
311 TIntermTyped *cond, TIntermTyped *trueBlock, TIntermTyped *falseBlock,
312 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000313{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700314 if (!cond || !trueBlock || !falseBlock ||
315 trueBlock->getType() != falseBlock->getType())
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -0400316 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700317 return NULL;
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000318 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000319
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000320 //
321 // See if all the operands are constant, then fold it otherwise not.
322 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000323
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700324 if (cond->getAsConstantUnion() &&
325 trueBlock->getAsConstantUnion() &&
326 falseBlock->getAsConstantUnion())
327 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +0000328 if (cond->getAsConstantUnion()->getBConst(0))
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000329 return trueBlock;
330 else
331 return falseBlock;
332 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000333
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000334 //
335 // Make a selection node.
336 //
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700337 TIntermSelection *node = new TIntermSelection(
338 cond, trueBlock, falseBlock, trueBlock->getType());
daniel@transgaming.com43affc52012-03-28 14:55:39 +0000339 node->getTypePointer()->setQualifier(EvqTemporary);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000340 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000341
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000342 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000343}
344
Olli Etuahoa3a36662015-02-17 13:46:51 +0200345TIntermSwitch *TIntermediate::addSwitch(
346 TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &line)
347{
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200348 TIntermSwitch *node = new TIntermSwitch(init, statementList);
349 node->setLine(line);
350
351 return node;
Olli Etuahoa3a36662015-02-17 13:46:51 +0200352}
353
354TIntermCase *TIntermediate::addCase(
355 TIntermTyped *condition, const TSourceLoc &line)
356{
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200357 TIntermCase *node = new TIntermCase(condition);
358 node->setLine(line);
359
360 return node;
Olli Etuahoa3a36662015-02-17 13:46:51 +0200361}
362
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000363//
364// Constant terminal nodes. Has a union that contains bool, float or int constants
365//
366// Returns the constant union node created.
367//
368
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700369TIntermConstantUnion *TIntermediate::addConstantUnion(
370 ConstantUnion *unionArrayPointer, const TType &t, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000371{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700372 TIntermConstantUnion *node = new TIntermConstantUnion(unionArrayPointer, t);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000373 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000374
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000375 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000376}
377
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700378TIntermTyped *TIntermediate::addSwizzle(
379 TVectorFields &fields, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000380{
381
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700382 TIntermAggregate *node = new TIntermAggregate(EOpSequence);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000383
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000384 node->setLine(line);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700385 TIntermConstantUnion *constIntNode;
386 TIntermSequence *sequenceVector = node->getSequence();
387 ConstantUnion *unionArray;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000388
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700389 for (int i = 0; i < fields.num; i++)
390 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000391 unionArray = new ConstantUnion[1];
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000392 unionArray->setIConst(fields.offsets[i]);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700393 constIntNode = addConstantUnion(
394 unionArray, TType(EbtInt, EbpUndefined, EvqConst), line);
395 sequenceVector->push_back(constIntNode);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000396 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000397
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000398 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000399}
400
401//
402// Create loop nodes.
403//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700404TIntermNode *TIntermediate::addLoop(
405 TLoopType type, TIntermNode *init, TIntermTyped *cond, TIntermTyped *expr,
406 TIntermNode *body, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000407{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700408 TIntermNode *node = new TIntermLoop(type, init, cond, expr, body);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000409 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000410
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000411 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000412}
413
414//
415// Add branches.
416//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700417TIntermBranch* TIntermediate::addBranch(
418 TOperator branchOp, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000419{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000420 return addBranch(branchOp, 0, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000421}
422
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700423TIntermBranch* TIntermediate::addBranch(
424 TOperator branchOp, TIntermTyped *expression, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000425{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700426 TIntermBranch *node = new TIntermBranch(branchOp, expression);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000427 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000428
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000429 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000430}
431
432//
433// This is to be executed once the final root is put on top by the parsing
434// process.
435//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700436bool TIntermediate::postProcess(TIntermNode *root)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000437{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700438 if (root == NULL)
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000439 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000440
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000441 //
442 // First, finish off the top level sequence, if any
443 //
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700444 TIntermAggregate *aggRoot = root->getAsAggregate();
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000445 if (aggRoot && aggRoot->getOp() == EOpNull)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000446 aggRoot->setOp(EOpSequence);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000447
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000448 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000449}