1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-23 11:13:28 +01:00

[llvm-dwp] Refactor handling of section identifiers. NFCI.

There is a number of places in llvm-dwp.cpp where a section identifier
is translated into an index of an internal array of section
contributions, and another place where the index is converted to an
on-disk value. All these places use direct expressions like
"<id> - DW_SECT_INFO" or "<index> + DW_SECT_INFO", exploiting the fact
that DW_SECT_INFO is the minimum valid value of that kind.

The patch adds distinct functions for that translation. The goal is to
make the code more readable and to prepare it to support index sections
of new versions, where the numeric scheme of section indexes is changed.

Differential Revision: https://reviews.llvm.org/D76067
This commit is contained in:
Igor Kudrin 2020-03-11 21:54:51 +07:00
parent 15766ad4d8
commit ac084dff67

View File

@ -216,6 +216,19 @@ struct UnitIndexEntry {
StringRef DWPName; StringRef DWPName;
}; };
// Convert a section identifier into the index to use with
// UnitIndexEntry::Contributions.
static unsigned getContributionIndex(DWARFSectionKind Kind) {
assert(Kind >= DW_SECT_INFO);
return Kind - DW_SECT_INFO;
}
// Convert a UnitIndexEntry::Contributions index to the corresponding on-disk
// value of the section identifier.
static unsigned getOnDiskSectionId(unsigned Index) {
return Index + DW_SECT_INFO;
}
static StringRef getSubsection(StringRef Section, static StringRef getSubsection(StringRef Section,
const DWARFUnitIndex::Entry &Entry, const DWARFUnitIndex::Entry &Entry,
DWARFSectionKind Kind) { DWARFSectionKind Kind) {
@ -241,15 +254,15 @@ static void addAllTypesFromDWP(
// Zero out the debug_info contribution // Zero out the debug_info contribution
Entry.Contributions[0] = {}; Entry.Contributions[0] = {};
for (auto Kind : TUIndex.getColumnKinds()) { for (auto Kind : TUIndex.getColumnKinds()) {
auto &C = Entry.Contributions[Kind - DW_SECT_INFO]; auto &C = Entry.Contributions[getContributionIndex(Kind)];
C.Offset += I->Offset; C.Offset += I->Offset;
C.Length = I->Length; C.Length = I->Length;
++I; ++I;
} }
auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO]; unsigned TypesIndex = getContributionIndex(DW_SECT_TYPES);
auto &C = Entry.Contributions[TypesIndex];
Out.emitBytes(Types.substr( Out.emitBytes(Types.substr(
C.Offset - TUEntry.Contributions[DW_SECT_TYPES - DW_SECT_INFO].Offset, C.Offset - TUEntry.Contributions[TypesIndex].Offset, C.Length));
C.Length));
C.Offset = TypesOffset; C.Offset = TypesOffset;
TypesOffset += C.Length; TypesOffset += C.Length;
} }
@ -268,7 +281,7 @@ static void addAllTypes(MCStreamer &Out,
UnitIndexEntry Entry = CUEntry; UnitIndexEntry Entry = CUEntry;
// Zero out the debug_info contribution // Zero out the debug_info contribution
Entry.Contributions[0] = {}; Entry.Contributions[0] = {};
auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO]; auto &C = Entry.Contributions[getContributionIndex(DW_SECT_TYPES)];
C.Offset = TypesOffset; C.Offset = TypesOffset;
auto PrevOffset = Offset; auto PrevOffset = Offset;
// Length of the unit, including the 4 byte length field. // Length of the unit, including the 4 byte length field.
@ -345,7 +358,7 @@ writeIndex(MCStreamer &Out, MCSection *Section,
// Write the column headers (which sections will appear in the table) // Write the column headers (which sections will appear in the table)
for (size_t i = 0; i != ContributionOffsets.size(); ++i) for (size_t i = 0; i != ContributionOffsets.size(); ++i)
if (ContributionOffsets[i]) if (ContributionOffsets[i])
Out.emitIntValue(i + DW_SECT_INFO, 4); Out.emitIntValue(getOnDiskSectionId(i), 4);
// Write the offsets. // Write the offsets.
writeIndexTable(Out, ContributionOffsets, IndexEntries, writeIndexTable(Out, ContributionOffsets, IndexEntries,
@ -438,7 +451,7 @@ static Error handleSection(
return Error::success(); return Error::success();
if (DWARFSectionKind Kind = SectionPair->second.second) { if (DWARFSectionKind Kind = SectionPair->second.second) {
auto Index = Kind - DW_SECT_INFO; auto Index = getContributionIndex(Kind);
if (Kind != DW_SECT_TYPES) { if (Kind != DW_SECT_TYPES) {
CurEntry.Contributions[Index].Offset = ContributionOffsets[Index]; CurEntry.Contributions[Index].Offset = ContributionOffsets[Index];
ContributionOffsets[Index] += ContributionOffsets[Index] +=
@ -589,7 +602,8 @@ static Error write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
P.first->second.Name = ID.Name; P.first->second.Name = ID.Name;
P.first->second.DWOName = ID.DWOName; P.first->second.DWOName = ID.DWOName;
addAllTypes(Out, TypeIndexEntries, TypesSection, CurTypesSection, addAllTypes(Out, TypeIndexEntries, TypesSection, CurTypesSection,
CurEntry, ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]); CurEntry,
ContributionOffsets[getContributionIndex(DW_SECT_TYPES)]);
continue; continue;
} }
@ -618,7 +632,7 @@ static Error write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
NewEntry.DWOName = ID.DWOName; NewEntry.DWOName = ID.DWOName;
NewEntry.DWPName = Input; NewEntry.DWPName = Input;
for (auto Kind : CUIndex.getColumnKinds()) { for (auto Kind : CUIndex.getColumnKinds()) {
auto &C = NewEntry.Contributions[Kind - DW_SECT_INFO]; auto &C = NewEntry.Contributions[getContributionIndex(Kind)];
C.Offset += I->Offset; C.Offset += I->Offset;
C.Length = I->Length; C.Length = I->Length;
++I; ++I;
@ -632,9 +646,9 @@ static Error write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
DataExtractor TUIndexData(CurTUIndexSection, Obj.isLittleEndian(), 0); DataExtractor TUIndexData(CurTUIndexSection, Obj.isLittleEndian(), 0);
if (!TUIndex.parse(TUIndexData)) if (!TUIndex.parse(TUIndexData))
return make_error<DWPError>("failed to parse tu_index"); return make_error<DWPError>("failed to parse tu_index");
addAllTypesFromDWP(Out, TypeIndexEntries, TUIndex, TypesSection, addAllTypesFromDWP(
CurTypesSection.front(), CurEntry, Out, TypeIndexEntries, TUIndex, TypesSection, CurTypesSection.front(),
ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]); CurEntry, ContributionOffsets[getContributionIndex(DW_SECT_TYPES)]);
} }
} }
@ -645,7 +659,7 @@ static Error write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
TypeIndexEntries); TypeIndexEntries);
// Lie about the type contribution // Lie about the type contribution
ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO] = 0; ContributionOffsets[getContributionIndex(DW_SECT_TYPES)] = 0;
// Unlie about the info contribution // Unlie about the info contribution
ContributionOffsets[0] = 1; ContributionOffsets[0] = 1;