move macho section uniquing from MCParser and TLOF to MCContext where

the compiler and asmparser now unique to the same sections.  This fixes
rdar://7835021.



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@100807 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2010-04-08 20:30:37 +00:00
parent a57fabe815
commit f0559e4b24
7 changed files with 87 additions and 106 deletions

View File

@ -90,8 +90,6 @@ public:
class TargetLoweringObjectFileMachO : public TargetLoweringObjectFile { class TargetLoweringObjectFileMachO : public TargetLoweringObjectFile {
mutable void *UniquingMap;
const MCSection *CStringSection; const MCSection *CStringSection;
const MCSection *UStringSection; const MCSection *UStringSection;
const MCSection *TextCoalSection; const MCSection *TextCoalSection;
@ -108,8 +106,8 @@ class TargetLoweringObjectFileMachO : public TargetLoweringObjectFile {
const MCSection *LazySymbolPointerSection; const MCSection *LazySymbolPointerSection;
const MCSection *NonLazySymbolPointerSection; const MCSection *NonLazySymbolPointerSection;
public: public:
TargetLoweringObjectFileMachO() : UniquingMap(0) {} TargetLoweringObjectFileMachO() {}
~TargetLoweringObjectFileMachO(); ~TargetLoweringObjectFileMachO() {}
virtual void Initialize(MCContext &Ctx, const TargetMachine &TM); virtual void Initialize(MCContext &Ctx, const TargetMachine &TM);

View File

@ -10,6 +10,7 @@
#ifndef LLVM_MC_MCCONTEXT_H #ifndef LLVM_MC_MCCONTEXT_H
#define LLVM_MC_MCCONTEXT_H #define LLVM_MC_MCCONTEXT_H
#include "llvm/MC/SectionKind.h"
#include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringMap.h"
#include "llvm/Support/Allocator.h" #include "llvm/Support/Allocator.h"
@ -21,6 +22,7 @@ namespace llvm {
class MCSymbol; class MCSymbol;
class StringRef; class StringRef;
class Twine; class Twine;
class MCSectionMachO;
/// MCContext - Context object for machine code objects. This class owns all /// MCContext - Context object for machine code objects. This class owns all
/// of the sections that it creates. /// of the sections that it creates.
@ -47,6 +49,8 @@ namespace llvm {
/// We use a bump pointer allocator to avoid the need to track all allocated /// We use a bump pointer allocator to avoid the need to track all allocated
/// objects. /// objects.
BumpPtrAllocator Allocator; BumpPtrAllocator Allocator;
void *MachOUniquingMap;
public: public:
explicit MCContext(const MCAsmInfo &MAI); explicit MCContext(const MCAsmInfo &MAI);
~MCContext(); ~MCContext();
@ -72,6 +76,17 @@ namespace llvm {
MCSymbol *LookupSymbol(StringRef Name) const; MCSymbol *LookupSymbol(StringRef Name) const;
/// @} /// @}
/// @name Section Managment
/// @{
const MCSectionMachO *getMachOSection(StringRef Segment,
StringRef Section,
unsigned TypeAndAttributes,
unsigned Reserved2,
SectionKind K);
/// @}
void *Allocate(unsigned Size, unsigned Align = 8) { void *Allocate(unsigned Size, unsigned Align = 8) {
return Allocator.Allocate(Size, Align); return Allocator.Allocate(Size, Align);

View File

@ -50,10 +50,6 @@ private:
AsmCond TheCondState; AsmCond TheCondState;
std::vector<AsmCond> TheCondStack; std::vector<AsmCond> TheCondStack;
// FIXME: Figure out where this should leave, the code is a copy of that which
// is also used by TargetLoweringObjectFile.
mutable void *SectionUniquingMap;
/// DirectiveMap - This is a table handlers for directives. Each handler is /// DirectiveMap - This is a table handlers for directives. Each handler is
/// invoked after the directive identifier is read and is responsible for /// invoked after the directive identifier is read and is responsible for
/// parsing and validating the rest of the directive. The handler is passed /// parsing and validating the rest of the directive. The handler is passed
@ -97,13 +93,6 @@ public:
private: private:
MCSymbol *CreateSymbol(StringRef Name); MCSymbol *CreateSymbol(StringRef Name);
// FIXME: See comment on SectionUniquingMap.
const MCSection *getMachOSection(const StringRef &Segment,
const StringRef &Section,
unsigned TypeAndAttributes,
unsigned Reserved2,
SectionKind Kind) const;
bool ParseStatement(); bool ParseStatement();
bool TokError(const char *Msg); bool TokError(const char *Msg);

View File

@ -50,7 +50,6 @@ class MCSectionMachO : public MCSection {
SectionName[i] = 0; SectionName[i] = 0;
} }
} }
public:
static MCSectionMachO *Create(StringRef Segment, static MCSectionMachO *Create(StringRef Segment,
StringRef Section, StringRef Section,
@ -58,6 +57,9 @@ public:
unsigned Reserved2, unsigned Reserved2,
SectionKind K, MCContext &Ctx); SectionKind K, MCContext &Ctx);
friend class MCContext;
public:
/// These are the section type and attributes fields. A MachO section can /// These are the section type and attributes fields. A MachO section can
/// have only one Type, but can have any of the attributes specified. /// have only one Type, but can have any of the attributes specified.
enum { enum {

View File

@ -426,40 +426,14 @@ getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
// MachO // MachO
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
TargetLoweringObjectFileMachO::~TargetLoweringObjectFileMachO() {
// If we have the MachO uniquing map, free it.
delete (MachOUniqueMapTy*)UniquingMap;
}
const MCSectionMachO *TargetLoweringObjectFileMachO:: const MCSectionMachO *TargetLoweringObjectFileMachO::
getMachOSection(StringRef Segment, StringRef Section, getMachOSection(StringRef Segment, StringRef Section,
unsigned TypeAndAttributes, unsigned TypeAndAttributes,
unsigned Reserved2, SectionKind Kind) const { unsigned Reserved2, SectionKind Kind) const {
// We unique sections by their segment/section pair. The returned section
// may not have the same flags as the requested section, if so this should be return getContext().getMachOSection(Segment, Section, TypeAndAttributes,
// diagnosed by the client as an error. Reserved2, Kind);
// Create the map if it doesn't already exist.
if (UniquingMap == 0)
UniquingMap = new MachOUniqueMapTy();
MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)UniquingMap;
// Form the name to look up.
SmallString<64> Name;
Name += Segment;
Name.push_back(',');
Name += Section;
// Do the lookup, if we have a hit, return it.
const MCSectionMachO *&Entry = Map[Name.str()];
if (Entry) return Entry;
// Otherwise, return a new section.
return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
Reserved2, Kind, getContext());
} }
@ -473,8 +447,6 @@ void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
IsFunctionEHFrameSymbolPrivate = false; IsFunctionEHFrameSymbolPrivate = false;
SupportsWeakOmittedEHFrame = false; SupportsWeakOmittedEHFrame = false;
if (UniquingMap != 0)
((MachOUniqueMapTy*)UniquingMap)->clear();
TargetLoweringObjectFile::Initialize(Ctx, TM); TargetLoweringObjectFile::Initialize(Ctx, TM);
TextSection // .text TextSection // .text
@ -806,7 +778,7 @@ const MCSection *TargetLoweringObjectFileCOFF::
getCOFFSection(StringRef Name, bool isDirective, SectionKind Kind) const { getCOFFSection(StringRef Name, bool isDirective, SectionKind Kind) const {
// Create the map if it doesn't already exist. // Create the map if it doesn't already exist.
if (UniquingMap == 0) if (UniquingMap == 0)
UniquingMap = new MachOUniqueMapTy(); UniquingMap = new COFFUniqueMapTy();
COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)UniquingMap; COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)UniquingMap;
// Do the lookup, if we have a hit, return it. // Do the lookup, if we have a hit, return it.

View File

@ -9,20 +9,31 @@
#include "llvm/MC/MCContext.h" #include "llvm/MC/MCContext.h"
#include "llvm/MC/MCAsmInfo.h" #include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCSection.h" #include "llvm/MC/MCSectionMachO.h"
#include "llvm/MC/MCSymbol.h" #include "llvm/MC/MCSymbol.h"
#include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallString.h"
#include "llvm/ADT/Twine.h" #include "llvm/ADT/Twine.h"
using namespace llvm; using namespace llvm;
typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
MCContext::MCContext(const MCAsmInfo &mai) : MAI(mai), NextUniqueID(0) { MCContext::MCContext(const MCAsmInfo &mai) : MAI(mai), NextUniqueID(0) {
MachOUniquingMap = 0;
} }
MCContext::~MCContext() { MCContext::~MCContext() {
// NOTE: The sections are all allocated out of a bump pointer allocator, // NOTE: The symbols are all allocated out of a bump pointer allocator,
// we don't need to free them here. // we don't need to free them here.
// If we have the MachO uniquing map, free it.
delete (MachOUniqueMapTy*)MachOUniquingMap;
} }
//===----------------------------------------------------------------------===//
// Symbol Manipulation
//===----------------------------------------------------------------------===//
MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) { MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name) {
assert(!Name.empty() && "Normal symbols cannot be unnamed!"); assert(!Name.empty() && "Normal symbols cannot be unnamed!");
@ -55,3 +66,36 @@ MCSymbol *MCContext::CreateTempSymbol() {
MCSymbol *MCContext::LookupSymbol(StringRef Name) const { MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
return Symbols.lookup(Name); return Symbols.lookup(Name);
} }
//===----------------------------------------------------------------------===//
// Section Management
//===----------------------------------------------------------------------===//
const MCSectionMachO *MCContext::
getMachOSection(StringRef Segment, StringRef Section,
unsigned TypeAndAttributes,
unsigned Reserved2, SectionKind Kind) {
// We unique sections by their segment/section pair. The returned section
// may not have the same flags as the requested section, if so this should be
// diagnosed by the client as an error.
// Create the map if it doesn't already exist.
if (MachOUniquingMap == 0)
MachOUniquingMap = new MachOUniqueMapTy();
MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap;
// Form the name to look up.
SmallString<64> Name;
Name += Segment;
Name.push_back(',');
Name += Section;
// Do the lookup, if we have a hit, return it.
const MCSectionMachO *&Entry = Map[Name.str()];
if (Entry) return Entry;
// Otherwise, return a new section.
return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
Reserved2, Kind, *this);
}

View File

@ -30,16 +30,10 @@ using namespace llvm;
enum { DEFAULT_ADDRSPACE = 0 }; enum { DEFAULT_ADDRSPACE = 0 };
// Mach-O section uniquing.
//
// FIXME: Figure out where this should live, it should be shared by
// TargetLoweringObjectFile.
typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out, AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
const MCAsmInfo &_MAI) const MCAsmInfo &_MAI)
: Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM), TargetParser(0), : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM), TargetParser(0),
CurBuffer(0), SectionUniquingMap(0) { CurBuffer(0) {
Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)); Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
// Debugging directives. // Debugging directives.
@ -51,39 +45,6 @@ AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
AsmParser::~AsmParser() { AsmParser::~AsmParser() {
// If we have the MachO uniquing map, free it.
delete (MachOUniqueMapTy*)SectionUniquingMap;
}
const MCSection *AsmParser::getMachOSection(const StringRef &Segment,
const StringRef &Section,
unsigned TypeAndAttributes,
unsigned Reserved2,
SectionKind Kind) const {
// We unique sections by their segment/section pair. The returned section
// may not have the same flags as the requested section, if so this should be
// diagnosed by the client as an error.
// Create the map if it doesn't already exist.
if (SectionUniquingMap == 0)
SectionUniquingMap = new MachOUniqueMapTy();
MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)SectionUniquingMap;
// Form the name to look up.
SmallString<64> Name;
Name += Segment;
Name.push_back(',');
Name += Section;
// Do the lookup, if we have a hit, return it.
const MCSectionMachO *&Entry = Map[Name.str()];
// FIXME: This should validate the type and attributes.
if (Entry) return Entry;
// Otherwise, return a new section.
return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
Reserved2, Kind, Ctx);
} }
void AsmParser::Warning(SMLoc L, const Twine &Msg) { void AsmParser::Warning(SMLoc L, const Twine &Msg) {
@ -143,7 +104,7 @@ bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
// //
// FIXME: Target hook & command line option for initial section. // FIXME: Target hook & command line option for initial section.
if (!NoInitialTextSection) if (!NoInitialTextSection)
Out.SwitchSection(getMachOSection("__TEXT", "__text", Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
0, SectionKind::getText())); 0, SectionKind::getText()));
@ -919,9 +880,9 @@ bool AsmParser::ParseDirectiveDarwinSection() {
// FIXME: Arch specific. // FIXME: Arch specific.
bool isText = Segment == "__TEXT"; // FIXME: Hack. bool isText = Segment == "__TEXT"; // FIXME: Hack.
Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize, Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
isText ? SectionKind::getText() isText ? SectionKind::getText()
: SectionKind::getDataRel())); : SectionKind::getDataRel()));
return false; return false;
} }
@ -936,9 +897,9 @@ bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
// FIXME: Arch specific. // FIXME: Arch specific.
bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack. bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize, Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
isText ? SectionKind::getText() isText ? SectionKind::getText()
: SectionKind::getDataRel())); : SectionKind::getDataRel()));
// Set the implicit alignment, if any. // Set the implicit alignment, if any.
// //
@ -1374,9 +1335,9 @@ bool AsmParser::ParseDirectiveComm(bool IsLocal) {
// '.lcomm' is equivalent to '.zerofill'. // '.lcomm' is equivalent to '.zerofill'.
// Create the Symbol as a common or local common with Size and Pow2Alignment // Create the Symbol as a common or local common with Size and Pow2Alignment
if (IsLocal) { if (IsLocal) {
Out.EmitZerofill(getMachOSection("__DATA", "__bss", Out.EmitZerofill(Ctx.getMachOSection("__DATA", "__bss",
MCSectionMachO::S_ZEROFILL, 0, MCSectionMachO::S_ZEROFILL, 0,
SectionKind::getBSS()), SectionKind::getBSS()),
Sym, Size, 1 << Pow2Alignment); Sym, Size, 1 << Pow2Alignment);
return false; return false;
} }
@ -1410,9 +1371,9 @@ bool AsmParser::ParseDirectiveDarwinZerofill() {
// the section but with no symbol. // the section but with no symbol.
if (Lexer.is(AsmToken::EndOfStatement)) { if (Lexer.is(AsmToken::EndOfStatement)) {
// Create the zerofill section but no symbol // Create the zerofill section but no symbol
Out.EmitZerofill(getMachOSection(Segment, Section, Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
MCSectionMachO::S_ZEROFILL, 0, MCSectionMachO::S_ZEROFILL, 0,
SectionKind::getBSS())); SectionKind::getBSS()));
return false; return false;
} }
@ -1468,9 +1429,9 @@ bool AsmParser::ParseDirectiveDarwinZerofill() {
// Create the zerofill Symbol with Size and Pow2Alignment // Create the zerofill Symbol with Size and Pow2Alignment
// //
// FIXME: Arch specific. // FIXME: Arch specific.
Out.EmitZerofill(getMachOSection(Segment, Section, Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
MCSectionMachO::S_ZEROFILL, 0, MCSectionMachO::S_ZEROFILL, 0,
SectionKind::getBSS()), SectionKind::getBSS()),
Sym, Size, 1 << Pow2Alignment); Sym, Size, 1 << Pow2Alignment);
return false; return false;