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