#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_CODEVIEWDEBUG_H
#define LLVM_LIB_CODEGEN_ASMPRINTER_CODEVIEWDEBUG_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/PointerUnion.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/CodeGen/DbgEntityHistoryCalculator.h"
#include "llvm/CodeGen/DebugHandlerBase.h"
#include "llvm/DebugInfo/CodeView/CodeView.h"
#include "llvm/DebugInfo/CodeView/GlobalTypeTableBuilder.h"
#include "llvm/DebugInfo/CodeView/TypeIndex.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/Compiler.h"
#include <cstdint>
#include <map>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
namespace llvm {
struct ClassInfo;
class StringRef;
class AsmPrinter;
class Function;
class GlobalVariable;
class MCSectionCOFF;
class MCStreamer;
class MCSymbol;
class MachineFunction;
class LLVM_LIBRARY_VISIBILITY CodeViewDebug : public DebugHandlerBase {
public:
struct LocalVarDef {
int InMemory : 1;
int DataOffset : 31;
uint16_t IsSubfield : 1;
uint16_t StructOffset : 15;
uint16_t CVRegister;
uint64_t static toOpaqueValue(const LocalVarDef DR) {
uint64_t Val = 0;
std::memcpy(&Val, &DR, sizeof(Val));
return Val;
}
LocalVarDef static createFromOpaqueValue(uint64_t Val) {
LocalVarDef DR;
std::memcpy(&DR, &Val, sizeof(Val));
return DR;
}
};
static_assert(sizeof(uint64_t) == sizeof(LocalVarDef), "");
private:
MCStreamer &OS;
BumpPtrAllocator Allocator;
codeview::GlobalTypeTableBuilder TypeTable;
bool EmitDebugGlobalHashes = false;
codeview::CPUType TheCPU;
static LocalVarDef createDefRangeMem(uint16_t CVRegister, int Offset);
struct LocalVariable {
const DILocalVariable *DIVar = nullptr;
MapVector<LocalVarDef,
SmallVector<std::pair<const MCSymbol *, const MCSymbol *>, 1>>
DefRanges;
bool UseReferenceType = false;
};
struct CVGlobalVariable {
const DIGlobalVariable *DIGV;
PointerUnion<const GlobalVariable *, const DIExpression *> GVInfo;
};
struct InlineSite {
SmallVector<LocalVariable, 1> InlinedLocals;
SmallVector<const DILocation *, 1> ChildSites;
const DISubprogram *Inlinee = nullptr;
unsigned SiteFuncId = 0;
};
struct LexicalBlock {
SmallVector<LocalVariable, 1> Locals;
SmallVector<CVGlobalVariable, 1> Globals;
SmallVector<LexicalBlock *, 1> Children;
const MCSymbol *Begin;
const MCSymbol *End;
StringRef Name;
};
struct FunctionInfo {
FunctionInfo() = default;
FunctionInfo(const FunctionInfo &FI) = delete;
std::unordered_map<const DILocation *, InlineSite> InlineSites;
SmallVector<const DILocation *, 1> ChildSites;
SmallVector<LocalVariable, 1> Locals;
SmallVector<CVGlobalVariable, 1> Globals;
std::unordered_map<const DILexicalBlockBase*, LexicalBlock> LexicalBlocks;
SmallVector<LexicalBlock *, 1> ChildBlocks;
std::vector<std::pair<MCSymbol *, MDNode *>> Annotations;
std::vector<std::tuple<const MCSymbol *, const MCSymbol *, const DIType *>>
HeapAllocSites;
const MCSymbol *Begin = nullptr;
const MCSymbol *End = nullptr;
unsigned FuncId = 0;
unsigned LastFileId = 0;
unsigned FrameSize = 0;
unsigned ParamSize = 0;
unsigned CSRSize = 0;
int OffsetAdjustment = 0;
codeview::EncodedFramePtrReg EncodedLocalFramePtrReg =
codeview::EncodedFramePtrReg::None;
codeview::EncodedFramePtrReg EncodedParamFramePtrReg =
codeview::EncodedFramePtrReg::None;
codeview::FrameProcedureOptions FrameProcOpts;
bool HasStackRealignment = false;
bool HaveLineInfo = false;
};
FunctionInfo *CurFn = nullptr;
codeview::SourceLanguage CurrentSourceLanguage =
codeview::SourceLanguage::Masm;
DenseMap<const DIGlobalVariable *, uint64_t> CVGlobalVariableOffsets;
DenseMap<const LexicalScope *, SmallVector<LocalVariable, 1>> ScopeVariables;
typedef SmallVector<CVGlobalVariable, 1> GlobalVariableList;
DenseMap<const DIScope*, std::unique_ptr<GlobalVariableList> > ScopeGlobals;
SmallVector<CVGlobalVariable, 1> ComdatVariables;
SmallVector<CVGlobalVariable, 1> GlobalVariables;
SmallVector<const DIDerivedType *, 4> StaticConstMembers;
DenseSet<MCSectionCOFF *> ComdatDebugSections;
void switchToDebugSectionForSymbol(const MCSymbol *GVSym);
unsigned NextFuncId = 0;
InlineSite &getInlineSite(const DILocation *InlinedAt,
const DISubprogram *Inlinee);
codeview::TypeIndex getFuncIdForSubprogram(const DISubprogram *SP);
void calculateRanges(LocalVariable &Var,
const DbgValueHistoryMap::Entries &Entries);
MapVector<const Function *, std::unique_ptr<FunctionInfo>> FnDebugInfo;
DenseMap<StringRef, unsigned> FileIdMap;
SmallSetVector<const DISubprogram *, 4> InlinedSubprograms;
DenseMap<std::pair<const DINode *, const DIType *>, codeview::TypeIndex>
TypeIndices;
DenseMap<const DICompositeType *, codeview::TypeIndex> CompleteTypeIndices;
SmallVector<const DICompositeType *, 4> DeferredCompleteTypes;
unsigned TypeEmissionLevel = 0;
codeview::TypeIndex VBPType;
const DISubprogram *CurrentSubprogram = nullptr;
std::vector<std::pair<std::string, const DIType *>> LocalUDTs;
std::vector<std::pair<std::string, const DIType *>> GlobalUDTs;
using FileToFilepathMapTy = std::map<const DIFile *, std::string>;
FileToFilepathMapTy FileToFilepathMap;
StringRef getFullFilepath(const DIFile *File);
unsigned maybeRecordFile(const DIFile *F);
void maybeRecordLocation(const DebugLoc &DL, const MachineFunction *MF);
void clear();
void setCurrentSubprogram(const DISubprogram *SP) {
CurrentSubprogram = SP;
LocalUDTs.clear();
}
void emitCodeViewMagicVersion();
void emitTypeInformation();
void emitTypeGlobalHashes();
void emitObjName();
void emitCompilerInformation();
void emitBuildInfo();
void emitInlineeLinesSubsection();
void emitDebugInfoForThunk(const Function *GV,
FunctionInfo &FI,
const MCSymbol *Fn);
void emitDebugInfoForFunction(const Function *GV, FunctionInfo &FI);
void emitDebugInfoForRetainedTypes();
void emitDebugInfoForUDTs(
const std::vector<std::pair<std::string, const DIType *>> &UDTs);
void collectDebugInfoForGlobals();
void emitDebugInfoForGlobals();
void emitGlobalVariableList(ArrayRef<CVGlobalVariable> Globals);
void emitConstantSymbolRecord(const DIType *DTy, APSInt &Value,
const std::string &QualifiedName);
void emitDebugInfoForGlobal(const CVGlobalVariable &CVGV);
void emitStaticConstMemberList();
MCSymbol *beginCVSubsection(codeview::DebugSubsectionKind Kind);
void endCVSubsection(MCSymbol *EndLabel);
MCSymbol *beginSymbolRecord(codeview::SymbolKind Kind);
void endSymbolRecord(MCSymbol *SymEnd);
void emitEndSymbolRecord(codeview::SymbolKind EndKind);
void emitInlinedCallSite(const FunctionInfo &FI, const DILocation *InlinedAt,
const InlineSite &Site);
using InlinedEntity = DbgValueHistoryMap::InlinedEntity;
void collectGlobalVariableInfo();
void collectVariableInfo(const DISubprogram *SP);
void collectVariableInfoFromMFTable(DenseSet<InlinedEntity> &Processed);
void collectLexicalBlockInfo(SmallVectorImpl<LexicalScope *> &Scopes,
SmallVectorImpl<LexicalBlock *> &Blocks,
SmallVectorImpl<LocalVariable> &Locals,
SmallVectorImpl<CVGlobalVariable> &Globals);
void collectLexicalBlockInfo(LexicalScope &Scope,
SmallVectorImpl<LexicalBlock *> &ParentBlocks,
SmallVectorImpl<LocalVariable> &ParentLocals,
SmallVectorImpl<CVGlobalVariable> &ParentGlobals);
void recordLocalVariable(LocalVariable &&Var, const LexicalScope *LS);
void emitLocalVariableList(const FunctionInfo &FI,
ArrayRef<LocalVariable> Locals);
void emitLocalVariable(const FunctionInfo &FI, const LocalVariable &Var);
void emitLexicalBlockList(ArrayRef<LexicalBlock *> Blocks,
const FunctionInfo& FI);
void emitLexicalBlock(const LexicalBlock &Block, const FunctionInfo& FI);
codeview::TypeIndex getTypeIndex(const DIType *Ty,
const DIType *ClassTy = nullptr);
codeview::TypeIndex
getTypeIndexForThisPtr(const DIDerivedType *PtrTy,
const DISubroutineType *SubroutineTy);
codeview::TypeIndex getTypeIndexForReferenceTo(const DIType *Ty);
codeview::TypeIndex getMemberFunctionType(const DISubprogram *SP,
const DICompositeType *Class);
codeview::TypeIndex getScopeIndex(const DIScope *Scope);
codeview::TypeIndex getVBPTypeIndex();
void addToUDTs(const DIType *Ty);
void addUDTSrcLine(const DIType *Ty, codeview::TypeIndex TI);
codeview::TypeIndex lowerType(const DIType *Ty, const DIType *ClassTy);
codeview::TypeIndex lowerTypeAlias(const DIDerivedType *Ty);
codeview::TypeIndex lowerTypeArray(const DICompositeType *Ty);
codeview::TypeIndex lowerTypeString(const DIStringType *Ty);
codeview::TypeIndex lowerTypeBasic(const DIBasicType *Ty);
codeview::TypeIndex lowerTypePointer(
const DIDerivedType *Ty,
codeview::PointerOptions PO = codeview::PointerOptions::None);
codeview::TypeIndex lowerTypeMemberPointer(
const DIDerivedType *Ty,
codeview::PointerOptions PO = codeview::PointerOptions::None);
codeview::TypeIndex lowerTypeModifier(const DIDerivedType *Ty);
codeview::TypeIndex lowerTypeFunction(const DISubroutineType *Ty);
codeview::TypeIndex lowerTypeVFTableShape(const DIDerivedType *Ty);
codeview::TypeIndex lowerTypeMemberFunction(
const DISubroutineType *Ty, const DIType *ClassTy, int ThisAdjustment,
bool IsStaticMethod,
codeview::FunctionOptions FO = codeview::FunctionOptions::None);
codeview::TypeIndex lowerTypeEnum(const DICompositeType *Ty);
codeview::TypeIndex lowerTypeClass(const DICompositeType *Ty);
codeview::TypeIndex lowerTypeUnion(const DICompositeType *Ty);
codeview::TypeIndex getCompleteTypeIndex(const DIType *Ty);
codeview::TypeIndex lowerCompleteTypeClass(const DICompositeType *Ty);
codeview::TypeIndex lowerCompleteTypeUnion(const DICompositeType *Ty);
struct TypeLoweringScope;
void emitDeferredCompleteTypes();
void collectMemberInfo(ClassInfo &Info, const DIDerivedType *DDTy);
ClassInfo collectClassInfo(const DICompositeType *Ty);
std::tuple<codeview::TypeIndex, codeview::TypeIndex, unsigned, bool>
lowerRecordFieldList(const DICompositeType *Ty);
codeview::TypeIndex recordTypeIndexForDINode(const DINode *Node,
codeview::TypeIndex TI,
const DIType *ClassTy = nullptr);
const DISubprogram *
collectParentScopeNames(const DIScope *Scope,
SmallVectorImpl<StringRef> &ParentScopeNames);
std::string getFullyQualifiedName(const DIScope *Scope, StringRef Name);
std::string getFullyQualifiedName(const DIScope *Scope);
unsigned getPointerSizeInBytes();
protected:
void beginFunctionImpl(const MachineFunction *MF) override;
void endFunctionImpl(const MachineFunction *) override;
bool moduleIsInFortran() {
return CurrentSourceLanguage == codeview::SourceLanguage::Fortran;
}
public:
CodeViewDebug(AsmPrinter *AP);
void beginModule(Module *M) override;
void setSymbolSize(const MCSymbol *, uint64_t) override {}
void endModule() override;
void beginInstruction(const MachineInstr *MI) override;
};
template <> struct DenseMapInfo<CodeViewDebug::LocalVarDef> {
static inline CodeViewDebug::LocalVarDef getEmptyKey() {
return CodeViewDebug::LocalVarDef::createFromOpaqueValue(~0ULL);
}
static inline CodeViewDebug::LocalVarDef getTombstoneKey() {
return CodeViewDebug::LocalVarDef::createFromOpaqueValue(~0ULL - 1ULL);
}
static unsigned getHashValue(const CodeViewDebug::LocalVarDef &DR) {
return CodeViewDebug::LocalVarDef::toOpaqueValue(DR) * 37ULL;
}
static bool isEqual(const CodeViewDebug::LocalVarDef &LHS,
const CodeViewDebug::LocalVarDef &RHS) {
return CodeViewDebug::LocalVarDef::toOpaqueValue(LHS) ==
CodeViewDebug::LocalVarDef::toOpaqueValue(RHS);
}
};
}
#endif