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