blob: ee224f8b4d90d780ad1324bd2547148276594107 [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) {
David Blaikie5106ce72014-11-19 05:49:42 +000019 std::pair<MCSymbol *, unsigned> &Entry = Pool[Str];
David Blaikiedaefdbf2014-04-25 21:34:35 +000020 if (!Entry.first) {
21 Entry.second = Pool.size() - 1;
Rafael Espindola9ab09232015-03-17 20:07:06 +000022 Entry.first = Asm.createTempSymbol(Prefix);
David Blaikiedaefdbf2014-04-25 21:34:35 +000023 }
24 return Entry;
25}
26
27MCSymbol *DwarfStringPool::getSymbol(AsmPrinter &Asm, StringRef Str) {
28 return getEntry(Asm, Pool, Prefix, Str).first;
29}
30
31unsigned DwarfStringPool::getIndex(AsmPrinter &Asm, StringRef Str) {
32 return getEntry(Asm, Pool, Prefix, Str).second;
33}
34
35void DwarfStringPool::emit(AsmPrinter &Asm, const MCSection *StrSection,
David Blaikie6741bb02014-09-11 21:12:48 +000036 const MCSection *OffsetSection) {
David Blaikiedaefdbf2014-04-25 21:34:35 +000037 if (Pool.empty())
38 return;
39
40 // Start the dwarf str section.
Lang Hames9ff69c82015-04-24 19:11:51 +000041 Asm.OutStreamer->SwitchSection(StrSection);
David Blaikiedaefdbf2014-04-25 21:34:35 +000042
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.
Lang Hames9ff69c82015-04-24 19:11:51 +000053 Asm.OutStreamer->EmitLabel(Entry->getValue().first);
David Blaikiedaefdbf2014-04-25 21:34:35 +000054
55 // Emit the string itself with a terminating null byte.
Lang Hames9ff69c82015-04-24 19:11:51 +000056 Asm.OutStreamer->EmitBytes(
David Blaikiedaefdbf2014-04-25 21:34:35 +000057 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) {
Lang Hames9ff69c82015-04-24 19:11:51 +000062 Asm.OutStreamer->SwitchSection(OffsetSection);
David Blaikiedaefdbf2014-04-25 21:34:35 +000063 unsigned offset = 0;
64 unsigned size = 4; // FIXME: DWARF64 is 8.
65 for (const auto &Entry : Entries) {
Lang Hames9ff69c82015-04-24 19:11:51 +000066 Asm.OutStreamer->EmitIntValue(offset, size);
David Blaikiedaefdbf2014-04-25 21:34:35 +000067 offset += Entry->getKeyLength() + 1;
68 }
69 }
70}