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