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 static PointerToMemberRepresentation
983 translatePtrToMemberRep(unsigned SizeInBytes, bool IsPMF, unsigned Flags) {
984   // SizeInBytes being zero generally implies that the member pointer type was
985   // incomplete, which can happen if it is part of a function prototype. In this
986   // case, use the unknown model instead of the general model.
987   if (IsPMF) {
988     switch (Flags & DINode::FlagPtrToMemberRep) {
989     case 0:
990       return SizeInBytes == 0 ? PointerToMemberRepresentation::Unknown
991                               : PointerToMemberRepresentation::GeneralFunction;
992     case DINode::FlagSingleInheritance:
993       return PointerToMemberRepresentation::SingleInheritanceFunction;
994     case DINode::FlagMultipleInheritance:
995       return PointerToMemberRepresentation::MultipleInheritanceFunction;
996     case DINode::FlagVirtualInheritance:
997       return PointerToMemberRepresentation::VirtualInheritanceFunction;
998     }
999   } else {
1000     switch (Flags & DINode::FlagPtrToMemberRep) {
1001     case 0:
1002       return SizeInBytes == 0 ? PointerToMemberRepresentation::Unknown
1003                               : PointerToMemberRepresentation::GeneralData;
1004     case DINode::FlagSingleInheritance:
1005       return PointerToMemberRepresentation::SingleInheritanceData;
1006     case DINode::FlagMultipleInheritance:
1007       return PointerToMemberRepresentation::MultipleInheritanceData;
1008     case DINode::FlagVirtualInheritance:
1009       return PointerToMemberRepresentation::VirtualInheritanceData;
1010     }
1011   }
1012   llvm_unreachable("invalid ptr to member representation");
1013 }
1014 
1015 TypeIndex CodeViewDebug::lowerTypeMemberPointer(const DIDerivedType *Ty) {
1016   assert(Ty->getTag() == dwarf::DW_TAG_ptr_to_member_type);
1017   TypeIndex ClassTI = getTypeIndex(Ty->getClassType());
1018   TypeIndex PointeeTI = getTypeIndex(Ty->getBaseType());
1019   PointerKind PK = Asm->MAI->getPointerSize() == 8 ? PointerKind::Near64
1020                                                    : PointerKind::Near32;
1021   bool IsPMF = isa<DISubroutineType>(Ty->getBaseType());
1022   PointerMode PM = IsPMF ? PointerMode::PointerToMemberFunction
1023                          : PointerMode::PointerToDataMember;
1024   PointerOptions PO = PointerOptions::None; // FIXME
1025   assert(Ty->getSizeInBits() / 8 <= 0xff && "pointer size too big");
1026   uint8_t SizeInBytes = Ty->getSizeInBits() / 8;
1027   MemberPointerInfo MPI(
1028       ClassTI, translatePtrToMemberRep(SizeInBytes, IsPMF, Ty->getFlags()));
1029   PointerRecord PR(PointeeTI, PK, PM, PO, SizeInBytes, MPI);
1030   return TypeTable.writePointer(PR);
1031 }
1032 
1033 /// Given a DWARF calling convention, get the CodeView equivalent. If we don't
1034 /// have a translation, use the NearC convention.
1035 static CallingConvention dwarfCCToCodeView(unsigned DwarfCC) {
1036   switch (DwarfCC) {
1037   case dwarf::DW_CC_normal:             return CallingConvention::NearC;
1038   case dwarf::DW_CC_BORLAND_msfastcall: return CallingConvention::NearFast;
1039   case dwarf::DW_CC_BORLAND_thiscall:   return CallingConvention::ThisCall;
1040   case dwarf::DW_CC_BORLAND_stdcall:    return CallingConvention::NearStdCall;
1041   case dwarf::DW_CC_BORLAND_pascal:     return CallingConvention::NearPascal;
1042   case dwarf::DW_CC_LLVM_vectorcall:    return CallingConvention::NearVector;
1043   }
1044   return CallingConvention::NearC;
1045 }
1046 
1047 TypeIndex CodeViewDebug::lowerTypeModifier(const DIDerivedType *Ty) {
1048   ModifierOptions Mods = ModifierOptions::None;
1049   bool IsModifier = true;
1050   const DIType *BaseTy = Ty;
1051   while (IsModifier && BaseTy) {
1052     // FIXME: Need to add DWARF tag for __unaligned.
1053     switch (BaseTy->getTag()) {
1054     case dwarf::DW_TAG_const_type:
1055       Mods |= ModifierOptions::Const;
1056       break;
1057     case dwarf::DW_TAG_volatile_type:
1058       Mods |= ModifierOptions::Volatile;
1059       break;
1060     default:
1061       IsModifier = false;
1062       break;
1063     }
1064     if (IsModifier)
1065       BaseTy = cast<DIDerivedType>(BaseTy)->getBaseType().resolve();
1066   }
1067   TypeIndex ModifiedTI = getTypeIndex(BaseTy);
1068   ModifierRecord MR(ModifiedTI, Mods);
1069   return TypeTable.writeModifier(MR);
1070 }
1071 
1072 TypeIndex CodeViewDebug::lowerTypeFunction(const DISubroutineType *Ty) {
1073   SmallVector<TypeIndex, 8> ReturnAndArgTypeIndices;
1074   for (DITypeRef ArgTypeRef : Ty->getTypeArray())
1075     ReturnAndArgTypeIndices.push_back(getTypeIndex(ArgTypeRef));
1076 
1077   TypeIndex ReturnTypeIndex = TypeIndex::Void();
1078   ArrayRef<TypeIndex> ArgTypeIndices = None;
1079   if (!ReturnAndArgTypeIndices.empty()) {
1080     auto ReturnAndArgTypesRef = makeArrayRef(ReturnAndArgTypeIndices);
1081     ReturnTypeIndex = ReturnAndArgTypesRef.front();
1082     ArgTypeIndices = ReturnAndArgTypesRef.drop_front();
1083   }
1084 
1085   ArgListRecord ArgListRec(TypeRecordKind::ArgList, ArgTypeIndices);
1086   TypeIndex ArgListIndex = TypeTable.writeArgList(ArgListRec);
1087 
1088   CallingConvention CC = dwarfCCToCodeView(Ty->getCC());
1089 
1090   // TODO: Some functions are member functions, we should use a more appropriate
1091   // record for those.
1092   ProcedureRecord Procedure(ReturnTypeIndex, CC, FunctionOptions::None,
1093                             ArgTypeIndices.size(), ArgListIndex);
1094   return TypeTable.writeProcedure(Procedure);
1095 }
1096 
1097 static MemberAccess translateAccessFlags(unsigned RecordTag,
1098                                          const DIType *Member) {
1099   switch (Member->getFlags() & DINode::FlagAccessibility) {
1100   case DINode::FlagPrivate:   return MemberAccess::Private;
1101   case DINode::FlagPublic:    return MemberAccess::Public;
1102   case DINode::FlagProtected: return MemberAccess::Protected;
1103   case 0:
1104     // If there was no explicit access control, provide the default for the tag.
1105     return RecordTag == dwarf::DW_TAG_class_type ? MemberAccess::Private
1106                                                  : MemberAccess::Public;
1107   }
1108   llvm_unreachable("access flags are exclusive");
1109 }
1110 
1111 static TypeRecordKind getRecordKind(const DICompositeType *Ty) {
1112   switch (Ty->getTag()) {
1113   case dwarf::DW_TAG_class_type:     return TypeRecordKind::Class;
1114   case dwarf::DW_TAG_structure_type: return TypeRecordKind::Struct;
1115   }
1116   llvm_unreachable("unexpected tag");
1117 }
1118 
1119 /// Return the HasUniqueName option if it should be present in ClassOptions, or
1120 /// None otherwise.
1121 static ClassOptions getRecordUniqueNameOption(const DICompositeType *Ty) {
1122   // MSVC always sets this flag now, even for local types. Clang doesn't always
1123   // appear to give every type a linkage name, which may be problematic for us.
1124   // FIXME: Investigate the consequences of not following them here.
1125   return !Ty->getIdentifier().empty() ? ClassOptions::HasUniqueName
1126                                       : ClassOptions::None;
1127 }
1128 
1129 TypeIndex CodeViewDebug::lowerTypeEnum(const DICompositeType *Ty) {
1130   ClassOptions CO = ClassOptions::None | getRecordUniqueNameOption(Ty);
1131   TypeIndex FTI;
1132   unsigned EnumeratorCount = 0;
1133 
1134   if (Ty->isForwardDecl()) {
1135     CO |= ClassOptions::ForwardReference;
1136   } else {
1137     FieldListRecordBuilder Fields;
1138     for (const DINode *Element : Ty->getElements()) {
1139       // We assume that the frontend provides all members in source declaration
1140       // order, which is what MSVC does.
1141       if (auto *Enumerator = dyn_cast_or_null<DIEnumerator>(Element)) {
1142         Fields.writeEnumerator(EnumeratorRecord(
1143             MemberAccess::Public, APSInt::getUnsigned(Enumerator->getValue()),
1144             Enumerator->getName()));
1145         EnumeratorCount++;
1146       }
1147     }
1148     FTI = TypeTable.writeFieldList(Fields);
1149   }
1150 
1151   return TypeTable.writeEnum(EnumRecord(EnumeratorCount, CO, FTI, Ty->getName(),
1152                                         Ty->getIdentifier(),
1153                                         getTypeIndex(Ty->getBaseType())));
1154 }
1155 
1156 TypeIndex CodeViewDebug::lowerTypeClass(const DICompositeType *Ty) {
1157   // First, construct the forward decl.  Don't look into Ty to compute the
1158   // forward decl options, since it might not be available in all TUs.
1159   TypeRecordKind Kind = getRecordKind(Ty);
1160   ClassOptions CO =
1161       ClassOptions::ForwardReference | getRecordUniqueNameOption(Ty);
1162   TypeIndex FwdDeclTI = TypeTable.writeClass(ClassRecord(
1163       Kind, 0, CO, HfaKind::None, WindowsRTClassKind::None, TypeIndex(),
1164       TypeIndex(), TypeIndex(), 0, Ty->getName(), Ty->getIdentifier()));
1165   return FwdDeclTI;
1166 }
1167 
1168 TypeIndex CodeViewDebug::lowerCompleteTypeClass(const DICompositeType *Ty) {
1169   // Construct the field list and complete type record.
1170   TypeRecordKind Kind = getRecordKind(Ty);
1171   // FIXME: Other ClassOptions, like ContainsNestedClass and NestedClass.
1172   ClassOptions CO = ClassOptions::None | getRecordUniqueNameOption(Ty);
1173   TypeIndex FTI;
1174   unsigned FieldCount;
1175   std::tie(FTI, FieldCount) = lowerRecordFieldList(Ty);
1176 
1177   uint64_t SizeInBytes = Ty->getSizeInBits() / 8;
1178   return TypeTable.writeClass(ClassRecord(Kind, FieldCount, CO, HfaKind::None,
1179                                           WindowsRTClassKind::None, FTI,
1180                                           TypeIndex(), TypeIndex(), SizeInBytes,
1181                                           Ty->getName(), Ty->getIdentifier()));
1182   // FIXME: Make an LF_UDT_SRC_LINE record.
1183 }
1184 
1185 TypeIndex CodeViewDebug::lowerTypeUnion(const DICompositeType *Ty) {
1186   ClassOptions CO =
1187       ClassOptions::ForwardReference | getRecordUniqueNameOption(Ty);
1188   TypeIndex FwdDeclTI =
1189       TypeTable.writeUnion(UnionRecord(0, CO, HfaKind::None, TypeIndex(), 0,
1190                                        Ty->getName(), Ty->getIdentifier()));
1191   return FwdDeclTI;
1192 }
1193 
1194 TypeIndex CodeViewDebug::lowerCompleteTypeUnion(const DICompositeType *Ty) {
1195   ClassOptions CO = ClassOptions::None | getRecordUniqueNameOption(Ty);
1196   TypeIndex FTI;
1197   unsigned FieldCount;
1198   std::tie(FTI, FieldCount) = lowerRecordFieldList(Ty);
1199   uint64_t SizeInBytes = Ty->getSizeInBits() / 8;
1200   return TypeTable.writeUnion(UnionRecord(FieldCount, CO, HfaKind::None, FTI,
1201                                           SizeInBytes, Ty->getName(),
1202                                           Ty->getIdentifier()));
1203   // FIXME: Make an LF_UDT_SRC_LINE record.
1204 }
1205 
1206 std::pair<TypeIndex, unsigned>
1207 CodeViewDebug::lowerRecordFieldList(const DICompositeType *Ty) {
1208   // Manually count members. MSVC appears to count everything that generates a
1209   // field list record. Each individual overload in a method overload group
1210   // contributes to this count, even though the overload group is a single field
1211   // list record.
1212   unsigned MemberCount = 0;
1213   FieldListRecordBuilder Fields;
1214   for (const DINode *Element : Ty->getElements()) {
1215     // We assume that the frontend provides all members in source declaration
1216     // order, which is what MSVC does.
1217     if (!Element)
1218       continue;
1219     if (auto *SP = dyn_cast<DISubprogram>(Element)) {
1220       // C++ method.
1221       // FIXME: Overloaded methods are grouped together, so we'll need two
1222       // passes to group them.
1223       (void)SP;
1224     } else if (auto *Member = dyn_cast<DIDerivedType>(Element)) {
1225       if (Member->getTag() == dwarf::DW_TAG_member) {
1226         if (Member->isStaticMember()) {
1227           // Static data member.
1228           Fields.writeStaticDataMember(StaticDataMemberRecord(
1229               translateAccessFlags(Ty->getTag(), Member),
1230               getTypeIndex(Member->getBaseType()), Member->getName()));
1231           MemberCount++;
1232         } else {
1233           // Data member.
1234           // FIXME: Make a BitFieldRecord for bitfields.
1235           Fields.writeDataMember(DataMemberRecord(
1236               translateAccessFlags(Ty->getTag(), Member),
1237               getTypeIndex(Member->getBaseType()),
1238               Member->getOffsetInBits() / 8, Member->getName()));
1239           MemberCount++;
1240         }
1241       } else if (Member->getTag() == dwarf::DW_TAG_friend) {
1242         // Ignore friend members. It appears that MSVC emitted info about
1243         // friends in the past, but modern versions do not.
1244       }
1245       // FIXME: Get clang to emit nested types here and do something with
1246       // them.
1247     }
1248     // Skip other unrecognized kinds of elements.
1249   }
1250   return {TypeTable.writeFieldList(Fields), MemberCount};
1251 }
1252 
1253 TypeIndex CodeViewDebug::getTypeIndex(DITypeRef TypeRef) {
1254   const DIType *Ty = TypeRef.resolve();
1255 
1256   // The null DIType is the void type. Don't try to hash it.
1257   if (!Ty)
1258     return TypeIndex::Void();
1259 
1260   // Check if we've already translated this type. Don't try to do a
1261   // get-or-create style insertion that caches the hash lookup across the
1262   // lowerType call. It will update the TypeIndices map.
1263   auto I = TypeIndices.find(Ty);
1264   if (I != TypeIndices.end())
1265     return I->second;
1266 
1267   TypeIndex TI = lowerType(Ty);
1268 
1269   recordTypeIndexForDINode(Ty, TI);
1270   return TI;
1271 }
1272 
1273 TypeIndex CodeViewDebug::getCompleteTypeIndex(DITypeRef TypeRef) {
1274   const DIType *Ty = TypeRef.resolve();
1275 
1276   // The null DIType is the void type. Don't try to hash it.
1277   if (!Ty)
1278     return TypeIndex::Void();
1279 
1280   // If this is a non-record type, the complete type index is the same as the
1281   // normal type index. Just call getTypeIndex.
1282   switch (Ty->getTag()) {
1283   case dwarf::DW_TAG_class_type:
1284   case dwarf::DW_TAG_structure_type:
1285   case dwarf::DW_TAG_union_type:
1286     break;
1287   default:
1288     return getTypeIndex(Ty);
1289   }
1290 
1291   // Check if we've already translated the complete record type.  Lowering a
1292   // complete type should never trigger lowering another complete type, so we
1293   // can reuse the hash table lookup result.
1294   const auto *CTy = cast<DICompositeType>(Ty);
1295   auto InsertResult = CompleteTypeIndices.insert({CTy, TypeIndex()});
1296   if (!InsertResult.second)
1297     return InsertResult.first->second;
1298 
1299   // Make sure the forward declaration is emitted first. It's unclear if this
1300   // is necessary, but MSVC does it, and we should follow suit until we can show
1301   // otherwise.
1302   TypeIndex FwdDeclTI = getTypeIndex(CTy);
1303 
1304   // Just use the forward decl if we don't have complete type info. This might
1305   // happen if the frontend is using modules and expects the complete definition
1306   // to be emitted elsewhere.
1307   if (CTy->isForwardDecl())
1308     return FwdDeclTI;
1309 
1310   TypeIndex TI;
1311   switch (CTy->getTag()) {
1312   case dwarf::DW_TAG_class_type:
1313   case dwarf::DW_TAG_structure_type:
1314     TI = lowerCompleteTypeClass(CTy);
1315     break;
1316   case dwarf::DW_TAG_union_type:
1317     TI = lowerCompleteTypeUnion(CTy);
1318     break;
1319   default:
1320     llvm_unreachable("not a record");
1321   }
1322 
1323   InsertResult.first->second = TI;
1324   return TI;
1325 }
1326 
1327 void CodeViewDebug::emitLocalVariable(const LocalVariable &Var) {
1328   // LocalSym record, see SymbolRecord.h for more info.
1329   MCSymbol *LocalBegin = MMI->getContext().createTempSymbol(),
1330            *LocalEnd = MMI->getContext().createTempSymbol();
1331   OS.AddComment("Record length");
1332   OS.emitAbsoluteSymbolDiff(LocalEnd, LocalBegin, 2);
1333   OS.EmitLabel(LocalBegin);
1334 
1335   OS.AddComment("Record kind: S_LOCAL");
1336   OS.EmitIntValue(unsigned(SymbolKind::S_LOCAL), 2);
1337 
1338   LocalSymFlags Flags = LocalSymFlags::None;
1339   if (Var.DIVar->isParameter())
1340     Flags |= LocalSymFlags::IsParameter;
1341   if (Var.DefRanges.empty())
1342     Flags |= LocalSymFlags::IsOptimizedOut;
1343 
1344   OS.AddComment("TypeIndex");
1345   TypeIndex TI = getCompleteTypeIndex(Var.DIVar->getType());
1346   OS.EmitIntValue(TI.getIndex(), 4);
1347   OS.AddComment("Flags");
1348   OS.EmitIntValue(static_cast<uint16_t>(Flags), 2);
1349   // Truncate the name so we won't overflow the record length field.
1350   emitNullTerminatedSymbolName(OS, Var.DIVar->getName());
1351   OS.EmitLabel(LocalEnd);
1352 
1353   // Calculate the on disk prefix of the appropriate def range record. The
1354   // records and on disk formats are described in SymbolRecords.h. BytePrefix
1355   // should be big enough to hold all forms without memory allocation.
1356   SmallString<20> BytePrefix;
1357   for (const LocalVarDefRange &DefRange : Var.DefRanges) {
1358     BytePrefix.clear();
1359     // FIXME: Handle bitpieces.
1360     if (DefRange.StructOffset != 0)
1361       continue;
1362 
1363     if (DefRange.InMemory) {
1364       DefRangeRegisterRelSym Sym(DefRange.CVRegister, 0, DefRange.DataOffset, 0,
1365                                  0, 0, ArrayRef<LocalVariableAddrGap>());
1366       ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER_REL);
1367       BytePrefix +=
1368           StringRef(reinterpret_cast<const char *>(&SymKind), sizeof(SymKind));
1369       BytePrefix +=
1370           StringRef(reinterpret_cast<const char *>(&Sym.Header),
1371                     sizeof(Sym.Header) - sizeof(LocalVariableAddrRange));
1372     } else {
1373       assert(DefRange.DataOffset == 0 && "unexpected offset into register");
1374       // Unclear what matters here.
1375       DefRangeRegisterSym Sym(DefRange.CVRegister, 0, 0, 0, 0,
1376                               ArrayRef<LocalVariableAddrGap>());
1377       ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER);
1378       BytePrefix +=
1379           StringRef(reinterpret_cast<const char *>(&SymKind), sizeof(SymKind));
1380       BytePrefix +=
1381           StringRef(reinterpret_cast<const char *>(&Sym.Header),
1382                     sizeof(Sym.Header) - sizeof(LocalVariableAddrRange));
1383     }
1384     OS.EmitCVDefRangeDirective(DefRange.Ranges, BytePrefix);
1385   }
1386 }
1387 
1388 void CodeViewDebug::endFunction(const MachineFunction *MF) {
1389   if (!Asm || !CurFn)  // We haven't created any debug info for this function.
1390     return;
1391 
1392   const Function *GV = MF->getFunction();
1393   assert(FnDebugInfo.count(GV));
1394   assert(CurFn == &FnDebugInfo[GV]);
1395 
1396   collectVariableInfo(GV->getSubprogram());
1397 
1398   DebugHandlerBase::endFunction(MF);
1399 
1400   // Don't emit anything if we don't have any line tables.
1401   if (!CurFn->HaveLineInfo) {
1402     FnDebugInfo.erase(GV);
1403     CurFn = nullptr;
1404     return;
1405   }
1406 
1407   CurFn->End = Asm->getFunctionEnd();
1408 
1409   CurFn = nullptr;
1410 }
1411 
1412 void CodeViewDebug::beginInstruction(const MachineInstr *MI) {
1413   DebugHandlerBase::beginInstruction(MI);
1414 
1415   // Ignore DBG_VALUE locations and function prologue.
1416   if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup))
1417     return;
1418   DebugLoc DL = MI->getDebugLoc();
1419   if (DL == PrevInstLoc || !DL)
1420     return;
1421   maybeRecordLocation(DL, Asm->MF);
1422 }
1423 
1424 MCSymbol *CodeViewDebug::beginCVSubsection(ModuleSubstreamKind Kind) {
1425   MCSymbol *BeginLabel = MMI->getContext().createTempSymbol(),
1426            *EndLabel = MMI->getContext().createTempSymbol();
1427   OS.EmitIntValue(unsigned(Kind), 4);
1428   OS.AddComment("Subsection size");
1429   OS.emitAbsoluteSymbolDiff(EndLabel, BeginLabel, 4);
1430   OS.EmitLabel(BeginLabel);
1431   return EndLabel;
1432 }
1433 
1434 void CodeViewDebug::endCVSubsection(MCSymbol *EndLabel) {
1435   OS.EmitLabel(EndLabel);
1436   // Every subsection must be aligned to a 4-byte boundary.
1437   OS.EmitValueToAlignment(4);
1438 }
1439 
1440 void CodeViewDebug::emitDebugInfoForUDTs(
1441     ArrayRef<std::pair<std::string, TypeIndex>> UDTs) {
1442   for (const std::pair<std::string, codeview::TypeIndex> &UDT : UDTs) {
1443     MCSymbol *UDTRecordBegin = MMI->getContext().createTempSymbol(),
1444              *UDTRecordEnd = MMI->getContext().createTempSymbol();
1445     OS.AddComment("Record length");
1446     OS.emitAbsoluteSymbolDiff(UDTRecordEnd, UDTRecordBegin, 2);
1447     OS.EmitLabel(UDTRecordBegin);
1448 
1449     OS.AddComment("Record kind: S_UDT");
1450     OS.EmitIntValue(unsigned(SymbolKind::S_UDT), 2);
1451 
1452     OS.AddComment("Type");
1453     OS.EmitIntValue(UDT.second.getIndex(), 4);
1454 
1455     emitNullTerminatedSymbolName(OS, UDT.first);
1456     OS.EmitLabel(UDTRecordEnd);
1457   }
1458 }
1459 
1460 void CodeViewDebug::emitDebugInfoForGlobals() {
1461   NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
1462   for (const MDNode *Node : CUs->operands()) {
1463     const auto *CU = cast<DICompileUnit>(Node);
1464 
1465     // First, emit all globals that are not in a comdat in a single symbol
1466     // substream. MSVC doesn't like it if the substream is empty, so only open
1467     // it if we have at least one global to emit.
1468     switchToDebugSectionForSymbol(nullptr);
1469     MCSymbol *EndLabel = nullptr;
1470     for (const DIGlobalVariable *G : CU->getGlobalVariables()) {
1471       if (const auto *GV = dyn_cast_or_null<GlobalVariable>(G->getVariable())) {
1472         if (!GV->hasComdat() && !GV->isDeclarationForLinker()) {
1473           if (!EndLabel) {
1474             OS.AddComment("Symbol subsection for globals");
1475             EndLabel = beginCVSubsection(ModuleSubstreamKind::Symbols);
1476           }
1477           emitDebugInfoForGlobal(G, Asm->getSymbol(GV));
1478         }
1479       }
1480     }
1481     if (EndLabel)
1482       endCVSubsection(EndLabel);
1483 
1484     // Second, emit each global that is in a comdat into its own .debug$S
1485     // section along with its own symbol substream.
1486     for (const DIGlobalVariable *G : CU->getGlobalVariables()) {
1487       if (const auto *GV = dyn_cast_or_null<GlobalVariable>(G->getVariable())) {
1488         if (GV->hasComdat()) {
1489           MCSymbol *GVSym = Asm->getSymbol(GV);
1490           OS.AddComment("Symbol subsection for " +
1491                         Twine(GlobalValue::getRealLinkageName(GV->getName())));
1492           switchToDebugSectionForSymbol(GVSym);
1493           EndLabel = beginCVSubsection(ModuleSubstreamKind::Symbols);
1494           emitDebugInfoForGlobal(G, GVSym);
1495           endCVSubsection(EndLabel);
1496         }
1497       }
1498     }
1499   }
1500 }
1501 
1502 void CodeViewDebug::emitDebugInfoForGlobal(const DIGlobalVariable *DIGV,
1503                                            MCSymbol *GVSym) {
1504   // DataSym record, see SymbolRecord.h for more info.
1505   // FIXME: Thread local data, etc
1506   MCSymbol *DataBegin = MMI->getContext().createTempSymbol(),
1507            *DataEnd = MMI->getContext().createTempSymbol();
1508   OS.AddComment("Record length");
1509   OS.emitAbsoluteSymbolDiff(DataEnd, DataBegin, 2);
1510   OS.EmitLabel(DataBegin);
1511   OS.AddComment("Record kind: S_GDATA32");
1512   OS.EmitIntValue(unsigned(SymbolKind::S_GDATA32), 2);
1513   OS.AddComment("Type");
1514   OS.EmitIntValue(getCompleteTypeIndex(DIGV->getType()).getIndex(), 4);
1515   OS.AddComment("DataOffset");
1516   OS.EmitCOFFSecRel32(GVSym);
1517   OS.AddComment("Segment");
1518   OS.EmitCOFFSectionIndex(GVSym);
1519   OS.AddComment("Name");
1520   emitNullTerminatedSymbolName(OS, DIGV->getName());
1521   OS.EmitLabel(DataEnd);
1522 }
1523