#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFDEBUG_H
#define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFDEBUG_H
#include "DwarfFile.h"
#include "AsmPrinterHandler.h"
#include "DIE.h"
#include "DbgValueHistoryCalculator.h"
#include "DebugLocEntry.h"
#include "DebugLocList.h"
#include "DwarfAccelTable.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/FoldingSet.h"
#include "llvm/CodeGen/LexicalScopes.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/MC/MachineLocation.h"
#include "llvm/MC/MCDwarf.h"
#include "llvm/Support/Allocator.h"
#include <memory>
namespace llvm {
class AsmPrinter;
class ByteStreamer;
class ConstantInt;
class ConstantFP;
class DwarfCompileUnit;
class DwarfDebug;
class DwarfTypeUnit;
class DwarfUnit;
class MachineModuleInfo;
class SrcLineInfo {
unsigned Line; unsigned Column; unsigned SourceID; MCSymbol *Label; public:
SrcLineInfo(unsigned L, unsigned C, unsigned S, MCSymbol *label)
: Line(L), Column(C), SourceID(S), Label(label) {}
unsigned getLine() const { return Line; }
unsigned getColumn() const { return Column; }
unsigned getSourceID() const { return SourceID; }
MCSymbol *getLabel() const { return Label; }
};
class DbgVariable {
DIVariable Var; DIE *TheDIE; unsigned DotDebugLocOffset; const MachineInstr *MInsn; int FrameIndex;
DwarfDebug *DD;
public:
DbgVariable(DIVariable V, DwarfDebug *DD)
: Var(V), TheDIE(nullptr), DotDebugLocOffset(~0U), MInsn(nullptr),
FrameIndex(~0), DD(DD) {}
DbgVariable(const MachineInstr *DbgValue, DwarfDebug *DD)
: Var(DbgValue->getDebugVariable()), TheDIE(nullptr),
DotDebugLocOffset(~0U), MInsn(DbgValue), FrameIndex(~0), DD(DD) {}
DIVariable getVariable() const { return Var; }
void setDIE(DIE &D) { TheDIE = &D; }
DIE *getDIE() const { return TheDIE; }
void setDotDebugLocOffset(unsigned O) { DotDebugLocOffset = O; }
unsigned getDotDebugLocOffset() const { return DotDebugLocOffset; }
StringRef getName() const { return Var.getName(); }
const MachineInstr *getMInsn() const { return MInsn; }
int getFrameIndex() const { return FrameIndex; }
void setFrameIndex(int FI) { FrameIndex = FI; }
dwarf::Tag getTag() const {
if (Var.getTag() == dwarf::DW_TAG_arg_variable)
return dwarf::DW_TAG_formal_parameter;
return dwarf::DW_TAG_variable;
}
bool isArtificial() const {
if (Var.isArtificial())
return true;
if (getType().isArtificial())
return true;
return false;
}
bool isObjectPointer() const {
if (Var.isObjectPointer())
return true;
if (getType().isObjectPointer())
return true;
return false;
}
bool variableHasComplexAddress() const {
assert(Var.isVariable() && "Invalid complex DbgVariable!");
return Var.hasComplexAddress();
}
bool isBlockByrefVariable() const;
unsigned getNumAddrElements() const {
assert(Var.isVariable() && "Invalid complex DbgVariable!");
return Var.getNumAddrElements();
}
uint64_t getAddrElement(unsigned i) const { return Var.getAddrElement(i); }
DIType getType() const;
private:
template <typename T> T resolve(DIRef<T> Ref) const;
};
struct SymbolCU {
SymbolCU(DwarfCompileUnit *CU, const MCSymbol *Sym) : Sym(Sym), CU(CU) {}
const MCSymbol *Sym;
DwarfCompileUnit *CU;
};
class DwarfDebug : public AsmPrinterHandler {
AsmPrinter *Asm;
MachineModuleInfo *MMI;
BumpPtrAllocator DIEValueAllocator;
DwarfCompileUnit *FirstCU;
MapVector<const MDNode *, DwarfCompileUnit *> CUMap;
DenseMap<const MDNode *, DwarfCompileUnit *> SPMap;
DenseMap<const DIE *, DwarfCompileUnit *> CUDieMap;
DenseMap<const MDNode *, DIE *> MDTypeNodeToDieMap;
std::vector<SymbolCU> ArangeLabels;
DenseMap<const MCSymbol *, uint64_t> SymSize;
typedef DenseMap<const MCSection *, SmallVector<SymbolCU, 8> > SectionMapType;
SectionMapType SectionMap;
SmallVector<DbgVariable *, 8> CurrentFnArguments;
LexicalScopes LScopes;
DenseMap<const MDNode *, DIE *> AbstractSPDies;
typedef DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >
ScopeVariablesMap;
ScopeVariablesMap ScopeVariables;
DenseMap<const MDNode *, std::unique_ptr<DbgVariable>> AbstractVariables;
SmallVector<std::unique_ptr<DbgVariable>, 64> ConcreteVariables;
SmallVector<DebugLocList, 4> DotDebugLocEntries;
SmallPtrSet<DIE *, 4> InlinedSubprogramDIEs;
SmallPtrSet<const MDNode *, 16> ProcessedSPNodes;
DenseMap<const MachineInstr *, MCSymbol *> LabelsBeforeInsn;
DenseMap<const MachineInstr *, MCSymbol *> LabelsAfterInsn;
DbgValueHistoryMap DbgValues;
DebugLoc PrevInstLoc;
MCSymbol *PrevLabel;
DebugLoc PrologEndLoc;
const MachineFunction *CurFn;
const MachineInstr *CurMI;
const MCSection *PrevSection;
const DwarfCompileUnit *PrevCU;
MCSymbol *DwarfInfoSectionSym, *DwarfAbbrevSectionSym;
MCSymbol *DwarfStrSectionSym, *TextSectionSym, *DwarfDebugRangeSectionSym;
MCSymbol *DwarfDebugLocSectionSym, *DwarfLineSectionSym, *DwarfAddrSectionSym;
MCSymbol *FunctionBeginSym, *FunctionEndSym;
MCSymbol *DwarfInfoDWOSectionSym, *DwarfAbbrevDWOSectionSym;
MCSymbol *DwarfTypesDWOSectionSym;
MCSymbol *DwarfStrDWOSectionSym;
MCSymbol *DwarfGnuPubNamesSectionSym, *DwarfGnuPubTypesSectionSym;
StringRef CompilationDir;
unsigned GlobalRangeCount;
DwarfFile InfoHolder;
typedef SmallVector<std::pair<const MDNode *, const MDNode *>, 32>
ImportedEntityMap;
ImportedEntityMap ScopesWithImportedEntities;
DenseMap<const MDNode *, const DwarfTypeUnit *> DwarfTypeUnits;
SmallVector<std::pair<std::unique_ptr<DwarfTypeUnit>, DICompositeType>, 1> TypeUnitsUnderConstruction;
bool HasDwarfPubSections;
bool HasCURanges;
bool UsedNonDefaultText;
unsigned DwarfVersion;
DITypeIdentifierMap TypeIdentifierMap;
bool HasDwarfAccelTables;
bool HasSplitDwarf;
DwarfFile SkeletonHolder;
MCDwarfDwoLineTable SplitTypeUnitFileTable;
bool SingleCU;
AddressPool AddrPool;
DwarfAccelTable AccelNames;
DwarfAccelTable AccelObjC;
DwarfAccelTable AccelNamespace;
DwarfAccelTable AccelTypes;
DenseMap<const Function *, DISubprogram> FunctionDIs;
MCDwarfDwoLineTable *getDwoLineTable(const DwarfCompileUnit &);
void addScopeVariable(LexicalScope *LS, DbgVariable *Var);
const SmallVectorImpl<std::unique_ptr<DwarfUnit>> &getUnits() {
return InfoHolder.getUnits();
}
DbgVariable *getExistingAbstractVariable(const DIVariable &DV,
DIVariable &Cleansed);
DbgVariable *getExistingAbstractVariable(const DIVariable &DV);
void createAbstractVariable(const DIVariable &DV, LexicalScope *Scope);
void ensureAbstractVariableIsCreated(const DIVariable &Var,
const MDNode *Scope);
void ensureAbstractVariableIsCreatedIfScoped(const DIVariable &Var,
const MDNode *Scope);
DIE &updateSubprogramScopeDIE(DwarfCompileUnit &SPCU, DISubprogram SP);
bool isLexicalScopeDIENull(LexicalScope *Scope);
void addScopeRangeList(DwarfCompileUnit &TheCU, DIE &ScopeDIE,
const SmallVectorImpl<InsnRange> &Range);
std::unique_ptr<DIE> constructLexicalScopeDIE(DwarfCompileUnit &TheCU,
LexicalScope *Scope);
std::unique_ptr<DIE> constructInlinedScopeDIE(DwarfCompileUnit &TheCU,
LexicalScope *Scope);
std::unique_ptr<DIE> constructScopeDIE(DwarfCompileUnit &TheCU,
LexicalScope *Scope);
void createAndAddScopeChildren(DwarfCompileUnit &TheCU, LexicalScope *Scope,
DIE &ScopeDIE);
void constructAbstractSubprogramScopeDIE(DwarfCompileUnit &TheCU,
LexicalScope *Scope);
DIE &constructSubprogramScopeDIE(DwarfCompileUnit &TheCU,
LexicalScope *Scope);
DIE *createScopeChildrenDIE(DwarfCompileUnit &TheCU, LexicalScope *Scope,
SmallVectorImpl<std::unique_ptr<DIE>> &Children);
void emitSectionLabels();
unsigned computeSizeAndOffset(DIE *Die, unsigned Offset);
void computeSizeAndOffsets();
void collectDeadVariables();
void finishVariableDefinitions();
void finishSubprogramDefinitions();
void finalizeModuleInfo();
void endSections();
void emitDebugInfo();
void emitAbbreviations();
void emitEndOfLineMatrix(unsigned SectionEnd);
void emitAccelNames();
void emitAccelObjC();
void emitAccelNamespaces();
void emitAccelTypes();
void emitDebugPubNames(bool GnuStyle = false);
void emitDebugPubTypes(bool GnuStyle = false);
void
emitDebugPubSection(bool GnuStyle, const MCSection *PSec, StringRef Name,
const StringMap<const DIE *> &(DwarfUnit::*Accessor)()
const);
void emitDebugStr();
void emitDebugLoc();
void emitDebugLocDWO();
void emitDebugARanges();
void emitDebugRanges();
void emitDebugInlineInfo();
void initSkeletonUnit(const DwarfUnit &U, DIE &Die,
std::unique_ptr<DwarfUnit> NewU);
DwarfCompileUnit &constructSkeletonCU(const DwarfCompileUnit &CU);
DwarfTypeUnit &constructSkeletonTU(DwarfTypeUnit &TU);
void emitDebugInfoDWO();
void emitDebugAbbrevDWO();
void emitDebugLineDWO();
void emitDebugStrDWO();
void addGnuPubAttributes(DwarfUnit &U, DIE &D) const;
DwarfCompileUnit &constructDwarfCompileUnit(DICompileUnit DIUnit);
void constructImportedEntityDIE(DwarfCompileUnit &TheCU, const MDNode *N);
void constructImportedEntityDIE(DwarfCompileUnit &TheCU, const MDNode *N,
DIE &Context);
void constructImportedEntityDIE(DwarfCompileUnit &TheCU,
const DIImportedEntity &Module, DIE &Context);
void recordSourceLine(unsigned Line, unsigned Col, const MDNode *Scope,
unsigned Flags);
void identifyScopeMarkers();
bool addCurrentFnArgument(DbgVariable *Var, LexicalScope *Scope);
void collectVariableInfo(SmallPtrSet<const MDNode *, 16> &ProcessedVars);
void buildLocationList(SmallVectorImpl<DebugLocEntry> &DebugLoc,
const DbgValueHistoryMap::InstrRanges &Ranges);
void collectVariableInfoFromMMITable(SmallPtrSet<const MDNode *, 16> &P);
void requestLabelBeforeInsn(const MachineInstr *MI) {
LabelsBeforeInsn.insert(std::make_pair(MI, nullptr));
}
MCSymbol *getLabelBeforeInsn(const MachineInstr *MI);
void requestLabelAfterInsn(const MachineInstr *MI) {
LabelsAfterInsn.insert(std::make_pair(MI, nullptr));
}
MCSymbol *getLabelAfterInsn(const MachineInstr *MI);
void attachRangesOrLowHighPC(DwarfCompileUnit &Unit, DIE &D,
const SmallVectorImpl<InsnRange> &Ranges);
void attachLowHighPC(DwarfCompileUnit &Unit, DIE &D, const MCSymbol *Begin,
const MCSymbol *End);
public:
DwarfDebug(AsmPrinter *A, Module *M);
~DwarfDebug() override;
void insertDIE(const MDNode *TypeMD, DIE *Die) {
MDTypeNodeToDieMap.insert(std::make_pair(TypeMD, Die));
}
DIE *getDIE(const MDNode *TypeMD) {
return MDTypeNodeToDieMap.lookup(TypeMD);
}
void beginModule();
void endModule() override;
void beginFunction(const MachineFunction *MF) override;
void endFunction(const MachineFunction *MF) override;
void beginInstruction(const MachineInstr *MI) override;
void endInstruction() override;
void addDwarfTypeUnitType(DwarfCompileUnit &CU, StringRef Identifier,
DIE &Die, DICompositeType CTy);
void addArangeLabel(SymbolCU SCU) { ArangeLabels.push_back(SCU); }
void setSymbolSize(const MCSymbol *Sym, uint64_t Size) override {
SymSize[Sym] = Size;
}
void emitDIE(DIE &Die);
bool useDwarfAccelTables() const { return HasDwarfAccelTables; }
bool useSplitDwarf() const { return HasSplitDwarf; }
unsigned getDwarfVersion() const { return DwarfVersion; }
MCSymbol *getDebugLocSym() const { return DwarfDebugLocSectionSym; }
const MCSection *getPrevSection() const { return PrevSection; }
const DwarfCompileUnit *getPrevCU() const { return PrevCU; }
const SmallVectorImpl<DebugLocList> &
getDebugLocEntries() const {
return DotDebugLocEntries;
}
void emitDebugLocEntry(ByteStreamer &Streamer, const DebugLocEntry &Entry);
void emitDebugLocValue(ByteStreamer &Streamer,
const DebugLocEntry::Value &Value);
void emitLocPieces(ByteStreamer &Streamer,
const DITypeIdentifierMap &Map,
ArrayRef<DebugLocEntry::Value> Values);
void emitDebugLocEntryLocation(const DebugLocEntry &Entry);
template <typename T> T resolve(DIRef<T> Ref) const {
return Ref.resolve(TypeIdentifierMap);
}
const DITypeIdentifierMap &getTypeIdentifierMap() const {
return TypeIdentifierMap;
}
DwarfCompileUnit *lookupUnit(const DIE *CU) const {
return CUDieMap.lookup(CU);
}
bool isSubprogramContext(const MDNode *Context);
void addSubprogramNames(DISubprogram SP, DIE &Die);
AddressPool &getAddressPool() { return AddrPool; }
void addAccelName(StringRef Name, const DIE &Die);
void addAccelObjC(StringRef Name, const DIE &Die);
void addAccelNamespace(StringRef Name, const DIE &Die);
void addAccelType(StringRef Name, const DIE &Die, char Flags);
};
}
#endif