1fb69e66cSEugene Zelenko //===- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h --------------*- C++ -*-===//
270f5bc99SReid Kleckner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
670f5bc99SReid Kleckner //
770f5bc99SReid Kleckner //===----------------------------------------------------------------------===//
870f5bc99SReid Kleckner //
970f5bc99SReid Kleckner // This file contains support for writing Microsoft CodeView debug info.
1070f5bc99SReid Kleckner //
1170f5bc99SReid Kleckner //===----------------------------------------------------------------------===//
1270f5bc99SReid Kleckner 
1370f5bc99SReid Kleckner #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_CODEVIEWDEBUG_H
1470f5bc99SReid Kleckner #define LLVM_LIB_CODEGEN_ASMPRINTER_CODEVIEWDEBUG_H
1570f5bc99SReid Kleckner 
16fb69e66cSEugene Zelenko #include "llvm/ADT/ArrayRef.h"
1770f5bc99SReid Kleckner #include "llvm/ADT/DenseMap.h"
18fb69e66cSEugene Zelenko #include "llvm/ADT/DenseSet.h"
19fb69e66cSEugene Zelenko #include "llvm/ADT/MapVector.h"
20c2029068SAmy Huang #include "llvm/ADT/PointerUnion.h"
21fb69e66cSEugene Zelenko #include "llvm/ADT/SetVector.h"
22fb69e66cSEugene Zelenko #include "llvm/ADT/SmallVector.h"
2361b189e0SYonghong Song #include "llvm/CodeGen/DbgEntityHistoryCalculator.h"
2461b189e0SYonghong Song #include "llvm/CodeGen/DebugHandlerBase.h"
25fb69e66cSEugene Zelenko #include "llvm/DebugInfo/CodeView/CodeView.h"
26048f8f99SZachary Turner #include "llvm/DebugInfo/CodeView/GlobalTypeTableBuilder.h"
27f3b9ba49SReid Kleckner #include "llvm/DebugInfo/CodeView/TypeIndex.h"
2870f5bc99SReid Kleckner #include "llvm/IR/DebugLoc.h"
29fb69e66cSEugene Zelenko #include "llvm/Support/Allocator.h"
30fb69e66cSEugene Zelenko #include "llvm/Support/Compiler.h"
31fb69e66cSEugene Zelenko #include <cstdint>
32fb69e66cSEugene Zelenko #include <map>
33fb69e66cSEugene Zelenko #include <string>
34fb69e66cSEugene Zelenko #include <tuple>
35fb69e66cSEugene Zelenko #include <unordered_map>
36fb69e66cSEugene Zelenko #include <utility>
37fb69e66cSEugene Zelenko #include <vector>
3870f5bc99SReid Kleckner 
3970f5bc99SReid Kleckner namespace llvm {
40f9c275feSReid Kleckner 
4176c9eb99SAmjad Aboud struct ClassInfo;
42fb69e66cSEugene Zelenko class StringRef;
43fb69e66cSEugene Zelenko class AsmPrinter;
44fb69e66cSEugene Zelenko class Function;
45fb69e66cSEugene Zelenko class GlobalVariable;
46fb69e66cSEugene Zelenko class MCSectionCOFF;
47fb69e66cSEugene Zelenko class MCStreamer;
48fb69e66cSEugene Zelenko class MCSymbol;
49fb69e66cSEugene Zelenko class MachineFunction;
50f9c275feSReid Kleckner 
515f8f34e4SAdrian Prantl /// Collects and handles line tables information in a CodeView format.
52f9c275feSReid Kleckner class LLVM_LIBRARY_VISIBILITY CodeViewDebug : public DebugHandlerBase {
53*98860462SZequan Wu public:
54*98860462SZequan Wu   struct LocalVarDef {
55876330d5SReid Kleckner     /// Indicates that variable data is stored in memory relative to the
56876330d5SReid Kleckner     /// specified register.
57876330d5SReid Kleckner     int InMemory : 1;
58876330d5SReid Kleckner 
59876330d5SReid Kleckner     /// Offset of variable data in memory.
60876330d5SReid Kleckner     int DataOffset : 31;
61876330d5SReid Kleckner 
622b3e6428SReid Kleckner     /// Non-zero if this is a piece of an aggregate.
632b3e6428SReid Kleckner     uint16_t IsSubfield : 1;
642b3e6428SReid Kleckner 
652b3e6428SReid Kleckner     /// Offset into aggregate.
662b3e6428SReid Kleckner     uint16_t StructOffset : 15;
67876330d5SReid Kleckner 
68876330d5SReid Kleckner     /// Register containing the data or the register base of the memory
69876330d5SReid Kleckner     /// location containing the data.
70876330d5SReid Kleckner     uint16_t CVRegister;
71876330d5SReid Kleckner 
toOpaqueValueLocalVarDef72*98860462SZequan Wu     uint64_t static toOpaqueValue(const LocalVarDef DR) {
73*98860462SZequan Wu       uint64_t Val = 0;
74*98860462SZequan Wu       std::memcpy(&Val, &DR, sizeof(Val));
75*98860462SZequan Wu       return Val;
76876330d5SReid Kleckner     }
77876330d5SReid Kleckner 
createFromOpaqueValueLocalVarDef78*98860462SZequan Wu     LocalVarDef static createFromOpaqueValue(uint64_t Val) {
79*98860462SZequan Wu       LocalVarDef DR;
80*98860462SZequan Wu       std::memcpy(&DR, &Val, sizeof(Val));
81*98860462SZequan Wu       return DR;
82*98860462SZequan Wu     }
83876330d5SReid Kleckner   };
84876330d5SReid Kleckner 
85*98860462SZequan Wu   static_assert(sizeof(uint64_t) == sizeof(LocalVarDef), "");
86*98860462SZequan Wu 
87*98860462SZequan Wu private:
88*98860462SZequan Wu   MCStreamer &OS;
89*98860462SZequan Wu   BumpPtrAllocator Allocator;
90*98860462SZequan Wu   codeview::GlobalTypeTableBuilder TypeTable;
91*98860462SZequan Wu 
92*98860462SZequan Wu   /// Whether to emit type record hashes into .debug$H.
93*98860462SZequan Wu   bool EmitDebugGlobalHashes = false;
94*98860462SZequan Wu 
95*98860462SZequan Wu   /// The codeview CPU type used by the translation unit.
96*98860462SZequan Wu   codeview::CPUType TheCPU;
97*98860462SZequan Wu 
98*98860462SZequan Wu   static LocalVarDef createDefRangeMem(uint16_t CVRegister, int Offset);
99876330d5SReid Kleckner 
100f9c275feSReid Kleckner   /// Similar to DbgVariable in DwarfDebug, but not dwarf-specific.
101f9c275feSReid Kleckner   struct LocalVariable {
102f9c275feSReid Kleckner     const DILocalVariable *DIVar = nullptr;
103*98860462SZequan Wu     MapVector<LocalVarDef,
104*98860462SZequan Wu               SmallVector<std::pair<const MCSymbol *, const MCSymbol *>, 1>>
105*98860462SZequan Wu         DefRanges;
10608f5fd51SReid Kleckner     bool UseReferenceType = false;
107f9c275feSReid Kleckner   };
10870f5bc99SReid Kleckner 
109b17464e4SBrock Wyma   struct CVGlobalVariable {
110b17464e4SBrock Wyma     const DIGlobalVariable *DIGV;
111c2029068SAmy Huang     PointerUnion<const GlobalVariable *, const DIExpression *> GVInfo;
112b17464e4SBrock Wyma   };
113b17464e4SBrock Wyma 
114f3b9ba49SReid Kleckner   struct InlineSite {
115f9c275feSReid Kleckner     SmallVector<LocalVariable, 1> InlinedLocals;
116f9c275feSReid Kleckner     SmallVector<const DILocation *, 1> ChildSites;
117f3b9ba49SReid Kleckner     const DISubprogram *Inlinee = nullptr;
118c29b4f07SReid Kleckner 
119c29b4f07SReid Kleckner     /// The ID of the inline site or function used with .cv_loc. Not a type
120c29b4f07SReid Kleckner     /// index.
121f3b9ba49SReid Kleckner     unsigned SiteFuncId = 0;
122f3b9ba49SReid Kleckner   };
123f3b9ba49SReid Kleckner 
1245a791ee4SReid Kleckner   // Combines information from DILexicalBlock and LexicalScope.
1255a791ee4SReid Kleckner   struct LexicalBlock {
1265a791ee4SReid Kleckner     SmallVector<LocalVariable, 1> Locals;
127b17464e4SBrock Wyma     SmallVector<CVGlobalVariable, 1> Globals;
1285a791ee4SReid Kleckner     SmallVector<LexicalBlock *, 1> Children;
1295a791ee4SReid Kleckner     const MCSymbol *Begin;
1305a791ee4SReid Kleckner     const MCSymbol *End;
1315a791ee4SReid Kleckner     StringRef Name;
1325a791ee4SReid Kleckner   };
1335a791ee4SReid Kleckner 
13470f5bc99SReid Kleckner   // For each function, store a vector of labels to its instructions, as well as
13570f5bc99SReid Kleckner   // to the end of the function.
13670f5bc99SReid Kleckner   struct FunctionInfo {
13755baeefdSReid Kleckner     FunctionInfo() = default;
13855baeefdSReid Kleckner 
13955baeefdSReid Kleckner     // Uncopyable.
14055baeefdSReid Kleckner     FunctionInfo(const FunctionInfo &FI) = delete;
14155baeefdSReid Kleckner 
142f3b9ba49SReid Kleckner     /// Map from inlined call site to inlined instructions and child inlined
143f3b9ba49SReid Kleckner     /// call sites. Listed in program order.
144f9c275feSReid Kleckner     std::unordered_map<const DILocation *, InlineSite> InlineSites;
145f9c275feSReid Kleckner 
146f9c275feSReid Kleckner     /// Ordered list of top-level inlined call sites.
147f9c275feSReid Kleckner     SmallVector<const DILocation *, 1> ChildSites;
148f9c275feSReid Kleckner 
149f9c275feSReid Kleckner     SmallVector<LocalVariable, 1> Locals;
150b17464e4SBrock Wyma     SmallVector<CVGlobalVariable, 1> Globals;
151f3b9ba49SReid Kleckner 
1525a791ee4SReid Kleckner     std::unordered_map<const DILexicalBlockBase*, LexicalBlock> LexicalBlocks;
1535a791ee4SReid Kleckner 
1545a791ee4SReid Kleckner     // Lexical blocks containing local variables.
1555a791ee4SReid Kleckner     SmallVector<LexicalBlock *, 1> ChildBlocks;
1565a791ee4SReid Kleckner 
157e33c94f1SReid Kleckner     std::vector<std::pair<MCSymbol *, MDNode *>> Annotations;
15874204304SAmy Huang     std::vector<std::tuple<const MCSymbol *, const MCSymbol *, const DIType *>>
159f332fe64SAmy Huang         HeapAllocSites;
160e33c94f1SReid Kleckner 
1611fcd610cSReid Kleckner     const MCSymbol *Begin = nullptr;
1621fcd610cSReid Kleckner     const MCSymbol *End = nullptr;
1632214ed89SReid Kleckner     unsigned FuncId = 0;
164f3b9ba49SReid Kleckner     unsigned LastFileId = 0;
1659ea2c012SReid Kleckner 
1669ea2c012SReid Kleckner     /// Number of bytes allocated in the prologue for all local stack objects.
1679ea2c012SReid Kleckner     unsigned FrameSize = 0;
1689ea2c012SReid Kleckner 
1699ea2c012SReid Kleckner     /// Number of bytes of parameters on the stack.
1709ea2c012SReid Kleckner     unsigned ParamSize = 0;
1719ea2c012SReid Kleckner 
1729ea2c012SReid Kleckner     /// Number of bytes pushed to save CSRs.
1739ea2c012SReid Kleckner     unsigned CSRSize = 0;
1749ea2c012SReid Kleckner 
1752bcb288aSReid Kleckner     /// Adjustment to apply on x86 when using the VFRAME frame pointer.
1762bcb288aSReid Kleckner     int OffsetAdjustment = 0;
1772bcb288aSReid Kleckner 
1789ea2c012SReid Kleckner     /// Two-bit value indicating which register is the designated frame pointer
1799ea2c012SReid Kleckner     /// register for local variables. Included in S_FRAMEPROC.
1809ea2c012SReid Kleckner     codeview::EncodedFramePtrReg EncodedLocalFramePtrReg =
1819ea2c012SReid Kleckner         codeview::EncodedFramePtrReg::None;
1829ea2c012SReid Kleckner 
1839ea2c012SReid Kleckner     /// Two-bit value indicating which register is the designated frame pointer
1849ea2c012SReid Kleckner     /// register for stack parameters. Included in S_FRAMEPROC.
1859ea2c012SReid Kleckner     codeview::EncodedFramePtrReg EncodedParamFramePtrReg =
1869ea2c012SReid Kleckner         codeview::EncodedFramePtrReg::None;
1879ea2c012SReid Kleckner 
1889ea2c012SReid Kleckner     codeview::FrameProcedureOptions FrameProcOpts;
1899ea2c012SReid Kleckner 
190d5e4ec74SReid Kleckner     bool HasStackRealignment = false;
191d5e4ec74SReid Kleckner 
1922214ed89SReid Kleckner     bool HaveLineInfo = false;
1939533af4fSReid Kleckner   };
194fb69e66cSEugene Zelenko   FunctionInfo *CurFn = nullptr;
19570f5bc99SReid Kleckner 
1962ed29d87SChih-Ping Chen   codeview::SourceLanguage CurrentSourceLanguage =
1972ed29d87SChih-Ping Chen       codeview::SourceLanguage::Masm;
1982ed29d87SChih-Ping Chen 
1992ed29d87SChih-Ping Chen   // This map records the constant offset in DIExpression of the
2002ed29d87SChih-Ping Chen   // DIGlobalVariableExpression referencing the DIGlobalVariable.
2012ed29d87SChih-Ping Chen   DenseMap<const DIGlobalVariable *, uint64_t> CVGlobalVariableOffsets;
2022ed29d87SChih-Ping Chen 
2035a791ee4SReid Kleckner   // Map used to seperate variables according to the lexical scope they belong
2045a791ee4SReid Kleckner   // in.  This is populated by recordLocalVariable() before
2055a791ee4SReid Kleckner   // collectLexicalBlocks() separates the variables between the FunctionInfo
2065a791ee4SReid Kleckner   // and LexicalBlocks.
2075a791ee4SReid Kleckner   DenseMap<const LexicalScope *, SmallVector<LocalVariable, 1>> ScopeVariables;
2085a791ee4SReid Kleckner 
209b17464e4SBrock Wyma   // Map to separate global variables according to the lexical scope they
210b17464e4SBrock Wyma   // belong in. A null local scope represents the global scope.
211b17464e4SBrock Wyma   typedef SmallVector<CVGlobalVariable, 1> GlobalVariableList;
212b17464e4SBrock Wyma   DenseMap<const DIScope*, std::unique_ptr<GlobalVariableList> > ScopeGlobals;
213b17464e4SBrock Wyma 
214b17464e4SBrock Wyma   // Array of global variables which  need to be emitted into a COMDAT section.
215b17464e4SBrock Wyma   SmallVector<CVGlobalVariable, 1> ComdatVariables;
216b17464e4SBrock Wyma 
217b17464e4SBrock Wyma   // Array of non-COMDAT global variables.
218b17464e4SBrock Wyma   SmallVector<CVGlobalVariable, 1> GlobalVariables;
219b17464e4SBrock Wyma 
2207669f3c0SAmy Huang   /// List of static const data members to be emitted as S_CONSTANTs.
2217669f3c0SAmy Huang   SmallVector<const DIDerivedType *, 4> StaticConstMembers;
2227669f3c0SAmy Huang 
2235d122f87SReid Kleckner   /// The set of comdat .debug$S sections that we've seen so far. Each section
2245d122f87SReid Kleckner   /// must start with a magic version number that must only be emitted once.
2255d122f87SReid Kleckner   /// This set tracks which sections we've already opened.
2265d122f87SReid Kleckner   DenseSet<MCSectionCOFF *> ComdatDebugSections;
2275d122f87SReid Kleckner 
2285d122f87SReid Kleckner   /// Switch to the appropriate .debug$S section for GVSym. If GVSym, the symbol
2295d122f87SReid Kleckner   /// of an emitted global value, is in a comdat COFF section, this will switch
2305d122f87SReid Kleckner   /// to a new .debug$S section in that comdat. This method ensures that the
2315d122f87SReid Kleckner   /// section starts with the magic version number on first use. If GVSym is
2325d122f87SReid Kleckner   /// null, uses the main .debug$S section.
2335d122f87SReid Kleckner   void switchToDebugSectionForSymbol(const MCSymbol *GVSym);
2345d122f87SReid Kleckner 
235fbd7787dSReid Kleckner   /// The next available function index for use with our .cv_* directives. Not
236fbd7787dSReid Kleckner   /// to be confused with type indices for LF_FUNC_ID records.
2372214ed89SReid Kleckner   unsigned NextFuncId = 0;
23870f5bc99SReid Kleckner 
239876330d5SReid Kleckner   InlineSite &getInlineSite(const DILocation *InlinedAt,
240876330d5SReid Kleckner                             const DISubprogram *Inlinee);
241f3b9ba49SReid Kleckner 
24275c3ebfaSDavid Majnemer   codeview::TypeIndex getFuncIdForSubprogram(const DISubprogram *SP);
2432280f932SReid Kleckner 
244223303c5SBob Haarman   void calculateRanges(LocalVariable &Var,
2456feef56dSDavid Stenberg                        const DbgValueHistoryMap::Entries &Entries);
246223303c5SBob Haarman 
2472214ed89SReid Kleckner   /// Remember some debug info about each function. Keep it in a stable order to
2482214ed89SReid Kleckner   /// emit at the end of the TU.
24955baeefdSReid Kleckner   MapVector<const Function *, std::unique_ptr<FunctionInfo>> FnDebugInfo;
25070f5bc99SReid Kleckner 
251bc6f52daSReid Kleckner   /// Map from full file path to .cv_file id. Full paths are built from DIFiles
252bc6f52daSReid Kleckner   /// and are stored in FileToFilepathMap;
253bc6f52daSReid Kleckner   DenseMap<StringRef, unsigned> FileIdMap;
25470f5bc99SReid Kleckner 
255fbd7787dSReid Kleckner   /// All inlined subprograms in the order they should be emitted.
2562280f932SReid Kleckner   SmallSetVector<const DISubprogram *, 4> InlinedSubprograms;
257f3b9ba49SReid Kleckner 
25876c9eb99SAmjad Aboud   /// Map from a pair of DI metadata nodes and its DI type (or scope) that can
25976c9eb99SAmjad Aboud   /// be nullptr, to CodeView type indices. Primarily indexed by
26076c9eb99SAmjad Aboud   /// {DIType*, DIType*} and {DISubprogram*, DIType*}.
26176c9eb99SAmjad Aboud   ///
26276c9eb99SAmjad Aboud   /// The second entry in the key is needed for methods as DISubroutineType
26376c9eb99SAmjad Aboud   /// representing static method type are shared with non-method function type.
26476c9eb99SAmjad Aboud   DenseMap<std::pair<const DINode *, const DIType *>, codeview::TypeIndex>
26576c9eb99SAmjad Aboud       TypeIndices;
266f3b9ba49SReid Kleckner 
267a8d57407SReid Kleckner   /// Map from DICompositeType* to complete type index. Non-record types are
268a8d57407SReid Kleckner   /// always looked up in the normal TypeIndices map.
269a8d57407SReid Kleckner   DenseMap<const DICompositeType *, codeview::TypeIndex> CompleteTypeIndices;
270a8d57407SReid Kleckner 
271643dd836SReid Kleckner   /// Complete record types to emit after all active type lowerings are
272643dd836SReid Kleckner   /// finished.
273643dd836SReid Kleckner   SmallVector<const DICompositeType *, 4> DeferredCompleteTypes;
274643dd836SReid Kleckner 
275643dd836SReid Kleckner   /// Number of type lowering frames active on the stack.
276643dd836SReid Kleckner   unsigned TypeEmissionLevel = 0;
277643dd836SReid Kleckner 
2789f7f3e1eSReid Kleckner   codeview::TypeIndex VBPType;
2799f7f3e1eSReid Kleckner 
2803128b10cSDavid Majnemer   const DISubprogram *CurrentSubprogram = nullptr;
2813128b10cSDavid Majnemer 
2823128b10cSDavid Majnemer   // The UDTs we have seen while processing types; each entry is a pair of type
2833128b10cSDavid Majnemer   // index and type name.
284a7b04174SZachary Turner   std::vector<std::pair<std::string, const DIType *>> LocalUDTs;
285a7b04174SZachary Turner   std::vector<std::pair<std::string, const DIType *>> GlobalUDTs;
2863128b10cSDavid Majnemer 
287fb69e66cSEugene Zelenko   using FileToFilepathMapTy = std::map<const DIFile *, std::string>;
2889533af4fSReid Kleckner   FileToFilepathMapTy FileToFilepathMap;
289fb69e66cSEugene Zelenko 
290cb0bab86SFangrui Song   StringRef getFullFilepath(const DIFile *File);
29170f5bc99SReid Kleckner 
2922214ed89SReid Kleckner   unsigned maybeRecordFile(const DIFile *F);
2932214ed89SReid Kleckner 
294bdc4956bSBenjamin Kramer   void maybeRecordLocation(const DebugLoc &DL, const MachineFunction *MF);
29570f5bc99SReid Kleckner 
29676c9eb99SAmjad Aboud   void clear();
2973128b10cSDavid Majnemer 
setCurrentSubprogram(const DISubprogram * SP)2983128b10cSDavid Majnemer   void setCurrentSubprogram(const DISubprogram *SP) {
2993128b10cSDavid Majnemer     CurrentSubprogram = SP;
3003128b10cSDavid Majnemer     LocalUDTs.clear();
30170f5bc99SReid Kleckner   }
30270f5bc99SReid Kleckner 
3035d122f87SReid Kleckner   /// Emit the magic version number at the start of a CodeView type or symbol
304d9e96741SAlexandre Ganea   /// section. Appears at the front of every .debug$S or .debug$T or .debug$P
305d9e96741SAlexandre Ganea   /// section.
3065d122f87SReid Kleckner   void emitCodeViewMagicVersion();
3075d122f87SReid Kleckner 
308f3b9ba49SReid Kleckner   void emitTypeInformation();
309f3b9ba49SReid Kleckner 
310048f8f99SZachary Turner   void emitTypeGlobalHashes();
311048f8f99SZachary Turner 
312a282ea48SAlexandre Ganea   void emitObjName();
313a282ea48SAlexandre Ganea 
314c64acfd4SAdrian McCarthy   void emitCompilerInformation();
315c64acfd4SAdrian McCarthy 
316810687cbSReid Kleckner   void emitBuildInfo();
317810687cbSReid Kleckner 
3185d122f87SReid Kleckner   void emitInlineeLinesSubsection();
3191fcd610cSReid Kleckner 
32094ece8fbSBrock Wyma   void emitDebugInfoForThunk(const Function *GV,
32194ece8fbSBrock Wyma                              FunctionInfo &FI,
32294ece8fbSBrock Wyma                              const MCSymbol *Fn);
32394ece8fbSBrock Wyma 
3242214ed89SReid Kleckner   void emitDebugInfoForFunction(const Function *GV, FunctionInfo &FI);
32570f5bc99SReid Kleckner 
326b510b458SHans Wennborg   void emitDebugInfoForRetainedTypes();
327b510b458SHans Wennborg 
32847cc6db9SReid Kleckner   void emitDebugInfoForUDTs(
32947cc6db9SReid Kleckner       const std::vector<std::pair<std::string, const DIType *>> &UDTs);
3303128b10cSDavid Majnemer 
3317669f3c0SAmy Huang   void collectDebugInfoForGlobals();
332b17464e4SBrock Wyma   void emitDebugInfoForGlobals();
333b17464e4SBrock Wyma   void emitGlobalVariableList(ArrayRef<CVGlobalVariable> Globals);
334f84c70a3SMatheus Izvekov   void emitConstantSymbolRecord(const DIType *DTy, APSInt &Value,
335f84c70a3SMatheus Izvekov                                 const std::string &QualifiedName);
336c2029068SAmy Huang   void emitDebugInfoForGlobal(const CVGlobalVariable &CVGV);
3377669f3c0SAmy Huang   void emitStaticConstMemberList();
3386f3406dfSReid Kleckner 
3396f3406dfSReid Kleckner   /// Opens a subsection of the given kind in a .debug$S codeview section.
3406f3406dfSReid Kleckner   /// Returns an end label for use with endCVSubsection when the subsection is
3416f3406dfSReid Kleckner   /// finished.
3428c099fe0SZachary Turner   MCSymbol *beginCVSubsection(codeview::DebugSubsectionKind Kind);
3436f3406dfSReid Kleckner   void endCVSubsection(MCSymbol *EndLabel);
3446f3406dfSReid Kleckner 
3455bf71d11SReid Kleckner   /// Opens a symbol record of the given kind. Returns an end label for use with
3465bf71d11SReid Kleckner   /// endSymbolRecord.
3475bf71d11SReid Kleckner   MCSymbol *beginSymbolRecord(codeview::SymbolKind Kind);
3485bf71d11SReid Kleckner   void endSymbolRecord(MCSymbol *SymEnd);
3495bf71d11SReid Kleckner 
3505bf71d11SReid Kleckner   /// Emits an S_END, S_INLINESITE_END, or S_PROC_ID_END record. These records
3515bf71d11SReid Kleckner   /// are empty, so we emit them with a simpler assembly sequence that doesn't
3525bf71d11SReid Kleckner   /// involve labels.
3535bf71d11SReid Kleckner   void emitEndSymbolRecord(codeview::SymbolKind EndKind);
3545bf71d11SReid Kleckner 
355f3b9ba49SReid Kleckner   void emitInlinedCallSite(const FunctionInfo &FI, const DILocation *InlinedAt,
356f3b9ba49SReid Kleckner                            const InlineSite &Site);
357f3b9ba49SReid Kleckner 
358760c1ab1SHsiangkai Wang   using InlinedEntity = DbgValueHistoryMap::InlinedEntity;
359876330d5SReid Kleckner 
360b17464e4SBrock Wyma   void collectGlobalVariableInfo();
361876330d5SReid Kleckner   void collectVariableInfo(const DISubprogram *SP);
362876330d5SReid Kleckner 
363760c1ab1SHsiangkai Wang   void collectVariableInfoFromMFTable(DenseSet<InlinedEntity> &Processed);
364876330d5SReid Kleckner 
3655a791ee4SReid Kleckner   // Construct the lexical block tree for a routine, pruning emptpy lexical
3665a791ee4SReid Kleckner   // scopes, and populate it with local variables.
3675a791ee4SReid Kleckner   void collectLexicalBlockInfo(SmallVectorImpl<LexicalScope *> &Scopes,
3685a791ee4SReid Kleckner                                SmallVectorImpl<LexicalBlock *> &Blocks,
369b17464e4SBrock Wyma                                SmallVectorImpl<LocalVariable> &Locals,
370b17464e4SBrock Wyma                                SmallVectorImpl<CVGlobalVariable> &Globals);
3715a791ee4SReid Kleckner   void collectLexicalBlockInfo(LexicalScope &Scope,
3725a791ee4SReid Kleckner                                SmallVectorImpl<LexicalBlock *> &ParentBlocks,
373b17464e4SBrock Wyma                                SmallVectorImpl<LocalVariable> &ParentLocals,
374b17464e4SBrock Wyma                                SmallVectorImpl<CVGlobalVariable> &ParentGlobals);
3755a791ee4SReid Kleckner 
376876330d5SReid Kleckner   /// Records information about a local variable in the appropriate scope. In
377876330d5SReid Kleckner   /// particular, locals from inlined code live inside the inlining site.
3785a791ee4SReid Kleckner   void recordLocalVariable(LocalVariable &&Var, const LexicalScope *LS);
379f9c275feSReid Kleckner 
38010dd55c5SReid Kleckner   /// Emits local variables in the appropriate order.
3819ea2c012SReid Kleckner   void emitLocalVariableList(const FunctionInfo &FI,
3829ea2c012SReid Kleckner                              ArrayRef<LocalVariable> Locals);
38310dd55c5SReid Kleckner 
38410dd55c5SReid Kleckner   /// Emits an S_LOCAL record and its associated defined ranges.
3859ea2c012SReid Kleckner   void emitLocalVariable(const FunctionInfo &FI, const LocalVariable &Var);
386f9c275feSReid Kleckner 
3875a791ee4SReid Kleckner   /// Emits a sequence of lexical block scopes and their children.
3885a791ee4SReid Kleckner   void emitLexicalBlockList(ArrayRef<LexicalBlock *> Blocks,
3895a791ee4SReid Kleckner                             const FunctionInfo& FI);
3905a791ee4SReid Kleckner 
3915a791ee4SReid Kleckner   /// Emit a lexical block scope and its children.
3925a791ee4SReid Kleckner   void emitLexicalBlock(const LexicalBlock &Block, const FunctionInfo& FI);
3935a791ee4SReid Kleckner 
3945acacbb0SReid Kleckner   /// Translates the DIType to codeview if necessary and returns a type index
3955acacbb0SReid Kleckner   /// for it.
396da82ce99SFangrui Song   codeview::TypeIndex getTypeIndex(const DIType *Ty,
397da82ce99SFangrui Song                                    const DIType *ClassTy = nullptr);
3985acacbb0SReid Kleckner 
399c68f8957SZachary Turner   codeview::TypeIndex
400c168c6f8SReid Kleckner   getTypeIndexForThisPtr(const DIDerivedType *PtrTy,
401c68f8957SZachary Turner                          const DISubroutineType *SubroutineTy);
402c68f8957SZachary Turner 
403da82ce99SFangrui Song   codeview::TypeIndex getTypeIndexForReferenceTo(const DIType *Ty);
404223303c5SBob Haarman 
4050c5d874bSReid Kleckner   codeview::TypeIndex getMemberFunctionType(const DISubprogram *SP,
4060c5d874bSReid Kleckner                                             const DICompositeType *Class);
4070c5d874bSReid Kleckner 
4080c5d874bSReid Kleckner   codeview::TypeIndex getScopeIndex(const DIScope *Scope);
4090c5d874bSReid Kleckner 
4109f7f3e1eSReid Kleckner   codeview::TypeIndex getVBPTypeIndex();
4119f7f3e1eSReid Kleckner 
412a7b04174SZachary Turner   void addToUDTs(const DIType *Ty);
4134b63a98dSHans Wennborg 
414122d9e79SAaron Smith   void addUDTSrcLine(const DIType *Ty, codeview::TypeIndex TI);
415122d9e79SAaron Smith 
41676c9eb99SAmjad Aboud   codeview::TypeIndex lowerType(const DIType *Ty, const DIType *ClassTy);
417d065e23dSDavid Majnemer   codeview::TypeIndex lowerTypeAlias(const DIDerivedType *Ty);
418f3c3c132SAdrian McCarthy   codeview::TypeIndex lowerTypeArray(const DICompositeType *Ty);
4192ed29d87SChih-Ping Chen   codeview::TypeIndex lowerTypeString(const DIStringType *Ty);
4205acacbb0SReid Kleckner   codeview::TypeIndex lowerTypeBasic(const DIBasicType *Ty);
4213acdc677SReid Kleckner   codeview::TypeIndex lowerTypePointer(
4223acdc677SReid Kleckner       const DIDerivedType *Ty,
4233acdc677SReid Kleckner       codeview::PointerOptions PO = codeview::PointerOptions::None);
4243acdc677SReid Kleckner   codeview::TypeIndex lowerTypeMemberPointer(
4253acdc677SReid Kleckner       const DIDerivedType *Ty,
4263acdc677SReid Kleckner       codeview::PointerOptions PO = codeview::PointerOptions::None);
4275acacbb0SReid Kleckner   codeview::TypeIndex lowerTypeModifier(const DIDerivedType *Ty);
42875c3ebfaSDavid Majnemer   codeview::TypeIndex lowerTypeFunction(const DISubroutineType *Ty);
4299dac4731SReid Kleckner   codeview::TypeIndex lowerTypeVFTableShape(const DIDerivedType *Ty);
430802b033dSAaron Smith   codeview::TypeIndex lowerTypeMemberFunction(
431802b033dSAaron Smith       const DISubroutineType *Ty, const DIType *ClassTy, int ThisAdjustment,
432802b033dSAaron Smith       bool IsStaticMethod,
433802b033dSAaron Smith       codeview::FunctionOptions FO = codeview::FunctionOptions::None);
434979cb888SDavid Majnemer   codeview::TypeIndex lowerTypeEnum(const DICompositeType *Ty);
435a8d57407SReid Kleckner   codeview::TypeIndex lowerTypeClass(const DICompositeType *Ty);
436a8d57407SReid Kleckner   codeview::TypeIndex lowerTypeUnion(const DICompositeType *Ty);
437a8d57407SReid Kleckner 
438a8d57407SReid Kleckner   /// Symbol records should point to complete types, but type records should
439a8d57407SReid Kleckner   /// always point to incomplete types to avoid cycles in the type graph. Only
440a8d57407SReid Kleckner   /// use this entry point when generating symbol records. The complete and
441a8d57407SReid Kleckner   /// incomplete type indices only differ for record types. All other types use
442a8d57407SReid Kleckner   /// the same index.
443da82ce99SFangrui Song   codeview::TypeIndex getCompleteTypeIndex(const DIType *Ty);
444a8d57407SReid Kleckner 
445a8d57407SReid Kleckner   codeview::TypeIndex lowerCompleteTypeClass(const DICompositeType *Ty);
446a8d57407SReid Kleckner   codeview::TypeIndex lowerCompleteTypeUnion(const DICompositeType *Ty);
447a8d57407SReid Kleckner 
448643dd836SReid Kleckner   struct TypeLoweringScope;
449643dd836SReid Kleckner 
450643dd836SReid Kleckner   void emitDeferredCompleteTypes();
451643dd836SReid Kleckner 
45276c9eb99SAmjad Aboud   void collectMemberInfo(ClassInfo &Info, const DIDerivedType *DDTy);
4531ab7eac8SReid Kleckner   ClassInfo collectClassInfo(const DICompositeType *Ty);
45476c9eb99SAmjad Aboud 
455a8d57407SReid Kleckner   /// Common record member lowering functionality for record types, which are
456a8d57407SReid Kleckner   /// structs, classes, and unions. Returns the field list index and the member
457a8d57407SReid Kleckner   /// count.
458820ca540SAdrian McCarthy   std::tuple<codeview::TypeIndex, codeview::TypeIndex, unsigned, bool>
459a8d57407SReid Kleckner   lowerRecordFieldList(const DICompositeType *Ty);
460a8d57407SReid Kleckner 
46176c9eb99SAmjad Aboud   /// Inserts {{Node, ClassTy}, TI} into TypeIndices and checks for duplicates.
4620c5d874bSReid Kleckner   codeview::TypeIndex recordTypeIndexForDINode(const DINode *Node,
4630c5d874bSReid Kleckner                                                codeview::TypeIndex TI,
46476c9eb99SAmjad Aboud                                                const DIType *ClassTy = nullptr);
46576c9eb99SAmjad Aboud 
4662b8c6accSAmy Huang   /// Collect the names of parent scopes, innermost to outermost. Return the
4672b8c6accSAmy Huang   /// innermost subprogram scope if present. Ensure that parent type scopes are
4682b8c6accSAmy Huang   /// inserted into the type table.
4692b8c6accSAmy Huang   const DISubprogram *
4702b8c6accSAmy Huang   collectParentScopeNames(const DIScope *Scope,
4712b8c6accSAmy Huang                           SmallVectorImpl<StringRef> &ParentScopeNames);
4722b8c6accSAmy Huang   std::string getFullyQualifiedName(const DIScope *Scope, StringRef Name);
4732b8c6accSAmy Huang   std::string getFullyQualifiedName(const DIScope *Scope);
4742b8c6accSAmy Huang 
47576c9eb99SAmjad Aboud   unsigned getPointerSizeInBytes();
4765acacbb0SReid Kleckner 
477b2fbb4b2SDavid Blaikie protected:
4785f8f34e4SAdrian Prantl   /// Gather pre-function debug information.
479b2fbb4b2SDavid Blaikie   void beginFunctionImpl(const MachineFunction *MF) override;
480b2fbb4b2SDavid Blaikie 
4815f8f34e4SAdrian Prantl   /// Gather post-function debug information.
482b2fbb4b2SDavid Blaikie   void endFunctionImpl(const MachineFunction *) override;
483b2fbb4b2SDavid Blaikie 
4842ed29d87SChih-Ping Chen   /// Check if the current module is in Fortran.
moduleIsInFortran()4852ed29d87SChih-Ping Chen   bool moduleIsInFortran() {
4862ed29d87SChih-Ping Chen     return CurrentSourceLanguage == codeview::SourceLanguage::Fortran;
4872ed29d87SChih-Ping Chen   }
4882ed29d87SChih-Ping Chen 
48970f5bc99SReid Kleckner public:
490cb0bab86SFangrui Song   CodeViewDebug(AsmPrinter *AP);
49170f5bc99SReid Kleckner 
492a0ad066cSJameson Nash   void beginModule(Module *M) override;
493a0ad066cSJameson Nash 
setSymbolSize(const MCSymbol *,uint64_t)494fb69e66cSEugene Zelenko   void setSymbolSize(const MCSymbol *, uint64_t) override {}
49570f5bc99SReid Kleckner 
4965f8f34e4SAdrian Prantl   /// Emit the COFF section that holds the line table information.
49770f5bc99SReid Kleckner   void endModule() override;
49870f5bc99SReid Kleckner 
4995f8f34e4SAdrian Prantl   /// Process beginning of an instruction.
50070f5bc99SReid Kleckner   void beginInstruction(const MachineInstr *MI) override;
50170f5bc99SReid Kleckner };
50270f5bc99SReid Kleckner 
503*98860462SZequan Wu template <> struct DenseMapInfo<CodeViewDebug::LocalVarDef> {
504*98860462SZequan Wu 
505*98860462SZequan Wu   static inline CodeViewDebug::LocalVarDef getEmptyKey() {
506*98860462SZequan Wu     return CodeViewDebug::LocalVarDef::createFromOpaqueValue(~0ULL);
507*98860462SZequan Wu   }
508*98860462SZequan Wu 
509*98860462SZequan Wu   static inline CodeViewDebug::LocalVarDef getTombstoneKey() {
510*98860462SZequan Wu     return CodeViewDebug::LocalVarDef::createFromOpaqueValue(~0ULL - 1ULL);
511*98860462SZequan Wu   }
512*98860462SZequan Wu 
513*98860462SZequan Wu   static unsigned getHashValue(const CodeViewDebug::LocalVarDef &DR) {
514*98860462SZequan Wu     return CodeViewDebug::LocalVarDef::toOpaqueValue(DR) * 37ULL;
515*98860462SZequan Wu   }
516*98860462SZequan Wu 
517*98860462SZequan Wu   static bool isEqual(const CodeViewDebug::LocalVarDef &LHS,
518*98860462SZequan Wu                       const CodeViewDebug::LocalVarDef &RHS) {
519*98860462SZequan Wu     return CodeViewDebug::LocalVarDef::toOpaqueValue(LHS) ==
520*98860462SZequan Wu            CodeViewDebug::LocalVarDef::toOpaqueValue(RHS);
521*98860462SZequan Wu   }
522*98860462SZequan Wu };
523*98860462SZequan Wu 
524fb69e66cSEugene Zelenko } // end namespace llvm
525fb69e66cSEugene Zelenko 
526fb69e66cSEugene Zelenko #endif // LLVM_LIB_CODEGEN_ASMPRINTER_CODEVIEWDEBUG_H
527