xref: /llvm-project/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp (revision 6900de1dfb2aa33e97ebed98628880ffb200a648)
1 //===- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp ----------------------===//
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 "DwarfExpression.h"
16 #include "llvm/ADT/APSInt.h"
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/DenseSet.h"
20 #include "llvm/ADT/MapVector.h"
21 #include "llvm/ADT/None.h"
22 #include "llvm/ADT/Optional.h"
23 #include "llvm/ADT/STLExtras.h"
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/ADT/StringRef.h"
27 #include "llvm/ADT/TinyPtrVector.h"
28 #include "llvm/ADT/Triple.h"
29 #include "llvm/ADT/Twine.h"
30 #include "llvm/BinaryFormat/COFF.h"
31 #include "llvm/BinaryFormat/Dwarf.h"
32 #include "llvm/CodeGen/AsmPrinter.h"
33 #include "llvm/CodeGen/LexicalScopes.h"
34 #include "llvm/CodeGen/MachineFunction.h"
35 #include "llvm/CodeGen/MachineInstr.h"
36 #include "llvm/CodeGen/MachineModuleInfo.h"
37 #include "llvm/CodeGen/MachineOperand.h"
38 #include "llvm/CodeGen/TargetFrameLowering.h"
39 #include "llvm/CodeGen/TargetLoweringObjectFile.h"
40 #include "llvm/CodeGen/TargetRegisterInfo.h"
41 #include "llvm/CodeGen/TargetSubtargetInfo.h"
42 #include "llvm/Config/llvm-config.h"
43 #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
44 #include "llvm/DebugInfo/CodeView/CodeView.h"
45 #include "llvm/DebugInfo/CodeView/ContinuationRecordBuilder.h"
46 #include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h"
47 #include "llvm/DebugInfo/CodeView/Line.h"
48 #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
49 #include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h"
50 #include "llvm/DebugInfo/CodeView/TypeIndex.h"
51 #include "llvm/DebugInfo/CodeView/TypeRecord.h"
52 #include "llvm/DebugInfo/CodeView/TypeTableCollection.h"
53 #include "llvm/IR/Constants.h"
54 #include "llvm/IR/DataLayout.h"
55 #include "llvm/IR/DebugInfoMetadata.h"
56 #include "llvm/IR/DebugLoc.h"
57 #include "llvm/IR/Function.h"
58 #include "llvm/IR/GlobalValue.h"
59 #include "llvm/IR/GlobalVariable.h"
60 #include "llvm/IR/Metadata.h"
61 #include "llvm/IR/Module.h"
62 #include "llvm/MC/MCAsmInfo.h"
63 #include "llvm/MC/MCContext.h"
64 #include "llvm/MC/MCSectionCOFF.h"
65 #include "llvm/MC/MCStreamer.h"
66 #include "llvm/MC/MCSymbol.h"
67 #include "llvm/Support/BinaryByteStream.h"
68 #include "llvm/Support/BinaryStreamReader.h"
69 #include "llvm/Support/Casting.h"
70 #include "llvm/Support/Compiler.h"
71 #include "llvm/Support/Endian.h"
72 #include "llvm/Support/Error.h"
73 #include "llvm/Support/ErrorHandling.h"
74 #include "llvm/Support/SMLoc.h"
75 #include "llvm/Support/ScopedPrinter.h"
76 #include "llvm/Target/TargetMachine.h"
77 #include <algorithm>
78 #include <cassert>
79 #include <cctype>
80 #include <cstddef>
81 #include <cstdint>
82 #include <iterator>
83 #include <limits>
84 #include <string>
85 #include <utility>
86 #include <vector>
87 
88 using namespace llvm;
89 using namespace llvm::codeview;
90 
91 CodeViewDebug::CodeViewDebug(AsmPrinter *AP)
92     : DebugHandlerBase(AP), OS(*Asm->OutStreamer), TypeTable(Allocator) {
93   // If module doesn't have named metadata anchors or COFF debug section
94   // is not available, skip any debug info related stuff.
95   if (!MMI->getModule()->getNamedMetadata("llvm.dbg.cu") ||
96       !AP->getObjFileLowering().getCOFFDebugSymbolsSection()) {
97     Asm = nullptr;
98     return;
99   }
100 
101   // Tell MMI that we have debug info.
102   MMI->setDebugInfoAvailability(true);
103 }
104 
105 StringRef CodeViewDebug::getFullFilepath(const DIFile *File) {
106   std::string &Filepath = FileToFilepathMap[File];
107   if (!Filepath.empty())
108     return Filepath;
109 
110   StringRef Dir = File->getDirectory(), Filename = File->getFilename();
111 
112   // Clang emits directory and relative filename info into the IR, but CodeView
113   // operates on full paths.  We could change Clang to emit full paths too, but
114   // that would increase the IR size and probably not needed for other users.
115   // For now, just concatenate and canonicalize the path here.
116   if (Filename.find(':') == 1)
117     Filepath = Filename;
118   else
119     Filepath = (Dir + "\\" + Filename).str();
120 
121   // Canonicalize the path.  We have to do it textually because we may no longer
122   // have access the file in the filesystem.
123   // First, replace all slashes with backslashes.
124   std::replace(Filepath.begin(), Filepath.end(), '/', '\\');
125 
126   // Remove all "\.\" with "\".
127   size_t Cursor = 0;
128   while ((Cursor = Filepath.find("\\.\\", Cursor)) != std::string::npos)
129     Filepath.erase(Cursor, 2);
130 
131   // Replace all "\XXX\..\" with "\".  Don't try too hard though as the original
132   // path should be well-formatted, e.g. start with a drive letter, etc.
133   Cursor = 0;
134   while ((Cursor = Filepath.find("\\..\\", Cursor)) != std::string::npos) {
135     // Something's wrong if the path starts with "\..\", abort.
136     if (Cursor == 0)
137       break;
138 
139     size_t PrevSlash = Filepath.rfind('\\', Cursor - 1);
140     if (PrevSlash == std::string::npos)
141       // Something's wrong, abort.
142       break;
143 
144     Filepath.erase(PrevSlash, Cursor + 3 - PrevSlash);
145     // The next ".." might be following the one we've just erased.
146     Cursor = PrevSlash;
147   }
148 
149   // Remove all duplicate backslashes.
150   Cursor = 0;
151   while ((Cursor = Filepath.find("\\\\", Cursor)) != std::string::npos)
152     Filepath.erase(Cursor, 1);
153 
154   return Filepath;
155 }
156 
157 unsigned CodeViewDebug::maybeRecordFile(const DIFile *F) {
158   StringRef FullPath = getFullFilepath(F);
159   unsigned NextId = FileIdMap.size() + 1;
160   auto Insertion = FileIdMap.insert(std::make_pair(FullPath, NextId));
161   if (Insertion.second) {
162     // We have to compute the full filepath and emit a .cv_file directive.
163     std::string Checksum = fromHex(F->getChecksum());
164     void *CKMem = OS.getContext().allocate(Checksum.size(), 1);
165     memcpy(CKMem, Checksum.data(), Checksum.size());
166     ArrayRef<uint8_t> ChecksumAsBytes(reinterpret_cast<const uint8_t *>(CKMem),
167                                       Checksum.size());
168     DIFile::ChecksumKind ChecksumKind = F->getChecksumKind();
169     bool Success = OS.EmitCVFileDirective(NextId, FullPath, ChecksumAsBytes,
170                                           static_cast<unsigned>(ChecksumKind));
171     (void)Success;
172     assert(Success && ".cv_file directive failed");
173   }
174   return Insertion.first->second;
175 }
176 
177 CodeViewDebug::InlineSite &
178 CodeViewDebug::getInlineSite(const DILocation *InlinedAt,
179                              const DISubprogram *Inlinee) {
180   auto SiteInsertion = CurFn->InlineSites.insert({InlinedAt, InlineSite()});
181   InlineSite *Site = &SiteInsertion.first->second;
182   if (SiteInsertion.second) {
183     unsigned ParentFuncId = CurFn->FuncId;
184     if (const DILocation *OuterIA = InlinedAt->getInlinedAt())
185       ParentFuncId =
186           getInlineSite(OuterIA, InlinedAt->getScope()->getSubprogram())
187               .SiteFuncId;
188 
189     Site->SiteFuncId = NextFuncId++;
190     OS.EmitCVInlineSiteIdDirective(
191         Site->SiteFuncId, ParentFuncId, maybeRecordFile(InlinedAt->getFile()),
192         InlinedAt->getLine(), InlinedAt->getColumn(), SMLoc());
193     Site->Inlinee = Inlinee;
194     InlinedSubprograms.insert(Inlinee);
195     getFuncIdForSubprogram(Inlinee);
196   }
197   return *Site;
198 }
199 
200 static StringRef getPrettyScopeName(const DIScope *Scope) {
201   StringRef ScopeName = Scope->getName();
202   if (!ScopeName.empty())
203     return ScopeName;
204 
205   switch (Scope->getTag()) {
206   case dwarf::DW_TAG_enumeration_type:
207   case dwarf::DW_TAG_class_type:
208   case dwarf::DW_TAG_structure_type:
209   case dwarf::DW_TAG_union_type:
210     return "<unnamed-tag>";
211   case dwarf::DW_TAG_namespace:
212     return "`anonymous namespace'";
213   }
214 
215   return StringRef();
216 }
217 
218 static const DISubprogram *getQualifiedNameComponents(
219     const DIScope *Scope, SmallVectorImpl<StringRef> &QualifiedNameComponents) {
220   const DISubprogram *ClosestSubprogram = nullptr;
221   while (Scope != nullptr) {
222     if (ClosestSubprogram == nullptr)
223       ClosestSubprogram = dyn_cast<DISubprogram>(Scope);
224     StringRef ScopeName = getPrettyScopeName(Scope);
225     if (!ScopeName.empty())
226       QualifiedNameComponents.push_back(ScopeName);
227     Scope = Scope->getScope().resolve();
228   }
229   return ClosestSubprogram;
230 }
231 
232 static std::string getQualifiedName(ArrayRef<StringRef> QualifiedNameComponents,
233                                     StringRef TypeName) {
234   std::string FullyQualifiedName;
235   for (StringRef QualifiedNameComponent :
236        llvm::reverse(QualifiedNameComponents)) {
237     FullyQualifiedName.append(QualifiedNameComponent);
238     FullyQualifiedName.append("::");
239   }
240   FullyQualifiedName.append(TypeName);
241   return FullyQualifiedName;
242 }
243 
244 static std::string getFullyQualifiedName(const DIScope *Scope, StringRef Name) {
245   SmallVector<StringRef, 5> QualifiedNameComponents;
246   getQualifiedNameComponents(Scope, QualifiedNameComponents);
247   return getQualifiedName(QualifiedNameComponents, Name);
248 }
249 
250 struct CodeViewDebug::TypeLoweringScope {
251   TypeLoweringScope(CodeViewDebug &CVD) : CVD(CVD) { ++CVD.TypeEmissionLevel; }
252   ~TypeLoweringScope() {
253     // Don't decrement TypeEmissionLevel until after emitting deferred types, so
254     // inner TypeLoweringScopes don't attempt to emit deferred types.
255     if (CVD.TypeEmissionLevel == 1)
256       CVD.emitDeferredCompleteTypes();
257     --CVD.TypeEmissionLevel;
258   }
259   CodeViewDebug &CVD;
260 };
261 
262 static std::string getFullyQualifiedName(const DIScope *Ty) {
263   const DIScope *Scope = Ty->getScope().resolve();
264   return getFullyQualifiedName(Scope, getPrettyScopeName(Ty));
265 }
266 
267 TypeIndex CodeViewDebug::getScopeIndex(const DIScope *Scope) {
268   // No scope means global scope and that uses the zero index.
269   if (!Scope || isa<DIFile>(Scope))
270     return TypeIndex();
271 
272   assert(!isa<DIType>(Scope) && "shouldn't make a namespace scope for a type");
273 
274   // Check if we've already translated this scope.
275   auto I = TypeIndices.find({Scope, nullptr});
276   if (I != TypeIndices.end())
277     return I->second;
278 
279   // Build the fully qualified name of the scope.
280   std::string ScopeName = getFullyQualifiedName(Scope);
281   StringIdRecord SID(TypeIndex(), ScopeName);
282   auto TI = TypeTable.writeLeafType(SID);
283   return recordTypeIndexForDINode(Scope, TI);
284 }
285 
286 TypeIndex CodeViewDebug::getFuncIdForSubprogram(const DISubprogram *SP) {
287   assert(SP);
288 
289   // Check if we've already translated this subprogram.
290   auto I = TypeIndices.find({SP, nullptr});
291   if (I != TypeIndices.end())
292     return I->second;
293 
294   // The display name includes function template arguments. Drop them to match
295   // MSVC.
296   StringRef DisplayName = SP->getName().split('<').first;
297 
298   const DIScope *Scope = SP->getScope().resolve();
299   TypeIndex TI;
300   if (const auto *Class = dyn_cast_or_null<DICompositeType>(Scope)) {
301     // If the scope is a DICompositeType, then this must be a method. Member
302     // function types take some special handling, and require access to the
303     // subprogram.
304     TypeIndex ClassType = getTypeIndex(Class);
305     MemberFuncIdRecord MFuncId(ClassType, getMemberFunctionType(SP, Class),
306                                DisplayName);
307     TI = TypeTable.writeLeafType(MFuncId);
308   } else {
309     // Otherwise, this must be a free function.
310     TypeIndex ParentScope = getScopeIndex(Scope);
311     FuncIdRecord FuncId(ParentScope, getTypeIndex(SP->getType()), DisplayName);
312     TI = TypeTable.writeLeafType(FuncId);
313   }
314 
315   return recordTypeIndexForDINode(SP, TI);
316 }
317 
318 TypeIndex CodeViewDebug::getMemberFunctionType(const DISubprogram *SP,
319                                                const DICompositeType *Class) {
320   // Always use the method declaration as the key for the function type. The
321   // method declaration contains the this adjustment.
322   if (SP->getDeclaration())
323     SP = SP->getDeclaration();
324   assert(!SP->getDeclaration() && "should use declaration as key");
325 
326   // Key the MemberFunctionRecord into the map as {SP, Class}. It won't collide
327   // with the MemberFuncIdRecord, which is keyed in as {SP, nullptr}.
328   auto I = TypeIndices.find({SP, Class});
329   if (I != TypeIndices.end())
330     return I->second;
331 
332   // Make sure complete type info for the class is emitted *after* the member
333   // function type, as the complete class type is likely to reference this
334   // member function type.
335   TypeLoweringScope S(*this);
336   const bool IsStaticMethod = (SP->getFlags() & DINode::FlagStaticMember) != 0;
337   TypeIndex TI = lowerTypeMemberFunction(
338       SP->getType(), Class, SP->getThisAdjustment(), IsStaticMethod);
339   return recordTypeIndexForDINode(SP, TI, Class);
340 }
341 
342 TypeIndex CodeViewDebug::recordTypeIndexForDINode(const DINode *Node,
343                                                   TypeIndex TI,
344                                                   const DIType *ClassTy) {
345   auto InsertResult = TypeIndices.insert({{Node, ClassTy}, TI});
346   (void)InsertResult;
347   assert(InsertResult.second && "DINode was already assigned a type index");
348   return TI;
349 }
350 
351 unsigned CodeViewDebug::getPointerSizeInBytes() {
352   return MMI->getModule()->getDataLayout().getPointerSizeInBits() / 8;
353 }
354 
355 void CodeViewDebug::recordLocalVariable(LocalVariable &&Var,
356                                         const DILocation *InlinedAt) {
357   if (InlinedAt) {
358     // This variable was inlined. Associate it with the InlineSite.
359     const DISubprogram *Inlinee = Var.DIVar->getScope()->getSubprogram();
360     InlineSite &Site = getInlineSite(InlinedAt, Inlinee);
361     Site.InlinedLocals.emplace_back(Var);
362   } else {
363     // This variable goes in the main ProcSym.
364     CurFn->Locals.emplace_back(Var);
365   }
366 }
367 
368 static void addLocIfNotPresent(SmallVectorImpl<const DILocation *> &Locs,
369                                const DILocation *Loc) {
370   auto B = Locs.begin(), E = Locs.end();
371   if (std::find(B, E, Loc) == E)
372     Locs.push_back(Loc);
373 }
374 
375 void CodeViewDebug::maybeRecordLocation(const DebugLoc &DL,
376                                         const MachineFunction *MF) {
377   // Skip this instruction if it has the same location as the previous one.
378   if (!DL || DL == PrevInstLoc)
379     return;
380 
381   const DIScope *Scope = DL.get()->getScope();
382   if (!Scope)
383     return;
384 
385   // Skip this line if it is longer than the maximum we can record.
386   LineInfo LI(DL.getLine(), DL.getLine(), /*IsStatement=*/true);
387   if (LI.getStartLine() != DL.getLine() || LI.isAlwaysStepInto() ||
388       LI.isNeverStepInto())
389     return;
390 
391   ColumnInfo CI(DL.getCol(), /*EndColumn=*/0);
392   if (CI.getStartColumn() != DL.getCol())
393     return;
394 
395   if (!CurFn->HaveLineInfo)
396     CurFn->HaveLineInfo = true;
397   unsigned FileId = 0;
398   if (PrevInstLoc.get() && PrevInstLoc->getFile() == DL->getFile())
399     FileId = CurFn->LastFileId;
400   else
401     FileId = CurFn->LastFileId = maybeRecordFile(DL->getFile());
402   PrevInstLoc = DL;
403 
404   unsigned FuncId = CurFn->FuncId;
405   if (const DILocation *SiteLoc = DL->getInlinedAt()) {
406     const DILocation *Loc = DL.get();
407 
408     // If this location was actually inlined from somewhere else, give it the ID
409     // of the inline call site.
410     FuncId =
411         getInlineSite(SiteLoc, Loc->getScope()->getSubprogram()).SiteFuncId;
412 
413     // Ensure we have links in the tree of inline call sites.
414     bool FirstLoc = true;
415     while ((SiteLoc = Loc->getInlinedAt())) {
416       InlineSite &Site =
417           getInlineSite(SiteLoc, Loc->getScope()->getSubprogram());
418       if (!FirstLoc)
419         addLocIfNotPresent(Site.ChildSites, Loc);
420       FirstLoc = false;
421       Loc = SiteLoc;
422     }
423     addLocIfNotPresent(CurFn->ChildSites, Loc);
424   }
425 
426   OS.EmitCVLocDirective(FuncId, FileId, DL.getLine(), DL.getCol(),
427                         /*PrologueEnd=*/false, /*IsStmt=*/false,
428                         DL->getFilename(), SMLoc());
429 }
430 
431 void CodeViewDebug::emitCodeViewMagicVersion() {
432   OS.EmitValueToAlignment(4);
433   OS.AddComment("Debug section magic");
434   OS.EmitIntValue(COFF::DEBUG_SECTION_MAGIC, 4);
435 }
436 
437 void CodeViewDebug::endModule() {
438   if (!Asm || !MMI->hasDebugInfo())
439     return;
440 
441   assert(Asm != nullptr);
442 
443   // The COFF .debug$S section consists of several subsections, each starting
444   // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length
445   // of the payload followed by the payload itself.  The subsections are 4-byte
446   // aligned.
447 
448   // Use the generic .debug$S section, and make a subsection for all the inlined
449   // subprograms.
450   switchToDebugSectionForSymbol(nullptr);
451 
452   MCSymbol *CompilerInfo = beginCVSubsection(DebugSubsectionKind::Symbols);
453   emitCompilerInformation();
454   endCVSubsection(CompilerInfo);
455 
456   emitInlineeLinesSubsection();
457 
458   // Emit per-function debug information.
459   for (auto &P : FnDebugInfo)
460     if (!P.first->isDeclarationForLinker())
461       emitDebugInfoForFunction(P.first, P.second);
462 
463   // Emit global variable debug information.
464   setCurrentSubprogram(nullptr);
465   emitDebugInfoForGlobals();
466 
467   // Emit retained types.
468   emitDebugInfoForRetainedTypes();
469 
470   // Switch back to the generic .debug$S section after potentially processing
471   // comdat symbol sections.
472   switchToDebugSectionForSymbol(nullptr);
473 
474   // Emit UDT records for any types used by global variables.
475   if (!GlobalUDTs.empty()) {
476     MCSymbol *SymbolsEnd = beginCVSubsection(DebugSubsectionKind::Symbols);
477     emitDebugInfoForUDTs(GlobalUDTs);
478     endCVSubsection(SymbolsEnd);
479   }
480 
481   // This subsection holds a file index to offset in string table table.
482   OS.AddComment("File index to string table offset subsection");
483   OS.EmitCVFileChecksumsDirective();
484 
485   // This subsection holds the string table.
486   OS.AddComment("String table");
487   OS.EmitCVStringTableDirective();
488 
489   // Emit type information last, so that any types we translate while emitting
490   // function info are included.
491   emitTypeInformation();
492 
493   clear();
494 }
495 
496 static void emitNullTerminatedSymbolName(MCStreamer &OS, StringRef S) {
497   // The maximum CV record length is 0xFF00. Most of the strings we emit appear
498   // after a fixed length portion of the record. The fixed length portion should
499   // always be less than 0xF00 (3840) bytes, so truncate the string so that the
500   // overall record size is less than the maximum allowed.
501   unsigned MaxFixedRecordLength = 0xF00;
502   SmallString<32> NullTerminatedString(
503       S.take_front(MaxRecordLength - MaxFixedRecordLength - 1));
504   NullTerminatedString.push_back('\0');
505   OS.EmitBytes(NullTerminatedString);
506 }
507 
508 void CodeViewDebug::emitTypeInformation() {
509   // Do nothing if we have no debug info or if no non-trivial types were emitted
510   // to TypeTable during codegen.
511   NamedMDNode *CU_Nodes = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
512   if (!CU_Nodes)
513     return;
514   if (TypeTable.empty())
515     return;
516 
517   // Start the .debug$T section with 0x4.
518   OS.SwitchSection(Asm->getObjFileLowering().getCOFFDebugTypesSection());
519   emitCodeViewMagicVersion();
520 
521   SmallString<8> CommentPrefix;
522   if (OS.isVerboseAsm()) {
523     CommentPrefix += '\t';
524     CommentPrefix += Asm->MAI->getCommentString();
525     CommentPrefix += ' ';
526   }
527 
528   TypeTableCollection Table(TypeTable.records());
529   Optional<TypeIndex> B = Table.getFirst();
530   while (B) {
531     // This will fail if the record data is invalid.
532     CVType Record = Table.getType(*B);
533 
534     if (OS.isVerboseAsm()) {
535       // Emit a block comment describing the type record for readability.
536       SmallString<512> CommentBlock;
537       raw_svector_ostream CommentOS(CommentBlock);
538       ScopedPrinter SP(CommentOS);
539       SP.setPrefix(CommentPrefix);
540       TypeDumpVisitor TDV(Table, &SP, false);
541 
542       Error E = codeview::visitTypeRecord(Record, *B, TDV);
543       if (E) {
544         logAllUnhandledErrors(std::move(E), errs(), "error: ");
545         llvm_unreachable("produced malformed type record");
546       }
547       // emitRawComment will insert its own tab and comment string before
548       // the first line, so strip off our first one. It also prints its own
549       // newline.
550       OS.emitRawComment(
551           CommentOS.str().drop_front(CommentPrefix.size() - 1).rtrim());
552     }
553     OS.EmitBinaryData(Record.str_data());
554     B = Table.getNext(*B);
555   }
556 }
557 
558 static SourceLanguage MapDWLangToCVLang(unsigned DWLang) {
559   switch (DWLang) {
560   case dwarf::DW_LANG_C:
561   case dwarf::DW_LANG_C89:
562   case dwarf::DW_LANG_C99:
563   case dwarf::DW_LANG_C11:
564   case dwarf::DW_LANG_ObjC:
565     return SourceLanguage::C;
566   case dwarf::DW_LANG_C_plus_plus:
567   case dwarf::DW_LANG_C_plus_plus_03:
568   case dwarf::DW_LANG_C_plus_plus_11:
569   case dwarf::DW_LANG_C_plus_plus_14:
570     return SourceLanguage::Cpp;
571   case dwarf::DW_LANG_Fortran77:
572   case dwarf::DW_LANG_Fortran90:
573   case dwarf::DW_LANG_Fortran03:
574   case dwarf::DW_LANG_Fortran08:
575     return SourceLanguage::Fortran;
576   case dwarf::DW_LANG_Pascal83:
577     return SourceLanguage::Pascal;
578   case dwarf::DW_LANG_Cobol74:
579   case dwarf::DW_LANG_Cobol85:
580     return SourceLanguage::Cobol;
581   case dwarf::DW_LANG_Java:
582     return SourceLanguage::Java;
583   case dwarf::DW_LANG_D:
584     return SourceLanguage::D;
585   default:
586     // There's no CodeView representation for this language, and CV doesn't
587     // have an "unknown" option for the language field, so we'll use MASM,
588     // as it's very low level.
589     return SourceLanguage::Masm;
590   }
591 }
592 
593 namespace {
594 struct Version {
595   int Part[4];
596 };
597 } // end anonymous namespace
598 
599 // Takes a StringRef like "clang 4.0.0.0 (other nonsense 123)" and parses out
600 // the version number.
601 static Version parseVersion(StringRef Name) {
602   Version V = {{0}};
603   int N = 0;
604   for (const char C : Name) {
605     if (isdigit(C)) {
606       V.Part[N] *= 10;
607       V.Part[N] += C - '0';
608     } else if (C == '.') {
609       ++N;
610       if (N >= 4)
611         return V;
612     } else if (N > 0)
613       return V;
614   }
615   return V;
616 }
617 
618 static CPUType mapArchToCVCPUType(Triple::ArchType Type) {
619   switch (Type) {
620   case Triple::ArchType::x86:
621     return CPUType::Pentium3;
622   case Triple::ArchType::x86_64:
623     return CPUType::X64;
624   case Triple::ArchType::thumb:
625     return CPUType::Thumb;
626   case Triple::ArchType::aarch64:
627     return CPUType::ARM64;
628   default:
629     report_fatal_error("target architecture doesn't map to a CodeView CPUType");
630   }
631 }
632 
633 void CodeViewDebug::emitCompilerInformation() {
634   MCContext &Context = MMI->getContext();
635   MCSymbol *CompilerBegin = Context.createTempSymbol(),
636            *CompilerEnd = Context.createTempSymbol();
637   OS.AddComment("Record length");
638   OS.emitAbsoluteSymbolDiff(CompilerEnd, CompilerBegin, 2);
639   OS.EmitLabel(CompilerBegin);
640   OS.AddComment("Record kind: S_COMPILE3");
641   OS.EmitIntValue(SymbolKind::S_COMPILE3, 2);
642   uint32_t Flags = 0;
643 
644   NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
645   const MDNode *Node = *CUs->operands().begin();
646   const auto *CU = cast<DICompileUnit>(Node);
647 
648   // The low byte of the flags indicates the source language.
649   Flags = MapDWLangToCVLang(CU->getSourceLanguage());
650   // TODO:  Figure out which other flags need to be set.
651 
652   OS.AddComment("Flags and language");
653   OS.EmitIntValue(Flags, 4);
654 
655   OS.AddComment("CPUType");
656   CPUType CPU =
657       mapArchToCVCPUType(Triple(MMI->getModule()->getTargetTriple()).getArch());
658   OS.EmitIntValue(static_cast<uint64_t>(CPU), 2);
659 
660   StringRef CompilerVersion = CU->getProducer();
661   Version FrontVer = parseVersion(CompilerVersion);
662   OS.AddComment("Frontend version");
663   for (int N = 0; N < 4; ++N)
664     OS.EmitIntValue(FrontVer.Part[N], 2);
665 
666   // Some Microsoft tools, like Binscope, expect a backend version number of at
667   // least 8.something, so we'll coerce the LLVM version into a form that
668   // guarantees it'll be big enough without really lying about the version.
669   int Major = 1000 * LLVM_VERSION_MAJOR +
670               10 * LLVM_VERSION_MINOR +
671               LLVM_VERSION_PATCH;
672   // Clamp it for builds that use unusually large version numbers.
673   Major = std::min<int>(Major, std::numeric_limits<uint16_t>::max());
674   Version BackVer = {{ Major, 0, 0, 0 }};
675   OS.AddComment("Backend version");
676   for (int N = 0; N < 4; ++N)
677     OS.EmitIntValue(BackVer.Part[N], 2);
678 
679   OS.AddComment("Null-terminated compiler version string");
680   emitNullTerminatedSymbolName(OS, CompilerVersion);
681 
682   OS.EmitLabel(CompilerEnd);
683 }
684 
685 void CodeViewDebug::emitInlineeLinesSubsection() {
686   if (InlinedSubprograms.empty())
687     return;
688 
689   OS.AddComment("Inlinee lines subsection");
690   MCSymbol *InlineEnd = beginCVSubsection(DebugSubsectionKind::InlineeLines);
691 
692   // We emit the checksum info for files.  This is used by debuggers to
693   // determine if a pdb matches the source before loading it.  Visual Studio,
694   // for instance, will display a warning that the breakpoints are not valid if
695   // the pdb does not match the source.
696   OS.AddComment("Inlinee lines signature");
697   OS.EmitIntValue(unsigned(InlineeLinesSignature::Normal), 4);
698 
699   for (const DISubprogram *SP : InlinedSubprograms) {
700     assert(TypeIndices.count({SP, nullptr}));
701     TypeIndex InlineeIdx = TypeIndices[{SP, nullptr}];
702 
703     OS.AddBlankLine();
704     unsigned FileId = maybeRecordFile(SP->getFile());
705     OS.AddComment("Inlined function " + SP->getName() + " starts at " +
706                   SP->getFilename() + Twine(':') + Twine(SP->getLine()));
707     OS.AddBlankLine();
708     OS.AddComment("Type index of inlined function");
709     OS.EmitIntValue(InlineeIdx.getIndex(), 4);
710     OS.AddComment("Offset into filechecksum table");
711     OS.EmitCVFileChecksumOffsetDirective(FileId);
712     OS.AddComment("Starting line number");
713     OS.EmitIntValue(SP->getLine(), 4);
714   }
715 
716   endCVSubsection(InlineEnd);
717 }
718 
719 void CodeViewDebug::emitInlinedCallSite(const FunctionInfo &FI,
720                                         const DILocation *InlinedAt,
721                                         const InlineSite &Site) {
722   MCSymbol *InlineBegin = MMI->getContext().createTempSymbol(),
723            *InlineEnd = MMI->getContext().createTempSymbol();
724 
725   assert(TypeIndices.count({Site.Inlinee, nullptr}));
726   TypeIndex InlineeIdx = TypeIndices[{Site.Inlinee, nullptr}];
727 
728   // SymbolRecord
729   OS.AddComment("Record length");
730   OS.emitAbsoluteSymbolDiff(InlineEnd, InlineBegin, 2);   // RecordLength
731   OS.EmitLabel(InlineBegin);
732   OS.AddComment("Record kind: S_INLINESITE");
733   OS.EmitIntValue(SymbolKind::S_INLINESITE, 2); // RecordKind
734 
735   OS.AddComment("PtrParent");
736   OS.EmitIntValue(0, 4);
737   OS.AddComment("PtrEnd");
738   OS.EmitIntValue(0, 4);
739   OS.AddComment("Inlinee type index");
740   OS.EmitIntValue(InlineeIdx.getIndex(), 4);
741 
742   unsigned FileId = maybeRecordFile(Site.Inlinee->getFile());
743   unsigned StartLineNum = Site.Inlinee->getLine();
744 
745   OS.EmitCVInlineLinetableDirective(Site.SiteFuncId, FileId, StartLineNum,
746                                     FI.Begin, FI.End);
747 
748   OS.EmitLabel(InlineEnd);
749 
750   emitLocalVariableList(Site.InlinedLocals);
751 
752   // Recurse on child inlined call sites before closing the scope.
753   for (const DILocation *ChildSite : Site.ChildSites) {
754     auto I = FI.InlineSites.find(ChildSite);
755     assert(I != FI.InlineSites.end() &&
756            "child site not in function inline site map");
757     emitInlinedCallSite(FI, ChildSite, I->second);
758   }
759 
760   // Close the scope.
761   OS.AddComment("Record length");
762   OS.EmitIntValue(2, 2);                                  // RecordLength
763   OS.AddComment("Record kind: S_INLINESITE_END");
764   OS.EmitIntValue(SymbolKind::S_INLINESITE_END, 2); // RecordKind
765 }
766 
767 void CodeViewDebug::switchToDebugSectionForSymbol(const MCSymbol *GVSym) {
768   // If we have a symbol, it may be in a section that is COMDAT. If so, find the
769   // comdat key. A section may be comdat because of -ffunction-sections or
770   // because it is comdat in the IR.
771   MCSectionCOFF *GVSec =
772       GVSym ? dyn_cast<MCSectionCOFF>(&GVSym->getSection()) : nullptr;
773   const MCSymbol *KeySym = GVSec ? GVSec->getCOMDATSymbol() : nullptr;
774 
775   MCSectionCOFF *DebugSec = cast<MCSectionCOFF>(
776       Asm->getObjFileLowering().getCOFFDebugSymbolsSection());
777   DebugSec = OS.getContext().getAssociativeCOFFSection(DebugSec, KeySym);
778 
779   OS.SwitchSection(DebugSec);
780 
781   // Emit the magic version number if this is the first time we've switched to
782   // this section.
783   if (ComdatDebugSections.insert(DebugSec).second)
784     emitCodeViewMagicVersion();
785 }
786 
787 void CodeViewDebug::emitDebugInfoForFunction(const Function *GV,
788                                              FunctionInfo &FI) {
789   // For each function there is a separate subsection
790   // which holds the PC to file:line table.
791   const MCSymbol *Fn = Asm->getSymbol(GV);
792   assert(Fn);
793 
794   // Switch to the to a comdat section, if appropriate.
795   switchToDebugSectionForSymbol(Fn);
796 
797   std::string FuncName;
798   auto *SP = GV->getSubprogram();
799   assert(SP);
800   setCurrentSubprogram(SP);
801 
802   // If we have a display name, build the fully qualified name by walking the
803   // chain of scopes.
804   if (!SP->getName().empty())
805     FuncName =
806         getFullyQualifiedName(SP->getScope().resolve(), SP->getName());
807 
808   // If our DISubprogram name is empty, use the mangled name.
809   if (FuncName.empty())
810     FuncName = GlobalValue::dropLLVMManglingEscape(GV->getName());
811 
812   // Emit FPO data, but only on 32-bit x86. No other platforms use it.
813   if (Triple(MMI->getModule()->getTargetTriple()).getArch() == Triple::x86)
814     OS.EmitCVFPOData(Fn);
815 
816   // Emit a symbol subsection, required by VS2012+ to find function boundaries.
817   OS.AddComment("Symbol subsection for " + Twine(FuncName));
818   MCSymbol *SymbolsEnd = beginCVSubsection(DebugSubsectionKind::Symbols);
819   {
820     MCSymbol *ProcRecordBegin = MMI->getContext().createTempSymbol(),
821              *ProcRecordEnd = MMI->getContext().createTempSymbol();
822     OS.AddComment("Record length");
823     OS.emitAbsoluteSymbolDiff(ProcRecordEnd, ProcRecordBegin, 2);
824     OS.EmitLabel(ProcRecordBegin);
825 
826     if (GV->hasLocalLinkage()) {
827       OS.AddComment("Record kind: S_LPROC32_ID");
828       OS.EmitIntValue(unsigned(SymbolKind::S_LPROC32_ID), 2);
829     } else {
830       OS.AddComment("Record kind: S_GPROC32_ID");
831       OS.EmitIntValue(unsigned(SymbolKind::S_GPROC32_ID), 2);
832     }
833 
834     // These fields are filled in by tools like CVPACK which run after the fact.
835     OS.AddComment("PtrParent");
836     OS.EmitIntValue(0, 4);
837     OS.AddComment("PtrEnd");
838     OS.EmitIntValue(0, 4);
839     OS.AddComment("PtrNext");
840     OS.EmitIntValue(0, 4);
841     // This is the important bit that tells the debugger where the function
842     // code is located and what's its size:
843     OS.AddComment("Code size");
844     OS.emitAbsoluteSymbolDiff(FI.End, Fn, 4);
845     OS.AddComment("Offset after prologue");
846     OS.EmitIntValue(0, 4);
847     OS.AddComment("Offset before epilogue");
848     OS.EmitIntValue(0, 4);
849     OS.AddComment("Function type index");
850     OS.EmitIntValue(getFuncIdForSubprogram(GV->getSubprogram()).getIndex(), 4);
851     OS.AddComment("Function section relative address");
852     OS.EmitCOFFSecRel32(Fn, /*Offset=*/0);
853     OS.AddComment("Function section index");
854     OS.EmitCOFFSectionIndex(Fn);
855     OS.AddComment("Flags");
856     OS.EmitIntValue(0, 1);
857     // Emit the function display name as a null-terminated string.
858     OS.AddComment("Function name");
859     // Truncate the name so we won't overflow the record length field.
860     emitNullTerminatedSymbolName(OS, FuncName);
861     OS.EmitLabel(ProcRecordEnd);
862 
863     emitLocalVariableList(FI.Locals);
864 
865     // Emit inlined call site information. Only emit functions inlined directly
866     // into the parent function. We'll emit the other sites recursively as part
867     // of their parent inline site.
868     for (const DILocation *InlinedAt : FI.ChildSites) {
869       auto I = FI.InlineSites.find(InlinedAt);
870       assert(I != FI.InlineSites.end() &&
871              "child site not in function inline site map");
872       emitInlinedCallSite(FI, InlinedAt, I->second);
873     }
874 
875     for (auto Annot : FI.Annotations) {
876       MCSymbol *Label = Annot.first;
877       MDTuple *Strs = cast<MDTuple>(Annot.second);
878       MCSymbol *AnnotBegin = MMI->getContext().createTempSymbol(),
879                *AnnotEnd = MMI->getContext().createTempSymbol();
880       OS.AddComment("Record length");
881       OS.emitAbsoluteSymbolDiff(AnnotEnd, AnnotBegin, 2);
882       OS.EmitLabel(AnnotBegin);
883       OS.AddComment("Record kind: S_ANNOTATION");
884       OS.EmitIntValue(SymbolKind::S_ANNOTATION, 2);
885       OS.EmitCOFFSecRel32(Label, /*Offset=*/0);
886       // FIXME: Make sure we don't overflow the max record size.
887       OS.EmitCOFFSectionIndex(Label);
888       OS.EmitIntValue(Strs->getNumOperands(), 2);
889       for (Metadata *MD : Strs->operands()) {
890         // MDStrings are null terminated, so we can do EmitBytes and get the
891         // nice .asciz directive.
892         StringRef Str = cast<MDString>(MD)->getString();
893         assert(Str.data()[Str.size()] == '\0' && "non-nullterminated MDString");
894         OS.EmitBytes(StringRef(Str.data(), Str.size() + 1));
895       }
896       OS.EmitLabel(AnnotEnd);
897     }
898 
899     if (SP != nullptr)
900       emitDebugInfoForUDTs(LocalUDTs);
901 
902     // We're done with this function.
903     OS.AddComment("Record length");
904     OS.EmitIntValue(0x0002, 2);
905     OS.AddComment("Record kind: S_PROC_ID_END");
906     OS.EmitIntValue(unsigned(SymbolKind::S_PROC_ID_END), 2);
907   }
908   endCVSubsection(SymbolsEnd);
909 
910   // We have an assembler directive that takes care of the whole line table.
911   OS.EmitCVLinetableDirective(FI.FuncId, Fn, FI.End);
912 }
913 
914 CodeViewDebug::LocalVarDefRange
915 CodeViewDebug::createDefRangeMem(uint16_t CVRegister, int Offset) {
916   LocalVarDefRange DR;
917   DR.InMemory = -1;
918   DR.DataOffset = Offset;
919   assert(DR.DataOffset == Offset && "truncation");
920   DR.IsSubfield = 0;
921   DR.StructOffset = 0;
922   DR.CVRegister = CVRegister;
923   return DR;
924 }
925 
926 CodeViewDebug::LocalVarDefRange
927 CodeViewDebug::createDefRangeGeneral(uint16_t CVRegister, bool InMemory,
928                                      int Offset, bool IsSubfield,
929                                      uint16_t StructOffset) {
930   LocalVarDefRange DR;
931   DR.InMemory = InMemory;
932   DR.DataOffset = Offset;
933   DR.IsSubfield = IsSubfield;
934   DR.StructOffset = StructOffset;
935   DR.CVRegister = CVRegister;
936   return DR;
937 }
938 
939 void CodeViewDebug::collectVariableInfoFromMFTable(
940     DenseSet<InlinedVariable> &Processed) {
941   const MachineFunction &MF = *Asm->MF;
942   const TargetSubtargetInfo &TSI = MF.getSubtarget();
943   const TargetFrameLowering *TFI = TSI.getFrameLowering();
944   const TargetRegisterInfo *TRI = TSI.getRegisterInfo();
945 
946   for (const MachineFunction::VariableDbgInfo &VI : MF.getVariableDbgInfo()) {
947     if (!VI.Var)
948       continue;
949     assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) &&
950            "Expected inlined-at fields to agree");
951 
952     Processed.insert(InlinedVariable(VI.Var, VI.Loc->getInlinedAt()));
953     LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc);
954 
955     // If variable scope is not found then skip this variable.
956     if (!Scope)
957       continue;
958 
959     // If the variable has an attached offset expression, extract it.
960     // FIXME: Try to handle DW_OP_deref as well.
961     int64_t ExprOffset = 0;
962     if (VI.Expr)
963       if (!VI.Expr->extractIfOffset(ExprOffset))
964         continue;
965 
966     // Get the frame register used and the offset.
967     unsigned FrameReg = 0;
968     int FrameOffset = TFI->getFrameIndexReference(*Asm->MF, VI.Slot, FrameReg);
969     uint16_t CVReg = TRI->getCodeViewRegNum(FrameReg);
970 
971     // Calculate the label ranges.
972     LocalVarDefRange DefRange =
973         createDefRangeMem(CVReg, FrameOffset + ExprOffset);
974     for (const InsnRange &Range : Scope->getRanges()) {
975       const MCSymbol *Begin = getLabelBeforeInsn(Range.first);
976       const MCSymbol *End = getLabelAfterInsn(Range.second);
977       End = End ? End : Asm->getFunctionEnd();
978       DefRange.Ranges.emplace_back(Begin, End);
979     }
980 
981     LocalVariable Var;
982     Var.DIVar = VI.Var;
983     Var.DefRanges.emplace_back(std::move(DefRange));
984     recordLocalVariable(std::move(Var), VI.Loc->getInlinedAt());
985   }
986 }
987 
988 static bool canUseReferenceType(const DbgVariableLocation &Loc) {
989   return !Loc.LoadChain.empty() && Loc.LoadChain.back() == 0;
990 }
991 
992 static bool needsReferenceType(const DbgVariableLocation &Loc) {
993   return Loc.LoadChain.size() == 2 && Loc.LoadChain.back() == 0;
994 }
995 
996 void CodeViewDebug::calculateRanges(
997     LocalVariable &Var, const DbgValueHistoryMap::InstrRanges &Ranges) {
998   const TargetRegisterInfo *TRI = Asm->MF->getSubtarget().getRegisterInfo();
999 
1000   // Calculate the definition ranges.
1001   for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1002     const InsnRange &Range = *I;
1003     const MachineInstr *DVInst = Range.first;
1004     assert(DVInst->isDebugValue() && "Invalid History entry");
1005     // FIXME: Find a way to represent constant variables, since they are
1006     // relatively common.
1007     Optional<DbgVariableLocation> Location =
1008         DbgVariableLocation::extractFromMachineInstruction(*DVInst);
1009     if (!Location)
1010       continue;
1011 
1012     // CodeView can only express variables in register and variables in memory
1013     // at a constant offset from a register. However, for variables passed
1014     // indirectly by pointer, it is common for that pointer to be spilled to a
1015     // stack location. For the special case of one offseted load followed by a
1016     // zero offset load (a pointer spilled to the stack), we change the type of
1017     // the local variable from a value type to a reference type. This tricks the
1018     // debugger into doing the load for us.
1019     if (Var.UseReferenceType) {
1020       // We're using a reference type. Drop the last zero offset load.
1021       if (canUseReferenceType(*Location))
1022         Location->LoadChain.pop_back();
1023       else
1024         continue;
1025     } else if (needsReferenceType(*Location)) {
1026       // This location can't be expressed without switching to a reference type.
1027       // Start over using that.
1028       Var.UseReferenceType = true;
1029       Var.DefRanges.clear();
1030       calculateRanges(Var, Ranges);
1031       return;
1032     }
1033 
1034     // We can only handle a register or an offseted load of a register.
1035     if (Location->Register == 0 || Location->LoadChain.size() > 1)
1036       continue;
1037     {
1038       LocalVarDefRange DR;
1039       DR.CVRegister = TRI->getCodeViewRegNum(Location->Register);
1040       DR.InMemory = !Location->LoadChain.empty();
1041       DR.DataOffset =
1042           !Location->LoadChain.empty() ? Location->LoadChain.back() : 0;
1043       if (Location->FragmentInfo) {
1044         DR.IsSubfield = true;
1045         DR.StructOffset = Location->FragmentInfo->OffsetInBits / 8;
1046       } else {
1047         DR.IsSubfield = false;
1048         DR.StructOffset = 0;
1049       }
1050 
1051       if (Var.DefRanges.empty() ||
1052           Var.DefRanges.back().isDifferentLocation(DR)) {
1053         Var.DefRanges.emplace_back(std::move(DR));
1054       }
1055     }
1056 
1057     // Compute the label range.
1058     const MCSymbol *Begin = getLabelBeforeInsn(Range.first);
1059     const MCSymbol *End = getLabelAfterInsn(Range.second);
1060     if (!End) {
1061       // This range is valid until the next overlapping bitpiece. In the
1062       // common case, ranges will not be bitpieces, so they will overlap.
1063       auto J = std::next(I);
1064       const DIExpression *DIExpr = DVInst->getDebugExpression();
1065       while (J != E &&
1066              !fragmentsOverlap(DIExpr, J->first->getDebugExpression()))
1067         ++J;
1068       if (J != E)
1069         End = getLabelBeforeInsn(J->first);
1070       else
1071         End = Asm->getFunctionEnd();
1072     }
1073 
1074     // If the last range end is our begin, just extend the last range.
1075     // Otherwise make a new range.
1076     SmallVectorImpl<std::pair<const MCSymbol *, const MCSymbol *>> &R =
1077         Var.DefRanges.back().Ranges;
1078     if (!R.empty() && R.back().second == Begin)
1079       R.back().second = End;
1080     else
1081       R.emplace_back(Begin, End);
1082 
1083     // FIXME: Do more range combining.
1084   }
1085 }
1086 
1087 void CodeViewDebug::collectVariableInfo(const DISubprogram *SP) {
1088   DenseSet<InlinedVariable> Processed;
1089   // Grab the variable info that was squirreled away in the MMI side-table.
1090   collectVariableInfoFromMFTable(Processed);
1091 
1092   for (const auto &I : DbgValues) {
1093     InlinedVariable IV = I.first;
1094     if (Processed.count(IV))
1095       continue;
1096     const DILocalVariable *DIVar = IV.first;
1097     const DILocation *InlinedAt = IV.second;
1098 
1099     // Instruction ranges, specifying where IV is accessible.
1100     const auto &Ranges = I.second;
1101 
1102     LexicalScope *Scope = nullptr;
1103     if (InlinedAt)
1104       Scope = LScopes.findInlinedScope(DIVar->getScope(), InlinedAt);
1105     else
1106       Scope = LScopes.findLexicalScope(DIVar->getScope());
1107     // If variable scope is not found then skip this variable.
1108     if (!Scope)
1109       continue;
1110 
1111     LocalVariable Var;
1112     Var.DIVar = DIVar;
1113 
1114     calculateRanges(Var, Ranges);
1115     recordLocalVariable(std::move(Var), InlinedAt);
1116   }
1117 }
1118 
1119 void CodeViewDebug::beginFunctionImpl(const MachineFunction *MF) {
1120   const Function *GV = MF->getFunction();
1121   assert(FnDebugInfo.count(GV) == false);
1122   CurFn = &FnDebugInfo[GV];
1123   CurFn->FuncId = NextFuncId++;
1124   CurFn->Begin = Asm->getFunctionBegin();
1125 
1126   OS.EmitCVFuncIdDirective(CurFn->FuncId);
1127 
1128   // Find the end of the function prolog.  First known non-DBG_VALUE and
1129   // non-frame setup location marks the beginning of the function body.
1130   // FIXME: is there a simpler a way to do this? Can we just search
1131   // for the first instruction of the function, not the last of the prolog?
1132   DebugLoc PrologEndLoc;
1133   bool EmptyPrologue = true;
1134   for (const auto &MBB : *MF) {
1135     for (const auto &MI : MBB) {
1136       if (!MI.isMetaInstruction() && !MI.getFlag(MachineInstr::FrameSetup) &&
1137           MI.getDebugLoc()) {
1138         PrologEndLoc = MI.getDebugLoc();
1139         break;
1140       } else if (!MI.isMetaInstruction()) {
1141         EmptyPrologue = false;
1142       }
1143     }
1144   }
1145 
1146   // Record beginning of function if we have a non-empty prologue.
1147   if (PrologEndLoc && !EmptyPrologue) {
1148     DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc();
1149     maybeRecordLocation(FnStartDL, MF);
1150   }
1151 }
1152 
1153 static bool shouldEmitUdt(const DIType *T) {
1154   if (!T)
1155     return false;
1156 
1157   // MSVC does not emit UDTs for typedefs that are scoped to classes.
1158   if (T->getTag() == dwarf::DW_TAG_typedef) {
1159     if (DIScope *Scope = T->getScope().resolve()) {
1160       switch (Scope->getTag()) {
1161       case dwarf::DW_TAG_structure_type:
1162       case dwarf::DW_TAG_class_type:
1163       case dwarf::DW_TAG_union_type:
1164         return false;
1165       }
1166     }
1167   }
1168 
1169   while (true) {
1170     if (!T || T->isForwardDecl())
1171       return false;
1172 
1173     const DIDerivedType *DT = dyn_cast<DIDerivedType>(T);
1174     if (!DT)
1175       return true;
1176     T = DT->getBaseType().resolve();
1177   }
1178   return true;
1179 }
1180 
1181 void CodeViewDebug::addToUDTs(const DIType *Ty) {
1182   // Don't record empty UDTs.
1183   if (Ty->getName().empty())
1184     return;
1185   if (!shouldEmitUdt(Ty))
1186     return;
1187 
1188   SmallVector<StringRef, 5> QualifiedNameComponents;
1189   const DISubprogram *ClosestSubprogram = getQualifiedNameComponents(
1190       Ty->getScope().resolve(), QualifiedNameComponents);
1191 
1192   std::string FullyQualifiedName =
1193       getQualifiedName(QualifiedNameComponents, getPrettyScopeName(Ty));
1194 
1195   if (ClosestSubprogram == nullptr) {
1196     GlobalUDTs.emplace_back(std::move(FullyQualifiedName), Ty);
1197   } else if (ClosestSubprogram == CurrentSubprogram) {
1198     LocalUDTs.emplace_back(std::move(FullyQualifiedName), Ty);
1199   }
1200 
1201   // TODO: What if the ClosestSubprogram is neither null or the current
1202   // subprogram?  Currently, the UDT just gets dropped on the floor.
1203   //
1204   // The current behavior is not desirable.  To get maximal fidelity, we would
1205   // need to perform all type translation before beginning emission of .debug$S
1206   // and then make LocalUDTs a member of FunctionInfo
1207 }
1208 
1209 TypeIndex CodeViewDebug::lowerType(const DIType *Ty, const DIType *ClassTy) {
1210   // Generic dispatch for lowering an unknown type.
1211   switch (Ty->getTag()) {
1212   case dwarf::DW_TAG_array_type:
1213     return lowerTypeArray(cast<DICompositeType>(Ty));
1214   case dwarf::DW_TAG_typedef:
1215     return lowerTypeAlias(cast<DIDerivedType>(Ty));
1216   case dwarf::DW_TAG_base_type:
1217     return lowerTypeBasic(cast<DIBasicType>(Ty));
1218   case dwarf::DW_TAG_pointer_type:
1219     if (cast<DIDerivedType>(Ty)->getName() == "__vtbl_ptr_type")
1220       return lowerTypeVFTableShape(cast<DIDerivedType>(Ty));
1221     LLVM_FALLTHROUGH;
1222   case dwarf::DW_TAG_reference_type:
1223   case dwarf::DW_TAG_rvalue_reference_type:
1224     return lowerTypePointer(cast<DIDerivedType>(Ty));
1225   case dwarf::DW_TAG_ptr_to_member_type:
1226     return lowerTypeMemberPointer(cast<DIDerivedType>(Ty));
1227   case dwarf::DW_TAG_const_type:
1228   case dwarf::DW_TAG_volatile_type:
1229   // TODO: add support for DW_TAG_atomic_type here
1230     return lowerTypeModifier(cast<DIDerivedType>(Ty));
1231   case dwarf::DW_TAG_subroutine_type:
1232     if (ClassTy) {
1233       // The member function type of a member function pointer has no
1234       // ThisAdjustment.
1235       return lowerTypeMemberFunction(cast<DISubroutineType>(Ty), ClassTy,
1236                                      /*ThisAdjustment=*/0,
1237                                      /*IsStaticMethod=*/false);
1238     }
1239     return lowerTypeFunction(cast<DISubroutineType>(Ty));
1240   case dwarf::DW_TAG_enumeration_type:
1241     return lowerTypeEnum(cast<DICompositeType>(Ty));
1242   case dwarf::DW_TAG_class_type:
1243   case dwarf::DW_TAG_structure_type:
1244     return lowerTypeClass(cast<DICompositeType>(Ty));
1245   case dwarf::DW_TAG_union_type:
1246     return lowerTypeUnion(cast<DICompositeType>(Ty));
1247   default:
1248     // Use the null type index.
1249     return TypeIndex();
1250   }
1251 }
1252 
1253 TypeIndex CodeViewDebug::lowerTypeAlias(const DIDerivedType *Ty) {
1254   DITypeRef UnderlyingTypeRef = Ty->getBaseType();
1255   TypeIndex UnderlyingTypeIndex = getTypeIndex(UnderlyingTypeRef);
1256   StringRef TypeName = Ty->getName();
1257 
1258   addToUDTs(Ty);
1259 
1260   if (UnderlyingTypeIndex == TypeIndex(SimpleTypeKind::Int32Long) &&
1261       TypeName == "HRESULT")
1262     return TypeIndex(SimpleTypeKind::HResult);
1263   if (UnderlyingTypeIndex == TypeIndex(SimpleTypeKind::UInt16Short) &&
1264       TypeName == "wchar_t")
1265     return TypeIndex(SimpleTypeKind::WideCharacter);
1266 
1267   return UnderlyingTypeIndex;
1268 }
1269 
1270 TypeIndex CodeViewDebug::lowerTypeArray(const DICompositeType *Ty) {
1271   DITypeRef ElementTypeRef = Ty->getBaseType();
1272   TypeIndex ElementTypeIndex = getTypeIndex(ElementTypeRef);
1273   // IndexType is size_t, which depends on the bitness of the target.
1274   TypeIndex IndexType = Asm->TM.getPointerSize() == 8
1275                             ? TypeIndex(SimpleTypeKind::UInt64Quad)
1276                             : TypeIndex(SimpleTypeKind::UInt32Long);
1277 
1278   uint64_t ElementSize = getBaseTypeSize(ElementTypeRef) / 8;
1279 
1280   // Add subranges to array type.
1281   DINodeArray Elements = Ty->getElements();
1282   for (int i = Elements.size() - 1; i >= 0; --i) {
1283     const DINode *Element = Elements[i];
1284     assert(Element->getTag() == dwarf::DW_TAG_subrange_type);
1285 
1286     const DISubrange *Subrange = cast<DISubrange>(Element);
1287     assert(Subrange->getLowerBound() == 0 &&
1288            "codeview doesn't support subranges with lower bounds");
1289     int64_t Count = Subrange->getCount();
1290 
1291     // Forward declarations of arrays without a size and VLAs use a count of -1.
1292     // Emit a count of zero in these cases to match what MSVC does for arrays
1293     // without a size. MSVC doesn't support VLAs, so it's not clear what we
1294     // should do for them even if we could distinguish them.
1295     if (Count == -1)
1296       Count = 0;
1297 
1298     // Update the element size and element type index for subsequent subranges.
1299     ElementSize *= Count;
1300 
1301     // If this is the outermost array, use the size from the array. It will be
1302     // more accurate if we had a VLA or an incomplete element type size.
1303     uint64_t ArraySize =
1304         (i == 0 && ElementSize == 0) ? Ty->getSizeInBits() / 8 : ElementSize;
1305 
1306     StringRef Name = (i == 0) ? Ty->getName() : "";
1307     ArrayRecord AR(ElementTypeIndex, IndexType, ArraySize, Name);
1308     ElementTypeIndex = TypeTable.writeLeafType(AR);
1309   }
1310 
1311   return ElementTypeIndex;
1312 }
1313 
1314 TypeIndex CodeViewDebug::lowerTypeBasic(const DIBasicType *Ty) {
1315   TypeIndex Index;
1316   dwarf::TypeKind Kind;
1317   uint32_t ByteSize;
1318 
1319   Kind = static_cast<dwarf::TypeKind>(Ty->getEncoding());
1320   ByteSize = Ty->getSizeInBits() / 8;
1321 
1322   SimpleTypeKind STK = SimpleTypeKind::None;
1323   switch (Kind) {
1324   case dwarf::DW_ATE_address:
1325     // FIXME: Translate
1326     break;
1327   case dwarf::DW_ATE_boolean:
1328     switch (ByteSize) {
1329     case 1:  STK = SimpleTypeKind::Boolean8;   break;
1330     case 2:  STK = SimpleTypeKind::Boolean16;  break;
1331     case 4:  STK = SimpleTypeKind::Boolean32;  break;
1332     case 8:  STK = SimpleTypeKind::Boolean64;  break;
1333     case 16: STK = SimpleTypeKind::Boolean128; break;
1334     }
1335     break;
1336   case dwarf::DW_ATE_complex_float:
1337     switch (ByteSize) {
1338     case 2:  STK = SimpleTypeKind::Complex16;  break;
1339     case 4:  STK = SimpleTypeKind::Complex32;  break;
1340     case 8:  STK = SimpleTypeKind::Complex64;  break;
1341     case 10: STK = SimpleTypeKind::Complex80;  break;
1342     case 16: STK = SimpleTypeKind::Complex128; break;
1343     }
1344     break;
1345   case dwarf::DW_ATE_float:
1346     switch (ByteSize) {
1347     case 2:  STK = SimpleTypeKind::Float16;  break;
1348     case 4:  STK = SimpleTypeKind::Float32;  break;
1349     case 6:  STK = SimpleTypeKind::Float48;  break;
1350     case 8:  STK = SimpleTypeKind::Float64;  break;
1351     case 10: STK = SimpleTypeKind::Float80;  break;
1352     case 16: STK = SimpleTypeKind::Float128; break;
1353     }
1354     break;
1355   case dwarf::DW_ATE_signed:
1356     switch (ByteSize) {
1357     case 1:  STK = SimpleTypeKind::SignedCharacter; break;
1358     case 2:  STK = SimpleTypeKind::Int16Short;      break;
1359     case 4:  STK = SimpleTypeKind::Int32;           break;
1360     case 8:  STK = SimpleTypeKind::Int64Quad;       break;
1361     case 16: STK = SimpleTypeKind::Int128Oct;       break;
1362     }
1363     break;
1364   case dwarf::DW_ATE_unsigned:
1365     switch (ByteSize) {
1366     case 1:  STK = SimpleTypeKind::UnsignedCharacter; break;
1367     case 2:  STK = SimpleTypeKind::UInt16Short;       break;
1368     case 4:  STK = SimpleTypeKind::UInt32;            break;
1369     case 8:  STK = SimpleTypeKind::UInt64Quad;        break;
1370     case 16: STK = SimpleTypeKind::UInt128Oct;        break;
1371     }
1372     break;
1373   case dwarf::DW_ATE_UTF:
1374     switch (ByteSize) {
1375     case 2: STK = SimpleTypeKind::Character16; break;
1376     case 4: STK = SimpleTypeKind::Character32; break;
1377     }
1378     break;
1379   case dwarf::DW_ATE_signed_char:
1380     if (ByteSize == 1)
1381       STK = SimpleTypeKind::SignedCharacter;
1382     break;
1383   case dwarf::DW_ATE_unsigned_char:
1384     if (ByteSize == 1)
1385       STK = SimpleTypeKind::UnsignedCharacter;
1386     break;
1387   default:
1388     break;
1389   }
1390 
1391   // Apply some fixups based on the source-level type name.
1392   if (STK == SimpleTypeKind::Int32 && Ty->getName() == "long int")
1393     STK = SimpleTypeKind::Int32Long;
1394   if (STK == SimpleTypeKind::UInt32 && Ty->getName() == "long unsigned int")
1395     STK = SimpleTypeKind::UInt32Long;
1396   if (STK == SimpleTypeKind::UInt16Short &&
1397       (Ty->getName() == "wchar_t" || Ty->getName() == "__wchar_t"))
1398     STK = SimpleTypeKind::WideCharacter;
1399   if ((STK == SimpleTypeKind::SignedCharacter ||
1400        STK == SimpleTypeKind::UnsignedCharacter) &&
1401       Ty->getName() == "char")
1402     STK = SimpleTypeKind::NarrowCharacter;
1403 
1404   return TypeIndex(STK);
1405 }
1406 
1407 TypeIndex CodeViewDebug::lowerTypePointer(const DIDerivedType *Ty) {
1408   TypeIndex PointeeTI = getTypeIndex(Ty->getBaseType());
1409 
1410   // Pointers to simple types can use SimpleTypeMode, rather than having a
1411   // dedicated pointer type record.
1412   if (PointeeTI.isSimple() &&
1413       PointeeTI.getSimpleMode() == SimpleTypeMode::Direct &&
1414       Ty->getTag() == dwarf::DW_TAG_pointer_type) {
1415     SimpleTypeMode Mode = Ty->getSizeInBits() == 64
1416                               ? SimpleTypeMode::NearPointer64
1417                               : SimpleTypeMode::NearPointer32;
1418     return TypeIndex(PointeeTI.getSimpleKind(), Mode);
1419   }
1420 
1421   PointerKind PK =
1422       Ty->getSizeInBits() == 64 ? PointerKind::Near64 : PointerKind::Near32;
1423   PointerMode PM = PointerMode::Pointer;
1424   switch (Ty->getTag()) {
1425   default: llvm_unreachable("not a pointer tag type");
1426   case dwarf::DW_TAG_pointer_type:
1427     PM = PointerMode::Pointer;
1428     break;
1429   case dwarf::DW_TAG_reference_type:
1430     PM = PointerMode::LValueReference;
1431     break;
1432   case dwarf::DW_TAG_rvalue_reference_type:
1433     PM = PointerMode::RValueReference;
1434     break;
1435   }
1436   // FIXME: MSVC folds qualifiers into PointerOptions in the context of a method
1437   // 'this' pointer, but not normal contexts. Figure out what we're supposed to
1438   // do.
1439   PointerOptions PO = PointerOptions::None;
1440   PointerRecord PR(PointeeTI, PK, PM, PO, Ty->getSizeInBits() / 8);
1441   return TypeTable.writeLeafType(PR);
1442 }
1443 
1444 static PointerToMemberRepresentation
1445 translatePtrToMemberRep(unsigned SizeInBytes, bool IsPMF, unsigned Flags) {
1446   // SizeInBytes being zero generally implies that the member pointer type was
1447   // incomplete, which can happen if it is part of a function prototype. In this
1448   // case, use the unknown model instead of the general model.
1449   if (IsPMF) {
1450     switch (Flags & DINode::FlagPtrToMemberRep) {
1451     case 0:
1452       return SizeInBytes == 0 ? PointerToMemberRepresentation::Unknown
1453                               : PointerToMemberRepresentation::GeneralFunction;
1454     case DINode::FlagSingleInheritance:
1455       return PointerToMemberRepresentation::SingleInheritanceFunction;
1456     case DINode::FlagMultipleInheritance:
1457       return PointerToMemberRepresentation::MultipleInheritanceFunction;
1458     case DINode::FlagVirtualInheritance:
1459       return PointerToMemberRepresentation::VirtualInheritanceFunction;
1460     }
1461   } else {
1462     switch (Flags & DINode::FlagPtrToMemberRep) {
1463     case 0:
1464       return SizeInBytes == 0 ? PointerToMemberRepresentation::Unknown
1465                               : PointerToMemberRepresentation::GeneralData;
1466     case DINode::FlagSingleInheritance:
1467       return PointerToMemberRepresentation::SingleInheritanceData;
1468     case DINode::FlagMultipleInheritance:
1469       return PointerToMemberRepresentation::MultipleInheritanceData;
1470     case DINode::FlagVirtualInheritance:
1471       return PointerToMemberRepresentation::VirtualInheritanceData;
1472     }
1473   }
1474   llvm_unreachable("invalid ptr to member representation");
1475 }
1476 
1477 TypeIndex CodeViewDebug::lowerTypeMemberPointer(const DIDerivedType *Ty) {
1478   assert(Ty->getTag() == dwarf::DW_TAG_ptr_to_member_type);
1479   TypeIndex ClassTI = getTypeIndex(Ty->getClassType());
1480   TypeIndex PointeeTI = getTypeIndex(Ty->getBaseType(), Ty->getClassType());
1481   PointerKind PK = Asm->TM.getPointerSize() == 8 ? PointerKind::Near64
1482                                                  : PointerKind::Near32;
1483   bool IsPMF = isa<DISubroutineType>(Ty->getBaseType());
1484   PointerMode PM = IsPMF ? PointerMode::PointerToMemberFunction
1485                          : PointerMode::PointerToDataMember;
1486   PointerOptions PO = PointerOptions::None; // FIXME
1487   assert(Ty->getSizeInBits() / 8 <= 0xff && "pointer size too big");
1488   uint8_t SizeInBytes = Ty->getSizeInBits() / 8;
1489   MemberPointerInfo MPI(
1490       ClassTI, translatePtrToMemberRep(SizeInBytes, IsPMF, Ty->getFlags()));
1491   PointerRecord PR(PointeeTI, PK, PM, PO, SizeInBytes, MPI);
1492   return TypeTable.writeLeafType(PR);
1493 }
1494 
1495 /// Given a DWARF calling convention, get the CodeView equivalent. If we don't
1496 /// have a translation, use the NearC convention.
1497 static CallingConvention dwarfCCToCodeView(unsigned DwarfCC) {
1498   switch (DwarfCC) {
1499   case dwarf::DW_CC_normal:             return CallingConvention::NearC;
1500   case dwarf::DW_CC_BORLAND_msfastcall: return CallingConvention::NearFast;
1501   case dwarf::DW_CC_BORLAND_thiscall:   return CallingConvention::ThisCall;
1502   case dwarf::DW_CC_BORLAND_stdcall:    return CallingConvention::NearStdCall;
1503   case dwarf::DW_CC_BORLAND_pascal:     return CallingConvention::NearPascal;
1504   case dwarf::DW_CC_LLVM_vectorcall:    return CallingConvention::NearVector;
1505   }
1506   return CallingConvention::NearC;
1507 }
1508 
1509 TypeIndex CodeViewDebug::lowerTypeModifier(const DIDerivedType *Ty) {
1510   ModifierOptions Mods = ModifierOptions::None;
1511   bool IsModifier = true;
1512   const DIType *BaseTy = Ty;
1513   while (IsModifier && BaseTy) {
1514     // FIXME: Need to add DWARF tags for __unaligned and _Atomic
1515     switch (BaseTy->getTag()) {
1516     case dwarf::DW_TAG_const_type:
1517       Mods |= ModifierOptions::Const;
1518       break;
1519     case dwarf::DW_TAG_volatile_type:
1520       Mods |= ModifierOptions::Volatile;
1521       break;
1522     default:
1523       IsModifier = false;
1524       break;
1525     }
1526     if (IsModifier)
1527       BaseTy = cast<DIDerivedType>(BaseTy)->getBaseType().resolve();
1528   }
1529   TypeIndex ModifiedTI = getTypeIndex(BaseTy);
1530   ModifierRecord MR(ModifiedTI, Mods);
1531   return TypeTable.writeLeafType(MR);
1532 }
1533 
1534 TypeIndex CodeViewDebug::lowerTypeFunction(const DISubroutineType *Ty) {
1535   SmallVector<TypeIndex, 8> ReturnAndArgTypeIndices;
1536   for (DITypeRef ArgTypeRef : Ty->getTypeArray())
1537     ReturnAndArgTypeIndices.push_back(getTypeIndex(ArgTypeRef));
1538 
1539   TypeIndex ReturnTypeIndex = TypeIndex::Void();
1540   ArrayRef<TypeIndex> ArgTypeIndices = None;
1541   if (!ReturnAndArgTypeIndices.empty()) {
1542     auto ReturnAndArgTypesRef = makeArrayRef(ReturnAndArgTypeIndices);
1543     ReturnTypeIndex = ReturnAndArgTypesRef.front();
1544     ArgTypeIndices = ReturnAndArgTypesRef.drop_front();
1545   }
1546 
1547   ArgListRecord ArgListRec(TypeRecordKind::ArgList, ArgTypeIndices);
1548   TypeIndex ArgListIndex = TypeTable.writeLeafType(ArgListRec);
1549 
1550   CallingConvention CC = dwarfCCToCodeView(Ty->getCC());
1551 
1552   ProcedureRecord Procedure(ReturnTypeIndex, CC, FunctionOptions::None,
1553                             ArgTypeIndices.size(), ArgListIndex);
1554   return TypeTable.writeLeafType(Procedure);
1555 }
1556 
1557 TypeIndex CodeViewDebug::lowerTypeMemberFunction(const DISubroutineType *Ty,
1558                                                  const DIType *ClassTy,
1559                                                  int ThisAdjustment,
1560                                                  bool IsStaticMethod) {
1561   // Lower the containing class type.
1562   TypeIndex ClassType = getTypeIndex(ClassTy);
1563 
1564   SmallVector<TypeIndex, 8> ReturnAndArgTypeIndices;
1565   for (DITypeRef ArgTypeRef : Ty->getTypeArray())
1566     ReturnAndArgTypeIndices.push_back(getTypeIndex(ArgTypeRef));
1567 
1568   TypeIndex ReturnTypeIndex = TypeIndex::Void();
1569   ArrayRef<TypeIndex> ArgTypeIndices = None;
1570   if (!ReturnAndArgTypeIndices.empty()) {
1571     auto ReturnAndArgTypesRef = makeArrayRef(ReturnAndArgTypeIndices);
1572     ReturnTypeIndex = ReturnAndArgTypesRef.front();
1573     ArgTypeIndices = ReturnAndArgTypesRef.drop_front();
1574   }
1575   TypeIndex ThisTypeIndex;
1576   if (!IsStaticMethod && !ArgTypeIndices.empty()) {
1577     ThisTypeIndex = ArgTypeIndices.front();
1578     ArgTypeIndices = ArgTypeIndices.drop_front();
1579   }
1580 
1581   ArgListRecord ArgListRec(TypeRecordKind::ArgList, ArgTypeIndices);
1582   TypeIndex ArgListIndex = TypeTable.writeLeafType(ArgListRec);
1583 
1584   CallingConvention CC = dwarfCCToCodeView(Ty->getCC());
1585 
1586   // TODO: Need to use the correct values for FunctionOptions.
1587   MemberFunctionRecord MFR(ReturnTypeIndex, ClassType, ThisTypeIndex, CC,
1588                            FunctionOptions::None, ArgTypeIndices.size(),
1589                            ArgListIndex, ThisAdjustment);
1590   return TypeTable.writeLeafType(MFR);
1591 }
1592 
1593 TypeIndex CodeViewDebug::lowerTypeVFTableShape(const DIDerivedType *Ty) {
1594   unsigned VSlotCount =
1595       Ty->getSizeInBits() / (8 * Asm->MAI->getCodePointerSize());
1596   SmallVector<VFTableSlotKind, 4> Slots(VSlotCount, VFTableSlotKind::Near);
1597 
1598   VFTableShapeRecord VFTSR(Slots);
1599   return TypeTable.writeLeafType(VFTSR);
1600 }
1601 
1602 static MemberAccess translateAccessFlags(unsigned RecordTag, unsigned Flags) {
1603   switch (Flags & DINode::FlagAccessibility) {
1604   case DINode::FlagPrivate:   return MemberAccess::Private;
1605   case DINode::FlagPublic:    return MemberAccess::Public;
1606   case DINode::FlagProtected: return MemberAccess::Protected;
1607   case 0:
1608     // If there was no explicit access control, provide the default for the tag.
1609     return RecordTag == dwarf::DW_TAG_class_type ? MemberAccess::Private
1610                                                  : MemberAccess::Public;
1611   }
1612   llvm_unreachable("access flags are exclusive");
1613 }
1614 
1615 static MethodOptions translateMethodOptionFlags(const DISubprogram *SP) {
1616   if (SP->isArtificial())
1617     return MethodOptions::CompilerGenerated;
1618 
1619   // FIXME: Handle other MethodOptions.
1620 
1621   return MethodOptions::None;
1622 }
1623 
1624 static MethodKind translateMethodKindFlags(const DISubprogram *SP,
1625                                            bool Introduced) {
1626   if (SP->getFlags() & DINode::FlagStaticMember)
1627     return MethodKind::Static;
1628 
1629   switch (SP->getVirtuality()) {
1630   case dwarf::DW_VIRTUALITY_none:
1631     break;
1632   case dwarf::DW_VIRTUALITY_virtual:
1633     return Introduced ? MethodKind::IntroducingVirtual : MethodKind::Virtual;
1634   case dwarf::DW_VIRTUALITY_pure_virtual:
1635     return Introduced ? MethodKind::PureIntroducingVirtual
1636                       : MethodKind::PureVirtual;
1637   default:
1638     llvm_unreachable("unhandled virtuality case");
1639   }
1640 
1641   return MethodKind::Vanilla;
1642 }
1643 
1644 static TypeRecordKind getRecordKind(const DICompositeType *Ty) {
1645   switch (Ty->getTag()) {
1646   case dwarf::DW_TAG_class_type:     return TypeRecordKind::Class;
1647   case dwarf::DW_TAG_structure_type: return TypeRecordKind::Struct;
1648   }
1649   llvm_unreachable("unexpected tag");
1650 }
1651 
1652 /// Return ClassOptions that should be present on both the forward declaration
1653 /// and the defintion of a tag type.
1654 static ClassOptions getCommonClassOptions(const DICompositeType *Ty) {
1655   ClassOptions CO = ClassOptions::None;
1656 
1657   // MSVC always sets this flag, even for local types. Clang doesn't always
1658   // appear to give every type a linkage name, which may be problematic for us.
1659   // FIXME: Investigate the consequences of not following them here.
1660   if (!Ty->getIdentifier().empty())
1661     CO |= ClassOptions::HasUniqueName;
1662 
1663   // Put the Nested flag on a type if it appears immediately inside a tag type.
1664   // Do not walk the scope chain. Do not attempt to compute ContainsNestedClass
1665   // here. That flag is only set on definitions, and not forward declarations.
1666   const DIScope *ImmediateScope = Ty->getScope().resolve();
1667   if (ImmediateScope && isa<DICompositeType>(ImmediateScope))
1668     CO |= ClassOptions::Nested;
1669 
1670   // Put the Scoped flag on function-local types.
1671   for (const DIScope *Scope = ImmediateScope; Scope != nullptr;
1672        Scope = Scope->getScope().resolve()) {
1673     if (isa<DISubprogram>(Scope)) {
1674       CO |= ClassOptions::Scoped;
1675       break;
1676     }
1677   }
1678 
1679   return CO;
1680 }
1681 
1682 TypeIndex CodeViewDebug::lowerTypeEnum(const DICompositeType *Ty) {
1683   ClassOptions CO = getCommonClassOptions(Ty);
1684   TypeIndex FTI;
1685   unsigned EnumeratorCount = 0;
1686 
1687   if (Ty->isForwardDecl()) {
1688     CO |= ClassOptions::ForwardReference;
1689   } else {
1690     ContinuationRecordBuilder ContinuationBuilder;
1691     ContinuationBuilder.begin(ContinuationRecordKind::FieldList);
1692     for (const DINode *Element : Ty->getElements()) {
1693       // We assume that the frontend provides all members in source declaration
1694       // order, which is what MSVC does.
1695       if (auto *Enumerator = dyn_cast_or_null<DIEnumerator>(Element)) {
1696         EnumeratorRecord ER(MemberAccess::Public,
1697                             APSInt::getUnsigned(Enumerator->getValue()),
1698                             Enumerator->getName());
1699         ContinuationBuilder.writeMemberType(ER);
1700         EnumeratorCount++;
1701       }
1702     }
1703     FTI = TypeTable.insertRecord(ContinuationBuilder);
1704   }
1705 
1706   std::string FullName = getFullyQualifiedName(Ty);
1707 
1708   EnumRecord ER(EnumeratorCount, CO, FTI, FullName, Ty->getIdentifier(),
1709                 getTypeIndex(Ty->getBaseType()));
1710   return TypeTable.writeLeafType(ER);
1711 }
1712 
1713 //===----------------------------------------------------------------------===//
1714 // ClassInfo
1715 //===----------------------------------------------------------------------===//
1716 
1717 struct llvm::ClassInfo {
1718   struct MemberInfo {
1719     const DIDerivedType *MemberTypeNode;
1720     uint64_t BaseOffset;
1721   };
1722   // [MemberInfo]
1723   using MemberList = std::vector<MemberInfo>;
1724 
1725   using MethodsList = TinyPtrVector<const DISubprogram *>;
1726   // MethodName -> MethodsList
1727   using MethodsMap = MapVector<MDString *, MethodsList>;
1728 
1729   /// Base classes.
1730   std::vector<const DIDerivedType *> Inheritance;
1731 
1732   /// Direct members.
1733   MemberList Members;
1734   // Direct overloaded methods gathered by name.
1735   MethodsMap Methods;
1736 
1737   TypeIndex VShapeTI;
1738 
1739   std::vector<const DIType *> NestedTypes;
1740 };
1741 
1742 void CodeViewDebug::clear() {
1743   assert(CurFn == nullptr);
1744   FileIdMap.clear();
1745   FnDebugInfo.clear();
1746   FileToFilepathMap.clear();
1747   LocalUDTs.clear();
1748   GlobalUDTs.clear();
1749   TypeIndices.clear();
1750   CompleteTypeIndices.clear();
1751 }
1752 
1753 void CodeViewDebug::collectMemberInfo(ClassInfo &Info,
1754                                       const DIDerivedType *DDTy) {
1755   if (!DDTy->getName().empty()) {
1756     Info.Members.push_back({DDTy, 0});
1757     return;
1758   }
1759   // An unnamed member must represent a nested struct or union. Add all the
1760   // indirect fields to the current record.
1761   assert((DDTy->getOffsetInBits() % 8) == 0 && "Unnamed bitfield member!");
1762   uint64_t Offset = DDTy->getOffsetInBits();
1763   const DIType *Ty = DDTy->getBaseType().resolve();
1764   const DICompositeType *DCTy = cast<DICompositeType>(Ty);
1765   ClassInfo NestedInfo = collectClassInfo(DCTy);
1766   for (const ClassInfo::MemberInfo &IndirectField : NestedInfo.Members)
1767     Info.Members.push_back(
1768         {IndirectField.MemberTypeNode, IndirectField.BaseOffset + Offset});
1769 }
1770 
1771 ClassInfo CodeViewDebug::collectClassInfo(const DICompositeType *Ty) {
1772   ClassInfo Info;
1773   // Add elements to structure type.
1774   DINodeArray Elements = Ty->getElements();
1775   for (auto *Element : Elements) {
1776     // We assume that the frontend provides all members in source declaration
1777     // order, which is what MSVC does.
1778     if (!Element)
1779       continue;
1780     if (auto *SP = dyn_cast<DISubprogram>(Element)) {
1781       Info.Methods[SP->getRawName()].push_back(SP);
1782     } else if (auto *DDTy = dyn_cast<DIDerivedType>(Element)) {
1783       if (DDTy->getTag() == dwarf::DW_TAG_member) {
1784         collectMemberInfo(Info, DDTy);
1785       } else if (DDTy->getTag() == dwarf::DW_TAG_inheritance) {
1786         Info.Inheritance.push_back(DDTy);
1787       } else if (DDTy->getTag() == dwarf::DW_TAG_pointer_type &&
1788                  DDTy->getName() == "__vtbl_ptr_type") {
1789         Info.VShapeTI = getTypeIndex(DDTy);
1790       } else if (DDTy->getTag() == dwarf::DW_TAG_typedef) {
1791         Info.NestedTypes.push_back(DDTy);
1792       } else if (DDTy->getTag() == dwarf::DW_TAG_friend) {
1793         // Ignore friend members. It appears that MSVC emitted info about
1794         // friends in the past, but modern versions do not.
1795       }
1796     } else if (auto *Composite = dyn_cast<DICompositeType>(Element)) {
1797       Info.NestedTypes.push_back(Composite);
1798     }
1799     // Skip other unrecognized kinds of elements.
1800   }
1801   return Info;
1802 }
1803 
1804 TypeIndex CodeViewDebug::lowerTypeClass(const DICompositeType *Ty) {
1805   // First, construct the forward decl.  Don't look into Ty to compute the
1806   // forward decl options, since it might not be available in all TUs.
1807   TypeRecordKind Kind = getRecordKind(Ty);
1808   ClassOptions CO =
1809       ClassOptions::ForwardReference | getCommonClassOptions(Ty);
1810   std::string FullName = getFullyQualifiedName(Ty);
1811   ClassRecord CR(Kind, 0, CO, TypeIndex(), TypeIndex(), TypeIndex(), 0,
1812                  FullName, Ty->getIdentifier());
1813   TypeIndex FwdDeclTI = TypeTable.writeLeafType(CR);
1814   if (!Ty->isForwardDecl())
1815     DeferredCompleteTypes.push_back(Ty);
1816   return FwdDeclTI;
1817 }
1818 
1819 TypeIndex CodeViewDebug::lowerCompleteTypeClass(const DICompositeType *Ty) {
1820   // Construct the field list and complete type record.
1821   TypeRecordKind Kind = getRecordKind(Ty);
1822   ClassOptions CO = getCommonClassOptions(Ty);
1823   TypeIndex FieldTI;
1824   TypeIndex VShapeTI;
1825   unsigned FieldCount;
1826   bool ContainsNestedClass;
1827   std::tie(FieldTI, VShapeTI, FieldCount, ContainsNestedClass) =
1828       lowerRecordFieldList(Ty);
1829 
1830   if (ContainsNestedClass)
1831     CO |= ClassOptions::ContainsNestedClass;
1832 
1833   std::string FullName = getFullyQualifiedName(Ty);
1834 
1835   uint64_t SizeInBytes = Ty->getSizeInBits() / 8;
1836 
1837   ClassRecord CR(Kind, FieldCount, CO, FieldTI, TypeIndex(), VShapeTI,
1838                  SizeInBytes, FullName, Ty->getIdentifier());
1839   TypeIndex ClassTI = TypeTable.writeLeafType(CR);
1840 
1841   if (const auto *File = Ty->getFile()) {
1842     StringIdRecord SIDR(TypeIndex(0x0), getFullFilepath(File));
1843     TypeIndex SIDI = TypeTable.writeLeafType(SIDR);
1844 
1845     UdtSourceLineRecord USLR(ClassTI, SIDI, Ty->getLine());
1846     TypeTable.writeLeafType(USLR);
1847   }
1848 
1849   addToUDTs(Ty);
1850 
1851   return ClassTI;
1852 }
1853 
1854 TypeIndex CodeViewDebug::lowerTypeUnion(const DICompositeType *Ty) {
1855   ClassOptions CO =
1856       ClassOptions::ForwardReference | getCommonClassOptions(Ty);
1857   std::string FullName = getFullyQualifiedName(Ty);
1858   UnionRecord UR(0, CO, TypeIndex(), 0, FullName, Ty->getIdentifier());
1859   TypeIndex FwdDeclTI = TypeTable.writeLeafType(UR);
1860   if (!Ty->isForwardDecl())
1861     DeferredCompleteTypes.push_back(Ty);
1862   return FwdDeclTI;
1863 }
1864 
1865 TypeIndex CodeViewDebug::lowerCompleteTypeUnion(const DICompositeType *Ty) {
1866   ClassOptions CO = ClassOptions::Sealed | getCommonClassOptions(Ty);
1867   TypeIndex FieldTI;
1868   unsigned FieldCount;
1869   bool ContainsNestedClass;
1870   std::tie(FieldTI, std::ignore, FieldCount, ContainsNestedClass) =
1871       lowerRecordFieldList(Ty);
1872 
1873   if (ContainsNestedClass)
1874     CO |= ClassOptions::ContainsNestedClass;
1875 
1876   uint64_t SizeInBytes = Ty->getSizeInBits() / 8;
1877   std::string FullName = getFullyQualifiedName(Ty);
1878 
1879   UnionRecord UR(FieldCount, CO, FieldTI, SizeInBytes, FullName,
1880                  Ty->getIdentifier());
1881   TypeIndex UnionTI = TypeTable.writeLeafType(UR);
1882 
1883   StringIdRecord SIR(TypeIndex(0x0), getFullFilepath(Ty->getFile()));
1884   TypeIndex SIRI = TypeTable.writeLeafType(SIR);
1885 
1886   UdtSourceLineRecord USLR(UnionTI, SIRI, Ty->getLine());
1887   TypeTable.writeLeafType(USLR);
1888 
1889   addToUDTs(Ty);
1890 
1891   return UnionTI;
1892 }
1893 
1894 std::tuple<TypeIndex, TypeIndex, unsigned, bool>
1895 CodeViewDebug::lowerRecordFieldList(const DICompositeType *Ty) {
1896   // Manually count members. MSVC appears to count everything that generates a
1897   // field list record. Each individual overload in a method overload group
1898   // contributes to this count, even though the overload group is a single field
1899   // list record.
1900   unsigned MemberCount = 0;
1901   ClassInfo Info = collectClassInfo(Ty);
1902   ContinuationRecordBuilder ContinuationBuilder;
1903   ContinuationBuilder.begin(ContinuationRecordKind::FieldList);
1904 
1905   // Create base classes.
1906   for (const DIDerivedType *I : Info.Inheritance) {
1907     if (I->getFlags() & DINode::FlagVirtual) {
1908       // Virtual base.
1909       // FIXME: Emit VBPtrOffset when the frontend provides it.
1910       unsigned VBPtrOffset = 0;
1911       // FIXME: Despite the accessor name, the offset is really in bytes.
1912       unsigned VBTableIndex = I->getOffsetInBits() / 4;
1913       auto RecordKind = (I->getFlags() & DINode::FlagIndirectVirtualBase) == DINode::FlagIndirectVirtualBase
1914                             ? TypeRecordKind::IndirectVirtualBaseClass
1915                             : TypeRecordKind::VirtualBaseClass;
1916       VirtualBaseClassRecord VBCR(
1917           RecordKind, translateAccessFlags(Ty->getTag(), I->getFlags()),
1918           getTypeIndex(I->getBaseType()), getVBPTypeIndex(), VBPtrOffset,
1919           VBTableIndex);
1920 
1921       ContinuationBuilder.writeMemberType(VBCR);
1922     } else {
1923       assert(I->getOffsetInBits() % 8 == 0 &&
1924              "bases must be on byte boundaries");
1925       BaseClassRecord BCR(translateAccessFlags(Ty->getTag(), I->getFlags()),
1926                           getTypeIndex(I->getBaseType()),
1927                           I->getOffsetInBits() / 8);
1928       ContinuationBuilder.writeMemberType(BCR);
1929     }
1930   }
1931 
1932   // Create members.
1933   for (ClassInfo::MemberInfo &MemberInfo : Info.Members) {
1934     const DIDerivedType *Member = MemberInfo.MemberTypeNode;
1935     TypeIndex MemberBaseType = getTypeIndex(Member->getBaseType());
1936     StringRef MemberName = Member->getName();
1937     MemberAccess Access =
1938         translateAccessFlags(Ty->getTag(), Member->getFlags());
1939 
1940     if (Member->isStaticMember()) {
1941       StaticDataMemberRecord SDMR(Access, MemberBaseType, MemberName);
1942       ContinuationBuilder.writeMemberType(SDMR);
1943       MemberCount++;
1944       continue;
1945     }
1946 
1947     // Virtual function pointer member.
1948     if ((Member->getFlags() & DINode::FlagArtificial) &&
1949         Member->getName().startswith("_vptr$")) {
1950       VFPtrRecord VFPR(getTypeIndex(Member->getBaseType()));
1951       ContinuationBuilder.writeMemberType(VFPR);
1952       MemberCount++;
1953       continue;
1954     }
1955 
1956     // Data member.
1957     uint64_t MemberOffsetInBits =
1958         Member->getOffsetInBits() + MemberInfo.BaseOffset;
1959     if (Member->isBitField()) {
1960       uint64_t StartBitOffset = MemberOffsetInBits;
1961       if (const auto *CI =
1962               dyn_cast_or_null<ConstantInt>(Member->getStorageOffsetInBits())) {
1963         MemberOffsetInBits = CI->getZExtValue() + MemberInfo.BaseOffset;
1964       }
1965       StartBitOffset -= MemberOffsetInBits;
1966       BitFieldRecord BFR(MemberBaseType, Member->getSizeInBits(),
1967                          StartBitOffset);
1968       MemberBaseType = TypeTable.writeLeafType(BFR);
1969     }
1970     uint64_t MemberOffsetInBytes = MemberOffsetInBits / 8;
1971     DataMemberRecord DMR(Access, MemberBaseType, MemberOffsetInBytes,
1972                          MemberName);
1973     ContinuationBuilder.writeMemberType(DMR);
1974     MemberCount++;
1975   }
1976 
1977   // Create methods
1978   for (auto &MethodItr : Info.Methods) {
1979     StringRef Name = MethodItr.first->getString();
1980 
1981     std::vector<OneMethodRecord> Methods;
1982     for (const DISubprogram *SP : MethodItr.second) {
1983       TypeIndex MethodType = getMemberFunctionType(SP, Ty);
1984       bool Introduced = SP->getFlags() & DINode::FlagIntroducedVirtual;
1985 
1986       unsigned VFTableOffset = -1;
1987       if (Introduced)
1988         VFTableOffset = SP->getVirtualIndex() * getPointerSizeInBytes();
1989 
1990       Methods.push_back(OneMethodRecord(
1991           MethodType, translateAccessFlags(Ty->getTag(), SP->getFlags()),
1992           translateMethodKindFlags(SP, Introduced),
1993           translateMethodOptionFlags(SP), VFTableOffset, Name));
1994       MemberCount++;
1995     }
1996     assert(!Methods.empty() && "Empty methods map entry");
1997     if (Methods.size() == 1)
1998       ContinuationBuilder.writeMemberType(Methods[0]);
1999     else {
2000       // FIXME: Make this use its own ContinuationBuilder so that
2001       // MethodOverloadList can be split correctly.
2002       MethodOverloadListRecord MOLR(Methods);
2003       TypeIndex MethodList = TypeTable.writeLeafType(MOLR);
2004 
2005       OverloadedMethodRecord OMR(Methods.size(), MethodList, Name);
2006       ContinuationBuilder.writeMemberType(OMR);
2007     }
2008   }
2009 
2010   // Create nested classes.
2011   for (const DIType *Nested : Info.NestedTypes) {
2012     NestedTypeRecord R(getTypeIndex(DITypeRef(Nested)), Nested->getName());
2013     ContinuationBuilder.writeMemberType(R);
2014     MemberCount++;
2015   }
2016 
2017   TypeIndex FieldTI = TypeTable.insertRecord(ContinuationBuilder);
2018   return std::make_tuple(FieldTI, Info.VShapeTI, MemberCount,
2019                          !Info.NestedTypes.empty());
2020 }
2021 
2022 TypeIndex CodeViewDebug::getVBPTypeIndex() {
2023   if (!VBPType.getIndex()) {
2024     // Make a 'const int *' type.
2025     ModifierRecord MR(TypeIndex::Int32(), ModifierOptions::Const);
2026     TypeIndex ModifiedTI = TypeTable.writeLeafType(MR);
2027 
2028     PointerKind PK = getPointerSizeInBytes() == 8 ? PointerKind::Near64
2029                                                   : PointerKind::Near32;
2030     PointerMode PM = PointerMode::Pointer;
2031     PointerOptions PO = PointerOptions::None;
2032     PointerRecord PR(ModifiedTI, PK, PM, PO, getPointerSizeInBytes());
2033     VBPType = TypeTable.writeLeafType(PR);
2034   }
2035 
2036   return VBPType;
2037 }
2038 
2039 TypeIndex CodeViewDebug::getTypeIndex(DITypeRef TypeRef, DITypeRef ClassTyRef) {
2040   const DIType *Ty = TypeRef.resolve();
2041   const DIType *ClassTy = ClassTyRef.resolve();
2042 
2043   // The null DIType is the void type. Don't try to hash it.
2044   if (!Ty)
2045     return TypeIndex::Void();
2046 
2047   // Check if we've already translated this type. Don't try to do a
2048   // get-or-create style insertion that caches the hash lookup across the
2049   // lowerType call. It will update the TypeIndices map.
2050   auto I = TypeIndices.find({Ty, ClassTy});
2051   if (I != TypeIndices.end())
2052     return I->second;
2053 
2054   TypeLoweringScope S(*this);
2055   TypeIndex TI = lowerType(Ty, ClassTy);
2056   return recordTypeIndexForDINode(Ty, TI, ClassTy);
2057 }
2058 
2059 TypeIndex CodeViewDebug::getTypeIndexForReferenceTo(DITypeRef TypeRef) {
2060   DIType *Ty = TypeRef.resolve();
2061   PointerRecord PR(getTypeIndex(Ty),
2062                    getPointerSizeInBytes() == 8 ? PointerKind::Near64
2063                                                 : PointerKind::Near32,
2064                    PointerMode::LValueReference, PointerOptions::None,
2065                    Ty->getSizeInBits() / 8);
2066   return TypeTable.writeLeafType(PR);
2067 }
2068 
2069 TypeIndex CodeViewDebug::getCompleteTypeIndex(DITypeRef TypeRef) {
2070   const DIType *Ty = TypeRef.resolve();
2071 
2072   // The null DIType is the void type. Don't try to hash it.
2073   if (!Ty)
2074     return TypeIndex::Void();
2075 
2076   // If this is a non-record type, the complete type index is the same as the
2077   // normal type index. Just call getTypeIndex.
2078   switch (Ty->getTag()) {
2079   case dwarf::DW_TAG_class_type:
2080   case dwarf::DW_TAG_structure_type:
2081   case dwarf::DW_TAG_union_type:
2082     break;
2083   default:
2084     return getTypeIndex(Ty);
2085   }
2086 
2087   // Check if we've already translated the complete record type.  Lowering a
2088   // complete type should never trigger lowering another complete type, so we
2089   // can reuse the hash table lookup result.
2090   const auto *CTy = cast<DICompositeType>(Ty);
2091   auto InsertResult = CompleteTypeIndices.insert({CTy, TypeIndex()});
2092   if (!InsertResult.second)
2093     return InsertResult.first->second;
2094 
2095   TypeLoweringScope S(*this);
2096 
2097   // Make sure the forward declaration is emitted first. It's unclear if this
2098   // is necessary, but MSVC does it, and we should follow suit until we can show
2099   // otherwise.
2100   TypeIndex FwdDeclTI = getTypeIndex(CTy);
2101 
2102   // Just use the forward decl if we don't have complete type info. This might
2103   // happen if the frontend is using modules and expects the complete definition
2104   // to be emitted elsewhere.
2105   if (CTy->isForwardDecl())
2106     return FwdDeclTI;
2107 
2108   TypeIndex TI;
2109   switch (CTy->getTag()) {
2110   case dwarf::DW_TAG_class_type:
2111   case dwarf::DW_TAG_structure_type:
2112     TI = lowerCompleteTypeClass(CTy);
2113     break;
2114   case dwarf::DW_TAG_union_type:
2115     TI = lowerCompleteTypeUnion(CTy);
2116     break;
2117   default:
2118     llvm_unreachable("not a record");
2119   }
2120 
2121   InsertResult.first->second = TI;
2122   return TI;
2123 }
2124 
2125 /// Emit all the deferred complete record types. Try to do this in FIFO order,
2126 /// and do this until fixpoint, as each complete record type typically
2127 /// references
2128 /// many other record types.
2129 void CodeViewDebug::emitDeferredCompleteTypes() {
2130   SmallVector<const DICompositeType *, 4> TypesToEmit;
2131   while (!DeferredCompleteTypes.empty()) {
2132     std::swap(DeferredCompleteTypes, TypesToEmit);
2133     for (const DICompositeType *RecordTy : TypesToEmit)
2134       getCompleteTypeIndex(RecordTy);
2135     TypesToEmit.clear();
2136   }
2137 }
2138 
2139 void CodeViewDebug::emitLocalVariableList(ArrayRef<LocalVariable> Locals) {
2140   // Get the sorted list of parameters and emit them first.
2141   SmallVector<const LocalVariable *, 6> Params;
2142   for (const LocalVariable &L : Locals)
2143     if (L.DIVar->isParameter())
2144       Params.push_back(&L);
2145   std::sort(Params.begin(), Params.end(),
2146             [](const LocalVariable *L, const LocalVariable *R) {
2147               return L->DIVar->getArg() < R->DIVar->getArg();
2148             });
2149   for (const LocalVariable *L : Params)
2150     emitLocalVariable(*L);
2151 
2152   // Next emit all non-parameters in the order that we found them.
2153   for (const LocalVariable &L : Locals)
2154     if (!L.DIVar->isParameter())
2155       emitLocalVariable(L);
2156 }
2157 
2158 void CodeViewDebug::emitLocalVariable(const LocalVariable &Var) {
2159   // LocalSym record, see SymbolRecord.h for more info.
2160   MCSymbol *LocalBegin = MMI->getContext().createTempSymbol(),
2161            *LocalEnd = MMI->getContext().createTempSymbol();
2162   OS.AddComment("Record length");
2163   OS.emitAbsoluteSymbolDiff(LocalEnd, LocalBegin, 2);
2164   OS.EmitLabel(LocalBegin);
2165 
2166   OS.AddComment("Record kind: S_LOCAL");
2167   OS.EmitIntValue(unsigned(SymbolKind::S_LOCAL), 2);
2168 
2169   LocalSymFlags Flags = LocalSymFlags::None;
2170   if (Var.DIVar->isParameter())
2171     Flags |= LocalSymFlags::IsParameter;
2172   if (Var.DefRanges.empty())
2173     Flags |= LocalSymFlags::IsOptimizedOut;
2174 
2175   OS.AddComment("TypeIndex");
2176   TypeIndex TI = Var.UseReferenceType
2177                      ? getTypeIndexForReferenceTo(Var.DIVar->getType())
2178                      : getCompleteTypeIndex(Var.DIVar->getType());
2179   OS.EmitIntValue(TI.getIndex(), 4);
2180   OS.AddComment("Flags");
2181   OS.EmitIntValue(static_cast<uint16_t>(Flags), 2);
2182   // Truncate the name so we won't overflow the record length field.
2183   emitNullTerminatedSymbolName(OS, Var.DIVar->getName());
2184   OS.EmitLabel(LocalEnd);
2185 
2186   // Calculate the on disk prefix of the appropriate def range record. The
2187   // records and on disk formats are described in SymbolRecords.h. BytePrefix
2188   // should be big enough to hold all forms without memory allocation.
2189   SmallString<20> BytePrefix;
2190   for (const LocalVarDefRange &DefRange : Var.DefRanges) {
2191     BytePrefix.clear();
2192     if (DefRange.InMemory) {
2193       uint16_t RegRelFlags = 0;
2194       if (DefRange.IsSubfield) {
2195         RegRelFlags = DefRangeRegisterRelSym::IsSubfieldFlag |
2196                       (DefRange.StructOffset
2197                        << DefRangeRegisterRelSym::OffsetInParentShift);
2198       }
2199       DefRangeRegisterRelSym Sym(S_DEFRANGE_REGISTER_REL);
2200       Sym.Hdr.Register = DefRange.CVRegister;
2201       Sym.Hdr.Flags = RegRelFlags;
2202       Sym.Hdr.BasePointerOffset = DefRange.DataOffset;
2203       ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER_REL);
2204       BytePrefix +=
2205           StringRef(reinterpret_cast<const char *>(&SymKind), sizeof(SymKind));
2206       BytePrefix +=
2207           StringRef(reinterpret_cast<const char *>(&Sym.Hdr), sizeof(Sym.Hdr));
2208     } else {
2209       assert(DefRange.DataOffset == 0 && "unexpected offset into register");
2210       if (DefRange.IsSubfield) {
2211         // Unclear what matters here.
2212         DefRangeSubfieldRegisterSym Sym(S_DEFRANGE_SUBFIELD_REGISTER);
2213         Sym.Hdr.Register = DefRange.CVRegister;
2214         Sym.Hdr.MayHaveNoName = 0;
2215         Sym.Hdr.OffsetInParent = DefRange.StructOffset;
2216 
2217         ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_SUBFIELD_REGISTER);
2218         BytePrefix += StringRef(reinterpret_cast<const char *>(&SymKind),
2219                                 sizeof(SymKind));
2220         BytePrefix += StringRef(reinterpret_cast<const char *>(&Sym.Hdr),
2221                                 sizeof(Sym.Hdr));
2222       } else {
2223         // Unclear what matters here.
2224         DefRangeRegisterSym Sym(S_DEFRANGE_REGISTER);
2225         Sym.Hdr.Register = DefRange.CVRegister;
2226         Sym.Hdr.MayHaveNoName = 0;
2227         ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER);
2228         BytePrefix += StringRef(reinterpret_cast<const char *>(&SymKind),
2229                                 sizeof(SymKind));
2230         BytePrefix += StringRef(reinterpret_cast<const char *>(&Sym.Hdr),
2231                                 sizeof(Sym.Hdr));
2232       }
2233     }
2234     OS.EmitCVDefRangeDirective(DefRange.Ranges, BytePrefix);
2235   }
2236 }
2237 
2238 void CodeViewDebug::endFunctionImpl(const MachineFunction *MF) {
2239   const Function *GV = MF->getFunction();
2240   assert(FnDebugInfo.count(GV));
2241   assert(CurFn == &FnDebugInfo[GV]);
2242 
2243   collectVariableInfo(GV->getSubprogram());
2244 
2245   // Don't emit anything if we don't have any line tables.
2246   if (!CurFn->HaveLineInfo) {
2247     FnDebugInfo.erase(GV);
2248     CurFn = nullptr;
2249     return;
2250   }
2251 
2252   CurFn->Annotations = MF->getCodeViewAnnotations();
2253 
2254   CurFn->End = Asm->getFunctionEnd();
2255 
2256   CurFn = nullptr;
2257 }
2258 
2259 void CodeViewDebug::beginInstruction(const MachineInstr *MI) {
2260   DebugHandlerBase::beginInstruction(MI);
2261 
2262   // Ignore DBG_VALUE locations and function prologue.
2263   if (!Asm || !CurFn || MI->isDebugValue() ||
2264       MI->getFlag(MachineInstr::FrameSetup))
2265     return;
2266 
2267   // If the first instruction of a new MBB has no location, find the first
2268   // instruction with a location and use that.
2269   DebugLoc DL = MI->getDebugLoc();
2270   if (!DL && MI->getParent() != PrevInstBB) {
2271     for (const auto &NextMI : *MI->getParent()) {
2272       if (NextMI.isDebugValue())
2273         continue;
2274       DL = NextMI.getDebugLoc();
2275       if (DL)
2276         break;
2277     }
2278   }
2279   PrevInstBB = MI->getParent();
2280 
2281   // If we still don't have a debug location, don't record a location.
2282   if (!DL)
2283     return;
2284 
2285   maybeRecordLocation(DL, Asm->MF);
2286 }
2287 
2288 MCSymbol *CodeViewDebug::beginCVSubsection(DebugSubsectionKind Kind) {
2289   MCSymbol *BeginLabel = MMI->getContext().createTempSymbol(),
2290            *EndLabel = MMI->getContext().createTempSymbol();
2291   OS.EmitIntValue(unsigned(Kind), 4);
2292   OS.AddComment("Subsection size");
2293   OS.emitAbsoluteSymbolDiff(EndLabel, BeginLabel, 4);
2294   OS.EmitLabel(BeginLabel);
2295   return EndLabel;
2296 }
2297 
2298 void CodeViewDebug::endCVSubsection(MCSymbol *EndLabel) {
2299   OS.EmitLabel(EndLabel);
2300   // Every subsection must be aligned to a 4-byte boundary.
2301   OS.EmitValueToAlignment(4);
2302 }
2303 
2304 void CodeViewDebug::emitDebugInfoForUDTs(
2305     ArrayRef<std::pair<std::string, const DIType *>> UDTs) {
2306   for (const auto &UDT : UDTs) {
2307     const DIType *T = UDT.second;
2308     assert(shouldEmitUdt(T));
2309 
2310     MCSymbol *UDTRecordBegin = MMI->getContext().createTempSymbol(),
2311              *UDTRecordEnd = MMI->getContext().createTempSymbol();
2312     OS.AddComment("Record length");
2313     OS.emitAbsoluteSymbolDiff(UDTRecordEnd, UDTRecordBegin, 2);
2314     OS.EmitLabel(UDTRecordBegin);
2315 
2316     OS.AddComment("Record kind: S_UDT");
2317     OS.EmitIntValue(unsigned(SymbolKind::S_UDT), 2);
2318 
2319     OS.AddComment("Type");
2320     OS.EmitIntValue(getCompleteTypeIndex(T).getIndex(), 4);
2321 
2322     emitNullTerminatedSymbolName(OS, UDT.first);
2323     OS.EmitLabel(UDTRecordEnd);
2324   }
2325 }
2326 
2327 void CodeViewDebug::emitDebugInfoForGlobals() {
2328   DenseMap<const DIGlobalVariableExpression *, const GlobalVariable *>
2329       GlobalMap;
2330   for (const GlobalVariable &GV : MMI->getModule()->globals()) {
2331     SmallVector<DIGlobalVariableExpression *, 1> GVEs;
2332     GV.getDebugInfo(GVEs);
2333     for (const auto *GVE : GVEs)
2334       GlobalMap[GVE] = &GV;
2335   }
2336 
2337   NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
2338   for (const MDNode *Node : CUs->operands()) {
2339     const auto *CU = cast<DICompileUnit>(Node);
2340 
2341     // First, emit all globals that are not in a comdat in a single symbol
2342     // substream. MSVC doesn't like it if the substream is empty, so only open
2343     // it if we have at least one global to emit.
2344     switchToDebugSectionForSymbol(nullptr);
2345     MCSymbol *EndLabel = nullptr;
2346     for (const auto *GVE : CU->getGlobalVariables()) {
2347       if (const auto *GV = GlobalMap.lookup(GVE))
2348         if (!GV->hasComdat() && !GV->isDeclarationForLinker()) {
2349           if (!EndLabel) {
2350             OS.AddComment("Symbol subsection for globals");
2351             EndLabel = beginCVSubsection(DebugSubsectionKind::Symbols);
2352           }
2353           // FIXME: emitDebugInfoForGlobal() doesn't handle DIExpressions.
2354           emitDebugInfoForGlobal(GVE->getVariable(), GV, Asm->getSymbol(GV));
2355         }
2356     }
2357     if (EndLabel)
2358       endCVSubsection(EndLabel);
2359 
2360     // Second, emit each global that is in a comdat into its own .debug$S
2361     // section along with its own symbol substream.
2362     for (const auto *GVE : CU->getGlobalVariables()) {
2363       if (const auto *GV = GlobalMap.lookup(GVE)) {
2364         if (GV->hasComdat()) {
2365           MCSymbol *GVSym = Asm->getSymbol(GV);
2366           OS.AddComment("Symbol subsection for " +
2367                         Twine(GlobalValue::dropLLVMManglingEscape(GV->getName())));
2368           switchToDebugSectionForSymbol(GVSym);
2369           EndLabel = beginCVSubsection(DebugSubsectionKind::Symbols);
2370           // FIXME: emitDebugInfoForGlobal() doesn't handle DIExpressions.
2371           emitDebugInfoForGlobal(GVE->getVariable(), GV, GVSym);
2372           endCVSubsection(EndLabel);
2373         }
2374       }
2375     }
2376   }
2377 }
2378 
2379 void CodeViewDebug::emitDebugInfoForRetainedTypes() {
2380   NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
2381   for (const MDNode *Node : CUs->operands()) {
2382     for (auto *Ty : cast<DICompileUnit>(Node)->getRetainedTypes()) {
2383       if (DIType *RT = dyn_cast<DIType>(Ty)) {
2384         getTypeIndex(RT);
2385         // FIXME: Add to global/local DTU list.
2386       }
2387     }
2388   }
2389 }
2390 
2391 void CodeViewDebug::emitDebugInfoForGlobal(const DIGlobalVariable *DIGV,
2392                                            const GlobalVariable *GV,
2393                                            MCSymbol *GVSym) {
2394   // DataSym record, see SymbolRecord.h for more info.
2395   // FIXME: Thread local data, etc
2396   MCSymbol *DataBegin = MMI->getContext().createTempSymbol(),
2397            *DataEnd = MMI->getContext().createTempSymbol();
2398   OS.AddComment("Record length");
2399   OS.emitAbsoluteSymbolDiff(DataEnd, DataBegin, 2);
2400   OS.EmitLabel(DataBegin);
2401   if (DIGV->isLocalToUnit()) {
2402     if (GV->isThreadLocal()) {
2403       OS.AddComment("Record kind: S_LTHREAD32");
2404       OS.EmitIntValue(unsigned(SymbolKind::S_LTHREAD32), 2);
2405     } else {
2406       OS.AddComment("Record kind: S_LDATA32");
2407       OS.EmitIntValue(unsigned(SymbolKind::S_LDATA32), 2);
2408     }
2409   } else {
2410     if (GV->isThreadLocal()) {
2411       OS.AddComment("Record kind: S_GTHREAD32");
2412       OS.EmitIntValue(unsigned(SymbolKind::S_GTHREAD32), 2);
2413     } else {
2414       OS.AddComment("Record kind: S_GDATA32");
2415       OS.EmitIntValue(unsigned(SymbolKind::S_GDATA32), 2);
2416     }
2417   }
2418   OS.AddComment("Type");
2419   OS.EmitIntValue(getCompleteTypeIndex(DIGV->getType()).getIndex(), 4);
2420   OS.AddComment("DataOffset");
2421   OS.EmitCOFFSecRel32(GVSym, /*Offset=*/0);
2422   OS.AddComment("Segment");
2423   OS.EmitCOFFSectionIndex(GVSym);
2424   OS.AddComment("Name");
2425   emitNullTerminatedSymbolName(OS, DIGV->getName());
2426   OS.EmitLabel(DataEnd);
2427 }
2428