Introducing external weak linkage. Darwin codegen should be added later.

llvm-svn: 32052
diff --git a/llvm/lib/Target/CBackend/Writer.cpp b/llvm/lib/Target/CBackend/Writer.cpp
index fa869db..6d83aeb 100644
--- a/llvm/lib/Target/CBackend/Writer.cpp
+++ b/llvm/lib/Target/CBackend/Writer.cpp
@@ -1157,8 +1157,6 @@
       << "#define __attribute__(X)\n"
       << "#endif\n\n";
 
-#if 0
-  // At some point, we should support "external weak" vs. "weak" linkages.
   // On Mac OS X, "external weak" is spelled "__attribute__((weak_import))".
   Out << "#if defined(__GNUC__) && defined(__APPLE_CC__)\n"
       << "#define __EXTERNAL_WEAK__ __attribute__((weak_import))\n"
@@ -1167,7 +1165,6 @@
       << "#else\n"
       << "#define __EXTERNAL_WEAK__\n"
       << "#endif\n\n";
-#endif
 
   // For now, turn off the weak linkage attribute on Mac OS X. (See above.)
   Out << "#if defined(__GNUC__) && defined(__APPLE_CC__)\n"
@@ -1357,7 +1354,11 @@
         Out << "__declspec(dllimport) ";
         printType(Out, I->getType()->getElementType(), Mang->getValueName(I));
         Out << ";\n";        
-      }      
+      } else if (I->hasExternalWeakLinkage()) {
+        Out << "extern ";
+        printType(Out, I->getType()->getElementType(), Mang->getValueName(I));
+        Out << " __EXTERNAL_WEAK__ ;\n";
+      }
     }
   }
 
@@ -1370,9 +1371,13 @@
     // Don't print declarations for intrinsic functions.
     if (!I->getIntrinsicID() && I->getName() != "setjmp" && 
         I->getName() != "longjmp" && I->getName() != "_setjmp") {
+      if (I->hasExternalWeakLinkage())
+        Out << "extern ";
       printFunctionSignature(I, true);
       if (I->hasWeakLinkage() || I->hasLinkOnceLinkage()) 
         Out << " __ATTRIBUTE_WEAK__";
+      if (I->hasExternalWeakLinkage())
+        Out << " __EXTERNAL_WEAK__";
       if (StaticCtors.count(I))
         Out << " __ATTRIBUTE_CTOR__";
       if (StaticDtors.count(I))
@@ -1405,6 +1410,8 @@
           Out << " __attribute__((common))";
         else if (I->hasWeakLinkage())
           Out << " __ATTRIBUTE_WEAK__";
+        else if (I->hasExternalWeakLinkage())
+          Out << " __EXTERNAL_WEAK__";
         Out << ";\n";
       }
   }
diff --git a/llvm/lib/Target/X86/X86ATTAsmPrinter.cpp b/llvm/lib/Target/X86/X86ATTAsmPrinter.cpp
index 7784f62..88d0c7c 100755
--- a/llvm/lib/Target/X86/X86ATTAsmPrinter.cpp
+++ b/llvm/lib/Target/X86/X86ATTAsmPrinter.cpp
@@ -255,6 +255,10 @@
       }       
       O << Name;
     }
+
+    if (GV->hasExternalWeakLinkage()) {
+      ExtWeakSymbols.insert(Name);
+    }
     
     int Offset = MO.getOffset();
     if (Offset > 0)
diff --git a/llvm/lib/Target/X86/X86AsmPrinter.cpp b/llvm/lib/Target/X86/X86AsmPrinter.cpp
index 2c53b72..cb792ad 100644
--- a/llvm/lib/Target/X86/X86AsmPrinter.cpp
+++ b/llvm/lib/Target/X86/X86AsmPrinter.cpp
@@ -128,11 +128,12 @@
   // from how MASM does things.  When making changes here don't forget to look
   // at X86IntelAsmPrinter::doFinalization().
   const TargetData *TD = TM.getTargetData();
-
+  
   // Print out module-level global variables here.
   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
        I != E; ++I) {
-    if (!I->hasInitializer()) continue;   // External global require no code
+    if (!I->hasInitializer() && !I->hasExternalWeakLinkage())
+      continue;   // External global require no code
     
     // Check to see if this is a special global used by LLVM, if so, emit it.
     if (EmitSpecialLLVMGlobal(I))
@@ -176,6 +177,17 @@
       O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
     } else {
       switch (I->getLinkage()) {
+      case GlobalValue::ExternalWeakLinkage:
+       if (Subtarget->isTargetDarwin()) {
+         assert(0 && "External weak linkage for Darwin not implemented yet");
+       } else if (Subtarget->isTargetCygwin()) {
+         // There is no external weak linkage on Mingw32 platform.
+         // Defaulting just to external
+         O << "\t.globl " << name << "\n";
+       } else {
+         O << "\t.weak " << name << "\n";
+         break;
+       }
       case GlobalValue::LinkOnceLinkage:
       case GlobalValue::WeakLinkage:
         if (Subtarget->isTargetDarwin()) {
@@ -270,7 +282,24 @@
          i != e; ++i) {
     O << "\t.ascii \" -export:" << *i << "\"\n";
   }    
- 
+
+  if (Subtarget->isTargetDarwin()) {
+    if (ExtWeakSymbols.begin() != ExtWeakSymbols.end())
+      assert(0 && "External weak linkage for Darwin not implemented yet");
+  } else if (Subtarget->isTargetCygwin()) {
+    // There is no external weak linkage on Mingw32 platform.
+    // Defaulting to external
+  } else {
+    if (ExtWeakSymbols.begin() != ExtWeakSymbols.end())
+      SwitchToDataSection("");
+
+    for (std::set<std::string>::iterator i = ExtWeakSymbols.begin(),
+         e = ExtWeakSymbols.end();
+         i != e; ++i) {
+      O << "\t.weak " << *i << "\n";
+    }
+  }
+  
   if (Subtarget->isTargetDarwin()) {
     SwitchToDataSection("");
 
diff --git a/llvm/lib/Target/X86/X86AsmPrinter.h b/llvm/lib/Target/X86/X86AsmPrinter.h
index 3503e37..8dad733 100755
--- a/llvm/lib/Target/X86/X86AsmPrinter.h
+++ b/llvm/lib/Target/X86/X86AsmPrinter.h
@@ -84,6 +84,9 @@
 
   // Necessary for dllexport support
   std::set<std::string> DLLExportedFns, DLLExportedGVs;
+
+  // Necessary for external weak linkage support
+  std::set<std::string> ExtWeakSymbols;
   
   inline static bool isScale(const MachineOperand &MO) {
     return MO.isImmediate() &&