14a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo// 24a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. 34a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo// Use of this source code is governed by a BSD-style license that can be 44a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo// found in the LICENSE file. 54a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo// 64a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo 74a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo#include "compiler/translator/InitializeVariables.h" 84a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo#include "compiler/translator/compilerdebug.h" 94a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo 104a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Monamespace 114a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo{ 124a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo 13e40d1e9c70f1d1d5d0d92f24f8868fcf0b610639Zhenyao MoTIntermConstantUnion *constructFloatConstUnionNode(const TType &type) 144a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo{ 154a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo TType myType = type; 164a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo unsigned char size = myType.getNominalSize(); 174a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo if (myType.isMatrix()) 184a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo size *= size; 194a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo ConstantUnion *u = new ConstantUnion[size]; 204a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo for (int ii = 0; ii < size; ++ii) 214a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo u[ii].setFConst(0.0f); 224a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo 234a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo myType.clearArrayness(); 244a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo myType.setQualifier(EvqConst); 254a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo TIntermConstantUnion *node = new TIntermConstantUnion(u, myType); 264a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo return node; 274a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo} 284a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo 29e40d1e9c70f1d1d5d0d92f24f8868fcf0b610639Zhenyao MoTIntermConstantUnion *constructIndexNode(int index) 304a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo{ 314a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo ConstantUnion *u = new ConstantUnion[1]; 324a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo u[0].setIConst(index); 334a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo 344a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo TType type(EbtInt, EbpUndefined, EvqConst, 1); 354a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo TIntermConstantUnion *node = new TIntermConstantUnion(u, type); 364a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo return node; 374a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo} 384a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo 394a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo} // namespace anonymous 404a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo 41e40d1e9c70f1d1d5d0d92f24f8868fcf0b610639Zhenyao Mobool InitializeVariables::visitAggregate(Visit visit, TIntermAggregate *node) 424a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo{ 434a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo bool visitChildren = !mCodeInserted; 444a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo switch (node->getOp()) 454a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo { 464a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo case EOpSequence: 474a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo break; 484a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo case EOpFunction: 494a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo { 504a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo // Function definition. 514a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo ASSERT(visit == PreVisit); 524a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo if (node->getName() == "main(") 534a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo { 54e40d1e9c70f1d1d5d0d92f24f8868fcf0b610639Zhenyao Mo TIntermSequence *sequence = node->getSequence(); 55e40d1e9c70f1d1d5d0d92f24f8868fcf0b610639Zhenyao Mo ASSERT((sequence->size() == 1) || (sequence->size() == 2)); 564a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo TIntermAggregate *body = NULL; 57e40d1e9c70f1d1d5d0d92f24f8868fcf0b610639Zhenyao Mo if (sequence->size() == 1) 584a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo { 594a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo body = new TIntermAggregate(EOpSequence); 60e40d1e9c70f1d1d5d0d92f24f8868fcf0b610639Zhenyao Mo sequence->push_back(body); 614a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo } 624a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo else 634a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo { 64e40d1e9c70f1d1d5d0d92f24f8868fcf0b610639Zhenyao Mo body = (*sequence)[1]->getAsAggregate(); 654a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo } 664a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo ASSERT(body); 674a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo insertInitCode(body->getSequence()); 684a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo mCodeInserted = true; 694a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo } 704a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo break; 714a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo } 724a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo default: 734a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo visitChildren = false; 744a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo break; 754a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo } 764a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo return visitChildren; 774a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo} 784a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo 79e40d1e9c70f1d1d5d0d92f24f8868fcf0b610639Zhenyao Movoid InitializeVariables::insertInitCode(TIntermSequence *sequence) 804a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo{ 814a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo for (size_t ii = 0; ii < mVariables.size(); ++ii) 824a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo { 83e40d1e9c70f1d1d5d0d92f24f8868fcf0b610639Zhenyao Mo const InitVariableInfo &varInfo = mVariables[ii]; 844a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo 854a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo if (varInfo.type.isArray()) 864a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo { 874a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo for (int index = varInfo.type.getArraySize() - 1; index >= 0; --index) 884a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo { 894a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo TIntermBinary *assign = new TIntermBinary(EOpAssign); 90e40d1e9c70f1d1d5d0d92f24f8868fcf0b610639Zhenyao Mo sequence->insert(sequence->begin(), assign); 914a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo 924a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo TIntermBinary *indexDirect = new TIntermBinary(EOpIndexDirect); 934a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo TIntermSymbol *symbol = new TIntermSymbol(0, varInfo.name, varInfo.type); 944a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo indexDirect->setLeft(symbol); 954a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo TIntermConstantUnion *indexNode = constructIndexNode(index); 964a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo indexDirect->setRight(indexNode); 974a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo 984a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo assign->setLeft(indexDirect); 994a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo 1004a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo TIntermConstantUnion *zeroConst = constructFloatConstUnionNode(varInfo.type); 1014a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo assign->setRight(zeroConst); 1024a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo } 1034a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo } 1044a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo else 1054a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo { 1064a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo TIntermBinary *assign = new TIntermBinary(EOpAssign); 107e40d1e9c70f1d1d5d0d92f24f8868fcf0b610639Zhenyao Mo sequence->insert(sequence->begin(), assign); 1084a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo TIntermSymbol *symbol = new TIntermSymbol(0, varInfo.name, varInfo.type); 1094a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo assign->setLeft(symbol); 1104a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo TIntermConstantUnion *zeroConst = constructFloatConstUnionNode(varInfo.type); 1114a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo assign->setRight(zeroConst); 1124a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo } 1134a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo 1144a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo } 1154a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo} 1164a667fe96e55df4713532e3830dd215ac7696f53Zhenyao Mo 117