blob: 7a5c4f3136a21b440431133c2e9fe617f55a714d [file] [log] [blame]
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +00001//===-- 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 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
25StringRef WinCodeViewLineTables::getFullFilepath(const MDNode *S) {
26 assert(S);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000027 assert((isa<DICompileUnit>(S) || isa<DIFile>(S) || isa<DISubprogram>(S) ||
28 isa<DILexicalBlockBase>(S)) &&
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000029 "Unexpected scope info");
30
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000031 auto *Scope = cast<DIScope>(S);
Duncan P. N. Exon Smithb273d062015-04-16 01:37:00 +000032 StringRef Dir = Scope->getDirectory(),
33 Filename = Scope->getFilename();
Reid Kleckner1f11b4e2015-12-02 22:34:30 +000034 std::string &Filepath =
35 DirAndFilenameToFilepathMap[std::make_pair(Dir, Filename)];
36 if (!Filepath.empty())
37 return Filepath;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000038
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 Iskhodzhanovf166f6c2014-01-30 01:39:17 +000043 if (Filename.find(':') == 1)
44 Filepath = Filename;
45 else
Yaron Keren75e0c4b2015-03-27 17:51:30 +000046 Filepath = (Dir + "\\" + Filename).str();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000047
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 Kleckner1f11b4e2015-12-02 22:34:30 +000081 return Filepath;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000082}
83
84void WinCodeViewLineTables::maybeRecordLocation(DebugLoc DL,
85 const MachineFunction *MF) {
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +000086 const MDNode *Scope = DL.getScope();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000087 if (!Scope)
88 return;
David Majnemerc3340db2016-01-13 01:05:23 +000089 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 Iskhodzhanovf166f6c2014-01-30 01:39:17 +000099 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 Majnemerc3340db2016-01-13 01:05:23 +0000105 if (LastInstr.Filename == Filename && LastInstr.LineNumber == LineNumber &&
106 LastInstr.ColumnNumber == ColumnNumber)
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000107 return;
108 }
109 FileNameRegistry.add(Filename);
110
Jim Grosbach6f482002015-05-18 18:43:14 +0000111 MCSymbol *MCL = Asm->MMI->getContext().createTempSymbol();
Lang Hames9ff69c82015-04-24 19:11:51 +0000112 Asm->OutStreamer->EmitLabel(MCL);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000113 CurFn->Instrs.push_back(MCL);
David Majnemerc3340db2016-01-13 01:05:23 +0000114 InstrInfo[MCL] = InstrInfoTy(Filename, LineNumber, ColumnNumber);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000115}
116
117WinCodeViewLineTables::WinCodeViewLineTables(AsmPrinter *AP)
Craig Topper353eda42014-04-24 06:44:33 +0000118 : Asm(nullptr), CurFn(nullptr) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000119 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 Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000132void WinCodeViewLineTables::endModule() {
133 if (FnDebugInfo.empty())
134 return;
135
Reid Kleckner6b3faef2016-01-13 23:44:57 +0000136 // 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 Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000139 assert(Asm != nullptr);
Lang Hames9ff69c82015-04-24 19:11:51 +0000140 Asm->OutStreamer->SwitchSection(
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000141 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 Hames9ff69c82015-04-24 19:11:51 +0000155 Asm->OutStreamer->AddComment("File index to string table offset subsection");
Reid Kleckner6b3faef2016-01-13 23:44:57 +0000156 Asm->EmitInt32(unsigned(ModuleSubstreamKind::FileChecksums));
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000157 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 Hames9ff69c82015-04-24 19:11:51 +0000168 Asm->OutStreamer->AddComment("String table");
Reid Kleckner6b3faef2016-01-13 23:44:57 +0000169 Asm->EmitInt32(unsigned(ModuleSubstreamKind::StringTable));
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000170 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 Hames9ff69c82015-04-24 19:11:51 +0000176 Asm->OutStreamer->EmitBytes(FileNameRegistry.Filenames[I]);
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000177 Asm->EmitInt8(0);
178 }
179
180 // No more subsections. Fill with zeros to align the end of the section by 4.
Lang Hames9ff69c82015-04-24 19:11:51 +0000181 Asm->OutStreamer->EmitFill((-FileNameRegistry.LastOffset) % 4, 0);
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000182
183 clear();
184}
185
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000186static void EmitLabelDiff(MCStreamer &Streamer,
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000187 const MCSymbol *From, const MCSymbol *To,
188 unsigned int Size = 4) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000189 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
190 MCContext &Context = Streamer.getContext();
Jim Grosbach13760bd2015-05-30 01:25:56 +0000191 const MCExpr *FromRef = MCSymbolRefExpr::create(From, Variant, Context),
192 *ToRef = MCSymbolRefExpr::create(To, Variant, Context);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000193 const MCExpr *AddrDelta =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000194 MCBinaryExpr::create(MCBinaryExpr::Sub, ToRef, FromRef, Context);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000195 Streamer.EmitValue(AddrDelta, Size);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000196}
197
198void 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 Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000202 assert(Fn);
Timur Iskhodzhanov8499a122014-03-26 09:50:36 +0000203
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 Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000208
Duncan P. N. Exon Smith23e56ec2015-03-20 19:50:00 +0000209 StringRef FuncName;
Duncan P. N. Exon Smith2fbe1352015-04-20 22:10:08 +0000210 if (auto *SP = getDISubprogram(GV))
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000211 FuncName = SP->getDisplayName();
Duncan P. N. Exon Smith23e56ec2015-03-20 19:50:00 +0000212
Timur Iskhodzhanov0e76a162014-11-12 20:21:20 +0000213 // FIXME Clang currently sets DisplayName to "bar" for a C++
214 // "namespace_foo::bar" function, see PR21528. Luckily, dbghelp.dll is trying
215 // to demangle display names anyways, so let's just put a mangled name into
216 // the symbols subsection until Clang gives us what we need.
Reid Kleckner72e2ba72016-01-13 19:32:35 +0000217 if (FuncName.empty())
218 FuncName = GlobalValue::getRealLinkageName(GV->getName());
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000219 // Emit a symbol subsection, required by VS2012+ to find function boundaries.
Jim Grosbach6f482002015-05-18 18:43:14 +0000220 MCSymbol *SymbolsBegin = Asm->MMI->getContext().createTempSymbol(),
221 *SymbolsEnd = Asm->MMI->getContext().createTempSymbol();
Lang Hames9ff69c82015-04-24 19:11:51 +0000222 Asm->OutStreamer->AddComment("Symbol subsection for " + Twine(FuncName));
Reid Kleckner6b3faef2016-01-13 23:44:57 +0000223 Asm->EmitInt32(unsigned(ModuleSubstreamKind::Symbols));
Lang Hames9ff69c82015-04-24 19:11:51 +0000224 EmitLabelDiff(*Asm->OutStreamer, SymbolsBegin, SymbolsEnd);
225 Asm->OutStreamer->EmitLabel(SymbolsBegin);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000226 {
Jim Grosbach6f482002015-05-18 18:43:14 +0000227 MCSymbol *ProcSegmentBegin = Asm->MMI->getContext().createTempSymbol(),
228 *ProcSegmentEnd = Asm->MMI->getContext().createTempSymbol();
Lang Hames9ff69c82015-04-24 19:11:51 +0000229 EmitLabelDiff(*Asm->OutStreamer, ProcSegmentBegin, ProcSegmentEnd, 2);
230 Asm->OutStreamer->EmitLabel(ProcSegmentBegin);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000231
Reid Kleckner6b3faef2016-01-13 23:44:57 +0000232 Asm->EmitInt16(unsigned(SymbolRecordKind::S_GPROC32_ID));
233
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000234 // Some bytes of this segment don't seem to be required for basic debugging,
235 // so just fill them with zeroes.
Lang Hames9ff69c82015-04-24 19:11:51 +0000236 Asm->OutStreamer->EmitFill(12, 0);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000237 // This is the important bit that tells the debugger where the function
238 // code is located and what's its size:
Lang Hames9ff69c82015-04-24 19:11:51 +0000239 EmitLabelDiff(*Asm->OutStreamer, Fn, FI.End);
240 Asm->OutStreamer->EmitFill(12, 0);
241 Asm->OutStreamer->EmitCOFFSecRel32(Fn);
242 Asm->OutStreamer->EmitCOFFSectionIndex(Fn);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000243 Asm->EmitInt8(0);
Timur Iskhodzhanova11b32b2014-11-12 20:10:09 +0000244 // Emit the function display name as a null-terminated string.
Lang Hames9ff69c82015-04-24 19:11:51 +0000245 Asm->OutStreamer->EmitBytes(FuncName);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000246 Asm->EmitInt8(0);
Lang Hames9ff69c82015-04-24 19:11:51 +0000247 Asm->OutStreamer->EmitLabel(ProcSegmentEnd);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000248
249 // We're done with this function.
250 Asm->EmitInt16(0x0002);
Reid Kleckner6b3faef2016-01-13 23:44:57 +0000251 Asm->EmitInt16(unsigned(SymbolRecordKind::S_PROC_ID_END));
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000252 }
Lang Hames9ff69c82015-04-24 19:11:51 +0000253 Asm->OutStreamer->EmitLabel(SymbolsEnd);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000254 // Every subsection must be aligned to a 4-byte boundary.
Lang Hames9ff69c82015-04-24 19:11:51 +0000255 Asm->OutStreamer->EmitFill((-FuncName.size()) % 4, 0);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000256
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000257 // PCs/Instructions are grouped into segments sharing the same filename.
258 // Pre-calculate the lengths (in instructions) of these segments and store
259 // them in a map for convenience. Each index in the map is the sequential
260 // number of the respective instruction that starts a new segment.
261 DenseMap<size_t, size_t> FilenameSegmentLengths;
262 size_t LastSegmentEnd = 0;
263 StringRef PrevFilename = InstrInfo[FI.Instrs[0]].Filename;
264 for (size_t J = 1, F = FI.Instrs.size(); J != F; ++J) {
265 if (PrevFilename == InstrInfo[FI.Instrs[J]].Filename)
266 continue;
267 FilenameSegmentLengths[LastSegmentEnd] = J - LastSegmentEnd;
268 LastSegmentEnd = J;
269 PrevFilename = InstrInfo[FI.Instrs[J]].Filename;
270 }
271 FilenameSegmentLengths[LastSegmentEnd] = FI.Instrs.size() - LastSegmentEnd;
272
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000273 // Emit a line table subsection, required to do PC-to-file:line lookup.
Lang Hames9ff69c82015-04-24 19:11:51 +0000274 Asm->OutStreamer->AddComment("Line table subsection for " + Twine(FuncName));
Reid Kleckner6b3faef2016-01-13 23:44:57 +0000275 Asm->EmitInt32(unsigned(ModuleSubstreamKind::Lines));
Jim Grosbach6f482002015-05-18 18:43:14 +0000276 MCSymbol *LineTableBegin = Asm->MMI->getContext().createTempSymbol(),
277 *LineTableEnd = Asm->MMI->getContext().createTempSymbol();
Lang Hames9ff69c82015-04-24 19:11:51 +0000278 EmitLabelDiff(*Asm->OutStreamer, LineTableBegin, LineTableEnd);
279 Asm->OutStreamer->EmitLabel(LineTableBegin);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000280
281 // Identify the function this subsection is for.
Lang Hames9ff69c82015-04-24 19:11:51 +0000282 Asm->OutStreamer->EmitCOFFSecRel32(Fn);
283 Asm->OutStreamer->EmitCOFFSectionIndex(Fn);
David Majnemer3f49e662015-07-09 00:19:51 +0000284 // Insert flags after a 16-bit section index.
285 Asm->EmitInt16(COFF::DEBUG_LINE_TABLES_HAVE_COLUMN_RECORDS);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000286
287 // Length of the function's code, in bytes.
Lang Hames9ff69c82015-04-24 19:11:51 +0000288 EmitLabelDiff(*Asm->OutStreamer, Fn, FI.End);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000289
290 // PC-to-linenumber lookup table:
Craig Topper353eda42014-04-24 06:44:33 +0000291 MCSymbol *FileSegmentEnd = nullptr;
David Majnemer3f49e662015-07-09 00:19:51 +0000292
293 // The start of the last segment:
294 size_t LastSegmentStart = 0;
295
296 auto FinishPreviousChunk = [&] {
297 if (!FileSegmentEnd)
298 return;
299 for (size_t ColSegI = LastSegmentStart,
300 ColSegEnd = ColSegI + FilenameSegmentLengths[LastSegmentStart];
301 ColSegI != ColSegEnd; ++ColSegI) {
302 unsigned ColumnNumber = InstrInfo[FI.Instrs[ColSegI]].ColumnNumber;
David Majnemerc3340db2016-01-13 01:05:23 +0000303 assert(ColumnNumber <= COFF::CVL_MaxColumnNumber);
David Majnemer3f49e662015-07-09 00:19:51 +0000304 Asm->EmitInt16(ColumnNumber); // Start column
David Majnemerc81c8c62016-01-12 21:58:20 +0000305 Asm->EmitInt16(0); // End column
David Majnemer3f49e662015-07-09 00:19:51 +0000306 }
307 Asm->OutStreamer->EmitLabel(FileSegmentEnd);
308 };
309
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000310 for (size_t J = 0, F = FI.Instrs.size(); J != F; ++J) {
311 MCSymbol *Instr = FI.Instrs[J];
312 assert(InstrInfo.count(Instr));
313
314 if (FilenameSegmentLengths.count(J)) {
315 // We came to a beginning of a new filename segment.
David Majnemer3f49e662015-07-09 00:19:51 +0000316 FinishPreviousChunk();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000317 StringRef CurFilename = InstrInfo[FI.Instrs[J]].Filename;
318 assert(FileNameRegistry.Infos.count(CurFilename));
319 size_t IndexInStringTable =
320 FileNameRegistry.Infos[CurFilename].FilenameID;
321 // Each segment starts with the offset of the filename
322 // in the string table.
Lang Hames9ff69c82015-04-24 19:11:51 +0000323 Asm->OutStreamer->AddComment(
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000324 "Segment for file '" + Twine(CurFilename) + "' begins");
Jim Grosbach6f482002015-05-18 18:43:14 +0000325 MCSymbol *FileSegmentBegin = Asm->MMI->getContext().createTempSymbol();
Lang Hames9ff69c82015-04-24 19:11:51 +0000326 Asm->OutStreamer->EmitLabel(FileSegmentBegin);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000327 Asm->EmitInt32(8 * IndexInStringTable);
328
329 // Number of PC records in the lookup table.
330 size_t SegmentLength = FilenameSegmentLengths[J];
331 Asm->EmitInt32(SegmentLength);
332
333 // Full size of the segment for this filename, including the prev two
334 // records.
Jim Grosbach6f482002015-05-18 18:43:14 +0000335 FileSegmentEnd = Asm->MMI->getContext().createTempSymbol();
Lang Hames9ff69c82015-04-24 19:11:51 +0000336 EmitLabelDiff(*Asm->OutStreamer, FileSegmentBegin, FileSegmentEnd);
David Majnemer3f49e662015-07-09 00:19:51 +0000337 LastSegmentStart = J;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000338 }
339
340 // The first PC with the given linenumber and the linenumber itself.
Lang Hames9ff69c82015-04-24 19:11:51 +0000341 EmitLabelDiff(*Asm->OutStreamer, Fn, Instr);
David Majnemerc3340db2016-01-13 01:05:23 +0000342 uint32_t LineNumber = InstrInfo[Instr].LineNumber;
343 assert(LineNumber <= COFF::CVL_MaxLineNumber);
344 uint32_t LineData = LineNumber | COFF::CVL_IsStatement;
345 Asm->EmitInt32(LineData);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000346 }
347
David Majnemer3f49e662015-07-09 00:19:51 +0000348 FinishPreviousChunk();
Lang Hames9ff69c82015-04-24 19:11:51 +0000349 Asm->OutStreamer->EmitLabel(LineTableEnd);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000350}
351
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000352void WinCodeViewLineTables::beginFunction(const MachineFunction *MF) {
353 assert(!CurFn && "Can't process two functions at once!");
354
355 if (!Asm || !Asm->MMI->hasDebugInfo())
356 return;
357
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000358 const Function *GV = MF->getFunction();
359 assert(FnDebugInfo.count(GV) == false);
360 VisitedFunctions.push_back(GV);
361 CurFn = &FnDebugInfo[GV];
362
363 // Find the end of the function prolog.
364 // FIXME: is there a simpler a way to do this? Can we just search
365 // for the first instruction of the function, not the last of the prolog?
366 DebugLoc PrologEndLoc;
367 bool EmptyPrologue = true;
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000368 for (const auto &MBB : *MF) {
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000369 if (PrologEndLoc)
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000370 break;
371 for (const auto &MI : MBB) {
372 if (MI.isDebugValue())
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000373 continue;
374
375 // First known non-DBG_VALUE and non-frame setup location marks
376 // the beginning of the function body.
377 // FIXME: do we need the first subcondition?
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000378 if (!MI.getFlag(MachineInstr::FrameSetup) && MI.getDebugLoc()) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000379 PrologEndLoc = MI.getDebugLoc();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000380 break;
381 }
382 EmptyPrologue = false;
383 }
384 }
385 // Record beginning of function if we have a non-empty prologue.
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000386 if (PrologEndLoc && !EmptyPrologue) {
387 DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000388 maybeRecordLocation(FnStartDL, MF);
389 }
390}
391
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000392void WinCodeViewLineTables::endFunction(const MachineFunction *MF) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000393 if (!Asm || !CurFn) // We haven't created any debug info for this function.
394 return;
395
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000396 const Function *GV = MF->getFunction();
Yaron Keren6d3194f2014-06-20 10:26:56 +0000397 assert(FnDebugInfo.count(GV));
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000398 assert(CurFn == &FnDebugInfo[GV]);
399
400 if (CurFn->Instrs.empty()) {
401 FnDebugInfo.erase(GV);
402 VisitedFunctions.pop_back();
403 } else {
Rafael Espindola07c03d32015-03-05 02:05:42 +0000404 CurFn->End = Asm->getFunctionEnd();
Timur Iskhodzhanov8499a122014-03-26 09:50:36 +0000405 }
Craig Topper353eda42014-04-24 06:44:33 +0000406 CurFn = nullptr;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000407}
408
409void WinCodeViewLineTables::beginInstruction(const MachineInstr *MI) {
410 // Ignore DBG_VALUE locations and function prologue.
411 if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup))
412 return;
413 DebugLoc DL = MI->getDebugLoc();
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000414 if (DL == PrevInstLoc || !DL)
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000415 return;
416 maybeRecordLocation(DL, Asm->MF);
417}
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000418}