xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
10b57cec5SDimitry Andric //===- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h --------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file contains support for writing Microsoft CodeView debug info.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_CODEVIEWDEBUG_H
140b57cec5SDimitry Andric #define LLVM_LIB_CODEGEN_ASMPRINTER_CODEVIEWDEBUG_H
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
170b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
180b57cec5SDimitry Andric #include "llvm/ADT/DenseSet.h"
190b57cec5SDimitry Andric #include "llvm/ADT/MapVector.h"
200b57cec5SDimitry Andric #include "llvm/ADT/PointerUnion.h"
210b57cec5SDimitry Andric #include "llvm/ADT/SetVector.h"
220b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/DbgEntityHistoryCalculator.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/DebugHandlerBase.h"
250b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/CodeView.h"
260b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/GlobalTypeTableBuilder.h"
270b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/TypeIndex.h"
280b57cec5SDimitry Andric #include "llvm/IR/DebugLoc.h"
290b57cec5SDimitry Andric #include "llvm/Support/Allocator.h"
300b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
310b57cec5SDimitry Andric #include <cstdint>
320b57cec5SDimitry Andric #include <map>
330b57cec5SDimitry Andric #include <string>
340b57cec5SDimitry Andric #include <tuple>
350b57cec5SDimitry Andric #include <unordered_map>
360b57cec5SDimitry Andric #include <utility>
370b57cec5SDimitry Andric #include <vector>
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric namespace llvm {
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric struct ClassInfo;
420b57cec5SDimitry Andric class StringRef;
430b57cec5SDimitry Andric class AsmPrinter;
440b57cec5SDimitry Andric class Function;
450b57cec5SDimitry Andric class GlobalVariable;
460b57cec5SDimitry Andric class MCSectionCOFF;
470b57cec5SDimitry Andric class MCStreamer;
480b57cec5SDimitry Andric class MCSymbol;
490b57cec5SDimitry Andric class MachineFunction;
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric /// Collects and handles line tables information in a CodeView format.
520b57cec5SDimitry Andric class LLVM_LIBRARY_VISIBILITY CodeViewDebug : public DebugHandlerBase {
53*81ad6265SDimitry Andric public:
54*81ad6265SDimitry Andric   struct LocalVarDef {
550b57cec5SDimitry Andric     /// Indicates that variable data is stored in memory relative to the
560b57cec5SDimitry Andric     /// specified register.
570b57cec5SDimitry Andric     int InMemory : 1;
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric     /// Offset of variable data in memory.
600b57cec5SDimitry Andric     int DataOffset : 31;
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric     /// Non-zero if this is a piece of an aggregate.
630b57cec5SDimitry Andric     uint16_t IsSubfield : 1;
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric     /// Offset into aggregate.
660b57cec5SDimitry Andric     uint16_t StructOffset : 15;
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric     /// Register containing the data or the register base of the memory
690b57cec5SDimitry Andric     /// location containing the data.
700b57cec5SDimitry Andric     uint16_t CVRegister;
710b57cec5SDimitry Andric 
72*81ad6265SDimitry Andric     uint64_t static toOpaqueValue(const LocalVarDef DR) {
73*81ad6265SDimitry Andric       uint64_t Val = 0;
74*81ad6265SDimitry Andric       std::memcpy(&Val, &DR, sizeof(Val));
75*81ad6265SDimitry Andric       return Val;
760b57cec5SDimitry Andric     }
770b57cec5SDimitry Andric 
78*81ad6265SDimitry Andric     LocalVarDef static createFromOpaqueValue(uint64_t Val) {
79*81ad6265SDimitry Andric       LocalVarDef DR;
80*81ad6265SDimitry Andric       std::memcpy(&DR, &Val, sizeof(Val));
81*81ad6265SDimitry Andric       return DR;
82*81ad6265SDimitry Andric     }
830b57cec5SDimitry Andric   };
840b57cec5SDimitry Andric 
85*81ad6265SDimitry Andric   static_assert(sizeof(uint64_t) == sizeof(LocalVarDef), "");
86*81ad6265SDimitry Andric 
87*81ad6265SDimitry Andric private:
88*81ad6265SDimitry Andric   MCStreamer &OS;
89*81ad6265SDimitry Andric   BumpPtrAllocator Allocator;
90*81ad6265SDimitry Andric   codeview::GlobalTypeTableBuilder TypeTable;
91*81ad6265SDimitry Andric 
92*81ad6265SDimitry Andric   /// Whether to emit type record hashes into .debug$H.
93*81ad6265SDimitry Andric   bool EmitDebugGlobalHashes = false;
94*81ad6265SDimitry Andric 
95*81ad6265SDimitry Andric   /// The codeview CPU type used by the translation unit.
96*81ad6265SDimitry Andric   codeview::CPUType TheCPU;
97*81ad6265SDimitry Andric 
98*81ad6265SDimitry Andric   static LocalVarDef createDefRangeMem(uint16_t CVRegister, int Offset);
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric   /// Similar to DbgVariable in DwarfDebug, but not dwarf-specific.
1010b57cec5SDimitry Andric   struct LocalVariable {
1020b57cec5SDimitry Andric     const DILocalVariable *DIVar = nullptr;
103*81ad6265SDimitry Andric     MapVector<LocalVarDef,
104*81ad6265SDimitry Andric               SmallVector<std::pair<const MCSymbol *, const MCSymbol *>, 1>>
105*81ad6265SDimitry Andric         DefRanges;
1060b57cec5SDimitry Andric     bool UseReferenceType = false;
1070b57cec5SDimitry Andric   };
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric   struct CVGlobalVariable {
1100b57cec5SDimitry Andric     const DIGlobalVariable *DIGV;
1110b57cec5SDimitry Andric     PointerUnion<const GlobalVariable *, const DIExpression *> GVInfo;
1120b57cec5SDimitry Andric   };
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric   struct InlineSite {
1150b57cec5SDimitry Andric     SmallVector<LocalVariable, 1> InlinedLocals;
1160b57cec5SDimitry Andric     SmallVector<const DILocation *, 1> ChildSites;
1170b57cec5SDimitry Andric     const DISubprogram *Inlinee = nullptr;
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric     /// The ID of the inline site or function used with .cv_loc. Not a type
1200b57cec5SDimitry Andric     /// index.
1210b57cec5SDimitry Andric     unsigned SiteFuncId = 0;
1220b57cec5SDimitry Andric   };
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric   // Combines information from DILexicalBlock and LexicalScope.
1250b57cec5SDimitry Andric   struct LexicalBlock {
1260b57cec5SDimitry Andric     SmallVector<LocalVariable, 1> Locals;
1270b57cec5SDimitry Andric     SmallVector<CVGlobalVariable, 1> Globals;
1280b57cec5SDimitry Andric     SmallVector<LexicalBlock *, 1> Children;
1290b57cec5SDimitry Andric     const MCSymbol *Begin;
1300b57cec5SDimitry Andric     const MCSymbol *End;
1310b57cec5SDimitry Andric     StringRef Name;
1320b57cec5SDimitry Andric   };
1330b57cec5SDimitry Andric 
1340b57cec5SDimitry Andric   // For each function, store a vector of labels to its instructions, as well as
1350b57cec5SDimitry Andric   // to the end of the function.
1360b57cec5SDimitry Andric   struct FunctionInfo {
1370b57cec5SDimitry Andric     FunctionInfo() = default;
1380b57cec5SDimitry Andric 
1390b57cec5SDimitry Andric     // Uncopyable.
1400b57cec5SDimitry Andric     FunctionInfo(const FunctionInfo &FI) = delete;
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric     /// Map from inlined call site to inlined instructions and child inlined
1430b57cec5SDimitry Andric     /// call sites. Listed in program order.
1440b57cec5SDimitry Andric     std::unordered_map<const DILocation *, InlineSite> InlineSites;
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric     /// Ordered list of top-level inlined call sites.
1470b57cec5SDimitry Andric     SmallVector<const DILocation *, 1> ChildSites;
1480b57cec5SDimitry Andric 
1490b57cec5SDimitry Andric     SmallVector<LocalVariable, 1> Locals;
1500b57cec5SDimitry Andric     SmallVector<CVGlobalVariable, 1> Globals;
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric     std::unordered_map<const DILexicalBlockBase*, LexicalBlock> LexicalBlocks;
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric     // Lexical blocks containing local variables.
1550b57cec5SDimitry Andric     SmallVector<LexicalBlock *, 1> ChildBlocks;
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric     std::vector<std::pair<MCSymbol *, MDNode *>> Annotations;
158c14a5a88SDimitry Andric     std::vector<std::tuple<const MCSymbol *, const MCSymbol *, const DIType *>>
159c14a5a88SDimitry Andric         HeapAllocSites;
1600b57cec5SDimitry Andric 
1610b57cec5SDimitry Andric     const MCSymbol *Begin = nullptr;
1620b57cec5SDimitry Andric     const MCSymbol *End = nullptr;
1630b57cec5SDimitry Andric     unsigned FuncId = 0;
1640b57cec5SDimitry Andric     unsigned LastFileId = 0;
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric     /// Number of bytes allocated in the prologue for all local stack objects.
1670b57cec5SDimitry Andric     unsigned FrameSize = 0;
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric     /// Number of bytes of parameters on the stack.
1700b57cec5SDimitry Andric     unsigned ParamSize = 0;
1710b57cec5SDimitry Andric 
1720b57cec5SDimitry Andric     /// Number of bytes pushed to save CSRs.
1730b57cec5SDimitry Andric     unsigned CSRSize = 0;
1740b57cec5SDimitry Andric 
1750b57cec5SDimitry Andric     /// Adjustment to apply on x86 when using the VFRAME frame pointer.
1760b57cec5SDimitry Andric     int OffsetAdjustment = 0;
1770b57cec5SDimitry Andric 
1780b57cec5SDimitry Andric     /// Two-bit value indicating which register is the designated frame pointer
1790b57cec5SDimitry Andric     /// register for local variables. Included in S_FRAMEPROC.
1800b57cec5SDimitry Andric     codeview::EncodedFramePtrReg EncodedLocalFramePtrReg =
1810b57cec5SDimitry Andric         codeview::EncodedFramePtrReg::None;
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric     /// Two-bit value indicating which register is the designated frame pointer
1840b57cec5SDimitry Andric     /// register for stack parameters. Included in S_FRAMEPROC.
1850b57cec5SDimitry Andric     codeview::EncodedFramePtrReg EncodedParamFramePtrReg =
1860b57cec5SDimitry Andric         codeview::EncodedFramePtrReg::None;
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric     codeview::FrameProcedureOptions FrameProcOpts;
1890b57cec5SDimitry Andric 
1900b57cec5SDimitry Andric     bool HasStackRealignment = false;
1910b57cec5SDimitry Andric 
1920b57cec5SDimitry Andric     bool HaveLineInfo = false;
1930b57cec5SDimitry Andric   };
1940b57cec5SDimitry Andric   FunctionInfo *CurFn = nullptr;
1950b57cec5SDimitry 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 
2030b57cec5SDimitry Andric   // Map used to seperate variables according to the lexical scope they belong
2040b57cec5SDimitry Andric   // in.  This is populated by recordLocalVariable() before
2050b57cec5SDimitry Andric   // collectLexicalBlocks() separates the variables between the FunctionInfo
2060b57cec5SDimitry Andric   // and LexicalBlocks.
2070b57cec5SDimitry Andric   DenseMap<const LexicalScope *, SmallVector<LocalVariable, 1>> ScopeVariables;
2080b57cec5SDimitry Andric 
2090b57cec5SDimitry Andric   // Map to separate global variables according to the lexical scope they
2100b57cec5SDimitry Andric   // belong in. A null local scope represents the global scope.
2110b57cec5SDimitry Andric   typedef SmallVector<CVGlobalVariable, 1> GlobalVariableList;
2120b57cec5SDimitry Andric   DenseMap<const DIScope*, std::unique_ptr<GlobalVariableList> > ScopeGlobals;
2130b57cec5SDimitry Andric 
2140b57cec5SDimitry Andric   // Array of global variables which  need to be emitted into a COMDAT section.
2150b57cec5SDimitry Andric   SmallVector<CVGlobalVariable, 1> ComdatVariables;
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric   // Array of non-COMDAT global variables.
2180b57cec5SDimitry Andric   SmallVector<CVGlobalVariable, 1> GlobalVariables;
2190b57cec5SDimitry Andric 
220e8d8bef9SDimitry Andric   /// List of static const data members to be emitted as S_CONSTANTs.
221e8d8bef9SDimitry Andric   SmallVector<const DIDerivedType *, 4> StaticConstMembers;
222e8d8bef9SDimitry Andric 
2230b57cec5SDimitry Andric   /// The set of comdat .debug$S sections that we've seen so far. Each section
2240b57cec5SDimitry Andric   /// must start with a magic version number that must only be emitted once.
2250b57cec5SDimitry Andric   /// This set tracks which sections we've already opened.
2260b57cec5SDimitry Andric   DenseSet<MCSectionCOFF *> ComdatDebugSections;
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric   /// Switch to the appropriate .debug$S section for GVSym. If GVSym, the symbol
2290b57cec5SDimitry Andric   /// of an emitted global value, is in a comdat COFF section, this will switch
2300b57cec5SDimitry Andric   /// to a new .debug$S section in that comdat. This method ensures that the
2310b57cec5SDimitry Andric   /// section starts with the magic version number on first use. If GVSym is
2320b57cec5SDimitry Andric   /// null, uses the main .debug$S section.
2330b57cec5SDimitry Andric   void switchToDebugSectionForSymbol(const MCSymbol *GVSym);
2340b57cec5SDimitry Andric 
2350b57cec5SDimitry Andric   /// The next available function index for use with our .cv_* directives. Not
2360b57cec5SDimitry Andric   /// to be confused with type indices for LF_FUNC_ID records.
2370b57cec5SDimitry Andric   unsigned NextFuncId = 0;
2380b57cec5SDimitry Andric 
2390b57cec5SDimitry Andric   InlineSite &getInlineSite(const DILocation *InlinedAt,
2400b57cec5SDimitry Andric                             const DISubprogram *Inlinee);
2410b57cec5SDimitry Andric 
2420b57cec5SDimitry Andric   codeview::TypeIndex getFuncIdForSubprogram(const DISubprogram *SP);
2430b57cec5SDimitry Andric 
2440b57cec5SDimitry Andric   void calculateRanges(LocalVariable &Var,
2450b57cec5SDimitry Andric                        const DbgValueHistoryMap::Entries &Entries);
2460b57cec5SDimitry Andric 
2470b57cec5SDimitry Andric   /// Remember some debug info about each function. Keep it in a stable order to
2480b57cec5SDimitry Andric   /// emit at the end of the TU.
2490b57cec5SDimitry Andric   MapVector<const Function *, std::unique_ptr<FunctionInfo>> FnDebugInfo;
2500b57cec5SDimitry Andric 
2510b57cec5SDimitry Andric   /// Map from full file path to .cv_file id. Full paths are built from DIFiles
2520b57cec5SDimitry Andric   /// and are stored in FileToFilepathMap;
2530b57cec5SDimitry Andric   DenseMap<StringRef, unsigned> FileIdMap;
2540b57cec5SDimitry Andric 
2550b57cec5SDimitry Andric   /// All inlined subprograms in the order they should be emitted.
2560b57cec5SDimitry Andric   SmallSetVector<const DISubprogram *, 4> InlinedSubprograms;
2570b57cec5SDimitry Andric 
2580b57cec5SDimitry Andric   /// Map from a pair of DI metadata nodes and its DI type (or scope) that can
2590b57cec5SDimitry Andric   /// be nullptr, to CodeView type indices. Primarily indexed by
2600b57cec5SDimitry Andric   /// {DIType*, DIType*} and {DISubprogram*, DIType*}.
2610b57cec5SDimitry Andric   ///
2620b57cec5SDimitry Andric   /// The second entry in the key is needed for methods as DISubroutineType
2630b57cec5SDimitry Andric   /// representing static method type are shared with non-method function type.
2640b57cec5SDimitry Andric   DenseMap<std::pair<const DINode *, const DIType *>, codeview::TypeIndex>
2650b57cec5SDimitry Andric       TypeIndices;
2660b57cec5SDimitry Andric 
2670b57cec5SDimitry Andric   /// Map from DICompositeType* to complete type index. Non-record types are
2680b57cec5SDimitry Andric   /// always looked up in the normal TypeIndices map.
2690b57cec5SDimitry Andric   DenseMap<const DICompositeType *, codeview::TypeIndex> CompleteTypeIndices;
2700b57cec5SDimitry Andric 
2710b57cec5SDimitry Andric   /// Complete record types to emit after all active type lowerings are
2720b57cec5SDimitry Andric   /// finished.
2730b57cec5SDimitry Andric   SmallVector<const DICompositeType *, 4> DeferredCompleteTypes;
2740b57cec5SDimitry Andric 
2750b57cec5SDimitry Andric   /// Number of type lowering frames active on the stack.
2760b57cec5SDimitry Andric   unsigned TypeEmissionLevel = 0;
2770b57cec5SDimitry Andric 
2780b57cec5SDimitry Andric   codeview::TypeIndex VBPType;
2790b57cec5SDimitry Andric 
2800b57cec5SDimitry Andric   const DISubprogram *CurrentSubprogram = nullptr;
2810b57cec5SDimitry Andric 
2820b57cec5SDimitry Andric   // The UDTs we have seen while processing types; each entry is a pair of type
2830b57cec5SDimitry Andric   // index and type name.
2840b57cec5SDimitry Andric   std::vector<std::pair<std::string, const DIType *>> LocalUDTs;
2850b57cec5SDimitry Andric   std::vector<std::pair<std::string, const DIType *>> GlobalUDTs;
2860b57cec5SDimitry Andric 
2870b57cec5SDimitry Andric   using FileToFilepathMapTy = std::map<const DIFile *, std::string>;
2880b57cec5SDimitry Andric   FileToFilepathMapTy FileToFilepathMap;
2890b57cec5SDimitry Andric 
2900b57cec5SDimitry Andric   StringRef getFullFilepath(const DIFile *File);
2910b57cec5SDimitry Andric 
2920b57cec5SDimitry Andric   unsigned maybeRecordFile(const DIFile *F);
2930b57cec5SDimitry Andric 
2940b57cec5SDimitry Andric   void maybeRecordLocation(const DebugLoc &DL, const MachineFunction *MF);
2950b57cec5SDimitry Andric 
2960b57cec5SDimitry Andric   void clear();
2970b57cec5SDimitry Andric 
2980b57cec5SDimitry Andric   void setCurrentSubprogram(const DISubprogram *SP) {
2990b57cec5SDimitry Andric     CurrentSubprogram = SP;
3000b57cec5SDimitry Andric     LocalUDTs.clear();
3010b57cec5SDimitry Andric   }
3020b57cec5SDimitry Andric 
3030b57cec5SDimitry Andric   /// Emit the magic version number at the start of a CodeView type or symbol
3040b57cec5SDimitry Andric   /// section. Appears at the front of every .debug$S or .debug$T or .debug$P
3050b57cec5SDimitry Andric   /// section.
3060b57cec5SDimitry Andric   void emitCodeViewMagicVersion();
3070b57cec5SDimitry Andric 
3080b57cec5SDimitry Andric   void emitTypeInformation();
3090b57cec5SDimitry Andric 
3100b57cec5SDimitry Andric   void emitTypeGlobalHashes();
3110b57cec5SDimitry Andric 
3120eae32dcSDimitry Andric   void emitObjName();
3130eae32dcSDimitry Andric 
3140b57cec5SDimitry Andric   void emitCompilerInformation();
3150b57cec5SDimitry Andric 
3160b57cec5SDimitry Andric   void emitBuildInfo();
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric   void emitInlineeLinesSubsection();
3190b57cec5SDimitry Andric 
3200b57cec5SDimitry Andric   void emitDebugInfoForThunk(const Function *GV,
3210b57cec5SDimitry Andric                              FunctionInfo &FI,
3220b57cec5SDimitry Andric                              const MCSymbol *Fn);
3230b57cec5SDimitry Andric 
3240b57cec5SDimitry Andric   void emitDebugInfoForFunction(const Function *GV, FunctionInfo &FI);
3250b57cec5SDimitry Andric 
3260b57cec5SDimitry Andric   void emitDebugInfoForRetainedTypes();
3270b57cec5SDimitry Andric 
3285ffd83dbSDimitry Andric   void emitDebugInfoForUDTs(
3295ffd83dbSDimitry Andric       const std::vector<std::pair<std::string, const DIType *>> &UDTs);
3300b57cec5SDimitry Andric 
331e8d8bef9SDimitry Andric   void collectDebugInfoForGlobals();
3320b57cec5SDimitry Andric   void emitDebugInfoForGlobals();
3330b57cec5SDimitry Andric   void emitGlobalVariableList(ArrayRef<CVGlobalVariable> Globals);
334fe6060f1SDimitry Andric   void emitConstantSymbolRecord(const DIType *DTy, APSInt &Value,
335fe6060f1SDimitry Andric                                 const std::string &QualifiedName);
3360b57cec5SDimitry Andric   void emitDebugInfoForGlobal(const CVGlobalVariable &CVGV);
337e8d8bef9SDimitry Andric   void emitStaticConstMemberList();
3380b57cec5SDimitry Andric 
3390b57cec5SDimitry Andric   /// Opens a subsection of the given kind in a .debug$S codeview section.
3400b57cec5SDimitry Andric   /// Returns an end label for use with endCVSubsection when the subsection is
3410b57cec5SDimitry Andric   /// finished.
3420b57cec5SDimitry Andric   MCSymbol *beginCVSubsection(codeview::DebugSubsectionKind Kind);
3430b57cec5SDimitry Andric   void endCVSubsection(MCSymbol *EndLabel);
3440b57cec5SDimitry Andric 
3450b57cec5SDimitry Andric   /// Opens a symbol record of the given kind. Returns an end label for use with
3460b57cec5SDimitry Andric   /// endSymbolRecord.
3470b57cec5SDimitry Andric   MCSymbol *beginSymbolRecord(codeview::SymbolKind Kind);
3480b57cec5SDimitry Andric   void endSymbolRecord(MCSymbol *SymEnd);
3490b57cec5SDimitry Andric 
3500b57cec5SDimitry Andric   /// Emits an S_END, S_INLINESITE_END, or S_PROC_ID_END record. These records
3510b57cec5SDimitry Andric   /// are empty, so we emit them with a simpler assembly sequence that doesn't
3520b57cec5SDimitry Andric   /// involve labels.
3530b57cec5SDimitry Andric   void emitEndSymbolRecord(codeview::SymbolKind EndKind);
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric   void emitInlinedCallSite(const FunctionInfo &FI, const DILocation *InlinedAt,
3560b57cec5SDimitry Andric                            const InlineSite &Site);
3570b57cec5SDimitry Andric 
3580b57cec5SDimitry Andric   using InlinedEntity = DbgValueHistoryMap::InlinedEntity;
3590b57cec5SDimitry Andric 
3600b57cec5SDimitry Andric   void collectGlobalVariableInfo();
3610b57cec5SDimitry Andric   void collectVariableInfo(const DISubprogram *SP);
3620b57cec5SDimitry Andric 
3630b57cec5SDimitry Andric   void collectVariableInfoFromMFTable(DenseSet<InlinedEntity> &Processed);
3640b57cec5SDimitry Andric 
3650b57cec5SDimitry Andric   // Construct the lexical block tree for a routine, pruning emptpy lexical
3660b57cec5SDimitry Andric   // scopes, and populate it with local variables.
3670b57cec5SDimitry Andric   void collectLexicalBlockInfo(SmallVectorImpl<LexicalScope *> &Scopes,
3680b57cec5SDimitry Andric                                SmallVectorImpl<LexicalBlock *> &Blocks,
3690b57cec5SDimitry Andric                                SmallVectorImpl<LocalVariable> &Locals,
3700b57cec5SDimitry Andric                                SmallVectorImpl<CVGlobalVariable> &Globals);
3710b57cec5SDimitry Andric   void collectLexicalBlockInfo(LexicalScope &Scope,
3720b57cec5SDimitry Andric                                SmallVectorImpl<LexicalBlock *> &ParentBlocks,
3730b57cec5SDimitry Andric                                SmallVectorImpl<LocalVariable> &ParentLocals,
3740b57cec5SDimitry Andric                                SmallVectorImpl<CVGlobalVariable> &ParentGlobals);
3750b57cec5SDimitry Andric 
3760b57cec5SDimitry Andric   /// Records information about a local variable in the appropriate scope. In
3770b57cec5SDimitry Andric   /// particular, locals from inlined code live inside the inlining site.
3780b57cec5SDimitry Andric   void recordLocalVariable(LocalVariable &&Var, const LexicalScope *LS);
3790b57cec5SDimitry Andric 
3800b57cec5SDimitry Andric   /// Emits local variables in the appropriate order.
3810b57cec5SDimitry Andric   void emitLocalVariableList(const FunctionInfo &FI,
3820b57cec5SDimitry Andric                              ArrayRef<LocalVariable> Locals);
3830b57cec5SDimitry Andric 
3840b57cec5SDimitry Andric   /// Emits an S_LOCAL record and its associated defined ranges.
3850b57cec5SDimitry Andric   void emitLocalVariable(const FunctionInfo &FI, const LocalVariable &Var);
3860b57cec5SDimitry Andric 
3870b57cec5SDimitry Andric   /// Emits a sequence of lexical block scopes and their children.
3880b57cec5SDimitry Andric   void emitLexicalBlockList(ArrayRef<LexicalBlock *> Blocks,
3890b57cec5SDimitry Andric                             const FunctionInfo& FI);
3900b57cec5SDimitry Andric 
3910b57cec5SDimitry Andric   /// Emit a lexical block scope and its children.
3920b57cec5SDimitry Andric   void emitLexicalBlock(const LexicalBlock &Block, const FunctionInfo& FI);
3930b57cec5SDimitry Andric 
3940b57cec5SDimitry Andric   /// Translates the DIType to codeview if necessary and returns a type index
3950b57cec5SDimitry Andric   /// for it.
3960b57cec5SDimitry Andric   codeview::TypeIndex getTypeIndex(const DIType *Ty,
3970b57cec5SDimitry Andric                                    const DIType *ClassTy = nullptr);
3980b57cec5SDimitry Andric 
3990b57cec5SDimitry Andric   codeview::TypeIndex
4000b57cec5SDimitry Andric   getTypeIndexForThisPtr(const DIDerivedType *PtrTy,
4010b57cec5SDimitry Andric                          const DISubroutineType *SubroutineTy);
4020b57cec5SDimitry Andric 
4030b57cec5SDimitry Andric   codeview::TypeIndex getTypeIndexForReferenceTo(const DIType *Ty);
4040b57cec5SDimitry Andric 
4050b57cec5SDimitry Andric   codeview::TypeIndex getMemberFunctionType(const DISubprogram *SP,
4060b57cec5SDimitry Andric                                             const DICompositeType *Class);
4070b57cec5SDimitry Andric 
4080b57cec5SDimitry Andric   codeview::TypeIndex getScopeIndex(const DIScope *Scope);
4090b57cec5SDimitry Andric 
4100b57cec5SDimitry Andric   codeview::TypeIndex getVBPTypeIndex();
4110b57cec5SDimitry Andric 
4120b57cec5SDimitry Andric   void addToUDTs(const DIType *Ty);
4130b57cec5SDimitry Andric 
4140b57cec5SDimitry Andric   void addUDTSrcLine(const DIType *Ty, codeview::TypeIndex TI);
4150b57cec5SDimitry Andric 
4160b57cec5SDimitry Andric   codeview::TypeIndex lowerType(const DIType *Ty, const DIType *ClassTy);
4170b57cec5SDimitry Andric   codeview::TypeIndex lowerTypeAlias(const DIDerivedType *Ty);
4180b57cec5SDimitry Andric   codeview::TypeIndex lowerTypeArray(const DICompositeType *Ty);
419349cc55cSDimitry Andric   codeview::TypeIndex lowerTypeString(const DIStringType *Ty);
4200b57cec5SDimitry Andric   codeview::TypeIndex lowerTypeBasic(const DIBasicType *Ty);
4210b57cec5SDimitry Andric   codeview::TypeIndex lowerTypePointer(
4220b57cec5SDimitry Andric       const DIDerivedType *Ty,
4230b57cec5SDimitry Andric       codeview::PointerOptions PO = codeview::PointerOptions::None);
4240b57cec5SDimitry Andric   codeview::TypeIndex lowerTypeMemberPointer(
4250b57cec5SDimitry Andric       const DIDerivedType *Ty,
4260b57cec5SDimitry Andric       codeview::PointerOptions PO = codeview::PointerOptions::None);
4270b57cec5SDimitry Andric   codeview::TypeIndex lowerTypeModifier(const DIDerivedType *Ty);
4280b57cec5SDimitry Andric   codeview::TypeIndex lowerTypeFunction(const DISubroutineType *Ty);
4290b57cec5SDimitry Andric   codeview::TypeIndex lowerTypeVFTableShape(const DIDerivedType *Ty);
4300b57cec5SDimitry Andric   codeview::TypeIndex lowerTypeMemberFunction(
4310b57cec5SDimitry Andric       const DISubroutineType *Ty, const DIType *ClassTy, int ThisAdjustment,
4320b57cec5SDimitry Andric       bool IsStaticMethod,
4330b57cec5SDimitry Andric       codeview::FunctionOptions FO = codeview::FunctionOptions::None);
4340b57cec5SDimitry Andric   codeview::TypeIndex lowerTypeEnum(const DICompositeType *Ty);
4350b57cec5SDimitry Andric   codeview::TypeIndex lowerTypeClass(const DICompositeType *Ty);
4360b57cec5SDimitry Andric   codeview::TypeIndex lowerTypeUnion(const DICompositeType *Ty);
4370b57cec5SDimitry Andric 
4380b57cec5SDimitry Andric   /// Symbol records should point to complete types, but type records should
4390b57cec5SDimitry Andric   /// always point to incomplete types to avoid cycles in the type graph. Only
4400b57cec5SDimitry Andric   /// use this entry point when generating symbol records. The complete and
4410b57cec5SDimitry Andric   /// incomplete type indices only differ for record types. All other types use
4420b57cec5SDimitry Andric   /// the same index.
4430b57cec5SDimitry Andric   codeview::TypeIndex getCompleteTypeIndex(const DIType *Ty);
4440b57cec5SDimitry Andric 
4450b57cec5SDimitry Andric   codeview::TypeIndex lowerCompleteTypeClass(const DICompositeType *Ty);
4460b57cec5SDimitry Andric   codeview::TypeIndex lowerCompleteTypeUnion(const DICompositeType *Ty);
4470b57cec5SDimitry Andric 
4480b57cec5SDimitry Andric   struct TypeLoweringScope;
4490b57cec5SDimitry Andric 
4500b57cec5SDimitry Andric   void emitDeferredCompleteTypes();
4510b57cec5SDimitry Andric 
4520b57cec5SDimitry Andric   void collectMemberInfo(ClassInfo &Info, const DIDerivedType *DDTy);
4530b57cec5SDimitry Andric   ClassInfo collectClassInfo(const DICompositeType *Ty);
4540b57cec5SDimitry Andric 
4550b57cec5SDimitry Andric   /// Common record member lowering functionality for record types, which are
4560b57cec5SDimitry Andric   /// structs, classes, and unions. Returns the field list index and the member
4570b57cec5SDimitry Andric   /// count.
4580b57cec5SDimitry Andric   std::tuple<codeview::TypeIndex, codeview::TypeIndex, unsigned, bool>
4590b57cec5SDimitry Andric   lowerRecordFieldList(const DICompositeType *Ty);
4600b57cec5SDimitry Andric 
4610b57cec5SDimitry Andric   /// Inserts {{Node, ClassTy}, TI} into TypeIndices and checks for duplicates.
4620b57cec5SDimitry Andric   codeview::TypeIndex recordTypeIndexForDINode(const DINode *Node,
4630b57cec5SDimitry Andric                                                codeview::TypeIndex TI,
4640b57cec5SDimitry Andric                                                const DIType *ClassTy = nullptr);
4650b57cec5SDimitry 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 
4750b57cec5SDimitry Andric   unsigned getPointerSizeInBytes();
4760b57cec5SDimitry Andric 
4770b57cec5SDimitry Andric protected:
4780b57cec5SDimitry Andric   /// Gather pre-function debug information.
4790b57cec5SDimitry Andric   void beginFunctionImpl(const MachineFunction *MF) override;
4800b57cec5SDimitry Andric 
4810b57cec5SDimitry Andric   /// Gather post-function debug information.
4820b57cec5SDimitry Andric   void endFunctionImpl(const MachineFunction *) override;
4830b57cec5SDimitry 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 
4890b57cec5SDimitry Andric public:
4900b57cec5SDimitry Andric   CodeViewDebug(AsmPrinter *AP);
4910b57cec5SDimitry Andric 
492e8d8bef9SDimitry Andric   void beginModule(Module *M) override;
493e8d8bef9SDimitry Andric 
4940b57cec5SDimitry Andric   void setSymbolSize(const MCSymbol *, uint64_t) override {}
4950b57cec5SDimitry Andric 
4960b57cec5SDimitry Andric   /// Emit the COFF section that holds the line table information.
4970b57cec5SDimitry Andric   void endModule() override;
4980b57cec5SDimitry Andric 
4990b57cec5SDimitry Andric   /// Process beginning of an instruction.
5000b57cec5SDimitry Andric   void beginInstruction(const MachineInstr *MI) override;
5010b57cec5SDimitry Andric };
5020b57cec5SDimitry Andric 
503*81ad6265SDimitry Andric template <> struct DenseMapInfo<CodeViewDebug::LocalVarDef> {
504*81ad6265SDimitry Andric 
505*81ad6265SDimitry Andric   static inline CodeViewDebug::LocalVarDef getEmptyKey() {
506*81ad6265SDimitry Andric     return CodeViewDebug::LocalVarDef::createFromOpaqueValue(~0ULL);
507*81ad6265SDimitry Andric   }
508*81ad6265SDimitry Andric 
509*81ad6265SDimitry Andric   static inline CodeViewDebug::LocalVarDef getTombstoneKey() {
510*81ad6265SDimitry Andric     return CodeViewDebug::LocalVarDef::createFromOpaqueValue(~0ULL - 1ULL);
511*81ad6265SDimitry Andric   }
512*81ad6265SDimitry Andric 
513*81ad6265SDimitry Andric   static unsigned getHashValue(const CodeViewDebug::LocalVarDef &DR) {
514*81ad6265SDimitry Andric     return CodeViewDebug::LocalVarDef::toOpaqueValue(DR) * 37ULL;
515*81ad6265SDimitry Andric   }
516*81ad6265SDimitry Andric 
517*81ad6265SDimitry Andric   static bool isEqual(const CodeViewDebug::LocalVarDef &LHS,
518*81ad6265SDimitry Andric                       const CodeViewDebug::LocalVarDef &RHS) {
519*81ad6265SDimitry Andric     return CodeViewDebug::LocalVarDef::toOpaqueValue(LHS) ==
520*81ad6265SDimitry Andric            CodeViewDebug::LocalVarDef::toOpaqueValue(RHS);
521*81ad6265SDimitry Andric   }
522*81ad6265SDimitry Andric };
523*81ad6265SDimitry Andric 
5240b57cec5SDimitry Andric } // end namespace llvm
5250b57cec5SDimitry Andric 
5260b57cec5SDimitry Andric #endif // LLVM_LIB_CODEGEN_ASMPRINTER_CODEVIEWDEBUG_H
527