LLVM  6.0.0svn
DwarfStringPool.cpp
Go to the documentation of this file.
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/ADT/SmallVector.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/ADT/Twine.h"
15 #include "llvm/MC/MCAsmInfo.h"
16 #include "llvm/MC/MCStreamer.h"
17 #include <cassert>
18 #include <utility>
19 
20 using namespace llvm;
21 
24  : Pool(A), Prefix(Prefix),
25  ShouldCreateSymbols(Asm.MAI->doesDwarfUseRelocationsAcrossSections()) {}
26 
28  StringRef Str) {
29  auto I = Pool.insert(std::make_pair(Str, EntryTy()));
30  if (I.second) {
31  auto &Entry = I.first->second;
32  Entry.Index = Pool.size() - 1;
33  Entry.Offset = NumBytes;
34  Entry.Symbol = ShouldCreateSymbols ? Asm.createTempSymbol(Prefix) : nullptr;
35 
36  NumBytes += Str.size() + 1;
37  assert(NumBytes > Entry.Offset && "Unexpected overflow");
38  }
39  return EntryRef(*I.first);
40 }
41 
43  MCSection *OffsetSection) {
44  if (Pool.empty())
45  return;
46 
47  // Start the dwarf str section.
48  Asm.OutStreamer->SwitchSection(StrSection);
49 
50  // Get all of the string pool entries and put them in an array by their ID so
51  // we can sort them.
52  SmallVector<const StringMapEntry<EntryTy> *, 64> Entries(Pool.size());
53 
54  for (const auto &E : Pool)
55  Entries[E.getValue().Index] = &E;
56 
57  for (const auto &Entry : Entries) {
58  assert(ShouldCreateSymbols == static_cast<bool>(Entry->getValue().Symbol) &&
59  "Mismatch between setting and entry");
60 
61  // Emit a label for reference from debug information entries.
62  if (ShouldCreateSymbols)
63  Asm.OutStreamer->EmitLabel(Entry->getValue().Symbol);
64 
65  // Emit the string itself with a terminating null byte.
66  Asm.OutStreamer->AddComment("string offset=" +
67  Twine(Entry->getValue().Offset));
68  Asm.OutStreamer->EmitBytes(
69  StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
70  }
71 
72  // If we've got an offset section go ahead and emit that now as well.
73  if (OffsetSection) {
74  Asm.OutStreamer->SwitchSection(OffsetSection);
75  unsigned size = 4; // FIXME: DWARF64 is 8.
76  for (const auto &Entry : Entries)
77  Asm.OutStreamer->EmitIntValue(Entry->getValue().Offset, size);
78  }
79 }
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:92
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
String pool entry reference.
Data for a string pool entry.
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:138
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:77
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:864
DwarfStringPoolEntryRef EntryRef
EntryRef getEntry(AsmPrinter &Asm, StringRef Str)
Get a reference to an entry in the string pool.
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void emit(AsmPrinter &Asm, MCSection *StrSection, MCSection *OffsetSection=nullptr)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
MCSymbol * createTempSymbol(const Twine &Name) const
DwarfStringPool(BumpPtrAllocator &A, AsmPrinter &Asm, StringRef Prefix)