blob: 34009ff3641f4115c618eae19017195d3c44c21e [file] [log] [blame]
Jamie Madillb1a85f42014-08-19 15:23:24 -04001//
2// Copyright (c) 2002-2014 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
11#include <float.h>
12#include <limits.h>
13#include <algorithm>
14
15#include "compiler/translator/HashNames.h"
16#include "compiler/translator/IntermNode.h"
17#include "compiler/translator/SymbolTable.h"
18
19namespace
20{
21
22TPrecision GetHigherPrecision(TPrecision left, TPrecision right)
23{
24 return left > right ? left : right;
25}
26
27bool ValidateMultiplication(TOperator op, const TType &left, const TType &right)
28{
29 switch (op)
30 {
31 case EOpMul:
32 case EOpMulAssign:
33 return left.getNominalSize() == right.getNominalSize() &&
34 left.getSecondarySize() == right.getSecondarySize();
35 case EOpVectorTimesScalar:
36 case EOpVectorTimesScalarAssign:
37 return true;
38 case EOpVectorTimesMatrix:
39 return left.getNominalSize() == right.getRows();
40 case EOpVectorTimesMatrixAssign:
41 return left.getNominalSize() == right.getRows() &&
42 left.getNominalSize() == right.getCols();
43 case EOpMatrixTimesVector:
44 return left.getCols() == right.getNominalSize();
45 case EOpMatrixTimesScalar:
46 case EOpMatrixTimesScalarAssign:
47 return true;
48 case EOpMatrixTimesMatrix:
49 return left.getCols() == right.getRows();
50 case EOpMatrixTimesMatrixAssign:
51 return left.getCols() == right.getCols() &&
52 left.getRows() == right.getRows();
53
54 default:
55 UNREACHABLE();
56 return false;
57 }
58}
59
60bool CompareStructure(const TType& leftNodeType,
61 ConstantUnion *rightUnionArray,
62 ConstantUnion *leftUnionArray);
63
64bool CompareStruct(const TType &leftNodeType,
65 ConstantUnion *rightUnionArray,
66 ConstantUnion *leftUnionArray)
67{
68 const TFieldList &fields = leftNodeType.getStruct()->fields();
69
70 size_t structSize = fields.size();
71 size_t index = 0;
72
73 for (size_t j = 0; j < structSize; j++)
74 {
75 size_t size = fields[j]->type()->getObjectSize();
76 for (size_t i = 0; i < size; i++)
77 {
78 if (fields[j]->type()->getBasicType() == EbtStruct)
79 {
80 if (!CompareStructure(*fields[j]->type(),
81 &rightUnionArray[index],
82 &leftUnionArray[index]))
83 {
84 return false;
85 }
86 }
87 else
88 {
89 if (leftUnionArray[index] != rightUnionArray[index])
90 return false;
91 index++;
92 }
93 }
94 }
95 return true;
96}
97
98bool CompareStructure(const TType &leftNodeType,
99 ConstantUnion *rightUnionArray,
100 ConstantUnion *leftUnionArray)
101{
102 if (leftNodeType.isArray())
103 {
104 TType typeWithoutArrayness = leftNodeType;
105 typeWithoutArrayness.clearArrayness();
106
107 size_t arraySize = leftNodeType.getArraySize();
108
109 for (size_t i = 0; i < arraySize; ++i)
110 {
111 size_t offset = typeWithoutArrayness.getObjectSize() * i;
112 if (!CompareStruct(typeWithoutArrayness,
113 &rightUnionArray[offset],
114 &leftUnionArray[offset]))
115 {
116 return false;
117 }
118 }
119 }
120 else
121 {
122 return CompareStruct(leftNodeType, rightUnionArray, leftUnionArray);
123 }
124 return true;
125}
126
127} // namespace anonymous
128
129
130////////////////////////////////////////////////////////////////
131//
132// Member functions of the nodes used for building the tree.
133//
134////////////////////////////////////////////////////////////////
135
Olli Etuahod2a67b92014-10-21 16:42:57 +0300136void TIntermTyped::setTypePreservePrecision(const TType &t)
137{
138 TPrecision precision = getPrecision();
139 mType = t;
140 ASSERT(mType.getBasicType() != EbtBool || precision == EbpUndefined);
141 mType.setPrecision(precision);
142}
143
Jamie Madillb1a85f42014-08-19 15:23:24 -0400144#define REPLACE_IF_IS(node, type, original, replacement) \
145 if (node == original) { \
146 node = static_cast<type *>(replacement); \
147 return true; \
148 }
149
150bool TIntermLoop::replaceChildNode(
151 TIntermNode *original, TIntermNode *replacement)
152{
153 REPLACE_IF_IS(mInit, TIntermNode, original, replacement);
154 REPLACE_IF_IS(mCond, TIntermTyped, original, replacement);
155 REPLACE_IF_IS(mExpr, TIntermTyped, original, replacement);
156 REPLACE_IF_IS(mBody, TIntermNode, original, replacement);
157 return false;
158}
159
Jamie Madillb1a85f42014-08-19 15:23:24 -0400160bool TIntermBranch::replaceChildNode(
161 TIntermNode *original, TIntermNode *replacement)
162{
163 REPLACE_IF_IS(mExpression, TIntermTyped, original, replacement);
164 return false;
165}
166
Jamie Madillb1a85f42014-08-19 15:23:24 -0400167bool TIntermBinary::replaceChildNode(
168 TIntermNode *original, TIntermNode *replacement)
169{
170 REPLACE_IF_IS(mLeft, TIntermTyped, original, replacement);
171 REPLACE_IF_IS(mRight, TIntermTyped, original, replacement);
172 return false;
173}
174
Jamie Madillb1a85f42014-08-19 15:23:24 -0400175bool TIntermUnary::replaceChildNode(
176 TIntermNode *original, TIntermNode *replacement)
177{
178 REPLACE_IF_IS(mOperand, TIntermTyped, original, replacement);
179 return false;
180}
181
Jamie Madillb1a85f42014-08-19 15:23:24 -0400182bool TIntermAggregate::replaceChildNode(
183 TIntermNode *original, TIntermNode *replacement)
184{
185 for (size_t ii = 0; ii < mSequence.size(); ++ii)
186 {
187 REPLACE_IF_IS(mSequence[ii], TIntermNode, original, replacement);
188 }
189 return false;
190}
191
Olli Etuahod2a67b92014-10-21 16:42:57 +0300192void TIntermAggregate::setPrecisionFromChildren()
193{
194 if (getBasicType() == EbtBool)
195 {
196 mType.setPrecision(EbpUndefined);
197 return;
198 }
199
200 TPrecision precision = EbpUndefined;
201 TIntermSequence::iterator childIter = mSequence.begin();
202 while (childIter != mSequence.end())
203 {
204 TIntermTyped *typed = (*childIter)->getAsTyped();
205 if (typed)
206 precision = GetHigherPrecision(typed->getPrecision(), precision);
207 ++childIter;
208 }
209 mType.setPrecision(precision);
210}
211
212void TIntermAggregate::setBuiltInFunctionPrecision()
213{
214 // All built-ins returning bool should be handled as ops, not functions.
215 ASSERT(getBasicType() != EbtBool);
216
217 TPrecision precision = EbpUndefined;
218 TIntermSequence::iterator childIter = mSequence.begin();
219 while (childIter != mSequence.end())
220 {
221 TIntermTyped *typed = (*childIter)->getAsTyped();
222 // ESSL spec section 8: texture functions get their precision from the sampler.
223 if (typed && IsSampler(typed->getBasicType()))
224 {
225 precision = typed->getPrecision();
226 break;
227 }
228 ++childIter;
229 }
230 // ESSL 3.0 spec section 8: textureSize always gets highp precision.
231 // All other functions that take a sampler are assumed to be texture functions.
232 if (mName.find("textureSize") == 0)
233 mType.setPrecision(EbpHigh);
234 else
235 mType.setPrecision(precision);
236}
237
Jamie Madillb1a85f42014-08-19 15:23:24 -0400238bool TIntermSelection::replaceChildNode(
239 TIntermNode *original, TIntermNode *replacement)
240{
241 REPLACE_IF_IS(mCondition, TIntermTyped, original, replacement);
242 REPLACE_IF_IS(mTrueBlock, TIntermNode, original, replacement);
243 REPLACE_IF_IS(mFalseBlock, TIntermNode, original, replacement);
244 return false;
245}
246
Jamie Madillb1a85f42014-08-19 15:23:24 -0400247//
248// Say whether or not an operation node changes the value of a variable.
249//
250bool TIntermOperator::isAssignment() const
251{
252 switch (mOp)
253 {
254 case EOpPostIncrement:
255 case EOpPostDecrement:
256 case EOpPreIncrement:
257 case EOpPreDecrement:
258 case EOpAssign:
259 case EOpAddAssign:
260 case EOpSubAssign:
261 case EOpMulAssign:
262 case EOpVectorTimesMatrixAssign:
263 case EOpVectorTimesScalarAssign:
264 case EOpMatrixTimesScalarAssign:
265 case EOpMatrixTimesMatrixAssign:
266 case EOpDivAssign:
267 return true;
268 default:
269 return false;
270 }
271}
272
273//
274// returns true if the operator is for one of the constructors
275//
276bool TIntermOperator::isConstructor() const
277{
278 switch (mOp)
279 {
280 case EOpConstructVec2:
281 case EOpConstructVec3:
282 case EOpConstructVec4:
283 case EOpConstructMat2:
284 case EOpConstructMat3:
285 case EOpConstructMat4:
286 case EOpConstructFloat:
287 case EOpConstructIVec2:
288 case EOpConstructIVec3:
289 case EOpConstructIVec4:
290 case EOpConstructInt:
291 case EOpConstructUVec2:
292 case EOpConstructUVec3:
293 case EOpConstructUVec4:
294 case EOpConstructUInt:
295 case EOpConstructBVec2:
296 case EOpConstructBVec3:
297 case EOpConstructBVec4:
298 case EOpConstructBool:
299 case EOpConstructStruct:
300 return true;
301 default:
302 return false;
303 }
304}
305
306//
307// Make sure the type of a unary operator is appropriate for its
308// combination of operation and operand type.
309//
310// Returns false in nothing makes sense.
311//
312bool TIntermUnary::promote(TInfoSink &)
313{
314 switch (mOp)
315 {
316 case EOpLogicalNot:
317 if (mOperand->getBasicType() != EbtBool)
318 return false;
319 break;
320 case EOpNegative:
Zhenyao Mode1e00e2014-10-09 16:55:32 -0700321 case EOpPositive:
Jamie Madillb1a85f42014-08-19 15:23:24 -0400322 case EOpPostIncrement:
323 case EOpPostDecrement:
324 case EOpPreIncrement:
325 case EOpPreDecrement:
326 if (mOperand->getBasicType() == EbtBool)
327 return false;
328 break;
329
330 // operators for built-ins are already type checked against their prototype
331 case EOpAny:
332 case EOpAll:
333 case EOpVectorLogicalNot:
334 return true;
335
336 default:
337 if (mOperand->getBasicType() != EbtFloat)
338 return false;
339 }
340
341 setType(mOperand->getType());
342 mType.setQualifier(EvqTemporary);
343
344 return true;
345}
346
347//
348// Establishes the type of the resultant operation, as well as
349// makes the operator the correct one for the operands.
350//
351// Returns false if operator can't work on operands.
352//
353bool TIntermBinary::promote(TInfoSink &infoSink)
354{
355 // This function only handles scalars, vectors, and matrices.
356 if (mLeft->isArray() || mRight->isArray())
357 {
358 infoSink.info.message(EPrefixInternalError, getLine(),
359 "Invalid operation for arrays");
360 return false;
361 }
362
363 // GLSL ES 2.0 does not support implicit type casting.
364 // So the basic type should always match.
365 if (mLeft->getBasicType() != mRight->getBasicType())
366 {
367 return false;
368 }
369
370 //
371 // Base assumption: just make the type the same as the left
372 // operand. Then only deviations from this need be coded.
373 //
374 setType(mLeft->getType());
375
376 // The result gets promoted to the highest precision.
377 TPrecision higherPrecision = GetHigherPrecision(
378 mLeft->getPrecision(), mRight->getPrecision());
379 getTypePointer()->setPrecision(higherPrecision);
380
381 // Binary operations results in temporary variables unless both
382 // operands are const.
383 if (mLeft->getQualifier() != EvqConst || mRight->getQualifier() != EvqConst)
384 {
385 getTypePointer()->setQualifier(EvqTemporary);
386 }
387
388 const int nominalSize =
389 std::max(mLeft->getNominalSize(), mRight->getNominalSize());
390
391 //
392 // All scalars or structs. Code after this test assumes this case is removed!
393 //
394 if (nominalSize == 1)
395 {
396 switch (mOp)
397 {
398 //
399 // Promote to conditional
400 //
401 case EOpEqual:
402 case EOpNotEqual:
403 case EOpLessThan:
404 case EOpGreaterThan:
405 case EOpLessThanEqual:
406 case EOpGreaterThanEqual:
407 setType(TType(EbtBool, EbpUndefined));
408 break;
409
410 //
411 // And and Or operate on conditionals
412 //
413 case EOpLogicalAnd:
414 case EOpLogicalOr:
415 // Both operands must be of type bool.
416 if (mLeft->getBasicType() != EbtBool || mRight->getBasicType() != EbtBool)
417 {
418 return false;
419 }
420 setType(TType(EbtBool, EbpUndefined));
421 break;
422
423 default:
424 break;
425 }
426 return true;
427 }
428
429 // If we reach here, at least one of the operands is vector or matrix.
430 // The other operand could be a scalar, vector, or matrix.
431 // Can these two operands be combined?
432 //
433 TBasicType basicType = mLeft->getBasicType();
434 switch (mOp)
435 {
436 case EOpMul:
437 if (!mLeft->isMatrix() && mRight->isMatrix())
438 {
439 if (mLeft->isVector())
440 {
441 mOp = EOpVectorTimesMatrix;
442 setType(TType(basicType, higherPrecision, EvqTemporary,
443 mRight->getCols(), 1));
444 }
445 else
446 {
447 mOp = EOpMatrixTimesScalar;
448 setType(TType(basicType, higherPrecision, EvqTemporary,
449 mRight->getCols(), mRight->getRows()));
450 }
451 }
452 else if (mLeft->isMatrix() && !mRight->isMatrix())
453 {
454 if (mRight->isVector())
455 {
456 mOp = EOpMatrixTimesVector;
457 setType(TType(basicType, higherPrecision, EvqTemporary,
458 mLeft->getRows(), 1));
459 }
460 else
461 {
462 mOp = EOpMatrixTimesScalar;
463 }
464 }
465 else if (mLeft->isMatrix() && mRight->isMatrix())
466 {
467 mOp = EOpMatrixTimesMatrix;
468 setType(TType(basicType, higherPrecision, EvqTemporary,
469 mRight->getCols(), mLeft->getRows()));
470 }
471 else if (!mLeft->isMatrix() && !mRight->isMatrix())
472 {
473 if (mLeft->isVector() && mRight->isVector())
474 {
475 // leave as component product
476 }
477 else if (mLeft->isVector() || mRight->isVector())
478 {
479 mOp = EOpVectorTimesScalar;
480 setType(TType(basicType, higherPrecision, EvqTemporary,
481 nominalSize, 1));
482 }
483 }
484 else
485 {
486 infoSink.info.message(EPrefixInternalError, getLine(),
487 "Missing elses");
488 return false;
489 }
490
491 if (!ValidateMultiplication(mOp, mLeft->getType(), mRight->getType()))
492 {
493 return false;
494 }
495 break;
496
497 case EOpMulAssign:
498 if (!mLeft->isMatrix() && mRight->isMatrix())
499 {
500 if (mLeft->isVector())
501 {
502 mOp = EOpVectorTimesMatrixAssign;
503 }
504 else
505 {
506 return false;
507 }
508 }
509 else if (mLeft->isMatrix() && !mRight->isMatrix())
510 {
511 if (mRight->isVector())
512 {
513 return false;
514 }
515 else
516 {
517 mOp = EOpMatrixTimesScalarAssign;
518 }
519 }
520 else if (mLeft->isMatrix() && mRight->isMatrix())
521 {
522 mOp = EOpMatrixTimesMatrixAssign;
523 setType(TType(basicType, higherPrecision, EvqTemporary,
524 mRight->getCols(), mLeft->getRows()));
525 }
526 else if (!mLeft->isMatrix() && !mRight->isMatrix())
527 {
528 if (mLeft->isVector() && mRight->isVector())
529 {
530 // leave as component product
531 }
532 else if (mLeft->isVector() || mRight->isVector())
533 {
534 if (!mLeft->isVector())
535 return false;
536 mOp = EOpVectorTimesScalarAssign;
537 setType(TType(basicType, higherPrecision, EvqTemporary,
538 mLeft->getNominalSize(), 1));
539 }
540 }
541 else
542 {
543 infoSink.info.message(EPrefixInternalError, getLine(),
544 "Missing elses");
545 return false;
546 }
547
548 if (!ValidateMultiplication(mOp, mLeft->getType(), mRight->getType()))
549 {
550 return false;
551 }
552 break;
553
554 case EOpAssign:
555 case EOpInitialize:
556 case EOpAdd:
557 case EOpSub:
558 case EOpDiv:
559 case EOpAddAssign:
560 case EOpSubAssign:
561 case EOpDivAssign:
562 if ((mLeft->isMatrix() && mRight->isVector()) ||
563 (mLeft->isVector() && mRight->isMatrix()))
564 {
565 return false;
566 }
567
568 // Are the sizes compatible?
569 if (mLeft->getNominalSize() != mRight->getNominalSize() ||
570 mLeft->getSecondarySize() != mRight->getSecondarySize())
571 {
572 // If the nominal size of operands do not match:
573 // One of them must be scalar.
574 if (!mLeft->isScalar() && !mRight->isScalar())
575 return false;
576
577 // Operator cannot be of type pure assignment.
578 if (mOp == EOpAssign || mOp == EOpInitialize)
579 return false;
580 }
581
582 {
583 const int secondarySize = std::max(
584 mLeft->getSecondarySize(), mRight->getSecondarySize());
585 setType(TType(basicType, higherPrecision, EvqTemporary,
586 nominalSize, secondarySize));
587 }
588 break;
589
590 case EOpEqual:
591 case EOpNotEqual:
592 case EOpLessThan:
593 case EOpGreaterThan:
594 case EOpLessThanEqual:
595 case EOpGreaterThanEqual:
596 if ((mLeft->getNominalSize() != mRight->getNominalSize()) ||
597 (mLeft->getSecondarySize() != mRight->getSecondarySize()))
598 {
599 return false;
600 }
601 setType(TType(EbtBool, EbpUndefined));
602 break;
603
604 default:
605 return false;
606 }
607 return true;
608}
609
610//
611// The fold functions see if an operation on a constant can be done in place,
612// without generating run-time code.
613//
614// Returns the node to keep using, which may or may not be the node passed in.
615//
616TIntermTyped *TIntermConstantUnion::fold(
617 TOperator op, TIntermTyped *constantNode, TInfoSink &infoSink)
618{
619 ConstantUnion *unionArray = getUnionArrayPointer();
620
621 if (!unionArray)
622 return NULL;
623
624 size_t objectSize = getType().getObjectSize();
625
626 if (constantNode)
627 {
628 // binary operations
629 TIntermConstantUnion *node = constantNode->getAsConstantUnion();
630 ConstantUnion *rightUnionArray = node->getUnionArrayPointer();
631 TType returnType = getType();
632
633 if (!rightUnionArray)
634 return NULL;
635
636 // for a case like float f = 1.2 + vec4(2,3,4,5);
637 if (constantNode->getType().getObjectSize() == 1 && objectSize > 1)
638 {
639 rightUnionArray = new ConstantUnion[objectSize];
640 for (size_t i = 0; i < objectSize; ++i)
641 {
642 rightUnionArray[i] = *node->getUnionArrayPointer();
643 }
644 returnType = getType();
645 }
646 else if (constantNode->getType().getObjectSize() > 1 && objectSize == 1)
647 {
648 // for a case like float f = vec4(2,3,4,5) + 1.2;
649 unionArray = new ConstantUnion[constantNode->getType().getObjectSize()];
650 for (size_t i = 0; i < constantNode->getType().getObjectSize(); ++i)
651 {
652 unionArray[i] = *getUnionArrayPointer();
653 }
654 returnType = node->getType();
655 objectSize = constantNode->getType().getObjectSize();
656 }
657
658 ConstantUnion *tempConstArray = NULL;
659 TIntermConstantUnion *tempNode;
660
661 bool boolNodeFlag = false;
662 switch(op)
663 {
664 case EOpAdd:
665 tempConstArray = new ConstantUnion[objectSize];
666 for (size_t i = 0; i < objectSize; i++)
667 tempConstArray[i] = unionArray[i] + rightUnionArray[i];
668 break;
669 case EOpSub:
670 tempConstArray = new ConstantUnion[objectSize];
671 for (size_t i = 0; i < objectSize; i++)
672 tempConstArray[i] = unionArray[i] - rightUnionArray[i];
673 break;
674
675 case EOpMul:
676 case EOpVectorTimesScalar:
677 case EOpMatrixTimesScalar:
678 tempConstArray = new ConstantUnion[objectSize];
679 for (size_t i = 0; i < objectSize; i++)
680 tempConstArray[i] = unionArray[i] * rightUnionArray[i];
681 break;
682
683 case EOpMatrixTimesMatrix:
684 {
685 if (getType().getBasicType() != EbtFloat ||
686 node->getBasicType() != EbtFloat)
687 {
688 infoSink.info.message(
689 EPrefixInternalError, getLine(),
690 "Constant Folding cannot be done for matrix multiply");
691 return NULL;
692 }
693
694 const int leftCols = getCols();
695 const int leftRows = getRows();
696 const int rightCols = constantNode->getType().getCols();
697 const int rightRows = constantNode->getType().getRows();
698 const int resultCols = rightCols;
699 const int resultRows = leftRows;
700
701 tempConstArray = new ConstantUnion[resultCols*resultRows];
702 for (int row = 0; row < resultRows; row++)
703 {
704 for (int column = 0; column < resultCols; column++)
705 {
706 tempConstArray[resultRows * column + row].setFConst(0.0f);
707 for (int i = 0; i < leftCols; i++)
708 {
709 tempConstArray[resultRows * column + row].setFConst(
710 tempConstArray[resultRows * column + row].getFConst() +
711 unionArray[i * leftRows + row].getFConst() *
712 rightUnionArray[column * rightRows + i].getFConst());
713 }
714 }
715 }
716
717 // update return type for matrix product
718 returnType.setPrimarySize(resultCols);
719 returnType.setSecondarySize(resultRows);
720 }
721 break;
722
723 case EOpDiv:
724 {
725 tempConstArray = new ConstantUnion[objectSize];
726 for (size_t i = 0; i < objectSize; i++)
727 {
728 switch (getType().getBasicType())
729 {
730 case EbtFloat:
731 if (rightUnionArray[i] == 0.0f)
732 {
733 infoSink.info.message(
734 EPrefixWarning, getLine(),
735 "Divide by zero error during constant folding");
736 tempConstArray[i].setFConst(
737 unionArray[i].getFConst() < 0 ? -FLT_MAX : FLT_MAX);
738 }
739 else
740 {
741 tempConstArray[i].setFConst(
742 unionArray[i].getFConst() /
743 rightUnionArray[i].getFConst());
744 }
745 break;
746
747 case EbtInt:
748 if (rightUnionArray[i] == 0)
749 {
750 infoSink.info.message(
751 EPrefixWarning, getLine(),
752 "Divide by zero error during constant folding");
753 tempConstArray[i].setIConst(INT_MAX);
754 }
755 else
756 {
757 tempConstArray[i].setIConst(
758 unionArray[i].getIConst() /
759 rightUnionArray[i].getIConst());
760 }
761 break;
762
763 case EbtUInt:
764 if (rightUnionArray[i] == 0)
765 {
766 infoSink.info.message(
767 EPrefixWarning, getLine(),
768 "Divide by zero error during constant folding");
769 tempConstArray[i].setUConst(UINT_MAX);
770 }
771 else
772 {
773 tempConstArray[i].setUConst(
774 unionArray[i].getUConst() /
775 rightUnionArray[i].getUConst());
776 }
777 break;
778
779 default:
780 infoSink.info.message(
781 EPrefixInternalError, getLine(),
782 "Constant folding cannot be done for \"/\"");
783 return NULL;
784 }
785 }
786 }
787 break;
788
789 case EOpMatrixTimesVector:
790 {
791 if (node->getBasicType() != EbtFloat)
792 {
793 infoSink.info.message(
794 EPrefixInternalError, getLine(),
795 "Constant Folding cannot be done for matrix times vector");
796 return NULL;
797 }
798
799 const int matrixCols = getCols();
800 const int matrixRows = getRows();
801
802 tempConstArray = new ConstantUnion[matrixRows];
803
804 for (int matrixRow = 0; matrixRow < matrixRows; matrixRow++)
805 {
806 tempConstArray[matrixRow].setFConst(0.0f);
807 for (int col = 0; col < matrixCols; col++)
808 {
809 tempConstArray[matrixRow].setFConst(
810 tempConstArray[matrixRow].getFConst() +
811 unionArray[col * matrixRows + matrixRow].getFConst() *
812 rightUnionArray[col].getFConst());
813 }
814 }
815
816 returnType = node->getType();
817 returnType.setPrimarySize(matrixRows);
818
819 tempNode = new TIntermConstantUnion(tempConstArray, returnType);
820 tempNode->setLine(getLine());
821
822 return tempNode;
823 }
824
825 case EOpVectorTimesMatrix:
826 {
827 if (getType().getBasicType() != EbtFloat)
828 {
829 infoSink.info.message(
830 EPrefixInternalError, getLine(),
831 "Constant Folding cannot be done for vector times matrix");
832 return NULL;
833 }
834
835 const int matrixCols = constantNode->getType().getCols();
836 const int matrixRows = constantNode->getType().getRows();
837
838 tempConstArray = new ConstantUnion[matrixCols];
839
840 for (int matrixCol = 0; matrixCol < matrixCols; matrixCol++)
841 {
842 tempConstArray[matrixCol].setFConst(0.0f);
843 for (int matrixRow = 0; matrixRow < matrixRows; matrixRow++)
844 {
845 tempConstArray[matrixCol].setFConst(
846 tempConstArray[matrixCol].getFConst() +
847 unionArray[matrixRow].getFConst() *
848 rightUnionArray[matrixCol * matrixRows + matrixRow].getFConst());
849 }
850 }
851
852 returnType.setPrimarySize(matrixCols);
853 }
854 break;
855
856 case EOpLogicalAnd:
857 // this code is written for possible future use,
858 // will not get executed currently
859 {
860 tempConstArray = new ConstantUnion[objectSize];
861 for (size_t i = 0; i < objectSize; i++)
862 {
863 tempConstArray[i] = unionArray[i] && rightUnionArray[i];
864 }
865 }
866 break;
867
868 case EOpLogicalOr:
869 // this code is written for possible future use,
870 // will not get executed currently
871 {
872 tempConstArray = new ConstantUnion[objectSize];
873 for (size_t i = 0; i < objectSize; i++)
874 {
875 tempConstArray[i] = unionArray[i] || rightUnionArray[i];
876 }
877 }
878 break;
879
880 case EOpLogicalXor:
881 {
882 tempConstArray = new ConstantUnion[objectSize];
883 for (size_t i = 0; i < objectSize; i++)
884 {
885 switch (getType().getBasicType())
886 {
887 case EbtBool:
888 tempConstArray[i].setBConst(
889 unionArray[i] == rightUnionArray[i] ? false : true);
890 break;
891 default:
892 UNREACHABLE();
893 break;
894 }
895 }
896 }
897 break;
898
899 case EOpLessThan:
900 ASSERT(objectSize == 1);
901 tempConstArray = new ConstantUnion[1];
902 tempConstArray->setBConst(*unionArray < *rightUnionArray);
903 returnType = TType(EbtBool, EbpUndefined, EvqConst);
904 break;
905
906 case EOpGreaterThan:
907 ASSERT(objectSize == 1);
908 tempConstArray = new ConstantUnion[1];
909 tempConstArray->setBConst(*unionArray > *rightUnionArray);
910 returnType = TType(EbtBool, EbpUndefined, EvqConst);
911 break;
912
913 case EOpLessThanEqual:
914 {
915 ASSERT(objectSize == 1);
916 ConstantUnion constant;
917 constant.setBConst(*unionArray > *rightUnionArray);
918 tempConstArray = new ConstantUnion[1];
919 tempConstArray->setBConst(!constant.getBConst());
920 returnType = TType(EbtBool, EbpUndefined, EvqConst);
921 break;
922 }
923
924 case EOpGreaterThanEqual:
925 {
926 ASSERT(objectSize == 1);
927 ConstantUnion constant;
928 constant.setBConst(*unionArray < *rightUnionArray);
929 tempConstArray = new ConstantUnion[1];
930 tempConstArray->setBConst(!constant.getBConst());
931 returnType = TType(EbtBool, EbpUndefined, EvqConst);
932 break;
933 }
934
935 case EOpEqual:
936 if (getType().getBasicType() == EbtStruct)
937 {
938 if (!CompareStructure(node->getType(),
939 node->getUnionArrayPointer(),
940 unionArray))
941 {
942 boolNodeFlag = true;
943 }
944 }
945 else
946 {
947 for (size_t i = 0; i < objectSize; i++)
948 {
949 if (unionArray[i] != rightUnionArray[i])
950 {
951 boolNodeFlag = true;
952 break; // break out of for loop
953 }
954 }
955 }
956
957 tempConstArray = new ConstantUnion[1];
958 if (!boolNodeFlag)
959 {
960 tempConstArray->setBConst(true);
961 }
962 else
963 {
964 tempConstArray->setBConst(false);
965 }
966
967 tempNode = new TIntermConstantUnion(
968 tempConstArray, TType(EbtBool, EbpUndefined, EvqConst));
969 tempNode->setLine(getLine());
970
971 return tempNode;
972
973 case EOpNotEqual:
974 if (getType().getBasicType() == EbtStruct)
975 {
976 if (CompareStructure(node->getType(),
977 node->getUnionArrayPointer(),
978 unionArray))
979 {
980 boolNodeFlag = true;
981 }
982 }
983 else
984 {
985 for (size_t i = 0; i < objectSize; i++)
986 {
987 if (unionArray[i] == rightUnionArray[i])
988 {
989 boolNodeFlag = true;
990 break; // break out of for loop
991 }
992 }
993 }
994
995 tempConstArray = new ConstantUnion[1];
996 if (!boolNodeFlag)
997 {
998 tempConstArray->setBConst(true);
999 }
1000 else
1001 {
1002 tempConstArray->setBConst(false);
1003 }
1004
1005 tempNode = new TIntermConstantUnion(
1006 tempConstArray, TType(EbtBool, EbpUndefined, EvqConst));
1007 tempNode->setLine(getLine());
1008
1009 return tempNode;
1010
1011 default:
1012 infoSink.info.message(
1013 EPrefixInternalError, getLine(),
1014 "Invalid operator for constant folding");
1015 return NULL;
1016 }
1017 tempNode = new TIntermConstantUnion(tempConstArray, returnType);
1018 tempNode->setLine(getLine());
1019
1020 return tempNode;
1021 }
1022 else
1023 {
1024 //
1025 // Do unary operations
1026 //
1027 TIntermConstantUnion *newNode = 0;
1028 ConstantUnion* tempConstArray = new ConstantUnion[objectSize];
1029 for (size_t i = 0; i < objectSize; i++)
1030 {
1031 switch(op)
1032 {
1033 case EOpNegative:
1034 switch (getType().getBasicType())
1035 {
1036 case EbtFloat:
1037 tempConstArray[i].setFConst(-unionArray[i].getFConst());
1038 break;
1039 case EbtInt:
1040 tempConstArray[i].setIConst(-unionArray[i].getIConst());
1041 break;
1042 case EbtUInt:
1043 tempConstArray[i].setUConst(static_cast<unsigned int>(
1044 -static_cast<int>(unionArray[i].getUConst())));
1045 break;
1046 default:
1047 infoSink.info.message(
1048 EPrefixInternalError, getLine(),
1049 "Unary operation not folded into constant");
1050 return NULL;
1051 }
1052 break;
1053
Zhenyao Mode1e00e2014-10-09 16:55:32 -07001054 case EOpPositive:
1055 switch (getType().getBasicType())
1056 {
1057 case EbtFloat:
1058 tempConstArray[i].setFConst(unionArray[i].getFConst());
1059 break;
1060 case EbtInt:
1061 tempConstArray[i].setIConst(unionArray[i].getIConst());
1062 break;
1063 case EbtUInt:
1064 tempConstArray[i].setUConst(static_cast<unsigned int>(
1065 static_cast<int>(unionArray[i].getUConst())));
1066 break;
1067 default:
1068 infoSink.info.message(
1069 EPrefixInternalError, getLine(),
1070 "Unary operation not folded into constant");
1071 return NULL;
1072 }
1073 break;
1074
Jamie Madillb1a85f42014-08-19 15:23:24 -04001075 case EOpLogicalNot:
1076 // this code is written for possible future use,
1077 // will not get executed currently
1078 switch (getType().getBasicType())
1079 {
1080 case EbtBool:
1081 tempConstArray[i].setBConst(!unionArray[i].getBConst());
1082 break;
1083 default:
1084 infoSink.info.message(
1085 EPrefixInternalError, getLine(),
1086 "Unary operation not folded into constant");
1087 return NULL;
1088 }
1089 break;
1090
1091 default:
1092 return NULL;
1093 }
1094 }
1095 newNode = new TIntermConstantUnion(tempConstArray, getType());
1096 newNode->setLine(getLine());
1097 return newNode;
1098 }
1099}
1100
1101// static
1102TString TIntermTraverser::hash(const TString &name, ShHashFunction64 hashFunction)
1103{
1104 if (hashFunction == NULL || name.empty())
1105 return name;
1106 khronos_uint64_t number = (*hashFunction)(name.c_str(), name.length());
1107 TStringStream stream;
1108 stream << HASHED_NAME_PREFIX << std::hex << number;
1109 TString hashedName = stream.str();
1110 return hashedName;
1111}
Olli Etuaho853dc1a2014-11-06 17:25:48 +02001112
1113void TIntermTraverser::updateTree()
1114{
1115 for (size_t ii = 0; ii < mReplacements.size(); ++ii)
1116 {
1117 const NodeUpdateEntry& entry = mReplacements[ii];
1118 ASSERT(entry.parent);
1119 bool replaced = entry.parent->replaceChildNode(
1120 entry.original, entry.replacement);
1121 ASSERT(replaced);
1122
1123 if (!entry.originalBecomesChildOfReplacement)
1124 {
1125 // In AST traversing, a parent is visited before its children.
1126 // After we replace a node, if an immediate child is to
1127 // be replaced, we need to make sure we don't update the replaced
1128 // node; instead, we update the replacement node.
1129 for (size_t jj = ii + 1; jj < mReplacements.size(); ++jj)
1130 {
1131 NodeUpdateEntry& entry2 = mReplacements[jj];
1132 if (entry2.parent == entry.original)
1133 entry2.parent = entry.replacement;
1134 }
1135 }
1136 }
1137}