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