Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 1 | //===-- llvm/lib/CodeGen/AsmPrinter/WinCodeViewLineTables.cpp --*- C++ -*--===// |
| 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 | // |
| 10 | // This file contains support for writing line tables info into COFF files. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "WinCodeViewLineTables.h" |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/CodeView/CodeView.h" |
| 16 | #include "llvm/DebugInfo/CodeView/SymbolRecord.h" |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCExpr.h" |
| 18 | #include "llvm/MC/MCSymbol.h" |
| 19 | #include "llvm/Support/COFF.h" |
| 20 | |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 21 | using namespace llvm::codeview; |
| 22 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 23 | namespace llvm { |
| 24 | |
| 25 | StringRef WinCodeViewLineTables::getFullFilepath(const MDNode *S) { |
| 26 | assert(S); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 27 | assert((isa<DICompileUnit>(S) || isa<DIFile>(S) || isa<DISubprogram>(S) || |
| 28 | isa<DILexicalBlockBase>(S)) && |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 29 | "Unexpected scope info"); |
| 30 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 31 | auto *Scope = cast<DIScope>(S); |
Duncan P. N. Exon Smith | b273d06 | 2015-04-16 01:37:00 +0000 | [diff] [blame] | 32 | StringRef Dir = Scope->getDirectory(), |
| 33 | Filename = Scope->getFilename(); |
Reid Kleckner | 1f11b4e | 2015-12-02 22:34:30 +0000 | [diff] [blame] | 34 | std::string &Filepath = |
| 35 | DirAndFilenameToFilepathMap[std::make_pair(Dir, Filename)]; |
| 36 | if (!Filepath.empty()) |
| 37 | return Filepath; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 38 | |
| 39 | // Clang emits directory and relative filename info into the IR, but CodeView |
| 40 | // operates on full paths. We could change Clang to emit full paths too, but |
| 41 | // that would increase the IR size and probably not needed for other users. |
| 42 | // For now, just concatenate and canonicalize the path here. |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 43 | if (Filename.find(':') == 1) |
| 44 | Filepath = Filename; |
| 45 | else |
Yaron Keren | 75e0c4b | 2015-03-27 17:51:30 +0000 | [diff] [blame] | 46 | Filepath = (Dir + "\\" + Filename).str(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 47 | |
| 48 | // Canonicalize the path. We have to do it textually because we may no longer |
| 49 | // have access the file in the filesystem. |
| 50 | // First, replace all slashes with backslashes. |
| 51 | std::replace(Filepath.begin(), Filepath.end(), '/', '\\'); |
| 52 | |
| 53 | // Remove all "\.\" with "\". |
| 54 | size_t Cursor = 0; |
| 55 | while ((Cursor = Filepath.find("\\.\\", Cursor)) != std::string::npos) |
| 56 | Filepath.erase(Cursor, 2); |
| 57 | |
| 58 | // Replace all "\XXX\..\" with "\". Don't try too hard though as the original |
| 59 | // path should be well-formatted, e.g. start with a drive letter, etc. |
| 60 | Cursor = 0; |
| 61 | while ((Cursor = Filepath.find("\\..\\", Cursor)) != std::string::npos) { |
| 62 | // Something's wrong if the path starts with "\..\", abort. |
| 63 | if (Cursor == 0) |
| 64 | break; |
| 65 | |
| 66 | size_t PrevSlash = Filepath.rfind('\\', Cursor - 1); |
| 67 | if (PrevSlash == std::string::npos) |
| 68 | // Something's wrong, abort. |
| 69 | break; |
| 70 | |
| 71 | Filepath.erase(PrevSlash, Cursor + 3 - PrevSlash); |
| 72 | // The next ".." might be following the one we've just erased. |
| 73 | Cursor = PrevSlash; |
| 74 | } |
| 75 | |
| 76 | // Remove all duplicate backslashes. |
| 77 | Cursor = 0; |
| 78 | while ((Cursor = Filepath.find("\\\\", Cursor)) != std::string::npos) |
| 79 | Filepath.erase(Cursor, 1); |
| 80 | |
Reid Kleckner | 1f11b4e | 2015-12-02 22:34:30 +0000 | [diff] [blame] | 81 | return Filepath; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void WinCodeViewLineTables::maybeRecordLocation(DebugLoc DL, |
| 85 | const MachineFunction *MF) { |
Duncan P. N. Exon Smith | 9dffcd0 | 2015-03-30 19:14:47 +0000 | [diff] [blame] | 86 | const MDNode *Scope = DL.getScope(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 87 | if (!Scope) |
| 88 | return; |
David Majnemer | c3340db | 2016-01-13 01:05:23 +0000 | [diff] [blame] | 89 | unsigned LineNumber = DL.getLine(); |
| 90 | // Skip this line if it is longer than the maximum we can record. |
| 91 | if (LineNumber > COFF::CVL_MaxLineNumber) |
| 92 | return; |
| 93 | |
| 94 | unsigned ColumnNumber = DL.getCol(); |
| 95 | // Truncate the column number if it is longer than the maximum we can record. |
| 96 | if (ColumnNumber > COFF::CVL_MaxColumnNumber) |
| 97 | ColumnNumber = 0; |
| 98 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 99 | StringRef Filename = getFullFilepath(Scope); |
| 100 | |
| 101 | // Skip this instruction if it has the same file:line as the previous one. |
| 102 | assert(CurFn); |
| 103 | if (!CurFn->Instrs.empty()) { |
| 104 | const InstrInfoTy &LastInstr = InstrInfo[CurFn->Instrs.back()]; |
David Majnemer | c3340db | 2016-01-13 01:05:23 +0000 | [diff] [blame] | 105 | if (LastInstr.Filename == Filename && LastInstr.LineNumber == LineNumber && |
| 106 | LastInstr.ColumnNumber == ColumnNumber) |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 107 | return; |
| 108 | } |
| 109 | FileNameRegistry.add(Filename); |
| 110 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 111 | MCSymbol *MCL = Asm->MMI->getContext().createTempSymbol(); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 112 | Asm->OutStreamer->EmitLabel(MCL); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 113 | CurFn->Instrs.push_back(MCL); |
David Majnemer | c3340db | 2016-01-13 01:05:23 +0000 | [diff] [blame] | 114 | InstrInfo[MCL] = InstrInfoTy(Filename, LineNumber, ColumnNumber); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | WinCodeViewLineTables::WinCodeViewLineTables(AsmPrinter *AP) |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 118 | : Asm(nullptr), CurFn(nullptr) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 119 | MachineModuleInfo *MMI = AP->MMI; |
| 120 | |
| 121 | // If module doesn't have named metadata anchors or COFF debug section |
| 122 | // is not available, skip any debug info related stuff. |
| 123 | if (!MMI->getModule()->getNamedMetadata("llvm.dbg.cu") || |
| 124 | !AP->getObjFileLowering().getCOFFDebugSymbolsSection()) |
| 125 | return; |
| 126 | |
| 127 | // Tell MMI that we have debug info. |
| 128 | MMI->setDebugInfoAvailability(true); |
| 129 | Asm = AP; |
| 130 | } |
| 131 | |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 132 | void WinCodeViewLineTables::endModule() { |
| 133 | if (FnDebugInfo.empty()) |
| 134 | return; |
| 135 | |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 136 | // FIXME: For functions that are comdat, we should emit separate .debug$S |
| 137 | // sections that are comdat associative with the main function instead of |
| 138 | // having one big .debug$S section. |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 139 | assert(Asm != nullptr); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 140 | Asm->OutStreamer->SwitchSection( |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 141 | Asm->getObjFileLowering().getCOFFDebugSymbolsSection()); |
| 142 | Asm->EmitInt32(COFF::DEBUG_SECTION_MAGIC); |
| 143 | |
| 144 | // The COFF .debug$S section consists of several subsections, each starting |
| 145 | // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length |
| 146 | // of the payload followed by the payload itself. The subsections are 4-byte |
| 147 | // aligned. |
| 148 | |
| 149 | // Emit per-function debug information. This code is extracted into a |
| 150 | // separate function for readability. |
| 151 | for (size_t I = 0, E = VisitedFunctions.size(); I != E; ++I) |
| 152 | emitDebugInfoForFunction(VisitedFunctions[I]); |
| 153 | |
| 154 | // This subsection holds a file index to offset in string table table. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 155 | Asm->OutStreamer->AddComment("File index to string table offset subsection"); |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 156 | Asm->EmitInt32(unsigned(ModuleSubstreamKind::FileChecksums)); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 157 | size_t NumFilenames = FileNameRegistry.Infos.size(); |
| 158 | Asm->EmitInt32(8 * NumFilenames); |
| 159 | for (size_t I = 0, E = FileNameRegistry.Filenames.size(); I != E; ++I) { |
| 160 | StringRef Filename = FileNameRegistry.Filenames[I]; |
| 161 | // For each unique filename, just write its offset in the string table. |
| 162 | Asm->EmitInt32(FileNameRegistry.Infos[Filename].StartOffset); |
| 163 | // The function name offset is not followed by any additional data. |
| 164 | Asm->EmitInt32(0); |
| 165 | } |
| 166 | |
| 167 | // This subsection holds the string table. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 168 | Asm->OutStreamer->AddComment("String table"); |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 169 | Asm->EmitInt32(unsigned(ModuleSubstreamKind::StringTable)); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 170 | Asm->EmitInt32(FileNameRegistry.LastOffset); |
| 171 | // The payload starts with a null character. |
| 172 | Asm->EmitInt8(0); |
| 173 | |
| 174 | for (size_t I = 0, E = FileNameRegistry.Filenames.size(); I != E; ++I) { |
| 175 | // Just emit unique filenames one by one, separated by a null character. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 176 | Asm->OutStreamer->EmitBytes(FileNameRegistry.Filenames[I]); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 177 | Asm->EmitInt8(0); |
| 178 | } |
| 179 | |
| 180 | // No more subsections. Fill with zeros to align the end of the section by 4. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 181 | Asm->OutStreamer->EmitFill((-FileNameRegistry.LastOffset) % 4, 0); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 182 | |
| 183 | clear(); |
| 184 | } |
| 185 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 186 | static void EmitLabelDiff(MCStreamer &Streamer, |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 187 | const MCSymbol *From, const MCSymbol *To, |
| 188 | unsigned int Size = 4) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 189 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
| 190 | MCContext &Context = Streamer.getContext(); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 191 | const MCExpr *FromRef = MCSymbolRefExpr::create(From, Variant, Context), |
| 192 | *ToRef = MCSymbolRefExpr::create(To, Variant, Context); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 193 | const MCExpr *AddrDelta = |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 194 | MCBinaryExpr::create(MCBinaryExpr::Sub, ToRef, FromRef, Context); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 195 | Streamer.EmitValue(AddrDelta, Size); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void WinCodeViewLineTables::emitDebugInfoForFunction(const Function *GV) { |
| 199 | // For each function there is a separate subsection |
| 200 | // which holds the PC to file:line table. |
| 201 | const MCSymbol *Fn = Asm->getSymbol(GV); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 202 | assert(Fn); |
Timur Iskhodzhanov | 8499a12 | 2014-03-26 09:50:36 +0000 | [diff] [blame] | 203 | |
| 204 | const FunctionInfo &FI = FnDebugInfo[GV]; |
| 205 | if (FI.Instrs.empty()) |
| 206 | return; |
| 207 | assert(FI.End && "Don't know where the function ends?"); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 208 | |
Duncan P. N. Exon Smith | 23e56ec | 2015-03-20 19:50:00 +0000 | [diff] [blame] | 209 | StringRef FuncName; |
Duncan P. N. Exon Smith | 2fbe135 | 2015-04-20 22:10:08 +0000 | [diff] [blame] | 210 | if (auto *SP = getDISubprogram(GV)) |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 211 | FuncName = SP->getDisplayName(); |
Duncan P. N. Exon Smith | 23e56ec | 2015-03-20 19:50:00 +0000 | [diff] [blame] | 212 | |
Reid Kleckner | 3c0ff98 | 2016-01-14 00:12:54 +0000 | [diff] [blame^] | 213 | // If our DISubprogram name is empty, use the mangled name. |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 214 | if (FuncName.empty()) |
| 215 | FuncName = GlobalValue::getRealLinkageName(GV->getName()); |
Reid Kleckner | 3c0ff98 | 2016-01-14 00:12:54 +0000 | [diff] [blame^] | 216 | |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 217 | // Emit a symbol subsection, required by VS2012+ to find function boundaries. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 218 | MCSymbol *SymbolsBegin = Asm->MMI->getContext().createTempSymbol(), |
| 219 | *SymbolsEnd = Asm->MMI->getContext().createTempSymbol(); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 220 | Asm->OutStreamer->AddComment("Symbol subsection for " + Twine(FuncName)); |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 221 | Asm->EmitInt32(unsigned(ModuleSubstreamKind::Symbols)); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 222 | EmitLabelDiff(*Asm->OutStreamer, SymbolsBegin, SymbolsEnd); |
| 223 | Asm->OutStreamer->EmitLabel(SymbolsBegin); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 224 | { |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 225 | MCSymbol *ProcSegmentBegin = Asm->MMI->getContext().createTempSymbol(), |
| 226 | *ProcSegmentEnd = Asm->MMI->getContext().createTempSymbol(); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 227 | EmitLabelDiff(*Asm->OutStreamer, ProcSegmentBegin, ProcSegmentEnd, 2); |
| 228 | Asm->OutStreamer->EmitLabel(ProcSegmentBegin); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 229 | |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 230 | Asm->EmitInt16(unsigned(SymbolRecordKind::S_GPROC32_ID)); |
| 231 | |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 232 | // Some bytes of this segment don't seem to be required for basic debugging, |
| 233 | // so just fill them with zeroes. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 234 | Asm->OutStreamer->EmitFill(12, 0); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 235 | // This is the important bit that tells the debugger where the function |
| 236 | // code is located and what's its size: |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 237 | EmitLabelDiff(*Asm->OutStreamer, Fn, FI.End); |
| 238 | Asm->OutStreamer->EmitFill(12, 0); |
| 239 | Asm->OutStreamer->EmitCOFFSecRel32(Fn); |
| 240 | Asm->OutStreamer->EmitCOFFSectionIndex(Fn); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 241 | Asm->EmitInt8(0); |
Timur Iskhodzhanov | a11b32b | 2014-11-12 20:10:09 +0000 | [diff] [blame] | 242 | // Emit the function display name as a null-terminated string. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 243 | Asm->OutStreamer->EmitBytes(FuncName); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 244 | Asm->EmitInt8(0); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 245 | Asm->OutStreamer->EmitLabel(ProcSegmentEnd); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 246 | |
| 247 | // We're done with this function. |
| 248 | Asm->EmitInt16(0x0002); |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 249 | Asm->EmitInt16(unsigned(SymbolRecordKind::S_PROC_ID_END)); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 250 | } |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 251 | Asm->OutStreamer->EmitLabel(SymbolsEnd); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 252 | // Every subsection must be aligned to a 4-byte boundary. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 253 | Asm->OutStreamer->EmitFill((-FuncName.size()) % 4, 0); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 254 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 255 | // PCs/Instructions are grouped into segments sharing the same filename. |
| 256 | // Pre-calculate the lengths (in instructions) of these segments and store |
| 257 | // them in a map for convenience. Each index in the map is the sequential |
| 258 | // number of the respective instruction that starts a new segment. |
| 259 | DenseMap<size_t, size_t> FilenameSegmentLengths; |
| 260 | size_t LastSegmentEnd = 0; |
| 261 | StringRef PrevFilename = InstrInfo[FI.Instrs[0]].Filename; |
| 262 | for (size_t J = 1, F = FI.Instrs.size(); J != F; ++J) { |
| 263 | if (PrevFilename == InstrInfo[FI.Instrs[J]].Filename) |
| 264 | continue; |
| 265 | FilenameSegmentLengths[LastSegmentEnd] = J - LastSegmentEnd; |
| 266 | LastSegmentEnd = J; |
| 267 | PrevFilename = InstrInfo[FI.Instrs[J]].Filename; |
| 268 | } |
| 269 | FilenameSegmentLengths[LastSegmentEnd] = FI.Instrs.size() - LastSegmentEnd; |
| 270 | |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 271 | // Emit a line table subsection, required to do PC-to-file:line lookup. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 272 | Asm->OutStreamer->AddComment("Line table subsection for " + Twine(FuncName)); |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 273 | Asm->EmitInt32(unsigned(ModuleSubstreamKind::Lines)); |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 274 | MCSymbol *LineTableBegin = Asm->MMI->getContext().createTempSymbol(), |
| 275 | *LineTableEnd = Asm->MMI->getContext().createTempSymbol(); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 276 | EmitLabelDiff(*Asm->OutStreamer, LineTableBegin, LineTableEnd); |
| 277 | Asm->OutStreamer->EmitLabel(LineTableBegin); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 278 | |
| 279 | // Identify the function this subsection is for. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 280 | Asm->OutStreamer->EmitCOFFSecRel32(Fn); |
| 281 | Asm->OutStreamer->EmitCOFFSectionIndex(Fn); |
David Majnemer | 3f49e66 | 2015-07-09 00:19:51 +0000 | [diff] [blame] | 282 | // Insert flags after a 16-bit section index. |
| 283 | Asm->EmitInt16(COFF::DEBUG_LINE_TABLES_HAVE_COLUMN_RECORDS); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 284 | |
| 285 | // Length of the function's code, in bytes. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 286 | EmitLabelDiff(*Asm->OutStreamer, Fn, FI.End); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 287 | |
| 288 | // PC-to-linenumber lookup table: |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 289 | MCSymbol *FileSegmentEnd = nullptr; |
David Majnemer | 3f49e66 | 2015-07-09 00:19:51 +0000 | [diff] [blame] | 290 | |
| 291 | // The start of the last segment: |
| 292 | size_t LastSegmentStart = 0; |
| 293 | |
| 294 | auto FinishPreviousChunk = [&] { |
| 295 | if (!FileSegmentEnd) |
| 296 | return; |
| 297 | for (size_t ColSegI = LastSegmentStart, |
| 298 | ColSegEnd = ColSegI + FilenameSegmentLengths[LastSegmentStart]; |
| 299 | ColSegI != ColSegEnd; ++ColSegI) { |
| 300 | unsigned ColumnNumber = InstrInfo[FI.Instrs[ColSegI]].ColumnNumber; |
David Majnemer | c3340db | 2016-01-13 01:05:23 +0000 | [diff] [blame] | 301 | assert(ColumnNumber <= COFF::CVL_MaxColumnNumber); |
David Majnemer | 3f49e66 | 2015-07-09 00:19:51 +0000 | [diff] [blame] | 302 | Asm->EmitInt16(ColumnNumber); // Start column |
David Majnemer | c81c8c6 | 2016-01-12 21:58:20 +0000 | [diff] [blame] | 303 | Asm->EmitInt16(0); // End column |
David Majnemer | 3f49e66 | 2015-07-09 00:19:51 +0000 | [diff] [blame] | 304 | } |
| 305 | Asm->OutStreamer->EmitLabel(FileSegmentEnd); |
| 306 | }; |
| 307 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 308 | for (size_t J = 0, F = FI.Instrs.size(); J != F; ++J) { |
| 309 | MCSymbol *Instr = FI.Instrs[J]; |
| 310 | assert(InstrInfo.count(Instr)); |
| 311 | |
| 312 | if (FilenameSegmentLengths.count(J)) { |
| 313 | // We came to a beginning of a new filename segment. |
David Majnemer | 3f49e66 | 2015-07-09 00:19:51 +0000 | [diff] [blame] | 314 | FinishPreviousChunk(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 315 | StringRef CurFilename = InstrInfo[FI.Instrs[J]].Filename; |
| 316 | assert(FileNameRegistry.Infos.count(CurFilename)); |
| 317 | size_t IndexInStringTable = |
| 318 | FileNameRegistry.Infos[CurFilename].FilenameID; |
| 319 | // Each segment starts with the offset of the filename |
| 320 | // in the string table. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 321 | Asm->OutStreamer->AddComment( |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 322 | "Segment for file '" + Twine(CurFilename) + "' begins"); |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 323 | MCSymbol *FileSegmentBegin = Asm->MMI->getContext().createTempSymbol(); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 324 | Asm->OutStreamer->EmitLabel(FileSegmentBegin); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 325 | Asm->EmitInt32(8 * IndexInStringTable); |
| 326 | |
| 327 | // Number of PC records in the lookup table. |
| 328 | size_t SegmentLength = FilenameSegmentLengths[J]; |
| 329 | Asm->EmitInt32(SegmentLength); |
| 330 | |
| 331 | // Full size of the segment for this filename, including the prev two |
| 332 | // records. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 333 | FileSegmentEnd = Asm->MMI->getContext().createTempSymbol(); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 334 | EmitLabelDiff(*Asm->OutStreamer, FileSegmentBegin, FileSegmentEnd); |
David Majnemer | 3f49e66 | 2015-07-09 00:19:51 +0000 | [diff] [blame] | 335 | LastSegmentStart = J; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | // The first PC with the given linenumber and the linenumber itself. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 339 | EmitLabelDiff(*Asm->OutStreamer, Fn, Instr); |
David Majnemer | c3340db | 2016-01-13 01:05:23 +0000 | [diff] [blame] | 340 | uint32_t LineNumber = InstrInfo[Instr].LineNumber; |
| 341 | assert(LineNumber <= COFF::CVL_MaxLineNumber); |
| 342 | uint32_t LineData = LineNumber | COFF::CVL_IsStatement; |
| 343 | Asm->EmitInt32(LineData); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 344 | } |
| 345 | |
David Majnemer | 3f49e66 | 2015-07-09 00:19:51 +0000 | [diff] [blame] | 346 | FinishPreviousChunk(); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 347 | Asm->OutStreamer->EmitLabel(LineTableEnd); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 350 | void WinCodeViewLineTables::beginFunction(const MachineFunction *MF) { |
| 351 | assert(!CurFn && "Can't process two functions at once!"); |
| 352 | |
| 353 | if (!Asm || !Asm->MMI->hasDebugInfo()) |
| 354 | return; |
| 355 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 356 | const Function *GV = MF->getFunction(); |
| 357 | assert(FnDebugInfo.count(GV) == false); |
| 358 | VisitedFunctions.push_back(GV); |
| 359 | CurFn = &FnDebugInfo[GV]; |
| 360 | |
| 361 | // Find the end of the function prolog. |
| 362 | // FIXME: is there a simpler a way to do this? Can we just search |
| 363 | // for the first instruction of the function, not the last of the prolog? |
| 364 | DebugLoc PrologEndLoc; |
| 365 | bool EmptyPrologue = true; |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 366 | for (const auto &MBB : *MF) { |
Duncan P. N. Exon Smith | 9dffcd0 | 2015-03-30 19:14:47 +0000 | [diff] [blame] | 367 | if (PrologEndLoc) |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 368 | break; |
| 369 | for (const auto &MI : MBB) { |
| 370 | if (MI.isDebugValue()) |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 371 | continue; |
| 372 | |
| 373 | // First known non-DBG_VALUE and non-frame setup location marks |
| 374 | // the beginning of the function body. |
| 375 | // FIXME: do we need the first subcondition? |
Duncan P. N. Exon Smith | 9dffcd0 | 2015-03-30 19:14:47 +0000 | [diff] [blame] | 376 | if (!MI.getFlag(MachineInstr::FrameSetup) && MI.getDebugLoc()) { |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 377 | PrologEndLoc = MI.getDebugLoc(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 378 | break; |
| 379 | } |
| 380 | EmptyPrologue = false; |
| 381 | } |
| 382 | } |
| 383 | // 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] | 384 | if (PrologEndLoc && !EmptyPrologue) { |
| 385 | DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 386 | maybeRecordLocation(FnStartDL, MF); |
| 387 | } |
| 388 | } |
| 389 | |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 390 | void WinCodeViewLineTables::endFunction(const MachineFunction *MF) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 391 | if (!Asm || !CurFn) // We haven't created any debug info for this function. |
| 392 | return; |
| 393 | |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 394 | const Function *GV = MF->getFunction(); |
Yaron Keren | 6d3194f | 2014-06-20 10:26:56 +0000 | [diff] [blame] | 395 | assert(FnDebugInfo.count(GV)); |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 396 | assert(CurFn == &FnDebugInfo[GV]); |
| 397 | |
| 398 | if (CurFn->Instrs.empty()) { |
| 399 | FnDebugInfo.erase(GV); |
| 400 | VisitedFunctions.pop_back(); |
| 401 | } else { |
Rafael Espindola | 07c03d3 | 2015-03-05 02:05:42 +0000 | [diff] [blame] | 402 | CurFn->End = Asm->getFunctionEnd(); |
Timur Iskhodzhanov | 8499a12 | 2014-03-26 09:50:36 +0000 | [diff] [blame] | 403 | } |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 404 | CurFn = nullptr; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | void WinCodeViewLineTables::beginInstruction(const MachineInstr *MI) { |
| 408 | // Ignore DBG_VALUE locations and function prologue. |
| 409 | if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup)) |
| 410 | return; |
| 411 | DebugLoc DL = MI->getDebugLoc(); |
Duncan P. N. Exon Smith | 9dffcd0 | 2015-03-30 19:14:47 +0000 | [diff] [blame] | 412 | if (DL == PrevInstLoc || !DL) |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 413 | return; |
| 414 | maybeRecordLocation(DL, Asm->MF); |
| 415 | } |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 416 | } |