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