diff --git a/llvm/include/llvm/Target/COFFTargetAsmInfo.h b/llvm/include/llvm/Target/COFFTargetAsmInfo.h index 45eef01d2e44..4a60c0cce42d 100644 --- a/llvm/include/llvm/Target/COFFTargetAsmInfo.h +++ b/llvm/include/llvm/Target/COFFTargetAsmInfo.h @@ -17,12 +17,12 @@ namespace llvm { protected: explicit COFFTargetAsmInfo(const TargetMachine &TM); public: - - virtual const char * - getSectionPrefixForUniqueGlobal(SectionKind kind) const; - virtual void getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl &Str) const; + + virtual const Section * + SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind) const; + }; } diff --git a/llvm/include/llvm/Target/ELFTargetAsmInfo.h b/llvm/include/llvm/Target/ELFTargetAsmInfo.h index a0cbc8e5455c..1b5bc54db62f 100644 --- a/llvm/include/llvm/Target/ELFTargetAsmInfo.h +++ b/llvm/include/llvm/Target/ELFTargetAsmInfo.h @@ -33,8 +33,6 @@ namespace llvm { void getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl &Str) const; - const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) const; - virtual const Section* SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind) const; diff --git a/llvm/include/llvm/Target/TargetAsmInfo.h b/llvm/include/llvm/Target/TargetAsmInfo.h index 9ddaf29f465a..9031dd21388e 100644 --- a/llvm/include/llvm/Target/TargetAsmInfo.h +++ b/llvm/include/llvm/Target/TargetAsmInfo.h @@ -144,6 +144,9 @@ namespace llvm { bool ExplicitSection : 1; public: + // FIXME: REMOVE. + Kind getKind() const { return K; } + bool isWeak() const { return Weak; } bool hasExplicitSection() const { return ExplicitSection; } @@ -669,15 +672,6 @@ namespace llvm { virtual const Section *getSectionForMergeableConstant(SectionKind Kind)const; - /// getSectionPrefixForUniqueGlobal - Return a string that we should prepend - /// onto a global's name in order to get the unique section name for the - /// global. This is important for globals that need to be merged across - /// translation units. - virtual const char * - getSectionPrefixForUniqueGlobal(SectionKind Kind) const { - return 0; - } - /// getKindForNamedSection - If this target wants to be able to override /// section flags based on the name of the section specified for a global /// variable, it can implement this. This is used on ELF systems so that diff --git a/llvm/lib/Target/COFFTargetAsmInfo.cpp b/llvm/lib/Target/COFFTargetAsmInfo.cpp index 5d080b425f7d..cd7356d55116 100644 --- a/llvm/lib/Target/COFFTargetAsmInfo.cpp +++ b/llvm/lib/Target/COFFTargetAsmInfo.cpp @@ -53,15 +53,6 @@ COFFTargetAsmInfo::COFFTargetAsmInfo(const TargetMachine &TM) DwarfMacroInfoSection = "\t.section\t.debug_macinfo,\"dr\""; } -const char *COFFTargetAsmInfo:: -getSectionPrefixForUniqueGlobal(SectionKind Kind) const { - if (Kind.isText()) - return ".text$linkonce"; - if (Kind.isWriteable()) - return ".data$linkonce"; - return ".rdata$linkonce"; -} - void COFFTargetAsmInfo::getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl &Str) const { // FIXME: Inefficient. @@ -74,3 +65,45 @@ void COFFTargetAsmInfo::getSectionFlagsAsString(SectionKind Kind, Str.append(Res.begin(), Res.end()); } + +//===----------------------------------------------------------------------===// +// Move to AsmPrinter (mangler access). +//===----------------------------------------------------------------------===// + +#include "llvm/GlobalVariable.h" + +static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) { + if (Kind.isText()) + return ".text$linkonce"; + if (Kind.isWriteable()) + return ".data$linkonce"; + return ".rdata$linkonce"; +} + +const Section * +COFFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV, + SectionKind Kind) const { + assert(!Kind.isThreadLocal() && "Doesn't support TLS"); + + // If this global is linkonce/weak and the target handles this by emitting it + // into a 'uniqued' section name, create and return the section now. + if (Kind.isWeak()) { + const char *Prefix = getSectionPrefixForUniqueGlobal(Kind); + // FIXME: Use mangler interface (PR4584). + std::string Name = Prefix+GV->getNameStr(); + return getOrCreateSection(Name.c_str(), false, Kind.getKind()); + } + + if (Kind.isText()) + return getTextSection(); + + if (Kind.isBSS()) + if (const Section *S = getBSSSection_()) + return S; + + if (Kind.isReadOnly()) + if (const Section *S = getReadOnlySection()) + return S; + + return getDataSection(); +} diff --git a/llvm/lib/Target/ELFTargetAsmInfo.cpp b/llvm/lib/Target/ELFTargetAsmInfo.cpp index dbb2ce30ca0e..556b494185ed 100644 --- a/llvm/lib/Target/ELFTargetAsmInfo.cpp +++ b/llvm/lib/Target/ELFTargetAsmInfo.cpp @@ -12,16 +12,8 @@ // //===----------------------------------------------------------------------===// -#include "llvm/Constants.h" -#include "llvm/DerivedTypes.h" -#include "llvm/Function.h" -#include "llvm/GlobalVariable.h" -#include "llvm/ADT/StringExtras.h" -#include "llvm/CodeGen/MachineConstantPool.h" -#include "llvm/Support/ErrorHandling.h" #include "llvm/Target/ELFTargetAsmInfo.h" -#include "llvm/Target/TargetMachine.h" -#include "llvm/Target/TargetData.h" +#include "llvm/ADT/SmallVector.h" using namespace llvm; ELFTargetAsmInfo::ELFTargetAsmInfo(const TargetMachine &TM) @@ -53,72 +45,6 @@ ELFTargetAsmInfo::ELFTargetAsmInfo(const TargetMachine &TM) SectionKind::MergeableConst16); } - -const Section* -ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV, - SectionKind Kind) const { - if (Kind.isText()) return TextSection; - if (Kind.isMergeableCString()) { - const TargetData *TD = TM.getTargetData(); - Constant *C = cast(GV)->getInitializer(); - const Type *Ty = cast(C->getType())->getElementType(); - - unsigned Size = TD->getTypeAllocSize(Ty); - if (Size <= 16) { - assert(getCStringSection() && "Should have string section prefix"); - - // We also need alignment here. - // FIXME: this is getting the alignment of the character, not the - // alignment of the string!! - unsigned Align = TD->getPrefTypeAlignment(Ty); - if (Align < Size) - Align = Size; - - std::string Name = getCStringSection() + utostr(Size) + '.' + - utostr(Align); - return getOrCreateSection(Name.c_str(), false, - SectionKind::MergeableCString); - } - - return getReadOnlySection(); - } - - if (Kind.isMergeableConst()) { - if (Kind.isMergeableConst4()) - return MergeableConst4Section; - if (Kind.isMergeableConst8()) - return MergeableConst8Section; - if (Kind.isMergeableConst16()) - return MergeableConst16Section; - return ReadOnlySection; // .const - } - - if (Kind.isReadOnly()) return getReadOnlySection(); - - - if (Kind.isThreadData()) return TLSDataSection; - if (Kind.isThreadBSS()) return TLSBSSSection; - - if (Kind.isBSS()) return getBSSSection_(); - - - if (Kind.isDataNoRel()) return DataSection; - if (Kind.isDataRelLocal()) return DataRelLocalSection; - if (Kind.isDataRel()) return DataRelSection; - if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; - - assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); - return DataRelROSection; -} - -/// getSectionForMergeableConstant - Given a Mergeable constant with the -/// specified size and relocation information, return a section that it -/// should be placed in. -const Section * -ELFTargetAsmInfo::getSectionForMergeableConstant(SectionKind Kind) const { - return SelectSectionForGlobal(0, Kind); -} - /// getFlagsForNamedSection - If this target wants to be able to infer /// section flags based on the name of the section specified for a global /// variable, it can implement this. @@ -148,24 +74,6 @@ SectionKind::Kind ELFTargetAsmInfo::getKindForNamedSection(const char *Name, return K; } -const char * -ELFTargetAsmInfo::getSectionPrefixForUniqueGlobal(SectionKind Kind) const{ - if (Kind.isText()) return ".gnu.linkonce.t."; - if (Kind.isReadOnly()) return ".gnu.linkonce.r."; - - if (Kind.isThreadData()) return ".gnu.linkonce.td."; - if (Kind.isThreadBSS()) return ".gnu.linkonce.tb."; - - if (Kind.isBSS()) return ".gnu.linkonce.b."; - if (Kind.isDataNoRel()) return ".gnu.linkonce.d."; - if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local."; - if (Kind.isDataRel()) return ".gnu.linkonce.d.rel."; - if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local."; - - assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); - return ".gnu.linkonce.d.rel.ro."; -} - void ELFTargetAsmInfo::getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl &Str) const { @@ -219,3 +127,108 @@ void ELFTargetAsmInfo::getSectionFlagsAsString(SectionKind Kind, Str.push_back('6'); } } + + + +//===----------------------------------------------------------------------===// +// Move to AsmPrinter (mangler access). +//===----------------------------------------------------------------------===// + +#include "llvm/DerivedTypes.h" +#include "llvm/GlobalVariable.h" +#include "llvm/Target/TargetData.h" +#include "llvm/Target/TargetMachine.h" +#include "llvm/ADT/StringExtras.h" + +static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) { + if (Kind.isText()) return ".gnu.linkonce.t."; + if (Kind.isReadOnly()) return ".gnu.linkonce.r."; + + if (Kind.isThreadData()) return ".gnu.linkonce.td."; + if (Kind.isThreadBSS()) return ".gnu.linkonce.tb."; + + if (Kind.isBSS()) return ".gnu.linkonce.b."; + if (Kind.isDataNoRel()) return ".gnu.linkonce.d."; + if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local."; + if (Kind.isDataRel()) return ".gnu.linkonce.d.rel."; + if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local."; + + assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); + return ".gnu.linkonce.d.rel.ro."; +} + +const Section* +ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV, + SectionKind Kind) const { + + // If this global is linkonce/weak and the target handles this by emitting it + // into a 'uniqued' section name, create and return the section now. + if (Kind.isWeak()) { + const char *Prefix = getSectionPrefixForUniqueGlobal(Kind); + // FIXME: Use mangler interface (PR4584). + std::string Name = Prefix+GV->getNameStr(); + return getOrCreateSection(Name.c_str(), false, Kind.getKind()); + } + + if (Kind.isText()) return TextSection; + if (Kind.isMergeableCString()) { + const TargetData *TD = TM.getTargetData(); + Constant *C = cast(GV)->getInitializer(); + const Type *Ty = cast(C->getType())->getElementType(); + + unsigned Size = TD->getTypeAllocSize(Ty); + if (Size <= 16) { + assert(getCStringSection() && "Should have string section prefix"); + + // We also need alignment here. + // FIXME: this is getting the alignment of the character, not the + // alignment of the string!! + unsigned Align = TD->getPrefTypeAlignment(Ty); + if (Align < Size) + Align = Size; + + std::string Name = getCStringSection() + utostr(Size) + '.' + + utostr(Align); + return getOrCreateSection(Name.c_str(), false, + SectionKind::MergeableCString); + } + + return getReadOnlySection(); + } + + if (Kind.isMergeableConst()) { + if (Kind.isMergeableConst4()) + return MergeableConst4Section; + if (Kind.isMergeableConst8()) + return MergeableConst8Section; + if (Kind.isMergeableConst16()) + return MergeableConst16Section; + return ReadOnlySection; // .const + } + + if (Kind.isReadOnly()) return getReadOnlySection(); + + + if (Kind.isThreadData()) return TLSDataSection; + if (Kind.isThreadBSS()) return TLSBSSSection; + + if (Kind.isBSS()) return getBSSSection_(); + + + if (Kind.isDataNoRel()) return DataSection; + if (Kind.isDataRelLocal()) return DataRelLocalSection; + if (Kind.isDataRel()) return DataRelSection; + if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; + + assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); + return DataRelROSection; +} + +/// getSectionForMergeableConstant - Given a Mergeable constant with the +/// specified size and relocation information, return a section that it +/// should be placed in. +const Section * +ELFTargetAsmInfo::getSectionForMergeableConstant(SectionKind Kind) const { + return SelectSectionForGlobal(0, Kind); +} + diff --git a/llvm/lib/Target/TargetAsmInfo.cpp b/llvm/lib/Target/TargetAsmInfo.cpp index f5c1458e3d83..1cb085bcd153 100644 --- a/llvm/lib/Target/TargetAsmInfo.cpp +++ b/llvm/lib/Target/TargetAsmInfo.cpp @@ -319,15 +319,6 @@ const Section *TargetAsmInfo::SectionForGlobal(const GlobalValue *GV) const { return getOrCreateSection(GV->getSection().c_str(), false, GVKind); } - // If this global is linkonce/weak and the target handles this by emitting it - // into a 'uniqued' section name, create and return the section now. - if (Kind.isWeak()) { - if (const char *Prefix = getSectionPrefixForUniqueGlobal(Kind)) { - // FIXME: Use mangler interface (PR4584). - std::string Name = Prefix+GV->getNameStr(); - return getOrCreateSection(Name.c_str(), false, GVKind); - } - } // Use default section depending on the 'type' of global return SelectSectionForGlobal(GV, Kind);