blob: a599126f0b73875b9327e0c4fa4821164df2af01 [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 Smith1a65e4a2015-05-24 16:14:59 +000015DwarfStringPool::EntryTy &DwarfStringPool::getEntry(AsmPrinter &Asm,
16 StringRef Str) {
17 auto &Entry = Pool[Str];
18 if (!Entry.Symbol) {
19 Entry.Index = Pool.size() - 1;
20 Entry.Offset = NumBytes;
21 Entry.Symbol = Asm.createTempSymbol(Prefix);
22
23 NumBytes += Str.size() + 1;
24 assert(NumBytes > Entry.Offset && "Unexpected overflow");
David Blaikiedaefdbf2014-04-25 21:34:35 +000025 }
26 return Entry;
27}
28
Rafael Espindola0709a7b2015-05-21 19:20:38 +000029void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection,
30 MCSection *OffsetSection) {
David Blaikiedaefdbf2014-04-25 21:34:35 +000031 if (Pool.empty())
32 return;
33
34 // Start the dwarf str section.
Lang Hames9ff69c82015-04-24 19:11:51 +000035 Asm.OutStreamer->SwitchSection(StrSection);
David Blaikiedaefdbf2014-04-25 21:34:35 +000036
37 // Get all of the string pool entries and put them in an array by their ID so
38 // we can sort them.
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000039 SmallVector<const StringMapEntry<EntryTy> *, 64> Entries(Pool.size());
David Blaikiedaefdbf2014-04-25 21:34:35 +000040
41 for (const auto &E : Pool)
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000042 Entries[E.getValue().Index] = &E;
David Blaikiedaefdbf2014-04-25 21:34:35 +000043
44 for (const auto &Entry : Entries) {
45 // Emit a label for reference from debug information entries.
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000046 Asm.OutStreamer->EmitLabel(Entry->getValue().Symbol);
David Blaikiedaefdbf2014-04-25 21:34:35 +000047
48 // Emit the string itself with a terminating null byte.
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000049 Asm.OutStreamer->AddComment("string offset=" +
50 Twine(Entry->getValue().Offset));
Lang Hames9ff69c82015-04-24 19:11:51 +000051 Asm.OutStreamer->EmitBytes(
David Blaikiedaefdbf2014-04-25 21:34:35 +000052 StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
53 }
54
55 // If we've got an offset section go ahead and emit that now as well.
56 if (OffsetSection) {
Lang Hames9ff69c82015-04-24 19:11:51 +000057 Asm.OutStreamer->SwitchSection(OffsetSection);
David Blaikiedaefdbf2014-04-25 21:34:35 +000058 unsigned size = 4; // FIXME: DWARF64 is 8.
Duncan P. N. Exon Smith1a65e4a2015-05-24 16:14:59 +000059 for (const auto &Entry : Entries)
60 Asm.OutStreamer->EmitIntValue(Entry->getValue().Offset, size);
David Blaikiedaefdbf2014-04-25 21:34:35 +000061 }
62}