1*0b57cec5SDimitry Andric //===- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h --------------*- C++ -*-===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // This file contains support for writing Microsoft CodeView debug info. 10*0b57cec5SDimitry Andric // 11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 12*0b57cec5SDimitry Andric 13*0b57cec5SDimitry Andric #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_CODEVIEWDEBUG_H 14*0b57cec5SDimitry Andric #define LLVM_LIB_CODEGEN_ASMPRINTER_CODEVIEWDEBUG_H 15*0b57cec5SDimitry Andric 16*0b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h" 17*0b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 18*0b57cec5SDimitry Andric #include "llvm/ADT/DenseSet.h" 19*0b57cec5SDimitry Andric #include "llvm/ADT/MapVector.h" 20*0b57cec5SDimitry Andric #include "llvm/ADT/PointerUnion.h" 21*0b57cec5SDimitry Andric #include "llvm/ADT/SetVector.h" 22*0b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 23*0b57cec5SDimitry Andric #include "llvm/CodeGen/DbgEntityHistoryCalculator.h" 24*0b57cec5SDimitry Andric #include "llvm/CodeGen/DebugHandlerBase.h" 25*0b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/CodeView.h" 26*0b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/GlobalTypeTableBuilder.h" 27*0b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/TypeIndex.h" 28*0b57cec5SDimitry Andric #include "llvm/IR/DebugLoc.h" 29*0b57cec5SDimitry Andric #include "llvm/Support/Allocator.h" 30*0b57cec5SDimitry Andric #include "llvm/Support/Compiler.h" 31*0b57cec5SDimitry Andric #include <cstdint> 32*0b57cec5SDimitry Andric #include <map> 33*0b57cec5SDimitry Andric #include <string> 34*0b57cec5SDimitry Andric #include <tuple> 35*0b57cec5SDimitry Andric #include <unordered_map> 36*0b57cec5SDimitry Andric #include <utility> 37*0b57cec5SDimitry Andric #include <vector> 38*0b57cec5SDimitry Andric 39*0b57cec5SDimitry Andric namespace llvm { 40*0b57cec5SDimitry Andric 41*0b57cec5SDimitry Andric struct ClassInfo; 42*0b57cec5SDimitry Andric class StringRef; 43*0b57cec5SDimitry Andric class AsmPrinter; 44*0b57cec5SDimitry Andric class Function; 45*0b57cec5SDimitry Andric class GlobalVariable; 46*0b57cec5SDimitry Andric class MCSectionCOFF; 47*0b57cec5SDimitry Andric class MCStreamer; 48*0b57cec5SDimitry Andric class MCSymbol; 49*0b57cec5SDimitry Andric class MachineFunction; 50*0b57cec5SDimitry Andric 51*0b57cec5SDimitry Andric /// Collects and handles line tables information in a CodeView format. 52*0b57cec5SDimitry Andric class LLVM_LIBRARY_VISIBILITY CodeViewDebug : public DebugHandlerBase { 5381ad6265SDimitry Andric public: 5481ad6265SDimitry Andric struct LocalVarDef { 55*0b57cec5SDimitry Andric /// Indicates that variable data is stored in memory relative to the 56*0b57cec5SDimitry Andric /// specified register. 57*0b57cec5SDimitry Andric int InMemory : 1; 58*0b57cec5SDimitry Andric 59*0b57cec5SDimitry Andric /// Offset of variable data in memory. 60*0b57cec5SDimitry Andric int DataOffset : 31; 61*0b57cec5SDimitry Andric 62*0b57cec5SDimitry Andric /// Non-zero if this is a piece of an aggregate. 63*0b57cec5SDimitry Andric uint16_t IsSubfield : 1; 64*0b57cec5SDimitry Andric 65*0b57cec5SDimitry Andric /// Offset into aggregate. 66*0b57cec5SDimitry Andric uint16_t StructOffset : 15; 67*0b57cec5SDimitry Andric 68*0b57cec5SDimitry Andric /// Register containing the data or the register base of the memory 69*0b57cec5SDimitry Andric /// location containing the data. 70*0b57cec5SDimitry Andric uint16_t CVRegister; 71*0b57cec5SDimitry Andric 7281ad6265SDimitry Andric uint64_t static toOpaqueValue(const LocalVarDef DR) { 7381ad6265SDimitry Andric uint64_t Val = 0; 7481ad6265SDimitry Andric std::memcpy(&Val, &DR, sizeof(Val)); 7581ad6265SDimitry Andric return Val; 76*0b57cec5SDimitry Andric } 77*0b57cec5SDimitry Andric 7881ad6265SDimitry Andric LocalVarDef static createFromOpaqueValue(uint64_t Val) { 7981ad6265SDimitry Andric LocalVarDef DR; 8081ad6265SDimitry Andric std::memcpy(&DR, &Val, sizeof(Val)); 8181ad6265SDimitry Andric return DR; 8281ad6265SDimitry Andric } 83*0b57cec5SDimitry Andric }; 84*0b57cec5SDimitry Andric 8581ad6265SDimitry Andric static_assert(sizeof(uint64_t) == sizeof(LocalVarDef), ""); 8681ad6265SDimitry Andric 8781ad6265SDimitry Andric private: 8881ad6265SDimitry Andric MCStreamer &OS; 8981ad6265SDimitry Andric BumpPtrAllocator Allocator; 9081ad6265SDimitry Andric codeview::GlobalTypeTableBuilder TypeTable; 9181ad6265SDimitry Andric 9281ad6265SDimitry Andric /// Whether to emit type record hashes into .debug$H. 9381ad6265SDimitry Andric bool EmitDebugGlobalHashes = false; 9481ad6265SDimitry Andric 9581ad6265SDimitry Andric /// The codeview CPU type used by the translation unit. 9681ad6265SDimitry Andric codeview::CPUType TheCPU; 9781ad6265SDimitry Andric 9881ad6265SDimitry Andric static LocalVarDef createDefRangeMem(uint16_t CVRegister, int Offset); 99*0b57cec5SDimitry Andric 100*0b57cec5SDimitry Andric /// Similar to DbgVariable in DwarfDebug, but not dwarf-specific. 101*0b57cec5SDimitry Andric struct LocalVariable { 102*0b57cec5SDimitry Andric const DILocalVariable *DIVar = nullptr; 10381ad6265SDimitry Andric MapVector<LocalVarDef, 10481ad6265SDimitry Andric SmallVector<std::pair<const MCSymbol *, const MCSymbol *>, 1>> 10581ad6265SDimitry Andric DefRanges; 106*0b57cec5SDimitry Andric bool UseReferenceType = false; 107*0b57cec5SDimitry Andric }; 108*0b57cec5SDimitry Andric 109*0b57cec5SDimitry Andric struct CVGlobalVariable { 110*0b57cec5SDimitry Andric const DIGlobalVariable *DIGV; 111*0b57cec5SDimitry Andric PointerUnion<const GlobalVariable *, const DIExpression *> GVInfo; 112*0b57cec5SDimitry Andric }; 113*0b57cec5SDimitry Andric 114*0b57cec5SDimitry Andric struct InlineSite { 115*0b57cec5SDimitry Andric SmallVector<LocalVariable, 1> InlinedLocals; 116*0b57cec5SDimitry Andric SmallVector<const DILocation *, 1> ChildSites; 117*0b57cec5SDimitry Andric const DISubprogram *Inlinee = nullptr; 118*0b57cec5SDimitry Andric 119*0b57cec5SDimitry Andric /// The ID of the inline site or function used with .cv_loc. Not a type 120*0b57cec5SDimitry Andric /// index. 121*0b57cec5SDimitry Andric unsigned SiteFuncId = 0; 122*0b57cec5SDimitry Andric }; 123*0b57cec5SDimitry Andric 124*0b57cec5SDimitry Andric // Combines information from DILexicalBlock and LexicalScope. 125*0b57cec5SDimitry Andric struct LexicalBlock { 126*0b57cec5SDimitry Andric SmallVector<LocalVariable, 1> Locals; 127*0b57cec5SDimitry Andric SmallVector<CVGlobalVariable, 1> Globals; 128*0b57cec5SDimitry Andric SmallVector<LexicalBlock *, 1> Children; 129*0b57cec5SDimitry Andric const MCSymbol *Begin; 130*0b57cec5SDimitry Andric const MCSymbol *End; 131*0b57cec5SDimitry Andric StringRef Name; 132*0b57cec5SDimitry Andric }; 133*0b57cec5SDimitry Andric 134*0b57cec5SDimitry Andric // For each function, store a vector of labels to its instructions, as well as 135*0b57cec5SDimitry Andric // to the end of the function. 136*0b57cec5SDimitry Andric struct FunctionInfo { 137*0b57cec5SDimitry Andric FunctionInfo() = default; 138*0b57cec5SDimitry Andric 139*0b57cec5SDimitry Andric // Uncopyable. 140*0b57cec5SDimitry Andric FunctionInfo(const FunctionInfo &FI) = delete; 141*0b57cec5SDimitry Andric 142*0b57cec5SDimitry Andric /// Map from inlined call site to inlined instructions and child inlined 143*0b57cec5SDimitry Andric /// call sites. Listed in program order. 144*0b57cec5SDimitry Andric std::unordered_map<const DILocation *, InlineSite> InlineSites; 145*0b57cec5SDimitry Andric 146*0b57cec5SDimitry Andric /// Ordered list of top-level inlined call sites. 147*0b57cec5SDimitry Andric SmallVector<const DILocation *, 1> ChildSites; 148*0b57cec5SDimitry Andric 149*0b57cec5SDimitry Andric SmallVector<LocalVariable, 1> Locals; 150*0b57cec5SDimitry Andric SmallVector<CVGlobalVariable, 1> Globals; 151*0b57cec5SDimitry Andric 152*0b57cec5SDimitry Andric std::unordered_map<const DILexicalBlockBase*, LexicalBlock> LexicalBlocks; 153*0b57cec5SDimitry Andric 154*0b57cec5SDimitry Andric // Lexical blocks containing local variables. 155*0b57cec5SDimitry Andric SmallVector<LexicalBlock *, 1> ChildBlocks; 156*0b57cec5SDimitry Andric 157*0b57cec5SDimitry Andric std::vector<std::pair<MCSymbol *, MDNode *>> Annotations; 158c14a5a88SDimitry Andric std::vector<std::tuple<const MCSymbol *, const MCSymbol *, const DIType *>> 159c14a5a88SDimitry Andric HeapAllocSites; 160*0b57cec5SDimitry Andric 161*0b57cec5SDimitry Andric const MCSymbol *Begin = nullptr; 162*0b57cec5SDimitry Andric const MCSymbol *End = nullptr; 163*0b57cec5SDimitry Andric unsigned FuncId = 0; 164*0b57cec5SDimitry Andric unsigned LastFileId = 0; 165*0b57cec5SDimitry Andric 166*0b57cec5SDimitry Andric /// Number of bytes allocated in the prologue for all local stack objects. 167*0b57cec5SDimitry Andric unsigned FrameSize = 0; 168*0b57cec5SDimitry Andric 169*0b57cec5SDimitry Andric /// Number of bytes of parameters on the stack. 170*0b57cec5SDimitry Andric unsigned ParamSize = 0; 171*0b57cec5SDimitry Andric 172*0b57cec5SDimitry Andric /// Number of bytes pushed to save CSRs. 173*0b57cec5SDimitry Andric unsigned CSRSize = 0; 174*0b57cec5SDimitry Andric 175*0b57cec5SDimitry Andric /// Adjustment to apply on x86 when using the VFRAME frame pointer. 176*0b57cec5SDimitry Andric int OffsetAdjustment = 0; 177*0b57cec5SDimitry Andric 178*0b57cec5SDimitry Andric /// Two-bit value indicating which register is the designated frame pointer 179*0b57cec5SDimitry Andric /// register for local variables. Included in S_FRAMEPROC. 180*0b57cec5SDimitry Andric codeview::EncodedFramePtrReg EncodedLocalFramePtrReg = 181*0b57cec5SDimitry Andric codeview::EncodedFramePtrReg::None; 182*0b57cec5SDimitry Andric 183*0b57cec5SDimitry Andric /// Two-bit value indicating which register is the designated frame pointer 184*0b57cec5SDimitry Andric /// register for stack parameters. Included in S_FRAMEPROC. 185*0b57cec5SDimitry Andric codeview::EncodedFramePtrReg EncodedParamFramePtrReg = 186*0b57cec5SDimitry Andric codeview::EncodedFramePtrReg::None; 187*0b57cec5SDimitry Andric 188*0b57cec5SDimitry Andric codeview::FrameProcedureOptions FrameProcOpts; 189*0b57cec5SDimitry Andric 190*0b57cec5SDimitry Andric bool HasStackRealignment = false; 191*0b57cec5SDimitry Andric 192*0b57cec5SDimitry Andric bool HaveLineInfo = false; 193*0b57cec5SDimitry Andric }; 194*0b57cec5SDimitry Andric FunctionInfo *CurFn = nullptr; 195*0b57cec5SDimitry Andric 196349cc55cSDimitry Andric codeview::SourceLanguage CurrentSourceLanguage = 197349cc55cSDimitry Andric codeview::SourceLanguage::Masm; 198349cc55cSDimitry Andric 199349cc55cSDimitry Andric // This map records the constant offset in DIExpression of the 200349cc55cSDimitry Andric // DIGlobalVariableExpression referencing the DIGlobalVariable. 201349cc55cSDimitry Andric DenseMap<const DIGlobalVariable *, uint64_t> CVGlobalVariableOffsets; 202349cc55cSDimitry Andric 203*0b57cec5SDimitry Andric // Map used to seperate variables according to the lexical scope they belong 204*0b57cec5SDimitry Andric // in. This is populated by recordLocalVariable() before 205*0b57cec5SDimitry Andric // collectLexicalBlocks() separates the variables between the FunctionInfo 206*0b57cec5SDimitry Andric // and LexicalBlocks. 207*0b57cec5SDimitry Andric DenseMap<const LexicalScope *, SmallVector<LocalVariable, 1>> ScopeVariables; 208*0b57cec5SDimitry Andric 209*0b57cec5SDimitry Andric // Map to separate global variables according to the lexical scope they 210*0b57cec5SDimitry Andric // belong in. A null local scope represents the global scope. 211*0b57cec5SDimitry Andric typedef SmallVector<CVGlobalVariable, 1> GlobalVariableList; 212*0b57cec5SDimitry Andric DenseMap<const DIScope*, std::unique_ptr<GlobalVariableList> > ScopeGlobals; 213*0b57cec5SDimitry Andric 214*0b57cec5SDimitry Andric // Array of global variables which need to be emitted into a COMDAT section. 215*0b57cec5SDimitry Andric SmallVector<CVGlobalVariable, 1> ComdatVariables; 216*0b57cec5SDimitry Andric 217*0b57cec5SDimitry Andric // Array of non-COMDAT global variables. 218*0b57cec5SDimitry Andric SmallVector<CVGlobalVariable, 1> GlobalVariables; 219*0b57cec5SDimitry Andric 220e8d8bef9SDimitry Andric /// List of static const data members to be emitted as S_CONSTANTs. 221e8d8bef9SDimitry Andric SmallVector<const DIDerivedType *, 4> StaticConstMembers; 222e8d8bef9SDimitry Andric 223*0b57cec5SDimitry Andric /// The set of comdat .debug$S sections that we've seen so far. Each section 224*0b57cec5SDimitry Andric /// must start with a magic version number that must only be emitted once. 225*0b57cec5SDimitry Andric /// This set tracks which sections we've already opened. 226*0b57cec5SDimitry Andric DenseSet<MCSectionCOFF *> ComdatDebugSections; 227*0b57cec5SDimitry Andric 228*0b57cec5SDimitry Andric /// Switch to the appropriate .debug$S section for GVSym. If GVSym, the symbol 229*0b57cec5SDimitry Andric /// of an emitted global value, is in a comdat COFF section, this will switch 230*0b57cec5SDimitry Andric /// to a new .debug$S section in that comdat. This method ensures that the 231*0b57cec5SDimitry Andric /// section starts with the magic version number on first use. If GVSym is 232*0b57cec5SDimitry Andric /// null, uses the main .debug$S section. 233*0b57cec5SDimitry Andric void switchToDebugSectionForSymbol(const MCSymbol *GVSym); 234*0b57cec5SDimitry Andric 235*0b57cec5SDimitry Andric /// The next available function index for use with our .cv_* directives. Not 236*0b57cec5SDimitry Andric /// to be confused with type indices for LF_FUNC_ID records. 237*0b57cec5SDimitry Andric unsigned NextFuncId = 0; 238*0b57cec5SDimitry Andric 239*0b57cec5SDimitry Andric InlineSite &getInlineSite(const DILocation *InlinedAt, 240*0b57cec5SDimitry Andric const DISubprogram *Inlinee); 241*0b57cec5SDimitry Andric 242*0b57cec5SDimitry Andric codeview::TypeIndex getFuncIdForSubprogram(const DISubprogram *SP); 243*0b57cec5SDimitry Andric 244*0b57cec5SDimitry Andric void calculateRanges(LocalVariable &Var, 245*0b57cec5SDimitry Andric const DbgValueHistoryMap::Entries &Entries); 246*0b57cec5SDimitry Andric 247*0b57cec5SDimitry Andric /// Remember some debug info about each function. Keep it in a stable order to 248*0b57cec5SDimitry Andric /// emit at the end of the TU. 249*0b57cec5SDimitry Andric MapVector<const Function *, std::unique_ptr<FunctionInfo>> FnDebugInfo; 250*0b57cec5SDimitry Andric 251*0b57cec5SDimitry Andric /// Map from full file path to .cv_file id. Full paths are built from DIFiles 252*0b57cec5SDimitry Andric /// and are stored in FileToFilepathMap; 253*0b57cec5SDimitry Andric DenseMap<StringRef, unsigned> FileIdMap; 254*0b57cec5SDimitry Andric 255*0b57cec5SDimitry Andric /// All inlined subprograms in the order they should be emitted. 256*0b57cec5SDimitry Andric SmallSetVector<const DISubprogram *, 4> InlinedSubprograms; 257*0b57cec5SDimitry Andric 258*0b57cec5SDimitry Andric /// Map from a pair of DI metadata nodes and its DI type (or scope) that can 259*0b57cec5SDimitry Andric /// be nullptr, to CodeView type indices. Primarily indexed by 260*0b57cec5SDimitry Andric /// {DIType*, DIType*} and {DISubprogram*, DIType*}. 261*0b57cec5SDimitry Andric /// 262*0b57cec5SDimitry Andric /// The second entry in the key is needed for methods as DISubroutineType 263*0b57cec5SDimitry Andric /// representing static method type are shared with non-method function type. 264*0b57cec5SDimitry Andric DenseMap<std::pair<const DINode *, const DIType *>, codeview::TypeIndex> 265*0b57cec5SDimitry Andric TypeIndices; 266*0b57cec5SDimitry Andric 267*0b57cec5SDimitry Andric /// Map from DICompositeType* to complete type index. Non-record types are 268*0b57cec5SDimitry Andric /// always looked up in the normal TypeIndices map. 269*0b57cec5SDimitry Andric DenseMap<const DICompositeType *, codeview::TypeIndex> CompleteTypeIndices; 270*0b57cec5SDimitry Andric 271*0b57cec5SDimitry Andric /// Complete record types to emit after all active type lowerings are 272*0b57cec5SDimitry Andric /// finished. 273*0b57cec5SDimitry Andric SmallVector<const DICompositeType *, 4> DeferredCompleteTypes; 274*0b57cec5SDimitry Andric 275*0b57cec5SDimitry Andric /// Number of type lowering frames active on the stack. 276*0b57cec5SDimitry Andric unsigned TypeEmissionLevel = 0; 277*0b57cec5SDimitry Andric 278*0b57cec5SDimitry Andric codeview::TypeIndex VBPType; 279*0b57cec5SDimitry Andric 280*0b57cec5SDimitry Andric const DISubprogram *CurrentSubprogram = nullptr; 281*0b57cec5SDimitry Andric 282*0b57cec5SDimitry Andric // The UDTs we have seen while processing types; each entry is a pair of type 283*0b57cec5SDimitry Andric // index and type name. 284*0b57cec5SDimitry Andric std::vector<std::pair<std::string, const DIType *>> LocalUDTs; 285*0b57cec5SDimitry Andric std::vector<std::pair<std::string, const DIType *>> GlobalUDTs; 286*0b57cec5SDimitry Andric 287*0b57cec5SDimitry Andric using FileToFilepathMapTy = std::map<const DIFile *, std::string>; 288*0b57cec5SDimitry Andric FileToFilepathMapTy FileToFilepathMap; 289*0b57cec5SDimitry Andric 290*0b57cec5SDimitry Andric StringRef getFullFilepath(const DIFile *File); 291*0b57cec5SDimitry Andric 292*0b57cec5SDimitry Andric unsigned maybeRecordFile(const DIFile *F); 293*0b57cec5SDimitry Andric 294*0b57cec5SDimitry Andric void maybeRecordLocation(const DebugLoc &DL, const MachineFunction *MF); 295*0b57cec5SDimitry Andric 296*0b57cec5SDimitry Andric void clear(); 297*0b57cec5SDimitry Andric 298*0b57cec5SDimitry Andric void setCurrentSubprogram(const DISubprogram *SP) { 299*0b57cec5SDimitry Andric CurrentSubprogram = SP; 300*0b57cec5SDimitry Andric LocalUDTs.clear(); 301*0b57cec5SDimitry Andric } 302*0b57cec5SDimitry Andric 303*0b57cec5SDimitry Andric /// Emit the magic version number at the start of a CodeView type or symbol 304*0b57cec5SDimitry Andric /// section. Appears at the front of every .debug$S or .debug$T or .debug$P 305*0b57cec5SDimitry Andric /// section. 306*0b57cec5SDimitry Andric void emitCodeViewMagicVersion(); 307*0b57cec5SDimitry Andric 308*0b57cec5SDimitry Andric void emitTypeInformation(); 309*0b57cec5SDimitry Andric 310*0b57cec5SDimitry Andric void emitTypeGlobalHashes(); 311*0b57cec5SDimitry Andric 3120eae32dcSDimitry Andric void emitObjName(); 3130eae32dcSDimitry Andric 314*0b57cec5SDimitry Andric void emitCompilerInformation(); 315*0b57cec5SDimitry Andric 316*0b57cec5SDimitry Andric void emitBuildInfo(); 317*0b57cec5SDimitry Andric 318*0b57cec5SDimitry Andric void emitInlineeLinesSubsection(); 319*0b57cec5SDimitry Andric 320*0b57cec5SDimitry Andric void emitDebugInfoForThunk(const Function *GV, 321*0b57cec5SDimitry Andric FunctionInfo &FI, 322*0b57cec5SDimitry Andric const MCSymbol *Fn); 323*0b57cec5SDimitry Andric 324*0b57cec5SDimitry Andric void emitDebugInfoForFunction(const Function *GV, FunctionInfo &FI); 325*0b57cec5SDimitry Andric 326*0b57cec5SDimitry Andric void emitDebugInfoForRetainedTypes(); 327*0b57cec5SDimitry Andric 3285ffd83dbSDimitry Andric void emitDebugInfoForUDTs( 3295ffd83dbSDimitry Andric const std::vector<std::pair<std::string, const DIType *>> &UDTs); 330*0b57cec5SDimitry Andric 331e8d8bef9SDimitry Andric void collectDebugInfoForGlobals(); 332*0b57cec5SDimitry Andric void emitDebugInfoForGlobals(); 333*0b57cec5SDimitry Andric void emitGlobalVariableList(ArrayRef<CVGlobalVariable> Globals); 334fe6060f1SDimitry Andric void emitConstantSymbolRecord(const DIType *DTy, APSInt &Value, 335fe6060f1SDimitry Andric const std::string &QualifiedName); 336*0b57cec5SDimitry Andric void emitDebugInfoForGlobal(const CVGlobalVariable &CVGV); 337e8d8bef9SDimitry Andric void emitStaticConstMemberList(); 338*0b57cec5SDimitry Andric 339*0b57cec5SDimitry Andric /// Opens a subsection of the given kind in a .debug$S codeview section. 340*0b57cec5SDimitry Andric /// Returns an end label for use with endCVSubsection when the subsection is 341*0b57cec5SDimitry Andric /// finished. 342*0b57cec5SDimitry Andric MCSymbol *beginCVSubsection(codeview::DebugSubsectionKind Kind); 343*0b57cec5SDimitry Andric void endCVSubsection(MCSymbol *EndLabel); 344*0b57cec5SDimitry Andric 345*0b57cec5SDimitry Andric /// Opens a symbol record of the given kind. Returns an end label for use with 346*0b57cec5SDimitry Andric /// endSymbolRecord. 347*0b57cec5SDimitry Andric MCSymbol *beginSymbolRecord(codeview::SymbolKind Kind); 348*0b57cec5SDimitry Andric void endSymbolRecord(MCSymbol *SymEnd); 349*0b57cec5SDimitry Andric 350*0b57cec5SDimitry Andric /// Emits an S_END, S_INLINESITE_END, or S_PROC_ID_END record. These records 351*0b57cec5SDimitry Andric /// are empty, so we emit them with a simpler assembly sequence that doesn't 352*0b57cec5SDimitry Andric /// involve labels. 353*0b57cec5SDimitry Andric void emitEndSymbolRecord(codeview::SymbolKind EndKind); 354*0b57cec5SDimitry Andric 355*0b57cec5SDimitry Andric void emitInlinedCallSite(const FunctionInfo &FI, const DILocation *InlinedAt, 356*0b57cec5SDimitry Andric const InlineSite &Site); 357*0b57cec5SDimitry Andric 358*0b57cec5SDimitry Andric using InlinedEntity = DbgValueHistoryMap::InlinedEntity; 359*0b57cec5SDimitry Andric 360*0b57cec5SDimitry Andric void collectGlobalVariableInfo(); 361*0b57cec5SDimitry Andric void collectVariableInfo(const DISubprogram *SP); 362*0b57cec5SDimitry Andric 363*0b57cec5SDimitry Andric void collectVariableInfoFromMFTable(DenseSet<InlinedEntity> &Processed); 364*0b57cec5SDimitry Andric 365*0b57cec5SDimitry Andric // Construct the lexical block tree for a routine, pruning emptpy lexical 366*0b57cec5SDimitry Andric // scopes, and populate it with local variables. 367*0b57cec5SDimitry Andric void collectLexicalBlockInfo(SmallVectorImpl<LexicalScope *> &Scopes, 368*0b57cec5SDimitry Andric SmallVectorImpl<LexicalBlock *> &Blocks, 369*0b57cec5SDimitry Andric SmallVectorImpl<LocalVariable> &Locals, 370*0b57cec5SDimitry Andric SmallVectorImpl<CVGlobalVariable> &Globals); 371*0b57cec5SDimitry Andric void collectLexicalBlockInfo(LexicalScope &Scope, 372*0b57cec5SDimitry Andric SmallVectorImpl<LexicalBlock *> &ParentBlocks, 373*0b57cec5SDimitry Andric SmallVectorImpl<LocalVariable> &ParentLocals, 374*0b57cec5SDimitry Andric SmallVectorImpl<CVGlobalVariable> &ParentGlobals); 375*0b57cec5SDimitry Andric 376*0b57cec5SDimitry Andric /// Records information about a local variable in the appropriate scope. In 377*0b57cec5SDimitry Andric /// particular, locals from inlined code live inside the inlining site. 378*0b57cec5SDimitry Andric void recordLocalVariable(LocalVariable &&Var, const LexicalScope *LS); 379*0b57cec5SDimitry Andric 380*0b57cec5SDimitry Andric /// Emits local variables in the appropriate order. 381*0b57cec5SDimitry Andric void emitLocalVariableList(const FunctionInfo &FI, 382*0b57cec5SDimitry Andric ArrayRef<LocalVariable> Locals); 383*0b57cec5SDimitry Andric 384*0b57cec5SDimitry Andric /// Emits an S_LOCAL record and its associated defined ranges. 385*0b57cec5SDimitry Andric void emitLocalVariable(const FunctionInfo &FI, const LocalVariable &Var); 386*0b57cec5SDimitry Andric 387*0b57cec5SDimitry Andric /// Emits a sequence of lexical block scopes and their children. 388*0b57cec5SDimitry Andric void emitLexicalBlockList(ArrayRef<LexicalBlock *> Blocks, 389*0b57cec5SDimitry Andric const FunctionInfo& FI); 390*0b57cec5SDimitry Andric 391*0b57cec5SDimitry Andric /// Emit a lexical block scope and its children. 392*0b57cec5SDimitry Andric void emitLexicalBlock(const LexicalBlock &Block, const FunctionInfo& FI); 393*0b57cec5SDimitry Andric 394*0b57cec5SDimitry Andric /// Translates the DIType to codeview if necessary and returns a type index 395*0b57cec5SDimitry Andric /// for it. 396*0b57cec5SDimitry Andric codeview::TypeIndex getTypeIndex(const DIType *Ty, 397*0b57cec5SDimitry Andric const DIType *ClassTy = nullptr); 398*0b57cec5SDimitry Andric 399*0b57cec5SDimitry Andric codeview::TypeIndex 400*0b57cec5SDimitry Andric getTypeIndexForThisPtr(const DIDerivedType *PtrTy, 401*0b57cec5SDimitry Andric const DISubroutineType *SubroutineTy); 402*0b57cec5SDimitry Andric 403*0b57cec5SDimitry Andric codeview::TypeIndex getTypeIndexForReferenceTo(const DIType *Ty); 404*0b57cec5SDimitry Andric 405*0b57cec5SDimitry Andric codeview::TypeIndex getMemberFunctionType(const DISubprogram *SP, 406*0b57cec5SDimitry Andric const DICompositeType *Class); 407*0b57cec5SDimitry Andric 408*0b57cec5SDimitry Andric codeview::TypeIndex getScopeIndex(const DIScope *Scope); 409*0b57cec5SDimitry Andric 410*0b57cec5SDimitry Andric codeview::TypeIndex getVBPTypeIndex(); 411*0b57cec5SDimitry Andric 412*0b57cec5SDimitry Andric void addToUDTs(const DIType *Ty); 413*0b57cec5SDimitry Andric 414*0b57cec5SDimitry Andric void addUDTSrcLine(const DIType *Ty, codeview::TypeIndex TI); 415*0b57cec5SDimitry Andric 416*0b57cec5SDimitry Andric codeview::TypeIndex lowerType(const DIType *Ty, const DIType *ClassTy); 417*0b57cec5SDimitry Andric codeview::TypeIndex lowerTypeAlias(const DIDerivedType *Ty); 418*0b57cec5SDimitry Andric codeview::TypeIndex lowerTypeArray(const DICompositeType *Ty); 419349cc55cSDimitry Andric codeview::TypeIndex lowerTypeString(const DIStringType *Ty); 420*0b57cec5SDimitry Andric codeview::TypeIndex lowerTypeBasic(const DIBasicType *Ty); 421*0b57cec5SDimitry Andric codeview::TypeIndex lowerTypePointer( 422*0b57cec5SDimitry Andric const DIDerivedType *Ty, 423*0b57cec5SDimitry Andric codeview::PointerOptions PO = codeview::PointerOptions::None); 424*0b57cec5SDimitry Andric codeview::TypeIndex lowerTypeMemberPointer( 425*0b57cec5SDimitry Andric const DIDerivedType *Ty, 426*0b57cec5SDimitry Andric codeview::PointerOptions PO = codeview::PointerOptions::None); 427*0b57cec5SDimitry Andric codeview::TypeIndex lowerTypeModifier(const DIDerivedType *Ty); 428*0b57cec5SDimitry Andric codeview::TypeIndex lowerTypeFunction(const DISubroutineType *Ty); 429*0b57cec5SDimitry Andric codeview::TypeIndex lowerTypeVFTableShape(const DIDerivedType *Ty); 430*0b57cec5SDimitry Andric codeview::TypeIndex lowerTypeMemberFunction( 431*0b57cec5SDimitry Andric const DISubroutineType *Ty, const DIType *ClassTy, int ThisAdjustment, 432*0b57cec5SDimitry Andric bool IsStaticMethod, 433*0b57cec5SDimitry Andric codeview::FunctionOptions FO = codeview::FunctionOptions::None); 434*0b57cec5SDimitry Andric codeview::TypeIndex lowerTypeEnum(const DICompositeType *Ty); 435*0b57cec5SDimitry Andric codeview::TypeIndex lowerTypeClass(const DICompositeType *Ty); 436*0b57cec5SDimitry Andric codeview::TypeIndex lowerTypeUnion(const DICompositeType *Ty); 437*0b57cec5SDimitry Andric 438*0b57cec5SDimitry Andric /// Symbol records should point to complete types, but type records should 439*0b57cec5SDimitry Andric /// always point to incomplete types to avoid cycles in the type graph. Only 440*0b57cec5SDimitry Andric /// use this entry point when generating symbol records. The complete and 441*0b57cec5SDimitry Andric /// incomplete type indices only differ for record types. All other types use 442*0b57cec5SDimitry Andric /// the same index. 443*0b57cec5SDimitry Andric codeview::TypeIndex getCompleteTypeIndex(const DIType *Ty); 444*0b57cec5SDimitry Andric 445*0b57cec5SDimitry Andric codeview::TypeIndex lowerCompleteTypeClass(const DICompositeType *Ty); 446*0b57cec5SDimitry Andric codeview::TypeIndex lowerCompleteTypeUnion(const DICompositeType *Ty); 447*0b57cec5SDimitry Andric 448*0b57cec5SDimitry Andric struct TypeLoweringScope; 449*0b57cec5SDimitry Andric 450*0b57cec5SDimitry Andric void emitDeferredCompleteTypes(); 451*0b57cec5SDimitry Andric 452*0b57cec5SDimitry Andric void collectMemberInfo(ClassInfo &Info, const DIDerivedType *DDTy); 453*0b57cec5SDimitry Andric ClassInfo collectClassInfo(const DICompositeType *Ty); 454*0b57cec5SDimitry Andric 455*0b57cec5SDimitry Andric /// Common record member lowering functionality for record types, which are 456*0b57cec5SDimitry Andric /// structs, classes, and unions. Returns the field list index and the member 457*0b57cec5SDimitry Andric /// count. 458*0b57cec5SDimitry Andric std::tuple<codeview::TypeIndex, codeview::TypeIndex, unsigned, bool> 459*0b57cec5SDimitry Andric lowerRecordFieldList(const DICompositeType *Ty); 460*0b57cec5SDimitry Andric 461*0b57cec5SDimitry Andric /// Inserts {{Node, ClassTy}, TI} into TypeIndices and checks for duplicates. 462*0b57cec5SDimitry Andric codeview::TypeIndex recordTypeIndexForDINode(const DINode *Node, 463*0b57cec5SDimitry Andric codeview::TypeIndex TI, 464*0b57cec5SDimitry Andric const DIType *ClassTy = nullptr); 465*0b57cec5SDimitry Andric 4665ffd83dbSDimitry Andric /// Collect the names of parent scopes, innermost to outermost. Return the 4675ffd83dbSDimitry Andric /// innermost subprogram scope if present. Ensure that parent type scopes are 4685ffd83dbSDimitry Andric /// inserted into the type table. 4695ffd83dbSDimitry Andric const DISubprogram * 4705ffd83dbSDimitry Andric collectParentScopeNames(const DIScope *Scope, 4715ffd83dbSDimitry Andric SmallVectorImpl<StringRef> &ParentScopeNames); 4725ffd83dbSDimitry Andric std::string getFullyQualifiedName(const DIScope *Scope, StringRef Name); 4735ffd83dbSDimitry Andric std::string getFullyQualifiedName(const DIScope *Scope); 4745ffd83dbSDimitry Andric 475*0b57cec5SDimitry Andric unsigned getPointerSizeInBytes(); 476*0b57cec5SDimitry Andric 477*0b57cec5SDimitry Andric protected: 478*0b57cec5SDimitry Andric /// Gather pre-function debug information. 479*0b57cec5SDimitry Andric void beginFunctionImpl(const MachineFunction *MF) override; 480*0b57cec5SDimitry Andric 481*0b57cec5SDimitry Andric /// Gather post-function debug information. 482*0b57cec5SDimitry Andric void endFunctionImpl(const MachineFunction *) override; 483*0b57cec5SDimitry Andric 484349cc55cSDimitry Andric /// Check if the current module is in Fortran. 485349cc55cSDimitry Andric bool moduleIsInFortran() { 486349cc55cSDimitry Andric return CurrentSourceLanguage == codeview::SourceLanguage::Fortran; 487349cc55cSDimitry Andric } 488349cc55cSDimitry Andric 489*0b57cec5SDimitry Andric public: 490*0b57cec5SDimitry Andric CodeViewDebug(AsmPrinter *AP); 491*0b57cec5SDimitry Andric 492e8d8bef9SDimitry Andric void beginModule(Module *M) override; 493e8d8bef9SDimitry Andric 494*0b57cec5SDimitry Andric void setSymbolSize(const MCSymbol *, uint64_t) override {} 495*0b57cec5SDimitry Andric 496*0b57cec5SDimitry Andric /// Emit the COFF section that holds the line table information. 497*0b57cec5SDimitry Andric void endModule() override; 498*0b57cec5SDimitry Andric 499*0b57cec5SDimitry Andric /// Process beginning of an instruction. 500*0b57cec5SDimitry Andric void beginInstruction(const MachineInstr *MI) override; 501*0b57cec5SDimitry Andric }; 502*0b57cec5SDimitry Andric 50381ad6265SDimitry Andric template <> struct DenseMapInfo<CodeViewDebug::LocalVarDef> { 50481ad6265SDimitry Andric 50581ad6265SDimitry Andric static inline CodeViewDebug::LocalVarDef getEmptyKey() { 50681ad6265SDimitry Andric return CodeViewDebug::LocalVarDef::createFromOpaqueValue(~0ULL); 50781ad6265SDimitry Andric } 50881ad6265SDimitry Andric 50981ad6265SDimitry Andric static inline CodeViewDebug::LocalVarDef getTombstoneKey() { 51081ad6265SDimitry Andric return CodeViewDebug::LocalVarDef::createFromOpaqueValue(~0ULL - 1ULL); 51181ad6265SDimitry Andric } 51281ad6265SDimitry Andric 51381ad6265SDimitry Andric static unsigned getHashValue(const CodeViewDebug::LocalVarDef &DR) { 51481ad6265SDimitry Andric return CodeViewDebug::LocalVarDef::toOpaqueValue(DR) * 37ULL; 51581ad6265SDimitry Andric } 51681ad6265SDimitry Andric 51781ad6265SDimitry Andric static bool isEqual(const CodeViewDebug::LocalVarDef &LHS, 51881ad6265SDimitry Andric const CodeViewDebug::LocalVarDef &RHS) { 51981ad6265SDimitry Andric return CodeViewDebug::LocalVarDef::toOpaqueValue(LHS) == 52081ad6265SDimitry Andric CodeViewDebug::LocalVarDef::toOpaqueValue(RHS); 52181ad6265SDimitry Andric } 52281ad6265SDimitry Andric }; 52381ad6265SDimitry Andric 524*0b57cec5SDimitry Andric } // end namespace llvm 525*0b57cec5SDimitry Andric 526*0b57cec5SDimitry Andric #endif // LLVM_LIB_CODEGEN_ASMPRINTER_CODEVIEWDEBUG_H 527