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