From: Devang Patel Date: Tue, 30 Oct 2007 21:27:20 +0000 (+0000) Subject: Refactor code into a separate method. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9e32d4b4a7270a9701b7cb454381eeaa4cc42a77;p=clang Refactor code into a separate method. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@43519 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/CodeGen/CodeGenModule.cpp b/CodeGen/CodeGenModule.cpp index b5f4c8d1ff..a596c08528 100644 --- a/CodeGen/CodeGenModule.cpp +++ b/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); diff --git a/CodeGen/CodeGenModule.h b/CodeGen/CodeGenModule.h index d94f54a846..4ca4f8c550 100644 --- a/CodeGen/CodeGenModule.h +++ b/CodeGen/CodeGenModule.h @@ -22,6 +22,7 @@ namespace llvm { class Module; class Constant; class Function; + class GlobalVariable; } namespace clang { @@ -67,6 +68,8 @@ public: void EmitFunction(const FunctionDecl *FD); void EmitGlobalVar(const FileVarDecl *D); void EmitGlobalVarDeclarator(const FileVarDecl *D); + llvm::Constant *EmitGlobalInit(const FileVarDecl *D, + llvm::GlobalVariable *GV); void PrintStats() {} };