blob: 2bb609a27c3dd862eca548f36df667fa56ee747a [file] [log] [blame]
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001//
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"
8#include "compiler/translator/compilerdebug.h"
9
10namespace
11{
12
Zhenyao Moe40d1e92014-07-16 17:40:36 -070013TIntermConstantUnion *constructFloatConstUnionNode(const TType &type)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080014{
15 TType myType = type;
Minmin Gong3b26e232015-04-07 18:31:54 -070016 unsigned char size = static_cast<unsigned char>(myType.getNominalSize());
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080017 if (myType.isMatrix())
18 size *= size;
19 ConstantUnion *u = new ConstantUnion[size];
20 for (int ii = 0; ii < size; ++ii)
21 u[ii].setFConst(0.0f);
22
23 myType.clearArrayness();
24 myType.setQualifier(EvqConst);
25 TIntermConstantUnion *node = new TIntermConstantUnion(u, myType);
26 return node;
27}
28
Zhenyao Moe40d1e92014-07-16 17:40:36 -070029TIntermConstantUnion *constructIndexNode(int index)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080030{
31 ConstantUnion *u = new ConstantUnion[1];
32 u[0].setIConst(index);
33
34 TType type(EbtInt, EbpUndefined, EvqConst, 1);
35 TIntermConstantUnion *node = new TIntermConstantUnion(u, type);
36 return node;
37}
38
39} // namespace anonymous
40
Zhenyao Moe40d1e92014-07-16 17:40:36 -070041bool InitializeVariables::visitAggregate(Visit visit, TIntermAggregate *node)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080042{
43 bool visitChildren = !mCodeInserted;
44 switch (node->getOp())
45 {
46 case EOpSequence:
47 break;
48 case EOpFunction:
49 {
50 // Function definition.
51 ASSERT(visit == PreVisit);
52 if (node->getName() == "main(")
53 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -070054 TIntermSequence *sequence = node->getSequence();
55 ASSERT((sequence->size() == 1) || (sequence->size() == 2));
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080056 TIntermAggregate *body = NULL;
Zhenyao Moe40d1e92014-07-16 17:40:36 -070057 if (sequence->size() == 1)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080058 {
59 body = new TIntermAggregate(EOpSequence);
Zhenyao Moe40d1e92014-07-16 17:40:36 -070060 sequence->push_back(body);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080061 }
62 else
63 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -070064 body = (*sequence)[1]->getAsAggregate();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080065 }
66 ASSERT(body);
67 insertInitCode(body->getSequence());
68 mCodeInserted = true;
69 }
70 break;
71 }
72 default:
73 visitChildren = false;
74 break;
75 }
76 return visitChildren;
77}
78
Zhenyao Moe40d1e92014-07-16 17:40:36 -070079void InitializeVariables::insertInitCode(TIntermSequence *sequence)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080080{
81 for (size_t ii = 0; ii < mVariables.size(); ++ii)
82 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -070083 const InitVariableInfo &varInfo = mVariables[ii];
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080084
85 if (varInfo.type.isArray())
86 {
87 for (int index = varInfo.type.getArraySize() - 1; index >= 0; --index)
88 {
89 TIntermBinary *assign = new TIntermBinary(EOpAssign);
Zhenyao Moe40d1e92014-07-16 17:40:36 -070090 sequence->insert(sequence->begin(), assign);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080091
92 TIntermBinary *indexDirect = new TIntermBinary(EOpIndexDirect);
93 TIntermSymbol *symbol = new TIntermSymbol(0, varInfo.name, varInfo.type);
94 indexDirect->setLeft(symbol);
95 TIntermConstantUnion *indexNode = constructIndexNode(index);
96 indexDirect->setRight(indexNode);
97
98 assign->setLeft(indexDirect);
99
100 TIntermConstantUnion *zeroConst = constructFloatConstUnionNode(varInfo.type);
101 assign->setRight(zeroConst);
102 }
103 }
104 else
105 {
106 TIntermBinary *assign = new TIntermBinary(EOpAssign);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700107 sequence->insert(sequence->begin(), assign);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800108 TIntermSymbol *symbol = new TIntermSymbol(0, varInfo.name, varInfo.type);
109 assign->setLeft(symbol);
110 TIntermConstantUnion *zeroConst = constructFloatConstUnionNode(varInfo.type);
111 assign->setRight(zeroConst);
112 }
113
114 }
115}
116