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(); |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame^] | 32 | bool visitDeclaration(Visit, TIntermDeclaration *node) override; |
Olli Etuaho | 822fa84 | 2015-04-16 14:26:10 +0300 | [diff] [blame] | 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 | |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame^] | 47 | bool SeparateArrayInitTraverser::visitDeclaration(Visit, TIntermDeclaration *node) |
Olli Etuaho | 822fa84 | 2015-04-16 14:26:10 +0300 | [diff] [blame] | 48 | { |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame^] | 49 | TIntermSequence *sequence = node->getSequence(); |
| 50 | TIntermBinary *initNode = sequence->back()->getAsBinaryNode(); |
| 51 | if (initNode != nullptr && initNode->getOp() == EOpInitialize) |
Olli Etuaho | 822fa84 | 2015-04-16 14:26:10 +0300 | [diff] [blame] | 52 | { |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame^] | 53 | TIntermTyped *initializer = initNode->getRight(); |
| 54 | if (initializer->isArray() && !sh::OutputHLSL::canWriteAsHLSLLiteral(initializer)) |
Olli Etuaho | 822fa84 | 2015-04-16 14:26:10 +0300 | [diff] [blame] | 55 | { |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame^] | 56 | // We rely on that array declarations have been isolated to single declarations. |
| 57 | ASSERT(sequence->size() == 1); |
| 58 | TIntermTyped *symbol = initNode->getLeft(); |
| 59 | TIntermBlock *parentBlock = getParentNode()->getAsBlock(); |
| 60 | ASSERT(parentBlock != nullptr); |
Olli Etuaho | 822fa84 | 2015-04-16 14:26:10 +0300 | [diff] [blame] | 61 | |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame^] | 62 | TIntermSequence replacements; |
Olli Etuaho | 822fa84 | 2015-04-16 14:26:10 +0300 | [diff] [blame] | 63 | |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame^] | 64 | TIntermDeclaration *replacementDeclaration = new TIntermDeclaration(); |
| 65 | replacementDeclaration->appendDeclarator(symbol); |
| 66 | replacementDeclaration->setLine(symbol->getLine()); |
| 67 | replacements.push_back(replacementDeclaration); |
Olli Etuaho | 822fa84 | 2015-04-16 14:26:10 +0300 | [diff] [blame] | 68 | |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame^] | 69 | TIntermBinary *replacementAssignment = |
| 70 | new TIntermBinary(EOpAssign, symbol, initializer); |
| 71 | replacementAssignment->setLine(symbol->getLine()); |
| 72 | replacements.push_back(replacementAssignment); |
Olli Etuaho | 822fa84 | 2015-04-16 14:26:10 +0300 | [diff] [blame] | 73 | |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame^] | 74 | mMultiReplacements.push_back( |
| 75 | NodeReplaceWithMultipleEntry(parentBlock, node, replacements)); |
Olli Etuaho | 822fa84 | 2015-04-16 14:26:10 +0300 | [diff] [blame] | 76 | } |
Olli Etuaho | 822fa84 | 2015-04-16 14:26:10 +0300 | [diff] [blame] | 77 | } |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame^] | 78 | return false; |
Olli Etuaho | 822fa84 | 2015-04-16 14:26:10 +0300 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | } // namespace |
| 82 | |
| 83 | void SeparateArrayInitialization(TIntermNode *root) |
| 84 | { |
| 85 | SeparateArrayInitTraverser::apply(root); |
| 86 | } |