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