DwarfStringPool.cpp [plain text]
#include "DwarfStringPool.h"
#include "llvm/MC/MCStreamer.h"
using namespace llvm;
static std::pair<MCSymbol *, unsigned> &
getEntry(AsmPrinter &Asm,
StringMap<std::pair<MCSymbol *, unsigned>, BumpPtrAllocator &> &Pool,
StringRef Prefix, StringRef Str) {
std::pair<MCSymbol *, unsigned> &Entry = Pool[Str];
if (!Entry.first) {
Entry.second = Pool.size() - 1;
Entry.first = Asm.GetTempSymbol(Prefix, Entry.second);
}
return Entry;
}
MCSymbol *DwarfStringPool::getSymbol(AsmPrinter &Asm, StringRef Str) {
return getEntry(Asm, Pool, Prefix, Str).first;
}
unsigned DwarfStringPool::getIndex(AsmPrinter &Asm, StringRef Str) {
return getEntry(Asm, Pool, Prefix, Str).second;
}
void DwarfStringPool::emit(AsmPrinter &Asm, const MCSection *StrSection,
const MCSection *OffsetSection) {
if (Pool.empty())
return;
Asm.OutStreamer.SwitchSection(StrSection);
SmallVector<const StringMapEntry<std::pair<MCSymbol *, unsigned>> *, 64>
Entries(Pool.size());
for (const auto &E : Pool)
Entries[E.getValue().second] = &E;
for (const auto &Entry : Entries) {
Asm.OutStreamer.EmitLabel(Entry->getValue().first);
Asm.OutStreamer.EmitBytes(
StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
}
if (OffsetSection) {
Asm.OutStreamer.SwitchSection(OffsetSection);
unsigned offset = 0;
unsigned size = 4; for (const auto &Entry : Entries) {
Asm.OutStreamer.EmitIntValue(offset, size);
offset += Entry->getKeyLength() + 1;
}
}
}