David Blaikie | daefdbf | 2014-04-25 21:34:35 +0000 | [diff] [blame] | 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 | |
Benjamin Kramer | 322053c | 2014-04-27 14:54:59 +0000 | [diff] [blame] | 15 | static std::pair<MCSymbol *, unsigned> & |
David Blaikie | daefdbf | 2014-04-25 21:34:35 +0000 | [diff] [blame] | 16 | getEntry(AsmPrinter &Asm, |
| 17 | StringMap<std::pair<MCSymbol *, unsigned>, BumpPtrAllocator &> &Pool, |
| 18 | StringRef Prefix, StringRef Str) { |
David Blaikie | 5106ce7 | 2014-11-19 05:49:42 +0000 | [diff] [blame] | 19 | std::pair<MCSymbol *, unsigned> &Entry = Pool[Str]; |
David Blaikie | daefdbf | 2014-04-25 21:34:35 +0000 | [diff] [blame] | 20 | if (!Entry.first) { |
| 21 | Entry.second = Pool.size() - 1; |
Rafael Espindola | 9ab0923 | 2015-03-17 20:07:06 +0000 | [diff] [blame] | 22 | Entry.first = Asm.createTempSymbol(Prefix); |
David Blaikie | daefdbf | 2014-04-25 21:34:35 +0000 | [diff] [blame] | 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, |
David Blaikie | 6741bb0 | 2014-09-11 21:12:48 +0000 | [diff] [blame] | 36 | const MCSection *OffsetSection) { |
David Blaikie | daefdbf | 2014-04-25 21:34:35 +0000 | [diff] [blame] | 37 | if (Pool.empty()) |
| 38 | return; |
| 39 | |
| 40 | // Start the dwarf str section. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame^] | 41 | Asm.OutStreamer->SwitchSection(StrSection); |
David Blaikie | daefdbf | 2014-04-25 21:34:35 +0000 | [diff] [blame] | 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. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame^] | 53 | Asm.OutStreamer->EmitLabel(Entry->getValue().first); |
David Blaikie | daefdbf | 2014-04-25 21:34:35 +0000 | [diff] [blame] | 54 | |
| 55 | // Emit the string itself with a terminating null byte. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame^] | 56 | Asm.OutStreamer->EmitBytes( |
David Blaikie | daefdbf | 2014-04-25 21:34:35 +0000 | [diff] [blame] | 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) { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame^] | 62 | Asm.OutStreamer->SwitchSection(OffsetSection); |
David Blaikie | daefdbf | 2014-04-25 21:34:35 +0000 | [diff] [blame] | 63 | unsigned offset = 0; |
| 64 | unsigned size = 4; // FIXME: DWARF64 is 8. |
| 65 | for (const auto &Entry : Entries) { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame^] | 66 | Asm.OutStreamer->EmitIntValue(offset, size); |
David Blaikie | daefdbf | 2014-04-25 21:34:35 +0000 | [diff] [blame] | 67 | offset += Entry->getKeyLength() + 1; |
| 68 | } |
| 69 | } |
| 70 | } |