Rip all of the global variable lowering logic out of TargetAsmInfo.  Since
it is highly specific to the object file that will be generated in the end,
this introduces a new TargetLoweringObjectFile interface that is implemented
for each of ELF/MachO/COFF/Alpha/PIC16 and XCore.

Though still is still a brutal and ugly refactoring, this is a major step
towards goodness.

This patch also:
1. fixes a bunch of dangling pointer problems in the PIC16 backend.
2. disables the TargetLowering copy ctor which PIC16 was accidentally using.
3. gets us closer to xcore having its own crazy target section flags and
   pic16 not having to shadow sections with its own objects.
4. fixes wierdness where ELF targets would set CStringSection but not
   CStringSection_.  Factor the code better.
5. fixes some bugs in string lowering on ELF targets.




git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@77294 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Target/PIC16/PIC16AsmPrinter.cpp b/lib/Target/PIC16/PIC16AsmPrinter.cpp
index ec86585..7fad6f3 100644
--- a/lib/Target/PIC16/PIC16AsmPrinter.cpp
+++ b/lib/Target/PIC16/PIC16AsmPrinter.cpp
@@ -25,11 +25,20 @@
 #include "llvm/CodeGen/DwarfWriter.h"
 #include "llvm/CodeGen/MachineModuleInfo.h"
 #include "llvm/Target/TargetRegistry.h"
+#include "llvm/Target/TargetLoweringObjectFile.h"
 
 using namespace llvm;
 
 #include "PIC16GenAsmWriter.inc"
 
+PIC16AsmPrinter::PIC16AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
+                                 const TargetAsmInfo *T, bool V)
+: AsmPrinter(O, TM, T, V), DbgInfo(O, T) {
+  PTLI = static_cast<const PIC16TargetLowering*>(TM.getTargetLowering());
+  PTAI = static_cast<const PIC16TargetAsmInfo*>(T);
+  PTOF = static_cast<const PIC16TargetObjectFile*>(&PTLI->getObjFileLowering());
+}
+
 bool PIC16AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
   printInstruction(MI);
   return true;
@@ -59,10 +68,12 @@
   EmitAutos(CurrentFnName);
 
   // Now emit the instructions of function in its code section.
-  const char *codeSection = PAN::getCodeSectionName(CurrentFnName).c_str();
+  std::string T = PAN::getCodeSectionName(CurrentFnName);
+  const char *codeSection = T.c_str();
  
   const Section *fCodeSection = 
-    TAI->getOrCreateSection(codeSection, false, SectionKind::Text);
+    getObjFileLowering().getOrCreateSection(codeSection, false, 
+                                            SectionKind::Text);
   // Start the Code Section.
   O <<  "\n";
   SwitchToSection(fCodeSection);
@@ -211,9 +222,8 @@
 
   // Set the section names for all globals.
   for (Module::global_iterator I = M.global_begin(), E = M.global_end();
-       I != E; ++I) {
-    I->setSection(TAI->SectionForGlobal(I)->getName());
-  }
+       I != E; ++I)
+    I->setSection(getObjFileLowering().SectionForGlobal(I, TM)->getName());
 
   DbgInfo.BeginModule(M);
   EmitFunctionDecls(M);
@@ -256,7 +266,7 @@
 
 // Emit variables imported from other Modules.
 void PIC16AsmPrinter::EmitUndefinedVars(Module &M) {
-  std::vector<const GlobalVariable*> Items = PTAI->ExternalVarDecls->Items;
+  std::vector<const GlobalVariable*> Items = PTOF->ExternalVarDecls->Items;
   if (!Items.size()) return;
 
   O << "\n" << TAI->getCommentString() << "Imported Variables - BEGIN" << "\n";
@@ -268,7 +278,7 @@
 
 // Emit variables defined in this module and are available to other modules.
 void PIC16AsmPrinter::EmitDefinedVars(Module &M) {
-  std::vector<const GlobalVariable*> Items = PTAI->ExternalVarDefs->Items;
+  std::vector<const GlobalVariable*> Items = PTOF->ExternalVarDefs->Items;
   if (!Items.size()) return;
 
   O << "\n" << TAI->getCommentString() << "Exported Variables - BEGIN" << "\n";
@@ -281,12 +291,12 @@
 // Emit initialized data placed in ROM.
 void PIC16AsmPrinter::EmitRomData(Module &M) {
   // Print ROM Data section.
-  const std::vector<PIC16Section*> &ROSections = PTAI->ROSections;
+  const std::vector<PIC16Section*> &ROSections = PTOF->ROSections;
   for (unsigned i = 0; i < ROSections.size(); i++) {
     const std::vector<const GlobalVariable*> &Items = ROSections[i]->Items;
     if (!Items.size()) continue;
     O << "\n";
-    SwitchToSection(PTAI->ROSections[i]->S_);
+    SwitchToSection(PTOF->ROSections[i]->S_);
     for (unsigned j = 0; j < Items.size(); j++) {
       O << Mang->getMangledName(Items[j]);
       Constant *C = Items[j]->getInitializer();
@@ -310,10 +320,12 @@
   const TargetData *TD = TM.getTargetData();
   // Emit the data section name.
   O << "\n"; 
-  const char *SectionName = PAN::getFrameSectionName(CurrentFnName).c_str();
+  std::string T = PAN::getFrameSectionName(CurrentFnName);
+  const char *SectionName = T.c_str();
 
   const Section *fPDataSection =
-    TAI->getOrCreateSection(SectionName, false, SectionKind::DataRel);
+    getObjFileLowering().getOrCreateSection(SectionName, false,
+                                            SectionKind::DataRel);
   SwitchToSection(fPDataSection);
   
   // Emit function frame label
@@ -352,7 +364,7 @@
 void PIC16AsmPrinter::EmitIData(Module &M) {
 
   // Print all IDATA sections.
-  const std::vector<PIC16Section*> &IDATASections = PTAI->IDATASections;
+  const std::vector<PIC16Section*> &IDATASections = PTOF->IDATASections;
   for (unsigned i = 0; i < IDATASections.size(); i++) {
     O << "\n";
     if (IDATASections[i]->S_->getName().find("llvm.") != std::string::npos)
@@ -373,7 +385,7 @@
   const TargetData *TD = TM.getTargetData();
 
   // Print all BSS sections.
-  const std::vector<PIC16Section*> &BSSSections = PTAI->BSSSections;
+  const std::vector<PIC16Section*> &BSSSections = PTOF->BSSSections;
   for (unsigned i = 0; i < BSSSections.size(); i++) {
     O << "\n";
     SwitchToSection(BSSSections[i]->S_);
@@ -395,7 +407,7 @@
 
   // Now print Autos section for this function.
   std::string SectionName = PAN::getAutosSectionName(FunctName);
-  const std::vector<PIC16Section*> &AutosSections = PTAI->AutosSections;
+  const std::vector<PIC16Section*> &AutosSections = PTOF->AutosSections;
   for (unsigned i = 0; i < AutosSections.size(); i++) {
     O << "\n";
     if (AutosSections[i]->S_->getName() == SectionName) { 
@@ -422,7 +434,7 @@
   const TargetData *TD = TM.getTargetData();
 
   // Now print Autos section for this function.
-  std::vector <PIC16Section *>AutosSections = PTAI->AutosSections;
+  std::vector <PIC16Section *>AutosSections = PTOF->AutosSections;
   for (unsigned i = 0; i < AutosSections.size(); i++) {
     
     // if the section is already printed then don't print again