Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 1 | //===-- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp --*- 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 | #include "CodeViewDebug.h" |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/CodeView/CodeView.h" |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/CodeView/FieldListRecordBuilder.h" |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 17 | #include "llvm/DebugInfo/CodeView/Line.h" |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/CodeView/SymbolRecord.h" |
Reid Kleckner | fbdbe9e | 2016-05-31 18:45:36 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/CodeView/TypeDumper.h" |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 20 | #include "llvm/DebugInfo/CodeView/TypeIndex.h" |
| 21 | #include "llvm/DebugInfo/CodeView/TypeRecord.h" |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCExpr.h" |
Reid Kleckner | 5d122f8 | 2016-05-25 23:16:12 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCSectionCOFF.h" |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCSymbol.h" |
| 25 | #include "llvm/Support/COFF.h" |
Reid Kleckner | fbdbe9e | 2016-05-31 18:45:36 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ScopedPrinter.h" |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetSubtargetInfo.h" |
| 28 | #include "llvm/Target/TargetRegisterInfo.h" |
| 29 | #include "llvm/Target/TargetFrameLowering.h" |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 30 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 31 | using namespace llvm; |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 32 | using namespace llvm::codeview; |
| 33 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 34 | CodeViewDebug::CodeViewDebug(AsmPrinter *AP) |
| 35 | : DebugHandlerBase(AP), OS(*Asm->OutStreamer), CurFn(nullptr) { |
| 36 | // If module doesn't have named metadata anchors or COFF debug section |
| 37 | // is not available, skip any debug info related stuff. |
| 38 | if (!MMI->getModule()->getNamedMetadata("llvm.dbg.cu") || |
| 39 | !AP->getObjFileLowering().getCOFFDebugSymbolsSection()) { |
| 40 | Asm = nullptr; |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | // Tell MMI that we have debug info. |
| 45 | MMI->setDebugInfoAvailability(true); |
| 46 | } |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 47 | |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 48 | StringRef CodeViewDebug::getFullFilepath(const DIFile *File) { |
| 49 | std::string &Filepath = FileToFilepathMap[File]; |
Reid Kleckner | 1f11b4e | 2015-12-02 22:34:30 +0000 | [diff] [blame] | 50 | if (!Filepath.empty()) |
| 51 | return Filepath; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 52 | |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 53 | StringRef Dir = File->getDirectory(), Filename = File->getFilename(); |
| 54 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 55 | // Clang emits directory and relative filename info into the IR, but CodeView |
| 56 | // operates on full paths. We could change Clang to emit full paths too, but |
| 57 | // that would increase the IR size and probably not needed for other users. |
| 58 | // For now, just concatenate and canonicalize the path here. |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 59 | if (Filename.find(':') == 1) |
| 60 | Filepath = Filename; |
| 61 | else |
Yaron Keren | 75e0c4b | 2015-03-27 17:51:30 +0000 | [diff] [blame] | 62 | Filepath = (Dir + "\\" + Filename).str(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 63 | |
| 64 | // Canonicalize the path. We have to do it textually because we may no longer |
| 65 | // have access the file in the filesystem. |
| 66 | // First, replace all slashes with backslashes. |
| 67 | std::replace(Filepath.begin(), Filepath.end(), '/', '\\'); |
| 68 | |
| 69 | // Remove all "\.\" with "\". |
| 70 | size_t Cursor = 0; |
| 71 | while ((Cursor = Filepath.find("\\.\\", Cursor)) != std::string::npos) |
| 72 | Filepath.erase(Cursor, 2); |
| 73 | |
| 74 | // Replace all "\XXX\..\" with "\". Don't try too hard though as the original |
| 75 | // path should be well-formatted, e.g. start with a drive letter, etc. |
| 76 | Cursor = 0; |
| 77 | while ((Cursor = Filepath.find("\\..\\", Cursor)) != std::string::npos) { |
| 78 | // Something's wrong if the path starts with "\..\", abort. |
| 79 | if (Cursor == 0) |
| 80 | break; |
| 81 | |
| 82 | size_t PrevSlash = Filepath.rfind('\\', Cursor - 1); |
| 83 | if (PrevSlash == std::string::npos) |
| 84 | // Something's wrong, abort. |
| 85 | break; |
| 86 | |
| 87 | Filepath.erase(PrevSlash, Cursor + 3 - PrevSlash); |
| 88 | // The next ".." might be following the one we've just erased. |
| 89 | Cursor = PrevSlash; |
| 90 | } |
| 91 | |
| 92 | // Remove all duplicate backslashes. |
| 93 | Cursor = 0; |
| 94 | while ((Cursor = Filepath.find("\\\\", Cursor)) != std::string::npos) |
| 95 | Filepath.erase(Cursor, 1); |
| 96 | |
Reid Kleckner | 1f11b4e | 2015-12-02 22:34:30 +0000 | [diff] [blame] | 97 | return Filepath; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 100 | unsigned CodeViewDebug::maybeRecordFile(const DIFile *F) { |
| 101 | unsigned NextId = FileIdMap.size() + 1; |
| 102 | auto Insertion = FileIdMap.insert(std::make_pair(F, NextId)); |
| 103 | if (Insertion.second) { |
| 104 | // We have to compute the full filepath and emit a .cv_file directive. |
| 105 | StringRef FullPath = getFullFilepath(F); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 106 | NextId = OS.EmitCVFileDirective(NextId, FullPath); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 107 | assert(NextId == FileIdMap.size() && ".cv_file directive failed"); |
| 108 | } |
| 109 | return Insertion.first->second; |
| 110 | } |
| 111 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 112 | CodeViewDebug::InlineSite & |
| 113 | CodeViewDebug::getInlineSite(const DILocation *InlinedAt, |
| 114 | const DISubprogram *Inlinee) { |
Reid Kleckner | fbd7787 | 2016-03-18 18:54:32 +0000 | [diff] [blame] | 115 | auto SiteInsertion = CurFn->InlineSites.insert({InlinedAt, InlineSite()}); |
| 116 | InlineSite *Site = &SiteInsertion.first->second; |
| 117 | if (SiteInsertion.second) { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 118 | Site->SiteFuncId = NextFuncId++; |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 119 | Site->Inlinee = Inlinee; |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 120 | InlinedSubprograms.insert(Inlinee); |
David Majnemer | 75c3ebf | 2016-06-02 17:13:53 +0000 | [diff] [blame] | 121 | getFuncIdForSubprogram(Inlinee); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 122 | } |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 123 | return *Site; |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 124 | } |
| 125 | |
David Majnemer | 75c3ebf | 2016-06-02 17:13:53 +0000 | [diff] [blame] | 126 | TypeIndex CodeViewDebug::getFuncIdForSubprogram(const DISubprogram *SP) { |
| 127 | // It's possible to ask for the FuncId of a function which doesn't have a |
| 128 | // subprogram: inlining a function with debug info into a function with none. |
| 129 | if (!SP) |
David Majnemer | b68f32f0 | 2016-06-02 18:51:24 +0000 | [diff] [blame] | 130 | return TypeIndex::None(); |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 131 | |
David Majnemer | 75c3ebf | 2016-06-02 17:13:53 +0000 | [diff] [blame] | 132 | // Check if we've already translated this subprogram. |
| 133 | auto I = TypeIndices.find(SP); |
| 134 | if (I != TypeIndices.end()) |
| 135 | return I->second; |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 136 | |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 137 | TypeIndex ParentScope = TypeIndex(0); |
| 138 | StringRef DisplayName = SP->getDisplayName(); |
David Majnemer | 75c3ebf | 2016-06-02 17:13:53 +0000 | [diff] [blame] | 139 | FuncIdRecord FuncId(ParentScope, getTypeIndex(SP->getType()), DisplayName); |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 140 | TypeIndex TI = TypeTable.writeFuncId(FuncId); |
David Majnemer | 75c3ebf | 2016-06-02 17:13:53 +0000 | [diff] [blame] | 141 | |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 142 | recordTypeIndexForDINode(SP, TI); |
David Majnemer | 75c3ebf | 2016-06-02 17:13:53 +0000 | [diff] [blame] | 143 | return TI; |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 146 | void CodeViewDebug::recordTypeIndexForDINode(const DINode *Node, TypeIndex TI) { |
| 147 | auto InsertResult = TypeIndices.insert({Node, TI}); |
| 148 | (void)InsertResult; |
| 149 | assert(InsertResult.second && "DINode was already assigned a type index"); |
| 150 | } |
| 151 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 152 | void CodeViewDebug::recordLocalVariable(LocalVariable &&Var, |
| 153 | const DILocation *InlinedAt) { |
| 154 | if (InlinedAt) { |
| 155 | // This variable was inlined. Associate it with the InlineSite. |
| 156 | const DISubprogram *Inlinee = Var.DIVar->getScope()->getSubprogram(); |
| 157 | InlineSite &Site = getInlineSite(InlinedAt, Inlinee); |
| 158 | Site.InlinedLocals.emplace_back(Var); |
| 159 | } else { |
| 160 | // This variable goes in the main ProcSym. |
| 161 | CurFn->Locals.emplace_back(Var); |
| 162 | } |
| 163 | } |
| 164 | |
Reid Kleckner | 829365a | 2016-02-11 19:41:47 +0000 | [diff] [blame] | 165 | static void addLocIfNotPresent(SmallVectorImpl<const DILocation *> &Locs, |
| 166 | const DILocation *Loc) { |
| 167 | auto B = Locs.begin(), E = Locs.end(); |
| 168 | if (std::find(B, E, Loc) == E) |
| 169 | Locs.push_back(Loc); |
| 170 | } |
| 171 | |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 172 | void CodeViewDebug::maybeRecordLocation(const DebugLoc &DL, |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 173 | const MachineFunction *MF) { |
| 174 | // Skip this instruction if it has the same location as the previous one. |
| 175 | if (DL == CurFn->LastLoc) |
| 176 | return; |
| 177 | |
| 178 | const DIScope *Scope = DL.get()->getScope(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 179 | if (!Scope) |
| 180 | return; |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 181 | |
David Majnemer | c3340db | 2016-01-13 01:05:23 +0000 | [diff] [blame] | 182 | // Skip this line if it is longer than the maximum we can record. |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 183 | LineInfo LI(DL.getLine(), DL.getLine(), /*IsStatement=*/true); |
| 184 | if (LI.getStartLine() != DL.getLine() || LI.isAlwaysStepInto() || |
| 185 | LI.isNeverStepInto()) |
David Majnemer | c3340db | 2016-01-13 01:05:23 +0000 | [diff] [blame] | 186 | return; |
| 187 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 188 | ColumnInfo CI(DL.getCol(), /*EndColumn=*/0); |
| 189 | if (CI.getStartColumn() != DL.getCol()) |
| 190 | return; |
Reid Kleckner | 00d9639 | 2016-01-29 00:13:28 +0000 | [diff] [blame] | 191 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 192 | if (!CurFn->HaveLineInfo) |
| 193 | CurFn->HaveLineInfo = true; |
| 194 | unsigned FileId = 0; |
| 195 | if (CurFn->LastLoc.get() && CurFn->LastLoc->getFile() == DL->getFile()) |
| 196 | FileId = CurFn->LastFileId; |
| 197 | else |
| 198 | FileId = CurFn->LastFileId = maybeRecordFile(DL->getFile()); |
| 199 | CurFn->LastLoc = DL; |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 200 | |
| 201 | unsigned FuncId = CurFn->FuncId; |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 202 | if (const DILocation *SiteLoc = DL->getInlinedAt()) { |
Reid Kleckner | 829365a | 2016-02-11 19:41:47 +0000 | [diff] [blame] | 203 | const DILocation *Loc = DL.get(); |
| 204 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 205 | // If this location was actually inlined from somewhere else, give it the ID |
| 206 | // of the inline call site. |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 207 | FuncId = |
| 208 | getInlineSite(SiteLoc, Loc->getScope()->getSubprogram()).SiteFuncId; |
Reid Kleckner | 829365a | 2016-02-11 19:41:47 +0000 | [diff] [blame] | 209 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 210 | // Ensure we have links in the tree of inline call sites. |
Reid Kleckner | 829365a | 2016-02-11 19:41:47 +0000 | [diff] [blame] | 211 | bool FirstLoc = true; |
| 212 | while ((SiteLoc = Loc->getInlinedAt())) { |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 213 | InlineSite &Site = |
| 214 | getInlineSite(SiteLoc, Loc->getScope()->getSubprogram()); |
Reid Kleckner | 829365a | 2016-02-11 19:41:47 +0000 | [diff] [blame] | 215 | if (!FirstLoc) |
| 216 | addLocIfNotPresent(Site.ChildSites, Loc); |
| 217 | FirstLoc = false; |
| 218 | Loc = SiteLoc; |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 219 | } |
Reid Kleckner | 829365a | 2016-02-11 19:41:47 +0000 | [diff] [blame] | 220 | addLocIfNotPresent(CurFn->ChildSites, Loc); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 223 | OS.EmitCVLocDirective(FuncId, FileId, DL.getLine(), DL.getCol(), |
| 224 | /*PrologueEnd=*/false, |
| 225 | /*IsStmt=*/false, DL->getFilename()); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Reid Kleckner | 5d122f8 | 2016-05-25 23:16:12 +0000 | [diff] [blame] | 228 | void CodeViewDebug::emitCodeViewMagicVersion() { |
| 229 | OS.EmitValueToAlignment(4); |
| 230 | OS.AddComment("Debug section magic"); |
| 231 | OS.EmitIntValue(COFF::DEBUG_SECTION_MAGIC, 4); |
| 232 | } |
| 233 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 234 | void CodeViewDebug::endModule() { |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 235 | if (!Asm || !MMI->hasDebugInfo()) |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 236 | return; |
| 237 | |
| 238 | assert(Asm != nullptr); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 239 | |
| 240 | // The COFF .debug$S section consists of several subsections, each starting |
| 241 | // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length |
| 242 | // of the payload followed by the payload itself. The subsections are 4-byte |
| 243 | // aligned. |
| 244 | |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 245 | // Use the generic .debug$S section, and make a subsection for all the inlined |
| 246 | // subprograms. |
| 247 | switchToDebugSectionForSymbol(nullptr); |
Reid Kleckner | 5d122f8 | 2016-05-25 23:16:12 +0000 | [diff] [blame] | 248 | emitInlineeLinesSubsection(); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 249 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 250 | // Emit per-function debug information. |
| 251 | for (auto &P : FnDebugInfo) |
David Majnemer | 577be0f | 2016-06-15 00:19:52 +0000 | [diff] [blame] | 252 | if (!P.first->isDeclarationForLinker()) |
| 253 | emitDebugInfoForFunction(P.first, P.second); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 254 | |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 255 | // Emit global variable debug information. |
David Majnemer | 3128b10 | 2016-06-15 18:00:01 +0000 | [diff] [blame] | 256 | setCurrentSubprogram(nullptr); |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 257 | emitDebugInfoForGlobals(); |
| 258 | |
Reid Kleckner | 5d122f8 | 2016-05-25 23:16:12 +0000 | [diff] [blame] | 259 | // Switch back to the generic .debug$S section after potentially processing |
| 260 | // comdat symbol sections. |
| 261 | switchToDebugSectionForSymbol(nullptr); |
| 262 | |
David Majnemer | 3128b10 | 2016-06-15 18:00:01 +0000 | [diff] [blame] | 263 | // Emit UDT records for any types used by global variables. |
| 264 | if (!GlobalUDTs.empty()) { |
| 265 | MCSymbol *SymbolsEnd = beginCVSubsection(ModuleSubstreamKind::Symbols); |
| 266 | emitDebugInfoForUDTs(GlobalUDTs); |
| 267 | endCVSubsection(SymbolsEnd); |
| 268 | } |
| 269 | |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 270 | // This subsection holds a file index to offset in string table table. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 271 | OS.AddComment("File index to string table offset subsection"); |
| 272 | OS.EmitCVFileChecksumsDirective(); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 273 | |
| 274 | // This subsection holds the string table. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 275 | OS.AddComment("String table"); |
| 276 | OS.EmitCVStringTableDirective(); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 277 | |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 278 | // Emit type information last, so that any types we translate while emitting |
| 279 | // function info are included. |
| 280 | emitTypeInformation(); |
| 281 | |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 282 | clear(); |
| 283 | } |
| 284 | |
David Majnemer | b9456a5 | 2016-03-14 05:15:09 +0000 | [diff] [blame] | 285 | static void emitNullTerminatedSymbolName(MCStreamer &OS, StringRef S) { |
| 286 | // Microsoft's linker seems to have trouble with symbol names longer than |
| 287 | // 0xffd8 bytes. |
| 288 | S = S.substr(0, 0xffd8); |
| 289 | SmallString<32> NullTerminatedString(S); |
| 290 | NullTerminatedString.push_back('\0'); |
| 291 | OS.EmitBytes(NullTerminatedString); |
| 292 | } |
| 293 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 294 | void CodeViewDebug::emitTypeInformation() { |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 295 | // Do nothing if we have no debug info or if no non-trivial types were emitted |
| 296 | // to TypeTable during codegen. |
Reid Kleckner | fbd7787 | 2016-03-18 18:54:32 +0000 | [diff] [blame] | 297 | NamedMDNode *CU_Nodes = |
| 298 | MMI->getModule()->getNamedMetadata("llvm.dbg.cu"); |
| 299 | if (!CU_Nodes) |
| 300 | return; |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 301 | if (TypeTable.empty()) |
Reid Kleckner | fbd7787 | 2016-03-18 18:54:32 +0000 | [diff] [blame] | 302 | return; |
| 303 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 304 | // Start the .debug$T section with 0x4. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 305 | OS.SwitchSection(Asm->getObjFileLowering().getCOFFDebugTypesSection()); |
Reid Kleckner | 5d122f8 | 2016-05-25 23:16:12 +0000 | [diff] [blame] | 306 | emitCodeViewMagicVersion(); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 307 | |
Reid Kleckner | fbdbe9e | 2016-05-31 18:45:36 +0000 | [diff] [blame] | 308 | SmallString<8> CommentPrefix; |
| 309 | if (OS.isVerboseAsm()) { |
| 310 | CommentPrefix += '\t'; |
| 311 | CommentPrefix += Asm->MAI->getCommentString(); |
| 312 | CommentPrefix += ' '; |
| 313 | } |
| 314 | |
| 315 | CVTypeDumper CVTD(nullptr, /*PrintRecordBytes=*/false); |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 316 | TypeTable.ForEachRecord( |
Reid Kleckner | fbdbe9e | 2016-05-31 18:45:36 +0000 | [diff] [blame] | 317 | [&](TypeIndex Index, StringRef Record) { |
| 318 | if (OS.isVerboseAsm()) { |
| 319 | // Emit a block comment describing the type record for readability. |
| 320 | SmallString<512> CommentBlock; |
| 321 | raw_svector_ostream CommentOS(CommentBlock); |
| 322 | ScopedPrinter SP(CommentOS); |
| 323 | SP.setPrefix(CommentPrefix); |
| 324 | CVTD.setPrinter(&SP); |
Zachary Turner | 01ee3dae | 2016-06-16 18:22:27 +0000 | [diff] [blame] | 325 | Error EC = CVTD.dump({Record.bytes_begin(), Record.bytes_end()}); |
| 326 | assert(!EC && "produced malformed type record"); |
| 327 | consumeError(std::move(EC)); |
Reid Kleckner | fbdbe9e | 2016-05-31 18:45:36 +0000 | [diff] [blame] | 328 | // emitRawComment will insert its own tab and comment string before |
| 329 | // the first line, so strip off our first one. It also prints its own |
| 330 | // newline. |
| 331 | OS.emitRawComment( |
| 332 | CommentOS.str().drop_front(CommentPrefix.size() - 1).rtrim()); |
| 333 | } |
| 334 | OS.EmitBinaryData(Record); |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 335 | }); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Reid Kleckner | 5d122f8 | 2016-05-25 23:16:12 +0000 | [diff] [blame] | 338 | void CodeViewDebug::emitInlineeLinesSubsection() { |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 339 | if (InlinedSubprograms.empty()) |
| 340 | return; |
| 341 | |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 342 | |
| 343 | OS.AddComment("Inlinee lines subsection"); |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 344 | MCSymbol *InlineEnd = beginCVSubsection(ModuleSubstreamKind::InlineeLines); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 345 | |
| 346 | // We don't provide any extra file info. |
| 347 | // FIXME: Find out if debuggers use this info. |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 348 | OS.AddComment("Inlinee lines signature"); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 349 | OS.EmitIntValue(unsigned(InlineeLinesSignature::Normal), 4); |
| 350 | |
| 351 | for (const DISubprogram *SP : InlinedSubprograms) { |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 352 | assert(TypeIndices.count(SP)); |
| 353 | TypeIndex InlineeIdx = TypeIndices[SP]; |
| 354 | |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 355 | OS.AddBlankLine(); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 356 | unsigned FileId = maybeRecordFile(SP->getFile()); |
| 357 | OS.AddComment("Inlined function " + SP->getDisplayName() + " starts at " + |
| 358 | SP->getFilename() + Twine(':') + Twine(SP->getLine())); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 359 | OS.AddBlankLine(); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 360 | // The filechecksum table uses 8 byte entries for now, and file ids start at |
| 361 | // 1. |
| 362 | unsigned FileOffset = (FileId - 1) * 8; |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 363 | OS.AddComment("Type index of inlined function"); |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 364 | OS.EmitIntValue(InlineeIdx.getIndex(), 4); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 365 | OS.AddComment("Offset into filechecksum table"); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 366 | OS.EmitIntValue(FileOffset, 4); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 367 | OS.AddComment("Starting line number"); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 368 | OS.EmitIntValue(SP->getLine(), 4); |
| 369 | } |
| 370 | |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 371 | endCVSubsection(InlineEnd); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 374 | void CodeViewDebug::collectInlineSiteChildren( |
| 375 | SmallVectorImpl<unsigned> &Children, const FunctionInfo &FI, |
| 376 | const InlineSite &Site) { |
| 377 | for (const DILocation *ChildSiteLoc : Site.ChildSites) { |
| 378 | auto I = FI.InlineSites.find(ChildSiteLoc); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 379 | const InlineSite &ChildSite = I->second; |
| 380 | Children.push_back(ChildSite.SiteFuncId); |
| 381 | collectInlineSiteChildren(Children, FI, ChildSite); |
| 382 | } |
| 383 | } |
| 384 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 385 | void CodeViewDebug::emitInlinedCallSite(const FunctionInfo &FI, |
| 386 | const DILocation *InlinedAt, |
| 387 | const InlineSite &Site) { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 388 | MCSymbol *InlineBegin = MMI->getContext().createTempSymbol(), |
| 389 | *InlineEnd = MMI->getContext().createTempSymbol(); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 390 | |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 391 | assert(TypeIndices.count(Site.Inlinee)); |
| 392 | TypeIndex InlineeIdx = TypeIndices[Site.Inlinee]; |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 393 | |
| 394 | // SymbolRecord |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 395 | OS.AddComment("Record length"); |
Reid Kleckner | eb3bcdd | 2016-02-03 21:24:42 +0000 | [diff] [blame] | 396 | OS.emitAbsoluteSymbolDiff(InlineEnd, InlineBegin, 2); // RecordLength |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 397 | OS.EmitLabel(InlineBegin); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 398 | OS.AddComment("Record kind: S_INLINESITE"); |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 399 | OS.EmitIntValue(SymbolKind::S_INLINESITE, 2); // RecordKind |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 400 | |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 401 | OS.AddComment("PtrParent"); |
| 402 | OS.EmitIntValue(0, 4); |
| 403 | OS.AddComment("PtrEnd"); |
| 404 | OS.EmitIntValue(0, 4); |
| 405 | OS.AddComment("Inlinee type index"); |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 406 | OS.EmitIntValue(InlineeIdx.getIndex(), 4); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 407 | |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 408 | unsigned FileId = maybeRecordFile(Site.Inlinee->getFile()); |
| 409 | unsigned StartLineNum = Site.Inlinee->getLine(); |
| 410 | SmallVector<unsigned, 3> SecondaryFuncIds; |
| 411 | collectInlineSiteChildren(SecondaryFuncIds, FI, Site); |
| 412 | |
| 413 | OS.EmitCVInlineLinetableDirective(Site.SiteFuncId, FileId, StartLineNum, |
David Majnemer | c9911f2 | 2016-02-02 19:22:34 +0000 | [diff] [blame] | 414 | FI.Begin, FI.End, SecondaryFuncIds); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 415 | |
| 416 | OS.EmitLabel(InlineEnd); |
| 417 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 418 | for (const LocalVariable &Var : Site.InlinedLocals) |
| 419 | emitLocalVariable(Var); |
| 420 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 421 | // Recurse on child inlined call sites before closing the scope. |
| 422 | for (const DILocation *ChildSite : Site.ChildSites) { |
| 423 | auto I = FI.InlineSites.find(ChildSite); |
| 424 | assert(I != FI.InlineSites.end() && |
| 425 | "child site not in function inline site map"); |
| 426 | emitInlinedCallSite(FI, ChildSite, I->second); |
| 427 | } |
| 428 | |
| 429 | // Close the scope. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 430 | OS.AddComment("Record length"); |
| 431 | OS.EmitIntValue(2, 2); // RecordLength |
| 432 | OS.AddComment("Record kind: S_INLINESITE_END"); |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 433 | OS.EmitIntValue(SymbolKind::S_INLINESITE_END, 2); // RecordKind |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Reid Kleckner | 5d122f8 | 2016-05-25 23:16:12 +0000 | [diff] [blame] | 436 | void CodeViewDebug::switchToDebugSectionForSymbol(const MCSymbol *GVSym) { |
| 437 | // If we have a symbol, it may be in a section that is COMDAT. If so, find the |
| 438 | // comdat key. A section may be comdat because of -ffunction-sections or |
| 439 | // because it is comdat in the IR. |
| 440 | MCSectionCOFF *GVSec = |
| 441 | GVSym ? dyn_cast<MCSectionCOFF>(&GVSym->getSection()) : nullptr; |
| 442 | const MCSymbol *KeySym = GVSec ? GVSec->getCOMDATSymbol() : nullptr; |
| 443 | |
| 444 | MCSectionCOFF *DebugSec = cast<MCSectionCOFF>( |
| 445 | Asm->getObjFileLowering().getCOFFDebugSymbolsSection()); |
| 446 | DebugSec = OS.getContext().getAssociativeCOFFSection(DebugSec, KeySym); |
| 447 | |
| 448 | OS.SwitchSection(DebugSec); |
| 449 | |
| 450 | // Emit the magic version number if this is the first time we've switched to |
| 451 | // this section. |
| 452 | if (ComdatDebugSections.insert(DebugSec).second) |
| 453 | emitCodeViewMagicVersion(); |
| 454 | } |
| 455 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 456 | void CodeViewDebug::emitDebugInfoForFunction(const Function *GV, |
| 457 | FunctionInfo &FI) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 458 | // For each function there is a separate subsection |
| 459 | // which holds the PC to file:line table. |
| 460 | const MCSymbol *Fn = Asm->getSymbol(GV); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 461 | assert(Fn); |
Timur Iskhodzhanov | 8499a12 | 2014-03-26 09:50:36 +0000 | [diff] [blame] | 462 | |
Reid Kleckner | 5d122f8 | 2016-05-25 23:16:12 +0000 | [diff] [blame] | 463 | // Switch to the to a comdat section, if appropriate. |
| 464 | switchToDebugSectionForSymbol(Fn); |
| 465 | |
Duncan P. N. Exon Smith | 23e56ec | 2015-03-20 19:50:00 +0000 | [diff] [blame] | 466 | StringRef FuncName; |
David Majnemer | 3128b10 | 2016-06-15 18:00:01 +0000 | [diff] [blame] | 467 | auto *SP = GV->getSubprogram(); |
| 468 | setCurrentSubprogram(SP); |
| 469 | if (SP != nullptr) |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 470 | FuncName = SP->getDisplayName(); |
Duncan P. N. Exon Smith | 23e56ec | 2015-03-20 19:50:00 +0000 | [diff] [blame] | 471 | |
Reid Kleckner | 3c0ff98 | 2016-01-14 00:12:54 +0000 | [diff] [blame] | 472 | // If our DISubprogram name is empty, use the mangled name. |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 473 | if (FuncName.empty()) |
| 474 | FuncName = GlobalValue::getRealLinkageName(GV->getName()); |
Reid Kleckner | 3c0ff98 | 2016-01-14 00:12:54 +0000 | [diff] [blame] | 475 | |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 476 | // Emit a symbol subsection, required by VS2012+ to find function boundaries. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 477 | OS.AddComment("Symbol subsection for " + Twine(FuncName)); |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 478 | MCSymbol *SymbolsEnd = beginCVSubsection(ModuleSubstreamKind::Symbols); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 479 | { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 480 | MCSymbol *ProcRecordBegin = MMI->getContext().createTempSymbol(), |
| 481 | *ProcRecordEnd = MMI->getContext().createTempSymbol(); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 482 | OS.AddComment("Record length"); |
Reid Kleckner | eb3bcdd | 2016-02-03 21:24:42 +0000 | [diff] [blame] | 483 | OS.emitAbsoluteSymbolDiff(ProcRecordEnd, ProcRecordBegin, 2); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 484 | OS.EmitLabel(ProcRecordBegin); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 485 | |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 486 | OS.AddComment("Record kind: S_GPROC32_ID"); |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 487 | OS.EmitIntValue(unsigned(SymbolKind::S_GPROC32_ID), 2); |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 488 | |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 489 | // These fields are filled in by tools like CVPACK which run after the fact. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 490 | OS.AddComment("PtrParent"); |
| 491 | OS.EmitIntValue(0, 4); |
| 492 | OS.AddComment("PtrEnd"); |
| 493 | OS.EmitIntValue(0, 4); |
| 494 | OS.AddComment("PtrNext"); |
| 495 | OS.EmitIntValue(0, 4); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 496 | // This is the important bit that tells the debugger where the function |
| 497 | // code is located and what's its size: |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 498 | OS.AddComment("Code size"); |
Reid Kleckner | eb3bcdd | 2016-02-03 21:24:42 +0000 | [diff] [blame] | 499 | OS.emitAbsoluteSymbolDiff(FI.End, Fn, 4); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 500 | OS.AddComment("Offset after prologue"); |
| 501 | OS.EmitIntValue(0, 4); |
| 502 | OS.AddComment("Offset before epilogue"); |
| 503 | OS.EmitIntValue(0, 4); |
| 504 | OS.AddComment("Function type index"); |
David Majnemer | 75c3ebf | 2016-06-02 17:13:53 +0000 | [diff] [blame] | 505 | OS.EmitIntValue(getFuncIdForSubprogram(GV->getSubprogram()).getIndex(), 4); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 506 | OS.AddComment("Function section relative address"); |
| 507 | OS.EmitCOFFSecRel32(Fn); |
| 508 | OS.AddComment("Function section index"); |
| 509 | OS.EmitCOFFSectionIndex(Fn); |
| 510 | OS.AddComment("Flags"); |
| 511 | OS.EmitIntValue(0, 1); |
Timur Iskhodzhanov | a11b32b | 2014-11-12 20:10:09 +0000 | [diff] [blame] | 512 | // Emit the function display name as a null-terminated string. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 513 | OS.AddComment("Function name"); |
David Majnemer | 1256125 | 2016-03-13 10:53:30 +0000 | [diff] [blame] | 514 | // Truncate the name so we won't overflow the record length field. |
David Majnemer | b9456a5 | 2016-03-14 05:15:09 +0000 | [diff] [blame] | 515 | emitNullTerminatedSymbolName(OS, FuncName); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 516 | OS.EmitLabel(ProcRecordEnd); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 517 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 518 | for (const LocalVariable &Var : FI.Locals) |
| 519 | emitLocalVariable(Var); |
| 520 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 521 | // Emit inlined call site information. Only emit functions inlined directly |
| 522 | // into the parent function. We'll emit the other sites recursively as part |
| 523 | // of their parent inline site. |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 524 | for (const DILocation *InlinedAt : FI.ChildSites) { |
| 525 | auto I = FI.InlineSites.find(InlinedAt); |
| 526 | assert(I != FI.InlineSites.end() && |
| 527 | "child site not in function inline site map"); |
| 528 | emitInlinedCallSite(FI, InlinedAt, I->second); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 529 | } |
| 530 | |
David Majnemer | 3128b10 | 2016-06-15 18:00:01 +0000 | [diff] [blame] | 531 | if (SP != nullptr) |
| 532 | emitDebugInfoForUDTs(LocalUDTs); |
| 533 | |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 534 | // We're done with this function. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 535 | OS.AddComment("Record length"); |
| 536 | OS.EmitIntValue(0x0002, 2); |
| 537 | OS.AddComment("Record kind: S_PROC_ID_END"); |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 538 | OS.EmitIntValue(unsigned(SymbolKind::S_PROC_ID_END), 2); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 539 | } |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 540 | endCVSubsection(SymbolsEnd); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 541 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 542 | // We have an assembler directive that takes care of the whole line table. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 543 | OS.EmitCVLinetableDirective(FI.FuncId, Fn, FI.End); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 546 | CodeViewDebug::LocalVarDefRange |
| 547 | CodeViewDebug::createDefRangeMem(uint16_t CVRegister, int Offset) { |
| 548 | LocalVarDefRange DR; |
Aaron Ballman | c6a2f21 | 2016-02-16 15:35:51 +0000 | [diff] [blame] | 549 | DR.InMemory = -1; |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 550 | DR.DataOffset = Offset; |
| 551 | assert(DR.DataOffset == Offset && "truncation"); |
| 552 | DR.StructOffset = 0; |
| 553 | DR.CVRegister = CVRegister; |
| 554 | return DR; |
| 555 | } |
| 556 | |
| 557 | CodeViewDebug::LocalVarDefRange |
| 558 | CodeViewDebug::createDefRangeReg(uint16_t CVRegister) { |
| 559 | LocalVarDefRange DR; |
| 560 | DR.InMemory = 0; |
| 561 | DR.DataOffset = 0; |
| 562 | DR.StructOffset = 0; |
| 563 | DR.CVRegister = CVRegister; |
| 564 | return DR; |
| 565 | } |
| 566 | |
| 567 | void CodeViewDebug::collectVariableInfoFromMMITable( |
| 568 | DenseSet<InlinedVariable> &Processed) { |
| 569 | const TargetSubtargetInfo &TSI = Asm->MF->getSubtarget(); |
| 570 | const TargetFrameLowering *TFI = TSI.getFrameLowering(); |
| 571 | const TargetRegisterInfo *TRI = TSI.getRegisterInfo(); |
| 572 | |
| 573 | for (const MachineModuleInfo::VariableDbgInfo &VI : |
| 574 | MMI->getVariableDbgInfo()) { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 575 | if (!VI.Var) |
| 576 | continue; |
| 577 | assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) && |
| 578 | "Expected inlined-at fields to agree"); |
| 579 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 580 | Processed.insert(InlinedVariable(VI.Var, VI.Loc->getInlinedAt())); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 581 | LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc); |
| 582 | |
| 583 | // If variable scope is not found then skip this variable. |
| 584 | if (!Scope) |
| 585 | continue; |
| 586 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 587 | // Get the frame register used and the offset. |
| 588 | unsigned FrameReg = 0; |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 589 | int FrameOffset = TFI->getFrameIndexReference(*Asm->MF, VI.Slot, FrameReg); |
| 590 | uint16_t CVReg = TRI->getCodeViewRegNum(FrameReg); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 591 | |
| 592 | // Calculate the label ranges. |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 593 | LocalVarDefRange DefRange = createDefRangeMem(CVReg, FrameOffset); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 594 | for (const InsnRange &Range : Scope->getRanges()) { |
| 595 | const MCSymbol *Begin = getLabelBeforeInsn(Range.first); |
| 596 | const MCSymbol *End = getLabelAfterInsn(Range.second); |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 597 | End = End ? End : Asm->getFunctionEnd(); |
| 598 | DefRange.Ranges.emplace_back(Begin, End); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 601 | LocalVariable Var; |
| 602 | Var.DIVar = VI.Var; |
| 603 | Var.DefRanges.emplace_back(std::move(DefRange)); |
| 604 | recordLocalVariable(std::move(Var), VI.Loc->getInlinedAt()); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | void CodeViewDebug::collectVariableInfo(const DISubprogram *SP) { |
| 609 | DenseSet<InlinedVariable> Processed; |
| 610 | // Grab the variable info that was squirreled away in the MMI side-table. |
| 611 | collectVariableInfoFromMMITable(Processed); |
| 612 | |
| 613 | const TargetRegisterInfo *TRI = Asm->MF->getSubtarget().getRegisterInfo(); |
| 614 | |
| 615 | for (const auto &I : DbgValues) { |
| 616 | InlinedVariable IV = I.first; |
| 617 | if (Processed.count(IV)) |
| 618 | continue; |
| 619 | const DILocalVariable *DIVar = IV.first; |
| 620 | const DILocation *InlinedAt = IV.second; |
| 621 | |
| 622 | // Instruction ranges, specifying where IV is accessible. |
| 623 | const auto &Ranges = I.second; |
| 624 | |
| 625 | LexicalScope *Scope = nullptr; |
| 626 | if (InlinedAt) |
| 627 | Scope = LScopes.findInlinedScope(DIVar->getScope(), InlinedAt); |
| 628 | else |
| 629 | Scope = LScopes.findLexicalScope(DIVar->getScope()); |
| 630 | // If variable scope is not found then skip this variable. |
| 631 | if (!Scope) |
| 632 | continue; |
| 633 | |
| 634 | LocalVariable Var; |
| 635 | Var.DIVar = DIVar; |
| 636 | |
| 637 | // Calculate the definition ranges. |
| 638 | for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) { |
| 639 | const InsnRange &Range = *I; |
| 640 | const MachineInstr *DVInst = Range.first; |
| 641 | assert(DVInst->isDebugValue() && "Invalid History entry"); |
| 642 | const DIExpression *DIExpr = DVInst->getDebugExpression(); |
| 643 | |
| 644 | // Bail if there is a complex DWARF expression for now. |
| 645 | if (DIExpr && DIExpr->getNumElements() > 0) |
| 646 | continue; |
| 647 | |
Reid Kleckner | 9a593ee | 2016-02-16 21:49:26 +0000 | [diff] [blame] | 648 | // Bail if operand 0 is not a valid register. This means the variable is a |
| 649 | // simple constant, or is described by a complex expression. |
| 650 | // FIXME: Find a way to represent constant variables, since they are |
| 651 | // relatively common. |
| 652 | unsigned Reg = |
| 653 | DVInst->getOperand(0).isReg() ? DVInst->getOperand(0).getReg() : 0; |
| 654 | if (Reg == 0) |
Reid Kleckner | 6e0d5f5 | 2016-02-16 21:14:51 +0000 | [diff] [blame] | 655 | continue; |
| 656 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 657 | // Handle the two cases we can handle: indirect in memory and in register. |
| 658 | bool IsIndirect = DVInst->getOperand(1).isImm(); |
| 659 | unsigned CVReg = TRI->getCodeViewRegNum(DVInst->getOperand(0).getReg()); |
| 660 | { |
| 661 | LocalVarDefRange DefRange; |
| 662 | if (IsIndirect) { |
| 663 | int64_t Offset = DVInst->getOperand(1).getImm(); |
| 664 | DefRange = createDefRangeMem(CVReg, Offset); |
| 665 | } else { |
| 666 | DefRange = createDefRangeReg(CVReg); |
| 667 | } |
| 668 | if (Var.DefRanges.empty() || |
| 669 | Var.DefRanges.back().isDifferentLocation(DefRange)) { |
| 670 | Var.DefRanges.emplace_back(std::move(DefRange)); |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | // Compute the label range. |
| 675 | const MCSymbol *Begin = getLabelBeforeInsn(Range.first); |
| 676 | const MCSymbol *End = getLabelAfterInsn(Range.second); |
| 677 | if (!End) { |
| 678 | if (std::next(I) != E) |
| 679 | End = getLabelBeforeInsn(std::next(I)->first); |
| 680 | else |
| 681 | End = Asm->getFunctionEnd(); |
| 682 | } |
| 683 | |
| 684 | // If the last range end is our begin, just extend the last range. |
| 685 | // Otherwise make a new range. |
| 686 | SmallVectorImpl<std::pair<const MCSymbol *, const MCSymbol *>> &Ranges = |
| 687 | Var.DefRanges.back().Ranges; |
| 688 | if (!Ranges.empty() && Ranges.back().second == Begin) |
| 689 | Ranges.back().second = End; |
| 690 | else |
| 691 | Ranges.emplace_back(Begin, End); |
| 692 | |
| 693 | // FIXME: Do more range combining. |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 694 | } |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 695 | |
| 696 | recordLocalVariable(std::move(Var), InlinedAt); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 700 | void CodeViewDebug::beginFunction(const MachineFunction *MF) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 701 | assert(!CurFn && "Can't process two functions at once!"); |
| 702 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 703 | if (!Asm || !MMI->hasDebugInfo()) |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 704 | return; |
| 705 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 706 | DebugHandlerBase::beginFunction(MF); |
| 707 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 708 | const Function *GV = MF->getFunction(); |
| 709 | assert(FnDebugInfo.count(GV) == false); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 710 | CurFn = &FnDebugInfo[GV]; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 711 | CurFn->FuncId = NextFuncId++; |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 712 | CurFn->Begin = Asm->getFunctionBegin(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 713 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 714 | // Find the end of the function prolog. First known non-DBG_VALUE and |
| 715 | // non-frame setup location marks the beginning of the function body. |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 716 | // FIXME: is there a simpler a way to do this? Can we just search |
| 717 | // for the first instruction of the function, not the last of the prolog? |
| 718 | DebugLoc PrologEndLoc; |
| 719 | bool EmptyPrologue = true; |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 720 | for (const auto &MBB : *MF) { |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 721 | for (const auto &MI : MBB) { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 722 | if (!MI.isDebugValue() && !MI.getFlag(MachineInstr::FrameSetup) && |
| 723 | MI.getDebugLoc()) { |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 724 | PrologEndLoc = MI.getDebugLoc(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 725 | break; |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 726 | } else if (!MI.isDebugValue()) { |
| 727 | EmptyPrologue = false; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 728 | } |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 729 | } |
| 730 | } |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 731 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 732 | // Record beginning of function if we have a non-empty prologue. |
Duncan P. N. Exon Smith | 9dffcd0 | 2015-03-30 19:14:47 +0000 | [diff] [blame] | 733 | if (PrologEndLoc && !EmptyPrologue) { |
| 734 | DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 735 | maybeRecordLocation(FnStartDL, MF); |
| 736 | } |
| 737 | } |
| 738 | |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 739 | TypeIndex CodeViewDebug::lowerType(const DIType *Ty) { |
| 740 | // Generic dispatch for lowering an unknown type. |
| 741 | switch (Ty->getTag()) { |
Adrian McCarthy | f3c3c13 | 2016-06-08 18:22:59 +0000 | [diff] [blame] | 742 | case dwarf::DW_TAG_array_type: |
| 743 | return lowerTypeArray(cast<DICompositeType>(Ty)); |
David Majnemer | d065e23 | 2016-06-02 06:21:37 +0000 | [diff] [blame] | 744 | case dwarf::DW_TAG_typedef: |
| 745 | return lowerTypeAlias(cast<DIDerivedType>(Ty)); |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 746 | case dwarf::DW_TAG_base_type: |
| 747 | return lowerTypeBasic(cast<DIBasicType>(Ty)); |
| 748 | case dwarf::DW_TAG_pointer_type: |
| 749 | case dwarf::DW_TAG_reference_type: |
| 750 | case dwarf::DW_TAG_rvalue_reference_type: |
| 751 | return lowerTypePointer(cast<DIDerivedType>(Ty)); |
| 752 | case dwarf::DW_TAG_ptr_to_member_type: |
| 753 | return lowerTypeMemberPointer(cast<DIDerivedType>(Ty)); |
| 754 | case dwarf::DW_TAG_const_type: |
| 755 | case dwarf::DW_TAG_volatile_type: |
| 756 | return lowerTypeModifier(cast<DIDerivedType>(Ty)); |
David Majnemer | 75c3ebf | 2016-06-02 17:13:53 +0000 | [diff] [blame] | 757 | case dwarf::DW_TAG_subroutine_type: |
| 758 | return lowerTypeFunction(cast<DISubroutineType>(Ty)); |
David Majnemer | 979cb88 | 2016-06-16 21:32:16 +0000 | [diff] [blame] | 759 | case dwarf::DW_TAG_enumeration_type: |
| 760 | return lowerTypeEnum(cast<DICompositeType>(Ty)); |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 761 | case dwarf::DW_TAG_class_type: |
| 762 | case dwarf::DW_TAG_structure_type: |
| 763 | return lowerTypeClass(cast<DICompositeType>(Ty)); |
| 764 | case dwarf::DW_TAG_union_type: |
| 765 | return lowerTypeUnion(cast<DICompositeType>(Ty)); |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 766 | default: |
| 767 | // Use the null type index. |
| 768 | return TypeIndex(); |
| 769 | } |
| 770 | } |
| 771 | |
David Majnemer | 3128b10 | 2016-06-15 18:00:01 +0000 | [diff] [blame] | 772 | static const DISubprogram *getQualifiedNameComponents( |
| 773 | const DIScope *Scope, SmallVectorImpl<StringRef> &QualifiedNameComponents) { |
| 774 | const DISubprogram *ClosestSubprogram = nullptr; |
| 775 | while (Scope != nullptr) { |
| 776 | if (ClosestSubprogram == nullptr) |
| 777 | ClosestSubprogram = dyn_cast<DISubprogram>(Scope); |
| 778 | StringRef ScopeName = Scope->getName(); |
| 779 | if (!ScopeName.empty()) |
| 780 | QualifiedNameComponents.push_back(ScopeName); |
| 781 | Scope = Scope->getScope().resolve(); |
| 782 | } |
| 783 | return ClosestSubprogram; |
| 784 | } |
| 785 | |
| 786 | static std::string getQualifiedName(ArrayRef<StringRef> QualifiedNameComponents, |
| 787 | StringRef TypeName) { |
| 788 | std::string FullyQualifiedName; |
| 789 | for (StringRef QualifiedNameComponent : reverse(QualifiedNameComponents)) { |
| 790 | FullyQualifiedName.append(QualifiedNameComponent); |
| 791 | FullyQualifiedName.append("::"); |
| 792 | } |
| 793 | FullyQualifiedName.append(TypeName); |
| 794 | return FullyQualifiedName; |
| 795 | } |
| 796 | |
David Majnemer | d065e23 | 2016-06-02 06:21:37 +0000 | [diff] [blame] | 797 | TypeIndex CodeViewDebug::lowerTypeAlias(const DIDerivedType *Ty) { |
David Majnemer | d065e23 | 2016-06-02 06:21:37 +0000 | [diff] [blame] | 798 | DITypeRef UnderlyingTypeRef = Ty->getBaseType(); |
| 799 | TypeIndex UnderlyingTypeIndex = getTypeIndex(UnderlyingTypeRef); |
David Majnemer | 3128b10 | 2016-06-15 18:00:01 +0000 | [diff] [blame] | 800 | StringRef TypeName = Ty->getName(); |
| 801 | |
| 802 | SmallVector<StringRef, 5> QualifiedNameComponents; |
| 803 | const DISubprogram *ClosestSubprogram = getQualifiedNameComponents( |
| 804 | Ty->getScope().resolve(), QualifiedNameComponents); |
| 805 | |
| 806 | if (ClosestSubprogram == nullptr) { |
| 807 | std::string FullyQualifiedName = |
| 808 | getQualifiedName(QualifiedNameComponents, TypeName); |
| 809 | GlobalUDTs.emplace_back(std::move(FullyQualifiedName), UnderlyingTypeIndex); |
| 810 | } else if (ClosestSubprogram == CurrentSubprogram) { |
| 811 | std::string FullyQualifiedName = |
| 812 | getQualifiedName(QualifiedNameComponents, TypeName); |
| 813 | LocalUDTs.emplace_back(std::move(FullyQualifiedName), UnderlyingTypeIndex); |
| 814 | } |
| 815 | // TODO: What if the ClosestSubprogram is neither null or the current |
| 816 | // subprogram? Currently, the UDT just gets dropped on the floor. |
| 817 | // |
| 818 | // The current behavior is not desirable. To get maximal fidelity, we would |
| 819 | // need to perform all type translation before beginning emission of .debug$S |
| 820 | // and then make LocalUDTs a member of FunctionInfo |
| 821 | |
David Majnemer | d065e23 | 2016-06-02 06:21:37 +0000 | [diff] [blame] | 822 | if (UnderlyingTypeIndex == TypeIndex(SimpleTypeKind::Int32Long) && |
David Majnemer | 3128b10 | 2016-06-15 18:00:01 +0000 | [diff] [blame] | 823 | TypeName == "HRESULT") |
David Majnemer | d065e23 | 2016-06-02 06:21:37 +0000 | [diff] [blame] | 824 | return TypeIndex(SimpleTypeKind::HResult); |
David Majnemer | 8c46a4c | 2016-06-04 15:40:33 +0000 | [diff] [blame] | 825 | if (UnderlyingTypeIndex == TypeIndex(SimpleTypeKind::UInt16Short) && |
David Majnemer | 3128b10 | 2016-06-15 18:00:01 +0000 | [diff] [blame] | 826 | TypeName == "wchar_t") |
David Majnemer | 8c46a4c | 2016-06-04 15:40:33 +0000 | [diff] [blame] | 827 | return TypeIndex(SimpleTypeKind::WideCharacter); |
David Majnemer | d065e23 | 2016-06-02 06:21:37 +0000 | [diff] [blame] | 828 | return UnderlyingTypeIndex; |
| 829 | } |
| 830 | |
Adrian McCarthy | f3c3c13 | 2016-06-08 18:22:59 +0000 | [diff] [blame] | 831 | TypeIndex CodeViewDebug::lowerTypeArray(const DICompositeType *Ty) { |
| 832 | DITypeRef ElementTypeRef = Ty->getBaseType(); |
| 833 | TypeIndex ElementTypeIndex = getTypeIndex(ElementTypeRef); |
| 834 | // IndexType is size_t, which depends on the bitness of the target. |
| 835 | TypeIndex IndexType = Asm->MAI->getPointerSize() == 8 |
| 836 | ? TypeIndex(SimpleTypeKind::UInt64Quad) |
| 837 | : TypeIndex(SimpleTypeKind::UInt32Long); |
| 838 | uint64_t Size = Ty->getSizeInBits() / 8; |
| 839 | ArrayRecord Record(ElementTypeIndex, IndexType, Size, Ty->getName()); |
| 840 | return TypeTable.writeArray(Record); |
| 841 | } |
| 842 | |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 843 | TypeIndex CodeViewDebug::lowerTypeBasic(const DIBasicType *Ty) { |
| 844 | TypeIndex Index; |
| 845 | dwarf::TypeKind Kind; |
| 846 | uint32_t ByteSize; |
| 847 | |
| 848 | Kind = static_cast<dwarf::TypeKind>(Ty->getEncoding()); |
David Majnemer | afefa67 | 2016-06-02 06:21:42 +0000 | [diff] [blame] | 849 | ByteSize = Ty->getSizeInBits() / 8; |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 850 | |
| 851 | SimpleTypeKind STK = SimpleTypeKind::None; |
| 852 | switch (Kind) { |
| 853 | case dwarf::DW_ATE_address: |
| 854 | // FIXME: Translate |
| 855 | break; |
| 856 | case dwarf::DW_ATE_boolean: |
| 857 | switch (ByteSize) { |
David Majnemer | 1c2cb1d | 2016-06-02 07:02:32 +0000 | [diff] [blame] | 858 | case 1: STK = SimpleTypeKind::Boolean8; break; |
| 859 | case 2: STK = SimpleTypeKind::Boolean16; break; |
| 860 | case 4: STK = SimpleTypeKind::Boolean32; break; |
| 861 | case 8: STK = SimpleTypeKind::Boolean64; break; |
| 862 | case 16: STK = SimpleTypeKind::Boolean128; break; |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 863 | } |
| 864 | break; |
| 865 | case dwarf::DW_ATE_complex_float: |
| 866 | switch (ByteSize) { |
David Majnemer | 1c2cb1d | 2016-06-02 07:02:32 +0000 | [diff] [blame] | 867 | case 2: STK = SimpleTypeKind::Complex16; break; |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 868 | case 4: STK = SimpleTypeKind::Complex32; break; |
| 869 | case 8: STK = SimpleTypeKind::Complex64; break; |
| 870 | case 10: STK = SimpleTypeKind::Complex80; break; |
| 871 | case 16: STK = SimpleTypeKind::Complex128; break; |
| 872 | } |
| 873 | break; |
| 874 | case dwarf::DW_ATE_float: |
| 875 | switch (ByteSize) { |
David Majnemer | 1c2cb1d | 2016-06-02 07:02:32 +0000 | [diff] [blame] | 876 | case 2: STK = SimpleTypeKind::Float16; break; |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 877 | case 4: STK = SimpleTypeKind::Float32; break; |
| 878 | case 6: STK = SimpleTypeKind::Float48; break; |
| 879 | case 8: STK = SimpleTypeKind::Float64; break; |
| 880 | case 10: STK = SimpleTypeKind::Float80; break; |
| 881 | case 16: STK = SimpleTypeKind::Float128; break; |
| 882 | } |
| 883 | break; |
| 884 | case dwarf::DW_ATE_signed: |
| 885 | switch (ByteSize) { |
David Majnemer | 1c2cb1d | 2016-06-02 07:02:32 +0000 | [diff] [blame] | 886 | case 1: STK = SimpleTypeKind::SByte; break; |
| 887 | case 2: STK = SimpleTypeKind::Int16Short; break; |
| 888 | case 4: STK = SimpleTypeKind::Int32; break; |
| 889 | case 8: STK = SimpleTypeKind::Int64Quad; break; |
| 890 | case 16: STK = SimpleTypeKind::Int128Oct; break; |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 891 | } |
| 892 | break; |
| 893 | case dwarf::DW_ATE_unsigned: |
| 894 | switch (ByteSize) { |
David Majnemer | 1c2cb1d | 2016-06-02 07:02:32 +0000 | [diff] [blame] | 895 | case 1: STK = SimpleTypeKind::Byte; break; |
| 896 | case 2: STK = SimpleTypeKind::UInt16Short; break; |
| 897 | case 4: STK = SimpleTypeKind::UInt32; break; |
| 898 | case 8: STK = SimpleTypeKind::UInt64Quad; break; |
| 899 | case 16: STK = SimpleTypeKind::UInt128Oct; break; |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 900 | } |
| 901 | break; |
| 902 | case dwarf::DW_ATE_UTF: |
| 903 | switch (ByteSize) { |
| 904 | case 2: STK = SimpleTypeKind::Character16; break; |
| 905 | case 4: STK = SimpleTypeKind::Character32; break; |
| 906 | } |
| 907 | break; |
| 908 | case dwarf::DW_ATE_signed_char: |
| 909 | if (ByteSize == 1) |
| 910 | STK = SimpleTypeKind::SignedCharacter; |
| 911 | break; |
| 912 | case dwarf::DW_ATE_unsigned_char: |
| 913 | if (ByteSize == 1) |
| 914 | STK = SimpleTypeKind::UnsignedCharacter; |
| 915 | break; |
| 916 | default: |
| 917 | break; |
| 918 | } |
| 919 | |
| 920 | // Apply some fixups based on the source-level type name. |
| 921 | if (STK == SimpleTypeKind::Int32 && Ty->getName() == "long int") |
| 922 | STK = SimpleTypeKind::Int32Long; |
| 923 | if (STK == SimpleTypeKind::UInt32 && Ty->getName() == "long unsigned int") |
| 924 | STK = SimpleTypeKind::UInt32Long; |
David Majnemer | 8c46a4c | 2016-06-04 15:40:33 +0000 | [diff] [blame] | 925 | if (STK == SimpleTypeKind::UInt16Short && |
| 926 | (Ty->getName() == "wchar_t" || Ty->getName() == "__wchar_t")) |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 927 | STK = SimpleTypeKind::WideCharacter; |
| 928 | if ((STK == SimpleTypeKind::SignedCharacter || |
| 929 | STK == SimpleTypeKind::UnsignedCharacter) && |
| 930 | Ty->getName() == "char") |
| 931 | STK = SimpleTypeKind::NarrowCharacter; |
| 932 | |
| 933 | return TypeIndex(STK); |
| 934 | } |
| 935 | |
| 936 | TypeIndex CodeViewDebug::lowerTypePointer(const DIDerivedType *Ty) { |
| 937 | TypeIndex PointeeTI = getTypeIndex(Ty->getBaseType()); |
| 938 | |
| 939 | // Pointers to simple types can use SimpleTypeMode, rather than having a |
| 940 | // dedicated pointer type record. |
| 941 | if (PointeeTI.isSimple() && |
| 942 | PointeeTI.getSimpleMode() == SimpleTypeMode::Direct && |
| 943 | Ty->getTag() == dwarf::DW_TAG_pointer_type) { |
| 944 | SimpleTypeMode Mode = Ty->getSizeInBits() == 64 |
| 945 | ? SimpleTypeMode::NearPointer64 |
| 946 | : SimpleTypeMode::NearPointer32; |
| 947 | return TypeIndex(PointeeTI.getSimpleKind(), Mode); |
| 948 | } |
| 949 | |
| 950 | PointerKind PK = |
| 951 | Ty->getSizeInBits() == 64 ? PointerKind::Near64 : PointerKind::Near32; |
| 952 | PointerMode PM = PointerMode::Pointer; |
| 953 | switch (Ty->getTag()) { |
| 954 | default: llvm_unreachable("not a pointer tag type"); |
| 955 | case dwarf::DW_TAG_pointer_type: |
| 956 | PM = PointerMode::Pointer; |
| 957 | break; |
| 958 | case dwarf::DW_TAG_reference_type: |
| 959 | PM = PointerMode::LValueReference; |
| 960 | break; |
| 961 | case dwarf::DW_TAG_rvalue_reference_type: |
| 962 | PM = PointerMode::RValueReference; |
| 963 | break; |
| 964 | } |
| 965 | // FIXME: MSVC folds qualifiers into PointerOptions in the context of a method |
| 966 | // 'this' pointer, but not normal contexts. Figure out what we're supposed to |
| 967 | // do. |
| 968 | PointerOptions PO = PointerOptions::None; |
| 969 | PointerRecord PR(PointeeTI, PK, PM, PO, Ty->getSizeInBits() / 8); |
| 970 | return TypeTable.writePointer(PR); |
| 971 | } |
| 972 | |
| 973 | TypeIndex CodeViewDebug::lowerTypeMemberPointer(const DIDerivedType *Ty) { |
| 974 | assert(Ty->getTag() == dwarf::DW_TAG_ptr_to_member_type); |
| 975 | TypeIndex ClassTI = getTypeIndex(Ty->getClassType()); |
| 976 | TypeIndex PointeeTI = getTypeIndex(Ty->getBaseType()); |
| 977 | PointerKind PK = Asm->MAI->getPointerSize() == 8 ? PointerKind::Near64 |
| 978 | : PointerKind::Near32; |
| 979 | PointerMode PM = isa<DISubroutineType>(Ty->getBaseType()) |
| 980 | ? PointerMode::PointerToMemberFunction |
| 981 | : PointerMode::PointerToDataMember; |
| 982 | PointerOptions PO = PointerOptions::None; // FIXME |
| 983 | // FIXME: Thread this ABI info through metadata. |
| 984 | PointerToMemberRepresentation PMR = PointerToMemberRepresentation::Unknown; |
| 985 | MemberPointerInfo MPI(ClassTI, PMR); |
| 986 | PointerRecord PR(PointeeTI, PK, PM, PO, Ty->getSizeInBits() / 8, MPI); |
| 987 | return TypeTable.writePointer(PR); |
| 988 | } |
| 989 | |
Reid Kleckner | de3d8b5 | 2016-06-08 20:34:29 +0000 | [diff] [blame] | 990 | /// Given a DWARF calling convention, get the CodeView equivalent. If we don't |
| 991 | /// have a translation, use the NearC convention. |
| 992 | static CallingConvention dwarfCCToCodeView(unsigned DwarfCC) { |
| 993 | switch (DwarfCC) { |
| 994 | case dwarf::DW_CC_normal: return CallingConvention::NearC; |
| 995 | case dwarf::DW_CC_BORLAND_msfastcall: return CallingConvention::NearFast; |
| 996 | case dwarf::DW_CC_BORLAND_thiscall: return CallingConvention::ThisCall; |
| 997 | case dwarf::DW_CC_BORLAND_stdcall: return CallingConvention::NearStdCall; |
| 998 | case dwarf::DW_CC_BORLAND_pascal: return CallingConvention::NearPascal; |
| 999 | case dwarf::DW_CC_LLVM_vectorcall: return CallingConvention::NearVector; |
| 1000 | } |
| 1001 | return CallingConvention::NearC; |
| 1002 | } |
| 1003 | |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1004 | TypeIndex CodeViewDebug::lowerTypeModifier(const DIDerivedType *Ty) { |
| 1005 | ModifierOptions Mods = ModifierOptions::None; |
| 1006 | bool IsModifier = true; |
| 1007 | const DIType *BaseTy = Ty; |
Reid Kleckner | b9c80fd | 2016-06-02 17:40:51 +0000 | [diff] [blame] | 1008 | while (IsModifier && BaseTy) { |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1009 | // FIXME: Need to add DWARF tag for __unaligned. |
| 1010 | switch (BaseTy->getTag()) { |
| 1011 | case dwarf::DW_TAG_const_type: |
| 1012 | Mods |= ModifierOptions::Const; |
| 1013 | break; |
| 1014 | case dwarf::DW_TAG_volatile_type: |
| 1015 | Mods |= ModifierOptions::Volatile; |
| 1016 | break; |
| 1017 | default: |
| 1018 | IsModifier = false; |
| 1019 | break; |
| 1020 | } |
| 1021 | if (IsModifier) |
| 1022 | BaseTy = cast<DIDerivedType>(BaseTy)->getBaseType().resolve(); |
| 1023 | } |
| 1024 | TypeIndex ModifiedTI = getTypeIndex(BaseTy); |
| 1025 | ModifierRecord MR(ModifiedTI, Mods); |
| 1026 | return TypeTable.writeModifier(MR); |
| 1027 | } |
| 1028 | |
David Majnemer | 75c3ebf | 2016-06-02 17:13:53 +0000 | [diff] [blame] | 1029 | TypeIndex CodeViewDebug::lowerTypeFunction(const DISubroutineType *Ty) { |
| 1030 | SmallVector<TypeIndex, 8> ReturnAndArgTypeIndices; |
| 1031 | for (DITypeRef ArgTypeRef : Ty->getTypeArray()) |
| 1032 | ReturnAndArgTypeIndices.push_back(getTypeIndex(ArgTypeRef)); |
| 1033 | |
| 1034 | TypeIndex ReturnTypeIndex = TypeIndex::Void(); |
| 1035 | ArrayRef<TypeIndex> ArgTypeIndices = None; |
| 1036 | if (!ReturnAndArgTypeIndices.empty()) { |
| 1037 | auto ReturnAndArgTypesRef = makeArrayRef(ReturnAndArgTypeIndices); |
| 1038 | ReturnTypeIndex = ReturnAndArgTypesRef.front(); |
| 1039 | ArgTypeIndices = ReturnAndArgTypesRef.drop_front(); |
| 1040 | } |
| 1041 | |
| 1042 | ArgListRecord ArgListRec(TypeRecordKind::ArgList, ArgTypeIndices); |
| 1043 | TypeIndex ArgListIndex = TypeTable.writeArgList(ArgListRec); |
| 1044 | |
Reid Kleckner | de3d8b5 | 2016-06-08 20:34:29 +0000 | [diff] [blame] | 1045 | CallingConvention CC = dwarfCCToCodeView(Ty->getCC()); |
| 1046 | |
David Majnemer | 75c3ebf | 2016-06-02 17:13:53 +0000 | [diff] [blame] | 1047 | // TODO: Some functions are member functions, we should use a more appropriate |
| 1048 | // record for those. |
Reid Kleckner | de3d8b5 | 2016-06-08 20:34:29 +0000 | [diff] [blame] | 1049 | ProcedureRecord Procedure(ReturnTypeIndex, CC, FunctionOptions::None, |
| 1050 | ArgTypeIndices.size(), ArgListIndex); |
David Majnemer | 75c3ebf | 2016-06-02 17:13:53 +0000 | [diff] [blame] | 1051 | return TypeTable.writeProcedure(Procedure); |
| 1052 | } |
| 1053 | |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 1054 | static MemberAccess translateAccessFlags(unsigned RecordTag, |
| 1055 | const DIType *Member) { |
| 1056 | switch (Member->getFlags() & DINode::FlagAccessibility) { |
| 1057 | case DINode::FlagPrivate: return MemberAccess::Private; |
| 1058 | case DINode::FlagPublic: return MemberAccess::Public; |
| 1059 | case DINode::FlagProtected: return MemberAccess::Protected; |
| 1060 | case 0: |
| 1061 | // If there was no explicit access control, provide the default for the tag. |
| 1062 | return RecordTag == dwarf::DW_TAG_class_type ? MemberAccess::Private |
| 1063 | : MemberAccess::Public; |
| 1064 | } |
| 1065 | llvm_unreachable("access flags are exclusive"); |
| 1066 | } |
| 1067 | |
| 1068 | static TypeRecordKind getRecordKind(const DICompositeType *Ty) { |
| 1069 | switch (Ty->getTag()) { |
| 1070 | case dwarf::DW_TAG_class_type: return TypeRecordKind::Class; |
| 1071 | case dwarf::DW_TAG_structure_type: return TypeRecordKind::Struct; |
| 1072 | } |
| 1073 | llvm_unreachable("unexpected tag"); |
| 1074 | } |
| 1075 | |
| 1076 | /// Return the HasUniqueName option if it should be present in ClassOptions, or |
| 1077 | /// None otherwise. |
| 1078 | static ClassOptions getRecordUniqueNameOption(const DICompositeType *Ty) { |
| 1079 | // MSVC always sets this flag now, even for local types. Clang doesn't always |
| 1080 | // appear to give every type a linkage name, which may be problematic for us. |
| 1081 | // FIXME: Investigate the consequences of not following them here. |
| 1082 | return !Ty->getIdentifier().empty() ? ClassOptions::HasUniqueName |
| 1083 | : ClassOptions::None; |
| 1084 | } |
| 1085 | |
David Majnemer | 979cb88 | 2016-06-16 21:32:16 +0000 | [diff] [blame] | 1086 | TypeIndex CodeViewDebug::lowerTypeEnum(const DICompositeType *Ty) { |
| 1087 | ClassOptions CO = ClassOptions::None | getRecordUniqueNameOption(Ty); |
| 1088 | TypeIndex FTI; |
| 1089 | unsigned FieldCount = 0; |
| 1090 | |
| 1091 | if (Ty->isForwardDecl()) |
| 1092 | CO |= ClassOptions::ForwardReference; |
| 1093 | else |
| 1094 | std::tie(FTI, FieldCount) = lowerRecordFieldList(Ty); |
| 1095 | |
| 1096 | return TypeTable.writeEnum(EnumRecord(FieldCount, CO, FTI, Ty->getName(), |
| 1097 | Ty->getIdentifier(), |
| 1098 | getTypeIndex(Ty->getBaseType()))); |
| 1099 | } |
| 1100 | |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 1101 | TypeIndex CodeViewDebug::lowerTypeClass(const DICompositeType *Ty) { |
| 1102 | // First, construct the forward decl. Don't look into Ty to compute the |
| 1103 | // forward decl options, since it might not be available in all TUs. |
| 1104 | TypeRecordKind Kind = getRecordKind(Ty); |
| 1105 | ClassOptions CO = |
| 1106 | ClassOptions::ForwardReference | getRecordUniqueNameOption(Ty); |
| 1107 | TypeIndex FwdDeclTI = TypeTable.writeClass(ClassRecord( |
| 1108 | Kind, 0, CO, HfaKind::None, WindowsRTClassKind::None, TypeIndex(), |
| 1109 | TypeIndex(), TypeIndex(), 0, Ty->getName(), Ty->getIdentifier())); |
| 1110 | return FwdDeclTI; |
| 1111 | } |
| 1112 | |
| 1113 | TypeIndex CodeViewDebug::lowerCompleteTypeClass(const DICompositeType *Ty) { |
| 1114 | // Construct the field list and complete type record. |
| 1115 | TypeRecordKind Kind = getRecordKind(Ty); |
| 1116 | // FIXME: Other ClassOptions, like ContainsNestedClass and NestedClass. |
| 1117 | ClassOptions CO = ClassOptions::None | getRecordUniqueNameOption(Ty); |
| 1118 | TypeIndex FTI; |
| 1119 | unsigned FieldCount; |
| 1120 | std::tie(FTI, FieldCount) = lowerRecordFieldList(Ty); |
| 1121 | |
| 1122 | uint64_t SizeInBytes = Ty->getSizeInBits() / 8; |
| 1123 | return TypeTable.writeClass(ClassRecord(Kind, FieldCount, CO, HfaKind::None, |
| 1124 | WindowsRTClassKind::None, FTI, |
| 1125 | TypeIndex(), TypeIndex(), SizeInBytes, |
| 1126 | Ty->getName(), Ty->getIdentifier())); |
| 1127 | // FIXME: Make an LF_UDT_SRC_LINE record. |
| 1128 | } |
| 1129 | |
| 1130 | TypeIndex CodeViewDebug::lowerTypeUnion(const DICompositeType *Ty) { |
| 1131 | ClassOptions CO = |
| 1132 | ClassOptions::ForwardReference | getRecordUniqueNameOption(Ty); |
| 1133 | TypeIndex FwdDeclTI = |
| 1134 | TypeTable.writeUnion(UnionRecord(0, CO, HfaKind::None, TypeIndex(), 0, |
| 1135 | Ty->getName(), Ty->getIdentifier())); |
| 1136 | return FwdDeclTI; |
| 1137 | } |
| 1138 | |
| 1139 | TypeIndex CodeViewDebug::lowerCompleteTypeUnion(const DICompositeType *Ty) { |
| 1140 | ClassOptions CO = ClassOptions::None | getRecordUniqueNameOption(Ty); |
| 1141 | TypeIndex FTI; |
| 1142 | unsigned FieldCount; |
| 1143 | std::tie(FTI, FieldCount) = lowerRecordFieldList(Ty); |
| 1144 | uint64_t SizeInBytes = Ty->getSizeInBits() / 8; |
| 1145 | return TypeTable.writeUnion(UnionRecord(FieldCount, CO, HfaKind::None, FTI, |
| 1146 | SizeInBytes, Ty->getName(), |
| 1147 | Ty->getIdentifier())); |
| 1148 | // FIXME: Make an LF_UDT_SRC_LINE record. |
| 1149 | } |
| 1150 | |
| 1151 | std::pair<TypeIndex, unsigned> |
| 1152 | CodeViewDebug::lowerRecordFieldList(const DICompositeType *Ty) { |
| 1153 | // Manually count members. MSVC appears to count everything that generates a |
| 1154 | // field list record. Each individual overload in a method overload group |
| 1155 | // contributes to this count, even though the overload group is a single field |
| 1156 | // list record. |
| 1157 | unsigned MemberCount = 0; |
| 1158 | FieldListRecordBuilder Fields; |
| 1159 | for (const DINode *Element : Ty->getElements()) { |
| 1160 | // We assume that the frontend provides all members in source declaration |
| 1161 | // order, which is what MSVC does. |
| 1162 | if (!Element) |
| 1163 | continue; |
| 1164 | if (auto *SP = dyn_cast<DISubprogram>(Element)) { |
| 1165 | // C++ method. |
| 1166 | // FIXME: Overloaded methods are grouped together, so we'll need two |
| 1167 | // passes to group them. |
| 1168 | (void)SP; |
| 1169 | } else if (auto *Member = dyn_cast<DIDerivedType>(Element)) { |
| 1170 | if (Member->getTag() == dwarf::DW_TAG_member) { |
| 1171 | if (Member->isStaticMember()) { |
| 1172 | // Static data member. |
| 1173 | Fields.writeStaticDataMember(StaticDataMemberRecord( |
| 1174 | translateAccessFlags(Ty->getTag(), Member), |
| 1175 | getTypeIndex(Member->getBaseType()), Member->getName())); |
| 1176 | MemberCount++; |
| 1177 | } else { |
| 1178 | // Data member. |
| 1179 | // FIXME: Make a BitFieldRecord for bitfields. |
| 1180 | Fields.writeDataMember(DataMemberRecord( |
| 1181 | translateAccessFlags(Ty->getTag(), Member), |
| 1182 | getTypeIndex(Member->getBaseType()), |
| 1183 | Member->getOffsetInBits() / 8, Member->getName())); |
| 1184 | MemberCount++; |
| 1185 | } |
| 1186 | } else if (Member->getTag() == dwarf::DW_TAG_friend) { |
| 1187 | // Ignore friend members. It appears that MSVC emitted info about |
| 1188 | // friends in the past, but modern versions do not. |
| 1189 | } |
| 1190 | // FIXME: Get clang to emit nested types here and do something with |
| 1191 | // them. |
David Majnemer | 979cb88 | 2016-06-16 21:32:16 +0000 | [diff] [blame] | 1192 | } else if (auto *Enumerator = dyn_cast<DIEnumerator>(Element)) { |
| 1193 | Fields.writeEnumerator(EnumeratorRecord( |
| 1194 | MemberAccess::Public, APSInt::getUnsigned(Enumerator->getValue()), |
| 1195 | Enumerator->getName())); |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 1196 | } |
| 1197 | // Skip other unrecognized kinds of elements. |
| 1198 | } |
| 1199 | return {TypeTable.writeFieldList(Fields), MemberCount}; |
| 1200 | } |
| 1201 | |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1202 | TypeIndex CodeViewDebug::getTypeIndex(DITypeRef TypeRef) { |
| 1203 | const DIType *Ty = TypeRef.resolve(); |
| 1204 | |
| 1205 | // The null DIType is the void type. Don't try to hash it. |
| 1206 | if (!Ty) |
| 1207 | return TypeIndex::Void(); |
| 1208 | |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 1209 | // Check if we've already translated this type. Don't try to do a |
| 1210 | // get-or-create style insertion that caches the hash lookup across the |
| 1211 | // lowerType call. It will update the TypeIndices map. |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1212 | auto I = TypeIndices.find(Ty); |
| 1213 | if (I != TypeIndices.end()) |
| 1214 | return I->second; |
| 1215 | |
| 1216 | TypeIndex TI = lowerType(Ty); |
| 1217 | |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 1218 | recordTypeIndexForDINode(Ty, TI); |
| 1219 | return TI; |
| 1220 | } |
| 1221 | |
| 1222 | TypeIndex CodeViewDebug::getCompleteTypeIndex(DITypeRef TypeRef) { |
| 1223 | const DIType *Ty = TypeRef.resolve(); |
| 1224 | |
| 1225 | // The null DIType is the void type. Don't try to hash it. |
| 1226 | if (!Ty) |
| 1227 | return TypeIndex::Void(); |
| 1228 | |
| 1229 | // If this is a non-record type, the complete type index is the same as the |
| 1230 | // normal type index. Just call getTypeIndex. |
| 1231 | switch (Ty->getTag()) { |
| 1232 | case dwarf::DW_TAG_class_type: |
| 1233 | case dwarf::DW_TAG_structure_type: |
| 1234 | case dwarf::DW_TAG_union_type: |
| 1235 | break; |
| 1236 | default: |
| 1237 | return getTypeIndex(Ty); |
| 1238 | } |
| 1239 | |
| 1240 | // Check if we've already translated the complete record type. Lowering a |
| 1241 | // complete type should never trigger lowering another complete type, so we |
| 1242 | // can reuse the hash table lookup result. |
| 1243 | const auto *CTy = cast<DICompositeType>(Ty); |
| 1244 | auto InsertResult = CompleteTypeIndices.insert({CTy, TypeIndex()}); |
| 1245 | if (!InsertResult.second) |
| 1246 | return InsertResult.first->second; |
| 1247 | |
| 1248 | // Make sure the forward declaration is emitted first. It's unclear if this |
| 1249 | // is necessary, but MSVC does it, and we should follow suit until we can show |
| 1250 | // otherwise. |
| 1251 | TypeIndex FwdDeclTI = getTypeIndex(CTy); |
| 1252 | |
| 1253 | // Just use the forward decl if we don't have complete type info. This might |
| 1254 | // happen if the frontend is using modules and expects the complete definition |
| 1255 | // to be emitted elsewhere. |
| 1256 | if (CTy->isForwardDecl()) |
| 1257 | return FwdDeclTI; |
| 1258 | |
| 1259 | TypeIndex TI; |
| 1260 | switch (CTy->getTag()) { |
| 1261 | case dwarf::DW_TAG_class_type: |
| 1262 | case dwarf::DW_TAG_structure_type: |
| 1263 | TI = lowerCompleteTypeClass(CTy); |
| 1264 | break; |
| 1265 | case dwarf::DW_TAG_union_type: |
| 1266 | TI = lowerCompleteTypeUnion(CTy); |
| 1267 | break; |
| 1268 | default: |
| 1269 | llvm_unreachable("not a record"); |
| 1270 | } |
| 1271 | |
| 1272 | InsertResult.first->second = TI; |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1273 | return TI; |
| 1274 | } |
| 1275 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1276 | void CodeViewDebug::emitLocalVariable(const LocalVariable &Var) { |
| 1277 | // LocalSym record, see SymbolRecord.h for more info. |
| 1278 | MCSymbol *LocalBegin = MMI->getContext().createTempSymbol(), |
| 1279 | *LocalEnd = MMI->getContext().createTempSymbol(); |
| 1280 | OS.AddComment("Record length"); |
| 1281 | OS.emitAbsoluteSymbolDiff(LocalEnd, LocalBegin, 2); |
| 1282 | OS.EmitLabel(LocalBegin); |
| 1283 | |
| 1284 | OS.AddComment("Record kind: S_LOCAL"); |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 1285 | OS.EmitIntValue(unsigned(SymbolKind::S_LOCAL), 2); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1286 | |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 1287 | LocalSymFlags Flags = LocalSymFlags::None; |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1288 | if (Var.DIVar->isParameter()) |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 1289 | Flags |= LocalSymFlags::IsParameter; |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 1290 | if (Var.DefRanges.empty()) |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 1291 | Flags |= LocalSymFlags::IsOptimizedOut; |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1292 | |
| 1293 | OS.AddComment("TypeIndex"); |
Reid Kleckner | a8d5740 | 2016-06-03 15:58:20 +0000 | [diff] [blame] | 1294 | TypeIndex TI = getCompleteTypeIndex(Var.DIVar->getType()); |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame] | 1295 | OS.EmitIntValue(TI.getIndex(), 4); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1296 | OS.AddComment("Flags"); |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 1297 | OS.EmitIntValue(static_cast<uint16_t>(Flags), 2); |
David Majnemer | 1256125 | 2016-03-13 10:53:30 +0000 | [diff] [blame] | 1298 | // Truncate the name so we won't overflow the record length field. |
David Majnemer | b9456a5 | 2016-03-14 05:15:09 +0000 | [diff] [blame] | 1299 | emitNullTerminatedSymbolName(OS, Var.DIVar->getName()); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1300 | OS.EmitLabel(LocalEnd); |
| 1301 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 1302 | // Calculate the on disk prefix of the appropriate def range record. The |
| 1303 | // records and on disk formats are described in SymbolRecords.h. BytePrefix |
| 1304 | // should be big enough to hold all forms without memory allocation. |
| 1305 | SmallString<20> BytePrefix; |
| 1306 | for (const LocalVarDefRange &DefRange : Var.DefRanges) { |
| 1307 | BytePrefix.clear(); |
| 1308 | // FIXME: Handle bitpieces. |
| 1309 | if (DefRange.StructOffset != 0) |
| 1310 | continue; |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1311 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 1312 | if (DefRange.InMemory) { |
Zachary Turner | a78ecd1 | 2016-05-23 18:49:06 +0000 | [diff] [blame] | 1313 | DefRangeRegisterRelSym Sym(DefRange.CVRegister, 0, DefRange.DataOffset, 0, |
| 1314 | 0, 0, ArrayRef<LocalVariableAddrGap>()); |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 1315 | ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER_REL); |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 1316 | BytePrefix += |
| 1317 | StringRef(reinterpret_cast<const char *>(&SymKind), sizeof(SymKind)); |
Zachary Turner | a78ecd1 | 2016-05-23 18:49:06 +0000 | [diff] [blame] | 1318 | BytePrefix += |
| 1319 | StringRef(reinterpret_cast<const char *>(&Sym.Header), |
| 1320 | sizeof(Sym.Header) - sizeof(LocalVariableAddrRange)); |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 1321 | } else { |
| 1322 | assert(DefRange.DataOffset == 0 && "unexpected offset into register"); |
Zachary Turner | a78ecd1 | 2016-05-23 18:49:06 +0000 | [diff] [blame] | 1323 | // Unclear what matters here. |
| 1324 | DefRangeRegisterSym Sym(DefRange.CVRegister, 0, 0, 0, 0, |
| 1325 | ArrayRef<LocalVariableAddrGap>()); |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 1326 | ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER); |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 1327 | BytePrefix += |
| 1328 | StringRef(reinterpret_cast<const char *>(&SymKind), sizeof(SymKind)); |
Zachary Turner | a78ecd1 | 2016-05-23 18:49:06 +0000 | [diff] [blame] | 1329 | BytePrefix += |
| 1330 | StringRef(reinterpret_cast<const char *>(&Sym.Header), |
| 1331 | sizeof(Sym.Header) - sizeof(LocalVariableAddrRange)); |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 1332 | } |
| 1333 | OS.EmitCVDefRangeDirective(DefRange.Ranges, BytePrefix); |
| 1334 | } |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 1337 | void CodeViewDebug::endFunction(const MachineFunction *MF) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 1338 | if (!Asm || !CurFn) // We haven't created any debug info for this function. |
| 1339 | return; |
| 1340 | |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 1341 | const Function *GV = MF->getFunction(); |
Yaron Keren | 6d3194f | 2014-06-20 10:26:56 +0000 | [diff] [blame] | 1342 | assert(FnDebugInfo.count(GV)); |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 1343 | assert(CurFn == &FnDebugInfo[GV]); |
| 1344 | |
Pete Cooper | adebb93 | 2016-03-11 02:14:16 +0000 | [diff] [blame] | 1345 | collectVariableInfo(GV->getSubprogram()); |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 1346 | |
| 1347 | DebugHandlerBase::endFunction(MF); |
| 1348 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 1349 | // Don't emit anything if we don't have any line tables. |
| 1350 | if (!CurFn->HaveLineInfo) { |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 1351 | FnDebugInfo.erase(GV); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1352 | CurFn = nullptr; |
| 1353 | return; |
Timur Iskhodzhanov | 8499a12 | 2014-03-26 09:50:36 +0000 | [diff] [blame] | 1354 | } |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1355 | |
| 1356 | CurFn->End = Asm->getFunctionEnd(); |
| 1357 | |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1358 | CurFn = nullptr; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 1361 | void CodeViewDebug::beginInstruction(const MachineInstr *MI) { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1362 | DebugHandlerBase::beginInstruction(MI); |
| 1363 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 1364 | // Ignore DBG_VALUE locations and function prologue. |
| 1365 | if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup)) |
| 1366 | return; |
| 1367 | DebugLoc DL = MI->getDebugLoc(); |
Duncan P. N. Exon Smith | 9dffcd0 | 2015-03-30 19:14:47 +0000 | [diff] [blame] | 1368 | if (DL == PrevInstLoc || !DL) |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 1369 | return; |
| 1370 | maybeRecordLocation(DL, Asm->MF); |
| 1371 | } |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 1372 | |
| 1373 | MCSymbol *CodeViewDebug::beginCVSubsection(ModuleSubstreamKind Kind) { |
| 1374 | MCSymbol *BeginLabel = MMI->getContext().createTempSymbol(), |
| 1375 | *EndLabel = MMI->getContext().createTempSymbol(); |
| 1376 | OS.EmitIntValue(unsigned(Kind), 4); |
| 1377 | OS.AddComment("Subsection size"); |
| 1378 | OS.emitAbsoluteSymbolDiff(EndLabel, BeginLabel, 4); |
| 1379 | OS.EmitLabel(BeginLabel); |
| 1380 | return EndLabel; |
| 1381 | } |
| 1382 | |
| 1383 | void CodeViewDebug::endCVSubsection(MCSymbol *EndLabel) { |
| 1384 | OS.EmitLabel(EndLabel); |
| 1385 | // Every subsection must be aligned to a 4-byte boundary. |
| 1386 | OS.EmitValueToAlignment(4); |
| 1387 | } |
| 1388 | |
David Majnemer | 3128b10 | 2016-06-15 18:00:01 +0000 | [diff] [blame] | 1389 | void CodeViewDebug::emitDebugInfoForUDTs( |
| 1390 | ArrayRef<std::pair<std::string, TypeIndex>> UDTs) { |
| 1391 | for (const std::pair<std::string, codeview::TypeIndex> &UDT : UDTs) { |
| 1392 | MCSymbol *UDTRecordBegin = MMI->getContext().createTempSymbol(), |
| 1393 | *UDTRecordEnd = MMI->getContext().createTempSymbol(); |
| 1394 | OS.AddComment("Record length"); |
| 1395 | OS.emitAbsoluteSymbolDiff(UDTRecordEnd, UDTRecordBegin, 2); |
| 1396 | OS.EmitLabel(UDTRecordBegin); |
| 1397 | |
| 1398 | OS.AddComment("Record kind: S_UDT"); |
| 1399 | OS.EmitIntValue(unsigned(SymbolKind::S_UDT), 2); |
| 1400 | |
| 1401 | OS.AddComment("Type"); |
| 1402 | OS.EmitIntValue(UDT.second.getIndex(), 4); |
| 1403 | |
| 1404 | emitNullTerminatedSymbolName(OS, UDT.first); |
| 1405 | OS.EmitLabel(UDTRecordEnd); |
| 1406 | } |
| 1407 | } |
| 1408 | |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 1409 | void CodeViewDebug::emitDebugInfoForGlobals() { |
| 1410 | NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu"); |
| 1411 | for (const MDNode *Node : CUs->operands()) { |
| 1412 | const auto *CU = cast<DICompileUnit>(Node); |
| 1413 | |
| 1414 | // First, emit all globals that are not in a comdat in a single symbol |
| 1415 | // substream. MSVC doesn't like it if the substream is empty, so only open |
| 1416 | // it if we have at least one global to emit. |
| 1417 | switchToDebugSectionForSymbol(nullptr); |
| 1418 | MCSymbol *EndLabel = nullptr; |
| 1419 | for (const DIGlobalVariable *G : CU->getGlobalVariables()) { |
Reid Kleckner | 6d1d275 | 2016-06-09 00:29:00 +0000 | [diff] [blame] | 1420 | if (const auto *GV = dyn_cast_or_null<GlobalVariable>(G->getVariable())) { |
David Majnemer | 577be0f | 2016-06-15 00:19:52 +0000 | [diff] [blame] | 1421 | if (!GV->hasComdat() && !GV->isDeclarationForLinker()) { |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 1422 | if (!EndLabel) { |
| 1423 | OS.AddComment("Symbol subsection for globals"); |
| 1424 | EndLabel = beginCVSubsection(ModuleSubstreamKind::Symbols); |
| 1425 | } |
| 1426 | emitDebugInfoForGlobal(G, Asm->getSymbol(GV)); |
| 1427 | } |
Reid Kleckner | 6d1d275 | 2016-06-09 00:29:00 +0000 | [diff] [blame] | 1428 | } |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 1429 | } |
| 1430 | if (EndLabel) |
| 1431 | endCVSubsection(EndLabel); |
| 1432 | |
| 1433 | // Second, emit each global that is in a comdat into its own .debug$S |
| 1434 | // section along with its own symbol substream. |
| 1435 | for (const DIGlobalVariable *G : CU->getGlobalVariables()) { |
Reid Kleckner | 6d1d275 | 2016-06-09 00:29:00 +0000 | [diff] [blame] | 1436 | if (const auto *GV = dyn_cast_or_null<GlobalVariable>(G->getVariable())) { |
Reid Kleckner | 6f3406d | 2016-06-07 00:02:03 +0000 | [diff] [blame] | 1437 | if (GV->hasComdat()) { |
| 1438 | MCSymbol *GVSym = Asm->getSymbol(GV); |
| 1439 | OS.AddComment("Symbol subsection for " + |
| 1440 | Twine(GlobalValue::getRealLinkageName(GV->getName()))); |
| 1441 | switchToDebugSectionForSymbol(GVSym); |
| 1442 | EndLabel = beginCVSubsection(ModuleSubstreamKind::Symbols); |
| 1443 | emitDebugInfoForGlobal(G, GVSym); |
| 1444 | endCVSubsection(EndLabel); |
| 1445 | } |
| 1446 | } |
| 1447 | } |
| 1448 | } |
| 1449 | } |
| 1450 | |
| 1451 | void CodeViewDebug::emitDebugInfoForGlobal(const DIGlobalVariable *DIGV, |
| 1452 | MCSymbol *GVSym) { |
| 1453 | // DataSym record, see SymbolRecord.h for more info. |
| 1454 | // FIXME: Thread local data, etc |
| 1455 | MCSymbol *DataBegin = MMI->getContext().createTempSymbol(), |
| 1456 | *DataEnd = MMI->getContext().createTempSymbol(); |
| 1457 | OS.AddComment("Record length"); |
| 1458 | OS.emitAbsoluteSymbolDiff(DataEnd, DataBegin, 2); |
| 1459 | OS.EmitLabel(DataBegin); |
| 1460 | OS.AddComment("Record kind: S_GDATA32"); |
| 1461 | OS.EmitIntValue(unsigned(SymbolKind::S_GDATA32), 2); |
| 1462 | OS.AddComment("Type"); |
| 1463 | OS.EmitIntValue(getCompleteTypeIndex(DIGV->getType()).getIndex(), 4); |
| 1464 | OS.AddComment("DataOffset"); |
| 1465 | OS.EmitCOFFSecRel32(GVSym); |
| 1466 | OS.AddComment("Segment"); |
| 1467 | OS.EmitCOFFSectionIndex(GVSym); |
| 1468 | OS.AddComment("Name"); |
| 1469 | emitNullTerminatedSymbolName(OS, DIGV->getName()); |
| 1470 | OS.EmitLabel(DataEnd); |
| 1471 | } |