2 // Copyright (c) 2002-2013 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/translator/HashNames.h"
16 #include "compiler/translator/localintermediate.h"
17 #include "compiler/translator/QualifierAlive.h"
18 #include "compiler/translator/RemoveTree.h"
20 bool CompareStructure(const TType& leftNodeType, ConstantUnion* rightUnionArray, ConstantUnion* leftUnionArray);
22 static TPrecision GetHigherPrecision(TPrecision left, TPrecision right)
24 return left > right ? left : right;
27 const char* getOperatorString(TOperator op)
30 case EOpInitialize: return "=";
31 case EOpAssign: return "=";
32 case EOpAddAssign: return "+=";
33 case EOpSubAssign: return "-=";
34 case EOpDivAssign: return "/=";
38 case EOpVectorTimesMatrixAssign:
39 case EOpVectorTimesScalarAssign:
40 case EOpMatrixTimesScalarAssign:
41 case EOpMatrixTimesMatrixAssign: return "*=";
45 case EOpIndexIndirect: return "[]";
47 case EOpIndexDirectStruct: return ".";
48 case EOpVectorSwizzle: return ".";
49 case EOpAdd: return "+";
50 case EOpSub: return "-";
51 case EOpMul: return "*";
52 case EOpDiv: return "/";
53 case EOpMod: UNIMPLEMENTED(); break;
54 case EOpEqual: return "==";
55 case EOpNotEqual: return "!=";
56 case EOpLessThan: return "<";
57 case EOpGreaterThan: return ">";
58 case EOpLessThanEqual: return "<=";
59 case EOpGreaterThanEqual: return ">=";
62 case EOpVectorTimesScalar:
63 case EOpVectorTimesMatrix:
64 case EOpMatrixTimesVector:
65 case EOpMatrixTimesScalar:
66 case EOpMatrixTimesMatrix: return "*";
68 case EOpLogicalOr: return "||";
69 case EOpLogicalXor: return "^^";
70 case EOpLogicalAnd: return "&&";
71 case EOpNegative: return "-";
72 case EOpVectorLogicalNot: return "not";
73 case EOpLogicalNot: return "!";
74 case EOpPostIncrement: return "++";
75 case EOpPostDecrement: return "--";
76 case EOpPreIncrement: return "++";
77 case EOpPreDecrement: return "--";
80 case EOpConvIntToBool:
81 case EOpConvFloatToBool: return "bool";
84 case EOpConvBoolToFloat:
85 case EOpConvIntToFloat: return "float";
88 case EOpConvFloatToInt:
89 case EOpConvBoolToInt: return "int";
91 case EOpRadians: return "radians";
92 case EOpDegrees: return "degrees";
93 case EOpSin: return "sin";
94 case EOpCos: return "cos";
95 case EOpTan: return "tan";
96 case EOpAsin: return "asin";
97 case EOpAcos: return "acos";
98 case EOpAtan: return "atan";
99 case EOpExp: return "exp";
100 case EOpLog: return "log";
101 case EOpExp2: return "exp2";
102 case EOpLog2: return "log2";
103 case EOpSqrt: return "sqrt";
104 case EOpInverseSqrt: return "inversesqrt";
105 case EOpAbs: return "abs";
106 case EOpSign: return "sign";
107 case EOpFloor: return "floor";
108 case EOpCeil: return "ceil";
109 case EOpFract: return "fract";
110 case EOpLength: return "length";
111 case EOpNormalize: return "normalize";
112 case EOpDFdx: return "dFdx";
113 case EOpDFdy: return "dFdy";
114 case EOpFwidth: return "fwidth";
115 case EOpAny: return "any";
116 case EOpAll: return "all";
123 ////////////////////////////////////////////////////////////////////////////
125 // First set of functions are to help build the intermediate representation.
126 // These functions are not member functions of the nodes.
127 // They are called from parser productions.
129 /////////////////////////////////////////////////////////////////////////////
132 // Add a terminal node for an identifier in an expression.
134 // Returns the added node.
136 TIntermSymbol* TIntermediate::addSymbol(int id, const TString& name, const TType& type, const TSourceLoc& line)
138 TIntermSymbol* node = new TIntermSymbol(id, name, type);
145 // Connect two nodes with a new parent that does a binary operation on the nodes.
147 // Returns the added node.
149 TIntermTyped* TIntermediate::addBinaryMath(TOperator op, TIntermTyped* left, TIntermTyped* right, const TSourceLoc& line, TSymbolTable& symbolTable)
159 case EOpLessThanEqual:
160 case EOpGreaterThanEqual:
161 if (left->isMatrix() || left->isArray() || left->isVector() || left->getBasicType() == EbtStruct) {
168 if (left->getBasicType() != EbtBool || left->isMatrix() || left->isArray() || left->isVector()) {
176 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
182 // First try converting the children to compatible types.
184 if (left->getType().getStruct() && right->getType().getStruct()) {
185 if (left->getType() != right->getType())
188 TIntermTyped* child = addConversion(op, left->getType(), right);
192 child = addConversion(op, right->getType(), left);
201 // Need a new node holding things together then. Make
202 // one and promote it to the right type.
204 TIntermBinary* node = new TIntermBinary(op);
208 node->setRight(right);
209 if (!node->promote(infoSink))
213 // See if we can fold constants.
215 TIntermConstantUnion *leftTempConstant = left->getAsConstantUnion();
216 TIntermConstantUnion *rightTempConstant = right->getAsConstantUnion();
217 if (leftTempConstant && rightTempConstant) {
218 TIntermTyped *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, const TSourceLoc& line)
235 // Like adding binary math, except the conversion can only go
236 // from right to left.
238 TIntermBinary* node = new TIntermBinary(op);
241 TIntermTyped* child = addConversion(op, left->getType(), right);
246 node->setRight(child);
247 if (! node->promote(infoSink))
254 // Connect two nodes through an index operator, where the left node is the base
255 // of an array or struct, and the right node is a direct or indirect offset.
257 // Returns the added node.
258 // The caller should set the type of the returned node.
260 TIntermTyped* TIntermediate::addIndex(TOperator op, TIntermTyped* base, TIntermTyped* index, const TSourceLoc& line)
262 TIntermBinary* node = new TIntermBinary(op);
265 node->setRight(index);
267 // caller should set the type
273 // Add one node as the parent of another that it operates on.
275 // Returns the added node.
277 TIntermTyped* TIntermediate::addUnaryMath(TOperator op, TIntermNode* childNode, const TSourceLoc& line, TSymbolTable& symbolTable)
280 TIntermTyped* child = childNode->getAsTyped();
283 infoSink.info.message(EPrefixInternalError, line, "Bad type in AddUnaryMath");
289 if (child->getType().getBasicType() != EbtBool || child->getType().isMatrix() || child->getType().isArray() || child->getType().isVector()) {
294 case EOpPostIncrement:
295 case EOpPreIncrement:
296 case EOpPostDecrement:
297 case EOpPreDecrement:
299 if (child->getType().getBasicType() == EbtStruct || child->getType().isArray())
305 // Do we need to promote the operand?
307 // Note: Implicit promotions were removed from the language.
309 TBasicType newType = EbtVoid;
311 case EOpConstructInt: newType = EbtInt; break;
312 case EOpConstructBool: newType = EbtBool; break;
313 case EOpConstructFloat: newType = EbtFloat; break;
317 if (newType != EbtVoid) {
318 child = addConversion(op, TType(newType, child->getPrecision(), EvqTemporary,
319 child->getNominalSize(),
328 // For constructors, we are now done, it's all in the conversion.
331 case EOpConstructInt:
332 case EOpConstructBool:
333 case EOpConstructFloat:
338 TIntermConstantUnion *childTempConstant = 0;
339 if (child->getAsConstantUnion())
340 childTempConstant = child->getAsConstantUnion();
343 // Make a new node for the operator.
345 node = new TIntermUnary(op);
347 node->setOperand(child);
349 if (! node->promote(infoSink))
352 if (childTempConstant) {
353 TIntermTyped* newChild = childTempConstant->fold(op, 0, infoSink);
363 // This is the safe way to change the operator on an aggregate, as it
364 // does lots of error checking and fixing. Especially for establishing
365 // a function call's operation on it's set of parameters. Sequences
366 // of instructions are also aggregates, but they just direnctly set
367 // their operator to EOpSequence.
369 // Returns an aggregate node, which could be the one passed in if
370 // it was already an aggregate but no operator was set.
372 TIntermAggregate* TIntermediate::setAggregateOperator(TIntermNode* node, TOperator op, const TSourceLoc& line)
374 TIntermAggregate* aggNode;
377 // Make sure we have an aggregate. If not turn it into one.
380 aggNode = node->getAsAggregate();
381 if (aggNode == 0 || aggNode->getOp() != EOpNull) {
383 // Make an aggregate containing this node.
385 aggNode = new TIntermAggregate();
386 aggNode->getSequence().push_back(node);
389 aggNode = new TIntermAggregate();
395 aggNode->setLine(line);
401 // Convert one type to another.
403 // Returns the node representing the conversion, which could be the same
404 // node passed in if no conversion was needed.
406 // Return 0 if a conversion can't be done.
408 TIntermTyped* TIntermediate::addConversion(TOperator op, const TType& type, TIntermTyped* node)
411 // Does the base type allow operation?
413 switch (node->getBasicType()) {
422 // Otherwise, if types are identical, no problem
424 if (type == node->getType())
428 // If one's a structure, then no conversions.
430 if (type.getStruct() || node->getType().getStruct())
434 // If one's an array, then no conversions.
436 if (type.isArray() || node->getType().isArray())
439 TBasicType promoteTo;
443 // Explicit conversions
445 case EOpConstructBool:
448 case EOpConstructFloat:
449 promoteTo = EbtFloat;
451 case EOpConstructInt:
456 // implicit conversions were removed from the language.
458 if (type.getBasicType() != node->getType().getBasicType())
461 // Size and structure could still differ, but that's
462 // handled by operator promotion.
467 if (node->getAsConstantUnion()) {
469 return (promoteConstantUnion(promoteTo, node->getAsConstantUnion()));
473 // Add a new newNode for the conversion.
475 TIntermUnary* newNode = 0;
477 TOperator newOp = EOpNull;
480 switch (node->getBasicType()) {
481 case EbtInt: newOp = EOpConvIntToFloat; break;
482 case EbtBool: newOp = EOpConvBoolToFloat; break;
484 infoSink.info.message(EPrefixInternalError, node->getLine(), "Bad promotion node");
489 switch (node->getBasicType()) {
490 case EbtInt: newOp = EOpConvIntToBool; break;
491 case EbtFloat: newOp = EOpConvFloatToBool; break;
493 infoSink.info.message(EPrefixInternalError, node->getLine(), "Bad promotion node");
498 switch (node->getBasicType()) {
499 case EbtBool: newOp = EOpConvBoolToInt; break;
500 case EbtFloat: newOp = EOpConvFloatToInt; break;
502 infoSink.info.message(EPrefixInternalError, node->getLine(), "Bad promotion node");
507 infoSink.info.message(EPrefixInternalError, node->getLine(), "Bad promotion type");
511 TType type(promoteTo, node->getPrecision(), EvqTemporary, node->getNominalSize(), node->isMatrix(), node->isArray());
512 newNode = new TIntermUnary(newOp, type);
513 newNode->setLine(node->getLine());
514 newNode->setOperand(node);
521 // Safe way to combine two nodes into an aggregate. Works with null pointers,
522 // a node that's not a aggregate yet, etc.
524 // Returns the resulting aggregate, unless 0 was passed in for
525 // both existing nodes.
527 TIntermAggregate* TIntermediate::growAggregate(TIntermNode* left, TIntermNode* right, const TSourceLoc& line)
529 if (left == 0 && right == 0)
532 TIntermAggregate* aggNode = 0;
534 aggNode = left->getAsAggregate();
535 if (!aggNode || aggNode->getOp() != EOpNull) {
536 aggNode = new TIntermAggregate;
538 aggNode->getSequence().push_back(left);
542 aggNode->getSequence().push_back(right);
544 aggNode->setLine(line);
550 // Turn an existing node into an aggregate.
552 // Returns an aggregate, unless 0 was passed in for the existing node.
554 TIntermAggregate* TIntermediate::makeAggregate(TIntermNode* node, const TSourceLoc& line)
559 TIntermAggregate* aggNode = new TIntermAggregate;
560 aggNode->getSequence().push_back(node);
561 aggNode->setLine(line);
567 // For "if" test nodes. There are three children; a condition,
568 // a true path, and a false path. The two paths are in the
571 // Returns the selection node created.
573 TIntermNode* TIntermediate::addSelection(TIntermTyped* cond, TIntermNodePair nodePair, const TSourceLoc& line)
576 // For compile time constant selections, prune the code and
580 if (cond->getAsTyped() && cond->getAsTyped()->getAsConstantUnion()) {
581 if (cond->getAsConstantUnion()->getBConst(0) == true)
582 return nodePair.node1 ? setAggregateOperator(nodePair.node1, EOpSequence, nodePair.node1->getLine()) : NULL;
584 return nodePair.node2 ? setAggregateOperator(nodePair.node2, EOpSequence, nodePair.node2->getLine()) : NULL;
587 TIntermSelection* node = new TIntermSelection(cond, nodePair.node1, nodePair.node2);
594 TIntermTyped* TIntermediate::addComma(TIntermTyped* left, TIntermTyped* right, const TSourceLoc& line)
596 if (left->getType().getQualifier() == EvqConst && right->getType().getQualifier() == EvqConst) {
599 TIntermTyped *commaAggregate = growAggregate(left, right, line);
600 commaAggregate->getAsAggregate()->setOp(EOpComma);
601 commaAggregate->setType(right->getType());
602 commaAggregate->getTypePointer()->setQualifier(EvqTemporary);
603 return commaAggregate;
608 // For "?:" test nodes. There are three children; a condition,
609 // a true path, and a false path. The two paths are specified
610 // as separate parameters.
612 // Returns the selection node created, or 0 if one could not be.
614 TIntermTyped* TIntermediate::addSelection(TIntermTyped* cond, TIntermTyped* trueBlock, TIntermTyped* falseBlock, const TSourceLoc& line)
617 // Get compatible types.
619 TIntermTyped* child = addConversion(EOpSequence, trueBlock->getType(), falseBlock);
623 child = addConversion(EOpSequence, falseBlock->getType(), trueBlock);
631 // See if all the operands are constant, then fold it otherwise not.
634 if (cond->getAsConstantUnion() && trueBlock->getAsConstantUnion() && falseBlock->getAsConstantUnion()) {
635 if (cond->getAsConstantUnion()->getBConst(0))
642 // Make a selection node.
644 TIntermSelection* node = new TIntermSelection(cond, trueBlock, falseBlock, trueBlock->getType());
645 node->getTypePointer()->setQualifier(EvqTemporary);
652 // Constant terminal nodes. Has a union that contains bool, float or int constants
654 // Returns the constant union node created.
657 TIntermConstantUnion* TIntermediate::addConstantUnion(ConstantUnion* unionArrayPointer, const TType& t, const TSourceLoc& line)
659 TIntermConstantUnion* node = new TIntermConstantUnion(unionArrayPointer, t);
665 TIntermTyped* TIntermediate::addSwizzle(TVectorFields& fields, const TSourceLoc& line)
668 TIntermAggregate* node = new TIntermAggregate(EOpSequence);
671 TIntermConstantUnion* constIntNode;
672 TIntermSequence &sequenceVector = node->getSequence();
673 ConstantUnion* unionArray;
675 for (int i = 0; i < fields.num; i++) {
676 unionArray = new ConstantUnion[1];
677 unionArray->setIConst(fields.offsets[i]);
678 constIntNode = addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), line);
679 sequenceVector.push_back(constIntNode);
686 // Create loop nodes.
688 TIntermNode* TIntermediate::addLoop(TLoopType type, TIntermNode* init, TIntermTyped* cond, TIntermTyped* expr, TIntermNode* body, const TSourceLoc& line)
690 TIntermNode* node = new TIntermLoop(type, init, cond, expr, body);
699 TIntermBranch* TIntermediate::addBranch(TOperator branchOp, const TSourceLoc& line)
701 return addBranch(branchOp, 0, line);
704 TIntermBranch* TIntermediate::addBranch(TOperator branchOp, TIntermTyped* expression, const TSourceLoc& line)
706 TIntermBranch* node = new TIntermBranch(branchOp, expression);
713 // This is to be executed once the final root is put on top by the parsing
716 bool TIntermediate::postProcess(TIntermNode* root)
722 // First, finish off the top level sequence, if any
724 TIntermAggregate* aggRoot = root->getAsAggregate();
725 if (aggRoot && aggRoot->getOp() == EOpNull)
726 aggRoot->setOp(EOpSequence);
732 // This deletes the tree.
734 void TIntermediate::remove(TIntermNode* root)
737 RemoveAllTreeNodes(root);
740 ////////////////////////////////////////////////////////////////
742 // Member functions of the nodes used for building the tree.
744 ////////////////////////////////////////////////////////////////
746 #define REPLACE_IF_IS(node, type, original, replacement) \
747 if (node == original) { \
748 node = static_cast<type *>(replacement); \
752 bool TIntermLoop::replaceChildNode(
753 TIntermNode *original, TIntermNode *replacement)
755 REPLACE_IF_IS(init, TIntermNode, original, replacement);
756 REPLACE_IF_IS(cond, TIntermTyped, original, replacement);
757 REPLACE_IF_IS(expr, TIntermTyped, original, replacement);
758 REPLACE_IF_IS(body, TIntermNode, original, replacement);
762 void TIntermLoop::enqueueChildren(std::queue<TIntermNode*> *nodeQueue) const
766 nodeQueue->push(init);
770 nodeQueue->push(cond);
774 nodeQueue->push(expr);
778 nodeQueue->push(body);
782 bool TIntermBranch::replaceChildNode(
783 TIntermNode *original, TIntermNode *replacement)
785 REPLACE_IF_IS(expression, TIntermTyped, original, replacement);
789 void TIntermBranch::enqueueChildren(std::queue<TIntermNode*> *nodeQueue) const
793 nodeQueue->push(expression);
797 bool TIntermBinary::replaceChildNode(
798 TIntermNode *original, TIntermNode *replacement)
800 REPLACE_IF_IS(left, TIntermTyped, original, replacement);
801 REPLACE_IF_IS(right, TIntermTyped, original, replacement);
805 void TIntermBinary::enqueueChildren(std::queue<TIntermNode*> *nodeQueue) const
809 nodeQueue->push(left);
813 nodeQueue->push(right);
817 bool TIntermUnary::replaceChildNode(
818 TIntermNode *original, TIntermNode *replacement)
820 REPLACE_IF_IS(operand, TIntermTyped, original, replacement);
824 void TIntermUnary::enqueueChildren(std::queue<TIntermNode*> *nodeQueue) const
828 nodeQueue->push(operand);
832 bool TIntermAggregate::replaceChildNode(
833 TIntermNode *original, TIntermNode *replacement)
835 for (size_t ii = 0; ii < sequence.size(); ++ii)
837 REPLACE_IF_IS(sequence[ii], TIntermNode, original, replacement);
842 void TIntermAggregate::enqueueChildren(std::queue<TIntermNode*> *nodeQueue) const
844 for (size_t childIndex = 0; childIndex < sequence.size(); childIndex++)
846 nodeQueue->push(sequence[childIndex]);
850 bool TIntermSelection::replaceChildNode(
851 TIntermNode *original, TIntermNode *replacement)
853 REPLACE_IF_IS(condition, TIntermTyped, original, replacement);
854 REPLACE_IF_IS(trueBlock, TIntermNode, original, replacement);
855 REPLACE_IF_IS(falseBlock, TIntermNode, original, replacement);
859 void TIntermSelection::enqueueChildren(std::queue<TIntermNode*> *nodeQueue) const
863 nodeQueue->push(condition);
867 nodeQueue->push(trueBlock);
871 nodeQueue->push(falseBlock);
876 // Say whether or not an operation node changes the value of a variable.
878 bool TIntermOperator::isAssignment() const
881 case EOpPostIncrement:
882 case EOpPostDecrement:
883 case EOpPreIncrement:
884 case EOpPreDecrement:
889 case EOpVectorTimesMatrixAssign:
890 case EOpVectorTimesScalarAssign:
891 case EOpMatrixTimesScalarAssign:
892 case EOpMatrixTimesMatrixAssign:
901 // returns true if the operator is for one of the constructors
903 bool TIntermOperator::isConstructor() const
906 case EOpConstructVec2:
907 case EOpConstructVec3:
908 case EOpConstructVec4:
909 case EOpConstructMat2:
910 case EOpConstructMat3:
911 case EOpConstructMat4:
912 case EOpConstructFloat:
913 case EOpConstructIVec2:
914 case EOpConstructIVec3:
915 case EOpConstructIVec4:
916 case EOpConstructInt:
917 case EOpConstructBVec2:
918 case EOpConstructBVec3:
919 case EOpConstructBVec4:
920 case EOpConstructBool:
921 case EOpConstructStruct:
929 // Make sure the type of a unary operator is appropriate for its
930 // combination of operation and operand type.
932 // Returns false in nothing makes sense.
934 bool TIntermUnary::promote(TInfoSink&)
938 if (operand->getBasicType() != EbtBool)
942 case EOpPostIncrement:
943 case EOpPostDecrement:
944 case EOpPreIncrement:
945 case EOpPreDecrement:
946 if (operand->getBasicType() == EbtBool)
950 // operators for built-ins are already type checked against their prototype
953 case EOpVectorLogicalNot:
957 if (operand->getBasicType() != EbtFloat)
961 setType(operand->getType());
962 type.setQualifier(EvqTemporary);
968 // Establishes the type of the resultant operation, as well as
969 // makes the operator the correct one for the operands.
971 // Returns false if operator can't work on operands.
973 bool TIntermBinary::promote(TInfoSink& infoSink)
975 // This function only handles scalars, vectors, and matrices.
976 if (left->isArray() || right->isArray()) {
977 infoSink.info.message(EPrefixInternalError, getLine(), "Invalid operation for arrays");
981 // GLSL ES 2.0 does not support implicit type casting.
982 // So the basic type should always match.
983 if (left->getBasicType() != right->getBasicType())
987 // Base assumption: just make the type the same as the left
988 // operand. Then only deviations from this need be coded.
990 setType(left->getType());
992 // The result gets promoted to the highest precision.
993 TPrecision higherPrecision = GetHigherPrecision(left->getPrecision(), right->getPrecision());
994 getTypePointer()->setPrecision(higherPrecision);
996 // Binary operations results in temporary variables unless both
997 // operands are const.
998 if (left->getQualifier() != EvqConst || right->getQualifier() != EvqConst) {
999 getTypePointer()->setQualifier(EvqTemporary);
1002 int size = std::max(left->getNominalSize(), right->getNominalSize());
1005 // All scalars. Code after this test assumes this case is removed!
1010 // Promote to conditional
1015 case EOpGreaterThan:
1016 case EOpLessThanEqual:
1017 case EOpGreaterThanEqual:
1018 setType(TType(EbtBool, EbpUndefined));
1022 // And and Or operate on conditionals
1026 // Both operands must be of type bool.
1027 if (left->getBasicType() != EbtBool || right->getBasicType() != EbtBool)
1029 setType(TType(EbtBool, EbpUndefined));
1038 // If we reach here, at least one of the operands is vector or matrix.
1039 // The other operand could be a scalar, vector, or matrix.
1040 // Are the sizes compatible?
1042 if (left->getNominalSize() != right->getNominalSize()) {
1043 // If the nominal size of operands do not match:
1044 // One of them must be scalar.
1045 if (left->getNominalSize() != 1 && right->getNominalSize() != 1)
1047 // Operator cannot be of type pure assignment.
1048 if (op == EOpAssign || op == EOpInitialize)
1053 // Can these two operands be combined?
1055 TBasicType basicType = left->getBasicType();
1058 if (!left->isMatrix() && right->isMatrix()) {
1059 if (left->isVector())
1060 op = EOpVectorTimesMatrix;
1062 op = EOpMatrixTimesScalar;
1063 setType(TType(basicType, higherPrecision, EvqTemporary, size, true));
1065 } else if (left->isMatrix() && !right->isMatrix()) {
1066 if (right->isVector()) {
1067 op = EOpMatrixTimesVector;
1068 setType(TType(basicType, higherPrecision, EvqTemporary, size, false));
1070 op = EOpMatrixTimesScalar;
1072 } else if (left->isMatrix() && right->isMatrix()) {
1073 op = EOpMatrixTimesMatrix;
1074 } else if (!left->isMatrix() && !right->isMatrix()) {
1075 if (left->isVector() && right->isVector()) {
1076 // leave as component product
1077 } else if (left->isVector() || right->isVector()) {
1078 op = EOpVectorTimesScalar;
1079 setType(TType(basicType, higherPrecision, EvqTemporary, size, false));
1082 infoSink.info.message(EPrefixInternalError, getLine(), "Missing elses");
1087 if (!left->isMatrix() && right->isMatrix()) {
1088 if (left->isVector())
1089 op = EOpVectorTimesMatrixAssign;
1093 } else if (left->isMatrix() && !right->isMatrix()) {
1094 if (right->isVector()) {
1097 op = EOpMatrixTimesScalarAssign;
1099 } else if (left->isMatrix() && right->isMatrix()) {
1100 op = EOpMatrixTimesMatrixAssign;
1101 } else if (!left->isMatrix() && !right->isMatrix()) {
1102 if (left->isVector() && right->isVector()) {
1103 // leave as component product
1104 } else if (left->isVector() || right->isVector()) {
1105 if (! left->isVector())
1107 op = EOpVectorTimesScalarAssign;
1108 setType(TType(basicType, higherPrecision, EvqTemporary, size, false));
1111 infoSink.info.message(EPrefixInternalError, getLine(), "Missing elses");
1124 if ((left->isMatrix() && right->isVector()) ||
1125 (left->isVector() && right->isMatrix()))
1127 setType(TType(basicType, higherPrecision, EvqTemporary, size, left->isMatrix() || right->isMatrix()));
1133 case EOpGreaterThan:
1134 case EOpLessThanEqual:
1135 case EOpGreaterThanEqual:
1136 if ((left->isMatrix() && right->isVector()) ||
1137 (left->isVector() && right->isMatrix()))
1139 setType(TType(EbtBool, EbpUndefined));
1149 bool CompareStruct(const TType& leftNodeType, ConstantUnion* rightUnionArray, ConstantUnion* leftUnionArray)
1151 const TFieldList& fields = leftNodeType.getStruct()->fields();
1153 size_t structSize = fields.size();
1156 for (size_t j = 0; j < structSize; j++) {
1157 size_t size = fields[j]->type()->getObjectSize();
1158 for (size_t i = 0; i < size; i++) {
1159 if (fields[j]->type()->getBasicType() == EbtStruct) {
1160 if (!CompareStructure(*(fields[j]->type()), &rightUnionArray[index], &leftUnionArray[index]))
1163 if (leftUnionArray[index] != rightUnionArray[index])
1172 bool CompareStructure(const TType& leftNodeType, ConstantUnion* rightUnionArray, ConstantUnion* leftUnionArray)
1174 if (leftNodeType.isArray()) {
1175 TType typeWithoutArrayness = leftNodeType;
1176 typeWithoutArrayness.clearArrayness();
1178 size_t arraySize = leftNodeType.getArraySize();
1180 for (size_t i = 0; i < arraySize; ++i) {
1181 size_t offset = typeWithoutArrayness.getObjectSize() * i;
1182 if (!CompareStruct(typeWithoutArrayness, &rightUnionArray[offset], &leftUnionArray[offset]))
1186 return CompareStruct(leftNodeType, rightUnionArray, leftUnionArray);
1192 // The fold functions see if an operation on a constant can be done in place,
1193 // without generating run-time code.
1195 // Returns the node to keep using, which may or may not be the node passed in.
1198 TIntermTyped* TIntermConstantUnion::fold(TOperator op, TIntermTyped* constantNode, TInfoSink& infoSink)
1200 ConstantUnion *unionArray = getUnionArrayPointer();
1205 size_t objectSize = getType().getObjectSize();
1207 if (constantNode) { // binary operations
1208 TIntermConstantUnion *node = constantNode->getAsConstantUnion();
1209 ConstantUnion *rightUnionArray = node->getUnionArrayPointer();
1210 TType returnType = getType();
1212 if (!rightUnionArray)
1215 // for a case like float f = 1.2 + vec4(2,3,4,5);
1216 if (constantNode->getType().getObjectSize() == 1 && objectSize > 1) {
1217 rightUnionArray = new ConstantUnion[objectSize];
1218 for (size_t i = 0; i < objectSize; ++i)
1219 rightUnionArray[i] = *node->getUnionArrayPointer();
1220 returnType = getType();
1221 } else if (constantNode->getType().getObjectSize() > 1 && objectSize == 1) {
1222 // for a case like float f = vec4(2,3,4,5) + 1.2;
1223 unionArray = new ConstantUnion[constantNode->getType().getObjectSize()];
1224 for (size_t i = 0; i < constantNode->getType().getObjectSize(); ++i)
1225 unionArray[i] = *getUnionArrayPointer();
1226 returnType = node->getType();
1227 objectSize = constantNode->getType().getObjectSize();
1230 ConstantUnion* tempConstArray = 0;
1231 TIntermConstantUnion *tempNode;
1233 bool boolNodeFlag = false;
1236 tempConstArray = new ConstantUnion[objectSize];
1237 {// support MSVC++6.0
1238 for (size_t i = 0; i < objectSize; i++)
1239 tempConstArray[i] = unionArray[i] + rightUnionArray[i];
1243 tempConstArray = new ConstantUnion[objectSize];
1244 {// support MSVC++6.0
1245 for (size_t i = 0; i < objectSize; i++)
1246 tempConstArray[i] = unionArray[i] - rightUnionArray[i];
1251 case EOpVectorTimesScalar:
1252 case EOpMatrixTimesScalar:
1253 tempConstArray = new ConstantUnion[objectSize];
1254 {// support MSVC++6.0
1255 for (size_t i = 0; i < objectSize; i++)
1256 tempConstArray[i] = unionArray[i] * rightUnionArray[i];
1259 case EOpMatrixTimesMatrix:
1260 if (getType().getBasicType() != EbtFloat || node->getBasicType() != EbtFloat) {
1261 infoSink.info.message(EPrefixInternalError, getLine(), "Constant Folding cannot be done for matrix multiply");
1264 {// support MSVC++6.0
1265 int size = getNominalSize();
1266 tempConstArray = new ConstantUnion[size*size];
1267 for (int row = 0; row < size; row++) {
1268 for (int column = 0; column < size; column++) {
1269 tempConstArray[size * column + row].setFConst(0.0f);
1270 for (int i = 0; i < size; i++) {
1271 tempConstArray[size * column + row].setFConst(tempConstArray[size * column + row].getFConst() + unionArray[i * size + row].getFConst() * (rightUnionArray[column * size + i].getFConst()));
1278 tempConstArray = new ConstantUnion[objectSize];
1279 {// support MSVC++6.0
1280 for (size_t i = 0; i < objectSize; i++) {
1281 switch (getType().getBasicType()) {
1283 if (rightUnionArray[i] == 0.0f) {
1284 infoSink.info.message(EPrefixWarning, getLine(), "Divide by zero error during constant folding");
1285 tempConstArray[i].setFConst(unionArray[i].getFConst() < 0 ? -FLT_MAX : FLT_MAX);
1287 tempConstArray[i].setFConst(unionArray[i].getFConst() / rightUnionArray[i].getFConst());
1291 if (rightUnionArray[i] == 0) {
1292 infoSink.info.message(EPrefixWarning, getLine(), "Divide by zero error during constant folding");
1293 tempConstArray[i].setIConst(INT_MAX);
1295 tempConstArray[i].setIConst(unionArray[i].getIConst() / rightUnionArray[i].getIConst());
1298 infoSink.info.message(EPrefixInternalError, getLine(), "Constant folding cannot be done for \"/\"");
1305 case EOpMatrixTimesVector:
1306 if (node->getBasicType() != EbtFloat) {
1307 infoSink.info.message(EPrefixInternalError, getLine(), "Constant Folding cannot be done for matrix times vector");
1310 tempConstArray = new ConstantUnion[getNominalSize()];
1312 {// support MSVC++6.0
1313 for (int size = getNominalSize(), i = 0; i < size; i++) {
1314 tempConstArray[i].setFConst(0.0f);
1315 for (int j = 0; j < size; j++) {
1316 tempConstArray[i].setFConst(tempConstArray[i].getFConst() + ((unionArray[j*size + i].getFConst()) * rightUnionArray[j].getFConst()));
1321 tempNode = new TIntermConstantUnion(tempConstArray, node->getType());
1322 tempNode->setLine(getLine());
1326 case EOpVectorTimesMatrix:
1327 if (getType().getBasicType() != EbtFloat) {
1328 infoSink.info.message(EPrefixInternalError, getLine(), "Constant Folding cannot be done for vector times matrix");
1332 tempConstArray = new ConstantUnion[getNominalSize()];
1333 {// support MSVC++6.0
1334 for (int size = getNominalSize(), i = 0; i < size; i++) {
1335 tempConstArray[i].setFConst(0.0f);
1336 for (int j = 0; j < size; j++) {
1337 tempConstArray[i].setFConst(tempConstArray[i].getFConst() + ((unionArray[j].getFConst()) * rightUnionArray[i*size + j].getFConst()));
1343 case EOpLogicalAnd: // this code is written for possible future use, will not get executed currently
1344 tempConstArray = new ConstantUnion[objectSize];
1345 {// support MSVC++6.0
1346 for (size_t i = 0; i < objectSize; i++)
1347 tempConstArray[i] = unionArray[i] && rightUnionArray[i];
1351 case EOpLogicalOr: // this code is written for possible future use, will not get executed currently
1352 tempConstArray = new ConstantUnion[objectSize];
1353 {// support MSVC++6.0
1354 for (size_t i = 0; i < objectSize; i++)
1355 tempConstArray[i] = unionArray[i] || rightUnionArray[i];
1360 tempConstArray = new ConstantUnion[objectSize];
1361 {// support MSVC++6.0
1362 for (size_t i = 0; i < objectSize; i++)
1363 switch (getType().getBasicType()) {
1364 case EbtBool: tempConstArray[i].setBConst((unionArray[i] == rightUnionArray[i]) ? false : true); break;
1365 default: assert(false && "Default missing");
1371 assert(objectSize == 1);
1372 tempConstArray = new ConstantUnion[1];
1373 tempConstArray->setBConst(*unionArray < *rightUnionArray);
1374 returnType = TType(EbtBool, EbpUndefined, EvqConst);
1376 case EOpGreaterThan:
1377 assert(objectSize == 1);
1378 tempConstArray = new ConstantUnion[1];
1379 tempConstArray->setBConst(*unionArray > *rightUnionArray);
1380 returnType = TType(EbtBool, EbpUndefined, EvqConst);
1382 case EOpLessThanEqual:
1384 assert(objectSize == 1);
1385 ConstantUnion constant;
1386 constant.setBConst(*unionArray > *rightUnionArray);
1387 tempConstArray = new ConstantUnion[1];
1388 tempConstArray->setBConst(!constant.getBConst());
1389 returnType = TType(EbtBool, EbpUndefined, EvqConst);
1392 case EOpGreaterThanEqual:
1394 assert(objectSize == 1);
1395 ConstantUnion constant;
1396 constant.setBConst(*unionArray < *rightUnionArray);
1397 tempConstArray = new ConstantUnion[1];
1398 tempConstArray->setBConst(!constant.getBConst());
1399 returnType = TType(EbtBool, EbpUndefined, EvqConst);
1404 if (getType().getBasicType() == EbtStruct) {
1405 if (!CompareStructure(node->getType(), node->getUnionArrayPointer(), unionArray))
1406 boolNodeFlag = true;
1408 for (size_t i = 0; i < objectSize; i++) {
1409 if (unionArray[i] != rightUnionArray[i]) {
1410 boolNodeFlag = true;
1411 break; // break out of for loop
1416 tempConstArray = new ConstantUnion[1];
1417 if (!boolNodeFlag) {
1418 tempConstArray->setBConst(true);
1421 tempConstArray->setBConst(false);
1424 tempNode = new TIntermConstantUnion(tempConstArray, TType(EbtBool, EbpUndefined, EvqConst));
1425 tempNode->setLine(getLine());
1430 if (getType().getBasicType() == EbtStruct) {
1431 if (CompareStructure(node->getType(), node->getUnionArrayPointer(), unionArray))
1432 boolNodeFlag = true;
1434 for (size_t i = 0; i < objectSize; i++) {
1435 if (unionArray[i] == rightUnionArray[i]) {
1436 boolNodeFlag = true;
1437 break; // break out of for loop
1442 tempConstArray = new ConstantUnion[1];
1443 if (!boolNodeFlag) {
1444 tempConstArray->setBConst(true);
1447 tempConstArray->setBConst(false);
1450 tempNode = new TIntermConstantUnion(tempConstArray, TType(EbtBool, EbpUndefined, EvqConst));
1451 tempNode->setLine(getLine());
1456 infoSink.info.message(EPrefixInternalError, getLine(), "Invalid operator for constant folding");
1459 tempNode = new TIntermConstantUnion(tempConstArray, returnType);
1460 tempNode->setLine(getLine());
1465 // Do unary operations
1467 TIntermConstantUnion *newNode = 0;
1468 ConstantUnion* tempConstArray = new ConstantUnion[objectSize];
1469 for (size_t i = 0; i < objectSize; i++) {
1472 switch (getType().getBasicType()) {
1473 case EbtFloat: tempConstArray[i].setFConst(-unionArray[i].getFConst()); break;
1474 case EbtInt: tempConstArray[i].setIConst(-unionArray[i].getIConst()); break;
1476 infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant");
1480 case EOpLogicalNot: // this code is written for possible future use, will not get executed currently
1481 switch (getType().getBasicType()) {
1482 case EbtBool: tempConstArray[i].setBConst(!unionArray[i].getBConst()); break;
1484 infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant");
1492 newNode = new TIntermConstantUnion(tempConstArray, getType());
1493 newNode->setLine(getLine());
1498 TIntermTyped* TIntermediate::promoteConstantUnion(TBasicType promoteTo, TIntermConstantUnion* node)
1500 size_t size = node->getType().getObjectSize();
1502 ConstantUnion *leftUnionArray = new ConstantUnion[size];
1504 for (size_t i = 0; i < size; i++) {
1506 switch (promoteTo) {
1508 switch (node->getType().getBasicType()) {
1510 leftUnionArray[i].setFConst(static_cast<float>(node->getIConst(i)));
1513 leftUnionArray[i].setFConst(static_cast<float>(node->getBConst(i)));
1516 leftUnionArray[i].setFConst(static_cast<float>(node->getFConst(i)));
1519 infoSink.info.message(EPrefixInternalError, node->getLine(), "Cannot promote");
1524 switch (node->getType().getBasicType()) {
1526 leftUnionArray[i].setIConst(static_cast<int>(node->getIConst(i)));
1529 leftUnionArray[i].setIConst(static_cast<int>(node->getBConst(i)));
1532 leftUnionArray[i].setIConst(static_cast<int>(node->getFConst(i)));
1535 infoSink.info.message(EPrefixInternalError, node->getLine(), "Cannot promote");
1540 switch (node->getType().getBasicType()) {
1542 leftUnionArray[i].setBConst(node->getIConst(i) != 0);
1545 leftUnionArray[i].setBConst(node->getBConst(i));
1548 leftUnionArray[i].setBConst(node->getFConst(i) != 0.0f);
1551 infoSink.info.message(EPrefixInternalError, node->getLine(), "Cannot promote");
1557 infoSink.info.message(EPrefixInternalError, node->getLine(), "Incorrect data type found");
1563 const TType& t = node->getType();
1565 return addConstantUnion(leftUnionArray, TType(promoteTo, t.getPrecision(), t.getQualifier(), t.getNominalSize(), t.isMatrix(), t.isArray()), node->getLine());
1569 TString TIntermTraverser::hash(const TString& name, ShHashFunction64 hashFunction)
1571 if (hashFunction == NULL || name.empty())
1573 khronos_uint64_t number = (*hashFunction)(name.c_str(), name.length());
1574 TStringStream stream;
1575 stream << HASHED_NAME_PREFIX << std::hex << number;
1576 TString hashedName = stream.str();