blob: 9dd233a67ab8ec58695005c9bce03a8a0acddfcb [file] [log] [blame]
David Blaikiedaefdbf2014-04-25 21:34:35 +00001//===-- llvm/CodeGen/DwarfStringPool.cpp - Dwarf Debug Framework ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "DwarfStringPool.h"
11#include "llvm/MC/MCStreamer.h"
12
13using namespace llvm;
14
Duncan P. N. Exon Smith03b7a1c2015-05-24 16:33:33 +000015DwarfStringPool::EntryRef DwarfStringPool::getEntry(AsmPrinter &Asm,
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000016 StringRef Str) {
Duncan P. N. Exon Smith03b7a1c2015-05-24 16:33:33 +000017 auto I = Pool.insert(std::make_pair(Str, EntryTy()));
18 if (I.second) {
19 auto &Entry = I.first->second;
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000020 Entry.Index = Pool.size() - 1;
21 Entry.Offset = NumBytes;
22 Entry.Symbol = Asm.createTempSymbol(Prefix);
23
24 NumBytes += Str.size() + 1;
25 assert(NumBytes > Entry.Offset && "Unexpected overflow");
David Blaikiedaefdbf2014-04-25 21:34:35 +000026 }
Duncan P. N. Exon Smith03b7a1c2015-05-24 16:33:33 +000027 return EntryRef(*I.first);
David Blaikiedaefdbf2014-04-25 21:34:35 +000028}
29
Rafael Espindola0709a7b2015-05-21 19:20:38 +000030void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection,
31 MCSection *OffsetSection) {
David Blaikiedaefdbf2014-04-25 21:34:35 +000032 if (Pool.empty())
33 return;
34
35 // Start the dwarf str section.
Lang Hames9ff69c82015-04-24 19:11:51 +000036 Asm.OutStreamer->SwitchSection(StrSection);
David Blaikiedaefdbf2014-04-25 21:34:35 +000037
38 // Get all of the string pool entries and put them in an array by their ID so
39 // we can sort them.
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000040 SmallVector<const StringMapEntry<EntryTy> *, 64> Entries(Pool.size());
David Blaikiedaefdbf2014-04-25 21:34:35 +000041
42 for (const auto &E : Pool)
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000043 Entries[E.getValue().Index] = &E;
David Blaikiedaefdbf2014-04-25 21:34:35 +000044
45 for (const auto &Entry : Entries) {
46 // Emit a label for reference from debug information entries.
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000047 Asm.OutStreamer->EmitLabel(Entry->getValue().Symbol);
David Blaikiedaefdbf2014-04-25 21:34:35 +000048
49 // Emit the string itself with a terminating null byte.
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000050 Asm.OutStreamer->AddComment("string offset=" +
51 Twine(Entry->getValue().Offset));
Lang Hames9ff69c82015-04-24 19:11:51 +000052 Asm.OutStreamer->EmitBytes(
David Blaikiedaefdbf2014-04-25 21:34:35 +000053 StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
54 }
55
56 // If we've got an offset section go ahead and emit that now as well.
57 if (OffsetSection) {
Lang Hames9ff69c82015-04-24 19:11:51 +000058 Asm.OutStreamer->SwitchSection(OffsetSection);
David Blaikiedaefdbf2014-04-25 21:34:35 +000059 unsigned size = 4; // FIXME: DWARF64 is 8.
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000060 for (const auto &Entry : Entries)
61 Asm.OutStreamer->EmitIntValue(Entry->getValue().Offset, size);
David Blaikiedaefdbf2014-04-25 21:34:35 +000062 }
63}