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 "DebugHandlerBase.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/CodeGen/AsmPrinter.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineModuleInfo.h"
23 #include "llvm/DebugInfo/CodeView/MemoryTypeTableBuilder.h"
24 #include "llvm/DebugInfo/CodeView/TypeIndex.h"
25 #include "llvm/IR/DebugInfo.h"
26 #include "llvm/IR/DebugLoc.h"
27 #include "llvm/MC/MCStreamer.h"
28 #include "llvm/Target/TargetLoweringObjectFile.h"
29 
30 namespace llvm {
31 
32 class StringRef;
33 class LexicalScope;
34 
35 /// \brief Collects and handles line tables information in a CodeView format.
36 class LLVM_LIBRARY_VISIBILITY CodeViewDebug : public DebugHandlerBase {
37   MCStreamer &OS;
38   codeview::MemoryTypeTableBuilder TypeTable;
39 
40   /// Represents the most general definition range.
41   struct LocalVarDefRange {
42     /// Indicates that variable data is stored in memory relative to the
43     /// specified register.
44     int InMemory : 1;
45 
46     /// Offset of variable data in memory.
47     int DataOffset : 31;
48 
49     /// Offset of the data into the user level struct. If zero, no splitting
50     /// occurred.
51     uint16_t StructOffset;
52 
53     /// Register containing the data or the register base of the memory
54     /// location containing the data.
55     uint16_t CVRegister;
56 
57     /// Compares all location fields. This includes all fields except the label
58     /// ranges.
59     bool isDifferentLocation(LocalVarDefRange &O) {
60       return InMemory != O.InMemory || DataOffset != O.DataOffset ||
61              StructOffset != O.StructOffset || CVRegister != O.CVRegister;
62     }
63 
64     SmallVector<std::pair<const MCSymbol *, const MCSymbol *>, 1> Ranges;
65   };
66 
67   static LocalVarDefRange createDefRangeMem(uint16_t CVRegister, int Offset);
68   static LocalVarDefRange createDefRangeReg(uint16_t CVRegister);
69 
70   /// Similar to DbgVariable in DwarfDebug, but not dwarf-specific.
71   struct LocalVariable {
72     const DILocalVariable *DIVar = nullptr;
73     SmallVector<LocalVarDefRange, 1> DefRanges;
74   };
75 
76   struct InlineSite {
77     SmallVector<LocalVariable, 1> InlinedLocals;
78     SmallVector<const DILocation *, 1> ChildSites;
79     const DISubprogram *Inlinee = nullptr;
80     unsigned SiteFuncId = 0;
81   };
82 
83   // For each function, store a vector of labels to its instructions, as well as
84   // to the end of the function.
85   struct FunctionInfo {
86     /// Map from inlined call site to inlined instructions and child inlined
87     /// call sites. Listed in program order.
88     std::unordered_map<const DILocation *, InlineSite> InlineSites;
89 
90     /// Ordered list of top-level inlined call sites.
91     SmallVector<const DILocation *, 1> ChildSites;
92 
93     SmallVector<LocalVariable, 1> Locals;
94 
95     DebugLoc LastLoc;
96     const MCSymbol *Begin = nullptr;
97     const MCSymbol *End = nullptr;
98     unsigned FuncId = 0;
99     unsigned LastFileId = 0;
100     bool HaveLineInfo = false;
101   };
102   FunctionInfo *CurFn;
103 
104   /// The set of comdat .debug$S sections that we've seen so far. Each section
105   /// must start with a magic version number that must only be emitted once.
106   /// This set tracks which sections we've already opened.
107   DenseSet<MCSectionCOFF *> ComdatDebugSections;
108 
109   /// Switch to the appropriate .debug$S section for GVSym. If GVSym, the symbol
110   /// of an emitted global value, is in a comdat COFF section, this will switch
111   /// to a new .debug$S section in that comdat. This method ensures that the
112   /// section starts with the magic version number on first use. If GVSym is
113   /// null, uses the main .debug$S section.
114   void switchToDebugSectionForSymbol(const MCSymbol *GVSym);
115 
116   /// The next available function index for use with our .cv_* directives. Not
117   /// to be confused with type indices for LF_FUNC_ID records.
118   unsigned NextFuncId = 0;
119 
120   InlineSite &getInlineSite(const DILocation *InlinedAt,
121                             const DISubprogram *Inlinee);
122 
123   codeview::TypeIndex getFuncIdForSubprogram(const DISubprogram *SP);
124 
125   static void collectInlineSiteChildren(SmallVectorImpl<unsigned> &Children,
126                                         const FunctionInfo &FI,
127                                         const InlineSite &Site);
128 
129   /// Remember some debug info about each function. Keep it in a stable order to
130   /// emit at the end of the TU.
131   MapVector<const Function *, FunctionInfo> FnDebugInfo;
132 
133   /// Map from DIFile to .cv_file id.
134   DenseMap<const DIFile *, unsigned> FileIdMap;
135 
136   /// All inlined subprograms in the order they should be emitted.
137   SmallSetVector<const DISubprogram *, 4> InlinedSubprograms;
138 
139   /// Map from DI metadata nodes to CodeView type indices. Primarily indexed by
140   /// DIType* and DISubprogram*.
141   DenseMap<const DINode *, codeview::TypeIndex> TypeIndices;
142 
143   /// Map from DICompositeType* to complete type index. Non-record types are
144   /// always looked up in the normal TypeIndices map.
145   DenseMap<const DICompositeType *, codeview::TypeIndex> CompleteTypeIndices;
146 
147   typedef std::map<const DIFile *, std::string> FileToFilepathMapTy;
148   FileToFilepathMapTy FileToFilepathMap;
149   StringRef getFullFilepath(const DIFile *S);
150 
151   unsigned maybeRecordFile(const DIFile *F);
152 
153   void maybeRecordLocation(const DebugLoc &DL, const MachineFunction *MF);
154 
155   void clear() {
156     assert(CurFn == nullptr);
157     FileIdMap.clear();
158     FnDebugInfo.clear();
159     FileToFilepathMap.clear();
160   }
161 
162   /// Emit the magic version number at the start of a CodeView type or symbol
163   /// section. Appears at the front of every .debug$S or .debug$T section.
164   void emitCodeViewMagicVersion();
165 
166   void emitTypeInformation();
167 
168   void emitInlineeLinesSubsection();
169 
170   void emitDebugInfoForFunction(const Function *GV, FunctionInfo &FI);
171 
172   void emitDebugInfoForGlobals();
173 
174   void emitDebugInfoForGlobal(const DIGlobalVariable *DIGV, MCSymbol *GVSym);
175 
176   /// Opens a subsection of the given kind in a .debug$S codeview section.
177   /// Returns an end label for use with endCVSubsection when the subsection is
178   /// finished.
179   MCSymbol *beginCVSubsection(codeview::ModuleSubstreamKind Kind);
180 
181   void endCVSubsection(MCSymbol *EndLabel);
182 
183   void emitInlinedCallSite(const FunctionInfo &FI, const DILocation *InlinedAt,
184                            const InlineSite &Site);
185 
186   typedef DbgValueHistoryMap::InlinedVariable InlinedVariable;
187 
188   void collectVariableInfo(const DISubprogram *SP);
189 
190   void collectVariableInfoFromMMITable(DenseSet<InlinedVariable> &Processed);
191 
192   /// Records information about a local variable in the appropriate scope. In
193   /// particular, locals from inlined code live inside the inlining site.
194   void recordLocalVariable(LocalVariable &&Var, const DILocation *Loc);
195 
196   void emitLocalVariable(const LocalVariable &Var);
197 
198   /// Translates the DIType to codeview if necessary and returns a type index
199   /// for it.
200   codeview::TypeIndex getTypeIndex(DITypeRef TypeRef);
201 
202   codeview::TypeIndex lowerType(const DIType *Ty);
203   codeview::TypeIndex lowerTypeAlias(const DIDerivedType *Ty);
204   codeview::TypeIndex lowerTypeArray(const DICompositeType *Ty);
205   codeview::TypeIndex lowerTypeBasic(const DIBasicType *Ty);
206   codeview::TypeIndex lowerTypePointer(const DIDerivedType *Ty);
207   codeview::TypeIndex lowerTypeMemberPointer(const DIDerivedType *Ty);
208   codeview::TypeIndex lowerTypeModifier(const DIDerivedType *Ty);
209   codeview::TypeIndex lowerTypeFunction(const DISubroutineType *Ty);
210   codeview::TypeIndex lowerTypeClass(const DICompositeType *Ty);
211   codeview::TypeIndex lowerTypeUnion(const DICompositeType *Ty);
212 
213   /// Symbol records should point to complete types, but type records should
214   /// always point to incomplete types to avoid cycles in the type graph. Only
215   /// use this entry point when generating symbol records. The complete and
216   /// incomplete type indices only differ for record types. All other types use
217   /// the same index.
218   codeview::TypeIndex getCompleteTypeIndex(DITypeRef TypeRef);
219 
220   codeview::TypeIndex lowerCompleteTypeClass(const DICompositeType *Ty);
221   codeview::TypeIndex lowerCompleteTypeUnion(const DICompositeType *Ty);
222 
223   /// Common record member lowering functionality for record types, which are
224   /// structs, classes, and unions. Returns the field list index and the member
225   /// count.
226   std::pair<codeview::TypeIndex, unsigned>
227   lowerRecordFieldList(const DICompositeType *Ty);
228 
229   /// Inserts {Node, TI} into TypeIndices and checks for duplicates.
230   void recordTypeIndexForDINode(const DINode *Node, codeview::TypeIndex TI);
231 
232 public:
233   CodeViewDebug(AsmPrinter *Asm);
234 
235   void setSymbolSize(const llvm::MCSymbol *, uint64_t) override {}
236 
237   /// \brief Emit the COFF section that holds the line table information.
238   void endModule() override;
239 
240   /// \brief Gather pre-function debug information.
241   void beginFunction(const MachineFunction *MF) override;
242 
243   /// \brief Gather post-function debug information.
244   void endFunction(const MachineFunction *) override;
245 
246   /// \brief Process beginning of an instruction.
247   void beginInstruction(const MachineInstr *MI) override;
248 };
249 } // End of namespace llvm
250 
251 #endif
252