Olli Etuaho | 822fa84 | 2015-04-16 14:26:10 +0300 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2002-2015 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 | // The SeparateArrayInitialization function splits each array initialization into a declaration and an assignment. |
| 7 | // Example: |
| 8 | // type[n] a = initializer; |
| 9 | // will effectively become |
| 10 | // type[n] a; |
| 11 | // a = initializer; |
Olli Etuaho | b1edc4f | 2015-11-02 17:20:03 +0200 | [diff] [blame] | 12 | // |
Olli Etuaho | 18b9deb | 2015-11-05 12:14:50 +0200 | [diff] [blame] | 13 | // Note that if the array is declared as const, the initialization may still be split, making the |
| 14 | // AST technically invalid. Because of that this transformation should only be used when subsequent |
| 15 | // stages don't care about const qualifiers. However, the initialization will not be split if the |
| 16 | // initializer can be written as a HLSL literal. |
Olli Etuaho | 822fa84 | 2015-04-16 14:26:10 +0300 | [diff] [blame] | 17 | |
| 18 | #include "compiler/translator/SeparateArrayInitialization.h" |
| 19 | |
| 20 | #include "compiler/translator/IntermNode.h" |
Olli Etuaho | 18b9deb | 2015-11-05 12:14:50 +0200 | [diff] [blame] | 21 | #include "compiler/translator/OutputHLSL.h" |
Olli Etuaho | 822fa84 | 2015-04-16 14:26:10 +0300 | [diff] [blame] | 22 | |
| 23 | namespace |
| 24 | { |
| 25 | |
| 26 | class SeparateArrayInitTraverser : private TIntermTraverser |
| 27 | { |
| 28 | public: |
| 29 | static void apply(TIntermNode *root); |
| 30 | private: |
| 31 | SeparateArrayInitTraverser(); |
| 32 | bool visitAggregate(Visit, TIntermAggregate *node) override; |
| 33 | }; |
| 34 | |
| 35 | void SeparateArrayInitTraverser::apply(TIntermNode *root) |
| 36 | { |
| 37 | SeparateArrayInitTraverser separateInit; |
| 38 | root->traverse(&separateInit); |
| 39 | separateInit.updateTree(); |
| 40 | } |
| 41 | |
| 42 | SeparateArrayInitTraverser::SeparateArrayInitTraverser() |
| 43 | : TIntermTraverser(true, false, false) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | bool SeparateArrayInitTraverser::visitAggregate(Visit, TIntermAggregate *node) |
| 48 | { |
| 49 | if (node->getOp() == EOpDeclaration) |
| 50 | { |
| 51 | TIntermSequence *sequence = node->getSequence(); |
| 52 | TIntermBinary *initNode = sequence->back()->getAsBinaryNode(); |
| 53 | if (initNode != nullptr && initNode->getOp() == EOpInitialize) |
| 54 | { |
| 55 | TIntermTyped *initializer = initNode->getRight(); |
Olli Etuaho | 18b9deb | 2015-11-05 12:14:50 +0200 | [diff] [blame] | 56 | if (initializer->isArray() && !sh::OutputHLSL::canWriteAsHLSLLiteral(initializer)) |
Olli Etuaho | 822fa84 | 2015-04-16 14:26:10 +0300 | [diff] [blame] | 57 | { |
| 58 | // We rely on that array declarations have been isolated to single declarations. |
| 59 | ASSERT(sequence->size() == 1); |
| 60 | TIntermTyped *symbol = initNode->getLeft(); |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame^] | 61 | TIntermBlock *parentBlock = getParentNode()->getAsBlock(); |
| 62 | ASSERT(parentBlock != nullptr); |
Olli Etuaho | 822fa84 | 2015-04-16 14:26:10 +0300 | [diff] [blame] | 63 | |
| 64 | TIntermSequence replacements; |
| 65 | |
| 66 | TIntermAggregate *replacementDeclaration = new TIntermAggregate; |
| 67 | replacementDeclaration->setOp(EOpDeclaration); |
| 68 | replacementDeclaration->getSequence()->push_back(symbol); |
| 69 | replacementDeclaration->setLine(symbol->getLine()); |
| 70 | replacements.push_back(replacementDeclaration); |
| 71 | |
Olli Etuaho | 3272a6d | 2016-08-29 17:54:50 +0300 | [diff] [blame] | 72 | TIntermBinary *replacementAssignment = |
| 73 | new TIntermBinary(EOpAssign, symbol, initializer); |
Olli Etuaho | 822fa84 | 2015-04-16 14:26:10 +0300 | [diff] [blame] | 74 | replacementAssignment->setLine(symbol->getLine()); |
| 75 | replacements.push_back(replacementAssignment); |
| 76 | |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame^] | 77 | mMultiReplacements.push_back( |
| 78 | NodeReplaceWithMultipleEntry(parentBlock, node, replacements)); |
Olli Etuaho | 822fa84 | 2015-04-16 14:26:10 +0300 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | return false; |
| 82 | } |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | } // namespace |
| 87 | |
| 88 | void SeparateArrayInitialization(TIntermNode *root) |
| 89 | { |
| 90 | SeparateArrayInitTraverser::apply(root); |
| 91 | } |