#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_CODEVIEWDEBUG_H
#define LLVM_LIB_CODEGEN_ASMPRINTER_CODEVIEWDEBUG_H
#include "DbgValueHistoryCalculator.h"
#include "DebugHandlerBase.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallVector.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 {
MCStreamer &OS;
BumpPtrAllocator Allocator;
codeview::GlobalTypeTableBuilder TypeTable;
struct LocalVarDefRange {
int InMemory : 1;
int DataOffset : 31;
uint16_t IsSubfield : 1;
uint16_t StructOffset : 15;
uint16_t CVRegister;
bool isDifferentLocation(LocalVarDefRange &O) {
return InMemory != O.InMemory || DataOffset != O.DataOffset ||
IsSubfield != O.IsSubfield || StructOffset != O.StructOffset ||
CVRegister != O.CVRegister;
}
SmallVector<std::pair<const MCSymbol *, const MCSymbol *>, 1> Ranges;
};
static LocalVarDefRange createDefRangeMem(uint16_t CVRegister, int Offset);
static LocalVarDefRange createDefRangeGeneral(uint16_t CVRegister,
bool InMemory, int Offset,
bool IsSubfield,
uint16_t StructOffset);
struct LocalVariable {
const DILocalVariable *DIVar = nullptr;
SmallVector<LocalVarDefRange, 1> DefRanges;
bool UseReferenceType = false;
};
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<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;
std::unordered_map<const DILexicalBlockBase*, LexicalBlock> LexicalBlocks;
SmallVector<LexicalBlock *, 1> ChildBlocks;
std::vector<std::pair<MCSymbol *, MDNode *>> Annotations;
const MCSymbol *Begin = nullptr;
const MCSymbol *End = nullptr;
unsigned FuncId = 0;
unsigned LastFileId = 0;
bool HaveLineInfo = false;
};
FunctionInfo *CurFn = nullptr;
DenseMap<const LexicalScope *, SmallVector<LocalVariable, 1>> ScopeVariables;
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::InstrRanges &Ranges);
static void collectInlineSiteChildren(SmallVectorImpl<unsigned> &Children,
const FunctionInfo &FI,
const InlineSite &Site);
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 emitCompilerInformation();
void emitInlineeLinesSubsection();
void emitDebugInfoForThunk(const Function *GV,
FunctionInfo &FI,
const MCSymbol *Fn);
void emitDebugInfoForFunction(const Function *GV, FunctionInfo &FI);
void emitDebugInfoForGlobals();
void emitDebugInfoForRetainedTypes();
void
emitDebugInfoForUDTs(ArrayRef<std::pair<std::string, const DIType *>> UDTs);
void emitDebugInfoForGlobal(const DIGlobalVariable *DIGV,
const GlobalVariable *GV, MCSymbol *GVSym);
MCSymbol *beginCVSubsection(codeview::DebugSubsectionKind Kind);
void endCVSubsection(MCSymbol *EndLabel);
void emitInlinedCallSite(const FunctionInfo &FI, const DILocation *InlinedAt,
const InlineSite &Site);
using InlinedVariable = DbgValueHistoryMap::InlinedVariable;
void collectVariableInfo(const DISubprogram *SP);
void collectVariableInfoFromMFTable(DenseSet<InlinedVariable> &Processed);
void collectLexicalBlockInfo(SmallVectorImpl<LexicalScope *> &Scopes,
SmallVectorImpl<LexicalBlock *> &Blocks,
SmallVectorImpl<LocalVariable> &Locals);
void collectLexicalBlockInfo(LexicalScope &Scope,
SmallVectorImpl<LexicalBlock *> &ParentBlocks,
SmallVectorImpl<LocalVariable> &ParentLocals);
void recordLocalVariable(LocalVariable &&Var, const LexicalScope *LS);
void emitLocalVariableList(ArrayRef<LocalVariable> Locals);
void emitLocalVariable(const LocalVariable &Var);
void emitLexicalBlockList(ArrayRef<LexicalBlock *> Blocks,
const FunctionInfo& FI);
void emitLexicalBlock(const LexicalBlock &Block, const FunctionInfo& FI);
codeview::TypeIndex getTypeIndex(DITypeRef TypeRef,
DITypeRef ClassTyRef = DITypeRef());
codeview::TypeIndex getTypeIndexForReferenceTo(DITypeRef TypeRef);
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 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::TypeIndex lowerTypeEnum(const DICompositeType *Ty);
codeview::TypeIndex lowerTypeClass(const DICompositeType *Ty);
codeview::TypeIndex lowerTypeUnion(const DICompositeType *Ty);
codeview::TypeIndex getCompleteTypeIndex(DITypeRef TypeRef);
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);
unsigned getPointerSizeInBytes();
protected:
void beginFunctionImpl(const MachineFunction *MF) override;
void endFunctionImpl(const MachineFunction *) override;
public:
CodeViewDebug(AsmPrinter *AP);
void setSymbolSize(const MCSymbol *, uint64_t) override {}
void endModule() override;
void beginInstruction(const MachineInstr *MI) override;
};
}
#endif