blob: b5c796a01bd87a9480dcaad91922e7fc5d4890a5 [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 Klecknerfbd77872016-03-18 18:54:32 +0000111 auto SiteInsertion = CurFn->InlineSites.insert({InlinedAt, InlineSite()});
112 InlineSite *Site = &SiteInsertion.first->second;
113 if (SiteInsertion.second) {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000114 Site->SiteFuncId = NextFuncId++;
Reid Kleckner876330d2016-02-12 21:48:30 +0000115 Site->Inlinee = Inlinee;
Reid Klecknerfbd77872016-03-18 18:54:32 +0000116 auto InlineeInsertion =
117 SubprogramIndices.insert({Inlinee, InlinedSubprograms.size()});
118 if (InlineeInsertion.second)
119 InlinedSubprograms.push_back(Inlinee);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000120 }
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000121 return *Site;
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000122}
123
Reid Kleckner876330d2016-02-12 21:48:30 +0000124void CodeViewDebug::recordLocalVariable(LocalVariable &&Var,
125 const DILocation *InlinedAt) {
126 if (InlinedAt) {
127 // This variable was inlined. Associate it with the InlineSite.
128 const DISubprogram *Inlinee = Var.DIVar->getScope()->getSubprogram();
129 InlineSite &Site = getInlineSite(InlinedAt, Inlinee);
130 Site.InlinedLocals.emplace_back(Var);
131 } else {
132 // This variable goes in the main ProcSym.
133 CurFn->Locals.emplace_back(Var);
134 }
135}
136
Reid Kleckner829365a2016-02-11 19:41:47 +0000137static void addLocIfNotPresent(SmallVectorImpl<const DILocation *> &Locs,
138 const DILocation *Loc) {
139 auto B = Locs.begin(), E = Locs.end();
140 if (std::find(B, E, Loc) == E)
141 Locs.push_back(Loc);
142}
143
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000144void CodeViewDebug::maybeRecordLocation(DebugLoc DL,
Reid Kleckner9533af42016-01-16 00:09:09 +0000145 const MachineFunction *MF) {
146 // Skip this instruction if it has the same location as the previous one.
147 if (DL == CurFn->LastLoc)
148 return;
149
150 const DIScope *Scope = DL.get()->getScope();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000151 if (!Scope)
152 return;
Reid Kleckner9533af42016-01-16 00:09:09 +0000153
David Majnemerc3340db2016-01-13 01:05:23 +0000154 // Skip this line if it is longer than the maximum we can record.
Reid Kleckner2214ed82016-01-29 00:49:42 +0000155 LineInfo LI(DL.getLine(), DL.getLine(), /*IsStatement=*/true);
156 if (LI.getStartLine() != DL.getLine() || LI.isAlwaysStepInto() ||
157 LI.isNeverStepInto())
David Majnemerc3340db2016-01-13 01:05:23 +0000158 return;
159
Reid Kleckner2214ed82016-01-29 00:49:42 +0000160 ColumnInfo CI(DL.getCol(), /*EndColumn=*/0);
161 if (CI.getStartColumn() != DL.getCol())
162 return;
Reid Kleckner00d96392016-01-29 00:13:28 +0000163
Reid Kleckner2214ed82016-01-29 00:49:42 +0000164 if (!CurFn->HaveLineInfo)
165 CurFn->HaveLineInfo = true;
166 unsigned FileId = 0;
167 if (CurFn->LastLoc.get() && CurFn->LastLoc->getFile() == DL->getFile())
168 FileId = CurFn->LastFileId;
169 else
170 FileId = CurFn->LastFileId = maybeRecordFile(DL->getFile());
171 CurFn->LastLoc = DL;
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000172
173 unsigned FuncId = CurFn->FuncId;
Reid Kleckner876330d2016-02-12 21:48:30 +0000174 if (const DILocation *SiteLoc = DL->getInlinedAt()) {
Reid Kleckner829365a2016-02-11 19:41:47 +0000175 const DILocation *Loc = DL.get();
176
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000177 // If this location was actually inlined from somewhere else, give it the ID
178 // of the inline call site.
Reid Kleckner876330d2016-02-12 21:48:30 +0000179 FuncId =
180 getInlineSite(SiteLoc, Loc->getScope()->getSubprogram()).SiteFuncId;
Reid Kleckner829365a2016-02-11 19:41:47 +0000181
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000182 // Ensure we have links in the tree of inline call sites.
Reid Kleckner829365a2016-02-11 19:41:47 +0000183 bool FirstLoc = true;
184 while ((SiteLoc = Loc->getInlinedAt())) {
Reid Kleckner876330d2016-02-12 21:48:30 +0000185 InlineSite &Site =
186 getInlineSite(SiteLoc, Loc->getScope()->getSubprogram());
Reid Kleckner829365a2016-02-11 19:41:47 +0000187 if (!FirstLoc)
188 addLocIfNotPresent(Site.ChildSites, Loc);
189 FirstLoc = false;
190 Loc = SiteLoc;
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000191 }
Reid Kleckner829365a2016-02-11 19:41:47 +0000192 addLocIfNotPresent(CurFn->ChildSites, Loc);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000193 }
194
Reid Klecknerdac21b42016-02-03 21:15:48 +0000195 OS.EmitCVLocDirective(FuncId, FileId, DL.getLine(), DL.getCol(),
196 /*PrologueEnd=*/false,
197 /*IsStmt=*/false, DL->getFilename());
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000198}
199
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000200void CodeViewDebug::endModule() {
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000201 if (FnDebugInfo.empty())
202 return;
203
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000204 emitTypeInformation();
205
Reid Kleckner6b3faef2016-01-13 23:44:57 +0000206 // FIXME: For functions that are comdat, we should emit separate .debug$S
207 // sections that are comdat associative with the main function instead of
208 // having one big .debug$S section.
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000209 assert(Asm != nullptr);
Reid Klecknerdac21b42016-02-03 21:15:48 +0000210 OS.SwitchSection(Asm->getObjFileLowering().getCOFFDebugSymbolsSection());
211 OS.AddComment("Debug section magic");
212 OS.EmitIntValue(COFF::DEBUG_SECTION_MAGIC, 4);
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000213
214 // The COFF .debug$S section consists of several subsections, each starting
215 // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length
216 // of the payload followed by the payload itself. The subsections are 4-byte
217 // aligned.
218
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000219 // Make a subsection for all the inlined subprograms.
Reid Klecknerfbd77872016-03-18 18:54:32 +0000220 emitInlineeFuncIdsAndLines();
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000221
Reid Kleckner2214ed82016-01-29 00:49:42 +0000222 // Emit per-function debug information.
223 for (auto &P : FnDebugInfo)
224 emitDebugInfoForFunction(P.first, P.second);
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000225
226 // This subsection holds a file index to offset in string table table.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000227 OS.AddComment("File index to string table offset subsection");
228 OS.EmitCVFileChecksumsDirective();
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000229
230 // This subsection holds the string table.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000231 OS.AddComment("String table");
232 OS.EmitCVStringTableDirective();
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000233
234 clear();
235}
236
David Majnemerb9456a52016-03-14 05:15:09 +0000237static void emitNullTerminatedSymbolName(MCStreamer &OS, StringRef S) {
238 // Microsoft's linker seems to have trouble with symbol names longer than
239 // 0xffd8 bytes.
240 S = S.substr(0, 0xffd8);
241 SmallString<32> NullTerminatedString(S);
242 NullTerminatedString.push_back('\0');
243 OS.EmitBytes(NullTerminatedString);
244}
245
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000246void CodeViewDebug::emitTypeInformation() {
Reid Klecknerfbd77872016-03-18 18:54:32 +0000247 // Do nothing if we have no debug info or no inlined subprograms. The types
248 // we currently emit exist only to support inlined call site info.
249 NamedMDNode *CU_Nodes =
250 MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
251 if (!CU_Nodes)
252 return;
253 if (InlinedSubprograms.empty())
254 return;
255
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000256 // Start the .debug$T section with 0x4.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000257 OS.SwitchSection(Asm->getObjFileLowering().getCOFFDebugTypesSection());
258 OS.AddComment("Debug section magic");
259 OS.EmitIntValue(COFF::DEBUG_SECTION_MAGIC, 4);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000260
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000261 // This type info currently only holds function ids for use with inline call
262 // frame info. All functions are assigned a simple 'void ()' type. Emit that
263 // type here.
David Majnemer6cd7c912016-05-23 01:37:45 +0000264 unsigned ArgListIndex = getNextTypeIndex();
265 OS.AddComment("Type record length");
266 OS.EmitIntValue(ArgListRecord::getLayoutSize(), 2);
267 OS.AddComment("Leaf type: LF_ARGLIST");
268 OS.EmitIntValue(LF_ARGLIST, 2);
269 OS.AddComment("Number of arguments");
270 OS.EmitIntValue(0, 4);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000271
David Majnemer6cd7c912016-05-23 01:37:45 +0000272 unsigned VoidFnTyIdx = getNextTypeIndex();
273 OS.AddComment("Type record length");
274 OS.EmitIntValue(ProcedureRecord::getLayoutSize(), 2);
275 OS.AddComment("Leaf type: LF_PROCEDURE");
276 OS.EmitIntValue(LF_PROCEDURE, 2);
277 OS.AddComment("Return type index");
278 OS.EmitIntValue(TypeIndex::Void().getIndex(), 4);
279 OS.AddComment("Calling convention");
280 OS.EmitIntValue(char(CallingConvention::NearC), 1);
281 OS.AddComment("Function options");
282 OS.EmitIntValue(char(FunctionOptions::None), 1);
283 OS.AddComment("# of parameters");
284 OS.EmitIntValue(0, 2);
285 OS.AddComment("Argument list type index");
286 OS.EmitIntValue(ArgListIndex, 4);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000287
Reid Klecknerfbd77872016-03-18 18:54:32 +0000288 // Emit LF_FUNC_ID records for all inlined subprograms to the type stream.
289 // Allocate one type index for each func id.
David Majnemer6cd7c912016-05-23 01:37:45 +0000290 unsigned NextIdx = getNextTypeIndex(InlinedSubprograms.size());
291 (void)NextIdx;
292 assert(NextIdx == FuncIdTypeIndexStart && "func id type indices broken");
Reid Klecknerfbd77872016-03-18 18:54:32 +0000293 for (auto *SP : InlinedSubprograms) {
294 StringRef DisplayName = SP->getDisplayName();
David Majnemer6cd7c912016-05-23 01:37:45 +0000295 OS.AddComment("Type record length");
296 MCSymbol *FuncBegin = MMI->getContext().createTempSymbol(),
297 *FuncEnd = MMI->getContext().createTempSymbol();
298 OS.emitAbsoluteSymbolDiff(FuncEnd, FuncBegin, 2);
299 OS.EmitLabel(FuncBegin);
300 OS.AddComment("Leaf type: LF_FUNC_ID");
301 OS.EmitIntValue(LF_FUNC_ID, 2);
Adrian McCarthya972d612016-05-19 20:12:56 +0000302
David Majnemer6cd7c912016-05-23 01:37:45 +0000303 OS.AddComment("Scope type index");
304 OS.EmitIntValue(0, 4);
305 OS.AddComment("Function type");
306 OS.EmitIntValue(VoidFnTyIdx, 4);
307 {
308 OS.AddComment("Function name");
309 emitNullTerminatedSymbolName(OS, DisplayName);
310 }
311 OS.EmitLabel(FuncEnd);
312 }
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000313}
314
Reid Klecknerfbd77872016-03-18 18:54:32 +0000315void CodeViewDebug::emitInlineeFuncIdsAndLines() {
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000316 if (InlinedSubprograms.empty())
317 return;
318
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000319 MCSymbol *InlineBegin = MMI->getContext().createTempSymbol(),
320 *InlineEnd = MMI->getContext().createTempSymbol();
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000321
322 OS.AddComment("Inlinee lines subsection");
323 OS.EmitIntValue(unsigned(ModuleSubstreamKind::InlineeLines), 4);
David Majnemer30579ec2016-02-02 23:18:23 +0000324 OS.AddComment("Subsection size");
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000325 OS.emitAbsoluteSymbolDiff(InlineEnd, InlineBegin, 4);
326 OS.EmitLabel(InlineBegin);
327
328 // We don't provide any extra file info.
329 // FIXME: Find out if debuggers use this info.
David Majnemer30579ec2016-02-02 23:18:23 +0000330 OS.AddComment("Inlinee lines signature");
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000331 OS.EmitIntValue(unsigned(InlineeLinesSignature::Normal), 4);
332
Reid Klecknerfbd77872016-03-18 18:54:32 +0000333 unsigned InlineeIndex = FuncIdTypeIndexStart;
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000334 for (const DISubprogram *SP : InlinedSubprograms) {
David Majnemer30579ec2016-02-02 23:18:23 +0000335 OS.AddBlankLine();
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000336 unsigned FileId = maybeRecordFile(SP->getFile());
337 OS.AddComment("Inlined function " + SP->getDisplayName() + " starts at " +
338 SP->getFilename() + Twine(':') + Twine(SP->getLine()));
David Majnemer30579ec2016-02-02 23:18:23 +0000339 OS.AddBlankLine();
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000340 // The filechecksum table uses 8 byte entries for now, and file ids start at
341 // 1.
342 unsigned FileOffset = (FileId - 1) * 8;
David Majnemer30579ec2016-02-02 23:18:23 +0000343 OS.AddComment("Type index of inlined function");
Reid Klecknerfbd77872016-03-18 18:54:32 +0000344 OS.EmitIntValue(InlineeIndex, 4);
David Majnemer30579ec2016-02-02 23:18:23 +0000345 OS.AddComment("Offset into filechecksum table");
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000346 OS.EmitIntValue(FileOffset, 4);
David Majnemer30579ec2016-02-02 23:18:23 +0000347 OS.AddComment("Starting line number");
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000348 OS.EmitIntValue(SP->getLine(), 4);
Reid Klecknerfbd77872016-03-18 18:54:32 +0000349
350 // The next inlined subprogram has the next function id.
351 InlineeIndex++;
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000352 }
353
354 OS.EmitLabel(InlineEnd);
355}
356
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000357void CodeViewDebug::collectInlineSiteChildren(
358 SmallVectorImpl<unsigned> &Children, const FunctionInfo &FI,
359 const InlineSite &Site) {
360 for (const DILocation *ChildSiteLoc : Site.ChildSites) {
361 auto I = FI.InlineSites.find(ChildSiteLoc);
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000362 const InlineSite &ChildSite = I->second;
363 Children.push_back(ChildSite.SiteFuncId);
364 collectInlineSiteChildren(Children, FI, ChildSite);
365 }
366}
367
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000368void CodeViewDebug::emitInlinedCallSite(const FunctionInfo &FI,
369 const DILocation *InlinedAt,
370 const InlineSite &Site) {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000371 MCSymbol *InlineBegin = MMI->getContext().createTempSymbol(),
372 *InlineEnd = MMI->getContext().createTempSymbol();
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000373
Reid Klecknerfbd77872016-03-18 18:54:32 +0000374 assert(SubprogramIndices.count(Site.Inlinee));
375 unsigned InlineeIdx = FuncIdTypeIndexStart + SubprogramIndices[Site.Inlinee];
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000376
377 // SymbolRecord
Reid Klecknerdac21b42016-02-03 21:15:48 +0000378 OS.AddComment("Record length");
Reid Klecknereb3bcdd2016-02-03 21:24:42 +0000379 OS.emitAbsoluteSymbolDiff(InlineEnd, InlineBegin, 2); // RecordLength
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000380 OS.EmitLabel(InlineBegin);
Reid Klecknerdac21b42016-02-03 21:15:48 +0000381 OS.AddComment("Record kind: S_INLINESITE");
Zachary Turner63a28462016-05-17 23:50:21 +0000382 OS.EmitIntValue(SymbolKind::S_INLINESITE, 2); // RecordKind
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000383
Reid Klecknerdac21b42016-02-03 21:15:48 +0000384 OS.AddComment("PtrParent");
385 OS.EmitIntValue(0, 4);
386 OS.AddComment("PtrEnd");
387 OS.EmitIntValue(0, 4);
388 OS.AddComment("Inlinee type index");
Reid Klecknerfbd77872016-03-18 18:54:32 +0000389 OS.EmitIntValue(InlineeIdx, 4);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000390
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000391 unsigned FileId = maybeRecordFile(Site.Inlinee->getFile());
392 unsigned StartLineNum = Site.Inlinee->getLine();
393 SmallVector<unsigned, 3> SecondaryFuncIds;
394 collectInlineSiteChildren(SecondaryFuncIds, FI, Site);
395
396 OS.EmitCVInlineLinetableDirective(Site.SiteFuncId, FileId, StartLineNum,
David Majnemerc9911f22016-02-02 19:22:34 +0000397 FI.Begin, FI.End, SecondaryFuncIds);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000398
399 OS.EmitLabel(InlineEnd);
400
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000401 for (const LocalVariable &Var : Site.InlinedLocals)
402 emitLocalVariable(Var);
403
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000404 // Recurse on child inlined call sites before closing the scope.
405 for (const DILocation *ChildSite : Site.ChildSites) {
406 auto I = FI.InlineSites.find(ChildSite);
407 assert(I != FI.InlineSites.end() &&
408 "child site not in function inline site map");
409 emitInlinedCallSite(FI, ChildSite, I->second);
410 }
411
412 // Close the scope.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000413 OS.AddComment("Record length");
414 OS.EmitIntValue(2, 2); // RecordLength
415 OS.AddComment("Record kind: S_INLINESITE_END");
Zachary Turner63a28462016-05-17 23:50:21 +0000416 OS.EmitIntValue(SymbolKind::S_INLINESITE_END, 2); // RecordKind
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000417}
418
Reid Kleckner2214ed82016-01-29 00:49:42 +0000419void CodeViewDebug::emitDebugInfoForFunction(const Function *GV,
420 FunctionInfo &FI) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000421 // For each function there is a separate subsection
422 // which holds the PC to file:line table.
423 const MCSymbol *Fn = Asm->getSymbol(GV);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000424 assert(Fn);
Timur Iskhodzhanov8499a122014-03-26 09:50:36 +0000425
Duncan P. N. Exon Smith23e56ec2015-03-20 19:50:00 +0000426 StringRef FuncName;
Pete Cooperadebb932016-03-11 02:14:16 +0000427 if (auto *SP = GV->getSubprogram())
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000428 FuncName = SP->getDisplayName();
Duncan P. N. Exon Smith23e56ec2015-03-20 19:50:00 +0000429
Reid Kleckner3c0ff982016-01-14 00:12:54 +0000430 // If our DISubprogram name is empty, use the mangled name.
Reid Kleckner72e2ba72016-01-13 19:32:35 +0000431 if (FuncName.empty())
432 FuncName = GlobalValue::getRealLinkageName(GV->getName());
Reid Kleckner3c0ff982016-01-14 00:12:54 +0000433
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000434 // Emit a symbol subsection, required by VS2012+ to find function boundaries.
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000435 MCSymbol *SymbolsBegin = MMI->getContext().createTempSymbol(),
436 *SymbolsEnd = MMI->getContext().createTempSymbol();
Reid Klecknerdac21b42016-02-03 21:15:48 +0000437 OS.AddComment("Symbol subsection for " + Twine(FuncName));
438 OS.EmitIntValue(unsigned(ModuleSubstreamKind::Symbols), 4);
439 OS.AddComment("Subsection size");
Reid Klecknereb3bcdd2016-02-03 21:24:42 +0000440 OS.emitAbsoluteSymbolDiff(SymbolsEnd, SymbolsBegin, 4);
Reid Klecknerdac21b42016-02-03 21:15:48 +0000441 OS.EmitLabel(SymbolsBegin);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000442 {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000443 MCSymbol *ProcRecordBegin = MMI->getContext().createTempSymbol(),
444 *ProcRecordEnd = MMI->getContext().createTempSymbol();
Reid Klecknerdac21b42016-02-03 21:15:48 +0000445 OS.AddComment("Record length");
Reid Klecknereb3bcdd2016-02-03 21:24:42 +0000446 OS.emitAbsoluteSymbolDiff(ProcRecordEnd, ProcRecordBegin, 2);
Reid Klecknerdac21b42016-02-03 21:15:48 +0000447 OS.EmitLabel(ProcRecordBegin);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000448
Reid Klecknerdac21b42016-02-03 21:15:48 +0000449 OS.AddComment("Record kind: S_GPROC32_ID");
Zachary Turner63a28462016-05-17 23:50:21 +0000450 OS.EmitIntValue(unsigned(SymbolKind::S_GPROC32_ID), 2);
Reid Kleckner6b3faef2016-01-13 23:44:57 +0000451
David Majnemer30579ec2016-02-02 23:18:23 +0000452 // These fields are filled in by tools like CVPACK which run after the fact.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000453 OS.AddComment("PtrParent");
454 OS.EmitIntValue(0, 4);
455 OS.AddComment("PtrEnd");
456 OS.EmitIntValue(0, 4);
457 OS.AddComment("PtrNext");
458 OS.EmitIntValue(0, 4);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000459 // This is the important bit that tells the debugger where the function
460 // code is located and what's its size:
Reid Klecknerdac21b42016-02-03 21:15:48 +0000461 OS.AddComment("Code size");
Reid Klecknereb3bcdd2016-02-03 21:24:42 +0000462 OS.emitAbsoluteSymbolDiff(FI.End, Fn, 4);
Reid Klecknerdac21b42016-02-03 21:15:48 +0000463 OS.AddComment("Offset after prologue");
464 OS.EmitIntValue(0, 4);
465 OS.AddComment("Offset before epilogue");
466 OS.EmitIntValue(0, 4);
467 OS.AddComment("Function type index");
468 OS.EmitIntValue(0, 4);
469 OS.AddComment("Function section relative address");
470 OS.EmitCOFFSecRel32(Fn);
471 OS.AddComment("Function section index");
472 OS.EmitCOFFSectionIndex(Fn);
473 OS.AddComment("Flags");
474 OS.EmitIntValue(0, 1);
Timur Iskhodzhanova11b32b2014-11-12 20:10:09 +0000475 // Emit the function display name as a null-terminated string.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000476 OS.AddComment("Function name");
David Majnemer12561252016-03-13 10:53:30 +0000477 // Truncate the name so we won't overflow the record length field.
David Majnemerb9456a52016-03-14 05:15:09 +0000478 emitNullTerminatedSymbolName(OS, FuncName);
Reid Klecknerdac21b42016-02-03 21:15:48 +0000479 OS.EmitLabel(ProcRecordEnd);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000480
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000481 for (const LocalVariable &Var : FI.Locals)
482 emitLocalVariable(Var);
483
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000484 // Emit inlined call site information. Only emit functions inlined directly
485 // into the parent function. We'll emit the other sites recursively as part
486 // of their parent inline site.
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000487 for (const DILocation *InlinedAt : FI.ChildSites) {
488 auto I = FI.InlineSites.find(InlinedAt);
489 assert(I != FI.InlineSites.end() &&
490 "child site not in function inline site map");
491 emitInlinedCallSite(FI, InlinedAt, I->second);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000492 }
493
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000494 // We're done with this function.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000495 OS.AddComment("Record length");
496 OS.EmitIntValue(0x0002, 2);
497 OS.AddComment("Record kind: S_PROC_ID_END");
Zachary Turner63a28462016-05-17 23:50:21 +0000498 OS.EmitIntValue(unsigned(SymbolKind::S_PROC_ID_END), 2);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000499 }
Reid Klecknerdac21b42016-02-03 21:15:48 +0000500 OS.EmitLabel(SymbolsEnd);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000501 // Every subsection must be aligned to a 4-byte boundary.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000502 OS.EmitValueToAlignment(4);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000503
Reid Kleckner2214ed82016-01-29 00:49:42 +0000504 // We have an assembler directive that takes care of the whole line table.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000505 OS.EmitCVLinetableDirective(FI.FuncId, Fn, FI.End);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000506}
507
Reid Kleckner876330d2016-02-12 21:48:30 +0000508CodeViewDebug::LocalVarDefRange
509CodeViewDebug::createDefRangeMem(uint16_t CVRegister, int Offset) {
510 LocalVarDefRange DR;
Aaron Ballmanc6a2f212016-02-16 15:35:51 +0000511 DR.InMemory = -1;
Reid Kleckner876330d2016-02-12 21:48:30 +0000512 DR.DataOffset = Offset;
513 assert(DR.DataOffset == Offset && "truncation");
514 DR.StructOffset = 0;
515 DR.CVRegister = CVRegister;
516 return DR;
517}
518
519CodeViewDebug::LocalVarDefRange
520CodeViewDebug::createDefRangeReg(uint16_t CVRegister) {
521 LocalVarDefRange DR;
522 DR.InMemory = 0;
523 DR.DataOffset = 0;
524 DR.StructOffset = 0;
525 DR.CVRegister = CVRegister;
526 return DR;
527}
528
529void CodeViewDebug::collectVariableInfoFromMMITable(
530 DenseSet<InlinedVariable> &Processed) {
531 const TargetSubtargetInfo &TSI = Asm->MF->getSubtarget();
532 const TargetFrameLowering *TFI = TSI.getFrameLowering();
533 const TargetRegisterInfo *TRI = TSI.getRegisterInfo();
534
535 for (const MachineModuleInfo::VariableDbgInfo &VI :
536 MMI->getVariableDbgInfo()) {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000537 if (!VI.Var)
538 continue;
539 assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) &&
540 "Expected inlined-at fields to agree");
541
Reid Kleckner876330d2016-02-12 21:48:30 +0000542 Processed.insert(InlinedVariable(VI.Var, VI.Loc->getInlinedAt()));
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000543 LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc);
544
545 // If variable scope is not found then skip this variable.
546 if (!Scope)
547 continue;
548
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000549 // Get the frame register used and the offset.
550 unsigned FrameReg = 0;
Reid Kleckner876330d2016-02-12 21:48:30 +0000551 int FrameOffset = TFI->getFrameIndexReference(*Asm->MF, VI.Slot, FrameReg);
552 uint16_t CVReg = TRI->getCodeViewRegNum(FrameReg);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000553
554 // Calculate the label ranges.
Reid Kleckner876330d2016-02-12 21:48:30 +0000555 LocalVarDefRange DefRange = createDefRangeMem(CVReg, FrameOffset);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000556 for (const InsnRange &Range : Scope->getRanges()) {
557 const MCSymbol *Begin = getLabelBeforeInsn(Range.first);
558 const MCSymbol *End = getLabelAfterInsn(Range.second);
Reid Kleckner876330d2016-02-12 21:48:30 +0000559 End = End ? End : Asm->getFunctionEnd();
560 DefRange.Ranges.emplace_back(Begin, End);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000561 }
562
Reid Kleckner876330d2016-02-12 21:48:30 +0000563 LocalVariable Var;
564 Var.DIVar = VI.Var;
565 Var.DefRanges.emplace_back(std::move(DefRange));
566 recordLocalVariable(std::move(Var), VI.Loc->getInlinedAt());
567 }
568}
569
570void CodeViewDebug::collectVariableInfo(const DISubprogram *SP) {
571 DenseSet<InlinedVariable> Processed;
572 // Grab the variable info that was squirreled away in the MMI side-table.
573 collectVariableInfoFromMMITable(Processed);
574
575 const TargetRegisterInfo *TRI = Asm->MF->getSubtarget().getRegisterInfo();
576
577 for (const auto &I : DbgValues) {
578 InlinedVariable IV = I.first;
579 if (Processed.count(IV))
580 continue;
581 const DILocalVariable *DIVar = IV.first;
582 const DILocation *InlinedAt = IV.second;
583
584 // Instruction ranges, specifying where IV is accessible.
585 const auto &Ranges = I.second;
586
587 LexicalScope *Scope = nullptr;
588 if (InlinedAt)
589 Scope = LScopes.findInlinedScope(DIVar->getScope(), InlinedAt);
590 else
591 Scope = LScopes.findLexicalScope(DIVar->getScope());
592 // If variable scope is not found then skip this variable.
593 if (!Scope)
594 continue;
595
596 LocalVariable Var;
597 Var.DIVar = DIVar;
598
599 // Calculate the definition ranges.
600 for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) {
601 const InsnRange &Range = *I;
602 const MachineInstr *DVInst = Range.first;
603 assert(DVInst->isDebugValue() && "Invalid History entry");
604 const DIExpression *DIExpr = DVInst->getDebugExpression();
605
606 // Bail if there is a complex DWARF expression for now.
607 if (DIExpr && DIExpr->getNumElements() > 0)
608 continue;
609
Reid Kleckner9a593ee2016-02-16 21:49:26 +0000610 // Bail if operand 0 is not a valid register. This means the variable is a
611 // simple constant, or is described by a complex expression.
612 // FIXME: Find a way to represent constant variables, since they are
613 // relatively common.
614 unsigned Reg =
615 DVInst->getOperand(0).isReg() ? DVInst->getOperand(0).getReg() : 0;
616 if (Reg == 0)
Reid Kleckner6e0d5f52016-02-16 21:14:51 +0000617 continue;
618
Reid Kleckner876330d2016-02-12 21:48:30 +0000619 // Handle the two cases we can handle: indirect in memory and in register.
620 bool IsIndirect = DVInst->getOperand(1).isImm();
621 unsigned CVReg = TRI->getCodeViewRegNum(DVInst->getOperand(0).getReg());
622 {
623 LocalVarDefRange DefRange;
624 if (IsIndirect) {
625 int64_t Offset = DVInst->getOperand(1).getImm();
626 DefRange = createDefRangeMem(CVReg, Offset);
627 } else {
628 DefRange = createDefRangeReg(CVReg);
629 }
630 if (Var.DefRanges.empty() ||
631 Var.DefRanges.back().isDifferentLocation(DefRange)) {
632 Var.DefRanges.emplace_back(std::move(DefRange));
633 }
634 }
635
636 // Compute the label range.
637 const MCSymbol *Begin = getLabelBeforeInsn(Range.first);
638 const MCSymbol *End = getLabelAfterInsn(Range.second);
639 if (!End) {
640 if (std::next(I) != E)
641 End = getLabelBeforeInsn(std::next(I)->first);
642 else
643 End = Asm->getFunctionEnd();
644 }
645
646 // If the last range end is our begin, just extend the last range.
647 // Otherwise make a new range.
648 SmallVectorImpl<std::pair<const MCSymbol *, const MCSymbol *>> &Ranges =
649 Var.DefRanges.back().Ranges;
650 if (!Ranges.empty() && Ranges.back().second == Begin)
651 Ranges.back().second = End;
652 else
653 Ranges.emplace_back(Begin, End);
654
655 // FIXME: Do more range combining.
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000656 }
Reid Kleckner876330d2016-02-12 21:48:30 +0000657
658 recordLocalVariable(std::move(Var), InlinedAt);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000659 }
660}
661
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000662void CodeViewDebug::beginFunction(const MachineFunction *MF) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000663 assert(!CurFn && "Can't process two functions at once!");
664
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000665 if (!Asm || !MMI->hasDebugInfo())
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000666 return;
667
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000668 DebugHandlerBase::beginFunction(MF);
669
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000670 const Function *GV = MF->getFunction();
671 assert(FnDebugInfo.count(GV) == false);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000672 CurFn = &FnDebugInfo[GV];
Reid Kleckner2214ed82016-01-29 00:49:42 +0000673 CurFn->FuncId = NextFuncId++;
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000674 CurFn->Begin = Asm->getFunctionBegin();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000675
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000676 // Find the end of the function prolog. First known non-DBG_VALUE and
677 // non-frame setup location marks the beginning of the function body.
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000678 // FIXME: is there a simpler a way to do this? Can we just search
679 // for the first instruction of the function, not the last of the prolog?
680 DebugLoc PrologEndLoc;
681 bool EmptyPrologue = true;
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000682 for (const auto &MBB : *MF) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000683 for (const auto &MI : MBB) {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000684 if (!MI.isDebugValue() && !MI.getFlag(MachineInstr::FrameSetup) &&
685 MI.getDebugLoc()) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000686 PrologEndLoc = MI.getDebugLoc();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000687 break;
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000688 } else if (!MI.isDebugValue()) {
689 EmptyPrologue = false;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000690 }
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000691 }
692 }
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000693
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000694 // Record beginning of function if we have a non-empty prologue.
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000695 if (PrologEndLoc && !EmptyPrologue) {
696 DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000697 maybeRecordLocation(FnStartDL, MF);
698 }
699}
700
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000701void CodeViewDebug::emitLocalVariable(const LocalVariable &Var) {
702 // LocalSym record, see SymbolRecord.h for more info.
703 MCSymbol *LocalBegin = MMI->getContext().createTempSymbol(),
704 *LocalEnd = MMI->getContext().createTempSymbol();
705 OS.AddComment("Record length");
706 OS.emitAbsoluteSymbolDiff(LocalEnd, LocalBegin, 2);
707 OS.EmitLabel(LocalBegin);
708
709 OS.AddComment("Record kind: S_LOCAL");
Zachary Turner63a28462016-05-17 23:50:21 +0000710 OS.EmitIntValue(unsigned(SymbolKind::S_LOCAL), 2);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000711
Zachary Turner63a28462016-05-17 23:50:21 +0000712 LocalSymFlags Flags = LocalSymFlags::None;
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000713 if (Var.DIVar->isParameter())
Zachary Turner63a28462016-05-17 23:50:21 +0000714 Flags |= LocalSymFlags::IsParameter;
Reid Kleckner876330d2016-02-12 21:48:30 +0000715 if (Var.DefRanges.empty())
Zachary Turner63a28462016-05-17 23:50:21 +0000716 Flags |= LocalSymFlags::IsOptimizedOut;
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000717
718 OS.AddComment("TypeIndex");
719 OS.EmitIntValue(TypeIndex::Int32().getIndex(), 4);
720 OS.AddComment("Flags");
Zachary Turner63a28462016-05-17 23:50:21 +0000721 OS.EmitIntValue(static_cast<uint16_t>(Flags), 2);
David Majnemer12561252016-03-13 10:53:30 +0000722 // Truncate the name so we won't overflow the record length field.
David Majnemerb9456a52016-03-14 05:15:09 +0000723 emitNullTerminatedSymbolName(OS, Var.DIVar->getName());
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000724 OS.EmitLabel(LocalEnd);
725
Reid Kleckner876330d2016-02-12 21:48:30 +0000726 // Calculate the on disk prefix of the appropriate def range record. The
727 // records and on disk formats are described in SymbolRecords.h. BytePrefix
728 // should be big enough to hold all forms without memory allocation.
729 SmallString<20> BytePrefix;
730 for (const LocalVarDefRange &DefRange : Var.DefRanges) {
731 BytePrefix.clear();
732 // FIXME: Handle bitpieces.
733 if (DefRange.StructOffset != 0)
734 continue;
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000735
Reid Kleckner876330d2016-02-12 21:48:30 +0000736 if (DefRange.InMemory) {
737 DefRangeRegisterRelSym Sym{};
738 ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER_REL);
739 Sym.BaseRegister = DefRange.CVRegister;
740 Sym.Flags = 0; // Unclear what matters here.
741 Sym.BasePointerOffset = DefRange.DataOffset;
742 BytePrefix +=
743 StringRef(reinterpret_cast<const char *>(&SymKind), sizeof(SymKind));
744 BytePrefix += StringRef(reinterpret_cast<const char *>(&Sym),
745 sizeof(Sym) - sizeof(LocalVariableAddrRange));
746 } else {
747 assert(DefRange.DataOffset == 0 && "unexpected offset into register");
748 DefRangeRegisterSym Sym{};
749 ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER);
750 Sym.Register = DefRange.CVRegister;
751 Sym.MayHaveNoName = 0; // Unclear what matters here.
752 BytePrefix +=
753 StringRef(reinterpret_cast<const char *>(&SymKind), sizeof(SymKind));
754 BytePrefix += StringRef(reinterpret_cast<const char *>(&Sym),
755 sizeof(Sym) - sizeof(LocalVariableAddrRange));
756 }
757 OS.EmitCVDefRangeDirective(DefRange.Ranges, BytePrefix);
758 }
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000759}
760
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000761void CodeViewDebug::endFunction(const MachineFunction *MF) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000762 if (!Asm || !CurFn) // We haven't created any debug info for this function.
763 return;
764
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000765 const Function *GV = MF->getFunction();
Yaron Keren6d3194f2014-06-20 10:26:56 +0000766 assert(FnDebugInfo.count(GV));
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000767 assert(CurFn == &FnDebugInfo[GV]);
768
Pete Cooperadebb932016-03-11 02:14:16 +0000769 collectVariableInfo(GV->getSubprogram());
Reid Kleckner876330d2016-02-12 21:48:30 +0000770
771 DebugHandlerBase::endFunction(MF);
772
Reid Kleckner2214ed82016-01-29 00:49:42 +0000773 // Don't emit anything if we don't have any line tables.
774 if (!CurFn->HaveLineInfo) {
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000775 FnDebugInfo.erase(GV);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000776 CurFn = nullptr;
777 return;
Timur Iskhodzhanov8499a122014-03-26 09:50:36 +0000778 }
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000779
780 CurFn->End = Asm->getFunctionEnd();
781
Craig Topper353eda42014-04-24 06:44:33 +0000782 CurFn = nullptr;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000783}
784
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000785void CodeViewDebug::beginInstruction(const MachineInstr *MI) {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000786 DebugHandlerBase::beginInstruction(MI);
787
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000788 // Ignore DBG_VALUE locations and function prologue.
789 if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup))
790 return;
791 DebugLoc DL = MI->getDebugLoc();
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000792 if (DL == PrevInstLoc || !DL)
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000793 return;
794 maybeRecordLocation(DL, Asm->MF);
795}