#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
#define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
#include "DbgValueHistoryCalculator.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/DIE.h"
#include "llvm/CodeGen/LexicalScopes.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/Support/Casting.h"
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <memory>
namespace llvm {
class AsmPrinter;
class DwarfFile;
class GlobalVariable;
class MCExpr;
class MCSymbol;
class MDNode;
class DwarfCompileUnit final : public DwarfUnit {
unsigned UniqueID;
DIE::value_iterator StmtListValue;
DwarfCompileUnit *Skeleton = nullptr;
MCSymbol *LabelBegin;
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<RangeSpanList, 1> CURangeLists;
SmallVector<RangeSpan, 2> CURanges;
const MCSymbol *BaseAddress = nullptr;
DenseMap<const MDNode *, DIE *> AbstractSPDies;
DenseMap<const MDNode *, std::unique_ptr<DbgVariable>> AbstractVariables;
uint64_t DWOId = 0;
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 MDNode *, std::unique_ptr<DbgVariable>> &getAbstractVariables() {
if (isDwoUnit() && !DD->shareAcrossDWOCUs())
return AbstractVariables;
return DU->getAbstractVariables();
}
public:
DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, AsmPrinter *A,
DwarfDebug *DW, DwarfFile *DWU);
unsigned getUniqueID() const { return UniqueID; }
DwarfCompileUnit *getSkeleton() const {
return Skeleton;
}
bool includeMinimalInlineScopes() const;
void initStmtList();
void applyStmtList(DIE &D);
struct GlobalExpr {
const GlobalVariable *Var;
const DIExpression *Expr;
};
DIE *
getOrCreateGlobalVariableDIE(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,
SmallVectorImpl<DIE *> &FinalChildren);
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 *createScopeChildrenDIE(LexicalScope *Scope,
SmallVectorImpl<DIE *> &Children,
bool *HasNonScopeChildren = nullptr);
void constructSubprogramScopeDIE(const DISubprogram *Sub, LexicalScope *Scope);
DIE *createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE);
void constructAbstractSubprogramScopeDIE(LexicalScope *Scope);
DIE *constructImportedEntityDIE(const DIImportedEntity *Module);
void finishSubprogramDefinition(const DISubprogram *SP);
void finishVariableDefinition(const DbgVariable &Var);
using InlinedVariable = DbgValueHistoryMap::InlinedVariable;
DbgVariable *getExistingAbstractVariable(InlinedVariable IV,
const DILocalVariable *&Cleansed);
DbgVariable *getExistingAbstractVariable(InlinedVariable IV);
void createAbstractVariable(const DILocalVariable *Var, 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 sizeof(uint32_t) +
getHeaderSize() + getUnitDie().getSize();
}
void emitHeader(bool UseOffsets) override;
MCSymbol *getLabelBegin() const {
assert(getSection());
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);
const SmallVectorImpl<RangeSpanList> &getRangeLists() const {
return (Skeleton ? Skeleton : this)->CURangeLists;
}
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;
};
}
#endif