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.
8 // Build the intermediate representation.
15 #include "compiler/localintermediate.h"
16 #include "compiler/QualifierAlive.h"
17 #include "compiler/RemoveTree.h"
19 bool CompareStructure(const TType& leftNodeType, ConstantUnion* rightUnionArray, ConstantUnion* leftUnionArray);
21 static TPrecision GetHigherPrecision( TPrecision left, TPrecision right ){
22 return left > right ? left : right;
25 const char* getOperatorString(TOperator op) {
27 case EOpInitialize: return "=";
28 case EOpAssign: return "=";
29 case EOpAddAssign: return "+=";
30 case EOpSubAssign: return "-=";
31 case EOpDivAssign: return "/=";
35 case EOpVectorTimesMatrixAssign:
36 case EOpVectorTimesScalarAssign:
37 case EOpMatrixTimesScalarAssign:
38 case EOpMatrixTimesMatrixAssign: return "*=";
42 case EOpIndexIndirect: return "[]";
44 case EOpIndexDirectStruct: return ".";
45 case EOpVectorSwizzle: return ".";
46 case EOpAdd: return "+";
47 case EOpSub: return "-";
48 case EOpMul: return "*";
49 case EOpDiv: return "/";
50 case EOpMod: UNIMPLEMENTED(); break;
51 case EOpEqual: return "==";
52 case EOpNotEqual: return "!=";
53 case EOpLessThan: return "<";
54 case EOpGreaterThan: return ">";
55 case EOpLessThanEqual: return "<=";
56 case EOpGreaterThanEqual: return ">=";
59 case EOpVectorTimesScalar:
60 case EOpVectorTimesMatrix:
61 case EOpMatrixTimesVector:
62 case EOpMatrixTimesScalar:
63 case EOpMatrixTimesMatrix: return "*";
65 case EOpLogicalOr: return "||";
66 case EOpLogicalXor: return "^^";
67 case EOpLogicalAnd: return "&&";
68 case EOpNegative: return "-";
69 case EOpVectorLogicalNot: return "not";
70 case EOpLogicalNot: return "!";
71 case EOpPostIncrement: return "++";
72 case EOpPostDecrement: return "--";
73 case EOpPreIncrement: return "++";
74 case EOpPreDecrement: return "--";
77 case EOpConvIntToBool:
78 case EOpConvFloatToBool: return "bool";
81 case EOpConvBoolToFloat:
82 case EOpConvIntToFloat: return "float";
85 case EOpConvFloatToInt:
86 case EOpConvBoolToInt: return "int";
88 case EOpRadians: return "radians";
89 case EOpDegrees: return "degrees";
90 case EOpSin: return "sin";
91 case EOpCos: return "cos";
92 case EOpTan: return "tan";
93 case EOpAsin: return "asin";
94 case EOpAcos: return "acos";
95 case EOpAtan: return "atan";
96 case EOpExp: return "exp";
97 case EOpLog: return "log";
98 case EOpExp2: return "exp2";
99 case EOpLog2: return "log2";
100 case EOpSqrt: return "sqrt";
101 case EOpInverseSqrt: return "inversesqrt";
102 case EOpAbs: return "abs";
103 case EOpSign: return "sign";
104 case EOpFloor: return "floor";
105 case EOpCeil: return "ceil";
106 case EOpFract: return "fract";
107 case EOpLength: return "length";
108 case EOpNormalize: return "normalize";
109 case EOpDFdx: return "dFdx";
110 case EOpDFdy: return "dFdy";
111 case EOpFwidth: return "fwidth";
112 case EOpAny: return "any";
113 case EOpAll: return "all";
120 ////////////////////////////////////////////////////////////////////////////
122 // First set of functions are to help build the intermediate representation.
123 // These functions are not member functions of the nodes.
124 // They are called from parser productions.
126 /////////////////////////////////////////////////////////////////////////////
129 // Add a terminal node for an identifier in an expression.
131 // Returns the added node.
133 TIntermSymbol* TIntermediate::addSymbol(int id, const TString& name, const TType& type, TSourceLoc line)
135 TIntermSymbol* node = new TIntermSymbol(id, name, type);
142 // Connect two nodes with a new parent that does a binary operation on the nodes.
144 // Returns the added node.
146 TIntermTyped* TIntermediate::addBinaryMath(TOperator op, TIntermTyped* left, TIntermTyped* right, TSourceLoc line, TSymbolTable& symbolTable)
156 case EOpLessThanEqual:
157 case EOpGreaterThanEqual:
158 if (left->isMatrix() || left->isArray() || left->isVector() || left->getBasicType() == EbtStruct) {
165 if (left->getBasicType() != EbtBool || left->isMatrix() || left->isArray() || left->isVector()) {
173 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
179 // First try converting the children to compatible types.
181 if (left->getType().getStruct() && right->getType().getStruct()) {
182 if (left->getType() != right->getType())
185 TIntermTyped* child = addConversion(op, left->getType(), right);
189 child = addConversion(op, right->getType(), left);
198 // Need a new node holding things together then. Make
199 // one and promote it to the right type.
201 TIntermBinary* node = new TIntermBinary(op);
203 line = right->getLine();
207 node->setRight(right);
208 if (!node->promote(infoSink))
212 // See if we can fold constants.
214 TIntermTyped* typedReturnNode = 0;
215 TIntermConstantUnion *leftTempConstant = left->getAsConstantUnion();
216 TIntermConstantUnion *rightTempConstant = right->getAsConstantUnion();
217 if (leftTempConstant && rightTempConstant) {
218 typedReturnNode = leftTempConstant->fold(node->getOp(), rightTempConstant, infoSink);
221 return typedReturnNode;
228 // Connect two nodes through an assignment.
230 // Returns the added node.
232 TIntermTyped* TIntermediate::addAssign(TOperator op, TIntermTyped* left, TIntermTyped* right, TSourceLoc line)
235 // Like adding binary math, except the conversion can only go
236 // from right to left.
238 TIntermBinary* node = new TIntermBinary(op);
240 line = left->getLine();
243 TIntermTyped* child = addConversion(op, left->getType(), right);
248 node->setRight(child);
249 if (! node->promote(infoSink))
256 // Connect two nodes through an index operator, where the left node is the base
257 // of an array or struct, and the right node is a direct or indirect offset.
259 // Returns the added node.
260 // The caller should set the type of the returned node.
262 TIntermTyped* TIntermediate::addIndex(TOperator op, TIntermTyped* base, TIntermTyped* index, TSourceLoc line)
264 TIntermBinary* node = new TIntermBinary(op);
266 line = index->getLine();
269 node->setRight(index);
271 // caller should set the type
277 // Add one node as the parent of another that it operates on.
279 // Returns the added node.
281 TIntermTyped* TIntermediate::addUnaryMath(TOperator op, TIntermNode* childNode, TSourceLoc line, TSymbolTable& symbolTable)
284 TIntermTyped* child = childNode->getAsTyped();
287 infoSink.info.message(EPrefixInternalError, "Bad type in AddUnaryMath", line);
293 if (child->getType().getBasicType() != EbtBool || child->getType().isMatrix() || child->getType().isArray() || child->getType().isVector()) {
298 case EOpPostIncrement:
299 case EOpPreIncrement:
300 case EOpPostDecrement:
301 case EOpPreDecrement:
303 if (child->getType().getBasicType() == EbtStruct || child->getType().isArray())
309 // Do we need to promote the operand?
311 // Note: Implicit promotions were removed from the language.
313 TBasicType newType = EbtVoid;
315 case EOpConstructInt: newType = EbtInt; break;
316 case EOpConstructBool: newType = EbtBool; break;
317 case EOpConstructFloat: newType = EbtFloat; break;
321 if (newType != EbtVoid) {
322 child = addConversion(op, TType(newType, child->getPrecision(), EvqTemporary,
323 child->getNominalSize(),
332 // For constructors, we are now done, it's all in the conversion.
335 case EOpConstructInt:
336 case EOpConstructBool:
337 case EOpConstructFloat:
342 TIntermConstantUnion *childTempConstant = 0;
343 if (child->getAsConstantUnion())
344 childTempConstant = child->getAsConstantUnion();
347 // Make a new node for the operator.
349 node = new TIntermUnary(op);
351 line = child->getLine();
353 node->setOperand(child);
355 if (! node->promote(infoSink))
358 if (childTempConstant) {
359 TIntermTyped* newChild = childTempConstant->fold(op, 0, infoSink);
369 // This is the safe way to change the operator on an aggregate, as it
370 // does lots of error checking and fixing. Especially for establishing
371 // a function call's operation on it's set of parameters. Sequences
372 // of instructions are also aggregates, but they just direnctly set
373 // their operator to EOpSequence.
375 // Returns an aggregate node, which could be the one passed in if
376 // it was already an aggregate.
378 TIntermAggregate* TIntermediate::setAggregateOperator(TIntermNode* node, TOperator op, TSourceLoc line)
380 TIntermAggregate* aggNode;
383 // Make sure we have an aggregate. If not turn it into one.
386 aggNode = node->getAsAggregate();
387 if (aggNode == 0 || aggNode->getOp() != EOpNull) {
389 // Make an aggregate containing this node.
391 aggNode = new TIntermAggregate();
392 aggNode->getSequence().push_back(node);
394 line = node->getLine();
397 aggNode = new TIntermAggregate();
404 aggNode->setLine(line);
410 // Convert one type to another.
412 // Returns the node representing the conversion, which could be the same
413 // node passed in if no conversion was needed.
415 // Return 0 if a conversion can't be done.
417 TIntermTyped* TIntermediate::addConversion(TOperator op, const TType& type, TIntermTyped* node)
420 // Does the base type allow operation?
422 switch (node->getBasicType()) {
431 // Otherwise, if types are identical, no problem
433 if (type == node->getType())
437 // If one's a structure, then no conversions.
439 if (type.getStruct() || node->getType().getStruct())
443 // If one's an array, then no conversions.
445 if (type.isArray() || node->getType().isArray())
448 TBasicType promoteTo;
452 // Explicit conversions
454 case EOpConstructBool:
457 case EOpConstructFloat:
458 promoteTo = EbtFloat;
460 case EOpConstructInt:
465 // implicit conversions were removed from the language.
467 if (type.getBasicType() != node->getType().getBasicType())
470 // Size and structure could still differ, but that's
471 // handled by operator promotion.
476 if (node->getAsConstantUnion()) {
478 return (promoteConstantUnion(promoteTo, node->getAsConstantUnion()));
482 // Add a new newNode for the conversion.
484 TIntermUnary* newNode = 0;
486 TOperator newOp = EOpNull;
489 switch (node->getBasicType()) {
490 case EbtInt: newOp = EOpConvIntToFloat; break;
491 case EbtBool: newOp = EOpConvBoolToFloat; break;
493 infoSink.info.message(EPrefixInternalError, "Bad promotion node", node->getLine());
498 switch (node->getBasicType()) {
499 case EbtInt: newOp = EOpConvIntToBool; break;
500 case EbtFloat: newOp = EOpConvFloatToBool; break;
502 infoSink.info.message(EPrefixInternalError, "Bad promotion node", node->getLine());
507 switch (node->getBasicType()) {
508 case EbtBool: newOp = EOpConvBoolToInt; break;
509 case EbtFloat: newOp = EOpConvFloatToInt; break;
511 infoSink.info.message(EPrefixInternalError, "Bad promotion node", node->getLine());
516 infoSink.info.message(EPrefixInternalError, "Bad promotion type", node->getLine());
520 TType type(promoteTo, node->getPrecision(), EvqTemporary, node->getNominalSize(), node->isMatrix(), node->isArray());
521 newNode = new TIntermUnary(newOp, type);
522 newNode->setLine(node->getLine());
523 newNode->setOperand(node);
530 // Safe way to combine two nodes into an aggregate. Works with null pointers,
531 // a node that's not a aggregate yet, etc.
533 // Returns the resulting aggregate, unless 0 was passed in for
534 // both existing nodes.
536 TIntermAggregate* TIntermediate::growAggregate(TIntermNode* left, TIntermNode* right, TSourceLoc line)
538 if (left == 0 && right == 0)
541 TIntermAggregate* aggNode = 0;
543 aggNode = left->getAsAggregate();
544 if (!aggNode || aggNode->getOp() != EOpNull) {
545 aggNode = new TIntermAggregate;
547 aggNode->getSequence().push_back(left);
551 aggNode->getSequence().push_back(right);
554 aggNode->setLine(line);
560 // Turn an existing node into an aggregate.
562 // Returns an aggregate, unless 0 was passed in for the existing node.
564 TIntermAggregate* TIntermediate::makeAggregate(TIntermNode* node, TSourceLoc line)
569 TIntermAggregate* aggNode = new TIntermAggregate;
570 aggNode->getSequence().push_back(node);
573 aggNode->setLine(line);
575 aggNode->setLine(node->getLine());
581 // For "if" test nodes. There are three children; a condition,
582 // a true path, and a false path. The two paths are in the
585 // Returns the selection node created.
587 TIntermNode* TIntermediate::addSelection(TIntermTyped* cond, TIntermNodePair nodePair, TSourceLoc line)
590 // For compile time constant selections, prune the code and
594 if (cond->getAsTyped() && cond->getAsTyped()->getAsConstantUnion()) {
595 if (cond->getAsTyped()->getAsConstantUnion()->getUnionArrayPointer()->getBConst())
596 return nodePair.node1;
598 return nodePair.node2;
601 TIntermSelection* node = new TIntermSelection(cond, nodePair.node1, nodePair.node2);
608 TIntermTyped* TIntermediate::addComma(TIntermTyped* left, TIntermTyped* right, TSourceLoc line)
610 if (left->getType().getQualifier() == EvqConst && right->getType().getQualifier() == EvqConst) {
613 TIntermTyped *commaAggregate = growAggregate(left, right, line);
614 commaAggregate->getAsAggregate()->setOp(EOpComma);
615 commaAggregate->setType(right->getType());
616 commaAggregate->getTypePointer()->setQualifier(EvqTemporary);
617 return commaAggregate;
622 // For "?:" test nodes. There are three children; a condition,
623 // a true path, and a false path. The two paths are specified
624 // as separate parameters.
626 // Returns the selection node created, or 0 if one could not be.
628 TIntermTyped* TIntermediate::addSelection(TIntermTyped* cond, TIntermTyped* trueBlock, TIntermTyped* falseBlock, TSourceLoc line)
631 // Get compatible types.
633 TIntermTyped* child = addConversion(EOpSequence, trueBlock->getType(), falseBlock);
637 child = addConversion(EOpSequence, falseBlock->getType(), trueBlock);
645 // See if all the operands are constant, then fold it otherwise not.
648 if (cond->getAsConstantUnion() && trueBlock->getAsConstantUnion() && falseBlock->getAsConstantUnion()) {
649 if (cond->getAsConstantUnion()->getUnionArrayPointer()->getBConst())
656 // Make a selection node.
658 TIntermSelection* node = new TIntermSelection(cond, trueBlock, falseBlock, trueBlock->getType());
665 // Constant terminal nodes. Has a union that contains bool, float or int constants
667 // Returns the constant union node created.
670 TIntermConstantUnion* TIntermediate::addConstantUnion(ConstantUnion* unionArrayPointer, const TType& t, TSourceLoc line)
672 TIntermConstantUnion* node = new TIntermConstantUnion(unionArrayPointer, t);
678 TIntermTyped* TIntermediate::addSwizzle(TVectorFields& fields, TSourceLoc line)
681 TIntermAggregate* node = new TIntermAggregate(EOpSequence);
684 TIntermConstantUnion* constIntNode;
685 TIntermSequence &sequenceVector = node->getSequence();
686 ConstantUnion* unionArray;
688 for (int i = 0; i < fields.num; i++) {
689 unionArray = new ConstantUnion[1];
690 unionArray->setIConst(fields.offsets[i]);
691 constIntNode = addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), line);
692 sequenceVector.push_back(constIntNode);
699 // Create loop nodes.
701 TIntermNode* TIntermediate::addLoop(TLoopType type, TIntermNode* init, TIntermTyped* cond, TIntermTyped* expr, TIntermNode* body, TSourceLoc line)
703 TIntermNode* node = new TIntermLoop(type, init, cond, expr, body);
712 TIntermBranch* TIntermediate::addBranch(TOperator branchOp, TSourceLoc line)
714 return addBranch(branchOp, 0, line);
717 TIntermBranch* TIntermediate::addBranch(TOperator branchOp, TIntermTyped* expression, TSourceLoc line)
719 TIntermBranch* node = new TIntermBranch(branchOp, expression);
726 // This is to be executed once the final root is put on top by the parsing
729 bool TIntermediate::postProcess(TIntermNode* root)
735 // First, finish off the top level sequence, if any
737 TIntermAggregate* aggRoot = root->getAsAggregate();
738 if (aggRoot && aggRoot->getOp() == EOpNull)
739 aggRoot->setOp(EOpSequence);
745 // This deletes the tree.
747 void TIntermediate::remove(TIntermNode* root)
750 RemoveAllTreeNodes(root);
753 ////////////////////////////////////////////////////////////////
755 // Member functions of the nodes used for building the tree.
757 ////////////////////////////////////////////////////////////////
760 // Say whether or not an operation node changes the value of a variable.
762 // Returns true if state is modified.
764 bool TIntermOperator::modifiesState() const
767 case EOpPostIncrement:
768 case EOpPostDecrement:
769 case EOpPreIncrement:
770 case EOpPreDecrement:
775 case EOpVectorTimesMatrixAssign:
776 case EOpVectorTimesScalarAssign:
777 case EOpMatrixTimesScalarAssign:
778 case EOpMatrixTimesMatrixAssign:
787 // returns true if the operator is for one of the constructors
789 bool TIntermOperator::isConstructor() const
792 case EOpConstructVec2:
793 case EOpConstructVec3:
794 case EOpConstructVec4:
795 case EOpConstructMat2:
796 case EOpConstructMat3:
797 case EOpConstructMat4:
798 case EOpConstructFloat:
799 case EOpConstructIVec2:
800 case EOpConstructIVec3:
801 case EOpConstructIVec4:
802 case EOpConstructInt:
803 case EOpConstructBVec2:
804 case EOpConstructBVec3:
805 case EOpConstructBVec4:
806 case EOpConstructBool:
807 case EOpConstructStruct:
814 // Make sure the type of a unary operator is appropriate for its
815 // combination of operation and operand type.
817 // Returns false in nothing makes sense.
819 bool TIntermUnary::promote(TInfoSink&)
823 if (operand->getBasicType() != EbtBool)
827 case EOpPostIncrement:
828 case EOpPostDecrement:
829 case EOpPreIncrement:
830 case EOpPreDecrement:
831 if (operand->getBasicType() == EbtBool)
835 // operators for built-ins are already type checked against their prototype
838 case EOpVectorLogicalNot:
842 if (operand->getBasicType() != EbtFloat)
846 setType(operand->getType());
852 // Establishes the type of the resultant operation, as well as
853 // makes the operator the correct one for the operands.
855 // Returns false if operator can't work on operands.
857 bool TIntermBinary::promote(TInfoSink& infoSink)
859 // This function only handles scalars, vectors, and matrices.
860 if (left->isArray() || right->isArray()) {
861 infoSink.info.message(EPrefixInternalError, "Invalid operation for arrays", getLine());
865 // GLSL ES 2.0 does not support implicit type casting.
866 // So the basic type should always match.
867 if (left->getBasicType() != right->getBasicType())
871 // Base assumption: just make the type the same as the left
872 // operand. Then only deviations from this need be coded.
874 setType(left->getType());
876 // The result gets promoted to the highest precision.
877 TPrecision higherPrecision = GetHigherPrecision(left->getPrecision(), right->getPrecision());
878 getTypePointer()->setPrecision(higherPrecision);
880 // Binary operations results in temporary variables unless both
881 // operands are const.
882 if (left->getQualifier() != EvqConst || right->getQualifier() != EvqConst) {
883 getTypePointer()->setQualifier(EvqTemporary);
886 int size = std::max(left->getNominalSize(), right->getNominalSize());
889 // All scalars. Code after this test assumes this case is removed!
894 // Promote to conditional
900 case EOpLessThanEqual:
901 case EOpGreaterThanEqual:
902 setType(TType(EbtBool, EbpUndefined));
906 // And and Or operate on conditionals
910 // Both operands must be of type bool.
911 if (left->getBasicType() != EbtBool || right->getBasicType() != EbtBool)
913 setType(TType(EbtBool, EbpUndefined));
922 // If we reach here, at least one of the operands is vector or matrix.
923 // The other operand could be a scalar, vector, or matrix.
924 // Are the sizes compatible?
926 if (left->getNominalSize() != right->getNominalSize()) {
927 // If the nominal size of operands do not match:
928 // One of them must be scalar.
929 if (left->getNominalSize() != 1 && right->getNominalSize() != 1)
931 // Operator cannot be of type pure assignment.
932 if (op == EOpAssign || op == EOpInitialize)
937 // Can these two operands be combined?
939 TBasicType basicType = left->getBasicType();
942 if (!left->isMatrix() && right->isMatrix()) {
943 if (left->isVector())
944 op = EOpVectorTimesMatrix;
946 op = EOpMatrixTimesScalar;
947 setType(TType(basicType, higherPrecision, EvqTemporary, size, true));
949 } else if (left->isMatrix() && !right->isMatrix()) {
950 if (right->isVector()) {
951 op = EOpMatrixTimesVector;
952 setType(TType(basicType, higherPrecision, EvqTemporary, size, false));
954 op = EOpMatrixTimesScalar;
956 } else if (left->isMatrix() && right->isMatrix()) {
957 op = EOpMatrixTimesMatrix;
958 } else if (!left->isMatrix() && !right->isMatrix()) {
959 if (left->isVector() && right->isVector()) {
960 // leave as component product
961 } else if (left->isVector() || right->isVector()) {
962 op = EOpVectorTimesScalar;
963 setType(TType(basicType, higherPrecision, EvqTemporary, size, false));
966 infoSink.info.message(EPrefixInternalError, "Missing elses", getLine());
971 if (!left->isMatrix() && right->isMatrix()) {
972 if (left->isVector())
973 op = EOpVectorTimesMatrixAssign;
977 } else if (left->isMatrix() && !right->isMatrix()) {
978 if (right->isVector()) {
981 op = EOpMatrixTimesScalarAssign;
983 } else if (left->isMatrix() && right->isMatrix()) {
984 op = EOpMatrixTimesMatrixAssign;
985 } else if (!left->isMatrix() && !right->isMatrix()) {
986 if (left->isVector() && right->isVector()) {
987 // leave as component product
988 } else if (left->isVector() || right->isVector()) {
989 if (! left->isVector())
991 op = EOpVectorTimesScalarAssign;
992 setType(TType(basicType, higherPrecision, EvqTemporary, size, false));
995 infoSink.info.message(EPrefixInternalError, "Missing elses", getLine());
1008 if ((left->isMatrix() && right->isVector()) ||
1009 (left->isVector() && right->isMatrix()))
1011 setType(TType(basicType, higherPrecision, EvqTemporary, size, left->isMatrix() || right->isMatrix()));
1017 case EOpGreaterThan:
1018 case EOpLessThanEqual:
1019 case EOpGreaterThanEqual:
1020 if ((left->isMatrix() && right->isVector()) ||
1021 (left->isVector() && right->isMatrix()))
1023 setType(TType(EbtBool, EbpUndefined));
1033 bool CompareStruct(const TType& leftNodeType, ConstantUnion* rightUnionArray, ConstantUnion* leftUnionArray)
1035 const TTypeList* fields = leftNodeType.getStruct();
1037 size_t structSize = fields->size();
1040 for (size_t j = 0; j < structSize; j++) {
1041 int size = (*fields)[j].type->getObjectSize();
1042 for (int i = 0; i < size; i++) {
1043 if ((*fields)[j].type->getBasicType() == EbtStruct) {
1044 if (!CompareStructure(*(*fields)[j].type, &rightUnionArray[index], &leftUnionArray[index]))
1047 if (leftUnionArray[index] != rightUnionArray[index])
1057 bool CompareStructure(const TType& leftNodeType, ConstantUnion* rightUnionArray, ConstantUnion* leftUnionArray)
1059 if (leftNodeType.isArray()) {
1060 TType typeWithoutArrayness = leftNodeType;
1061 typeWithoutArrayness.clearArrayness();
1063 int arraySize = leftNodeType.getArraySize();
1065 for (int i = 0; i < arraySize; ++i) {
1066 int offset = typeWithoutArrayness.getObjectSize() * i;
1067 if (!CompareStruct(typeWithoutArrayness, &rightUnionArray[offset], &leftUnionArray[offset]))
1071 return CompareStruct(leftNodeType, rightUnionArray, leftUnionArray);
1077 // The fold functions see if an operation on a constant can be done in place,
1078 // without generating run-time code.
1080 // Returns the node to keep using, which may or may not be the node passed in.
1083 TIntermTyped* TIntermConstantUnion::fold(TOperator op, TIntermTyped* constantNode, TInfoSink& infoSink)
1085 ConstantUnion *unionArray = getUnionArrayPointer();
1086 int objectSize = getType().getObjectSize();
1088 if (constantNode) { // binary operations
1089 TIntermConstantUnion *node = constantNode->getAsConstantUnion();
1090 ConstantUnion *rightUnionArray = node->getUnionArrayPointer();
1091 TType returnType = getType();
1093 // for a case like float f = 1.2 + vec4(2,3,4,5);
1094 if (constantNode->getType().getObjectSize() == 1 && objectSize > 1) {
1095 rightUnionArray = new ConstantUnion[objectSize];
1096 for (int i = 0; i < objectSize; ++i)
1097 rightUnionArray[i] = *node->getUnionArrayPointer();
1098 returnType = getType();
1099 } else if (constantNode->getType().getObjectSize() > 1 && objectSize == 1) {
1100 // for a case like float f = vec4(2,3,4,5) + 1.2;
1101 unionArray = new ConstantUnion[constantNode->getType().getObjectSize()];
1102 for (int i = 0; i < constantNode->getType().getObjectSize(); ++i)
1103 unionArray[i] = *getUnionArrayPointer();
1104 returnType = node->getType();
1105 objectSize = constantNode->getType().getObjectSize();
1108 ConstantUnion* tempConstArray = 0;
1109 TIntermConstantUnion *tempNode;
1111 bool boolNodeFlag = false;
1114 tempConstArray = new ConstantUnion[objectSize];
1115 {// support MSVC++6.0
1116 for (int i = 0; i < objectSize; i++)
1117 tempConstArray[i] = unionArray[i] + rightUnionArray[i];
1121 tempConstArray = new ConstantUnion[objectSize];
1122 {// support MSVC++6.0
1123 for (int i = 0; i < objectSize; i++)
1124 tempConstArray[i] = unionArray[i] - rightUnionArray[i];
1129 case EOpVectorTimesScalar:
1130 case EOpMatrixTimesScalar:
1131 tempConstArray = new ConstantUnion[objectSize];
1132 {// support MSVC++6.0
1133 for (int i = 0; i < objectSize; i++)
1134 tempConstArray[i] = unionArray[i] * rightUnionArray[i];
1137 case EOpMatrixTimesMatrix:
1138 if (getType().getBasicType() != EbtFloat || node->getBasicType() != EbtFloat) {
1139 infoSink.info.message(EPrefixInternalError, "Constant Folding cannot be done for matrix multiply", getLine());
1142 {// support MSVC++6.0
1143 int size = getNominalSize();
1144 tempConstArray = new ConstantUnion[size*size];
1145 for (int row = 0; row < size; row++) {
1146 for (int column = 0; column < size; column++) {
1147 tempConstArray[size * column + row].setFConst(0.0f);
1148 for (int i = 0; i < size; i++) {
1149 tempConstArray[size * column + row].setFConst(tempConstArray[size * column + row].getFConst() + unionArray[i * size + row].getFConst() * (rightUnionArray[column * size + i].getFConst()));
1156 tempConstArray = new ConstantUnion[objectSize];
1157 {// support MSVC++6.0
1158 for (int i = 0; i < objectSize; i++) {
1159 switch (getType().getBasicType()) {
1161 if (rightUnionArray[i] == 0.0f) {
1162 infoSink.info.message(EPrefixWarning, "Divide by zero error during constant folding", getLine());
1163 tempConstArray[i].setFConst(FLT_MAX);
1165 tempConstArray[i].setFConst(unionArray[i].getFConst() / rightUnionArray[i].getFConst());
1169 if (rightUnionArray[i] == 0) {
1170 infoSink.info.message(EPrefixWarning, "Divide by zero error during constant folding", getLine());
1171 tempConstArray[i].setIConst(INT_MAX);
1173 tempConstArray[i].setIConst(unionArray[i].getIConst() / rightUnionArray[i].getIConst());
1176 infoSink.info.message(EPrefixInternalError, "Constant folding cannot be done for \"/\"", getLine());
1183 case EOpMatrixTimesVector:
1184 if (node->getBasicType() != EbtFloat) {
1185 infoSink.info.message(EPrefixInternalError, "Constant Folding cannot be done for matrix times vector", getLine());
1188 tempConstArray = new ConstantUnion[getNominalSize()];
1190 {// support MSVC++6.0
1191 for (int size = getNominalSize(), i = 0; i < size; i++) {
1192 tempConstArray[i].setFConst(0.0f);
1193 for (int j = 0; j < size; j++) {
1194 tempConstArray[i].setFConst(tempConstArray[i].getFConst() + ((unionArray[j*size + i].getFConst()) * rightUnionArray[j].getFConst()));
1199 tempNode = new TIntermConstantUnion(tempConstArray, node->getType());
1200 tempNode->setLine(getLine());
1204 case EOpVectorTimesMatrix:
1205 if (getType().getBasicType() != EbtFloat) {
1206 infoSink.info.message(EPrefixInternalError, "Constant Folding cannot be done for vector times matrix", getLine());
1210 tempConstArray = new ConstantUnion[getNominalSize()];
1211 {// support MSVC++6.0
1212 for (int size = getNominalSize(), i = 0; i < size; i++) {
1213 tempConstArray[i].setFConst(0.0f);
1214 for (int j = 0; j < size; j++) {
1215 tempConstArray[i].setFConst(tempConstArray[i].getFConst() + ((unionArray[j].getFConst()) * rightUnionArray[i*size + j].getFConst()));
1221 case EOpLogicalAnd: // this code is written for possible future use, will not get executed currently
1222 tempConstArray = new ConstantUnion[objectSize];
1223 {// support MSVC++6.0
1224 for (int i = 0; i < objectSize; i++)
1225 tempConstArray[i] = unionArray[i] && rightUnionArray[i];
1229 case EOpLogicalOr: // this code is written for possible future use, will not get executed currently
1230 tempConstArray = new ConstantUnion[objectSize];
1231 {// support MSVC++6.0
1232 for (int i = 0; i < objectSize; i++)
1233 tempConstArray[i] = unionArray[i] || rightUnionArray[i];
1238 tempConstArray = new ConstantUnion[objectSize];
1239 {// support MSVC++6.0
1240 for (int i = 0; i < objectSize; i++)
1241 switch (getType().getBasicType()) {
1242 case EbtBool: tempConstArray[i].setBConst((unionArray[i] == rightUnionArray[i]) ? false : true); break;
1243 default: assert(false && "Default missing");
1249 assert(objectSize == 1);
1250 tempConstArray = new ConstantUnion[1];
1251 tempConstArray->setBConst(*unionArray < *rightUnionArray);
1252 returnType = TType(EbtBool, EbpUndefined, EvqConst);
1254 case EOpGreaterThan:
1255 assert(objectSize == 1);
1256 tempConstArray = new ConstantUnion[1];
1257 tempConstArray->setBConst(*unionArray > *rightUnionArray);
1258 returnType = TType(EbtBool, EbpUndefined, EvqConst);
1260 case EOpLessThanEqual:
1262 assert(objectSize == 1);
1263 ConstantUnion constant;
1264 constant.setBConst(*unionArray > *rightUnionArray);
1265 tempConstArray = new ConstantUnion[1];
1266 tempConstArray->setBConst(!constant.getBConst());
1267 returnType = TType(EbtBool, EbpUndefined, EvqConst);
1270 case EOpGreaterThanEqual:
1272 assert(objectSize == 1);
1273 ConstantUnion constant;
1274 constant.setBConst(*unionArray < *rightUnionArray);
1275 tempConstArray = new ConstantUnion[1];
1276 tempConstArray->setBConst(!constant.getBConst());
1277 returnType = TType(EbtBool, EbpUndefined, EvqConst);
1282 if (getType().getBasicType() == EbtStruct) {
1283 if (!CompareStructure(node->getType(), node->getUnionArrayPointer(), unionArray))
1284 boolNodeFlag = true;
1286 for (int i = 0; i < objectSize; i++) {
1287 if (unionArray[i] != rightUnionArray[i]) {
1288 boolNodeFlag = true;
1289 break; // break out of for loop
1294 tempConstArray = new ConstantUnion[1];
1295 if (!boolNodeFlag) {
1296 tempConstArray->setBConst(true);
1299 tempConstArray->setBConst(false);
1302 tempNode = new TIntermConstantUnion(tempConstArray, TType(EbtBool, EbpUndefined, EvqConst));
1303 tempNode->setLine(getLine());
1308 if (getType().getBasicType() == EbtStruct) {
1309 if (CompareStructure(node->getType(), node->getUnionArrayPointer(), unionArray))
1310 boolNodeFlag = true;
1312 for (int i = 0; i < objectSize; i++) {
1313 if (unionArray[i] == rightUnionArray[i]) {
1314 boolNodeFlag = true;
1315 break; // break out of for loop
1320 tempConstArray = new ConstantUnion[1];
1321 if (!boolNodeFlag) {
1322 tempConstArray->setBConst(true);
1325 tempConstArray->setBConst(false);
1328 tempNode = new TIntermConstantUnion(tempConstArray, TType(EbtBool, EbpUndefined, EvqConst));
1329 tempNode->setLine(getLine());
1334 infoSink.info.message(EPrefixInternalError, "Invalid operator for constant folding", getLine());
1337 tempNode = new TIntermConstantUnion(tempConstArray, returnType);
1338 tempNode->setLine(getLine());
1343 // Do unary operations
1345 TIntermConstantUnion *newNode = 0;
1346 ConstantUnion* tempConstArray = new ConstantUnion[objectSize];
1347 for (int i = 0; i < objectSize; i++) {
1350 switch (getType().getBasicType()) {
1351 case EbtFloat: tempConstArray[i].setFConst(-unionArray[i].getFConst()); break;
1352 case EbtInt: tempConstArray[i].setIConst(-unionArray[i].getIConst()); break;
1354 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1358 case EOpLogicalNot: // this code is written for possible future use, will not get executed currently
1359 switch (getType().getBasicType()) {
1360 case EbtBool: tempConstArray[i].setBConst(!unionArray[i].getBConst()); break;
1362 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1370 newNode = new TIntermConstantUnion(tempConstArray, getType());
1371 newNode->setLine(getLine());
1378 TIntermTyped* TIntermediate::promoteConstantUnion(TBasicType promoteTo, TIntermConstantUnion* node)
1380 ConstantUnion *rightUnionArray = node->getUnionArrayPointer();
1381 int size = node->getType().getObjectSize();
1383 ConstantUnion *leftUnionArray = new ConstantUnion[size];
1385 for (int i=0; i < size; i++) {
1387 switch (promoteTo) {
1389 switch (node->getType().getBasicType()) {
1391 leftUnionArray[i].setFConst(static_cast<float>(rightUnionArray[i].getIConst()));
1394 leftUnionArray[i].setFConst(static_cast<float>(rightUnionArray[i].getBConst()));
1397 leftUnionArray[i] = rightUnionArray[i];
1400 infoSink.info.message(EPrefixInternalError, "Cannot promote", node->getLine());
1405 switch (node->getType().getBasicType()) {
1407 leftUnionArray[i] = rightUnionArray[i];
1410 leftUnionArray[i].setIConst(static_cast<int>(rightUnionArray[i].getBConst()));
1413 leftUnionArray[i].setIConst(static_cast<int>(rightUnionArray[i].getFConst()));
1416 infoSink.info.message(EPrefixInternalError, "Cannot promote", node->getLine());
1421 switch (node->getType().getBasicType()) {
1423 leftUnionArray[i].setBConst(rightUnionArray[i].getIConst() != 0);
1426 leftUnionArray[i] = rightUnionArray[i];
1429 leftUnionArray[i].setBConst(rightUnionArray[i].getFConst() != 0.0f);
1432 infoSink.info.message(EPrefixInternalError, "Cannot promote", node->getLine());
1438 infoSink.info.message(EPrefixInternalError, "Incorrect data type found", node->getLine());
1444 const TType& t = node->getType();
1446 return addConstantUnion(leftUnionArray, TType(promoteTo, t.getPrecision(), t.getQualifier(), t.getNominalSize(), t.isMatrix(), t.isArray()), node->getLine());
1449 void TIntermAggregate::addToPragmaTable(const TPragmaTable& pTable)
1451 assert(!pragmaTable);
1452 pragmaTable = new TPragmaTable();
1453 *pragmaTable = pTable;