Skip to content

Commit 4613ca9

Browse files
committed
[dwarf][NFC] refactor emitRangeList function
This patch refactors the DWARF list emitter to prepare for adding a StartxEndx DWARF entry kind in a subsequent patch. Specifically, it introduces DwarfRangeListTraits to provide a unified description for both location list and range list DWARF entry kinds.
1 parent e2ad554 commit 4613ca9

File tree

1 file changed

+76
-40
lines changed

1 file changed

+76
-40
lines changed

llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp

Lines changed: 76 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3290,30 +3290,68 @@ static MCSymbol *emitLoclistsTableHeader(AsmPrinter *Asm,
32903290
return TableEnd;
32913291
}
32923292

3293-
template <typename Ranges, typename PayloadEmitter>
3294-
static void emitRangeList(
3295-
DwarfDebug &DD, AsmPrinter *Asm, MCSymbol *Sym, const Ranges &R,
3296-
const DwarfCompileUnit &CU, unsigned BaseAddressx, unsigned OffsetPair,
3297-
unsigned StartxLength, unsigned EndOfList,
3298-
StringRef (*StringifyEnum)(unsigned),
3299-
bool ShouldUseBaseAddress,
3300-
PayloadEmitter EmitPayload) {
3293+
namespace {
3294+
3295+
struct DebugLocSpanList {
3296+
MCSymbol *Label;
3297+
const DwarfCompileUnit *CU;
3298+
llvm::ArrayRef<llvm::DebugLocStream::Entry> Ranges;
3299+
};
3300+
3301+
template <typename DWARFSpanList> struct DwarfRangeListTraits {};
3302+
3303+
template <> struct DwarfRangeListTraits<DebugLocSpanList> {
3304+
static constexpr unsigned BaseAddressx = dwarf::DW_LLE_base_addressx;
3305+
static constexpr unsigned OffsetPair = dwarf::DW_LLE_offset_pair;
3306+
static constexpr unsigned StartxLength = dwarf::DW_LLE_startx_length;
3307+
static constexpr unsigned EndOfList = dwarf::DW_LLE_end_of_list;
3308+
3309+
static StringRef StringifyRangeKind(unsigned Encoding) {
3310+
return llvm::dwarf::LocListEncodingString(Encoding);
3311+
}
3312+
3313+
static void EmitPayload(DwarfDebug &DD, const DwarfCompileUnit *CU,
3314+
const llvm::DebugLocStream::Entry &E) {
3315+
DD.emitDebugLocEntryLocation(E, CU);
3316+
}
3317+
};
3318+
3319+
template <> struct DwarfRangeListTraits<RangeSpanList> {
3320+
static constexpr unsigned BaseAddressx = dwarf::DW_RLE_base_addressx;
3321+
static constexpr unsigned OffsetPair = dwarf::DW_RLE_offset_pair;
3322+
static constexpr unsigned StartxLength = dwarf::DW_RLE_startx_length;
3323+
static constexpr unsigned EndOfList = dwarf::DW_RLE_end_of_list;
3324+
3325+
static StringRef StringifyRangeKind(unsigned Encoding) {
3326+
return llvm::dwarf::RangeListEncodingString(Encoding);
3327+
}
3328+
3329+
static void EmitPayload(DwarfDebug &DD, const DwarfCompileUnit *CU,
3330+
const RangeSpan &E) {}
3331+
};
3332+
3333+
} // namespace
3334+
3335+
template <typename Ranges>
3336+
static void emitRangeList(DwarfDebug &DD, AsmPrinter *Asm, const Ranges &R,
3337+
bool ShouldUseBaseAddress) {
33013338

33023339
auto Size = Asm->MAI->getCodePointerSize();
33033340
bool UseDwarf5 = DD.getDwarfVersion() >= 5;
33043341

33053342
// Emit our symbol so we can find the beginning of the range.
3306-
Asm->OutStreamer->emitLabel(Sym);
3343+
Asm->OutStreamer->emitLabel(R.Label);
33073344

33083345
// Gather all the ranges that apply to the same section so they can share
33093346
// a base address entry.
3310-
SmallMapVector<const MCSection *, std::vector<decltype(&*R.begin())>, 16>
3347+
SmallMapVector<const MCSection *, std::vector<decltype(&*R.Ranges.begin())>,
3348+
16>
33113349
SectionRanges;
33123350

3313-
for (const auto &Range : R)
3351+
for (const auto &Range : R.Ranges)
33143352
SectionRanges[&Range.Begin->getSection()].push_back(&Range);
33153353

3316-
const MCSymbol *CUBase = CU.getBaseAddress();
3354+
const MCSymbol *CUBase = R.CU->getBaseAddress();
33173355
bool BaseIsSet = false;
33183356
for (const auto &P : SectionRanges) {
33193357
auto *Base = CUBase;
@@ -3342,8 +3380,10 @@ static void emitRangeList(
33423380
// * or, there's more than one entry to share the base address
33433381
Base = NewBase;
33443382
BaseIsSet = true;
3345-
Asm->OutStreamer->AddComment(StringifyEnum(BaseAddressx));
3346-
Asm->emitInt8(BaseAddressx);
3383+
Asm->OutStreamer->AddComment(
3384+
DwarfRangeListTraits<Ranges>::StringifyRangeKind(
3385+
DwarfRangeListTraits<Ranges>::BaseAddressx));
3386+
Asm->emitInt8(DwarfRangeListTraits<Ranges>::BaseAddressx);
33473387
Asm->OutStreamer->AddComment(" base address index");
33483388
Asm->emitULEB128(DD.getAddressPool().getIndex(Base));
33493389
}
@@ -3362,8 +3402,10 @@ static void emitRangeList(
33623402
if (Base) {
33633403
if (UseDwarf5) {
33643404
// Emit offset_pair when we have a base.
3365-
Asm->OutStreamer->AddComment(StringifyEnum(OffsetPair));
3366-
Asm->emitInt8(OffsetPair);
3405+
Asm->OutStreamer->AddComment(
3406+
DwarfRangeListTraits<Ranges>::StringifyRangeKind(
3407+
DwarfRangeListTraits<Ranges>::OffsetPair));
3408+
Asm->emitInt8(DwarfRangeListTraits<Ranges>::OffsetPair);
33673409
Asm->OutStreamer->AddComment(" starting offset");
33683410
Asm->emitLabelDifferenceAsULEB128(Begin, Base);
33693411
Asm->OutStreamer->AddComment(" ending offset");
@@ -3373,8 +3415,10 @@ static void emitRangeList(
33733415
Asm->emitLabelDifference(End, Base, Size);
33743416
}
33753417
} else if (UseDwarf5) {
3376-
Asm->OutStreamer->AddComment(StringifyEnum(StartxLength));
3377-
Asm->emitInt8(StartxLength);
3418+
Asm->OutStreamer->AddComment(
3419+
DwarfRangeListTraits<Ranges>::StringifyRangeKind(
3420+
DwarfRangeListTraits<Ranges>::StartxLength));
3421+
Asm->emitInt8(DwarfRangeListTraits<Ranges>::StartxLength);
33783422
Asm->OutStreamer->AddComment(" start index");
33793423
Asm->emitULEB128(DD.getAddressPool().getIndex(Begin));
33803424
Asm->OutStreamer->AddComment(" length");
@@ -3383,13 +3427,15 @@ static void emitRangeList(
33833427
Asm->OutStreamer->emitSymbolValue(Begin, Size);
33843428
Asm->OutStreamer->emitSymbolValue(End, Size);
33853429
}
3386-
EmitPayload(*RS);
3430+
DwarfRangeListTraits<Ranges>::EmitPayload(DD, R.CU, *RS);
33873431
}
33883432
}
33893433

33903434
if (UseDwarf5) {
3391-
Asm->OutStreamer->AddComment(StringifyEnum(EndOfList));
3392-
Asm->emitInt8(EndOfList);
3435+
Asm->OutStreamer->AddComment(
3436+
DwarfRangeListTraits<Ranges>::StringifyRangeKind(
3437+
DwarfRangeListTraits<Ranges>::EndOfList));
3438+
Asm->emitInt8(DwarfRangeListTraits<Ranges>::EndOfList);
33933439
} else {
33943440
// Terminate the list with two 0 values.
33953441
Asm->OutStreamer->emitIntValue(0, Size);
@@ -3399,14 +3445,9 @@ static void emitRangeList(
33993445

34003446
// Handles emission of both debug_loclist / debug_loclist.dwo
34013447
static void emitLocList(DwarfDebug &DD, AsmPrinter *Asm, const DebugLocStream::List &List) {
3402-
emitRangeList(DD, Asm, List.Label, DD.getDebugLocs().getEntries(List),
3403-
*List.CU, dwarf::DW_LLE_base_addressx,
3404-
dwarf::DW_LLE_offset_pair, dwarf::DW_LLE_startx_length,
3405-
dwarf::DW_LLE_end_of_list, llvm::dwarf::LocListEncodingString,
3406-
/* ShouldUseBaseAddress */ true,
3407-
[&](const DebugLocStream::Entry &E) {
3408-
DD.emitDebugLocEntryLocation(E, List.CU);
3409-
});
3448+
DebugLocSpanList Ranges = {List.Label, List.CU,
3449+
DD.getDebugLocs().getEntries(List)};
3450+
emitRangeList(DD, Asm, Ranges, /* ShouldUseBaseAddress */ true);
34103451
}
34113452

34123453
void DwarfDebug::emitDebugLocImpl(MCSection *Sec) {
@@ -3428,10 +3469,9 @@ void DwarfDebug::emitDebugLocImpl(MCSection *Sec) {
34283469

34293470
// Emit locations into the .debug_loc/.debug_loclists section.
34303471
void DwarfDebug::emitDebugLoc() {
3431-
emitDebugLocImpl(
3432-
getDwarfVersion() >= 5
3433-
? Asm->getObjFileLowering().getDwarfLoclistsSection()
3434-
: Asm->getObjFileLowering().getDwarfLocSection());
3472+
emitDebugLocImpl(getDwarfVersion() >= 5
3473+
? Asm->getObjFileLowering().getDwarfLoclistsSection()
3474+
: Asm->getObjFileLowering().getDwarfLocSection());
34353475
}
34363476

34373477
// Emit locations into the .debug_loc.dwo/.debug_loclists.dwo section.
@@ -3626,13 +3666,9 @@ void DwarfDebug::emitDebugARanges() {
36263666
/// Emit a single range list. We handle both DWARF v5 and earlier.
36273667
static void emitRangeList(DwarfDebug &DD, AsmPrinter *Asm,
36283668
const RangeSpanList &List) {
3629-
emitRangeList(DD, Asm, List.Label, List.Ranges, *List.CU,
3630-
dwarf::DW_RLE_base_addressx, dwarf::DW_RLE_offset_pair,
3631-
dwarf::DW_RLE_startx_length, dwarf::DW_RLE_end_of_list,
3632-
llvm::dwarf::RangeListEncodingString,
3633-
List.CU->getCUNode()->getRangesBaseAddress() ||
3634-
DD.getDwarfVersion() >= 5,
3635-
[](auto) {});
3669+
bool ShouldUseBaseAddress =
3670+
List.CU->getCUNode()->getRangesBaseAddress() || DD.getDwarfVersion() >= 5;
3671+
emitRangeList(DD, Asm, List, ShouldUseBaseAddress);
36363672
}
36373673

36383674
void DwarfDebug::emitDebugRangesImpl(const DwarfFile &Holder, MCSection *Section) {

0 commit comments

Comments
 (0)