blob: 830b04acb3ed28c50012f32ebfcf9c772e0c64c7 [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
Benjamin Kramer322053c2014-04-27 14:54:59 +000015static std::pair<MCSymbol *, unsigned> &
David Blaikiedaefdbf2014-04-25 21:34:35 +000016getEntry(AsmPrinter &Asm,
17 StringMap<std::pair<MCSymbol *, unsigned>, BumpPtrAllocator &> &Pool,
18 StringRef Prefix, StringRef Str) {
19 std::pair<MCSymbol *, unsigned> &Entry =
20 Pool.GetOrCreateValue(Str).getValue();
21 if (!Entry.first) {
22 Entry.second = Pool.size() - 1;
23 Entry.first = Asm.GetTempSymbol(Prefix, Entry.second);
24 }
25 return Entry;
26}
27
28MCSymbol *DwarfStringPool::getSymbol(AsmPrinter &Asm, StringRef Str) {
29 return getEntry(Asm, Pool, Prefix, Str).first;
30}
31
32unsigned DwarfStringPool::getIndex(AsmPrinter &Asm, StringRef Str) {
33 return getEntry(Asm, Pool, Prefix, Str).second;
34}
35
36void DwarfStringPool::emit(AsmPrinter &Asm, const MCSection *StrSection,
David Blaikie6741bb02014-09-11 21:12:48 +000037 const MCSection *OffsetSection) {
David Blaikiedaefdbf2014-04-25 21:34:35 +000038 if (Pool.empty())
39 return;
40
41 // Start the dwarf str section.
42 Asm.OutStreamer.SwitchSection(StrSection);
43
44 // Get all of the string pool entries and put them in an array by their ID so
45 // we can sort them.
46 SmallVector<const StringMapEntry<std::pair<MCSymbol *, unsigned>> *, 64>
47 Entries(Pool.size());
48
49 for (const auto &E : Pool)
50 Entries[E.getValue().second] = &E;
51
52 for (const auto &Entry : Entries) {
53 // Emit a label for reference from debug information entries.
54 Asm.OutStreamer.EmitLabel(Entry->getValue().first);
55
56 // Emit the string itself with a terminating null byte.
57 Asm.OutStreamer.EmitBytes(
58 StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
59 }
60
61 // If we've got an offset section go ahead and emit that now as well.
62 if (OffsetSection) {
63 Asm.OutStreamer.SwitchSection(OffsetSection);
64 unsigned offset = 0;
65 unsigned size = 4; // FIXME: DWARF64 is 8.
66 for (const auto &Entry : Entries) {
67 Asm.OutStreamer.EmitIntValue(offset, size);
68 offset += Entry->getKeyLength() + 1;
69 }
70 }
71}