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 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame^] | 23 | namespace sh |
| 24 | { |
| 25 | |
Olli Etuaho | 822fa84 | 2015-04-16 14:26:10 +0300 | [diff] [blame] | 26 | namespace |
| 27 | { |
| 28 | |
| 29 | class SeparateArrayInitTraverser : private TIntermTraverser |
| 30 | { |
| 31 | public: |
| 32 | static void apply(TIntermNode *root); |
| 33 | private: |
| 34 | SeparateArrayInitTraverser(); |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 35 | bool visitDeclaration(Visit, TIntermDeclaration *node) override; |
Olli Etuaho | 822fa84 | 2015-04-16 14:26:10 +0300 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | void SeparateArrayInitTraverser::apply(TIntermNode *root) |
| 39 | { |
| 40 | SeparateArrayInitTraverser separateInit; |
| 41 | root->traverse(&separateInit); |
| 42 | separateInit.updateTree(); |
| 43 | } |
| 44 | |
| 45 | SeparateArrayInitTraverser::SeparateArrayInitTraverser() |
| 46 | : TIntermTraverser(true, false, false) |
| 47 | { |
| 48 | } |
| 49 | |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 50 | bool SeparateArrayInitTraverser::visitDeclaration(Visit, TIntermDeclaration *node) |
Olli Etuaho | 822fa84 | 2015-04-16 14:26:10 +0300 | [diff] [blame] | 51 | { |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 52 | TIntermSequence *sequence = node->getSequence(); |
| 53 | TIntermBinary *initNode = sequence->back()->getAsBinaryNode(); |
| 54 | if (initNode != nullptr && initNode->getOp() == EOpInitialize) |
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 | TIntermTyped *initializer = initNode->getRight(); |
| 57 | if (initializer->isArray() && !sh::OutputHLSL::canWriteAsHLSLLiteral(initializer)) |
Olli Etuaho | 822fa84 | 2015-04-16 14:26:10 +0300 | [diff] [blame] | 58 | { |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 59 | // We rely on that array declarations have been isolated to single declarations. |
| 60 | ASSERT(sequence->size() == 1); |
| 61 | TIntermTyped *symbol = initNode->getLeft(); |
| 62 | TIntermBlock *parentBlock = getParentNode()->getAsBlock(); |
| 63 | ASSERT(parentBlock != nullptr); |
Olli Etuaho | 822fa84 | 2015-04-16 14:26:10 +0300 | [diff] [blame] | 64 | |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 65 | TIntermSequence replacements; |
Olli Etuaho | 822fa84 | 2015-04-16 14:26:10 +0300 | [diff] [blame] | 66 | |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 67 | TIntermDeclaration *replacementDeclaration = new TIntermDeclaration(); |
| 68 | replacementDeclaration->appendDeclarator(symbol); |
| 69 | replacementDeclaration->setLine(symbol->getLine()); |
| 70 | replacements.push_back(replacementDeclaration); |
Olli Etuaho | 822fa84 | 2015-04-16 14:26:10 +0300 | [diff] [blame] | 71 | |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 72 | TIntermBinary *replacementAssignment = |
| 73 | new TIntermBinary(EOpAssign, symbol, initializer); |
| 74 | replacementAssignment->setLine(symbol->getLine()); |
| 75 | replacements.push_back(replacementAssignment); |
Olli Etuaho | 822fa84 | 2015-04-16 14:26:10 +0300 | [diff] [blame] | 76 | |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +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 | } |
Olli Etuaho | 822fa84 | 2015-04-16 14:26:10 +0300 | [diff] [blame] | 80 | } |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 81 | return false; |
Olli Etuaho | 822fa84 | 2015-04-16 14:26:10 +0300 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | } // namespace |
| 85 | |
| 86 | void SeparateArrayInitialization(TIntermNode *root) |
| 87 | { |
| 88 | SeparateArrayInitTraverser::apply(root); |
| 89 | } |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame^] | 90 | |
| 91 | } // namespace sh |