comparison lib/CodeGen/AsmPrinter/DwarfStringPool.cpp @ 95:afa8332a0e37 LLVM3.8

LLVM 3.8
author Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
date Tue, 13 Oct 2015 17:48:58 +0900
parents 60c9769439b8
children 803732b1fca8
comparison
equal deleted inserted replaced
84:f3e34b893a5f 95:afa8332a0e37
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 9
10 #include "DwarfStringPool.h" 10 #include "DwarfStringPool.h"
11 #include "llvm/CodeGen/AsmPrinter.h"
12 #include "llvm/MC/MCAsmInfo.h"
11 #include "llvm/MC/MCStreamer.h" 13 #include "llvm/MC/MCStreamer.h"
12 14
13 using namespace llvm; 15 using namespace llvm;
14 16
15 static std::pair<MCSymbol *, unsigned> & 17 DwarfStringPool::DwarfStringPool(BumpPtrAllocator &A, AsmPrinter &Asm,
16 getEntry(AsmPrinter &Asm, 18 StringRef Prefix)
17 StringMap<std::pair<MCSymbol *, unsigned>, BumpPtrAllocator &> &Pool, 19 : Pool(A), Prefix(Prefix),
18 StringRef Prefix, StringRef Str) { 20 ShouldCreateSymbols(Asm.MAI->doesDwarfUseRelocationsAcrossSections()) {}
19 std::pair<MCSymbol *, unsigned> &Entry = Pool[Str]; 21
20 if (!Entry.first) { 22 DwarfStringPool::EntryRef DwarfStringPool::getEntry(AsmPrinter &Asm,
21 Entry.second = Pool.size() - 1; 23 StringRef Str) {
22 Entry.first = Asm.GetTempSymbol(Prefix, Entry.second); 24 auto I = Pool.insert(std::make_pair(Str, EntryTy()));
25 if (I.second) {
26 auto &Entry = I.first->second;
27 Entry.Index = Pool.size() - 1;
28 Entry.Offset = NumBytes;
29 Entry.Symbol = ShouldCreateSymbols ? Asm.createTempSymbol(Prefix) : nullptr;
30
31 NumBytes += Str.size() + 1;
32 assert(NumBytes > Entry.Offset && "Unexpected overflow");
23 } 33 }
24 return Entry; 34 return EntryRef(*I.first);
25 } 35 }
26 36
27 MCSymbol *DwarfStringPool::getSymbol(AsmPrinter &Asm, StringRef Str) { 37 void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection,
28 return getEntry(Asm, Pool, Prefix, Str).first; 38 MCSection *OffsetSection) {
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()) 39 if (Pool.empty())
38 return; 40 return;
39 41
40 // Start the dwarf str section. 42 // Start the dwarf str section.
41 Asm.OutStreamer.SwitchSection(StrSection); 43 Asm.OutStreamer->SwitchSection(StrSection);
42 44
43 // Get all of the string pool entries and put them in an array by their ID so 45 // Get all of the string pool entries and put them in an array by their ID so
44 // we can sort them. 46 // we can sort them.
45 SmallVector<const StringMapEntry<std::pair<MCSymbol *, unsigned>> *, 64> 47 SmallVector<const StringMapEntry<EntryTy> *, 64> Entries(Pool.size());
46 Entries(Pool.size());
47 48
48 for (const auto &E : Pool) 49 for (const auto &E : Pool)
49 Entries[E.getValue().second] = &E; 50 Entries[E.getValue().Index] = &E;
50 51
51 for (const auto &Entry : Entries) { 52 for (const auto &Entry : Entries) {
53 assert(ShouldCreateSymbols == static_cast<bool>(Entry->getValue().Symbol) &&
54 "Mismatch between setting and entry");
55
52 // Emit a label for reference from debug information entries. 56 // Emit a label for reference from debug information entries.
53 Asm.OutStreamer.EmitLabel(Entry->getValue().first); 57 if (ShouldCreateSymbols)
58 Asm.OutStreamer->EmitLabel(Entry->getValue().Symbol);
54 59
55 // Emit the string itself with a terminating null byte. 60 // Emit the string itself with a terminating null byte.
56 Asm.OutStreamer.EmitBytes( 61 Asm.OutStreamer->AddComment("string offset=" +
62 Twine(Entry->getValue().Offset));
63 Asm.OutStreamer->EmitBytes(
57 StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1)); 64 StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
58 } 65 }
59 66
60 // If we've got an offset section go ahead and emit that now as well. 67 // If we've got an offset section go ahead and emit that now as well.
61 if (OffsetSection) { 68 if (OffsetSection) {
62 Asm.OutStreamer.SwitchSection(OffsetSection); 69 Asm.OutStreamer->SwitchSection(OffsetSection);
63 unsigned offset = 0;
64 unsigned size = 4; // FIXME: DWARF64 is 8. 70 unsigned size = 4; // FIXME: DWARF64 is 8.
65 for (const auto &Entry : Entries) { 71 for (const auto &Entry : Entries)
66 Asm.OutStreamer.EmitIntValue(offset, size); 72 Asm.OutStreamer->EmitIntValue(Entry->getValue().Offset, size);
67 offset += Entry->getKeyLength() + 1;
68 }
69 } 73 }
70 } 74 }