Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 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 | // RewriteElseBlocks.cpp: Implementation for tree transform to change |
| 7 | // all if-else blocks to if-if blocks. |
| 8 | // |
| 9 | |
| 10 | #include "compiler/translator/RewriteElseBlocks.h" |
| 11 | #include "compiler/translator/NodeSearch.h" |
| 12 | #include "compiler/translator/SymbolTable.h" |
| 13 | |
| 14 | namespace sh |
| 15 | { |
| 16 | |
Jamie Madill | 4836d22 | 2014-07-24 06:55:51 -0400 | [diff] [blame^] | 17 | namespace |
| 18 | { |
| 19 | |
| 20 | class ElseBlockRewriter : public TIntermTraverser |
| 21 | { |
| 22 | public: |
| 23 | ElseBlockRewriter(); |
| 24 | |
| 25 | protected: |
| 26 | bool visitAggregate(Visit visit, TIntermAggregate *aggregate); |
| 27 | |
| 28 | private: |
| 29 | int mTemporaryIndex; |
| 30 | const TType *mFunctionType; |
| 31 | |
| 32 | TIntermNode *rewriteSelection(TIntermSelection *selection); |
| 33 | }; |
| 34 | |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 35 | TIntermSymbol *MakeNewTemporary(const TString &name, TBasicType type) |
| 36 | { |
| 37 | TType variableType(type, EbpHigh, EvqInternal); |
| 38 | return new TIntermSymbol(-1, name, variableType); |
| 39 | } |
| 40 | |
| 41 | TIntermBinary *MakeNewBinary(TOperator op, TIntermTyped *left, TIntermTyped *right, const TType &resultType) |
| 42 | { |
| 43 | TIntermBinary *binary = new TIntermBinary(op); |
| 44 | binary->setLeft(left); |
| 45 | binary->setRight(right); |
| 46 | binary->setType(resultType); |
| 47 | return binary; |
| 48 | } |
| 49 | |
| 50 | TIntermUnary *MakeNewUnary(TOperator op, TIntermTyped *operand) |
| 51 | { |
| 52 | TIntermUnary *unary = new TIntermUnary(op, operand->getType()); |
| 53 | unary->setOperand(operand); |
| 54 | return unary; |
| 55 | } |
| 56 | |
Jamie Madill | 787fc03 | 2014-07-07 12:49:45 -0400 | [diff] [blame] | 57 | ElseBlockRewriter::ElseBlockRewriter() |
| 58 | : TIntermTraverser(true, false, true, false), |
| 59 | mTemporaryIndex(0), |
| 60 | mFunctionType(NULL) |
| 61 | {} |
| 62 | |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 63 | bool ElseBlockRewriter::visitAggregate(Visit visit, TIntermAggregate *node) |
| 64 | { |
| 65 | switch (node->getOp()) |
| 66 | { |
| 67 | case EOpSequence: |
Jamie Madill | 787fc03 | 2014-07-07 12:49:45 -0400 | [diff] [blame] | 68 | if (visit == PostVisit) |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 69 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 70 | for (size_t statementIndex = 0; statementIndex != node->getSequence()->size(); statementIndex++) |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 71 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 72 | TIntermNode *statement = (*node->getSequence())[statementIndex]; |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 73 | TIntermSelection *selection = statement->getAsSelectionNode(); |
| 74 | if (selection && selection->getFalseBlock() != NULL) |
| 75 | { |
Jamie Madill | ed3eef1 | 2014-02-26 09:47:11 -0500 | [diff] [blame] | 76 | // Check for if / else if |
| 77 | TIntermSelection *elseIfBranch = selection->getFalseBlock()->getAsSelectionNode(); |
| 78 | if (elseIfBranch) |
| 79 | { |
| 80 | selection->replaceChildNode(elseIfBranch, rewriteSelection(elseIfBranch)); |
| 81 | delete elseIfBranch; |
| 82 | } |
| 83 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 84 | (*node->getSequence())[statementIndex] = rewriteSelection(selection); |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 85 | delete selection; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | break; |
| 90 | |
Jamie Madill | 787fc03 | 2014-07-07 12:49:45 -0400 | [diff] [blame] | 91 | case EOpFunction: |
| 92 | // Store the current function context (see comment below) |
| 93 | mFunctionType = ((visit == PreVisit) ? &node->getType() : NULL); |
| 94 | break; |
| 95 | |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 96 | default: break; |
| 97 | } |
| 98 | |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | TIntermNode *ElseBlockRewriter::rewriteSelection(TIntermSelection *selection) |
| 103 | { |
Jamie Madill | 4836d22 | 2014-07-24 06:55:51 -0400 | [diff] [blame^] | 104 | ASSERT(selection != NULL); |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 105 | |
| 106 | TString temporaryName = "cond_" + str(mTemporaryIndex++); |
| 107 | TIntermTyped *typedCondition = selection->getCondition()->getAsTyped(); |
| 108 | TType resultType(EbtBool, EbpUndefined); |
Jamie Madill | 4836d22 | 2014-07-24 06:55:51 -0400 | [diff] [blame^] | 109 | TIntermSymbol *conditionSymbolInit = MakeNewTemporary(temporaryName, EbtBool); |
| 110 | TIntermBinary *storeCondition = MakeNewBinary(EOpInitialize, conditionSymbolInit, |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 111 | typedCondition, resultType); |
Jamie Madill | 787fc03 | 2014-07-07 12:49:45 -0400 | [diff] [blame] | 112 | TIntermNode *negatedElse = NULL; |
| 113 | |
Jamie Madill | 4836d22 | 2014-07-24 06:55:51 -0400 | [diff] [blame^] | 114 | TIntermSelection *falseBlock = NULL; |
| 115 | |
| 116 | if (selection->getFalseBlock()) |
Jamie Madill | 787fc03 | 2014-07-07 12:49:45 -0400 | [diff] [blame] | 117 | { |
Jamie Madill | 4836d22 | 2014-07-24 06:55:51 -0400 | [diff] [blame^] | 118 | // crbug.com/346463 |
| 119 | // D3D generates error messages claiming a function has no return value, when rewriting |
| 120 | // an if-else clause that returns something non-void in a function. By appending dummy |
| 121 | // returns (that are unreachable) we can silence this compile error. |
| 122 | if (mFunctionType && mFunctionType->getBasicType() != EbtVoid) |
| 123 | { |
| 124 | TString typeString = mFunctionType->getStruct() ? mFunctionType->getStruct()->name() : |
| 125 | mFunctionType->getBasicString(); |
| 126 | TString rawText = "return (" + typeString + ")0"; |
| 127 | negatedElse = new TIntermRaw(*mFunctionType, rawText); |
| 128 | } |
| 129 | |
| 130 | TIntermSymbol *conditionSymbolElse = MakeNewTemporary(temporaryName, EbtBool); |
| 131 | TIntermUnary *negatedCondition = MakeNewUnary(EOpLogicalNot, conditionSymbolElse); |
| 132 | falseBlock = new TIntermSelection(negatedCondition, |
| 133 | selection->getFalseBlock(), negatedElse); |
Jamie Madill | 787fc03 | 2014-07-07 12:49:45 -0400 | [diff] [blame] | 134 | } |
| 135 | |
Jamie Madill | 4836d22 | 2014-07-24 06:55:51 -0400 | [diff] [blame^] | 136 | TIntermSymbol *conditionSymbolSel = MakeNewTemporary(temporaryName, EbtBool); |
| 137 | TIntermSelection *newSelection = new TIntermSelection(conditionSymbolSel, |
| 138 | selection->getTrueBlock(), falseBlock); |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 139 | |
| 140 | TIntermAggregate *declaration = new TIntermAggregate(EOpDeclaration); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 141 | declaration->getSequence()->push_back(storeCondition); |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 142 | |
| 143 | TIntermAggregate *block = new TIntermAggregate(EOpSequence); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 144 | block->getSequence()->push_back(declaration); |
Jamie Madill | 4836d22 | 2014-07-24 06:55:51 -0400 | [diff] [blame^] | 145 | block->getSequence()->push_back(newSelection); |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 146 | |
| 147 | return block; |
| 148 | } |
| 149 | |
Jamie Madill | 4836d22 | 2014-07-24 06:55:51 -0400 | [diff] [blame^] | 150 | } |
| 151 | |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 152 | void RewriteElseBlocks(TIntermNode *node) |
| 153 | { |
| 154 | ElseBlockRewriter rewriter; |
| 155 | node->traverse(&rewriter); |
| 156 | } |
| 157 | |
| 158 | } |