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   DebugLoc PrevInstLoc;
36 
37   struct InlineSite {
38     TinyPtrVector<const DILocation *> ChildSites;
39     const DISubprogram *Inlinee = nullptr;
40     unsigned SiteFuncId = 0;
41   };
42 
43   // For each function, store a vector of labels to its instructions, as well as
44   // to the end of the function.
45   struct FunctionInfo {
46     /// Map from inlined call site to inlined instructions and child inlined
47     /// call sites. Listed in program order.
48     MapVector<const DILocation *, InlineSite> InlineSites;
49 
50     DebugLoc LastLoc;
51     MCSymbol *End = nullptr;
52     unsigned FuncId = 0;
53     unsigned LastFileId = 0;
54     bool HaveLineInfo = false;
55   };
56   FunctionInfo *CurFn;
57 
58   unsigned NextFuncId = 0;
59 
60   InlineSite &getInlineSite(const DILocation *Loc);
61 
62   /// Remember some debug info about each function. Keep it in a stable order to
63   /// emit at the end of the TU.
64   MapVector<const Function *, FunctionInfo> FnDebugInfo;
65 
66   /// Map from DIFile to .cv_file id.
67   DenseMap<const DIFile *, unsigned> FileIdMap;
68 
69   DenseMap<const DISubprogram *, codeview::TypeIndex> SubprogramToFuncId;
70 
71   unsigned TypeCount = 0;
72 
73   /// Gets the next type index and increments the count of types streamed so
74   /// far.
75   codeview::TypeIndex getNextTypeIndex() {
76     return codeview::TypeIndex(codeview::TypeIndex::FirstNonSimpleIndex + TypeCount++);
77   }
78 
79   typedef std::map<const DIFile *, std::string> FileToFilepathMapTy;
80   FileToFilepathMapTy FileToFilepathMap;
81   StringRef getFullFilepath(const DIFile *S);
82 
83   unsigned maybeRecordFile(const DIFile *F);
84 
85   void maybeRecordLocation(DebugLoc DL, const MachineFunction *MF);
86 
87   void clear() {
88     assert(CurFn == nullptr);
89     FileIdMap.clear();
90     FnDebugInfo.clear();
91     FileToFilepathMap.clear();
92   }
93 
94   void emitTypeInformation();
95 
96   void emitDebugInfoForFunction(const Function *GV, FunctionInfo &FI);
97 
98   void emitInlinedCallSite(const FunctionInfo &FI, const DILocation *InlinedAt,
99                            const InlineSite &Site);
100 
101 public:
102   CodeViewDebug(AsmPrinter *Asm);
103 
104   void setSymbolSize(const llvm::MCSymbol *, uint64_t) override {}
105 
106   /// \brief Emit the COFF section that holds the line table information.
107   void endModule() override;
108 
109   /// \brief Gather pre-function debug information.
110   void beginFunction(const MachineFunction *MF) override;
111 
112   /// \brief Gather post-function debug information.
113   void endFunction(const MachineFunction *) override;
114 
115   /// \brief Process beginning of an instruction.
116   void beginInstruction(const MachineInstr *MI) override;
117 
118   /// \brief Process end of an instruction.
119   void endInstruction() override {}
120 };
121 } // End of namespace llvm
122 
123 #endif
124