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.
8 // Definition of the in-memory high-level intermediate representation
9 // of shaders. This is a tree that parser creates.
11 // Nodes in the tree are defined as a hierarchy of classes derived from
12 // TIntermNode. Each is a node in a tree. There is no preset branching factor;
13 // each node can have it's own type of list of children.
16 #ifndef COMPILER_TRANSLATOR_INTERMEDIATE_H_
17 #define COMPILER_TRANSLATOR_INTERMEDIATE_H_
19 #include "GLSLANG/ShaderLang.h"
24 #include "compiler/translator/Common.h"
25 #include "compiler/translator/Types.h"
26 #include "compiler/translator/ConstantUnion.h"
29 // Operators used by the high-level (parse tree) representation.
33 EOpNull, // if in a node, should only mean a node is still being built
34 EOpSequence, // denotes a list of statements, or parameters, etc.
36 EOpFunction, // For function definition
37 EOpParameters, // an aggregate listing the parameters to a function
85 EOpIndexDirectInterfaceBlock,
90 // Built-in functions potentially mapped to operators
132 EOpDFdx, // Fragment only, OES_standard_derivatives extension
133 EOpDFdy, // Fragment only, OES_standard_derivatives extension
134 EOpFwidth, // Fragment only, OES_standard_derivatives extension
136 EOpMatrixTimesMatrix,
145 EOpKill, // Fragment only
184 EOpVectorTimesMatrixAssign,
185 EOpVectorTimesScalarAssign,
186 EOpMatrixTimesScalarAssign,
187 EOpMatrixTimesMatrixAssign,
191 class TIntermTraverser;
192 class TIntermAggregate;
195 class TIntermConstantUnion;
196 class TIntermSelection;
204 // Base class for the tree nodes
209 POOL_ALLOCATOR_NEW_DELETE();
212 // TODO: Move this to TSourceLoc constructor
213 // after getting rid of TPublicType.
214 mLine.first_file = mLine.last_file = 0;
215 mLine.first_line = mLine.last_line = 0;
217 virtual ~TIntermNode() { }
219 const TSourceLoc &getLine() const { return mLine; }
220 void setLine(const TSourceLoc &l) { mLine = l; }
222 virtual void traverse(TIntermTraverser *) = 0;
223 virtual TIntermTyped *getAsTyped() { return 0; }
224 virtual TIntermConstantUnion *getAsConstantUnion() { return 0; }
225 virtual TIntermAggregate *getAsAggregate() { return 0; }
226 virtual TIntermBinary *getAsBinaryNode() { return 0; }
227 virtual TIntermUnary *getAsUnaryNode() { return 0; }
228 virtual TIntermSelection *getAsSelectionNode() { return 0; }
229 virtual TIntermSymbol *getAsSymbolNode() { return 0; }
230 virtual TIntermLoop *getAsLoopNode() { return 0; }
231 virtual TIntermRaw *getAsRawNode() { return 0; }
233 // Replace a child node. Return true if |original| is a child
234 // node and it is replaced; otherwise, return false.
235 virtual bool replaceChildNode(
236 TIntermNode *original, TIntermNode *replacement) = 0;
238 // For traversing a tree in no particular order, but using
240 virtual void enqueueChildren(std::queue<TIntermNode *> *nodeQueue) const = 0;
247 // This is just to help yacc.
249 struct TIntermNodePair
256 // Intermediate class for nodes that have a type.
258 class TIntermTyped : public TIntermNode
261 TIntermTyped(const TType &t) : mType(t) { }
262 virtual TIntermTyped *getAsTyped() { return this; }
264 virtual bool hasSideEffects() const = 0;
266 void setType(const TType &t) { mType = t; }
267 const TType &getType() const { return mType; }
268 TType *getTypePointer() { return &mType; }
270 TBasicType getBasicType() const { return mType.getBasicType(); }
271 TQualifier getQualifier() const { return mType.getQualifier(); }
272 TPrecision getPrecision() const { return mType.getPrecision(); }
273 int getCols() const { return mType.getCols(); }
274 int getRows() const { return mType.getRows(); }
275 int getNominalSize() const { return mType.getNominalSize(); }
276 int getSecondarySize() const { return mType.getSecondarySize(); }
278 bool isInterfaceBlock() const { return mType.isInterfaceBlock(); }
279 bool isMatrix() const { return mType.isMatrix(); }
280 bool isArray() const { return mType.isArray(); }
281 bool isVector() const { return mType.isVector(); }
282 bool isScalar() const { return mType.isScalar(); }
283 bool isScalarInt() const { return mType.isScalarInt(); }
284 const char *getBasicString() const { return mType.getBasicString(); }
285 const char *getQualifierString() const { return mType.getQualifierString(); }
286 TString getCompleteString() const { return mType.getCompleteString(); }
288 int getArraySize() const { return mType.getArraySize(); }
295 // Handle for, do-while, and while loops.
304 class TIntermLoop : public TIntermNode
307 TIntermLoop(TLoopType type,
308 TIntermNode *init, TIntermTyped *cond, TIntermTyped *expr,
315 mUnrollFlag(false) { }
317 virtual TIntermLoop *getAsLoopNode() { return this; }
318 virtual void traverse(TIntermTraverser *);
319 virtual bool replaceChildNode(
320 TIntermNode *original, TIntermNode *replacement);
322 TLoopType getType() const { return mType; }
323 TIntermNode *getInit() { return mInit; }
324 TIntermTyped *getCondition() { return mCond; }
325 TIntermTyped *getExpression() { return mExpr; }
326 TIntermNode *getBody() { return mBody; }
328 void setUnrollFlag(bool flag) { mUnrollFlag = flag; }
329 bool getUnrollFlag() const { return mUnrollFlag; }
331 virtual void enqueueChildren(std::queue<TIntermNode *> *nodeQueue) const;
335 TIntermNode *mInit; // for-loop initialization
336 TIntermTyped *mCond; // loop exit condition
337 TIntermTyped *mExpr; // for-loop expression
338 TIntermNode *mBody; // loop body
340 bool mUnrollFlag; // Whether the loop should be unrolled or not.
344 // Handle break, continue, return, and kill.
346 class TIntermBranch : public TIntermNode
349 TIntermBranch(TOperator op, TIntermTyped *e)
353 virtual void traverse(TIntermTraverser *);
354 virtual bool replaceChildNode(
355 TIntermNode *original, TIntermNode *replacement);
357 TOperator getFlowOp() { return mFlowOp; }
358 TIntermTyped* getExpression() { return mExpression; }
360 virtual void enqueueChildren(std::queue<TIntermNode *> *nodeQueue) const;
364 TIntermTyped *mExpression; // non-zero except for "return exp;" statements
368 // Nodes that correspond to symbols or constants in the source code.
370 class TIntermSymbol : public TIntermTyped
373 // if symbol is initialized as symbol(sym), the memory comes from the poolallocator of sym.
374 // If sym comes from per process globalpoolallocator, then it causes increased memory usage
375 // per compile it is essential to use "symbol = sym" to assign to symbol
376 TIntermSymbol(int id, const TString &symbol, const TType &type)
377 : TIntermTyped(type),
383 virtual bool hasSideEffects() const { return false; }
385 int getId() const { return mId; }
386 const TString &getSymbol() const { return mSymbol; }
388 void setId(int newId) { mId = newId; }
390 virtual void traverse(TIntermTraverser *);
391 virtual TIntermSymbol *getAsSymbolNode() { return this; }
392 virtual bool replaceChildNode(TIntermNode *, TIntermNode *) { return false; }
394 virtual void enqueueChildren(std::queue<TIntermNode *> *nodeQueue) const {}
401 // A Raw node stores raw code, that the translator will insert verbatim
402 // into the output stream. Useful for transformation operations that make
403 // complex code that might not fit naturally into the GLSL model.
404 class TIntermRaw : public TIntermTyped
407 TIntermRaw(const TType &type, const TString &rawText)
408 : TIntermTyped(type),
409 mRawText(rawText) { }
411 virtual bool hasSideEffects() const { return false; }
413 TString getRawText() const { return mRawText; }
415 virtual void traverse(TIntermTraverser *);
417 virtual TIntermRaw *getAsRawNode() { return this; }
418 virtual bool replaceChildNode(TIntermNode *, TIntermNode *) { return false; }
419 virtual void enqueueChildren(std::queue<TIntermNode *> *nodeQueue) const {}
425 class TIntermConstantUnion : public TIntermTyped
428 TIntermConstantUnion(ConstantUnion *unionPointer, const TType &type)
429 : TIntermTyped(type),
430 mUnionArrayPointer(unionPointer) { }
432 virtual bool hasSideEffects() const { return false; }
434 ConstantUnion *getUnionArrayPointer() const { return mUnionArrayPointer; }
436 int getIConst(size_t index) const
438 return mUnionArrayPointer ? mUnionArrayPointer[index].getIConst() : 0;
440 unsigned int getUConst(size_t index) const
442 return mUnionArrayPointer ? mUnionArrayPointer[index].getUConst() : 0;
444 float getFConst(size_t index) const
446 return mUnionArrayPointer ? mUnionArrayPointer[index].getFConst() : 0.0f;
448 bool getBConst(size_t index) const
450 return mUnionArrayPointer ? mUnionArrayPointer[index].getBConst() : false;
453 virtual TIntermConstantUnion *getAsConstantUnion() { return this; }
454 virtual void traverse(TIntermTraverser *);
455 virtual bool replaceChildNode(TIntermNode *, TIntermNode *) { return false; }
457 TIntermTyped *fold(TOperator, TIntermTyped *, TInfoSink &);
459 virtual void enqueueChildren(std::queue<TIntermNode *> *nodeQueue) const {}
462 ConstantUnion *mUnionArrayPointer;
466 // Intermediate class for node types that hold operators.
468 class TIntermOperator : public TIntermTyped
471 TOperator getOp() const { return mOp; }
472 void setOp(TOperator op) { mOp = op; }
474 bool isAssignment() const;
475 bool isConstructor() const;
477 virtual bool hasSideEffects() const { return isAssignment(); }
480 TIntermOperator(TOperator op)
481 : TIntermTyped(TType(EbtFloat, EbpUndefined)),
483 TIntermOperator(TOperator op, const TType &type)
484 : TIntermTyped(type),
491 // Nodes for all the basic binary math operators.
493 class TIntermBinary : public TIntermOperator
496 TIntermBinary(TOperator op)
497 : TIntermOperator(op),
498 mAddIndexClamp(false) {}
500 virtual TIntermBinary *getAsBinaryNode() { return this; }
501 virtual void traverse(TIntermTraverser *);
502 virtual bool replaceChildNode(
503 TIntermNode *original, TIntermNode *replacement);
505 virtual bool hasSideEffects() const
507 return isAssignment() || mLeft->hasSideEffects() || mRight->hasSideEffects();
510 void setLeft(TIntermTyped *node) { mLeft = node; }
511 void setRight(TIntermTyped *node) { mRight = node; }
512 TIntermTyped *getLeft() const { return mLeft; }
513 TIntermTyped *getRight() const { return mRight; }
514 bool promote(TInfoSink &);
516 void setAddIndexClamp() { mAddIndexClamp = true; }
517 bool getAddIndexClamp() { return mAddIndexClamp; }
519 virtual void enqueueChildren(std::queue<TIntermNode *> *nodeQueue) const;
523 TIntermTyped* mRight;
525 // If set to true, wrap any EOpIndexIndirect with a clamp to bounds.
530 // Nodes for unary math operators.
532 class TIntermUnary : public TIntermOperator
535 TIntermUnary(TOperator op, const TType &type)
536 : TIntermOperator(op, type),
538 mUseEmulatedFunction(false) {}
539 TIntermUnary(TOperator op)
540 : TIntermOperator(op),
542 mUseEmulatedFunction(false) {}
544 virtual void traverse(TIntermTraverser *);
545 virtual TIntermUnary *getAsUnaryNode() { return this; }
546 virtual bool replaceChildNode(
547 TIntermNode *original, TIntermNode *replacement);
549 virtual bool hasSideEffects() const
551 return isAssignment() || mOperand->hasSideEffects();
554 void setOperand(TIntermTyped *operand) { mOperand = operand; }
555 TIntermTyped *getOperand() { return mOperand; }
556 bool promote(TInfoSink &);
558 void setUseEmulatedFunction() { mUseEmulatedFunction = true; }
559 bool getUseEmulatedFunction() { return mUseEmulatedFunction; }
561 virtual void enqueueChildren(std::queue<TIntermNode *> *nodeQueue) const;
564 TIntermTyped *mOperand;
566 // If set to true, replace the built-in function call with an emulated one
567 // to work around driver bugs.
568 bool mUseEmulatedFunction;
571 typedef TVector<TIntermNode *> TIntermSequence;
572 typedef TVector<int> TQualifierList;
575 // Nodes that operate on an arbitrary sized set of children.
577 class TIntermAggregate : public TIntermOperator
581 : TIntermOperator(EOpNull),
583 mUseEmulatedFunction(false) { }
584 TIntermAggregate(TOperator op)
585 : TIntermOperator(op),
586 mUseEmulatedFunction(false) { }
587 ~TIntermAggregate() { }
589 virtual TIntermAggregate *getAsAggregate() { return this; }
590 virtual void traverse(TIntermTraverser *);
591 virtual bool replaceChildNode(
592 TIntermNode *original, TIntermNode *replacement);
594 // Conservatively assume function calls and other aggregate operators have side-effects
595 virtual bool hasSideEffects() const { return true; }
597 TIntermSequence *getSequence() { return &mSequence; }
599 void setName(const TString &name) { mName = name; }
600 const TString &getName() const { return mName; }
602 void setUserDefined() { mUserDefined = true; }
603 bool isUserDefined() const { return mUserDefined; }
605 void setOptimize(bool optimize) { mOptimize = optimize; }
606 bool getOptimize() const { return mOptimize; }
607 void setDebug(bool debug) { mDebug = debug; }
608 bool getDebug() const { return mDebug; }
610 void setUseEmulatedFunction() { mUseEmulatedFunction = true; }
611 bool getUseEmulatedFunction() { return mUseEmulatedFunction; }
613 virtual void enqueueChildren(std::queue<TIntermNode *> *nodeQueue) const;
616 TIntermAggregate(const TIntermAggregate &); // disallow copy constructor
617 TIntermAggregate &operator=(const TIntermAggregate &); // disallow assignment operator
618 TIntermSequence mSequence;
620 bool mUserDefined; // used for user defined function names
625 // If set to true, replace the built-in function call with an emulated one
626 // to work around driver bugs.
627 bool mUseEmulatedFunction;
631 // For if tests. Simplified since there is no switch statement.
633 class TIntermSelection : public TIntermTyped
636 TIntermSelection(TIntermTyped *cond, TIntermNode *trueB, TIntermNode *falseB)
637 : TIntermTyped(TType(EbtVoid, EbpUndefined)),
640 mFalseBlock(falseB) {}
641 TIntermSelection(TIntermTyped *cond, TIntermNode *trueB, TIntermNode *falseB,
643 : TIntermTyped(type),
646 mFalseBlock(falseB) {}
648 virtual void traverse(TIntermTraverser *);
649 virtual bool replaceChildNode(
650 TIntermNode *original, TIntermNode *replacement);
652 // Conservatively assume selections have side-effects
653 virtual bool hasSideEffects() const { return true; }
655 bool usesTernaryOperator() const { return getBasicType() != EbtVoid; }
656 TIntermNode *getCondition() const { return mCondition; }
657 TIntermNode *getTrueBlock() const { return mTrueBlock; }
658 TIntermNode *getFalseBlock() const { return mFalseBlock; }
659 TIntermSelection *getAsSelectionNode() { return this; }
661 virtual void enqueueChildren(std::queue<TIntermNode *> *nodeQueue) const;
664 TIntermTyped *mCondition;
665 TIntermNode *mTrueBlock;
666 TIntermNode *mFalseBlock;
677 // For traversing the tree. User should derive from this,
678 // put their traversal specific data in it, and then pass
679 // it to a Traverse method.
681 // When using this, just fill in the methods for nodes you want visited.
682 // Return false from a pre-visit to skip visiting that node's subtree.
684 class TIntermTraverser
687 POOL_ALLOCATOR_NEW_DELETE();
688 // TODO(zmo): remove default values.
689 TIntermTraverser(bool preVisit = true, bool inVisit = false, bool postVisit = false,
690 bool rightToLeft = false)
691 : preVisit(preVisit),
693 postVisit(postVisit),
694 rightToLeft(rightToLeft),
697 virtual ~TIntermTraverser() {}
699 virtual void visitSymbol(TIntermSymbol *) {}
700 virtual void visitRaw(TIntermRaw *) {}
701 virtual void visitConstantUnion(TIntermConstantUnion *) {}
702 virtual bool visitBinary(Visit, TIntermBinary *) { return true; }
703 virtual bool visitUnary(Visit, TIntermUnary *) { return true; }
704 virtual bool visitSelection(Visit, TIntermSelection *) { return true; }
705 virtual bool visitAggregate(Visit, TIntermAggregate *) { return true; }
706 virtual bool visitLoop(Visit, TIntermLoop *) { return true; }
707 virtual bool visitBranch(Visit, TIntermBranch *) { return true; }
709 int getMaxDepth() const { return mMaxDepth; }
711 void incrementDepth(TIntermNode *current)
714 mMaxDepth = std::max(mMaxDepth, mDepth);
715 mPath.push_back(current);
718 void decrementDepth()
724 TIntermNode *getParentNode()
726 return mPath.size() == 0 ? NULL : mPath.back();
729 // Return the original name if hash function pointer is NULL;
730 // otherwise return the hashed name.
731 static TString hash(const TString& name, ShHashFunction64 hashFunction);
735 const bool postVisit;
736 const bool rightToLeft;
742 // All the nodes from root to the current node's parent during traversing.
743 TVector<TIntermNode *> mPath;
747 // For traversing the tree, and computing max depth.
748 // Takes a maximum depth limit to prevent stack overflow.
750 class TMaxDepthTraverser : public TIntermTraverser
753 POOL_ALLOCATOR_NEW_DELETE();
754 TMaxDepthTraverser(int depthLimit)
755 : TIntermTraverser(true, true, false, false),
756 mDepthLimit(depthLimit) { }
758 virtual bool visitBinary(Visit, TIntermBinary *) { return depthCheck(); }
759 virtual bool visitUnary(Visit, TIntermUnary *) { return depthCheck(); }
760 virtual bool visitSelection(Visit, TIntermSelection *) { return depthCheck(); }
761 virtual bool visitAggregate(Visit, TIntermAggregate *) { return depthCheck(); }
762 virtual bool visitLoop(Visit, TIntermLoop *) { return depthCheck(); }
763 virtual bool visitBranch(Visit, TIntermBranch *) { return depthCheck(); }
766 bool depthCheck() const { return mMaxDepth < mDepthLimit; }
771 #endif // COMPILER_TRANSLATOR_INTERMEDIATE_H_