Switch Sparc to new section handling stuff. Refactor printing of module-level GVs significantly.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@54450 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Target/Sparc/SparcAsmPrinter.cpp b/lib/Target/Sparc/SparcAsmPrinter.cpp
index d0ee4ee..f122ae2 100644
--- a/lib/Target/Sparc/SparcAsmPrinter.cpp
+++ b/lib/Target/Sparc/SparcAsmPrinter.cpp
@@ -54,6 +54,7 @@
       return "Sparc Assembly Printer";
     }
 
+    void printModuleLevelGV(const GlobalVariable* GVar);
     void printOperand(const MachineInstr *MI, int opNum);
     void printMemOperand(const MachineInstr *MI, int opNum,
                          const char *Modifier = 0);
@@ -61,6 +62,7 @@
 
     bool printInstruction(const MachineInstr *MI);  // autogenerated.
     bool runOnMachineFunction(MachineFunction &F);
+    std::string getSectionForFunction(const Function &F) const;
     bool doInitialization(Module &M);
     bool doFinalization(Module &M);
   };
@@ -78,6 +80,11 @@
   return new SparcAsmPrinter(o, tm, tm.getTargetAsmInfo());
 }
 
+// Substitute old hook with new one temporary
+std::string SparcAsmPrinter::getSectionForFunction(const Function &F) const {
+  return TAI->SectionForGlobal(&F);
+}
+
 /// runOnMachineFunction - This uses the printInstruction()
 /// method to print assembly for each instruction.
 ///
@@ -177,21 +184,21 @@
 void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
                                       const char *Modifier) {
   printOperand(MI, opNum);
-  
+
   // If this is an ADD operand, emit it like normal operands.
   if (Modifier && !strcmp(Modifier, "arith")) {
     O << ", ";
     printOperand(MI, opNum+1);
     return;
   }
-  
+
   if (MI->getOperand(opNum+1).isRegister() &&
       MI->getOperand(opNum+1).getReg() == SP::G0)
     return;   // don't print "+%g0"
   if (MI->getOperand(opNum+1).isImmediate() &&
       MI->getOperand(opNum+1).getImm() == 0)
     return;   // don't print "+0"
-  
+
   O << "+";
   if (MI->getOperand(opNum+1).isGlobalAddress() ||
       MI->getOperand(opNum+1).isConstantPoolIndex()) {
@@ -216,77 +223,90 @@
 }
 
 bool SparcAsmPrinter::doFinalization(Module &M) {
-  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()) {   // External global require no code
-      // Check to see if this is a special global used by LLVM, if so, emit it.
-      if (EmitSpecialLLVMGlobal(I))
-        continue;
-      
-      O << "\n\n";
-      std::string name = Mang->getValueName(I);
-      Constant *C = I->getInitializer();
-      unsigned Size = TD->getABITypeSize(C->getType());
-      unsigned Align = TD->getPreferredAlignment(I);
+    printModuleLevelGV(I);
 
-      if (C->isNullValue() && (I->hasCommonLinkage() ||
-           I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
-           I->hasWeakLinkage() /* FIXME: Verify correct */)) {
-        SwitchToDataSection(".data", I);
-        if (I->hasInternalLinkage())
-          O << "\t.local " << name << "\n";
-
-        O << "\t.comm " << name << "," << TD->getABITypeSize(C->getType())
-          << "," << Align;
-        O << "\n";
-      } else {
-        switch (I->getLinkage()) {
-        case GlobalValue::CommonLinkage:
-        case GlobalValue::LinkOnceLinkage:
-        case GlobalValue::WeakLinkage:   // FIXME: Verify correct for weak.
-          // Nonnull linkonce -> weak
-          O << "\t.weak " << name << "\n";
-          SwitchToDataSection("", I);
-          O << "\t.section\t\".llvm.linkonce.d." << name
-            << "\",\"aw\",@progbits\n";
-          break;
-
-        case GlobalValue::AppendingLinkage:
-          // FIXME: appending linkage variables should go into a section of
-          // their name or something.  For now, just emit them as external.
-        case GlobalValue::ExternalLinkage:
-          // If external or appending, declare as a global symbol
-          O << "\t.globl " << name << "\n";
-          // FALL THROUGH
-        case GlobalValue::InternalLinkage:
-          if (C->isNullValue())
-            SwitchToDataSection(".bss", I);
-          else
-            SwitchToDataSection(".data", I);
-          break;
-        case GlobalValue::GhostLinkage:
-          cerr << "Should not have any unmaterialized functions!\n";
-          abort();
-        case GlobalValue::DLLImportLinkage:
-          cerr << "DLLImport linkage is not supported by this target!\n";
-          abort();
-        case GlobalValue::DLLExportLinkage:
-          cerr << "DLLExport linkage is not supported by this target!\n";
-          abort();
-        default:
-          assert(0 && "Unknown linkage type!");          
-        }
-
-        O << "\t.align " << Align << "\n";
-        O << "\t.type " << name << ",#object\n";
-        O << "\t.size " << name << "," << Size << "\n";
-        O << name << ":\n";
-        EmitGlobalConstant(C);
-      }
-    }
+  O << '\n';
 
   return AsmPrinter::doFinalization(M);
 }
