16e07bfd0SEugene Zelenko //===- llvm/CodeGen/AddressPool.cpp - Dwarf Debug Framework ---------------===//
269d0cf06SDavid Blaikie //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
669d0cf06SDavid Blaikie //
769d0cf06SDavid Blaikie //===----------------------------------------------------------------------===//
8e226b08eSDavid Blaikie
9e226b08eSDavid Blaikie #include "AddressPool.h"
106e07bfd0SEugene Zelenko #include "llvm/ADT/SmallVector.h"
11e226b08eSDavid Blaikie #include "llvm/CodeGen/AsmPrinter.h"
126e07bfd0SEugene Zelenko #include "llvm/IR/DataLayout.h"
13e226b08eSDavid Blaikie #include "llvm/MC/MCStreamer.h"
146054e650SDavid Blaikie #include "llvm/Target/TargetLoweringObjectFile.h"
156e07bfd0SEugene Zelenko #include <utility>
16e226b08eSDavid Blaikie
17e226b08eSDavid Blaikie using namespace llvm;
18e226b08eSDavid Blaikie
getIndex(const MCSymbol * Sym,bool TLS)19e226b08eSDavid Blaikie unsigned AddressPool::getIndex(const MCSymbol *Sym, bool TLS) {
20a5032b26SDavid Blaikie resetUsedFlag(true);
21e226b08eSDavid Blaikie auto IterBool =
22e226b08eSDavid Blaikie Pool.insert(std::make_pair(Sym, AddressPoolEntry(Pool.size(), TLS)));
23e226b08eSDavid Blaikie return IterBool.first->second.Number;
24e226b08eSDavid Blaikie }
25e226b08eSDavid Blaikie
emitHeader(AsmPrinter & Asm,MCSection * Section)267b585673SDavid Blaikie MCSymbol *AddressPool::emitHeader(AsmPrinter &Asm, MCSection *Section) {
2764e0c567SVictor Leschuk static const uint8_t AddrSize = Asm.getDataLayout().getPointerSize();
287b585673SDavid Blaikie
29d39bc36bSChen Zheng MCSymbol *EndLabel =
30d39bc36bSChen Zheng Asm.emitDwarfUnitLength("debug_addr", "Length of contribution");
31d671eb7eSDavid Blaikie Asm.OutStreamer->AddComment("DWARF version number");
3264e0c567SVictor Leschuk Asm.emitInt16(Asm.getDwarfVersion());
33d671eb7eSDavid Blaikie Asm.OutStreamer->AddComment("Address size");
3464e0c567SVictor Leschuk Asm.emitInt8(AddrSize);
35d671eb7eSDavid Blaikie Asm.OutStreamer->AddComment("Segment selector size");
3664e0c567SVictor Leschuk Asm.emitInt8(0); // TODO: Support non-zero segment_selector_size.
377b585673SDavid Blaikie
387b585673SDavid Blaikie return EndLabel;
3964e0c567SVictor Leschuk }
4064e0c567SVictor Leschuk
41e226b08eSDavid Blaikie // Emit addresses into the section given.
emit(AsmPrinter & Asm,MCSection * AddrSection)420709a7bdSRafael Espindola void AddressPool::emit(AsmPrinter &Asm, MCSection *AddrSection) {
43161dd3c1SDavid Blaikie if (isEmpty())
44161dd3c1SDavid Blaikie return;
45161dd3c1SDavid Blaikie
46425f7517SGeorge Rimar // Start the dwarf addr section.
47*adf4142fSFangrui Song Asm.OutStreamer->switchSection(AddrSection);
48425f7517SGeorge Rimar
497b585673SDavid Blaikie MCSymbol *EndLabel = nullptr;
507b585673SDavid Blaikie
5164e0c567SVictor Leschuk if (Asm.getDwarfVersion() >= 5)
527b585673SDavid Blaikie EndLabel = emitHeader(Asm, AddrSection);
5364e0c567SVictor Leschuk
54425f7517SGeorge Rimar // Define the symbol that marks the start of the contribution.
55425f7517SGeorge Rimar // It is referenced via DW_AT_addr_base.
566d2d589bSFangrui Song Asm.OutStreamer->emitLabel(AddressTableBaseSym);
57425f7517SGeorge Rimar
58e226b08eSDavid Blaikie // Order the address pool entries by ID
59e226b08eSDavid Blaikie SmallVector<const MCExpr *, 64> Entries(Pool.size());
60e226b08eSDavid Blaikie
61e226b08eSDavid Blaikie for (const auto &I : Pool)
62e226b08eSDavid Blaikie Entries[I.second.Number] =
63e226b08eSDavid Blaikie I.second.TLS
64e226b08eSDavid Blaikie ? Asm.getObjFileLowering().getDebugThreadLocalSymbol(I.first)
6513760bd1SJim Grosbach : MCSymbolRefExpr::create(I.first, Asm.OutContext);
66e226b08eSDavid Blaikie
67e226b08eSDavid Blaikie for (const MCExpr *Entry : Entries)
6877497103SFangrui Song Asm.OutStreamer->emitValue(Entry, Asm.getDataLayout().getPointerSize());
697b585673SDavid Blaikie
707b585673SDavid Blaikie if (EndLabel)
716d2d589bSFangrui Song Asm.OutStreamer->emitLabel(EndLabel);
72e226b08eSDavid Blaikie }
73