#include "AddressPool.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/CodeGen/AsmPrinter.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/Target/TargetLoweringObjectFile.h"
#include <utility>
using namespace llvm;
unsigned AddressPool::getIndex(const MCSymbol *Sym, bool TLS) {
HasBeenUsed = true;
auto IterBool =
Pool.insert(std::make_pair(Sym, AddressPoolEntry(Pool.size(), TLS)));
return IterBool.first->second.Number;
}
void AddressPool::emitHeader(AsmPrinter &Asm, MCSection *Section) {
static const uint8_t AddrSize = Asm.getDataLayout().getPointerSize();
Asm.OutStreamer->SwitchSection(Section);
uint64_t Length = sizeof(uint16_t)
+ sizeof(uint8_t)
+ sizeof(uint8_t)
+ AddrSize * Pool.size();
Asm.emitInt32(Length);
Asm.emitInt16(Asm.getDwarfVersion());
Asm.emitInt8(AddrSize);
Asm.emitInt8(0);
}
void AddressPool::emit(AsmPrinter &Asm, MCSection *AddrSection) {
if (Asm.getDwarfVersion() >= 5)
emitHeader(Asm, AddrSection);
if (Pool.empty())
return;
Asm.OutStreamer->SwitchSection(AddrSection);
SmallVector<const MCExpr *, 64> Entries(Pool.size());
for (const auto &I : Pool)
Entries[I.second.Number] =
I.second.TLS
? Asm.getObjFileLowering().getDebugThreadLocalSymbol(I.first)
: MCSymbolRefExpr::create(I.first, Asm.OutContext);
for (const MCExpr *Entry : Entries)
Asm.OutStreamer->EmitValue(Entry, Asm.getDataLayout().getPointerSize());
}