blob: 0eb3e44fba18ee24cb2195c984c8aa34fb6a04c0 [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 Kleckner2280f932016-05-23 20:23:46 +0000116 InlinedSubprograms.insert(Inlinee);
117 recordFuncIdForSubprogram(Inlinee);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000118 }
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000119 return *Site;
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000120}
121
Reid Kleckner2280f932016-05-23 20:23:46 +0000122TypeIndex CodeViewDebug::getGenericFunctionTypeIndex() {
123 if (VoidFnTyIdx.getIndex() != 0)
124 return VoidFnTyIdx;
125
126 ArrayRef<TypeIndex> NoArgs;
127 ArgListRecord ArgListRec(TypeRecordKind::ArgList, NoArgs);
128 TypeIndex ArgListIndex = TypeTable.writeArgList(ArgListRec);
129
130 ProcedureRecord Procedure(TypeIndex::Void(), CallingConvention::NearC,
131 FunctionOptions::None, 0, ArgListIndex);
132 VoidFnTyIdx = TypeTable.writeProcedure(Procedure);
133 return VoidFnTyIdx;
134}
135
136void CodeViewDebug::recordFuncIdForSubprogram(const DISubprogram *SP) {
137 TypeIndex ParentScope = TypeIndex(0);
138 StringRef DisplayName = SP->getDisplayName();
139 FuncIdRecord FuncId(ParentScope, getGenericFunctionTypeIndex(), DisplayName);
140 TypeIndex TI = TypeTable.writeFuncId(FuncId);
141 TypeIndices[SP] = TI;
142}
143
Reid Kleckner876330d2016-02-12 21:48:30 +0000144void CodeViewDebug::recordLocalVariable(LocalVariable &&Var,
145 const DILocation *InlinedAt) {
146 if (InlinedAt) {
147 // This variable was inlined. Associate it with the InlineSite.
148 const DISubprogram *Inlinee = Var.DIVar->getScope()->getSubprogram();
149 InlineSite &Site = getInlineSite(InlinedAt, Inlinee);
150 Site.InlinedLocals.emplace_back(Var);
151 } else {
152 // This variable goes in the main ProcSym.
153 CurFn->Locals.emplace_back(Var);
154 }
155}
156
Reid Kleckner829365a2016-02-11 19:41:47 +0000157static void addLocIfNotPresent(SmallVectorImpl<const DILocation *> &Locs,
158 const DILocation *Loc) {
159 auto B = Locs.begin(), E = Locs.end();
160 if (std::find(B, E, Loc) == E)
161 Locs.push_back(Loc);
162}
163
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000164void CodeViewDebug::maybeRecordLocation(DebugLoc DL,
Reid Kleckner9533af42016-01-16 00:09:09 +0000165 const MachineFunction *MF) {
166 // Skip this instruction if it has the same location as the previous one.
167 if (DL == CurFn->LastLoc)
168 return;
169
170 const DIScope *Scope = DL.get()->getScope();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000171 if (!Scope)
172 return;
Reid Kleckner9533af42016-01-16 00:09:09 +0000173
David Majnemerc3340db2016-01-13 01:05:23 +0000174 // Skip this line if it is longer than the maximum we can record.
Reid Kleckner2214ed82016-01-29 00:49:42 +0000175 LineInfo LI(DL.getLine(), DL.getLine(), /*IsStatement=*/true);
176 if (LI.getStartLine() != DL.getLine() || LI.isAlwaysStepInto() ||
177 LI.isNeverStepInto())
David Majnemerc3340db2016-01-13 01:05:23 +0000178 return;
179
Reid Kleckner2214ed82016-01-29 00:49:42 +0000180 ColumnInfo CI(DL.getCol(), /*EndColumn=*/0);
181 if (CI.getStartColumn() != DL.getCol())
182 return;
Reid Kleckner00d96392016-01-29 00:13:28 +0000183
Reid Kleckner2214ed82016-01-29 00:49:42 +0000184 if (!CurFn->HaveLineInfo)
185 CurFn->HaveLineInfo = true;
186 unsigned FileId = 0;
187 if (CurFn->LastLoc.get() && CurFn->LastLoc->getFile() == DL->getFile())
188 FileId = CurFn->LastFileId;
189 else
190 FileId = CurFn->LastFileId = maybeRecordFile(DL->getFile());
191 CurFn->LastLoc = DL;
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000192
193 unsigned FuncId = CurFn->FuncId;
Reid Kleckner876330d2016-02-12 21:48:30 +0000194 if (const DILocation *SiteLoc = DL->getInlinedAt()) {
Reid Kleckner829365a2016-02-11 19:41:47 +0000195 const DILocation *Loc = DL.get();
196
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000197 // If this location was actually inlined from somewhere else, give it the ID
198 // of the inline call site.
Reid Kleckner876330d2016-02-12 21:48:30 +0000199 FuncId =
200 getInlineSite(SiteLoc, Loc->getScope()->getSubprogram()).SiteFuncId;
Reid Kleckner829365a2016-02-11 19:41:47 +0000201
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000202 // Ensure we have links in the tree of inline call sites.
Reid Kleckner829365a2016-02-11 19:41:47 +0000203 bool FirstLoc = true;
204 while ((SiteLoc = Loc->getInlinedAt())) {
Reid Kleckner876330d2016-02-12 21:48:30 +0000205 InlineSite &Site =
206 getInlineSite(SiteLoc, Loc->getScope()->getSubprogram());
Reid Kleckner829365a2016-02-11 19:41:47 +0000207 if (!FirstLoc)
208 addLocIfNotPresent(Site.ChildSites, Loc);
209 FirstLoc = false;
210 Loc = SiteLoc;
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000211 }
Reid Kleckner829365a2016-02-11 19:41:47 +0000212 addLocIfNotPresent(CurFn->ChildSites, Loc);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000213 }
214
Reid Klecknerdac21b42016-02-03 21:15:48 +0000215 OS.EmitCVLocDirective(FuncId, FileId, DL.getLine(), DL.getCol(),
216 /*PrologueEnd=*/false,
217 /*IsStmt=*/false, DL->getFilename());
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000218}
219
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000220void CodeViewDebug::endModule() {
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000221 if (FnDebugInfo.empty())
222 return;
223
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000224 emitTypeInformation();
225
Reid Kleckner6b3faef2016-01-13 23:44:57 +0000226 // FIXME: For functions that are comdat, we should emit separate .debug$S
227 // sections that are comdat associative with the main function instead of
228 // having one big .debug$S section.
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000229 assert(Asm != nullptr);
Reid Klecknerdac21b42016-02-03 21:15:48 +0000230 OS.SwitchSection(Asm->getObjFileLowering().getCOFFDebugSymbolsSection());
231 OS.AddComment("Debug section magic");
232 OS.EmitIntValue(COFF::DEBUG_SECTION_MAGIC, 4);
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000233
234 // The COFF .debug$S section consists of several subsections, each starting
235 // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length
236 // of the payload followed by the payload itself. The subsections are 4-byte
237 // aligned.
238
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000239 // Make a subsection for all the inlined subprograms.
Reid Klecknerfbd77872016-03-18 18:54:32 +0000240 emitInlineeFuncIdsAndLines();
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000241
Reid Kleckner2214ed82016-01-29 00:49:42 +0000242 // Emit per-function debug information.
243 for (auto &P : FnDebugInfo)
244 emitDebugInfoForFunction(P.first, P.second);
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000245
246 // This subsection holds a file index to offset in string table table.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000247 OS.AddComment("File index to string table offset subsection");
248 OS.EmitCVFileChecksumsDirective();
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000249
250 // This subsection holds the string table.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000251 OS.AddComment("String table");
252 OS.EmitCVStringTableDirective();
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000253
254 clear();
255}
256
David Majnemerb9456a52016-03-14 05:15:09 +0000257static void emitNullTerminatedSymbolName(MCStreamer &OS, StringRef S) {
258 // Microsoft's linker seems to have trouble with symbol names longer than
259 // 0xffd8 bytes.
260 S = S.substr(0, 0xffd8);
261 SmallString<32> NullTerminatedString(S);
262 NullTerminatedString.push_back('\0');
263 OS.EmitBytes(NullTerminatedString);
264}
265
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000266void CodeViewDebug::emitTypeInformation() {
Reid Kleckner2280f932016-05-23 20:23:46 +0000267 // Do nothing if we have no debug info or if no non-trivial types were emitted
268 // to TypeTable during codegen.
Reid Klecknerfbd77872016-03-18 18:54:32 +0000269 NamedMDNode *CU_Nodes =
270 MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
271 if (!CU_Nodes)
272 return;
Reid Kleckner2280f932016-05-23 20:23:46 +0000273 if (TypeTable.empty())
Reid Klecknerfbd77872016-03-18 18:54:32 +0000274 return;
275
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000276 // Start the .debug$T section with 0x4.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000277 OS.SwitchSection(Asm->getObjFileLowering().getCOFFDebugTypesSection());
278 OS.AddComment("Debug section magic");
Reid Kleckner2280f932016-05-23 20:23:46 +0000279 OS.EmitValueToAlignment(4);
Reid Klecknerdac21b42016-02-03 21:15:48 +0000280 OS.EmitIntValue(COFF::DEBUG_SECTION_MAGIC, 4);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000281
Reid Kleckner2280f932016-05-23 20:23:46 +0000282 TypeTable.ForEachRecord(
283 [&](TypeIndex Index, const MemoryTypeTableBuilder::Record *R) {
284 // Each record should be 4 byte aligned. We achieve that by emitting
285 // LF_PAD padding bytes. The on-disk record size includes the padding
286 // bytes so that consumers don't have to skip past them.
287 uint64_t RecordSize = R->size() + 2;
288 uint64_t AlignedSize = alignTo(RecordSize, 4);
289 uint64_t AlignedRecordSize = AlignedSize - 2;
290 assert(AlignedRecordSize < (1 << 16) && "type record size overflow");
291 OS.AddComment("Type record length");
292 OS.EmitIntValue(AlignedRecordSize, 2);
293 OS.AddComment("Type record data");
294 OS.EmitBytes(StringRef(R->data(), R->size()));
295 // Pad the record with LF_PAD bytes.
296 for (unsigned I = AlignedSize - RecordSize; I > 0; --I)
297 OS.EmitIntValue(LF_PAD0 + I, 1);
298 });
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000299}
300
Reid Klecknerfbd77872016-03-18 18:54:32 +0000301void CodeViewDebug::emitInlineeFuncIdsAndLines() {
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000302 if (InlinedSubprograms.empty())
303 return;
304
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000305 MCSymbol *InlineBegin = MMI->getContext().createTempSymbol(),
306 *InlineEnd = MMI->getContext().createTempSymbol();
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000307
308 OS.AddComment("Inlinee lines subsection");
309 OS.EmitIntValue(unsigned(ModuleSubstreamKind::InlineeLines), 4);
David Majnemer30579ec2016-02-02 23:18:23 +0000310 OS.AddComment("Subsection size");
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000311 OS.emitAbsoluteSymbolDiff(InlineEnd, InlineBegin, 4);
312 OS.EmitLabel(InlineBegin);
313
314 // We don't provide any extra file info.
315 // FIXME: Find out if debuggers use this info.
David Majnemer30579ec2016-02-02 23:18:23 +0000316 OS.AddComment("Inlinee lines signature");
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000317 OS.EmitIntValue(unsigned(InlineeLinesSignature::Normal), 4);
318
319 for (const DISubprogram *SP : InlinedSubprograms) {
Reid Kleckner2280f932016-05-23 20:23:46 +0000320 assert(TypeIndices.count(SP));
321 TypeIndex InlineeIdx = TypeIndices[SP];
322
David Majnemer30579ec2016-02-02 23:18:23 +0000323 OS.AddBlankLine();
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000324 unsigned FileId = maybeRecordFile(SP->getFile());
325 OS.AddComment("Inlined function " + SP->getDisplayName() + " starts at " +
326 SP->getFilename() + Twine(':') + Twine(SP->getLine()));
David Majnemer30579ec2016-02-02 23:18:23 +0000327 OS.AddBlankLine();
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000328 // The filechecksum table uses 8 byte entries for now, and file ids start at
329 // 1.
330 unsigned FileOffset = (FileId - 1) * 8;
David Majnemer30579ec2016-02-02 23:18:23 +0000331 OS.AddComment("Type index of inlined function");
Reid Kleckner2280f932016-05-23 20:23:46 +0000332 OS.EmitIntValue(InlineeIdx.getIndex(), 4);
David Majnemer30579ec2016-02-02 23:18:23 +0000333 OS.AddComment("Offset into filechecksum table");
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000334 OS.EmitIntValue(FileOffset, 4);
David Majnemer30579ec2016-02-02 23:18:23 +0000335 OS.AddComment("Starting line number");
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000336 OS.EmitIntValue(SP->getLine(), 4);
337 }
338
339 OS.EmitLabel(InlineEnd);
340}
341
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000342void CodeViewDebug::collectInlineSiteChildren(
343 SmallVectorImpl<unsigned> &Children, const FunctionInfo &FI,
344 const InlineSite &Site) {
345 for (const DILocation *ChildSiteLoc : Site.ChildSites) {
346 auto I = FI.InlineSites.find(ChildSiteLoc);
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000347 const InlineSite &ChildSite = I->second;
348 Children.push_back(ChildSite.SiteFuncId);
349 collectInlineSiteChildren(Children, FI, ChildSite);
350 }
351}
352
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000353void CodeViewDebug::emitInlinedCallSite(const FunctionInfo &FI,
354 const DILocation *InlinedAt,
355 const InlineSite &Site) {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000356 MCSymbol *InlineBegin = MMI->getContext().createTempSymbol(),
357 *InlineEnd = MMI->getContext().createTempSymbol();
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000358
Reid Kleckner2280f932016-05-23 20:23:46 +0000359 assert(TypeIndices.count(Site.Inlinee));
360 TypeIndex InlineeIdx = TypeIndices[Site.Inlinee];
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000361
362 // SymbolRecord
Reid Klecknerdac21b42016-02-03 21:15:48 +0000363 OS.AddComment("Record length");
Reid Klecknereb3bcdd2016-02-03 21:24:42 +0000364 OS.emitAbsoluteSymbolDiff(InlineEnd, InlineBegin, 2); // RecordLength
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000365 OS.EmitLabel(InlineBegin);
Reid Klecknerdac21b42016-02-03 21:15:48 +0000366 OS.AddComment("Record kind: S_INLINESITE");
Zachary Turner63a28462016-05-17 23:50:21 +0000367 OS.EmitIntValue(SymbolKind::S_INLINESITE, 2); // RecordKind
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000368
Reid Klecknerdac21b42016-02-03 21:15:48 +0000369 OS.AddComment("PtrParent");
370 OS.EmitIntValue(0, 4);
371 OS.AddComment("PtrEnd");
372 OS.EmitIntValue(0, 4);
373 OS.AddComment("Inlinee type index");
Reid Kleckner2280f932016-05-23 20:23:46 +0000374 OS.EmitIntValue(InlineeIdx.getIndex(), 4);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000375
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000376 unsigned FileId = maybeRecordFile(Site.Inlinee->getFile());
377 unsigned StartLineNum = Site.Inlinee->getLine();
378 SmallVector<unsigned, 3> SecondaryFuncIds;
379 collectInlineSiteChildren(SecondaryFuncIds, FI, Site);
380
381 OS.EmitCVInlineLinetableDirective(Site.SiteFuncId, FileId, StartLineNum,
David Majnemerc9911f22016-02-02 19:22:34 +0000382 FI.Begin, FI.End, SecondaryFuncIds);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000383
384 OS.EmitLabel(InlineEnd);
385
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000386 for (const LocalVariable &Var : Site.InlinedLocals)
387 emitLocalVariable(Var);
388
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000389 // Recurse on child inlined call sites before closing the scope.
390 for (const DILocation *ChildSite : Site.ChildSites) {
391 auto I = FI.InlineSites.find(ChildSite);
392 assert(I != FI.InlineSites.end() &&
393 "child site not in function inline site map");
394 emitInlinedCallSite(FI, ChildSite, I->second);
395 }
396
397 // Close the scope.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000398 OS.AddComment("Record length");
399 OS.EmitIntValue(2, 2); // RecordLength
400 OS.AddComment("Record kind: S_INLINESITE_END");
Zachary Turner63a28462016-05-17 23:50:21 +0000401 OS.EmitIntValue(SymbolKind::S_INLINESITE_END, 2); // RecordKind
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000402}
403
Reid Kleckner2214ed82016-01-29 00:49:42 +0000404void CodeViewDebug::emitDebugInfoForFunction(const Function *GV,
405 FunctionInfo &FI) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000406 // For each function there is a separate subsection
407 // which holds the PC to file:line table.
408 const MCSymbol *Fn = Asm->getSymbol(GV);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000409 assert(Fn);
Timur Iskhodzhanov8499a122014-03-26 09:50:36 +0000410
Duncan P. N. Exon Smith23e56ec2015-03-20 19:50:00 +0000411 StringRef FuncName;
Pete Cooperadebb932016-03-11 02:14:16 +0000412 if (auto *SP = GV->getSubprogram())
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000413 FuncName = SP->getDisplayName();
Duncan P. N. Exon Smith23e56ec2015-03-20 19:50:00 +0000414
Reid Kleckner3c0ff982016-01-14 00:12:54 +0000415 // If our DISubprogram name is empty, use the mangled name.
Reid Kleckner72e2ba72016-01-13 19:32:35 +0000416 if (FuncName.empty())
417 FuncName = GlobalValue::getRealLinkageName(GV->getName());
Reid Kleckner3c0ff982016-01-14 00:12:54 +0000418
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000419 // Emit a symbol subsection, required by VS2012+ to find function boundaries.
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000420 MCSymbol *SymbolsBegin = MMI->getContext().createTempSymbol(),
421 *SymbolsEnd = MMI->getContext().createTempSymbol();
Reid Klecknerdac21b42016-02-03 21:15:48 +0000422 OS.AddComment("Symbol subsection for " + Twine(FuncName));
423 OS.EmitIntValue(unsigned(ModuleSubstreamKind::Symbols), 4);
424 OS.AddComment("Subsection size");
Reid Klecknereb3bcdd2016-02-03 21:24:42 +0000425 OS.emitAbsoluteSymbolDiff(SymbolsEnd, SymbolsBegin, 4);
Reid Klecknerdac21b42016-02-03 21:15:48 +0000426 OS.EmitLabel(SymbolsBegin);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000427 {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000428 MCSymbol *ProcRecordBegin = MMI->getContext().createTempSymbol(),
429 *ProcRecordEnd = MMI->getContext().createTempSymbol();
Reid Klecknerdac21b42016-02-03 21:15:48 +0000430 OS.AddComment("Record length");
Reid Klecknereb3bcdd2016-02-03 21:24:42 +0000431 OS.emitAbsoluteSymbolDiff(ProcRecordEnd, ProcRecordBegin, 2);
Reid Klecknerdac21b42016-02-03 21:15:48 +0000432 OS.EmitLabel(ProcRecordBegin);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000433
Reid Klecknerdac21b42016-02-03 21:15:48 +0000434 OS.AddComment("Record kind: S_GPROC32_ID");
Zachary Turner63a28462016-05-17 23:50:21 +0000435 OS.EmitIntValue(unsigned(SymbolKind::S_GPROC32_ID), 2);
Reid Kleckner6b3faef2016-01-13 23:44:57 +0000436
David Majnemer30579ec2016-02-02 23:18:23 +0000437 // These fields are filled in by tools like CVPACK which run after the fact.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000438 OS.AddComment("PtrParent");
439 OS.EmitIntValue(0, 4);
440 OS.AddComment("PtrEnd");
441 OS.EmitIntValue(0, 4);
442 OS.AddComment("PtrNext");
443 OS.EmitIntValue(0, 4);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000444 // This is the important bit that tells the debugger where the function
445 // code is located and what's its size:
Reid Klecknerdac21b42016-02-03 21:15:48 +0000446 OS.AddComment("Code size");
Reid Klecknereb3bcdd2016-02-03 21:24:42 +0000447 OS.emitAbsoluteSymbolDiff(FI.End, Fn, 4);
Reid Klecknerdac21b42016-02-03 21:15:48 +0000448 OS.AddComment("Offset after prologue");
449 OS.EmitIntValue(0, 4);
450 OS.AddComment("Offset before epilogue");
451 OS.EmitIntValue(0, 4);
452 OS.AddComment("Function type index");
453 OS.EmitIntValue(0, 4);
454 OS.AddComment("Function section relative address");
455 OS.EmitCOFFSecRel32(Fn);
456 OS.AddComment("Function section index");
457 OS.EmitCOFFSectionIndex(Fn);
458 OS.AddComment("Flags");
459 OS.EmitIntValue(0, 1);
Timur Iskhodzhanova11b32b2014-11-12 20:10:09 +0000460 // Emit the function display name as a null-terminated string.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000461 OS.AddComment("Function name");
David Majnemer12561252016-03-13 10:53:30 +0000462 // Truncate the name so we won't overflow the record length field.
David Majnemerb9456a52016-03-14 05:15:09 +0000463 emitNullTerminatedSymbolName(OS, FuncName);
Reid Klecknerdac21b42016-02-03 21:15:48 +0000464 OS.EmitLabel(ProcRecordEnd);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000465
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000466 for (const LocalVariable &Var : FI.Locals)
467 emitLocalVariable(Var);
468
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000469 // Emit inlined call site information. Only emit functions inlined directly
470 // into the parent function. We'll emit the other sites recursively as part
471 // of their parent inline site.
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000472 for (const DILocation *InlinedAt : FI.ChildSites) {
473 auto I = FI.InlineSites.find(InlinedAt);
474 assert(I != FI.InlineSites.end() &&
475 "child site not in function inline site map");
476 emitInlinedCallSite(FI, InlinedAt, I->second);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000477 }
478
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000479 // We're done with this function.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000480 OS.AddComment("Record length");
481 OS.EmitIntValue(0x0002, 2);
482 OS.AddComment("Record kind: S_PROC_ID_END");
Zachary Turner63a28462016-05-17 23:50:21 +0000483 OS.EmitIntValue(unsigned(SymbolKind::S_PROC_ID_END), 2);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000484 }
Reid Klecknerdac21b42016-02-03 21:15:48 +0000485 OS.EmitLabel(SymbolsEnd);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000486 // Every subsection must be aligned to a 4-byte boundary.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000487 OS.EmitValueToAlignment(4);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000488
Reid Kleckner2214ed82016-01-29 00:49:42 +0000489 // We have an assembler directive that takes care of the whole line table.
Reid Klecknerdac21b42016-02-03 21:15:48 +0000490 OS.EmitCVLinetableDirective(FI.FuncId, Fn, FI.End);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000491}
492
Reid Kleckner876330d2016-02-12 21:48:30 +0000493CodeViewDebug::LocalVarDefRange
494CodeViewDebug::createDefRangeMem(uint16_t CVRegister, int Offset) {
495 LocalVarDefRange DR;
Aaron Ballmanc6a2f212016-02-16 15:35:51 +0000496 DR.InMemory = -1;
Reid Kleckner876330d2016-02-12 21:48:30 +0000497 DR.DataOffset = Offset;
498 assert(DR.DataOffset == Offset && "truncation");
499 DR.StructOffset = 0;
500 DR.CVRegister = CVRegister;
501 return DR;
502}
503
504CodeViewDebug::LocalVarDefRange
505CodeViewDebug::createDefRangeReg(uint16_t CVRegister) {
506 LocalVarDefRange DR;
507 DR.InMemory = 0;
508 DR.DataOffset = 0;
509 DR.StructOffset = 0;
510 DR.CVRegister = CVRegister;
511 return DR;
512}
513
514void CodeViewDebug::collectVariableInfoFromMMITable(
515 DenseSet<InlinedVariable> &Processed) {
516 const TargetSubtargetInfo &TSI = Asm->MF->getSubtarget();
517 const TargetFrameLowering *TFI = TSI.getFrameLowering();
518 const TargetRegisterInfo *TRI = TSI.getRegisterInfo();
519
520 for (const MachineModuleInfo::VariableDbgInfo &VI :
521 MMI->getVariableDbgInfo()) {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000522 if (!VI.Var)
523 continue;
524 assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) &&
525 "Expected inlined-at fields to agree");
526
Reid Kleckner876330d2016-02-12 21:48:30 +0000527 Processed.insert(InlinedVariable(VI.Var, VI.Loc->getInlinedAt()));
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000528 LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc);
529
530 // If variable scope is not found then skip this variable.
531 if (!Scope)
532 continue;
533
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000534 // Get the frame register used and the offset.
535 unsigned FrameReg = 0;
Reid Kleckner876330d2016-02-12 21:48:30 +0000536 int FrameOffset = TFI->getFrameIndexReference(*Asm->MF, VI.Slot, FrameReg);
537 uint16_t CVReg = TRI->getCodeViewRegNum(FrameReg);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000538
539 // Calculate the label ranges.
Reid Kleckner876330d2016-02-12 21:48:30 +0000540 LocalVarDefRange DefRange = createDefRangeMem(CVReg, FrameOffset);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000541 for (const InsnRange &Range : Scope->getRanges()) {
542 const MCSymbol *Begin = getLabelBeforeInsn(Range.first);
543 const MCSymbol *End = getLabelAfterInsn(Range.second);
Reid Kleckner876330d2016-02-12 21:48:30 +0000544 End = End ? End : Asm->getFunctionEnd();
545 DefRange.Ranges.emplace_back(Begin, End);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000546 }
547
Reid Kleckner876330d2016-02-12 21:48:30 +0000548 LocalVariable Var;
549 Var.DIVar = VI.Var;
550 Var.DefRanges.emplace_back(std::move(DefRange));
551 recordLocalVariable(std::move(Var), VI.Loc->getInlinedAt());
552 }
553}
554
555void CodeViewDebug::collectVariableInfo(const DISubprogram *SP) {
556 DenseSet<InlinedVariable> Processed;
557 // Grab the variable info that was squirreled away in the MMI side-table.
558 collectVariableInfoFromMMITable(Processed);
559
560 const TargetRegisterInfo *TRI = Asm->MF->getSubtarget().getRegisterInfo();
561
562 for (const auto &I : DbgValues) {
563 InlinedVariable IV = I.first;
564 if (Processed.count(IV))
565 continue;
566 const DILocalVariable *DIVar = IV.first;
567 const DILocation *InlinedAt = IV.second;
568
569 // Instruction ranges, specifying where IV is accessible.
570 const auto &Ranges = I.second;
571
572 LexicalScope *Scope = nullptr;
573 if (InlinedAt)
574 Scope = LScopes.findInlinedScope(DIVar->getScope(), InlinedAt);
575 else
576 Scope = LScopes.findLexicalScope(DIVar->getScope());
577 // If variable scope is not found then skip this variable.
578 if (!Scope)
579 continue;
580
581 LocalVariable Var;
582 Var.DIVar = DIVar;
583
584 // Calculate the definition ranges.
585 for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) {
586 const InsnRange &Range = *I;
587 const MachineInstr *DVInst = Range.first;
588 assert(DVInst->isDebugValue() && "Invalid History entry");
589 const DIExpression *DIExpr = DVInst->getDebugExpression();
590
591 // Bail if there is a complex DWARF expression for now.
592 if (DIExpr && DIExpr->getNumElements() > 0)
593 continue;
594
Reid Kleckner9a593ee2016-02-16 21:49:26 +0000595 // Bail if operand 0 is not a valid register. This means the variable is a
596 // simple constant, or is described by a complex expression.
597 // FIXME: Find a way to represent constant variables, since they are
598 // relatively common.
599 unsigned Reg =
600 DVInst->getOperand(0).isReg() ? DVInst->getOperand(0).getReg() : 0;
601 if (Reg == 0)
Reid Kleckner6e0d5f52016-02-16 21:14:51 +0000602 continue;
603
Reid Kleckner876330d2016-02-12 21:48:30 +0000604 // Handle the two cases we can handle: indirect in memory and in register.
605 bool IsIndirect = DVInst->getOperand(1).isImm();
606 unsigned CVReg = TRI->getCodeViewRegNum(DVInst->getOperand(0).getReg());
607 {
608 LocalVarDefRange DefRange;
609 if (IsIndirect) {
610 int64_t Offset = DVInst->getOperand(1).getImm();
611 DefRange = createDefRangeMem(CVReg, Offset);
612 } else {
613 DefRange = createDefRangeReg(CVReg);
614 }
615 if (Var.DefRanges.empty() ||
616 Var.DefRanges.back().isDifferentLocation(DefRange)) {
617 Var.DefRanges.emplace_back(std::move(DefRange));
618 }
619 }
620
621 // Compute the label range.
622 const MCSymbol *Begin = getLabelBeforeInsn(Range.first);
623 const MCSymbol *End = getLabelAfterInsn(Range.second);
624 if (!End) {
625 if (std::next(I) != E)
626 End = getLabelBeforeInsn(std::next(I)->first);
627 else
628 End = Asm->getFunctionEnd();
629 }
630
631 // If the last range end is our begin, just extend the last range.
632 // Otherwise make a new range.
633 SmallVectorImpl<std::pair<const MCSymbol *, const MCSymbol *>> &Ranges =
634 Var.DefRanges.back().Ranges;
635 if (!Ranges.empty() && Ranges.back().second == Begin)
636 Ranges.back().second = End;
637 else
638 Ranges.emplace_back(Begin, End);
639
640 // FIXME: Do more range combining.
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000641 }
Reid Kleckner876330d2016-02-12 21:48:30 +0000642
643 recordLocalVariable(std::move(Var), InlinedAt);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000644 }
645}
646
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000647void CodeViewDebug::beginFunction(const MachineFunction *MF) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000648 assert(!CurFn && "Can't process two functions at once!");
649
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000650 if (!Asm || !MMI->hasDebugInfo())
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000651 return;
652
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000653 DebugHandlerBase::beginFunction(MF);
654
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000655 const Function *GV = MF->getFunction();
656 assert(FnDebugInfo.count(GV) == false);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000657 CurFn = &FnDebugInfo[GV];
Reid Kleckner2214ed82016-01-29 00:49:42 +0000658 CurFn->FuncId = NextFuncId++;
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000659 CurFn->Begin = Asm->getFunctionBegin();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000660
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000661 // Find the end of the function prolog. First known non-DBG_VALUE and
662 // non-frame setup location marks the beginning of the function body.
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000663 // FIXME: is there a simpler a way to do this? Can we just search
664 // for the first instruction of the function, not the last of the prolog?
665 DebugLoc PrologEndLoc;
666 bool EmptyPrologue = true;
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000667 for (const auto &MBB : *MF) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000668 for (const auto &MI : MBB) {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000669 if (!MI.isDebugValue() && !MI.getFlag(MachineInstr::FrameSetup) &&
670 MI.getDebugLoc()) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000671 PrologEndLoc = MI.getDebugLoc();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000672 break;
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000673 } else if (!MI.isDebugValue()) {
674 EmptyPrologue = false;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000675 }
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000676 }
677 }
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000678
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000679 // Record beginning of function if we have a non-empty prologue.
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000680 if (PrologEndLoc && !EmptyPrologue) {
681 DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000682 maybeRecordLocation(FnStartDL, MF);
683 }
684}
685
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000686void CodeViewDebug::emitLocalVariable(const LocalVariable &Var) {
687 // LocalSym record, see SymbolRecord.h for more info.
688 MCSymbol *LocalBegin = MMI->getContext().createTempSymbol(),
689 *LocalEnd = MMI->getContext().createTempSymbol();
690 OS.AddComment("Record length");
691 OS.emitAbsoluteSymbolDiff(LocalEnd, LocalBegin, 2);
692 OS.EmitLabel(LocalBegin);
693
694 OS.AddComment("Record kind: S_LOCAL");
Zachary Turner63a28462016-05-17 23:50:21 +0000695 OS.EmitIntValue(unsigned(SymbolKind::S_LOCAL), 2);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000696
Zachary Turner63a28462016-05-17 23:50:21 +0000697 LocalSymFlags Flags = LocalSymFlags::None;
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000698 if (Var.DIVar->isParameter())
Zachary Turner63a28462016-05-17 23:50:21 +0000699 Flags |= LocalSymFlags::IsParameter;
Reid Kleckner876330d2016-02-12 21:48:30 +0000700 if (Var.DefRanges.empty())
Zachary Turner63a28462016-05-17 23:50:21 +0000701 Flags |= LocalSymFlags::IsOptimizedOut;
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000702
703 OS.AddComment("TypeIndex");
704 OS.EmitIntValue(TypeIndex::Int32().getIndex(), 4);
705 OS.AddComment("Flags");
Zachary Turner63a28462016-05-17 23:50:21 +0000706 OS.EmitIntValue(static_cast<uint16_t>(Flags), 2);
David Majnemer12561252016-03-13 10:53:30 +0000707 // Truncate the name so we won't overflow the record length field.
David Majnemerb9456a52016-03-14 05:15:09 +0000708 emitNullTerminatedSymbolName(OS, Var.DIVar->getName());
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000709 OS.EmitLabel(LocalEnd);
710
Reid Kleckner876330d2016-02-12 21:48:30 +0000711 // Calculate the on disk prefix of the appropriate def range record. The
712 // records and on disk formats are described in SymbolRecords.h. BytePrefix
713 // should be big enough to hold all forms without memory allocation.
714 SmallString<20> BytePrefix;
715 for (const LocalVarDefRange &DefRange : Var.DefRanges) {
716 BytePrefix.clear();
717 // FIXME: Handle bitpieces.
718 if (DefRange.StructOffset != 0)
719 continue;
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000720
Reid Kleckner876330d2016-02-12 21:48:30 +0000721 if (DefRange.InMemory) {
Zachary Turnera78ecd12016-05-23 18:49:06 +0000722 DefRangeRegisterRelSym Sym(DefRange.CVRegister, 0, DefRange.DataOffset, 0,
723 0, 0, ArrayRef<LocalVariableAddrGap>());
Reid Kleckner876330d2016-02-12 21:48:30 +0000724 ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER_REL);
Reid Kleckner876330d2016-02-12 21:48:30 +0000725 BytePrefix +=
726 StringRef(reinterpret_cast<const char *>(&SymKind), sizeof(SymKind));
Zachary Turnera78ecd12016-05-23 18:49:06 +0000727 BytePrefix +=
728 StringRef(reinterpret_cast<const char *>(&Sym.Header),
729 sizeof(Sym.Header) - sizeof(LocalVariableAddrRange));
Reid Kleckner876330d2016-02-12 21:48:30 +0000730 } else {
731 assert(DefRange.DataOffset == 0 && "unexpected offset into register");
Zachary Turnera78ecd12016-05-23 18:49:06 +0000732 // Unclear what matters here.
733 DefRangeRegisterSym Sym(DefRange.CVRegister, 0, 0, 0, 0,
734 ArrayRef<LocalVariableAddrGap>());
Reid Kleckner876330d2016-02-12 21:48:30 +0000735 ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER);
Reid Kleckner876330d2016-02-12 21:48:30 +0000736 BytePrefix +=
737 StringRef(reinterpret_cast<const char *>(&SymKind), sizeof(SymKind));
Zachary Turnera78ecd12016-05-23 18:49:06 +0000738 BytePrefix +=
739 StringRef(reinterpret_cast<const char *>(&Sym.Header),
740 sizeof(Sym.Header) - sizeof(LocalVariableAddrRange));
Reid Kleckner876330d2016-02-12 21:48:30 +0000741 }
742 OS.EmitCVDefRangeDirective(DefRange.Ranges, BytePrefix);
743 }
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000744}
745
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000746void CodeViewDebug::endFunction(const MachineFunction *MF) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000747 if (!Asm || !CurFn) // We haven't created any debug info for this function.
748 return;
749
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000750 const Function *GV = MF->getFunction();
Yaron Keren6d3194f2014-06-20 10:26:56 +0000751 assert(FnDebugInfo.count(GV));
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000752 assert(CurFn == &FnDebugInfo[GV]);
753
Pete Cooperadebb932016-03-11 02:14:16 +0000754 collectVariableInfo(GV->getSubprogram());
Reid Kleckner876330d2016-02-12 21:48:30 +0000755
756 DebugHandlerBase::endFunction(MF);
757
Reid Kleckner2214ed82016-01-29 00:49:42 +0000758 // Don't emit anything if we don't have any line tables.
759 if (!CurFn->HaveLineInfo) {
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000760 FnDebugInfo.erase(GV);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000761 CurFn = nullptr;
762 return;
Timur Iskhodzhanov8499a122014-03-26 09:50:36 +0000763 }
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000764
765 CurFn->End = Asm->getFunctionEnd();
766
Craig Topper353eda42014-04-24 06:44:33 +0000767 CurFn = nullptr;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000768}
769
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000770void CodeViewDebug::beginInstruction(const MachineInstr *MI) {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000771 DebugHandlerBase::beginInstruction(MI);
772
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000773 // Ignore DBG_VALUE locations and function prologue.
774 if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup))
775 return;
776 DebugLoc DL = MI->getDebugLoc();
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000777 if (DL == PrevInstLoc || !DL)
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000778 return;
779 maybeRecordLocation(DL, Asm->MF);
780}