1 //===-- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp --*- C++ -*--===//
2 //
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 //
10 // This file contains support for writing Microsoft CodeView debug info.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CodeViewDebug.h"
15 #include "llvm/DebugInfo/CodeView/CodeView.h"
16 #include "llvm/DebugInfo/CodeView/Line.h"
17 #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
18 #include "llvm/DebugInfo/CodeView/TypeIndex.h"
19 #include "llvm/DebugInfo/CodeView/TypeRecord.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/MC/MCSymbol.h"
22 #include "llvm/Support/COFF.h"
23 #include "llvm/Target/TargetSubtargetInfo.h"
24 #include "llvm/Target/TargetRegisterInfo.h"
25 #include "llvm/Target/TargetFrameLowering.h"
26 
27 using namespace llvm;
28 using namespace llvm::codeview;
29 
30 CodeViewDebug::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 }
43 
44 StringRef CodeViewDebug::getFullFilepath(const DIFile *File) {
45   std::string &Filepath = FileToFilepathMap[File];
46   if (!Filepath.empty())
47     return Filepath;
48 
49   StringRef Dir = File->getDirectory(), Filename = File->getFilename();
50 
51   // 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.
55   if (Filename.find(':') == 1)
56     Filepath = Filename;
57   else
58     Filepath = (Dir + "\\" + Filename).str();
59 
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 
93   return Filepath;
94 }
95 
96 unsigned 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);
102     NextId = OS.EmitCVFileDirective(NextId, FullPath);
103     assert(NextId == FileIdMap.size() && ".cv_file directive failed");
104   }
105   return Insertion.first->second;
106 }
107 
108 CodeViewDebug::InlineSite &
109 CodeViewDebug::getInlineSite(const DILocation *InlinedAt,
110                              const DISubprogram *Inlinee) {
111   auto Insertion = CurFn->InlineSites.insert({InlinedAt, InlineSite()});
112   InlineSite *Site = &Insertion.first->second;
113   if (Insertion.second) {
114     Site->SiteFuncId = NextFuncId++;
115     Site->Inlinee = Inlinee;
116     InlinedSubprograms.insert(Inlinee);
117   }
118   return *Site;
119 }
120 
121 void 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 
134 static 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 
141 void CodeViewDebug::maybeRecordLocation(DebugLoc DL,
142                                         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();
148   if (!Scope)
149     return;
150 
151   // Skip this line if it is longer than the maximum we can record.
152   LineInfo LI(DL.getLine(), DL.getLine(), /*IsStatement=*/true);
153   if (LI.getStartLine() != DL.getLine() || LI.isAlwaysStepInto() ||
154       LI.isNeverStepInto())
155     return;
156 
157   ColumnInfo CI(DL.getCol(), /*EndColumn=*/0);
158   if (CI.getStartColumn() != DL.getCol())
159     return;
160 
161   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;
169 
170   unsigned FuncId = CurFn->FuncId;
171   if (const DILocation *SiteLoc = DL->getInlinedAt()) {
172     const DILocation *Loc = DL.get();
173 
174     // If this location was actually inlined from somewhere else, give it the ID
175     // of the inline call site.
176     FuncId =
177         getInlineSite(SiteLoc, Loc->getScope()->getSubprogram()).SiteFuncId;
178 
179     // Ensure we have links in the tree of inline call sites.
180     bool FirstLoc = true;
181     while ((SiteLoc = Loc->getInlinedAt())) {
182       InlineSite &Site =
183           getInlineSite(SiteLoc, Loc->getScope()->getSubprogram());
184       if (!FirstLoc)
185         addLocIfNotPresent(Site.ChildSites, Loc);
186       FirstLoc = false;
187       Loc = SiteLoc;
188     }
189     addLocIfNotPresent(CurFn->ChildSites, Loc);
190   }
191 
192   OS.EmitCVLocDirective(FuncId, FileId, DL.getLine(), DL.getCol(),
193                         /*PrologueEnd=*/false,
194                         /*IsStmt=*/false, DL->getFilename());
195 }
196 
197 void CodeViewDebug::endModule() {
198   if (FnDebugInfo.empty())
199     return;
200 
201   emitTypeInformation();
202 
203   // 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.
206   assert(Asm != nullptr);
207   OS.SwitchSection(Asm->getObjFileLowering().getCOFFDebugSymbolsSection());
208   OS.AddComment("Debug section magic");
209   OS.EmitIntValue(COFF::DEBUG_SECTION_MAGIC, 4);
210 
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 
216   // Make a subsection for all the inlined subprograms.
217   emitInlineeLinesSubsection();
218 
219   // Emit per-function debug information.
220   for (auto &P : FnDebugInfo)
221     emitDebugInfoForFunction(P.first, P.second);
222 
223   // This subsection holds a file index to offset in string table table.
224   OS.AddComment("File index to string table offset subsection");
225   OS.EmitCVFileChecksumsDirective();
226 
227   // This subsection holds the string table.
228   OS.AddComment("String table");
229   OS.EmitCVStringTableDirective();
230 
231   clear();
232 }
233 
234 void CodeViewDebug::emitTypeInformation() {
235   // Start the .debug$T section with 0x4.
236   OS.SwitchSection(Asm->getObjFileLowering().getCOFFDebugTypesSection());
237   OS.AddComment("Debug section magic");
238   OS.EmitIntValue(COFF::DEBUG_SECTION_MAGIC, 4);
239 
240   NamedMDNode *CU_Nodes =
241       MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
242   if (!CU_Nodes)
243     return;
244 
245   // This type info currently only holds function ids for use with inline call
246   // frame info. All functions are assigned a simple 'void ()' type. Emit that
247   // type here.
248   TypeIndex ArgListIdx = getNextTypeIndex();
249   OS.AddComment("Type record length");
250   OS.EmitIntValue(2 + sizeof(ArgList), 2);
251   OS.AddComment("Leaf type: LF_ARGLIST");
252   OS.EmitIntValue(LF_ARGLIST, 2);
253   OS.AddComment("Number of arguments");
254   OS.EmitIntValue(0, 4);
255 
256   TypeIndex VoidProcIdx = getNextTypeIndex();
257   OS.AddComment("Type record length");
258   OS.EmitIntValue(2 + sizeof(ProcedureType), 2);
259   OS.AddComment("Leaf type: LF_PROCEDURE");
260   OS.EmitIntValue(LF_PROCEDURE, 2);
261   OS.AddComment("Return type index");
262   OS.EmitIntValue(TypeIndex::Void().getIndex(), 4);
263   OS.AddComment("Calling convention");
264   OS.EmitIntValue(char(CallingConvention::NearC), 1);
265   OS.AddComment("Function options");
266   OS.EmitIntValue(char(FunctionOptions::None), 1);
267   OS.AddComment("# of parameters");
268   OS.EmitIntValue(0, 2);
269   OS.AddComment("Argument list type index");
270   OS.EmitIntValue(ArgListIdx.getIndex(), 4);
271 
272   for (MDNode *N : CU_Nodes->operands()) {
273     auto *CUNode = cast<DICompileUnit>(N);
274     for (auto *SP : CUNode->getSubprograms()) {
275       StringRef DisplayName = SP->getDisplayName();
276       OS.AddComment("Type record length");
277       OS.EmitIntValue(2 + sizeof(FuncId) + DisplayName.size() + 1, 2);
278       OS.AddComment("Leaf type: LF_FUNC_ID");
279       OS.EmitIntValue(LF_FUNC_ID, 2);
280 
281       OS.AddComment("Scope type index");
282       OS.EmitIntValue(TypeIndex().getIndex(), 4);
283       OS.AddComment("Function type");
284       OS.EmitIntValue(VoidProcIdx.getIndex(), 4);
285       {
286         SmallString<32> NullTerminatedString(DisplayName);
287         if (NullTerminatedString.empty() || NullTerminatedString.back() != '\0')
288           NullTerminatedString.push_back('\0');
289         OS.AddComment("Function name");
290         OS.EmitBytes(NullTerminatedString);
291       }
292 
293       TypeIndex FuncIdIdx = getNextTypeIndex();
294       SubprogramToFuncId.insert(std::make_pair(SP, FuncIdIdx));
295     }
296   }
297 }
298 
299 void CodeViewDebug::emitInlineeLinesSubsection() {
300   if (InlinedSubprograms.empty())
301     return;
302 
303   MCSymbol *InlineBegin = MMI->getContext().createTempSymbol(),
304            *InlineEnd = MMI->getContext().createTempSymbol();
305 
306   OS.AddComment("Inlinee lines subsection");
307   OS.EmitIntValue(unsigned(ModuleSubstreamKind::InlineeLines), 4);
308   OS.AddComment("Subsection size");
309   OS.emitAbsoluteSymbolDiff(InlineEnd, InlineBegin, 4);
310   OS.EmitLabel(InlineBegin);
311 
312   // We don't provide any extra file info.
313   // FIXME: Find out if debuggers use this info.
314   OS.AddComment("Inlinee lines signature");
315   OS.EmitIntValue(unsigned(InlineeLinesSignature::Normal), 4);
316 
317   for (const DISubprogram *SP : InlinedSubprograms) {
318     OS.AddBlankLine();
319     TypeIndex TypeId = SubprogramToFuncId[SP];
320     unsigned FileId = maybeRecordFile(SP->getFile());
321     OS.AddComment("Inlined function " + SP->getDisplayName() + " starts at " +
322                   SP->getFilename() + Twine(':') + Twine(SP->getLine()));
323     OS.AddBlankLine();
324     // The filechecksum table uses 8 byte entries for now, and file ids start at
325     // 1.
326     unsigned FileOffset = (FileId - 1) * 8;
327     OS.AddComment("Type index of inlined function");
328     OS.EmitIntValue(TypeId.getIndex(), 4);
329     OS.AddComment("Offset into filechecksum table");
330     OS.EmitIntValue(FileOffset, 4);
331     OS.AddComment("Starting line number");
332     OS.EmitIntValue(SP->getLine(), 4);
333   }
334 
335   OS.EmitLabel(InlineEnd);
336 }
337 
338 void CodeViewDebug::collectInlineSiteChildren(
339     SmallVectorImpl<unsigned> &Children, const FunctionInfo &FI,
340     const InlineSite &Site) {
341   for (const DILocation *ChildSiteLoc : Site.ChildSites) {
342     auto I = FI.InlineSites.find(ChildSiteLoc);
343     const InlineSite &ChildSite = I->second;
344     Children.push_back(ChildSite.SiteFuncId);
345     collectInlineSiteChildren(Children, FI, ChildSite);
346   }
347 }
348 
349 void CodeViewDebug::emitInlinedCallSite(const FunctionInfo &FI,
350                                         const DILocation *InlinedAt,
351                                         const InlineSite &Site) {
352   MCSymbol *InlineBegin = MMI->getContext().createTempSymbol(),
353            *InlineEnd = MMI->getContext().createTempSymbol();
354 
355   assert(SubprogramToFuncId.count(Site.Inlinee));
356   TypeIndex InlineeIdx = SubprogramToFuncId[Site.Inlinee];
357 
358   // SymbolRecord
359   OS.AddComment("Record length");
360   OS.emitAbsoluteSymbolDiff(InlineEnd, InlineBegin, 2);   // RecordLength
361   OS.EmitLabel(InlineBegin);
362   OS.AddComment("Record kind: S_INLINESITE");
363   OS.EmitIntValue(SymbolRecordKind::S_INLINESITE, 2); // RecordKind
364 
365   OS.AddComment("PtrParent");
366   OS.EmitIntValue(0, 4);
367   OS.AddComment("PtrEnd");
368   OS.EmitIntValue(0, 4);
369   OS.AddComment("Inlinee type index");
370   OS.EmitIntValue(InlineeIdx.getIndex(), 4);
371 
372   unsigned FileId = maybeRecordFile(Site.Inlinee->getFile());
373   unsigned StartLineNum = Site.Inlinee->getLine();
374   SmallVector<unsigned, 3> SecondaryFuncIds;
375   collectInlineSiteChildren(SecondaryFuncIds, FI, Site);
376 
377   OS.EmitCVInlineLinetableDirective(Site.SiteFuncId, FileId, StartLineNum,
378                                     FI.Begin, FI.End, SecondaryFuncIds);
379 
380   OS.EmitLabel(InlineEnd);
381 
382   for (const LocalVariable &Var : Site.InlinedLocals)
383     emitLocalVariable(Var);
384 
385   // Recurse on child inlined call sites before closing the scope.
386   for (const DILocation *ChildSite : Site.ChildSites) {
387     auto I = FI.InlineSites.find(ChildSite);
388     assert(I != FI.InlineSites.end() &&
389            "child site not in function inline site map");
390     emitInlinedCallSite(FI, ChildSite, I->second);
391   }
392 
393   // Close the scope.
394   OS.AddComment("Record length");
395   OS.EmitIntValue(2, 2);                                  // RecordLength
396   OS.AddComment("Record kind: S_INLINESITE_END");
397   OS.EmitIntValue(SymbolRecordKind::S_INLINESITE_END, 2); // RecordKind
398 }
399 
400 static void emitNullTerminatedString(MCStreamer &OS, StringRef S) {
401   SmallString<32> NullTerminatedString(S);
402   if (NullTerminatedString.empty() || NullTerminatedString.back() != '\0')
403     NullTerminatedString.push_back('\0');
404   OS.EmitBytes(NullTerminatedString);
405 }
406 
407 void CodeViewDebug::emitDebugInfoForFunction(const Function *GV,
408                                              FunctionInfo &FI) {
409   // For each function there is a separate subsection
410   // which holds the PC to file:line table.
411   const MCSymbol *Fn = Asm->getSymbol(GV);
412   assert(Fn);
413 
414   StringRef FuncName;
415   if (auto *SP = getDISubprogram(GV))
416     FuncName = SP->getDisplayName();
417 
418   // If our DISubprogram name is empty, use the mangled name.
419   if (FuncName.empty())
420     FuncName = GlobalValue::getRealLinkageName(GV->getName());
421 
422   // Emit a symbol subsection, required by VS2012+ to find function boundaries.
423   MCSymbol *SymbolsBegin = MMI->getContext().createTempSymbol(),
424            *SymbolsEnd = MMI->getContext().createTempSymbol();
425   OS.AddComment("Symbol subsection for " + Twine(FuncName));
426   OS.EmitIntValue(unsigned(ModuleSubstreamKind::Symbols), 4);
427   OS.AddComment("Subsection size");
428   OS.emitAbsoluteSymbolDiff(SymbolsEnd, SymbolsBegin, 4);
429   OS.EmitLabel(SymbolsBegin);
430   {
431     MCSymbol *ProcRecordBegin = MMI->getContext().createTempSymbol(),
432              *ProcRecordEnd = MMI->getContext().createTempSymbol();
433     OS.AddComment("Record length");
434     OS.emitAbsoluteSymbolDiff(ProcRecordEnd, ProcRecordBegin, 2);
435     OS.EmitLabel(ProcRecordBegin);
436 
437     OS.AddComment("Record kind: S_GPROC32_ID");
438     OS.EmitIntValue(unsigned(SymbolRecordKind::S_GPROC32_ID), 2);
439 
440     // These fields are filled in by tools like CVPACK which run after the fact.
441     OS.AddComment("PtrParent");
442     OS.EmitIntValue(0, 4);
443     OS.AddComment("PtrEnd");
444     OS.EmitIntValue(0, 4);
445     OS.AddComment("PtrNext");
446     OS.EmitIntValue(0, 4);
447     // This is the important bit that tells the debugger where the function
448     // code is located and what's its size:
449     OS.AddComment("Code size");
450     OS.emitAbsoluteSymbolDiff(FI.End, Fn, 4);
451     OS.AddComment("Offset after prologue");
452     OS.EmitIntValue(0, 4);
453     OS.AddComment("Offset before epilogue");
454     OS.EmitIntValue(0, 4);
455     OS.AddComment("Function type index");
456     OS.EmitIntValue(0, 4);
457     OS.AddComment("Function section relative address");
458     OS.EmitCOFFSecRel32(Fn);
459     OS.AddComment("Function section index");
460     OS.EmitCOFFSectionIndex(Fn);
461     OS.AddComment("Flags");
462     OS.EmitIntValue(0, 1);
463     // Emit the function display name as a null-terminated string.
464     OS.AddComment("Function name");
465     emitNullTerminatedString(OS, FuncName);
466     OS.EmitLabel(ProcRecordEnd);
467 
468     for (const LocalVariable &Var : FI.Locals)
469       emitLocalVariable(Var);
470 
471     // Emit inlined call site information. Only emit functions inlined directly
472     // into the parent function. We'll emit the other sites recursively as part
473     // of their parent inline site.
474     for (const DILocation *InlinedAt : FI.ChildSites) {
475       auto I = FI.InlineSites.find(InlinedAt);
476       assert(I != FI.InlineSites.end() &&
477              "child site not in function inline site map");
478       emitInlinedCallSite(FI, InlinedAt, I->second);
479     }
480 
481     // We're done with this function.
482     OS.AddComment("Record length");
483     OS.EmitIntValue(0x0002, 2);
484     OS.AddComment("Record kind: S_PROC_ID_END");
485     OS.EmitIntValue(unsigned(SymbolRecordKind::S_PROC_ID_END), 2);
486   }
487   OS.EmitLabel(SymbolsEnd);
488   // Every subsection must be aligned to a 4-byte boundary.
489   OS.EmitValueToAlignment(4);
490 
491   // We have an assembler directive that takes care of the whole line table.
492   OS.EmitCVLinetableDirective(FI.FuncId, Fn, FI.End);
493 }
494 
495 CodeViewDebug::LocalVarDefRange
496 CodeViewDebug::createDefRangeMem(uint16_t CVRegister, int Offset) {
497   LocalVarDefRange DR;
498   DR.InMemory = -1;
499   DR.DataOffset = Offset;
500   assert(DR.DataOffset == Offset && "truncation");
501   DR.StructOffset = 0;
502   DR.CVRegister = CVRegister;
503   return DR;
504 }
505 
506 CodeViewDebug::LocalVarDefRange
507 CodeViewDebug::createDefRangeReg(uint16_t CVRegister) {
508   LocalVarDefRange DR;
509   DR.InMemory = 0;
510   DR.DataOffset = 0;
511   DR.StructOffset = 0;
512   DR.CVRegister = CVRegister;
513   return DR;
514 }
515 
516 void CodeViewDebug::collectVariableInfoFromMMITable(
517     DenseSet<InlinedVariable> &Processed) {
518   const TargetSubtargetInfo &TSI = Asm->MF->getSubtarget();
519   const TargetFrameLowering *TFI = TSI.getFrameLowering();
520   const TargetRegisterInfo *TRI = TSI.getRegisterInfo();
521 
522   for (const MachineModuleInfo::VariableDbgInfo &VI :
523        MMI->getVariableDbgInfo()) {
524     if (!VI.Var)
525       continue;
526     assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) &&
527            "Expected inlined-at fields to agree");
528 
529     Processed.insert(InlinedVariable(VI.Var, VI.Loc->getInlinedAt()));
530     LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc);
531 
532     // If variable scope is not found then skip this variable.
533     if (!Scope)
534       continue;
535 
536     // Get the frame register used and the offset.
537     unsigned FrameReg = 0;
538     int FrameOffset = TFI->getFrameIndexReference(*Asm->MF, VI.Slot, FrameReg);
539     uint16_t CVReg = TRI->getCodeViewRegNum(FrameReg);
540 
541     // Calculate the label ranges.
542     LocalVarDefRange DefRange = createDefRangeMem(CVReg, FrameOffset);
543     for (const InsnRange &Range : Scope->getRanges()) {
544       const MCSymbol *Begin = getLabelBeforeInsn(Range.first);
545       const MCSymbol *End = getLabelAfterInsn(Range.second);
546       End = End ? End : Asm->getFunctionEnd();
547       DefRange.Ranges.emplace_back(Begin, End);
548     }
549 
550     LocalVariable Var;
551     Var.DIVar = VI.Var;
552     Var.DefRanges.emplace_back(std::move(DefRange));
553     recordLocalVariable(std::move(Var), VI.Loc->getInlinedAt());
554   }
555 }
556 
557 void CodeViewDebug::collectVariableInfo(const DISubprogram *SP) {
558   DenseSet<InlinedVariable> Processed;
559   // Grab the variable info that was squirreled away in the MMI side-table.
560   collectVariableInfoFromMMITable(Processed);
561 
562   const TargetRegisterInfo *TRI = Asm->MF->getSubtarget().getRegisterInfo();
563 
564   for (const auto &I : DbgValues) {
565     InlinedVariable IV = I.first;
566     if (Processed.count(IV))
567       continue;
568     const DILocalVariable *DIVar = IV.first;
569     const DILocation *InlinedAt = IV.second;
570 
571     // Instruction ranges, specifying where IV is accessible.
572     const auto &Ranges = I.second;
573 
574     LexicalScope *Scope = nullptr;
575     if (InlinedAt)
576       Scope = LScopes.findInlinedScope(DIVar->getScope(), InlinedAt);
577     else
578       Scope = LScopes.findLexicalScope(DIVar->getScope());
579     // If variable scope is not found then skip this variable.
580     if (!Scope)
581       continue;
582 
583     LocalVariable Var;
584     Var.DIVar = DIVar;
585 
586     // Calculate the definition ranges.
587     for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) {
588       const InsnRange &Range = *I;
589       const MachineInstr *DVInst = Range.first;
590       assert(DVInst->isDebugValue() && "Invalid History entry");
591       const DIExpression *DIExpr = DVInst->getDebugExpression();
592 
593       // Bail if there is a complex DWARF expression for now.
594       if (DIExpr && DIExpr->getNumElements() > 0)
595         continue;
596 
597       // Bail if operand 0 is not a valid register. This means the variable is a
598       // simple constant, or is described by a complex expression.
599       // FIXME: Find a way to represent constant variables, since they are
600       // relatively common.
601       unsigned Reg =
602           DVInst->getOperand(0).isReg() ? DVInst->getOperand(0).getReg() : 0;
603       if (Reg == 0)
604         continue;
605 
606       // Handle the two cases we can handle: indirect in memory and in register.
607       bool IsIndirect = DVInst->getOperand(1).isImm();
608       unsigned CVReg = TRI->getCodeViewRegNum(DVInst->getOperand(0).getReg());
609       {
610         LocalVarDefRange DefRange;
611         if (IsIndirect) {
612           int64_t Offset = DVInst->getOperand(1).getImm();
613           DefRange = createDefRangeMem(CVReg, Offset);
614         } else {
615           DefRange = createDefRangeReg(CVReg);
616         }
617         if (Var.DefRanges.empty() ||
618             Var.DefRanges.back().isDifferentLocation(DefRange)) {
619           Var.DefRanges.emplace_back(std::move(DefRange));
620         }
621       }
622 
623       // Compute the label range.
624       const MCSymbol *Begin = getLabelBeforeInsn(Range.first);
625       const MCSymbol *End = getLabelAfterInsn(Range.second);
626       if (!End) {
627         if (std::next(I) != E)
628           End = getLabelBeforeInsn(std::next(I)->first);
629         else
630           End = Asm->getFunctionEnd();
631       }
632 
633       // If the last range end is our begin, just extend the last range.
634       // Otherwise make a new range.
635       SmallVectorImpl<std::pair<const MCSymbol *, const MCSymbol *>> &Ranges =
636           Var.DefRanges.back().Ranges;
637       if (!Ranges.empty() && Ranges.back().second == Begin)
638         Ranges.back().second = End;
639       else
640         Ranges.emplace_back(Begin, End);
641 
642       // FIXME: Do more range combining.
643     }
644 
645     recordLocalVariable(std::move(Var), InlinedAt);
646   }
647 }
648 
649 void CodeViewDebug::beginFunction(const MachineFunction *MF) {
650   assert(!CurFn && "Can't process two functions at once!");
651 
652   if (!Asm || !MMI->hasDebugInfo())
653     return;
654 
655   DebugHandlerBase::beginFunction(MF);
656 
657   const Function *GV = MF->getFunction();
658   assert(FnDebugInfo.count(GV) == false);
659   CurFn = &FnDebugInfo[GV];
660   CurFn->FuncId = NextFuncId++;
661   CurFn->Begin = Asm->getFunctionBegin();
662 
663   // Find the end of the function prolog.  First known non-DBG_VALUE and
664   // non-frame setup location marks the beginning of the function body.
665   // FIXME: is there a simpler a way to do this? Can we just search
666   // for the first instruction of the function, not the last of the prolog?
667   DebugLoc PrologEndLoc;
668   bool EmptyPrologue = true;
669   for (const auto &MBB : *MF) {
670     for (const auto &MI : MBB) {
671       if (!MI.isDebugValue() && !MI.getFlag(MachineInstr::FrameSetup) &&
672           MI.getDebugLoc()) {
673         PrologEndLoc = MI.getDebugLoc();
674         break;
675       } else if (!MI.isDebugValue()) {
676         EmptyPrologue = false;
677       }
678     }
679   }
680 
681   // Record beginning of function if we have a non-empty prologue.
682   if (PrologEndLoc && !EmptyPrologue) {
683     DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc();
684     maybeRecordLocation(FnStartDL, MF);
685   }
686 }
687 
688 void CodeViewDebug::emitLocalVariable(const LocalVariable &Var) {
689   // LocalSym record, see SymbolRecord.h for more info.
690   MCSymbol *LocalBegin = MMI->getContext().createTempSymbol(),
691            *LocalEnd = MMI->getContext().createTempSymbol();
692   OS.AddComment("Record length");
693   OS.emitAbsoluteSymbolDiff(LocalEnd, LocalBegin, 2);
694   OS.EmitLabel(LocalBegin);
695 
696   OS.AddComment("Record kind: S_LOCAL");
697   OS.EmitIntValue(unsigned(SymbolRecordKind::S_LOCAL), 2);
698 
699   uint16_t Flags = 0;
700   if (Var.DIVar->isParameter())
701     Flags |= LocalSym::IsParameter;
702   if (Var.DefRanges.empty())
703     Flags |= LocalSym::IsOptimizedOut;
704 
705   OS.AddComment("TypeIndex");
706   OS.EmitIntValue(TypeIndex::Int32().getIndex(), 4);
707   OS.AddComment("Flags");
708   OS.EmitIntValue(Flags, 2);
709   emitNullTerminatedString(OS, Var.DIVar->getName());
710   OS.EmitLabel(LocalEnd);
711 
712   // Calculate the on disk prefix of the appropriate def range record. The
713   // records and on disk formats are described in SymbolRecords.h. BytePrefix
714   // should be big enough to hold all forms without memory allocation.
715   SmallString<20> BytePrefix;
716   for (const LocalVarDefRange &DefRange : Var.DefRanges) {
717     BytePrefix.clear();
718     // FIXME: Handle bitpieces.
719     if (DefRange.StructOffset != 0)
720       continue;
721 
722     if (DefRange.InMemory) {
723       DefRangeRegisterRelSym Sym{};
724       ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER_REL);
725       Sym.BaseRegister = DefRange.CVRegister;
726       Sym.Flags = 0; // Unclear what matters here.
727       Sym.BasePointerOffset = DefRange.DataOffset;
728       BytePrefix +=
729           StringRef(reinterpret_cast<const char *>(&SymKind), sizeof(SymKind));
730       BytePrefix += StringRef(reinterpret_cast<const char *>(&Sym),
731                               sizeof(Sym) - sizeof(LocalVariableAddrRange));
732     } else {
733       assert(DefRange.DataOffset == 0 && "unexpected offset into register");
734       DefRangeRegisterSym Sym{};
735       ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER);
736       Sym.Register = DefRange.CVRegister;
737       Sym.MayHaveNoName = 0; // Unclear what matters here.
738       BytePrefix +=
739           StringRef(reinterpret_cast<const char *>(&SymKind), sizeof(SymKind));
740       BytePrefix += StringRef(reinterpret_cast<const char *>(&Sym),
741                               sizeof(Sym) - sizeof(LocalVariableAddrRange));
742     }
743     OS.EmitCVDefRangeDirective(DefRange.Ranges, BytePrefix);
744   }
745 }
746 
747 void CodeViewDebug::endFunction(const MachineFunction *MF) {
748   if (!Asm || !CurFn)  // We haven't created any debug info for this function.
749     return;
750 
751   const Function *GV = MF->getFunction();
752   assert(FnDebugInfo.count(GV));
753   assert(CurFn == &FnDebugInfo[GV]);
754 
755   collectVariableInfo(getDISubprogram(GV));
756 
757   DebugHandlerBase::endFunction(MF);
758 
759   // Don't emit anything if we don't have any line tables.
760   if (!CurFn->HaveLineInfo) {
761     FnDebugInfo.erase(GV);
762     CurFn = nullptr;
763     return;
764   }
765 
766   CurFn->End = Asm->getFunctionEnd();
767 
768   CurFn = nullptr;
769 }
770 
771 void CodeViewDebug::beginInstruction(const MachineInstr *MI) {
772   DebugHandlerBase::beginInstruction(MI);
773 
774   // Ignore DBG_VALUE locations and function prologue.
775   if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup))
776     return;
777   DebugLoc DL = MI->getDebugLoc();
778   if (DL == PrevInstLoc || !DL)
779     return;
780   maybeRecordLocation(DL, Asm->MF);
781 }
782