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