LCOV - code coverage report
Current view: top level - lib/CodeGen/AsmPrinter - CodeViewDebug.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 1149 1195 96.2 %
Date: 2018-07-13 00:08:38 Functions: 82 85 96.5 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13