Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 1 | //===-- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h ----*- C++ -*--===// |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 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 | // |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 10 | // This file contains support for writing Microsoft CodeView debug info. |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 14 | #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_CODEVIEWDEBUG_H |
| 15 | #define LLVM_LIB_CODEGEN_ASMPRINTER_CODEVIEWDEBUG_H |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 16 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 17 | #include "DebugHandlerBase.h" |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseMap.h" |
| 19 | #include "llvm/ADT/StringMap.h" |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/AsmPrinter.h" |
| 21 | #include "llvm/CodeGen/MachineFunction.h" |
| 22 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 23 | #include "llvm/DebugInfo/CodeView/MemoryTypeTableBuilder.h" |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 24 | #include "llvm/DebugInfo/CodeView/TypeIndex.h" |
Chandler Carruth | 9a4c9e5 | 2014-03-06 00:46:21 +0000 | [diff] [blame] | 25 | #include "llvm/IR/DebugInfo.h" |
Chandler Carruth | 9205140 | 2014-03-05 10:30:38 +0000 | [diff] [blame] | 26 | #include "llvm/IR/DebugLoc.h" |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCStreamer.h" |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetLoweringObjectFile.h" |
| 29 | |
| 30 | namespace llvm { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 31 | |
Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 32 | class StringRef; |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 33 | class LexicalScope; |
| 34 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 35 | /// \brief Collects and handles line tables information in a CodeView format. |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 36 | class LLVM_LIBRARY_VISIBILITY CodeViewDebug : public DebugHandlerBase { |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 37 | MCStreamer &OS; |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 38 | codeview::MemoryTypeTableBuilder TypeTable; |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 39 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 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 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 70 | /// Similar to DbgVariable in DwarfDebug, but not dwarf-specific. |
| 71 | struct LocalVariable { |
| 72 | const DILocalVariable *DIVar = nullptr; |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 73 | SmallVector<LocalVarDefRange, 1> DefRanges; |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 74 | }; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 75 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 76 | struct InlineSite { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 77 | SmallVector<LocalVariable, 1> InlinedLocals; |
| 78 | SmallVector<const DILocation *, 1> ChildSites; |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 79 | const DISubprogram *Inlinee = nullptr; |
| 80 | unsigned SiteFuncId = 0; |
| 81 | }; |
| 82 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 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 { |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 86 | /// Map from inlined call site to inlined instructions and child inlined |
| 87 | /// call sites. Listed in program order. |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 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; |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 94 | |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 95 | DebugLoc LastLoc; |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 96 | const MCSymbol *Begin = nullptr; |
| 97 | const MCSymbol *End = nullptr; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 98 | unsigned FuncId = 0; |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 99 | unsigned LastFileId = 0; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 100 | bool HaveLineInfo = false; |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 101 | }; |
| 102 | FunctionInfo *CurFn; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 103 | |
Reid Kleckner | 5d122f8 | 2016-05-25 23:16:12 +0000 | [diff] [blame] | 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 | |
Reid Kleckner | fbd7787 | 2016-03-18 18:54:32 +0000 | [diff] [blame] | 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. |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 118 | unsigned NextFuncId = 0; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 119 | |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 120 | codeview::TypeIndex VoidFnTyIdx; |
Reid Kleckner | fbd7787 | 2016-03-18 18:54:32 +0000 | [diff] [blame] | 121 | |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 122 | /// Get a type index for a generic void function type. |
| 123 | codeview::TypeIndex getGenericFunctionTypeIndex(); |
Reid Kleckner | fbd7787 | 2016-03-18 18:54:32 +0000 | [diff] [blame] | 124 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 125 | InlineSite &getInlineSite(const DILocation *InlinedAt, |
| 126 | const DISubprogram *Inlinee); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 127 | |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 128 | void recordFuncIdForSubprogram(const DISubprogram *SP); |
| 129 | |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 130 | static void collectInlineSiteChildren(SmallVectorImpl<unsigned> &Children, |
| 131 | const FunctionInfo &FI, |
| 132 | const InlineSite &Site); |
| 133 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 134 | /// Remember some debug info about each function. Keep it in a stable order to |
| 135 | /// emit at the end of the TU. |
| 136 | MapVector<const Function *, FunctionInfo> FnDebugInfo; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 137 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 138 | /// Map from DIFile to .cv_file id. |
| 139 | DenseMap<const DIFile *, unsigned> FileIdMap; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 140 | |
Reid Kleckner | fbd7787 | 2016-03-18 18:54:32 +0000 | [diff] [blame] | 141 | /// All inlined subprograms in the order they should be emitted. |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 142 | SmallSetVector<const DISubprogram *, 4> InlinedSubprograms; |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 143 | |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 144 | /// Map from DI metadata nodes to CodeView type indices. Primarily indexed by |
| 145 | /// DIType* and DISubprogram*. |
| 146 | DenseMap<const DINode *, codeview::TypeIndex> TypeIndices; |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 147 | |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 148 | typedef std::map<const DIFile *, std::string> FileToFilepathMapTy; |
| 149 | FileToFilepathMapTy FileToFilepathMap; |
| 150 | StringRef getFullFilepath(const DIFile *S); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 151 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 152 | unsigned maybeRecordFile(const DIFile *F); |
| 153 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 154 | void maybeRecordLocation(DebugLoc DL, const MachineFunction *MF); |
| 155 | |
| 156 | void clear() { |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 157 | assert(CurFn == nullptr); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 158 | FileIdMap.clear(); |
| 159 | FnDebugInfo.clear(); |
| 160 | FileToFilepathMap.clear(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Reid Kleckner | 5d122f8 | 2016-05-25 23:16:12 +0000 | [diff] [blame] | 163 | /// Emit the magic version number at the start of a CodeView type or symbol |
| 164 | /// section. Appears at the front of every .debug$S or .debug$T section. |
| 165 | void emitCodeViewMagicVersion(); |
| 166 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 167 | void emitTypeInformation(); |
| 168 | |
Reid Kleckner | 5d122f8 | 2016-05-25 23:16:12 +0000 | [diff] [blame] | 169 | void emitInlineeLinesSubsection(); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 170 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 171 | void emitDebugInfoForFunction(const Function *GV, FunctionInfo &FI); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 172 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 173 | void emitInlinedCallSite(const FunctionInfo &FI, const DILocation *InlinedAt, |
| 174 | const InlineSite &Site); |
| 175 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 176 | typedef DbgValueHistoryMap::InlinedVariable InlinedVariable; |
| 177 | |
| 178 | void collectVariableInfo(const DISubprogram *SP); |
| 179 | |
| 180 | void collectVariableInfoFromMMITable(DenseSet<InlinedVariable> &Processed); |
| 181 | |
| 182 | /// Records information about a local variable in the appropriate scope. In |
| 183 | /// particular, locals from inlined code live inside the inlining site. |
| 184 | void recordLocalVariable(LocalVariable &&Var, const DILocation *Loc); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 185 | |
| 186 | void emitLocalVariable(const LocalVariable &Var); |
| 187 | |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame^] | 188 | /// Translates the DIType to codeview if necessary and returns a type index |
| 189 | /// for it. |
| 190 | codeview::TypeIndex getTypeIndex(DITypeRef Ty); |
| 191 | |
| 192 | codeview::TypeIndex lowerType(const DIType *Ty); |
| 193 | codeview::TypeIndex lowerTypeBasic(const DIBasicType *Ty); |
| 194 | codeview::TypeIndex lowerTypePointer(const DIDerivedType *Ty); |
| 195 | codeview::TypeIndex lowerTypeMemberPointer(const DIDerivedType *Ty); |
| 196 | codeview::TypeIndex lowerTypeModifier(const DIDerivedType *Ty); |
| 197 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 198 | public: |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 199 | CodeViewDebug(AsmPrinter *Asm); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 200 | |
Craig Topper | 7b883b3 | 2014-03-08 06:31:39 +0000 | [diff] [blame] | 201 | void setSymbolSize(const llvm::MCSymbol *, uint64_t) override {} |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 202 | |
| 203 | /// \brief Emit the COFF section that holds the line table information. |
Craig Topper | 7b883b3 | 2014-03-08 06:31:39 +0000 | [diff] [blame] | 204 | void endModule() override; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 205 | |
| 206 | /// \brief Gather pre-function debug information. |
Craig Topper | 7b883b3 | 2014-03-08 06:31:39 +0000 | [diff] [blame] | 207 | void beginFunction(const MachineFunction *MF) override; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 208 | |
| 209 | /// \brief Gather post-function debug information. |
Craig Topper | 7b883b3 | 2014-03-08 06:31:39 +0000 | [diff] [blame] | 210 | void endFunction(const MachineFunction *) override; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 211 | |
| 212 | /// \brief Process beginning of an instruction. |
Craig Topper | 7b883b3 | 2014-03-08 06:31:39 +0000 | [diff] [blame] | 213 | void beginInstruction(const MachineInstr *MI) override; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 214 | }; |
| 215 | } // End of namespace llvm |
| 216 | |
| 217 | #endif |