1 //===-- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h ----*- C++ -*--===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains support for writing Microsoft CodeView debug info.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_CODEVIEWDEBUG_H
15 #define LLVM_LIB_CODEGEN_ASMPRINTER_CODEVIEWDEBUG_H
16 
17 #include "AsmPrinterHandler.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/CodeGen/AsmPrinter.h"
22 #include "llvm/CodeGen/LexicalScopes.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/MachineModuleInfo.h"
25 #include "llvm/DebugInfo/CodeView/TypeIndex.h"
26 #include "llvm/IR/DebugInfo.h"
27 #include "llvm/IR/DebugLoc.h"
28 #include "llvm/MC/MCStreamer.h"
29 #include "llvm/Target/TargetLoweringObjectFile.h"
30 
31 namespace llvm {
32 /// \brief Collects and handles line tables information in a CodeView format.
33 class LLVM_LIBRARY_VISIBILITY CodeViewDebug : public AsmPrinterHandler {
34   AsmPrinter *Asm;
35   MCStreamer &OS;
36   DebugLoc PrevInstLoc;
37 
38   struct InlineSite {
39     TinyPtrVector<const DILocation *> ChildSites;
40     const DISubprogram *Inlinee = nullptr;
41     unsigned SiteFuncId = 0;
42   };
43 
44   // For each function, store a vector of labels to its instructions, as well as
45   // to the end of the function.
46   struct FunctionInfo {
47     /// Map from inlined call site to inlined instructions and child inlined
48     /// call sites. Listed in program order.
49     MapVector<const DILocation *, InlineSite> InlineSites;
50 
51     DebugLoc LastLoc;
52     const MCSymbol *Begin = nullptr;
53     const MCSymbol *End = nullptr;
54     unsigned FuncId = 0;
55     unsigned LastFileId = 0;
56     bool HaveLineInfo = false;
57   };
58   FunctionInfo *CurFn;
59 
60   unsigned NextFuncId = 0;
61 
62   InlineSite &getInlineSite(const DILocation *Loc);
63 
64   static void collectInlineSiteChildren(SmallVectorImpl<unsigned> &Children,
65                                         const FunctionInfo &FI,
66                                         const InlineSite &Site);
67 
68   /// Remember some debug info about each function. Keep it in a stable order to
69   /// emit at the end of the TU.
70   MapVector<const Function *, FunctionInfo> FnDebugInfo;
71 
72   /// Map from DIFile to .cv_file id.
73   DenseMap<const DIFile *, unsigned> FileIdMap;
74 
75   SmallSetVector<const DISubprogram *, 4> InlinedSubprograms;
76 
77   DenseMap<const DISubprogram *, codeview::TypeIndex> SubprogramToFuncId;
78 
79   unsigned TypeCount = 0;
80 
81   /// Gets the next type index and increments the count of types streamed so
82   /// far.
83   codeview::TypeIndex getNextTypeIndex() {
84     return codeview::TypeIndex(codeview::TypeIndex::FirstNonSimpleIndex + TypeCount++);
85   }
86 
87   typedef std::map<const DIFile *, std::string> FileToFilepathMapTy;
88   FileToFilepathMapTy FileToFilepathMap;
89   StringRef getFullFilepath(const DIFile *S);
90 
91   unsigned maybeRecordFile(const DIFile *F);
92 
93   void maybeRecordLocation(DebugLoc DL, const MachineFunction *MF);
94 
95   void clear() {
96     assert(CurFn == nullptr);
97     FileIdMap.clear();
98     FnDebugInfo.clear();
99     FileToFilepathMap.clear();
100   }
101 
102   void emitTypeInformation();
103 
104   void emitInlineeLinesSubsection();
105 
106   void emitDebugInfoForFunction(const Function *GV, FunctionInfo &FI);
107 
108   void emitInlinedCallSite(const FunctionInfo &FI, const DILocation *InlinedAt,
109                            const InlineSite &Site);
110 
111 public:
112   CodeViewDebug(AsmPrinter *Asm);
113 
114   void setSymbolSize(const llvm::MCSymbol *, uint64_t) override {}
115 
116   /// \brief Emit the COFF section that holds the line table information.
117   void endModule() override;
118 
119   /// \brief Gather pre-function debug information.
120   void beginFunction(const MachineFunction *MF) override;
121 
122   /// \brief Gather post-function debug information.
123   void endFunction(const MachineFunction *) override;
124 
125   /// \brief Process beginning of an instruction.
126   void beginInstruction(const MachineInstr *MI) override;
127 
128   /// \brief Process end of an instruction.
129   void endInstruction() override {}
130 };
131 } // End of namespace llvm
132 
133 #endif
134