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 | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/CodeView/Line.h" |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 17 | #include "llvm/DebugInfo/CodeView/SymbolRecord.h" |
Reid Kleckner | fbdbe9e | 2016-05-31 18:45:36 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/CodeView/TypeDumper.h" |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/CodeView/TypeIndex.h" |
| 20 | #include "llvm/DebugInfo/CodeView/TypeRecord.h" |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCExpr.h" |
Reid Kleckner | 5d122f8 | 2016-05-25 23:16:12 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCSectionCOFF.h" |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCSymbol.h" |
| 24 | #include "llvm/Support/COFF.h" |
Reid Kleckner | fbdbe9e | 2016-05-31 18:45:36 +0000 | [diff] [blame] | 25 | #include "llvm/Support/ScopedPrinter.h" |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetSubtargetInfo.h" |
| 27 | #include "llvm/Target/TargetRegisterInfo.h" |
| 28 | #include "llvm/Target/TargetFrameLowering.h" |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 29 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 30 | using namespace llvm; |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 31 | using namespace llvm::codeview; |
| 32 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 33 | CodeViewDebug::CodeViewDebug(AsmPrinter *AP) |
| 34 | : DebugHandlerBase(AP), OS(*Asm->OutStreamer), CurFn(nullptr) { |
| 35 | // If module doesn't have named metadata anchors or COFF debug section |
| 36 | // is not available, skip any debug info related stuff. |
| 37 | if (!MMI->getModule()->getNamedMetadata("llvm.dbg.cu") || |
| 38 | !AP->getObjFileLowering().getCOFFDebugSymbolsSection()) { |
| 39 | Asm = nullptr; |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | // Tell MMI that we have debug info. |
| 44 | MMI->setDebugInfoAvailability(true); |
| 45 | } |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 46 | |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 47 | StringRef CodeViewDebug::getFullFilepath(const DIFile *File) { |
| 48 | std::string &Filepath = FileToFilepathMap[File]; |
Reid Kleckner | 1f11b4e | 2015-12-02 22:34:30 +0000 | [diff] [blame] | 49 | if (!Filepath.empty()) |
| 50 | return Filepath; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 51 | |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 52 | StringRef Dir = File->getDirectory(), Filename = File->getFilename(); |
| 53 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 54 | // Clang emits directory and relative filename info into the IR, but CodeView |
| 55 | // operates on full paths. We could change Clang to emit full paths too, but |
| 56 | // that would increase the IR size and probably not needed for other users. |
| 57 | // For now, just concatenate and canonicalize the path here. |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 58 | if (Filename.find(':') == 1) |
| 59 | Filepath = Filename; |
| 60 | else |
Yaron Keren | 75e0c4b | 2015-03-27 17:51:30 +0000 | [diff] [blame] | 61 | Filepath = (Dir + "\\" + Filename).str(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 62 | |
| 63 | // Canonicalize the path. We have to do it textually because we may no longer |
| 64 | // have access the file in the filesystem. |
| 65 | // First, replace all slashes with backslashes. |
| 66 | std::replace(Filepath.begin(), Filepath.end(), '/', '\\'); |
| 67 | |
| 68 | // Remove all "\.\" with "\". |
| 69 | size_t Cursor = 0; |
| 70 | while ((Cursor = Filepath.find("\\.\\", Cursor)) != std::string::npos) |
| 71 | Filepath.erase(Cursor, 2); |
| 72 | |
| 73 | // Replace all "\XXX\..\" with "\". Don't try too hard though as the original |
| 74 | // path should be well-formatted, e.g. start with a drive letter, etc. |
| 75 | Cursor = 0; |
| 76 | while ((Cursor = Filepath.find("\\..\\", Cursor)) != std::string::npos) { |
| 77 | // Something's wrong if the path starts with "\..\", abort. |
| 78 | if (Cursor == 0) |
| 79 | break; |
| 80 | |
| 81 | size_t PrevSlash = Filepath.rfind('\\', Cursor - 1); |
| 82 | if (PrevSlash == std::string::npos) |
| 83 | // Something's wrong, abort. |
| 84 | break; |
| 85 | |
| 86 | Filepath.erase(PrevSlash, Cursor + 3 - PrevSlash); |
| 87 | // The next ".." might be following the one we've just erased. |
| 88 | Cursor = PrevSlash; |
| 89 | } |
| 90 | |
| 91 | // Remove all duplicate backslashes. |
| 92 | Cursor = 0; |
| 93 | while ((Cursor = Filepath.find("\\\\", Cursor)) != std::string::npos) |
| 94 | Filepath.erase(Cursor, 1); |
| 95 | |
Reid Kleckner | 1f11b4e | 2015-12-02 22:34:30 +0000 | [diff] [blame] | 96 | return Filepath; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 99 | unsigned CodeViewDebug::maybeRecordFile(const DIFile *F) { |
| 100 | unsigned NextId = FileIdMap.size() + 1; |
| 101 | auto Insertion = FileIdMap.insert(std::make_pair(F, NextId)); |
| 102 | if (Insertion.second) { |
| 103 | // We have to compute the full filepath and emit a .cv_file directive. |
| 104 | StringRef FullPath = getFullFilepath(F); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 105 | NextId = OS.EmitCVFileDirective(NextId, FullPath); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 106 | assert(NextId == FileIdMap.size() && ".cv_file directive failed"); |
| 107 | } |
| 108 | return Insertion.first->second; |
| 109 | } |
| 110 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 111 | CodeViewDebug::InlineSite & |
| 112 | CodeViewDebug::getInlineSite(const DILocation *InlinedAt, |
| 113 | const DISubprogram *Inlinee) { |
Reid Kleckner | fbd7787 | 2016-03-18 18:54:32 +0000 | [diff] [blame] | 114 | auto SiteInsertion = CurFn->InlineSites.insert({InlinedAt, InlineSite()}); |
| 115 | InlineSite *Site = &SiteInsertion.first->second; |
| 116 | if (SiteInsertion.second) { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 117 | Site->SiteFuncId = NextFuncId++; |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 118 | Site->Inlinee = Inlinee; |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 119 | InlinedSubprograms.insert(Inlinee); |
| 120 | recordFuncIdForSubprogram(Inlinee); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 121 | } |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 122 | return *Site; |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 125 | TypeIndex CodeViewDebug::getGenericFunctionTypeIndex() { |
| 126 | if (VoidFnTyIdx.getIndex() != 0) |
| 127 | return VoidFnTyIdx; |
| 128 | |
| 129 | ArrayRef<TypeIndex> NoArgs; |
| 130 | ArgListRecord ArgListRec(TypeRecordKind::ArgList, NoArgs); |
| 131 | TypeIndex ArgListIndex = TypeTable.writeArgList(ArgListRec); |
| 132 | |
| 133 | ProcedureRecord Procedure(TypeIndex::Void(), CallingConvention::NearC, |
| 134 | FunctionOptions::None, 0, ArgListIndex); |
| 135 | VoidFnTyIdx = TypeTable.writeProcedure(Procedure); |
| 136 | return VoidFnTyIdx; |
| 137 | } |
| 138 | |
| 139 | void CodeViewDebug::recordFuncIdForSubprogram(const DISubprogram *SP) { |
| 140 | TypeIndex ParentScope = TypeIndex(0); |
| 141 | StringRef DisplayName = SP->getDisplayName(); |
| 142 | FuncIdRecord FuncId(ParentScope, getGenericFunctionTypeIndex(), DisplayName); |
| 143 | TypeIndex TI = TypeTable.writeFuncId(FuncId); |
| 144 | TypeIndices[SP] = TI; |
| 145 | } |
| 146 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 147 | void CodeViewDebug::recordLocalVariable(LocalVariable &&Var, |
| 148 | const DILocation *InlinedAt) { |
| 149 | if (InlinedAt) { |
| 150 | // This variable was inlined. Associate it with the InlineSite. |
| 151 | const DISubprogram *Inlinee = Var.DIVar->getScope()->getSubprogram(); |
| 152 | InlineSite &Site = getInlineSite(InlinedAt, Inlinee); |
| 153 | Site.InlinedLocals.emplace_back(Var); |
| 154 | } else { |
| 155 | // This variable goes in the main ProcSym. |
| 156 | CurFn->Locals.emplace_back(Var); |
| 157 | } |
| 158 | } |
| 159 | |
Reid Kleckner | 829365a | 2016-02-11 19:41:47 +0000 | [diff] [blame] | 160 | static void addLocIfNotPresent(SmallVectorImpl<const DILocation *> &Locs, |
| 161 | const DILocation *Loc) { |
| 162 | auto B = Locs.begin(), E = Locs.end(); |
| 163 | if (std::find(B, E, Loc) == E) |
| 164 | Locs.push_back(Loc); |
| 165 | } |
| 166 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 167 | void CodeViewDebug::maybeRecordLocation(DebugLoc DL, |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 168 | const MachineFunction *MF) { |
| 169 | // Skip this instruction if it has the same location as the previous one. |
| 170 | if (DL == CurFn->LastLoc) |
| 171 | return; |
| 172 | |
| 173 | const DIScope *Scope = DL.get()->getScope(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 174 | if (!Scope) |
| 175 | return; |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 176 | |
David Majnemer | c3340db | 2016-01-13 01:05:23 +0000 | [diff] [blame] | 177 | // 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] | 178 | LineInfo LI(DL.getLine(), DL.getLine(), /*IsStatement=*/true); |
| 179 | if (LI.getStartLine() != DL.getLine() || LI.isAlwaysStepInto() || |
| 180 | LI.isNeverStepInto()) |
David Majnemer | c3340db | 2016-01-13 01:05:23 +0000 | [diff] [blame] | 181 | return; |
| 182 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 183 | ColumnInfo CI(DL.getCol(), /*EndColumn=*/0); |
| 184 | if (CI.getStartColumn() != DL.getCol()) |
| 185 | return; |
Reid Kleckner | 00d9639 | 2016-01-29 00:13:28 +0000 | [diff] [blame] | 186 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 187 | if (!CurFn->HaveLineInfo) |
| 188 | CurFn->HaveLineInfo = true; |
| 189 | unsigned FileId = 0; |
| 190 | if (CurFn->LastLoc.get() && CurFn->LastLoc->getFile() == DL->getFile()) |
| 191 | FileId = CurFn->LastFileId; |
| 192 | else |
| 193 | FileId = CurFn->LastFileId = maybeRecordFile(DL->getFile()); |
| 194 | CurFn->LastLoc = DL; |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 195 | |
| 196 | unsigned FuncId = CurFn->FuncId; |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 197 | if (const DILocation *SiteLoc = DL->getInlinedAt()) { |
Reid Kleckner | 829365a | 2016-02-11 19:41:47 +0000 | [diff] [blame] | 198 | const DILocation *Loc = DL.get(); |
| 199 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 200 | // If this location was actually inlined from somewhere else, give it the ID |
| 201 | // of the inline call site. |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 202 | FuncId = |
| 203 | getInlineSite(SiteLoc, Loc->getScope()->getSubprogram()).SiteFuncId; |
Reid Kleckner | 829365a | 2016-02-11 19:41:47 +0000 | [diff] [blame] | 204 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 205 | // Ensure we have links in the tree of inline call sites. |
Reid Kleckner | 829365a | 2016-02-11 19:41:47 +0000 | [diff] [blame] | 206 | bool FirstLoc = true; |
| 207 | while ((SiteLoc = Loc->getInlinedAt())) { |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 208 | InlineSite &Site = |
| 209 | getInlineSite(SiteLoc, Loc->getScope()->getSubprogram()); |
Reid Kleckner | 829365a | 2016-02-11 19:41:47 +0000 | [diff] [blame] | 210 | if (!FirstLoc) |
| 211 | addLocIfNotPresent(Site.ChildSites, Loc); |
| 212 | FirstLoc = false; |
| 213 | Loc = SiteLoc; |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 214 | } |
Reid Kleckner | 829365a | 2016-02-11 19:41:47 +0000 | [diff] [blame] | 215 | addLocIfNotPresent(CurFn->ChildSites, Loc); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 218 | OS.EmitCVLocDirective(FuncId, FileId, DL.getLine(), DL.getCol(), |
| 219 | /*PrologueEnd=*/false, |
| 220 | /*IsStmt=*/false, DL->getFilename()); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Reid Kleckner | 5d122f8 | 2016-05-25 23:16:12 +0000 | [diff] [blame] | 223 | void CodeViewDebug::emitCodeViewMagicVersion() { |
| 224 | OS.EmitValueToAlignment(4); |
| 225 | OS.AddComment("Debug section magic"); |
| 226 | OS.EmitIntValue(COFF::DEBUG_SECTION_MAGIC, 4); |
| 227 | } |
| 228 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 229 | void CodeViewDebug::endModule() { |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 230 | if (FnDebugInfo.empty()) |
| 231 | return; |
| 232 | |
| 233 | assert(Asm != nullptr); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 234 | |
| 235 | // The COFF .debug$S section consists of several subsections, each starting |
| 236 | // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length |
| 237 | // of the payload followed by the payload itself. The subsections are 4-byte |
| 238 | // aligned. |
| 239 | |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 240 | // Make a subsection for all the inlined subprograms. |
Reid Kleckner | 5d122f8 | 2016-05-25 23:16:12 +0000 | [diff] [blame] | 241 | emitInlineeLinesSubsection(); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 242 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 243 | // Emit per-function debug information. |
| 244 | for (auto &P : FnDebugInfo) |
| 245 | emitDebugInfoForFunction(P.first, P.second); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 246 | |
Reid Kleckner | 5d122f8 | 2016-05-25 23:16:12 +0000 | [diff] [blame] | 247 | // Switch back to the generic .debug$S section after potentially processing |
| 248 | // comdat symbol sections. |
| 249 | switchToDebugSectionForSymbol(nullptr); |
| 250 | |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 251 | // This subsection holds a file index to offset in string table table. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 252 | OS.AddComment("File index to string table offset subsection"); |
| 253 | OS.EmitCVFileChecksumsDirective(); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 254 | |
| 255 | // This subsection holds the string table. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 256 | OS.AddComment("String table"); |
| 257 | OS.EmitCVStringTableDirective(); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 258 | |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame^] | 259 | // Emit type information last, so that any types we translate while emitting |
| 260 | // function info are included. |
| 261 | emitTypeInformation(); |
| 262 | |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 263 | clear(); |
| 264 | } |
| 265 | |
David Majnemer | b9456a5 | 2016-03-14 05:15:09 +0000 | [diff] [blame] | 266 | static void emitNullTerminatedSymbolName(MCStreamer &OS, StringRef S) { |
| 267 | // Microsoft's linker seems to have trouble with symbol names longer than |
| 268 | // 0xffd8 bytes. |
| 269 | S = S.substr(0, 0xffd8); |
| 270 | SmallString<32> NullTerminatedString(S); |
| 271 | NullTerminatedString.push_back('\0'); |
| 272 | OS.EmitBytes(NullTerminatedString); |
| 273 | } |
| 274 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 275 | void CodeViewDebug::emitTypeInformation() { |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 276 | // Do nothing if we have no debug info or if no non-trivial types were emitted |
| 277 | // to TypeTable during codegen. |
Reid Kleckner | fbd7787 | 2016-03-18 18:54:32 +0000 | [diff] [blame] | 278 | NamedMDNode *CU_Nodes = |
| 279 | MMI->getModule()->getNamedMetadata("llvm.dbg.cu"); |
| 280 | if (!CU_Nodes) |
| 281 | return; |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 282 | if (TypeTable.empty()) |
Reid Kleckner | fbd7787 | 2016-03-18 18:54:32 +0000 | [diff] [blame] | 283 | return; |
| 284 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 285 | // Start the .debug$T section with 0x4. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 286 | OS.SwitchSection(Asm->getObjFileLowering().getCOFFDebugTypesSection()); |
Reid Kleckner | 5d122f8 | 2016-05-25 23:16:12 +0000 | [diff] [blame] | 287 | emitCodeViewMagicVersion(); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 288 | |
Reid Kleckner | fbdbe9e | 2016-05-31 18:45:36 +0000 | [diff] [blame] | 289 | SmallString<8> CommentPrefix; |
| 290 | if (OS.isVerboseAsm()) { |
| 291 | CommentPrefix += '\t'; |
| 292 | CommentPrefix += Asm->MAI->getCommentString(); |
| 293 | CommentPrefix += ' '; |
| 294 | } |
| 295 | |
| 296 | CVTypeDumper CVTD(nullptr, /*PrintRecordBytes=*/false); |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 297 | TypeTable.ForEachRecord( |
Reid Kleckner | fbdbe9e | 2016-05-31 18:45:36 +0000 | [diff] [blame] | 298 | [&](TypeIndex Index, StringRef Record) { |
| 299 | if (OS.isVerboseAsm()) { |
| 300 | // Emit a block comment describing the type record for readability. |
| 301 | SmallString<512> CommentBlock; |
| 302 | raw_svector_ostream CommentOS(CommentBlock); |
| 303 | ScopedPrinter SP(CommentOS); |
| 304 | SP.setPrefix(CommentPrefix); |
| 305 | CVTD.setPrinter(&SP); |
| 306 | bool DumpSuccess = |
| 307 | CVTD.dump({Record.bytes_begin(), Record.bytes_end()}); |
| 308 | (void)DumpSuccess; |
| 309 | assert(DumpSuccess && "produced malformed type record"); |
| 310 | // emitRawComment will insert its own tab and comment string before |
| 311 | // the first line, so strip off our first one. It also prints its own |
| 312 | // newline. |
| 313 | OS.emitRawComment( |
| 314 | CommentOS.str().drop_front(CommentPrefix.size() - 1).rtrim()); |
| 315 | } |
| 316 | OS.EmitBinaryData(Record); |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 317 | }); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Reid Kleckner | 5d122f8 | 2016-05-25 23:16:12 +0000 | [diff] [blame] | 320 | void CodeViewDebug::emitInlineeLinesSubsection() { |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 321 | if (InlinedSubprograms.empty()) |
| 322 | return; |
| 323 | |
Reid Kleckner | 5d122f8 | 2016-05-25 23:16:12 +0000 | [diff] [blame] | 324 | // Use the generic .debug$S section. |
| 325 | switchToDebugSectionForSymbol(nullptr); |
| 326 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 327 | MCSymbol *InlineBegin = MMI->getContext().createTempSymbol(), |
| 328 | *InlineEnd = MMI->getContext().createTempSymbol(); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 329 | |
| 330 | OS.AddComment("Inlinee lines subsection"); |
| 331 | OS.EmitIntValue(unsigned(ModuleSubstreamKind::InlineeLines), 4); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 332 | OS.AddComment("Subsection size"); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 333 | OS.emitAbsoluteSymbolDiff(InlineEnd, InlineBegin, 4); |
| 334 | OS.EmitLabel(InlineBegin); |
| 335 | |
| 336 | // We don't provide any extra file info. |
| 337 | // FIXME: Find out if debuggers use this info. |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 338 | OS.AddComment("Inlinee lines signature"); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 339 | OS.EmitIntValue(unsigned(InlineeLinesSignature::Normal), 4); |
| 340 | |
| 341 | for (const DISubprogram *SP : InlinedSubprograms) { |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 342 | assert(TypeIndices.count(SP)); |
| 343 | TypeIndex InlineeIdx = TypeIndices[SP]; |
| 344 | |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 345 | OS.AddBlankLine(); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 346 | unsigned FileId = maybeRecordFile(SP->getFile()); |
| 347 | OS.AddComment("Inlined function " + SP->getDisplayName() + " starts at " + |
| 348 | SP->getFilename() + Twine(':') + Twine(SP->getLine())); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 349 | OS.AddBlankLine(); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 350 | // The filechecksum table uses 8 byte entries for now, and file ids start at |
| 351 | // 1. |
| 352 | unsigned FileOffset = (FileId - 1) * 8; |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 353 | OS.AddComment("Type index of inlined function"); |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 354 | OS.EmitIntValue(InlineeIdx.getIndex(), 4); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 355 | OS.AddComment("Offset into filechecksum table"); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 356 | OS.EmitIntValue(FileOffset, 4); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 357 | OS.AddComment("Starting line number"); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 358 | OS.EmitIntValue(SP->getLine(), 4); |
| 359 | } |
| 360 | |
| 361 | OS.EmitLabel(InlineEnd); |
| 362 | } |
| 363 | |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 364 | void CodeViewDebug::collectInlineSiteChildren( |
| 365 | SmallVectorImpl<unsigned> &Children, const FunctionInfo &FI, |
| 366 | const InlineSite &Site) { |
| 367 | for (const DILocation *ChildSiteLoc : Site.ChildSites) { |
| 368 | auto I = FI.InlineSites.find(ChildSiteLoc); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 369 | const InlineSite &ChildSite = I->second; |
| 370 | Children.push_back(ChildSite.SiteFuncId); |
| 371 | collectInlineSiteChildren(Children, FI, ChildSite); |
| 372 | } |
| 373 | } |
| 374 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 375 | void CodeViewDebug::emitInlinedCallSite(const FunctionInfo &FI, |
| 376 | const DILocation *InlinedAt, |
| 377 | const InlineSite &Site) { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 378 | MCSymbol *InlineBegin = MMI->getContext().createTempSymbol(), |
| 379 | *InlineEnd = MMI->getContext().createTempSymbol(); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 380 | |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 381 | assert(TypeIndices.count(Site.Inlinee)); |
| 382 | TypeIndex InlineeIdx = TypeIndices[Site.Inlinee]; |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 383 | |
| 384 | // SymbolRecord |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 385 | OS.AddComment("Record length"); |
Reid Kleckner | eb3bcdd | 2016-02-03 21:24:42 +0000 | [diff] [blame] | 386 | OS.emitAbsoluteSymbolDiff(InlineEnd, InlineBegin, 2); // RecordLength |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 387 | OS.EmitLabel(InlineBegin); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 388 | OS.AddComment("Record kind: S_INLINESITE"); |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 389 | OS.EmitIntValue(SymbolKind::S_INLINESITE, 2); // RecordKind |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 390 | |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 391 | OS.AddComment("PtrParent"); |
| 392 | OS.EmitIntValue(0, 4); |
| 393 | OS.AddComment("PtrEnd"); |
| 394 | OS.EmitIntValue(0, 4); |
| 395 | OS.AddComment("Inlinee type index"); |
Reid Kleckner | 2280f93 | 2016-05-23 20:23:46 +0000 | [diff] [blame] | 396 | OS.EmitIntValue(InlineeIdx.getIndex(), 4); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 397 | |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 398 | unsigned FileId = maybeRecordFile(Site.Inlinee->getFile()); |
| 399 | unsigned StartLineNum = Site.Inlinee->getLine(); |
| 400 | SmallVector<unsigned, 3> SecondaryFuncIds; |
| 401 | collectInlineSiteChildren(SecondaryFuncIds, FI, Site); |
| 402 | |
| 403 | OS.EmitCVInlineLinetableDirective(Site.SiteFuncId, FileId, StartLineNum, |
David Majnemer | c9911f2 | 2016-02-02 19:22:34 +0000 | [diff] [blame] | 404 | FI.Begin, FI.End, SecondaryFuncIds); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 405 | |
| 406 | OS.EmitLabel(InlineEnd); |
| 407 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 408 | for (const LocalVariable &Var : Site.InlinedLocals) |
| 409 | emitLocalVariable(Var); |
| 410 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 411 | // Recurse on child inlined call sites before closing the scope. |
| 412 | for (const DILocation *ChildSite : Site.ChildSites) { |
| 413 | auto I = FI.InlineSites.find(ChildSite); |
| 414 | assert(I != FI.InlineSites.end() && |
| 415 | "child site not in function inline site map"); |
| 416 | emitInlinedCallSite(FI, ChildSite, I->second); |
| 417 | } |
| 418 | |
| 419 | // Close the scope. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 420 | OS.AddComment("Record length"); |
| 421 | OS.EmitIntValue(2, 2); // RecordLength |
| 422 | OS.AddComment("Record kind: S_INLINESITE_END"); |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 423 | OS.EmitIntValue(SymbolKind::S_INLINESITE_END, 2); // RecordKind |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Reid Kleckner | 5d122f8 | 2016-05-25 23:16:12 +0000 | [diff] [blame] | 426 | void CodeViewDebug::switchToDebugSectionForSymbol(const MCSymbol *GVSym) { |
| 427 | // If we have a symbol, it may be in a section that is COMDAT. If so, find the |
| 428 | // comdat key. A section may be comdat because of -ffunction-sections or |
| 429 | // because it is comdat in the IR. |
| 430 | MCSectionCOFF *GVSec = |
| 431 | GVSym ? dyn_cast<MCSectionCOFF>(&GVSym->getSection()) : nullptr; |
| 432 | const MCSymbol *KeySym = GVSec ? GVSec->getCOMDATSymbol() : nullptr; |
| 433 | |
| 434 | MCSectionCOFF *DebugSec = cast<MCSectionCOFF>( |
| 435 | Asm->getObjFileLowering().getCOFFDebugSymbolsSection()); |
| 436 | DebugSec = OS.getContext().getAssociativeCOFFSection(DebugSec, KeySym); |
| 437 | |
| 438 | OS.SwitchSection(DebugSec); |
| 439 | |
| 440 | // Emit the magic version number if this is the first time we've switched to |
| 441 | // this section. |
| 442 | if (ComdatDebugSections.insert(DebugSec).second) |
| 443 | emitCodeViewMagicVersion(); |
| 444 | } |
| 445 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 446 | void CodeViewDebug::emitDebugInfoForFunction(const Function *GV, |
| 447 | FunctionInfo &FI) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 448 | // For each function there is a separate subsection |
| 449 | // which holds the PC to file:line table. |
| 450 | const MCSymbol *Fn = Asm->getSymbol(GV); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 451 | assert(Fn); |
Timur Iskhodzhanov | 8499a12 | 2014-03-26 09:50:36 +0000 | [diff] [blame] | 452 | |
Reid Kleckner | 5d122f8 | 2016-05-25 23:16:12 +0000 | [diff] [blame] | 453 | // Switch to the to a comdat section, if appropriate. |
| 454 | switchToDebugSectionForSymbol(Fn); |
| 455 | |
Duncan P. N. Exon Smith | 23e56ec | 2015-03-20 19:50:00 +0000 | [diff] [blame] | 456 | StringRef FuncName; |
Pete Cooper | adebb93 | 2016-03-11 02:14:16 +0000 | [diff] [blame] | 457 | if (auto *SP = GV->getSubprogram()) |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 458 | FuncName = SP->getDisplayName(); |
Duncan P. N. Exon Smith | 23e56ec | 2015-03-20 19:50:00 +0000 | [diff] [blame] | 459 | |
Reid Kleckner | 3c0ff98 | 2016-01-14 00:12:54 +0000 | [diff] [blame] | 460 | // If our DISubprogram name is empty, use the mangled name. |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 461 | if (FuncName.empty()) |
| 462 | FuncName = GlobalValue::getRealLinkageName(GV->getName()); |
Reid Kleckner | 3c0ff98 | 2016-01-14 00:12:54 +0000 | [diff] [blame] | 463 | |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 464 | // Emit a symbol subsection, required by VS2012+ to find function boundaries. |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 465 | MCSymbol *SymbolsBegin = MMI->getContext().createTempSymbol(), |
| 466 | *SymbolsEnd = MMI->getContext().createTempSymbol(); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 467 | OS.AddComment("Symbol subsection for " + Twine(FuncName)); |
| 468 | OS.EmitIntValue(unsigned(ModuleSubstreamKind::Symbols), 4); |
| 469 | OS.AddComment("Subsection size"); |
Reid Kleckner | eb3bcdd | 2016-02-03 21:24:42 +0000 | [diff] [blame] | 470 | OS.emitAbsoluteSymbolDiff(SymbolsEnd, SymbolsBegin, 4); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 471 | OS.EmitLabel(SymbolsBegin); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 472 | { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 473 | MCSymbol *ProcRecordBegin = MMI->getContext().createTempSymbol(), |
| 474 | *ProcRecordEnd = MMI->getContext().createTempSymbol(); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 475 | OS.AddComment("Record length"); |
Reid Kleckner | eb3bcdd | 2016-02-03 21:24:42 +0000 | [diff] [blame] | 476 | OS.emitAbsoluteSymbolDiff(ProcRecordEnd, ProcRecordBegin, 2); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 477 | OS.EmitLabel(ProcRecordBegin); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 478 | |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 479 | OS.AddComment("Record kind: S_GPROC32_ID"); |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 480 | OS.EmitIntValue(unsigned(SymbolKind::S_GPROC32_ID), 2); |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 481 | |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 482 | // 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] | 483 | OS.AddComment("PtrParent"); |
| 484 | OS.EmitIntValue(0, 4); |
| 485 | OS.AddComment("PtrEnd"); |
| 486 | OS.EmitIntValue(0, 4); |
| 487 | OS.AddComment("PtrNext"); |
| 488 | OS.EmitIntValue(0, 4); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 489 | // This is the important bit that tells the debugger where the function |
| 490 | // code is located and what's its size: |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 491 | OS.AddComment("Code size"); |
Reid Kleckner | eb3bcdd | 2016-02-03 21:24:42 +0000 | [diff] [blame] | 492 | OS.emitAbsoluteSymbolDiff(FI.End, Fn, 4); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 493 | OS.AddComment("Offset after prologue"); |
| 494 | OS.EmitIntValue(0, 4); |
| 495 | OS.AddComment("Offset before epilogue"); |
| 496 | OS.EmitIntValue(0, 4); |
| 497 | OS.AddComment("Function type index"); |
| 498 | OS.EmitIntValue(0, 4); |
| 499 | OS.AddComment("Function section relative address"); |
| 500 | OS.EmitCOFFSecRel32(Fn); |
| 501 | OS.AddComment("Function section index"); |
| 502 | OS.EmitCOFFSectionIndex(Fn); |
| 503 | OS.AddComment("Flags"); |
| 504 | OS.EmitIntValue(0, 1); |
Timur Iskhodzhanov | a11b32b | 2014-11-12 20:10:09 +0000 | [diff] [blame] | 505 | // Emit the function display name as a null-terminated string. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 506 | OS.AddComment("Function name"); |
David Majnemer | 1256125 | 2016-03-13 10:53:30 +0000 | [diff] [blame] | 507 | // Truncate the name so we won't overflow the record length field. |
David Majnemer | b9456a5 | 2016-03-14 05:15:09 +0000 | [diff] [blame] | 508 | emitNullTerminatedSymbolName(OS, FuncName); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 509 | OS.EmitLabel(ProcRecordEnd); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 510 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 511 | for (const LocalVariable &Var : FI.Locals) |
| 512 | emitLocalVariable(Var); |
| 513 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 514 | // Emit inlined call site information. Only emit functions inlined directly |
| 515 | // into the parent function. We'll emit the other sites recursively as part |
| 516 | // of their parent inline site. |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 517 | for (const DILocation *InlinedAt : FI.ChildSites) { |
| 518 | auto I = FI.InlineSites.find(InlinedAt); |
| 519 | assert(I != FI.InlineSites.end() && |
| 520 | "child site not in function inline site map"); |
| 521 | emitInlinedCallSite(FI, InlinedAt, I->second); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 524 | // We're done with this function. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 525 | OS.AddComment("Record length"); |
| 526 | OS.EmitIntValue(0x0002, 2); |
| 527 | OS.AddComment("Record kind: S_PROC_ID_END"); |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 528 | OS.EmitIntValue(unsigned(SymbolKind::S_PROC_ID_END), 2); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 529 | } |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 530 | OS.EmitLabel(SymbolsEnd); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 531 | // Every subsection must be aligned to a 4-byte boundary. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 532 | OS.EmitValueToAlignment(4); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 533 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 534 | // 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] | 535 | OS.EmitCVLinetableDirective(FI.FuncId, Fn, FI.End); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 538 | CodeViewDebug::LocalVarDefRange |
| 539 | CodeViewDebug::createDefRangeMem(uint16_t CVRegister, int Offset) { |
| 540 | LocalVarDefRange DR; |
Aaron Ballman | c6a2f21 | 2016-02-16 15:35:51 +0000 | [diff] [blame] | 541 | DR.InMemory = -1; |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 542 | DR.DataOffset = Offset; |
| 543 | assert(DR.DataOffset == Offset && "truncation"); |
| 544 | DR.StructOffset = 0; |
| 545 | DR.CVRegister = CVRegister; |
| 546 | return DR; |
| 547 | } |
| 548 | |
| 549 | CodeViewDebug::LocalVarDefRange |
| 550 | CodeViewDebug::createDefRangeReg(uint16_t CVRegister) { |
| 551 | LocalVarDefRange DR; |
| 552 | DR.InMemory = 0; |
| 553 | DR.DataOffset = 0; |
| 554 | DR.StructOffset = 0; |
| 555 | DR.CVRegister = CVRegister; |
| 556 | return DR; |
| 557 | } |
| 558 | |
| 559 | void CodeViewDebug::collectVariableInfoFromMMITable( |
| 560 | DenseSet<InlinedVariable> &Processed) { |
| 561 | const TargetSubtargetInfo &TSI = Asm->MF->getSubtarget(); |
| 562 | const TargetFrameLowering *TFI = TSI.getFrameLowering(); |
| 563 | const TargetRegisterInfo *TRI = TSI.getRegisterInfo(); |
| 564 | |
| 565 | for (const MachineModuleInfo::VariableDbgInfo &VI : |
| 566 | MMI->getVariableDbgInfo()) { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 567 | if (!VI.Var) |
| 568 | continue; |
| 569 | assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) && |
| 570 | "Expected inlined-at fields to agree"); |
| 571 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 572 | Processed.insert(InlinedVariable(VI.Var, VI.Loc->getInlinedAt())); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 573 | LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc); |
| 574 | |
| 575 | // If variable scope is not found then skip this variable. |
| 576 | if (!Scope) |
| 577 | continue; |
| 578 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 579 | // Get the frame register used and the offset. |
| 580 | unsigned FrameReg = 0; |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 581 | int FrameOffset = TFI->getFrameIndexReference(*Asm->MF, VI.Slot, FrameReg); |
| 582 | uint16_t CVReg = TRI->getCodeViewRegNum(FrameReg); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 583 | |
| 584 | // Calculate the label ranges. |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 585 | LocalVarDefRange DefRange = createDefRangeMem(CVReg, FrameOffset); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 586 | for (const InsnRange &Range : Scope->getRanges()) { |
| 587 | const MCSymbol *Begin = getLabelBeforeInsn(Range.first); |
| 588 | const MCSymbol *End = getLabelAfterInsn(Range.second); |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 589 | End = End ? End : Asm->getFunctionEnd(); |
| 590 | DefRange.Ranges.emplace_back(Begin, End); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 593 | LocalVariable Var; |
| 594 | Var.DIVar = VI.Var; |
| 595 | Var.DefRanges.emplace_back(std::move(DefRange)); |
| 596 | recordLocalVariable(std::move(Var), VI.Loc->getInlinedAt()); |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | void CodeViewDebug::collectVariableInfo(const DISubprogram *SP) { |
| 601 | DenseSet<InlinedVariable> Processed; |
| 602 | // Grab the variable info that was squirreled away in the MMI side-table. |
| 603 | collectVariableInfoFromMMITable(Processed); |
| 604 | |
| 605 | const TargetRegisterInfo *TRI = Asm->MF->getSubtarget().getRegisterInfo(); |
| 606 | |
| 607 | for (const auto &I : DbgValues) { |
| 608 | InlinedVariable IV = I.first; |
| 609 | if (Processed.count(IV)) |
| 610 | continue; |
| 611 | const DILocalVariable *DIVar = IV.first; |
| 612 | const DILocation *InlinedAt = IV.second; |
| 613 | |
| 614 | // Instruction ranges, specifying where IV is accessible. |
| 615 | const auto &Ranges = I.second; |
| 616 | |
| 617 | LexicalScope *Scope = nullptr; |
| 618 | if (InlinedAt) |
| 619 | Scope = LScopes.findInlinedScope(DIVar->getScope(), InlinedAt); |
| 620 | else |
| 621 | Scope = LScopes.findLexicalScope(DIVar->getScope()); |
| 622 | // If variable scope is not found then skip this variable. |
| 623 | if (!Scope) |
| 624 | continue; |
| 625 | |
| 626 | LocalVariable Var; |
| 627 | Var.DIVar = DIVar; |
| 628 | |
| 629 | // Calculate the definition ranges. |
| 630 | for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) { |
| 631 | const InsnRange &Range = *I; |
| 632 | const MachineInstr *DVInst = Range.first; |
| 633 | assert(DVInst->isDebugValue() && "Invalid History entry"); |
| 634 | const DIExpression *DIExpr = DVInst->getDebugExpression(); |
| 635 | |
| 636 | // Bail if there is a complex DWARF expression for now. |
| 637 | if (DIExpr && DIExpr->getNumElements() > 0) |
| 638 | continue; |
| 639 | |
Reid Kleckner | 9a593ee | 2016-02-16 21:49:26 +0000 | [diff] [blame] | 640 | // Bail if operand 0 is not a valid register. This means the variable is a |
| 641 | // simple constant, or is described by a complex expression. |
| 642 | // FIXME: Find a way to represent constant variables, since they are |
| 643 | // relatively common. |
| 644 | unsigned Reg = |
| 645 | DVInst->getOperand(0).isReg() ? DVInst->getOperand(0).getReg() : 0; |
| 646 | if (Reg == 0) |
Reid Kleckner | 6e0d5f5 | 2016-02-16 21:14:51 +0000 | [diff] [blame] | 647 | continue; |
| 648 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 649 | // Handle the two cases we can handle: indirect in memory and in register. |
| 650 | bool IsIndirect = DVInst->getOperand(1).isImm(); |
| 651 | unsigned CVReg = TRI->getCodeViewRegNum(DVInst->getOperand(0).getReg()); |
| 652 | { |
| 653 | LocalVarDefRange DefRange; |
| 654 | if (IsIndirect) { |
| 655 | int64_t Offset = DVInst->getOperand(1).getImm(); |
| 656 | DefRange = createDefRangeMem(CVReg, Offset); |
| 657 | } else { |
| 658 | DefRange = createDefRangeReg(CVReg); |
| 659 | } |
| 660 | if (Var.DefRanges.empty() || |
| 661 | Var.DefRanges.back().isDifferentLocation(DefRange)) { |
| 662 | Var.DefRanges.emplace_back(std::move(DefRange)); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | // Compute the label range. |
| 667 | const MCSymbol *Begin = getLabelBeforeInsn(Range.first); |
| 668 | const MCSymbol *End = getLabelAfterInsn(Range.second); |
| 669 | if (!End) { |
| 670 | if (std::next(I) != E) |
| 671 | End = getLabelBeforeInsn(std::next(I)->first); |
| 672 | else |
| 673 | End = Asm->getFunctionEnd(); |
| 674 | } |
| 675 | |
| 676 | // If the last range end is our begin, just extend the last range. |
| 677 | // Otherwise make a new range. |
| 678 | SmallVectorImpl<std::pair<const MCSymbol *, const MCSymbol *>> &Ranges = |
| 679 | Var.DefRanges.back().Ranges; |
| 680 | if (!Ranges.empty() && Ranges.back().second == Begin) |
| 681 | Ranges.back().second = End; |
| 682 | else |
| 683 | Ranges.emplace_back(Begin, End); |
| 684 | |
| 685 | // FIXME: Do more range combining. |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 686 | } |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 687 | |
| 688 | recordLocalVariable(std::move(Var), InlinedAt); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 692 | void CodeViewDebug::beginFunction(const MachineFunction *MF) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 693 | assert(!CurFn && "Can't process two functions at once!"); |
| 694 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 695 | if (!Asm || !MMI->hasDebugInfo()) |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 696 | return; |
| 697 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 698 | DebugHandlerBase::beginFunction(MF); |
| 699 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 700 | const Function *GV = MF->getFunction(); |
| 701 | assert(FnDebugInfo.count(GV) == false); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 702 | CurFn = &FnDebugInfo[GV]; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 703 | CurFn->FuncId = NextFuncId++; |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 704 | CurFn->Begin = Asm->getFunctionBegin(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 705 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 706 | // Find the end of the function prolog. First known non-DBG_VALUE and |
| 707 | // non-frame setup location marks the beginning of the function body. |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 708 | // FIXME: is there a simpler a way to do this? Can we just search |
| 709 | // for the first instruction of the function, not the last of the prolog? |
| 710 | DebugLoc PrologEndLoc; |
| 711 | bool EmptyPrologue = true; |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 712 | for (const auto &MBB : *MF) { |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 713 | for (const auto &MI : MBB) { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 714 | if (!MI.isDebugValue() && !MI.getFlag(MachineInstr::FrameSetup) && |
| 715 | MI.getDebugLoc()) { |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 716 | PrologEndLoc = MI.getDebugLoc(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 717 | break; |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 718 | } else if (!MI.isDebugValue()) { |
| 719 | EmptyPrologue = false; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 720 | } |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 721 | } |
| 722 | } |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 723 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 724 | // 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] | 725 | if (PrologEndLoc && !EmptyPrologue) { |
| 726 | DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 727 | maybeRecordLocation(FnStartDL, MF); |
| 728 | } |
| 729 | } |
| 730 | |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame^] | 731 | TypeIndex CodeViewDebug::lowerType(const DIType *Ty) { |
| 732 | // Generic dispatch for lowering an unknown type. |
| 733 | switch (Ty->getTag()) { |
| 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)); |
| 745 | default: |
| 746 | // Use the null type index. |
| 747 | return TypeIndex(); |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | TypeIndex CodeViewDebug::lowerTypeBasic(const DIBasicType *Ty) { |
| 752 | TypeIndex Index; |
| 753 | dwarf::TypeKind Kind; |
| 754 | uint32_t ByteSize; |
| 755 | |
| 756 | Kind = static_cast<dwarf::TypeKind>(Ty->getEncoding()); |
| 757 | ByteSize = Ty->getSizeInBits() >> 3; |
| 758 | |
| 759 | SimpleTypeKind STK = SimpleTypeKind::None; |
| 760 | switch (Kind) { |
| 761 | case dwarf::DW_ATE_address: |
| 762 | // FIXME: Translate |
| 763 | break; |
| 764 | case dwarf::DW_ATE_boolean: |
| 765 | switch (ByteSize) { |
| 766 | case 1: STK = SimpleTypeKind::Boolean8; break; |
| 767 | case 2: STK = SimpleTypeKind::Boolean16; break; |
| 768 | case 4: STK = SimpleTypeKind::Boolean32; break; |
| 769 | case 8: STK = SimpleTypeKind::Boolean64; break; |
| 770 | } |
| 771 | break; |
| 772 | case dwarf::DW_ATE_complex_float: |
| 773 | switch (ByteSize) { |
| 774 | case 4: STK = SimpleTypeKind::Complex32; break; |
| 775 | case 8: STK = SimpleTypeKind::Complex64; break; |
| 776 | case 10: STK = SimpleTypeKind::Complex80; break; |
| 777 | case 16: STK = SimpleTypeKind::Complex128; break; |
| 778 | } |
| 779 | break; |
| 780 | case dwarf::DW_ATE_float: |
| 781 | switch (ByteSize) { |
| 782 | case 4: STK = SimpleTypeKind::Float32; break; |
| 783 | case 6: STK = SimpleTypeKind::Float48; break; |
| 784 | case 8: STK = SimpleTypeKind::Float64; break; |
| 785 | case 10: STK = SimpleTypeKind::Float80; break; |
| 786 | case 16: STK = SimpleTypeKind::Float128; break; |
| 787 | } |
| 788 | break; |
| 789 | case dwarf::DW_ATE_signed: |
| 790 | switch (ByteSize) { |
| 791 | case 1: STK = SimpleTypeKind::SByte; break; |
| 792 | case 2: STK = SimpleTypeKind::Int16Short; break; |
| 793 | case 4: STK = SimpleTypeKind::Int32; break; |
| 794 | case 8: STK = SimpleTypeKind::Int64; break; |
| 795 | } |
| 796 | break; |
| 797 | case dwarf::DW_ATE_unsigned: |
| 798 | switch (ByteSize) { |
| 799 | case 1: STK = SimpleTypeKind::Byte; break; |
| 800 | case 2: STK = SimpleTypeKind::UInt16Short; break; |
| 801 | case 4: STK = SimpleTypeKind::UInt32; break; |
| 802 | case 8: STK = SimpleTypeKind::UInt64; break; |
| 803 | } |
| 804 | break; |
| 805 | case dwarf::DW_ATE_UTF: |
| 806 | switch (ByteSize) { |
| 807 | case 2: STK = SimpleTypeKind::Character16; break; |
| 808 | case 4: STK = SimpleTypeKind::Character32; break; |
| 809 | } |
| 810 | break; |
| 811 | case dwarf::DW_ATE_signed_char: |
| 812 | if (ByteSize == 1) |
| 813 | STK = SimpleTypeKind::SignedCharacter; |
| 814 | break; |
| 815 | case dwarf::DW_ATE_unsigned_char: |
| 816 | if (ByteSize == 1) |
| 817 | STK = SimpleTypeKind::UnsignedCharacter; |
| 818 | break; |
| 819 | default: |
| 820 | break; |
| 821 | } |
| 822 | |
| 823 | // Apply some fixups based on the source-level type name. |
| 824 | if (STK == SimpleTypeKind::Int32 && Ty->getName() == "long int") |
| 825 | STK = SimpleTypeKind::Int32Long; |
| 826 | if (STK == SimpleTypeKind::UInt32 && Ty->getName() == "long unsigned int") |
| 827 | STK = SimpleTypeKind::UInt32Long; |
| 828 | if ((STK == SimpleTypeKind::Int16Short || |
| 829 | STK == SimpleTypeKind::UInt16Short) && |
| 830 | Ty->getName() == "wchar_t") |
| 831 | STK = SimpleTypeKind::WideCharacter; |
| 832 | if ((STK == SimpleTypeKind::SignedCharacter || |
| 833 | STK == SimpleTypeKind::UnsignedCharacter) && |
| 834 | Ty->getName() == "char") |
| 835 | STK = SimpleTypeKind::NarrowCharacter; |
| 836 | |
| 837 | return TypeIndex(STK); |
| 838 | } |
| 839 | |
| 840 | TypeIndex CodeViewDebug::lowerTypePointer(const DIDerivedType *Ty) { |
| 841 | TypeIndex PointeeTI = getTypeIndex(Ty->getBaseType()); |
| 842 | |
| 843 | // Pointers to simple types can use SimpleTypeMode, rather than having a |
| 844 | // dedicated pointer type record. |
| 845 | if (PointeeTI.isSimple() && |
| 846 | PointeeTI.getSimpleMode() == SimpleTypeMode::Direct && |
| 847 | Ty->getTag() == dwarf::DW_TAG_pointer_type) { |
| 848 | SimpleTypeMode Mode = Ty->getSizeInBits() == 64 |
| 849 | ? SimpleTypeMode::NearPointer64 |
| 850 | : SimpleTypeMode::NearPointer32; |
| 851 | return TypeIndex(PointeeTI.getSimpleKind(), Mode); |
| 852 | } |
| 853 | |
| 854 | PointerKind PK = |
| 855 | Ty->getSizeInBits() == 64 ? PointerKind::Near64 : PointerKind::Near32; |
| 856 | PointerMode PM = PointerMode::Pointer; |
| 857 | switch (Ty->getTag()) { |
| 858 | default: llvm_unreachable("not a pointer tag type"); |
| 859 | case dwarf::DW_TAG_pointer_type: |
| 860 | PM = PointerMode::Pointer; |
| 861 | break; |
| 862 | case dwarf::DW_TAG_reference_type: |
| 863 | PM = PointerMode::LValueReference; |
| 864 | break; |
| 865 | case dwarf::DW_TAG_rvalue_reference_type: |
| 866 | PM = PointerMode::RValueReference; |
| 867 | break; |
| 868 | } |
| 869 | // FIXME: MSVC folds qualifiers into PointerOptions in the context of a method |
| 870 | // 'this' pointer, but not normal contexts. Figure out what we're supposed to |
| 871 | // do. |
| 872 | PointerOptions PO = PointerOptions::None; |
| 873 | PointerRecord PR(PointeeTI, PK, PM, PO, Ty->getSizeInBits() / 8); |
| 874 | return TypeTable.writePointer(PR); |
| 875 | } |
| 876 | |
| 877 | TypeIndex CodeViewDebug::lowerTypeMemberPointer(const DIDerivedType *Ty) { |
| 878 | assert(Ty->getTag() == dwarf::DW_TAG_ptr_to_member_type); |
| 879 | TypeIndex ClassTI = getTypeIndex(Ty->getClassType()); |
| 880 | TypeIndex PointeeTI = getTypeIndex(Ty->getBaseType()); |
| 881 | PointerKind PK = Asm->MAI->getPointerSize() == 8 ? PointerKind::Near64 |
| 882 | : PointerKind::Near32; |
| 883 | PointerMode PM = isa<DISubroutineType>(Ty->getBaseType()) |
| 884 | ? PointerMode::PointerToMemberFunction |
| 885 | : PointerMode::PointerToDataMember; |
| 886 | PointerOptions PO = PointerOptions::None; // FIXME |
| 887 | // FIXME: Thread this ABI info through metadata. |
| 888 | PointerToMemberRepresentation PMR = PointerToMemberRepresentation::Unknown; |
| 889 | MemberPointerInfo MPI(ClassTI, PMR); |
| 890 | PointerRecord PR(PointeeTI, PK, PM, PO, Ty->getSizeInBits() / 8, MPI); |
| 891 | return TypeTable.writePointer(PR); |
| 892 | } |
| 893 | |
| 894 | TypeIndex CodeViewDebug::lowerTypeModifier(const DIDerivedType *Ty) { |
| 895 | ModifierOptions Mods = ModifierOptions::None; |
| 896 | bool IsModifier = true; |
| 897 | const DIType *BaseTy = Ty; |
| 898 | while (IsModifier) { |
| 899 | assert(BaseTy); |
| 900 | // FIXME: Need to add DWARF tag for __unaligned. |
| 901 | switch (BaseTy->getTag()) { |
| 902 | case dwarf::DW_TAG_const_type: |
| 903 | Mods |= ModifierOptions::Const; |
| 904 | break; |
| 905 | case dwarf::DW_TAG_volatile_type: |
| 906 | Mods |= ModifierOptions::Volatile; |
| 907 | break; |
| 908 | default: |
| 909 | IsModifier = false; |
| 910 | break; |
| 911 | } |
| 912 | if (IsModifier) |
| 913 | BaseTy = cast<DIDerivedType>(BaseTy)->getBaseType().resolve(); |
| 914 | } |
| 915 | TypeIndex ModifiedTI = getTypeIndex(BaseTy); |
| 916 | ModifierRecord MR(ModifiedTI, Mods); |
| 917 | return TypeTable.writeModifier(MR); |
| 918 | } |
| 919 | |
| 920 | TypeIndex CodeViewDebug::getTypeIndex(DITypeRef TypeRef) { |
| 921 | const DIType *Ty = TypeRef.resolve(); |
| 922 | |
| 923 | // The null DIType is the void type. Don't try to hash it. |
| 924 | if (!Ty) |
| 925 | return TypeIndex::Void(); |
| 926 | |
| 927 | // Check if we've already translated this type. |
| 928 | auto I = TypeIndices.find(Ty); |
| 929 | if (I != TypeIndices.end()) |
| 930 | return I->second; |
| 931 | |
| 932 | TypeIndex TI = lowerType(Ty); |
| 933 | |
| 934 | auto InsertResult = TypeIndices.insert({Ty, TI}); |
| 935 | assert(InsertResult.second && "DIType lowered twice"); |
| 936 | return TI; |
| 937 | } |
| 938 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 939 | void CodeViewDebug::emitLocalVariable(const LocalVariable &Var) { |
| 940 | // LocalSym record, see SymbolRecord.h for more info. |
| 941 | MCSymbol *LocalBegin = MMI->getContext().createTempSymbol(), |
| 942 | *LocalEnd = MMI->getContext().createTempSymbol(); |
| 943 | OS.AddComment("Record length"); |
| 944 | OS.emitAbsoluteSymbolDiff(LocalEnd, LocalBegin, 2); |
| 945 | OS.EmitLabel(LocalBegin); |
| 946 | |
| 947 | OS.AddComment("Record kind: S_LOCAL"); |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 948 | OS.EmitIntValue(unsigned(SymbolKind::S_LOCAL), 2); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 949 | |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 950 | LocalSymFlags Flags = LocalSymFlags::None; |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 951 | if (Var.DIVar->isParameter()) |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 952 | Flags |= LocalSymFlags::IsParameter; |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 953 | if (Var.DefRanges.empty()) |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 954 | Flags |= LocalSymFlags::IsOptimizedOut; |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 955 | |
| 956 | OS.AddComment("TypeIndex"); |
Reid Kleckner | 5acacbb | 2016-06-01 17:05:51 +0000 | [diff] [blame^] | 957 | TypeIndex TI = getTypeIndex(Var.DIVar->getType()); |
| 958 | OS.EmitIntValue(TI.getIndex(), 4); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 959 | OS.AddComment("Flags"); |
Zachary Turner | 63a2846 | 2016-05-17 23:50:21 +0000 | [diff] [blame] | 960 | OS.EmitIntValue(static_cast<uint16_t>(Flags), 2); |
David Majnemer | 1256125 | 2016-03-13 10:53:30 +0000 | [diff] [blame] | 961 | // Truncate the name so we won't overflow the record length field. |
David Majnemer | b9456a5 | 2016-03-14 05:15:09 +0000 | [diff] [blame] | 962 | emitNullTerminatedSymbolName(OS, Var.DIVar->getName()); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 963 | OS.EmitLabel(LocalEnd); |
| 964 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 965 | // Calculate the on disk prefix of the appropriate def range record. The |
| 966 | // records and on disk formats are described in SymbolRecords.h. BytePrefix |
| 967 | // should be big enough to hold all forms without memory allocation. |
| 968 | SmallString<20> BytePrefix; |
| 969 | for (const LocalVarDefRange &DefRange : Var.DefRanges) { |
| 970 | BytePrefix.clear(); |
| 971 | // FIXME: Handle bitpieces. |
| 972 | if (DefRange.StructOffset != 0) |
| 973 | continue; |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 974 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 975 | if (DefRange.InMemory) { |
Zachary Turner | a78ecd1 | 2016-05-23 18:49:06 +0000 | [diff] [blame] | 976 | DefRangeRegisterRelSym Sym(DefRange.CVRegister, 0, DefRange.DataOffset, 0, |
| 977 | 0, 0, ArrayRef<LocalVariableAddrGap>()); |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 978 | ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER_REL); |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 979 | BytePrefix += |
| 980 | StringRef(reinterpret_cast<const char *>(&SymKind), sizeof(SymKind)); |
Zachary Turner | a78ecd1 | 2016-05-23 18:49:06 +0000 | [diff] [blame] | 981 | BytePrefix += |
| 982 | StringRef(reinterpret_cast<const char *>(&Sym.Header), |
| 983 | sizeof(Sym.Header) - sizeof(LocalVariableAddrRange)); |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 984 | } else { |
| 985 | assert(DefRange.DataOffset == 0 && "unexpected offset into register"); |
Zachary Turner | a78ecd1 | 2016-05-23 18:49:06 +0000 | [diff] [blame] | 986 | // Unclear what matters here. |
| 987 | DefRangeRegisterSym Sym(DefRange.CVRegister, 0, 0, 0, 0, |
| 988 | ArrayRef<LocalVariableAddrGap>()); |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 989 | ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER); |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 990 | BytePrefix += |
| 991 | StringRef(reinterpret_cast<const char *>(&SymKind), sizeof(SymKind)); |
Zachary Turner | a78ecd1 | 2016-05-23 18:49:06 +0000 | [diff] [blame] | 992 | BytePrefix += |
| 993 | StringRef(reinterpret_cast<const char *>(&Sym.Header), |
| 994 | sizeof(Sym.Header) - sizeof(LocalVariableAddrRange)); |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 995 | } |
| 996 | OS.EmitCVDefRangeDirective(DefRange.Ranges, BytePrefix); |
| 997 | } |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 998 | } |
| 999 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 1000 | void CodeViewDebug::endFunction(const MachineFunction *MF) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 1001 | if (!Asm || !CurFn) // We haven't created any debug info for this function. |
| 1002 | return; |
| 1003 | |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 1004 | const Function *GV = MF->getFunction(); |
Yaron Keren | 6d3194f | 2014-06-20 10:26:56 +0000 | [diff] [blame] | 1005 | assert(FnDebugInfo.count(GV)); |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 1006 | assert(CurFn == &FnDebugInfo[GV]); |
| 1007 | |
Pete Cooper | adebb93 | 2016-03-11 02:14:16 +0000 | [diff] [blame] | 1008 | collectVariableInfo(GV->getSubprogram()); |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 1009 | |
| 1010 | DebugHandlerBase::endFunction(MF); |
| 1011 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 1012 | // Don't emit anything if we don't have any line tables. |
| 1013 | if (!CurFn->HaveLineInfo) { |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 1014 | FnDebugInfo.erase(GV); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1015 | CurFn = nullptr; |
| 1016 | return; |
Timur Iskhodzhanov | 8499a12 | 2014-03-26 09:50:36 +0000 | [diff] [blame] | 1017 | } |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1018 | |
| 1019 | CurFn->End = Asm->getFunctionEnd(); |
| 1020 | |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1021 | CurFn = nullptr; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 1024 | void CodeViewDebug::beginInstruction(const MachineInstr *MI) { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1025 | DebugHandlerBase::beginInstruction(MI); |
| 1026 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 1027 | // Ignore DBG_VALUE locations and function prologue. |
| 1028 | if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup)) |
| 1029 | return; |
| 1030 | DebugLoc DL = MI->getDebugLoc(); |
Duncan P. N. Exon Smith | 9dffcd0 | 2015-03-30 19:14:47 +0000 | [diff] [blame] | 1031 | if (DL == PrevInstLoc || !DL) |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 1032 | return; |
| 1033 | maybeRecordLocation(DL, Asm->MF); |
| 1034 | } |