Run through the list of globals once and sectionize all types of globlas includeing declarations. Later emit them from their section lists.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@71661 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Target/PIC16/PIC16TargetAsmInfo.cpp b/lib/Target/PIC16/PIC16TargetAsmInfo.cpp
index 295188f..9d7592a 100644
--- a/lib/Target/PIC16/PIC16TargetAsmInfo.cpp
+++ b/lib/Target/PIC16/PIC16TargetAsmInfo.cpp
@@ -45,6 +45,8 @@
   // in BeginModule, and gpasm cribbs for that .text symbol.
   TextSection = getUnnamedSection("", SectionFlags::Code);
   ROSection = new PIC16Section(getReadOnlySection());
+  ExternalVarDecls = new PIC16Section(getNamedSection("ExternalVarDecls"));
+  ExternalVarDefs = new PIC16Section(getNamedSection("ExternalVarDefs"));
 }
 
 const char *PIC16TargetAsmInfo::getRomDirective(unsigned size) const
@@ -196,9 +198,18 @@
   // We select the section based on the initializer here, so it really
   // has to be a GlobalVariable.
   const GlobalVariable *GV = dyn_cast<GlobalVariable>(GV1); 
-  if (!GV1 || ! GV->hasInitializer())
+
+  if (!GV)
     return TargetAsmInfo::SelectSectionForGlobal(GV1);
 
+  // Record Exteranl Var Decls.
+  if (GV->isDeclaration()) {
+    ExternalVarDecls->Items.push_back(GV);
+    return ExternalVarDecls->S_;
+  }
+    
+  assert (GV->hasInitializer() && "A def without initializer?");
+
   // First, if this is an automatic variable for a function, get the section
   // name for it and return.
   const std::string name = GV->getName();
@@ -206,6 +217,11 @@
     return getSectionForAuto(GV);
   }
 
+  // Record Exteranl Var Defs.
+  if (GV->hasExternalLinkage() || GV->hasCommonLinkage()) {
+    ExternalVarDefs->Items.push_back(GV);
+  }
+
   // See if this is an uninitialized global.
   const Constant *C = GV->getInitializer();
   if (C->isNullValue()) 
@@ -240,4 +256,6 @@
   }
 
   delete ROSection;
+  delete ExternalVarDecls;
+  delete ExternalVarDefs;
 }