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