Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2002-2013 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 | |
| 7 | #include "compiler/translator/InitializeVariables.h" |
Olli Etuaho | d57e0db | 2015-04-24 15:05:08 +0300 | [diff] [blame^] | 8 | |
| 9 | #include "common/debug.h" |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 10 | |
| 11 | namespace |
| 12 | { |
| 13 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 14 | TIntermConstantUnion *constructFloatConstUnionNode(const TType &type) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 15 | { |
| 16 | TType myType = type; |
Minmin Gong | 794e000 | 2015-04-07 18:31:54 -0700 | [diff] [blame] | 17 | unsigned char size = static_cast<unsigned char>(myType.getNominalSize()); |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 18 | if (myType.isMatrix()) |
| 19 | size *= size; |
| 20 | ConstantUnion *u = new ConstantUnion[size]; |
| 21 | for (int ii = 0; ii < size; ++ii) |
| 22 | u[ii].setFConst(0.0f); |
| 23 | |
| 24 | myType.clearArrayness(); |
| 25 | myType.setQualifier(EvqConst); |
| 26 | TIntermConstantUnion *node = new TIntermConstantUnion(u, myType); |
| 27 | return node; |
| 28 | } |
| 29 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 30 | TIntermConstantUnion *constructIndexNode(int index) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 31 | { |
| 32 | ConstantUnion *u = new ConstantUnion[1]; |
| 33 | u[0].setIConst(index); |
| 34 | |
| 35 | TType type(EbtInt, EbpUndefined, EvqConst, 1); |
| 36 | TIntermConstantUnion *node = new TIntermConstantUnion(u, type); |
| 37 | return node; |
| 38 | } |
| 39 | |
| 40 | } // namespace anonymous |
| 41 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 42 | bool InitializeVariables::visitAggregate(Visit visit, TIntermAggregate *node) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 43 | { |
| 44 | bool visitChildren = !mCodeInserted; |
| 45 | switch (node->getOp()) |
| 46 | { |
| 47 | case EOpSequence: |
| 48 | break; |
| 49 | case EOpFunction: |
| 50 | { |
| 51 | // Function definition. |
| 52 | ASSERT(visit == PreVisit); |
| 53 | if (node->getName() == "main(") |
| 54 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 55 | TIntermSequence *sequence = node->getSequence(); |
| 56 | ASSERT((sequence->size() == 1) || (sequence->size() == 2)); |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 57 | TIntermAggregate *body = NULL; |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 58 | if (sequence->size() == 1) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 59 | { |
| 60 | body = new TIntermAggregate(EOpSequence); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 61 | sequence->push_back(body); |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 62 | } |
| 63 | else |
| 64 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 65 | body = (*sequence)[1]->getAsAggregate(); |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 66 | } |
| 67 | ASSERT(body); |
| 68 | insertInitCode(body->getSequence()); |
| 69 | mCodeInserted = true; |
| 70 | } |
| 71 | break; |
| 72 | } |
| 73 | default: |
| 74 | visitChildren = false; |
| 75 | break; |
| 76 | } |
| 77 | return visitChildren; |
| 78 | } |
| 79 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 80 | void InitializeVariables::insertInitCode(TIntermSequence *sequence) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 81 | { |
| 82 | for (size_t ii = 0; ii < mVariables.size(); ++ii) |
| 83 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 84 | const InitVariableInfo &varInfo = mVariables[ii]; |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 85 | |
| 86 | if (varInfo.type.isArray()) |
| 87 | { |
| 88 | for (int index = varInfo.type.getArraySize() - 1; index >= 0; --index) |
| 89 | { |
| 90 | TIntermBinary *assign = new TIntermBinary(EOpAssign); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 91 | sequence->insert(sequence->begin(), assign); |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 92 | |
| 93 | TIntermBinary *indexDirect = new TIntermBinary(EOpIndexDirect); |
| 94 | TIntermSymbol *symbol = new TIntermSymbol(0, varInfo.name, varInfo.type); |
| 95 | indexDirect->setLeft(symbol); |
| 96 | TIntermConstantUnion *indexNode = constructIndexNode(index); |
| 97 | indexDirect->setRight(indexNode); |
| 98 | |
| 99 | assign->setLeft(indexDirect); |
| 100 | |
| 101 | TIntermConstantUnion *zeroConst = constructFloatConstUnionNode(varInfo.type); |
| 102 | assign->setRight(zeroConst); |
| 103 | } |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | TIntermBinary *assign = new TIntermBinary(EOpAssign); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 108 | sequence->insert(sequence->begin(), assign); |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 109 | TIntermSymbol *symbol = new TIntermSymbol(0, varInfo.name, varInfo.type); |
| 110 | assign->setLeft(symbol); |
| 111 | TIntermConstantUnion *zeroConst = constructFloatConstUnionNode(varInfo.type); |
| 112 | assign->setRight(zeroConst); |
| 113 | } |
| 114 | |
| 115 | } |
| 116 | } |
| 117 | |