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