Encapsulate the DWARF string pool in a separate type.

Pulls out some more code from some of the rather monolithic DWARF
classes. Unlike the address table, the string table won't move up into
DwarfDebug - each DWARF file has its own string table (but there can be
only one address table).

llvm-svn: 207277
This commit is contained in:
David Blaikie 2014-04-25 21:34:35 +00:00
parent 001ecd9aa9
commit daefdbf3ad
8 changed files with 146 additions and 92 deletions

View File

@ -11,6 +11,7 @@ add_llvm_library(LLVMAsmPrinter
DwarfDebug.cpp
DwarfException.cpp
DwarfFile.cpp
DwarfStringPool.cpp
DwarfUnit.cpp
ErlangGCPrinter.cpp
OcamlGCPrinter.cpp

View File

@ -179,8 +179,8 @@ void DwarfAccelTable::EmitData(AsmPrinter *Asm, DwarfFile *D) {
// Remember to emit the label for our offset.
Asm->OutStreamer.EmitLabel((*HI)->Sym);
Asm->OutStreamer.AddComment((*HI)->Str);
Asm->EmitSectionOffset(D->getStringPoolEntry((*HI)->Str),
D->getStringPoolSym());
Asm->EmitSectionOffset(D->getStringPool().getSymbol(*Asm, (*HI)->Str),
D->getStringPool().getSectionSymbol());
Asm->OutStreamer.AddComment("Num DIEs");
Asm->EmitInt32((*HI)->Data.size());
for (ArrayRef<HashDataContents *>::const_iterator

View File

@ -2545,27 +2545,27 @@ void DwarfDebug::attachLowHighPC(DwarfCompileUnit &Unit, DIE &D,
void DwarfDebug::addAccelName(StringRef Name, const DIE &Die) {
if (!useDwarfAccelTables())
return;
InfoHolder.getStringPoolEntry(Name);
InfoHolder.getStringPool().getSymbol(*Asm, Name);
AccelNames.AddName(Name, &Die);
}
void DwarfDebug::addAccelObjC(StringRef Name, const DIE &Die) {
if (!useDwarfAccelTables())
return;
InfoHolder.getStringPoolEntry(Name);
InfoHolder.getStringPool().getSymbol(*Asm, Name);
AccelObjC.AddName(Name, &Die);
}
void DwarfDebug::addAccelNamespace(StringRef Name, const DIE &Die) {
if (!useDwarfAccelTables())
return;
InfoHolder.getStringPoolEntry(Name);
InfoHolder.getStringPool().getSymbol(*Asm, Name);
AccelNamespace.AddName(Name, &Die);
}
void DwarfDebug::addAccelType(StringRef Name, const DIE &Die, char Flags) {
if (!useDwarfAccelTables())
return;
InfoHolder.getStringPoolEntry(Name);
InfoHolder.getStringPool().getSymbol(*Asm, Name);
AccelTypes.AddName(Name, &Die, Flags);
}

View File

@ -18,36 +18,11 @@
#include "llvm/Target/TargetLoweringObjectFile.h"
namespace llvm {
DwarfFile::DwarfFile(AsmPrinter *AP, const char *Pref, BumpPtrAllocator &DA)
: Asm(AP), StringPool(DA), NextStringPoolNumber(0), StringPref(Pref) {}
DwarfFile::DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA)
: Asm(AP), StrPool(DA, *Asm, Pref) {}
DwarfFile::~DwarfFile() {}
MCSymbol *DwarfFile::getStringPoolSym() {
return Asm->GetTempSymbol(StringPref);
}
MCSymbol *DwarfFile::getStringPoolEntry(StringRef Str) {
std::pair<MCSymbol *, unsigned> &Entry =
StringPool.GetOrCreateValue(Str).getValue();
if (Entry.first)
return Entry.first;
Entry.second = NextStringPoolNumber++;
return Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
}
unsigned DwarfFile::getStringPoolIndex(StringRef Str) {
std::pair<MCSymbol *, unsigned> &Entry =
StringPool.GetOrCreateValue(Str).getValue();
if (Entry.first)
return Entry.second;
Entry.second = NextStringPoolNumber++;
Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
return Entry.second;
}
// Define a unique number for the abbreviation.
//
void DwarfFile::assignAbbrevNumber(DIEAbbrev &Abbrev) {
@ -176,40 +151,6 @@ void DwarfFile::emitAbbrevs(const MCSection *Section) {
void DwarfFile::emitStrings(const MCSection *StrSection,
const MCSection *OffsetSection,
const MCSymbol *StrSecSym) {
if (StringPool.empty())
return;
// Start the dwarf str section.
Asm->OutStreamer.SwitchSection(StrSection);
// Get all of the string pool entries and put them in an array by their ID so
// we can sort them.
SmallVector<std::pair<unsigned, const StrPool::value_type *>, 64> Entries;
for (const auto &I : StringPool)
Entries.push_back(std::make_pair(I.second.second, &I));
array_pod_sort(Entries.begin(), Entries.end());
for (const auto &Entry : Entries) {
// Emit a label for reference from debug information entries.
Asm->OutStreamer.EmitLabel(Entry.second->getValue().first);
// Emit the string itself with a terminating null byte.
Asm->OutStreamer.EmitBytes(StringRef(Entry.second->getKeyData(),
Entry.second->getKeyLength() + 1));
}
// If we've got an offset section go ahead and emit that now as well.
if (OffsetSection) {
Asm->OutStreamer.SwitchSection(OffsetSection);
unsigned offset = 0;
unsigned size = 4; // FIXME: DWARF64 is 8.
for (const auto &Entry : Entries) {
Asm->OutStreamer.EmitIntValue(offset, size);
offset += Entry.second->getKeyLength() + 1;
}
}
StrPool.emit(*Asm, StrSection, OffsetSection, StrSecSym);
}
}

View File

@ -16,6 +16,7 @@
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/Allocator.h"
#include "AddressPool.h"
#include "DwarfStringPool.h"
#include <vector>
#include <string>
@ -43,17 +44,10 @@ class DwarfFile {
// A pointer to all units in the section.
SmallVector<std::unique_ptr<DwarfUnit>, 1> CUs;
// Collection of strings for this unit and assorted symbols.
// A String->Symbol mapping of strings used by indirect
// references.
typedef StringMap<std::pair<MCSymbol *, unsigned>, BumpPtrAllocator &>
StrPool;
StrPool StringPool;
unsigned NextStringPoolNumber;
std::string StringPref;
DwarfStringPool StrPool;
public:
DwarfFile(AsmPrinter *AP, const char *Pref, BumpPtrAllocator &DA);
DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA);
~DwarfFile();
@ -83,19 +77,8 @@ public:
const MCSection *OffsetSection = nullptr,
const MCSymbol *StrSecSym = nullptr);
/// \brief Returns the entry into the start of the pool.
MCSymbol *getStringPoolSym();
/// \brief Returns an entry into the string pool with the given
/// string text.
MCSymbol *getStringPoolEntry(StringRef Str);
/// \brief Returns the index into the string pool with the given
/// string text.
unsigned getStringPoolIndex(StringRef Str);
/// \brief Returns the string pool.
StrPool *getStringPool() { return &StringPool; }
DwarfStringPool &getStringPool() { return StrPool; }
};
}
#endif

View File

@ -0,0 +1,74 @@
//===-- llvm/CodeGen/DwarfStringPool.cpp - Dwarf Debug Framework ----------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "DwarfStringPool.h"
#include "llvm/MC/MCStreamer.h"
using namespace llvm;
MCSymbol *DwarfStringPool::getSectionSymbol() { return SectionSymbol; }
std::pair<MCSymbol *, unsigned> &
getEntry(AsmPrinter &Asm,
StringMap<std::pair<MCSymbol *, unsigned>, BumpPtrAllocator &> &Pool,
StringRef Prefix, StringRef Str) {
std::pair<MCSymbol *, unsigned> &Entry =
Pool.GetOrCreateValue(Str).getValue();
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,
const MCSymbol *StrSecSym) {
if (Pool.empty())
return;
// Start the dwarf str section.
Asm.OutStreamer.SwitchSection(StrSection);
// Get all of the string pool entries and put them in an array by their ID so
// we can sort them.
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) {
// Emit a label for reference from debug information entries.
Asm.OutStreamer.EmitLabel(Entry->getValue().first);
// Emit the string itself with a terminating null byte.
Asm.OutStreamer.EmitBytes(
StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
}
// If we've got an offset section go ahead and emit that now as well.
if (OffsetSection) {
Asm.OutStreamer.SwitchSection(OffsetSection);
unsigned offset = 0;
unsigned size = 4; // FIXME: DWARF64 is 8.
for (const auto &Entry : Entries) {
Asm.OutStreamer.EmitIntValue(offset, size);
offset += Entry->getKeyLength() + 1;
}
}
}

View File

@ -0,0 +1,55 @@
//===-- llvm/CodeGen/DwarfStringPool.h - Dwarf Debug Framework -*- C++ -*--===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef CODEGEN_ASMPRINTER_STRINGPOOL_H__
#define CODEGEN_ASMPRINTER_STRINGPOOL_H__
#include "llvm/ADT/StringMap.h"
#include "llvm/CodeGen/AsmPrinter.h"
#include "llvm/Support/Allocator.h"
#include <utility>
namespace llvm {
class MCSymbol;
class MCSection;
class StringRef;
// Collection of strings for this unit and assorted symbols.
// A String->Symbol mapping of strings used by indirect
// references.
class DwarfStringPool {
StringMap<std::pair<MCSymbol *, unsigned>, BumpPtrAllocator &> Pool;
StringRef Prefix;
MCSymbol *SectionSymbol;
public:
DwarfStringPool(BumpPtrAllocator &A, AsmPrinter &Asm, StringRef Prefix)
: Pool(A), Prefix(Prefix), SectionSymbol(Asm.GetTempSymbol(Prefix)) {}
void emit(AsmPrinter &Asm, const MCSection *StrSection,
const MCSection *OffsetSection = nullptr,
const MCSymbol *StrSecSym = nullptr);
/// \brief Returns the entry into the start of the pool.
MCSymbol *getSectionSymbol();
/// \brief Returns an entry into the string pool with the given
/// string text.
MCSymbol *getSymbol(AsmPrinter &Asm, StringRef Str);
/// \brief Returns the index into the string pool with the given
/// string text.
unsigned getIndex(AsmPrinter &Asm, StringRef Str);
bool empty() const { return Pool.empty(); }
};
}
#endif

View File

@ -208,7 +208,7 @@ void DwarfUnit::addString(DIE &Die, dwarf::Attribute Attribute,
if (!DD->useSplitDwarf())
return addLocalString(Die, Attribute, String);
unsigned idx = DU->getStringPoolIndex(String);
unsigned idx = DU->getStringPool().getIndex(*Asm, String);
DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
Die.addValue(Attribute, dwarf::DW_FORM_GNU_str_index, Str);
@ -218,12 +218,12 @@ void DwarfUnit::addString(DIE &Die, dwarf::Attribute Attribute,
/// to be in the local string pool instead of indirected.
void DwarfUnit::addLocalString(DIE &Die, dwarf::Attribute Attribute,
StringRef String) {
MCSymbol *Symb = DU->getStringPoolEntry(String);
MCSymbol *Symb = DU->getStringPool().getSymbol(*Asm, String);
DIEValue *Value;
if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Value = new (DIEValueAllocator) DIELabel(Symb);
else {
MCSymbol *StringPool = DU->getStringPoolSym();
MCSymbol *StringPool = DU->getStringPool().getSectionSymbol();
Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
}
DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);