From 0b57cec536236d46e3dba9bd041533462f33dbb7 Mon Sep 17 00:00:00 2001 From: Dimitry Andric Date: Fri, 20 Dec 2019 19:53:05 +0000 Subject: Move all sources from the llvm project into contrib/llvm-project. This uses the new layout of the upstream repository, which was recently migrated to GitHub, and converted into a "monorepo". That is, most of the earlier separate sub-projects with their own branches and tags were consolidated into one top-level directory, and are now branched and tagged together. Updating the vendor area to match this layout is next. --- .../lib/CodeGen/AsmPrinter/DwarfStringPool.cpp | 130 +++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/DwarfStringPool.cpp (limited to 'contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/DwarfStringPool.cpp') diff --git a/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/DwarfStringPool.cpp b/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/DwarfStringPool.cpp new file mode 100644 index 000000000000..2a76dcb1b082 --- /dev/null +++ b/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/DwarfStringPool.cpp @@ -0,0 +1,130 @@ +//===- llvm/CodeGen/DwarfStringPool.cpp - Dwarf Debug Framework -----------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "DwarfStringPool.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/Twine.h" +#include "llvm/CodeGen/AsmPrinter.h" +#include "llvm/MC/MCAsmInfo.h" +#include "llvm/MC/MCStreamer.h" +#include +#include + +using namespace llvm; + +DwarfStringPool::DwarfStringPool(BumpPtrAllocator &A, AsmPrinter &Asm, + StringRef Prefix) + : Pool(A), Prefix(Prefix), + ShouldCreateSymbols(Asm.MAI->doesDwarfUseRelocationsAcrossSections()) {} + +StringMapEntry & +DwarfStringPool::getEntryImpl(AsmPrinter &Asm, StringRef Str) { + auto I = Pool.insert(std::make_pair(Str, EntryTy())); + auto &Entry = I.first->second; + if (I.second) { + Entry.Index = EntryTy::NotIndexed; + Entry.Offset = NumBytes; + Entry.Symbol = ShouldCreateSymbols ? Asm.createTempSymbol(Prefix) : nullptr; + + NumBytes += Str.size() + 1; + assert(NumBytes > Entry.Offset && "Unexpected overflow"); + } + return *I.first; +} + +DwarfStringPool::EntryRef DwarfStringPool::getEntry(AsmPrinter &Asm, + StringRef Str) { + auto &MapEntry = getEntryImpl(Asm, Str); + return EntryRef(MapEntry, false); +} + +DwarfStringPool::EntryRef DwarfStringPool::getIndexedEntry(AsmPrinter &Asm, + StringRef Str) { + auto &MapEntry = getEntryImpl(Asm, Str); + if (!MapEntry.getValue().isIndexed()) + MapEntry.getValue().Index = NumIndexedStrings++; + return EntryRef(MapEntry, true); +} + +void DwarfStringPool::emitStringOffsetsTableHeader(AsmPrinter &Asm, + MCSection *Section, + MCSymbol *StartSym) { + if (getNumIndexedStrings() == 0) + return; + Asm.OutStreamer->SwitchSection(Section); + unsigned EntrySize = 4; + // FIXME: DWARF64 + // We are emitting the header for a contribution to the string offsets + // table. The header consists of an entry with the contribution's + // size (not including the size of the length field), the DWARF version and + // 2 bytes of padding. + Asm.emitInt32(getNumIndexedStrings() * EntrySize + 4); + Asm.emitInt16(Asm.getDwarfVersion()); + Asm.emitInt16(0); + // Define the symbol that marks the start of the contribution. It is + // referenced by most unit headers via DW_AT_str_offsets_base. + // Split units do not use the attribute. + if (StartSym) + Asm.OutStreamer->EmitLabel(StartSym); +} + +void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection, + MCSection *OffsetSection, bool UseRelativeOffsets) { + if (Pool.empty()) + return; + + // Start the dwarf str section. + Asm.OutStreamer->SwitchSection(StrSection); + + // Get all of the string pool entries and sort them by their offset. + SmallVector *, 64> Entries; + Entries.reserve(Pool.size()); + + for (const auto &E : Pool) + Entries.push_back(&E); + + llvm::sort(Entries, [](const StringMapEntry *A, + const StringMapEntry *B) { + return A->getValue().Offset < B->getValue().Offset; + }); + + for (const auto &Entry : Entries) { + assert(ShouldCreateSymbols == static_cast(Entry->getValue().Symbol) && + "Mismatch between setting and entry"); + + // Emit a label for reference from debug information entries. + if (ShouldCreateSymbols) + Asm.OutStreamer->EmitLabel(Entry->getValue().Symbol); + + // Emit the string itself with a terminating null byte. + Asm.OutStreamer->AddComment("string offset=" + + Twine(Entry->getValue().Offset)); + Asm.OutStreamer->EmitBytes( + StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1)); + } + + // If we've got an offset section go ahead and emit that now as well. + if (OffsetSection) { + // Now only take the indexed entries and put them in an array by their ID so + // we can emit them in order. + Entries.resize(NumIndexedStrings); + for (const auto &Entry : Pool) { + if (Entry.getValue().isIndexed()) + Entries[Entry.getValue().Index] = &Entry; + } + + Asm.OutStreamer->SwitchSection(OffsetSection); + unsigned size = 4; // FIXME: DWARF64 is 8. + for (const auto &Entry : Entries) + if (UseRelativeOffsets) + Asm.emitDwarfStringOffset(Entry->getValue()); + else + Asm.OutStreamer->EmitIntValue(Entry->getValue().Offset, size); + } +} -- cgit v1.2.3