blob: 5b422334ec589b5e9c3902b0d58dcabc3395be3d [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3// 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>
12
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000013#include "compiler/localintermediate.h"
14#include "compiler/QualifierAlive.h"
15#include "compiler/RemoveTree.h"
16
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000017bool CompareStructure(const TType& leftNodeType, constUnion* rightUnionArray, constUnion* leftUnionArray);
18
19////////////////////////////////////////////////////////////////////////////
20//
21// First set of functions are to help build the intermediate representation.
22// These functions are not member functions of the nodes.
23// They are called from parser productions.
24//
25/////////////////////////////////////////////////////////////////////////////
26
27//
28// Add a terminal node for an identifier in an expression.
29//
30// Returns the added node.
31//
32TIntermSymbol* TIntermediate::addSymbol(int id, const TString& name, const TType& type, TSourceLoc line)
33{
alokp@chromium.org2cf17712010-03-30 20:33:18 +000034 TIntermSymbol* node = new TIntermSymbol(id, name, type);
35 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//
45TIntermTyped* TIntermediate::addBinaryMath(TOperator op, TIntermTyped* left, TIntermTyped* right, TSourceLoc line, TSymbolTable& symbolTable)
46{
alokp@chromium.org2cf17712010-03-30 20:33:18 +000047 switch (op) {
48 case EOpLessThan:
49 case EOpGreaterThan:
50 case EOpLessThanEqual:
51 case EOpGreaterThanEqual:
52 if (left->getType().isMatrix() || left->getType().isArray() || left->getType().isVector() || left->getType().getBasicType() == EbtStruct) {
53 return 0;
54 }
55 break;
56 case EOpLogicalOr:
57 case EOpLogicalXor:
58 case EOpLogicalAnd:
59 if (left->getType().getBasicType() != EbtBool || left->getType().isMatrix() || left->getType().isArray() || left->getType().isVector()) {
60 return 0;
61 }
62 break;
63 case EOpAdd:
64 case EOpSub:
65 case EOpDiv:
66 case EOpMul:
67 if (left->getType().getBasicType() == EbtStruct || left->getType().getBasicType() == EbtBool)
68 return 0;
69 default: break;
70 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000071
alokp@chromium.org2cf17712010-03-30 20:33:18 +000072 //
73 // First try converting the children to compatible types.
74 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000075
alokp@chromium.org2cf17712010-03-30 20:33:18 +000076 if (!(left->getType().getStruct() && right->getType().getStruct())) {
77 TIntermTyped* child = addConversion(op, left->getType(), right);
78 if (child)
79 right = child;
80 else {
81 child = addConversion(op, right->getType(), left);
82 if (child)
83 left = child;
84 else
85 return 0;
86 }
87 } else {
88 if (left->getType() != right->getType())
89 return 0;
90 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000091
92
alokp@chromium.org2cf17712010-03-30 20:33:18 +000093 //
94 // Need a new node holding things together then. Make
95 // one and promote it to the right type.
96 //
97 TIntermBinary* node = new TIntermBinary(op);
98 if (line == 0)
99 line = right->getLine();
100 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000101
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000102 node->setLeft(left);
103 node->setRight(right);
104 if (! node->promote(infoSink))
105 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000107 TIntermConstantUnion *leftTempConstant = left->getAsConstantUnion();
108 TIntermConstantUnion *rightTempConstant = right->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000109
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000110 if (leftTempConstant)
111 leftTempConstant = left->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000112
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000113 if (rightTempConstant)
114 rightTempConstant = right->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000115
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000116 //
117 // See if we can fold constants.
118 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000119
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000120 TIntermTyped* typedReturnNode = 0;
121 if ( leftTempConstant && rightTempConstant) {
122 typedReturnNode = leftTempConstant->fold(node->getOp(), rightTempConstant, infoSink);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000123
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000124 if (typedReturnNode)
125 return typedReturnNode;
126 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000127
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000128 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000129}
130
131//
132// Connect two nodes through an assignment.
133//
134// Returns the added node.
135//
136TIntermTyped* TIntermediate::addAssign(TOperator op, TIntermTyped* left, TIntermTyped* right, TSourceLoc line)
137{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000138 //
139 // Like adding binary math, except the conversion can only go
140 // from right to left.
141 //
142 TIntermBinary* node = new TIntermBinary(op);
143 if (line == 0)
144 line = left->getLine();
145 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000146
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000147 TIntermTyped* child = addConversion(op, left->getType(), right);
148 if (child == 0)
149 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000150
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000151 node->setLeft(left);
152 node->setRight(child);
153 if (! node->promote(infoSink))
154 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000155
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000156 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000157}
158
159//
160// Connect two nodes through an index operator, where the left node is the base
161// of an array or struct, and the right node is a direct or indirect offset.
162//
163// Returns the added node.
164// The caller should set the type of the returned node.
165//
166TIntermTyped* TIntermediate::addIndex(TOperator op, TIntermTyped* base, TIntermTyped* index, TSourceLoc line)
167{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000168 TIntermBinary* node = new TIntermBinary(op);
169 if (line == 0)
170 line = index->getLine();
171 node->setLine(line);
172 node->setLeft(base);
173 node->setRight(index);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000174
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000175 // caller should set the type
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000176
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000177 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000178}
179
180//
181// Add one node as the parent of another that it operates on.
182//
183// Returns the added node.
184//
185TIntermTyped* TIntermediate::addUnaryMath(TOperator op, TIntermNode* childNode, TSourceLoc line, TSymbolTable& symbolTable)
186{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000187 TIntermUnary* node;
188 TIntermTyped* child = childNode->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000189
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000190 if (child == 0) {
191 infoSink.info.message(EPrefixInternalError, "Bad type in AddUnaryMath", line);
192 return 0;
193 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000194
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000195 switch (op) {
196 case EOpLogicalNot:
197 if (child->getType().getBasicType() != EbtBool || child->getType().isMatrix() || child->getType().isArray() || child->getType().isVector()) {
198 return 0;
199 }
200 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000201
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000202 case EOpPostIncrement:
203 case EOpPreIncrement:
204 case EOpPostDecrement:
205 case EOpPreDecrement:
206 case EOpNegative:
207 if (child->getType().getBasicType() == EbtStruct || child->getType().isArray())
208 return 0;
209 default: break;
210 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000211
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000212 //
213 // Do we need to promote the operand?
214 //
215 // Note: Implicit promotions were removed from the language.
216 //
217 TBasicType newType = EbtVoid;
218 switch (op) {
219 case EOpConstructInt: newType = EbtInt; break;
220 case EOpConstructBool: newType = EbtBool; break;
221 case EOpConstructFloat: newType = EbtFloat; break;
222 default: break;
223 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000224
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000225 if (newType != EbtVoid) {
226 child = addConversion(op, TType(newType, EvqTemporary, child->getNominalSize(),
227 child->isMatrix(),
228 child->isArray()),
229 child);
230 if (child == 0)
231 return 0;
232 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000233
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000234 //
235 // For constructors, we are now done, it's all in the conversion.
236 //
237 switch (op) {
238 case EOpConstructInt:
239 case EOpConstructBool:
240 case EOpConstructFloat:
241 return child;
242 default: break;
243 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000244
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000245 TIntermConstantUnion *childTempConstant = 0;
246 if (child->getAsConstantUnion())
247 childTempConstant = child->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000248
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000249 //
250 // Make a new node for the operator.
251 //
252 node = new TIntermUnary(op);
253 if (line == 0)
254 line = child->getLine();
255 node->setLine(line);
256 node->setOperand(child);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000257
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000258 if (! node->promote(infoSink))
259 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000260
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000261 if (childTempConstant) {
262 TIntermTyped* newChild = childTempConstant->fold(op, 0, infoSink);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000263
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000264 if (newChild)
265 return newChild;
266 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000267
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000268 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000269}
270
271//
272// This is the safe way to change the operator on an aggregate, as it
273// does lots of error checking and fixing. Especially for establishing
274// a function call's operation on it's set of parameters. Sequences
275// of instructions are also aggregates, but they just direnctly set
276// their operator to EOpSequence.
277//
278// Returns an aggregate node, which could be the one passed in if
279// it was already an aggregate.
280//
281TIntermAggregate* TIntermediate::setAggregateOperator(TIntermNode* node, TOperator op, TSourceLoc line)
282{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000283 TIntermAggregate* aggNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000284
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000285 //
286 // Make sure we have an aggregate. If not turn it into one.
287 //
288 if (node) {
289 aggNode = node->getAsAggregate();
290 if (aggNode == 0 || aggNode->getOp() != EOpNull) {
291 //
292 // Make an aggregate containing this node.
293 //
294 aggNode = new TIntermAggregate();
295 aggNode->getSequence().push_back(node);
296 if (line == 0)
297 line = node->getLine();
298 }
299 } else
300 aggNode = new TIntermAggregate();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000301
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000302 //
303 // Set the operator.
304 //
305 aggNode->setOperator(op);
306 if (line != 0)
307 aggNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000308
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000309 return aggNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000310}
311
312//
313// Convert one type to another.
314//
315// Returns the node representing the conversion, which could be the same
316// node passed in if no conversion was needed.
317//
318// Return 0 if a conversion can't be done.
319//
320TIntermTyped* TIntermediate::addConversion(TOperator op, const TType& type, TIntermTyped* node)
321{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000322 //
323 // Does the base type allow operation?
324 //
325 switch (node->getBasicType()) {
326 case EbtVoid:
327 case EbtSampler2D:
328 case EbtSamplerCube:
329 return 0;
330 default: break;
331 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000332
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000333 //
334 // Otherwise, if types are identical, no problem
335 //
336 if (type == node->getType())
337 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000338
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000339 //
340 // If one's a structure, then no conversions.
341 //
342 if (type.getStruct() || node->getType().getStruct())
343 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000344
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000345 //
346 // If one's an array, then no conversions.
347 //
348 if (type.isArray() || node->getType().isArray())
349 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000350
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000351 TBasicType promoteTo;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000352
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000353 switch (op) {
354 //
355 // Explicit conversions
356 //
357 case EOpConstructBool:
358 promoteTo = EbtBool;
359 break;
360 case EOpConstructFloat:
361 promoteTo = EbtFloat;
362 break;
363 case EOpConstructInt:
364 promoteTo = EbtInt;
365 break;
366 default:
367 //
368 // implicit conversions were removed from the language.
369 //
370 if (type.getBasicType() != node->getType().getBasicType())
371 return 0;
372 //
373 // Size and structure could still differ, but that's
374 // handled by operator promotion.
375 //
376 return node;
377 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000378
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000379 if (node->getAsConstantUnion()) {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000380
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000381 return (promoteConstantUnion(promoteTo, node->getAsConstantUnion()));
382 } else {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000383
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000384 //
385 // Add a new newNode for the conversion.
386 //
387 TIntermUnary* newNode = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000388
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000389 TOperator newOp = EOpNull;
390 switch (promoteTo) {
391 case EbtFloat:
392 switch (node->getBasicType()) {
393 case EbtInt: newOp = EOpConvIntToFloat; break;
394 case EbtBool: newOp = EOpConvBoolToFloat; break;
395 default:
396 infoSink.info.message(EPrefixInternalError, "Bad promotion node", node->getLine());
397 return 0;
398 }
399 break;
400 case EbtBool:
401 switch (node->getBasicType()) {
402 case EbtInt: newOp = EOpConvIntToBool; break;
403 case EbtFloat: newOp = EOpConvFloatToBool; break;
404 default:
405 infoSink.info.message(EPrefixInternalError, "Bad promotion node", node->getLine());
406 return 0;
407 }
408 break;
409 case EbtInt:
410 switch (node->getBasicType()) {
411 case EbtBool: newOp = EOpConvBoolToInt; break;
412 case EbtFloat: newOp = EOpConvFloatToInt; break;
413 default:
414 infoSink.info.message(EPrefixInternalError, "Bad promotion node", node->getLine());
415 return 0;
416 }
417 break;
418 default:
419 infoSink.info.message(EPrefixInternalError, "Bad promotion type", node->getLine());
420 return 0;
421 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000422
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000423 TType type(promoteTo, EvqTemporary, node->getNominalSize(), node->isMatrix(), node->isArray());
424 newNode = new TIntermUnary(newOp, type);
425 newNode->setLine(node->getLine());
426 newNode->setOperand(node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000427
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000428 return newNode;
429 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000430}
431
432//
433// Safe way to combine two nodes into an aggregate. Works with null pointers,
434// a node that's not a aggregate yet, etc.
435//
436// Returns the resulting aggregate, unless 0 was passed in for
437// both existing nodes.
438//
439TIntermAggregate* TIntermediate::growAggregate(TIntermNode* left, TIntermNode* right, TSourceLoc line)
440{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000441 if (left == 0 && right == 0)
442 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000443
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000444 TIntermAggregate* aggNode = 0;
445 if (left)
446 aggNode = left->getAsAggregate();
447 if (!aggNode || aggNode->getOp() != EOpNull) {
448 aggNode = new TIntermAggregate;
449 if (left)
450 aggNode->getSequence().push_back(left);
451 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000452
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000453 if (right)
454 aggNode->getSequence().push_back(right);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000455
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000456 if (line != 0)
457 aggNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000458
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000459 return aggNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000460}
461
462//
463// Turn an existing node into an aggregate.
464//
465// Returns an aggregate, unless 0 was passed in for the existing node.
466//
467TIntermAggregate* TIntermediate::makeAggregate(TIntermNode* node, TSourceLoc line)
468{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000469 if (node == 0)
470 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000471
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000472 TIntermAggregate* aggNode = new TIntermAggregate;
473 aggNode->getSequence().push_back(node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000474
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000475 if (line != 0)
476 aggNode->setLine(line);
477 else
478 aggNode->setLine(node->getLine());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000479
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000480 return aggNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000481}
482
483//
484// For "if" test nodes. There are three children; a condition,
485// a true path, and a false path. The two paths are in the
486// nodePair.
487//
488// Returns the selection node created.
489//
490TIntermNode* TIntermediate::addSelection(TIntermTyped* cond, TIntermNodePair nodePair, TSourceLoc line)
491{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000492 //
493 // For compile time constant selections, prune the code and
494 // test now.
495 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000496
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000497 if (cond->getAsTyped() && cond->getAsTyped()->getAsConstantUnion()) {
498 if (cond->getAsTyped()->getAsConstantUnion()->getUnionArrayPointer()->getBConst())
499 return nodePair.node1;
500 else
501 return nodePair.node2;
502 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000503
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000504 TIntermSelection* node = new TIntermSelection(cond, nodePair.node1, nodePair.node2);
505 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000506
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000507 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000508}
509
510
511TIntermTyped* TIntermediate::addComma(TIntermTyped* left, TIntermTyped* right, TSourceLoc line)
512{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000513 if (left->getType().getQualifier() == EvqConst && right->getType().getQualifier() == EvqConst) {
514 return right;
515 } else {
516 TIntermTyped *commaAggregate = growAggregate(left, right, line);
517 commaAggregate->getAsAggregate()->setOperator(EOpComma);
518 commaAggregate->setType(right->getType());
519 commaAggregate->getTypePointer()->changeQualifier(EvqTemporary);
520 return commaAggregate;
521 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000522}
523
524//
525// For "?:" test nodes. There are three children; a condition,
526// a true path, and a false path. The two paths are specified
527// as separate parameters.
528//
529// Returns the selection node created, or 0 if one could not be.
530//
531TIntermTyped* TIntermediate::addSelection(TIntermTyped* cond, TIntermTyped* trueBlock, TIntermTyped* falseBlock, TSourceLoc line)
532{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000533 //
534 // Get compatible types.
535 //
536 TIntermTyped* child = addConversion(EOpSequence, trueBlock->getType(), falseBlock);
537 if (child)
538 falseBlock = child;
539 else {
540 child = addConversion(EOpSequence, falseBlock->getType(), trueBlock);
541 if (child)
542 trueBlock = child;
543 else
544 return 0;
545 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000546
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000547 //
548 // See if all the operands are constant, then fold it otherwise not.
549 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000550
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000551 if (cond->getAsConstantUnion() && trueBlock->getAsConstantUnion() && falseBlock->getAsConstantUnion()) {
552 if (cond->getAsConstantUnion()->getUnionArrayPointer()->getBConst())
553 return trueBlock;
554 else
555 return falseBlock;
556 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000557
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000558 //
559 // Make a selection node.
560 //
561 TIntermSelection* node = new TIntermSelection(cond, trueBlock, falseBlock, trueBlock->getType());
562 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000563
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000564 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000565}
566
567//
568// Constant terminal nodes. Has a union that contains bool, float or int constants
569//
570// Returns the constant union node created.
571//
572
573TIntermConstantUnion* TIntermediate::addConstantUnion(constUnion* unionArrayPointer, const TType& t, TSourceLoc line)
574{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000575 TIntermConstantUnion* node = new TIntermConstantUnion(unionArrayPointer, t);
576 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000577
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000578 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000579}
580
581TIntermTyped* TIntermediate::addSwizzle(TVectorFields& fields, TSourceLoc line)
582{
583
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000584 TIntermAggregate* node = new TIntermAggregate(EOpSequence);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000585
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000586 node->setLine(line);
587 TIntermConstantUnion* constIntNode;
588 TIntermSequence &sequenceVector = node->getSequence();
589 constUnion* unionArray;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000590
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000591 for (int i = 0; i < fields.num; i++) {
592 unionArray = new constUnion[1];
593 unionArray->setIConst(fields.offsets[i]);
594 constIntNode = addConstantUnion(unionArray, TType(EbtInt, EvqConst), line);
595 sequenceVector.push_back(constIntNode);
596 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000597
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000598 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000599}
600
601//
602// Create loop nodes.
603//
604TIntermNode* TIntermediate::addLoop(TIntermNode *init, TIntermNode* body, TIntermTyped* test, TIntermTyped* terminal, bool testFirst, TSourceLoc line)
605{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000606 TIntermNode* node = new TIntermLoop(init, body, test, terminal, testFirst);
607 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000608
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000609 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000610}
611
612//
613// Add branches.
614//
615TIntermBranch* TIntermediate::addBranch(TOperator branchOp, TSourceLoc line)
616{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000617 return addBranch(branchOp, 0, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000618}
619
620TIntermBranch* TIntermediate::addBranch(TOperator branchOp, TIntermTyped* expression, TSourceLoc line)
621{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000622 TIntermBranch* node = new TIntermBranch(branchOp, expression);
623 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000624
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000625 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000626}
627
628//
629// This is to be executed once the final root is put on top by the parsing
630// process.
631//
632bool TIntermediate::postProcess(TIntermNode* root, EShLanguage language)
633{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000634 if (root == 0)
635 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000636
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000637 //
638 // First, finish off the top level sequence, if any
639 //
640 TIntermAggregate* aggRoot = root->getAsAggregate();
641 if (aggRoot && aggRoot->getOp() == EOpNull)
642 aggRoot->setOperator(EOpSequence);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000643
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000644 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000645}
646
647//
648// This deletes the tree.
649//
650void TIntermediate::remove(TIntermNode* root)
651{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000652 if (root)
653 RemoveAllTreeNodes(root);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000654}
655
656////////////////////////////////////////////////////////////////
657//
658// Member functions of the nodes used for building the tree.
659//
660////////////////////////////////////////////////////////////////
661
662//
663// Say whether or not an operation node changes the value of a variable.
664//
665// Returns true if state is modified.
666//
667bool TIntermOperator::modifiesState() const
668{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000669 switch (op) {
670 case EOpPostIncrement:
671 case EOpPostDecrement:
672 case EOpPreIncrement:
673 case EOpPreDecrement:
674 case EOpAssign:
675 case EOpAddAssign:
676 case EOpSubAssign:
677 case EOpMulAssign:
678 case EOpVectorTimesMatrixAssign:
679 case EOpVectorTimesScalarAssign:
680 case EOpMatrixTimesScalarAssign:
681 case EOpMatrixTimesMatrixAssign:
682 case EOpDivAssign:
683 return true;
684 default:
685 return false;
686 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000687}
688
689//
690// returns true if the operator is for one of the constructors
691//
692bool TIntermOperator::isConstructor() const
693{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000694 switch (op) {
695 case EOpConstructVec2:
696 case EOpConstructVec3:
697 case EOpConstructVec4:
698 case EOpConstructMat2:
699 case EOpConstructMat3:
700 case EOpConstructMat4:
701 case EOpConstructFloat:
702 case EOpConstructIVec2:
703 case EOpConstructIVec3:
704 case EOpConstructIVec4:
705 case EOpConstructInt:
706 case EOpConstructBVec2:
707 case EOpConstructBVec3:
708 case EOpConstructBVec4:
709 case EOpConstructBool:
710 case EOpConstructStruct:
711 return true;
712 default:
713 return false;
714 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000715}
716//
717// Make sure the type of a unary operator is appropriate for its
718// combination of operation and operand type.
719//
720// Returns false in nothing makes sense.
721//
722bool TIntermUnary::promote(TInfoSink&)
723{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000724 switch (op) {
725 case EOpLogicalNot:
726 if (operand->getBasicType() != EbtBool)
727 return false;
728 break;
729 case EOpNegative:
730 case EOpPostIncrement:
731 case EOpPostDecrement:
732 case EOpPreIncrement:
733 case EOpPreDecrement:
734 if (operand->getBasicType() == EbtBool)
735 return false;
736 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000737
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000738 // operators for built-ins are already type checked against their prototype
739 case EOpAny:
740 case EOpAll:
741 case EOpVectorLogicalNot:
742 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000743
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000744 default:
745 if (operand->getBasicType() != EbtFloat)
746 return false;
747 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000748
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000749 setType(operand->getType());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000750
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000751 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000752}
753
754//
755// Establishes the type of the resultant operation, as well as
756// makes the operator the correct one for the operands.
757//
758// Returns false if operator can't work on operands.
759//
760bool TIntermBinary::promote(TInfoSink& infoSink)
761{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000762 int size = left->getNominalSize();
763 if (right->getNominalSize() > size)
764 size = right->getNominalSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000765
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000766 TBasicType type = left->getBasicType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000767
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000768 //
769 // Arrays have to be exact matches.
770 //
771 if ((left->isArray() || right->isArray()) && (left->getType() != right->getType()))
772 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000773
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000774 //
775 // Base assumption: just make the type the same as the left
776 // operand. Then only deviations from this need be coded.
777 //
daniel@transgaming.comfe565152010-04-10 05:29:07 +0000778 setType(left->getType());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000779
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000780 //
781 // Array operations.
782 //
783 if (left->isArray()) {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000784
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000785 switch (op) {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000786
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000787 //
788 // Promote to conditional
789 //
790 case EOpEqual:
791 case EOpNotEqual:
792 setType(TType(EbtBool));
793 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000794
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000795 //
796 // Set array information.
797 //
798 case EOpAssign:
799 case EOpInitialize:
800 getTypePointer()->setArraySize(left->getType().getArraySize());
801 getTypePointer()->setArrayInformationType(left->getType().getArrayInformationType());
802 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000803
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000804 default:
805 return false;
806 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000807
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000808 return true;
809 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000810
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000811 //
812 // All scalars. Code after this test assumes this case is removed!
813 //
814 if (size == 1) {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000815
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000816 switch (op) {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000817
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000818 //
819 // Promote to conditional
820 //
821 case EOpEqual:
822 case EOpNotEqual:
823 case EOpLessThan:
824 case EOpGreaterThan:
825 case EOpLessThanEqual:
826 case EOpGreaterThanEqual:
827 setType(TType(EbtBool));
828 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000829
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000830 //
831 // And and Or operate on conditionals
832 //
833 case EOpLogicalAnd:
834 case EOpLogicalOr:
835 if (left->getBasicType() != EbtBool || right->getBasicType() != EbtBool)
836 return false;
837 setType(TType(EbtBool));
838 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000839
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000840 //
841 // Everything else should have matching types
842 //
843 default:
844 if (left->getBasicType() != right->getBasicType() ||
845 left->isMatrix() != right->isMatrix())
846 return false;
847 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000848
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000849 return true;
850 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000852 //
853 // Are the sizes compatible?
854 //
855 if ( left->getNominalSize() != size && left->getNominalSize() != 1 ||
856 right->getNominalSize() != size && right->getNominalSize() != 1)
857 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000858
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000859 //
860 // Can these two operands be combined?
861 //
862 switch (op) {
863 case EOpMul:
864 if (!left->isMatrix() && right->isMatrix()) {
865 if (left->isVector())
866 op = EOpVectorTimesMatrix;
867 else {
868 op = EOpMatrixTimesScalar;
869 setType(TType(type, EvqTemporary, size, true));
870 }
871 } else if (left->isMatrix() && !right->isMatrix()) {
872 if (right->isVector()) {
873 op = EOpMatrixTimesVector;
874 setType(TType(type, EvqTemporary, size, false));
875 } else {
876 op = EOpMatrixTimesScalar;
877 }
878 } else if (left->isMatrix() && right->isMatrix()) {
879 op = EOpMatrixTimesMatrix;
880 } else if (!left->isMatrix() && !right->isMatrix()) {
881 if (left->isVector() && right->isVector()) {
882 // leave as component product
883 } else if (left->isVector() || right->isVector()) {
884 op = EOpVectorTimesScalar;
885 setType(TType(type, EvqTemporary, size, false));
886 }
887 } else {
888 infoSink.info.message(EPrefixInternalError, "Missing elses", getLine());
889 return false;
890 }
891 break;
892 case EOpMulAssign:
893 if (!left->isMatrix() && right->isMatrix()) {
894 if (left->isVector())
895 op = EOpVectorTimesMatrixAssign;
896 else {
897 return false;
898 }
899 } else if (left->isMatrix() && !right->isMatrix()) {
900 if (right->isVector()) {
901 return false;
902 } else {
903 op = EOpMatrixTimesScalarAssign;
904 }
905 } else if (left->isMatrix() && right->isMatrix()) {
906 op = EOpMatrixTimesMatrixAssign;
907 } else if (!left->isMatrix() && !right->isMatrix()) {
908 if (left->isVector() && right->isVector()) {
909 // leave as component product
910 } else if (left->isVector() || right->isVector()) {
911 if (! left->isVector())
912 return false;
913 op = EOpVectorTimesScalarAssign;
914 setType(TType(type, EvqTemporary, size, false));
915 }
916 } else {
917 infoSink.info.message(EPrefixInternalError, "Missing elses", getLine());
918 return false;
919 }
920 break;
921 case EOpAssign:
922 case EOpInitialize:
923 if (left->getNominalSize() != right->getNominalSize())
924 return false;
925 // fall through
926 case EOpAdd:
927 case EOpSub:
928 case EOpDiv:
929 case EOpAddAssign:
930 case EOpSubAssign:
931 case EOpDivAssign:
932 if (left->isMatrix() && right->isVector() ||
933 left->isVector() && right->isMatrix() ||
934 left->getBasicType() != right->getBasicType())
935 return false;
936 setType(TType(type, EvqTemporary, size, left->isMatrix() || right->isMatrix()));
937 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000938
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000939 case EOpEqual:
940 case EOpNotEqual:
941 case EOpLessThan:
942 case EOpGreaterThan:
943 case EOpLessThanEqual:
944 case EOpGreaterThanEqual:
945 if (left->isMatrix() && right->isVector() ||
946 left->isVector() && right->isMatrix() ||
947 left->getBasicType() != right->getBasicType())
948 return false;
949 setType(TType(EbtBool));
950 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000951
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000952 default:
953 return false;
954 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000955
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000956 //
957 // One more check for assignment. The Resulting type has to match the left operand.
958 //
959 switch (op) {
960 case EOpAssign:
961 case EOpInitialize:
962 case EOpAddAssign:
963 case EOpSubAssign:
964 case EOpMulAssign:
965 case EOpDivAssign:
966 if (getType() != left->getType())
967 return false;
968 break;
969 default:
970 break;
971 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000973 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000974}
975
976bool CompareStruct(const TType& leftNodeType, constUnion* rightUnionArray, constUnion* leftUnionArray)
977{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000978 TTypeList* fields = leftNodeType.getStruct();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000979
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000980 size_t structSize = fields->size();
981 int index = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000982
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000983 for (size_t j = 0; j < structSize; j++) {
984 int size = (*fields)[j].type->getObjectSize();
985 for (int i = 0; i < size; i++) {
986 if ((*fields)[j].type->getBasicType() == EbtStruct) {
987 if (!CompareStructure(*(*fields)[j].type, &rightUnionArray[index], &leftUnionArray[index]))
988 return false;
989 } else {
990 if (leftUnionArray[index] != rightUnionArray[index])
991 return false;
992 index++;
993 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000994
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000995 }
996 }
997 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000998}
999
1000bool CompareStructure(const TType& leftNodeType, constUnion* rightUnionArray, constUnion* leftUnionArray)
1001{
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001002 if (leftNodeType.isArray()) {
1003 TType typeWithoutArrayness = leftNodeType;
1004 typeWithoutArrayness.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001005
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001006 int arraySize = leftNodeType.getArraySize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001007
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001008 for (int i = 0; i < arraySize; ++i) {
1009 int offset = typeWithoutArrayness.getObjectSize() * i;
1010 if (!CompareStruct(typeWithoutArrayness, &rightUnionArray[offset], &leftUnionArray[offset]))
1011 return false;
1012 }
1013 } else
1014 return CompareStruct(leftNodeType, rightUnionArray, leftUnionArray);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001015
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001016 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001017}
1018
1019//
1020// The fold functions see if an operation on a constant can be done in place,
1021// without generating run-time code.
1022//
1023// Returns the node to keep using, which may or may not be the node passed in.
1024//
1025
1026TIntermTyped* TIntermConstantUnion::fold(TOperator op, TIntermTyped* constantNode, TInfoSink& infoSink)
1027{
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001028 constUnion *unionArray = getUnionArrayPointer();
1029 int objectSize = getType().getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001030
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001031 if (constantNode) { // binary operations
1032 TIntermConstantUnion *node = constantNode->getAsConstantUnion();
1033 constUnion *rightUnionArray = node->getUnionArrayPointer();
1034 TType returnType = getType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001035
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001036 // for a case like float f = 1.2 + vec4(2,3,4,5);
1037 if (constantNode->getType().getObjectSize() == 1 && objectSize > 1) {
1038 rightUnionArray = new constUnion[objectSize];
1039 for (int i = 0; i < objectSize; ++i)
1040 rightUnionArray[i] = *node->getUnionArrayPointer();
1041 returnType = getType();
1042 } else if (constantNode->getType().getObjectSize() > 1 && objectSize == 1) {
1043 // for a case like float f = vec4(2,3,4,5) + 1.2;
1044 unionArray = new constUnion[constantNode->getType().getObjectSize()];
1045 for (int i = 0; i < constantNode->getType().getObjectSize(); ++i)
1046 unionArray[i] = *getUnionArrayPointer();
1047 returnType = node->getType();
1048 objectSize = constantNode->getType().getObjectSize();
1049 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001050
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001051 constUnion* tempConstArray = 0;
1052 TIntermConstantUnion *tempNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001053
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001054 bool boolNodeFlag = false;
1055 switch(op) {
1056 case EOpAdd:
1057 tempConstArray = new constUnion[objectSize];
1058 {// support MSVC++6.0
1059 for (int i = 0; i < objectSize; i++)
1060 tempConstArray[i] = unionArray[i] + rightUnionArray[i];
1061 }
1062 break;
1063 case EOpSub:
1064 tempConstArray = new constUnion[objectSize];
1065 {// support MSVC++6.0
1066 for (int i = 0; i < objectSize; i++)
1067 tempConstArray[i] = unionArray[i] - rightUnionArray[i];
1068 }
1069 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001071 case EOpMul:
1072 case EOpVectorTimesScalar:
1073 case EOpMatrixTimesScalar:
1074 tempConstArray = new constUnion[objectSize];
1075 {// support MSVC++6.0
1076 for (int i = 0; i < objectSize; i++)
1077 tempConstArray[i] = unionArray[i] * rightUnionArray[i];
1078 }
1079 break;
1080 case EOpMatrixTimesMatrix:
1081 if (getType().getBasicType() != EbtFloat || node->getBasicType() != EbtFloat) {
1082 infoSink.info.message(EPrefixInternalError, "Constant Folding cannot be done for matrix multiply", getLine());
1083 return 0;
1084 }
1085 {// support MSVC++6.0
1086 int size = getNominalSize();
1087 tempConstArray = new constUnion[size*size];
1088 for (int row = 0; row < size; row++) {
1089 for (int column = 0; column < size; column++) {
1090 tempConstArray[size * column + row].setFConst(0.0f);
1091 for (int i = 0; i < size; i++) {
1092 tempConstArray[size * column + row].setFConst(tempConstArray[size * column + row].getFConst() + unionArray[i * size + row].getFConst() * (rightUnionArray[column * size + i].getFConst()));
1093 }
1094 }
1095 }
1096 }
1097 break;
1098 case EOpDiv:
1099 tempConstArray = new constUnion[objectSize];
1100 {// support MSVC++6.0
1101 for (int i = 0; i < objectSize; i++) {
1102 switch (getType().getBasicType()) {
1103 case EbtFloat:
1104 if (rightUnionArray[i] == 0.0f) {
1105 infoSink.info.message(EPrefixWarning, "Divide by zero error during constant folding", getLine());
1106 tempConstArray[i].setFConst(FLT_MAX);
1107 } else
1108 tempConstArray[i].setFConst(unionArray[i].getFConst() / rightUnionArray[i].getFConst());
1109 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001110
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001111 case EbtInt:
1112 if (rightUnionArray[i] == 0) {
1113 infoSink.info.message(EPrefixWarning, "Divide by zero error during constant folding", getLine());
1114 tempConstArray[i].setIConst(INT_MAX);
1115 } else
1116 tempConstArray[i].setIConst(unionArray[i].getIConst() / rightUnionArray[i].getIConst());
1117 break;
1118 default:
1119 infoSink.info.message(EPrefixInternalError, "Constant folding cannot be done for \"/\"", getLine());
1120 return 0;
1121 }
1122 }
1123 }
1124 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001125
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001126 case EOpMatrixTimesVector:
1127 if (node->getBasicType() != EbtFloat) {
1128 infoSink.info.message(EPrefixInternalError, "Constant Folding cannot be done for matrix times vector", getLine());
1129 return 0;
1130 }
1131 tempConstArray = new constUnion[getNominalSize()];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001132
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001133 {// support MSVC++6.0
1134 for (int size = getNominalSize(), i = 0; i < size; i++) {
1135 tempConstArray[i].setFConst(0.0f);
1136 for (int j = 0; j < size; j++) {
1137 tempConstArray[i].setFConst(tempConstArray[i].getFConst() + ((unionArray[j*size + i].getFConst()) * rightUnionArray[j].getFConst()));
1138 }
1139 }
1140 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001141
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001142 tempNode = new TIntermConstantUnion(tempConstArray, node->getType());
1143 tempNode->setLine(getLine());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001144
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001145 return tempNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001146
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001147 case EOpVectorTimesMatrix:
1148 if (getType().getBasicType() != EbtFloat) {
1149 infoSink.info.message(EPrefixInternalError, "Constant Folding cannot be done for vector times matrix", getLine());
1150 return 0;
1151 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001152
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001153 tempConstArray = new constUnion[getNominalSize()];
1154 {// support MSVC++6.0
1155 for (int size = getNominalSize(), i = 0; i < size; i++) {
1156 tempConstArray[i].setFConst(0.0f);
1157 for (int j = 0; j < size; j++) {
1158 tempConstArray[i].setFConst(tempConstArray[i].getFConst() + ((unionArray[j].getFConst()) * rightUnionArray[i*size + j].getFConst()));
1159 }
1160 }
1161 }
1162 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001163
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001164 case EOpLogicalAnd: // this code is written for possible future use, will not get executed currently
1165 tempConstArray = new constUnion[objectSize];
1166 {// support MSVC++6.0
1167 for (int i = 0; i < objectSize; i++)
1168 tempConstArray[i] = unionArray[i] && rightUnionArray[i];
1169 }
1170 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001171
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001172 case EOpLogicalOr: // this code is written for possible future use, will not get executed currently
1173 tempConstArray = new constUnion[objectSize];
1174 {// support MSVC++6.0
1175 for (int i = 0; i < objectSize; i++)
1176 tempConstArray[i] = unionArray[i] || rightUnionArray[i];
1177 }
1178 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001179
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001180 case EOpLogicalXor:
1181 tempConstArray = new constUnion[objectSize];
1182 {// support MSVC++6.0
1183 for (int i = 0; i < objectSize; i++)
1184 switch (getType().getBasicType()) {
1185 case EbtBool: tempConstArray[i].setBConst((unionArray[i] == rightUnionArray[i]) ? false : true); break;
1186 default: assert(false && "Default missing");
1187 }
1188 }
1189 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001190
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001191 case EOpLessThan:
1192 assert(objectSize == 1);
1193 tempConstArray = new constUnion[1];
1194 tempConstArray->setBConst(*unionArray < *rightUnionArray);
1195 returnType = TType(EbtBool, EvqConst);
1196 break;
1197 case EOpGreaterThan:
1198 assert(objectSize == 1);
1199 tempConstArray = new constUnion[1];
1200 tempConstArray->setBConst(*unionArray > *rightUnionArray);
1201 returnType = TType(EbtBool, EvqConst);
1202 break;
1203 case EOpLessThanEqual:
1204 {
1205 assert(objectSize == 1);
1206 constUnion constant;
1207 constant.setBConst(*unionArray > *rightUnionArray);
1208 tempConstArray = new constUnion[1];
1209 tempConstArray->setBConst(!constant.getBConst());
1210 returnType = TType(EbtBool, EvqConst);
1211 break;
1212 }
1213 case EOpGreaterThanEqual:
1214 {
1215 assert(objectSize == 1);
1216 constUnion constant;
1217 constant.setBConst(*unionArray < *rightUnionArray);
1218 tempConstArray = new constUnion[1];
1219 tempConstArray->setBConst(!constant.getBConst());
1220 returnType = TType(EbtBool, EvqConst);
1221 break;
1222 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001223
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001224 case EOpEqual:
1225 if (getType().getBasicType() == EbtStruct) {
1226 if (!CompareStructure(node->getType(), node->getUnionArrayPointer(), unionArray))
1227 boolNodeFlag = true;
1228 } else {
1229 for (int i = 0; i < objectSize; i++) {
1230 if (unionArray[i] != rightUnionArray[i]) {
1231 boolNodeFlag = true;
1232 break; // break out of for loop
1233 }
1234 }
1235 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001236
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001237 tempConstArray = new constUnion[1];
1238 if (!boolNodeFlag) {
1239 tempConstArray->setBConst(true);
1240 }
1241 else {
1242 tempConstArray->setBConst(false);
1243 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001244
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001245 tempNode = new TIntermConstantUnion(tempConstArray, TType(EbtBool, EvqConst));
1246 tempNode->setLine(getLine());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001247
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001248 return tempNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001249
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001250 case EOpNotEqual:
1251 if (getType().getBasicType() == EbtStruct) {
1252 if (CompareStructure(node->getType(), node->getUnionArrayPointer(), unionArray))
1253 boolNodeFlag = true;
1254 } else {
1255 for (int i = 0; i < objectSize; i++) {
1256 if (unionArray[i] == rightUnionArray[i]) {
1257 boolNodeFlag = true;
1258 break; // break out of for loop
1259 }
1260 }
1261 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001262
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001263 tempConstArray = new constUnion[1];
1264 if (!boolNodeFlag) {
1265 tempConstArray->setBConst(true);
1266 }
1267 else {
1268 tempConstArray->setBConst(false);
1269 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001270
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001271 tempNode = new TIntermConstantUnion(tempConstArray, TType(EbtBool, EvqConst));
1272 tempNode->setLine(getLine());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001273
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001274 return tempNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001275
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001276 default:
1277 infoSink.info.message(EPrefixInternalError, "Invalid operator for constant folding", getLine());
1278 return 0;
1279 }
1280 tempNode = new TIntermConstantUnion(tempConstArray, returnType);
1281 tempNode->setLine(getLine());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001282
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001283 return tempNode;
1284 } else {
1285 //
1286 // Do unary operations
1287 //
1288 TIntermConstantUnion *newNode = 0;
1289 constUnion* tempConstArray = new constUnion[objectSize];
1290 for (int i = 0; i < objectSize; i++) {
1291 switch(op) {
1292 case EOpNegative:
1293 switch (getType().getBasicType()) {
1294 case EbtFloat: tempConstArray[i].setFConst(-unionArray[i].getFConst()); break;
1295 case EbtInt: tempConstArray[i].setIConst(-unionArray[i].getIConst()); break;
1296 default:
1297 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1298 return 0;
1299 }
1300 break;
1301 case EOpLogicalNot: // this code is written for possible future use, will not get executed currently
1302 switch (getType().getBasicType()) {
1303 case EbtBool: tempConstArray[i].setBConst(!unionArray[i].getBConst()); break;
1304 default:
1305 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1306 return 0;
1307 }
1308 break;
1309 default:
1310 return 0;
1311 }
1312 }
1313 newNode = new TIntermConstantUnion(tempConstArray, getType());
1314 newNode->setLine(getLine());
1315 return newNode;
1316 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001317
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001318 return this;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001319}
1320
1321TIntermTyped* TIntermediate::promoteConstantUnion(TBasicType promoteTo, TIntermConstantUnion* node)
1322{
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001323 constUnion *rightUnionArray = node->getUnionArrayPointer();
1324 int size = node->getType().getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001325
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001326 constUnion *leftUnionArray = new constUnion[size];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001327
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001328 for (int i=0; i < size; i++) {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001329
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001330 switch (promoteTo) {
1331 case EbtFloat:
1332 switch (node->getType().getBasicType()) {
1333 case EbtInt:
1334 leftUnionArray[i].setFConst(static_cast<float>(rightUnionArray[i].getIConst()));
1335 break;
1336 case EbtBool:
1337 leftUnionArray[i].setFConst(static_cast<float>(rightUnionArray[i].getBConst()));
1338 break;
1339 case EbtFloat:
1340 leftUnionArray[i] = rightUnionArray[i];
1341 break;
1342 default:
1343 infoSink.info.message(EPrefixInternalError, "Cannot promote", node->getLine());
1344 return 0;
1345 }
1346 break;
1347 case EbtInt:
1348 switch (node->getType().getBasicType()) {
1349 case EbtInt:
1350 leftUnionArray[i] = rightUnionArray[i];
1351 break;
1352 case EbtBool:
1353 leftUnionArray[i].setIConst(static_cast<int>(rightUnionArray[i].getBConst()));
1354 break;
1355 case EbtFloat:
1356 leftUnionArray[i].setIConst(static_cast<int>(rightUnionArray[i].getFConst()));
1357 break;
1358 default:
1359 infoSink.info.message(EPrefixInternalError, "Cannot promote", node->getLine());
1360 return 0;
1361 }
1362 break;
1363 case EbtBool:
1364 switch (node->getType().getBasicType()) {
1365 case EbtInt:
1366 leftUnionArray[i].setBConst(rightUnionArray[i].getIConst() != 0);
1367 break;
1368 case EbtBool:
1369 leftUnionArray[i] = rightUnionArray[i];
1370 break;
1371 case EbtFloat:
1372 leftUnionArray[i].setBConst(rightUnionArray[i].getFConst() != 0.0f);
1373 break;
1374 default:
1375 infoSink.info.message(EPrefixInternalError, "Cannot promote", node->getLine());
1376 return 0;
1377 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001378
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001379 break;
1380 default:
1381 infoSink.info.message(EPrefixInternalError, "Incorrect data type found", node->getLine());
1382 return 0;
1383 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001384
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001385 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001386
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001387 const TType& t = node->getType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001388
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001389 return addConstantUnion(leftUnionArray, TType(promoteTo, t.getQualifier(), t.getNominalSize(), t.isMatrix(), t.isArray()), node->getLine());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001390}
1391
1392void TIntermAggregate::addToPragmaTable(const TPragmaTable& pTable)
1393{
alokp@chromium.org2cf17712010-03-30 20:33:18 +00001394 assert(!pragmaTable);
1395 pragmaTable = new TPragmaTable();
1396 *pragmaTable = pTable;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001397}