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