xref: /netbsd-src/external/apache2/llvm/dist/llvm/lib/CodeGen/AsmPrinter/AddressPool.cpp (revision 82d56013d7b633d116a93943de88e08335357a7c)
17330f729Sjoerg //===- llvm/CodeGen/AddressPool.cpp - Dwarf Debug Framework ---------------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg 
97330f729Sjoerg #include "AddressPool.h"
107330f729Sjoerg #include "llvm/ADT/SmallVector.h"
117330f729Sjoerg #include "llvm/CodeGen/AsmPrinter.h"
127330f729Sjoerg #include "llvm/IR/DataLayout.h"
137330f729Sjoerg #include "llvm/MC/MCStreamer.h"
147330f729Sjoerg #include "llvm/Target/TargetLoweringObjectFile.h"
157330f729Sjoerg #include <utility>
167330f729Sjoerg 
177330f729Sjoerg using namespace llvm;
187330f729Sjoerg 
getIndex(const MCSymbol * Sym,bool TLS)197330f729Sjoerg unsigned AddressPool::getIndex(const MCSymbol *Sym, bool TLS) {
207330f729Sjoerg   HasBeenUsed = true;
217330f729Sjoerg   auto IterBool =
227330f729Sjoerg       Pool.insert(std::make_pair(Sym, AddressPoolEntry(Pool.size(), TLS)));
237330f729Sjoerg   return IterBool.first->second.Number;
247330f729Sjoerg }
257330f729Sjoerg 
emitHeader(AsmPrinter & Asm,MCSection * Section)267330f729Sjoerg MCSymbol *AddressPool::emitHeader(AsmPrinter &Asm, MCSection *Section) {
277330f729Sjoerg   static const uint8_t AddrSize = Asm.getDataLayout().getPointerSize();
287330f729Sjoerg 
29*82d56013Sjoerg   MCSymbol *EndLabel =
30*82d56013Sjoerg       Asm.emitDwarfUnitLength("debug_addr", "Length of contribution");
317330f729Sjoerg   Asm.OutStreamer->AddComment("DWARF version number");
327330f729Sjoerg   Asm.emitInt16(Asm.getDwarfVersion());
337330f729Sjoerg   Asm.OutStreamer->AddComment("Address size");
347330f729Sjoerg   Asm.emitInt8(AddrSize);
357330f729Sjoerg   Asm.OutStreamer->AddComment("Segment selector size");
367330f729Sjoerg   Asm.emitInt8(0); // TODO: Support non-zero segment_selector_size.
377330f729Sjoerg 
387330f729Sjoerg   return EndLabel;
397330f729Sjoerg }
407330f729Sjoerg 
417330f729Sjoerg // Emit addresses into the section given.
emit(AsmPrinter & Asm,MCSection * AddrSection)427330f729Sjoerg void AddressPool::emit(AsmPrinter &Asm, MCSection *AddrSection) {
437330f729Sjoerg   if (isEmpty())
447330f729Sjoerg     return;
457330f729Sjoerg 
467330f729Sjoerg   // Start the dwarf addr section.
477330f729Sjoerg   Asm.OutStreamer->SwitchSection(AddrSection);
487330f729Sjoerg 
497330f729Sjoerg   MCSymbol *EndLabel = nullptr;
507330f729Sjoerg 
517330f729Sjoerg   if (Asm.getDwarfVersion() >= 5)
527330f729Sjoerg     EndLabel = emitHeader(Asm, AddrSection);
537330f729Sjoerg 
547330f729Sjoerg   // Define the symbol that marks the start of the contribution.
557330f729Sjoerg   // It is referenced via DW_AT_addr_base.
56*82d56013Sjoerg   Asm.OutStreamer->emitLabel(AddressTableBaseSym);
577330f729Sjoerg 
587330f729Sjoerg   // Order the address pool entries by ID
597330f729Sjoerg   SmallVector<const MCExpr *, 64> Entries(Pool.size());
607330f729Sjoerg 
617330f729Sjoerg   for (const auto &I : Pool)
627330f729Sjoerg     Entries[I.second.Number] =
637330f729Sjoerg         I.second.TLS
647330f729Sjoerg             ? Asm.getObjFileLowering().getDebugThreadLocalSymbol(I.first)
657330f729Sjoerg             : MCSymbolRefExpr::create(I.first, Asm.OutContext);
667330f729Sjoerg 
677330f729Sjoerg   for (const MCExpr *Entry : Entries)
68*82d56013Sjoerg     Asm.OutStreamer->emitValue(Entry, Asm.getDataLayout().getPointerSize());
697330f729Sjoerg 
707330f729Sjoerg   if (EndLabel)
71*82d56013Sjoerg     Asm.OutStreamer->emitLabel(EndLabel);
727330f729Sjoerg }
73