#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
+#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/ilist.h"
friend class MCAsmLayout;
public:
- typedef iplist<MCSectionData> SectionDataListType;
+ typedef SetVector<MCSection *> SectionListType;
typedef std::vector<const MCSymbol *> SymbolDataListType;
- typedef SectionDataListType::const_iterator const_iterator;
- typedef SectionDataListType::iterator iterator;
+ typedef pointee_iterator<SectionListType::const_iterator> const_iterator;
+ typedef pointee_iterator<SectionListType::iterator> iterator;
typedef pointee_iterator<SymbolDataListType::const_iterator>
const_symbol_iterator;
raw_ostream &OS;
- iplist<MCSectionData> Sections;
+ SectionListType Sections;
SymbolDataListType Symbols;
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<DataRegionData> DataRegions;
/// \name Backend Data Access
/// @{
- MCSectionData &getSectionData(const MCSection &Section) const {
- MCSectionData *Entry = SectionMap.lookup(&Section);
- assert(Entry && "Missing section data!");
- return *Entry;
+ MCSectionData &getSectionData(MCSection &Section) {
+ assert(Sections.count(&Section) && "Unknown Seciton");
+ return Section.getSectionData();
+ }
+
+ const MCSectionData &getSectionData(const MCSection &Section) const {
+ return const_cast<MCAssembler *>(this)
+ ->getSectionData(const_cast<MCSection &>(Section));
}
MCSectionData &getOrCreateSectionData(MCSection &Section,
bool *Created = nullptr) {
- MCSectionData *&Entry = SectionMap[&Section];
-
+ bool C = Sections.insert(&Section);
if (Created)
- *Created = !Entry;
- if (!Entry) {
- Entry = new MCSectionData(Section);
- Sections.push_back(Entry);
- }
-
- return *Entry;
+ *Created = C;
+ return Section.getSectionData();
}
bool hasSymbolData(const MCSymbol &Symbol) const { return Symbol.hasData(); }
class MCSymbol;
class raw_ostream;
-class MCSectionData : public ilist_node<MCSectionData> {
+class MCSectionData {
friend class MCAsmLayout;
MCSectionData(const MCSectionData &) = delete;
/// @}
public:
- // Only for use as sentinel.
- MCSectionData();
- MCSectionData(MCSection &Section);
+ explicit MCSectionData(MCSection &Section);
MCSection &getSection() const { return *Section; }
/// Whether this section has had instructions emitted into it.
unsigned HasInstructions : 1;
+ MCSectionData Data;
+
protected:
- MCSection(SectionVariant V, SectionKind K, MCSymbol *Begin)
- : Begin(Begin), HasInstructions(false), Variant(V), Kind(K) {}
+ MCSection(SectionVariant V, SectionKind K, MCSymbol *Begin);
SectionVariant Variant;
SectionKind Kind;
bool hasInstructions() const { return HasInstructions; }
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,
const MCExpr *Subsection) const = 0;
SectionOffsetsTy SectionOffsets;
std::vector<MCSectionELF *> Groups;
std::vector<MCSectionELF *> Relocations;
- for (const MCSectionData &SD : Asm) {
- const MCSectionELF &Section = static_cast<MCSectionELF &>(SD.getSection());
+ for (const MCSection &Sec : Asm) {
+ const MCSectionELF &Section = static_cast<const MCSectionELF &>(Sec);
+ const MCSectionData &SD = Section.getSectionData();
uint64_t Padding = OffsetToAlignment(OS.tell(), Section.getAlignment());
WriteZeros(Padding);
{
// Compute the section layout order. Virtual sections must go last.
for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
- if (!it->getSection().isVirtualSection())
- SectionOrder.push_back(&*it);
+ if (!it->isVirtualSection())
+ SectionOrder.push_back(&it->getSectionData());
for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
- if (it->getSection().isVirtualSection())
- SectionOrder.push_back(&*it);
+ if (it->isVirtualSection())
+ SectionOrder.push_back(&it->getSectionData());
}
bool MCAsmLayout::isFragmentValid(const MCFragment *F) const {
/* *** */
-MCSectionData::MCSectionData() : Section(nullptr) {}
-
MCSectionData::MCSectionData(MCSection &Section) : Section(&Section) {}
MCSectionData::iterator
void MCAssembler::reset() {
Sections.clear();
Symbols.clear();
- SectionMap.clear();
IndirectSymbols.clear();
DataRegions.clear();
LinkerOptions.clear();
// Create dummy fragments to eliminate any empty sections, this simplifies
// layout.
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.
bool WasRelaxed = false;
for (iterator it = begin(), ie = end(); it != ie; ++it) {
- MCSectionData &SD = *it;
- while (layoutSectionOnce(Layout, SD))
+ MCSection &Sec = *it;
+ while (layoutSectionOnce(Layout, Sec.getSectionData()))
WasRelaxed = true;
}
OS << " Sections:[\n ";
for (iterator it = begin(), ie = end(); it != ie; ++it) {
if (it != begin()) OS << ",\n ";
- it->dump();
+ it->getSectionData().dump();
}
OS << "],\n";
OS << " Symbols:[";
// MCSection
//===----------------------------------------------------------------------===//
+MCSection::MCSection(SectionVariant V, SectionKind K, MCSymbol *Begin)
+ : Begin(Begin), HasInstructions(false), Data(*this), Variant(V), Kind(K) {}
+
MCSymbol *MCSection::getEndSymbol(MCContext &Ctx) {
if (!End)
End = Ctx.createTempSymbol("sec_end", true);
++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::end() { return Fragments.end(); }
unsigned Index = 1;
for (MCAssembler::iterator it = Asm.begin(),
ie = Asm.end(); it != ie; ++it, ++Index)
- SectionIndexMap[&it->getSection()] = Index;
+ SectionIndexMap[&*it] = Index;
assert(Index <= 256 && "Too many sections!");
// Build the string table.
for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
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];
for (RelAndSymbol &Rel : Relocs) {
if (!Rel.Sym)
uint64_t VMSize = 0;
for (MCAssembler::const_iterator it = Asm.begin(),
ie = Asm.end(); it != ie; ++it) {
- const MCSectionData &SD = *it;
+ const MCSectionData &SD = it->getSectionData();
uint64_t Address = getSectionAddress(&SD);
uint64_t Size = Layout.getSectionAddressSize(&SD);
uint64_t FileSize = Layout.getSectionFileSize(&SD);
uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize;
for (MCAssembler::const_iterator it = Asm.begin(),
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();
- uint64_t SectionStart = SectionDataStart + getSectionAddress(it);
- WriteSection(Asm, Layout, *it, SectionStart, RelocTableEnd, NumRelocs);
+ uint64_t SectionStart = SectionDataStart + getSectionAddress(&SD);
+ WriteSection(Asm, Layout, SD, SectionStart, RelocTableEnd, NumRelocs);
RelocTableEnd += NumRelocs * sizeof(MachO::any_relocation_info);
}
// Write the actual section data.
for (MCAssembler::const_iterator it = Asm.begin(),
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);
}
ie = Asm.end(); it != ie; ++it) {
// Write the section relocation entries, in reverse order to match 'as'
// (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) {
Write32(Relocs[e - i - 1].MRE.r_word0);
Write32(Relocs[e - i - 1].MRE.r_word1);
// "Define" each section & symbol. This creates section & symbol
// entries in the staging area.
for (const auto &Section : Asm)
- DefineSection(Section);
+ DefineSection(Section.getSectionData());
for (const MCSymbol &Symbol : Asm.symbols())
if (ExportSymbol(Symbol, Asm))
offset += COFF::SectionSize * Header.NumberOfSections;
for (const auto &Section : Asm) {
- COFFSection *Sec = SectionMap[&Section.getSection()];
+ COFFSection *Sec = SectionMap[&Section];
if (Sec->Number == -1)
continue;
- Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
+ Sec->Header.SizeOfRawData =
+ Layout.getSectionAddressSize(&Section.getSectionData());
if (IsPhysicalSection(Sec)) {
// Align the section data to a four byte boundary.
WriteZeros(SectionDataPadding);
- Asm.writeSectionData(j, Layout);
+ Asm.writeSectionData(&j->getSectionData(), Layout);
}
if ((*i)->Relocations.size() > 0) {
void AMDGPUMCObjectWriter::WriteObject(MCAssembler &Asm,
const MCAsmLayout &Layout) {
for (MCAssembler::iterator I = Asm.begin(), E = Asm.end(); I != E; ++I) {
- Asm.writeSectionData(I, Layout);
+ Asm.writeSectionData(&I->getSectionData(), Layout);
}
}