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