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