[cfe-commits] r43383 - in /cfe/trunk: CodeGen/CodeGenModule.cpp test/CodeGen/globalinit.c

Devang Patel dpatel at apple.com
Fri Oct 26 09:31:40 PDT 2007


Author: dpatel
Date: Fri Oct 26 11:31:40 2007
New Revision: 43383

URL: http://llvm.org/viewvc/llvm-project?rev=43383&view=rev
Log:
Codegen global array initializers.

Added:
    cfe/trunk/test/CodeGen/globalinit.c
Modified:
    cfe/trunk/CodeGen/CodeGenModule.cpp

Modified: cfe/trunk/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CodeGenModule.cpp?rev=43383&r1=43382&r2=43383&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/CodeGen/CodeGenModule.cpp Fri Oct 26 11:31:40 2007
@@ -72,7 +72,41 @@
     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<InitListExpr>(D->getInit())) {
+
+      unsigned NumInitElements = ILE->getNumInits();
+
+      assert ( ILE->getType()->isArrayType() 
+               && "FIXME: Only Array initializers are supported");
+
+      std::vector<llvm::Constant*> ArrayElts;
+      const llvm::PointerType *APType = cast<llvm::PointerType>(GV->getType());
+      const llvm::ArrayType *AType = cast<llvm::ArrayType>(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<uint32_t>(
+           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);
   

Added: cfe/trunk/test/CodeGen/globalinit.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/globalinit.c?rev=43383&view=auto

==============================================================================
--- cfe/trunk/test/CodeGen/globalinit.c (added)
+++ cfe/trunk/test/CodeGen/globalinit.c Fri Oct 26 11:31:40 2007
@@ -0,0 +1,4 @@
+// RUN: clang -emit-llvm %s
+
+int A[10] = { 1,2,3,4,5 };
+





More information about the cfe-commits mailing list