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