blob: 12fb5bdf4f963f4fbc10815f6715609625aa2d7c [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);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000142
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700143 if (!node->promote(mInfoSink))
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000144 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000145
Olli Etuaho7700ff62015-01-15 12:16:29 +0200146 switch (op)
147 {
Olli Etuaho5e5c8262015-03-26 14:04:54 +0200148 case EOpFloatBitsToInt:
149 case EOpFloatBitsToUint:
150 case EOpIntBitsToFloat:
151 case EOpUintBitsToFloat:
Olli Etuaho7700ff62015-01-15 12:16:29 +0200152 case EOpPackSnorm2x16:
153 case EOpPackUnorm2x16:
154 case EOpPackHalf2x16:
155 case EOpUnpackSnorm2x16:
156 case EOpUnpackUnorm2x16:
157 node->getTypePointer()->setPrecision(EbpHigh);
158 break;
159 case EOpUnpackHalf2x16:
160 node->getTypePointer()->setPrecision(EbpMedium);
161 break;
162 default:
163 break;
164 }
165
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700166 if (childTempConstant)
167 {
168 TIntermTyped *newChild = childTempConstant->fold(op, 0, mInfoSink);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000169
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000170 if (newChild)
171 return newChild;
172 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000173
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000174 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000175}
176
177//
178// This is the safe way to change the operator on an aggregate, as it
179// does lots of error checking and fixing. Especially for establishing
180// a function call's operation on it's set of parameters. Sequences
181// of instructions are also aggregates, but they just direnctly set
182// their operator to EOpSequence.
183//
184// Returns an aggregate node, which could be the one passed in if
daniel@transgaming.com978702d2012-04-04 15:05:58 +0000185// it was already an aggregate but no operator was set.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000186//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700187TIntermAggregate *TIntermediate::setAggregateOperator(
188 TIntermNode *node, TOperator op, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000189{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700190 TIntermAggregate *aggNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000191
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000192 //
193 // Make sure we have an aggregate. If not turn it into one.
194 //
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700195 if (node)
196 {
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000197 aggNode = node->getAsAggregate();
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700198 if (aggNode == NULL || aggNode->getOp() != EOpNull)
199 {
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000200 //
201 // Make an aggregate containing this node.
202 //
203 aggNode = new TIntermAggregate();
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700204 aggNode->getSequence()->push_back(node);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000205 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700206 }
207 else
208 {
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000209 aggNode = new TIntermAggregate();
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700210 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000211
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000212 //
213 // Set the operator.
214 //
alokp@chromium.org58e54292010-08-24 21:40:03 +0000215 aggNode->setOp(op);
Jamie Madill075edd82013-07-08 13:30:19 -0400216 aggNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000217
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000218 return aggNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000219}
220
221//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000222// Safe way to combine two nodes into an aggregate. Works with null pointers,
223// a node that's not a aggregate yet, etc.
224//
225// Returns the resulting aggregate, unless 0 was passed in for
226// both existing nodes.
227//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700228TIntermAggregate *TIntermediate::growAggregate(
229 TIntermNode *left, TIntermNode *right, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000230{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700231 if (left == NULL && right == NULL)
232 return NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000233
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700234 TIntermAggregate *aggNode = NULL;
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000235 if (left)
236 aggNode = left->getAsAggregate();
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700237 if (!aggNode || aggNode->getOp() != EOpNull)
238 {
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000239 aggNode = new TIntermAggregate;
240 if (left)
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700241 aggNode->getSequence()->push_back(left);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000242 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000243
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000244 if (right)
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700245 aggNode->getSequence()->push_back(right);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000246
Jamie Madill075edd82013-07-08 13:30:19 -0400247 aggNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000248
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000249 return aggNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000250}
251
252//
253// Turn an existing node into an aggregate.
254//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700255// Returns an aggregate, unless NULL was passed in for the existing node.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000256//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700257TIntermAggregate *TIntermediate::makeAggregate(
258 TIntermNode *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000259{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700260 if (node == NULL)
261 return NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000262
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700263 TIntermAggregate *aggNode = new TIntermAggregate;
264 aggNode->getSequence()->push_back(node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000265
Jamie Madill075edd82013-07-08 13:30:19 -0400266 aggNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000267
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000268 return aggNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000269}
270
271//
272// For "if" test nodes. There are three children; a condition,
273// a true path, and a false path. The two paths are in the
274// nodePair.
275//
276// Returns the selection node created.
277//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700278TIntermNode *TIntermediate::addSelection(
279 TIntermTyped *cond, TIntermNodePair nodePair, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000280{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000281 //
282 // For compile time constant selections, prune the code and
283 // test now.
284 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000285
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700286 if (cond->getAsTyped() && cond->getAsTyped()->getAsConstantUnion())
287 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +0000288 if (cond->getAsConstantUnion()->getBConst(0) == true)
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700289 {
290 return nodePair.node1 ? setAggregateOperator(
291 nodePair.node1, EOpSequence, nodePair.node1->getLine()) : NULL;
292 }
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000293 else
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700294 {
295 return nodePair.node2 ? setAggregateOperator(
296 nodePair.node2, EOpSequence, nodePair.node2->getLine()) : NULL;
297 }
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000298 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000299
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700300 TIntermSelection *node = new TIntermSelection(
301 cond, nodePair.node1, nodePair.node2);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000302 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000303
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000304 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305}
306
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700307TIntermTyped *TIntermediate::addComma(
308 TIntermTyped *left, TIntermTyped *right, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000309{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700310 if (left->getType().getQualifier() == EvqConst &&
311 right->getType().getQualifier() == EvqConst)
312 {
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000313 return right;
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700314 }
315 else
316 {
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000317 TIntermTyped *commaAggregate = growAggregate(left, right, line);
alokp@chromium.org58e54292010-08-24 21:40:03 +0000318 commaAggregate->getAsAggregate()->setOp(EOpComma);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000319 commaAggregate->setType(right->getType());
alokp@chromium.org58e54292010-08-24 21:40:03 +0000320 commaAggregate->getTypePointer()->setQualifier(EvqTemporary);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000321 return commaAggregate;
322 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000323}
324
325//
326// For "?:" test nodes. There are three children; a condition,
327// a true path, and a false path. The two paths are specified
328// as separate parameters.
329//
330// Returns the selection node created, or 0 if one could not be.
331//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700332TIntermTyped *TIntermediate::addSelection(
333 TIntermTyped *cond, TIntermTyped *trueBlock, TIntermTyped *falseBlock,
334 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000335{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700336 if (!cond || !trueBlock || !falseBlock ||
337 trueBlock->getType() != falseBlock->getType())
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -0400338 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700339 return NULL;
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000340 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000341
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000342 //
343 // See if all the operands are constant, then fold it otherwise not.
344 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000345
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700346 if (cond->getAsConstantUnion() &&
347 trueBlock->getAsConstantUnion() &&
348 falseBlock->getAsConstantUnion())
349 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +0000350 if (cond->getAsConstantUnion()->getBConst(0))
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000351 return trueBlock;
352 else
353 return falseBlock;
354 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000355
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000356 //
357 // Make a selection node.
358 //
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700359 TIntermSelection *node = new TIntermSelection(
360 cond, trueBlock, falseBlock, trueBlock->getType());
daniel@transgaming.com43affc52012-03-28 14:55:39 +0000361 node->getTypePointer()->setQualifier(EvqTemporary);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000362 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000363
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000364 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000365}
366
Olli Etuahoa3a36662015-02-17 13:46:51 +0200367TIntermSwitch *TIntermediate::addSwitch(
368 TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &line)
369{
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200370 TIntermSwitch *node = new TIntermSwitch(init, statementList);
371 node->setLine(line);
372
373 return node;
Olli Etuahoa3a36662015-02-17 13:46:51 +0200374}
375
376TIntermCase *TIntermediate::addCase(
377 TIntermTyped *condition, const TSourceLoc &line)
378{
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200379 TIntermCase *node = new TIntermCase(condition);
380 node->setLine(line);
381
382 return node;
Olli Etuahoa3a36662015-02-17 13:46:51 +0200383}
384
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000385//
386// Constant terminal nodes. Has a union that contains bool, float or int constants
387//
388// Returns the constant union node created.
389//
390
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700391TIntermConstantUnion *TIntermediate::addConstantUnion(
392 ConstantUnion *unionArrayPointer, const TType &t, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000393{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700394 TIntermConstantUnion *node = new TIntermConstantUnion(unionArrayPointer, t);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000395 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000396
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000397 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000398}
399
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700400TIntermTyped *TIntermediate::addSwizzle(
401 TVectorFields &fields, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000402{
403
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700404 TIntermAggregate *node = new TIntermAggregate(EOpSequence);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000405
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000406 node->setLine(line);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700407 TIntermConstantUnion *constIntNode;
408 TIntermSequence *sequenceVector = node->getSequence();
409 ConstantUnion *unionArray;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000410
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700411 for (int i = 0; i < fields.num; i++)
412 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000413 unionArray = new ConstantUnion[1];
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000414 unionArray->setIConst(fields.offsets[i]);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700415 constIntNode = addConstantUnion(
416 unionArray, TType(EbtInt, EbpUndefined, EvqConst), line);
417 sequenceVector->push_back(constIntNode);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000418 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000419
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000420 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000421}
422
423//
424// Create loop nodes.
425//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700426TIntermNode *TIntermediate::addLoop(
427 TLoopType type, TIntermNode *init, TIntermTyped *cond, TIntermTyped *expr,
428 TIntermNode *body, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000429{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700430 TIntermNode *node = new TIntermLoop(type, init, cond, expr, body);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000431 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000432
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000433 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000434}
435
436//
437// Add branches.
438//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700439TIntermBranch* TIntermediate::addBranch(
440 TOperator branchOp, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000441{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000442 return addBranch(branchOp, 0, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000443}
444
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700445TIntermBranch* TIntermediate::addBranch(
446 TOperator branchOp, TIntermTyped *expression, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000447{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700448 TIntermBranch *node = new TIntermBranch(branchOp, expression);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000449 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000450
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000451 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000452}
453
454//
455// This is to be executed once the final root is put on top by the parsing
456// process.
457//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700458bool TIntermediate::postProcess(TIntermNode *root)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000459{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700460 if (root == NULL)
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000461 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000462
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000463 //
464 // First, finish off the top level sequence, if any
465 //
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700466 TIntermAggregate *aggRoot = root->getAsAggregate();
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000467 if (aggRoot && aggRoot->getOp() == EOpNull)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000468 aggRoot->setOp(EOpSequence);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000469
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000470 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000471}