+
+void SparcAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
+  const TargetData *TD = TM.getTargetData();
+
+  if (!GVar->hasInitializer())
+    return;  // External global require no code
+
+  // Check to see if this is a special global used by LLVM, if so, emit it.
+  if (EmitSpecialLLVMGlobal(GVar))
+    return;
+
+  O << "\n\n";
+  std::string SectionName = TAI->SectionForGlobal(GVar);
+  std::string name = Mang->getValueName(GVar);
+  Constant *C = GVar->getInitializer();
+  unsigned Size = TD->getABITypeSize(C->getType());
+  unsigned Align = TD->getPreferredAlignment(GVar);
+
+  // FIXME: ELF supports visibility
+  SwitchToDataSection(SectionName.c_str());
+
+  if (C->isNullValue() && !GVar->hasSection()) {
+    if (!GVar->isThreadLocal() &&
+        (GVar->hasInternalLinkage() || GVar->isWeakForLinker())) {
+      if (Size == 0) Size = 1;   // .comm Foo, 0 is undefined, avoid it.
+
+      if (GVar->hasInternalLinkage())
+        O << "\t.local " << name << "\n";
+
+      O << TAI->getCOMMDirective() << name << ',' << Size;
+      if (TAI->getCOMMDirectiveTakesAlignment())
+        O << ',' << (1 << Align);
+
+      O << '\n';
+      return;
+    }
+  }
+
+  switch (GVar->getLinkage()) {
+   case GlobalValue::CommonLinkage:
+   case GlobalValue::LinkOnceLinkage:
+   case GlobalValue::WeakLinkage:   // FIXME: Verify correct for weak.
+    // Nonnull linkonce -> weak
+    O << "\t.weak " << name << "\n";
+    break;
+   case GlobalValue::AppendingLinkage:
+    // FIXME: appending linkage variables should go into a section of
+    // their name or something.  For now, just emit them as external.
+   case GlobalValue::ExternalLinkage:
+    // If external or appending, declare as a global symbol
+    O << TAI->getGlobalDirective() << name << "\n";
+    // FALL THROUGH
+   case GlobalValue::InternalLinkage:
+    break;
+   case GlobalValue::GhostLinkage:
+    cerr << "Should not have any unmaterialized functions!\n";
+    abort();
+   case GlobalValue::DLLImportLinkage:
+    cerr << "DLLImport linkage is not supported by this target!\n";
+    abort();
+   case GlobalValue::DLLExportLinkage:
+    cerr << "DLLExport linkage is not supported by this target!\n";
+    abort();
+   default:
+    assert(0 && "Unknown linkage type!");
+  }
+
+  if (Align)
+    O << "\t.align " << Align << "\n";
+
+  if (TAI->hasDotTypeDotSizeDirective()) {
+    O << "\t.type " << name << ",#object\n";
+    O << "\t.size " << name << "," << Size << "\n";
+  }
+
+  O << name << ":\n";
+  EmitGlobalConstant(C);
+}