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