blob: 51a71dfdc23c86934e38d4343c071aba2743456d [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
David Majnemerb9456a52016-03-14 05:15:09 +0000234static void emitNullTerminatedSymbolName(MCStreamer &OS, StringRef S) {
235 // Microsoft's linker seems to have trouble with symbol names longer than
236 // 0xffd8 bytes.
237 S = S.substr(0, 0xffd8);
238 SmallString<32> NullTerminatedString(S);
239 NullTerminatedString.push_back('\0');
240 OS.EmitBytes(NullTerminatedString);
241}
242
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000243void CodeViewDebug::emitTypeInformation() {
244 // Start the .debug$T section with 0x4.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000245 OS.SwitchSection(Asm->getObjFileLowering().getCOFFDebugTypesSection());
246 OS.AddComment("Debug section magic");
247 OS.EmitIntValue(COFF::DEBUG_SECTION_MAGIC, 4);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000248
249 NamedMDNode *CU_Nodes =
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000250 MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000251 if (!CU_Nodes)
252 return;
253
254 // This type info currently only holds function ids for use with inline call
255 // frame info. All functions are assigned a simple 'void ()' type. Emit that
256 // type here.
257 TypeIndex ArgListIdx = getNextTypeIndex();
Reid Klecknerdac21b42016-02-03 21:15:48 +0000258 OS.AddComment("Type record length");
259 OS.EmitIntValue(2 + sizeof(ArgList), 2);
260 OS.AddComment("Leaf type: LF_ARGLIST");
261 OS.EmitIntValue(LF_ARGLIST, 2);
262 OS.AddComment("Number of arguments");
263 OS.EmitIntValue(0, 4);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000264
265 TypeIndex VoidProcIdx = getNextTypeIndex();
Reid Klecknerdac21b42016-02-03 21:15:48 +0000266 OS.AddComment("Type record length");
267 OS.EmitIntValue(2 + sizeof(ProcedureType), 2);
268 OS.AddComment("Leaf type: LF_PROCEDURE");
269 OS.EmitIntValue(LF_PROCEDURE, 2);
270 OS.AddComment("Return type index");
271 OS.EmitIntValue(TypeIndex::Void().getIndex(), 4);
272 OS.AddComment("Calling convention");
273 OS.EmitIntValue(char(CallingConvention::NearC), 1);
274 OS.AddComment("Function options");
275 OS.EmitIntValue(char(FunctionOptions::None), 1);
276 OS.AddComment("# of parameters");
277 OS.EmitIntValue(0, 2);
278 OS.AddComment("Argument list type index");
279 OS.EmitIntValue(ArgListIdx.getIndex(), 4);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000280
281 for (MDNode *N : CU_Nodes->operands()) {
282 auto *CUNode = cast<DICompileUnit>(N);
283 for (auto *SP : CUNode->getSubprograms()) {
284 StringRef DisplayName = SP->getDisplayName();
Reid Klecknerdac21b42016-02-03 21:15:48 +0000285 OS.AddComment("Type record length");
David Majnemerb9456a52016-03-14 05:15:09 +0000286 MCSymbol *FuncBegin = MMI->getContext().createTempSymbol(),
287 *FuncEnd = MMI->getContext().createTempSymbol();
288 OS.emitAbsoluteSymbolDiff(FuncEnd, FuncBegin, 2);
289 OS.EmitLabel(FuncBegin);
Reid Klecknerdac21b42016-02-03 21:15:48 +0000290 OS.AddComment("Leaf type: LF_FUNC_ID");
291 OS.EmitIntValue(LF_FUNC_ID, 2);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000292
Reid Klecknerdac21b42016-02-03 21:15:48 +0000293 OS.AddComment("Scope type index");
294 OS.EmitIntValue(TypeIndex().getIndex(), 4);
295 OS.AddComment("Function type");
296 OS.EmitIntValue(VoidProcIdx.getIndex(), 4);
David Majnemer30579ec2016-02-02 23:18:23 +0000297 {
Reid Klecknerdac21b42016-02-03 21:15:48 +0000298 OS.AddComment("Function name");
David Majnemerb9456a52016-03-14 05:15:09 +0000299 emitNullTerminatedSymbolName(OS, DisplayName);
David Majnemer30579ec2016-02-02 23:18:23 +0000300 }
David Majnemerb9456a52016-03-14 05:15:09 +0000301 OS.EmitLabel(FuncEnd);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000302
303 TypeIndex FuncIdIdx = getNextTypeIndex();
304 SubprogramToFuncId.insert(std::make_pair(SP, FuncIdIdx));
305 }
306 }
307}
308
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000309void CodeViewDebug::emitInlineeLinesSubsection() {
310 if (InlinedSubprograms.empty())
311 return;
312
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000313 MCSymbol *InlineBegin = MMI->getContext().createTempSymbol(),
314 *InlineEnd = MMI->getContext().createTempSymbol();
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000315
316 OS.AddComment("Inlinee lines subsection");
317 OS.EmitIntValue(unsigned(ModuleSubstreamKind::InlineeLines), 4);
David Majnemer30579ec2016-02-02 23:18:23 +0000318 OS.AddComment("Subsection size");
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000319 OS.emitAbsoluteSymbolDiff(InlineEnd, InlineBegin, 4);
320 OS.EmitLabel(InlineBegin);
321
322 // We don't provide any extra file info.
323 // FIXME: Find out if debuggers use this info.
David Majnemer30579ec2016-02-02 23:18:23 +0000324 OS.AddComment("Inlinee lines signature");
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000325 OS.EmitIntValue(unsigned(InlineeLinesSignature::Normal), 4);
326
327 for (const DISubprogram *SP : InlinedSubprograms) {
David Majnemer30579ec2016-02-02 23:18:23 +0000328 OS.AddBlankLine();
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000329 TypeIndex TypeId = SubprogramToFuncId[SP];
330 unsigned FileId = maybeRecordFile(SP->getFile());
331 OS.AddComment("Inlined function " + SP->getDisplayName() + " starts at " +
332 SP->getFilename() + Twine(':') + Twine(SP->getLine()));
David Majnemer30579ec2016-02-02 23:18:23 +0000333 OS.AddBlankLine();
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000334 // The filechecksum table uses 8 byte entries for now, and file ids start at
335 // 1.
336 unsigned FileOffset = (FileId - 1) * 8;
David Majnemer30579ec2016-02-02 23:18:23 +0000337 OS.AddComment("Type index of inlined function");
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000338 OS.EmitIntValue(TypeId.getIndex(), 4);
David Majnemer30579ec2016-02-02 23:18:23 +0000339 OS.AddComment("Offset into filechecksum table");
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000340 OS.EmitIntValue(FileOffset, 4);
David Majnemer30579ec2016-02-02 23:18:23 +0000341 OS.AddComment("Starting line number");
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000342 OS.EmitIntValue(SP->getLine(), 4);
343 }
344
345 OS.EmitLabel(InlineEnd);
346}
347
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000348void CodeViewDebug::collectInlineSiteChildren(
349 SmallVectorImpl<unsigned> &Children, const FunctionInfo &FI,
350 const InlineSite &Site) {
351 for (const DILocation *ChildSiteLoc : Site.ChildSites) {
352 auto I = FI.InlineSites.find(ChildSiteLoc);
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000353 const InlineSite &ChildSite = I->second;
354 Children.push_back(ChildSite.SiteFuncId);
355 collectInlineSiteChildren(Children, FI, ChildSite);
356 }
357}
358
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000359void CodeViewDebug::emitInlinedCallSite(const FunctionInfo &FI,
360 const DILocation *InlinedAt,
361 const InlineSite &Site) {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000362 MCSymbol *InlineBegin = MMI->getContext().createTempSymbol(),
363 *InlineEnd = MMI->getContext().createTempSymbol();
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000364
365 assert(SubprogramToFuncId.count(Site.Inlinee));
366 TypeIndex InlineeIdx = SubprogramToFuncId[Site.Inlinee];
367
368 // SymbolRecord
Reid Klecknerdac21b42016-02-03 21:15:48 +0000369 OS.AddComment("Record length");
Reid Klecknereb3bcdd2016-02-03 21:24:42 +0000370 OS.emitAbsoluteSymbolDiff(InlineEnd, InlineBegin, 2); // RecordLength
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000371 OS.EmitLabel(InlineBegin);
Reid Klecknerdac21b42016-02-03 21:15:48 +0000372 OS.AddComment("Record kind: S_INLINESITE");
373 OS.EmitIntValue(SymbolRecordKind::S_INLINESITE, 2); // RecordKind
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000374
Reid Klecknerdac21b42016-02-03 21:15:48 +0000375 OS.AddComment("PtrParent");
376 OS.EmitIntValue(0, 4);
377 OS.AddComment("PtrEnd");
378 OS.EmitIntValue(0, 4);
379 OS.AddComment("Inlinee type index");
380 OS.EmitIntValue(InlineeIdx.getIndex(), 4);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000381
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000382 unsigned FileId = maybeRecordFile(Site.Inlinee->getFile());
383 unsigned StartLineNum = Site.Inlinee->getLine();
384 SmallVector<unsigned, 3> SecondaryFuncIds;
385 collectInlineSiteChildren(SecondaryFuncIds, FI, Site);
386
387 OS.EmitCVInlineLinetableDirective(Site.SiteFuncId, FileId, StartLineNum,
David Majnemerc9911f22016-02-02 19:22:34 +0000388 FI.Begin, FI.End, SecondaryFuncIds);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000389
390 OS.EmitLabel(InlineEnd);
391
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000392 for (const LocalVariable &Var : Site.InlinedLocals)
393 emitLocalVariable(Var);
394
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000395 // Recurse on child inlined call sites before closing the scope.
396 for (const DILocation *ChildSite : Site.ChildSites) {
397 auto I = FI.InlineSites.find(ChildSite);
398 assert(I != FI.InlineSites.end() &&
399 "child site not in function inline site map");
400 emitInlinedCallSite(FI, ChildSite, I->second);
401 }
402
403 // Close the scope.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000404 OS.AddComment("Record length");
405 OS.EmitIntValue(2, 2); // RecordLength
406 OS.AddComment("Record kind: S_INLINESITE_END");
407 OS.EmitIntValue(SymbolRecordKind::S_INLINESITE_END, 2); // RecordKind
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000408}
409
Reid Kleckner2214ed82016-01-29 00:49:42 +0000410void CodeViewDebug::emitDebugInfoForFunction(const Function *GV,
411 FunctionInfo &FI) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000412 // For each function there is a separate subsection
413 // which holds the PC to file:line table.
414 const MCSymbol *Fn = Asm->getSymbol(GV);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000415 assert(Fn);
Timur Iskhodzhanov8499a122014-03-26 09:50:36 +0000416
Duncan P. N. Exon Smith23e56ec2015-03-20 19:50:00 +0000417 StringRef FuncName;
Pete Cooperadebb932016-03-11 02:14:16 +0000418 if (auto *SP = GV->getSubprogram())
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000419 FuncName = SP->getDisplayName();
Duncan P. N. Exon Smith23e56ec2015-03-20 19:50:00 +0000420
Reid Kleckner3c0ff982016-01-14 00:12:54 +0000421 // If our DISubprogram name is empty, use the mangled name.
Reid Kleckner72e2ba72016-01-13 19:32:35 +0000422 if (FuncName.empty())
423 FuncName = GlobalValue::getRealLinkageName(GV->getName());
Reid Kleckner3c0ff982016-01-14 00:12:54 +0000424
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000425 // Emit a symbol subsection, required by VS2012+ to find function boundaries.
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000426 MCSymbol *SymbolsBegin = MMI->getContext().createTempSymbol(),
427 *SymbolsEnd = MMI->getContext().createTempSymbol();
Reid Klecknerdac21b42016-02-03 21:15:48 +0000428 OS.AddComment("Symbol subsection for " + Twine(FuncName));
429 OS.EmitIntValue(unsigned(ModuleSubstreamKind::Symbols), 4);
430 OS.AddComment("Subsection size");
Reid Klecknereb3bcdd2016-02-03 21:24:42 +0000431 OS.emitAbsoluteSymbolDiff(SymbolsEnd, SymbolsBegin, 4);
Reid Klecknerdac21b42016-02-03 21:15:48 +0000432 OS.EmitLabel(SymbolsBegin);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000433 {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000434 MCSymbol *ProcRecordBegin = MMI->getContext().createTempSymbol(),
435 *ProcRecordEnd = MMI->getContext().createTempSymbol();
Reid Klecknerdac21b42016-02-03 21:15:48 +0000436 OS.AddComment("Record length");
Reid Klecknereb3bcdd2016-02-03 21:24:42 +0000437 OS.emitAbsoluteSymbolDiff(ProcRecordEnd, ProcRecordBegin, 2);
Reid Klecknerdac21b42016-02-03 21:15:48 +0000438 OS.EmitLabel(ProcRecordBegin);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000439
Reid Klecknerdac21b42016-02-03 21:15:48 +0000440 OS.AddComment("Record kind: S_GPROC32_ID");
441 OS.EmitIntValue(unsigned(SymbolRecordKind::S_GPROC32_ID), 2);
Reid Kleckner6b3faef2016-01-13 23:44:57 +0000442
David Majnemer30579ec2016-02-02 23:18:23 +0000443 // These fields are filled in by tools like CVPACK which run after the fact.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000444 OS.AddComment("PtrParent");
445 OS.EmitIntValue(0, 4);
446 OS.AddComment("PtrEnd");
447 OS.EmitIntValue(0, 4);
448 OS.AddComment("PtrNext");
449 OS.EmitIntValue(0, 4);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000450 // This is the important bit that tells the debugger where the function
451 // code is located and what's its size:
Reid Klecknerdac21b42016-02-03 21:15:48 +0000452 OS.AddComment("Code size");
Reid Klecknereb3bcdd2016-02-03 21:24:42 +0000453 OS.emitAbsoluteSymbolDiff(FI.End, Fn, 4);
Reid Klecknerdac21b42016-02-03 21:15:48 +0000454 OS.AddComment("Offset after prologue");
455 OS.EmitIntValue(0, 4);
456 OS.AddComment("Offset before epilogue");
457 OS.EmitIntValue(0, 4);
458 OS.AddComment("Function type index");
459 OS.EmitIntValue(0, 4);
460 OS.AddComment("Function section relative address");
461 OS.EmitCOFFSecRel32(Fn);
462 OS.AddComment("Function section index");
463 OS.EmitCOFFSectionIndex(Fn);
464 OS.AddComment("Flags");
465 OS.EmitIntValue(0, 1);
Timur Iskhodzhanova11b32b2014-11-12 20:10:09 +0000466 // Emit the function display name as a null-terminated string.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000467 OS.AddComment("Function name");
David Majnemer12561252016-03-13 10:53:30 +0000468 // Truncate the name so we won't overflow the record length field.
David Majnemerb9456a52016-03-14 05:15:09 +0000469 emitNullTerminatedSymbolName(OS, FuncName);
Reid Klecknerdac21b42016-02-03 21:15:48 +0000470 OS.EmitLabel(ProcRecordEnd);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000471
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000472 for (const LocalVariable &Var : FI.Locals)
473 emitLocalVariable(Var);
474
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000475 // Emit inlined call site information. Only emit functions inlined directly
476 // into the parent function. We'll emit the other sites recursively as part
477 // of their parent inline site.
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000478 for (const DILocation *InlinedAt : FI.ChildSites) {
479 auto I = FI.InlineSites.find(InlinedAt);
480 assert(I != FI.InlineSites.end() &&
481 "child site not in function inline site map");
482 emitInlinedCallSite(FI, InlinedAt, I->second);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000483 }
484
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000485 // We're done with this function.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000486 OS.AddComment("Record length");
487 OS.EmitIntValue(0x0002, 2);
488 OS.AddComment("Record kind: S_PROC_ID_END");
489 OS.EmitIntValue(unsigned(SymbolRecordKind::S_PROC_ID_END), 2);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000490 }
Reid Klecknerdac21b42016-02-03 21:15:48 +0000491 OS.EmitLabel(SymbolsEnd);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000492 // Every subsection must be aligned to a 4-byte boundary.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000493 OS.EmitValueToAlignment(4);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000494
Reid Kleckner2214ed82016-01-29 00:49:42 +0000495 // We have an assembler directive that takes care of the whole line table.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000496 OS.EmitCVLinetableDirective(FI.FuncId, Fn, FI.End);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000497}
498
Reid Kleckner876330d2016-02-12 21:48:30 +0000499CodeViewDebug::LocalVarDefRange
500CodeViewDebug::createDefRangeMem(uint16_t CVRegister, int Offset) {
501 LocalVarDefRange DR;
Aaron Ballmanc6a2f212016-02-16 15:35:51 +0000502 DR.InMemory = -1;
Reid Kleckner876330d2016-02-12 21:48:30 +0000503 DR.DataOffset = Offset;
504 assert(DR.DataOffset == Offset && "truncation");
505 DR.StructOffset = 0;
506 DR.CVRegister = CVRegister;
507 return DR;
508}
509
510CodeViewDebug::LocalVarDefRange
511CodeViewDebug::createDefRangeReg(uint16_t CVRegister) {
512 LocalVarDefRange DR;
513 DR.InMemory = 0;
514 DR.DataOffset = 0;
515 DR.StructOffset = 0;
516 DR.CVRegister = CVRegister;
517 return DR;
518}
519
520void CodeViewDebug::collectVariableInfoFromMMITable(
521 DenseSet<InlinedVariable> &Processed) {
522 const TargetSubtargetInfo &TSI = Asm->MF->getSubtarget();
523 const TargetFrameLowering *TFI = TSI.getFrameLowering();
524 const TargetRegisterInfo *TRI = TSI.getRegisterInfo();
525
526 for (const MachineModuleInfo::VariableDbgInfo &VI :
527 MMI->getVariableDbgInfo()) {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000528 if (!VI.Var)
529 continue;
530 assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) &&
531 "Expected inlined-at fields to agree");
532
Reid Kleckner876330d2016-02-12 21:48:30 +0000533 Processed.insert(InlinedVariable(VI.Var, VI.Loc->getInlinedAt()));
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000534 LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc);
535
536 // If variable scope is not found then skip this variable.
537 if (!Scope)
538 continue;
539
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000540 // Get the frame register used and the offset.
541 unsigned FrameReg = 0;
Reid Kleckner876330d2016-02-12 21:48:30 +0000542 int FrameOffset = TFI->getFrameIndexReference(*Asm->MF, VI.Slot, FrameReg);
543 uint16_t CVReg = TRI->getCodeViewRegNum(FrameReg);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000544
545 // Calculate the label ranges.
Reid Kleckner876330d2016-02-12 21:48:30 +0000546 LocalVarDefRange DefRange = createDefRangeMem(CVReg, FrameOffset);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000547 for (const InsnRange &Range : Scope->getRanges()) {
548 const MCSymbol *Begin = getLabelBeforeInsn(Range.first);
549 const MCSymbol *End = getLabelAfterInsn(Range.second);
Reid Kleckner876330d2016-02-12 21:48:30 +0000550 End = End ? End : Asm->getFunctionEnd();
551 DefRange.Ranges.emplace_back(Begin, End);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000552 }
553
Reid Kleckner876330d2016-02-12 21:48:30 +0000554 LocalVariable Var;
555 Var.DIVar = VI.Var;
556 Var.DefRanges.emplace_back(std::move(DefRange));
557 recordLocalVariable(std::move(Var), VI.Loc->getInlinedAt());
558 }
559}
560
561void CodeViewDebug::collectVariableInfo(const DISubprogram *SP) {
562 DenseSet<InlinedVariable> Processed;
563 // Grab the variable info that was squirreled away in the MMI side-table.
564 collectVariableInfoFromMMITable(Processed);
565
566 const TargetRegisterInfo *TRI = Asm->MF->getSubtarget().getRegisterInfo();
567
568 for (const auto &I : DbgValues) {
569 InlinedVariable IV = I.first;
570 if (Processed.count(IV))
571 continue;
572 const DILocalVariable *DIVar = IV.first;
573 const DILocation *InlinedAt = IV.second;
574
575 // Instruction ranges, specifying where IV is accessible.
576 const auto &Ranges = I.second;
577
578 LexicalScope *Scope = nullptr;
579 if (InlinedAt)
580 Scope = LScopes.findInlinedScope(DIVar->getScope(), InlinedAt);
581 else
582 Scope = LScopes.findLexicalScope(DIVar->getScope());
583 // If variable scope is not found then skip this variable.
584 if (!Scope)
585 continue;
586
587 LocalVariable Var;
588 Var.DIVar = DIVar;
589
590 // Calculate the definition ranges.
591 for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) {
592 const InsnRange &Range = *I;
593 const MachineInstr *DVInst = Range.first;
594 assert(DVInst->isDebugValue() && "Invalid History entry");
595 const DIExpression *DIExpr = DVInst->getDebugExpression();
596
597 // Bail if there is a complex DWARF expression for now.
598 if (DIExpr && DIExpr->getNumElements() > 0)
599 continue;
600
Reid Kleckner9a593ee2016-02-16 21:49:26 +0000601 // Bail if operand 0 is not a valid register. This means the variable is a
602 // simple constant, or is described by a complex expression.
603 // FIXME: Find a way to represent constant variables, since they are
604 // relatively common.
605 unsigned Reg =
606 DVInst->getOperand(0).isReg() ? DVInst->getOperand(0).getReg() : 0;
607 if (Reg == 0)
Reid Kleckner6e0d5f52016-02-16 21:14:51 +0000608 continue;
609
Reid Kleckner876330d2016-02-12 21:48:30 +0000610 // Handle the two cases we can handle: indirect in memory and in register.
611 bool IsIndirect = DVInst->getOperand(1).isImm();
612 unsigned CVReg = TRI->getCodeViewRegNum(DVInst->getOperand(0).getReg());
613 {
614 LocalVarDefRange DefRange;
615 if (IsIndirect) {
616 int64_t Offset = DVInst->getOperand(1).getImm();
617 DefRange = createDefRangeMem(CVReg, Offset);
618 } else {
619 DefRange = createDefRangeReg(CVReg);
620 }
621 if (Var.DefRanges.empty() ||
622 Var.DefRanges.back().isDifferentLocation(DefRange)) {
623 Var.DefRanges.emplace_back(std::move(DefRange));
624 }
625 }
626
627 // Compute the label range.
628 const MCSymbol *Begin = getLabelBeforeInsn(Range.first);
629 const MCSymbol *End = getLabelAfterInsn(Range.second);
630 if (!End) {
631 if (std::next(I) != E)
632 End = getLabelBeforeInsn(std::next(I)->first);
633 else
634 End = Asm->getFunctionEnd();
635 }
636
637 // If the last range end is our begin, just extend the last range.
638 // Otherwise make a new range.
639 SmallVectorImpl<std::pair<const MCSymbol *, const MCSymbol *>> &Ranges =
640 Var.DefRanges.back().Ranges;
641 if (!Ranges.empty() && Ranges.back().second == Begin)
642 Ranges.back().second = End;
643 else
644 Ranges.emplace_back(Begin, End);
645
646 // FIXME: Do more range combining.
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000647 }
Reid Kleckner876330d2016-02-12 21:48:30 +0000648
649 recordLocalVariable(std::move(Var), InlinedAt);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000650 }
651}
652
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000653void CodeViewDebug::beginFunction(const MachineFunction *MF) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000654 assert(!CurFn && "Can't process two functions at once!");
655
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000656 if (!Asm || !MMI->hasDebugInfo())
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000657 return;
658
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000659 DebugHandlerBase::beginFunction(MF);
660
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000661 const Function *GV = MF->getFunction();
662 assert(FnDebugInfo.count(GV) == false);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000663 CurFn = &FnDebugInfo[GV];
Reid Kleckner2214ed82016-01-29 00:49:42 +0000664 CurFn->FuncId = NextFuncId++;
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000665 CurFn->Begin = Asm->getFunctionBegin();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000666
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000667 // Find the end of the function prolog. First known non-DBG_VALUE and
668 // non-frame setup location marks the beginning of the function body.
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000669 // FIXME: is there a simpler a way to do this? Can we just search
670 // for the first instruction of the function, not the last of the prolog?
671 DebugLoc PrologEndLoc;
672 bool EmptyPrologue = true;
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000673 for (const auto &MBB : *MF) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000674 for (const auto &MI : MBB) {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000675 if (!MI.isDebugValue() && !MI.getFlag(MachineInstr::FrameSetup) &&
676 MI.getDebugLoc()) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000677 PrologEndLoc = MI.getDebugLoc();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000678 break;
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000679 } else if (!MI.isDebugValue()) {
680 EmptyPrologue = false;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000681 }
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000682 }
683 }
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000684
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000685 // Record beginning of function if we have a non-empty prologue.
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000686 if (PrologEndLoc && !EmptyPrologue) {
687 DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000688 maybeRecordLocation(FnStartDL, MF);
689 }
690}
691
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000692void CodeViewDebug::emitLocalVariable(const LocalVariable &Var) {
693 // LocalSym record, see SymbolRecord.h for more info.
694 MCSymbol *LocalBegin = MMI->getContext().createTempSymbol(),
695 *LocalEnd = MMI->getContext().createTempSymbol();
696 OS.AddComment("Record length");
697 OS.emitAbsoluteSymbolDiff(LocalEnd, LocalBegin, 2);
698 OS.EmitLabel(LocalBegin);
699
700 OS.AddComment("Record kind: S_LOCAL");
701 OS.EmitIntValue(unsigned(SymbolRecordKind::S_LOCAL), 2);
702
703 uint16_t Flags = 0;
704 if (Var.DIVar->isParameter())
705 Flags |= LocalSym::IsParameter;
Reid Kleckner876330d2016-02-12 21:48:30 +0000706 if (Var.DefRanges.empty())
707 Flags |= LocalSym::IsOptimizedOut;
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000708
709 OS.AddComment("TypeIndex");
710 OS.EmitIntValue(TypeIndex::Int32().getIndex(), 4);
711 OS.AddComment("Flags");
712 OS.EmitIntValue(Flags, 2);
David Majnemer12561252016-03-13 10:53:30 +0000713 // Truncate the name so we won't overflow the record length field.
David Majnemerb9456a52016-03-14 05:15:09 +0000714 emitNullTerminatedSymbolName(OS, Var.DIVar->getName());
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000715 OS.EmitLabel(LocalEnd);
716
Reid Kleckner876330d2016-02-12 21:48:30 +0000717 // Calculate the on disk prefix of the appropriate def range record. The
718 // records and on disk formats are described in SymbolRecords.h. BytePrefix
719 // should be big enough to hold all forms without memory allocation.
720 SmallString<20> BytePrefix;
721 for (const LocalVarDefRange &DefRange : Var.DefRanges) {
722 BytePrefix.clear();
723 // FIXME: Handle bitpieces.
724 if (DefRange.StructOffset != 0)
725 continue;
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000726
Reid Kleckner876330d2016-02-12 21:48:30 +0000727 if (DefRange.InMemory) {
728 DefRangeRegisterRelSym Sym{};
729 ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER_REL);
730 Sym.BaseRegister = DefRange.CVRegister;
731 Sym.Flags = 0; // Unclear what matters here.
732 Sym.BasePointerOffset = DefRange.DataOffset;
733 BytePrefix +=
734 StringRef(reinterpret_cast<const char *>(&SymKind), sizeof(SymKind));
735 BytePrefix += StringRef(reinterpret_cast<const char *>(&Sym),
736 sizeof(Sym) - sizeof(LocalVariableAddrRange));
737 } else {
738 assert(DefRange.DataOffset == 0 && "unexpected offset into register");
739 DefRangeRegisterSym Sym{};
740 ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER);
741 Sym.Register = DefRange.CVRegister;
742 Sym.MayHaveNoName = 0; // Unclear what matters here.
743 BytePrefix +=
744 StringRef(reinterpret_cast<const char *>(&SymKind), sizeof(SymKind));
745 BytePrefix += StringRef(reinterpret_cast<const char *>(&Sym),
746 sizeof(Sym) - sizeof(LocalVariableAddrRange));
747 }
748 OS.EmitCVDefRangeDirective(DefRange.Ranges, BytePrefix);
749 }
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000750}
751
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000752void CodeViewDebug::endFunction(const MachineFunction *MF) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000753 if (!Asm || !CurFn) // We haven't created any debug info for this function.
754 return;
755
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000756 const Function *GV = MF->getFunction();
Yaron Keren6d3194f2014-06-20 10:26:56 +0000757 assert(FnDebugInfo.count(GV));
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000758 assert(CurFn == &FnDebugInfo[GV]);
759
Pete Cooperadebb932016-03-11 02:14:16 +0000760 collectVariableInfo(GV->getSubprogram());
Reid Kleckner876330d2016-02-12 21:48:30 +0000761
762 DebugHandlerBase::endFunction(MF);
763
Reid Kleckner2214ed82016-01-29 00:49:42 +0000764 // Don't emit anything if we don't have any line tables.
765 if (!CurFn->HaveLineInfo) {
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000766 FnDebugInfo.erase(GV);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000767 CurFn = nullptr;
768 return;
Timur Iskhodzhanov8499a122014-03-26 09:50:36 +0000769 }
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000770
771 CurFn->End = Asm->getFunctionEnd();
772
Craig Topper353eda42014-04-24 06:44:33 +0000773 CurFn = nullptr;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000774}
775
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000776void CodeViewDebug::beginInstruction(const MachineInstr *MI) {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000777 DebugHandlerBase::beginInstruction(MI);
778
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000779 // Ignore DBG_VALUE locations and function prologue.
780 if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup))
781 return;
782 DebugLoc DL = MI->getDebugLoc();
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000783 if (DL == PrevInstLoc || !DL)
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000784 return;
785 maybeRecordLocation(DL, Asm->MF);
786}