move FnStubs/GVSTubs/HiddenGVStub handling out of the X86 asmprinter
authorChris Lattner <sabre@nondot.org>
Wed, 16 Sep 2009 06:25:03 +0000 (06:25 +0000)
committerChris Lattner <sabre@nondot.org>
Wed, 16 Sep 2009 06:25:03 +0000 (06:25 +0000)
and use MachineModuleInfoMachO instead.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@82022 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/CodeGen/AsmPrinter.h
include/llvm/CodeGen/MachineModuleInfo.h
include/llvm/CodeGen/MachineModuleInfoImpls.h
lib/Target/X86/AsmPrinter/X86ATTAsmPrinter.cpp
lib/Target/X86/AsmPrinter/X86ATTAsmPrinter.h
lib/Target/X86/AsmPrinter/X86MCInstLower.cpp
lib/Target/X86/AsmPrinter/X86MCInstLower.h

index fe6249b88de287f722d982924a4ccc4158be87f4..a1765c4c2523238cd31f380e4d7de704218cf5f2 100644 (file)
@@ -76,10 +76,11 @@ namespace llvm {
     ///
     MachineLoopInfo *LI;
 
-  protected:
+  public:
     /// MMI - If available, this is a pointer to the current MachineModuleInfo.
     MachineModuleInfo *MMI;
     
+  protected:
     /// DW - If available, this is a pointer to the current dwarf writer.
     DwarfWriter *DW;
 
index 2d23495b48998d26e937c9c97c3b0b9cfe5a8e7d..e5a962d26b7a8886408ec3d9ba2e79d42d9b9b2b 100644 (file)
@@ -163,17 +163,17 @@ public:
   /// backends that would like to do so.
   ///
   template<typename Ty>
-  Ty *getObjFileInfo() {
+  Ty &getObjFileInfo() {
     if (ObjFileMMI == 0)
       ObjFileMMI = new Ty(*this);
     
     assert((void*)dynamic_cast<Ty*>(ObjFileMMI) == (void*)ObjFileMMI &&
            "Invalid concrete type or multiple inheritence for getInfo");
-    return static_cast<Ty*>(ObjFileMMI);
+    return *static_cast<Ty*>(ObjFileMMI);
   }
   
   template<typename Ty>
-  const Ty *getObjFileInfo() const {
+  const Ty &getObjFileInfo() const {
     return const_cast<MachineModuleInfo*>(this)->getObjFileInfo<Ty>();
   }
   
index 492c016fd6c03d030610bdfc75f36a736b36c583..44813cbdcd959e09f6c2cbeb16a359c77c6d41fc 100644 (file)
@@ -38,18 +38,19 @@ namespace llvm {
     
     virtual void Anchor();  // Out of line virtual method.
   public:
+    MachineModuleInfoMachO(const MachineModuleInfo &) {}
     
     const MCSymbol *&getFnStubEntry(const MCSymbol *Sym) {
       assert(Sym && "Key cannot be null");
       return FnStubs[Sym];
     }
 
-    const MCSymbol *&getGVStubsEntry(const MCSymbol *Sym) {
+    const MCSymbol *&getGVStubEntry(const MCSymbol *Sym) {
       assert(Sym && "Key cannot be null");
       return GVStubs[Sym];
     }
 
-    const MCSymbol *&getHiddenGVStubsEntry(const MCSymbol *Sym) {
+    const MCSymbol *&getHiddenGVStubEntry(const MCSymbol *Sym) {
       assert(Sym && "Key cannot be null");
       return HiddenGVStubs[Sym];
     }
index 160d298be33fb5a1d2e92c91211f3c4c5b5fdeb0..6365bc0f9573429d517baf912c136c825678bf30 100644 (file)
@@ -31,6 +31,7 @@
 #include "llvm/MC/MCStreamer.h"
 #include "llvm/MC/MCSymbol.h"
 #include "llvm/CodeGen/MachineJumpTableInfo.h"
+#include "llvm/CodeGen/MachineModuleInfoImpls.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/FormattedStream.h"
 #include "llvm/Support/Mangler.h"
@@ -320,7 +321,9 @@ void X86ATTAsmPrinter::printSymbolOperand(const MachineOperand &MO) {
       Mang->getNameWithPrefix(NameStr, GV, true);
       NameStr += "$non_lazy_ptr";
       MCSymbol *Sym = OutContext.GetOrCreateSymbol(NameStr.str());
-      MCSymbol *&StubSym = GVStubs[Sym];
+      
+      const MCSymbol *&StubSym = 
+        MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(Sym);
       if (StubSym == 0) {
         NameStr.clear();
         Mang->getNameWithPrefix(NameStr, GV, false);
@@ -331,7 +334,8 @@ void X86ATTAsmPrinter::printSymbolOperand(const MachineOperand &MO) {
       Mang->getNameWithPrefix(NameStr, GV, true);
       NameStr += "$non_lazy_ptr";
       MCSymbol *Sym = OutContext.GetOrCreateSymbol(NameStr.str());
-      MCSymbol *&StubSym = HiddenGVStubs[Sym];
+      const MCSymbol *&StubSym =
+        MMI->getObjFileInfo<MachineModuleInfoMachO>().getHiddenGVStubEntry(Sym);
       if (StubSym == 0) {
         NameStr.clear();
         Mang->getNameWithPrefix(NameStr, GV, false);
@@ -342,7 +346,8 @@ void X86ATTAsmPrinter::printSymbolOperand(const MachineOperand &MO) {
       Mang->getNameWithPrefix(NameStr, GV, true);
       NameStr += "$stub";
       MCSymbol *Sym = OutContext.GetOrCreateSymbol(NameStr.str());
-      MCSymbol *&StubSym = FnStubs[Sym];
+      const MCSymbol *&StubSym =
+        MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
       if (StubSym == 0) {
         NameStr.clear();
         Mang->getNameWithPrefix(NameStr, GV, false);
@@ -364,7 +369,9 @@ void X86ATTAsmPrinter::printSymbolOperand(const MachineOperand &MO) {
     std::string Name = Mang->makeNameProper(MO.getSymbolName());
     if (MO.getTargetFlags() == X86II::MO_DARWIN_STUB) {
       Name += "$stub";
-      MCSymbol *&StubSym = FnStubs[OutContext.GetOrCreateSymbol(Name)];
+      MCSymbol *Sym = OutContext.GetOrCreateSymbol(Name);
+      const MCSymbol *&StubSym =
+        MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
       if (StubSym == 0) {
         Name.erase(Name.end()-5, Name.end());
         StubSym = OutContext.GetOrCreateSymbol(Name);
@@ -872,28 +879,15 @@ void X86ATTAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
     O << "\t.size\t" << name << ", " << Size << '\n';
 }
 
-static int SortSymbolPair(const void *LHS, const void *RHS) {
-  MCSymbol *LHSS = ((const std::pair<MCSymbol*, MCSymbol*>*)LHS)->first;
-  MCSymbol *RHSS = ((const std::pair<MCSymbol*, MCSymbol*>*)RHS)->first;
-  return LHSS->getName().compare(RHSS->getName());
-}
-
-/// GetSortedStubs - Return the entries from a DenseMap in a deterministic
-/// sorted orer.
-static std::vector<std::pair<MCSymbol*, MCSymbol*> >
-GetSortedStubs(const DenseMap<MCSymbol*, MCSymbol*> &Map) {
-  assert(!Map.empty());
-  std::vector<std::pair<MCSymbol*, MCSymbol*> > List(Map.begin(), Map.end());
-  qsort(&List[0], List.size(), sizeof(List[0]), SortSymbolPair);
-  return List;
-}
-
 bool X86ATTAsmPrinter::doFinalization(Module &M) {
   if (Subtarget->isTargetDarwin()) {
     // All darwin targets use mach-o.
     TargetLoweringObjectFileMachO &TLOFMacho = 
       static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
     
+    MachineModuleInfoMachO &MMIMacho =
+      MMI->getObjFileInfo<MachineModuleInfoMachO>();
+    
     // Add the (possibly multiple) personalities to the set of global value
     // stubs.  Only referenced functions get into the Personalities list.
     if (!Subtarget->is64Bit()) {
@@ -907,18 +901,18 @@ bool X86ATTAsmPrinter::doFinalization(Module &M) {
         Name += "$non_lazy_ptr";
         MCSymbol *NLPName = OutContext.GetOrCreateSymbol(Name.str());
 
-        MCSymbol *&StubName = GVStubs[NLPName];
-        if (StubName != 0) continue;
-        
-
+        const MCSymbol *&StubName = MMIMacho.getGVStubEntry(NLPName);
         Name.clear();
         Mang->getNameWithPrefix(Name, Personalities[i], false);
         StubName = OutContext.GetOrCreateSymbol(Name.str());
       }
     }
 
-    // Output stubs for dynamically-linked functions
-    if (!FnStubs.empty()) {
+    // Output stubs for dynamically-linked functions.
+    MachineModuleInfoMachO::SymbolListTy Stubs;
+
+    Stubs = MMIMacho.GetFnStubList();
+    if (!Stubs.empty()) {
       const MCSection *TheSection = 
         TLOFMacho.getMachOSection("__IMPORT", "__jump_table",
                                   MCSectionMachO::S_SYMBOL_STUBS |
@@ -927,8 +921,6 @@ bool X86ATTAsmPrinter::doFinalization(Module &M) {
                                   5, SectionKind::getMetadata());
       OutStreamer.SwitchSection(TheSection);
 
-      std::vector<std::pair<MCSymbol*, MCSymbol*> > Stubs
-        = GetSortedStubs(FnStubs);
       for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
         Stubs[i].first->print(O, MAI);
         O << ":\n" << "\t.indirect_symbol ";
@@ -937,38 +929,40 @@ bool X86ATTAsmPrinter::doFinalization(Module &M) {
         O << "\n\thlt ; hlt ; hlt ; hlt ; hlt\n";
       }
       O << '\n';
+      
+      Stubs.clear();
     }
 
     // Output stubs for external and common global variables.
-    if (!GVStubs.empty()) {
+    Stubs = MMIMacho.GetGVStubList();
+    if (!Stubs.empty()) {
       const MCSection *TheSection = 
         TLOFMacho.getMachOSection("__IMPORT", "__pointers",
                                   MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
                                   SectionKind::getMetadata());
       OutStreamer.SwitchSection(TheSection);
 
-      std::vector<std::pair<MCSymbol*, MCSymbol*> > Stubs
-        = GetSortedStubs(GVStubs);
       for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
         Stubs[i].first->print(O, MAI);
         O << ":\n\t.indirect_symbol ";
         Stubs[i].second->print(O, MAI);
         O << "\n\t.long\t0\n";
       }
+      Stubs.clear();
     }
 
-    if (!HiddenGVStubs.empty()) {
+    Stubs = MMIMacho.GetHiddenGVStubList();
+    if (!Stubs.empty()) {
       OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
       EmitAlignment(2);
 
-      std::vector<std::pair<MCSymbol*, MCSymbol*> > Stubs
-        = GetSortedStubs(HiddenGVStubs);
       for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
         Stubs[i].first->print(O, MAI);
         O << ":\n" << MAI->getData32bitsDirective();
         Stubs[i].second->print(O, MAI);
         O << '\n';
       }
+      Stubs.clear();
     }
 
     // Funny Darwin hack: This flag tells the linker that no global symbols
index 33042dc7591e94e86913e3d24a69cb6d445bbbfa..78e3d7de62cef51d1bd07fb83664dd62315744ff 100644 (file)
@@ -152,11 +152,6 @@ class VISIBILITY_HIDDEN X86ATTAsmPrinter : public AsmPrinter {
 
   void emitFunctionHeader(const MachineFunction &MF);
 
-  // Necessary for Darwin to print out the appropriate types of linker stubs.
-  DenseMap<MCSymbol*, MCSymbol*> FnStubs;  // Darwin $stub stubs.
-  DenseMap<MCSymbol*, MCSymbol*> GVStubs;  // Darwin $non_lazy_ptr stub.
-  DenseMap<MCSymbol*, MCSymbol*> HiddenGVStubs;  // Darwin $non_lazy_ptr stub.
-
   // Necessary for dllexport support
   StringSet<> CygMingStubs, DLLExportedFns, DLLExportedGVs;
 
index fcdfdc7e6a9c4d995fcb8ad3afdba7f8beeebf98..116e4b050230114c1191938cad36067c692014ec 100644 (file)
@@ -15,6 +15,7 @@
 #include "X86MCInstLower.h"
 #include "X86ATTAsmPrinter.h"
 #include "X86MCAsmInfo.h"
+#include "llvm/CodeGen/MachineModuleInfoImpls.h"
 #include "llvm/MC/MCContext.h"
 #include "llvm/MC/MCExpr.h"
 #include "llvm/MC/MCInst.h"
@@ -29,6 +30,11 @@ const X86Subtarget &X86MCInstLower::getSubtarget() const {
   return AsmPrinter.getSubtarget();
 }
 
+MachineModuleInfoMachO &X86MCInstLower::getMachOMMI() const {
+  assert(getSubtarget().isTargetDarwin() &&"Can only get MachO info on darwin");
+  return AsmPrinter.MMI->getObjFileInfo<MachineModuleInfoMachO>(); 
+}
+
 
 MCSymbol *X86MCInstLower::GetPICBaseSymbol() const {
   SmallString<60> Name;
@@ -72,7 +78,8 @@ GetGlobalAddressSymbol(const MachineOperand &MO) const {
   case X86II::MO_DARWIN_NONLAZY_PIC_BASE: {
     Name += "$non_lazy_ptr";
     MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name.str());
-    MCSymbol *&StubSym = AsmPrinter.GVStubs[Sym];
+
+    const MCSymbol *&StubSym = getMachOMMI().getGVStubEntry(Sym);
     if (StubSym == 0) {
       Name.clear();
       Mang->getNameWithPrefix(Name, GV, false);
@@ -83,7 +90,7 @@ GetGlobalAddressSymbol(const MachineOperand &MO) const {
   case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE: {
     Name += "$non_lazy_ptr";
     MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name.str());
-    MCSymbol *&StubSym = AsmPrinter.HiddenGVStubs[Sym];
+    const MCSymbol *&StubSym = getMachOMMI().getHiddenGVStubEntry(Sym);
     if (StubSym == 0) {
       Name.clear();
       Mang->getNameWithPrefix(Name, GV, false);
@@ -94,7 +101,7 @@ GetGlobalAddressSymbol(const MachineOperand &MO) const {
   case X86II::MO_DARWIN_STUB: {
     Name += "$stub";
     MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name.str());
-    MCSymbol *&StubSym = AsmPrinter.FnStubs[Sym];
+    const MCSymbol *&StubSym = getMachOMMI().getFnStubEntry(Sym);
     if (StubSym == 0) {
       Name.clear();
       Mang->getNameWithPrefix(Name, GV, false);
@@ -138,7 +145,8 @@ GetExternalSymbolSymbol(const MachineOperand &MO) const {
   case X86II::MO_DARWIN_STUB: {
     Name += "$stub";
     MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name.str());
-    MCSymbol *&StubSym = AsmPrinter.FnStubs[Sym];
+    const MCSymbol *&StubSym = getMachOMMI().getFnStubEntry(Sym);
+
     if (StubSym == 0) {
       Name.erase(Name.end()-5, Name.end());
       StubSym = Ctx.GetOrCreateSymbol(Name.str());
index 6bf557462eb52c7bb383c3a46667c1485cf8d97c..effd9604028011b025edf7f123bd64936556c489 100644 (file)
@@ -18,6 +18,7 @@ namespace llvm {
   class MCOperand;
   class MCSymbol;
   class MachineInstr;
+  class MachineModuleInfoMachO;
   class MachineOperand;
   class Mangler;
   class X86ATTAsmPrinter;
@@ -43,6 +44,9 @@ public:
   MCSymbol *GetJumpTableSymbol(const MachineOperand &MO) const;
   MCSymbol *GetConstantPoolIndexSymbol(const MachineOperand &MO) const;
   MCOperand LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const;
+  
+private:
+  MachineModuleInfoMachO &getMachOMMI() const;
 };
 
 }