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/FieldListRecordBuilder.h"
17 #include "llvm/DebugInfo/CodeView/Line.h"
18 #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
19 #include "llvm/DebugInfo/CodeView/TypeDumper.h"
20 #include "llvm/DebugInfo/CodeView/TypeIndex.h"
21 #include "llvm/DebugInfo/CodeView/TypeRecord.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCSectionCOFF.h"
24 #include "llvm/MC/MCSymbol.h"
25 #include "llvm/Support/COFF.h"
26 #include "llvm/Support/ScopedPrinter.h"
27 #include "llvm/Target/TargetSubtargetInfo.h"
28 #include "llvm/Target/TargetRegisterInfo.h"
29 #include "llvm/Target/TargetFrameLowering.h"
30 
31 using namespace llvm;
32 using namespace llvm::codeview;
33 
34 CodeViewDebug::CodeViewDebug(AsmPrinter *AP)
35     : DebugHandlerBase(AP), OS(*Asm->OutStreamer), CurFn(nullptr) {
36   // If module doesn't have named metadata anchors or COFF debug section
37   // is not available, skip any debug info related stuff.
38   if (!MMI->getModule()->getNamedMetadata("llvm.dbg.cu") ||
39       !AP->getObjFileLowering().getCOFFDebugSymbolsSection()) {
40     Asm = nullptr;
41     return;
42   }
43 
44   // Tell MMI that we have debug info.
45   MMI->setDebugInfoAvailability(true);
46 }
47 
48 StringRef CodeViewDebug::getFullFilepath(const DIFile *File) {
49   std::string &Filepath = FileToFilepathMap[File];
50   if (!Filepath.empty())
51     return Filepath;
52 
53   StringRef Dir = File->getDirectory(), Filename = File->getFilename();
54 
55   // Clang emits directory and relative filename info into the IR, but CodeView
56   // operates on full paths.  We could change Clang to emit full paths too, but
57   // that would increase the IR size and probably not needed for other users.
58   // For now, just concatenate and canonicalize the path here.
59   if (Filename.find(':') == 1)
60     Filepath = Filename;
61   else
62     Filepath = (Dir + "\\" + Filename).str();
63 
64   // Canonicalize the path.  We have to do it textually because we may no longer
65   // have access the file in the filesystem.
66   // First, replace all slashes with backslashes.
67   std::replace(Filepath.begin(), Filepath.end(), '/', '\\');
68 
69   // Remove all "\.\" with "\".
70   size_t Cursor = 0;
71   while ((Cursor = Filepath.find("\\.\\", Cursor)) != std::string::npos)
72     Filepath.erase(Cursor, 2);
73 
74   // Replace all "\XXX\..\" with "\".  Don't try too hard though as the original
75   // path should be well-formatted, e.g. start with a drive letter, etc.
76   Cursor = 0;
77   while ((Cursor = Filepath.find("\\..\\", Cursor)) != std::string::npos) {
78     // Something's wrong if the path starts with "\..\", abort.
79     if (Cursor == 0)
80       break;
81 
82     size_t PrevSlash = Filepath.rfind('\\', Cursor - 1);
83     if (PrevSlash == std::string::npos)
84       // Something's wrong, abort.
85       break;
86 
87     Filepath.erase(PrevSlash, Cursor + 3 - PrevSlash);
88     // The next ".." might be following the one we've just erased.
89     Cursor = PrevSlash;
90   }
91 
92   // Remove all duplicate backslashes.
93   Cursor = 0;
94   while ((Cursor = Filepath.find("\\\\", Cursor)) != std::string::npos)
95     Filepath.erase(Cursor, 1);
96 
97   return Filepath;
98 }
99 
100 unsigned CodeViewDebug::maybeRecordFile(const DIFile *F) {
101   unsigned NextId = FileIdMap.size() + 1;
102   auto Insertion = FileIdMap.insert(std::make_pair(F, NextId));
103   if (Insertion.second) {
104     // We have to compute the full filepath and emit a .cv_file directive.
105     StringRef FullPath = getFullFilepath(F);
106     NextId = OS.EmitCVFileDirective(NextId, FullPath);
107     assert(NextId == FileIdMap.size() && ".cv_file directive failed");
108   }
109   return Insertion.first->second;
110 }
111 
112 CodeViewDebug::InlineSite &
113 CodeViewDebug::getInlineSite(const DILocation *InlinedAt,
114                              const DISubprogram *Inlinee) {
115   auto SiteInsertion = CurFn->InlineSites.insert({InlinedAt, InlineSite()});
116   InlineSite *Site = &SiteInsertion.first->second;
117   if (SiteInsertion.second) {
118     Site->SiteFuncId = NextFuncId++;
119     Site->Inlinee = Inlinee;
120     InlinedSubprograms.insert(Inlinee);
121     getFuncIdForSubprogram(Inlinee);
122   }
123   return *Site;
124 }
125 
126 TypeIndex CodeViewDebug::getFuncIdForSubprogram(const DISubprogram *SP) {
127   // It's possible to ask for the FuncId of a function which doesn't have a
128   // subprogram: inlining a function with debug info into a function with none.
129   if (!SP)
130     return TypeIndex::None();
131 
132   // Check if we've already translated this subprogram.
133   auto I = TypeIndices.find(SP);
134   if (I != TypeIndices.end())
135     return I->second;
136 
137   TypeIndex ParentScope = TypeIndex(0);
138   // The display name includes function template arguments. Drop them to match
139   // MSVC.
140   StringRef DisplayName = SP->getDisplayName().split('<').first;
141   FuncIdRecord FuncId(ParentScope, getTypeIndex(SP->getType()), DisplayName);
142   TypeIndex TI = TypeTable.writeFuncId(FuncId);
143 
144   recordTypeIndexForDINode(SP, TI);
145   return TI;
146 }
147 
148 void CodeViewDebug::recordTypeIndexForDINode(const DINode *Node, TypeIndex TI) {
149   auto InsertResult = TypeIndices.insert({Node, TI});
150   (void)InsertResult;
151   assert(InsertResult.second && "DINode was already assigned a type index");
152 }
153 
154 void CodeViewDebug::recordLocalVariable(LocalVariable &&Var,
155                                         const DILocation *InlinedAt) {
156   if (InlinedAt) {
157     // This variable was inlined. Associate it with the InlineSite.
158     const DISubprogram *Inlinee = Var.DIVar->getScope()->getSubprogram();
159     InlineSite &Site = getInlineSite(InlinedAt, Inlinee);
160     Site.InlinedLocals.emplace_back(Var);
161   } else {
162     // This variable goes in the main ProcSym.
163     CurFn->Locals.emplace_back(Var);
164   }
165 }
166 
167 static void addLocIfNotPresent(SmallVectorImpl<const DILocation *> &Locs,
168                                const DILocation *Loc) {
169   auto B = Locs.begin(), E = Locs.end();
170   if (std::find(B, E, Loc) == E)
171     Locs.push_back(Loc);
172 }
173 
174 void CodeViewDebug::maybeRecordLocation(const DebugLoc &DL,
175                                         const MachineFunction *MF) {
176   // Skip this instruction if it has the same location as the previous one.
177   if (DL == CurFn->LastLoc)
178     return;
179 
180   const DIScope *Scope = DL.get()->getScope();
181   if (!Scope)
182     return;
183 
184   // Skip this line if it is longer than the maximum we can record.
185   LineInfo LI(DL.getLine(), DL.getLine(), /*IsStatement=*/true);
186   if (LI.getStartLine() != DL.getLine() || LI.isAlwaysStepInto() ||
187       LI.isNeverStepInto())
188     return;
189 
190   ColumnInfo CI(DL.getCol(), /*EndColumn=*/0);
191   if (CI.getStartColumn() != DL.getCol())
192     return;
193 
194   if (!CurFn->HaveLineInfo)
195     CurFn->HaveLineInfo = true;
196   unsigned FileId = 0;
197   if (CurFn->LastLoc.get() && CurFn->LastLoc->getFile() == DL->getFile())
198     FileId = CurFn->LastFileId;
199   else
200     FileId = CurFn->LastFileId = maybeRecordFile(DL->getFile());
201   CurFn->LastLoc = DL;
202 
203   unsigned FuncId = CurFn->FuncId;
204   if (const DILocation *SiteLoc = DL->getInlinedAt()) {
205     const DILocation *Loc = DL.get();
206 
207     // If this location was actually inlined from somewhere else, give it the ID
208     // of the inline call site.
209     FuncId =
210         getInlineSite(SiteLoc, Loc->getScope()->getSubprogram()).SiteFuncId;
211 
212     // Ensure we have links in the tree of inline call sites.
213     bool FirstLoc = true;
214     while ((SiteLoc = Loc->getInlinedAt())) {
215       InlineSite &Site =
216           getInlineSite(SiteLoc, Loc->getScope()->getSubprogram());
217       if (!FirstLoc)
218         addLocIfNotPresent(Site.ChildSites, Loc);
219       FirstLoc = false;
220       Loc = SiteLoc;
221     }
222     addLocIfNotPresent(CurFn->ChildSites, Loc);
223   }
224 
225   OS.EmitCVLocDirective(FuncId, FileId, DL.getLine(), DL.getCol(),
226                         /*PrologueEnd=*/false,
227                         /*IsStmt=*/false, DL->getFilename());
228 }
229 
230 void CodeViewDebug::emitCodeViewMagicVersion() {
231   OS.EmitValueToAlignment(4);
232   OS.AddComment("Debug section magic");
233   OS.EmitIntValue(COFF::DEBUG_SECTION_MAGIC, 4);
234 }
235 
236 void CodeViewDebug::endModule() {
237   if (!Asm || !MMI->hasDebugInfo())
238     return;
239 
240   assert(Asm != nullptr);
241 
242   // The COFF .debug$S section consists of several subsections, each starting
243   // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length
244   // of the payload followed by the payload itself.  The subsections are 4-byte
245   // aligned.
246 
247   // Use the generic .debug$S section, and make a subsection for all the inlined
248   // subprograms.
249   switchToDebugSectionForSymbol(nullptr);
250   emitInlineeLinesSubsection();
251 
252   // Emit per-function debug information.
253   for (auto &P : FnDebugInfo)
254     if (!P.first->isDeclarationForLinker())
255       emitDebugInfoForFunction(P.first, P.second);
256 
257   // Emit global variable debug information.
258   setCurrentSubprogram(nullptr);
259   emitDebugInfoForGlobals();
260 
261   // Switch back to the generic .debug$S section after potentially processing
262   // comdat symbol sections.
263   switchToDebugSectionForSymbol(nullptr);
264 
265   // Emit UDT records for any types used by global variables.
266   if (!GlobalUDTs.empty()) {
267     MCSymbol *SymbolsEnd = beginCVSubsection(ModuleSubstreamKind::Symbols);
268     emitDebugInfoForUDTs(GlobalUDTs);
269     endCVSubsection(SymbolsEnd);
270   }
271 
272   // This subsection holds a file index to offset in string table table.
273   OS.AddComment("File index to string table offset subsection");
274   OS.EmitCVFileChecksumsDirective();
275 
276   // This subsection holds the string table.
277   OS.AddComment("String table");
278   OS.EmitCVStringTableDirective();
279 
280   // Emit type information last, so that any types we translate while emitting
281   // function info are included.
282   emitTypeInformation();
283 
284   clear();
285 }
286 
287 static void emitNullTerminatedSymbolName(MCStreamer &OS, StringRef S) {
288   // Microsoft's linker seems to have trouble with symbol names longer than
289   // 0xffd8 bytes.
290   S = S.substr(0, 0xffd8);
291   SmallString<32> NullTerminatedString(S);
292   NullTerminatedString.push_back('\0');
293   OS.EmitBytes(NullTerminatedString);
294 }
295 
296 void CodeViewDebug::emitTypeInformation() {
297   // Do nothing if we have no debug info or if no non-trivial types were emitted
298   // to TypeTable during codegen.
299   NamedMDNode *CU_Nodes =
300       MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
301   if (!CU_Nodes)
302     return;
303   if (TypeTable.empty())
304     return;
305 
306   // Start the .debug$T section with 0x4.
307   OS.SwitchSection(Asm->getObjFileLowering().getCOFFDebugTypesSection());
308   emitCodeViewMagicVersion();
309 
310   SmallString<8> CommentPrefix;
311   if (OS.isVerboseAsm()) {
312     CommentPrefix += '\t';
313     CommentPrefix += Asm->MAI->getCommentString();
314     CommentPrefix += ' ';
315   }
316 
317   CVTypeDumper CVTD(nullptr, /*PrintRecordBytes=*/false);
318   TypeTable.ForEachRecord(
319       [&](TypeIndex Index, StringRef Record) {
320         if (OS.isVerboseAsm()) {
321           // Emit a block comment describing the type record for readability.
322           SmallString<512> CommentBlock;
323           raw_svector_ostream CommentOS(CommentBlock);
324           ScopedPrinter SP(CommentOS);
325           SP.setPrefix(CommentPrefix);
326           CVTD.setPrinter(&SP);
327           Error EC = CVTD.dump({Record.bytes_begin(), Record.bytes_end()});
328           assert(!EC && "produced malformed type record");
329           consumeError(std::move(EC));
330           // emitRawComment will insert its own tab and comment string before
331           // the first line, so strip off our first one. It also prints its own
332           // newline.
333           OS.emitRawComment(
334               CommentOS.str().drop_front(CommentPrefix.size() - 1).rtrim());
335         }
336         OS.EmitBinaryData(Record);
337       });
338 }
339 
340 void CodeViewDebug::emitInlineeLinesSubsection() {
341   if (InlinedSubprograms.empty())
342     return;
343 
344 
345   OS.AddComment("Inlinee lines subsection");
346   MCSymbol *InlineEnd = beginCVSubsection(ModuleSubstreamKind::InlineeLines);
347 
348   // We don't provide any extra file info.
349   // FIXME: Find out if debuggers use this info.
350   OS.AddComment("Inlinee lines signature");
351   OS.EmitIntValue(unsigned(InlineeLinesSignature::Normal), 4);
352 
353   for (const DISubprogram *SP : InlinedSubprograms) {
354     assert(TypeIndices.count(SP));
355     TypeIndex InlineeIdx = TypeIndices[SP];
356 
357     OS.AddBlankLine();
358     unsigned FileId = maybeRecordFile(SP->getFile());
359     OS.AddComment("Inlined function " + SP->getDisplayName() + " starts at " +
360                   SP->getFilename() + Twine(':') + Twine(SP->getLine()));
361     OS.AddBlankLine();
362     // The filechecksum table uses 8 byte entries for now, and file ids start at
363     // 1.
364     unsigned FileOffset = (FileId - 1) * 8;
365     OS.AddComment("Type index of inlined function");
366     OS.EmitIntValue(InlineeIdx.getIndex(), 4);
367     OS.AddComment("Offset into filechecksum table");
368     OS.EmitIntValue(FileOffset, 4);
369     OS.AddComment("Starting line number");
370     OS.EmitIntValue(SP->getLine(), 4);
371   }
372 
373   endCVSubsection(InlineEnd);
374 }
375 
376 void CodeViewDebug::collectInlineSiteChildren(
377     SmallVectorImpl<unsigned> &Children, const FunctionInfo &FI,
378     const InlineSite &Site) {
379   for (const DILocation *ChildSiteLoc : Site.ChildSites) {
380     auto I = FI.InlineSites.find(ChildSiteLoc);
381     const InlineSite &ChildSite = I->second;
382     Children.push_back(ChildSite.SiteFuncId);
383     collectInlineSiteChildren(Children, FI, ChildSite);
384   }
385 }
386 
387 void CodeViewDebug::emitInlinedCallSite(const FunctionInfo &FI,
388                                         const DILocation *InlinedAt,
389                                         const InlineSite &Site) {
390   MCSymbol *InlineBegin = MMI->getContext().createTempSymbol(),
391            *InlineEnd = MMI->getContext().createTempSymbol();
392 
393   assert(TypeIndices.count(Site.Inlinee));
394   TypeIndex InlineeIdx = TypeIndices[Site.Inlinee];
395 
396   // SymbolRecord
397   OS.AddComment("Record length");
398   OS.emitAbsoluteSymbolDiff(InlineEnd, InlineBegin, 2);   // RecordLength
399   OS.EmitLabel(InlineBegin);
400   OS.AddComment("Record kind: S_INLINESITE");
401   OS.EmitIntValue(SymbolKind::S_INLINESITE, 2); // RecordKind
402 
403   OS.AddComment("PtrParent");
404   OS.EmitIntValue(0, 4);
405   OS.AddComment("PtrEnd");
406   OS.EmitIntValue(0, 4);
407   OS.AddComment("Inlinee type index");
408   OS.EmitIntValue(InlineeIdx.getIndex(), 4);
409 
410   unsigned FileId = maybeRecordFile(Site.Inlinee->getFile());
411   unsigned StartLineNum = Site.Inlinee->getLine();
412   SmallVector<unsigned, 3> SecondaryFuncIds;
413   collectInlineSiteChildren(SecondaryFuncIds, FI, Site);
414 
415   OS.EmitCVInlineLinetableDirective(Site.SiteFuncId, FileId, StartLineNum,
416                                     FI.Begin, FI.End, SecondaryFuncIds);
417 
418   OS.EmitLabel(InlineEnd);
419 
420   for (const LocalVariable &Var : Site.InlinedLocals)
421     emitLocalVariable(Var);
422 
423   // Recurse on child inlined call sites before closing the scope.
424   for (const DILocation *ChildSite : Site.ChildSites) {
425     auto I = FI.InlineSites.find(ChildSite);
426     assert(I != FI.InlineSites.end() &&
427            "child site not in function inline site map");
428     emitInlinedCallSite(FI, ChildSite, I->second);
429   }
430 
431   // Close the scope.
432   OS.AddComment("Record length");
433   OS.EmitIntValue(2, 2);                                  // RecordLength
434   OS.AddComment("Record kind: S_INLINESITE_END");
435   OS.EmitIntValue(SymbolKind::S_INLINESITE_END, 2); // RecordKind
436 }
437 
438 void CodeViewDebug::switchToDebugSectionForSymbol(const MCSymbol *GVSym) {
439   // If we have a symbol, it may be in a section that is COMDAT. If so, find the
440   // comdat key. A section may be comdat because of -ffunction-sections or
441   // because it is comdat in the IR.
442   MCSectionCOFF *GVSec =
443       GVSym ? dyn_cast<MCSectionCOFF>(&GVSym->getSection()) : nullptr;
444   const MCSymbol *KeySym = GVSec ? GVSec->getCOMDATSymbol() : nullptr;
445 
446   MCSectionCOFF *DebugSec = cast<MCSectionCOFF>(
447       Asm->getObjFileLowering().getCOFFDebugSymbolsSection());
448   DebugSec = OS.getContext().getAssociativeCOFFSection(DebugSec, KeySym);
449 
450   OS.SwitchSection(DebugSec);
451 
452   // Emit the magic version number if this is the first time we've switched to
453   // this section.
454   if (ComdatDebugSections.insert(DebugSec).second)
455     emitCodeViewMagicVersion();
456 }
457 
458 static const DISubprogram *getQualifiedNameComponents(
459     const DIScope *Scope, SmallVectorImpl<StringRef> &QualifiedNameComponents) {
460   const DISubprogram *ClosestSubprogram = nullptr;
461   while (Scope != nullptr) {
462     if (ClosestSubprogram == nullptr)
463       ClosestSubprogram = dyn_cast<DISubprogram>(Scope);
464     StringRef ScopeName = Scope->getName();
465     if (!ScopeName.empty())
466       QualifiedNameComponents.push_back(ScopeName);
467     Scope = Scope->getScope().resolve();
468   }
469   return ClosestSubprogram;
470 }
471 
472 static std::string getQualifiedName(ArrayRef<StringRef> QualifiedNameComponents,
473                                     StringRef TypeName) {
474   std::string FullyQualifiedName;
475   for (StringRef QualifiedNameComponent : reverse(QualifiedNameComponents)) {
476     FullyQualifiedName.append(QualifiedNameComponent);
477     FullyQualifiedName.append("::");
478   }
479   FullyQualifiedName.append(TypeName);
480   return FullyQualifiedName;
481 }
482 
483 void CodeViewDebug::emitDebugInfoForFunction(const Function *GV,
484                                              FunctionInfo &FI) {
485   // For each function there is a separate subsection
486   // which holds the PC to file:line table.
487   const MCSymbol *Fn = Asm->getSymbol(GV);
488   assert(Fn);
489 
490   // Switch to the to a comdat section, if appropriate.
491   switchToDebugSectionForSymbol(Fn);
492 
493   std::string FuncName;
494   auto *SP = GV->getSubprogram();
495   setCurrentSubprogram(SP);
496 
497   // If we have a display name, build the fully qualified name by walking the
498   // chain of scopes.
499   if (SP != nullptr && !SP->getDisplayName().empty()) {
500     SmallVector<StringRef, 5> QualifiedNameComponents;
501     getQualifiedNameComponents(SP->getScope().resolve(),
502                                QualifiedNameComponents);
503     FuncName = getQualifiedName(QualifiedNameComponents, SP->getDisplayName());
504   }
505 
506   // If our DISubprogram name is empty, use the mangled name.
507   if (FuncName.empty())
508     FuncName = GlobalValue::getRealLinkageName(GV->getName());
509 
510   // Emit a symbol subsection, required by VS2012+ to find function boundaries.
511   OS.AddComment("Symbol subsection for " + Twine(FuncName));
512   MCSymbol *SymbolsEnd = beginCVSubsection(ModuleSubstreamKind::Symbols);
513   {
514     MCSymbol *ProcRecordBegin = MMI->getContext().createTempSymbol(),
515              *ProcRecordEnd = MMI->getContext().createTempSymbol();
516     OS.AddComment("Record length");
517     OS.emitAbsoluteSymbolDiff(ProcRecordEnd, ProcRecordBegin, 2);
518     OS.EmitLabel(ProcRecordBegin);
519 
520     OS.AddComment("Record kind: S_GPROC32_ID");
521     OS.EmitIntValue(unsigned(SymbolKind::S_GPROC32_ID), 2);
522 
523     // These fields are filled in by tools like CVPACK which run after the fact.
524     OS.AddComment("PtrParent");
525     OS.EmitIntValue(0, 4);
526     OS.AddComment("PtrEnd");
527     OS.EmitIntValue(0, 4);
528     OS.AddComment("PtrNext");
529     OS.EmitIntValue(0, 4);
530     // This is the important bit that tells the debugger where the function
531     // code is located and what's its size:
532     OS.AddComment("Code size");
533     OS.emitAbsoluteSymbolDiff(FI.End, Fn, 4);
534     OS.AddComment("Offset after prologue");
535     OS.EmitIntValue(0, 4);
536     OS.AddComment("Offset before epilogue");
537     OS.EmitIntValue(0, 4);
538     OS.AddComment("Function type index");
539     OS.EmitIntValue(getFuncIdForSubprogram(GV->getSubprogram()).getIndex(), 4);
540     OS.AddComment("Function section relative address");
541     OS.EmitCOFFSecRel32(Fn);
542     OS.AddComment("Function section index");
543     OS.EmitCOFFSectionIndex(Fn);
544     OS.AddComment("Flags");
545     OS.EmitIntValue(0, 1);
546     // Emit the function display name as a null-terminated string.
547     OS.AddComment("Function name");
548     // Truncate the name so we won't overflow the record length field.
549     emitNullTerminatedSymbolName(OS, FuncName);
550     OS.EmitLabel(ProcRecordEnd);
551 
552     for (const LocalVariable &Var : FI.Locals)
553       emitLocalVariable(Var);
554 
555     // Emit inlined call site information. Only emit functions inlined directly
556     // into the parent function. We'll emit the other sites recursively as part
557     // of their parent inline site.
558     for (const DILocation *InlinedAt : FI.ChildSites) {
559       auto I = FI.InlineSites.find(InlinedAt);
560       assert(I != FI.InlineSites.end() &&
561              "child site not in function inline site map");
562       emitInlinedCallSite(FI, InlinedAt, I->second);
563     }
564 
565     if (SP != nullptr)
566       emitDebugInfoForUDTs(LocalUDTs);
567 
568     // We're done with this function.
569     OS.AddComment("Record length");
570     OS.EmitIntValue(0x0002, 2);
571     OS.AddComment("Record kind: S_PROC_ID_END");
572     OS.EmitIntValue(unsigned(SymbolKind::S_PROC_ID_END), 2);
573   }
574   endCVSubsection(SymbolsEnd);
575 
576   // We have an assembler directive that takes care of the whole line table.
577   OS.EmitCVLinetableDirective(FI.FuncId, Fn, FI.End);
578 }
579 
580 CodeViewDebug::LocalVarDefRange
581 CodeViewDebug::createDefRangeMem(uint16_t CVRegister, int Offset) {
582   LocalVarDefRange DR;
583   DR.InMemory = -1;
584   DR.DataOffset = Offset;
585   assert(DR.DataOffset == Offset && "truncation");
586   DR.StructOffset = 0;
587   DR.CVRegister = CVRegister;
588   return DR;
589 }
590 
591 CodeViewDebug::LocalVarDefRange
592 CodeViewDebug::createDefRangeReg(uint16_t CVRegister) {
593   LocalVarDefRange DR;
594   DR.InMemory = 0;
595   DR.DataOffset = 0;
596   DR.StructOffset = 0;
597   DR.CVRegister = CVRegister;
598   return DR;
599 }
600 
601 void CodeViewDebug::collectVariableInfoFromMMITable(
602     DenseSet<InlinedVariable> &Processed) {
603   const TargetSubtargetInfo &TSI = Asm->MF->getSubtarget();
604   const TargetFrameLowering *TFI = TSI.getFrameLowering();
605   const TargetRegisterInfo *TRI = TSI.getRegisterInfo();
606 
607   for (const MachineModuleInfo::VariableDbgInfo &VI :
608        MMI->getVariableDbgInfo()) {
609     if (!VI.Var)
610       continue;
611     assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) &&
612            "Expected inlined-at fields to agree");
613 
614     Processed.insert(InlinedVariable(VI.Var, VI.Loc->getInlinedAt()));
615     LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc);
616 
617     // If variable scope is not found then skip this variable.
618     if (!Scope)
619       continue;
620 
621     // Get the frame register used and the offset.
622     unsigned FrameReg = 0;
623     int FrameOffset = TFI->getFrameIndexReference(*Asm->MF, VI.Slot, FrameReg);
624     uint16_t CVReg = TRI->getCodeViewRegNum(FrameReg);
625 
626     // Calculate the label ranges.
627     LocalVarDefRange DefRange = createDefRangeMem(CVReg, FrameOffset);
628     for (const InsnRange &Range : Scope->getRanges()) {
629       const MCSymbol *Begin = getLabelBeforeInsn(Range.first);
630       const MCSymbol *End = getLabelAfterInsn(Range.second);
631       End = End ? End : Asm->getFunctionEnd();
632       DefRange.Ranges.emplace_back(Begin, End);
633     }
634 
635     LocalVariable Var;
636     Var.DIVar = VI.Var;
637     Var.DefRanges.emplace_back(std::move(DefRange));
638     recordLocalVariable(std::move(Var), VI.Loc->getInlinedAt());
639   }
640 }
641 
642 void CodeViewDebug::collectVariableInfo(const DISubprogram *SP) {
643   DenseSet<InlinedVariable> Processed;
644   // Grab the variable info that was squirreled away in the MMI side-table.
645   collectVariableInfoFromMMITable(Processed);
646 
647   const TargetRegisterInfo *TRI = Asm->MF->getSubtarget().getRegisterInfo();
648 
649   for (const auto &I : DbgValues) {
650     InlinedVariable IV = I.first;
651     if (Processed.count(IV))
652       continue;
653     const DILocalVariable *DIVar = IV.first;
654     const DILocation *InlinedAt = IV.second;
655 
656     // Instruction ranges, specifying where IV is accessible.
657     const auto &Ranges = I.second;
658 
659     LexicalScope *Scope = nullptr;
660     if (InlinedAt)
661       Scope = LScopes.findInlinedScope(DIVar->getScope(), InlinedAt);
662     else
663       Scope = LScopes.findLexicalScope(DIVar->getScope());
664     // If variable scope is not found then skip this variable.
665     if (!Scope)
666       continue;
667 
668     LocalVariable Var;
669     Var.DIVar = DIVar;
670 
671     // Calculate the definition ranges.
672     for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) {
673       const InsnRange &Range = *I;
674       const MachineInstr *DVInst = Range.first;
675       assert(DVInst->isDebugValue() && "Invalid History entry");
676       const DIExpression *DIExpr = DVInst->getDebugExpression();
677 
678       // Bail if there is a complex DWARF expression for now.
679       if (DIExpr && DIExpr->getNumElements() > 0)
680         continue;
681 
682       // Bail if operand 0 is not a valid register. This means the variable is a
683       // simple constant, or is described by a complex expression.
684       // FIXME: Find a way to represent constant variables, since they are
685       // relatively common.
686       unsigned Reg =
687           DVInst->getOperand(0).isReg() ? DVInst->getOperand(0).getReg() : 0;
688       if (Reg == 0)
689         continue;
690 
691       // Handle the two cases we can handle: indirect in memory and in register.
692       bool IsIndirect = DVInst->getOperand(1).isImm();
693       unsigned CVReg = TRI->getCodeViewRegNum(DVInst->getOperand(0).getReg());
694       {
695         LocalVarDefRange DefRange;
696         if (IsIndirect) {
697           int64_t Offset = DVInst->getOperand(1).getImm();
698           DefRange = createDefRangeMem(CVReg, Offset);
699         } else {
700           DefRange = createDefRangeReg(CVReg);
701         }
702         if (Var.DefRanges.empty() ||
703             Var.DefRanges.back().isDifferentLocation(DefRange)) {
704           Var.DefRanges.emplace_back(std::move(DefRange));
705         }
706       }
707 
708       // Compute the label range.
709       const MCSymbol *Begin = getLabelBeforeInsn(Range.first);
710       const MCSymbol *End = getLabelAfterInsn(Range.second);
711       if (!End) {
712         if (std::next(I) != E)
713           End = getLabelBeforeInsn(std::next(I)->first);
714         else
715           End = Asm->getFunctionEnd();
716       }
717 
718       // If the last range end is our begin, just extend the last range.
719       // Otherwise make a new range.
720       SmallVectorImpl<std::pair<const MCSymbol *, const MCSymbol *>> &Ranges =
721           Var.DefRanges.back().Ranges;
722       if (!Ranges.empty() && Ranges.back().second == Begin)
723         Ranges.back().second = End;
724       else
725         Ranges.emplace_back(Begin, End);
726 
727       // FIXME: Do more range combining.
728     }
729 
730     recordLocalVariable(std::move(Var), InlinedAt);
731   }
732 }
733 
734 void CodeViewDebug::beginFunction(const MachineFunction *MF) {
735   assert(!CurFn && "Can't process two functions at once!");
736 
737   if (!Asm || !MMI->hasDebugInfo())
738     return;
739 
740   DebugHandlerBase::beginFunction(MF);
741 
742   const Function *GV = MF->getFunction();
743   assert(FnDebugInfo.count(GV) == false);
744   CurFn = &FnDebugInfo[GV];
745   CurFn->FuncId = NextFuncId++;
746   CurFn->Begin = Asm->getFunctionBegin();
747 
748   // Find the end of the function prolog.  First known non-DBG_VALUE and
749   // non-frame setup location marks the beginning of the function body.
750   // FIXME: is there a simpler a way to do this? Can we just search
751   // for the first instruction of the function, not the last of the prolog?
752   DebugLoc PrologEndLoc;
753   bool EmptyPrologue = true;
754   for (const auto &MBB : *MF) {
755     for (const auto &MI : MBB) {
756       if (!MI.isDebugValue() && !MI.getFlag(MachineInstr::FrameSetup) &&
757           MI.getDebugLoc()) {
758         PrologEndLoc = MI.getDebugLoc();
759         break;
760       } else if (!MI.isDebugValue()) {
761         EmptyPrologue = false;
762       }
763     }
764   }
765 
766   // Record beginning of function if we have a non-empty prologue.
767   if (PrologEndLoc && !EmptyPrologue) {
768     DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc();
769     maybeRecordLocation(FnStartDL, MF);
770   }
771 }
772 
773 TypeIndex CodeViewDebug::lowerType(const DIType *Ty) {
774   // Generic dispatch for lowering an unknown type.
775   switch (Ty->getTag()) {
776   case dwarf::DW_TAG_array_type:
777     return lowerTypeArray(cast<DICompositeType>(Ty));
778   case dwarf::DW_TAG_typedef:
779     return lowerTypeAlias(cast<DIDerivedType>(Ty));
780   case dwarf::DW_TAG_base_type:
781     return lowerTypeBasic(cast<DIBasicType>(Ty));
782   case dwarf::DW_TAG_pointer_type:
783   case dwarf::DW_TAG_reference_type:
784   case dwarf::DW_TAG_rvalue_reference_type:
785     return lowerTypePointer(cast<DIDerivedType>(Ty));
786   case dwarf::DW_TAG_ptr_to_member_type:
787     return lowerTypeMemberPointer(cast<DIDerivedType>(Ty));
788   case dwarf::DW_TAG_const_type:
789   case dwarf::DW_TAG_volatile_type:
790     return lowerTypeModifier(cast<DIDerivedType>(Ty));
791   case dwarf::DW_TAG_subroutine_type:
792     return lowerTypeFunction(cast<DISubroutineType>(Ty));
793   case dwarf::DW_TAG_enumeration_type:
794     return lowerTypeEnum(cast<DICompositeType>(Ty));
795   case dwarf::DW_TAG_class_type:
796   case dwarf::DW_TAG_structure_type:
797     return lowerTypeClass(cast<DICompositeType>(Ty));
798   case dwarf::DW_TAG_union_type:
799     return lowerTypeUnion(cast<DICompositeType>(Ty));
800   default:
801     // Use the null type index.
802     return TypeIndex();
803   }
804 }
805 
806 TypeIndex CodeViewDebug::lowerTypeAlias(const DIDerivedType *Ty) {
807   DITypeRef UnderlyingTypeRef = Ty->getBaseType();
808   TypeIndex UnderlyingTypeIndex = getTypeIndex(UnderlyingTypeRef);
809   StringRef TypeName = Ty->getName();
810 
811   SmallVector<StringRef, 5> QualifiedNameComponents;
812   const DISubprogram *ClosestSubprogram = getQualifiedNameComponents(
813       Ty->getScope().resolve(), QualifiedNameComponents);
814 
815   if (ClosestSubprogram == nullptr) {
816     std::string FullyQualifiedName =
817         getQualifiedName(QualifiedNameComponents, TypeName);
818     GlobalUDTs.emplace_back(std::move(FullyQualifiedName), UnderlyingTypeIndex);
819   } else if (ClosestSubprogram == CurrentSubprogram) {
820     std::string FullyQualifiedName =
821         getQualifiedName(QualifiedNameComponents, TypeName);
822     LocalUDTs.emplace_back(std::move(FullyQualifiedName), UnderlyingTypeIndex);
823   }
824   // TODO: What if the ClosestSubprogram is neither null or the current
825   // subprogram?  Currently, the UDT just gets dropped on the floor.
826   //
827   // The current behavior is not desirable.  To get maximal fidelity, we would
828   // need to perform all type translation before beginning emission of .debug$S
829   // and then make LocalUDTs a member of FunctionInfo
830 
831   if (UnderlyingTypeIndex == TypeIndex(SimpleTypeKind::Int32Long) &&
832       TypeName == "HRESULT")
833     return TypeIndex(SimpleTypeKind::HResult);
834   if (UnderlyingTypeIndex == TypeIndex(SimpleTypeKind::UInt16Short) &&
835       TypeName == "wchar_t")
836     return TypeIndex(SimpleTypeKind::WideCharacter);
837   return UnderlyingTypeIndex;
838 }
839 
840 TypeIndex CodeViewDebug::lowerTypeArray(const DICompositeType *Ty) {
841   DITypeRef ElementTypeRef = Ty->getBaseType();
842   TypeIndex ElementTypeIndex = getTypeIndex(ElementTypeRef);
843   // IndexType is size_t, which depends on the bitness of the target.
844   TypeIndex IndexType = Asm->MAI->getPointerSize() == 8
845                             ? TypeIndex(SimpleTypeKind::UInt64Quad)
846                             : TypeIndex(SimpleTypeKind::UInt32Long);
847   uint64_t Size = Ty->getSizeInBits() / 8;
848   ArrayRecord Record(ElementTypeIndex, IndexType, Size, Ty->getName());
849   return TypeTable.writeArray(Record);
850 }
851 
852 TypeIndex CodeViewDebug::lowerTypeBasic(const DIBasicType *Ty) {
853   TypeIndex Index;
854   dwarf::TypeKind Kind;
855   uint32_t ByteSize;
856 
857   Kind = static_cast<dwarf::TypeKind>(Ty->getEncoding());
858   ByteSize = Ty->getSizeInBits() / 8;
859 
860   SimpleTypeKind STK = SimpleTypeKind::None;
861   switch (Kind) {
862   case dwarf::DW_ATE_address:
863     // FIXME: Translate
864     break;
865   case dwarf::DW_ATE_boolean:
866     switch (ByteSize) {
867     case 1:  STK = SimpleTypeKind::Boolean8;   break;
868     case 2:  STK = SimpleTypeKind::Boolean16;  break;
869     case 4:  STK = SimpleTypeKind::Boolean32;  break;
870     case 8:  STK = SimpleTypeKind::Boolean64;  break;
871     case 16: STK = SimpleTypeKind::Boolean128; break;
872     }
873     break;
874   case dwarf::DW_ATE_complex_float:
875     switch (ByteSize) {
876     case 2:  STK = SimpleTypeKind::Complex16;  break;
877     case 4:  STK = SimpleTypeKind::Complex32;  break;
878     case 8:  STK = SimpleTypeKind::Complex64;  break;
879     case 10: STK = SimpleTypeKind::Complex80;  break;
880     case 16: STK = SimpleTypeKind::Complex128; break;
881     }
882     break;
883   case dwarf::DW_ATE_float:
884     switch (ByteSize) {
885     case 2:  STK = SimpleTypeKind::Float16;  break;
886     case 4:  STK = SimpleTypeKind::Float32;  break;
887     case 6:  STK = SimpleTypeKind::Float48;  break;
888     case 8:  STK = SimpleTypeKind::Float64;  break;
889     case 10: STK = SimpleTypeKind::Float80;  break;
890     case 16: STK = SimpleTypeKind::Float128; break;
891     }
892     break;
893   case dwarf::DW_ATE_signed:
894     switch (ByteSize) {
895     case 1:  STK = SimpleTypeKind::SByte;      break;
896     case 2:  STK = SimpleTypeKind::Int16Short; break;
897     case 4:  STK = SimpleTypeKind::Int32;      break;
898     case 8:  STK = SimpleTypeKind::Int64Quad;  break;
899     case 16: STK = SimpleTypeKind::Int128Oct;  break;
900     }
901     break;
902   case dwarf::DW_ATE_unsigned:
903     switch (ByteSize) {
904     case 1:  STK = SimpleTypeKind::Byte;        break;
905     case 2:  STK = SimpleTypeKind::UInt16Short; break;
906     case 4:  STK = SimpleTypeKind::UInt32;      break;
907     case 8:  STK = SimpleTypeKind::UInt64Quad;  break;
908     case 16: STK = SimpleTypeKind::UInt128Oct;  break;
909     }
910     break;
911   case dwarf::DW_ATE_UTF:
912     switch (ByteSize) {
913     case 2: STK = SimpleTypeKind::Character16; break;
914     case 4: STK = SimpleTypeKind::Character32; break;
915     }
916     break;
917   case dwarf::DW_ATE_signed_char:
918     if (ByteSize == 1)
919       STK = SimpleTypeKind::SignedCharacter;
920     break;
921   case dwarf::DW_ATE_unsigned_char:
922     if (ByteSize == 1)
923       STK = SimpleTypeKind::UnsignedCharacter;
924     break;
925   default:
926     break;
927   }
928 
929   // Apply some fixups based on the source-level type name.
930   if (STK == SimpleTypeKind::Int32 && Ty->getName() == "long int")
931     STK = SimpleTypeKind::Int32Long;
932   if (STK == SimpleTypeKind::UInt32 && Ty->getName() == "long unsigned int")
933     STK = SimpleTypeKind::UInt32Long;
934   if (STK == SimpleTypeKind::UInt16Short &&
935       (Ty->getName() == "wchar_t" || Ty->getName() == "__wchar_t"))
936     STK = SimpleTypeKind::WideCharacter;
937   if ((STK == SimpleTypeKind::SignedCharacter ||
938        STK == SimpleTypeKind::UnsignedCharacter) &&
939       Ty->getName() == "char")
940     STK = SimpleTypeKind::NarrowCharacter;
941 
942   return TypeIndex(STK);
943 }
944 
945 TypeIndex CodeViewDebug::lowerTypePointer(const DIDerivedType *Ty) {
946   TypeIndex PointeeTI = getTypeIndex(Ty->getBaseType());
947 
948   // Pointers to simple types can use SimpleTypeMode, rather than having a
949   // dedicated pointer type record.
950   if (PointeeTI.isSimple() &&
951       PointeeTI.getSimpleMode() == SimpleTypeMode::Direct &&
952       Ty->getTag() == dwarf::DW_TAG_pointer_type) {
953     SimpleTypeMode Mode = Ty->getSizeInBits() == 64
954                               ? SimpleTypeMode::NearPointer64
955                               : SimpleTypeMode::NearPointer32;
956     return TypeIndex(PointeeTI.getSimpleKind(), Mode);
957   }
958 
959   PointerKind PK =
960       Ty->getSizeInBits() == 64 ? PointerKind::Near64 : PointerKind::Near32;
961   PointerMode PM = PointerMode::Pointer;
962   switch (Ty->getTag()) {
963   default: llvm_unreachable("not a pointer tag type");
964   case dwarf::DW_TAG_pointer_type:
965     PM = PointerMode::Pointer;
966     break;
967   case dwarf::DW_TAG_reference_type:
968     PM = PointerMode::LValueReference;
969     break;
970   case dwarf::DW_TAG_rvalue_reference_type:
971     PM = PointerMode::RValueReference;
972     break;
973   }
974   // FIXME: MSVC folds qualifiers into PointerOptions in the context of a method
975   // 'this' pointer, but not normal contexts. Figure out what we're supposed to
976   // do.
977   PointerOptions PO = PointerOptions::None;
978   PointerRecord PR(PointeeTI, PK, PM, PO, Ty->getSizeInBits() / 8);
979   return TypeTable.writePointer(PR);
980 }
981 
982 TypeIndex CodeViewDebug::lowerTypeMemberPointer(const DIDerivedType *Ty) {
983   assert(Ty->getTag() == dwarf::DW_TAG_ptr_to_member_type);
984   TypeIndex ClassTI = getTypeIndex(Ty->getClassType());
985   TypeIndex PointeeTI = getTypeIndex(Ty->getBaseType());
986   PointerKind PK = Asm->MAI->getPointerSize() == 8 ? PointerKind::Near64
987                                                    : PointerKind::Near32;
988   PointerMode PM = isa<DISubroutineType>(Ty->getBaseType())
989                        ? PointerMode::PointerToMemberFunction
990                        : PointerMode::PointerToDataMember;
991   PointerOptions PO = PointerOptions::None; // FIXME
992   // FIXME: Thread this ABI info through metadata.
993   PointerToMemberRepresentation PMR = PointerToMemberRepresentation::Unknown;
994   MemberPointerInfo MPI(ClassTI, PMR);
995   PointerRecord PR(PointeeTI, PK, PM, PO, Ty->getSizeInBits() / 8, MPI);
996   return TypeTable.writePointer(PR);
997 }
998 
999 /// Given a DWARF calling convention, get the CodeView equivalent. If we don't
1000 /// have a translation, use the NearC convention.
1001 static CallingConvention dwarfCCToCodeView(unsigned DwarfCC) {
1002   switch (DwarfCC) {
1003   case dwarf::DW_CC_normal:             return CallingConvention::NearC;
1004   case dwarf::DW_CC_BORLAND_msfastcall: return CallingConvention::NearFast;
1005   case dwarf::DW_CC_BORLAND_thiscall:   return CallingConvention::ThisCall;
1006   case dwarf::DW_CC_BORLAND_stdcall:    return CallingConvention::NearStdCall;
1007   case dwarf::DW_CC_BORLAND_pascal:     return CallingConvention::NearPascal;
1008   case dwarf::DW_CC_LLVM_vectorcall:    return CallingConvention::NearVector;
1009   }
1010   return CallingConvention::NearC;
1011 }
1012 
1013 TypeIndex CodeViewDebug::lowerTypeModifier(const DIDerivedType *Ty) {
1014   ModifierOptions Mods = ModifierOptions::None;
1015   bool IsModifier = true;
1016   const DIType *BaseTy = Ty;
1017   while (IsModifier && BaseTy) {
1018     // FIXME: Need to add DWARF tag for __unaligned.
1019     switch (BaseTy->getTag()) {
1020     case dwarf::DW_TAG_const_type:
1021       Mods |= ModifierOptions::Const;
1022       break;
1023     case dwarf::DW_TAG_volatile_type:
1024       Mods |= ModifierOptions::Volatile;
1025       break;
1026     default:
1027       IsModifier = false;
1028       break;
1029     }
1030     if (IsModifier)
1031       BaseTy = cast<DIDerivedType>(BaseTy)->getBaseType().resolve();
1032   }
1033   TypeIndex ModifiedTI = getTypeIndex(BaseTy);
1034   ModifierRecord MR(ModifiedTI, Mods);
1035   return TypeTable.writeModifier(MR);
1036 }
1037 
1038 TypeIndex CodeViewDebug::lowerTypeFunction(const DISubroutineType *Ty) {
1039   SmallVector<TypeIndex, 8> ReturnAndArgTypeIndices;
1040   for (DITypeRef ArgTypeRef : Ty->getTypeArray())
1041     ReturnAndArgTypeIndices.push_back(getTypeIndex(ArgTypeRef));
1042 
1043   TypeIndex ReturnTypeIndex = TypeIndex::Void();
1044   ArrayRef<TypeIndex> ArgTypeIndices = None;
1045   if (!ReturnAndArgTypeIndices.empty()) {
1046     auto ReturnAndArgTypesRef = makeArrayRef(ReturnAndArgTypeIndices);
1047     ReturnTypeIndex = ReturnAndArgTypesRef.front();
1048     ArgTypeIndices = ReturnAndArgTypesRef.drop_front();
1049   }
1050 
1051   ArgListRecord ArgListRec(TypeRecordKind::ArgList, ArgTypeIndices);
1052   TypeIndex ArgListIndex = TypeTable.writeArgList(ArgListRec);
1053 
1054   CallingConvention CC = dwarfCCToCodeView(Ty->getCC());
1055 
1056   // TODO: Some functions are member functions, we should use a more appropriate
1057   // record for those.
1058   ProcedureRecord Procedure(ReturnTypeIndex, CC, FunctionOptions::None,
1059                             ArgTypeIndices.size(), ArgListIndex);
1060   return TypeTable.writeProcedure(Procedure);
1061 }
1062 
1063 static MemberAccess translateAccessFlags(unsigned RecordTag,
1064                                          const DIType *Member) {
1065   switch (Member->getFlags() & DINode::FlagAccessibility) {
1066   case DINode::FlagPrivate:   return MemberAccess::Private;
1067   case DINode::FlagPublic:    return MemberAccess::Public;
1068   case DINode::FlagProtected: return MemberAccess::Protected;
1069   case 0:
1070     // If there was no explicit access control, provide the default for the tag.
1071     return RecordTag == dwarf::DW_TAG_class_type ? MemberAccess::Private
1072                                                  : MemberAccess::Public;
1073   }
1074   llvm_unreachable("access flags are exclusive");
1075 }
1076 
1077 static TypeRecordKind getRecordKind(const DICompositeType *Ty) {
1078   switch (Ty->getTag()) {
1079   case dwarf::DW_TAG_class_type:     return TypeRecordKind::Class;
1080   case dwarf::DW_TAG_structure_type: return TypeRecordKind::Struct;
1081   }
1082   llvm_unreachable("unexpected tag");
1083 }
1084 
1085 /// Return the HasUniqueName option if it should be present in ClassOptions, or
1086 /// None otherwise.
1087 static ClassOptions getRecordUniqueNameOption(const DICompositeType *Ty) {
1088   // MSVC always sets this flag now, even for local types. Clang doesn't always
1089   // appear to give every type a linkage name, which may be problematic for us.
1090   // FIXME: Investigate the consequences of not following them here.
1091   return !Ty->getIdentifier().empty() ? ClassOptions::HasUniqueName
1092                                       : ClassOptions::None;
1093 }
1094 
1095 TypeIndex CodeViewDebug::lowerTypeEnum(const DICompositeType *Ty) {
1096   ClassOptions CO = ClassOptions::None | getRecordUniqueNameOption(Ty);
1097   TypeIndex FTI;
1098   unsigned FieldCount = 0;
1099 
1100   if (Ty->isForwardDecl())
1101     CO |= ClassOptions::ForwardReference;
1102   else
1103     std::tie(FTI, FieldCount) = lowerRecordFieldList(Ty);
1104 
1105   return TypeTable.writeEnum(EnumRecord(FieldCount, CO, FTI, Ty->getName(),
1106                                         Ty->getIdentifier(),
1107                                         getTypeIndex(Ty->getBaseType())));
1108 }
1109 
1110 TypeIndex CodeViewDebug::lowerTypeClass(const DICompositeType *Ty) {
1111   // First, construct the forward decl.  Don't look into Ty to compute the
1112   // forward decl options, since it might not be available in all TUs.
1113   TypeRecordKind Kind = getRecordKind(Ty);
1114   ClassOptions CO =
1115       ClassOptions::ForwardReference | getRecordUniqueNameOption(Ty);
1116   TypeIndex FwdDeclTI = TypeTable.writeClass(ClassRecord(
1117       Kind, 0, CO, HfaKind::None, WindowsRTClassKind::None, TypeIndex(),
1118       TypeIndex(), TypeIndex(), 0, Ty->getName(), Ty->getIdentifier()));
1119   return FwdDeclTI;
1120 }
1121 
1122 TypeIndex CodeViewDebug::lowerCompleteTypeClass(const DICompositeType *Ty) {
1123   // Construct the field list and complete type record.
1124   TypeRecordKind Kind = getRecordKind(Ty);
1125   // FIXME: Other ClassOptions, like ContainsNestedClass and NestedClass.
1126   ClassOptions CO = ClassOptions::None | getRecordUniqueNameOption(Ty);
1127   TypeIndex FTI;
1128   unsigned FieldCount;
1129   std::tie(FTI, FieldCount) = lowerRecordFieldList(Ty);
1130 
1131   uint64_t SizeInBytes = Ty->getSizeInBits() / 8;
1132   return TypeTable.writeClass(ClassRecord(Kind, FieldCount, CO, HfaKind::None,
1133                                           WindowsRTClassKind::None, FTI,
1134                                           TypeIndex(), TypeIndex(), SizeInBytes,
1135                                           Ty->getName(), Ty->getIdentifier()));
1136   // FIXME: Make an LF_UDT_SRC_LINE record.
1137 }
1138 
1139 TypeIndex CodeViewDebug::lowerTypeUnion(const DICompositeType *Ty) {
1140   ClassOptions CO =
1141       ClassOptions::ForwardReference | getRecordUniqueNameOption(Ty);
1142   TypeIndex FwdDeclTI =
1143       TypeTable.writeUnion(UnionRecord(0, CO, HfaKind::None, TypeIndex(), 0,
1144                                        Ty->getName(), Ty->getIdentifier()));
1145   return FwdDeclTI;
1146 }
1147 
1148 TypeIndex CodeViewDebug::lowerCompleteTypeUnion(const DICompositeType *Ty) {
1149   ClassOptions CO = ClassOptions::None | getRecordUniqueNameOption(Ty);
1150   TypeIndex FTI;
1151   unsigned FieldCount;
1152   std::tie(FTI, FieldCount) = lowerRecordFieldList(Ty);
1153   uint64_t SizeInBytes = Ty->getSizeInBits() / 8;
1154   return TypeTable.writeUnion(UnionRecord(FieldCount, CO, HfaKind::None, FTI,
1155                                           SizeInBytes, Ty->getName(),
1156                                           Ty->getIdentifier()));
1157   // FIXME: Make an LF_UDT_SRC_LINE record.
1158 }
1159 
1160 std::pair<TypeIndex, unsigned>
1161 CodeViewDebug::lowerRecordFieldList(const DICompositeType *Ty) {
1162   // Manually count members. MSVC appears to count everything that generates a
1163   // field list record. Each individual overload in a method overload group
1164   // contributes to this count, even though the overload group is a single field
1165   // list record.
1166   unsigned MemberCount = 0;
1167   FieldListRecordBuilder Fields;
1168   for (const DINode *Element : Ty->getElements()) {
1169     // We assume that the frontend provides all members in source declaration
1170     // order, which is what MSVC does.
1171     if (!Element)
1172       continue;
1173     if (auto *SP = dyn_cast<DISubprogram>(Element)) {
1174       // C++ method.
1175       // FIXME: Overloaded methods are grouped together, so we'll need two
1176       // passes to group them.
1177       (void)SP;
1178     } else if (auto *Member = dyn_cast<DIDerivedType>(Element)) {
1179       if (Member->getTag() == dwarf::DW_TAG_member) {
1180         if (Member->isStaticMember()) {
1181           // Static data member.
1182           Fields.writeStaticDataMember(StaticDataMemberRecord(
1183               translateAccessFlags(Ty->getTag(), Member),
1184               getTypeIndex(Member->getBaseType()), Member->getName()));
1185           MemberCount++;
1186         } else {
1187           // Data member.
1188           // FIXME: Make a BitFieldRecord for bitfields.
1189           Fields.writeDataMember(DataMemberRecord(
1190               translateAccessFlags(Ty->getTag(), Member),
1191               getTypeIndex(Member->getBaseType()),
1192               Member->getOffsetInBits() / 8, Member->getName()));
1193           MemberCount++;
1194         }
1195       } else if (Member->getTag() == dwarf::DW_TAG_friend) {
1196         // Ignore friend members. It appears that MSVC emitted info about
1197         // friends in the past, but modern versions do not.
1198       }
1199       // FIXME: Get clang to emit nested types here and do something with
1200       // them.
1201     } else if (auto *Enumerator = dyn_cast<DIEnumerator>(Element)) {
1202       Fields.writeEnumerator(EnumeratorRecord(
1203           MemberAccess::Public, APSInt::getUnsigned(Enumerator->getValue()),
1204           Enumerator->getName()));
1205     }
1206     // Skip other unrecognized kinds of elements.
1207   }
1208   return {TypeTable.writeFieldList(Fields), MemberCount};
1209 }
1210 
1211 TypeIndex CodeViewDebug::getTypeIndex(DITypeRef TypeRef) {
1212   const DIType *Ty = TypeRef.resolve();
1213 
1214   // The null DIType is the void type. Don't try to hash it.
1215   if (!Ty)
1216     return TypeIndex::Void();
1217 
1218   // Check if we've already translated this type. Don't try to do a
1219   // get-or-create style insertion that caches the hash lookup across the
1220   // lowerType call. It will update the TypeIndices map.
1221   auto I = TypeIndices.find(Ty);
1222   if (I != TypeIndices.end())
1223     return I->second;
1224 
1225   TypeIndex TI = lowerType(Ty);
1226 
1227   recordTypeIndexForDINode(Ty, TI);
1228   return TI;
1229 }
1230 
1231 TypeIndex CodeViewDebug::getCompleteTypeIndex(DITypeRef TypeRef) {
1232   const DIType *Ty = TypeRef.resolve();
1233 
1234   // The null DIType is the void type. Don't try to hash it.
1235   if (!Ty)
1236     return TypeIndex::Void();
1237 
1238   // If this is a non-record type, the complete type index is the same as the
1239   // normal type index. Just call getTypeIndex.
1240   switch (Ty->getTag()) {
1241   case dwarf::DW_TAG_class_type:
1242   case dwarf::DW_TAG_structure_type:
1243   case dwarf::DW_TAG_union_type:
1244     break;
1245   default:
1246     return getTypeIndex(Ty);
1247   }
1248 
1249   // Check if we've already translated the complete record type.  Lowering a
1250   // complete type should never trigger lowering another complete type, so we
1251   // can reuse the hash table lookup result.
1252   const auto *CTy = cast<DICompositeType>(Ty);
1253   auto InsertResult = CompleteTypeIndices.insert({CTy, TypeIndex()});
1254   if (!InsertResult.second)
1255     return InsertResult.first->second;
1256 
1257   // Make sure the forward declaration is emitted first. It's unclear if this
1258   // is necessary, but MSVC does it, and we should follow suit until we can show
1259   // otherwise.
1260   TypeIndex FwdDeclTI = getTypeIndex(CTy);
1261 
1262   // Just use the forward decl if we don't have complete type info. This might
1263   // happen if the frontend is using modules and expects the complete definition
1264   // to be emitted elsewhere.
1265   if (CTy->isForwardDecl())
1266     return FwdDeclTI;
1267 
1268   TypeIndex TI;
1269   switch (CTy->getTag()) {
1270   case dwarf::DW_TAG_class_type:
1271   case dwarf::DW_TAG_structure_type:
1272     TI = lowerCompleteTypeClass(CTy);
1273     break;
1274   case dwarf::DW_TAG_union_type:
1275     TI = lowerCompleteTypeUnion(CTy);
1276     break;
1277   default:
1278     llvm_unreachable("not a record");
1279   }
1280 
1281   InsertResult.first->second = TI;
1282   return TI;
1283 }
1284 
1285 void CodeViewDebug::emitLocalVariable(const LocalVariable &Var) {
1286   // LocalSym record, see SymbolRecord.h for more info.
1287   MCSymbol *LocalBegin = MMI->getContext().createTempSymbol(),
1288            *LocalEnd = MMI->getContext().createTempSymbol();
1289   OS.AddComment("Record length");
1290   OS.emitAbsoluteSymbolDiff(LocalEnd, LocalBegin, 2);
1291   OS.EmitLabel(LocalBegin);
1292 
1293   OS.AddComment("Record kind: S_LOCAL");
1294   OS.EmitIntValue(unsigned(SymbolKind::S_LOCAL), 2);
1295 
1296   LocalSymFlags Flags = LocalSymFlags::None;
1297   if (Var.DIVar->isParameter())
1298     Flags |= LocalSymFlags::IsParameter;
1299   if (Var.DefRanges.empty())
1300     Flags |= LocalSymFlags::IsOptimizedOut;
1301 
1302   OS.AddComment("TypeIndex");
1303   TypeIndex TI = getCompleteTypeIndex(Var.DIVar->getType());
1304   OS.EmitIntValue(TI.getIndex(), 4);
1305   OS.AddComment("Flags");
1306   OS.EmitIntValue(static_cast<uint16_t>(Flags), 2);
1307   // Truncate the name so we won't overflow the record length field.
1308   emitNullTerminatedSymbolName(OS, Var.DIVar->getName());
1309   OS.EmitLabel(LocalEnd);
1310 
1311   // Calculate the on disk prefix of the appropriate def range record. The
1312   // records and on disk formats are described in SymbolRecords.h. BytePrefix
1313   // should be big enough to hold all forms without memory allocation.
1314   SmallString<20> BytePrefix;
1315   for (const LocalVarDefRange &DefRange : Var.DefRanges) {
1316     BytePrefix.clear();
1317     // FIXME: Handle bitpieces.
1318     if (DefRange.StructOffset != 0)
1319       continue;
1320 
1321     if (DefRange.InMemory) {
1322       DefRangeRegisterRelSym Sym(DefRange.CVRegister, 0, DefRange.DataOffset, 0,
1323                                  0, 0, ArrayRef<LocalVariableAddrGap>());
1324       ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER_REL);
1325       BytePrefix +=
1326           StringRef(reinterpret_cast<const char *>(&SymKind), sizeof(SymKind));
1327       BytePrefix +=
1328           StringRef(reinterpret_cast<const char *>(&Sym.Header),
1329                     sizeof(Sym.Header) - sizeof(LocalVariableAddrRange));
1330     } else {
1331       assert(DefRange.DataOffset == 0 && "unexpected offset into register");
1332       // Unclear what matters here.
1333       DefRangeRegisterSym Sym(DefRange.CVRegister, 0, 0, 0, 0,
1334                               ArrayRef<LocalVariableAddrGap>());
1335       ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER);
1336       BytePrefix +=
1337           StringRef(reinterpret_cast<const char *>(&SymKind), sizeof(SymKind));
1338       BytePrefix +=
1339           StringRef(reinterpret_cast<const char *>(&Sym.Header),
1340                     sizeof(Sym.Header) - sizeof(LocalVariableAddrRange));
1341     }
1342     OS.EmitCVDefRangeDirective(DefRange.Ranges, BytePrefix);
1343   }
1344 }
1345 
1346 void CodeViewDebug::endFunction(const MachineFunction *MF) {
1347   if (!Asm || !CurFn)  // We haven't created any debug info for this function.
1348     return;
1349 
1350   const Function *GV = MF->getFunction();
1351   assert(FnDebugInfo.count(GV));
1352   assert(CurFn == &FnDebugInfo[GV]);
1353 
1354   collectVariableInfo(GV->getSubprogram());
1355 
1356   DebugHandlerBase::endFunction(MF);
1357 
1358   // Don't emit anything if we don't have any line tables.
1359   if (!CurFn->HaveLineInfo) {
1360     FnDebugInfo.erase(GV);
1361     CurFn = nullptr;
1362     return;
1363   }
1364 
1365   CurFn->End = Asm->getFunctionEnd();
1366 
1367   CurFn = nullptr;
1368 }
1369 
1370 void CodeViewDebug::beginInstruction(const MachineInstr *MI) {
1371   DebugHandlerBase::beginInstruction(MI);
1372 
1373   // Ignore DBG_VALUE locations and function prologue.
1374   if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup))
1375     return;
1376   DebugLoc DL = MI->getDebugLoc();
1377   if (DL == PrevInstLoc || !DL)
1378     return;
1379   maybeRecordLocation(DL, Asm->MF);
1380 }
1381 
1382 MCSymbol *CodeViewDebug::beginCVSubsection(ModuleSubstreamKind Kind) {
1383   MCSymbol *BeginLabel = MMI->getContext().createTempSymbol(),
1384            *EndLabel = MMI->getContext().createTempSymbol();
1385   OS.EmitIntValue(unsigned(Kind), 4);
1386   OS.AddComment("Subsection size");
1387   OS.emitAbsoluteSymbolDiff(EndLabel, BeginLabel, 4);
1388   OS.EmitLabel(BeginLabel);
1389   return EndLabel;
1390 }
1391 
1392 void CodeViewDebug::endCVSubsection(MCSymbol *EndLabel) {
1393   OS.EmitLabel(EndLabel);
1394   // Every subsection must be aligned to a 4-byte boundary.
1395   OS.EmitValueToAlignment(4);
1396 }
1397 
1398 void CodeViewDebug::emitDebugInfoForUDTs(
1399     ArrayRef<std::pair<std::string, TypeIndex>> UDTs) {
1400   for (const std::pair<std::string, codeview::TypeIndex> &UDT : UDTs) {
1401     MCSymbol *UDTRecordBegin = MMI->getContext().createTempSymbol(),
1402              *UDTRecordEnd = MMI->getContext().createTempSymbol();
1403     OS.AddComment("Record length");
1404     OS.emitAbsoluteSymbolDiff(UDTRecordEnd, UDTRecordBegin, 2);
1405     OS.EmitLabel(UDTRecordBegin);
1406 
1407     OS.AddComment("Record kind: S_UDT");
1408     OS.EmitIntValue(unsigned(SymbolKind::S_UDT), 2);
1409 
1410     OS.AddComment("Type");
1411     OS.EmitIntValue(UDT.second.getIndex(), 4);
1412 
1413     emitNullTerminatedSymbolName(OS, UDT.first);
1414     OS.EmitLabel(UDTRecordEnd);
1415   }
1416 }
1417 
1418 void CodeViewDebug::emitDebugInfoForGlobals() {
1419   NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
1420   for (const MDNode *Node : CUs->operands()) {
1421     const auto *CU = cast<DICompileUnit>(Node);
1422 
1423     // First, emit all globals that are not in a comdat in a single symbol
1424     // substream. MSVC doesn't like it if the substream is empty, so only open
1425     // it if we have at least one global to emit.
1426     switchToDebugSectionForSymbol(nullptr);
1427     MCSymbol *EndLabel = nullptr;
1428     for (const DIGlobalVariable *G : CU->getGlobalVariables()) {
1429       if (const auto *GV = dyn_cast_or_null<GlobalVariable>(G->getVariable())) {
1430         if (!GV->hasComdat() && !GV->isDeclarationForLinker()) {
1431           if (!EndLabel) {
1432             OS.AddComment("Symbol subsection for globals");
1433             EndLabel = beginCVSubsection(ModuleSubstreamKind::Symbols);
1434           }
1435           emitDebugInfoForGlobal(G, Asm->getSymbol(GV));
1436         }
1437       }
1438     }
1439     if (EndLabel)
1440       endCVSubsection(EndLabel);
1441 
1442     // Second, emit each global that is in a comdat into its own .debug$S
1443     // section along with its own symbol substream.
1444     for (const DIGlobalVariable *G : CU->getGlobalVariables()) {
1445       if (const auto *GV = dyn_cast_or_null<GlobalVariable>(G->getVariable())) {
1446         if (GV->hasComdat()) {
1447           MCSymbol *GVSym = Asm->getSymbol(GV);
1448           OS.AddComment("Symbol subsection for " +
1449                         Twine(GlobalValue::getRealLinkageName(GV->getName())));
1450           switchToDebugSectionForSymbol(GVSym);
1451           EndLabel = beginCVSubsection(ModuleSubstreamKind::Symbols);
1452           emitDebugInfoForGlobal(G, GVSym);
1453           endCVSubsection(EndLabel);
1454         }
1455       }
1456     }
1457   }
1458 }
1459 
1460 void CodeViewDebug::emitDebugInfoForGlobal(const DIGlobalVariable *DIGV,
1461                                            MCSymbol *GVSym) {
1462   // DataSym record, see SymbolRecord.h for more info.
1463   // FIXME: Thread local data, etc
1464   MCSymbol *DataBegin = MMI->getContext().createTempSymbol(),
1465            *DataEnd = MMI->getContext().createTempSymbol();
1466   OS.AddComment("Record length");
1467   OS.emitAbsoluteSymbolDiff(DataEnd, DataBegin, 2);
1468   OS.EmitLabel(DataBegin);
1469   OS.AddComment("Record kind: S_GDATA32");
1470   OS.EmitIntValue(unsigned(SymbolKind::S_GDATA32), 2);
1471   OS.AddComment("Type");
1472   OS.EmitIntValue(getCompleteTypeIndex(DIGV->getType()).getIndex(), 4);
1473   OS.AddComment("DataOffset");
1474   OS.EmitCOFFSecRel32(GVSym);
1475   OS.AddComment("Segment");
1476   OS.EmitCOFFSectionIndex(GVSym);
1477   OS.AddComment("Name");
1478   emitNullTerminatedSymbolName(OS, DIGV->getName());
1479   OS.EmitLabel(DataEnd);
1480 }
1481