LLVM API Documentation

DwarfStringPool.cpp
Go to the documentation of this file.
00001 //===-- llvm/CodeGen/DwarfStringPool.cpp - Dwarf Debug Framework ----------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "DwarfStringPool.h"
00011 #include "llvm/MC/MCStreamer.h"
00012 
00013 using namespace llvm;
00014 
00015 static std::pair<MCSymbol *, unsigned> &
00016 getEntry(AsmPrinter &Asm,
00017          StringMap<std::pair<MCSymbol *, unsigned>, BumpPtrAllocator &> &Pool,
00018          StringRef Prefix, StringRef Str) {
00019   std::pair<MCSymbol *, unsigned> &Entry =
00020       Pool.GetOrCreateValue(Str).getValue();
00021   if (!Entry.first) {
00022     Entry.second = Pool.size() - 1;
00023     Entry.first = Asm.GetTempSymbol(Prefix, Entry.second);
00024   }
00025   return Entry;
00026 }
00027 
00028 MCSymbol *DwarfStringPool::getSymbol(AsmPrinter &Asm, StringRef Str) {
00029   return getEntry(Asm, Pool, Prefix, Str).first;
00030 }
00031 
00032 unsigned DwarfStringPool::getIndex(AsmPrinter &Asm, StringRef Str) {
00033   return getEntry(Asm, Pool, Prefix, Str).second;
00034 }
00035 
00036 void DwarfStringPool::emit(AsmPrinter &Asm, const MCSection *StrSection,
00037                            const MCSection *OffsetSection) {
00038   if (Pool.empty())
00039     return;
00040 
00041   // Start the dwarf str section.
00042   Asm.OutStreamer.SwitchSection(StrSection);
00043 
00044   // Get all of the string pool entries and put them in an array by their ID so
00045   // we can sort them.
00046   SmallVector<const StringMapEntry<std::pair<MCSymbol *, unsigned>> *, 64>
00047   Entries(Pool.size());
00048 
00049   for (const auto &E : Pool)
00050     Entries[E.getValue().second] = &E;
00051 
00052   for (const auto &Entry : Entries) {
00053     // Emit a label for reference from debug information entries.
00054     Asm.OutStreamer.EmitLabel(Entry->getValue().first);
00055 
00056     // Emit the string itself with a terminating null byte.
00057     Asm.OutStreamer.EmitBytes(
00058         StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
00059   }
00060 
00061   // If we've got an offset section go ahead and emit that now as well.
00062   if (OffsetSection) {
00063     Asm.OutStreamer.SwitchSection(OffsetSection);
00064     unsigned offset = 0;
00065     unsigned size = 4; // FIXME: DWARF64 is 8.
00066     for (const auto &Entry : Entries) {
00067       Asm.OutStreamer.EmitIntValue(offset, size);
00068       offset += Entry->getKeyLength() + 1;
00069     }
00070   }
00071 }