blob: f1716361a1dc019e0afd328b1c578ba65dc807a6 [file] [log] [blame]
Reid Kleckner70f5bc92016-01-14 19:25:04 +00001//===-- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp --*- C++ -*--===//
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +00002//
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 Kleckner70f5bc92016-01-14 19:25:04 +000010// This file contains support for writing Microsoft CodeView debug info.
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000011//
12//===----------------------------------------------------------------------===//
13
Reid Kleckner70f5bc92016-01-14 19:25:04 +000014#include "CodeViewDebug.h"
Reid Kleckner6b3faef2016-01-13 23:44:57 +000015#include "llvm/DebugInfo/CodeView/CodeView.h"
16#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000017#include "llvm/MC/MCExpr.h"
18#include "llvm/MC/MCSymbol.h"
19#include "llvm/Support/COFF.h"
20
Reid Kleckner6b3faef2016-01-13 23:44:57 +000021using namespace llvm::codeview;
22
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000023namespace llvm {
24
Reid Kleckner9533af42016-01-16 00:09:09 +000025StringRef CodeViewDebug::getFullFilepath(const DIFile *File) {
26 std::string &Filepath = FileToFilepathMap[File];
Reid Kleckner1f11b4e2015-12-02 22:34:30 +000027 if (!Filepath.empty())
28 return Filepath;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000029
Reid Kleckner9533af42016-01-16 00:09:09 +000030 StringRef Dir = File->getDirectory(), Filename = File->getFilename();
31
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000032 // Clang emits directory and relative filename info into the IR, but CodeView
33 // operates on full paths. We could change Clang to emit full paths too, but
34 // that would increase the IR size and probably not needed for other users.
35 // For now, just concatenate and canonicalize the path here.
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000036 if (Filename.find(':') == 1)
37 Filepath = Filename;
38 else
Yaron Keren75e0c4b2015-03-27 17:51:30 +000039 Filepath = (Dir + "\\" + Filename).str();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000040
41 // Canonicalize the path. We have to do it textually because we may no longer
42 // have access the file in the filesystem.
43 // First, replace all slashes with backslashes.
44 std::replace(Filepath.begin(), Filepath.end(), '/', '\\');
45
46 // Remove all "\.\" with "\".
47 size_t Cursor = 0;
48 while ((Cursor = Filepath.find("\\.\\", Cursor)) != std::string::npos)
49 Filepath.erase(Cursor, 2);
50
51 // Replace all "\XXX\..\" with "\". Don't try too hard though as the original
52 // path should be well-formatted, e.g. start with a drive letter, etc.
53 Cursor = 0;
54 while ((Cursor = Filepath.find("\\..\\", Cursor)) != std::string::npos) {
55 // Something's wrong if the path starts with "\..\", abort.
56 if (Cursor == 0)
57 break;
58
59 size_t PrevSlash = Filepath.rfind('\\', Cursor - 1);
60 if (PrevSlash == std::string::npos)
61 // Something's wrong, abort.
62 break;
63
64 Filepath.erase(PrevSlash, Cursor + 3 - PrevSlash);
65 // The next ".." might be following the one we've just erased.
66 Cursor = PrevSlash;
67 }
68
69 // Remove all duplicate backslashes.
70 Cursor = 0;
71 while ((Cursor = Filepath.find("\\\\", Cursor)) != std::string::npos)
72 Filepath.erase(Cursor, 1);
73
Reid Kleckner1f11b4e2015-12-02 22:34:30 +000074 return Filepath;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000075}
76
Reid Kleckner70f5bc92016-01-14 19:25:04 +000077void CodeViewDebug::maybeRecordLocation(DebugLoc DL,
Reid Kleckner9533af42016-01-16 00:09:09 +000078 const MachineFunction *MF) {
79 // Skip this instruction if it has the same location as the previous one.
80 if (DL == CurFn->LastLoc)
81 return;
82
83 const DIScope *Scope = DL.get()->getScope();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000084 if (!Scope)
85 return;
Reid Kleckner9533af42016-01-16 00:09:09 +000086
David Majnemerc3340db2016-01-13 01:05:23 +000087 // Skip this line if it is longer than the maximum we can record.
Reid Kleckner00d96392016-01-29 00:13:28 +000088 if (DL.getLine() > COFF::CVL_MaxLineNumber)
David Majnemerc3340db2016-01-13 01:05:23 +000089 return;
90
Reid Klecknerc62e3792016-01-28 23:31:52 +000091 CurFn->LastLoc = DL;
Reid Kleckner00d96392016-01-29 00:13:28 +000092
93 MCSymbol *MCL = Asm->MMI->getContext().createTempSymbol();
94 Asm->OutStreamer->EmitLabel(MCL);
95 CurFn->Instrs.push_back(MCL);
96 LabelsAndLocs[MCL] = DL;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000097}
98
Reid Kleckner70f5bc92016-01-14 19:25:04 +000099CodeViewDebug::CodeViewDebug(AsmPrinter *AP)
Craig Topper353eda42014-04-24 06:44:33 +0000100 : Asm(nullptr), CurFn(nullptr) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000101 MachineModuleInfo *MMI = AP->MMI;
102
103 // If module doesn't have named metadata anchors or COFF debug section
104 // is not available, skip any debug info related stuff.
105 if (!MMI->getModule()->getNamedMetadata("llvm.dbg.cu") ||
106 !AP->getObjFileLowering().getCOFFDebugSymbolsSection())
107 return;
108
109 // Tell MMI that we have debug info.
110 MMI->setDebugInfoAvailability(true);
111 Asm = AP;
112}
113
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000114void CodeViewDebug::endModule() {
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000115 if (FnDebugInfo.empty())
116 return;
117
Reid Kleckner6b3faef2016-01-13 23:44:57 +0000118 // FIXME: For functions that are comdat, we should emit separate .debug$S
119 // sections that are comdat associative with the main function instead of
120 // having one big .debug$S section.
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000121 assert(Asm != nullptr);
Lang Hames9ff69c82015-04-24 19:11:51 +0000122 Asm->OutStreamer->SwitchSection(
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000123 Asm->getObjFileLowering().getCOFFDebugSymbolsSection());
124 Asm->EmitInt32(COFF::DEBUG_SECTION_MAGIC);
125
126 // The COFF .debug$S section consists of several subsections, each starting
127 // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length
128 // of the payload followed by the payload itself. The subsections are 4-byte
129 // aligned.
130
Reid Kleckner00d96392016-01-29 00:13:28 +0000131 // Emit per-function debug information. This code is extracted into a
132 // separate function for readability.
133 for (size_t I = 0, E = VisitedFunctions.size(); I != E; ++I)
134 emitDebugInfoForFunction(VisitedFunctions[I]);
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000135
136 // This subsection holds a file index to offset in string table table.
Lang Hames9ff69c82015-04-24 19:11:51 +0000137 Asm->OutStreamer->AddComment("File index to string table offset subsection");
Reid Kleckner00d96392016-01-29 00:13:28 +0000138 Asm->EmitInt32(unsigned(ModuleSubstreamKind::FileChecksums));
139 size_t NumFilenames = FileNameRegistry.Infos.size();
140 Asm->EmitInt32(8 * NumFilenames);
141 for (size_t I = 0, E = FileNameRegistry.Filenames.size(); I != E; ++I) {
142 StringRef Filename = FileNameRegistry.Filenames[I];
143 // For each unique filename, just write its offset in the string table.
144 Asm->EmitInt32(FileNameRegistry.Infos[Filename].StartOffset);
145 // The function name offset is not followed by any additional data.
146 Asm->EmitInt32(0);
147 }
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000148
149 // This subsection holds the string table.
Lang Hames9ff69c82015-04-24 19:11:51 +0000150 Asm->OutStreamer->AddComment("String table");
Reid Kleckner00d96392016-01-29 00:13:28 +0000151 Asm->EmitInt32(unsigned(ModuleSubstreamKind::StringTable));
152 Asm->EmitInt32(FileNameRegistry.LastOffset);
153 // The payload starts with a null character.
154 Asm->EmitInt8(0);
155
156 for (size_t I = 0, E = FileNameRegistry.Filenames.size(); I != E; ++I) {
157 // Just emit unique filenames one by one, separated by a null character.
158 Asm->OutStreamer->EmitBytes(FileNameRegistry.Filenames[I]);
159 Asm->EmitInt8(0);
160 }
161
162 // No more subsections. Fill with zeros to align the end of the section by 4.
163 Asm->OutStreamer->EmitFill((-FileNameRegistry.LastOffset) % 4, 0);
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000164
165 clear();
166}
167
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000168static void EmitLabelDiff(MCStreamer &Streamer,
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000169 const MCSymbol *From, const MCSymbol *To,
170 unsigned int Size = 4) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000171 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
172 MCContext &Context = Streamer.getContext();
Jim Grosbach13760bd2015-05-30 01:25:56 +0000173 const MCExpr *FromRef = MCSymbolRefExpr::create(From, Variant, Context),
174 *ToRef = MCSymbolRefExpr::create(To, Variant, Context);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000175 const MCExpr *AddrDelta =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000176 MCBinaryExpr::create(MCBinaryExpr::Sub, ToRef, FromRef, Context);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000177 Streamer.EmitValue(AddrDelta, Size);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000178}
179
Reid Kleckner00d96392016-01-29 00:13:28 +0000180static const DIFile *getFileFromLoc(DebugLoc DL) {
181 return DL.get()->getScope()->getFile();
182}
183
184void CodeViewDebug::emitDebugInfoForFunction(const Function *GV) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000185 // For each function there is a separate subsection
186 // which holds the PC to file:line table.
187 const MCSymbol *Fn = Asm->getSymbol(GV);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000188 assert(Fn);
Timur Iskhodzhanov8499a122014-03-26 09:50:36 +0000189
Reid Kleckner00d96392016-01-29 00:13:28 +0000190 const FunctionInfo &FI = FnDebugInfo[GV];
191 if (FI.Instrs.empty())
192 return;
193 assert(FI.End && "Don't know where the function ends?");
194
Duncan P. N. Exon Smith23e56ec2015-03-20 19:50:00 +0000195 StringRef FuncName;
Duncan P. N. Exon Smith2fbe1352015-04-20 22:10:08 +0000196 if (auto *SP = getDISubprogram(GV))
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000197 FuncName = SP->getDisplayName();
Duncan P. N. Exon Smith23e56ec2015-03-20 19:50:00 +0000198
Reid Kleckner3c0ff982016-01-14 00:12:54 +0000199 // If our DISubprogram name is empty, use the mangled name.
Reid Kleckner72e2ba72016-01-13 19:32:35 +0000200 if (FuncName.empty())
201 FuncName = GlobalValue::getRealLinkageName(GV->getName());
Reid Kleckner3c0ff982016-01-14 00:12:54 +0000202
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000203 // Emit a symbol subsection, required by VS2012+ to find function boundaries.
Jim Grosbach6f482002015-05-18 18:43:14 +0000204 MCSymbol *SymbolsBegin = Asm->MMI->getContext().createTempSymbol(),
205 *SymbolsEnd = Asm->MMI->getContext().createTempSymbol();
Lang Hames9ff69c82015-04-24 19:11:51 +0000206 Asm->OutStreamer->AddComment("Symbol subsection for " + Twine(FuncName));
Reid Kleckner6b3faef2016-01-13 23:44:57 +0000207 Asm->EmitInt32(unsigned(ModuleSubstreamKind::Symbols));
Lang Hames9ff69c82015-04-24 19:11:51 +0000208 EmitLabelDiff(*Asm->OutStreamer, SymbolsBegin, SymbolsEnd);
209 Asm->OutStreamer->EmitLabel(SymbolsBegin);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000210 {
Jim Grosbach6f482002015-05-18 18:43:14 +0000211 MCSymbol *ProcSegmentBegin = Asm->MMI->getContext().createTempSymbol(),
212 *ProcSegmentEnd = Asm->MMI->getContext().createTempSymbol();
Lang Hames9ff69c82015-04-24 19:11:51 +0000213 EmitLabelDiff(*Asm->OutStreamer, ProcSegmentBegin, ProcSegmentEnd, 2);
214 Asm->OutStreamer->EmitLabel(ProcSegmentBegin);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000215
Reid Kleckner6b3faef2016-01-13 23:44:57 +0000216 Asm->EmitInt16(unsigned(SymbolRecordKind::S_GPROC32_ID));
217
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000218 // Some bytes of this segment don't seem to be required for basic debugging,
219 // so just fill them with zeroes.
Lang Hames9ff69c82015-04-24 19:11:51 +0000220 Asm->OutStreamer->EmitFill(12, 0);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000221 // This is the important bit that tells the debugger where the function
222 // code is located and what's its size:
Lang Hames9ff69c82015-04-24 19:11:51 +0000223 EmitLabelDiff(*Asm->OutStreamer, Fn, FI.End);
224 Asm->OutStreamer->EmitFill(12, 0);
225 Asm->OutStreamer->EmitCOFFSecRel32(Fn);
226 Asm->OutStreamer->EmitCOFFSectionIndex(Fn);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000227 Asm->EmitInt8(0);
Timur Iskhodzhanova11b32b2014-11-12 20:10:09 +0000228 // Emit the function display name as a null-terminated string.
Lang Hames9ff69c82015-04-24 19:11:51 +0000229 Asm->OutStreamer->EmitBytes(FuncName);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000230 Asm->EmitInt8(0);
Lang Hames9ff69c82015-04-24 19:11:51 +0000231 Asm->OutStreamer->EmitLabel(ProcSegmentEnd);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000232
233 // We're done with this function.
234 Asm->EmitInt16(0x0002);
Reid Kleckner6b3faef2016-01-13 23:44:57 +0000235 Asm->EmitInt16(unsigned(SymbolRecordKind::S_PROC_ID_END));
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000236 }
Lang Hames9ff69c82015-04-24 19:11:51 +0000237 Asm->OutStreamer->EmitLabel(SymbolsEnd);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000238 // Every subsection must be aligned to a 4-byte boundary.
Lang Hames9ff69c82015-04-24 19:11:51 +0000239 Asm->OutStreamer->EmitFill((-FuncName.size()) % 4, 0);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000240
Reid Kleckner00d96392016-01-29 00:13:28 +0000241 // PCs/Instructions are grouped into segments sharing the same filename.
242 // Pre-calculate the lengths (in instructions) of these segments and store
243 // them in a map for convenience. Each index in the map is the sequential
244 // number of the respective instruction that starts a new segment.
245 DenseMap<size_t, size_t> FilenameSegmentLengths;
246 size_t LastSegmentEnd = 0;
247 const DIFile *PrevFile = getFileFromLoc(LabelsAndLocs[FI.Instrs[0]]);
248 for (size_t J = 1, F = FI.Instrs.size(); J != F; ++J) {
249 const DIFile *CurFile = getFileFromLoc(LabelsAndLocs[FI.Instrs[J]]);
250 if (PrevFile == CurFile)
251 continue;
252 FilenameSegmentLengths[LastSegmentEnd] = J - LastSegmentEnd;
253 LastSegmentEnd = J;
254 PrevFile = CurFile;
255 }
256 FilenameSegmentLengths[LastSegmentEnd] = FI.Instrs.size() - LastSegmentEnd;
257
258 // Emit a line table subsection, required to do PC-to-file:line lookup.
259 Asm->OutStreamer->AddComment("Line table subsection for " + Twine(FuncName));
260 Asm->EmitInt32(unsigned(ModuleSubstreamKind::Lines));
261 MCSymbol *LineTableBegin = Asm->MMI->getContext().createTempSymbol(),
262 *LineTableEnd = Asm->MMI->getContext().createTempSymbol();
263 EmitLabelDiff(*Asm->OutStreamer, LineTableBegin, LineTableEnd);
264 Asm->OutStreamer->EmitLabel(LineTableBegin);
265
266 // Identify the function this subsection is for.
267 Asm->OutStreamer->EmitCOFFSecRel32(Fn);
268 Asm->OutStreamer->EmitCOFFSectionIndex(Fn);
269 // Insert flags after a 16-bit section index.
270 Asm->EmitInt16(COFF::DEBUG_LINE_TABLES_HAVE_COLUMN_RECORDS);
271
272 // Length of the function's code, in bytes.
273 EmitLabelDiff(*Asm->OutStreamer, Fn, FI.End);
274
275 // PC-to-linenumber lookup table:
276 MCSymbol *FileSegmentEnd = nullptr;
277
278 // The start of the last segment:
279 size_t LastSegmentStart = 0;
280
281 auto FinishPreviousChunk = [&] {
282 if (!FileSegmentEnd)
283 return;
284 for (size_t ColSegI = LastSegmentStart,
285 ColSegEnd = ColSegI + FilenameSegmentLengths[LastSegmentStart];
286 ColSegI != ColSegEnd; ++ColSegI) {
287 unsigned ColumnNumber = LabelsAndLocs[FI.Instrs[ColSegI]].getCol();
288 // Truncate the column number if it is longer than the maximum we can
289 // record.
290 if (ColumnNumber > COFF::CVL_MaxColumnNumber)
291 ColumnNumber = 0;
292 Asm->EmitInt16(ColumnNumber); // Start column
293 Asm->EmitInt16(0); // End column
294 }
295 Asm->OutStreamer->EmitLabel(FileSegmentEnd);
296 };
297
298 for (size_t J = 0, F = FI.Instrs.size(); J != F; ++J) {
299 MCSymbol *Instr = FI.Instrs[J];
300 assert(LabelsAndLocs.count(Instr));
301
302 if (FilenameSegmentLengths.count(J)) {
303 // We came to a beginning of a new filename segment.
304 FinishPreviousChunk();
305 const DIFile *File = getFileFromLoc(LabelsAndLocs[FI.Instrs[J]]);
306 StringRef CurFilename = getFullFilepath(File);
307 size_t IndexInFileTable = FileNameRegistry.add(CurFilename);
308 // Each segment starts with the offset of the filename
309 // in the string table.
310 Asm->OutStreamer->AddComment(
311 "Segment for file '" + Twine(CurFilename) + "' begins");
312 MCSymbol *FileSegmentBegin = Asm->MMI->getContext().createTempSymbol();
313 Asm->OutStreamer->EmitLabel(FileSegmentBegin);
314 Asm->EmitInt32(8 * IndexInFileTable);
315
316 // Number of PC records in the lookup table.
317 size_t SegmentLength = FilenameSegmentLengths[J];
318 Asm->EmitInt32(SegmentLength);
319
320 // Full size of the segment for this filename, including the prev two
321 // records.
322 FileSegmentEnd = Asm->MMI->getContext().createTempSymbol();
323 EmitLabelDiff(*Asm->OutStreamer, FileSegmentBegin, FileSegmentEnd);
324 LastSegmentStart = J;
325 }
326
327 // The first PC with the given linenumber and the linenumber itself.
328 EmitLabelDiff(*Asm->OutStreamer, Fn, Instr);
329 uint32_t LineNumber = LabelsAndLocs[Instr].getLine();
330 assert(LineNumber <= COFF::CVL_MaxLineNumber);
331 uint32_t LineData = LineNumber | COFF::CVL_IsStatement;
332 Asm->EmitInt32(LineData);
333 }
334
335 FinishPreviousChunk();
336 Asm->OutStreamer->EmitLabel(LineTableEnd);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000337}
338
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000339void CodeViewDebug::beginFunction(const MachineFunction *MF) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000340 assert(!CurFn && "Can't process two functions at once!");
341
342 if (!Asm || !Asm->MMI->hasDebugInfo())
343 return;
344
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000345 const Function *GV = MF->getFunction();
346 assert(FnDebugInfo.count(GV) == false);
Reid Kleckner00d96392016-01-29 00:13:28 +0000347 VisitedFunctions.push_back(GV);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000348 CurFn = &FnDebugInfo[GV];
349
350 // Find the end of the function prolog.
351 // FIXME: is there a simpler a way to do this? Can we just search
352 // for the first instruction of the function, not the last of the prolog?
353 DebugLoc PrologEndLoc;
354 bool EmptyPrologue = true;
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000355 for (const auto &MBB : *MF) {
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000356 if (PrologEndLoc)
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000357 break;
358 for (const auto &MI : MBB) {
359 if (MI.isDebugValue())
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000360 continue;
361
362 // First known non-DBG_VALUE and non-frame setup location marks
363 // the beginning of the function body.
364 // FIXME: do we need the first subcondition?
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000365 if (!MI.getFlag(MachineInstr::FrameSetup) && MI.getDebugLoc()) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000366 PrologEndLoc = MI.getDebugLoc();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000367 break;
368 }
369 EmptyPrologue = false;
370 }
371 }
372 // Record beginning of function if we have a non-empty prologue.
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000373 if (PrologEndLoc && !EmptyPrologue) {
374 DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000375 maybeRecordLocation(FnStartDL, MF);
376 }
377}
378
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000379void CodeViewDebug::endFunction(const MachineFunction *MF) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000380 if (!Asm || !CurFn) // We haven't created any debug info for this function.
381 return;
382
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000383 const Function *GV = MF->getFunction();
Yaron Keren6d3194f2014-06-20 10:26:56 +0000384 assert(FnDebugInfo.count(GV));
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000385 assert(CurFn == &FnDebugInfo[GV]);
386
Reid Kleckner00d96392016-01-29 00:13:28 +0000387 if (CurFn->Instrs.empty()) {
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000388 FnDebugInfo.erase(GV);
Reid Kleckner00d96392016-01-29 00:13:28 +0000389 VisitedFunctions.pop_back();
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000390 } else {
Rafael Espindola07c03d32015-03-05 02:05:42 +0000391 CurFn->End = Asm->getFunctionEnd();
Timur Iskhodzhanov8499a122014-03-26 09:50:36 +0000392 }
Craig Topper353eda42014-04-24 06:44:33 +0000393 CurFn = nullptr;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000394}
395
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000396void CodeViewDebug::beginInstruction(const MachineInstr *MI) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000397 // Ignore DBG_VALUE locations and function prologue.
398 if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup))
399 return;
400 DebugLoc DL = MI->getDebugLoc();
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000401 if (DL == PrevInstLoc || !DL)
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000402 return;
403 maybeRecordLocation(DL, Asm->MF);
404}
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000405}