Turn MCSectionData into a field of MCSection.

This also changes MCAssembler to store a vector of MCSections instead of an
iplist of MCSectionData.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@238159 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rafael Espindola
2015-05-25 23:14:17 +00:00
parent 5e435847b0
commit e86bc46939
8 changed files with 87 additions and 56 deletions

View File

@@ -12,6 +12,7 @@
#include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h" #include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallString.h"
#include "llvm/ADT/ilist.h" #include "llvm/ADT/ilist.h"
@@ -551,11 +552,11 @@ class MCAssembler {
friend class MCAsmLayout; friend class MCAsmLayout;
public: public:
typedef iplist<MCSectionData> SectionDataListType; typedef SetVector<MCSection *> SectionListType;
typedef std::vector<const MCSymbol *> SymbolDataListType; typedef std::vector<const MCSymbol *> SymbolDataListType;
typedef SectionDataListType::const_iterator const_iterator; typedef pointee_iterator<SectionListType::const_iterator> const_iterator;
typedef SectionDataListType::iterator iterator; typedef pointee_iterator<SectionListType::iterator> iterator;
typedef pointee_iterator<SymbolDataListType::const_iterator> typedef pointee_iterator<SymbolDataListType::const_iterator>
const_symbol_iterator; const_symbol_iterator;
@@ -599,17 +600,12 @@ private:
raw_ostream &OS; raw_ostream &OS;
iplist<MCSectionData> Sections; SectionListType Sections;
SymbolDataListType Symbols; SymbolDataListType Symbols;
DenseSet<const MCSymbol *> LocalsUsedInReloc; DenseSet<const MCSymbol *> LocalsUsedInReloc;
/// The map of sections to their associated assembler backend data.
//
// FIXME: Avoid this indirection?
DenseMap<const MCSection *, MCSectionData *> SectionMap;
std::vector<IndirectSymbolData> IndirectSymbols; std::vector<IndirectSymbolData> IndirectSymbols;
std::vector<DataRegionData> DataRegions; std::vector<DataRegionData> DataRegions;
@@ -888,24 +884,22 @@ public:
/// \name Backend Data Access /// \name Backend Data Access
/// @{ /// @{
MCSectionData &getSectionData(const MCSection &Section) const { MCSectionData &getSectionData(MCSection &Section) {
MCSectionData *Entry = SectionMap.lookup(&Section); assert(Sections.count(&Section) && "Unknown Seciton");
assert(Entry && "Missing section data!"); return Section.getSectionData();
return *Entry; }
const MCSectionData &getSectionData(const MCSection &Section) const {
return const_cast<MCAssembler *>(this)
->getSectionData(const_cast<MCSection &>(Section));
} }
MCSectionData &getOrCreateSectionData(MCSection &Section, MCSectionData &getOrCreateSectionData(MCSection &Section,
bool *Created = nullptr) { bool *Created = nullptr) {
MCSectionData *&Entry = SectionMap[&Section]; bool C = Sections.insert(&Section);
if (Created) if (Created)
*Created = !Entry; *Created = C;
if (!Entry) { return Section.getSectionData();
Entry = new MCSectionData(Section);
Sections.push_back(Entry);
}
return *Entry;
} }
bool hasSymbolData(const MCSymbol &Symbol) const { return Symbol.hasData(); } bool hasSymbolData(const MCSymbol &Symbol) const { return Symbol.hasData(); }

View File

@@ -31,7 +31,7 @@ class MCSection;
class MCSymbol; class MCSymbol;
class raw_ostream; class raw_ostream;
class MCSectionData : public ilist_node<MCSectionData> { class MCSectionData {
friend class MCAsmLayout; friend class MCAsmLayout;
MCSectionData(const MCSectionData &) = delete; MCSectionData(const MCSectionData &) = delete;
@@ -62,9 +62,7 @@ private:
/// @} /// @}
public: public:
// Only for use as sentinel. explicit MCSectionData(MCSection &Section);
MCSectionData();
MCSectionData(MCSection &Section);
MCSection &getSection() const { return *Section; } MCSection &getSection() const { return *Section; }
@@ -144,9 +142,10 @@ private:
/// Whether this section has had instructions emitted into it. /// Whether this section has had instructions emitted into it.
unsigned HasInstructions : 1; unsigned HasInstructions : 1;
MCSectionData Data;
protected: protected:
MCSection(SectionVariant V, SectionKind K, MCSymbol *Begin) MCSection(SectionVariant V, SectionKind K, MCSymbol *Begin);
: Begin(Begin), HasInstructions(false), Variant(V), Kind(K) {}
SectionVariant Variant; SectionVariant Variant;
SectionKind Kind; SectionKind Kind;
@@ -191,6 +190,31 @@ public:
bool hasInstructions() const { return HasInstructions; } bool hasInstructions() const { return HasInstructions; }
void setHasInstructions(bool Value) { HasInstructions = Value; } void setHasInstructions(bool Value) { HasInstructions = Value; }
MCSectionData &getSectionData() { return Data; }
const MCSectionData &getSectionData() const {
return const_cast<MCSection *>(this)->getSectionData();
}
MCSectionData::FragmentListType &getFragmentList();
const MCSectionData::FragmentListType &getFragmentList() const {
return const_cast<MCSection *>(this)->getFragmentList();
}
MCSectionData::iterator begin();
MCSectionData::const_iterator begin() const {
return const_cast<MCSection *>(this)->begin();
}
MCSectionData::iterator end();
MCSectionData::const_iterator end() const {
return const_cast<MCSection *>(this)->end();
}
MCSectionData::reverse_iterator rend();
MCSectionData::const_reverse_iterator rend() const {
return const_cast<MCSection *>(this)->rend();
}
virtual void PrintSwitchToSection(const MCAsmInfo &MAI, raw_ostream &OS, virtual void PrintSwitchToSection(const MCAsmInfo &MAI, raw_ostream &OS,
const MCExpr *Subsection) const = 0; const MCExpr *Subsection) const = 0;

View File

@@ -1348,8 +1348,9 @@ void ELFObjectWriter::WriteObject(MCAssembler &Asm,
SectionOffsetsTy SectionOffsets; SectionOffsetsTy SectionOffsets;
std::vector<MCSectionELF *> Groups; std::vector<MCSectionELF *> Groups;
std::vector<MCSectionELF *> Relocations; std::vector<MCSectionELF *> Relocations;
for (const MCSectionData &SD : Asm) { for (const MCSection &Sec : Asm) {
const MCSectionELF &Section = static_cast<MCSectionELF &>(SD.getSection()); const MCSectionELF &Section = static_cast<const MCSectionELF &>(Sec);
const MCSectionData &SD = Section.getSectionData();
uint64_t Padding = OffsetToAlignment(OS.tell(), Section.getAlignment()); uint64_t Padding = OffsetToAlignment(OS.tell(), Section.getAlignment());
WriteZeros(Padding); WriteZeros(Padding);

View File

@@ -69,11 +69,11 @@ MCAsmLayout::MCAsmLayout(MCAssembler &Asm)
{ {
// Compute the section layout order. Virtual sections must go last. // Compute the section layout order. Virtual sections must go last.
for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it) for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
if (!it->getSection().isVirtualSection()) if (!it->isVirtualSection())
SectionOrder.push_back(&*it); SectionOrder.push_back(&it->getSectionData());
for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it) for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
if (it->getSection().isVirtualSection()) if (it->isVirtualSection())
SectionOrder.push_back(&*it); SectionOrder.push_back(&it->getSectionData());
} }
bool MCAsmLayout::isFragmentValid(const MCFragment *F) const { bool MCAsmLayout::isFragmentValid(const MCFragment *F) const {
@@ -290,8 +290,6 @@ MCEncodedFragmentWithFixups::~MCEncodedFragmentWithFixups() {
/* *** */ /* *** */
MCSectionData::MCSectionData() : Section(nullptr) {}
MCSectionData::MCSectionData(MCSection &Section) : Section(&Section) {} MCSectionData::MCSectionData(MCSection &Section) : Section(&Section) {}
MCSectionData::iterator MCSectionData::iterator
@@ -342,7 +340,6 @@ MCAssembler::~MCAssembler() {
void MCAssembler::reset() { void MCAssembler::reset() {
Sections.clear(); Sections.clear();
Symbols.clear(); Symbols.clear();
SectionMap.clear();
IndirectSymbols.clear(); IndirectSymbols.clear();
DataRegions.clear(); DataRegions.clear();
LinkerOptions.clear(); LinkerOptions.clear();
@@ -862,9 +859,9 @@ void MCAssembler::Finish() {
// Create dummy fragments to eliminate any empty sections, this simplifies // Create dummy fragments to eliminate any empty sections, this simplifies
// layout. // layout.
if (it->getFragmentList().empty()) if (it->getFragmentList().empty())
new MCDataFragment(it); new MCDataFragment(&it->getSectionData());
it->getSection().setOrdinal(SectionIndex++); it->setOrdinal(SectionIndex++);
} }
// Assign layout order indices to sections and fragments. // Assign layout order indices to sections and fragments.
@@ -1084,8 +1081,8 @@ bool MCAssembler::layoutOnce(MCAsmLayout &Layout) {
bool WasRelaxed = false; bool WasRelaxed = false;
for (iterator it = begin(), ie = end(); it != ie; ++it) { for (iterator it = begin(), ie = end(); it != ie; ++it) {
MCSectionData &SD = *it; MCSection &Sec = *it;
while (layoutSectionOnce(Layout, SD)) while (layoutSectionOnce(Layout, Sec.getSectionData()))
WasRelaxed = true; WasRelaxed = true;
} }
@@ -1261,7 +1258,7 @@ void MCAssembler::dump() {
OS << " Sections:[\n "; OS << " Sections:[\n ";
for (iterator it = begin(), ie = end(); it != ie; ++it) { for (iterator it = begin(), ie = end(); it != ie; ++it) {
if (it != begin()) OS << ",\n "; if (it != begin()) OS << ",\n ";
it->dump(); it->getSectionData().dump();
} }
OS << "],\n"; OS << "],\n";
OS << " Symbols:["; OS << " Symbols:[";

View File

@@ -19,6 +19,9 @@ using namespace llvm;
// MCSection // MCSection
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
MCSection::MCSection(SectionVariant V, SectionKind K, MCSymbol *Begin)
: Begin(Begin), HasInstructions(false), Data(*this), Variant(V), Kind(K) {}
MCSymbol *MCSection::getEndSymbol(MCContext &Ctx) { MCSymbol *MCSection::getEndSymbol(MCContext &Ctx) {
if (!End) if (!End)
End = Ctx.createTempSymbol("sec_end", true); End = Ctx.createTempSymbol("sec_end", true);
@@ -49,6 +52,14 @@ void MCSection::setBundleLockState(BundleLockStateType NewState) {
++BundleLockNestingDepth; ++BundleLockNestingDepth;
} }
MCSectionData::iterator MCSection::begin() { return Data.begin(); }
MCSectionData::iterator MCSection::end() { return Data.end(); }
MCSectionData::FragmentListType &MCSection::getFragmentList() {
return Data.getFragmentList();
}
MCSectionData::iterator MCSectionData::begin() { return Fragments.begin(); } MCSectionData::iterator MCSectionData::begin() { return Fragments.begin(); }
MCSectionData::iterator MCSectionData::end() { return Fragments.end(); } MCSectionData::iterator MCSectionData::end() { return Fragments.end(); }

View File

@@ -540,7 +540,7 @@ void MachObjectWriter::ComputeSymbolTable(
unsigned Index = 1; unsigned Index = 1;
for (MCAssembler::iterator it = Asm.begin(), for (MCAssembler::iterator it = Asm.begin(),
ie = Asm.end(); it != ie; ++it, ++Index) ie = Asm.end(); it != ie; ++it, ++Index)
SectionIndexMap[&it->getSection()] = Index; SectionIndexMap[&*it] = Index;
assert(Index <= 256 && "Too many sections!"); assert(Index <= 256 && "Too many sections!");
// Build the string table. // Build the string table.
@@ -622,7 +622,8 @@ void MachObjectWriter::ComputeSymbolTable(
for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
UndefinedSymbolData[i].Symbol->setIndex(Index++); UndefinedSymbolData[i].Symbol->setIndex(Index++);
for (const MCSectionData &SD : Asm) { for (const MCSection &Section : Asm) {
const MCSectionData &SD = Section.getSectionData();
std::vector<RelAndSymbol> &Relocs = Relocations[&SD]; std::vector<RelAndSymbol> &Relocs = Relocations[&SD];
for (RelAndSymbol &Rel : Relocs) { for (RelAndSymbol &Rel : Relocs) {
if (!Rel.Sym) if (!Rel.Sym)
@@ -801,7 +802,7 @@ void MachObjectWriter::WriteObject(MCAssembler &Asm,
uint64_t VMSize = 0; uint64_t VMSize = 0;
for (MCAssembler::const_iterator it = Asm.begin(), for (MCAssembler::const_iterator it = Asm.begin(),
ie = Asm.end(); it != ie; ++it) { ie = Asm.end(); it != ie; ++it) {
const MCSectionData &SD = *it; const MCSectionData &SD = it->getSectionData();
uint64_t Address = getSectionAddress(&SD); uint64_t Address = getSectionAddress(&SD);
uint64_t Size = Layout.getSectionAddressSize(&SD); uint64_t Size = Layout.getSectionAddressSize(&SD);
uint64_t FileSize = Layout.getSectionFileSize(&SD); uint64_t FileSize = Layout.getSectionFileSize(&SD);
@@ -832,10 +833,11 @@ void MachObjectWriter::WriteObject(MCAssembler &Asm,
uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize; uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize;
for (MCAssembler::const_iterator it = Asm.begin(), for (MCAssembler::const_iterator it = Asm.begin(),
ie = Asm.end(); it != ie; ++it) { ie = Asm.end(); it != ie; ++it) {
std::vector<RelAndSymbol> &Relocs = Relocations[it]; const MCSectionData &SD = it->getSectionData();
std::vector<RelAndSymbol> &Relocs = Relocations[&SD];
unsigned NumRelocs = Relocs.size(); unsigned NumRelocs = Relocs.size();
uint64_t SectionStart = SectionDataStart + getSectionAddress(it); uint64_t SectionStart = SectionDataStart + getSectionAddress(&SD);
WriteSection(Asm, Layout, *it, SectionStart, RelocTableEnd, NumRelocs); WriteSection(Asm, Layout, SD, SectionStart, RelocTableEnd, NumRelocs);
RelocTableEnd += NumRelocs * sizeof(MachO::any_relocation_info); RelocTableEnd += NumRelocs * sizeof(MachO::any_relocation_info);
} }
@@ -911,9 +913,10 @@ void MachObjectWriter::WriteObject(MCAssembler &Asm,
// Write the actual section data. // Write the actual section data.
for (MCAssembler::const_iterator it = Asm.begin(), for (MCAssembler::const_iterator it = Asm.begin(),
ie = Asm.end(); it != ie; ++it) { ie = Asm.end(); it != ie; ++it) {
Asm.writeSectionData(it, Layout); const MCSectionData &SD = it->getSectionData();
Asm.writeSectionData(&SD, Layout);
uint64_t Pad = getPaddingSize(it, Layout); uint64_t Pad = getPaddingSize(&SD, Layout);
WriteZeros(Pad); WriteZeros(Pad);
} }
@@ -925,7 +928,7 @@ void MachObjectWriter::WriteObject(MCAssembler &Asm,
ie = Asm.end(); it != ie; ++it) { ie = Asm.end(); it != ie; ++it) {
// Write the section relocation entries, in reverse order to match 'as' // Write the section relocation entries, in reverse order to match 'as'
// (approximately, the exact algorithm is more complicated than this). // (approximately, the exact algorithm is more complicated than this).
std::vector<RelAndSymbol> &Relocs = Relocations[it]; std::vector<RelAndSymbol> &Relocs = Relocations[&it->getSectionData()];
for (unsigned i = 0, e = Relocs.size(); i != e; ++i) { for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
Write32(Relocs[e - i - 1].MRE.r_word0); Write32(Relocs[e - i - 1].MRE.r_word0);
Write32(Relocs[e - i - 1].MRE.r_word1); Write32(Relocs[e - i - 1].MRE.r_word1);

View File

@@ -636,7 +636,7 @@ void WinCOFFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
// "Define" each section & symbol. This creates section & symbol // "Define" each section & symbol. This creates section & symbol
// entries in the staging area. // entries in the staging area.
for (const auto &Section : Asm) for (const auto &Section : Asm)
DefineSection(Section); DefineSection(Section.getSectionData());
for (const MCSymbol &Symbol : Asm.symbols()) for (const MCSymbol &Symbol : Asm.symbols())
if (ExportSymbol(Symbol, Asm)) if (ExportSymbol(Symbol, Asm))
@@ -946,12 +946,13 @@ void WinCOFFObjectWriter::WriteObject(MCAssembler &Asm,
offset += COFF::SectionSize * Header.NumberOfSections; offset += COFF::SectionSize * Header.NumberOfSections;
for (const auto &Section : Asm) { for (const auto &Section : Asm) {
COFFSection *Sec = SectionMap[&Section.getSection()]; COFFSection *Sec = SectionMap[&Section];
if (Sec->Number == -1) if (Sec->Number == -1)
continue; continue;
Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section); Sec->Header.SizeOfRawData =
Layout.getSectionAddressSize(&Section.getSectionData());
if (IsPhysicalSection(Sec)) { if (IsPhysicalSection(Sec)) {
// Align the section data to a four byte boundary. // Align the section data to a four byte boundary.
@@ -1035,7 +1036,7 @@ void WinCOFFObjectWriter::WriteObject(MCAssembler &Asm,
WriteZeros(SectionDataPadding); WriteZeros(SectionDataPadding);
Asm.writeSectionData(j, Layout); Asm.writeSectionData(&j->getSectionData(), Layout);
} }
if ((*i)->Relocations.size() > 0) { if ((*i)->Relocations.size() > 0) {

View File

@@ -67,7 +67,7 @@ public:
void AMDGPUMCObjectWriter::WriteObject(MCAssembler &Asm, void AMDGPUMCObjectWriter::WriteObject(MCAssembler &Asm,
const MCAsmLayout &Layout) { const MCAsmLayout &Layout) {
for (MCAssembler::iterator I = Asm.begin(), E = Asm.end(); I != E; ++I) { for (MCAssembler::iterator I = Asm.begin(), E = Asm.end(); I != E; ++I) {
Asm.writeSectionData(I, Layout); Asm.writeSectionData(&I->getSectionData(), Layout);
} }
} }