From 73721a12ca9d200cdc2bea178d40073cbbca0476 Mon Sep 17 00:00:00 2001 From: Devang Patel Date: Tue, 30 Oct 2007 21:27:20 +0000 Subject: Refactor code into a separate method. llvm-svn: 43519 --- clang/CodeGen/CodeGenModule.cpp | 83 ++++++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 38 deletions(-) (limited to 'clang/CodeGen/CodeGenModule.cpp') diff --git a/clang/CodeGen/CodeGenModule.cpp b/clang/CodeGen/CodeGenModule.cpp index b5f4c8d1ff5..a596c08528d 100644 --- a/clang/CodeGen/CodeGenModule.cpp +++ b/clang/CodeGen/CodeGenModule.cpp @@ -54,6 +54,47 @@ void CodeGenModule::EmitFunction(const FunctionDecl *FD) { CodeGenFunction(*this).GenerateCode(FD); } +llvm::Constant *CodeGenModule::EmitGlobalInit(const FileVarDecl *D, + llvm::GlobalVariable *GV) { + + const InitListExpr *ILE = dyn_cast(D->getInit()); + if (!ILE) + return 0; + + unsigned NumInitElements = ILE->getNumInits(); + + assert ( ILE->getType()->isArrayType() + && "FIXME: Only Array initializers are supported"); + + std::vector ArrayElts; + const llvm::PointerType *APType = cast(GV->getType()); + const llvm::ArrayType *AType = + cast(APType->getElementType()); + + // Copy initializer elements. + unsigned i = 0; + for (i = 0; i < NumInitElements; ++i) { + assert (ILE->getInit(i)->getType()->isIntegerType() + && "Only IntegerType global array initializers are supported"); + llvm::APSInt + Value(static_cast + (getContext().getTypeSize(ILE->getInit(i)->getType(), + SourceLocation()))); + if (ILE->getInit(i)->isIntegerConstantExpr(Value, Context)) { + llvm::Constant *C = llvm::ConstantInt::get(Value); + ArrayElts.push_back(C); + } + } + + // Initialize remaining array elements. + unsigned NumArrayElements = AType->getNumElements(); + const llvm::Type *AElemTy = AType->getElementType(); + for (; i < NumArrayElements; ++i) + ArrayElts.push_back(llvm::Constant::getNullValue(AElemTy)); + + return llvm::ConstantArray::get(AType, ArrayElts); +} + void CodeGenModule::EmitGlobalVar(const FileVarDecl *D) { llvm::GlobalVariable *GV = cast(GetAddrOfGlobalDecl(D)); @@ -73,44 +114,10 @@ void CodeGenModule::EmitGlobalVar(const FileVarDecl *D) { Init = llvm::ConstantInt::get(Value); } - if (!Init) { - if (const InitListExpr *ILE = dyn_cast(D->getInit())) { - - unsigned NumInitElements = ILE->getNumInits(); - - assert ( ILE->getType()->isArrayType() - && "FIXME: Only Array initializers are supported"); - - std::vector ArrayElts; - const llvm::PointerType *APType = cast(GV->getType()); - const llvm::ArrayType *AType = - cast(APType->getElementType()); - - // Copy initializer elements. - unsigned i = 0; - for (i = 0; i < NumInitElements; ++i) { - assert (ILE->getInit(i)->getType()->isIntegerType() - && "Only IntegerType global array initializers are supported"); - llvm::APSInt - Value(static_cast - (getContext().getTypeSize(ILE->getInit(i)->getType(), - SourceLocation()))); - if (ILE->getInit(i)->isIntegerConstantExpr(Value, Context)) { - llvm::Constant *C = llvm::ConstantInt::get(Value); - ArrayElts.push_back(C); - } - } - - // Initialize remaining array elements. - unsigned NumArrayElements = AType->getNumElements(); - const llvm::Type *AElemTy = AType->getElementType(); - for (; i < NumArrayElements; ++i) - ArrayElts.push_back(llvm::Constant::getNullValue(AElemTy)); - - Init = llvm::ConstantArray::get(AType, ArrayElts); - } else - assert(Init && "FIXME: Global variable initializers unimp!"); - } + if (!Init) + Init = EmitGlobalInit(D, GV); + + assert(Init && "FIXME: Global variable initializers unimp!"); GV->setInitializer(Init); -- cgit v1.2.3