#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCSTREAM_H
#define LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCSTREAM_H
#include "ByteStreamer.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
namespace llvm {
class AsmPrinter;
class DbgVariable;
class DwarfCompileUnit;
class MachineInstr;
class MCSymbol;
class DebugLocStream {
public:
struct List {
DwarfCompileUnit *CU;
MCSymbol *Label = nullptr;
size_t EntryOffset;
List(DwarfCompileUnit *CU, size_t EntryOffset)
: CU(CU), EntryOffset(EntryOffset) {}
};
struct Entry {
const MCSymbol *Begin;
const MCSymbol *End;
size_t ByteOffset;
size_t CommentOffset;
};
private:
SmallVector<List, 4> Lists;
SmallVector<Entry, 32> Entries;
SmallString<256> DWARFBytes;
std::vector<std::string> Comments;
MCSymbol *Sym;
bool GenerateComments;
public:
DebugLocStream(bool GenerateComments) : GenerateComments(GenerateComments) { }
size_t getNumLists() const { return Lists.size(); }
const List &getList(size_t LI) const { return Lists[LI]; }
ArrayRef<List> getLists() const { return Lists; }
MCSymbol *getSym() const {
return Sym;
}
void setSym(MCSymbol *Sym) {
this->Sym = Sym;
}
class ListBuilder;
class EntryBuilder;
private:
size_t startList(DwarfCompileUnit *CU) {
size_t LI = Lists.size();
Lists.emplace_back(CU, Entries.size());
return LI;
}
bool finalizeList(AsmPrinter &Asm);
void startEntry(const MCSymbol *BeginSym, const MCSymbol *EndSym) {
Entries.push_back({BeginSym, EndSym, DWARFBytes.size(), Comments.size()});
}
void finalizeEntry();
public:
BufferByteStreamer getStreamer() {
return BufferByteStreamer(DWARFBytes, Comments, GenerateComments);
}
ArrayRef<Entry> getEntries(const List &L) const {
size_t LI = getIndex(L);
return makeArrayRef(Entries)
.slice(Lists[LI].EntryOffset, getNumEntries(LI));
}
ArrayRef<char> getBytes(const Entry &E) const {
size_t EI = getIndex(E);
return makeArrayRef(DWARFBytes.begin(), DWARFBytes.end())
.slice(Entries[EI].ByteOffset, getNumBytes(EI));
}
ArrayRef<std::string> getComments(const Entry &E) const {
size_t EI = getIndex(E);
return makeArrayRef(Comments)
.slice(Entries[EI].CommentOffset, getNumComments(EI));
}
private:
size_t getIndex(const List &L) const {
assert(&Lists.front() <= &L && &L <= &Lists.back() &&
"Expected valid list");
return &L - &Lists.front();
}
size_t getIndex(const Entry &E) const {
assert(&Entries.front() <= &E && &E <= &Entries.back() &&
"Expected valid entry");
return &E - &Entries.front();
}
size_t getNumEntries(size_t LI) const {
if (LI + 1 == Lists.size())
return Entries.size() - Lists[LI].EntryOffset;
return Lists[LI + 1].EntryOffset - Lists[LI].EntryOffset;
}
size_t getNumBytes(size_t EI) const {
if (EI + 1 == Entries.size())
return DWARFBytes.size() - Entries[EI].ByteOffset;
return Entries[EI + 1].ByteOffset - Entries[EI].ByteOffset;
}
size_t getNumComments(size_t EI) const {
if (EI + 1 == Entries.size())
return Comments.size() - Entries[EI].CommentOffset;
return Entries[EI + 1].CommentOffset - Entries[EI].CommentOffset;
}
};
class DebugLocStream::ListBuilder {
DebugLocStream &Locs;
AsmPrinter &Asm;
DbgVariable &V;
const MachineInstr &MI;
size_t ListIndex;
Optional<uint8_t> TagOffset;
public:
ListBuilder(DebugLocStream &Locs, DwarfCompileUnit &CU, AsmPrinter &Asm,
DbgVariable &V, const MachineInstr &MI)
: Locs(Locs), Asm(Asm), V(V), MI(MI), ListIndex(Locs.startList(&CU)),
TagOffset(None) {}
void setTagOffset(uint8_t TO) {
TagOffset = TO;
}
~ListBuilder();
DebugLocStream &getLocs() { return Locs; }
};
class DebugLocStream::EntryBuilder {
DebugLocStream &Locs;
public:
EntryBuilder(ListBuilder &List, const MCSymbol *Begin, const MCSymbol *End)
: Locs(List.getLocs()) {
Locs.startEntry(Begin, End);
}
~EntryBuilder() { Locs.finalizeEntry(); }
BufferByteStreamer getStreamer() { return Locs.getStreamer(); }
};
}
#endif