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