2014-04-26 05:34:35 +08:00
|
|
|
//===-- 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"
|
2015-05-25 00:54:59 +08:00
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
2014-04-26 05:34:35 +08:00
|
|
|
#include "llvm/MC/MCStreamer.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2015-05-25 00:33:33 +08:00
|
|
|
DwarfStringPool::EntryRef DwarfStringPool::getEntry(AsmPrinter &Asm,
|
2015-05-25 00:14:59 +08:00
|
|
|
StringRef Str) {
|
2015-05-25 00:33:33 +08:00
|
|
|
auto I = Pool.insert(std::make_pair(Str, EntryTy()));
|
|
|
|
if (I.second) {
|
|
|
|
auto &Entry = I.first->second;
|
2015-05-25 00:14:59 +08:00
|
|
|
Entry.Index = Pool.size() - 1;
|
|
|
|
Entry.Offset = NumBytes;
|
|
|
|
Entry.Symbol = Asm.createTempSymbol(Prefix);
|
|
|
|
|
|
|
|
NumBytes += Str.size() + 1;
|
|
|
|
assert(NumBytes > Entry.Offset && "Unexpected overflow");
|
2014-04-26 05:34:35 +08:00
|
|
|
}
|
2015-05-25 00:33:33 +08:00
|
|
|
return EntryRef(*I.first);
|
2014-04-26 05:34:35 +08:00
|
|
|
}
|
|
|
|
|
2015-05-22 03:20:38 +08:00
|
|
|
void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection,
|
|
|
|
MCSection *OffsetSection) {
|
2014-04-26 05:34:35 +08:00
|
|
|
if (Pool.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Start the dwarf str section.
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm.OutStreamer->SwitchSection(StrSection);
|
2014-04-26 05:34:35 +08:00
|
|
|
|
|
|
|
// Get all of the string pool entries and put them in an array by their ID so
|
|
|
|
// we can sort them.
|
2015-05-25 00:14:59 +08:00
|
|
|
SmallVector<const StringMapEntry<EntryTy> *, 64> Entries(Pool.size());
|
2014-04-26 05:34:35 +08:00
|
|
|
|
|
|
|
for (const auto &E : Pool)
|
2015-05-25 00:14:59 +08:00
|
|
|
Entries[E.getValue().Index] = &E;
|
2014-04-26 05:34:35 +08:00
|
|
|
|
|
|
|
for (const auto &Entry : Entries) {
|
|
|
|
// Emit a label for reference from debug information entries.
|
2015-05-25 00:14:59 +08:00
|
|
|
Asm.OutStreamer->EmitLabel(Entry->getValue().Symbol);
|
2014-04-26 05:34:35 +08:00
|
|
|
|
|
|
|
// Emit the string itself with a terminating null byte.
|
2015-05-25 00:14:59 +08:00
|
|
|
Asm.OutStreamer->AddComment("string offset=" +
|
|
|
|
Twine(Entry->getValue().Offset));
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm.OutStreamer->EmitBytes(
|
2014-04-26 05:34:35 +08:00
|
|
|
StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we've got an offset section go ahead and emit that now as well.
|
|
|
|
if (OffsetSection) {
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm.OutStreamer->SwitchSection(OffsetSection);
|
2014-04-26 05:34:35 +08:00
|
|
|
unsigned size = 4; // FIXME: DWARF64 is 8.
|
2015-05-25 00:14:59 +08:00
|
|
|
for (const auto &Entry : Entries)
|
|
|
|
Asm.OutStreamer->EmitIntValue(Entry->getValue().Offset, size);
|
2014-04-26 05:34:35 +08:00
|
|
|
}
|
|
|
|
}
|