Encapsulate the DWARF string pool in a separate type.
authorDavid Blaikie <dblaikie@gmail.com>
Fri, 25 Apr 2014 21:34:35 +0000 (21:34 +0000)
committerDavid Blaikie <dblaikie@gmail.com>
Fri, 25 Apr 2014 21:34:35 +0000 (21:34 +0000)
Pulls out some more code from some of the rather monolithic DWARF
classes. Unlike the address table, the string table won't move up into
DwarfDebug - each DWARF file has its own string table (but there can be
only one address table).

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@207277 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/AsmPrinter/CMakeLists.txt
lib/CodeGen/AsmPrinter/DwarfAccelTable.cpp
lib/CodeGen/AsmPrinter/DwarfDebug.cpp
lib/CodeGen/AsmPrinter/DwarfFile.cpp
lib/CodeGen/AsmPrinter/DwarfFile.h
lib/CodeGen/AsmPrinter/DwarfStringPool.cpp [new file with mode: 0644]
lib/CodeGen/AsmPrinter/DwarfStringPool.h [new file with mode: 0644]
lib/CodeGen/AsmPrinter/DwarfUnit.cpp

index b4f8dc2bd83da5632556579e7c1a39c112b19c93..a9afc84e84a0a6093f53ff15e10be0019ae27da6 100644 (file)
@@ -11,6 +11,7 @@ add_llvm_library(LLVMAsmPrinter
   DwarfDebug.cpp
   DwarfException.cpp
   DwarfFile.cpp
+  DwarfStringPool.cpp
   DwarfUnit.cpp
   ErlangGCPrinter.cpp
   OcamlGCPrinter.cpp
index 33662ff2a6af2d80ed629e001ee8434df8cf81ed..d1eeb9308f2f6cdfa5ee7279f827798d56f931ba 100644 (file)
@@ -179,8 +179,8 @@ void DwarfAccelTable::EmitData(AsmPrinter *Asm, DwarfFile *D) {
       // Remember to emit the label for our offset.
       Asm->OutStreamer.EmitLabel((*HI)->Sym);
       Asm->OutStreamer.AddComment((*HI)->Str);
-      Asm->EmitSectionOffset(D->getStringPoolEntry((*HI)->Str),
-                             D->getStringPoolSym());
+      Asm->EmitSectionOffset(D->getStringPool().getSymbol(*Asm, (*HI)->Str),
+                             D->getStringPool().getSectionSymbol());
       Asm->OutStreamer.AddComment("Num DIEs");
       Asm->EmitInt32((*HI)->Data.size());
       for (ArrayRef<HashDataContents *>::const_iterator
index 3bc359425c023420918694c3354356e60d0160da..90c127c5dd2b512b09f2daeaa406e039ee04f1a5 100644 (file)
@@ -2545,27 +2545,27 @@ void DwarfDebug::attachLowHighPC(DwarfCompileUnit &Unit, DIE &D,
 void DwarfDebug::addAccelName(StringRef Name, const DIE &Die) {
   if (!useDwarfAccelTables())
     return;
-  InfoHolder.getStringPoolEntry(Name);
+  InfoHolder.getStringPool().getSymbol(*Asm, Name);
   AccelNames.AddName(Name, &Die);
 }
 
 void DwarfDebug::addAccelObjC(StringRef Name, const DIE &Die) {
   if (!useDwarfAccelTables())
     return;
-  InfoHolder.getStringPoolEntry(Name);
+  InfoHolder.getStringPool().getSymbol(*Asm, Name);
   AccelObjC.AddName(Name, &Die);
 }
 
 void DwarfDebug::addAccelNamespace(StringRef Name, const DIE &Die) {
   if (!useDwarfAccelTables())
     return;
-  InfoHolder.getStringPoolEntry(Name);
+  InfoHolder.getStringPool().getSymbol(*Asm, Name);
   AccelNamespace.AddName(Name, &Die);
 }
 
 void DwarfDebug::addAccelType(StringRef Name, const DIE &Die, char Flags) {
   if (!useDwarfAccelTables())
     return;
-  InfoHolder.getStringPoolEntry(Name);
+  InfoHolder.getStringPool().getSymbol(*Asm, Name);
   AccelTypes.AddName(Name, &Die, Flags);
 }
index 131d7fb814518f61b7c83b1d5611f01878a1157c..737ee54f94b53c7b890a139f5ddc433b082bf10b 100644 (file)
 #include "llvm/Target/TargetLoweringObjectFile.h"
 
 namespace llvm {
-DwarfFile::DwarfFile(AsmPrinter *AP, const char *Pref, BumpPtrAllocator &DA)
-    : Asm(AP), StringPool(DA), NextStringPoolNumber(0), StringPref(Pref) {}
+DwarfFile::DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA)
+    : Asm(AP), StrPool(DA, *Asm, Pref) {}
 
 DwarfFile::~DwarfFile() {}
 
-MCSymbol *DwarfFile::getStringPoolSym() {
-  return Asm->GetTempSymbol(StringPref);
-}
-
-MCSymbol *DwarfFile::getStringPoolEntry(StringRef Str) {
-  std::pair<MCSymbol *, unsigned> &Entry =
-      StringPool.GetOrCreateValue(Str).getValue();
-  if (Entry.first)
-    return Entry.first;
-
-  Entry.second = NextStringPoolNumber++;
-  return Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
-}
-
-unsigned DwarfFile::getStringPoolIndex(StringRef Str) {
-  std::pair<MCSymbol *, unsigned> &Entry =
-      StringPool.GetOrCreateValue(Str).getValue();
-  if (Entry.first)
-    return Entry.second;
-
-  Entry.second = NextStringPoolNumber++;
-  Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
-  return Entry.second;
-}
-
 // Define a unique number for the abbreviation.
 //
 void DwarfFile::assignAbbrevNumber(DIEAbbrev &Abbrev) {
@@ -176,40 +151,6 @@ void DwarfFile::emitAbbrevs(const MCSection *Section) {
 void DwarfFile::emitStrings(const MCSection *StrSection,
                             const MCSection *OffsetSection,
                             const MCSymbol *StrSecSym) {
-
-  if (StringPool.empty())
-    return;
-
-  // Start the dwarf str section.
-  Asm->OutStreamer.SwitchSection(StrSection);
-
-  // Get all of the string pool entries and put them in an array by their ID so
-  // we can sort them.
-  SmallVector<std::pair<unsigned, const StrPool::value_type *>, 64> Entries;
-
-  for (const auto &I : StringPool)
-    Entries.push_back(std::make_pair(I.second.second, &I));
-
-  array_pod_sort(Entries.begin(), Entries.end());
-
-  for (const auto &Entry : Entries) {
-    // Emit a label for reference from debug information entries.
-    Asm->OutStreamer.EmitLabel(Entry.second->getValue().first);
-
-    // Emit the string itself with a terminating null byte.
-    Asm->OutStreamer.EmitBytes(StringRef(Entry.second->getKeyData(),
-                                         Entry.second->getKeyLength() + 1));
-  }
-
-  // If we've got an offset section go ahead and emit that now as well.
-  if (OffsetSection) {
-    Asm->OutStreamer.SwitchSection(OffsetSection);
-    unsigned offset = 0;
-    unsigned size = 4; // FIXME: DWARF64 is 8.
-    for (const auto &Entry : Entries) {
-      Asm->OutStreamer.EmitIntValue(offset, size);
-      offset += Entry.second->getKeyLength() + 1;
-    }
-  }
+  StrPool.emit(*Asm, StrSection, OffsetSection, StrSecSym);
 }
 }
index 68ac530fd35a9cb03a07d6f2bdf3c93d7d1d91e6..3985eb28cd6d078b6da7a5c4660fb12d7d107623 100644 (file)
@@ -16,6 +16,7 @@
 #include "llvm/ADT/StringMap.h"
 #include "llvm/Support/Allocator.h"
 #include "AddressPool.h"
+#include "DwarfStringPool.h"
 
 #include <vector>
 #include <string>
@@ -43,17 +44,10 @@ class DwarfFile {
   // A pointer to all units in the section.
   SmallVector<std::unique_ptr<DwarfUnit>, 1> CUs;
 
-  // Collection of strings for this unit and assorted symbols.
-  // A String->Symbol mapping of strings used by indirect
-  // references.
-  typedef StringMap<std::pair<MCSymbol *, unsigned>, BumpPtrAllocator &>
-  StrPool;
-  StrPool StringPool;
-  unsigned NextStringPoolNumber;
-  std::string StringPref;
+  DwarfStringPool StrPool;
 
 public:
-  DwarfFile(AsmPrinter *AP, const char *Pref, BumpPtrAllocator &DA);
+  DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA);
 
   ~DwarfFile();
 
@@ -83,19 +77,8 @@ public:
                    const MCSection *OffsetSection = nullptr,
                    const MCSymbol *StrSecSym = nullptr);
 
-  /// \brief Returns the entry into the start of the pool.
-  MCSymbol *getStringPoolSym();
-
-  /// \brief Returns an entry into the string pool with the given
-  /// string text.
-  MCSymbol *getStringPoolEntry(StringRef Str);
-
-  /// \brief Returns the index into the string pool with the given
-  /// string text.
-  unsigned getStringPoolIndex(StringRef Str);
-
   /// \brief Returns the string pool.
-  StrPool *getStringPool() { return &StringPool; }
+  DwarfStringPool &getStringPool() { return StrPool; }
 };
 }
 #endif
diff --git a/lib/CodeGen/AsmPrinter/DwarfStringPool.cpp b/lib/CodeGen/AsmPrinter/DwarfStringPool.cpp
new file mode 100644 (file)
index 0000000..84c9a88
--- /dev/null
@@ -0,0 +1,74 @@
+//===-- llvm/CodeGen/DwarfStringPool.cpp - Dwarf Debug Framework ----------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "DwarfStringPool.h"
+#include "llvm/MC/MCStreamer.h"
+
+using namespace llvm;
+
+MCSymbol *DwarfStringPool::getSectionSymbol() { return SectionSymbol; }
+
+std::pair<MCSymbol *, unsigned> &
+getEntry(AsmPrinter &Asm,
+         StringMap<std::pair<MCSymbol *, unsigned>, BumpPtrAllocator &> &Pool,
+         StringRef Prefix, StringRef Str) {
+  std::pair<MCSymbol *, unsigned> &Entry =
+      Pool.GetOrCreateValue(Str).getValue();
+  if (!Entry.first) {
+    Entry.second = Pool.size() - 1;
+    Entry.first = Asm.GetTempSymbol(Prefix, Entry.second);
+  }
+  return Entry;
+}
+
+MCSymbol *DwarfStringPool::getSymbol(AsmPrinter &Asm, StringRef Str) {
+  return getEntry(Asm, Pool, Prefix, Str).first;
+}
+
+unsigned DwarfStringPool::getIndex(AsmPrinter &Asm, StringRef Str) {
+  return getEntry(Asm, Pool, Prefix, Str).second;
+}
+
+void DwarfStringPool::emit(AsmPrinter &Asm, const MCSection *StrSection,
+                           const MCSection *OffsetSection,
+                           const MCSymbol *StrSecSym) {
+  if (Pool.empty())
+    return;
+
+  // Start the dwarf str section.
+  Asm.OutStreamer.SwitchSection(StrSection);
+
+  // Get all of the string pool entries and put them in an array by their ID so
+  // we can sort them.
+  SmallVector<const StringMapEntry<std::pair<MCSymbol *, unsigned>> *, 64>
+  Entries(Pool.size());
+
+  for (const auto &E : Pool)
+    Entries[E.getValue().second] = &E;
+
+  for (const auto &Entry : Entries) {
+    // Emit a label for reference from debug information entries.
+    Asm.OutStreamer.EmitLabel(Entry->getValue().first);
+
+    // Emit the string itself with a terminating null byte.
+    Asm.OutStreamer.EmitBytes(
+        StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
+  }
+
+  // If we've got an offset section go ahead and emit that now as well.
+  if (OffsetSection) {
+    Asm.OutStreamer.SwitchSection(OffsetSection);
+    unsigned offset = 0;
+    unsigned size = 4; // FIXME: DWARF64 is 8.
+    for (const auto &Entry : Entries) {
+      Asm.OutStreamer.EmitIntValue(offset, size);
+      offset += Entry->getKeyLength() + 1;
+    }
+  }
+}
diff --git a/lib/CodeGen/AsmPrinter/DwarfStringPool.h b/lib/CodeGen/AsmPrinter/DwarfStringPool.h
new file mode 100644 (file)
index 0000000..c1615fb
--- /dev/null
@@ -0,0 +1,55 @@
+//===-- llvm/CodeGen/DwarfStringPool.h - Dwarf Debug Framework -*- C++ -*--===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef CODEGEN_ASMPRINTER_STRINGPOOL_H__
+#define CODEGEN_ASMPRINTER_STRINGPOOL_H__
+
+#include "llvm/ADT/StringMap.h"
+#include "llvm/CodeGen/AsmPrinter.h"
+#include "llvm/Support/Allocator.h"
+
+#include <utility>
+
+namespace llvm {
+
+class MCSymbol;
+class MCSection;
+class StringRef;
+
+// Collection of strings for this unit and assorted symbols.
+// A String->Symbol mapping of strings used by indirect
+// references.
+class DwarfStringPool {
+  StringMap<std::pair<MCSymbol *, unsigned>, BumpPtrAllocator &> Pool;
+  StringRef Prefix;
+  MCSymbol *SectionSymbol;
+
+public:
+  DwarfStringPool(BumpPtrAllocator &A, AsmPrinter &Asm, StringRef Prefix)
+      : Pool(A), Prefix(Prefix), SectionSymbol(Asm.GetTempSymbol(Prefix)) {}
+
+  void emit(AsmPrinter &Asm, const MCSection *StrSection,
+            const MCSection *OffsetSection = nullptr,
+            const MCSymbol *StrSecSym = nullptr);
+
+  /// \brief Returns the entry into the start of the pool.
+  MCSymbol *getSectionSymbol();
+
+  /// \brief Returns an entry into the string pool with the given
+  /// string text.
+  MCSymbol *getSymbol(AsmPrinter &Asm, StringRef Str);
+
+  /// \brief Returns the index into the string pool with the given
+  /// string text.
+  unsigned getIndex(AsmPrinter &Asm, StringRef Str);
+
+  bool empty() const { return Pool.empty(); }
+};
+}
+#endif
index c7d22724ce31819b87f0fea58d8ad91628fd95b4..326243278ec0f3e74af76ba5a9e0f8e6350ab74a 100644 (file)
@@ -208,7 +208,7 @@ void DwarfUnit::addString(DIE &Die, dwarf::Attribute Attribute,
   if (!DD->useSplitDwarf())
     return addLocalString(Die, Attribute, String);
 
-  unsigned idx = DU->getStringPoolIndex(String);
+  unsigned idx = DU->getStringPool().getIndex(*Asm, String);
   DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
   DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
   Die.addValue(Attribute, dwarf::DW_FORM_GNU_str_index, Str);
@@ -218,12 +218,12 @@ void DwarfUnit::addString(DIE &Die, dwarf::Attribute Attribute,
 /// to be in the local string pool instead of indirected.
 void DwarfUnit::addLocalString(DIE &Die, dwarf::Attribute Attribute,
                                StringRef String) {
-  MCSymbol *Symb = DU->getStringPoolEntry(String);
+  MCSymbol *Symb = DU->getStringPool().getSymbol(*Asm, String);
   DIEValue *Value;
   if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
     Value = new (DIEValueAllocator) DIELabel(Symb);
   else {
-    MCSymbol *StringPool = DU->getStringPoolSym();
+    MCSymbol *StringPool = DU->getStringPool().getSectionSymbol();
     Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
   }
   DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);