#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
#define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
#include "DwarfDebug.h"
#include "DwarfUnit.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/CodeGen/DbgEntityHistoryCalculator.h"
#include "llvm/CodeGen/LexicalScopes.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/Support/Casting.h"
#include <cassert>
#include <cstdint>
#include <memory>
namespace llvm {
class AsmPrinter;
class DIE;
class DIELoc;
class DIEValueList;
class DwarfFile;
class GlobalVariable;
class MCExpr;
class MCSymbol;
class MDNode;
enum class UnitKind { Skeleton, Full };
class DwarfCompileUnit final : public DwarfUnit {
unsigned UniqueID;
bool HasRangeLists = false;
MCSymbol *LineTableStartSym;
DwarfCompileUnit *Skeleton = nullptr;
MCSymbol *LabelBegin = nullptr;
MCSymbol *MacroLabelBegin;
using ImportedEntityList = SmallVector<const MDNode *, 8>;
using ImportedEntityMap = DenseMap<const MDNode *, ImportedEntityList>;
ImportedEntityMap ImportedEntities;
StringMap<const DIE *> GlobalNames;
StringMap<const DIE *> GlobalTypes;
SmallVector<RangeSpan, 2> CURanges;
const MCSymbol *BaseAddress = nullptr;
DenseMap<const MDNode *, DIE *> AbstractSPDies;
DenseMap<const DINode *, std::unique_ptr<DbgEntity>> AbstractEntities;
uint64_t DWOId = 0;
const DIFile *LastFile = nullptr;
unsigned LastFileID;
DIE *constructVariableDIEImpl(const DbgVariable &DV, bool Abstract);
bool isDwoUnit() const override;
DenseMap<const MDNode *, DIE *> &getAbstractSPDies() {
if (isDwoUnit() && !DD->shareAcrossDWOCUs())
return AbstractSPDies;
return DU->getAbstractSPDies();
}
DenseMap<const DINode *, std::unique_ptr<DbgEntity>> &getAbstractEntities() {
if (isDwoUnit() && !DD->shareAcrossDWOCUs())
return AbstractEntities;
return DU->getAbstractEntities();
}
void finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) override;
public:
DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, AsmPrinter *A,
DwarfDebug *DW, DwarfFile *DWU,
UnitKind Kind = UnitKind::Full);
bool hasRangeLists() const { return HasRangeLists; }
unsigned getUniqueID() const { return UniqueID; }
DwarfCompileUnit *getSkeleton() const {
return Skeleton;
}
bool includeMinimalInlineScopes() const;
void initStmtList();
void applyStmtList(DIE &D);
MCSymbol *getLineTableStartSym() const { return LineTableStartSym; }
struct GlobalExpr {
const GlobalVariable *Var;
const DIExpression *Expr;
};
struct BaseTypeRef {
BaseTypeRef(unsigned BitSize, dwarf::TypeKind Encoding) :
BitSize(BitSize), Encoding(Encoding) {}
unsigned BitSize;
dwarf::TypeKind Encoding;
DIE *Die = nullptr;
};
std::vector<BaseTypeRef> ExprRefedBaseTypes;
DIE *
getOrCreateGlobalVariableDIE(const DIGlobalVariable *GV,
ArrayRef<GlobalExpr> GlobalExprs);
DIE *getOrCreateCommonBlock(const DICommonBlock *CB,
ArrayRef<GlobalExpr> GlobalExprs);
void addLocationAttribute(DIE *ToDIE, const DIGlobalVariable *GV,
ArrayRef<GlobalExpr> GlobalExprs);
void addLabelAddress(DIE &Die, dwarf::Attribute Attribute,
const MCSymbol *Label);
void addLocalLabelAddress(DIE &Die, dwarf::Attribute Attribute,
const MCSymbol *Label);
DwarfCompileUnit &getCU() override { return *this; }
unsigned getOrCreateSourceID(const DIFile *File) override;
void addImportedEntity(const DIImportedEntity* IE) {
DIScope *Scope = IE->getScope();
assert(Scope && "Invalid Scope encoding!");
if (!isa<DILocalScope>(Scope))
return;
auto *LocalScope = cast<DILocalScope>(Scope)->getNonLexicalBlockFileScope();
ImportedEntities[LocalScope].push_back(IE);
}
void addRange(RangeSpan Range);
void attachLowHighPC(DIE &D, const MCSymbol *Begin, const MCSymbol *End);
DIE &updateSubprogramScopeDIE(const DISubprogram *SP);
void constructScopeDIE(LexicalScope *Scope, DIE &ParentScopeDIE);
void addScopeRangeList(DIE &ScopeDIE, SmallVector<RangeSpan, 2> Range);
void attachRangesOrLowHighPC(DIE &D, SmallVector<RangeSpan, 2> Ranges);
void attachRangesOrLowHighPC(DIE &D,
const SmallVectorImpl<InsnRange> &Ranges);
DIE *constructInlinedScopeDIE(LexicalScope *Scope);
DIE *constructLexicalScopeDIE(LexicalScope *Scope);
DIE *constructVariableDIE(DbgVariable &DV, bool Abstract = false);
DIE *constructVariableDIE(DbgVariable &DV, const LexicalScope &Scope,
DIE *&ObjectPointer);
DIE *constructLabelDIE(DbgLabel &DL, const LexicalScope &Scope);
void createBaseTypeDIEs();
DIE &constructSubprogramScopeDIE(const DISubprogram *Sub,
LexicalScope *Scope);
DIE *createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE);
void constructAbstractSubprogramScopeDIE(LexicalScope *Scope);
bool useGNUAnalogForDwarf5Feature() const;
dwarf::Tag getDwarf5OrGNUTag(dwarf::Tag Tag) const;
dwarf::Attribute getDwarf5OrGNUAttr(dwarf::Attribute Attr) const;
dwarf::LocationAtom getDwarf5OrGNULocationAtom(dwarf::LocationAtom Loc) const;
DIE &constructCallSiteEntryDIE(DIE &ScopeDIE, const DISubprogram *CalleeSP,
bool IsTail, const MCSymbol *PCAddr,
const MCSymbol *CallAddr, unsigned CallReg);
void constructCallSiteParmEntryDIEs(DIE &CallSiteDIE,
SmallVector<DbgCallSiteParam, 4> &Params);
DIE *constructImportedEntityDIE(const DIImportedEntity *Module);
void finishSubprogramDefinition(const DISubprogram *SP);
void finishEntityDefinition(const DbgEntity *Entity);
using InlinedEntity = DbgValueHistoryMap::InlinedEntity;
DbgEntity *getExistingAbstractEntity(const DINode *Node);
void createAbstractEntity(const DINode *Node, LexicalScope *Scope);
void setSkeleton(DwarfCompileUnit &Skel) { Skeleton = &Skel; }
unsigned getHeaderSize() const override {
unsigned DWOIdSize =
DD->getDwarfVersion() >= 5 && DD->useSplitDwarf() ? sizeof(uint64_t)
: 0;
return DwarfUnit::getHeaderSize() + DWOIdSize;
}
unsigned getLength() {
return Asm->getUnitLengthFieldByteSize() + getHeaderSize() + getUnitDie().getSize();
}
void emitHeader(bool UseOffsets) override;
void addAddrTableBase();
MCSymbol *getLabelBegin() const {
assert(LabelBegin && "LabelBegin is not initialized");
return LabelBegin;
}
MCSymbol *getMacroLabelBegin() const {
return MacroLabelBegin;
}
void addGlobalName(StringRef Name, const DIE &Die,
const DIScope *Context) override;
void addGlobalNameForTypeUnit(StringRef Name, const DIScope *Context);
void addGlobalType(const DIType *Ty, const DIE &Die,
const DIScope *Context) override;
void addGlobalTypeUnitType(const DIType *Ty, const DIScope *Context);
const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
void addVariableAddress(const DbgVariable &DV, DIE &Die,
MachineLocation Location);
void addAddress(DIE &Die, dwarf::Attribute Attribute,
const MachineLocation &Location);
void addComplexAddress(const DbgVariable &DV, DIE &Die,
dwarf::Attribute Attribute,
const MachineLocation &Location);
void addLocationList(DIE &Die, dwarf::Attribute Attribute, unsigned Index);
void applyVariableAttributes(const DbgVariable &Var, DIE &VariableDie);
void addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr);
void applySubprogramAttributesToDefinition(const DISubprogram *SP,
DIE &SPDie);
void applyLabelAttributes(const DbgLabel &Label, DIE &LabelDie);
const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; }
SmallVector<RangeSpan, 2> takeRanges() { return std::move(CURanges); }
void setBaseAddress(const MCSymbol *Base) { BaseAddress = Base; }
const MCSymbol *getBaseAddress() const { return BaseAddress; }
uint64_t getDWOId() const { return DWOId; }
void setDWOId(uint64_t DwoId) { DWOId = DwoId; }
bool hasDwarfPubSections() const;
void addBaseTypeRef(DIEValueList &Die, int64_t Idx);
};
}
#endif