16e07bfd0SEugene Zelenko //===- llvm/CodeGen/AddressPool.cpp - Dwarf Debug Framework ---------------===//
269d0cf06SDavid Blaikie //
369d0cf06SDavid Blaikie //                     The LLVM Compiler Infrastructure
469d0cf06SDavid Blaikie //
569d0cf06SDavid Blaikie // This file is distributed under the University of Illinois Open Source
669d0cf06SDavid Blaikie // License. See LICENSE.TXT for details.
769d0cf06SDavid Blaikie //
869d0cf06SDavid Blaikie //===----------------------------------------------------------------------===//
9e226b08eSDavid Blaikie 
10e226b08eSDavid Blaikie #include "AddressPool.h"
116e07bfd0SEugene Zelenko #include "llvm/ADT/SmallVector.h"
12e226b08eSDavid Blaikie #include "llvm/CodeGen/AsmPrinter.h"
136e07bfd0SEugene Zelenko #include "llvm/IR/DataLayout.h"
14e226b08eSDavid Blaikie #include "llvm/MC/MCStreamer.h"
156054e650SDavid Blaikie #include "llvm/Target/TargetLoweringObjectFile.h"
166e07bfd0SEugene Zelenko #include <utility>
17e226b08eSDavid Blaikie 
18e226b08eSDavid Blaikie using namespace llvm;
19e226b08eSDavid Blaikie 
20e226b08eSDavid Blaikie unsigned AddressPool::getIndex(const MCSymbol *Sym, bool TLS) {
21e12b49a6SDavid Blaikie   HasBeenUsed = true;
22e226b08eSDavid Blaikie   auto IterBool =
23e226b08eSDavid Blaikie       Pool.insert(std::make_pair(Sym, AddressPoolEntry(Pool.size(), TLS)));
24e226b08eSDavid Blaikie   return IterBool.first->second.Number;
25e226b08eSDavid Blaikie }
26e226b08eSDavid Blaikie 
2764e0c567SVictor Leschuk 
2864e0c567SVictor Leschuk void AddressPool::emitHeader(AsmPrinter &Asm, MCSection *Section) {
2964e0c567SVictor Leschuk   static const uint8_t AddrSize = Asm.getDataLayout().getPointerSize();
3064e0c567SVictor Leschuk   uint64_t Length = sizeof(uint16_t) // version
3164e0c567SVictor Leschuk                   + sizeof(uint8_t)  // address_size
3264e0c567SVictor Leschuk                   + sizeof(uint8_t)  // segment_selector_size
3364e0c567SVictor Leschuk                   + AddrSize * Pool.size(); // entries
34*d671eb7eSDavid Blaikie   Asm.OutStreamer->AddComment("Length of contribution");
3564e0c567SVictor Leschuk   Asm.emitInt32(Length); // TODO: Support DWARF64 format.
36*d671eb7eSDavid Blaikie   Asm.OutStreamer->AddComment("DWARF version number");
3764e0c567SVictor Leschuk   Asm.emitInt16(Asm.getDwarfVersion());
38*d671eb7eSDavid Blaikie   Asm.OutStreamer->AddComment("Address size");
3964e0c567SVictor Leschuk   Asm.emitInt8(AddrSize);
40*d671eb7eSDavid Blaikie   Asm.OutStreamer->AddComment("Segment selector size");
4164e0c567SVictor Leschuk   Asm.emitInt8(0); // TODO: Support non-zero segment_selector_size.
4264e0c567SVictor Leschuk }
4364e0c567SVictor Leschuk 
44e226b08eSDavid Blaikie // Emit addresses into the section given.
450709a7bdSRafael Espindola void AddressPool::emit(AsmPrinter &Asm, MCSection *AddrSection) {
46161dd3c1SDavid Blaikie   if (isEmpty())
47161dd3c1SDavid Blaikie     return;
48161dd3c1SDavid Blaikie 
49425f7517SGeorge Rimar   // Start the dwarf addr section.
50425f7517SGeorge Rimar   Asm.OutStreamer->SwitchSection(AddrSection);
51425f7517SGeorge Rimar 
5264e0c567SVictor Leschuk   if (Asm.getDwarfVersion() >= 5)
5364e0c567SVictor Leschuk     emitHeader(Asm, AddrSection);
5464e0c567SVictor Leschuk 
55425f7517SGeorge Rimar   // Define the symbol that marks the start of the contribution.
56425f7517SGeorge Rimar   // It is referenced via DW_AT_addr_base.
57425f7517SGeorge Rimar   Asm.OutStreamer->EmitLabel(AddressTableBaseSym);
58425f7517SGeorge Rimar 
59e226b08eSDavid Blaikie   // Order the address pool entries by ID
60e226b08eSDavid Blaikie   SmallVector<const MCExpr *, 64> Entries(Pool.size());
61e226b08eSDavid Blaikie 
62e226b08eSDavid Blaikie   for (const auto &I : Pool)
63e226b08eSDavid Blaikie     Entries[I.second.Number] =
64e226b08eSDavid Blaikie         I.second.TLS
65e226b08eSDavid Blaikie             ? Asm.getObjFileLowering().getDebugThreadLocalSymbol(I.first)
6613760bd1SJim Grosbach             : MCSymbolRefExpr::create(I.first, Asm.OutContext);
67e226b08eSDavid Blaikie 
68e226b08eSDavid Blaikie   for (const MCExpr *Entry : Entries)
699ff69c8fSLang Hames     Asm.OutStreamer->EmitValue(Entry, Asm.getDataLayout().getPointerSize());
70e226b08eSDavid Blaikie }
71