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