From: Devang Patel Date: Fri, 26 Oct 2007 16:31:40 +0000 (+0000) Subject: Codegen global array initializers. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8e53e720b3d7c962e91138a130dbd5d6c2def0e5;p=clang Codegen global array initializers. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@43383 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/CodeGen/CodeGenModule.cpp b/CodeGen/CodeGenModule.cpp index 8cb138ebff..00c262167c 100644 --- a/CodeGen/CodeGenModule.cpp +++ b/CodeGen/CodeGenModule.cpp @@ -72,7 +72,41 @@ void CodeGenModule::EmitGlobalVar(const FileVarDecl *D) { if (D->getInit()->isIntegerConstantExpr(Value, Context)) Init = llvm::ConstantInt::get(Value); } - assert(Init && "FIXME: Global variable initializers unimp!"); + + 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(); + for (; i < NumArrayElements; ++i) + ArrayElts.push_back(llvm::Constant::getNullValue(AType->getElementType())); + + Init = llvm::ConstantArray::get(AType, ArrayElts); + } else + assert(Init && "FIXME: Global variable initializers unimp!"); + } GV->setInitializer(Init); diff --git a/test/CodeGen/globalinit.c b/test/CodeGen/globalinit.c new file mode 100644 index 0000000000..7087669cbd --- /dev/null +++ b/test/CodeGen/globalinit.c @@ -0,0 +1,4 @@ +// RUN: clang -emit-llvm %s + +int A[10] = { 1,2,3,4,5 }; +