Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.0.0git
DwarfStringPool.cpp
Go to the documentation of this file.
1//===- llvm/CodeGen/DwarfStringPool.cpp - Dwarf Debug Framework -----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "DwarfStringPool.h"
10#include "llvm/ADT/SmallVector.h"
11#include "llvm/ADT/Twine.h"
12#include "llvm/CodeGen/AsmPrinter.h"
13#include "llvm/MC/MCAsmInfo.h"
14#include "llvm/MC/MCStreamer.h"
15#include <cassert>
16#include <utility>
17
18using namespacellvm;
19
20DwarfStringPool::DwarfStringPool(BumpPtrAllocator &A,AsmPrinter &Asm,
21StringRef Prefix)
22 : Pool(A), Prefix(Prefix),
23 ShouldCreateSymbols(Asm.doesDwarfUseRelocationsAcrossSections()) {}
24
25StringMapEntry<DwarfStringPool::EntryTy> &
26DwarfStringPool::getEntryImpl(AsmPrinter &Asm,StringRef Str) {
27autoI = Pool.insert(std::make_pair(Str, EntryTy()));
28auto &Entry =I.first->second;
29if (I.second) {
30 Entry.Index =EntryTy::NotIndexed;
31 Entry.Offset = NumBytes;
32 Entry.Symbol = ShouldCreateSymbols ? Asm.createTempSymbol(Prefix) :nullptr;
33
34 NumBytes += Str.size() + 1;
35 }
36return *I.first;
37}
38
39DwarfStringPool::EntryRefDwarfStringPool::getEntry(AsmPrinter &Asm,
40StringRef Str) {
41auto &MapEntry = getEntryImpl(Asm, Str);
42returnEntryRef(MapEntry);
43}
44
45DwarfStringPool::EntryRefDwarfStringPool::getIndexedEntry(AsmPrinter &Asm,
46StringRef Str) {
47auto &MapEntry = getEntryImpl(Asm, Str);
48if (!MapEntry.getValue().isIndexed())
49 MapEntry.getValue().Index = NumIndexedStrings++;
50returnEntryRef(MapEntry);
51}
52
53voidDwarfStringPool::emitStringOffsetsTableHeader(AsmPrinter &Asm,
54MCSection *Section,
55MCSymbol *StartSym) {
56if (getNumIndexedStrings() == 0)
57return;
58 Asm.OutStreamer->switchSection(Section);
59unsigned EntrySize = Asm.getDwarfOffsetByteSize();
60// We are emitting the header for a contribution to the string offsets
61// table. The header consists of an entry with the contribution's
62// size (not including the size of the length field), the DWARF version and
63// 2 bytes of padding.
64 Asm.emitDwarfUnitLength(getNumIndexedStrings() * EntrySize + 4,
65"Length of String Offsets Set");
66 Asm.emitInt16(Asm.getDwarfVersion());
67 Asm.emitInt16(0);
68// Define the symbol that marks the start of the contribution. It is
69// referenced by most unit headers via DW_AT_str_offsets_base.
70// Split units do not use the attribute.
71if (StartSym)
72 Asm.OutStreamer->emitLabel(StartSym);
73}
74
75voidDwarfStringPool::emit(AsmPrinter &Asm,MCSection *StrSection,
76MCSection *OffsetSection,bool UseRelativeOffsets) {
77if (Pool.empty())
78return;
79
80// Start the dwarf str section.
81 Asm.OutStreamer->switchSection(StrSection);
82
83// Get all of the string pool entries and sort them by their offset.
84SmallVector<const StringMapEntry<EntryTy> *, 64> Entries;
85 Entries.reserve(Pool.size());
86
87for (constauto &E : Pool)
88 Entries.push_back(&E);
89
90llvm::sort(Entries, [](constStringMapEntry<EntryTy> *A,
91constStringMapEntry<EntryTy> *B) {
92returnA->getValue().Offset < B->getValue().Offset;
93 });
94
95for (constauto &Entry : Entries) {
96assert(ShouldCreateSymbols ==static_cast<bool>(Entry->getValue().Symbol) &&
97"Mismatch between setting and entry");
98
99// Emit a label for reference from debug information entries.
100if (ShouldCreateSymbols)
101 Asm.OutStreamer->emitLabel(Entry->getValue().Symbol);
102
103// Emit the string itself with a terminating null byte.
104 Asm.OutStreamer->AddComment("string offset=" +
105Twine(Entry->getValue().Offset));
106 Asm.OutStreamer->emitBytes(
107StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
108 }
109
110// If we've got an offset section go ahead and emit that now as well.
111if (OffsetSection) {
112// Now only take the indexed entries and put them in an array by their ID so
113// we can emit them in order.
114 Entries.resize(NumIndexedStrings);
115for (constauto &Entry : Pool) {
116if (Entry.getValue().isIndexed())
117 Entries[Entry.getValue().Index] = &Entry;
118 }
119
120 Asm.OutStreamer->switchSection(OffsetSection);
121unsignedsize = Asm.getDwarfOffsetByteSize();
122for (constauto &Entry : Entries)
123if (UseRelativeOffsets)
124 Asm.emitDwarfStringOffset(Entry->getValue());
125else
126 Asm.OutStreamer->emitIntValue(Entry->getValue().Offset,size);
127 }
128}
AsmPrinter.h
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
A
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
DwarfStringPool.h
MCAsmInfo.h
MCStreamer.h
I
#define I(x, y, z)
Definition:MD5.cpp:58
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
SmallVector.h
This file defines the SmallVector class.
Twine.h
llvm::AsmPrinter
This class is intended to be used as a driving class for all asm writers.
Definition:AsmPrinter.h:87
llvm::BumpPtrAllocatorImpl
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition:Allocator.h:66
llvm::DwarfStringPoolEntryRef
DwarfStringPoolEntryRef: Dwarf string pool entry reference.
Definition:DwarfStringPoolEntry.h:49
llvm::DwarfStringPool::getEntry
EntryRef getEntry(AsmPrinter &Asm, StringRef Str)
Get a reference to an entry in the string pool.
Definition:DwarfStringPool.cpp:39
llvm::DwarfStringPool::getIndexedEntry
EntryRef getIndexedEntry(AsmPrinter &Asm, StringRef Str)
Same as getEntry, except that you can use EntryRef::getIndex to obtain a unique ID of this entry (e....
Definition:DwarfStringPool.cpp:45
llvm::DwarfStringPool::EntryRef
DwarfStringPoolEntryRef EntryRef
Definition:DwarfStringPool.h:38
llvm::DwarfStringPool::getNumIndexedStrings
unsigned getNumIndexedStrings() const
Definition:DwarfStringPool.h:53
llvm::DwarfStringPool::emitStringOffsetsTableHeader
void emitStringOffsetsTableHeader(AsmPrinter &Asm, MCSection *OffsetSection, MCSymbol *StartSym)
Definition:DwarfStringPool.cpp:53
llvm::DwarfStringPool::size
unsigned size() const
Definition:DwarfStringPool.h:51
llvm::DwarfStringPool::DwarfStringPool
DwarfStringPool(BumpPtrAllocator &A, AsmPrinter &Asm, StringRef Prefix)
Definition:DwarfStringPool.cpp:20
llvm::DwarfStringPool::emit
void emit(AsmPrinter &Asm, MCSection *StrSection, MCSection *OffsetSection=nullptr, bool UseRelativeOffsets=false)
Definition:DwarfStringPool.cpp:75
llvm::MCSection
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition:MCSection.h:36
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition:MCSymbol.h:41
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition:SmallVector.h:1196
llvm::StringMapEntry
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
Definition:StringMapEntry.h:102
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition:StringRef.h:51
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition:Twine.h:81
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::Offset
@ Offset
Definition:DWP.cpp:480
llvm::sort
void sort(IteratorTy Start, IteratorTy End)
Definition:STLExtras.h:1664
llvm::DwarfStringPoolEntry::NotIndexed
static constexpr unsigned NotIndexed
Definition:DwarfStringPoolEntry.h:21

Generated on Fri Jul 18 2025 10:27:57 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp