xref: /llvm-project/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp (revision 9aa378d89ef0027b72f37c1e8fb4998d891e34e6)
1 //===- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp ----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains support for writing Microsoft CodeView debug info.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "CodeViewDebug.h"
14 #include "llvm/ADT/APSInt.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallBitVector.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/TinyPtrVector.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/BinaryFormat/COFF.h"
22 #include "llvm/BinaryFormat/Dwarf.h"
23 #include "llvm/CodeGen/AsmPrinter.h"
24 #include "llvm/CodeGen/LexicalScopes.h"
25 #include "llvm/CodeGen/MachineFrameInfo.h"
26 #include "llvm/CodeGen/MachineFunction.h"
27 #include "llvm/CodeGen/MachineInstr.h"
28 #include "llvm/CodeGen/MachineModuleInfo.h"
29 #include "llvm/CodeGen/TargetFrameLowering.h"
30 #include "llvm/CodeGen/TargetLowering.h"
31 #include "llvm/CodeGen/TargetRegisterInfo.h"
32 #include "llvm/CodeGen/TargetSubtargetInfo.h"
33 #include "llvm/Config/llvm-config.h"
34 #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
35 #include "llvm/DebugInfo/CodeView/CodeViewRecordIO.h"
36 #include "llvm/DebugInfo/CodeView/ContinuationRecordBuilder.h"
37 #include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h"
38 #include "llvm/DebugInfo/CodeView/EnumTables.h"
39 #include "llvm/DebugInfo/CodeView/Line.h"
40 #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
41 #include "llvm/DebugInfo/CodeView/TypeRecord.h"
42 #include "llvm/DebugInfo/CodeView/TypeTableCollection.h"
43 #include "llvm/DebugInfo/CodeView/TypeVisitorCallbackPipeline.h"
44 #include "llvm/IR/Constants.h"
45 #include "llvm/IR/DataLayout.h"
46 #include "llvm/IR/DebugInfoMetadata.h"
47 #include "llvm/IR/Function.h"
48 #include "llvm/IR/GlobalValue.h"
49 #include "llvm/IR/GlobalVariable.h"
50 #include "llvm/IR/Metadata.h"
51 #include "llvm/IR/Module.h"
52 #include "llvm/MC/MCAsmInfo.h"
53 #include "llvm/MC/MCContext.h"
54 #include "llvm/MC/MCSectionCOFF.h"
55 #include "llvm/MC/MCStreamer.h"
56 #include "llvm/MC/MCSymbol.h"
57 #include "llvm/Support/BinaryStreamWriter.h"
58 #include "llvm/Support/Casting.h"
59 #include "llvm/Support/Endian.h"
60 #include "llvm/Support/Error.h"
61 #include "llvm/Support/ErrorHandling.h"
62 #include "llvm/Support/FormatVariadic.h"
63 #include "llvm/Support/Path.h"
64 #include "llvm/Support/Program.h"
65 #include "llvm/Support/SMLoc.h"
66 #include "llvm/Support/ScopedPrinter.h"
67 #include "llvm/Target/TargetLoweringObjectFile.h"
68 #include "llvm/Target/TargetMachine.h"
69 #include "llvm/TargetParser/Triple.h"
70 #include <algorithm>
71 #include <cassert>
72 #include <cctype>
73 #include <cstddef>
74 #include <iterator>
75 #include <limits>
76 
77 using namespace llvm;
78 using namespace llvm::codeview;
79 
80 namespace {
81 class CVMCAdapter : public CodeViewRecordStreamer {
82 public:
83   CVMCAdapter(MCStreamer &OS, TypeCollection &TypeTable)
84       : OS(&OS), TypeTable(TypeTable) {}
85 
86   void emitBytes(StringRef Data) override { OS->emitBytes(Data); }
87 
88   void emitIntValue(uint64_t Value, unsigned Size) override {
89     OS->emitIntValueInHex(Value, Size);
90   }
91 
92   void emitBinaryData(StringRef Data) override { OS->emitBinaryData(Data); }
93 
94   void AddComment(const Twine &T) override { OS->AddComment(T); }
95 
96   void AddRawComment(const Twine &T) override { OS->emitRawComment(T); }
97 
98   bool isVerboseAsm() override { return OS->isVerboseAsm(); }
99 
100   std::string getTypeName(TypeIndex TI) override {
101     std::string TypeName;
102     if (!TI.isNoneType()) {
103       if (TI.isSimple())
104         TypeName = std::string(TypeIndex::simpleTypeName(TI));
105       else
106         TypeName = std::string(TypeTable.getTypeName(TI));
107     }
108     return TypeName;
109   }
110 
111 private:
112   MCStreamer *OS = nullptr;
113   TypeCollection &TypeTable;
114 };
115 } // namespace
116 
117 static CPUType mapArchToCVCPUType(Triple::ArchType Type) {
118   switch (Type) {
119   case Triple::ArchType::x86:
120     return CPUType::Pentium3;
121   case Triple::ArchType::x86_64:
122     return CPUType::X64;
123   case Triple::ArchType::thumb:
124     // LLVM currently doesn't support Windows CE and so thumb
125     // here is indiscriminately mapped to ARMNT specifically.
126     return CPUType::ARMNT;
127   case Triple::ArchType::aarch64:
128     return CPUType::ARM64;
129   default:
130     report_fatal_error("target architecture doesn't map to a CodeView CPUType");
131   }
132 }
133 
134 CodeViewDebug::CodeViewDebug(AsmPrinter *AP)
135     : DebugHandlerBase(AP), OS(*Asm->OutStreamer), TypeTable(Allocator) {}
136 
137 StringRef CodeViewDebug::getFullFilepath(const DIFile *File) {
138   std::string &Filepath = FileToFilepathMap[File];
139   if (!Filepath.empty())
140     return Filepath;
141 
142   StringRef Dir = File->getDirectory(), Filename = File->getFilename();
143 
144   // If this is a Unix-style path, just use it as is. Don't try to canonicalize
145   // it textually because one of the path components could be a symlink.
146   if (Dir.startswith("/") || Filename.startswith("/")) {
147     if (llvm::sys::path::is_absolute(Filename, llvm::sys::path::Style::posix))
148       return Filename;
149     Filepath = std::string(Dir);
150     if (Dir.back() != '/')
151       Filepath += '/';
152     Filepath += Filename;
153     return Filepath;
154   }
155 
156   // Clang emits directory and relative filename info into the IR, but CodeView
157   // operates on full paths.  We could change Clang to emit full paths too, but
158   // that would increase the IR size and probably not needed for other users.
159   // For now, just concatenate and canonicalize the path here.
160   if (Filename.find(':') == 1)
161     Filepath = std::string(Filename);
162   else
163     Filepath = (Dir + "\\" + Filename).str();
164 
165   // Canonicalize the path.  We have to do it textually because we may no longer
166   // have access the file in the filesystem.
167   // First, replace all slashes with backslashes.
168   std::replace(Filepath.begin(), Filepath.end(), '/', '\\');
169 
170   // Remove all "\.\" with "\".
171   size_t Cursor = 0;
172   while ((Cursor = Filepath.find("\\.\\", Cursor)) != std::string::npos)
173     Filepath.erase(Cursor, 2);
174 
175   // Replace all "\XXX\..\" with "\".  Don't try too hard though as the original
176   // path should be well-formatted, e.g. start with a drive letter, etc.
177   Cursor = 0;
178   while ((Cursor = Filepath.find("\\..\\", Cursor)) != std::string::npos) {
179     // Something's wrong if the path starts with "\..\", abort.
180     if (Cursor == 0)
181       break;
182 
183     size_t PrevSlash = Filepath.rfind('\\', Cursor - 1);
184     if (PrevSlash == std::string::npos)
185       // Something's wrong, abort.
186       break;
187 
188     Filepath.erase(PrevSlash, Cursor + 3 - PrevSlash);
189     // The next ".." might be following the one we've just erased.
190     Cursor = PrevSlash;
191   }
192 
193   // Remove all duplicate backslashes.
194   Cursor = 0;
195   while ((Cursor = Filepath.find("\\\\", Cursor)) != std::string::npos)
196     Filepath.erase(Cursor, 1);
197 
198   return Filepath;
199 }
200 
201 unsigned CodeViewDebug::maybeRecordFile(const DIFile *F) {
202   StringRef FullPath = getFullFilepath(F);
203   unsigned NextId = FileIdMap.size() + 1;
204   auto Insertion = FileIdMap.insert(std::make_pair(FullPath, NextId));
205   if (Insertion.second) {
206     // We have to compute the full filepath and emit a .cv_file directive.
207     ArrayRef<uint8_t> ChecksumAsBytes;
208     FileChecksumKind CSKind = FileChecksumKind::None;
209     if (F->getChecksum()) {
210       std::string Checksum = fromHex(F->getChecksum()->Value);
211       void *CKMem = OS.getContext().allocate(Checksum.size(), 1);
212       memcpy(CKMem, Checksum.data(), Checksum.size());
213       ChecksumAsBytes = ArrayRef<uint8_t>(
214           reinterpret_cast<const uint8_t *>(CKMem), Checksum.size());
215       switch (F->getChecksum()->Kind) {
216       case DIFile::CSK_MD5:
217         CSKind = FileChecksumKind::MD5;
218         break;
219       case DIFile::CSK_SHA1:
220         CSKind = FileChecksumKind::SHA1;
221         break;
222       case DIFile::CSK_SHA256:
223         CSKind = FileChecksumKind::SHA256;
224         break;
225       }
226     }
227     bool Success = OS.emitCVFileDirective(NextId, FullPath, ChecksumAsBytes,
228                                           static_cast<unsigned>(CSKind));
229     (void)Success;
230     assert(Success && ".cv_file directive failed");
231   }
232   return Insertion.first->second;
233 }
234 
235 CodeViewDebug::InlineSite &
236 CodeViewDebug::getInlineSite(const DILocation *InlinedAt,
237                              const DISubprogram *Inlinee) {
238   auto SiteInsertion = CurFn->InlineSites.insert({InlinedAt, InlineSite()});
239   InlineSite *Site = &SiteInsertion.first->second;
240   if (SiteInsertion.second) {
241     unsigned ParentFuncId = CurFn->FuncId;
242     if (const DILocation *OuterIA = InlinedAt->getInlinedAt())
243       ParentFuncId =
244           getInlineSite(OuterIA, InlinedAt->getScope()->getSubprogram())
245               .SiteFuncId;
246 
247     Site->SiteFuncId = NextFuncId++;
248     OS.emitCVInlineSiteIdDirective(
249         Site->SiteFuncId, ParentFuncId, maybeRecordFile(InlinedAt->getFile()),
250         InlinedAt->getLine(), InlinedAt->getColumn(), SMLoc());
251     Site->Inlinee = Inlinee;
252     InlinedSubprograms.insert(Inlinee);
253     auto InlineeIdx = getFuncIdForSubprogram(Inlinee);
254 
255     if (InlinedAt->getInlinedAt() == nullptr)
256       CurFn->Inlinees.insert(InlineeIdx);
257   }
258   return *Site;
259 }
260 
261 static StringRef getPrettyScopeName(const DIScope *Scope) {
262   StringRef ScopeName = Scope->getName();
263   if (!ScopeName.empty())
264     return ScopeName;
265 
266   switch (Scope->getTag()) {
267   case dwarf::DW_TAG_enumeration_type:
268   case dwarf::DW_TAG_class_type:
269   case dwarf::DW_TAG_structure_type:
270   case dwarf::DW_TAG_union_type:
271     return "<unnamed-tag>";
272   case dwarf::DW_TAG_namespace:
273     return "`anonymous namespace'";
274   default:
275     return StringRef();
276   }
277 }
278 
279 const DISubprogram *CodeViewDebug::collectParentScopeNames(
280     const DIScope *Scope, SmallVectorImpl<StringRef> &QualifiedNameComponents) {
281   const DISubprogram *ClosestSubprogram = nullptr;
282   while (Scope != nullptr) {
283     if (ClosestSubprogram == nullptr)
284       ClosestSubprogram = dyn_cast<DISubprogram>(Scope);
285 
286     // If a type appears in a scope chain, make sure it gets emitted. The
287     // frontend will be responsible for deciding if this should be a forward
288     // declaration or a complete type.
289     if (const auto *Ty = dyn_cast<DICompositeType>(Scope))
290       DeferredCompleteTypes.push_back(Ty);
291 
292     StringRef ScopeName = getPrettyScopeName(Scope);
293     if (!ScopeName.empty())
294       QualifiedNameComponents.push_back(ScopeName);
295     Scope = Scope->getScope();
296   }
297   return ClosestSubprogram;
298 }
299 
300 static std::string formatNestedName(ArrayRef<StringRef> QualifiedNameComponents,
301                                     StringRef TypeName) {
302   std::string FullyQualifiedName;
303   for (StringRef QualifiedNameComponent :
304        llvm::reverse(QualifiedNameComponents)) {
305     FullyQualifiedName.append(std::string(QualifiedNameComponent));
306     FullyQualifiedName.append("::");
307   }
308   FullyQualifiedName.append(std::string(TypeName));
309   return FullyQualifiedName;
310 }
311 
312 struct CodeViewDebug::TypeLoweringScope {
313   TypeLoweringScope(CodeViewDebug &CVD) : CVD(CVD) { ++CVD.TypeEmissionLevel; }
314   ~TypeLoweringScope() {
315     // Don't decrement TypeEmissionLevel until after emitting deferred types, so
316     // inner TypeLoweringScopes don't attempt to emit deferred types.
317     if (CVD.TypeEmissionLevel == 1)
318       CVD.emitDeferredCompleteTypes();
319     --CVD.TypeEmissionLevel;
320   }
321   CodeViewDebug &CVD;
322 };
323 
324 std::string CodeViewDebug::getFullyQualifiedName(const DIScope *Scope,
325                                                  StringRef Name) {
326   // Ensure types in the scope chain are emitted as soon as possible.
327   // This can create otherwise a situation where S_UDTs are emitted while
328   // looping in emitDebugInfoForUDTs.
329   TypeLoweringScope S(*this);
330   SmallVector<StringRef, 5> QualifiedNameComponents;
331   collectParentScopeNames(Scope, QualifiedNameComponents);
332   return formatNestedName(QualifiedNameComponents, Name);
333 }
334 
335 std::string CodeViewDebug::getFullyQualifiedName(const DIScope *Ty) {
336   const DIScope *Scope = Ty->getScope();
337   return getFullyQualifiedName(Scope, getPrettyScopeName(Ty));
338 }
339 
340 TypeIndex CodeViewDebug::getScopeIndex(const DIScope *Scope) {
341   // No scope means global scope and that uses the zero index.
342   //
343   // We also use zero index when the scope is a DISubprogram
344   // to suppress the emission of LF_STRING_ID for the function,
345   // which can trigger a link-time error with the linker in
346   // VS2019 version 16.11.2 or newer.
347   // Note, however, skipping the debug info emission for the DISubprogram
348   // is a temporary fix. The root issue here is that we need to figure out
349   // the proper way to encode a function nested in another function
350   // (as introduced by the Fortran 'contains' keyword) in CodeView.
351   if (!Scope || isa<DIFile>(Scope) || isa<DISubprogram>(Scope))
352     return TypeIndex();
353 
354   assert(!isa<DIType>(Scope) && "shouldn't make a namespace scope for a type");
355 
356   // Check if we've already translated this scope.
357   auto I = TypeIndices.find({Scope, nullptr});
358   if (I != TypeIndices.end())
359     return I->second;
360 
361   // Build the fully qualified name of the scope.
362   std::string ScopeName = getFullyQualifiedName(Scope);
363   StringIdRecord SID(TypeIndex(), ScopeName);
364   auto TI = TypeTable.writeLeafType(SID);
365   return recordTypeIndexForDINode(Scope, TI);
366 }
367 
368 static StringRef removeTemplateArgs(StringRef Name) {
369   // Remove template args from the display name. Assume that the template args
370   // are the last thing in the name.
371   if (Name.empty() || Name.back() != '>')
372     return Name;
373 
374   int OpenBrackets = 0;
375   for (int i = Name.size() - 1; i >= 0; --i) {
376     if (Name[i] == '>')
377       ++OpenBrackets;
378     else if (Name[i] == '<') {
379       --OpenBrackets;
380       if (OpenBrackets == 0)
381         return Name.substr(0, i);
382     }
383   }
384   return Name;
385 }
386 
387 TypeIndex CodeViewDebug::getFuncIdForSubprogram(const DISubprogram *SP) {
388   assert(SP);
389 
390   // Check if we've already translated this subprogram.
391   auto I = TypeIndices.find({SP, nullptr});
392   if (I != TypeIndices.end())
393     return I->second;
394 
395   // The display name includes function template arguments. Drop them to match
396   // MSVC. We need to have the template arguments in the DISubprogram name
397   // because they are used in other symbol records, such as S_GPROC32_IDs.
398   StringRef DisplayName = removeTemplateArgs(SP->getName());
399 
400   const DIScope *Scope = SP->getScope();
401   TypeIndex TI;
402   if (const auto *Class = dyn_cast_or_null<DICompositeType>(Scope)) {
403     // If the scope is a DICompositeType, then this must be a method. Member
404     // function types take some special handling, and require access to the
405     // subprogram.
406     TypeIndex ClassType = getTypeIndex(Class);
407     MemberFuncIdRecord MFuncId(ClassType, getMemberFunctionType(SP, Class),
408                                DisplayName);
409     TI = TypeTable.writeLeafType(MFuncId);
410   } else {
411     // Otherwise, this must be a free function.
412     TypeIndex ParentScope = getScopeIndex(Scope);
413     FuncIdRecord FuncId(ParentScope, getTypeIndex(SP->getType()), DisplayName);
414     TI = TypeTable.writeLeafType(FuncId);
415   }
416 
417   return recordTypeIndexForDINode(SP, TI);
418 }
419 
420 static bool isNonTrivial(const DICompositeType *DCTy) {
421   return ((DCTy->getFlags() & DINode::FlagNonTrivial) == DINode::FlagNonTrivial);
422 }
423 
424 static FunctionOptions
425 getFunctionOptions(const DISubroutineType *Ty,
426                    const DICompositeType *ClassTy = nullptr,
427                    StringRef SPName = StringRef("")) {
428   FunctionOptions FO = FunctionOptions::None;
429   const DIType *ReturnTy = nullptr;
430   if (auto TypeArray = Ty->getTypeArray()) {
431     if (TypeArray.size())
432       ReturnTy = TypeArray[0];
433   }
434 
435   // Add CxxReturnUdt option to functions that return nontrivial record types
436   // or methods that return record types.
437   if (auto *ReturnDCTy = dyn_cast_or_null<DICompositeType>(ReturnTy))
438     if (isNonTrivial(ReturnDCTy) || ClassTy)
439       FO |= FunctionOptions::CxxReturnUdt;
440 
441   // DISubroutineType is unnamed. Use DISubprogram's i.e. SPName in comparison.
442   if (ClassTy && isNonTrivial(ClassTy) && SPName == ClassTy->getName()) {
443     FO |= FunctionOptions::Constructor;
444 
445   // TODO: put the FunctionOptions::ConstructorWithVirtualBases flag.
446 
447   }
448   return FO;
449 }
450 
451 TypeIndex CodeViewDebug::getMemberFunctionType(const DISubprogram *SP,
452                                                const DICompositeType *Class) {
453   // Always use the method declaration as the key for the function type. The
454   // method declaration contains the this adjustment.
455   if (SP->getDeclaration())
456     SP = SP->getDeclaration();
457   assert(!SP->getDeclaration() && "should use declaration as key");
458 
459   // Key the MemberFunctionRecord into the map as {SP, Class}. It won't collide
460   // with the MemberFuncIdRecord, which is keyed in as {SP, nullptr}.
461   auto I = TypeIndices.find({SP, Class});
462   if (I != TypeIndices.end())
463     return I->second;
464 
465   // Make sure complete type info for the class is emitted *after* the member
466   // function type, as the complete class type is likely to reference this
467   // member function type.
468   TypeLoweringScope S(*this);
469   const bool IsStaticMethod = (SP->getFlags() & DINode::FlagStaticMember) != 0;
470 
471   FunctionOptions FO = getFunctionOptions(SP->getType(), Class, SP->getName());
472   TypeIndex TI = lowerTypeMemberFunction(
473       SP->getType(), Class, SP->getThisAdjustment(), IsStaticMethod, FO);
474   return recordTypeIndexForDINode(SP, TI, Class);
475 }
476 
477 TypeIndex CodeViewDebug::recordTypeIndexForDINode(const DINode *Node,
478                                                   TypeIndex TI,
479                                                   const DIType *ClassTy) {
480   auto InsertResult = TypeIndices.insert({{Node, ClassTy}, TI});
481   (void)InsertResult;
482   assert(InsertResult.second && "DINode was already assigned a type index");
483   return TI;
484 }
485 
486 unsigned CodeViewDebug::getPointerSizeInBytes() {
487   return MMI->getModule()->getDataLayout().getPointerSizeInBits() / 8;
488 }
489 
490 void CodeViewDebug::recordLocalVariable(LocalVariable &&Var,
491                                         const LexicalScope *LS) {
492   if (const DILocation *InlinedAt = LS->getInlinedAt()) {
493     // This variable was inlined. Associate it with the InlineSite.
494     const DISubprogram *Inlinee = Var.DIVar->getScope()->getSubprogram();
495     InlineSite &Site = getInlineSite(InlinedAt, Inlinee);
496     Site.InlinedLocals.emplace_back(std::move(Var));
497   } else {
498     // This variable goes into the corresponding lexical scope.
499     ScopeVariables[LS].emplace_back(std::move(Var));
500   }
501 }
502 
503 static void addLocIfNotPresent(SmallVectorImpl<const DILocation *> &Locs,
504                                const DILocation *Loc) {
505   if (!llvm::is_contained(Locs, Loc))
506     Locs.push_back(Loc);
507 }
508 
509 void CodeViewDebug::maybeRecordLocation(const DebugLoc &DL,
510                                         const MachineFunction *MF) {
511   // Skip this instruction if it has the same location as the previous one.
512   if (!DL || DL == PrevInstLoc)
513     return;
514 
515   const DIScope *Scope = DL->getScope();
516   if (!Scope)
517     return;
518 
519   // Skip this line if it is longer than the maximum we can record.
520   LineInfo LI(DL.getLine(), DL.getLine(), /*IsStatement=*/true);
521   if (LI.getStartLine() != DL.getLine() || LI.isAlwaysStepInto() ||
522       LI.isNeverStepInto())
523     return;
524 
525   ColumnInfo CI(DL.getCol(), /*EndColumn=*/0);
526   if (CI.getStartColumn() != DL.getCol())
527     return;
528 
529   if (!CurFn->HaveLineInfo)
530     CurFn->HaveLineInfo = true;
531   unsigned FileId = 0;
532   if (PrevInstLoc.get() && PrevInstLoc->getFile() == DL->getFile())
533     FileId = CurFn->LastFileId;
534   else
535     FileId = CurFn->LastFileId = maybeRecordFile(DL->getFile());
536   PrevInstLoc = DL;
537 
538   unsigned FuncId = CurFn->FuncId;
539   if (const DILocation *SiteLoc = DL->getInlinedAt()) {
540     const DILocation *Loc = DL.get();
541 
542     // If this location was actually inlined from somewhere else, give it the ID
543     // of the inline call site.
544     FuncId =
545         getInlineSite(SiteLoc, Loc->getScope()->getSubprogram()).SiteFuncId;
546 
547     // Ensure we have links in the tree of inline call sites.
548     bool FirstLoc = true;
549     while ((SiteLoc = Loc->getInlinedAt())) {
550       InlineSite &Site =
551           getInlineSite(SiteLoc, Loc->getScope()->getSubprogram());
552       if (!FirstLoc)
553         addLocIfNotPresent(Site.ChildSites, Loc);
554       FirstLoc = false;
555       Loc = SiteLoc;
556     }
557     addLocIfNotPresent(CurFn->ChildSites, Loc);
558   }
559 
560   OS.emitCVLocDirective(FuncId, FileId, DL.getLine(), DL.getCol(),
561                         /*PrologueEnd=*/false, /*IsStmt=*/false,
562                         DL->getFilename(), SMLoc());
563 }
564 
565 void CodeViewDebug::emitCodeViewMagicVersion() {
566   OS.emitValueToAlignment(Align(4));
567   OS.AddComment("Debug section magic");
568   OS.emitInt32(COFF::DEBUG_SECTION_MAGIC);
569 }
570 
571 static SourceLanguage MapDWLangToCVLang(unsigned DWLang) {
572   switch (DWLang) {
573   case dwarf::DW_LANG_C:
574   case dwarf::DW_LANG_C89:
575   case dwarf::DW_LANG_C99:
576   case dwarf::DW_LANG_C11:
577     return SourceLanguage::C;
578   case dwarf::DW_LANG_C_plus_plus:
579   case dwarf::DW_LANG_C_plus_plus_03:
580   case dwarf::DW_LANG_C_plus_plus_11:
581   case dwarf::DW_LANG_C_plus_plus_14:
582     return SourceLanguage::Cpp;
583   case dwarf::DW_LANG_Fortran77:
584   case dwarf::DW_LANG_Fortran90:
585   case dwarf::DW_LANG_Fortran95:
586   case dwarf::DW_LANG_Fortran03:
587   case dwarf::DW_LANG_Fortran08:
588     return SourceLanguage::Fortran;
589   case dwarf::DW_LANG_Pascal83:
590     return SourceLanguage::Pascal;
591   case dwarf::DW_LANG_Cobol74:
592   case dwarf::DW_LANG_Cobol85:
593     return SourceLanguage::Cobol;
594   case dwarf::DW_LANG_Java:
595     return SourceLanguage::Java;
596   case dwarf::DW_LANG_D:
597     return SourceLanguage::D;
598   case dwarf::DW_LANG_Swift:
599     return SourceLanguage::Swift;
600   case dwarf::DW_LANG_Rust:
601     return SourceLanguage::Rust;
602   case dwarf::DW_LANG_ObjC:
603     return SourceLanguage::ObjC;
604   case dwarf::DW_LANG_ObjC_plus_plus:
605     return SourceLanguage::ObjCpp;
606   default:
607     // There's no CodeView representation for this language, and CV doesn't
608     // have an "unknown" option for the language field, so we'll use MASM,
609     // as it's very low level.
610     return SourceLanguage::Masm;
611   }
612 }
613 
614 void CodeViewDebug::beginModule(Module *M) {
615   // If module doesn't have named metadata anchors or COFF debug section
616   // is not available, skip any debug info related stuff.
617   if (!MMI->hasDebugInfo() ||
618       !Asm->getObjFileLowering().getCOFFDebugSymbolsSection()) {
619     Asm = nullptr;
620     return;
621   }
622 
623   TheCPU = mapArchToCVCPUType(Triple(M->getTargetTriple()).getArch());
624 
625   // Get the current source language.
626   const MDNode *Node = *M->debug_compile_units_begin();
627   const auto *CU = cast<DICompileUnit>(Node);
628 
629   CurrentSourceLanguage = MapDWLangToCVLang(CU->getSourceLanguage());
630 
631   collectGlobalVariableInfo();
632 
633   // Check if we should emit type record hashes.
634   ConstantInt *GH =
635       mdconst::extract_or_null<ConstantInt>(M->getModuleFlag("CodeViewGHash"));
636   EmitDebugGlobalHashes = GH && !GH->isZero();
637 }
638 
639 void CodeViewDebug::endModule() {
640   if (!Asm || !MMI->hasDebugInfo())
641     return;
642 
643   // The COFF .debug$S section consists of several subsections, each starting
644   // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length
645   // of the payload followed by the payload itself.  The subsections are 4-byte
646   // aligned.
647 
648   // Use the generic .debug$S section, and make a subsection for all the inlined
649   // subprograms.
650   switchToDebugSectionForSymbol(nullptr);
651 
652   MCSymbol *CompilerInfo = beginCVSubsection(DebugSubsectionKind::Symbols);
653   emitObjName();
654   emitCompilerInformation();
655   endCVSubsection(CompilerInfo);
656 
657   emitInlineeLinesSubsection();
658 
659   // Emit per-function debug information.
660   for (auto &P : FnDebugInfo)
661     if (!P.first->isDeclarationForLinker())
662       emitDebugInfoForFunction(P.first, *P.second);
663 
664   // Get types used by globals without emitting anything.
665   // This is meant to collect all static const data members so they can be
666   // emitted as globals.
667   collectDebugInfoForGlobals();
668 
669   // Emit retained types.
670   emitDebugInfoForRetainedTypes();
671 
672   // Emit global variable debug information.
673   setCurrentSubprogram(nullptr);
674   emitDebugInfoForGlobals();
675 
676   // Switch back to the generic .debug$S section after potentially processing
677   // comdat symbol sections.
678   switchToDebugSectionForSymbol(nullptr);
679 
680   // Emit UDT records for any types used by global variables.
681   if (!GlobalUDTs.empty()) {
682     MCSymbol *SymbolsEnd = beginCVSubsection(DebugSubsectionKind::Symbols);
683     emitDebugInfoForUDTs(GlobalUDTs);
684     endCVSubsection(SymbolsEnd);
685   }
686 
687   // This subsection holds a file index to offset in string table table.
688   OS.AddComment("File index to string table offset subsection");
689   OS.emitCVFileChecksumsDirective();
690 
691   // This subsection holds the string table.
692   OS.AddComment("String table");
693   OS.emitCVStringTableDirective();
694 
695   // Emit S_BUILDINFO, which points to LF_BUILDINFO. Put this in its own symbol
696   // subsection in the generic .debug$S section at the end. There is no
697   // particular reason for this ordering other than to match MSVC.
698   emitBuildInfo();
699 
700   // Emit type information and hashes last, so that any types we translate while
701   // emitting function info are included.
702   emitTypeInformation();
703 
704   if (EmitDebugGlobalHashes)
705     emitTypeGlobalHashes();
706 
707   clear();
708 }
709 
710 static void
711 emitNullTerminatedSymbolName(MCStreamer &OS, StringRef S,
712                              unsigned MaxFixedRecordLength = 0xF00) {
713   // The maximum CV record length is 0xFF00. Most of the strings we emit appear
714   // after a fixed length portion of the record. The fixed length portion should
715   // always be less than 0xF00 (3840) bytes, so truncate the string so that the
716   // overall record size is less than the maximum allowed.
717   SmallString<32> NullTerminatedString(
718       S.take_front(MaxRecordLength - MaxFixedRecordLength - 1));
719   NullTerminatedString.push_back('\0');
720   OS.emitBytes(NullTerminatedString);
721 }
722 
723 void CodeViewDebug::emitTypeInformation() {
724   if (TypeTable.empty())
725     return;
726 
727   // Start the .debug$T or .debug$P section with 0x4.
728   OS.switchSection(Asm->getObjFileLowering().getCOFFDebugTypesSection());
729   emitCodeViewMagicVersion();
730 
731   TypeTableCollection Table(TypeTable.records());
732   TypeVisitorCallbackPipeline Pipeline;
733 
734   // To emit type record using Codeview MCStreamer adapter
735   CVMCAdapter CVMCOS(OS, Table);
736   TypeRecordMapping typeMapping(CVMCOS);
737   Pipeline.addCallbackToPipeline(typeMapping);
738 
739   std::optional<TypeIndex> B = Table.getFirst();
740   while (B) {
741     // This will fail if the record data is invalid.
742     CVType Record = Table.getType(*B);
743 
744     Error E = codeview::visitTypeRecord(Record, *B, Pipeline);
745 
746     if (E) {
747       logAllUnhandledErrors(std::move(E), errs(), "error: ");
748       llvm_unreachable("produced malformed type record");
749     }
750 
751     B = Table.getNext(*B);
752   }
753 }
754 
755 void CodeViewDebug::emitTypeGlobalHashes() {
756   if (TypeTable.empty())
757     return;
758 
759   // Start the .debug$H section with the version and hash algorithm, currently
760   // hardcoded to version 0, SHA1.
761   OS.switchSection(Asm->getObjFileLowering().getCOFFGlobalTypeHashesSection());
762 
763   OS.emitValueToAlignment(Align(4));
764   OS.AddComment("Magic");
765   OS.emitInt32(COFF::DEBUG_HASHES_SECTION_MAGIC);
766   OS.AddComment("Section Version");
767   OS.emitInt16(0);
768   OS.AddComment("Hash Algorithm");
769   OS.emitInt16(uint16_t(GlobalTypeHashAlg::BLAKE3));
770 
771   TypeIndex TI(TypeIndex::FirstNonSimpleIndex);
772   for (const auto &GHR : TypeTable.hashes()) {
773     if (OS.isVerboseAsm()) {
774       // Emit an EOL-comment describing which TypeIndex this hash corresponds
775       // to, as well as the stringified SHA1 hash.
776       SmallString<32> Comment;
777       raw_svector_ostream CommentOS(Comment);
778       CommentOS << formatv("{0:X+} [{1}]", TI.getIndex(), GHR);
779       OS.AddComment(Comment);
780       ++TI;
781     }
782     assert(GHR.Hash.size() == 8);
783     StringRef S(reinterpret_cast<const char *>(GHR.Hash.data()),
784                 GHR.Hash.size());
785     OS.emitBinaryData(S);
786   }
787 }
788 
789 void CodeViewDebug::emitObjName() {
790   MCSymbol *CompilerEnd = beginSymbolRecord(SymbolKind::S_OBJNAME);
791 
792   StringRef PathRef(Asm->TM.Options.ObjectFilenameForDebug);
793   llvm::SmallString<256> PathStore(PathRef);
794 
795   if (PathRef.empty() || PathRef == "-") {
796     // Don't emit the filename if we're writing to stdout or to /dev/null.
797     PathRef = {};
798   } else {
799     PathRef = PathStore;
800   }
801 
802   OS.AddComment("Signature");
803   OS.emitIntValue(0, 4);
804 
805   OS.AddComment("Object name");
806   emitNullTerminatedSymbolName(OS, PathRef);
807 
808   endSymbolRecord(CompilerEnd);
809 }
810 
811 namespace {
812 struct Version {
813   int Part[4];
814 };
815 } // end anonymous namespace
816 
817 // Takes a StringRef like "clang 4.0.0.0 (other nonsense 123)" and parses out
818 // the version number.
819 static Version parseVersion(StringRef Name) {
820   Version V = {{0}};
821   int N = 0;
822   for (const char C : Name) {
823     if (isdigit(C)) {
824       V.Part[N] *= 10;
825       V.Part[N] += C - '0';
826       V.Part[N] =
827           std::min<int>(V.Part[N], std::numeric_limits<uint16_t>::max());
828     } else if (C == '.') {
829       ++N;
830       if (N >= 4)
831         return V;
832     } else if (N > 0)
833       return V;
834   }
835   return V;
836 }
837 
838 void CodeViewDebug::emitCompilerInformation() {
839   MCSymbol *CompilerEnd = beginSymbolRecord(SymbolKind::S_COMPILE3);
840   uint32_t Flags = 0;
841 
842   // The low byte of the flags indicates the source language.
843   Flags = CurrentSourceLanguage;
844   // TODO:  Figure out which other flags need to be set.
845   if (MMI->getModule()->getProfileSummary(/*IsCS*/ false) != nullptr) {
846     Flags |= static_cast<uint32_t>(CompileSym3Flags::PGO);
847   }
848   using ArchType = llvm::Triple::ArchType;
849   ArchType Arch = Triple(MMI->getModule()->getTargetTriple()).getArch();
850   if (Asm->TM.Options.Hotpatch || Arch == ArchType::thumb ||
851       Arch == ArchType::aarch64) {
852     Flags |= static_cast<uint32_t>(CompileSym3Flags::HotPatch);
853   }
854 
855   OS.AddComment("Flags and language");
856   OS.emitInt32(Flags);
857 
858   OS.AddComment("CPUType");
859   OS.emitInt16(static_cast<uint64_t>(TheCPU));
860 
861   NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
862   const MDNode *Node = *CUs->operands().begin();
863   const auto *CU = cast<DICompileUnit>(Node);
864 
865   StringRef CompilerVersion = CU->getProducer();
866   Version FrontVer = parseVersion(CompilerVersion);
867   OS.AddComment("Frontend version");
868   for (int N : FrontVer.Part) {
869     OS.emitInt16(N);
870   }
871 
872   // Some Microsoft tools, like Binscope, expect a backend version number of at
873   // least 8.something, so we'll coerce the LLVM version into a form that
874   // guarantees it'll be big enough without really lying about the version.
875   int Major = 1000 * LLVM_VERSION_MAJOR +
876               10 * LLVM_VERSION_MINOR +
877               LLVM_VERSION_PATCH;
878   // Clamp it for builds that use unusually large version numbers.
879   Major = std::min<int>(Major, std::numeric_limits<uint16_t>::max());
880   Version BackVer = {{ Major, 0, 0, 0 }};
881   OS.AddComment("Backend version");
882   for (int N : BackVer.Part)
883     OS.emitInt16(N);
884 
885   OS.AddComment("Null-terminated compiler version string");
886   emitNullTerminatedSymbolName(OS, CompilerVersion);
887 
888   endSymbolRecord(CompilerEnd);
889 }
890 
891 static TypeIndex getStringIdTypeIdx(GlobalTypeTableBuilder &TypeTable,
892                                     StringRef S) {
893   StringIdRecord SIR(TypeIndex(0x0), S);
894   return TypeTable.writeLeafType(SIR);
895 }
896 
897 static std::string flattenCommandLine(ArrayRef<std::string> Args,
898                                       StringRef MainFilename) {
899   std::string FlatCmdLine;
900   raw_string_ostream OS(FlatCmdLine);
901   bool PrintedOneArg = false;
902   if (!StringRef(Args[0]).contains("-cc1")) {
903     llvm::sys::printArg(OS, "-cc1", /*Quote=*/true);
904     PrintedOneArg = true;
905   }
906   for (unsigned i = 0; i < Args.size(); i++) {
907     StringRef Arg = Args[i];
908     if (Arg.empty())
909       continue;
910     if (Arg == "-main-file-name" || Arg == "-o") {
911       i++; // Skip this argument and next one.
912       continue;
913     }
914     if (Arg.startswith("-object-file-name") || Arg == MainFilename)
915       continue;
916     // Skip fmessage-length for reproduciability.
917     if (Arg.startswith("-fmessage-length"))
918       continue;
919     if (PrintedOneArg)
920       OS << " ";
921     llvm::sys::printArg(OS, Arg, /*Quote=*/true);
922     PrintedOneArg = true;
923   }
924   OS.flush();
925   return FlatCmdLine;
926 }
927 
928 void CodeViewDebug::emitBuildInfo() {
929   // First, make LF_BUILDINFO. It's a sequence of strings with various bits of
930   // build info. The known prefix is:
931   // - Absolute path of current directory
932   // - Compiler path
933   // - Main source file path, relative to CWD or absolute
934   // - Type server PDB file
935   // - Canonical compiler command line
936   // If frontend and backend compilation are separated (think llc or LTO), it's
937   // not clear if the compiler path should refer to the executable for the
938   // frontend or the backend. Leave it blank for now.
939   TypeIndex BuildInfoArgs[BuildInfoRecord::MaxArgs] = {};
940   NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
941   const MDNode *Node = *CUs->operands().begin(); // FIXME: Multiple CUs.
942   const auto *CU = cast<DICompileUnit>(Node);
943   const DIFile *MainSourceFile = CU->getFile();
944   BuildInfoArgs[BuildInfoRecord::CurrentDirectory] =
945       getStringIdTypeIdx(TypeTable, MainSourceFile->getDirectory());
946   BuildInfoArgs[BuildInfoRecord::SourceFile] =
947       getStringIdTypeIdx(TypeTable, MainSourceFile->getFilename());
948   // FIXME: PDB is intentionally blank unless we implement /Zi type servers.
949   BuildInfoArgs[BuildInfoRecord::TypeServerPDB] =
950       getStringIdTypeIdx(TypeTable, "");
951   if (Asm->TM.Options.MCOptions.Argv0 != nullptr) {
952     BuildInfoArgs[BuildInfoRecord::BuildTool] =
953         getStringIdTypeIdx(TypeTable, Asm->TM.Options.MCOptions.Argv0);
954     BuildInfoArgs[BuildInfoRecord::CommandLine] = getStringIdTypeIdx(
955         TypeTable, flattenCommandLine(Asm->TM.Options.MCOptions.CommandLineArgs,
956                                       MainSourceFile->getFilename()));
957   }
958   BuildInfoRecord BIR(BuildInfoArgs);
959   TypeIndex BuildInfoIndex = TypeTable.writeLeafType(BIR);
960 
961   // Make a new .debug$S subsection for the S_BUILDINFO record, which points
962   // from the module symbols into the type stream.
963   MCSymbol *BISubsecEnd = beginCVSubsection(DebugSubsectionKind::Symbols);
964   MCSymbol *BIEnd = beginSymbolRecord(SymbolKind::S_BUILDINFO);
965   OS.AddComment("LF_BUILDINFO index");
966   OS.emitInt32(BuildInfoIndex.getIndex());
967   endSymbolRecord(BIEnd);
968   endCVSubsection(BISubsecEnd);
969 }
970 
971 void CodeViewDebug::emitInlineeLinesSubsection() {
972   if (InlinedSubprograms.empty())
973     return;
974 
975   OS.AddComment("Inlinee lines subsection");
976   MCSymbol *InlineEnd = beginCVSubsection(DebugSubsectionKind::InlineeLines);
977 
978   // We emit the checksum info for files.  This is used by debuggers to
979   // determine if a pdb matches the source before loading it.  Visual Studio,
980   // for instance, will display a warning that the breakpoints are not valid if
981   // the pdb does not match the source.
982   OS.AddComment("Inlinee lines signature");
983   OS.emitInt32(unsigned(InlineeLinesSignature::Normal));
984 
985   for (const DISubprogram *SP : InlinedSubprograms) {
986     assert(TypeIndices.count({SP, nullptr}));
987     TypeIndex InlineeIdx = TypeIndices[{SP, nullptr}];
988 
989     OS.addBlankLine();
990     unsigned FileId = maybeRecordFile(SP->getFile());
991     OS.AddComment("Inlined function " + SP->getName() + " starts at " +
992                   SP->getFilename() + Twine(':') + Twine(SP->getLine()));
993     OS.addBlankLine();
994     OS.AddComment("Type index of inlined function");
995     OS.emitInt32(InlineeIdx.getIndex());
996     OS.AddComment("Offset into filechecksum table");
997     OS.emitCVFileChecksumOffsetDirective(FileId);
998     OS.AddComment("Starting line number");
999     OS.emitInt32(SP->getLine());
1000   }
1001 
1002   endCVSubsection(InlineEnd);
1003 }
1004 
1005 void CodeViewDebug::emitInlinedCallSite(const FunctionInfo &FI,
1006                                         const DILocation *InlinedAt,
1007                                         const InlineSite &Site) {
1008   assert(TypeIndices.count({Site.Inlinee, nullptr}));
1009   TypeIndex InlineeIdx = TypeIndices[{Site.Inlinee, nullptr}];
1010 
1011   // SymbolRecord
1012   MCSymbol *InlineEnd = beginSymbolRecord(SymbolKind::S_INLINESITE);
1013 
1014   OS.AddComment("PtrParent");
1015   OS.emitInt32(0);
1016   OS.AddComment("PtrEnd");
1017   OS.emitInt32(0);
1018   OS.AddComment("Inlinee type index");
1019   OS.emitInt32(InlineeIdx.getIndex());
1020 
1021   unsigned FileId = maybeRecordFile(Site.Inlinee->getFile());
1022   unsigned StartLineNum = Site.Inlinee->getLine();
1023 
1024   OS.emitCVInlineLinetableDirective(Site.SiteFuncId, FileId, StartLineNum,
1025                                     FI.Begin, FI.End);
1026 
1027   endSymbolRecord(InlineEnd);
1028 
1029   emitLocalVariableList(FI, Site.InlinedLocals);
1030 
1031   // Recurse on child inlined call sites before closing the scope.
1032   for (const DILocation *ChildSite : Site.ChildSites) {
1033     auto I = FI.InlineSites.find(ChildSite);
1034     assert(I != FI.InlineSites.end() &&
1035            "child site not in function inline site map");
1036     emitInlinedCallSite(FI, ChildSite, I->second);
1037   }
1038 
1039   // Close the scope.
1040   emitEndSymbolRecord(SymbolKind::S_INLINESITE_END);
1041 }
1042 
1043 void CodeViewDebug::switchToDebugSectionForSymbol(const MCSymbol *GVSym) {
1044   // If we have a symbol, it may be in a section that is COMDAT. If so, find the
1045   // comdat key. A section may be comdat because of -ffunction-sections or
1046   // because it is comdat in the IR.
1047   MCSectionCOFF *GVSec =
1048       GVSym ? dyn_cast<MCSectionCOFF>(&GVSym->getSection()) : nullptr;
1049   const MCSymbol *KeySym = GVSec ? GVSec->getCOMDATSymbol() : nullptr;
1050 
1051   MCSectionCOFF *DebugSec = cast<MCSectionCOFF>(
1052       Asm->getObjFileLowering().getCOFFDebugSymbolsSection());
1053   DebugSec = OS.getContext().getAssociativeCOFFSection(DebugSec, KeySym);
1054 
1055   OS.switchSection(DebugSec);
1056 
1057   // Emit the magic version number if this is the first time we've switched to
1058   // this section.
1059   if (ComdatDebugSections.insert(DebugSec).second)
1060     emitCodeViewMagicVersion();
1061 }
1062 
1063 // Emit an S_THUNK32/S_END symbol pair for a thunk routine.
1064 // The only supported thunk ordinal is currently the standard type.
1065 void CodeViewDebug::emitDebugInfoForThunk(const Function *GV,
1066                                           FunctionInfo &FI,
1067                                           const MCSymbol *Fn) {
1068   std::string FuncName =
1069       std::string(GlobalValue::dropLLVMManglingEscape(GV->getName()));
1070   const ThunkOrdinal ordinal = ThunkOrdinal::Standard; // Only supported kind.
1071 
1072   OS.AddComment("Symbol subsection for " + Twine(FuncName));
1073   MCSymbol *SymbolsEnd = beginCVSubsection(DebugSubsectionKind::Symbols);
1074 
1075   // Emit S_THUNK32
1076   MCSymbol *ThunkRecordEnd = beginSymbolRecord(SymbolKind::S_THUNK32);
1077   OS.AddComment("PtrParent");
1078   OS.emitInt32(0);
1079   OS.AddComment("PtrEnd");
1080   OS.emitInt32(0);
1081   OS.AddComment("PtrNext");
1082   OS.emitInt32(0);
1083   OS.AddComment("Thunk section relative address");
1084   OS.emitCOFFSecRel32(Fn, /*Offset=*/0);
1085   OS.AddComment("Thunk section index");
1086   OS.emitCOFFSectionIndex(Fn);
1087   OS.AddComment("Code size");
1088   OS.emitAbsoluteSymbolDiff(FI.End, Fn, 2);
1089   OS.AddComment("Ordinal");
1090   OS.emitInt8(unsigned(ordinal));
1091   OS.AddComment("Function name");
1092   emitNullTerminatedSymbolName(OS, FuncName);
1093   // Additional fields specific to the thunk ordinal would go here.
1094   endSymbolRecord(ThunkRecordEnd);
1095 
1096   // Local variables/inlined routines are purposely omitted here.  The point of
1097   // marking this as a thunk is so Visual Studio will NOT stop in this routine.
1098 
1099   // Emit S_PROC_ID_END
1100   emitEndSymbolRecord(SymbolKind::S_PROC_ID_END);
1101 
1102   endCVSubsection(SymbolsEnd);
1103 }
1104 
1105 void CodeViewDebug::emitDebugInfoForFunction(const Function *GV,
1106                                              FunctionInfo &FI) {
1107   // For each function there is a separate subsection which holds the PC to
1108   // file:line table.
1109   const MCSymbol *Fn = Asm->getSymbol(GV);
1110   assert(Fn);
1111 
1112   // Switch to the to a comdat section, if appropriate.
1113   switchToDebugSectionForSymbol(Fn);
1114 
1115   std::string FuncName;
1116   auto *SP = GV->getSubprogram();
1117   assert(SP);
1118   setCurrentSubprogram(SP);
1119 
1120   if (SP->isThunk()) {
1121     emitDebugInfoForThunk(GV, FI, Fn);
1122     return;
1123   }
1124 
1125   // If we have a display name, build the fully qualified name by walking the
1126   // chain of scopes.
1127   if (!SP->getName().empty())
1128     FuncName = getFullyQualifiedName(SP->getScope(), SP->getName());
1129 
1130   // If our DISubprogram name is empty, use the mangled name.
1131   if (FuncName.empty())
1132     FuncName = std::string(GlobalValue::dropLLVMManglingEscape(GV->getName()));
1133 
1134   // Emit FPO data, but only on 32-bit x86. No other platforms use it.
1135   if (Triple(MMI->getModule()->getTargetTriple()).getArch() == Triple::x86)
1136     OS.emitCVFPOData(Fn);
1137 
1138   // Emit a symbol subsection, required by VS2012+ to find function boundaries.
1139   OS.AddComment("Symbol subsection for " + Twine(FuncName));
1140   MCSymbol *SymbolsEnd = beginCVSubsection(DebugSubsectionKind::Symbols);
1141   {
1142     SymbolKind ProcKind = GV->hasLocalLinkage() ? SymbolKind::S_LPROC32_ID
1143                                                 : SymbolKind::S_GPROC32_ID;
1144     MCSymbol *ProcRecordEnd = beginSymbolRecord(ProcKind);
1145 
1146     // These fields are filled in by tools like CVPACK which run after the fact.
1147     OS.AddComment("PtrParent");
1148     OS.emitInt32(0);
1149     OS.AddComment("PtrEnd");
1150     OS.emitInt32(0);
1151     OS.AddComment("PtrNext");
1152     OS.emitInt32(0);
1153     // This is the important bit that tells the debugger where the function
1154     // code is located and what's its size:
1155     OS.AddComment("Code size");
1156     OS.emitAbsoluteSymbolDiff(FI.End, Fn, 4);
1157     OS.AddComment("Offset after prologue");
1158     OS.emitInt32(0);
1159     OS.AddComment("Offset before epilogue");
1160     OS.emitInt32(0);
1161     OS.AddComment("Function type index");
1162     OS.emitInt32(getFuncIdForSubprogram(GV->getSubprogram()).getIndex());
1163     OS.AddComment("Function section relative address");
1164     OS.emitCOFFSecRel32(Fn, /*Offset=*/0);
1165     OS.AddComment("Function section index");
1166     OS.emitCOFFSectionIndex(Fn);
1167     OS.AddComment("Flags");
1168     ProcSymFlags ProcFlags = ProcSymFlags::HasOptimizedDebugInfo;
1169     if (FI.HasFramePointer)
1170       ProcFlags |= ProcSymFlags::HasFP;
1171     if (GV->hasFnAttribute(Attribute::NoReturn))
1172       ProcFlags |= ProcSymFlags::IsNoReturn;
1173     if (GV->hasFnAttribute(Attribute::NoInline))
1174       ProcFlags |= ProcSymFlags::IsNoInline;
1175     OS.emitInt8(static_cast<uint8_t>(ProcFlags));
1176     // Emit the function display name as a null-terminated string.
1177     OS.AddComment("Function name");
1178     // Truncate the name so we won't overflow the record length field.
1179     emitNullTerminatedSymbolName(OS, FuncName);
1180     endSymbolRecord(ProcRecordEnd);
1181 
1182     MCSymbol *FrameProcEnd = beginSymbolRecord(SymbolKind::S_FRAMEPROC);
1183     // Subtract out the CSR size since MSVC excludes that and we include it.
1184     OS.AddComment("FrameSize");
1185     OS.emitInt32(FI.FrameSize - FI.CSRSize);
1186     OS.AddComment("Padding");
1187     OS.emitInt32(0);
1188     OS.AddComment("Offset of padding");
1189     OS.emitInt32(0);
1190     OS.AddComment("Bytes of callee saved registers");
1191     OS.emitInt32(FI.CSRSize);
1192     OS.AddComment("Exception handler offset");
1193     OS.emitInt32(0);
1194     OS.AddComment("Exception handler section");
1195     OS.emitInt16(0);
1196     OS.AddComment("Flags (defines frame register)");
1197     OS.emitInt32(uint32_t(FI.FrameProcOpts));
1198     endSymbolRecord(FrameProcEnd);
1199 
1200     emitInlinees(FI.Inlinees);
1201     emitLocalVariableList(FI, FI.Locals);
1202     emitGlobalVariableList(FI.Globals);
1203     emitLexicalBlockList(FI.ChildBlocks, FI);
1204 
1205     // Emit inlined call site information. Only emit functions inlined directly
1206     // into the parent function. We'll emit the other sites recursively as part
1207     // of their parent inline site.
1208     for (const DILocation *InlinedAt : FI.ChildSites) {
1209       auto I = FI.InlineSites.find(InlinedAt);
1210       assert(I != FI.InlineSites.end() &&
1211              "child site not in function inline site map");
1212       emitInlinedCallSite(FI, InlinedAt, I->second);
1213     }
1214 
1215     for (auto Annot : FI.Annotations) {
1216       MCSymbol *Label = Annot.first;
1217       MDTuple *Strs = cast<MDTuple>(Annot.second);
1218       MCSymbol *AnnotEnd = beginSymbolRecord(SymbolKind::S_ANNOTATION);
1219       OS.emitCOFFSecRel32(Label, /*Offset=*/0);
1220       // FIXME: Make sure we don't overflow the max record size.
1221       OS.emitCOFFSectionIndex(Label);
1222       OS.emitInt16(Strs->getNumOperands());
1223       for (Metadata *MD : Strs->operands()) {
1224         // MDStrings are null terminated, so we can do EmitBytes and get the
1225         // nice .asciz directive.
1226         StringRef Str = cast<MDString>(MD)->getString();
1227         assert(Str.data()[Str.size()] == '\0' && "non-nullterminated MDString");
1228         OS.emitBytes(StringRef(Str.data(), Str.size() + 1));
1229       }
1230       endSymbolRecord(AnnotEnd);
1231     }
1232 
1233     for (auto HeapAllocSite : FI.HeapAllocSites) {
1234       const MCSymbol *BeginLabel = std::get<0>(HeapAllocSite);
1235       const MCSymbol *EndLabel = std::get<1>(HeapAllocSite);
1236       const DIType *DITy = std::get<2>(HeapAllocSite);
1237       MCSymbol *HeapAllocEnd = beginSymbolRecord(SymbolKind::S_HEAPALLOCSITE);
1238       OS.AddComment("Call site offset");
1239       OS.emitCOFFSecRel32(BeginLabel, /*Offset=*/0);
1240       OS.AddComment("Call site section index");
1241       OS.emitCOFFSectionIndex(BeginLabel);
1242       OS.AddComment("Call instruction length");
1243       OS.emitAbsoluteSymbolDiff(EndLabel, BeginLabel, 2);
1244       OS.AddComment("Type index");
1245       OS.emitInt32(getCompleteTypeIndex(DITy).getIndex());
1246       endSymbolRecord(HeapAllocEnd);
1247     }
1248 
1249     if (SP != nullptr)
1250       emitDebugInfoForUDTs(LocalUDTs);
1251 
1252     emitDebugInfoForJumpTables(FI);
1253 
1254     // We're done with this function.
1255     emitEndSymbolRecord(SymbolKind::S_PROC_ID_END);
1256   }
1257   endCVSubsection(SymbolsEnd);
1258 
1259   // We have an assembler directive that takes care of the whole line table.
1260   OS.emitCVLinetableDirective(FI.FuncId, Fn, FI.End);
1261 }
1262 
1263 CodeViewDebug::LocalVarDef
1264 CodeViewDebug::createDefRangeMem(uint16_t CVRegister, int Offset) {
1265   LocalVarDef DR;
1266   DR.InMemory = -1;
1267   DR.DataOffset = Offset;
1268   assert(DR.DataOffset == Offset && "truncation");
1269   DR.IsSubfield = 0;
1270   DR.StructOffset = 0;
1271   DR.CVRegister = CVRegister;
1272   return DR;
1273 }
1274 
1275 void CodeViewDebug::collectVariableInfoFromMFTable(
1276     DenseSet<InlinedEntity> &Processed) {
1277   const MachineFunction &MF = *Asm->MF;
1278   const TargetSubtargetInfo &TSI = MF.getSubtarget();
1279   const TargetFrameLowering *TFI = TSI.getFrameLowering();
1280   const TargetRegisterInfo *TRI = TSI.getRegisterInfo();
1281 
1282   for (const MachineFunction::VariableDbgInfo &VI :
1283        MF.getInStackSlotVariableDbgInfo()) {
1284     if (!VI.Var)
1285       continue;
1286     assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) &&
1287            "Expected inlined-at fields to agree");
1288 
1289     Processed.insert(InlinedEntity(VI.Var, VI.Loc->getInlinedAt()));
1290     LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc);
1291 
1292     // If variable scope is not found then skip this variable.
1293     if (!Scope)
1294       continue;
1295 
1296     // If the variable has an attached offset expression, extract it.
1297     // FIXME: Try to handle DW_OP_deref as well.
1298     int64_t ExprOffset = 0;
1299     bool Deref = false;
1300     if (VI.Expr) {
1301       // If there is one DW_OP_deref element, use offset of 0 and keep going.
1302       if (VI.Expr->getNumElements() == 1 &&
1303           VI.Expr->getElement(0) == llvm::dwarf::DW_OP_deref)
1304         Deref = true;
1305       else if (!VI.Expr->extractIfOffset(ExprOffset))
1306         continue;
1307     }
1308 
1309     // Get the frame register used and the offset.
1310     Register FrameReg;
1311     StackOffset FrameOffset =
1312         TFI->getFrameIndexReference(*Asm->MF, VI.getStackSlot(), FrameReg);
1313     uint16_t CVReg = TRI->getCodeViewRegNum(FrameReg);
1314 
1315     assert(!FrameOffset.getScalable() &&
1316            "Frame offsets with a scalable component are not supported");
1317 
1318     // Calculate the label ranges.
1319     LocalVarDef DefRange =
1320         createDefRangeMem(CVReg, FrameOffset.getFixed() + ExprOffset);
1321 
1322     LocalVariable Var;
1323     Var.DIVar = VI.Var;
1324 
1325     for (const InsnRange &Range : Scope->getRanges()) {
1326       const MCSymbol *Begin = getLabelBeforeInsn(Range.first);
1327       const MCSymbol *End = getLabelAfterInsn(Range.second);
1328       End = End ? End : Asm->getFunctionEnd();
1329       Var.DefRanges[DefRange].emplace_back(Begin, End);
1330     }
1331 
1332     if (Deref)
1333       Var.UseReferenceType = true;
1334 
1335     recordLocalVariable(std::move(Var), Scope);
1336   }
1337 }
1338 
1339 static bool canUseReferenceType(const DbgVariableLocation &Loc) {
1340   return !Loc.LoadChain.empty() && Loc.LoadChain.back() == 0;
1341 }
1342 
1343 static bool needsReferenceType(const DbgVariableLocation &Loc) {
1344   return Loc.LoadChain.size() == 2 && Loc.LoadChain.back() == 0;
1345 }
1346 
1347 void CodeViewDebug::calculateRanges(
1348     LocalVariable &Var, const DbgValueHistoryMap::Entries &Entries) {
1349   const TargetRegisterInfo *TRI = Asm->MF->getSubtarget().getRegisterInfo();
1350 
1351   // Calculate the definition ranges.
1352   for (auto I = Entries.begin(), E = Entries.end(); I != E; ++I) {
1353     const auto &Entry = *I;
1354     if (!Entry.isDbgValue())
1355       continue;
1356     const MachineInstr *DVInst = Entry.getInstr();
1357     assert(DVInst->isDebugValue() && "Invalid History entry");
1358     // FIXME: Find a way to represent constant variables, since they are
1359     // relatively common.
1360     std::optional<DbgVariableLocation> Location =
1361         DbgVariableLocation::extractFromMachineInstruction(*DVInst);
1362     if (!Location)
1363     {
1364       // When we don't have a location this is usually because LLVM has
1365       // transformed it into a constant and we only have an llvm.dbg.value. We
1366       // can't represent these well in CodeView since S_LOCAL only works on
1367       // registers and memory locations. Instead, we will pretend this to be a
1368       // constant value to at least have it show up in the debugger.
1369       auto Op = DVInst->getDebugOperand(0);
1370       if (Op.isImm())
1371         Var.ConstantValue = APSInt(APInt(64, Op.getImm()), false);
1372       continue;
1373     }
1374 
1375     // CodeView can only express variables in register and variables in memory
1376     // at a constant offset from a register. However, for variables passed
1377     // indirectly by pointer, it is common for that pointer to be spilled to a
1378     // stack location. For the special case of one offseted load followed by a
1379     // zero offset load (a pointer spilled to the stack), we change the type of
1380     // the local variable from a value type to a reference type. This tricks the
1381     // debugger into doing the load for us.
1382     if (Var.UseReferenceType) {
1383       // We're using a reference type. Drop the last zero offset load.
1384       if (canUseReferenceType(*Location))
1385         Location->LoadChain.pop_back();
1386       else
1387         continue;
1388     } else if (needsReferenceType(*Location)) {
1389       // This location can't be expressed without switching to a reference type.
1390       // Start over using that.
1391       Var.UseReferenceType = true;
1392       Var.DefRanges.clear();
1393       calculateRanges(Var, Entries);
1394       return;
1395     }
1396 
1397     // We can only handle a register or an offseted load of a register.
1398     if (Location->Register == 0 || Location->LoadChain.size() > 1)
1399       continue;
1400 
1401     LocalVarDef DR;
1402     DR.CVRegister = TRI->getCodeViewRegNum(Location->Register);
1403     DR.InMemory = !Location->LoadChain.empty();
1404     DR.DataOffset =
1405         !Location->LoadChain.empty() ? Location->LoadChain.back() : 0;
1406     if (Location->FragmentInfo) {
1407       DR.IsSubfield = true;
1408       DR.StructOffset = Location->FragmentInfo->OffsetInBits / 8;
1409     } else {
1410       DR.IsSubfield = false;
1411       DR.StructOffset = 0;
1412     }
1413 
1414     // Compute the label range.
1415     const MCSymbol *Begin = getLabelBeforeInsn(Entry.getInstr());
1416     const MCSymbol *End;
1417     if (Entry.getEndIndex() != DbgValueHistoryMap::NoEntry) {
1418       auto &EndingEntry = Entries[Entry.getEndIndex()];
1419       End = EndingEntry.isDbgValue()
1420                 ? getLabelBeforeInsn(EndingEntry.getInstr())
1421                 : getLabelAfterInsn(EndingEntry.getInstr());
1422     } else
1423       End = Asm->getFunctionEnd();
1424 
1425     // If the last range end is our begin, just extend the last range.
1426     // Otherwise make a new range.
1427     SmallVectorImpl<std::pair<const MCSymbol *, const MCSymbol *>> &R =
1428         Var.DefRanges[DR];
1429     if (!R.empty() && R.back().second == Begin)
1430       R.back().second = End;
1431     else
1432       R.emplace_back(Begin, End);
1433 
1434     // FIXME: Do more range combining.
1435   }
1436 }
1437 
1438 void CodeViewDebug::collectVariableInfo(const DISubprogram *SP) {
1439   DenseSet<InlinedEntity> Processed;
1440   // Grab the variable info that was squirreled away in the MMI side-table.
1441   collectVariableInfoFromMFTable(Processed);
1442 
1443   for (const auto &I : DbgValues) {
1444     InlinedEntity IV = I.first;
1445     if (Processed.count(IV))
1446       continue;
1447     const DILocalVariable *DIVar = cast<DILocalVariable>(IV.first);
1448     const DILocation *InlinedAt = IV.second;
1449 
1450     // Instruction ranges, specifying where IV is accessible.
1451     const auto &Entries = I.second;
1452 
1453     LexicalScope *Scope = nullptr;
1454     if (InlinedAt)
1455       Scope = LScopes.findInlinedScope(DIVar->getScope(), InlinedAt);
1456     else
1457       Scope = LScopes.findLexicalScope(DIVar->getScope());
1458     // If variable scope is not found then skip this variable.
1459     if (!Scope)
1460       continue;
1461 
1462     LocalVariable Var;
1463     Var.DIVar = DIVar;
1464 
1465     calculateRanges(Var, Entries);
1466     recordLocalVariable(std::move(Var), Scope);
1467   }
1468 }
1469 
1470 void CodeViewDebug::beginFunctionImpl(const MachineFunction *MF) {
1471   const TargetSubtargetInfo &TSI = MF->getSubtarget();
1472   const TargetRegisterInfo *TRI = TSI.getRegisterInfo();
1473   const MachineFrameInfo &MFI = MF->getFrameInfo();
1474   const Function &GV = MF->getFunction();
1475   auto Insertion = FnDebugInfo.insert({&GV, std::make_unique<FunctionInfo>()});
1476   assert(Insertion.second && "function already has info");
1477   CurFn = Insertion.first->second.get();
1478   CurFn->FuncId = NextFuncId++;
1479   CurFn->Begin = Asm->getFunctionBegin();
1480 
1481   // The S_FRAMEPROC record reports the stack size, and how many bytes of
1482   // callee-saved registers were used. For targets that don't use a PUSH
1483   // instruction (AArch64), this will be zero.
1484   CurFn->CSRSize = MFI.getCVBytesOfCalleeSavedRegisters();
1485   CurFn->FrameSize = MFI.getStackSize();
1486   CurFn->OffsetAdjustment = MFI.getOffsetAdjustment();
1487   CurFn->HasStackRealignment = TRI->hasStackRealignment(*MF);
1488 
1489   // For this function S_FRAMEPROC record, figure out which codeview register
1490   // will be the frame pointer.
1491   CurFn->EncodedParamFramePtrReg = EncodedFramePtrReg::None; // None.
1492   CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::None; // None.
1493   if (CurFn->FrameSize > 0) {
1494     if (!TSI.getFrameLowering()->hasFP(*MF)) {
1495       CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::StackPtr;
1496       CurFn->EncodedParamFramePtrReg = EncodedFramePtrReg::StackPtr;
1497     } else {
1498       CurFn->HasFramePointer = true;
1499       // If there is an FP, parameters are always relative to it.
1500       CurFn->EncodedParamFramePtrReg = EncodedFramePtrReg::FramePtr;
1501       if (CurFn->HasStackRealignment) {
1502         // If the stack needs realignment, locals are relative to SP or VFRAME.
1503         CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::StackPtr;
1504       } else {
1505         // Otherwise, locals are relative to EBP, and we probably have VLAs or
1506         // other stack adjustments.
1507         CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::FramePtr;
1508       }
1509     }
1510   }
1511 
1512   // Compute other frame procedure options.
1513   FrameProcedureOptions FPO = FrameProcedureOptions::None;
1514   if (MFI.hasVarSizedObjects())
1515     FPO |= FrameProcedureOptions::HasAlloca;
1516   if (MF->exposesReturnsTwice())
1517     FPO |= FrameProcedureOptions::HasSetJmp;
1518   // FIXME: Set HasLongJmp if we ever track that info.
1519   if (MF->hasInlineAsm())
1520     FPO |= FrameProcedureOptions::HasInlineAssembly;
1521   if (GV.hasPersonalityFn()) {
1522     if (isAsynchronousEHPersonality(
1523             classifyEHPersonality(GV.getPersonalityFn())))
1524       FPO |= FrameProcedureOptions::HasStructuredExceptionHandling;
1525     else
1526       FPO |= FrameProcedureOptions::HasExceptionHandling;
1527   }
1528   if (GV.hasFnAttribute(Attribute::InlineHint))
1529     FPO |= FrameProcedureOptions::MarkedInline;
1530   if (GV.hasFnAttribute(Attribute::Naked))
1531     FPO |= FrameProcedureOptions::Naked;
1532   if (MFI.hasStackProtectorIndex()) {
1533     FPO |= FrameProcedureOptions::SecurityChecks;
1534     if (GV.hasFnAttribute(Attribute::StackProtectStrong) ||
1535         GV.hasFnAttribute(Attribute::StackProtectReq)) {
1536       FPO |= FrameProcedureOptions::StrictSecurityChecks;
1537     }
1538   } else if (!GV.hasStackProtectorFnAttr()) {
1539     // __declspec(safebuffers) disables stack guards.
1540     FPO |= FrameProcedureOptions::SafeBuffers;
1541   }
1542   FPO |= FrameProcedureOptions(uint32_t(CurFn->EncodedLocalFramePtrReg) << 14U);
1543   FPO |= FrameProcedureOptions(uint32_t(CurFn->EncodedParamFramePtrReg) << 16U);
1544   if (Asm->TM.getOptLevel() != CodeGenOptLevel::None && !GV.hasOptSize() &&
1545       !GV.hasOptNone())
1546     FPO |= FrameProcedureOptions::OptimizedForSpeed;
1547   if (GV.hasProfileData()) {
1548     FPO |= FrameProcedureOptions::ValidProfileCounts;
1549     FPO |= FrameProcedureOptions::ProfileGuidedOptimization;
1550   }
1551   // FIXME: Set GuardCfg when it is implemented.
1552   CurFn->FrameProcOpts = FPO;
1553 
1554   OS.emitCVFuncIdDirective(CurFn->FuncId);
1555 
1556   // Find the end of the function prolog.  First known non-DBG_VALUE and
1557   // non-frame setup location marks the beginning of the function body.
1558   // FIXME: is there a simpler a way to do this? Can we just search
1559   // for the first instruction of the function, not the last of the prolog?
1560   DebugLoc PrologEndLoc;
1561   bool EmptyPrologue = true;
1562   for (const auto &MBB : *MF) {
1563     for (const auto &MI : MBB) {
1564       if (!MI.isMetaInstruction() && !MI.getFlag(MachineInstr::FrameSetup) &&
1565           MI.getDebugLoc()) {
1566         PrologEndLoc = MI.getDebugLoc();
1567         break;
1568       } else if (!MI.isMetaInstruction()) {
1569         EmptyPrologue = false;
1570       }
1571     }
1572   }
1573 
1574   // Record beginning of function if we have a non-empty prologue.
1575   if (PrologEndLoc && !EmptyPrologue) {
1576     DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc();
1577     maybeRecordLocation(FnStartDL, MF);
1578   }
1579 
1580   // Find heap alloc sites and emit labels around them.
1581   for (const auto &MBB : *MF) {
1582     for (const auto &MI : MBB) {
1583       if (MI.getHeapAllocMarker()) {
1584         requestLabelBeforeInsn(&MI);
1585         requestLabelAfterInsn(&MI);
1586       }
1587     }
1588   }
1589 
1590   // Mark branches that may potentially be using jump tables with labels.
1591   bool isThumb = Triple(MMI->getModule()->getTargetTriple()).getArch() ==
1592                  llvm::Triple::ArchType::thumb;
1593   discoverJumpTableBranches(MF, isThumb);
1594 }
1595 
1596 static bool shouldEmitUdt(const DIType *T) {
1597   if (!T)
1598     return false;
1599 
1600   // MSVC does not emit UDTs for typedefs that are scoped to classes.
1601   if (T->getTag() == dwarf::DW_TAG_typedef) {
1602     if (DIScope *Scope = T->getScope()) {
1603       switch (Scope->getTag()) {
1604       case dwarf::DW_TAG_structure_type:
1605       case dwarf::DW_TAG_class_type:
1606       case dwarf::DW_TAG_union_type:
1607         return false;
1608       default:
1609           // do nothing.
1610           ;
1611       }
1612     }
1613   }
1614 
1615   while (true) {
1616     if (!T || T->isForwardDecl())
1617       return false;
1618 
1619     const DIDerivedType *DT = dyn_cast<DIDerivedType>(T);
1620     if (!DT)
1621       return true;
1622     T = DT->getBaseType();
1623   }
1624   return true;
1625 }
1626 
1627 void CodeViewDebug::addToUDTs(const DIType *Ty) {
1628   // Don't record empty UDTs.
1629   if (Ty->getName().empty())
1630     return;
1631   if (!shouldEmitUdt(Ty))
1632     return;
1633 
1634   SmallVector<StringRef, 5> ParentScopeNames;
1635   const DISubprogram *ClosestSubprogram =
1636       collectParentScopeNames(Ty->getScope(), ParentScopeNames);
1637 
1638   std::string FullyQualifiedName =
1639       formatNestedName(ParentScopeNames, getPrettyScopeName(Ty));
1640 
1641   if (ClosestSubprogram == nullptr) {
1642     GlobalUDTs.emplace_back(std::move(FullyQualifiedName), Ty);
1643   } else if (ClosestSubprogram == CurrentSubprogram) {
1644     LocalUDTs.emplace_back(std::move(FullyQualifiedName), Ty);
1645   }
1646 
1647   // TODO: What if the ClosestSubprogram is neither null or the current
1648   // subprogram?  Currently, the UDT just gets dropped on the floor.
1649   //
1650   // The current behavior is not desirable.  To get maximal fidelity, we would
1651   // need to perform all type translation before beginning emission of .debug$S
1652   // and then make LocalUDTs a member of FunctionInfo
1653 }
1654 
1655 TypeIndex CodeViewDebug::lowerType(const DIType *Ty, const DIType *ClassTy) {
1656   // Generic dispatch for lowering an unknown type.
1657   switch (Ty->getTag()) {
1658   case dwarf::DW_TAG_array_type:
1659     return lowerTypeArray(cast<DICompositeType>(Ty));
1660   case dwarf::DW_TAG_typedef:
1661     return lowerTypeAlias(cast<DIDerivedType>(Ty));
1662   case dwarf::DW_TAG_base_type:
1663     return lowerTypeBasic(cast<DIBasicType>(Ty));
1664   case dwarf::DW_TAG_pointer_type:
1665     if (cast<DIDerivedType>(Ty)->getName() == "__vtbl_ptr_type")
1666       return lowerTypeVFTableShape(cast<DIDerivedType>(Ty));
1667     [[fallthrough]];
1668   case dwarf::DW_TAG_reference_type:
1669   case dwarf::DW_TAG_rvalue_reference_type:
1670     return lowerTypePointer(cast<DIDerivedType>(Ty));
1671   case dwarf::DW_TAG_ptr_to_member_type:
1672     return lowerTypeMemberPointer(cast<DIDerivedType>(Ty));
1673   case dwarf::DW_TAG_restrict_type:
1674   case dwarf::DW_TAG_const_type:
1675   case dwarf::DW_TAG_volatile_type:
1676   // TODO: add support for DW_TAG_atomic_type here
1677     return lowerTypeModifier(cast<DIDerivedType>(Ty));
1678   case dwarf::DW_TAG_subroutine_type:
1679     if (ClassTy) {
1680       // The member function type of a member function pointer has no
1681       // ThisAdjustment.
1682       return lowerTypeMemberFunction(cast<DISubroutineType>(Ty), ClassTy,
1683                                      /*ThisAdjustment=*/0,
1684                                      /*IsStaticMethod=*/false);
1685     }
1686     return lowerTypeFunction(cast<DISubroutineType>(Ty));
1687   case dwarf::DW_TAG_enumeration_type:
1688     return lowerTypeEnum(cast<DICompositeType>(Ty));
1689   case dwarf::DW_TAG_class_type:
1690   case dwarf::DW_TAG_structure_type:
1691     return lowerTypeClass(cast<DICompositeType>(Ty));
1692   case dwarf::DW_TAG_union_type:
1693     return lowerTypeUnion(cast<DICompositeType>(Ty));
1694   case dwarf::DW_TAG_string_type:
1695     return lowerTypeString(cast<DIStringType>(Ty));
1696   case dwarf::DW_TAG_unspecified_type:
1697     if (Ty->getName() == "decltype(nullptr)")
1698       return TypeIndex::NullptrT();
1699     return TypeIndex::None();
1700   default:
1701     // Use the null type index.
1702     return TypeIndex();
1703   }
1704 }
1705 
1706 TypeIndex CodeViewDebug::lowerTypeAlias(const DIDerivedType *Ty) {
1707   TypeIndex UnderlyingTypeIndex = getTypeIndex(Ty->getBaseType());
1708   StringRef TypeName = Ty->getName();
1709 
1710   addToUDTs(Ty);
1711 
1712   if (UnderlyingTypeIndex == TypeIndex(SimpleTypeKind::Int32Long) &&
1713       TypeName == "HRESULT")
1714     return TypeIndex(SimpleTypeKind::HResult);
1715   if (UnderlyingTypeIndex == TypeIndex(SimpleTypeKind::UInt16Short) &&
1716       TypeName == "wchar_t")
1717     return TypeIndex(SimpleTypeKind::WideCharacter);
1718 
1719   return UnderlyingTypeIndex;
1720 }
1721 
1722 TypeIndex CodeViewDebug::lowerTypeArray(const DICompositeType *Ty) {
1723   const DIType *ElementType = Ty->getBaseType();
1724   TypeIndex ElementTypeIndex = getTypeIndex(ElementType);
1725   // IndexType is size_t, which depends on the bitness of the target.
1726   TypeIndex IndexType = getPointerSizeInBytes() == 8
1727                             ? TypeIndex(SimpleTypeKind::UInt64Quad)
1728                             : TypeIndex(SimpleTypeKind::UInt32Long);
1729 
1730   uint64_t ElementSize = getBaseTypeSize(ElementType) / 8;
1731 
1732   // Add subranges to array type.
1733   DINodeArray Elements = Ty->getElements();
1734   for (int i = Elements.size() - 1; i >= 0; --i) {
1735     const DINode *Element = Elements[i];
1736     assert(Element->getTag() == dwarf::DW_TAG_subrange_type);
1737 
1738     const DISubrange *Subrange = cast<DISubrange>(Element);
1739     int64_t Count = -1;
1740 
1741     // If Subrange has a Count field, use it.
1742     // Otherwise, if it has an upperboud, use (upperbound - lowerbound + 1),
1743     // where lowerbound is from the LowerBound field of the Subrange,
1744     // or the language default lowerbound if that field is unspecified.
1745     if (auto *CI = dyn_cast_if_present<ConstantInt *>(Subrange->getCount()))
1746       Count = CI->getSExtValue();
1747     else if (auto *UI = dyn_cast_if_present<ConstantInt *>(
1748                  Subrange->getUpperBound())) {
1749       // Fortran uses 1 as the default lowerbound; other languages use 0.
1750       int64_t Lowerbound = (moduleIsInFortran()) ? 1 : 0;
1751       auto *LI = dyn_cast_if_present<ConstantInt *>(Subrange->getLowerBound());
1752       Lowerbound = (LI) ? LI->getSExtValue() : Lowerbound;
1753       Count = UI->getSExtValue() - Lowerbound + 1;
1754     }
1755 
1756     // Forward declarations of arrays without a size and VLAs use a count of -1.
1757     // Emit a count of zero in these cases to match what MSVC does for arrays
1758     // without a size. MSVC doesn't support VLAs, so it's not clear what we
1759     // should do for them even if we could distinguish them.
1760     if (Count == -1)
1761       Count = 0;
1762 
1763     // Update the element size and element type index for subsequent subranges.
1764     ElementSize *= Count;
1765 
1766     // If this is the outermost array, use the size from the array. It will be
1767     // more accurate if we had a VLA or an incomplete element type size.
1768     uint64_t ArraySize =
1769         (i == 0 && ElementSize == 0) ? Ty->getSizeInBits() / 8 : ElementSize;
1770 
1771     StringRef Name = (i == 0) ? Ty->getName() : "";
1772     ArrayRecord AR(ElementTypeIndex, IndexType, ArraySize, Name);
1773     ElementTypeIndex = TypeTable.writeLeafType(AR);
1774   }
1775 
1776   return ElementTypeIndex;
1777 }
1778 
1779 // This function lowers a Fortran character type (DIStringType).
1780 // Note that it handles only the character*n variant (using SizeInBits
1781 // field in DIString to describe the type size) at the moment.
1782 // Other variants (leveraging the StringLength and StringLengthExp
1783 // fields in DIStringType) remain TBD.
1784 TypeIndex CodeViewDebug::lowerTypeString(const DIStringType *Ty) {
1785   TypeIndex CharType = TypeIndex(SimpleTypeKind::NarrowCharacter);
1786   uint64_t ArraySize = Ty->getSizeInBits() >> 3;
1787   StringRef Name = Ty->getName();
1788   // IndexType is size_t, which depends on the bitness of the target.
1789   TypeIndex IndexType = getPointerSizeInBytes() == 8
1790                             ? TypeIndex(SimpleTypeKind::UInt64Quad)
1791                             : TypeIndex(SimpleTypeKind::UInt32Long);
1792 
1793   // Create a type of character array of ArraySize.
1794   ArrayRecord AR(CharType, IndexType, ArraySize, Name);
1795 
1796   return TypeTable.writeLeafType(AR);
1797 }
1798 
1799 TypeIndex CodeViewDebug::lowerTypeBasic(const DIBasicType *Ty) {
1800   TypeIndex Index;
1801   dwarf::TypeKind Kind;
1802   uint32_t ByteSize;
1803 
1804   Kind = static_cast<dwarf::TypeKind>(Ty->getEncoding());
1805   ByteSize = Ty->getSizeInBits() / 8;
1806 
1807   SimpleTypeKind STK = SimpleTypeKind::None;
1808   switch (Kind) {
1809   case dwarf::DW_ATE_address:
1810     // FIXME: Translate
1811     break;
1812   case dwarf::DW_ATE_boolean:
1813     switch (ByteSize) {
1814     case 1:  STK = SimpleTypeKind::Boolean8;   break;
1815     case 2:  STK = SimpleTypeKind::Boolean16;  break;
1816     case 4:  STK = SimpleTypeKind::Boolean32;  break;
1817     case 8:  STK = SimpleTypeKind::Boolean64;  break;
1818     case 16: STK = SimpleTypeKind::Boolean128; break;
1819     }
1820     break;
1821   case dwarf::DW_ATE_complex_float:
1822     // The CodeView size for a complex represents the size of
1823     // an individual component.
1824     switch (ByteSize) {
1825     case 4:  STK = SimpleTypeKind::Complex16;  break;
1826     case 8:  STK = SimpleTypeKind::Complex32;  break;
1827     case 16: STK = SimpleTypeKind::Complex64;  break;
1828     case 20: STK = SimpleTypeKind::Complex80;  break;
1829     case 32: STK = SimpleTypeKind::Complex128; break;
1830     }
1831     break;
1832   case dwarf::DW_ATE_float:
1833     switch (ByteSize) {
1834     case 2:  STK = SimpleTypeKind::Float16;  break;
1835     case 4:  STK = SimpleTypeKind::Float32;  break;
1836     case 6:  STK = SimpleTypeKind::Float48;  break;
1837     case 8:  STK = SimpleTypeKind::Float64;  break;
1838     case 10: STK = SimpleTypeKind::Float80;  break;
1839     case 16: STK = SimpleTypeKind::Float128; break;
1840     }
1841     break;
1842   case dwarf::DW_ATE_signed:
1843     switch (ByteSize) {
1844     case 1:  STK = SimpleTypeKind::SignedCharacter; break;
1845     case 2:  STK = SimpleTypeKind::Int16Short;      break;
1846     case 4:  STK = SimpleTypeKind::Int32;           break;
1847     case 8:  STK = SimpleTypeKind::Int64Quad;       break;
1848     case 16: STK = SimpleTypeKind::Int128Oct;       break;
1849     }
1850     break;
1851   case dwarf::DW_ATE_unsigned:
1852     switch (ByteSize) {
1853     case 1:  STK = SimpleTypeKind::UnsignedCharacter; break;
1854     case 2:  STK = SimpleTypeKind::UInt16Short;       break;
1855     case 4:  STK = SimpleTypeKind::UInt32;            break;
1856     case 8:  STK = SimpleTypeKind::UInt64Quad;        break;
1857     case 16: STK = SimpleTypeKind::UInt128Oct;        break;
1858     }
1859     break;
1860   case dwarf::DW_ATE_UTF:
1861     switch (ByteSize) {
1862     case 1: STK = SimpleTypeKind::Character8; break;
1863     case 2: STK = SimpleTypeKind::Character16; break;
1864     case 4: STK = SimpleTypeKind::Character32; break;
1865     }
1866     break;
1867   case dwarf::DW_ATE_signed_char:
1868     if (ByteSize == 1)
1869       STK = SimpleTypeKind::SignedCharacter;
1870     break;
1871   case dwarf::DW_ATE_unsigned_char:
1872     if (ByteSize == 1)
1873       STK = SimpleTypeKind::UnsignedCharacter;
1874     break;
1875   default:
1876     break;
1877   }
1878 
1879   // Apply some fixups based on the source-level type name.
1880   // Include some amount of canonicalization from an old naming scheme Clang
1881   // used to use for integer types (in an outdated effort to be compatible with
1882   // GCC's debug info/GDB's behavior, which has since been addressed).
1883   if (STK == SimpleTypeKind::Int32 &&
1884       (Ty->getName() == "long int" || Ty->getName() == "long"))
1885     STK = SimpleTypeKind::Int32Long;
1886   if (STK == SimpleTypeKind::UInt32 && (Ty->getName() == "long unsigned int" ||
1887                                         Ty->getName() == "unsigned long"))
1888     STK = SimpleTypeKind::UInt32Long;
1889   if (STK == SimpleTypeKind::UInt16Short &&
1890       (Ty->getName() == "wchar_t" || Ty->getName() == "__wchar_t"))
1891     STK = SimpleTypeKind::WideCharacter;
1892   if ((STK == SimpleTypeKind::SignedCharacter ||
1893        STK == SimpleTypeKind::UnsignedCharacter) &&
1894       Ty->getName() == "char")
1895     STK = SimpleTypeKind::NarrowCharacter;
1896 
1897   return TypeIndex(STK);
1898 }
1899 
1900 TypeIndex CodeViewDebug::lowerTypePointer(const DIDerivedType *Ty,
1901                                           PointerOptions PO) {
1902   TypeIndex PointeeTI = getTypeIndex(Ty->getBaseType());
1903 
1904   // Pointers to simple types without any options can use SimpleTypeMode, rather
1905   // than having a dedicated pointer type record.
1906   if (PointeeTI.isSimple() && PO == PointerOptions::None &&
1907       PointeeTI.getSimpleMode() == SimpleTypeMode::Direct &&
1908       Ty->getTag() == dwarf::DW_TAG_pointer_type) {
1909     SimpleTypeMode Mode = Ty->getSizeInBits() == 64
1910                               ? SimpleTypeMode::NearPointer64
1911                               : SimpleTypeMode::NearPointer32;
1912     return TypeIndex(PointeeTI.getSimpleKind(), Mode);
1913   }
1914 
1915   PointerKind PK =
1916       Ty->getSizeInBits() == 64 ? PointerKind::Near64 : PointerKind::Near32;
1917   PointerMode PM = PointerMode::Pointer;
1918   switch (Ty->getTag()) {
1919   default: llvm_unreachable("not a pointer tag type");
1920   case dwarf::DW_TAG_pointer_type:
1921     PM = PointerMode::Pointer;
1922     break;
1923   case dwarf::DW_TAG_reference_type:
1924     PM = PointerMode::LValueReference;
1925     break;
1926   case dwarf::DW_TAG_rvalue_reference_type:
1927     PM = PointerMode::RValueReference;
1928     break;
1929   }
1930 
1931   if (Ty->isObjectPointer())
1932     PO |= PointerOptions::Const;
1933 
1934   PointerRecord PR(PointeeTI, PK, PM, PO, Ty->getSizeInBits() / 8);
1935   return TypeTable.writeLeafType(PR);
1936 }
1937 
1938 static PointerToMemberRepresentation
1939 translatePtrToMemberRep(unsigned SizeInBytes, bool IsPMF, unsigned Flags) {
1940   // SizeInBytes being zero generally implies that the member pointer type was
1941   // incomplete, which can happen if it is part of a function prototype. In this
1942   // case, use the unknown model instead of the general model.
1943   if (IsPMF) {
1944     switch (Flags & DINode::FlagPtrToMemberRep) {
1945     case 0:
1946       return SizeInBytes == 0 ? PointerToMemberRepresentation::Unknown
1947                               : PointerToMemberRepresentation::GeneralFunction;
1948     case DINode::FlagSingleInheritance:
1949       return PointerToMemberRepresentation::SingleInheritanceFunction;
1950     case DINode::FlagMultipleInheritance:
1951       return PointerToMemberRepresentation::MultipleInheritanceFunction;
1952     case DINode::FlagVirtualInheritance:
1953       return PointerToMemberRepresentation::VirtualInheritanceFunction;
1954     }
1955   } else {
1956     switch (Flags & DINode::FlagPtrToMemberRep) {
1957     case 0:
1958       return SizeInBytes == 0 ? PointerToMemberRepresentation::Unknown
1959                               : PointerToMemberRepresentation::GeneralData;
1960     case DINode::FlagSingleInheritance:
1961       return PointerToMemberRepresentation::SingleInheritanceData;
1962     case DINode::FlagMultipleInheritance:
1963       return PointerToMemberRepresentation::MultipleInheritanceData;
1964     case DINode::FlagVirtualInheritance:
1965       return PointerToMemberRepresentation::VirtualInheritanceData;
1966     }
1967   }
1968   llvm_unreachable("invalid ptr to member representation");
1969 }
1970 
1971 TypeIndex CodeViewDebug::lowerTypeMemberPointer(const DIDerivedType *Ty,
1972                                                 PointerOptions PO) {
1973   assert(Ty->getTag() == dwarf::DW_TAG_ptr_to_member_type);
1974   bool IsPMF = isa<DISubroutineType>(Ty->getBaseType());
1975   TypeIndex ClassTI = getTypeIndex(Ty->getClassType());
1976   TypeIndex PointeeTI =
1977       getTypeIndex(Ty->getBaseType(), IsPMF ? Ty->getClassType() : nullptr);
1978   PointerKind PK = getPointerSizeInBytes() == 8 ? PointerKind::Near64
1979                                                 : PointerKind::Near32;
1980   PointerMode PM = IsPMF ? PointerMode::PointerToMemberFunction
1981                          : PointerMode::PointerToDataMember;
1982 
1983   assert(Ty->getSizeInBits() / 8 <= 0xff && "pointer size too big");
1984   uint8_t SizeInBytes = Ty->getSizeInBits() / 8;
1985   MemberPointerInfo MPI(
1986       ClassTI, translatePtrToMemberRep(SizeInBytes, IsPMF, Ty->getFlags()));
1987   PointerRecord PR(PointeeTI, PK, PM, PO, SizeInBytes, MPI);
1988   return TypeTable.writeLeafType(PR);
1989 }
1990 
1991 /// Given a DWARF calling convention, get the CodeView equivalent. If we don't
1992 /// have a translation, use the NearC convention.
1993 static CallingConvention dwarfCCToCodeView(unsigned DwarfCC) {
1994   switch (DwarfCC) {
1995   case dwarf::DW_CC_normal:             return CallingConvention::NearC;
1996   case dwarf::DW_CC_BORLAND_msfastcall: return CallingConvention::NearFast;
1997   case dwarf::DW_CC_BORLAND_thiscall:   return CallingConvention::ThisCall;
1998   case dwarf::DW_CC_BORLAND_stdcall:    return CallingConvention::NearStdCall;
1999   case dwarf::DW_CC_BORLAND_pascal:     return CallingConvention::NearPascal;
2000   case dwarf::DW_CC_LLVM_vectorcall:    return CallingConvention::NearVector;
2001   }
2002   return CallingConvention::NearC;
2003 }
2004 
2005 TypeIndex CodeViewDebug::lowerTypeModifier(const DIDerivedType *Ty) {
2006   ModifierOptions Mods = ModifierOptions::None;
2007   PointerOptions PO = PointerOptions::None;
2008   bool IsModifier = true;
2009   const DIType *BaseTy = Ty;
2010   while (IsModifier && BaseTy) {
2011     // FIXME: Need to add DWARF tags for __unaligned and _Atomic
2012     switch (BaseTy->getTag()) {
2013     case dwarf::DW_TAG_const_type:
2014       Mods |= ModifierOptions::Const;
2015       PO |= PointerOptions::Const;
2016       break;
2017     case dwarf::DW_TAG_volatile_type:
2018       Mods |= ModifierOptions::Volatile;
2019       PO |= PointerOptions::Volatile;
2020       break;
2021     case dwarf::DW_TAG_restrict_type:
2022       // Only pointer types be marked with __restrict. There is no known flag
2023       // for __restrict in LF_MODIFIER records.
2024       PO |= PointerOptions::Restrict;
2025       break;
2026     default:
2027       IsModifier = false;
2028       break;
2029     }
2030     if (IsModifier)
2031       BaseTy = cast<DIDerivedType>(BaseTy)->getBaseType();
2032   }
2033 
2034   // Check if the inner type will use an LF_POINTER record. If so, the
2035   // qualifiers will go in the LF_POINTER record. This comes up for types like
2036   // 'int *const' and 'int *__restrict', not the more common cases like 'const
2037   // char *'.
2038   if (BaseTy) {
2039     switch (BaseTy->getTag()) {
2040     case dwarf::DW_TAG_pointer_type:
2041     case dwarf::DW_TAG_reference_type:
2042     case dwarf::DW_TAG_rvalue_reference_type:
2043       return lowerTypePointer(cast<DIDerivedType>(BaseTy), PO);
2044     case dwarf::DW_TAG_ptr_to_member_type:
2045       return lowerTypeMemberPointer(cast<DIDerivedType>(BaseTy), PO);
2046     default:
2047       break;
2048     }
2049   }
2050 
2051   TypeIndex ModifiedTI = getTypeIndex(BaseTy);
2052 
2053   // Return the base type index if there aren't any modifiers. For example, the
2054   // metadata could contain restrict wrappers around non-pointer types.
2055   if (Mods == ModifierOptions::None)
2056     return ModifiedTI;
2057 
2058   ModifierRecord MR(ModifiedTI, Mods);
2059   return TypeTable.writeLeafType(MR);
2060 }
2061 
2062 TypeIndex CodeViewDebug::lowerTypeFunction(const DISubroutineType *Ty) {
2063   SmallVector<TypeIndex, 8> ReturnAndArgTypeIndices;
2064   for (const DIType *ArgType : Ty->getTypeArray())
2065     ReturnAndArgTypeIndices.push_back(getTypeIndex(ArgType));
2066 
2067   // MSVC uses type none for variadic argument.
2068   if (ReturnAndArgTypeIndices.size() > 1 &&
2069       ReturnAndArgTypeIndices.back() == TypeIndex::Void()) {
2070     ReturnAndArgTypeIndices.back() = TypeIndex::None();
2071   }
2072   TypeIndex ReturnTypeIndex = TypeIndex::Void();
2073   ArrayRef<TypeIndex> ArgTypeIndices = std::nullopt;
2074   if (!ReturnAndArgTypeIndices.empty()) {
2075     auto ReturnAndArgTypesRef = ArrayRef(ReturnAndArgTypeIndices);
2076     ReturnTypeIndex = ReturnAndArgTypesRef.front();
2077     ArgTypeIndices = ReturnAndArgTypesRef.drop_front();
2078   }
2079 
2080   ArgListRecord ArgListRec(TypeRecordKind::ArgList, ArgTypeIndices);
2081   TypeIndex ArgListIndex = TypeTable.writeLeafType(ArgListRec);
2082 
2083   CallingConvention CC = dwarfCCToCodeView(Ty->getCC());
2084 
2085   FunctionOptions FO = getFunctionOptions(Ty);
2086   ProcedureRecord Procedure(ReturnTypeIndex, CC, FO, ArgTypeIndices.size(),
2087                             ArgListIndex);
2088   return TypeTable.writeLeafType(Procedure);
2089 }
2090 
2091 TypeIndex CodeViewDebug::lowerTypeMemberFunction(const DISubroutineType *Ty,
2092                                                  const DIType *ClassTy,
2093                                                  int ThisAdjustment,
2094                                                  bool IsStaticMethod,
2095                                                  FunctionOptions FO) {
2096   // Lower the containing class type.
2097   TypeIndex ClassType = getTypeIndex(ClassTy);
2098 
2099   DITypeRefArray ReturnAndArgs = Ty->getTypeArray();
2100 
2101   unsigned Index = 0;
2102   SmallVector<TypeIndex, 8> ArgTypeIndices;
2103   TypeIndex ReturnTypeIndex = TypeIndex::Void();
2104   if (ReturnAndArgs.size() > Index) {
2105     ReturnTypeIndex = getTypeIndex(ReturnAndArgs[Index++]);
2106   }
2107 
2108   // If the first argument is a pointer type and this isn't a static method,
2109   // treat it as the special 'this' parameter, which is encoded separately from
2110   // the arguments.
2111   TypeIndex ThisTypeIndex;
2112   if (!IsStaticMethod && ReturnAndArgs.size() > Index) {
2113     if (const DIDerivedType *PtrTy =
2114             dyn_cast_or_null<DIDerivedType>(ReturnAndArgs[Index])) {
2115       if (PtrTy->getTag() == dwarf::DW_TAG_pointer_type) {
2116         ThisTypeIndex = getTypeIndexForThisPtr(PtrTy, Ty);
2117         Index++;
2118       }
2119     }
2120   }
2121 
2122   while (Index < ReturnAndArgs.size())
2123     ArgTypeIndices.push_back(getTypeIndex(ReturnAndArgs[Index++]));
2124 
2125   // MSVC uses type none for variadic argument.
2126   if (!ArgTypeIndices.empty() && ArgTypeIndices.back() == TypeIndex::Void())
2127     ArgTypeIndices.back() = TypeIndex::None();
2128 
2129   ArgListRecord ArgListRec(TypeRecordKind::ArgList, ArgTypeIndices);
2130   TypeIndex ArgListIndex = TypeTable.writeLeafType(ArgListRec);
2131 
2132   CallingConvention CC = dwarfCCToCodeView(Ty->getCC());
2133 
2134   MemberFunctionRecord MFR(ReturnTypeIndex, ClassType, ThisTypeIndex, CC, FO,
2135                            ArgTypeIndices.size(), ArgListIndex, ThisAdjustment);
2136   return TypeTable.writeLeafType(MFR);
2137 }
2138 
2139 TypeIndex CodeViewDebug::lowerTypeVFTableShape(const DIDerivedType *Ty) {
2140   unsigned VSlotCount =
2141       Ty->getSizeInBits() / (8 * Asm->MAI->getCodePointerSize());
2142   SmallVector<VFTableSlotKind, 4> Slots(VSlotCount, VFTableSlotKind::Near);
2143 
2144   VFTableShapeRecord VFTSR(Slots);
2145   return TypeTable.writeLeafType(VFTSR);
2146 }
2147 
2148 static MemberAccess translateAccessFlags(unsigned RecordTag, unsigned Flags) {
2149   switch (Flags & DINode::FlagAccessibility) {
2150   case DINode::FlagPrivate:   return MemberAccess::Private;
2151   case DINode::FlagPublic:    return MemberAccess::Public;
2152   case DINode::FlagProtected: return MemberAccess::Protected;
2153   case 0:
2154     // If there was no explicit access control, provide the default for the tag.
2155     return RecordTag == dwarf::DW_TAG_class_type ? MemberAccess::Private
2156                                                  : MemberAccess::Public;
2157   }
2158   llvm_unreachable("access flags are exclusive");
2159 }
2160 
2161 static MethodOptions translateMethodOptionFlags(const DISubprogram *SP) {
2162   if (SP->isArtificial())
2163     return MethodOptions::CompilerGenerated;
2164 
2165   // FIXME: Handle other MethodOptions.
2166 
2167   return MethodOptions::None;
2168 }
2169 
2170 static MethodKind translateMethodKindFlags(const DISubprogram *SP,
2171                                            bool Introduced) {
2172   if (SP->getFlags() & DINode::FlagStaticMember)
2173     return MethodKind::Static;
2174 
2175   switch (SP->getVirtuality()) {
2176   case dwarf::DW_VIRTUALITY_none:
2177     break;
2178   case dwarf::DW_VIRTUALITY_virtual:
2179     return Introduced ? MethodKind::IntroducingVirtual : MethodKind::Virtual;
2180   case dwarf::DW_VIRTUALITY_pure_virtual:
2181     return Introduced ? MethodKind::PureIntroducingVirtual
2182                       : MethodKind::PureVirtual;
2183   default:
2184     llvm_unreachable("unhandled virtuality case");
2185   }
2186 
2187   return MethodKind::Vanilla;
2188 }
2189 
2190 static TypeRecordKind getRecordKind(const DICompositeType *Ty) {
2191   switch (Ty->getTag()) {
2192   case dwarf::DW_TAG_class_type:
2193     return TypeRecordKind::Class;
2194   case dwarf::DW_TAG_structure_type:
2195     return TypeRecordKind::Struct;
2196   default:
2197     llvm_unreachable("unexpected tag");
2198   }
2199 }
2200 
2201 /// Return ClassOptions that should be present on both the forward declaration
2202 /// and the defintion of a tag type.
2203 static ClassOptions getCommonClassOptions(const DICompositeType *Ty) {
2204   ClassOptions CO = ClassOptions::None;
2205 
2206   // MSVC always sets this flag, even for local types. Clang doesn't always
2207   // appear to give every type a linkage name, which may be problematic for us.
2208   // FIXME: Investigate the consequences of not following them here.
2209   if (!Ty->getIdentifier().empty())
2210     CO |= ClassOptions::HasUniqueName;
2211 
2212   // Put the Nested flag on a type if it appears immediately inside a tag type.
2213   // Do not walk the scope chain. Do not attempt to compute ContainsNestedClass
2214   // here. That flag is only set on definitions, and not forward declarations.
2215   const DIScope *ImmediateScope = Ty->getScope();
2216   if (ImmediateScope && isa<DICompositeType>(ImmediateScope))
2217     CO |= ClassOptions::Nested;
2218 
2219   // Put the Scoped flag on function-local types. MSVC puts this flag for enum
2220   // type only when it has an immediate function scope. Clang never puts enums
2221   // inside DILexicalBlock scopes. Enum types, as generated by clang, are
2222   // always in function, class, or file scopes.
2223   if (Ty->getTag() == dwarf::DW_TAG_enumeration_type) {
2224     if (ImmediateScope && isa<DISubprogram>(ImmediateScope))
2225       CO |= ClassOptions::Scoped;
2226   } else {
2227     for (const DIScope *Scope = ImmediateScope; Scope != nullptr;
2228          Scope = Scope->getScope()) {
2229       if (isa<DISubprogram>(Scope)) {
2230         CO |= ClassOptions::Scoped;
2231         break;
2232       }
2233     }
2234   }
2235 
2236   return CO;
2237 }
2238 
2239 void CodeViewDebug::addUDTSrcLine(const DIType *Ty, TypeIndex TI) {
2240   switch (Ty->getTag()) {
2241   case dwarf::DW_TAG_class_type:
2242   case dwarf::DW_TAG_structure_type:
2243   case dwarf::DW_TAG_union_type:
2244   case dwarf::DW_TAG_enumeration_type:
2245     break;
2246   default:
2247     return;
2248   }
2249 
2250   if (const auto *File = Ty->getFile()) {
2251     StringIdRecord SIDR(TypeIndex(0x0), getFullFilepath(File));
2252     TypeIndex SIDI = TypeTable.writeLeafType(SIDR);
2253 
2254     UdtSourceLineRecord USLR(TI, SIDI, Ty->getLine());
2255     TypeTable.writeLeafType(USLR);
2256   }
2257 }
2258 
2259 TypeIndex CodeViewDebug::lowerTypeEnum(const DICompositeType *Ty) {
2260   ClassOptions CO = getCommonClassOptions(Ty);
2261   TypeIndex FTI;
2262   unsigned EnumeratorCount = 0;
2263 
2264   if (Ty->isForwardDecl()) {
2265     CO |= ClassOptions::ForwardReference;
2266   } else {
2267     ContinuationRecordBuilder ContinuationBuilder;
2268     ContinuationBuilder.begin(ContinuationRecordKind::FieldList);
2269     for (const DINode *Element : Ty->getElements()) {
2270       // We assume that the frontend provides all members in source declaration
2271       // order, which is what MSVC does.
2272       if (auto *Enumerator = dyn_cast_or_null<DIEnumerator>(Element)) {
2273         // FIXME: Is it correct to always emit these as unsigned here?
2274         EnumeratorRecord ER(MemberAccess::Public,
2275                             APSInt(Enumerator->getValue(), true),
2276                             Enumerator->getName());
2277         ContinuationBuilder.writeMemberType(ER);
2278         EnumeratorCount++;
2279       }
2280     }
2281     FTI = TypeTable.insertRecord(ContinuationBuilder);
2282   }
2283 
2284   std::string FullName = getFullyQualifiedName(Ty);
2285 
2286   EnumRecord ER(EnumeratorCount, CO, FTI, FullName, Ty->getIdentifier(),
2287                 getTypeIndex(Ty->getBaseType()));
2288   TypeIndex EnumTI = TypeTable.writeLeafType(ER);
2289 
2290   addUDTSrcLine(Ty, EnumTI);
2291 
2292   return EnumTI;
2293 }
2294 
2295 //===----------------------------------------------------------------------===//
2296 // ClassInfo
2297 //===----------------------------------------------------------------------===//
2298 
2299 struct llvm::ClassInfo {
2300   struct MemberInfo {
2301     const DIDerivedType *MemberTypeNode;
2302     uint64_t BaseOffset;
2303   };
2304   // [MemberInfo]
2305   using MemberList = std::vector<MemberInfo>;
2306 
2307   using MethodsList = TinyPtrVector<const DISubprogram *>;
2308   // MethodName -> MethodsList
2309   using MethodsMap = MapVector<MDString *, MethodsList>;
2310 
2311   /// Base classes.
2312   std::vector<const DIDerivedType *> Inheritance;
2313 
2314   /// Direct members.
2315   MemberList Members;
2316   // Direct overloaded methods gathered by name.
2317   MethodsMap Methods;
2318 
2319   TypeIndex VShapeTI;
2320 
2321   std::vector<const DIType *> NestedTypes;
2322 };
2323 
2324 void CodeViewDebug::clear() {
2325   assert(CurFn == nullptr);
2326   FileIdMap.clear();
2327   FnDebugInfo.clear();
2328   FileToFilepathMap.clear();
2329   LocalUDTs.clear();
2330   GlobalUDTs.clear();
2331   TypeIndices.clear();
2332   CompleteTypeIndices.clear();
2333   ScopeGlobals.clear();
2334   CVGlobalVariableOffsets.clear();
2335 }
2336 
2337 void CodeViewDebug::collectMemberInfo(ClassInfo &Info,
2338                                       const DIDerivedType *DDTy) {
2339   if (!DDTy->getName().empty()) {
2340     Info.Members.push_back({DDTy, 0});
2341 
2342     // Collect static const data members with values.
2343     if ((DDTy->getFlags() & DINode::FlagStaticMember) ==
2344         DINode::FlagStaticMember) {
2345       if (DDTy->getConstant() && (isa<ConstantInt>(DDTy->getConstant()) ||
2346                                   isa<ConstantFP>(DDTy->getConstant())))
2347         StaticConstMembers.push_back(DDTy);
2348     }
2349 
2350     return;
2351   }
2352 
2353   // An unnamed member may represent a nested struct or union. Attempt to
2354   // interpret the unnamed member as a DICompositeType possibly wrapped in
2355   // qualifier types. Add all the indirect fields to the current record if that
2356   // succeeds, and drop the member if that fails.
2357   assert((DDTy->getOffsetInBits() % 8) == 0 && "Unnamed bitfield member!");
2358   uint64_t Offset = DDTy->getOffsetInBits();
2359   const DIType *Ty = DDTy->getBaseType();
2360   bool FullyResolved = false;
2361   while (!FullyResolved) {
2362     switch (Ty->getTag()) {
2363     case dwarf::DW_TAG_const_type:
2364     case dwarf::DW_TAG_volatile_type:
2365       // FIXME: we should apply the qualifier types to the indirect fields
2366       // rather than dropping them.
2367       Ty = cast<DIDerivedType>(Ty)->getBaseType();
2368       break;
2369     default:
2370       FullyResolved = true;
2371       break;
2372     }
2373   }
2374 
2375   const DICompositeType *DCTy = dyn_cast<DICompositeType>(Ty);
2376   if (!DCTy)
2377     return;
2378 
2379   ClassInfo NestedInfo = collectClassInfo(DCTy);
2380   for (const ClassInfo::MemberInfo &IndirectField : NestedInfo.Members)
2381     Info.Members.push_back(
2382         {IndirectField.MemberTypeNode, IndirectField.BaseOffset + Offset});
2383 }
2384 
2385 ClassInfo CodeViewDebug::collectClassInfo(const DICompositeType *Ty) {
2386   ClassInfo Info;
2387   // Add elements to structure type.
2388   DINodeArray Elements = Ty->getElements();
2389   for (auto *Element : Elements) {
2390     // We assume that the frontend provides all members in source declaration
2391     // order, which is what MSVC does.
2392     if (!Element)
2393       continue;
2394     if (auto *SP = dyn_cast<DISubprogram>(Element)) {
2395       Info.Methods[SP->getRawName()].push_back(SP);
2396     } else if (auto *DDTy = dyn_cast<DIDerivedType>(Element)) {
2397       if (DDTy->getTag() == dwarf::DW_TAG_member) {
2398         collectMemberInfo(Info, DDTy);
2399       } else if (DDTy->getTag() == dwarf::DW_TAG_inheritance) {
2400         Info.Inheritance.push_back(DDTy);
2401       } else if (DDTy->getTag() == dwarf::DW_TAG_pointer_type &&
2402                  DDTy->getName() == "__vtbl_ptr_type") {
2403         Info.VShapeTI = getTypeIndex(DDTy);
2404       } else if (DDTy->getTag() == dwarf::DW_TAG_typedef) {
2405         Info.NestedTypes.push_back(DDTy);
2406       } else if (DDTy->getTag() == dwarf::DW_TAG_friend) {
2407         // Ignore friend members. It appears that MSVC emitted info about
2408         // friends in the past, but modern versions do not.
2409       }
2410     } else if (auto *Composite = dyn_cast<DICompositeType>(Element)) {
2411       Info.NestedTypes.push_back(Composite);
2412     }
2413     // Skip other unrecognized kinds of elements.
2414   }
2415   return Info;
2416 }
2417 
2418 static bool shouldAlwaysEmitCompleteClassType(const DICompositeType *Ty) {
2419   // This routine is used by lowerTypeClass and lowerTypeUnion to determine
2420   // if a complete type should be emitted instead of a forward reference.
2421   return Ty->getName().empty() && Ty->getIdentifier().empty() &&
2422       !Ty->isForwardDecl();
2423 }
2424 
2425 TypeIndex CodeViewDebug::lowerTypeClass(const DICompositeType *Ty) {
2426   // Emit the complete type for unnamed structs.  C++ classes with methods
2427   // which have a circular reference back to the class type are expected to
2428   // be named by the front-end and should not be "unnamed".  C unnamed
2429   // structs should not have circular references.
2430   if (shouldAlwaysEmitCompleteClassType(Ty)) {
2431     // If this unnamed complete type is already in the process of being defined
2432     // then the description of the type is malformed and cannot be emitted
2433     // into CodeView correctly so report a fatal error.
2434     auto I = CompleteTypeIndices.find(Ty);
2435     if (I != CompleteTypeIndices.end() && I->second == TypeIndex())
2436       report_fatal_error("cannot debug circular reference to unnamed type");
2437     return getCompleteTypeIndex(Ty);
2438   }
2439 
2440   // First, construct the forward decl.  Don't look into Ty to compute the
2441   // forward decl options, since it might not be available in all TUs.
2442   TypeRecordKind Kind = getRecordKind(Ty);
2443   ClassOptions CO =
2444       ClassOptions::ForwardReference | getCommonClassOptions(Ty);
2445   std::string FullName = getFullyQualifiedName(Ty);
2446   ClassRecord CR(Kind, 0, CO, TypeIndex(), TypeIndex(), TypeIndex(), 0,
2447                  FullName, Ty->getIdentifier());
2448   TypeIndex FwdDeclTI = TypeTable.writeLeafType(CR);
2449   if (!Ty->isForwardDecl())
2450     DeferredCompleteTypes.push_back(Ty);
2451   return FwdDeclTI;
2452 }
2453 
2454 TypeIndex CodeViewDebug::lowerCompleteTypeClass(const DICompositeType *Ty) {
2455   // Construct the field list and complete type record.
2456   TypeRecordKind Kind = getRecordKind(Ty);
2457   ClassOptions CO = getCommonClassOptions(Ty);
2458   TypeIndex FieldTI;
2459   TypeIndex VShapeTI;
2460   unsigned FieldCount;
2461   bool ContainsNestedClass;
2462   std::tie(FieldTI, VShapeTI, FieldCount, ContainsNestedClass) =
2463       lowerRecordFieldList(Ty);
2464 
2465   if (ContainsNestedClass)
2466     CO |= ClassOptions::ContainsNestedClass;
2467 
2468   // MSVC appears to set this flag by searching any destructor or method with
2469   // FunctionOptions::Constructor among the emitted members. Clang AST has all
2470   // the members, however special member functions are not yet emitted into
2471   // debug information. For now checking a class's non-triviality seems enough.
2472   // FIXME: not true for a nested unnamed struct.
2473   if (isNonTrivial(Ty))
2474     CO |= ClassOptions::HasConstructorOrDestructor;
2475 
2476   std::string FullName = getFullyQualifiedName(Ty);
2477 
2478   uint64_t SizeInBytes = Ty->getSizeInBits() / 8;
2479 
2480   ClassRecord CR(Kind, FieldCount, CO, FieldTI, TypeIndex(), VShapeTI,
2481                  SizeInBytes, FullName, Ty->getIdentifier());
2482   TypeIndex ClassTI = TypeTable.writeLeafType(CR);
2483 
2484   addUDTSrcLine(Ty, ClassTI);
2485 
2486   addToUDTs(Ty);
2487 
2488   return ClassTI;
2489 }
2490 
2491 TypeIndex CodeViewDebug::lowerTypeUnion(const DICompositeType *Ty) {
2492   // Emit the complete type for unnamed unions.
2493   if (shouldAlwaysEmitCompleteClassType(Ty))
2494     return getCompleteTypeIndex(Ty);
2495 
2496   ClassOptions CO =
2497       ClassOptions::ForwardReference | getCommonClassOptions(Ty);
2498   std::string FullName = getFullyQualifiedName(Ty);
2499   UnionRecord UR(0, CO, TypeIndex(), 0, FullName, Ty->getIdentifier());
2500   TypeIndex FwdDeclTI = TypeTable.writeLeafType(UR);
2501   if (!Ty->isForwardDecl())
2502     DeferredCompleteTypes.push_back(Ty);
2503   return FwdDeclTI;
2504 }
2505 
2506 TypeIndex CodeViewDebug::lowerCompleteTypeUnion(const DICompositeType *Ty) {
2507   ClassOptions CO = ClassOptions::Sealed | getCommonClassOptions(Ty);
2508   TypeIndex FieldTI;
2509   unsigned FieldCount;
2510   bool ContainsNestedClass;
2511   std::tie(FieldTI, std::ignore, FieldCount, ContainsNestedClass) =
2512       lowerRecordFieldList(Ty);
2513 
2514   if (ContainsNestedClass)
2515     CO |= ClassOptions::ContainsNestedClass;
2516 
2517   uint64_t SizeInBytes = Ty->getSizeInBits() / 8;
2518   std::string FullName = getFullyQualifiedName(Ty);
2519 
2520   UnionRecord UR(FieldCount, CO, FieldTI, SizeInBytes, FullName,
2521                  Ty->getIdentifier());
2522   TypeIndex UnionTI = TypeTable.writeLeafType(UR);
2523 
2524   addUDTSrcLine(Ty, UnionTI);
2525 
2526   addToUDTs(Ty);
2527 
2528   return UnionTI;
2529 }
2530 
2531 std::tuple<TypeIndex, TypeIndex, unsigned, bool>
2532 CodeViewDebug::lowerRecordFieldList(const DICompositeType *Ty) {
2533   // Manually count members. MSVC appears to count everything that generates a
2534   // field list record. Each individual overload in a method overload group
2535   // contributes to this count, even though the overload group is a single field
2536   // list record.
2537   unsigned MemberCount = 0;
2538   ClassInfo Info = collectClassInfo(Ty);
2539   ContinuationRecordBuilder ContinuationBuilder;
2540   ContinuationBuilder.begin(ContinuationRecordKind::FieldList);
2541 
2542   // Create base classes.
2543   for (const DIDerivedType *I : Info.Inheritance) {
2544     if (I->getFlags() & DINode::FlagVirtual) {
2545       // Virtual base.
2546       unsigned VBPtrOffset = I->getVBPtrOffset();
2547       // FIXME: Despite the accessor name, the offset is really in bytes.
2548       unsigned VBTableIndex = I->getOffsetInBits() / 4;
2549       auto RecordKind = (I->getFlags() & DINode::FlagIndirectVirtualBase) == DINode::FlagIndirectVirtualBase
2550                             ? TypeRecordKind::IndirectVirtualBaseClass
2551                             : TypeRecordKind::VirtualBaseClass;
2552       VirtualBaseClassRecord VBCR(
2553           RecordKind, translateAccessFlags(Ty->getTag(), I->getFlags()),
2554           getTypeIndex(I->getBaseType()), getVBPTypeIndex(), VBPtrOffset,
2555           VBTableIndex);
2556 
2557       ContinuationBuilder.writeMemberType(VBCR);
2558       MemberCount++;
2559     } else {
2560       assert(I->getOffsetInBits() % 8 == 0 &&
2561              "bases must be on byte boundaries");
2562       BaseClassRecord BCR(translateAccessFlags(Ty->getTag(), I->getFlags()),
2563                           getTypeIndex(I->getBaseType()),
2564                           I->getOffsetInBits() / 8);
2565       ContinuationBuilder.writeMemberType(BCR);
2566       MemberCount++;
2567     }
2568   }
2569 
2570   // Create members.
2571   for (ClassInfo::MemberInfo &MemberInfo : Info.Members) {
2572     const DIDerivedType *Member = MemberInfo.MemberTypeNode;
2573     TypeIndex MemberBaseType = getTypeIndex(Member->getBaseType());
2574     StringRef MemberName = Member->getName();
2575     MemberAccess Access =
2576         translateAccessFlags(Ty->getTag(), Member->getFlags());
2577 
2578     if (Member->isStaticMember()) {
2579       StaticDataMemberRecord SDMR(Access, MemberBaseType, MemberName);
2580       ContinuationBuilder.writeMemberType(SDMR);
2581       MemberCount++;
2582       continue;
2583     }
2584 
2585     // Virtual function pointer member.
2586     if ((Member->getFlags() & DINode::FlagArtificial) &&
2587         Member->getName().startswith("_vptr$")) {
2588       VFPtrRecord VFPR(getTypeIndex(Member->getBaseType()));
2589       ContinuationBuilder.writeMemberType(VFPR);
2590       MemberCount++;
2591       continue;
2592     }
2593 
2594     // Data member.
2595     uint64_t MemberOffsetInBits =
2596         Member->getOffsetInBits() + MemberInfo.BaseOffset;
2597     if (Member->isBitField()) {
2598       uint64_t StartBitOffset = MemberOffsetInBits;
2599       if (const auto *CI =
2600               dyn_cast_or_null<ConstantInt>(Member->getStorageOffsetInBits())) {
2601         MemberOffsetInBits = CI->getZExtValue() + MemberInfo.BaseOffset;
2602       }
2603       StartBitOffset -= MemberOffsetInBits;
2604       BitFieldRecord BFR(MemberBaseType, Member->getSizeInBits(),
2605                          StartBitOffset);
2606       MemberBaseType = TypeTable.writeLeafType(BFR);
2607     }
2608     uint64_t MemberOffsetInBytes = MemberOffsetInBits / 8;
2609     DataMemberRecord DMR(Access, MemberBaseType, MemberOffsetInBytes,
2610                          MemberName);
2611     ContinuationBuilder.writeMemberType(DMR);
2612     MemberCount++;
2613   }
2614 
2615   // Create methods
2616   for (auto &MethodItr : Info.Methods) {
2617     StringRef Name = MethodItr.first->getString();
2618 
2619     std::vector<OneMethodRecord> Methods;
2620     for (const DISubprogram *SP : MethodItr.second) {
2621       TypeIndex MethodType = getMemberFunctionType(SP, Ty);
2622       bool Introduced = SP->getFlags() & DINode::FlagIntroducedVirtual;
2623 
2624       unsigned VFTableOffset = -1;
2625       if (Introduced)
2626         VFTableOffset = SP->getVirtualIndex() * getPointerSizeInBytes();
2627 
2628       Methods.push_back(OneMethodRecord(
2629           MethodType, translateAccessFlags(Ty->getTag(), SP->getFlags()),
2630           translateMethodKindFlags(SP, Introduced),
2631           translateMethodOptionFlags(SP), VFTableOffset, Name));
2632       MemberCount++;
2633     }
2634     assert(!Methods.empty() && "Empty methods map entry");
2635     if (Methods.size() == 1)
2636       ContinuationBuilder.writeMemberType(Methods[0]);
2637     else {
2638       // FIXME: Make this use its own ContinuationBuilder so that
2639       // MethodOverloadList can be split correctly.
2640       MethodOverloadListRecord MOLR(Methods);
2641       TypeIndex MethodList = TypeTable.writeLeafType(MOLR);
2642 
2643       OverloadedMethodRecord OMR(Methods.size(), MethodList, Name);
2644       ContinuationBuilder.writeMemberType(OMR);
2645     }
2646   }
2647 
2648   // Create nested classes.
2649   for (const DIType *Nested : Info.NestedTypes) {
2650     NestedTypeRecord R(getTypeIndex(Nested), Nested->getName());
2651     ContinuationBuilder.writeMemberType(R);
2652     MemberCount++;
2653   }
2654 
2655   TypeIndex FieldTI = TypeTable.insertRecord(ContinuationBuilder);
2656   return std::make_tuple(FieldTI, Info.VShapeTI, MemberCount,
2657                          !Info.NestedTypes.empty());
2658 }
2659 
2660 TypeIndex CodeViewDebug::getVBPTypeIndex() {
2661   if (!VBPType.getIndex()) {
2662     // Make a 'const int *' type.
2663     ModifierRecord MR(TypeIndex::Int32(), ModifierOptions::Const);
2664     TypeIndex ModifiedTI = TypeTable.writeLeafType(MR);
2665 
2666     PointerKind PK = getPointerSizeInBytes() == 8 ? PointerKind::Near64
2667                                                   : PointerKind::Near32;
2668     PointerMode PM = PointerMode::Pointer;
2669     PointerOptions PO = PointerOptions::None;
2670     PointerRecord PR(ModifiedTI, PK, PM, PO, getPointerSizeInBytes());
2671     VBPType = TypeTable.writeLeafType(PR);
2672   }
2673 
2674   return VBPType;
2675 }
2676 
2677 TypeIndex CodeViewDebug::getTypeIndex(const DIType *Ty, const DIType *ClassTy) {
2678   // The null DIType is the void type. Don't try to hash it.
2679   if (!Ty)
2680     return TypeIndex::Void();
2681 
2682   // Check if we've already translated this type. Don't try to do a
2683   // get-or-create style insertion that caches the hash lookup across the
2684   // lowerType call. It will update the TypeIndices map.
2685   auto I = TypeIndices.find({Ty, ClassTy});
2686   if (I != TypeIndices.end())
2687     return I->second;
2688 
2689   TypeLoweringScope S(*this);
2690   TypeIndex TI = lowerType(Ty, ClassTy);
2691   return recordTypeIndexForDINode(Ty, TI, ClassTy);
2692 }
2693 
2694 codeview::TypeIndex
2695 CodeViewDebug::getTypeIndexForThisPtr(const DIDerivedType *PtrTy,
2696                                       const DISubroutineType *SubroutineTy) {
2697   assert(PtrTy->getTag() == dwarf::DW_TAG_pointer_type &&
2698          "this type must be a pointer type");
2699 
2700   PointerOptions Options = PointerOptions::None;
2701   if (SubroutineTy->getFlags() & DINode::DIFlags::FlagLValueReference)
2702     Options = PointerOptions::LValueRefThisPointer;
2703   else if (SubroutineTy->getFlags() & DINode::DIFlags::FlagRValueReference)
2704     Options = PointerOptions::RValueRefThisPointer;
2705 
2706   // Check if we've already translated this type.  If there is no ref qualifier
2707   // on the function then we look up this pointer type with no associated class
2708   // so that the TypeIndex for the this pointer can be shared with the type
2709   // index for other pointers to this class type.  If there is a ref qualifier
2710   // then we lookup the pointer using the subroutine as the parent type.
2711   auto I = TypeIndices.find({PtrTy, SubroutineTy});
2712   if (I != TypeIndices.end())
2713     return I->second;
2714 
2715   TypeLoweringScope S(*this);
2716   TypeIndex TI = lowerTypePointer(PtrTy, Options);
2717   return recordTypeIndexForDINode(PtrTy, TI, SubroutineTy);
2718 }
2719 
2720 TypeIndex CodeViewDebug::getTypeIndexForReferenceTo(const DIType *Ty) {
2721   PointerRecord PR(getTypeIndex(Ty),
2722                    getPointerSizeInBytes() == 8 ? PointerKind::Near64
2723                                                 : PointerKind::Near32,
2724                    PointerMode::LValueReference, PointerOptions::None,
2725                    Ty->getSizeInBits() / 8);
2726   return TypeTable.writeLeafType(PR);
2727 }
2728 
2729 TypeIndex CodeViewDebug::getCompleteTypeIndex(const DIType *Ty) {
2730   // The null DIType is the void type. Don't try to hash it.
2731   if (!Ty)
2732     return TypeIndex::Void();
2733 
2734   // Look through typedefs when getting the complete type index. Call
2735   // getTypeIndex on the typdef to ensure that any UDTs are accumulated and are
2736   // emitted only once.
2737   if (Ty->getTag() == dwarf::DW_TAG_typedef)
2738     (void)getTypeIndex(Ty);
2739   while (Ty->getTag() == dwarf::DW_TAG_typedef)
2740     Ty = cast<DIDerivedType>(Ty)->getBaseType();
2741 
2742   // If this is a non-record type, the complete type index is the same as the
2743   // normal type index. Just call getTypeIndex.
2744   switch (Ty->getTag()) {
2745   case dwarf::DW_TAG_class_type:
2746   case dwarf::DW_TAG_structure_type:
2747   case dwarf::DW_TAG_union_type:
2748     break;
2749   default:
2750     return getTypeIndex(Ty);
2751   }
2752 
2753   const auto *CTy = cast<DICompositeType>(Ty);
2754 
2755   TypeLoweringScope S(*this);
2756 
2757   // Make sure the forward declaration is emitted first. It's unclear if this
2758   // is necessary, but MSVC does it, and we should follow suit until we can show
2759   // otherwise.
2760   // We only emit a forward declaration for named types.
2761   if (!CTy->getName().empty() || !CTy->getIdentifier().empty()) {
2762     TypeIndex FwdDeclTI = getTypeIndex(CTy);
2763 
2764     // Just use the forward decl if we don't have complete type info. This
2765     // might happen if the frontend is using modules and expects the complete
2766     // definition to be emitted elsewhere.
2767     if (CTy->isForwardDecl())
2768       return FwdDeclTI;
2769   }
2770 
2771   // Check if we've already translated the complete record type.
2772   // Insert the type with a null TypeIndex to signify that the type is currently
2773   // being lowered.
2774   auto InsertResult = CompleteTypeIndices.insert({CTy, TypeIndex()});
2775   if (!InsertResult.second)
2776     return InsertResult.first->second;
2777 
2778   TypeIndex TI;
2779   switch (CTy->getTag()) {
2780   case dwarf::DW_TAG_class_type:
2781   case dwarf::DW_TAG_structure_type:
2782     TI = lowerCompleteTypeClass(CTy);
2783     break;
2784   case dwarf::DW_TAG_union_type:
2785     TI = lowerCompleteTypeUnion(CTy);
2786     break;
2787   default:
2788     llvm_unreachable("not a record");
2789   }
2790 
2791   // Update the type index associated with this CompositeType.  This cannot
2792   // use the 'InsertResult' iterator above because it is potentially
2793   // invalidated by map insertions which can occur while lowering the class
2794   // type above.
2795   CompleteTypeIndices[CTy] = TI;
2796   return TI;
2797 }
2798 
2799 /// Emit all the deferred complete record types. Try to do this in FIFO order,
2800 /// and do this until fixpoint, as each complete record type typically
2801 /// references
2802 /// many other record types.
2803 void CodeViewDebug::emitDeferredCompleteTypes() {
2804   SmallVector<const DICompositeType *, 4> TypesToEmit;
2805   while (!DeferredCompleteTypes.empty()) {
2806     std::swap(DeferredCompleteTypes, TypesToEmit);
2807     for (const DICompositeType *RecordTy : TypesToEmit)
2808       getCompleteTypeIndex(RecordTy);
2809     TypesToEmit.clear();
2810   }
2811 }
2812 
2813 void CodeViewDebug::emitLocalVariableList(const FunctionInfo &FI,
2814                                           ArrayRef<LocalVariable> Locals) {
2815   // Get the sorted list of parameters and emit them first.
2816   SmallVector<const LocalVariable *, 6> Params;
2817   for (const LocalVariable &L : Locals)
2818     if (L.DIVar->isParameter())
2819       Params.push_back(&L);
2820   llvm::sort(Params, [](const LocalVariable *L, const LocalVariable *R) {
2821     return L->DIVar->getArg() < R->DIVar->getArg();
2822   });
2823   for (const LocalVariable *L : Params)
2824     emitLocalVariable(FI, *L);
2825 
2826   // Next emit all non-parameters in the order that we found them.
2827   for (const LocalVariable &L : Locals) {
2828     if (!L.DIVar->isParameter()) {
2829       if (L.ConstantValue) {
2830         // If ConstantValue is set we will emit it as a S_CONSTANT instead of a
2831         // S_LOCAL in order to be able to represent it at all.
2832         const DIType *Ty = L.DIVar->getType();
2833         APSInt Val(*L.ConstantValue);
2834         emitConstantSymbolRecord(Ty, Val, std::string(L.DIVar->getName()));
2835       } else {
2836         emitLocalVariable(FI, L);
2837       }
2838     }
2839   }
2840 }
2841 
2842 void CodeViewDebug::emitLocalVariable(const FunctionInfo &FI,
2843                                       const LocalVariable &Var) {
2844   // LocalSym record, see SymbolRecord.h for more info.
2845   MCSymbol *LocalEnd = beginSymbolRecord(SymbolKind::S_LOCAL);
2846 
2847   LocalSymFlags Flags = LocalSymFlags::None;
2848   if (Var.DIVar->isParameter())
2849     Flags |= LocalSymFlags::IsParameter;
2850   if (Var.DefRanges.empty())
2851     Flags |= LocalSymFlags::IsOptimizedOut;
2852 
2853   OS.AddComment("TypeIndex");
2854   TypeIndex TI = Var.UseReferenceType
2855                      ? getTypeIndexForReferenceTo(Var.DIVar->getType())
2856                      : getCompleteTypeIndex(Var.DIVar->getType());
2857   OS.emitInt32(TI.getIndex());
2858   OS.AddComment("Flags");
2859   OS.emitInt16(static_cast<uint16_t>(Flags));
2860   // Truncate the name so we won't overflow the record length field.
2861   emitNullTerminatedSymbolName(OS, Var.DIVar->getName());
2862   endSymbolRecord(LocalEnd);
2863 
2864   // Calculate the on disk prefix of the appropriate def range record. The
2865   // records and on disk formats are described in SymbolRecords.h. BytePrefix
2866   // should be big enough to hold all forms without memory allocation.
2867   SmallString<20> BytePrefix;
2868   for (const auto &Pair : Var.DefRanges) {
2869     LocalVarDef DefRange = Pair.first;
2870     const auto &Ranges = Pair.second;
2871     BytePrefix.clear();
2872     if (DefRange.InMemory) {
2873       int Offset = DefRange.DataOffset;
2874       unsigned Reg = DefRange.CVRegister;
2875 
2876       // 32-bit x86 call sequences often use PUSH instructions, which disrupt
2877       // ESP-relative offsets. Use the virtual frame pointer, VFRAME or $T0,
2878       // instead. In frames without stack realignment, $T0 will be the CFA.
2879       if (RegisterId(Reg) == RegisterId::ESP) {
2880         Reg = unsigned(RegisterId::VFRAME);
2881         Offset += FI.OffsetAdjustment;
2882       }
2883 
2884       // If we can use the chosen frame pointer for the frame and this isn't a
2885       // sliced aggregate, use the smaller S_DEFRANGE_FRAMEPOINTER_REL record.
2886       // Otherwise, use S_DEFRANGE_REGISTER_REL.
2887       EncodedFramePtrReg EncFP = encodeFramePtrReg(RegisterId(Reg), TheCPU);
2888       if (!DefRange.IsSubfield && EncFP != EncodedFramePtrReg::None &&
2889           (bool(Flags & LocalSymFlags::IsParameter)
2890                ? (EncFP == FI.EncodedParamFramePtrReg)
2891                : (EncFP == FI.EncodedLocalFramePtrReg))) {
2892         DefRangeFramePointerRelHeader DRHdr;
2893         DRHdr.Offset = Offset;
2894         OS.emitCVDefRangeDirective(Ranges, DRHdr);
2895       } else {
2896         uint16_t RegRelFlags = 0;
2897         if (DefRange.IsSubfield) {
2898           RegRelFlags = DefRangeRegisterRelSym::IsSubfieldFlag |
2899                         (DefRange.StructOffset
2900                          << DefRangeRegisterRelSym::OffsetInParentShift);
2901         }
2902         DefRangeRegisterRelHeader DRHdr;
2903         DRHdr.Register = Reg;
2904         DRHdr.Flags = RegRelFlags;
2905         DRHdr.BasePointerOffset = Offset;
2906         OS.emitCVDefRangeDirective(Ranges, DRHdr);
2907       }
2908     } else {
2909       assert(DefRange.DataOffset == 0 && "unexpected offset into register");
2910       if (DefRange.IsSubfield) {
2911         DefRangeSubfieldRegisterHeader DRHdr;
2912         DRHdr.Register = DefRange.CVRegister;
2913         DRHdr.MayHaveNoName = 0;
2914         DRHdr.OffsetInParent = DefRange.StructOffset;
2915         OS.emitCVDefRangeDirective(Ranges, DRHdr);
2916       } else {
2917         DefRangeRegisterHeader DRHdr;
2918         DRHdr.Register = DefRange.CVRegister;
2919         DRHdr.MayHaveNoName = 0;
2920         OS.emitCVDefRangeDirective(Ranges, DRHdr);
2921       }
2922     }
2923   }
2924 }
2925 
2926 void CodeViewDebug::emitLexicalBlockList(ArrayRef<LexicalBlock *> Blocks,
2927                                          const FunctionInfo& FI) {
2928   for (LexicalBlock *Block : Blocks)
2929     emitLexicalBlock(*Block, FI);
2930 }
2931 
2932 /// Emit an S_BLOCK32 and S_END record pair delimiting the contents of a
2933 /// lexical block scope.
2934 void CodeViewDebug::emitLexicalBlock(const LexicalBlock &Block,
2935                                      const FunctionInfo& FI) {
2936   MCSymbol *RecordEnd = beginSymbolRecord(SymbolKind::S_BLOCK32);
2937   OS.AddComment("PtrParent");
2938   OS.emitInt32(0); // PtrParent
2939   OS.AddComment("PtrEnd");
2940   OS.emitInt32(0); // PtrEnd
2941   OS.AddComment("Code size");
2942   OS.emitAbsoluteSymbolDiff(Block.End, Block.Begin, 4);   // Code Size
2943   OS.AddComment("Function section relative address");
2944   OS.emitCOFFSecRel32(Block.Begin, /*Offset=*/0); // Func Offset
2945   OS.AddComment("Function section index");
2946   OS.emitCOFFSectionIndex(FI.Begin); // Func Symbol
2947   OS.AddComment("Lexical block name");
2948   emitNullTerminatedSymbolName(OS, Block.Name);           // Name
2949   endSymbolRecord(RecordEnd);
2950 
2951   // Emit variables local to this lexical block.
2952   emitLocalVariableList(FI, Block.Locals);
2953   emitGlobalVariableList(Block.Globals);
2954 
2955   // Emit lexical blocks contained within this block.
2956   emitLexicalBlockList(Block.Children, FI);
2957 
2958   // Close the lexical block scope.
2959   emitEndSymbolRecord(SymbolKind::S_END);
2960 }
2961 
2962 /// Convenience routine for collecting lexical block information for a list
2963 /// of lexical scopes.
2964 void CodeViewDebug::collectLexicalBlockInfo(
2965         SmallVectorImpl<LexicalScope *> &Scopes,
2966         SmallVectorImpl<LexicalBlock *> &Blocks,
2967         SmallVectorImpl<LocalVariable> &Locals,
2968         SmallVectorImpl<CVGlobalVariable> &Globals) {
2969   for (LexicalScope *Scope : Scopes)
2970     collectLexicalBlockInfo(*Scope, Blocks, Locals, Globals);
2971 }
2972 
2973 /// Populate the lexical blocks and local variable lists of the parent with
2974 /// information about the specified lexical scope.
2975 void CodeViewDebug::collectLexicalBlockInfo(
2976     LexicalScope &Scope,
2977     SmallVectorImpl<LexicalBlock *> &ParentBlocks,
2978     SmallVectorImpl<LocalVariable> &ParentLocals,
2979     SmallVectorImpl<CVGlobalVariable> &ParentGlobals) {
2980   if (Scope.isAbstractScope())
2981     return;
2982 
2983   // Gather information about the lexical scope including local variables,
2984   // global variables, and address ranges.
2985   bool IgnoreScope = false;
2986   auto LI = ScopeVariables.find(&Scope);
2987   SmallVectorImpl<LocalVariable> *Locals =
2988       LI != ScopeVariables.end() ? &LI->second : nullptr;
2989   auto GI = ScopeGlobals.find(Scope.getScopeNode());
2990   SmallVectorImpl<CVGlobalVariable> *Globals =
2991       GI != ScopeGlobals.end() ? GI->second.get() : nullptr;
2992   const DILexicalBlock *DILB = dyn_cast<DILexicalBlock>(Scope.getScopeNode());
2993   const SmallVectorImpl<InsnRange> &Ranges = Scope.getRanges();
2994 
2995   // Ignore lexical scopes which do not contain variables.
2996   if (!Locals && !Globals)
2997     IgnoreScope = true;
2998 
2999   // Ignore lexical scopes which are not lexical blocks.
3000   if (!DILB)
3001     IgnoreScope = true;
3002 
3003   // Ignore scopes which have too many address ranges to represent in the
3004   // current CodeView format or do not have a valid address range.
3005   //
3006   // For lexical scopes with multiple address ranges you may be tempted to
3007   // construct a single range covering every instruction where the block is
3008   // live and everything in between.  Unfortunately, Visual Studio only
3009   // displays variables from the first matching lexical block scope.  If the
3010   // first lexical block contains exception handling code or cold code which
3011   // is moved to the bottom of the routine creating a single range covering
3012   // nearly the entire routine, then it will hide all other lexical blocks
3013   // and the variables they contain.
3014   if (Ranges.size() != 1 || !getLabelAfterInsn(Ranges.front().second))
3015     IgnoreScope = true;
3016 
3017   if (IgnoreScope) {
3018     // This scope can be safely ignored and eliminating it will reduce the
3019     // size of the debug information. Be sure to collect any variable and scope
3020     // information from the this scope or any of its children and collapse them
3021     // into the parent scope.
3022     if (Locals)
3023       ParentLocals.append(Locals->begin(), Locals->end());
3024     if (Globals)
3025       ParentGlobals.append(Globals->begin(), Globals->end());
3026     collectLexicalBlockInfo(Scope.getChildren(),
3027                             ParentBlocks,
3028                             ParentLocals,
3029                             ParentGlobals);
3030     return;
3031   }
3032 
3033   // Create a new CodeView lexical block for this lexical scope.  If we've
3034   // seen this DILexicalBlock before then the scope tree is malformed and
3035   // we can handle this gracefully by not processing it a second time.
3036   auto BlockInsertion = CurFn->LexicalBlocks.insert({DILB, LexicalBlock()});
3037   if (!BlockInsertion.second)
3038     return;
3039 
3040   // Create a lexical block containing the variables and collect the
3041   // lexical block information for the children.
3042   const InsnRange &Range = Ranges.front();
3043   assert(Range.first && Range.second);
3044   LexicalBlock &Block = BlockInsertion.first->second;
3045   Block.Begin = getLabelBeforeInsn(Range.first);
3046   Block.End = getLabelAfterInsn(Range.second);
3047   assert(Block.Begin && "missing label for scope begin");
3048   assert(Block.End && "missing label for scope end");
3049   Block.Name = DILB->getName();
3050   if (Locals)
3051     Block.Locals = std::move(*Locals);
3052   if (Globals)
3053     Block.Globals = std::move(*Globals);
3054   ParentBlocks.push_back(&Block);
3055   collectLexicalBlockInfo(Scope.getChildren(),
3056                           Block.Children,
3057                           Block.Locals,
3058                           Block.Globals);
3059 }
3060 
3061 void CodeViewDebug::endFunctionImpl(const MachineFunction *MF) {
3062   const Function &GV = MF->getFunction();
3063   assert(FnDebugInfo.count(&GV));
3064   assert(CurFn == FnDebugInfo[&GV].get());
3065 
3066   collectVariableInfo(GV.getSubprogram());
3067 
3068   // Build the lexical block structure to emit for this routine.
3069   if (LexicalScope *CFS = LScopes.getCurrentFunctionScope())
3070     collectLexicalBlockInfo(*CFS,
3071                             CurFn->ChildBlocks,
3072                             CurFn->Locals,
3073                             CurFn->Globals);
3074 
3075   // Clear the scope and variable information from the map which will not be
3076   // valid after we have finished processing this routine.  This also prepares
3077   // the map for the subsequent routine.
3078   ScopeVariables.clear();
3079 
3080   // Don't emit anything if we don't have any line tables.
3081   // Thunks are compiler-generated and probably won't have source correlation.
3082   if (!CurFn->HaveLineInfo && !GV.getSubprogram()->isThunk()) {
3083     FnDebugInfo.erase(&GV);
3084     CurFn = nullptr;
3085     return;
3086   }
3087 
3088   // Find heap alloc sites and add to list.
3089   for (const auto &MBB : *MF) {
3090     for (const auto &MI : MBB) {
3091       if (MDNode *MD = MI.getHeapAllocMarker()) {
3092         CurFn->HeapAllocSites.push_back(std::make_tuple(getLabelBeforeInsn(&MI),
3093                                                         getLabelAfterInsn(&MI),
3094                                                         dyn_cast<DIType>(MD)));
3095       }
3096     }
3097   }
3098 
3099   bool isThumb = Triple(MMI->getModule()->getTargetTriple()).getArch() ==
3100                  llvm::Triple::ArchType::thumb;
3101   collectDebugInfoForJumpTables(MF, isThumb);
3102 
3103   CurFn->Annotations = MF->getCodeViewAnnotations();
3104 
3105   CurFn->End = Asm->getFunctionEnd();
3106 
3107   CurFn = nullptr;
3108 }
3109 
3110 // Usable locations are valid with non-zero line numbers. A line number of zero
3111 // corresponds to optimized code that doesn't have a distinct source location.
3112 // In this case, we try to use the previous or next source location depending on
3113 // the context.
3114 static bool isUsableDebugLoc(DebugLoc DL) {
3115   return DL && DL.getLine() != 0;
3116 }
3117 
3118 void CodeViewDebug::beginInstruction(const MachineInstr *MI) {
3119   DebugHandlerBase::beginInstruction(MI);
3120 
3121   // Ignore DBG_VALUE and DBG_LABEL locations and function prologue.
3122   if (!Asm || !CurFn || MI->isDebugInstr() ||
3123       MI->getFlag(MachineInstr::FrameSetup))
3124     return;
3125 
3126   // If the first instruction of a new MBB has no location, find the first
3127   // instruction with a location and use that.
3128   DebugLoc DL = MI->getDebugLoc();
3129   if (!isUsableDebugLoc(DL) && MI->getParent() != PrevInstBB) {
3130     for (const auto &NextMI : *MI->getParent()) {
3131       if (NextMI.isDebugInstr())
3132         continue;
3133       DL = NextMI.getDebugLoc();
3134       if (isUsableDebugLoc(DL))
3135         break;
3136     }
3137     // FIXME: Handle the case where the BB has no valid locations. This would
3138     // probably require doing a real dataflow analysis.
3139   }
3140   PrevInstBB = MI->getParent();
3141 
3142   // If we still don't have a debug location, don't record a location.
3143   if (!isUsableDebugLoc(DL))
3144     return;
3145 
3146   maybeRecordLocation(DL, Asm->MF);
3147 }
3148 
3149 MCSymbol *CodeViewDebug::beginCVSubsection(DebugSubsectionKind Kind) {
3150   MCSymbol *BeginLabel = MMI->getContext().createTempSymbol(),
3151            *EndLabel = MMI->getContext().createTempSymbol();
3152   OS.emitInt32(unsigned(Kind));
3153   OS.AddComment("Subsection size");
3154   OS.emitAbsoluteSymbolDiff(EndLabel, BeginLabel, 4);
3155   OS.emitLabel(BeginLabel);
3156   return EndLabel;
3157 }
3158 
3159 void CodeViewDebug::endCVSubsection(MCSymbol *EndLabel) {
3160   OS.emitLabel(EndLabel);
3161   // Every subsection must be aligned to a 4-byte boundary.
3162   OS.emitValueToAlignment(Align(4));
3163 }
3164 
3165 static StringRef getSymbolName(SymbolKind SymKind) {
3166   for (const EnumEntry<SymbolKind> &EE : getSymbolTypeNames())
3167     if (EE.Value == SymKind)
3168       return EE.Name;
3169   return "";
3170 }
3171 
3172 MCSymbol *CodeViewDebug::beginSymbolRecord(SymbolKind SymKind) {
3173   MCSymbol *BeginLabel = MMI->getContext().createTempSymbol(),
3174            *EndLabel = MMI->getContext().createTempSymbol();
3175   OS.AddComment("Record length");
3176   OS.emitAbsoluteSymbolDiff(EndLabel, BeginLabel, 2);
3177   OS.emitLabel(BeginLabel);
3178   if (OS.isVerboseAsm())
3179     OS.AddComment("Record kind: " + getSymbolName(SymKind));
3180   OS.emitInt16(unsigned(SymKind));
3181   return EndLabel;
3182 }
3183 
3184 void CodeViewDebug::endSymbolRecord(MCSymbol *SymEnd) {
3185   // MSVC does not pad out symbol records to four bytes, but LLVM does to avoid
3186   // an extra copy of every symbol record in LLD. This increases object file
3187   // size by less than 1% in the clang build, and is compatible with the Visual
3188   // C++ linker.
3189   OS.emitValueToAlignment(Align(4));
3190   OS.emitLabel(SymEnd);
3191 }
3192 
3193 void CodeViewDebug::emitEndSymbolRecord(SymbolKind EndKind) {
3194   OS.AddComment("Record length");
3195   OS.emitInt16(2);
3196   if (OS.isVerboseAsm())
3197     OS.AddComment("Record kind: " + getSymbolName(EndKind));
3198   OS.emitInt16(uint16_t(EndKind)); // Record Kind
3199 }
3200 
3201 void CodeViewDebug::emitDebugInfoForUDTs(
3202     const std::vector<std::pair<std::string, const DIType *>> &UDTs) {
3203 #ifndef NDEBUG
3204   size_t OriginalSize = UDTs.size();
3205 #endif
3206   for (const auto &UDT : UDTs) {
3207     const DIType *T = UDT.second;
3208     assert(shouldEmitUdt(T));
3209     MCSymbol *UDTRecordEnd = beginSymbolRecord(SymbolKind::S_UDT);
3210     OS.AddComment("Type");
3211     OS.emitInt32(getCompleteTypeIndex(T).getIndex());
3212     assert(OriginalSize == UDTs.size() &&
3213            "getCompleteTypeIndex found new UDTs!");
3214     emitNullTerminatedSymbolName(OS, UDT.first);
3215     endSymbolRecord(UDTRecordEnd);
3216   }
3217 }
3218 
3219 void CodeViewDebug::collectGlobalVariableInfo() {
3220   DenseMap<const DIGlobalVariableExpression *, const GlobalVariable *>
3221       GlobalMap;
3222   for (const GlobalVariable &GV : MMI->getModule()->globals()) {
3223     SmallVector<DIGlobalVariableExpression *, 1> GVEs;
3224     GV.getDebugInfo(GVEs);
3225     for (const auto *GVE : GVEs)
3226       GlobalMap[GVE] = &GV;
3227   }
3228 
3229   NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
3230   for (const MDNode *Node : CUs->operands()) {
3231     const auto *CU = cast<DICompileUnit>(Node);
3232     for (const auto *GVE : CU->getGlobalVariables()) {
3233       const DIGlobalVariable *DIGV = GVE->getVariable();
3234       const DIExpression *DIE = GVE->getExpression();
3235       // Don't emit string literals in CodeView, as the only useful parts are
3236       // generally the filename and line number, which isn't possible to output
3237       // in CodeView. String literals should be the only unnamed GlobalVariable
3238       // with debug info.
3239       if (DIGV->getName().empty()) continue;
3240 
3241       if ((DIE->getNumElements() == 2) &&
3242           (DIE->getElement(0) == dwarf::DW_OP_plus_uconst))
3243         // Record the constant offset for the variable.
3244         //
3245         // A Fortran common block uses this idiom to encode the offset
3246         // of a variable from the common block's starting address.
3247         CVGlobalVariableOffsets.insert(
3248             std::make_pair(DIGV, DIE->getElement(1)));
3249 
3250       // Emit constant global variables in a global symbol section.
3251       if (GlobalMap.count(GVE) == 0 && DIE->isConstant()) {
3252         CVGlobalVariable CVGV = {DIGV, DIE};
3253         GlobalVariables.emplace_back(std::move(CVGV));
3254       }
3255 
3256       const auto *GV = GlobalMap.lookup(GVE);
3257       if (!GV || GV->isDeclarationForLinker())
3258         continue;
3259 
3260       DIScope *Scope = DIGV->getScope();
3261       SmallVector<CVGlobalVariable, 1> *VariableList;
3262       if (Scope && isa<DILocalScope>(Scope)) {
3263         // Locate a global variable list for this scope, creating one if
3264         // necessary.
3265         auto Insertion = ScopeGlobals.insert(
3266             {Scope, std::unique_ptr<GlobalVariableList>()});
3267         if (Insertion.second)
3268           Insertion.first->second = std::make_unique<GlobalVariableList>();
3269         VariableList = Insertion.first->second.get();
3270       } else if (GV->hasComdat())
3271         // Emit this global variable into a COMDAT section.
3272         VariableList = &ComdatVariables;
3273       else
3274         // Emit this global variable in a single global symbol section.
3275         VariableList = &GlobalVariables;
3276       CVGlobalVariable CVGV = {DIGV, GV};
3277       VariableList->emplace_back(std::move(CVGV));
3278     }
3279   }
3280 }
3281 
3282 void CodeViewDebug::collectDebugInfoForGlobals() {
3283   for (const CVGlobalVariable &CVGV : GlobalVariables) {
3284     const DIGlobalVariable *DIGV = CVGV.DIGV;
3285     const DIScope *Scope = DIGV->getScope();
3286     getCompleteTypeIndex(DIGV->getType());
3287     getFullyQualifiedName(Scope, DIGV->getName());
3288   }
3289 
3290   for (const CVGlobalVariable &CVGV : ComdatVariables) {
3291     const DIGlobalVariable *DIGV = CVGV.DIGV;
3292     const DIScope *Scope = DIGV->getScope();
3293     getCompleteTypeIndex(DIGV->getType());
3294     getFullyQualifiedName(Scope, DIGV->getName());
3295   }
3296 }
3297 
3298 void CodeViewDebug::emitDebugInfoForGlobals() {
3299   // First, emit all globals that are not in a comdat in a single symbol
3300   // substream. MSVC doesn't like it if the substream is empty, so only open
3301   // it if we have at least one global to emit.
3302   switchToDebugSectionForSymbol(nullptr);
3303   if (!GlobalVariables.empty() || !StaticConstMembers.empty()) {
3304     OS.AddComment("Symbol subsection for globals");
3305     MCSymbol *EndLabel = beginCVSubsection(DebugSubsectionKind::Symbols);
3306     emitGlobalVariableList(GlobalVariables);
3307     emitStaticConstMemberList();
3308     endCVSubsection(EndLabel);
3309   }
3310 
3311   // Second, emit each global that is in a comdat into its own .debug$S
3312   // section along with its own symbol substream.
3313   for (const CVGlobalVariable &CVGV : ComdatVariables) {
3314     const GlobalVariable *GV = cast<const GlobalVariable *>(CVGV.GVInfo);
3315     MCSymbol *GVSym = Asm->getSymbol(GV);
3316     OS.AddComment("Symbol subsection for " +
3317                   Twine(GlobalValue::dropLLVMManglingEscape(GV->getName())));
3318     switchToDebugSectionForSymbol(GVSym);
3319     MCSymbol *EndLabel = beginCVSubsection(DebugSubsectionKind::Symbols);
3320     // FIXME: emitDebugInfoForGlobal() doesn't handle DIExpressions.
3321     emitDebugInfoForGlobal(CVGV);
3322     endCVSubsection(EndLabel);
3323   }
3324 }
3325 
3326 void CodeViewDebug::emitDebugInfoForRetainedTypes() {
3327   NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
3328   for (const MDNode *Node : CUs->operands()) {
3329     for (auto *Ty : cast<DICompileUnit>(Node)->getRetainedTypes()) {
3330       if (DIType *RT = dyn_cast<DIType>(Ty)) {
3331         getTypeIndex(RT);
3332         // FIXME: Add to global/local DTU list.
3333       }
3334     }
3335   }
3336 }
3337 
3338 // Emit each global variable in the specified array.
3339 void CodeViewDebug::emitGlobalVariableList(ArrayRef<CVGlobalVariable> Globals) {
3340   for (const CVGlobalVariable &CVGV : Globals) {
3341     // FIXME: emitDebugInfoForGlobal() doesn't handle DIExpressions.
3342     emitDebugInfoForGlobal(CVGV);
3343   }
3344 }
3345 
3346 void CodeViewDebug::emitConstantSymbolRecord(const DIType *DTy, APSInt &Value,
3347                                              const std::string &QualifiedName) {
3348   MCSymbol *SConstantEnd = beginSymbolRecord(SymbolKind::S_CONSTANT);
3349   OS.AddComment("Type");
3350   OS.emitInt32(getTypeIndex(DTy).getIndex());
3351 
3352   OS.AddComment("Value");
3353 
3354   // Encoded integers shouldn't need more than 10 bytes.
3355   uint8_t Data[10];
3356   BinaryStreamWriter Writer(Data, llvm::support::endianness::little);
3357   CodeViewRecordIO IO(Writer);
3358   cantFail(IO.mapEncodedInteger(Value));
3359   StringRef SRef((char *)Data, Writer.getOffset());
3360   OS.emitBinaryData(SRef);
3361 
3362   OS.AddComment("Name");
3363   emitNullTerminatedSymbolName(OS, QualifiedName);
3364   endSymbolRecord(SConstantEnd);
3365 }
3366 
3367 void CodeViewDebug::emitStaticConstMemberList() {
3368   for (const DIDerivedType *DTy : StaticConstMembers) {
3369     const DIScope *Scope = DTy->getScope();
3370 
3371     APSInt Value;
3372     if (const ConstantInt *CI =
3373             dyn_cast_or_null<ConstantInt>(DTy->getConstant()))
3374       Value = APSInt(CI->getValue(),
3375                      DebugHandlerBase::isUnsignedDIType(DTy->getBaseType()));
3376     else if (const ConstantFP *CFP =
3377                  dyn_cast_or_null<ConstantFP>(DTy->getConstant()))
3378       Value = APSInt(CFP->getValueAPF().bitcastToAPInt(), true);
3379     else
3380       llvm_unreachable("cannot emit a constant without a value");
3381 
3382     emitConstantSymbolRecord(DTy->getBaseType(), Value,
3383                              getFullyQualifiedName(Scope, DTy->getName()));
3384   }
3385 }
3386 
3387 static bool isFloatDIType(const DIType *Ty) {
3388   if (isa<DICompositeType>(Ty))
3389     return false;
3390 
3391   if (auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
3392     dwarf::Tag T = (dwarf::Tag)Ty->getTag();
3393     if (T == dwarf::DW_TAG_pointer_type ||
3394         T == dwarf::DW_TAG_ptr_to_member_type ||
3395         T == dwarf::DW_TAG_reference_type ||
3396         T == dwarf::DW_TAG_rvalue_reference_type)
3397       return false;
3398     assert(DTy->getBaseType() && "Expected valid base type");
3399     return isFloatDIType(DTy->getBaseType());
3400   }
3401 
3402   auto *BTy = cast<DIBasicType>(Ty);
3403   return (BTy->getEncoding() == dwarf::DW_ATE_float);
3404 }
3405 
3406 void CodeViewDebug::emitDebugInfoForGlobal(const CVGlobalVariable &CVGV) {
3407   const DIGlobalVariable *DIGV = CVGV.DIGV;
3408 
3409   const DIScope *Scope = DIGV->getScope();
3410   // For static data members, get the scope from the declaration.
3411   if (const auto *MemberDecl = dyn_cast_or_null<DIDerivedType>(
3412           DIGV->getRawStaticDataMemberDeclaration()))
3413     Scope = MemberDecl->getScope();
3414   // For static local variables and Fortran, the scoping portion is elided
3415   // in its name so that we can reference the variable in the command line
3416   // of the VS debugger.
3417   std::string QualifiedName =
3418       (moduleIsInFortran() || (Scope && isa<DILocalScope>(Scope)))
3419           ? std::string(DIGV->getName())
3420           : getFullyQualifiedName(Scope, DIGV->getName());
3421 
3422   if (const GlobalVariable *GV =
3423           dyn_cast_if_present<const GlobalVariable *>(CVGV.GVInfo)) {
3424     // DataSym record, see SymbolRecord.h for more info. Thread local data
3425     // happens to have the same format as global data.
3426     MCSymbol *GVSym = Asm->getSymbol(GV);
3427     SymbolKind DataSym = GV->isThreadLocal()
3428                              ? (DIGV->isLocalToUnit() ? SymbolKind::S_LTHREAD32
3429                                                       : SymbolKind::S_GTHREAD32)
3430                              : (DIGV->isLocalToUnit() ? SymbolKind::S_LDATA32
3431                                                       : SymbolKind::S_GDATA32);
3432     MCSymbol *DataEnd = beginSymbolRecord(DataSym);
3433     OS.AddComment("Type");
3434     OS.emitInt32(getCompleteTypeIndex(DIGV->getType()).getIndex());
3435     OS.AddComment("DataOffset");
3436 
3437     uint64_t Offset = 0;
3438     if (CVGlobalVariableOffsets.contains(DIGV))
3439       // Use the offset seen while collecting info on globals.
3440       Offset = CVGlobalVariableOffsets[DIGV];
3441     OS.emitCOFFSecRel32(GVSym, Offset);
3442 
3443     OS.AddComment("Segment");
3444     OS.emitCOFFSectionIndex(GVSym);
3445     OS.AddComment("Name");
3446     const unsigned LengthOfDataRecord = 12;
3447     emitNullTerminatedSymbolName(OS, QualifiedName, LengthOfDataRecord);
3448     endSymbolRecord(DataEnd);
3449   } else {
3450     const DIExpression *DIE = cast<const DIExpression *>(CVGV.GVInfo);
3451     assert(DIE->isConstant() &&
3452            "Global constant variables must contain a constant expression.");
3453 
3454     // Use unsigned for floats.
3455     bool isUnsigned = isFloatDIType(DIGV->getType())
3456                           ? true
3457                           : DebugHandlerBase::isUnsignedDIType(DIGV->getType());
3458     APSInt Value(APInt(/*BitWidth=*/64, DIE->getElement(1)), isUnsigned);
3459     emitConstantSymbolRecord(DIGV->getType(), Value, QualifiedName);
3460   }
3461 }
3462 
3463 void forEachJumpTableBranch(
3464     const MachineFunction *MF, bool isThumb,
3465     const std::function<void(const MachineJumpTableInfo &, const MachineInstr &,
3466                              int64_t)> &Callback) {
3467   auto JTI = MF->getJumpTableInfo();
3468   if (JTI && !JTI->isEmpty()) {
3469 #ifndef NDEBUG
3470     auto UsedJTs = llvm::SmallBitVector(JTI->getJumpTables().size());
3471 #endif
3472     for (const auto &MBB : *MF) {
3473       // Search for indirect branches...
3474       const auto LastMI = MBB.getFirstTerminator();
3475       if (LastMI != MBB.end() && LastMI->isIndirectBranch()) {
3476         if (isThumb) {
3477           // ... that directly use jump table operands.
3478           // NOTE: ARM uses pattern matching to lower its BR_JT SDNode to
3479           // machine instructions, hence inserting a JUMP_TABLE_DEBUG_INFO node
3480           // interferes with this process *but* the resulting pseudo-instruction
3481           // uses a Jump Table operand, so extract the jump table index directly
3482           // from that.
3483           for (const auto &MO : LastMI->operands()) {
3484             if (MO.isJTI()) {
3485               unsigned Index = MO.getIndex();
3486 #ifndef NDEBUG
3487               UsedJTs.set(Index);
3488 #endif
3489               Callback(*JTI, *LastMI, Index);
3490               break;
3491             }
3492           }
3493         } else {
3494           // ... that have jump table debug info.
3495           // NOTE: The debug info is inserted as a JUMP_TABLE_DEBUG_INFO node
3496           // when lowering the BR_JT SDNode to an indirect branch.
3497           for (auto I = MBB.instr_rbegin(), E = MBB.instr_rend(); I != E; ++I) {
3498             if (I->isJumpTableDebugInfo()) {
3499               unsigned Index = I->getOperand(0).getImm();
3500 #ifndef NDEBUG
3501               UsedJTs.set(Index);
3502 #endif
3503               Callback(*JTI, *LastMI, Index);
3504               break;
3505             }
3506           }
3507         }
3508       }
3509     }
3510 #ifndef NDEBUG
3511     assert(UsedJTs.all() &&
3512            "Some of jump tables were not used in a debug info instruction");
3513 #endif
3514   }
3515 }
3516 
3517 void CodeViewDebug::discoverJumpTableBranches(const MachineFunction *MF,
3518                                               bool isThumb) {
3519   forEachJumpTableBranch(
3520       MF, isThumb,
3521       [this](const MachineJumpTableInfo &, const MachineInstr &BranchMI,
3522              int64_t) { requestLabelBeforeInsn(&BranchMI); });
3523 }
3524 
3525 void CodeViewDebug::collectDebugInfoForJumpTables(const MachineFunction *MF,
3526                                                   bool isThumb) {
3527   forEachJumpTableBranch(
3528       MF, isThumb,
3529       [this, MF](const MachineJumpTableInfo &JTI, const MachineInstr &BranchMI,
3530                  int64_t JumpTableIndex) {
3531         // For label-difference jump tables, find the base expression.
3532         // Otherwise the jump table uses an absolute address (so no base
3533         // is required).
3534         const MCSymbol *Base;
3535         uint64_t BaseOffset = 0;
3536         const MCSymbol *Branch = getLabelBeforeInsn(&BranchMI);
3537         JumpTableEntrySize EntrySize;
3538         switch (JTI.getEntryKind()) {
3539         case MachineJumpTableInfo::EK_Custom32:
3540         case MachineJumpTableInfo::EK_GPRel32BlockAddress:
3541         case MachineJumpTableInfo::EK_GPRel64BlockAddress:
3542           llvm_unreachable(
3543               "EK_Custom32, EK_GPRel32BlockAddress, and "
3544               "EK_GPRel64BlockAddress should never be emitted for COFF");
3545         case MachineJumpTableInfo::EK_BlockAddress:
3546           // Each entry is an absolute address.
3547           EntrySize = JumpTableEntrySize::Pointer;
3548           Base = nullptr;
3549           break;
3550         case MachineJumpTableInfo::EK_Inline:
3551         case MachineJumpTableInfo::EK_LabelDifference32:
3552         case MachineJumpTableInfo::EK_LabelDifference64:
3553           // Ask the AsmPrinter.
3554           std::tie(Base, BaseOffset, Branch, EntrySize) =
3555               Asm->getCodeViewJumpTableInfo(JumpTableIndex, &BranchMI, Branch);
3556           break;
3557         }
3558 
3559         CurFn->JumpTables.push_back(
3560             {EntrySize, Base, BaseOffset, Branch,
3561              MF->getJTISymbol(JumpTableIndex, MMI->getContext()),
3562              JTI.getJumpTables()[JumpTableIndex].MBBs.size()});
3563       });
3564 }
3565 
3566 void CodeViewDebug::emitDebugInfoForJumpTables(const FunctionInfo &FI) {
3567   for (auto JumpTable : FI.JumpTables) {
3568     MCSymbol *JumpTableEnd = beginSymbolRecord(SymbolKind::S_ARMSWITCHTABLE);
3569     if (JumpTable.Base) {
3570       OS.AddComment("Base offset");
3571       OS.emitCOFFSecRel32(JumpTable.Base, JumpTable.BaseOffset);
3572       OS.AddComment("Base section index");
3573       OS.emitCOFFSectionIndex(JumpTable.Base);
3574     } else {
3575       OS.AddComment("Base offset");
3576       OS.emitInt32(0);
3577       OS.AddComment("Base section index");
3578       OS.emitInt16(0);
3579     }
3580     OS.AddComment("Switch type");
3581     OS.emitInt16(static_cast<uint16_t>(JumpTable.EntrySize));
3582     OS.AddComment("Branch offset");
3583     OS.emitCOFFSecRel32(JumpTable.Branch, /*Offset=*/0);
3584     OS.AddComment("Table offset");
3585     OS.emitCOFFSecRel32(JumpTable.Table, /*Offset=*/0);
3586     OS.AddComment("Branch section index");
3587     OS.emitCOFFSectionIndex(JumpTable.Branch);
3588     OS.AddComment("Table section index");
3589     OS.emitCOFFSectionIndex(JumpTable.Table);
3590     OS.AddComment("Entries count");
3591     OS.emitInt32(JumpTable.TableSize);
3592     endSymbolRecord(JumpTableEnd);
3593   }
3594 }
3595 
3596 void CodeViewDebug::emitInlinees(
3597     const SmallSet<codeview::TypeIndex, 1> &Inlinees) {
3598   // Divide the list of inlinees into chunks such that each chunk fits within
3599   // one record.
3600   constexpr size_t ChunkSize =
3601       (MaxRecordLength - sizeof(SymbolKind) - sizeof(uint32_t)) /
3602       sizeof(uint32_t);
3603 
3604   SmallVector<TypeIndex> SortedInlinees{Inlinees.begin(), Inlinees.end()};
3605   llvm::sort(SortedInlinees);
3606 
3607   size_t CurrentIndex = 0;
3608   while (CurrentIndex < SortedInlinees.size()) {
3609     auto Symbol = beginSymbolRecord(SymbolKind::S_INLINEES);
3610     auto CurrentChunkSize =
3611         std::min(ChunkSize, SortedInlinees.size() - CurrentIndex);
3612     OS.AddComment("Count");
3613     OS.emitInt32(CurrentChunkSize);
3614 
3615     const size_t CurrentChunkEnd = CurrentIndex + CurrentChunkSize;
3616     for (; CurrentIndex < CurrentChunkEnd; ++CurrentIndex) {
3617       OS.AddComment("Inlinee");
3618       OS.emitInt32(SortedInlinees[CurrentIndex].getIndex());
3619     }
3620     endSymbolRecord(Symbol);
3621   }
3622 }
3623