xref: /aosp_15_r20/external/llvm/lib/CodeGen/AsmPrinter/DwarfStringPool.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- llvm/CodeGen/DwarfStringPool.cpp - Dwarf Debug Framework ----------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker 
10*9880d681SAndroid Build Coastguard Worker #include "DwarfStringPool.h"
11*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/AsmPrinter.h"
12*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCAsmInfo.h"
13*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCStreamer.h"
14*9880d681SAndroid Build Coastguard Worker 
15*9880d681SAndroid Build Coastguard Worker using namespace llvm;
16*9880d681SAndroid Build Coastguard Worker 
DwarfStringPool(BumpPtrAllocator & A,AsmPrinter & Asm,StringRef Prefix)17*9880d681SAndroid Build Coastguard Worker DwarfStringPool::DwarfStringPool(BumpPtrAllocator &A, AsmPrinter &Asm,
18*9880d681SAndroid Build Coastguard Worker                                  StringRef Prefix)
19*9880d681SAndroid Build Coastguard Worker     : Pool(A), Prefix(Prefix),
20*9880d681SAndroid Build Coastguard Worker       ShouldCreateSymbols(Asm.MAI->doesDwarfUseRelocationsAcrossSections()) {}
21*9880d681SAndroid Build Coastguard Worker 
getEntry(AsmPrinter & Asm,StringRef Str)22*9880d681SAndroid Build Coastguard Worker DwarfStringPool::EntryRef DwarfStringPool::getEntry(AsmPrinter &Asm,
23*9880d681SAndroid Build Coastguard Worker                                                     StringRef Str) {
24*9880d681SAndroid Build Coastguard Worker   auto I = Pool.insert(std::make_pair(Str, EntryTy()));
25*9880d681SAndroid Build Coastguard Worker   if (I.second) {
26*9880d681SAndroid Build Coastguard Worker     auto &Entry = I.first->second;
27*9880d681SAndroid Build Coastguard Worker     Entry.Index = Pool.size() - 1;
28*9880d681SAndroid Build Coastguard Worker     Entry.Offset = NumBytes;
29*9880d681SAndroid Build Coastguard Worker     Entry.Symbol = ShouldCreateSymbols ? Asm.createTempSymbol(Prefix) : nullptr;
30*9880d681SAndroid Build Coastguard Worker 
31*9880d681SAndroid Build Coastguard Worker     NumBytes += Str.size() + 1;
32*9880d681SAndroid Build Coastguard Worker     assert(NumBytes > Entry.Offset && "Unexpected overflow");
33*9880d681SAndroid Build Coastguard Worker   }
34*9880d681SAndroid Build Coastguard Worker   return EntryRef(*I.first);
35*9880d681SAndroid Build Coastguard Worker }
36*9880d681SAndroid Build Coastguard Worker 
emit(AsmPrinter & Asm,MCSection * StrSection,MCSection * OffsetSection)37*9880d681SAndroid Build Coastguard Worker void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection,
38*9880d681SAndroid Build Coastguard Worker                            MCSection *OffsetSection) {
39*9880d681SAndroid Build Coastguard Worker   if (Pool.empty())
40*9880d681SAndroid Build Coastguard Worker     return;
41*9880d681SAndroid Build Coastguard Worker 
42*9880d681SAndroid Build Coastguard Worker   // Start the dwarf str section.
43*9880d681SAndroid Build Coastguard Worker   Asm.OutStreamer->SwitchSection(StrSection);
44*9880d681SAndroid Build Coastguard Worker 
45*9880d681SAndroid Build Coastguard Worker   // Get all of the string pool entries and put them in an array by their ID so
46*9880d681SAndroid Build Coastguard Worker   // we can sort them.
47*9880d681SAndroid Build Coastguard Worker   SmallVector<const StringMapEntry<EntryTy> *, 64> Entries(Pool.size());
48*9880d681SAndroid Build Coastguard Worker 
49*9880d681SAndroid Build Coastguard Worker   for (const auto &E : Pool)
50*9880d681SAndroid Build Coastguard Worker     Entries[E.getValue().Index] = &E;
51*9880d681SAndroid Build Coastguard Worker 
52*9880d681SAndroid Build Coastguard Worker   for (const auto &Entry : Entries) {
53*9880d681SAndroid Build Coastguard Worker     assert(ShouldCreateSymbols == static_cast<bool>(Entry->getValue().Symbol) &&
54*9880d681SAndroid Build Coastguard Worker            "Mismatch between setting and entry");
55*9880d681SAndroid Build Coastguard Worker 
56*9880d681SAndroid Build Coastguard Worker     // Emit a label for reference from debug information entries.
57*9880d681SAndroid Build Coastguard Worker     if (ShouldCreateSymbols)
58*9880d681SAndroid Build Coastguard Worker       Asm.OutStreamer->EmitLabel(Entry->getValue().Symbol);
59*9880d681SAndroid Build Coastguard Worker 
60*9880d681SAndroid Build Coastguard Worker     // Emit the string itself with a terminating null byte.
61*9880d681SAndroid Build Coastguard Worker     Asm.OutStreamer->AddComment("string offset=" +
62*9880d681SAndroid Build Coastguard Worker                                 Twine(Entry->getValue().Offset));
63*9880d681SAndroid Build Coastguard Worker     Asm.OutStreamer->EmitBytes(
64*9880d681SAndroid Build Coastguard Worker         StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
65*9880d681SAndroid Build Coastguard Worker   }
66*9880d681SAndroid Build Coastguard Worker 
67*9880d681SAndroid Build Coastguard Worker   // If we've got an offset section go ahead and emit that now as well.
68*9880d681SAndroid Build Coastguard Worker   if (OffsetSection) {
69*9880d681SAndroid Build Coastguard Worker     Asm.OutStreamer->SwitchSection(OffsetSection);
70*9880d681SAndroid Build Coastguard Worker     unsigned size = 4; // FIXME: DWARF64 is 8.
71*9880d681SAndroid Build Coastguard Worker     for (const auto &Entry : Entries)
72*9880d681SAndroid Build Coastguard Worker       Asm.OutStreamer->EmitIntValue(Entry->getValue().Offset, size);
73*9880d681SAndroid Build Coastguard Worker   }
74*9880d681SAndroid Build Coastguard Worker }
75