brintos

brintos / llvm-project-archived public Read only

0
0
Text · 133.8 KiB · 2ebccee Raw
3667 lines · cpp
1//===- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp ----------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// This file contains support for writing Microsoft CodeView debug info.10//11//===----------------------------------------------------------------------===//12 13#include "CodeViewDebug.h"14#include "llvm/ADT/APSInt.h"15#include "llvm/ADT/STLExtras.h"16#include "llvm/ADT/SmallBitVector.h"17#include "llvm/ADT/SmallString.h"18#include "llvm/ADT/StringRef.h"19#include "llvm/ADT/TinyPtrVector.h"20#include "llvm/ADT/Twine.h"21#include "llvm/BinaryFormat/COFF.h"22#include "llvm/BinaryFormat/Dwarf.h"23#include "llvm/CodeGen/AsmPrinter.h"24#include "llvm/CodeGen/LexicalScopes.h"25#include "llvm/CodeGen/MachineFrameInfo.h"26#include "llvm/CodeGen/MachineFunction.h"27#include "llvm/CodeGen/MachineInstr.h"28#include "llvm/CodeGen/MachineModuleInfo.h"29#include "llvm/CodeGen/TargetFrameLowering.h"30#include "llvm/CodeGen/TargetLowering.h"31#include "llvm/CodeGen/TargetRegisterInfo.h"32#include "llvm/CodeGen/TargetSubtargetInfo.h"33#include "llvm/Config/llvm-config.h"34#include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"35#include "llvm/DebugInfo/CodeView/CodeViewRecordIO.h"36#include "llvm/DebugInfo/CodeView/ContinuationRecordBuilder.h"37#include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h"38#include "llvm/DebugInfo/CodeView/EnumTables.h"39#include "llvm/DebugInfo/CodeView/Line.h"40#include "llvm/DebugInfo/CodeView/SymbolRecord.h"41#include "llvm/DebugInfo/CodeView/TypeRecord.h"42#include "llvm/DebugInfo/CodeView/TypeTableCollection.h"43#include "llvm/DebugInfo/CodeView/TypeVisitorCallbackPipeline.h"44#include "llvm/IR/Constants.h"45#include "llvm/IR/DataLayout.h"46#include "llvm/IR/DebugInfoMetadata.h"47#include "llvm/IR/Function.h"48#include "llvm/IR/GlobalValue.h"49#include "llvm/IR/GlobalVariable.h"50#include "llvm/IR/Metadata.h"51#include "llvm/IR/Module.h"52#include "llvm/MC/MCAsmInfo.h"53#include "llvm/MC/MCContext.h"54#include "llvm/MC/MCSectionCOFF.h"55#include "llvm/MC/MCStreamer.h"56#include "llvm/MC/MCSymbol.h"57#include "llvm/Support/BinaryStreamWriter.h"58#include "llvm/Support/Casting.h"59#include "llvm/Support/Error.h"60#include "llvm/Support/ErrorHandling.h"61#include "llvm/Support/FormatVariadic.h"62#include "llvm/Support/Path.h"63#include "llvm/Support/SMLoc.h"64#include "llvm/Support/ScopedPrinter.h"65#include "llvm/Target/TargetLoweringObjectFile.h"66#include "llvm/Target/TargetMachine.h"67#include "llvm/TargetParser/Triple.h"68#include <algorithm>69#include <cassert>70#include <cctype>71#include <cstddef>72#include <limits>73 74using namespace llvm;75using namespace llvm::codeview;76 77namespace {78class CVMCAdapter : public CodeViewRecordStreamer {79public:80  CVMCAdapter(MCStreamer &OS, TypeCollection &TypeTable)81      : OS(&OS), TypeTable(TypeTable) {}82 83  void emitBytes(StringRef Data) override { OS->emitBytes(Data); }84 85  void emitIntValue(uint64_t Value, unsigned Size) override {86    OS->emitIntValueInHex(Value, Size);87  }88 89  void emitBinaryData(StringRef Data) override { OS->emitBinaryData(Data); }90 91  void AddComment(const Twine &T) override { OS->AddComment(T); }92 93  void AddRawComment(const Twine &T) override { OS->emitRawComment(T); }94 95  bool isVerboseAsm() override { return OS->isVerboseAsm(); }96 97  std::string getTypeName(TypeIndex TI) override {98    std::string TypeName;99    if (!TI.isNoneType()) {100      if (TI.isSimple())101        TypeName = std::string(TypeIndex::simpleTypeName(TI));102      else103        TypeName = std::string(TypeTable.getTypeName(TI));104    }105    return TypeName;106  }107 108private:109  MCStreamer *OS = nullptr;110  TypeCollection &TypeTable;111};112} // namespace113 114static CPUType mapArchToCVCPUType(Triple::ArchType Type) {115  switch (Type) {116  case Triple::ArchType::x86:117    return CPUType::Pentium3;118  case Triple::ArchType::x86_64:119    return CPUType::X64;120  case Triple::ArchType::thumb:121    // LLVM currently doesn't support Windows CE and so thumb122    // here is indiscriminately mapped to ARMNT specifically.123    return CPUType::ARMNT;124  case Triple::ArchType::aarch64:125    return CPUType::ARM64;126  case Triple::ArchType::mipsel:127    return CPUType::MIPS;128  case Triple::ArchType::UnknownArch:129    return CPUType::Unknown;130  default:131    report_fatal_error("target architecture doesn't map to a CodeView CPUType");132  }133}134 135CodeViewDebug::CodeViewDebug(AsmPrinter *AP)136    : DebugHandlerBase(AP), OS(*Asm->OutStreamer), TypeTable(Allocator) {}137 138StringRef CodeViewDebug::getFullFilepath(const DIFile *File) {139  std::string &Filepath = FileToFilepathMap[File];140  if (!Filepath.empty())141    return Filepath;142 143  StringRef Dir = File->getDirectory(), Filename = File->getFilename();144 145  // If this is a Unix-style path, just use it as is. Don't try to canonicalize146  // it textually because one of the path components could be a symlink.147  if (Dir.starts_with("/") || Filename.starts_with("/")) {148    if (llvm::sys::path::is_absolute(Filename, llvm::sys::path::Style::posix))149      return Filename;150    Filepath = std::string(Dir);151    if (Dir.back() != '/')152      Filepath += '/';153    Filepath += Filename;154    return Filepath;155  }156 157  // Clang emits directory and relative filename info into the IR, but CodeView158  // operates on full paths.  We could change Clang to emit full paths too, but159  // that would increase the IR size and probably not needed for other users.160  // For now, just concatenate and canonicalize the path here.161  if (Filename.find(':') == 1)162    Filepath = std::string(Filename);163  else164    Filepath = (Dir + "\\" + Filename).str();165 166  // Canonicalize the path.  We have to do it textually because we may no longer167  // have access the file in the filesystem.168  // First, replace all slashes with backslashes.169  llvm::replace(Filepath, '/', '\\');170 171  // Remove all "\.\" with "\".172  size_t Cursor = 0;173  while ((Cursor = Filepath.find("\\.\\", Cursor)) != std::string::npos)174    Filepath.erase(Cursor, 2);175 176  // Replace all "\XXX\..\" with "\".  Don't try too hard though as the original177  // path should be well-formatted, e.g. start with a drive letter, etc.178  Cursor = 0;179  while ((Cursor = Filepath.find("\\..\\", Cursor)) != std::string::npos) {180    // Something's wrong if the path starts with "\..\", abort.181    if (Cursor == 0)182      break;183 184    size_t PrevSlash = Filepath.rfind('\\', Cursor - 1);185    if (PrevSlash == std::string::npos)186      // Something's wrong, abort.187      break;188 189    Filepath.erase(PrevSlash, Cursor + 3 - PrevSlash);190    // The next ".." might be following the one we've just erased.191    Cursor = PrevSlash;192  }193 194  // Remove all duplicate backslashes.195  Cursor = 0;196  while ((Cursor = Filepath.find("\\\\", Cursor)) != std::string::npos)197    Filepath.erase(Cursor, 1);198 199  return Filepath;200}201 202unsigned CodeViewDebug::maybeRecordFile(const DIFile *F) {203  StringRef FullPath = getFullFilepath(F);204  unsigned NextId = FileIdMap.size() + 1;205  auto Insertion = FileIdMap.insert(std::make_pair(FullPath, NextId));206  if (Insertion.second) {207    // We have to compute the full filepath and emit a .cv_file directive.208    ArrayRef<uint8_t> ChecksumAsBytes;209    FileChecksumKind CSKind = FileChecksumKind::None;210    if (F->getChecksum()) {211      std::string Checksum = fromHex(F->getChecksum()->Value);212      void *CKMem = OS.getContext().allocate(Checksum.size(), 1);213      memcpy(CKMem, Checksum.data(), Checksum.size());214      ChecksumAsBytes = ArrayRef<uint8_t>(215          reinterpret_cast<const uint8_t *>(CKMem), Checksum.size());216      switch (F->getChecksum()->Kind) {217      case DIFile::CSK_MD5:218        CSKind = FileChecksumKind::MD5;219        break;220      case DIFile::CSK_SHA1:221        CSKind = FileChecksumKind::SHA1;222        break;223      case DIFile::CSK_SHA256:224        CSKind = FileChecksumKind::SHA256;225        break;226      }227    }228    bool Success = OS.emitCVFileDirective(NextId, FullPath, ChecksumAsBytes,229                                          static_cast<unsigned>(CSKind));230    (void)Success;231    assert(Success && ".cv_file directive failed");232  }233  return Insertion.first->second;234}235 236CodeViewDebug::InlineSite &237CodeViewDebug::getInlineSite(const DILocation *InlinedAt,238                             const DISubprogram *Inlinee) {239  auto SiteInsertion = CurFn->InlineSites.try_emplace(InlinedAt);240  InlineSite *Site = &SiteInsertion.first->second;241  if (SiteInsertion.second) {242    unsigned ParentFuncId = CurFn->FuncId;243    if (const DILocation *OuterIA = InlinedAt->getInlinedAt())244      ParentFuncId =245          getInlineSite(OuterIA, InlinedAt->getScope()->getSubprogram())246              .SiteFuncId;247 248    Site->SiteFuncId = NextFuncId++;249    OS.emitCVInlineSiteIdDirective(250        Site->SiteFuncId, ParentFuncId, maybeRecordFile(InlinedAt->getFile()),251        InlinedAt->getLine(), InlinedAt->getColumn(), SMLoc());252    Site->Inlinee = Inlinee;253    InlinedSubprograms.insert(Inlinee);254    auto InlineeIdx = getFuncIdForSubprogram(Inlinee);255 256    if (InlinedAt->getInlinedAt() == nullptr)257      CurFn->Inlinees.insert(InlineeIdx);258  }259  return *Site;260}261 262static StringRef getPrettyScopeName(const DIScope *Scope) {263  StringRef ScopeName = Scope->getName();264  if (!ScopeName.empty())265    return ScopeName;266 267  switch (Scope->getTag()) {268  case dwarf::DW_TAG_enumeration_type:269  case dwarf::DW_TAG_class_type:270  case dwarf::DW_TAG_structure_type:271  case dwarf::DW_TAG_union_type:272    return "<unnamed-tag>";273  case dwarf::DW_TAG_namespace:274    return "`anonymous namespace'";275  default:276    return StringRef();277  }278}279 280const DISubprogram *CodeViewDebug::collectParentScopeNames(281    const DIScope *Scope, SmallVectorImpl<StringRef> &QualifiedNameComponents) {282  const DISubprogram *ClosestSubprogram = nullptr;283  while (Scope != nullptr) {284    if (ClosestSubprogram == nullptr)285      ClosestSubprogram = dyn_cast<DISubprogram>(Scope);286 287    // If a type appears in a scope chain, make sure it gets emitted. The288    // frontend will be responsible for deciding if this should be a forward289    // declaration or a complete type.290    if (const auto *Ty = dyn_cast<DICompositeType>(Scope))291      DeferredCompleteTypes.push_back(Ty);292 293    StringRef ScopeName = getPrettyScopeName(Scope);294    if (!ScopeName.empty())295      QualifiedNameComponents.push_back(ScopeName);296    Scope = Scope->getScope();297  }298  return ClosestSubprogram;299}300 301static std::string formatNestedName(ArrayRef<StringRef> QualifiedNameComponents,302                                    StringRef TypeName) {303  std::string FullyQualifiedName;304  for (StringRef QualifiedNameComponent :305       llvm::reverse(QualifiedNameComponents)) {306    FullyQualifiedName.append(std::string(QualifiedNameComponent));307    FullyQualifiedName.append("::");308  }309  FullyQualifiedName.append(std::string(TypeName));310  return FullyQualifiedName;311}312 313struct CodeViewDebug::TypeLoweringScope {314  TypeLoweringScope(CodeViewDebug &CVD) : CVD(CVD) { ++CVD.TypeEmissionLevel; }315  ~TypeLoweringScope() {316    // Don't decrement TypeEmissionLevel until after emitting deferred types, so317    // inner TypeLoweringScopes don't attempt to emit deferred types.318    if (CVD.TypeEmissionLevel == 1)319      CVD.emitDeferredCompleteTypes();320    --CVD.TypeEmissionLevel;321  }322  CodeViewDebug &CVD;323};324 325std::string CodeViewDebug::getFullyQualifiedName(const DIScope *Scope,326                                                 StringRef Name) {327  // Ensure types in the scope chain are emitted as soon as possible.328  // This can create otherwise a situation where S_UDTs are emitted while329  // looping in emitDebugInfoForUDTs.330  TypeLoweringScope S(*this);331  SmallVector<StringRef, 5> QualifiedNameComponents;332  collectParentScopeNames(Scope, QualifiedNameComponents);333  return formatNestedName(QualifiedNameComponents, Name);334}335 336std::string CodeViewDebug::getFullyQualifiedName(const DIScope *Ty) {337  const DIScope *Scope = Ty->getScope();338  return getFullyQualifiedName(Scope, getPrettyScopeName(Ty));339}340 341TypeIndex CodeViewDebug::getScopeIndex(const DIScope *Scope) {342  // No scope means global scope and that uses the zero index.343  //344  // We also use zero index when the scope is a DISubprogram345  // to suppress the emission of LF_STRING_ID for the function,346  // which can trigger a link-time error with the linker in347  // VS2019 version 16.11.2 or newer.348  // Note, however, skipping the debug info emission for the DISubprogram349  // is a temporary fix. The root issue here is that we need to figure out350  // the proper way to encode a function nested in another function351  // (as introduced by the Fortran 'contains' keyword) in CodeView.352  if (!Scope || isa<DIFile>(Scope) || isa<DISubprogram>(Scope))353    return TypeIndex();354 355  assert(!isa<DIType>(Scope) && "shouldn't make a namespace scope for a type");356 357  // Check if we've already translated this scope.358  auto I = TypeIndices.find({Scope, nullptr});359  if (I != TypeIndices.end())360    return I->second;361 362  // Build the fully qualified name of the scope.363  std::string ScopeName = getFullyQualifiedName(Scope);364  StringIdRecord SID(TypeIndex(), ScopeName);365  auto TI = TypeTable.writeLeafType(SID);366  return recordTypeIndexForDINode(Scope, TI);367}368 369static StringRef removeTemplateArgs(StringRef Name) {370  // Remove template args from the display name. Assume that the template args371  // are the last thing in the name.372  if (Name.empty() || Name.back() != '>')373    return Name;374 375  int OpenBrackets = 0;376  for (int i = Name.size() - 1; i >= 0; --i) {377    if (Name[i] == '>')378      ++OpenBrackets;379    else if (Name[i] == '<') {380      --OpenBrackets;381      if (OpenBrackets == 0)382        return Name.substr(0, i);383    }384  }385  return Name;386}387 388TypeIndex CodeViewDebug::getFuncIdForSubprogram(const DISubprogram *SP) {389  assert(SP);390 391  // Check if we've already translated this subprogram.392  auto I = TypeIndices.find({SP, nullptr});393  if (I != TypeIndices.end())394    return I->second;395 396  // The display name includes function template arguments. Drop them to match397  // MSVC. We need to have the template arguments in the DISubprogram name398  // because they are used in other symbol records, such as S_GPROC32_IDs.399  StringRef DisplayName = removeTemplateArgs(SP->getName());400 401  const DIScope *Scope = SP->getScope();402  TypeIndex TI;403  if (const auto *Class = dyn_cast_or_null<DICompositeType>(Scope)) {404    // If the scope is a DICompositeType, then this must be a method. Member405    // function types take some special handling, and require access to the406    // subprogram.407    TypeIndex ClassType = getTypeIndex(Class);408    MemberFuncIdRecord MFuncId(ClassType, getMemberFunctionType(SP, Class),409                               DisplayName);410    TI = TypeTable.writeLeafType(MFuncId);411  } else {412    // Otherwise, this must be a free function.413    TypeIndex ParentScope = getScopeIndex(Scope);414    FuncIdRecord FuncId(ParentScope, getTypeIndex(SP->getType()), DisplayName);415    TI = TypeTable.writeLeafType(FuncId);416  }417 418  return recordTypeIndexForDINode(SP, TI);419}420 421static bool isNonTrivial(const DICompositeType *DCTy) {422  return ((DCTy->getFlags() & DINode::FlagNonTrivial) == DINode::FlagNonTrivial);423}424 425static FunctionOptions426getFunctionOptions(const DISubroutineType *Ty,427                   const DICompositeType *ClassTy = nullptr,428                   StringRef SPName = StringRef("")) {429  FunctionOptions FO = FunctionOptions::None;430  const DIType *ReturnTy = nullptr;431  if (auto TypeArray = Ty->getTypeArray()) {432    if (TypeArray.size())433      ReturnTy = TypeArray[0];434  }435 436  // Add CxxReturnUdt option to functions that return nontrivial record types437  // or methods that return record types.438  if (auto *ReturnDCTy = dyn_cast_or_null<DICompositeType>(ReturnTy))439    if (isNonTrivial(ReturnDCTy) || ClassTy)440      FO |= FunctionOptions::CxxReturnUdt;441 442  // DISubroutineType is unnamed. Use DISubprogram's i.e. SPName in comparison.443  if (ClassTy && isNonTrivial(ClassTy) && SPName == ClassTy->getName()) {444    FO |= FunctionOptions::Constructor;445 446  // TODO: put the FunctionOptions::ConstructorWithVirtualBases flag.447 448  }449  return FO;450}451 452TypeIndex CodeViewDebug::getMemberFunctionType(const DISubprogram *SP,453                                               const DICompositeType *Class) {454  // Always use the method declaration as the key for the function type. The455  // method declaration contains the this adjustment.456  if (SP->getDeclaration())457    SP = SP->getDeclaration();458  assert(!SP->getDeclaration() && "should use declaration as key");459 460  // Key the MemberFunctionRecord into the map as {SP, Class}. It won't collide461  // with the MemberFuncIdRecord, which is keyed in as {SP, nullptr}.462  auto I = TypeIndices.find({SP, Class});463  if (I != TypeIndices.end())464    return I->second;465 466  // Make sure complete type info for the class is emitted *after* the member467  // function type, as the complete class type is likely to reference this468  // member function type.469  TypeLoweringScope S(*this);470  const bool IsStaticMethod = (SP->getFlags() & DINode::FlagStaticMember) != 0;471 472  FunctionOptions FO = getFunctionOptions(SP->getType(), Class, SP->getName());473  TypeIndex TI = lowerTypeMemberFunction(474      SP->getType(), Class, SP->getThisAdjustment(), IsStaticMethod, FO);475  return recordTypeIndexForDINode(SP, TI, Class);476}477 478TypeIndex CodeViewDebug::recordTypeIndexForDINode(const DINode *Node,479                                                  TypeIndex TI,480                                                  const DIType *ClassTy) {481  auto InsertResult = TypeIndices.insert({{Node, ClassTy}, TI});482  (void)InsertResult;483  assert(InsertResult.second && "DINode was already assigned a type index");484  return TI;485}486 487unsigned CodeViewDebug::getPointerSizeInBytes() {488  return MMI->getModule()->getDataLayout().getPointerSizeInBits() / 8;489}490 491void CodeViewDebug::recordLocalVariable(LocalVariable &&Var,492                                        const LexicalScope *LS) {493  if (const DILocation *InlinedAt = LS->getInlinedAt()) {494    // This variable was inlined. Associate it with the InlineSite.495    const DISubprogram *Inlinee = Var.DIVar->getScope()->getSubprogram();496    InlineSite &Site = getInlineSite(InlinedAt, Inlinee);497    Site.InlinedLocals.emplace_back(std::move(Var));498  } else {499    // This variable goes into the corresponding lexical scope.500    ScopeVariables[LS].emplace_back(std::move(Var));501  }502}503 504static void addLocIfNotPresent(SmallVectorImpl<const DILocation *> &Locs,505                               const DILocation *Loc) {506  if (!llvm::is_contained(Locs, Loc))507    Locs.push_back(Loc);508}509 510void CodeViewDebug::maybeRecordLocation(const DebugLoc &DL,511                                        const MachineFunction *MF) {512  // Skip this instruction if it has the same location as the previous one.513  if (!DL || DL == PrevInstLoc)514    return;515 516  const DIScope *Scope = DL->getScope();517  if (!Scope)518    return;519 520  // Skip this line if it is longer than the maximum we can record.521  LineInfo LI(DL.getLine(), DL.getLine(), /*IsStatement=*/true);522  if (LI.getStartLine() != DL.getLine() || LI.isAlwaysStepInto() ||523      LI.isNeverStepInto())524    return;525 526  ColumnInfo CI(DL.getCol(), /*EndColumn=*/0);527  if (CI.getStartColumn() != DL.getCol())528    return;529 530  if (!CurFn->HaveLineInfo)531    CurFn->HaveLineInfo = true;532  unsigned FileId = 0;533  if (PrevInstLoc.get() && PrevInstLoc->getFile() == DL->getFile())534    FileId = CurFn->LastFileId;535  else536    FileId = CurFn->LastFileId = maybeRecordFile(DL->getFile());537  PrevInstLoc = DL;538 539  unsigned FuncId = CurFn->FuncId;540  if (const DILocation *SiteLoc = DL->getInlinedAt()) {541    const DILocation *Loc = DL.get();542 543    // If this location was actually inlined from somewhere else, give it the ID544    // of the inline call site.545    FuncId =546        getInlineSite(SiteLoc, Loc->getScope()->getSubprogram()).SiteFuncId;547 548    // Ensure we have links in the tree of inline call sites.549    bool FirstLoc = true;550    while ((SiteLoc = Loc->getInlinedAt())) {551      InlineSite &Site =552          getInlineSite(SiteLoc, Loc->getScope()->getSubprogram());553      if (!FirstLoc)554        addLocIfNotPresent(Site.ChildSites, Loc);555      FirstLoc = false;556      Loc = SiteLoc;557    }558    addLocIfNotPresent(CurFn->ChildSites, Loc);559  }560 561  OS.emitCVLocDirective(FuncId, FileId, DL.getLine(), DL.getCol(),562                        /*PrologueEnd=*/false, /*IsStmt=*/false,563                        DL->getFilename(), SMLoc());564}565 566void CodeViewDebug::emitCodeViewMagicVersion() {567  OS.emitValueToAlignment(Align(4));568  OS.AddComment("Debug section magic");569  OS.emitInt32(COFF::DEBUG_SECTION_MAGIC);570}571 572static SourceLanguage573MapDWARFLanguageToCVLang(dwarf::SourceLanguageName DWLName) {574  switch (DWLName) {575  case dwarf::DW_LNAME_C:576    return SourceLanguage::C;577  case dwarf::DW_LNAME_C_plus_plus:578    return SourceLanguage::Cpp;579  case dwarf::DW_LNAME_Fortran:580    return SourceLanguage::Fortran;581  case dwarf::DW_LNAME_Pascal:582    return SourceLanguage::Pascal;583  case dwarf::DW_LNAME_Cobol:584    return SourceLanguage::Cobol;585  case dwarf::DW_LNAME_Java:586    return SourceLanguage::Java;587  case dwarf::DW_LNAME_D:588    return SourceLanguage::D;589  case dwarf::DW_LNAME_Swift:590    return SourceLanguage::Swift;591  case dwarf::DW_LNAME_Rust:592    return SourceLanguage::Rust;593  case dwarf::DW_LNAME_ObjC:594    return SourceLanguage::ObjC;595  case dwarf::DW_LNAME_ObjC_plus_plus:596    return SourceLanguage::ObjCpp;597  default:598    // There's no CodeView representation for this language, and CV doesn't599    // have an "unknown" option for the language field, so we'll use MASM,600    // as it's very low level.601    return SourceLanguage::Masm;602  }603}604 605static SourceLanguage MapDWARFLanguageToCVLang(dwarf::SourceLanguage DWLang) {606  auto MaybeLName = dwarf::toDW_LNAME(DWLang);607  if (!MaybeLName)608    return MapDWARFLanguageToCVLang(static_cast<dwarf::SourceLanguageName>(0));609 610  return MapDWARFLanguageToCVLang(MaybeLName->first);611}612 613void CodeViewDebug::beginModule(Module *M) {614  // If COFF debug section is not available, skip any debug info related stuff.615  if (!Asm->getObjFileLowering().getCOFFDebugSymbolsSection()) {616    Asm = nullptr;617    return;618  }619 620  CompilerInfoAsm = Asm;621  TheCPU = mapArchToCVCPUType(M->getTargetTriple().getArch());622 623  // Get the current source language.624  const MDNode *Node;625  if (Asm->hasDebugInfo()) {626    Node = *M->debug_compile_units_begin();627  } else {628    // When emitting only compiler information, we may have only NoDebug CUs,629    // which would be skipped by debug_compile_units_begin.630    NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");631    if (CUs->operands().empty()) {632      Asm = nullptr;633      return;634    }635    Node = *CUs->operands().begin();636  }637 638  TheCU = cast<DICompileUnit>(Node);639  DISourceLanguageName Lang = TheCU->getSourceLanguage();640  CurrentSourceLanguage =641      Lang.hasVersionedName()642          ? MapDWARFLanguageToCVLang(643                static_cast<dwarf::SourceLanguageName>(Lang.getName()))644          : MapDWARFLanguageToCVLang(645                static_cast<dwarf::SourceLanguage>(Lang.getName()));646  if (!M->getCodeViewFlag() ||647      TheCU->getEmissionKind() == DICompileUnit::NoDebug) {648    Asm = nullptr;649    return;650  }651 652  collectGlobalVariableInfo();653 654  // Check if we should emit type record hashes.655  ConstantInt *GH =656      mdconst::extract_or_null<ConstantInt>(M->getModuleFlag("CodeViewGHash"));657  EmitDebugGlobalHashes = GH && !GH->isZero();658}659 660void CodeViewDebug::endModule() {661  if (!CompilerInfoAsm)662    return;663 664  // The COFF .debug$S section consists of several subsections, each starting665  // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length666  // of the payload followed by the payload itself.  The subsections are 4-byte667  // aligned.668 669  // Use the generic .debug$S section, and make a subsection for all the inlined670  // subprograms.671  switchToDebugSectionForSymbol(nullptr);672 673  MCSymbol *CompilerInfo = beginCVSubsection(DebugSubsectionKind::Symbols);674  emitObjName();675  emitCompilerInformation();676  endCVSubsection(CompilerInfo);677  if (!Asm)678    return;679 680  emitSecureHotPatchInformation();681 682  emitInlineeLinesSubsection();683 684  // Emit per-function debug information.685  for (auto &P : FnDebugInfo)686    if (!P.first->isDeclarationForLinker())687      emitDebugInfoForFunction(P.first, *P.second);688 689  // Get types used by globals without emitting anything.690  // This is meant to collect all static const data members so they can be691  // emitted as globals.692  collectDebugInfoForGlobals();693 694  // Emit retained types.695  emitDebugInfoForRetainedTypes();696 697  // Emit global variable debug information.698  setCurrentSubprogram(nullptr);699  emitDebugInfoForGlobals();700 701  // Switch back to the generic .debug$S section after potentially processing702  // comdat symbol sections.703  switchToDebugSectionForSymbol(nullptr);704 705  // Emit UDT records for any types used by global variables.706  if (!GlobalUDTs.empty()) {707    MCSymbol *SymbolsEnd = beginCVSubsection(DebugSubsectionKind::Symbols);708    emitDebugInfoForUDTs(GlobalUDTs);709    endCVSubsection(SymbolsEnd);710  }711 712  // This subsection holds a file index to offset in string table table.713  OS.AddComment("File index to string table offset subsection");714  OS.emitCVFileChecksumsDirective();715 716  // This subsection holds the string table.717  OS.AddComment("String table");718  OS.emitCVStringTableDirective();719 720  // Emit S_BUILDINFO, which points to LF_BUILDINFO. Put this in its own symbol721  // subsection in the generic .debug$S section at the end. There is no722  // particular reason for this ordering other than to match MSVC.723  emitBuildInfo();724 725  // Emit type information and hashes last, so that any types we translate while726  // emitting function info are included.727  emitTypeInformation();728 729  if (EmitDebugGlobalHashes)730    emitTypeGlobalHashes();731 732  clear();733}734 735static void736emitNullTerminatedSymbolName(MCStreamer &OS, StringRef S,737                             unsigned MaxFixedRecordLength = 0xF00) {738  // The maximum CV record length is 0xFF00. Most of the strings we emit appear739  // after a fixed length portion of the record. The fixed length portion should740  // always be less than 0xF00 (3840) bytes, so truncate the string so that the741  // overall record size is less than the maximum allowed.742  SmallString<32> NullTerminatedString(743      S.take_front(MaxRecordLength - MaxFixedRecordLength - 1));744  NullTerminatedString.push_back('\0');745  OS.emitBytes(NullTerminatedString);746}747 748void CodeViewDebug::emitTypeInformation() {749  if (TypeTable.empty())750    return;751 752  // Start the .debug$T or .debug$P section with 0x4.753  OS.switchSection(Asm->getObjFileLowering().getCOFFDebugTypesSection());754  emitCodeViewMagicVersion();755 756  TypeTableCollection Table(TypeTable.records());757  TypeVisitorCallbackPipeline Pipeline;758 759  // To emit type record using Codeview MCStreamer adapter760  CVMCAdapter CVMCOS(OS, Table);761  TypeRecordMapping typeMapping(CVMCOS);762  Pipeline.addCallbackToPipeline(typeMapping);763 764  std::optional<TypeIndex> B = Table.getFirst();765  while (B) {766    // This will fail if the record data is invalid.767    CVType Record = Table.getType(*B);768 769    Error E = codeview::visitTypeRecord(Record, *B, Pipeline);770 771    if (E) {772      logAllUnhandledErrors(std::move(E), errs(), "error: ");773      llvm_unreachable("produced malformed type record");774    }775 776    B = Table.getNext(*B);777  }778}779 780void CodeViewDebug::emitTypeGlobalHashes() {781  if (TypeTable.empty())782    return;783 784  // Start the .debug$H section with the version and hash algorithm, currently785  // hardcoded to version 0, SHA1.786  OS.switchSection(Asm->getObjFileLowering().getCOFFGlobalTypeHashesSection());787 788  OS.emitValueToAlignment(Align(4));789  OS.AddComment("Magic");790  OS.emitInt32(COFF::DEBUG_HASHES_SECTION_MAGIC);791  OS.AddComment("Section Version");792  OS.emitInt16(0);793  OS.AddComment("Hash Algorithm");794  OS.emitInt16(uint16_t(GlobalTypeHashAlg::BLAKE3));795 796  TypeIndex TI(TypeIndex::FirstNonSimpleIndex);797  for (const auto &GHR : TypeTable.hashes()) {798    if (OS.isVerboseAsm()) {799      // Emit an EOL-comment describing which TypeIndex this hash corresponds800      // to, as well as the stringified SHA1 hash.801      SmallString<32> Comment;802      raw_svector_ostream CommentOS(Comment);803      CommentOS << formatv("{0:X+} [{1}]", TI.getIndex(), GHR);804      OS.AddComment(Comment);805      ++TI;806    }807    assert(GHR.Hash.size() == 8);808    StringRef S(reinterpret_cast<const char *>(GHR.Hash.data()),809                GHR.Hash.size());810    OS.emitBinaryData(S);811  }812}813 814void CodeViewDebug::emitObjName() {815  MCSymbol *CompilerEnd = beginSymbolRecord(SymbolKind::S_OBJNAME);816 817  StringRef PathRef(CompilerInfoAsm->TM.Options.ObjectFilenameForDebug);818  llvm::SmallString<256> PathStore(PathRef);819 820  if (PathRef.empty() || PathRef == "-") {821    // Don't emit the filename if we're writing to stdout or to /dev/null.822    PathRef = {};823  } else {824    PathRef = PathStore;825  }826 827  OS.AddComment("Signature");828  OS.emitIntValue(0, 4);829 830  OS.AddComment("Object name");831  emitNullTerminatedSymbolName(OS, PathRef);832 833  endSymbolRecord(CompilerEnd);834}835 836void CodeViewDebug::emitSecureHotPatchInformation() {837  MCSymbol *hotPatchInfo = nullptr;838 839  for (const auto &F : MMI->getModule()->functions()) {840    if (!F.isDeclarationForLinker() &&841        F.hasFnAttribute("marked_for_windows_hot_patching")) {842      if (hotPatchInfo == nullptr)843        hotPatchInfo = beginCVSubsection(DebugSubsectionKind::Symbols);844      MCSymbol *HotPatchEnd = beginSymbolRecord(SymbolKind::S_HOTPATCHFUNC);845      auto *SP = F.getSubprogram();846      OS.AddComment("Function");847      OS.emitInt32(getFuncIdForSubprogram(SP).getIndex());848      OS.AddComment("Name");849      emitNullTerminatedSymbolName(OS, F.getName());850      endSymbolRecord(HotPatchEnd);851    }852  }853 854  if (hotPatchInfo != nullptr)855    endCVSubsection(hotPatchInfo);856}857 858namespace {859struct Version {860  int Part[4];861};862} // end anonymous namespace863 864// Takes a StringRef like "clang 4.0.0.0 (other nonsense 123)" and parses out865// the version number.866static Version parseVersion(StringRef Name) {867  Version V = {{0}};868  int N = 0;869  for (const char C : Name) {870    if (isdigit(C)) {871      V.Part[N] *= 10;872      V.Part[N] += C - '0';873      V.Part[N] =874          std::min<int>(V.Part[N], std::numeric_limits<uint16_t>::max());875    } else if (C == '.') {876      ++N;877      if (N >= 4)878        return V;879    } else if (N > 0)880      return V;881  }882  return V;883}884 885void CodeViewDebug::emitCompilerInformation() {886  MCSymbol *CompilerEnd = beginSymbolRecord(SymbolKind::S_COMPILE3);887  uint32_t Flags = 0;888 889  // The low byte of the flags indicates the source language.890  Flags = CurrentSourceLanguage;891  // TODO:  Figure out which other flags need to be set.892  if (MMI->getModule()->getProfileSummary(/*IsCS*/ false) != nullptr) {893    Flags |= static_cast<uint32_t>(CompileSym3Flags::PGO);894  }895  using ArchType = llvm::Triple::ArchType;896  ArchType Arch = MMI->getModule()->getTargetTriple().getArch();897  if (CompilerInfoAsm->TM.Options.Hotpatch || Arch == ArchType::thumb ||898      Arch == ArchType::aarch64) {899    Flags |= static_cast<uint32_t>(CompileSym3Flags::HotPatch);900  }901 902  OS.AddComment("Flags and language");903  OS.emitInt32(Flags);904 905  OS.AddComment("CPUType");906  OS.emitInt16(static_cast<uint64_t>(TheCPU));907 908  StringRef CompilerVersion = "0";909  if (TheCU)910    CompilerVersion = TheCU->getProducer();911 912  Version FrontVer = parseVersion(CompilerVersion);913  OS.AddComment("Frontend version");914  for (int N : FrontVer.Part) {915    OS.emitInt16(N);916  }917 918  // Some Microsoft tools, like Binscope, expect a backend version number of at919  // least 8.something, so we'll coerce the LLVM version into a form that920  // guarantees it'll be big enough without really lying about the version.921  int Major = 1000 * LLVM_VERSION_MAJOR +922              10 * LLVM_VERSION_MINOR +923              LLVM_VERSION_PATCH;924  // Clamp it for builds that use unusually large version numbers.925  Major = std::min<int>(Major, std::numeric_limits<uint16_t>::max());926  Version BackVer = {{ Major, 0, 0, 0 }};927  OS.AddComment("Backend version");928  for (int N : BackVer.Part)929    OS.emitInt16(N);930 931  OS.AddComment("Null-terminated compiler version string");932  emitNullTerminatedSymbolName(OS, CompilerVersion);933 934  endSymbolRecord(CompilerEnd);935}936 937static TypeIndex getStringIdTypeIdx(GlobalTypeTableBuilder &TypeTable,938                                    StringRef S) {939  StringIdRecord SIR(TypeIndex(0x0), S);940  return TypeTable.writeLeafType(SIR);941}942 943void CodeViewDebug::emitBuildInfo() {944  // First, make LF_BUILDINFO. It's a sequence of strings with various bits of945  // build info. The known prefix is:946  // - Absolute path of current directory947  // - Compiler path948  // - Main source file path, relative to CWD or absolute949  // - Type server PDB file950  // - Canonical compiler command line951  // If frontend and backend compilation are separated (think llc or LTO), it's952  // not clear if the compiler path should refer to the executable for the953  // frontend or the backend. Leave it blank for now.954  TypeIndex BuildInfoArgs[BuildInfoRecord::MaxArgs] = {};955  NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");956  const MDNode *Node = *CUs->operands().begin(); // FIXME: Multiple CUs.957  const auto *CU = cast<DICompileUnit>(Node);958  const DIFile *MainSourceFile = CU->getFile();959  BuildInfoArgs[BuildInfoRecord::CurrentDirectory] =960      getStringIdTypeIdx(TypeTable, MainSourceFile->getDirectory());961  BuildInfoArgs[BuildInfoRecord::SourceFile] =962      getStringIdTypeIdx(TypeTable, MainSourceFile->getFilename());963  // FIXME: PDB is intentionally blank unless we implement /Zi type servers.964  BuildInfoArgs[BuildInfoRecord::TypeServerPDB] =965      getStringIdTypeIdx(TypeTable, "");966  BuildInfoArgs[BuildInfoRecord::BuildTool] =967      getStringIdTypeIdx(TypeTable, Asm->TM.Options.MCOptions.Argv0);968  BuildInfoArgs[BuildInfoRecord::CommandLine] = getStringIdTypeIdx(969      TypeTable, Asm->TM.Options.MCOptions.CommandlineArgs);970 971  BuildInfoRecord BIR(BuildInfoArgs);972  TypeIndex BuildInfoIndex = TypeTable.writeLeafType(BIR);973 974  // Make a new .debug$S subsection for the S_BUILDINFO record, which points975  // from the module symbols into the type stream.976  MCSymbol *BISubsecEnd = beginCVSubsection(DebugSubsectionKind::Symbols);977  MCSymbol *BIEnd = beginSymbolRecord(SymbolKind::S_BUILDINFO);978  OS.AddComment("LF_BUILDINFO index");979  OS.emitInt32(BuildInfoIndex.getIndex());980  endSymbolRecord(BIEnd);981  endCVSubsection(BISubsecEnd);982}983 984void CodeViewDebug::emitInlineeLinesSubsection() {985  if (InlinedSubprograms.empty())986    return;987 988  OS.AddComment("Inlinee lines subsection");989  MCSymbol *InlineEnd = beginCVSubsection(DebugSubsectionKind::InlineeLines);990 991  // We emit the checksum info for files.  This is used by debuggers to992  // determine if a pdb matches the source before loading it.  Visual Studio,993  // for instance, will display a warning that the breakpoints are not valid if994  // the pdb does not match the source.995  OS.AddComment("Inlinee lines signature");996  OS.emitInt32(unsigned(InlineeLinesSignature::Normal));997 998  for (const DISubprogram *SP : InlinedSubprograms) {999    assert(TypeIndices.count({SP, nullptr}));1000    TypeIndex InlineeIdx = TypeIndices[{SP, nullptr}];1001 1002    OS.addBlankLine();1003    unsigned FileId = maybeRecordFile(SP->getFile());1004    OS.AddComment("Inlined function " + SP->getName() + " starts at " +1005                  SP->getFilename() + Twine(':') + Twine(SP->getLine()));1006    OS.addBlankLine();1007    OS.AddComment("Type index of inlined function");1008    OS.emitInt32(InlineeIdx.getIndex());1009    OS.AddComment("Offset into filechecksum table");1010    OS.emitCVFileChecksumOffsetDirective(FileId);1011    OS.AddComment("Starting line number");1012    OS.emitInt32(SP->getLine());1013  }1014 1015  endCVSubsection(InlineEnd);1016}1017 1018void CodeViewDebug::emitInlinedCallSite(const FunctionInfo &FI,1019                                        const DILocation *InlinedAt,1020                                        const InlineSite &Site) {1021  assert(TypeIndices.count({Site.Inlinee, nullptr}));1022  TypeIndex InlineeIdx = TypeIndices[{Site.Inlinee, nullptr}];1023 1024  // SymbolRecord1025  MCSymbol *InlineEnd = beginSymbolRecord(SymbolKind::S_INLINESITE);1026 1027  OS.AddComment("PtrParent");1028  OS.emitInt32(0);1029  OS.AddComment("PtrEnd");1030  OS.emitInt32(0);1031  OS.AddComment("Inlinee type index");1032  OS.emitInt32(InlineeIdx.getIndex());1033 1034  unsigned FileId = maybeRecordFile(Site.Inlinee->getFile());1035  unsigned StartLineNum = Site.Inlinee->getLine();1036 1037  OS.emitCVInlineLinetableDirective(Site.SiteFuncId, FileId, StartLineNum,1038                                    FI.Begin, FI.End);1039 1040  endSymbolRecord(InlineEnd);1041 1042  emitLocalVariableList(FI, Site.InlinedLocals);1043 1044  // Recurse on child inlined call sites before closing the scope.1045  for (const DILocation *ChildSite : Site.ChildSites) {1046    auto I = FI.InlineSites.find(ChildSite);1047    assert(I != FI.InlineSites.end() &&1048           "child site not in function inline site map");1049    emitInlinedCallSite(FI, ChildSite, I->second);1050  }1051 1052  // Close the scope.1053  emitEndSymbolRecord(SymbolKind::S_INLINESITE_END);1054}1055 1056void CodeViewDebug::switchToDebugSectionForSymbol(const MCSymbol *GVSym) {1057  // If we have a symbol, it may be in a section that is COMDAT. If so, find the1058  // comdat key. A section may be comdat because of -ffunction-sections or1059  // because it is comdat in the IR.1060  MCSectionCOFF *GVSec =1061      GVSym ? static_cast<MCSectionCOFF *>(&GVSym->getSection()) : nullptr;1062  const MCSymbol *KeySym = GVSec ? GVSec->getCOMDATSymbol() : nullptr;1063 1064  auto *DebugSec = static_cast<MCSectionCOFF *>(1065      CompilerInfoAsm->getObjFileLowering().getCOFFDebugSymbolsSection());1066  DebugSec = OS.getContext().getAssociativeCOFFSection(DebugSec, KeySym);1067 1068  OS.switchSection(DebugSec);1069 1070  // Emit the magic version number if this is the first time we've switched to1071  // this section.1072  if (ComdatDebugSections.insert(DebugSec).second)1073    emitCodeViewMagicVersion();1074}1075 1076// Emit an S_THUNK32/S_END symbol pair for a thunk routine.1077// The only supported thunk ordinal is currently the standard type.1078void CodeViewDebug::emitDebugInfoForThunk(const Function *GV,1079                                          FunctionInfo &FI,1080                                          const MCSymbol *Fn) {1081  std::string FuncName =1082      std::string(GlobalValue::dropLLVMManglingEscape(GV->getName()));1083  const ThunkOrdinal ordinal = ThunkOrdinal::Standard; // Only supported kind.1084 1085  OS.AddComment("Symbol subsection for " + Twine(FuncName));1086  MCSymbol *SymbolsEnd = beginCVSubsection(DebugSubsectionKind::Symbols);1087 1088  // Emit S_THUNK321089  MCSymbol *ThunkRecordEnd = beginSymbolRecord(SymbolKind::S_THUNK32);1090  OS.AddComment("PtrParent");1091  OS.emitInt32(0);1092  OS.AddComment("PtrEnd");1093  OS.emitInt32(0);1094  OS.AddComment("PtrNext");1095  OS.emitInt32(0);1096  OS.AddComment("Thunk section relative address");1097  OS.emitCOFFSecRel32(Fn, /*Offset=*/0);1098  OS.AddComment("Thunk section index");1099  OS.emitCOFFSectionIndex(Fn);1100  OS.AddComment("Code size");1101  OS.emitAbsoluteSymbolDiff(FI.End, Fn, 2);1102  OS.AddComment("Ordinal");1103  OS.emitInt8(unsigned(ordinal));1104  OS.AddComment("Function name");1105  emitNullTerminatedSymbolName(OS, FuncName);1106  // Additional fields specific to the thunk ordinal would go here.1107  endSymbolRecord(ThunkRecordEnd);1108 1109  // Local variables/inlined routines are purposely omitted here.  The point of1110  // marking this as a thunk is so Visual Studio will NOT stop in this routine.1111 1112  // Emit S_PROC_ID_END1113  emitEndSymbolRecord(SymbolKind::S_PROC_ID_END);1114 1115  endCVSubsection(SymbolsEnd);1116}1117 1118void CodeViewDebug::emitDebugInfoForFunction(const Function *GV,1119                                             FunctionInfo &FI) {1120  // For each function there is a separate subsection which holds the PC to1121  // file:line table.1122  const MCSymbol *Fn = Asm->getSymbol(GV);1123  assert(Fn);1124 1125  // Switch to the to a comdat section, if appropriate.1126  switchToDebugSectionForSymbol(Fn);1127 1128  std::string FuncName;1129  auto *SP = GV->getSubprogram();1130  assert(SP);1131  setCurrentSubprogram(SP);1132 1133  if (SP->isThunk()) {1134    emitDebugInfoForThunk(GV, FI, Fn);1135    return;1136  }1137 1138  // If we have a display name, build the fully qualified name by walking the1139  // chain of scopes.1140  if (!SP->getName().empty())1141    FuncName = getFullyQualifiedName(SP->getScope(), SP->getName());1142 1143  // If our DISubprogram name is empty, use the mangled name.1144  if (FuncName.empty())1145    FuncName = std::string(GlobalValue::dropLLVMManglingEscape(GV->getName()));1146 1147  // Emit FPO data, but only on 32-bit x86. No other platforms use it.1148  if (MMI->getModule()->getTargetTriple().getArch() == Triple::x86)1149    OS.emitCVFPOData(Fn);1150 1151  // Emit a symbol subsection, required by VS2012+ to find function boundaries.1152  OS.AddComment("Symbol subsection for " + Twine(FuncName));1153  MCSymbol *SymbolsEnd = beginCVSubsection(DebugSubsectionKind::Symbols);1154  {1155    SymbolKind ProcKind = GV->hasLocalLinkage() ? SymbolKind::S_LPROC32_ID1156                                                : SymbolKind::S_GPROC32_ID;1157    MCSymbol *ProcRecordEnd = beginSymbolRecord(ProcKind);1158 1159    // These fields are filled in by tools like CVPACK which run after the fact.1160    OS.AddComment("PtrParent");1161    OS.emitInt32(0);1162    OS.AddComment("PtrEnd");1163    OS.emitInt32(0);1164    OS.AddComment("PtrNext");1165    OS.emitInt32(0);1166    // This is the important bit that tells the debugger where the function1167    // code is located and what's its size:1168    OS.AddComment("Code size");1169    OS.emitAbsoluteSymbolDiff(FI.End, Fn, 4);1170    OS.AddComment("Offset after prologue");1171    OS.emitInt32(0);1172    OS.AddComment("Offset before epilogue");1173    OS.emitInt32(0);1174    OS.AddComment("Function type index");1175    OS.emitInt32(getFuncIdForSubprogram(GV->getSubprogram()).getIndex());1176    OS.AddComment("Function section relative address");1177    OS.emitCOFFSecRel32(Fn, /*Offset=*/0);1178    OS.AddComment("Function section index");1179    OS.emitCOFFSectionIndex(Fn);1180    OS.AddComment("Flags");1181    ProcSymFlags ProcFlags = ProcSymFlags::HasOptimizedDebugInfo;1182    if (FI.HasFramePointer)1183      ProcFlags |= ProcSymFlags::HasFP;1184    if (GV->hasFnAttribute(Attribute::NoReturn))1185      ProcFlags |= ProcSymFlags::IsNoReturn;1186    if (GV->hasFnAttribute(Attribute::NoInline))1187      ProcFlags |= ProcSymFlags::IsNoInline;1188    OS.emitInt8(static_cast<uint8_t>(ProcFlags));1189    // Emit the function display name as a null-terminated string.1190    OS.AddComment("Function name");1191    // Truncate the name so we won't overflow the record length field.1192    emitNullTerminatedSymbolName(OS, FuncName);1193    endSymbolRecord(ProcRecordEnd);1194 1195    MCSymbol *FrameProcEnd = beginSymbolRecord(SymbolKind::S_FRAMEPROC);1196    // Subtract out the CSR size since MSVC excludes that and we include it.1197    OS.AddComment("FrameSize");1198    OS.emitInt32(FI.FrameSize - FI.CSRSize);1199    OS.AddComment("Padding");1200    OS.emitInt32(0);1201    OS.AddComment("Offset of padding");1202    OS.emitInt32(0);1203    OS.AddComment("Bytes of callee saved registers");1204    OS.emitInt32(FI.CSRSize);1205    OS.AddComment("Exception handler offset");1206    OS.emitInt32(0);1207    OS.AddComment("Exception handler section");1208    OS.emitInt16(0);1209    OS.AddComment("Flags (defines frame register)");1210    OS.emitInt32(uint32_t(FI.FrameProcOpts));1211    endSymbolRecord(FrameProcEnd);1212 1213    emitInlinees(FI.Inlinees);1214    emitLocalVariableList(FI, FI.Locals);1215    emitGlobalVariableList(FI.Globals);1216    emitLexicalBlockList(FI.ChildBlocks, FI);1217 1218    // Emit inlined call site information. Only emit functions inlined directly1219    // into the parent function. We'll emit the other sites recursively as part1220    // of their parent inline site.1221    for (const DILocation *InlinedAt : FI.ChildSites) {1222      auto I = FI.InlineSites.find(InlinedAt);1223      assert(I != FI.InlineSites.end() &&1224             "child site not in function inline site map");1225      emitInlinedCallSite(FI, InlinedAt, I->second);1226    }1227 1228    for (auto Annot : FI.Annotations) {1229      MCSymbol *Label = Annot.first;1230      MDTuple *Strs = cast<MDTuple>(Annot.second);1231      MCSymbol *AnnotEnd = beginSymbolRecord(SymbolKind::S_ANNOTATION);1232      OS.emitCOFFSecRel32(Label, /*Offset=*/0);1233      // FIXME: Make sure we don't overflow the max record size.1234      OS.emitCOFFSectionIndex(Label);1235      OS.emitInt16(Strs->getNumOperands());1236      for (Metadata *MD : Strs->operands()) {1237        // MDStrings are null terminated, so we can do EmitBytes and get the1238        // nice .asciz directive.1239        StringRef Str = cast<MDString>(MD)->getString();1240        assert(Str.data()[Str.size()] == '\0' && "non-nullterminated MDString");1241        OS.emitBytes(StringRef(Str.data(), Str.size() + 1));1242      }1243      endSymbolRecord(AnnotEnd);1244    }1245 1246    for (auto HeapAllocSite : FI.HeapAllocSites) {1247      const MCSymbol *BeginLabel = std::get<0>(HeapAllocSite);1248      const MCSymbol *EndLabel = std::get<1>(HeapAllocSite);1249      const DIType *DITy = std::get<2>(HeapAllocSite);1250      MCSymbol *HeapAllocEnd = beginSymbolRecord(SymbolKind::S_HEAPALLOCSITE);1251      OS.AddComment("Call site offset");1252      OS.emitCOFFSecRel32(BeginLabel, /*Offset=*/0);1253      OS.AddComment("Call site section index");1254      OS.emitCOFFSectionIndex(BeginLabel);1255      OS.AddComment("Call instruction length");1256      OS.emitAbsoluteSymbolDiff(EndLabel, BeginLabel, 2);1257      OS.AddComment("Type index");1258      OS.emitInt32(getCompleteTypeIndex(DITy).getIndex());1259      endSymbolRecord(HeapAllocEnd);1260    }1261 1262    if (SP != nullptr)1263      emitDebugInfoForUDTs(LocalUDTs);1264 1265    emitDebugInfoForJumpTables(FI);1266 1267    // We're done with this function.1268    emitEndSymbolRecord(SymbolKind::S_PROC_ID_END);1269  }1270  endCVSubsection(SymbolsEnd);1271 1272  // We have an assembler directive that takes care of the whole line table.1273  OS.emitCVLinetableDirective(FI.FuncId, Fn, FI.End);1274}1275 1276CodeViewDebug::LocalVarDef1277CodeViewDebug::createDefRangeMem(uint16_t CVRegister, int Offset) {1278  LocalVarDef DR;1279  DR.InMemory = -1;1280  DR.DataOffset = Offset;1281  assert(DR.DataOffset == Offset && "truncation");1282  DR.IsSubfield = 0;1283  DR.StructOffset = 0;1284  DR.CVRegister = CVRegister;1285  return DR;1286}1287 1288void CodeViewDebug::collectVariableInfoFromMFTable(1289    DenseSet<InlinedEntity> &Processed) {1290  const MachineFunction &MF = *Asm->MF;1291  const TargetSubtargetInfo &TSI = MF.getSubtarget();1292  const TargetFrameLowering *TFI = TSI.getFrameLowering();1293  const TargetRegisterInfo *TRI = TSI.getRegisterInfo();1294 1295  for (const MachineFunction::VariableDbgInfo &VI :1296       MF.getInStackSlotVariableDbgInfo()) {1297    if (!VI.Var)1298      continue;1299    assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) &&1300           "Expected inlined-at fields to agree");1301 1302    Processed.insert(InlinedEntity(VI.Var, VI.Loc->getInlinedAt()));1303    LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc);1304 1305    // If variable scope is not found then skip this variable.1306    if (!Scope)1307      continue;1308 1309    // If the variable has an attached offset expression, extract it.1310    // FIXME: Try to handle DW_OP_deref as well.1311    int64_t ExprOffset = 0;1312    bool Deref = false;1313    if (VI.Expr) {1314      // If there is one DW_OP_deref element, use offset of 0 and keep going.1315      if (VI.Expr->getNumElements() == 1 &&1316          VI.Expr->getElement(0) == llvm::dwarf::DW_OP_deref)1317        Deref = true;1318      else if (!VI.Expr->extractIfOffset(ExprOffset))1319        continue;1320    }1321 1322    // Get the frame register used and the offset.1323    Register FrameReg;1324    StackOffset FrameOffset =1325        TFI->getFrameIndexReference(*Asm->MF, VI.getStackSlot(), FrameReg);1326    uint16_t CVReg = TRI->getCodeViewRegNum(FrameReg);1327 1328    if (FrameOffset.getScalable()) {1329      // No encoding currently exists for scalable offsets; bail out.1330      continue;1331    }1332 1333    // Calculate the label ranges.1334    LocalVarDef DefRange =1335        createDefRangeMem(CVReg, FrameOffset.getFixed() + ExprOffset);1336 1337    LocalVariable Var;1338    Var.DIVar = VI.Var;1339 1340    for (const InsnRange &Range : Scope->getRanges()) {1341      const MCSymbol *Begin = getLabelBeforeInsn(Range.first);1342      const MCSymbol *End = getLabelAfterInsn(Range.second);1343      End = End ? End : Asm->getFunctionEnd();1344      Var.DefRanges[DefRange].emplace_back(Begin, End);1345    }1346 1347    if (Deref)1348      Var.UseReferenceType = true;1349 1350    recordLocalVariable(std::move(Var), Scope);1351  }1352}1353 1354static bool canUseReferenceType(const DbgVariableLocation &Loc) {1355  return !Loc.LoadChain.empty() && Loc.LoadChain.back() == 0;1356}1357 1358static bool needsReferenceType(const DbgVariableLocation &Loc) {1359  return Loc.LoadChain.size() == 2 && Loc.LoadChain.back() == 0;1360}1361 1362void CodeViewDebug::calculateRanges(1363    LocalVariable &Var, const DbgValueHistoryMap::Entries &Entries) {1364  const TargetRegisterInfo *TRI = Asm->MF->getSubtarget().getRegisterInfo();1365 1366  // Calculate the definition ranges.1367  for (auto I = Entries.begin(), E = Entries.end(); I != E; ++I) {1368    const auto &Entry = *I;1369    if (!Entry.isDbgValue())1370      continue;1371    const MachineInstr *DVInst = Entry.getInstr();1372    assert(DVInst->isDebugValue() && "Invalid History entry");1373    // FIXME: Find a way to represent constant variables, since they are1374    // relatively common.1375    std::optional<DbgVariableLocation> Location =1376        DbgVariableLocation::extractFromMachineInstruction(*DVInst);1377    if (!Location)1378    {1379      // When we don't have a location this is usually because LLVM has1380      // transformed it into a constant and we only have an llvm.dbg.value. We1381      // can't represent these well in CodeView since S_LOCAL only works on1382      // registers and memory locations. Instead, we will pretend this to be a1383      // constant value to at least have it show up in the debugger.1384      auto Op = DVInst->getDebugOperand(0);1385      if (Op.isImm())1386        Var.ConstantValue = APSInt(APInt(64, Op.getImm()), false);1387      continue;1388    }1389 1390    // CodeView can only express variables in register and variables in memory1391    // at a constant offset from a register. However, for variables passed1392    // indirectly by pointer, it is common for that pointer to be spilled to a1393    // stack location. For the special case of one offseted load followed by a1394    // zero offset load (a pointer spilled to the stack), we change the type of1395    // the local variable from a value type to a reference type. This tricks the1396    // debugger into doing the load for us.1397    if (Var.UseReferenceType) {1398      // We're using a reference type. Drop the last zero offset load.1399      if (canUseReferenceType(*Location))1400        Location->LoadChain.pop_back();1401      else1402        continue;1403    } else if (needsReferenceType(*Location)) {1404      // This location can't be expressed without switching to a reference type.1405      // Start over using that.1406      Var.UseReferenceType = true;1407      Var.DefRanges.clear();1408      calculateRanges(Var, Entries);1409      return;1410    }1411 1412    // We can only handle a register or an offseted load of a register.1413    if (!Location->Register || Location->LoadChain.size() > 1)1414      continue;1415 1416    // Codeview can only express byte-aligned offsets, ensure that we have a1417    // byte-boundaried location.1418    if (Location->FragmentInfo)1419      if (Location->FragmentInfo->OffsetInBits % 8)1420        continue;1421 1422    if (TRI->isIgnoredCVReg(Location->Register)) {1423      // No encoding currently exists for this register; bail out.1424      continue;1425    }1426 1427    LocalVarDef DR;1428    DR.CVRegister = TRI->getCodeViewRegNum(Location->Register);1429    DR.InMemory = !Location->LoadChain.empty();1430    DR.DataOffset =1431        !Location->LoadChain.empty() ? Location->LoadChain.back() : 0;1432    if (Location->FragmentInfo) {1433      DR.IsSubfield = true;1434      DR.StructOffset = Location->FragmentInfo->OffsetInBits / 8;1435    } else {1436      DR.IsSubfield = false;1437      DR.StructOffset = 0;1438    }1439 1440    // Compute the label range.1441    const MCSymbol *Begin = getLabelBeforeInsn(Entry.getInstr());1442    const MCSymbol *End;1443    if (Entry.getEndIndex() != DbgValueHistoryMap::NoEntry) {1444      auto &EndingEntry = Entries[Entry.getEndIndex()];1445      End = EndingEntry.isDbgValue()1446                ? getLabelBeforeInsn(EndingEntry.getInstr())1447                : getLabelAfterInsn(EndingEntry.getInstr());1448    } else1449      End = Asm->getFunctionEnd();1450 1451    // If the last range end is our begin, just extend the last range.1452    // Otherwise make a new range.1453    SmallVectorImpl<std::pair<const MCSymbol *, const MCSymbol *>> &R =1454        Var.DefRanges[DR];1455    if (!R.empty() && R.back().second == Begin)1456      R.back().second = End;1457    else1458      R.emplace_back(Begin, End);1459 1460    // FIXME: Do more range combining.1461  }1462}1463 1464void CodeViewDebug::collectVariableInfo(const DISubprogram *SP) {1465  DenseSet<InlinedEntity> Processed;1466  // Grab the variable info that was squirreled away in the MMI side-table.1467  collectVariableInfoFromMFTable(Processed);1468 1469  for (const auto &I : DbgValues) {1470    InlinedEntity IV = I.first;1471    if (Processed.count(IV))1472      continue;1473    const DILocalVariable *DIVar = cast<DILocalVariable>(IV.first);1474    const DILocation *InlinedAt = IV.second;1475 1476    // Instruction ranges, specifying where IV is accessible.1477    const auto &Entries = I.second;1478 1479    LexicalScope *Scope = nullptr;1480    if (InlinedAt)1481      Scope = LScopes.findInlinedScope(DIVar->getScope(), InlinedAt);1482    else1483      Scope = LScopes.findLexicalScope(DIVar->getScope());1484    // If variable scope is not found then skip this variable.1485    if (!Scope)1486      continue;1487 1488    LocalVariable Var;1489    Var.DIVar = DIVar;1490 1491    calculateRanges(Var, Entries);1492    recordLocalVariable(std::move(Var), Scope);1493  }1494}1495 1496void CodeViewDebug::beginFunctionImpl(const MachineFunction *MF) {1497  const TargetSubtargetInfo &TSI = MF->getSubtarget();1498  const TargetRegisterInfo *TRI = TSI.getRegisterInfo();1499  const MachineFrameInfo &MFI = MF->getFrameInfo();1500  const Function &GV = MF->getFunction();1501  auto Insertion = FnDebugInfo.insert({&GV, std::make_unique<FunctionInfo>()});1502  assert(Insertion.second && "function already has info");1503  CurFn = Insertion.first->second.get();1504  CurFn->FuncId = NextFuncId++;1505  CurFn->Begin = Asm->getFunctionBegin();1506 1507  // The S_FRAMEPROC record reports the stack size, and how many bytes of1508  // callee-saved registers were used. For targets that don't use a PUSH1509  // instruction (AArch64), this will be zero.1510  CurFn->CSRSize = MFI.getCVBytesOfCalleeSavedRegisters();1511  CurFn->FrameSize = MFI.getStackSize();1512  CurFn->OffsetAdjustment = MFI.getOffsetAdjustment();1513  CurFn->HasStackRealignment = TRI->hasStackRealignment(*MF);1514 1515  // For this function S_FRAMEPROC record, figure out which codeview register1516  // will be the frame pointer.1517  CurFn->EncodedParamFramePtrReg = EncodedFramePtrReg::None; // None.1518  CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::None; // None.1519  if (CurFn->FrameSize > 0) {1520    if (!TSI.getFrameLowering()->hasFP(*MF)) {1521      CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::StackPtr;1522      CurFn->EncodedParamFramePtrReg = EncodedFramePtrReg::StackPtr;1523    } else {1524      CurFn->HasFramePointer = true;1525      // If there is an FP, parameters are always relative to it.1526      CurFn->EncodedParamFramePtrReg = EncodedFramePtrReg::FramePtr;1527      if (CurFn->HasStackRealignment) {1528        // If the stack needs realignment, locals are relative to SP or VFRAME.1529        CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::StackPtr;1530      } else {1531        // Otherwise, locals are relative to EBP, and we probably have VLAs or1532        // other stack adjustments.1533        CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::FramePtr;1534      }1535    }1536  }1537 1538  // Compute other frame procedure options.1539  FrameProcedureOptions FPO = FrameProcedureOptions::None;1540  if (MFI.hasVarSizedObjects())1541    FPO |= FrameProcedureOptions::HasAlloca;1542  if (MF->exposesReturnsTwice())1543    FPO |= FrameProcedureOptions::HasSetJmp;1544  // FIXME: Set HasLongJmp if we ever track that info.1545  if (MF->hasInlineAsm())1546    FPO |= FrameProcedureOptions::HasInlineAssembly;1547  if (GV.hasPersonalityFn()) {1548    if (isAsynchronousEHPersonality(1549            classifyEHPersonality(GV.getPersonalityFn())))1550      FPO |= FrameProcedureOptions::HasStructuredExceptionHandling;1551    else1552      FPO |= FrameProcedureOptions::HasExceptionHandling;1553  }1554  if (GV.hasFnAttribute(Attribute::InlineHint))1555    FPO |= FrameProcedureOptions::MarkedInline;1556  if (GV.hasFnAttribute(Attribute::Naked))1557    FPO |= FrameProcedureOptions::Naked;1558  if (MFI.hasStackProtectorIndex()) {1559    FPO |= FrameProcedureOptions::SecurityChecks;1560    if (GV.hasFnAttribute(Attribute::StackProtectStrong) ||1561        GV.hasFnAttribute(Attribute::StackProtectReq)) {1562      FPO |= FrameProcedureOptions::StrictSecurityChecks;1563    }1564  } else if (!GV.hasStackProtectorFnAttr()) {1565    // __declspec(safebuffers) disables stack guards.1566    FPO |= FrameProcedureOptions::SafeBuffers;1567  }1568  FPO |= FrameProcedureOptions(uint32_t(CurFn->EncodedLocalFramePtrReg) << 14U);1569  FPO |= FrameProcedureOptions(uint32_t(CurFn->EncodedParamFramePtrReg) << 16U);1570  if (Asm->TM.getOptLevel() != CodeGenOptLevel::None && !GV.hasOptSize() &&1571      !GV.hasOptNone())1572    FPO |= FrameProcedureOptions::OptimizedForSpeed;1573  if (GV.hasProfileData()) {1574    FPO |= FrameProcedureOptions::ValidProfileCounts;1575    FPO |= FrameProcedureOptions::ProfileGuidedOptimization;1576  }1577  // FIXME: Set GuardCfg when it is implemented.1578  CurFn->FrameProcOpts = FPO;1579 1580  OS.emitCVFuncIdDirective(CurFn->FuncId);1581 1582  // Find the end of the function prolog.  First known non-DBG_VALUE and1583  // non-frame setup location marks the beginning of the function body.1584  // FIXME: is there a simpler a way to do this? Can we just search1585  // for the first instruction of the function, not the last of the prolog?1586  DebugLoc PrologEndLoc;1587  bool EmptyPrologue = true;1588  for (const auto &MBB : *MF) {1589    for (const auto &MI : MBB) {1590      if (!MI.isMetaInstruction() && !MI.getFlag(MachineInstr::FrameSetup) &&1591          MI.getDebugLoc()) {1592        PrologEndLoc = MI.getDebugLoc();1593        break;1594      } else if (!MI.isMetaInstruction()) {1595        EmptyPrologue = false;1596      }1597    }1598  }1599 1600  // Record beginning of function if we have a non-empty prologue.1601  if (PrologEndLoc && !EmptyPrologue) {1602    DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc();1603    maybeRecordLocation(FnStartDL, MF);1604  }1605 1606  // Find heap alloc sites and emit labels around them.1607  for (const auto &MBB : *MF) {1608    for (const auto &MI : MBB) {1609      if (MI.getHeapAllocMarker()) {1610        requestLabelBeforeInsn(&MI);1611        requestLabelAfterInsn(&MI);1612      }1613    }1614  }1615 1616  // Mark branches that may potentially be using jump tables with labels.1617  bool isThumb = MMI->getModule()->getTargetTriple().getArch() ==1618                 llvm::Triple::ArchType::thumb;1619  discoverJumpTableBranches(MF, isThumb);1620}1621 1622static bool shouldEmitUdt(const DIType *T) {1623  if (!T)1624    return false;1625 1626  // MSVC does not emit UDTs for typedefs that are scoped to classes.1627  if (T->getTag() == dwarf::DW_TAG_typedef) {1628    if (DIScope *Scope = T->getScope()) {1629      switch (Scope->getTag()) {1630      case dwarf::DW_TAG_structure_type:1631      case dwarf::DW_TAG_class_type:1632      case dwarf::DW_TAG_union_type:1633        return false;1634      default:1635          // do nothing.1636          ;1637      }1638    }1639  }1640 1641  while (true) {1642    if (!T || T->isForwardDecl())1643      return false;1644 1645    const DIDerivedType *DT = dyn_cast<DIDerivedType>(T);1646    if (!DT)1647      return true;1648    T = DT->getBaseType();1649  }1650  return true;1651}1652 1653void CodeViewDebug::addToUDTs(const DIType *Ty) {1654  // Don't record empty UDTs.1655  if (Ty->getName().empty())1656    return;1657  if (!shouldEmitUdt(Ty))1658    return;1659 1660  SmallVector<StringRef, 5> ParentScopeNames;1661  const DISubprogram *ClosestSubprogram =1662      collectParentScopeNames(Ty->getScope(), ParentScopeNames);1663 1664  std::string FullyQualifiedName =1665      formatNestedName(ParentScopeNames, getPrettyScopeName(Ty));1666 1667  if (ClosestSubprogram == nullptr) {1668    GlobalUDTs.emplace_back(std::move(FullyQualifiedName), Ty);1669  } else if (ClosestSubprogram == CurrentSubprogram) {1670    LocalUDTs.emplace_back(std::move(FullyQualifiedName), Ty);1671  }1672 1673  // TODO: What if the ClosestSubprogram is neither null or the current1674  // subprogram?  Currently, the UDT just gets dropped on the floor.1675  //1676  // The current behavior is not desirable.  To get maximal fidelity, we would1677  // need to perform all type translation before beginning emission of .debug$S1678  // and then make LocalUDTs a member of FunctionInfo1679}1680 1681TypeIndex CodeViewDebug::lowerType(const DIType *Ty, const DIType *ClassTy) {1682  // Generic dispatch for lowering an unknown type.1683  switch (Ty->getTag()) {1684  case dwarf::DW_TAG_array_type:1685    return lowerTypeArray(cast<DICompositeType>(Ty));1686  case dwarf::DW_TAG_typedef:1687    return lowerTypeAlias(cast<DIDerivedType>(Ty));1688  case dwarf::DW_TAG_base_type:1689    return lowerTypeBasic(cast<DIBasicType>(Ty));1690  case dwarf::DW_TAG_pointer_type:1691    if (cast<DIDerivedType>(Ty)->getName() == "__vtbl_ptr_type")1692      return lowerTypeVFTableShape(cast<DIDerivedType>(Ty));1693    [[fallthrough]];1694  case dwarf::DW_TAG_reference_type:1695  case dwarf::DW_TAG_rvalue_reference_type:1696    return lowerTypePointer(cast<DIDerivedType>(Ty));1697  case dwarf::DW_TAG_ptr_to_member_type:1698    return lowerTypeMemberPointer(cast<DIDerivedType>(Ty));1699  case dwarf::DW_TAG_restrict_type:1700  case dwarf::DW_TAG_const_type:1701  case dwarf::DW_TAG_volatile_type:1702  // TODO: add support for DW_TAG_atomic_type here1703    return lowerTypeModifier(cast<DIDerivedType>(Ty));1704  case dwarf::DW_TAG_subroutine_type:1705    if (ClassTy) {1706      // The member function type of a member function pointer has no1707      // ThisAdjustment.1708      return lowerTypeMemberFunction(cast<DISubroutineType>(Ty), ClassTy,1709                                     /*ThisAdjustment=*/0,1710                                     /*IsStaticMethod=*/false);1711    }1712    return lowerTypeFunction(cast<DISubroutineType>(Ty));1713  case dwarf::DW_TAG_enumeration_type:1714    return lowerTypeEnum(cast<DICompositeType>(Ty));1715  case dwarf::DW_TAG_class_type:1716  case dwarf::DW_TAG_structure_type:1717    return lowerTypeClass(cast<DICompositeType>(Ty));1718  case dwarf::DW_TAG_union_type:1719    return lowerTypeUnion(cast<DICompositeType>(Ty));1720  case dwarf::DW_TAG_string_type:1721    return lowerTypeString(cast<DIStringType>(Ty));1722  case dwarf::DW_TAG_unspecified_type:1723    if (Ty->getName() == "decltype(nullptr)")1724      return TypeIndex::NullptrT();1725    return TypeIndex::None();1726  default:1727    // Use the null type index.1728    return TypeIndex();1729  }1730}1731 1732TypeIndex CodeViewDebug::lowerTypeAlias(const DIDerivedType *Ty) {1733  TypeIndex UnderlyingTypeIndex = getTypeIndex(Ty->getBaseType());1734  StringRef TypeName = Ty->getName();1735 1736  addToUDTs(Ty);1737 1738  if (UnderlyingTypeIndex == TypeIndex(SimpleTypeKind::Int32Long) &&1739      TypeName == "HRESULT")1740    return TypeIndex(SimpleTypeKind::HResult);1741  if (UnderlyingTypeIndex == TypeIndex(SimpleTypeKind::UInt16Short) &&1742      TypeName == "wchar_t")1743    return TypeIndex(SimpleTypeKind::WideCharacter);1744 1745  return UnderlyingTypeIndex;1746}1747 1748TypeIndex CodeViewDebug::lowerTypeArray(const DICompositeType *Ty) {1749  const DIType *ElementType = Ty->getBaseType();1750  TypeIndex ElementTypeIndex = getTypeIndex(ElementType);1751  // IndexType is size_t, which depends on the bitness of the target.1752  TypeIndex IndexType = getPointerSizeInBytes() == 81753                            ? TypeIndex(SimpleTypeKind::UInt64Quad)1754                            : TypeIndex(SimpleTypeKind::UInt32Long);1755 1756  uint64_t ElementSize = getBaseTypeSize(ElementType) / 8;1757 1758  // Add subranges to array type.1759  DINodeArray Elements = Ty->getElements();1760  for (int i = Elements.size() - 1; i >= 0; --i) {1761    const DINode *Element = Elements[i];1762    assert(Element->getTag() == dwarf::DW_TAG_subrange_type);1763 1764    const DISubrange *Subrange = cast<DISubrange>(Element);1765    int64_t Count = -1;1766 1767    // If Subrange has a Count field, use it.1768    // Otherwise, if it has an upperboud, use (upperbound - lowerbound + 1),1769    // where lowerbound is from the LowerBound field of the Subrange,1770    // or the language default lowerbound if that field is unspecified.1771    if (auto *CI = dyn_cast_if_present<ConstantInt *>(Subrange->getCount()))1772      Count = CI->getSExtValue();1773    else if (auto *UI = dyn_cast_if_present<ConstantInt *>(1774                 Subrange->getUpperBound())) {1775      // Fortran uses 1 as the default lowerbound; other languages use 0.1776      int64_t Lowerbound = (moduleIsInFortran()) ? 1 : 0;1777      auto *LI = dyn_cast_if_present<ConstantInt *>(Subrange->getLowerBound());1778      Lowerbound = (LI) ? LI->getSExtValue() : Lowerbound;1779      Count = UI->getSExtValue() - Lowerbound + 1;1780    }1781 1782    // Forward declarations of arrays without a size and VLAs use a count of -1.1783    // Emit a count of zero in these cases to match what MSVC does for arrays1784    // without a size. MSVC doesn't support VLAs, so it's not clear what we1785    // should do for them even if we could distinguish them.1786    if (Count == -1)1787      Count = 0;1788 1789    // Update the element size and element type index for subsequent subranges.1790    ElementSize *= Count;1791 1792    // If this is the outermost array, use the size from the array. It will be1793    // more accurate if we had a VLA or an incomplete element type size.1794    uint64_t ArraySize =1795        (i == 0 && ElementSize == 0) ? Ty->getSizeInBits() / 8 : ElementSize;1796 1797    StringRef Name = (i == 0) ? Ty->getName() : "";1798    ArrayRecord AR(ElementTypeIndex, IndexType, ArraySize, Name);1799    ElementTypeIndex = TypeTable.writeLeafType(AR);1800  }1801 1802  return ElementTypeIndex;1803}1804 1805// This function lowers a Fortran character type (DIStringType).1806// Note that it handles only the character*n variant (using SizeInBits1807// field in DIString to describe the type size) at the moment.1808// Other variants (leveraging the StringLength and StringLengthExp1809// fields in DIStringType) remain TBD.1810TypeIndex CodeViewDebug::lowerTypeString(const DIStringType *Ty) {1811  TypeIndex CharType = TypeIndex(SimpleTypeKind::NarrowCharacter);1812  uint64_t ArraySize = Ty->getSizeInBits() >> 3;1813  StringRef Name = Ty->getName();1814  // IndexType is size_t, which depends on the bitness of the target.1815  TypeIndex IndexType = getPointerSizeInBytes() == 81816                            ? TypeIndex(SimpleTypeKind::UInt64Quad)1817                            : TypeIndex(SimpleTypeKind::UInt32Long);1818 1819  // Create a type of character array of ArraySize.1820  ArrayRecord AR(CharType, IndexType, ArraySize, Name);1821 1822  return TypeTable.writeLeafType(AR);1823}1824 1825TypeIndex CodeViewDebug::lowerTypeBasic(const DIBasicType *Ty) {1826  TypeIndex Index;1827  dwarf::TypeKind Kind;1828  uint32_t ByteSize;1829 1830  Kind = static_cast<dwarf::TypeKind>(Ty->getEncoding());1831  ByteSize = Ty->getSizeInBits() / 8;1832 1833  SimpleTypeKind STK = SimpleTypeKind::None;1834  switch (Kind) {1835  case dwarf::DW_ATE_address:1836    // FIXME: Translate1837    break;1838  case dwarf::DW_ATE_boolean:1839    switch (ByteSize) {1840    case 1:  STK = SimpleTypeKind::Boolean8;   break;1841    case 2:  STK = SimpleTypeKind::Boolean16;  break;1842    case 4:  STK = SimpleTypeKind::Boolean32;  break;1843    case 8:  STK = SimpleTypeKind::Boolean64;  break;1844    case 16: STK = SimpleTypeKind::Boolean128; break;1845    }1846    break;1847  case dwarf::DW_ATE_complex_float:1848    // The CodeView size for a complex represents the size of1849    // an individual component.1850    switch (ByteSize) {1851    case 4:  STK = SimpleTypeKind::Complex16;  break;1852    case 8:  STK = SimpleTypeKind::Complex32;  break;1853    case 16: STK = SimpleTypeKind::Complex64;  break;1854    case 20: STK = SimpleTypeKind::Complex80;  break;1855    case 32: STK = SimpleTypeKind::Complex128; break;1856    }1857    break;1858  case dwarf::DW_ATE_float:1859    switch (ByteSize) {1860    case 2:  STK = SimpleTypeKind::Float16;  break;1861    case 4:  STK = SimpleTypeKind::Float32;  break;1862    case 6:  STK = SimpleTypeKind::Float48;  break;1863    case 8:  STK = SimpleTypeKind::Float64;  break;1864    case 10: STK = SimpleTypeKind::Float80;  break;1865    case 16: STK = SimpleTypeKind::Float128; break;1866    }1867    break;1868  case dwarf::DW_ATE_signed:1869    switch (ByteSize) {1870    case 1:  STK = SimpleTypeKind::SignedCharacter; break;1871    case 2:  STK = SimpleTypeKind::Int16Short;      break;1872    case 4:  STK = SimpleTypeKind::Int32;           break;1873    case 8:  STK = SimpleTypeKind::Int64Quad;       break;1874    case 16: STK = SimpleTypeKind::Int128Oct;       break;1875    }1876    break;1877  case dwarf::DW_ATE_unsigned:1878    switch (ByteSize) {1879    case 1:  STK = SimpleTypeKind::UnsignedCharacter; break;1880    case 2:  STK = SimpleTypeKind::UInt16Short;       break;1881    case 4:  STK = SimpleTypeKind::UInt32;            break;1882    case 8:  STK = SimpleTypeKind::UInt64Quad;        break;1883    case 16: STK = SimpleTypeKind::UInt128Oct;        break;1884    }1885    break;1886  case dwarf::DW_ATE_UTF:1887    switch (ByteSize) {1888    case 1: STK = SimpleTypeKind::Character8; break;1889    case 2: STK = SimpleTypeKind::Character16; break;1890    case 4: STK = SimpleTypeKind::Character32; break;1891    }1892    break;1893  case dwarf::DW_ATE_signed_char:1894    if (ByteSize == 1)1895      STK = SimpleTypeKind::SignedCharacter;1896    break;1897  case dwarf::DW_ATE_unsigned_char:1898    if (ByteSize == 1)1899      STK = SimpleTypeKind::UnsignedCharacter;1900    break;1901  default:1902    break;1903  }1904 1905  // Apply some fixups based on the source-level type name.1906  // Include some amount of canonicalization from an old naming scheme Clang1907  // used to use for integer types (in an outdated effort to be compatible with1908  // GCC's debug info/GDB's behavior, which has since been addressed).1909  if (STK == SimpleTypeKind::Int32 &&1910      (Ty->getName() == "long int" || Ty->getName() == "long"))1911    STK = SimpleTypeKind::Int32Long;1912  if (STK == SimpleTypeKind::UInt32 && (Ty->getName() == "long unsigned int" ||1913                                        Ty->getName() == "unsigned long"))1914    STK = SimpleTypeKind::UInt32Long;1915  if (STK == SimpleTypeKind::UInt16Short &&1916      (Ty->getName() == "wchar_t" || Ty->getName() == "__wchar_t"))1917    STK = SimpleTypeKind::WideCharacter;1918  if ((STK == SimpleTypeKind::SignedCharacter ||1919       STK == SimpleTypeKind::UnsignedCharacter) &&1920      Ty->getName() == "char")1921    STK = SimpleTypeKind::NarrowCharacter;1922 1923  return TypeIndex(STK);1924}1925 1926TypeIndex CodeViewDebug::lowerTypePointer(const DIDerivedType *Ty,1927                                          PointerOptions PO) {1928  TypeIndex PointeeTI = getTypeIndex(Ty->getBaseType());1929 1930  // Pointers to simple types without any options can use SimpleTypeMode, rather1931  // than having a dedicated pointer type record.1932  if (PointeeTI.isSimple() && PO == PointerOptions::None &&1933      PointeeTI.getSimpleMode() == SimpleTypeMode::Direct &&1934      Ty->getTag() == dwarf::DW_TAG_pointer_type) {1935    SimpleTypeMode Mode = Ty->getSizeInBits() == 641936                              ? SimpleTypeMode::NearPointer641937                              : SimpleTypeMode::NearPointer32;1938    return TypeIndex(PointeeTI.getSimpleKind(), Mode);1939  }1940 1941  PointerKind PK =1942      Ty->getSizeInBits() == 64 ? PointerKind::Near64 : PointerKind::Near32;1943  PointerMode PM = PointerMode::Pointer;1944  switch (Ty->getTag()) {1945  default: llvm_unreachable("not a pointer tag type");1946  case dwarf::DW_TAG_pointer_type:1947    PM = PointerMode::Pointer;1948    break;1949  case dwarf::DW_TAG_reference_type:1950    PM = PointerMode::LValueReference;1951    break;1952  case dwarf::DW_TAG_rvalue_reference_type:1953    PM = PointerMode::RValueReference;1954    break;1955  }1956 1957  if (Ty->isObjectPointer())1958    PO |= PointerOptions::Const;1959 1960  PointerRecord PR(PointeeTI, PK, PM, PO, Ty->getSizeInBits() / 8);1961  return TypeTable.writeLeafType(PR);1962}1963 1964static PointerToMemberRepresentation1965translatePtrToMemberRep(unsigned SizeInBytes, bool IsPMF, unsigned Flags) {1966  // SizeInBytes being zero generally implies that the member pointer type was1967  // incomplete, which can happen if it is part of a function prototype. In this1968  // case, use the unknown model instead of the general model.1969  if (IsPMF) {1970    switch (Flags & DINode::FlagPtrToMemberRep) {1971    case 0:1972      return SizeInBytes == 0 ? PointerToMemberRepresentation::Unknown1973                              : PointerToMemberRepresentation::GeneralFunction;1974    case DINode::FlagSingleInheritance:1975      return PointerToMemberRepresentation::SingleInheritanceFunction;1976    case DINode::FlagMultipleInheritance:1977      return PointerToMemberRepresentation::MultipleInheritanceFunction;1978    case DINode::FlagVirtualInheritance:1979      return PointerToMemberRepresentation::VirtualInheritanceFunction;1980    }1981  } else {1982    switch (Flags & DINode::FlagPtrToMemberRep) {1983    case 0:1984      return SizeInBytes == 0 ? PointerToMemberRepresentation::Unknown1985                              : PointerToMemberRepresentation::GeneralData;1986    case DINode::FlagSingleInheritance:1987      return PointerToMemberRepresentation::SingleInheritanceData;1988    case DINode::FlagMultipleInheritance:1989      return PointerToMemberRepresentation::MultipleInheritanceData;1990    case DINode::FlagVirtualInheritance:1991      return PointerToMemberRepresentation::VirtualInheritanceData;1992    }1993  }1994  llvm_unreachable("invalid ptr to member representation");1995}1996 1997TypeIndex CodeViewDebug::lowerTypeMemberPointer(const DIDerivedType *Ty,1998                                                PointerOptions PO) {1999  assert(Ty->getTag() == dwarf::DW_TAG_ptr_to_member_type);2000  bool IsPMF = isa<DISubroutineType>(Ty->getBaseType());2001  TypeIndex ClassTI = getTypeIndex(Ty->getClassType());2002  TypeIndex PointeeTI =2003      getTypeIndex(Ty->getBaseType(), IsPMF ? Ty->getClassType() : nullptr);2004  PointerKind PK = getPointerSizeInBytes() == 8 ? PointerKind::Near642005                                                : PointerKind::Near32;2006  PointerMode PM = IsPMF ? PointerMode::PointerToMemberFunction2007                         : PointerMode::PointerToDataMember;2008 2009  assert(Ty->getSizeInBits() / 8 <= 0xff && "pointer size too big");2010  uint8_t SizeInBytes = Ty->getSizeInBits() / 8;2011  MemberPointerInfo MPI(2012      ClassTI, translatePtrToMemberRep(SizeInBytes, IsPMF, Ty->getFlags()));2013  PointerRecord PR(PointeeTI, PK, PM, PO, SizeInBytes, MPI);2014  return TypeTable.writeLeafType(PR);2015}2016 2017/// Given a DWARF calling convention, get the CodeView equivalent. If we don't2018/// have a translation, use the NearC convention.2019static CallingConvention dwarfCCToCodeView(unsigned DwarfCC) {2020  switch (DwarfCC) {2021  case dwarf::DW_CC_normal:             return CallingConvention::NearC;2022  case dwarf::DW_CC_BORLAND_msfastcall: return CallingConvention::NearFast;2023  case dwarf::DW_CC_BORLAND_thiscall:   return CallingConvention::ThisCall;2024  case dwarf::DW_CC_BORLAND_stdcall:    return CallingConvention::NearStdCall;2025  case dwarf::DW_CC_BORLAND_pascal:     return CallingConvention::NearPascal;2026  case dwarf::DW_CC_LLVM_vectorcall:    return CallingConvention::NearVector;2027  }2028  return CallingConvention::NearC;2029}2030 2031TypeIndex CodeViewDebug::lowerTypeModifier(const DIDerivedType *Ty) {2032  ModifierOptions Mods = ModifierOptions::None;2033  PointerOptions PO = PointerOptions::None;2034  bool IsModifier = true;2035  const DIType *BaseTy = Ty;2036  while (IsModifier && BaseTy) {2037    // FIXME: Need to add DWARF tags for __unaligned and _Atomic2038    switch (BaseTy->getTag()) {2039    case dwarf::DW_TAG_const_type:2040      Mods |= ModifierOptions::Const;2041      PO |= PointerOptions::Const;2042      break;2043    case dwarf::DW_TAG_volatile_type:2044      Mods |= ModifierOptions::Volatile;2045      PO |= PointerOptions::Volatile;2046      break;2047    case dwarf::DW_TAG_restrict_type:2048      // Only pointer types be marked with __restrict. There is no known flag2049      // for __restrict in LF_MODIFIER records.2050      PO |= PointerOptions::Restrict;2051      break;2052    default:2053      IsModifier = false;2054      break;2055    }2056    if (IsModifier)2057      BaseTy = cast<DIDerivedType>(BaseTy)->getBaseType();2058  }2059 2060  // Check if the inner type will use an LF_POINTER record. If so, the2061  // qualifiers will go in the LF_POINTER record. This comes up for types like2062  // 'int *const' and 'int *__restrict', not the more common cases like 'const2063  // char *'.2064  if (BaseTy) {2065    switch (BaseTy->getTag()) {2066    case dwarf::DW_TAG_pointer_type:2067    case dwarf::DW_TAG_reference_type:2068    case dwarf::DW_TAG_rvalue_reference_type:2069      return lowerTypePointer(cast<DIDerivedType>(BaseTy), PO);2070    case dwarf::DW_TAG_ptr_to_member_type:2071      return lowerTypeMemberPointer(cast<DIDerivedType>(BaseTy), PO);2072    default:2073      break;2074    }2075  }2076 2077  TypeIndex ModifiedTI = getTypeIndex(BaseTy);2078 2079  // Return the base type index if there aren't any modifiers. For example, the2080  // metadata could contain restrict wrappers around non-pointer types.2081  if (Mods == ModifierOptions::None)2082    return ModifiedTI;2083 2084  ModifierRecord MR(ModifiedTI, Mods);2085  return TypeTable.writeLeafType(MR);2086}2087 2088TypeIndex CodeViewDebug::lowerTypeFunction(const DISubroutineType *Ty) {2089  SmallVector<TypeIndex, 8> ReturnAndArgTypeIndices;2090  for (const DIType *ArgType : Ty->getTypeArray())2091    ReturnAndArgTypeIndices.push_back(getTypeIndex(ArgType));2092 2093  // MSVC uses type none for variadic argument.2094  if (ReturnAndArgTypeIndices.size() > 1 &&2095      ReturnAndArgTypeIndices.back() == TypeIndex::Void()) {2096    ReturnAndArgTypeIndices.back() = TypeIndex::None();2097  }2098  TypeIndex ReturnTypeIndex = TypeIndex::Void();2099  ArrayRef<TypeIndex> ArgTypeIndices = {};2100  if (!ReturnAndArgTypeIndices.empty()) {2101    auto ReturnAndArgTypesRef = ArrayRef(ReturnAndArgTypeIndices);2102    ReturnTypeIndex = ReturnAndArgTypesRef.consume_front();2103    ArgTypeIndices = ReturnAndArgTypesRef;2104  }2105 2106  ArgListRecord ArgListRec(TypeRecordKind::ArgList, ArgTypeIndices);2107  TypeIndex ArgListIndex = TypeTable.writeLeafType(ArgListRec);2108 2109  CallingConvention CC = dwarfCCToCodeView(Ty->getCC());2110 2111  FunctionOptions FO = getFunctionOptions(Ty);2112  ProcedureRecord Procedure(ReturnTypeIndex, CC, FO, ArgTypeIndices.size(),2113                            ArgListIndex);2114  return TypeTable.writeLeafType(Procedure);2115}2116 2117TypeIndex CodeViewDebug::lowerTypeMemberFunction(const DISubroutineType *Ty,2118                                                 const DIType *ClassTy,2119                                                 int ThisAdjustment,2120                                                 bool IsStaticMethod,2121                                                 FunctionOptions FO) {2122  // Lower the containing class type.2123  TypeIndex ClassType = getTypeIndex(ClassTy);2124 2125  DITypeRefArray ReturnAndArgs = Ty->getTypeArray();2126 2127  unsigned Index = 0;2128  SmallVector<TypeIndex, 8> ArgTypeIndices;2129  TypeIndex ReturnTypeIndex = TypeIndex::Void();2130  if (ReturnAndArgs.size() > Index) {2131    ReturnTypeIndex = getTypeIndex(ReturnAndArgs[Index++]);2132  }2133 2134  // If the first argument is a pointer type and this isn't a static method,2135  // treat it as the special 'this' parameter, which is encoded separately from2136  // the arguments.2137  TypeIndex ThisTypeIndex;2138  if (!IsStaticMethod && ReturnAndArgs.size() > Index) {2139    if (const DIDerivedType *PtrTy =2140            dyn_cast_or_null<DIDerivedType>(ReturnAndArgs[Index])) {2141      if (PtrTy->getTag() == dwarf::DW_TAG_pointer_type) {2142        ThisTypeIndex = getTypeIndexForThisPtr(PtrTy, Ty);2143        Index++;2144      }2145    }2146  }2147 2148  while (Index < ReturnAndArgs.size())2149    ArgTypeIndices.push_back(getTypeIndex(ReturnAndArgs[Index++]));2150 2151  // MSVC uses type none for variadic argument.2152  if (!ArgTypeIndices.empty() && ArgTypeIndices.back() == TypeIndex::Void())2153    ArgTypeIndices.back() = TypeIndex::None();2154 2155  ArgListRecord ArgListRec(TypeRecordKind::ArgList, ArgTypeIndices);2156  TypeIndex ArgListIndex = TypeTable.writeLeafType(ArgListRec);2157 2158  CallingConvention CC = dwarfCCToCodeView(Ty->getCC());2159 2160  MemberFunctionRecord MFR(ReturnTypeIndex, ClassType, ThisTypeIndex, CC, FO,2161                           ArgTypeIndices.size(), ArgListIndex, ThisAdjustment);2162  return TypeTable.writeLeafType(MFR);2163}2164 2165TypeIndex CodeViewDebug::lowerTypeVFTableShape(const DIDerivedType *Ty) {2166  unsigned VSlotCount =2167      Ty->getSizeInBits() / (8 * Asm->MAI->getCodePointerSize());2168  SmallVector<VFTableSlotKind, 4> Slots(VSlotCount, VFTableSlotKind::Near);2169 2170  VFTableShapeRecord VFTSR(Slots);2171  return TypeTable.writeLeafType(VFTSR);2172}2173 2174static MemberAccess translateAccessFlags(unsigned RecordTag, unsigned Flags) {2175  switch (Flags & DINode::FlagAccessibility) {2176  case DINode::FlagPrivate:   return MemberAccess::Private;2177  case DINode::FlagPublic:    return MemberAccess::Public;2178  case DINode::FlagProtected: return MemberAccess::Protected;2179  case 0:2180    // If there was no explicit access control, provide the default for the tag.2181    return RecordTag == dwarf::DW_TAG_class_type ? MemberAccess::Private2182                                                 : MemberAccess::Public;2183  }2184  llvm_unreachable("access flags are exclusive");2185}2186 2187static MethodOptions translateMethodOptionFlags(const DISubprogram *SP) {2188  if (SP->isArtificial())2189    return MethodOptions::CompilerGenerated;2190 2191  // FIXME: Handle other MethodOptions.2192 2193  return MethodOptions::None;2194}2195 2196static MethodKind translateMethodKindFlags(const DISubprogram *SP,2197                                           bool Introduced) {2198  if (SP->getFlags() & DINode::FlagStaticMember)2199    return MethodKind::Static;2200 2201  switch (SP->getVirtuality()) {2202  case dwarf::DW_VIRTUALITY_none:2203    break;2204  case dwarf::DW_VIRTUALITY_virtual:2205    return Introduced ? MethodKind::IntroducingVirtual : MethodKind::Virtual;2206  case dwarf::DW_VIRTUALITY_pure_virtual:2207    return Introduced ? MethodKind::PureIntroducingVirtual2208                      : MethodKind::PureVirtual;2209  default:2210    llvm_unreachable("unhandled virtuality case");2211  }2212 2213  return MethodKind::Vanilla;2214}2215 2216static TypeRecordKind getRecordKind(const DICompositeType *Ty) {2217  switch (Ty->getTag()) {2218  case dwarf::DW_TAG_class_type:2219    return TypeRecordKind::Class;2220  case dwarf::DW_TAG_structure_type:2221    return TypeRecordKind::Struct;2222  default:2223    llvm_unreachable("unexpected tag");2224  }2225}2226 2227/// Return ClassOptions that should be present on both the forward declaration2228/// and the defintion of a tag type.2229static ClassOptions getCommonClassOptions(const DICompositeType *Ty) {2230  ClassOptions CO = ClassOptions::None;2231 2232  // MSVC always sets this flag, even for local types. Clang doesn't always2233  // appear to give every type a linkage name, which may be problematic for us.2234  // FIXME: Investigate the consequences of not following them here.2235  if (!Ty->getIdentifier().empty())2236    CO |= ClassOptions::HasUniqueName;2237 2238  // Put the Nested flag on a type if it appears immediately inside a tag type.2239  // Do not walk the scope chain. Do not attempt to compute ContainsNestedClass2240  // here. That flag is only set on definitions, and not forward declarations.2241  const DIScope *ImmediateScope = Ty->getScope();2242  if (ImmediateScope && isa<DICompositeType>(ImmediateScope))2243    CO |= ClassOptions::Nested;2244 2245  // Put the Scoped flag on function-local types. MSVC puts this flag for enum2246  // type only when it has an immediate function scope. Clang never puts enums2247  // inside DILexicalBlock scopes. Enum types, as generated by clang, are2248  // always in function, class, or file scopes.2249  if (Ty->getTag() == dwarf::DW_TAG_enumeration_type) {2250    if (ImmediateScope && isa<DISubprogram>(ImmediateScope))2251      CO |= ClassOptions::Scoped;2252  } else {2253    for (const DIScope *Scope = ImmediateScope; Scope != nullptr;2254         Scope = Scope->getScope()) {2255      if (isa<DISubprogram>(Scope)) {2256        CO |= ClassOptions::Scoped;2257        break;2258      }2259    }2260  }2261 2262  return CO;2263}2264 2265void CodeViewDebug::addUDTSrcLine(const DIType *Ty, TypeIndex TI) {2266  switch (Ty->getTag()) {2267  case dwarf::DW_TAG_class_type:2268  case dwarf::DW_TAG_structure_type:2269  case dwarf::DW_TAG_union_type:2270  case dwarf::DW_TAG_enumeration_type:2271    break;2272  default:2273    return;2274  }2275 2276  if (const auto *File = Ty->getFile()) {2277    StringIdRecord SIDR(TypeIndex(0x0), getFullFilepath(File));2278    TypeIndex SIDI = TypeTable.writeLeafType(SIDR);2279 2280    UdtSourceLineRecord USLR(TI, SIDI, Ty->getLine());2281    TypeTable.writeLeafType(USLR);2282  }2283}2284 2285TypeIndex CodeViewDebug::lowerTypeEnum(const DICompositeType *Ty) {2286  ClassOptions CO = getCommonClassOptions(Ty);2287  TypeIndex FTI;2288  unsigned EnumeratorCount = 0;2289 2290  if (Ty->isForwardDecl()) {2291    CO |= ClassOptions::ForwardReference;2292  } else {2293    ContinuationRecordBuilder ContinuationBuilder;2294    ContinuationBuilder.begin(ContinuationRecordKind::FieldList);2295    for (const DINode *Element : Ty->getElements()) {2296      // We assume that the frontend provides all members in source declaration2297      // order, which is what MSVC does.2298      if (auto *Enumerator = dyn_cast_or_null<DIEnumerator>(Element)) {2299        // FIXME: Is it correct to always emit these as unsigned here?2300        EnumeratorRecord ER(MemberAccess::Public,2301                            APSInt(Enumerator->getValue(), true),2302                            Enumerator->getName());2303        ContinuationBuilder.writeMemberType(ER);2304        EnumeratorCount++;2305      }2306    }2307    FTI = TypeTable.insertRecord(ContinuationBuilder);2308  }2309 2310  std::string FullName = getFullyQualifiedName(Ty);2311 2312  EnumRecord ER(EnumeratorCount, CO, FTI, FullName, Ty->getIdentifier(),2313                getTypeIndex(Ty->getBaseType()));2314  TypeIndex EnumTI = TypeTable.writeLeafType(ER);2315 2316  addUDTSrcLine(Ty, EnumTI);2317 2318  return EnumTI;2319}2320 2321//===----------------------------------------------------------------------===//2322// ClassInfo2323//===----------------------------------------------------------------------===//2324 2325struct llvm::ClassInfo {2326  struct MemberInfo {2327    const DIDerivedType *MemberTypeNode;2328    uint64_t BaseOffset;2329  };2330  // [MemberInfo]2331  using MemberList = std::vector<MemberInfo>;2332 2333  using MethodsList = TinyPtrVector<const DISubprogram *>;2334  // MethodName -> MethodsList2335  using MethodsMap = MapVector<MDString *, MethodsList>;2336 2337  /// Base classes.2338  std::vector<const DIDerivedType *> Inheritance;2339 2340  /// Direct members.2341  MemberList Members;2342  // Direct overloaded methods gathered by name.2343  MethodsMap Methods;2344 2345  TypeIndex VShapeTI;2346 2347  std::vector<const DIType *> NestedTypes;2348};2349 2350void CodeViewDebug::clear() {2351  assert(CurFn == nullptr);2352  FileIdMap.clear();2353  FnDebugInfo.clear();2354  FileToFilepathMap.clear();2355  LocalUDTs.clear();2356  GlobalUDTs.clear();2357  TypeIndices.clear();2358  CompleteTypeIndices.clear();2359  ScopeGlobals.clear();2360  CVGlobalVariableOffsets.clear();2361}2362 2363void CodeViewDebug::collectMemberInfo(ClassInfo &Info,2364                                      const DIDerivedType *DDTy) {2365  if (!DDTy->getName().empty()) {2366    Info.Members.push_back({DDTy, 0});2367 2368    // Collect static const data members with values.2369    if ((DDTy->getFlags() & DINode::FlagStaticMember) ==2370        DINode::FlagStaticMember) {2371      if (DDTy->getConstant() && (isa<ConstantInt>(DDTy->getConstant()) ||2372                                  isa<ConstantFP>(DDTy->getConstant())))2373        StaticConstMembers.push_back(DDTy);2374    }2375 2376    return;2377  }2378 2379  // An unnamed member may represent a nested struct or union. Attempt to2380  // interpret the unnamed member as a DICompositeType possibly wrapped in2381  // qualifier types. Add all the indirect fields to the current record if that2382  // succeeds, and drop the member if that fails.2383  assert((DDTy->getOffsetInBits() % 8) == 0 && "Unnamed bitfield member!");2384  uint64_t Offset = DDTy->getOffsetInBits();2385  const DIType *Ty = DDTy->getBaseType();2386  bool FullyResolved = false;2387  while (!FullyResolved) {2388    switch (Ty->getTag()) {2389    case dwarf::DW_TAG_const_type:2390    case dwarf::DW_TAG_volatile_type:2391      // FIXME: we should apply the qualifier types to the indirect fields2392      // rather than dropping them.2393      Ty = cast<DIDerivedType>(Ty)->getBaseType();2394      break;2395    default:2396      FullyResolved = true;2397      break;2398    }2399  }2400 2401  const DICompositeType *DCTy = dyn_cast<DICompositeType>(Ty);2402  if (!DCTy)2403    return;2404 2405  ClassInfo NestedInfo = collectClassInfo(DCTy);2406  for (const ClassInfo::MemberInfo &IndirectField : NestedInfo.Members)2407    Info.Members.push_back(2408        {IndirectField.MemberTypeNode, IndirectField.BaseOffset + Offset});2409}2410 2411ClassInfo CodeViewDebug::collectClassInfo(const DICompositeType *Ty) {2412  ClassInfo Info;2413  // Add elements to structure type.2414  DINodeArray Elements = Ty->getElements();2415  for (auto *Element : Elements) {2416    // We assume that the frontend provides all members in source declaration2417    // order, which is what MSVC does.2418    if (!Element)2419      continue;2420    if (auto *SP = dyn_cast<DISubprogram>(Element)) {2421      Info.Methods[SP->getRawName()].push_back(SP);2422    } else if (auto *DDTy = dyn_cast<DIDerivedType>(Element)) {2423      if (DDTy->getTag() == dwarf::DW_TAG_member) {2424        collectMemberInfo(Info, DDTy);2425      } else if (DDTy->getTag() == dwarf::DW_TAG_inheritance) {2426        Info.Inheritance.push_back(DDTy);2427      } else if (DDTy->getTag() == dwarf::DW_TAG_pointer_type &&2428                 DDTy->getName() == "__vtbl_ptr_type") {2429        Info.VShapeTI = getTypeIndex(DDTy);2430      } else if (DDTy->getTag() == dwarf::DW_TAG_typedef) {2431        Info.NestedTypes.push_back(DDTy);2432      } else if (DDTy->getTag() == dwarf::DW_TAG_friend) {2433        // Ignore friend members. It appears that MSVC emitted info about2434        // friends in the past, but modern versions do not.2435      }2436    } else if (auto *Composite = dyn_cast<DICompositeType>(Element)) {2437      Info.NestedTypes.push_back(Composite);2438    }2439    // Skip other unrecognized kinds of elements.2440  }2441  return Info;2442}2443 2444static bool shouldAlwaysEmitCompleteClassType(const DICompositeType *Ty) {2445  // This routine is used by lowerTypeClass and lowerTypeUnion to determine2446  // if a complete type should be emitted instead of a forward reference.2447  return Ty->getName().empty() && Ty->getIdentifier().empty() &&2448      !Ty->isForwardDecl();2449}2450 2451TypeIndex CodeViewDebug::lowerTypeClass(const DICompositeType *Ty) {2452  // Emit the complete type for unnamed structs.  C++ classes with methods2453  // which have a circular reference back to the class type are expected to2454  // be named by the front-end and should not be "unnamed".  C unnamed2455  // structs should not have circular references.2456  if (shouldAlwaysEmitCompleteClassType(Ty)) {2457    // If this unnamed complete type is already in the process of being defined2458    // then the description of the type is malformed and cannot be emitted2459    // into CodeView correctly so report a fatal error.2460    auto I = CompleteTypeIndices.find(Ty);2461    if (I != CompleteTypeIndices.end() && I->second == TypeIndex())2462      report_fatal_error("cannot debug circular reference to unnamed type");2463    return getCompleteTypeIndex(Ty);2464  }2465 2466  // First, construct the forward decl.  Don't look into Ty to compute the2467  // forward decl options, since it might not be available in all TUs.2468  TypeRecordKind Kind = getRecordKind(Ty);2469  ClassOptions CO =2470      ClassOptions::ForwardReference | getCommonClassOptions(Ty);2471  std::string FullName = getFullyQualifiedName(Ty);2472  ClassRecord CR(Kind, 0, CO, TypeIndex(), TypeIndex(), TypeIndex(), 0,2473                 FullName, Ty->getIdentifier());2474  TypeIndex FwdDeclTI = TypeTable.writeLeafType(CR);2475  if (!Ty->isForwardDecl())2476    DeferredCompleteTypes.push_back(Ty);2477  return FwdDeclTI;2478}2479 2480TypeIndex CodeViewDebug::lowerCompleteTypeClass(const DICompositeType *Ty) {2481  // Construct the field list and complete type record.2482  TypeRecordKind Kind = getRecordKind(Ty);2483  ClassOptions CO = getCommonClassOptions(Ty);2484  TypeIndex FieldTI;2485  TypeIndex VShapeTI;2486  unsigned FieldCount;2487  bool ContainsNestedClass;2488  std::tie(FieldTI, VShapeTI, FieldCount, ContainsNestedClass) =2489      lowerRecordFieldList(Ty);2490 2491  if (ContainsNestedClass)2492    CO |= ClassOptions::ContainsNestedClass;2493 2494  // MSVC appears to set this flag by searching any destructor or method with2495  // FunctionOptions::Constructor among the emitted members. Clang AST has all2496  // the members, however special member functions are not yet emitted into2497  // debug information. For now checking a class's non-triviality seems enough.2498  // FIXME: not true for a nested unnamed struct.2499  if (isNonTrivial(Ty))2500    CO |= ClassOptions::HasConstructorOrDestructor;2501 2502  std::string FullName = getFullyQualifiedName(Ty);2503 2504  uint64_t SizeInBytes = Ty->getSizeInBits() / 8;2505 2506  ClassRecord CR(Kind, FieldCount, CO, FieldTI, TypeIndex(), VShapeTI,2507                 SizeInBytes, FullName, Ty->getIdentifier());2508  TypeIndex ClassTI = TypeTable.writeLeafType(CR);2509 2510  addUDTSrcLine(Ty, ClassTI);2511 2512  addToUDTs(Ty);2513 2514  return ClassTI;2515}2516 2517TypeIndex CodeViewDebug::lowerTypeUnion(const DICompositeType *Ty) {2518  // Emit the complete type for unnamed unions.2519  if (shouldAlwaysEmitCompleteClassType(Ty))2520    return getCompleteTypeIndex(Ty);2521 2522  ClassOptions CO =2523      ClassOptions::ForwardReference | getCommonClassOptions(Ty);2524  std::string FullName = getFullyQualifiedName(Ty);2525  UnionRecord UR(0, CO, TypeIndex(), 0, FullName, Ty->getIdentifier());2526  TypeIndex FwdDeclTI = TypeTable.writeLeafType(UR);2527  if (!Ty->isForwardDecl())2528    DeferredCompleteTypes.push_back(Ty);2529  return FwdDeclTI;2530}2531 2532TypeIndex CodeViewDebug::lowerCompleteTypeUnion(const DICompositeType *Ty) {2533  ClassOptions CO = ClassOptions::Sealed | getCommonClassOptions(Ty);2534  TypeIndex FieldTI;2535  unsigned FieldCount;2536  bool ContainsNestedClass;2537  std::tie(FieldTI, std::ignore, FieldCount, ContainsNestedClass) =2538      lowerRecordFieldList(Ty);2539 2540  if (ContainsNestedClass)2541    CO |= ClassOptions::ContainsNestedClass;2542 2543  uint64_t SizeInBytes = Ty->getSizeInBits() / 8;2544  std::string FullName = getFullyQualifiedName(Ty);2545 2546  UnionRecord UR(FieldCount, CO, FieldTI, SizeInBytes, FullName,2547                 Ty->getIdentifier());2548  TypeIndex UnionTI = TypeTable.writeLeafType(UR);2549 2550  addUDTSrcLine(Ty, UnionTI);2551 2552  addToUDTs(Ty);2553 2554  return UnionTI;2555}2556 2557std::tuple<TypeIndex, TypeIndex, unsigned, bool>2558CodeViewDebug::lowerRecordFieldList(const DICompositeType *Ty) {2559  // Manually count members. MSVC appears to count everything that generates a2560  // field list record. Each individual overload in a method overload group2561  // contributes to this count, even though the overload group is a single field2562  // list record.2563  unsigned MemberCount = 0;2564  ClassInfo Info = collectClassInfo(Ty);2565  ContinuationRecordBuilder ContinuationBuilder;2566  ContinuationBuilder.begin(ContinuationRecordKind::FieldList);2567 2568  // Create base classes.2569  for (const DIDerivedType *I : Info.Inheritance) {2570    if (I->getFlags() & DINode::FlagVirtual) {2571      // Virtual base.2572      unsigned VBPtrOffset = I->getVBPtrOffset();2573      // FIXME: Despite the accessor name, the offset is really in bytes.2574      unsigned VBTableIndex = I->getOffsetInBits() / 4;2575      auto RecordKind = (I->getFlags() & DINode::FlagIndirectVirtualBase) == DINode::FlagIndirectVirtualBase2576                            ? TypeRecordKind::IndirectVirtualBaseClass2577                            : TypeRecordKind::VirtualBaseClass;2578      VirtualBaseClassRecord VBCR(2579          RecordKind, translateAccessFlags(Ty->getTag(), I->getFlags()),2580          getTypeIndex(I->getBaseType()), getVBPTypeIndex(), VBPtrOffset,2581          VBTableIndex);2582 2583      ContinuationBuilder.writeMemberType(VBCR);2584      MemberCount++;2585    } else {2586      assert(I->getOffsetInBits() % 8 == 0 &&2587             "bases must be on byte boundaries");2588      BaseClassRecord BCR(translateAccessFlags(Ty->getTag(), I->getFlags()),2589                          getTypeIndex(I->getBaseType()),2590                          I->getOffsetInBits() / 8);2591      ContinuationBuilder.writeMemberType(BCR);2592      MemberCount++;2593    }2594  }2595 2596  // Create members.2597  for (ClassInfo::MemberInfo &MemberInfo : Info.Members) {2598    const DIDerivedType *Member = MemberInfo.MemberTypeNode;2599    TypeIndex MemberBaseType = getTypeIndex(Member->getBaseType());2600    StringRef MemberName = Member->getName();2601    MemberAccess Access =2602        translateAccessFlags(Ty->getTag(), Member->getFlags());2603 2604    if (Member->isStaticMember()) {2605      StaticDataMemberRecord SDMR(Access, MemberBaseType, MemberName);2606      ContinuationBuilder.writeMemberType(SDMR);2607      MemberCount++;2608      continue;2609    }2610 2611    // Virtual function pointer member.2612    if ((Member->getFlags() & DINode::FlagArtificial) &&2613        Member->getName().starts_with("_vptr$")) {2614      VFPtrRecord VFPR(getTypeIndex(Member->getBaseType()));2615      ContinuationBuilder.writeMemberType(VFPR);2616      MemberCount++;2617      continue;2618    }2619 2620    // Data member.2621    uint64_t MemberOffsetInBits =2622        Member->getOffsetInBits() + MemberInfo.BaseOffset;2623    if (Member->isBitField()) {2624      uint64_t StartBitOffset = MemberOffsetInBits;2625      if (const auto *CI =2626              dyn_cast_or_null<ConstantInt>(Member->getStorageOffsetInBits())) {2627        MemberOffsetInBits = CI->getZExtValue() + MemberInfo.BaseOffset;2628      }2629      StartBitOffset -= MemberOffsetInBits;2630      BitFieldRecord BFR(MemberBaseType, Member->getSizeInBits(),2631                         StartBitOffset);2632      MemberBaseType = TypeTable.writeLeafType(BFR);2633    }2634    uint64_t MemberOffsetInBytes = MemberOffsetInBits / 8;2635    DataMemberRecord DMR(Access, MemberBaseType, MemberOffsetInBytes,2636                         MemberName);2637    ContinuationBuilder.writeMemberType(DMR);2638    MemberCount++;2639  }2640 2641  // Create methods2642  for (auto &MethodItr : Info.Methods) {2643    StringRef Name = MethodItr.first->getString();2644 2645    std::vector<OneMethodRecord> Methods;2646    for (const DISubprogram *SP : MethodItr.second) {2647      TypeIndex MethodType = getMemberFunctionType(SP, Ty);2648      bool Introduced = SP->getFlags() & DINode::FlagIntroducedVirtual;2649 2650      unsigned VFTableOffset = -1;2651      if (Introduced)2652        VFTableOffset = SP->getVirtualIndex() * getPointerSizeInBytes();2653 2654      Methods.push_back(OneMethodRecord(2655          MethodType, translateAccessFlags(Ty->getTag(), SP->getFlags()),2656          translateMethodKindFlags(SP, Introduced),2657          translateMethodOptionFlags(SP), VFTableOffset, Name));2658      MemberCount++;2659    }2660    assert(!Methods.empty() && "Empty methods map entry");2661    if (Methods.size() == 1)2662      ContinuationBuilder.writeMemberType(Methods[0]);2663    else {2664      // FIXME: Make this use its own ContinuationBuilder so that2665      // MethodOverloadList can be split correctly.2666      MethodOverloadListRecord MOLR(Methods);2667      TypeIndex MethodList = TypeTable.writeLeafType(MOLR);2668 2669      OverloadedMethodRecord OMR(Methods.size(), MethodList, Name);2670      ContinuationBuilder.writeMemberType(OMR);2671    }2672  }2673 2674  // Create nested classes.2675  for (const DIType *Nested : Info.NestedTypes) {2676    NestedTypeRecord R(getTypeIndex(Nested), Nested->getName());2677    ContinuationBuilder.writeMemberType(R);2678    MemberCount++;2679  }2680 2681  TypeIndex FieldTI = TypeTable.insertRecord(ContinuationBuilder);2682  return std::make_tuple(FieldTI, Info.VShapeTI, MemberCount,2683                         !Info.NestedTypes.empty());2684}2685 2686TypeIndex CodeViewDebug::getVBPTypeIndex() {2687  if (!VBPType.getIndex()) {2688    // Make a 'const int *' type.2689    ModifierRecord MR(TypeIndex::Int32(), ModifierOptions::Const);2690    TypeIndex ModifiedTI = TypeTable.writeLeafType(MR);2691 2692    PointerKind PK = getPointerSizeInBytes() == 8 ? PointerKind::Near642693                                                  : PointerKind::Near32;2694    PointerMode PM = PointerMode::Pointer;2695    PointerOptions PO = PointerOptions::None;2696    PointerRecord PR(ModifiedTI, PK, PM, PO, getPointerSizeInBytes());2697    VBPType = TypeTable.writeLeafType(PR);2698  }2699 2700  return VBPType;2701}2702 2703TypeIndex CodeViewDebug::getTypeIndex(const DIType *Ty, const DIType *ClassTy) {2704  // The null DIType is the void type. Don't try to hash it.2705  if (!Ty)2706    return TypeIndex::Void();2707 2708  // Check if we've already translated this type. Don't try to do a2709  // get-or-create style insertion that caches the hash lookup across the2710  // lowerType call. It will update the TypeIndices map.2711  auto I = TypeIndices.find({Ty, ClassTy});2712  if (I != TypeIndices.end())2713    return I->second;2714 2715  TypeLoweringScope S(*this);2716  TypeIndex TI = lowerType(Ty, ClassTy);2717  return recordTypeIndexForDINode(Ty, TI, ClassTy);2718}2719 2720codeview::TypeIndex2721CodeViewDebug::getTypeIndexForThisPtr(const DIDerivedType *PtrTy,2722                                      const DISubroutineType *SubroutineTy) {2723  assert(PtrTy->getTag() == dwarf::DW_TAG_pointer_type &&2724         "this type must be a pointer type");2725 2726  PointerOptions Options = PointerOptions::None;2727  if (SubroutineTy->getFlags() & DINode::DIFlags::FlagLValueReference)2728    Options = PointerOptions::LValueRefThisPointer;2729  else if (SubroutineTy->getFlags() & DINode::DIFlags::FlagRValueReference)2730    Options = PointerOptions::RValueRefThisPointer;2731 2732  // Check if we've already translated this type.  If there is no ref qualifier2733  // on the function then we look up this pointer type with no associated class2734  // so that the TypeIndex for the this pointer can be shared with the type2735  // index for other pointers to this class type.  If there is a ref qualifier2736  // then we lookup the pointer using the subroutine as the parent type.2737  auto I = TypeIndices.find({PtrTy, SubroutineTy});2738  if (I != TypeIndices.end())2739    return I->second;2740 2741  TypeLoweringScope S(*this);2742  TypeIndex TI = lowerTypePointer(PtrTy, Options);2743  return recordTypeIndexForDINode(PtrTy, TI, SubroutineTy);2744}2745 2746TypeIndex CodeViewDebug::getTypeIndexForReferenceTo(const DIType *Ty) {2747  PointerRecord PR(getTypeIndex(Ty),2748                   getPointerSizeInBytes() == 8 ? PointerKind::Near642749                                                : PointerKind::Near32,2750                   PointerMode::LValueReference, PointerOptions::None,2751                   Ty->getSizeInBits() / 8);2752  return TypeTable.writeLeafType(PR);2753}2754 2755TypeIndex CodeViewDebug::getCompleteTypeIndex(const DIType *Ty) {2756  // The null DIType is the void type. Don't try to hash it.2757  if (!Ty)2758    return TypeIndex::Void();2759 2760  // Look through typedefs when getting the complete type index. Call2761  // getTypeIndex on the typdef to ensure that any UDTs are accumulated and are2762  // emitted only once.2763  if (Ty->getTag() == dwarf::DW_TAG_typedef)2764    (void)getTypeIndex(Ty);2765  while (Ty->getTag() == dwarf::DW_TAG_typedef)2766    Ty = cast<DIDerivedType>(Ty)->getBaseType();2767 2768  // If this is a non-record type, the complete type index is the same as the2769  // normal type index. Just call getTypeIndex.2770  switch (Ty->getTag()) {2771  case dwarf::DW_TAG_class_type:2772  case dwarf::DW_TAG_structure_type:2773  case dwarf::DW_TAG_union_type:2774    break;2775  default:2776    return getTypeIndex(Ty);2777  }2778 2779  const auto *CTy = cast<DICompositeType>(Ty);2780 2781  TypeLoweringScope S(*this);2782 2783  // Make sure the forward declaration is emitted first. It's unclear if this2784  // is necessary, but MSVC does it, and we should follow suit until we can show2785  // otherwise.2786  // We only emit a forward declaration for named types.2787  if (!CTy->getName().empty() || !CTy->getIdentifier().empty()) {2788    TypeIndex FwdDeclTI = getTypeIndex(CTy);2789 2790    // Just use the forward decl if we don't have complete type info. This2791    // might happen if the frontend is using modules and expects the complete2792    // definition to be emitted elsewhere.2793    if (CTy->isForwardDecl())2794      return FwdDeclTI;2795  }2796 2797  // Check if we've already translated the complete record type.2798  // Insert the type with a null TypeIndex to signify that the type is currently2799  // being lowered.2800  auto InsertResult = CompleteTypeIndices.try_emplace(CTy);2801  if (!InsertResult.second)2802    return InsertResult.first->second;2803 2804  TypeIndex TI;2805  switch (CTy->getTag()) {2806  case dwarf::DW_TAG_class_type:2807  case dwarf::DW_TAG_structure_type:2808    TI = lowerCompleteTypeClass(CTy);2809    break;2810  case dwarf::DW_TAG_union_type:2811    TI = lowerCompleteTypeUnion(CTy);2812    break;2813  default:2814    llvm_unreachable("not a record");2815  }2816 2817  // Update the type index associated with this CompositeType.  This cannot2818  // use the 'InsertResult' iterator above because it is potentially2819  // invalidated by map insertions which can occur while lowering the class2820  // type above.2821  CompleteTypeIndices[CTy] = TI;2822  return TI;2823}2824 2825/// Emit all the deferred complete record types. Try to do this in FIFO order,2826/// and do this until fixpoint, as each complete record type typically2827/// references2828/// many other record types.2829void CodeViewDebug::emitDeferredCompleteTypes() {2830  SmallVector<const DICompositeType *, 4> TypesToEmit;2831  while (!DeferredCompleteTypes.empty()) {2832    std::swap(DeferredCompleteTypes, TypesToEmit);2833    for (const DICompositeType *RecordTy : TypesToEmit)2834      getCompleteTypeIndex(RecordTy);2835    TypesToEmit.clear();2836  }2837}2838 2839void CodeViewDebug::emitLocalVariableList(const FunctionInfo &FI,2840                                          ArrayRef<LocalVariable> Locals) {2841  // Get the sorted list of parameters and emit them first.2842  SmallVector<const LocalVariable *, 6> Params;2843  for (const LocalVariable &L : Locals)2844    if (L.DIVar->isParameter())2845      Params.push_back(&L);2846  llvm::sort(Params, [](const LocalVariable *L, const LocalVariable *R) {2847    return L->DIVar->getArg() < R->DIVar->getArg();2848  });2849  for (const LocalVariable *L : Params)2850    emitLocalVariable(FI, *L);2851 2852  // Next emit all non-parameters in the order that we found them.2853  for (const LocalVariable &L : Locals) {2854    if (!L.DIVar->isParameter()) {2855      if (L.ConstantValue) {2856        // If ConstantValue is set we will emit it as a S_CONSTANT instead of a2857        // S_LOCAL in order to be able to represent it at all.2858        const DIType *Ty = L.DIVar->getType();2859        APSInt Val(*L.ConstantValue);2860        emitConstantSymbolRecord(Ty, Val, std::string(L.DIVar->getName()));2861      } else {2862        emitLocalVariable(FI, L);2863      }2864    }2865  }2866}2867 2868void CodeViewDebug::emitLocalVariable(const FunctionInfo &FI,2869                                      const LocalVariable &Var) {2870  // LocalSym record, see SymbolRecord.h for more info.2871  MCSymbol *LocalEnd = beginSymbolRecord(SymbolKind::S_LOCAL);2872 2873  LocalSymFlags Flags = LocalSymFlags::None;2874  if (Var.DIVar->isParameter())2875    Flags |= LocalSymFlags::IsParameter;2876  if (Var.DefRanges.empty())2877    Flags |= LocalSymFlags::IsOptimizedOut;2878 2879  OS.AddComment("TypeIndex");2880  TypeIndex TI = Var.UseReferenceType2881                     ? getTypeIndexForReferenceTo(Var.DIVar->getType())2882                     : getCompleteTypeIndex(Var.DIVar->getType());2883  OS.emitInt32(TI.getIndex());2884  OS.AddComment("Flags");2885  OS.emitInt16(static_cast<uint16_t>(Flags));2886  // Truncate the name so we won't overflow the record length field.2887  emitNullTerminatedSymbolName(OS, Var.DIVar->getName());2888  endSymbolRecord(LocalEnd);2889 2890  // Calculate the on disk prefix of the appropriate def range record. The2891  // records and on disk formats are described in SymbolRecords.h. BytePrefix2892  // should be big enough to hold all forms without memory allocation.2893  SmallString<20> BytePrefix;2894  for (const auto &Pair : Var.DefRanges) {2895    LocalVarDef DefRange = Pair.first;2896    const auto &Ranges = Pair.second;2897    BytePrefix.clear();2898    if (DefRange.InMemory) {2899      int Offset = DefRange.DataOffset;2900      unsigned Reg = DefRange.CVRegister;2901 2902      // 32-bit x86 call sequences often use PUSH instructions, which disrupt2903      // ESP-relative offsets. Use the virtual frame pointer, VFRAME or $T0,2904      // instead. In frames without stack realignment, $T0 will be the CFA.2905      if (RegisterId(Reg) == RegisterId::ESP) {2906        Reg = unsigned(RegisterId::VFRAME);2907        Offset += FI.OffsetAdjustment;2908      }2909 2910      // If we can use the chosen frame pointer for the frame and this isn't a2911      // sliced aggregate, use the smaller S_DEFRANGE_FRAMEPOINTER_REL record.2912      // Otherwise, use S_DEFRANGE_REGISTER_REL.2913      EncodedFramePtrReg EncFP = encodeFramePtrReg(RegisterId(Reg), TheCPU);2914      if (!DefRange.IsSubfield && EncFP != EncodedFramePtrReg::None &&2915          (bool(Flags & LocalSymFlags::IsParameter)2916               ? (EncFP == FI.EncodedParamFramePtrReg)2917               : (EncFP == FI.EncodedLocalFramePtrReg))) {2918        DefRangeFramePointerRelHeader DRHdr;2919        DRHdr.Offset = Offset;2920        OS.emitCVDefRangeDirective(Ranges, DRHdr);2921      } else {2922        uint16_t RegRelFlags = 0;2923        if (DefRange.IsSubfield) {2924          RegRelFlags = DefRangeRegisterRelSym::IsSubfieldFlag |2925                        (DefRange.StructOffset2926                         << DefRangeRegisterRelSym::OffsetInParentShift);2927        }2928        DefRangeRegisterRelHeader DRHdr;2929        DRHdr.Register = Reg;2930        DRHdr.Flags = RegRelFlags;2931        DRHdr.BasePointerOffset = Offset;2932        OS.emitCVDefRangeDirective(Ranges, DRHdr);2933      }2934    } else {2935      assert(DefRange.DataOffset == 0 && "unexpected offset into register");2936      if (DefRange.IsSubfield) {2937        DefRangeSubfieldRegisterHeader DRHdr;2938        DRHdr.Register = DefRange.CVRegister;2939        DRHdr.MayHaveNoName = 0;2940        DRHdr.OffsetInParent = DefRange.StructOffset;2941        OS.emitCVDefRangeDirective(Ranges, DRHdr);2942      } else {2943        DefRangeRegisterHeader DRHdr;2944        DRHdr.Register = DefRange.CVRegister;2945        DRHdr.MayHaveNoName = 0;2946        OS.emitCVDefRangeDirective(Ranges, DRHdr);2947      }2948    }2949  }2950}2951 2952void CodeViewDebug::emitLexicalBlockList(ArrayRef<LexicalBlock *> Blocks,2953                                         const FunctionInfo& FI) {2954  for (LexicalBlock *Block : Blocks)2955    emitLexicalBlock(*Block, FI);2956}2957 2958/// Emit an S_BLOCK32 and S_END record pair delimiting the contents of a2959/// lexical block scope.2960void CodeViewDebug::emitLexicalBlock(const LexicalBlock &Block,2961                                     const FunctionInfo& FI) {2962  MCSymbol *RecordEnd = beginSymbolRecord(SymbolKind::S_BLOCK32);2963  OS.AddComment("PtrParent");2964  OS.emitInt32(0); // PtrParent2965  OS.AddComment("PtrEnd");2966  OS.emitInt32(0); // PtrEnd2967  OS.AddComment("Code size");2968  OS.emitAbsoluteSymbolDiff(Block.End, Block.Begin, 4);   // Code Size2969  OS.AddComment("Function section relative address");2970  OS.emitCOFFSecRel32(Block.Begin, /*Offset=*/0); // Func Offset2971  OS.AddComment("Function section index");2972  OS.emitCOFFSectionIndex(FI.Begin); // Func Symbol2973  OS.AddComment("Lexical block name");2974  emitNullTerminatedSymbolName(OS, Block.Name);           // Name2975  endSymbolRecord(RecordEnd);2976 2977  // Emit variables local to this lexical block.2978  emitLocalVariableList(FI, Block.Locals);2979  emitGlobalVariableList(Block.Globals);2980 2981  // Emit lexical blocks contained within this block.2982  emitLexicalBlockList(Block.Children, FI);2983 2984  // Close the lexical block scope.2985  emitEndSymbolRecord(SymbolKind::S_END);2986}2987 2988/// Convenience routine for collecting lexical block information for a list2989/// of lexical scopes.2990void CodeViewDebug::collectLexicalBlockInfo(2991        SmallVectorImpl<LexicalScope *> &Scopes,2992        SmallVectorImpl<LexicalBlock *> &Blocks,2993        SmallVectorImpl<LocalVariable> &Locals,2994        SmallVectorImpl<CVGlobalVariable> &Globals) {2995  for (LexicalScope *Scope : Scopes)2996    collectLexicalBlockInfo(*Scope, Blocks, Locals, Globals);2997}2998 2999/// Populate the lexical blocks and local variable lists of the parent with3000/// information about the specified lexical scope.3001void CodeViewDebug::collectLexicalBlockInfo(3002    LexicalScope &Scope,3003    SmallVectorImpl<LexicalBlock *> &ParentBlocks,3004    SmallVectorImpl<LocalVariable> &ParentLocals,3005    SmallVectorImpl<CVGlobalVariable> &ParentGlobals) {3006  if (Scope.isAbstractScope())3007    return;3008 3009  // Gather information about the lexical scope including local variables,3010  // global variables, and address ranges.3011  bool IgnoreScope = false;3012  auto LI = ScopeVariables.find(&Scope);3013  SmallVectorImpl<LocalVariable> *Locals =3014      LI != ScopeVariables.end() ? &LI->second : nullptr;3015  auto GI = ScopeGlobals.find(Scope.getScopeNode());3016  SmallVectorImpl<CVGlobalVariable> *Globals =3017      GI != ScopeGlobals.end() ? GI->second.get() : nullptr;3018  const DILexicalBlock *DILB = dyn_cast<DILexicalBlock>(Scope.getScopeNode());3019  const SmallVectorImpl<InsnRange> &Ranges = Scope.getRanges();3020 3021  // Ignore lexical scopes which do not contain variables.3022  if (!Locals && !Globals)3023    IgnoreScope = true;3024 3025  // Ignore lexical scopes which are not lexical blocks.3026  if (!DILB)3027    IgnoreScope = true;3028 3029  // Ignore scopes which have too many address ranges to represent in the3030  // current CodeView format or do not have a valid address range.3031  //3032  // For lexical scopes with multiple address ranges you may be tempted to3033  // construct a single range covering every instruction where the block is3034  // live and everything in between.  Unfortunately, Visual Studio only3035  // displays variables from the first matching lexical block scope.  If the3036  // first lexical block contains exception handling code or cold code which3037  // is moved to the bottom of the routine creating a single range covering3038  // nearly the entire routine, then it will hide all other lexical blocks3039  // and the variables they contain.3040  if (Ranges.size() != 1 || !getLabelAfterInsn(Ranges.front().second))3041    IgnoreScope = true;3042 3043  if (IgnoreScope) {3044    // This scope can be safely ignored and eliminating it will reduce the3045    // size of the debug information. Be sure to collect any variable and scope3046    // information from the this scope or any of its children and collapse them3047    // into the parent scope.3048    if (Locals)3049      ParentLocals.append(Locals->begin(), Locals->end());3050    if (Globals)3051      ParentGlobals.append(Globals->begin(), Globals->end());3052    collectLexicalBlockInfo(Scope.getChildren(),3053                            ParentBlocks,3054                            ParentLocals,3055                            ParentGlobals);3056    return;3057  }3058 3059  // Create a new CodeView lexical block for this lexical scope.  If we've3060  // seen this DILexicalBlock before then the scope tree is malformed and3061  // we can handle this gracefully by not processing it a second time.3062  auto BlockInsertion = CurFn->LexicalBlocks.try_emplace(DILB);3063  if (!BlockInsertion.second)3064    return;3065 3066  // Create a lexical block containing the variables and collect the3067  // lexical block information for the children.3068  const InsnRange &Range = Ranges.front();3069  assert(Range.first && Range.second);3070  LexicalBlock &Block = BlockInsertion.first->second;3071  Block.Begin = getLabelBeforeInsn(Range.first);3072  Block.End = getLabelAfterInsn(Range.second);3073  assert(Block.Begin && "missing label for scope begin");3074  assert(Block.End && "missing label for scope end");3075  Block.Name = DILB->getName();3076  if (Locals)3077    Block.Locals = std::move(*Locals);3078  if (Globals)3079    Block.Globals = std::move(*Globals);3080  ParentBlocks.push_back(&Block);3081  collectLexicalBlockInfo(Scope.getChildren(),3082                          Block.Children,3083                          Block.Locals,3084                          Block.Globals);3085}3086 3087void CodeViewDebug::endFunctionImpl(const MachineFunction *MF) {3088  const Function &GV = MF->getFunction();3089  assert(FnDebugInfo.count(&GV));3090  assert(CurFn == FnDebugInfo[&GV].get());3091 3092  collectVariableInfo(GV.getSubprogram());3093 3094  // Build the lexical block structure to emit for this routine.3095  if (LexicalScope *CFS = LScopes.getCurrentFunctionScope())3096    collectLexicalBlockInfo(*CFS,3097                            CurFn->ChildBlocks,3098                            CurFn->Locals,3099                            CurFn->Globals);3100 3101  // Clear the scope and variable information from the map which will not be3102  // valid after we have finished processing this routine.  This also prepares3103  // the map for the subsequent routine.3104  ScopeVariables.clear();3105 3106  // Don't emit anything if we don't have any line tables.3107  // Thunks are compiler-generated and probably won't have source correlation.3108  if (!CurFn->HaveLineInfo && !GV.getSubprogram()->isThunk()) {3109    FnDebugInfo.erase(&GV);3110    CurFn = nullptr;3111    return;3112  }3113 3114  // Find heap alloc sites and add to list.3115  for (const auto &MBB : *MF) {3116    for (const auto &MI : MBB) {3117      if (MDNode *MD = MI.getHeapAllocMarker()) {3118        CurFn->HeapAllocSites.push_back(std::make_tuple(getLabelBeforeInsn(&MI),3119                                                        getLabelAfterInsn(&MI),3120                                                        dyn_cast<DIType>(MD)));3121      }3122    }3123  }3124 3125  bool isThumb = MMI->getModule()->getTargetTriple().getArch() ==3126                 llvm::Triple::ArchType::thumb;3127  collectDebugInfoForJumpTables(MF, isThumb);3128 3129  CurFn->Annotations = MF->getCodeViewAnnotations();3130 3131  CurFn->End = Asm->getFunctionEnd();3132 3133  CurFn = nullptr;3134}3135 3136// Usable locations are valid with non-zero line numbers. A line number of zero3137// corresponds to optimized code that doesn't have a distinct source location.3138// In this case, we try to use the previous or next source location depending on3139// the context.3140static bool isUsableDebugLoc(DebugLoc DL) {3141  return DL && DL.getLine() != 0;3142}3143 3144void CodeViewDebug::beginInstruction(const MachineInstr *MI) {3145  DebugHandlerBase::beginInstruction(MI);3146 3147  // Ignore DBG_VALUE and DBG_LABEL locations and function prologue.3148  if (!Asm || !CurFn || MI->isDebugInstr() ||3149      MI->getFlag(MachineInstr::FrameSetup))3150    return;3151 3152  // If the first instruction of a new MBB has no location, find the first3153  // instruction with a location and use that.3154  DebugLoc DL = MI->getDebugLoc();3155  if (!isUsableDebugLoc(DL) && MI->getParent() != PrevInstBB) {3156    for (const auto &NextMI : *MI->getParent()) {3157      if (NextMI.isDebugInstr())3158        continue;3159      DL = NextMI.getDebugLoc();3160      if (isUsableDebugLoc(DL))3161        break;3162    }3163    // FIXME: Handle the case where the BB has no valid locations. This would3164    // probably require doing a real dataflow analysis.3165  }3166  PrevInstBB = MI->getParent();3167 3168  // If we still don't have a debug location, don't record a location.3169  if (!isUsableDebugLoc(DL))3170    return;3171 3172  maybeRecordLocation(DL, Asm->MF);3173}3174 3175MCSymbol *CodeViewDebug::beginCVSubsection(DebugSubsectionKind Kind) {3176  MCSymbol *BeginLabel = MMI->getContext().createTempSymbol(),3177           *EndLabel = MMI->getContext().createTempSymbol();3178  OS.emitInt32(unsigned(Kind));3179  OS.AddComment("Subsection size");3180  OS.emitAbsoluteSymbolDiff(EndLabel, BeginLabel, 4);3181  OS.emitLabel(BeginLabel);3182  return EndLabel;3183}3184 3185void CodeViewDebug::endCVSubsection(MCSymbol *EndLabel) {3186  OS.emitLabel(EndLabel);3187  // Every subsection must be aligned to a 4-byte boundary.3188  OS.emitValueToAlignment(Align(4));3189}3190 3191static StringRef getSymbolName(SymbolKind SymKind) {3192  for (const EnumEntry<SymbolKind> &EE : getSymbolTypeNames())3193    if (EE.Value == SymKind)3194      return EE.Name;3195  return "";3196}3197 3198MCSymbol *CodeViewDebug::beginSymbolRecord(SymbolKind SymKind) {3199  MCSymbol *BeginLabel = MMI->getContext().createTempSymbol(),3200           *EndLabel = MMI->getContext().createTempSymbol();3201  OS.AddComment("Record length");3202  OS.emitAbsoluteSymbolDiff(EndLabel, BeginLabel, 2);3203  OS.emitLabel(BeginLabel);3204  if (OS.isVerboseAsm())3205    OS.AddComment("Record kind: " + getSymbolName(SymKind));3206  OS.emitInt16(unsigned(SymKind));3207  return EndLabel;3208}3209 3210void CodeViewDebug::endSymbolRecord(MCSymbol *SymEnd) {3211  // MSVC does not pad out symbol records to four bytes, but LLVM does to avoid3212  // an extra copy of every symbol record in LLD. This increases object file3213  // size by less than 1% in the clang build, and is compatible with the Visual3214  // C++ linker.3215  OS.emitValueToAlignment(Align(4));3216  OS.emitLabel(SymEnd);3217}3218 3219void CodeViewDebug::emitEndSymbolRecord(SymbolKind EndKind) {3220  OS.AddComment("Record length");3221  OS.emitInt16(2);3222  if (OS.isVerboseAsm())3223    OS.AddComment("Record kind: " + getSymbolName(EndKind));3224  OS.emitInt16(uint16_t(EndKind)); // Record Kind3225}3226 3227void CodeViewDebug::emitDebugInfoForUDTs(3228    const std::vector<std::pair<std::string, const DIType *>> &UDTs) {3229#ifndef NDEBUG3230  size_t OriginalSize = UDTs.size();3231#endif3232  for (const auto &UDT : UDTs) {3233    const DIType *T = UDT.second;3234    assert(shouldEmitUdt(T));3235    MCSymbol *UDTRecordEnd = beginSymbolRecord(SymbolKind::S_UDT);3236    OS.AddComment("Type");3237    OS.emitInt32(getCompleteTypeIndex(T).getIndex());3238    assert(OriginalSize == UDTs.size() &&3239           "getCompleteTypeIndex found new UDTs!");3240    emitNullTerminatedSymbolName(OS, UDT.first);3241    endSymbolRecord(UDTRecordEnd);3242  }3243}3244 3245void CodeViewDebug::collectGlobalVariableInfo() {3246  DenseMap<const DIGlobalVariableExpression *, const GlobalVariable *>3247      GlobalMap;3248  for (const GlobalVariable &GV : MMI->getModule()->globals()) {3249    SmallVector<DIGlobalVariableExpression *, 1> GVEs;3250    GV.getDebugInfo(GVEs);3251    for (const auto *GVE : GVEs)3252      GlobalMap[GVE] = &GV;3253  }3254 3255  NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");3256  for (const MDNode *Node : CUs->operands()) {3257    const auto *CU = cast<DICompileUnit>(Node);3258    for (const auto *GVE : CU->getGlobalVariables()) {3259      const DIGlobalVariable *DIGV = GVE->getVariable();3260      const DIExpression *DIE = GVE->getExpression();3261      // Don't emit string literals in CodeView, as the only useful parts are3262      // generally the filename and line number, which isn't possible to output3263      // in CodeView. String literals should be the only unnamed GlobalVariable3264      // with debug info.3265      if (DIGV->getName().empty()) continue;3266 3267      if ((DIE->getNumElements() == 2) &&3268          (DIE->getElement(0) == dwarf::DW_OP_plus_uconst))3269        // Record the constant offset for the variable.3270        //3271        // A Fortran common block uses this idiom to encode the offset3272        // of a variable from the common block's starting address.3273        CVGlobalVariableOffsets.insert(3274            std::make_pair(DIGV, DIE->getElement(1)));3275 3276      // Emit constant global variables in a global symbol section.3277      if (GlobalMap.count(GVE) == 0 && DIE->isConstant()) {3278        CVGlobalVariable CVGV = {DIGV, DIE};3279        GlobalVariables.emplace_back(std::move(CVGV));3280      }3281 3282      const auto *GV = GlobalMap.lookup(GVE);3283      if (!GV || GV->isDeclarationForLinker())3284        continue;3285 3286      DIScope *Scope = DIGV->getScope();3287      SmallVector<CVGlobalVariable, 1> *VariableList;3288      if (Scope && isa<DILocalScope>(Scope)) {3289        // Locate a global variable list for this scope, creating one if3290        // necessary.3291        auto Insertion = ScopeGlobals.insert(3292            {Scope, std::unique_ptr<GlobalVariableList>()});3293        if (Insertion.second)3294          Insertion.first->second = std::make_unique<GlobalVariableList>();3295        VariableList = Insertion.first->second.get();3296      } else if (GV->hasComdat())3297        // Emit this global variable into a COMDAT section.3298        VariableList = &ComdatVariables;3299      else3300        // Emit this global variable in a single global symbol section.3301        VariableList = &GlobalVariables;3302      CVGlobalVariable CVGV = {DIGV, GV};3303      VariableList->emplace_back(std::move(CVGV));3304    }3305  }3306}3307 3308void CodeViewDebug::collectDebugInfoForGlobals() {3309  for (const CVGlobalVariable &CVGV : GlobalVariables) {3310    const DIGlobalVariable *DIGV = CVGV.DIGV;3311    const DIScope *Scope = DIGV->getScope();3312    getCompleteTypeIndex(DIGV->getType());3313    getFullyQualifiedName(Scope, DIGV->getName());3314  }3315 3316  for (const CVGlobalVariable &CVGV : ComdatVariables) {3317    const DIGlobalVariable *DIGV = CVGV.DIGV;3318    const DIScope *Scope = DIGV->getScope();3319    getCompleteTypeIndex(DIGV->getType());3320    getFullyQualifiedName(Scope, DIGV->getName());3321  }3322}3323 3324void CodeViewDebug::emitDebugInfoForGlobals() {3325  // First, emit all globals that are not in a comdat in a single symbol3326  // substream. MSVC doesn't like it if the substream is empty, so only open3327  // it if we have at least one global to emit.3328  switchToDebugSectionForSymbol(nullptr);3329  if (!GlobalVariables.empty() || !StaticConstMembers.empty()) {3330    OS.AddComment("Symbol subsection for globals");3331    MCSymbol *EndLabel = beginCVSubsection(DebugSubsectionKind::Symbols);3332    emitGlobalVariableList(GlobalVariables);3333    emitStaticConstMemberList();3334    endCVSubsection(EndLabel);3335  }3336 3337  // Second, emit each global that is in a comdat into its own .debug$S3338  // section along with its own symbol substream.3339  for (const CVGlobalVariable &CVGV : ComdatVariables) {3340    const GlobalVariable *GV = cast<const GlobalVariable *>(CVGV.GVInfo);3341    MCSymbol *GVSym = Asm->getSymbol(GV);3342    OS.AddComment("Symbol subsection for " +3343                  Twine(GlobalValue::dropLLVMManglingEscape(GV->getName())));3344    switchToDebugSectionForSymbol(GVSym);3345    MCSymbol *EndLabel = beginCVSubsection(DebugSubsectionKind::Symbols);3346    // FIXME: emitDebugInfoForGlobal() doesn't handle DIExpressions.3347    emitDebugInfoForGlobal(CVGV);3348    endCVSubsection(EndLabel);3349  }3350}3351 3352void CodeViewDebug::emitDebugInfoForRetainedTypes() {3353  NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");3354  for (const MDNode *Node : CUs->operands()) {3355    for (auto *Ty : cast<DICompileUnit>(Node)->getRetainedTypes()) {3356      if (DIType *RT = dyn_cast<DIType>(Ty)) {3357        getTypeIndex(RT);3358        // FIXME: Add to global/local DTU list.3359      }3360    }3361  }3362}3363 3364// Emit each global variable in the specified array.3365void CodeViewDebug::emitGlobalVariableList(ArrayRef<CVGlobalVariable> Globals) {3366  for (const CVGlobalVariable &CVGV : Globals) {3367    // FIXME: emitDebugInfoForGlobal() doesn't handle DIExpressions.3368    emitDebugInfoForGlobal(CVGV);3369  }3370}3371 3372void CodeViewDebug::emitConstantSymbolRecord(const DIType *DTy, APSInt &Value,3373                                             const std::string &QualifiedName) {3374  MCSymbol *SConstantEnd = beginSymbolRecord(SymbolKind::S_CONSTANT);3375  OS.AddComment("Type");3376  OS.emitInt32(getTypeIndex(DTy).getIndex());3377 3378  OS.AddComment("Value");3379 3380  // Encoded integers shouldn't need more than 10 bytes.3381  uint8_t Data[10];3382  BinaryStreamWriter Writer(Data, llvm::endianness::little);3383  CodeViewRecordIO IO(Writer);3384  cantFail(IO.mapEncodedInteger(Value));3385  StringRef SRef((char *)Data, Writer.getOffset());3386  OS.emitBinaryData(SRef);3387 3388  OS.AddComment("Name");3389  emitNullTerminatedSymbolName(OS, QualifiedName);3390  endSymbolRecord(SConstantEnd);3391}3392 3393void CodeViewDebug::emitStaticConstMemberList() {3394  for (const DIDerivedType *DTy : StaticConstMembers) {3395    const DIScope *Scope = DTy->getScope();3396 3397    APSInt Value;3398    if (const ConstantInt *CI =3399            dyn_cast_or_null<ConstantInt>(DTy->getConstant()))3400      Value = APSInt(CI->getValue(),3401                     DebugHandlerBase::isUnsignedDIType(DTy->getBaseType()));3402    else if (const ConstantFP *CFP =3403                 dyn_cast_or_null<ConstantFP>(DTy->getConstant()))3404      Value = APSInt(CFP->getValueAPF().bitcastToAPInt(), true);3405    else3406      llvm_unreachable("cannot emit a constant without a value");3407 3408    emitConstantSymbolRecord(DTy->getBaseType(), Value,3409                             getFullyQualifiedName(Scope, DTy->getName()));3410  }3411}3412 3413static bool isFloatDIType(const DIType *Ty) {3414  if (isa<DICompositeType>(Ty))3415    return false;3416 3417  if (auto *DTy = dyn_cast<DIDerivedType>(Ty)) {3418    dwarf::Tag T = (dwarf::Tag)Ty->getTag();3419    if (T == dwarf::DW_TAG_pointer_type ||3420        T == dwarf::DW_TAG_ptr_to_member_type ||3421        T == dwarf::DW_TAG_reference_type ||3422        T == dwarf::DW_TAG_rvalue_reference_type)3423      return false;3424    assert(DTy->getBaseType() && "Expected valid base type");3425    return isFloatDIType(DTy->getBaseType());3426  }3427 3428  auto *BTy = cast<DIBasicType>(Ty);3429  return (BTy->getEncoding() == dwarf::DW_ATE_float);3430}3431 3432void CodeViewDebug::emitDebugInfoForGlobal(const CVGlobalVariable &CVGV) {3433  const DIGlobalVariable *DIGV = CVGV.DIGV;3434 3435  const DIScope *Scope = DIGV->getScope();3436  // For static data members, get the scope from the declaration.3437  if (const auto *MemberDecl = dyn_cast_or_null<DIDerivedType>(3438          DIGV->getRawStaticDataMemberDeclaration()))3439    Scope = MemberDecl->getScope();3440  // For static local variables and Fortran, the scoping portion is elided3441  // in its name so that we can reference the variable in the command line3442  // of the VS debugger.3443  std::string QualifiedName =3444      (moduleIsInFortran() || (Scope && isa<DILocalScope>(Scope)))3445          ? std::string(DIGV->getName())3446          : getFullyQualifiedName(Scope, DIGV->getName());3447 3448  if (const GlobalVariable *GV =3449          dyn_cast_if_present<const GlobalVariable *>(CVGV.GVInfo)) {3450    // DataSym record, see SymbolRecord.h for more info. Thread local data3451    // happens to have the same format as global data.3452    MCSymbol *GVSym = Asm->getSymbol(GV);3453    SymbolKind DataSym = GV->isThreadLocal()3454                             ? (DIGV->isLocalToUnit() ? SymbolKind::S_LTHREAD323455                                                      : SymbolKind::S_GTHREAD32)3456                             : (DIGV->isLocalToUnit() ? SymbolKind::S_LDATA323457                                                      : SymbolKind::S_GDATA32);3458    MCSymbol *DataEnd = beginSymbolRecord(DataSym);3459    OS.AddComment("Type");3460    OS.emitInt32(getCompleteTypeIndex(DIGV->getType()).getIndex());3461    OS.AddComment("DataOffset");3462 3463    // Use the offset seen while collecting info on globals.3464    uint64_t Offset = CVGlobalVariableOffsets.lookup(DIGV);3465    OS.emitCOFFSecRel32(GVSym, Offset);3466 3467    OS.AddComment("Segment");3468    OS.emitCOFFSectionIndex(GVSym);3469    OS.AddComment("Name");3470    const unsigned LengthOfDataRecord = 12;3471    emitNullTerminatedSymbolName(OS, QualifiedName, LengthOfDataRecord);3472    endSymbolRecord(DataEnd);3473  } else {3474    const DIExpression *DIE = cast<const DIExpression *>(CVGV.GVInfo);3475    assert(DIE->isConstant() &&3476           "Global constant variables must contain a constant expression.");3477 3478    // Use unsigned for floats.3479    bool isUnsigned = isFloatDIType(DIGV->getType())3480                          ? true3481                          : DebugHandlerBase::isUnsignedDIType(DIGV->getType());3482    APSInt Value(APInt(/*BitWidth=*/64, DIE->getElement(1)), isUnsigned);3483    emitConstantSymbolRecord(DIGV->getType(), Value, QualifiedName);3484  }3485}3486 3487void forEachJumpTableBranch(3488    const MachineFunction *MF, bool isThumb,3489    const std::function<void(const MachineJumpTableInfo &, const MachineInstr &,3490                             int64_t)> &Callback) {3491  auto JTI = MF->getJumpTableInfo();3492  if (JTI && !JTI->isEmpty()) {3493#ifndef NDEBUG3494    auto UsedJTs = llvm::SmallBitVector(JTI->getJumpTables().size());3495#endif3496    for (const auto &MBB : *MF) {3497      // Search for indirect branches...3498      const auto LastMI = MBB.getFirstTerminator();3499      if (LastMI != MBB.end() && LastMI->isIndirectBranch()) {3500        if (isThumb) {3501          // ... that directly use jump table operands.3502          // NOTE: ARM uses pattern matching to lower its BR_JT SDNode to3503          // machine instructions, hence inserting a JUMP_TABLE_DEBUG_INFO node3504          // interferes with this process *but* the resulting pseudo-instruction3505          // uses a Jump Table operand, so extract the jump table index directly3506          // from that.3507          for (const auto &MO : LastMI->operands()) {3508            if (MO.isJTI()) {3509              unsigned Index = MO.getIndex();3510#ifndef NDEBUG3511              UsedJTs.set(Index);3512#endif3513              Callback(*JTI, *LastMI, Index);3514              break;3515            }3516          }3517        } else {3518          // ... that have jump table debug info.3519          // NOTE: The debug info is inserted as a JUMP_TABLE_DEBUG_INFO node3520          // when lowering the BR_JT SDNode to an indirect branch.3521          for (auto I = MBB.instr_rbegin(), E = MBB.instr_rend(); I != E; ++I) {3522            if (I->isJumpTableDebugInfo()) {3523              unsigned Index = I->getOperand(0).getImm();3524#ifndef NDEBUG3525              UsedJTs.set(Index);3526#endif3527              Callback(*JTI, *LastMI, Index);3528              break;3529            }3530          }3531        }3532      }3533    }3534#ifndef NDEBUG3535    assert(UsedJTs.all() &&3536           "Some of jump tables were not used in a debug info instruction");3537#endif3538  }3539}3540 3541void CodeViewDebug::discoverJumpTableBranches(const MachineFunction *MF,3542                                              bool isThumb) {3543  forEachJumpTableBranch(3544      MF, isThumb,3545      [this](const MachineJumpTableInfo &, const MachineInstr &BranchMI,3546             int64_t) { requestLabelBeforeInsn(&BranchMI); });3547}3548 3549void CodeViewDebug::collectDebugInfoForJumpTables(const MachineFunction *MF,3550                                                  bool isThumb) {3551  forEachJumpTableBranch(3552      MF, isThumb,3553      [this, MF](const MachineJumpTableInfo &JTI, const MachineInstr &BranchMI,3554                 int64_t JumpTableIndex) {3555        // For label-difference jump tables, find the base expression.3556        // Otherwise the jump table uses an absolute address (so no base3557        // is required).3558        const MCSymbol *Base;3559        uint64_t BaseOffset = 0;3560        const MCSymbol *Branch = getLabelBeforeInsn(&BranchMI);3561        JumpTableEntrySize EntrySize;3562        switch (JTI.getEntryKind()) {3563        case MachineJumpTableInfo::EK_Custom32:3564        case MachineJumpTableInfo::EK_GPRel32BlockAddress:3565        case MachineJumpTableInfo::EK_GPRel64BlockAddress:3566          llvm_unreachable(3567              "EK_Custom32, EK_GPRel32BlockAddress, and "3568              "EK_GPRel64BlockAddress should never be emitted for COFF");3569        case MachineJumpTableInfo::EK_BlockAddress:3570          // Each entry is an absolute address.3571          EntrySize = JumpTableEntrySize::Pointer;3572          Base = nullptr;3573          break;3574        case MachineJumpTableInfo::EK_Inline:3575        case MachineJumpTableInfo::EK_LabelDifference32:3576        case MachineJumpTableInfo::EK_LabelDifference64:3577          // Ask the AsmPrinter.3578          std::tie(Base, BaseOffset, Branch, EntrySize) =3579              Asm->getCodeViewJumpTableInfo(JumpTableIndex, &BranchMI, Branch);3580          break;3581        }3582 3583        const MachineJumpTableEntry &JTE = JTI.getJumpTables()[JumpTableIndex];3584        JumpTableInfo CVJTI{EntrySize,3585                            Base,3586                            BaseOffset,3587                            Branch,3588                            MF->getJTISymbol(JumpTableIndex, MMI->getContext()),3589                            JTE.MBBs.size(),3590                            {}};3591        for (const auto &MBB : JTE.MBBs)3592          CVJTI.Cases.push_back(MBB->getSymbol());3593        CurFn->JumpTables.push_back(std::move(CVJTI));3594      });3595}3596 3597void CodeViewDebug::emitDebugInfoForJumpTables(const FunctionInfo &FI) {3598  // Emit S_LABEL32 records for each jump target3599  for (const auto &JumpTable : FI.JumpTables) {3600    for (const auto &CaseSym : JumpTable.Cases) {3601      MCSymbol *LabelEnd = beginSymbolRecord(SymbolKind::S_LABEL32);3602      OS.AddComment("Offset and segment");3603      OS.emitCOFFSecRel32(CaseSym, 0);3604      OS.AddComment("Flags");3605      OS.emitInt8(0);3606      emitNullTerminatedSymbolName(OS, CaseSym->getName());3607      endSymbolRecord(LabelEnd);3608    }3609  }3610 3611  for (const auto &JumpTable : FI.JumpTables) {3612    MCSymbol *JumpTableEnd = beginSymbolRecord(SymbolKind::S_ARMSWITCHTABLE);3613    if (JumpTable.Base) {3614      OS.AddComment("Base offset");3615      OS.emitCOFFSecRel32(JumpTable.Base, JumpTable.BaseOffset);3616      OS.AddComment("Base section index");3617      OS.emitCOFFSectionIndex(JumpTable.Base);3618    } else {3619      OS.AddComment("Base offset");3620      OS.emitInt32(0);3621      OS.AddComment("Base section index");3622      OS.emitInt16(0);3623    }3624    OS.AddComment("Switch type");3625    OS.emitInt16(static_cast<uint16_t>(JumpTable.EntrySize));3626    OS.AddComment("Branch offset");3627    OS.emitCOFFSecRel32(JumpTable.Branch, /*Offset=*/0);3628    OS.AddComment("Table offset");3629    OS.emitCOFFSecRel32(JumpTable.Table, /*Offset=*/0);3630    OS.AddComment("Branch section index");3631    OS.emitCOFFSectionIndex(JumpTable.Branch);3632    OS.AddComment("Table section index");3633    OS.emitCOFFSectionIndex(JumpTable.Table);3634    OS.AddComment("Entries count");3635    OS.emitInt32(JumpTable.TableSize);3636    endSymbolRecord(JumpTableEnd);3637  }3638}3639 3640void CodeViewDebug::emitInlinees(3641    const SmallSet<codeview::TypeIndex, 1> &Inlinees) {3642  // Divide the list of inlinees into chunks such that each chunk fits within3643  // one record.3644  constexpr size_t ChunkSize =3645      (MaxRecordLength - sizeof(SymbolKind) - sizeof(uint32_t)) /3646      sizeof(uint32_t);3647 3648  SmallVector<TypeIndex> SortedInlinees{Inlinees.begin(), Inlinees.end()};3649  llvm::sort(SortedInlinees);3650 3651  size_t CurrentIndex = 0;3652  while (CurrentIndex < SortedInlinees.size()) {3653    auto Symbol = beginSymbolRecord(SymbolKind::S_INLINEES);3654    auto CurrentChunkSize =3655        std::min(ChunkSize, SortedInlinees.size() - CurrentIndex);3656    OS.AddComment("Count");3657    OS.emitInt32(CurrentChunkSize);3658 3659    const size_t CurrentChunkEnd = CurrentIndex + CurrentChunkSize;3660    for (; CurrentIndex < CurrentChunkEnd; ++CurrentIndex) {3661      OS.AddComment("Inlinee");3662      OS.emitInt32(SortedInlinees[CurrentIndex].getIndex());3663    }3664    endSymbolRecord(Symbol);3665  }3666}3667