blob: 233bce41b1964b55a67cbc8a7170a72afcacbf3c [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
David Majnemer12561252016-03-13 10:53:30 +0000400static void emitNullTerminatedString(MCStreamer &OS, StringRef S,
401 size_t MaxSize) {
402 S = S.substr(0, MaxSize);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000403 SmallString<32> NullTerminatedString(S);
David Majnemer12561252016-03-13 10:53:30 +0000404 NullTerminatedString.push_back('\0');
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000405 OS.EmitBytes(NullTerminatedString);
406}
407
Reid Kleckner2214ed82016-01-29 00:49:42 +0000408void CodeViewDebug::emitDebugInfoForFunction(const Function *GV,
409 FunctionInfo &FI) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000410 // For each function there is a separate subsection
411 // which holds the PC to file:line table.
412 const MCSymbol *Fn = Asm->getSymbol(GV);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000413 assert(Fn);
Timur Iskhodzhanov8499a122014-03-26 09:50:36 +0000414
Duncan P. N. Exon Smith23e56ec2015-03-20 19:50:00 +0000415 StringRef FuncName;
Pete Cooperadebb932016-03-11 02:14:16 +0000416 if (auto *SP = GV->getSubprogram())
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000417 FuncName = SP->getDisplayName();
Duncan P. N. Exon Smith23e56ec2015-03-20 19:50:00 +0000418
Reid Kleckner3c0ff982016-01-14 00:12:54 +0000419 // If our DISubprogram name is empty, use the mangled name.
Reid Kleckner72e2ba72016-01-13 19:32:35 +0000420 if (FuncName.empty())
421 FuncName = GlobalValue::getRealLinkageName(GV->getName());
Reid Kleckner3c0ff982016-01-14 00:12:54 +0000422
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000423 // Emit a symbol subsection, required by VS2012+ to find function boundaries.
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000424 MCSymbol *SymbolsBegin = MMI->getContext().createTempSymbol(),
425 *SymbolsEnd = MMI->getContext().createTempSymbol();
Reid Klecknerdac21b42016-02-03 21:15:48 +0000426 OS.AddComment("Symbol subsection for " + Twine(FuncName));
427 OS.EmitIntValue(unsigned(ModuleSubstreamKind::Symbols), 4);
428 OS.AddComment("Subsection size");
Reid Klecknereb3bcdd2016-02-03 21:24:42 +0000429 OS.emitAbsoluteSymbolDiff(SymbolsEnd, SymbolsBegin, 4);
Reid Klecknerdac21b42016-02-03 21:15:48 +0000430 OS.EmitLabel(SymbolsBegin);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000431 {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000432 MCSymbol *ProcRecordBegin = MMI->getContext().createTempSymbol(),
433 *ProcRecordEnd = MMI->getContext().createTempSymbol();
Reid Klecknerdac21b42016-02-03 21:15:48 +0000434 OS.AddComment("Record length");
Reid Klecknereb3bcdd2016-02-03 21:24:42 +0000435 OS.emitAbsoluteSymbolDiff(ProcRecordEnd, ProcRecordBegin, 2);
Reid Klecknerdac21b42016-02-03 21:15:48 +0000436 OS.EmitLabel(ProcRecordBegin);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000437
Reid Klecknerdac21b42016-02-03 21:15:48 +0000438 OS.AddComment("Record kind: S_GPROC32_ID");
439 OS.EmitIntValue(unsigned(SymbolRecordKind::S_GPROC32_ID), 2);
Reid Kleckner6b3faef2016-01-13 23:44:57 +0000440
David Majnemer30579ec2016-02-02 23:18:23 +0000441 // These fields are filled in by tools like CVPACK which run after the fact.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000442 OS.AddComment("PtrParent");
443 OS.EmitIntValue(0, 4);
444 OS.AddComment("PtrEnd");
445 OS.EmitIntValue(0, 4);
446 OS.AddComment("PtrNext");
447 OS.EmitIntValue(0, 4);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000448 // This is the important bit that tells the debugger where the function
449 // code is located and what's its size:
Reid Klecknerdac21b42016-02-03 21:15:48 +0000450 OS.AddComment("Code size");
Reid Klecknereb3bcdd2016-02-03 21:24:42 +0000451 OS.emitAbsoluteSymbolDiff(FI.End, Fn, 4);
Reid Klecknerdac21b42016-02-03 21:15:48 +0000452 OS.AddComment("Offset after prologue");
453 OS.EmitIntValue(0, 4);
454 OS.AddComment("Offset before epilogue");
455 OS.EmitIntValue(0, 4);
456 OS.AddComment("Function type index");
457 OS.EmitIntValue(0, 4);
458 OS.AddComment("Function section relative address");
459 OS.EmitCOFFSecRel32(Fn);
460 OS.AddComment("Function section index");
461 OS.EmitCOFFSectionIndex(Fn);
462 OS.AddComment("Flags");
463 OS.EmitIntValue(0, 1);
Timur Iskhodzhanova11b32b2014-11-12 20:10:09 +0000464 // Emit the function display name as a null-terminated string.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000465 OS.AddComment("Function name");
David Majnemer12561252016-03-13 10:53:30 +0000466 // Truncate the name so we won't overflow the record length field.
467 emitNullTerminatedString(OS, FuncName, 0xffd9);
Reid Klecknerdac21b42016-02-03 21:15:48 +0000468 OS.EmitLabel(ProcRecordEnd);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000469
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000470 for (const LocalVariable &Var : FI.Locals)
471 emitLocalVariable(Var);
472
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000473 // Emit inlined call site information. Only emit functions inlined directly
474 // into the parent function. We'll emit the other sites recursively as part
475 // of their parent inline site.
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000476 for (const DILocation *InlinedAt : FI.ChildSites) {
477 auto I = FI.InlineSites.find(InlinedAt);
478 assert(I != FI.InlineSites.end() &&
479 "child site not in function inline site map");
480 emitInlinedCallSite(FI, InlinedAt, I->second);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000481 }
482
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000483 // We're done with this function.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000484 OS.AddComment("Record length");
485 OS.EmitIntValue(0x0002, 2);
486 OS.AddComment("Record kind: S_PROC_ID_END");
487 OS.EmitIntValue(unsigned(SymbolRecordKind::S_PROC_ID_END), 2);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000488 }
Reid Klecknerdac21b42016-02-03 21:15:48 +0000489 OS.EmitLabel(SymbolsEnd);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000490 // Every subsection must be aligned to a 4-byte boundary.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000491 OS.EmitValueToAlignment(4);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000492
Reid Kleckner2214ed82016-01-29 00:49:42 +0000493 // We have an assembler directive that takes care of the whole line table.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000494 OS.EmitCVLinetableDirective(FI.FuncId, Fn, FI.End);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000495}
496
Reid Kleckner876330d2016-02-12 21:48:30 +0000497CodeViewDebug::LocalVarDefRange
498CodeViewDebug::createDefRangeMem(uint16_t CVRegister, int Offset) {
499 LocalVarDefRange DR;
Aaron Ballmanc6a2f212016-02-16 15:35:51 +0000500 DR.InMemory = -1;
Reid Kleckner876330d2016-02-12 21:48:30 +0000501 DR.DataOffset = Offset;
502 assert(DR.DataOffset == Offset && "truncation");
503 DR.StructOffset = 0;
504 DR.CVRegister = CVRegister;
505 return DR;
506}
507
508CodeViewDebug::LocalVarDefRange
509CodeViewDebug::createDefRangeReg(uint16_t CVRegister) {
510 LocalVarDefRange DR;
511 DR.InMemory = 0;
512 DR.DataOffset = 0;
513 DR.StructOffset = 0;
514 DR.CVRegister = CVRegister;
515 return DR;
516}
517
518void CodeViewDebug::collectVariableInfoFromMMITable(
519 DenseSet<InlinedVariable> &Processed) {
520 const TargetSubtargetInfo &TSI = Asm->MF->getSubtarget();
521 const TargetFrameLowering *TFI = TSI.getFrameLowering();
522 const TargetRegisterInfo *TRI = TSI.getRegisterInfo();
523
524 for (const MachineModuleInfo::VariableDbgInfo &VI :
525 MMI->getVariableDbgInfo()) {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000526 if (!VI.Var)
527 continue;
528 assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) &&
529 "Expected inlined-at fields to agree");
530
Reid Kleckner876330d2016-02-12 21:48:30 +0000531 Processed.insert(InlinedVariable(VI.Var, VI.Loc->getInlinedAt()));
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000532 LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc);
533
534 // If variable scope is not found then skip this variable.
535 if (!Scope)
536 continue;
537
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000538 // Get the frame register used and the offset.
539 unsigned FrameReg = 0;
Reid Kleckner876330d2016-02-12 21:48:30 +0000540 int FrameOffset = TFI->getFrameIndexReference(*Asm->MF, VI.Slot, FrameReg);
541 uint16_t CVReg = TRI->getCodeViewRegNum(FrameReg);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000542
543 // Calculate the label ranges.
Reid Kleckner876330d2016-02-12 21:48:30 +0000544 LocalVarDefRange DefRange = createDefRangeMem(CVReg, FrameOffset);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000545 for (const InsnRange &Range : Scope->getRanges()) {
546 const MCSymbol *Begin = getLabelBeforeInsn(Range.first);
547 const MCSymbol *End = getLabelAfterInsn(Range.second);
Reid Kleckner876330d2016-02-12 21:48:30 +0000548 End = End ? End : Asm->getFunctionEnd();
549 DefRange.Ranges.emplace_back(Begin, End);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000550 }
551
Reid Kleckner876330d2016-02-12 21:48:30 +0000552 LocalVariable Var;
553 Var.DIVar = VI.Var;
554 Var.DefRanges.emplace_back(std::move(DefRange));
555 recordLocalVariable(std::move(Var), VI.Loc->getInlinedAt());
556 }
557}
558
559void CodeViewDebug::collectVariableInfo(const DISubprogram *SP) {
560 DenseSet<InlinedVariable> Processed;
561 // Grab the variable info that was squirreled away in the MMI side-table.
562 collectVariableInfoFromMMITable(Processed);
563
564 const TargetRegisterInfo *TRI = Asm->MF->getSubtarget().getRegisterInfo();
565
566 for (const auto &I : DbgValues) {
567 InlinedVariable IV = I.first;
568 if (Processed.count(IV))
569 continue;
570 const DILocalVariable *DIVar = IV.first;
571 const DILocation *InlinedAt = IV.second;
572
573 // Instruction ranges, specifying where IV is accessible.
574 const auto &Ranges = I.second;
575
576 LexicalScope *Scope = nullptr;
577 if (InlinedAt)
578 Scope = LScopes.findInlinedScope(DIVar->getScope(), InlinedAt);
579 else
580 Scope = LScopes.findLexicalScope(DIVar->getScope());
581 // If variable scope is not found then skip this variable.
582 if (!Scope)
583 continue;
584
585 LocalVariable Var;
586 Var.DIVar = DIVar;
587
588 // Calculate the definition ranges.
589 for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) {
590 const InsnRange &Range = *I;
591 const MachineInstr *DVInst = Range.first;
592 assert(DVInst->isDebugValue() && "Invalid History entry");
593 const DIExpression *DIExpr = DVInst->getDebugExpression();
594
595 // Bail if there is a complex DWARF expression for now.
596 if (DIExpr && DIExpr->getNumElements() > 0)
597 continue;
598
Reid Kleckner9a593ee2016-02-16 21:49:26 +0000599 // Bail if operand 0 is not a valid register. This means the variable is a
600 // simple constant, or is described by a complex expression.
601 // FIXME: Find a way to represent constant variables, since they are
602 // relatively common.
603 unsigned Reg =
604 DVInst->getOperand(0).isReg() ? DVInst->getOperand(0).getReg() : 0;
605 if (Reg == 0)
Reid Kleckner6e0d5f52016-02-16 21:14:51 +0000606 continue;
607
Reid Kleckner876330d2016-02-12 21:48:30 +0000608 // Handle the two cases we can handle: indirect in memory and in register.
609 bool IsIndirect = DVInst->getOperand(1).isImm();
610 unsigned CVReg = TRI->getCodeViewRegNum(DVInst->getOperand(0).getReg());
611 {
612 LocalVarDefRange DefRange;
613 if (IsIndirect) {
614 int64_t Offset = DVInst->getOperand(1).getImm();
615 DefRange = createDefRangeMem(CVReg, Offset);
616 } else {
617 DefRange = createDefRangeReg(CVReg);
618 }
619 if (Var.DefRanges.empty() ||
620 Var.DefRanges.back().isDifferentLocation(DefRange)) {
621 Var.DefRanges.emplace_back(std::move(DefRange));
622 }
623 }
624
625 // Compute the label range.
626 const MCSymbol *Begin = getLabelBeforeInsn(Range.first);
627 const MCSymbol *End = getLabelAfterInsn(Range.second);
628 if (!End) {
629 if (std::next(I) != E)
630 End = getLabelBeforeInsn(std::next(I)->first);
631 else
632 End = Asm->getFunctionEnd();
633 }
634
635 // If the last range end is our begin, just extend the last range.
636 // Otherwise make a new range.
637 SmallVectorImpl<std::pair<const MCSymbol *, const MCSymbol *>> &Ranges =
638 Var.DefRanges.back().Ranges;
639 if (!Ranges.empty() && Ranges.back().second == Begin)
640 Ranges.back().second = End;
641 else
642 Ranges.emplace_back(Begin, End);
643
644 // FIXME: Do more range combining.
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000645 }
Reid Kleckner876330d2016-02-12 21:48:30 +0000646
647 recordLocalVariable(std::move(Var), InlinedAt);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000648 }
649}
650
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000651void CodeViewDebug::beginFunction(const MachineFunction *MF) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000652 assert(!CurFn && "Can't process two functions at once!");
653
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000654 if (!Asm || !MMI->hasDebugInfo())
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000655 return;
656
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000657 DebugHandlerBase::beginFunction(MF);
658
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000659 const Function *GV = MF->getFunction();
660 assert(FnDebugInfo.count(GV) == false);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000661 CurFn = &FnDebugInfo[GV];
Reid Kleckner2214ed82016-01-29 00:49:42 +0000662 CurFn->FuncId = NextFuncId++;
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000663 CurFn->Begin = Asm->getFunctionBegin();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000664
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000665 // Find the end of the function prolog. First known non-DBG_VALUE and
666 // non-frame setup location marks the beginning of the function body.
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000667 // FIXME: is there a simpler a way to do this? Can we just search
668 // for the first instruction of the function, not the last of the prolog?
669 DebugLoc PrologEndLoc;
670 bool EmptyPrologue = true;
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000671 for (const auto &MBB : *MF) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000672 for (const auto &MI : MBB) {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000673 if (!MI.isDebugValue() && !MI.getFlag(MachineInstr::FrameSetup) &&
674 MI.getDebugLoc()) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000675 PrologEndLoc = MI.getDebugLoc();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000676 break;
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000677 } else if (!MI.isDebugValue()) {
678 EmptyPrologue = false;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000679 }
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000680 }
681 }
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000682
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000683 // Record beginning of function if we have a non-empty prologue.
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000684 if (PrologEndLoc && !EmptyPrologue) {
685 DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000686 maybeRecordLocation(FnStartDL, MF);
687 }
688}
689
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000690void CodeViewDebug::emitLocalVariable(const LocalVariable &Var) {
691 // LocalSym record, see SymbolRecord.h for more info.
692 MCSymbol *LocalBegin = MMI->getContext().createTempSymbol(),
693 *LocalEnd = MMI->getContext().createTempSymbol();
694 OS.AddComment("Record length");
695 OS.emitAbsoluteSymbolDiff(LocalEnd, LocalBegin, 2);
696 OS.EmitLabel(LocalBegin);
697
698 OS.AddComment("Record kind: S_LOCAL");
699 OS.EmitIntValue(unsigned(SymbolRecordKind::S_LOCAL), 2);
700
701 uint16_t Flags = 0;
702 if (Var.DIVar->isParameter())
703 Flags |= LocalSym::IsParameter;
Reid Kleckner876330d2016-02-12 21:48:30 +0000704 if (Var.DefRanges.empty())
705 Flags |= LocalSym::IsOptimizedOut;
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000706
707 OS.AddComment("TypeIndex");
708 OS.EmitIntValue(TypeIndex::Int32().getIndex(), 4);
709 OS.AddComment("Flags");
710 OS.EmitIntValue(Flags, 2);
David Majnemer12561252016-03-13 10:53:30 +0000711 // Truncate the name so we won't overflow the record length field.
712 emitNullTerminatedString(OS, Var.DIVar->getName(), 0xfff6);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000713 OS.EmitLabel(LocalEnd);
714
Reid Kleckner876330d2016-02-12 21:48:30 +0000715 // Calculate the on disk prefix of the appropriate def range record. The
716 // records and on disk formats are described in SymbolRecords.h. BytePrefix
717 // should be big enough to hold all forms without memory allocation.
718 SmallString<20> BytePrefix;
719 for (const LocalVarDefRange &DefRange : Var.DefRanges) {
720 BytePrefix.clear();
721 // FIXME: Handle bitpieces.
722 if (DefRange.StructOffset != 0)
723 continue;
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000724
Reid Kleckner876330d2016-02-12 21:48:30 +0000725 if (DefRange.InMemory) {
726 DefRangeRegisterRelSym Sym{};
727 ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER_REL);
728 Sym.BaseRegister = DefRange.CVRegister;
729 Sym.Flags = 0; // Unclear what matters here.
730 Sym.BasePointerOffset = DefRange.DataOffset;
731 BytePrefix +=
732 StringRef(reinterpret_cast<const char *>(&SymKind), sizeof(SymKind));
733 BytePrefix += StringRef(reinterpret_cast<const char *>(&Sym),
734 sizeof(Sym) - sizeof(LocalVariableAddrRange));
735 } else {
736 assert(DefRange.DataOffset == 0 && "unexpected offset into register");
737 DefRangeRegisterSym Sym{};
738 ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER);
739 Sym.Register = DefRange.CVRegister;
740 Sym.MayHaveNoName = 0; // Unclear what matters here.
741 BytePrefix +=
742 StringRef(reinterpret_cast<const char *>(&SymKind), sizeof(SymKind));
743 BytePrefix += StringRef(reinterpret_cast<const char *>(&Sym),
744 sizeof(Sym) - sizeof(LocalVariableAddrRange));
745 }
746 OS.EmitCVDefRangeDirective(DefRange.Ranges, BytePrefix);
747 }
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000748}
749
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000750void CodeViewDebug::endFunction(const MachineFunction *MF) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000751 if (!Asm || !CurFn) // We haven't created any debug info for this function.
752 return;
753
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000754 const Function *GV = MF->getFunction();
Yaron Keren6d3194f2014-06-20 10:26:56 +0000755 assert(FnDebugInfo.count(GV));
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000756 assert(CurFn == &FnDebugInfo[GV]);
757
Pete Cooperadebb932016-03-11 02:14:16 +0000758 collectVariableInfo(GV->getSubprogram());
Reid Kleckner876330d2016-02-12 21:48:30 +0000759
760 DebugHandlerBase::endFunction(MF);
761
Reid Kleckner2214ed82016-01-29 00:49:42 +0000762 // Don't emit anything if we don't have any line tables.
763 if (!CurFn->HaveLineInfo) {
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000764 FnDebugInfo.erase(GV);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000765 CurFn = nullptr;
766 return;
Timur Iskhodzhanov8499a122014-03-26 09:50:36 +0000767 }
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000768
769 CurFn->End = Asm->getFunctionEnd();
770
Craig Topper353eda42014-04-24 06:44:33 +0000771 CurFn = nullptr;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000772}
773
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000774void CodeViewDebug::beginInstruction(const MachineInstr *MI) {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000775 DebugHandlerBase::beginInstruction(MI);
776
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000777 // Ignore DBG_VALUE locations and function prologue.
778 if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup))
779 return;
780 DebugLoc DL = MI->getDebugLoc();
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000781 if (DL == PrevInstLoc || !DL)
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000782 return;
783 maybeRecordLocation(DL, Asm->MF);
784}