From dc62ea0f1c6eb0673f39c543f1936ce125efdb30 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Wed, 16 Sep 2009 06:25:03 +0000 Subject: [PATCH] move FnStubs/GVSTubs/HiddenGVStub handling out of the X86 asmprinter 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 | 3 +- include/llvm/CodeGen/MachineModuleInfo.h | 6 +- include/llvm/CodeGen/MachineModuleInfoImpls.h | 5 +- .../X86/AsmPrinter/X86ATTAsmPrinter.cpp | 62 +++++++++---------- lib/Target/X86/AsmPrinter/X86ATTAsmPrinter.h | 5 -- lib/Target/X86/AsmPrinter/X86MCInstLower.cpp | 16 +++-- lib/Target/X86/AsmPrinter/X86MCInstLower.h | 4 ++ 7 files changed, 52 insertions(+), 49 deletions(-) diff --git a/include/llvm/CodeGen/AsmPrinter.h b/include/llvm/CodeGen/AsmPrinter.h index fe6249b88de..a1765c4c252 100644 --- a/include/llvm/CodeGen/AsmPrinter.h +++ b/include/llvm/CodeGen/AsmPrinter.h @@ -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; diff --git a/include/llvm/CodeGen/MachineModuleInfo.h b/include/llvm/CodeGen/MachineModuleInfo.h index 2d23495b489..e5a962d26b7 100644 --- a/include/llvm/CodeGen/MachineModuleInfo.h +++ b/include/llvm/CodeGen/MachineModuleInfo.h @@ -163,17 +163,17 @@ public: /// backends that would like to do so. /// template - Ty *getObjFileInfo() { + Ty &getObjFileInfo() { if (ObjFileMMI == 0) ObjFileMMI = new Ty(*this); assert((void*)dynamic_cast(ObjFileMMI) == (void*)ObjFileMMI && "Invalid concrete type or multiple inheritence for getInfo"); - return static_cast(ObjFileMMI); + return *static_cast(ObjFileMMI); } template - const Ty *getObjFileInfo() const { + const Ty &getObjFileInfo() const { return const_cast(this)->getObjFileInfo(); } diff --git a/include/llvm/CodeGen/MachineModuleInfoImpls.h b/include/llvm/CodeGen/MachineModuleInfoImpls.h index 492c016fd6c..44813cbdcd9 100644 --- a/include/llvm/CodeGen/MachineModuleInfoImpls.h +++ b/include/llvm/CodeGen/MachineModuleInfoImpls.h @@ -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]; } diff --git a/lib/Target/X86/AsmPrinter/X86ATTAsmPrinter.cpp b/lib/Target/X86/AsmPrinter/X86ATTAsmPrinter.cpp index 160d298be33..6365bc0f957 100644 --- a/lib/Target/X86/AsmPrinter/X86ATTAsmPrinter.cpp +++ b/lib/Target/X86/AsmPrinter/X86ATTAsmPrinter.cpp @@ -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().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().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().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().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*)LHS)->first; - MCSymbol *RHSS = ((const std::pair*)RHS)->first; - return LHSS->getName().compare(RHSS->getName()); -} - -/// GetSortedStubs - Return the entries from a DenseMap in a deterministic -/// sorted orer. -static std::vector > -GetSortedStubs(const DenseMap &Map) { - assert(!Map.empty()); - std::vector > 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(getObjFileLowering()); + MachineModuleInfoMachO &MMIMacho = + MMI->getObjFileInfo(); + // 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 > 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 > 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 > 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 diff --git a/lib/Target/X86/AsmPrinter/X86ATTAsmPrinter.h b/lib/Target/X86/AsmPrinter/X86ATTAsmPrinter.h index 33042dc7591..78e3d7de62c 100644 --- a/lib/Target/X86/AsmPrinter/X86ATTAsmPrinter.h +++ b/lib/Target/X86/AsmPrinter/X86ATTAsmPrinter.h @@ -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 FnStubs; // Darwin $stub stubs. - DenseMap GVStubs; // Darwin $non_lazy_ptr stub. - DenseMap HiddenGVStubs; // Darwin $non_lazy_ptr stub. - // Necessary for dllexport support StringSet<> CygMingStubs, DLLExportedFns, DLLExportedGVs; diff --git a/lib/Target/X86/AsmPrinter/X86MCInstLower.cpp b/lib/Target/X86/AsmPrinter/X86MCInstLower.cpp index fcdfdc7e6a9..116e4b05023 100644 --- a/lib/Target/X86/AsmPrinter/X86MCInstLower.cpp +++ b/lib/Target/X86/AsmPrinter/X86MCInstLower.cpp @@ -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(); +} + 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()); diff --git a/lib/Target/X86/AsmPrinter/X86MCInstLower.h b/lib/Target/X86/AsmPrinter/X86MCInstLower.h index 6bf557462eb..effd9604028 100644 --- a/lib/Target/X86/AsmPrinter/X86MCInstLower.h +++ b/lib/Target/X86/AsmPrinter/X86MCInstLower.h @@ -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; }; }