blob: 9749d1cff3530f58a151405a6875d371c4228839 [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"
Reid Kleckner2214ed82016-01-29 00:49:42 +000016#include "llvm/DebugInfo/CodeView/Line.h"
Reid Kleckner6b3faef2016-01-13 23:44:57 +000017#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
Reid Klecknerf3b9ba42016-01-29 18:16:43 +000018#include "llvm/DebugInfo/CodeView/TypeIndex.h"
19#include "llvm/DebugInfo/CodeView/TypeRecord.h"
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000020#include "llvm/MC/MCExpr.h"
21#include "llvm/MC/MCSymbol.h"
22#include "llvm/Support/COFF.h"
Reid Klecknerf9c275f2016-02-10 20:55:49 +000023#include "llvm/Target/TargetSubtargetInfo.h"
24#include "llvm/Target/TargetRegisterInfo.h"
25#include "llvm/Target/TargetFrameLowering.h"
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000026
Reid Klecknerf9c275f2016-02-10 20:55:49 +000027using namespace llvm;
Reid Kleckner6b3faef2016-01-13 23:44:57 +000028using namespace llvm::codeview;
29
Reid Klecknerf9c275f2016-02-10 20:55:49 +000030CodeViewDebug::CodeViewDebug(AsmPrinter *AP)
31 : DebugHandlerBase(AP), OS(*Asm->OutStreamer), CurFn(nullptr) {
32 // If module doesn't have named metadata anchors or COFF debug section
33 // is not available, skip any debug info related stuff.
34 if (!MMI->getModule()->getNamedMetadata("llvm.dbg.cu") ||
35 !AP->getObjFileLowering().getCOFFDebugSymbolsSection()) {
36 Asm = nullptr;
37 return;
38 }
39
40 // Tell MMI that we have debug info.
41 MMI->setDebugInfoAvailability(true);
42}
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000043
Reid Kleckner9533af42016-01-16 00:09:09 +000044StringRef CodeViewDebug::getFullFilepath(const DIFile *File) {
45 std::string &Filepath = FileToFilepathMap[File];
Reid Kleckner1f11b4e2015-12-02 22:34:30 +000046 if (!Filepath.empty())
47 return Filepath;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000048
Reid Kleckner9533af42016-01-16 00:09:09 +000049 StringRef Dir = File->getDirectory(), Filename = File->getFilename();
50
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000051 // Clang emits directory and relative filename info into the IR, but CodeView
52 // operates on full paths. We could change Clang to emit full paths too, but
53 // that would increase the IR size and probably not needed for other users.
54 // For now, just concatenate and canonicalize the path here.
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000055 if (Filename.find(':') == 1)
56 Filepath = Filename;
57 else
Yaron Keren75e0c4b2015-03-27 17:51:30 +000058 Filepath = (Dir + "\\" + Filename).str();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000059
60 // Canonicalize the path. We have to do it textually because we may no longer
61 // have access the file in the filesystem.
62 // First, replace all slashes with backslashes.
63 std::replace(Filepath.begin(), Filepath.end(), '/', '\\');
64
65 // Remove all "\.\" with "\".
66 size_t Cursor = 0;
67 while ((Cursor = Filepath.find("\\.\\", Cursor)) != std::string::npos)
68 Filepath.erase(Cursor, 2);
69
70 // Replace all "\XXX\..\" with "\". Don't try too hard though as the original
71 // path should be well-formatted, e.g. start with a drive letter, etc.
72 Cursor = 0;
73 while ((Cursor = Filepath.find("\\..\\", Cursor)) != std::string::npos) {
74 // Something's wrong if the path starts with "\..\", abort.
75 if (Cursor == 0)
76 break;
77
78 size_t PrevSlash = Filepath.rfind('\\', Cursor - 1);
79 if (PrevSlash == std::string::npos)
80 // Something's wrong, abort.
81 break;
82
83 Filepath.erase(PrevSlash, Cursor + 3 - PrevSlash);
84 // The next ".." might be following the one we've just erased.
85 Cursor = PrevSlash;
86 }
87
88 // Remove all duplicate backslashes.
89 Cursor = 0;
90 while ((Cursor = Filepath.find("\\\\", Cursor)) != std::string::npos)
91 Filepath.erase(Cursor, 1);
92
Reid Kleckner1f11b4e2015-12-02 22:34:30 +000093 return Filepath;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000094}
95
Reid Kleckner2214ed82016-01-29 00:49:42 +000096unsigned CodeViewDebug::maybeRecordFile(const DIFile *F) {
97 unsigned NextId = FileIdMap.size() + 1;
98 auto Insertion = FileIdMap.insert(std::make_pair(F, NextId));
99 if (Insertion.second) {
100 // We have to compute the full filepath and emit a .cv_file directive.
101 StringRef FullPath = getFullFilepath(F);
Reid Klecknerdac21b42016-02-03 21:15:48 +0000102 NextId = OS.EmitCVFileDirective(NextId, FullPath);
Reid Kleckner2214ed82016-01-29 00:49:42 +0000103 assert(NextId == FileIdMap.size() && ".cv_file directive failed");
104 }
105 return Insertion.first->second;
106}
107
Reid Kleckner876330d2016-02-12 21:48:30 +0000108CodeViewDebug::InlineSite &
109CodeViewDebug::getInlineSite(const DILocation *InlinedAt,
110 const DISubprogram *Inlinee) {
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000111 auto Insertion = CurFn->InlineSites.insert({InlinedAt, InlineSite()});
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000112 InlineSite *Site = &Insertion.first->second;
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000113 if (Insertion.second) {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000114 Site->SiteFuncId = NextFuncId++;
Reid Kleckner876330d2016-02-12 21:48:30 +0000115 Site->Inlinee = Inlinee;
116 InlinedSubprograms.insert(Inlinee);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000117 }
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000118 return *Site;
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000119}
120
Reid Kleckner876330d2016-02-12 21:48:30 +0000121void CodeViewDebug::recordLocalVariable(LocalVariable &&Var,
122 const DILocation *InlinedAt) {
123 if (InlinedAt) {
124 // This variable was inlined. Associate it with the InlineSite.
125 const DISubprogram *Inlinee = Var.DIVar->getScope()->getSubprogram();
126 InlineSite &Site = getInlineSite(InlinedAt, Inlinee);
127 Site.InlinedLocals.emplace_back(Var);
128 } else {
129 // This variable goes in the main ProcSym.
130 CurFn->Locals.emplace_back(Var);
131 }
132}
133
Reid Kleckner829365a2016-02-11 19:41:47 +0000134static void addLocIfNotPresent(SmallVectorImpl<const DILocation *> &Locs,
135 const DILocation *Loc) {
136 auto B = Locs.begin(), E = Locs.end();
137 if (std::find(B, E, Loc) == E)
138 Locs.push_back(Loc);
139}
140
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000141void CodeViewDebug::maybeRecordLocation(DebugLoc DL,
Reid Kleckner9533af42016-01-16 00:09:09 +0000142 const MachineFunction *MF) {
143 // Skip this instruction if it has the same location as the previous one.
144 if (DL == CurFn->LastLoc)
145 return;
146
147 const DIScope *Scope = DL.get()->getScope();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000148 if (!Scope)
149 return;
Reid Kleckner9533af42016-01-16 00:09:09 +0000150
David Majnemerc3340db2016-01-13 01:05:23 +0000151 // Skip this line if it is longer than the maximum we can record.
Reid Kleckner2214ed82016-01-29 00:49:42 +0000152 LineInfo LI(DL.getLine(), DL.getLine(), /*IsStatement=*/true);
153 if (LI.getStartLine() != DL.getLine() || LI.isAlwaysStepInto() ||
154 LI.isNeverStepInto())
David Majnemerc3340db2016-01-13 01:05:23 +0000155 return;
156
Reid Kleckner2214ed82016-01-29 00:49:42 +0000157 ColumnInfo CI(DL.getCol(), /*EndColumn=*/0);
158 if (CI.getStartColumn() != DL.getCol())
159 return;
Reid Kleckner00d96392016-01-29 00:13:28 +0000160
Reid Kleckner2214ed82016-01-29 00:49:42 +0000161 if (!CurFn->HaveLineInfo)
162 CurFn->HaveLineInfo = true;
163 unsigned FileId = 0;
164 if (CurFn->LastLoc.get() && CurFn->LastLoc->getFile() == DL->getFile())
165 FileId = CurFn->LastFileId;
166 else
167 FileId = CurFn->LastFileId = maybeRecordFile(DL->getFile());
168 CurFn->LastLoc = DL;
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000169
170 unsigned FuncId = CurFn->FuncId;
Reid Kleckner876330d2016-02-12 21:48:30 +0000171 if (const DILocation *SiteLoc = DL->getInlinedAt()) {
Reid Kleckner829365a2016-02-11 19:41:47 +0000172 const DILocation *Loc = DL.get();
173
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000174 // If this location was actually inlined from somewhere else, give it the ID
175 // of the inline call site.
Reid Kleckner876330d2016-02-12 21:48:30 +0000176 FuncId =
177 getInlineSite(SiteLoc, Loc->getScope()->getSubprogram()).SiteFuncId;
Reid Kleckner829365a2016-02-11 19:41:47 +0000178
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000179 // Ensure we have links in the tree of inline call sites.
Reid Kleckner829365a2016-02-11 19:41:47 +0000180 bool FirstLoc = true;
181 while ((SiteLoc = Loc->getInlinedAt())) {
Reid Kleckner876330d2016-02-12 21:48:30 +0000182 InlineSite &Site =
183 getInlineSite(SiteLoc, Loc->getScope()->getSubprogram());
Reid Kleckner829365a2016-02-11 19:41:47 +0000184 if (!FirstLoc)
185 addLocIfNotPresent(Site.ChildSites, Loc);
186 FirstLoc = false;
187 Loc = SiteLoc;
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000188 }
Reid Kleckner829365a2016-02-11 19:41:47 +0000189 addLocIfNotPresent(CurFn->ChildSites, Loc);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000190 }
191
Reid Klecknerdac21b42016-02-03 21:15:48 +0000192 OS.EmitCVLocDirective(FuncId, FileId, DL.getLine(), DL.getCol(),
193 /*PrologueEnd=*/false,
194 /*IsStmt=*/false, DL->getFilename());
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000195}
196
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000197void CodeViewDebug::endModule() {
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000198 if (FnDebugInfo.empty())
199 return;
200
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000201 emitTypeInformation();
202
Reid Kleckner6b3faef2016-01-13 23:44:57 +0000203 // FIXME: For functions that are comdat, we should emit separate .debug$S
204 // sections that are comdat associative with the main function instead of
205 // having one big .debug$S section.
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000206 assert(Asm != nullptr);
Reid Klecknerdac21b42016-02-03 21:15:48 +0000207 OS.SwitchSection(Asm->getObjFileLowering().getCOFFDebugSymbolsSection());
208 OS.AddComment("Debug section magic");
209 OS.EmitIntValue(COFF::DEBUG_SECTION_MAGIC, 4);
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000210
211 // The COFF .debug$S section consists of several subsections, each starting
212 // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length
213 // of the payload followed by the payload itself. The subsections are 4-byte
214 // aligned.
215
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000216 // Make a subsection for all the inlined subprograms.
217 emitInlineeLinesSubsection();
218
Reid Kleckner2214ed82016-01-29 00:49:42 +0000219 // Emit per-function debug information.
220 for (auto &P : FnDebugInfo)
221 emitDebugInfoForFunction(P.first, P.second);
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000222
223 // This subsection holds a file index to offset in string table table.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000224 OS.AddComment("File index to string table offset subsection");
225 OS.EmitCVFileChecksumsDirective();
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000226
227 // This subsection holds the string table.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000228 OS.AddComment("String table");
229 OS.EmitCVStringTableDirective();
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000230
231 clear();
232}
233
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000234void CodeViewDebug::emitTypeInformation() {
235 // Start the .debug$T section with 0x4.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000236 OS.SwitchSection(Asm->getObjFileLowering().getCOFFDebugTypesSection());
237 OS.AddComment("Debug section magic");
238 OS.EmitIntValue(COFF::DEBUG_SECTION_MAGIC, 4);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000239
240 NamedMDNode *CU_Nodes =
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000241 MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000242 if (!CU_Nodes)
243 return;
244
245 // This type info currently only holds function ids for use with inline call
246 // frame info. All functions are assigned a simple 'void ()' type. Emit that
247 // type here.
248 TypeIndex ArgListIdx = getNextTypeIndex();
Reid Klecknerdac21b42016-02-03 21:15:48 +0000249 OS.AddComment("Type record length");
250 OS.EmitIntValue(2 + sizeof(ArgList), 2);
251 OS.AddComment("Leaf type: LF_ARGLIST");
252 OS.EmitIntValue(LF_ARGLIST, 2);
253 OS.AddComment("Number of arguments");
254 OS.EmitIntValue(0, 4);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000255
256 TypeIndex VoidProcIdx = getNextTypeIndex();
Reid Klecknerdac21b42016-02-03 21:15:48 +0000257 OS.AddComment("Type record length");
258 OS.EmitIntValue(2 + sizeof(ProcedureType), 2);
259 OS.AddComment("Leaf type: LF_PROCEDURE");
260 OS.EmitIntValue(LF_PROCEDURE, 2);
261 OS.AddComment("Return type index");
262 OS.EmitIntValue(TypeIndex::Void().getIndex(), 4);
263 OS.AddComment("Calling convention");
264 OS.EmitIntValue(char(CallingConvention::NearC), 1);
265 OS.AddComment("Function options");
266 OS.EmitIntValue(char(FunctionOptions::None), 1);
267 OS.AddComment("# of parameters");
268 OS.EmitIntValue(0, 2);
269 OS.AddComment("Argument list type index");
270 OS.EmitIntValue(ArgListIdx.getIndex(), 4);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000271
272 for (MDNode *N : CU_Nodes->operands()) {
273 auto *CUNode = cast<DICompileUnit>(N);
274 for (auto *SP : CUNode->getSubprograms()) {
275 StringRef DisplayName = SP->getDisplayName();
Reid Klecknerdac21b42016-02-03 21:15:48 +0000276 OS.AddComment("Type record length");
277 OS.EmitIntValue(2 + sizeof(FuncId) + DisplayName.size() + 1, 2);
278 OS.AddComment("Leaf type: LF_FUNC_ID");
279 OS.EmitIntValue(LF_FUNC_ID, 2);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000280
Reid Klecknerdac21b42016-02-03 21:15:48 +0000281 OS.AddComment("Scope type index");
282 OS.EmitIntValue(TypeIndex().getIndex(), 4);
283 OS.AddComment("Function type");
284 OS.EmitIntValue(VoidProcIdx.getIndex(), 4);
David Majnemer30579ec2016-02-02 23:18:23 +0000285 {
286 SmallString<32> NullTerminatedString(DisplayName);
287 if (NullTerminatedString.empty() || NullTerminatedString.back() != '\0')
288 NullTerminatedString.push_back('\0');
Reid Klecknerdac21b42016-02-03 21:15:48 +0000289 OS.AddComment("Function name");
290 OS.EmitBytes(NullTerminatedString);
David Majnemer30579ec2016-02-02 23:18:23 +0000291 }
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000292
293 TypeIndex FuncIdIdx = getNextTypeIndex();
294 SubprogramToFuncId.insert(std::make_pair(SP, FuncIdIdx));
295 }
296 }
297}
298
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000299void CodeViewDebug::emitInlineeLinesSubsection() {
300 if (InlinedSubprograms.empty())
301 return;
302
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000303 MCSymbol *InlineBegin = MMI->getContext().createTempSymbol(),
304 *InlineEnd = MMI->getContext().createTempSymbol();
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000305
306 OS.AddComment("Inlinee lines subsection");
307 OS.EmitIntValue(unsigned(ModuleSubstreamKind::InlineeLines), 4);
David Majnemer30579ec2016-02-02 23:18:23 +0000308 OS.AddComment("Subsection size");
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000309 OS.emitAbsoluteSymbolDiff(InlineEnd, InlineBegin, 4);
310 OS.EmitLabel(InlineBegin);
311
312 // We don't provide any extra file info.
313 // FIXME: Find out if debuggers use this info.
David Majnemer30579ec2016-02-02 23:18:23 +0000314 OS.AddComment("Inlinee lines signature");
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000315 OS.EmitIntValue(unsigned(InlineeLinesSignature::Normal), 4);
316
317 for (const DISubprogram *SP : InlinedSubprograms) {
David Majnemer30579ec2016-02-02 23:18:23 +0000318 OS.AddBlankLine();
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000319 TypeIndex TypeId = SubprogramToFuncId[SP];
320 unsigned FileId = maybeRecordFile(SP->getFile());
321 OS.AddComment("Inlined function " + SP->getDisplayName() + " starts at " +
322 SP->getFilename() + Twine(':') + Twine(SP->getLine()));
David Majnemer30579ec2016-02-02 23:18:23 +0000323 OS.AddBlankLine();
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000324 // The filechecksum table uses 8 byte entries for now, and file ids start at
325 // 1.
326 unsigned FileOffset = (FileId - 1) * 8;
David Majnemer30579ec2016-02-02 23:18:23 +0000327 OS.AddComment("Type index of inlined function");
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000328 OS.EmitIntValue(TypeId.getIndex(), 4);
David Majnemer30579ec2016-02-02 23:18:23 +0000329 OS.AddComment("Offset into filechecksum table");
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000330 OS.EmitIntValue(FileOffset, 4);
David Majnemer30579ec2016-02-02 23:18:23 +0000331 OS.AddComment("Starting line number");
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000332 OS.EmitIntValue(SP->getLine(), 4);
333 }
334
335 OS.EmitLabel(InlineEnd);
336}
337
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000338void CodeViewDebug::collectInlineSiteChildren(
339 SmallVectorImpl<unsigned> &Children, const FunctionInfo &FI,
340 const InlineSite &Site) {
341 for (const DILocation *ChildSiteLoc : Site.ChildSites) {
342 auto I = FI.InlineSites.find(ChildSiteLoc);
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000343 const InlineSite &ChildSite = I->second;
344 Children.push_back(ChildSite.SiteFuncId);
345 collectInlineSiteChildren(Children, FI, ChildSite);
346 }
347}
348
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000349void CodeViewDebug::emitInlinedCallSite(const FunctionInfo &FI,
350 const DILocation *InlinedAt,
351 const InlineSite &Site) {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000352 MCSymbol *InlineBegin = MMI->getContext().createTempSymbol(),
353 *InlineEnd = MMI->getContext().createTempSymbol();
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000354
355 assert(SubprogramToFuncId.count(Site.Inlinee));
356 TypeIndex InlineeIdx = SubprogramToFuncId[Site.Inlinee];
357
358 // SymbolRecord
Reid Klecknerdac21b42016-02-03 21:15:48 +0000359 OS.AddComment("Record length");
Reid Klecknereb3bcdd2016-02-03 21:24:42 +0000360 OS.emitAbsoluteSymbolDiff(InlineEnd, InlineBegin, 2); // RecordLength
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000361 OS.EmitLabel(InlineBegin);
Reid Klecknerdac21b42016-02-03 21:15:48 +0000362 OS.AddComment("Record kind: S_INLINESITE");
363 OS.EmitIntValue(SymbolRecordKind::S_INLINESITE, 2); // RecordKind
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000364
Reid Klecknerdac21b42016-02-03 21:15:48 +0000365 OS.AddComment("PtrParent");
366 OS.EmitIntValue(0, 4);
367 OS.AddComment("PtrEnd");
368 OS.EmitIntValue(0, 4);
369 OS.AddComment("Inlinee type index");
370 OS.EmitIntValue(InlineeIdx.getIndex(), 4);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000371
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000372 unsigned FileId = maybeRecordFile(Site.Inlinee->getFile());
373 unsigned StartLineNum = Site.Inlinee->getLine();
374 SmallVector<unsigned, 3> SecondaryFuncIds;
375 collectInlineSiteChildren(SecondaryFuncIds, FI, Site);
376
377 OS.EmitCVInlineLinetableDirective(Site.SiteFuncId, FileId, StartLineNum,
David Majnemerc9911f22016-02-02 19:22:34 +0000378 FI.Begin, FI.End, SecondaryFuncIds);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000379
380 OS.EmitLabel(InlineEnd);
381
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000382 for (const LocalVariable &Var : Site.InlinedLocals)
383 emitLocalVariable(Var);
384
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000385 // Recurse on child inlined call sites before closing the scope.
386 for (const DILocation *ChildSite : Site.ChildSites) {
387 auto I = FI.InlineSites.find(ChildSite);
388 assert(I != FI.InlineSites.end() &&
389 "child site not in function inline site map");
390 emitInlinedCallSite(FI, ChildSite, I->second);
391 }
392
393 // Close the scope.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000394 OS.AddComment("Record length");
395 OS.EmitIntValue(2, 2); // RecordLength
396 OS.AddComment("Record kind: S_INLINESITE_END");
397 OS.EmitIntValue(SymbolRecordKind::S_INLINESITE_END, 2); // RecordKind
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000398}
399
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000400static void emitNullTerminatedString(MCStreamer &OS, StringRef S) {
401 SmallString<32> NullTerminatedString(S);
402 if (NullTerminatedString.empty() || NullTerminatedString.back() != '\0')
403 NullTerminatedString.push_back('\0');
404 OS.EmitBytes(NullTerminatedString);
405}
406
Reid Kleckner2214ed82016-01-29 00:49:42 +0000407void CodeViewDebug::emitDebugInfoForFunction(const Function *GV,
408 FunctionInfo &FI) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000409 // For each function there is a separate subsection
410 // which holds the PC to file:line table.
411 const MCSymbol *Fn = Asm->getSymbol(GV);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000412 assert(Fn);
Timur Iskhodzhanov8499a122014-03-26 09:50:36 +0000413
Duncan P. N. Exon Smith23e56ec2015-03-20 19:50:00 +0000414 StringRef FuncName;
Duncan P. N. Exon Smith2fbe1352015-04-20 22:10:08 +0000415 if (auto *SP = getDISubprogram(GV))
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000416 FuncName = SP->getDisplayName();
Duncan P. N. Exon Smith23e56ec2015-03-20 19:50:00 +0000417
Reid Kleckner3c0ff982016-01-14 00:12:54 +0000418 // If our DISubprogram name is empty, use the mangled name.
Reid Kleckner72e2ba72016-01-13 19:32:35 +0000419 if (FuncName.empty())
420 FuncName = GlobalValue::getRealLinkageName(GV->getName());
Reid Kleckner3c0ff982016-01-14 00:12:54 +0000421
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000422 // Emit a symbol subsection, required by VS2012+ to find function boundaries.
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000423 MCSymbol *SymbolsBegin = MMI->getContext().createTempSymbol(),
424 *SymbolsEnd = MMI->getContext().createTempSymbol();
Reid Klecknerdac21b42016-02-03 21:15:48 +0000425 OS.AddComment("Symbol subsection for " + Twine(FuncName));
426 OS.EmitIntValue(unsigned(ModuleSubstreamKind::Symbols), 4);
427 OS.AddComment("Subsection size");
Reid Klecknereb3bcdd2016-02-03 21:24:42 +0000428 OS.emitAbsoluteSymbolDiff(SymbolsEnd, SymbolsBegin, 4);
Reid Klecknerdac21b42016-02-03 21:15:48 +0000429 OS.EmitLabel(SymbolsBegin);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000430 {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000431 MCSymbol *ProcRecordBegin = MMI->getContext().createTempSymbol(),
432 *ProcRecordEnd = MMI->getContext().createTempSymbol();
Reid Klecknerdac21b42016-02-03 21:15:48 +0000433 OS.AddComment("Record length");
Reid Klecknereb3bcdd2016-02-03 21:24:42 +0000434 OS.emitAbsoluteSymbolDiff(ProcRecordEnd, ProcRecordBegin, 2);
Reid Klecknerdac21b42016-02-03 21:15:48 +0000435 OS.EmitLabel(ProcRecordBegin);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000436
Reid Klecknerdac21b42016-02-03 21:15:48 +0000437 OS.AddComment("Record kind: S_GPROC32_ID");
438 OS.EmitIntValue(unsigned(SymbolRecordKind::S_GPROC32_ID), 2);
Reid Kleckner6b3faef2016-01-13 23:44:57 +0000439
David Majnemer30579ec2016-02-02 23:18:23 +0000440 // These fields are filled in by tools like CVPACK which run after the fact.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000441 OS.AddComment("PtrParent");
442 OS.EmitIntValue(0, 4);
443 OS.AddComment("PtrEnd");
444 OS.EmitIntValue(0, 4);
445 OS.AddComment("PtrNext");
446 OS.EmitIntValue(0, 4);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000447 // This is the important bit that tells the debugger where the function
448 // code is located and what's its size:
Reid Klecknerdac21b42016-02-03 21:15:48 +0000449 OS.AddComment("Code size");
Reid Klecknereb3bcdd2016-02-03 21:24:42 +0000450 OS.emitAbsoluteSymbolDiff(FI.End, Fn, 4);
Reid Klecknerdac21b42016-02-03 21:15:48 +0000451 OS.AddComment("Offset after prologue");
452 OS.EmitIntValue(0, 4);
453 OS.AddComment("Offset before epilogue");
454 OS.EmitIntValue(0, 4);
455 OS.AddComment("Function type index");
456 OS.EmitIntValue(0, 4);
457 OS.AddComment("Function section relative address");
458 OS.EmitCOFFSecRel32(Fn);
459 OS.AddComment("Function section index");
460 OS.EmitCOFFSectionIndex(Fn);
461 OS.AddComment("Flags");
462 OS.EmitIntValue(0, 1);
Timur Iskhodzhanova11b32b2014-11-12 20:10:09 +0000463 // Emit the function display name as a null-terminated string.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000464 OS.AddComment("Function name");
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000465 emitNullTerminatedString(OS, FuncName);
Reid Klecknerdac21b42016-02-03 21:15:48 +0000466 OS.EmitLabel(ProcRecordEnd);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000467
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000468 for (const LocalVariable &Var : FI.Locals)
469 emitLocalVariable(Var);
470
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000471 // Emit inlined call site information. Only emit functions inlined directly
472 // into the parent function. We'll emit the other sites recursively as part
473 // of their parent inline site.
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000474 for (const DILocation *InlinedAt : FI.ChildSites) {
475 auto I = FI.InlineSites.find(InlinedAt);
476 assert(I != FI.InlineSites.end() &&
477 "child site not in function inline site map");
478 emitInlinedCallSite(FI, InlinedAt, I->second);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000479 }
480
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000481 // We're done with this function.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000482 OS.AddComment("Record length");
483 OS.EmitIntValue(0x0002, 2);
484 OS.AddComment("Record kind: S_PROC_ID_END");
485 OS.EmitIntValue(unsigned(SymbolRecordKind::S_PROC_ID_END), 2);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000486 }
Reid Klecknerdac21b42016-02-03 21:15:48 +0000487 OS.EmitLabel(SymbolsEnd);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000488 // Every subsection must be aligned to a 4-byte boundary.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000489 OS.EmitValueToAlignment(4);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000490
Reid Kleckner2214ed82016-01-29 00:49:42 +0000491 // We have an assembler directive that takes care of the whole line table.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000492 OS.EmitCVLinetableDirective(FI.FuncId, Fn, FI.End);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000493}
494
Reid Kleckner876330d2016-02-12 21:48:30 +0000495CodeViewDebug::LocalVarDefRange
496CodeViewDebug::createDefRangeMem(uint16_t CVRegister, int Offset) {
497 LocalVarDefRange DR;
Aaron Ballmanc6a2f212016-02-16 15:35:51 +0000498 DR.InMemory = -1;
Reid Kleckner876330d2016-02-12 21:48:30 +0000499 DR.DataOffset = Offset;
500 assert(DR.DataOffset == Offset && "truncation");
501 DR.StructOffset = 0;
502 DR.CVRegister = CVRegister;
503 return DR;
504}
505
506CodeViewDebug::LocalVarDefRange
507CodeViewDebug::createDefRangeReg(uint16_t CVRegister) {
508 LocalVarDefRange DR;
509 DR.InMemory = 0;
510 DR.DataOffset = 0;
511 DR.StructOffset = 0;
512 DR.CVRegister = CVRegister;
513 return DR;
514}
515
516void CodeViewDebug::collectVariableInfoFromMMITable(
517 DenseSet<InlinedVariable> &Processed) {
518 const TargetSubtargetInfo &TSI = Asm->MF->getSubtarget();
519 const TargetFrameLowering *TFI = TSI.getFrameLowering();
520 const TargetRegisterInfo *TRI = TSI.getRegisterInfo();
521
522 for (const MachineModuleInfo::VariableDbgInfo &VI :
523 MMI->getVariableDbgInfo()) {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000524 if (!VI.Var)
525 continue;
526 assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) &&
527 "Expected inlined-at fields to agree");
528
Reid Kleckner876330d2016-02-12 21:48:30 +0000529 Processed.insert(InlinedVariable(VI.Var, VI.Loc->getInlinedAt()));
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000530 LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc);
531
532 // If variable scope is not found then skip this variable.
533 if (!Scope)
534 continue;
535
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000536 // Get the frame register used and the offset.
537 unsigned FrameReg = 0;
Reid Kleckner876330d2016-02-12 21:48:30 +0000538 int FrameOffset = TFI->getFrameIndexReference(*Asm->MF, VI.Slot, FrameReg);
539 uint16_t CVReg = TRI->getCodeViewRegNum(FrameReg);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000540
541 // Calculate the label ranges.
Reid Kleckner876330d2016-02-12 21:48:30 +0000542 LocalVarDefRange DefRange = createDefRangeMem(CVReg, FrameOffset);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000543 for (const InsnRange &Range : Scope->getRanges()) {
544 const MCSymbol *Begin = getLabelBeforeInsn(Range.first);
545 const MCSymbol *End = getLabelAfterInsn(Range.second);
Reid Kleckner876330d2016-02-12 21:48:30 +0000546 End = End ? End : Asm->getFunctionEnd();
547 DefRange.Ranges.emplace_back(Begin, End);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000548 }
549
Reid Kleckner876330d2016-02-12 21:48:30 +0000550 LocalVariable Var;
551 Var.DIVar = VI.Var;
552 Var.DefRanges.emplace_back(std::move(DefRange));
553 recordLocalVariable(std::move(Var), VI.Loc->getInlinedAt());
554 }
555}
556
557void CodeViewDebug::collectVariableInfo(const DISubprogram *SP) {
558 DenseSet<InlinedVariable> Processed;
559 // Grab the variable info that was squirreled away in the MMI side-table.
560 collectVariableInfoFromMMITable(Processed);
561
562 const TargetRegisterInfo *TRI = Asm->MF->getSubtarget().getRegisterInfo();
563
564 for (const auto &I : DbgValues) {
565 InlinedVariable IV = I.first;
566 if (Processed.count(IV))
567 continue;
568 const DILocalVariable *DIVar = IV.first;
569 const DILocation *InlinedAt = IV.second;
570
571 // Instruction ranges, specifying where IV is accessible.
572 const auto &Ranges = I.second;
573
574 LexicalScope *Scope = nullptr;
575 if (InlinedAt)
576 Scope = LScopes.findInlinedScope(DIVar->getScope(), InlinedAt);
577 else
578 Scope = LScopes.findLexicalScope(DIVar->getScope());
579 // If variable scope is not found then skip this variable.
580 if (!Scope)
581 continue;
582
583 LocalVariable Var;
584 Var.DIVar = DIVar;
585
586 // Calculate the definition ranges.
587 for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) {
588 const InsnRange &Range = *I;
589 const MachineInstr *DVInst = Range.first;
590 assert(DVInst->isDebugValue() && "Invalid History entry");
591 const DIExpression *DIExpr = DVInst->getDebugExpression();
592
593 // Bail if there is a complex DWARF expression for now.
594 if (DIExpr && DIExpr->getNumElements() > 0)
595 continue;
596
Reid Kleckner9a593ee2016-02-16 21:49:26 +0000597 // Bail if operand 0 is not a valid register. This means the variable is a
598 // simple constant, or is described by a complex expression.
599 // FIXME: Find a way to represent constant variables, since they are
600 // relatively common.
601 unsigned Reg =
602 DVInst->getOperand(0).isReg() ? DVInst->getOperand(0).getReg() : 0;
603 if (Reg == 0)
Reid Kleckner6e0d5f52016-02-16 21:14:51 +0000604 continue;
605
Reid Kleckner876330d2016-02-12 21:48:30 +0000606 // Handle the two cases we can handle: indirect in memory and in register.
607 bool IsIndirect = DVInst->getOperand(1).isImm();
608 unsigned CVReg = TRI->getCodeViewRegNum(DVInst->getOperand(0).getReg());
609 {
610 LocalVarDefRange DefRange;
611 if (IsIndirect) {
612 int64_t Offset = DVInst->getOperand(1).getImm();
613 DefRange = createDefRangeMem(CVReg, Offset);
614 } else {
615 DefRange = createDefRangeReg(CVReg);
616 }
617 if (Var.DefRanges.empty() ||
618 Var.DefRanges.back().isDifferentLocation(DefRange)) {
619 Var.DefRanges.emplace_back(std::move(DefRange));
620 }
621 }
622
623 // Compute the label range.
624 const MCSymbol *Begin = getLabelBeforeInsn(Range.first);
625 const MCSymbol *End = getLabelAfterInsn(Range.second);
626 if (!End) {
627 if (std::next(I) != E)
628 End = getLabelBeforeInsn(std::next(I)->first);
629 else
630 End = Asm->getFunctionEnd();
631 }
632
633 // If the last range end is our begin, just extend the last range.
634 // Otherwise make a new range.
635 SmallVectorImpl<std::pair<const MCSymbol *, const MCSymbol *>> &Ranges =
636 Var.DefRanges.back().Ranges;
637 if (!Ranges.empty() && Ranges.back().second == Begin)
638 Ranges.back().second = End;
639 else
640 Ranges.emplace_back(Begin, End);
641
642 // FIXME: Do more range combining.
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000643 }
Reid Kleckner876330d2016-02-12 21:48:30 +0000644
645 recordLocalVariable(std::move(Var), InlinedAt);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000646 }
647}
648
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000649void CodeViewDebug::beginFunction(const MachineFunction *MF) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000650 assert(!CurFn && "Can't process two functions at once!");
651
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000652 if (!Asm || !MMI->hasDebugInfo())
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000653 return;
654
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000655 DebugHandlerBase::beginFunction(MF);
656
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000657 const Function *GV = MF->getFunction();
658 assert(FnDebugInfo.count(GV) == false);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000659 CurFn = &FnDebugInfo[GV];
Reid Kleckner2214ed82016-01-29 00:49:42 +0000660 CurFn->FuncId = NextFuncId++;
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000661 CurFn->Begin = Asm->getFunctionBegin();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000662
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000663 // Find the end of the function prolog. First known non-DBG_VALUE and
664 // non-frame setup location marks the beginning of the function body.
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000665 // FIXME: is there a simpler a way to do this? Can we just search
666 // for the first instruction of the function, not the last of the prolog?
667 DebugLoc PrologEndLoc;
668 bool EmptyPrologue = true;
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000669 for (const auto &MBB : *MF) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000670 for (const auto &MI : MBB) {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000671 if (!MI.isDebugValue() && !MI.getFlag(MachineInstr::FrameSetup) &&
672 MI.getDebugLoc()) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000673 PrologEndLoc = MI.getDebugLoc();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000674 break;
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000675 } else if (!MI.isDebugValue()) {
676 EmptyPrologue = false;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000677 }
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000678 }
679 }
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000680
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000681 // Record beginning of function if we have a non-empty prologue.
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000682 if (PrologEndLoc && !EmptyPrologue) {
683 DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000684 maybeRecordLocation(FnStartDL, MF);
685 }
686}
687
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000688void CodeViewDebug::emitLocalVariable(const LocalVariable &Var) {
689 // LocalSym record, see SymbolRecord.h for more info.
690 MCSymbol *LocalBegin = MMI->getContext().createTempSymbol(),
691 *LocalEnd = MMI->getContext().createTempSymbol();
692 OS.AddComment("Record length");
693 OS.emitAbsoluteSymbolDiff(LocalEnd, LocalBegin, 2);
694 OS.EmitLabel(LocalBegin);
695
696 OS.AddComment("Record kind: S_LOCAL");
697 OS.EmitIntValue(unsigned(SymbolRecordKind::S_LOCAL), 2);
698
699 uint16_t Flags = 0;
700 if (Var.DIVar->isParameter())
701 Flags |= LocalSym::IsParameter;
Reid Kleckner876330d2016-02-12 21:48:30 +0000702 if (Var.DefRanges.empty())
703 Flags |= LocalSym::IsOptimizedOut;
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000704
705 OS.AddComment("TypeIndex");
706 OS.EmitIntValue(TypeIndex::Int32().getIndex(), 4);
707 OS.AddComment("Flags");
708 OS.EmitIntValue(Flags, 2);
709 emitNullTerminatedString(OS, Var.DIVar->getName());
710 OS.EmitLabel(LocalEnd);
711
Reid Kleckner876330d2016-02-12 21:48:30 +0000712 // Calculate the on disk prefix of the appropriate def range record. The
713 // records and on disk formats are described in SymbolRecords.h. BytePrefix
714 // should be big enough to hold all forms without memory allocation.
715 SmallString<20> BytePrefix;
716 for (const LocalVarDefRange &DefRange : Var.DefRanges) {
717 BytePrefix.clear();
718 // FIXME: Handle bitpieces.
719 if (DefRange.StructOffset != 0)
720 continue;
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000721
Reid Kleckner876330d2016-02-12 21:48:30 +0000722 if (DefRange.InMemory) {
723 DefRangeRegisterRelSym Sym{};
724 ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER_REL);
725 Sym.BaseRegister = DefRange.CVRegister;
726 Sym.Flags = 0; // Unclear what matters here.
727 Sym.BasePointerOffset = DefRange.DataOffset;
728 BytePrefix +=
729 StringRef(reinterpret_cast<const char *>(&SymKind), sizeof(SymKind));
730 BytePrefix += StringRef(reinterpret_cast<const char *>(&Sym),
731 sizeof(Sym) - sizeof(LocalVariableAddrRange));
732 } else {
733 assert(DefRange.DataOffset == 0 && "unexpected offset into register");
734 DefRangeRegisterSym Sym{};
735 ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER);
736 Sym.Register = DefRange.CVRegister;
737 Sym.MayHaveNoName = 0; // Unclear what matters here.
738 BytePrefix +=
739 StringRef(reinterpret_cast<const char *>(&SymKind), sizeof(SymKind));
740 BytePrefix += StringRef(reinterpret_cast<const char *>(&Sym),
741 sizeof(Sym) - sizeof(LocalVariableAddrRange));
742 }
743 OS.EmitCVDefRangeDirective(DefRange.Ranges, BytePrefix);
744 }
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000745}
746
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000747void CodeViewDebug::endFunction(const MachineFunction *MF) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000748 if (!Asm || !CurFn) // We haven't created any debug info for this function.
749 return;
750
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000751 const Function *GV = MF->getFunction();
Yaron Keren6d3194f2014-06-20 10:26:56 +0000752 assert(FnDebugInfo.count(GV));
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000753 assert(CurFn == &FnDebugInfo[GV]);
754
Reid Kleckner876330d2016-02-12 21:48:30 +0000755 collectVariableInfo(getDISubprogram(GV));
756
757 DebugHandlerBase::endFunction(MF);
758
Reid Kleckner2214ed82016-01-29 00:49:42 +0000759 // Don't emit anything if we don't have any line tables.
760 if (!CurFn->HaveLineInfo) {
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000761 FnDebugInfo.erase(GV);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000762 CurFn = nullptr;
763 return;
Timur Iskhodzhanov8499a122014-03-26 09:50:36 +0000764 }
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000765
766 CurFn->End = Asm->getFunctionEnd();
767
Craig Topper353eda42014-04-24 06:44:33 +0000768 CurFn = nullptr;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000769}
770
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000771void CodeViewDebug::beginInstruction(const MachineInstr *MI) {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000772 DebugHandlerBase::beginInstruction(MI);
773
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000774 // Ignore DBG_VALUE locations and function prologue.
775 if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup))
776 return;
777 DebugLoc DL = MI->getDebugLoc();
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000778 if (DL == PrevInstLoc || !DL)
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000779 return;
780 maybeRecordLocation(DL, Asm->MF);
781}