]> granicus.if.org Git - clang/commitdiff
Refactor code into a separate method.
authorDevang Patel <dpatel@apple.com>
Tue, 30 Oct 2007 21:27:20 +0000 (21:27 +0000)
committerDevang Patel <dpatel@apple.com>
Tue, 30 Oct 2007 21:27:20 +0000 (21:27 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@43519 91177308-0d34-0410-b5e6-96231b3b80d8

CodeGen/CodeGenModule.cpp
CodeGen/CodeGenModule.h

index b5f4c8d1ff50690ed8b8bb4c9b79215d1ebba008..a596c08528dd828a8735a555bf8d94c4e3f8ccd0 100644 (file)
@@ -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<InitListExpr>(D->getInit());
+  if (!ILE)
+    return 0;
+
+  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();
+  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<llvm::GlobalVariable>(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<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();
-      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);
   
index d94f54a846821fe285a67ab869a9f93e8c250fba..4ca4f8c55063e11e8f6b1b056446ec3275e6f68c 100644 (file)
@@ -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() {}
 };