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 | c62e379 | 2016-01-28 23:31:52 +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" |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCExpr.h" |
| 19 | #include "llvm/MC/MCSymbol.h" |
| 20 | #include "llvm/Support/COFF.h" |
| 21 | |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 22 | using namespace llvm::codeview; |
| 23 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 24 | namespace llvm { |
| 25 | |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 26 | StringRef CodeViewDebug::getFullFilepath(const DIFile *File) { |
| 27 | std::string &Filepath = FileToFilepathMap[File]; |
Reid Kleckner | 1f11b4e | 2015-12-02 22:34:30 +0000 | [diff] [blame] | 28 | if (!Filepath.empty()) |
| 29 | return Filepath; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 30 | |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 31 | StringRef Dir = File->getDirectory(), Filename = File->getFilename(); |
| 32 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 33 | // Clang emits directory and relative filename info into the IR, but CodeView |
| 34 | // operates on full paths. We could change Clang to emit full paths too, but |
| 35 | // that would increase the IR size and probably not needed for other users. |
| 36 | // For now, just concatenate and canonicalize the path here. |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 37 | if (Filename.find(':') == 1) |
| 38 | Filepath = Filename; |
| 39 | else |
Yaron Keren | 75e0c4b | 2015-03-27 17:51:30 +0000 | [diff] [blame] | 40 | Filepath = (Dir + "\\" + Filename).str(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 41 | |
| 42 | // Canonicalize the path. We have to do it textually because we may no longer |
| 43 | // have access the file in the filesystem. |
| 44 | // First, replace all slashes with backslashes. |
| 45 | std::replace(Filepath.begin(), Filepath.end(), '/', '\\'); |
| 46 | |
| 47 | // Remove all "\.\" with "\". |
| 48 | size_t Cursor = 0; |
| 49 | while ((Cursor = Filepath.find("\\.\\", Cursor)) != std::string::npos) |
| 50 | Filepath.erase(Cursor, 2); |
| 51 | |
| 52 | // Replace all "\XXX\..\" with "\". Don't try too hard though as the original |
| 53 | // path should be well-formatted, e.g. start with a drive letter, etc. |
| 54 | Cursor = 0; |
| 55 | while ((Cursor = Filepath.find("\\..\\", Cursor)) != std::string::npos) { |
| 56 | // Something's wrong if the path starts with "\..\", abort. |
| 57 | if (Cursor == 0) |
| 58 | break; |
| 59 | |
| 60 | size_t PrevSlash = Filepath.rfind('\\', Cursor - 1); |
| 61 | if (PrevSlash == std::string::npos) |
| 62 | // Something's wrong, abort. |
| 63 | break; |
| 64 | |
| 65 | Filepath.erase(PrevSlash, Cursor + 3 - PrevSlash); |
| 66 | // The next ".." might be following the one we've just erased. |
| 67 | Cursor = PrevSlash; |
| 68 | } |
| 69 | |
| 70 | // Remove all duplicate backslashes. |
| 71 | Cursor = 0; |
| 72 | while ((Cursor = Filepath.find("\\\\", Cursor)) != std::string::npos) |
| 73 | Filepath.erase(Cursor, 1); |
| 74 | |
Reid Kleckner | 1f11b4e | 2015-12-02 22:34:30 +0000 | [diff] [blame] | 75 | return Filepath; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Reid Kleckner | c62e379 | 2016-01-28 23:31:52 +0000 | [diff] [blame^] | 78 | unsigned CodeViewDebug::maybeRecordFile(const DIFile *F) { |
| 79 | unsigned NextId = FileIdMap.size() + 1; |
| 80 | auto Insertion = FileIdMap.insert(std::make_pair(F, NextId)); |
| 81 | if (Insertion.second) { |
| 82 | // We have to compute the full filepath and emit a .cv_file directive. |
| 83 | StringRef FullPath = getFullFilepath(F); |
| 84 | NextId = Asm->OutStreamer->EmitCVFileDirective(NextId, FullPath); |
| 85 | assert(NextId == FileIdMap.size() && ".cv_file directive failed"); |
| 86 | } |
| 87 | return Insertion.first->second; |
| 88 | } |
| 89 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 90 | void CodeViewDebug::maybeRecordLocation(DebugLoc DL, |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 91 | const MachineFunction *MF) { |
| 92 | // Skip this instruction if it has the same location as the previous one. |
| 93 | if (DL == CurFn->LastLoc) |
| 94 | return; |
| 95 | |
| 96 | const DIScope *Scope = DL.get()->getScope(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 97 | if (!Scope) |
| 98 | return; |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 99 | |
David Majnemer | c3340db | 2016-01-13 01:05:23 +0000 | [diff] [blame] | 100 | // Skip this line if it is longer than the maximum we can record. |
Reid Kleckner | c62e379 | 2016-01-28 23:31:52 +0000 | [diff] [blame^] | 101 | LineInfo LI(DL.getLine(), DL.getLine(), /*IsStatement=*/true); |
| 102 | if (LI.getStartLine() != DL.getLine() || LI.isAlwaysStepInto() || |
| 103 | LI.isNeverStepInto()) |
David Majnemer | c3340db | 2016-01-13 01:05:23 +0000 | [diff] [blame] | 104 | return; |
| 105 | |
Reid Kleckner | c62e379 | 2016-01-28 23:31:52 +0000 | [diff] [blame^] | 106 | ColumnInfo CI(DL.getCol(), /*EndColumn=*/0); |
| 107 | if (CI.getStartColumn() != DL.getCol()) |
| 108 | return; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 109 | |
Reid Kleckner | c62e379 | 2016-01-28 23:31:52 +0000 | [diff] [blame^] | 110 | if (!CurFn->HaveLineInfo) |
| 111 | CurFn->HaveLineInfo = true; |
| 112 | unsigned FileId = 0; |
| 113 | if (CurFn->LastLoc.get() && CurFn->LastLoc->getFile() == DL->getFile()) |
| 114 | FileId = CurFn->LastFileId; |
| 115 | else |
| 116 | FileId = CurFn->LastFileId = maybeRecordFile(DL->getFile()); |
| 117 | CurFn->LastLoc = DL; |
| 118 | Asm->OutStreamer->EmitCVLocDirective(CurFn->FuncId, FileId, DL.getLine(), |
| 119 | DL.getCol(), /*PrologueEnd=*/false, |
| 120 | /*IsStmt=*/false, DL->getFilename()); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 123 | CodeViewDebug::CodeViewDebug(AsmPrinter *AP) |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 124 | : Asm(nullptr), CurFn(nullptr) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 125 | MachineModuleInfo *MMI = AP->MMI; |
| 126 | |
| 127 | // If module doesn't have named metadata anchors or COFF debug section |
| 128 | // is not available, skip any debug info related stuff. |
| 129 | if (!MMI->getModule()->getNamedMetadata("llvm.dbg.cu") || |
| 130 | !AP->getObjFileLowering().getCOFFDebugSymbolsSection()) |
| 131 | return; |
| 132 | |
| 133 | // Tell MMI that we have debug info. |
| 134 | MMI->setDebugInfoAvailability(true); |
| 135 | Asm = AP; |
| 136 | } |
| 137 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 138 | void CodeViewDebug::endModule() { |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 139 | if (FnDebugInfo.empty()) |
| 140 | return; |
| 141 | |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 142 | // FIXME: For functions that are comdat, we should emit separate .debug$S |
| 143 | // sections that are comdat associative with the main function instead of |
| 144 | // having one big .debug$S section. |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 145 | assert(Asm != nullptr); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 146 | Asm->OutStreamer->SwitchSection( |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 147 | Asm->getObjFileLowering().getCOFFDebugSymbolsSection()); |
| 148 | Asm->EmitInt32(COFF::DEBUG_SECTION_MAGIC); |
| 149 | |
| 150 | // The COFF .debug$S section consists of several subsections, each starting |
| 151 | // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length |
| 152 | // of the payload followed by the payload itself. The subsections are 4-byte |
| 153 | // aligned. |
| 154 | |
Reid Kleckner | c62e379 | 2016-01-28 23:31:52 +0000 | [diff] [blame^] | 155 | // Emit per-function debug information. |
| 156 | for (auto &P : FnDebugInfo) |
| 157 | emitDebugInfoForFunction(P.first, P.second); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 158 | |
| 159 | // This subsection holds a file index to offset in string table table. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 160 | Asm->OutStreamer->AddComment("File index to string table offset subsection"); |
Reid Kleckner | c62e379 | 2016-01-28 23:31:52 +0000 | [diff] [blame^] | 161 | Asm->OutStreamer->EmitCVFileChecksumsDirective(); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 162 | |
| 163 | // This subsection holds the string table. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 164 | Asm->OutStreamer->AddComment("String table"); |
Reid Kleckner | c62e379 | 2016-01-28 23:31:52 +0000 | [diff] [blame^] | 165 | Asm->OutStreamer->EmitCVStringTableDirective(); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 166 | |
| 167 | clear(); |
| 168 | } |
| 169 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 170 | static void EmitLabelDiff(MCStreamer &Streamer, |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 171 | const MCSymbol *From, const MCSymbol *To, |
| 172 | unsigned int Size = 4) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 173 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
| 174 | MCContext &Context = Streamer.getContext(); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 175 | const MCExpr *FromRef = MCSymbolRefExpr::create(From, Variant, Context), |
| 176 | *ToRef = MCSymbolRefExpr::create(To, Variant, Context); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 177 | const MCExpr *AddrDelta = |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 178 | MCBinaryExpr::create(MCBinaryExpr::Sub, ToRef, FromRef, Context); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 179 | Streamer.EmitValue(AddrDelta, Size); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Reid Kleckner | c62e379 | 2016-01-28 23:31:52 +0000 | [diff] [blame^] | 182 | void CodeViewDebug::emitDebugInfoForFunction(const Function *GV, |
| 183 | FunctionInfo &FI) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 184 | // For each function there is a separate subsection |
| 185 | // which holds the PC to file:line table. |
| 186 | const MCSymbol *Fn = Asm->getSymbol(GV); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 187 | assert(Fn); |
Timur Iskhodzhanov | 8499a12 | 2014-03-26 09:50:36 +0000 | [diff] [blame] | 188 | |
Duncan P. N. Exon Smith | 23e56ec | 2015-03-20 19:50:00 +0000 | [diff] [blame] | 189 | StringRef FuncName; |
Duncan P. N. Exon Smith | 2fbe135 | 2015-04-20 22:10:08 +0000 | [diff] [blame] | 190 | if (auto *SP = getDISubprogram(GV)) |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 191 | FuncName = SP->getDisplayName(); |
Duncan P. N. Exon Smith | 23e56ec | 2015-03-20 19:50:00 +0000 | [diff] [blame] | 192 | |
Reid Kleckner | 3c0ff98 | 2016-01-14 00:12:54 +0000 | [diff] [blame] | 193 | // If our DISubprogram name is empty, use the mangled name. |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 194 | if (FuncName.empty()) |
| 195 | FuncName = GlobalValue::getRealLinkageName(GV->getName()); |
Reid Kleckner | 3c0ff98 | 2016-01-14 00:12:54 +0000 | [diff] [blame] | 196 | |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 197 | // Emit a symbol subsection, required by VS2012+ to find function boundaries. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 198 | MCSymbol *SymbolsBegin = Asm->MMI->getContext().createTempSymbol(), |
| 199 | *SymbolsEnd = Asm->MMI->getContext().createTempSymbol(); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 200 | Asm->OutStreamer->AddComment("Symbol subsection for " + Twine(FuncName)); |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 201 | Asm->EmitInt32(unsigned(ModuleSubstreamKind::Symbols)); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 202 | EmitLabelDiff(*Asm->OutStreamer, SymbolsBegin, SymbolsEnd); |
| 203 | Asm->OutStreamer->EmitLabel(SymbolsBegin); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 204 | { |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 205 | MCSymbol *ProcSegmentBegin = Asm->MMI->getContext().createTempSymbol(), |
| 206 | *ProcSegmentEnd = Asm->MMI->getContext().createTempSymbol(); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 207 | EmitLabelDiff(*Asm->OutStreamer, ProcSegmentBegin, ProcSegmentEnd, 2); |
| 208 | Asm->OutStreamer->EmitLabel(ProcSegmentBegin); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 209 | |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 210 | Asm->EmitInt16(unsigned(SymbolRecordKind::S_GPROC32_ID)); |
| 211 | |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 212 | // Some bytes of this segment don't seem to be required for basic debugging, |
| 213 | // so just fill them with zeroes. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 214 | Asm->OutStreamer->EmitFill(12, 0); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 215 | // This is the important bit that tells the debugger where the function |
| 216 | // code is located and what's its size: |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 217 | EmitLabelDiff(*Asm->OutStreamer, Fn, FI.End); |
| 218 | Asm->OutStreamer->EmitFill(12, 0); |
| 219 | Asm->OutStreamer->EmitCOFFSecRel32(Fn); |
| 220 | Asm->OutStreamer->EmitCOFFSectionIndex(Fn); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 221 | Asm->EmitInt8(0); |
Timur Iskhodzhanov | a11b32b | 2014-11-12 20:10:09 +0000 | [diff] [blame] | 222 | // Emit the function display name as a null-terminated string. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 223 | Asm->OutStreamer->EmitBytes(FuncName); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 224 | Asm->EmitInt8(0); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 225 | Asm->OutStreamer->EmitLabel(ProcSegmentEnd); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 226 | |
| 227 | // We're done with this function. |
| 228 | Asm->EmitInt16(0x0002); |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 229 | Asm->EmitInt16(unsigned(SymbolRecordKind::S_PROC_ID_END)); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 230 | } |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 231 | Asm->OutStreamer->EmitLabel(SymbolsEnd); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 232 | // Every subsection must be aligned to a 4-byte boundary. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 233 | Asm->OutStreamer->EmitFill((-FuncName.size()) % 4, 0); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 234 | |
Reid Kleckner | c62e379 | 2016-01-28 23:31:52 +0000 | [diff] [blame^] | 235 | // We have an assembler directive that takes care of the whole line table. |
| 236 | Asm->OutStreamer->EmitCVLinetableDirective(FI.FuncId, Fn, FI.End); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 239 | void CodeViewDebug::beginFunction(const MachineFunction *MF) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 240 | assert(!CurFn && "Can't process two functions at once!"); |
| 241 | |
| 242 | if (!Asm || !Asm->MMI->hasDebugInfo()) |
| 243 | return; |
| 244 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 245 | const Function *GV = MF->getFunction(); |
| 246 | assert(FnDebugInfo.count(GV) == false); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 247 | CurFn = &FnDebugInfo[GV]; |
Reid Kleckner | c62e379 | 2016-01-28 23:31:52 +0000 | [diff] [blame^] | 248 | CurFn->FuncId = NextFuncId++; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 249 | |
| 250 | // Find the end of the function prolog. |
| 251 | // FIXME: is there a simpler a way to do this? Can we just search |
| 252 | // for the first instruction of the function, not the last of the prolog? |
| 253 | DebugLoc PrologEndLoc; |
| 254 | bool EmptyPrologue = true; |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 255 | for (const auto &MBB : *MF) { |
Duncan P. N. Exon Smith | 9dffcd0 | 2015-03-30 19:14:47 +0000 | [diff] [blame] | 256 | if (PrologEndLoc) |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 257 | break; |
| 258 | for (const auto &MI : MBB) { |
| 259 | if (MI.isDebugValue()) |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 260 | continue; |
| 261 | |
| 262 | // First known non-DBG_VALUE and non-frame setup location marks |
| 263 | // the beginning of the function body. |
| 264 | // FIXME: do we need the first subcondition? |
Duncan P. N. Exon Smith | 9dffcd0 | 2015-03-30 19:14:47 +0000 | [diff] [blame] | 265 | if (!MI.getFlag(MachineInstr::FrameSetup) && MI.getDebugLoc()) { |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 266 | PrologEndLoc = MI.getDebugLoc(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 267 | break; |
| 268 | } |
| 269 | EmptyPrologue = false; |
| 270 | } |
| 271 | } |
| 272 | // 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] | 273 | if (PrologEndLoc && !EmptyPrologue) { |
| 274 | DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 275 | maybeRecordLocation(FnStartDL, MF); |
| 276 | } |
| 277 | } |
| 278 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 279 | void CodeViewDebug::endFunction(const MachineFunction *MF) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 280 | if (!Asm || !CurFn) // We haven't created any debug info for this function. |
| 281 | return; |
| 282 | |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 283 | const Function *GV = MF->getFunction(); |
Yaron Keren | 6d3194f | 2014-06-20 10:26:56 +0000 | [diff] [blame] | 284 | assert(FnDebugInfo.count(GV)); |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 285 | assert(CurFn == &FnDebugInfo[GV]); |
| 286 | |
Reid Kleckner | c62e379 | 2016-01-28 23:31:52 +0000 | [diff] [blame^] | 287 | // Don't emit anything if we don't have any line tables. |
| 288 | if (!CurFn->HaveLineInfo) { |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 289 | FnDebugInfo.erase(GV); |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 290 | } else { |
Rafael Espindola | 07c03d3 | 2015-03-05 02:05:42 +0000 | [diff] [blame] | 291 | CurFn->End = Asm->getFunctionEnd(); |
Timur Iskhodzhanov | 8499a12 | 2014-03-26 09:50:36 +0000 | [diff] [blame] | 292 | } |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 293 | CurFn = nullptr; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 296 | void CodeViewDebug::beginInstruction(const MachineInstr *MI) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 297 | // Ignore DBG_VALUE locations and function prologue. |
| 298 | if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup)) |
| 299 | return; |
| 300 | DebugLoc DL = MI->getDebugLoc(); |
Duncan P. N. Exon Smith | 9dffcd0 | 2015-03-30 19:14:47 +0000 | [diff] [blame] | 301 | if (DL == PrevInstLoc || !DL) |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 302 | return; |
| 303 | maybeRecordLocation(DL, Asm->MF); |
| 304 | } |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 305 | } |