xref: /aosp_15_r20/external/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp --*- C++ -*--===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file contains support for writing Microsoft CodeView debug info.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker 
14*9880d681SAndroid Build Coastguard Worker #include "CodeViewDebug.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/TinyPtrVector.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/DebugInfo/CodeView/ByteStream.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/DebugInfo/CodeView/CodeView.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/DebugInfo/CodeView/FieldListRecordBuilder.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/DebugInfo/CodeView/Line.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/DebugInfo/CodeView/TypeDumper.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/DebugInfo/CodeView/TypeIndex.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/DebugInfo/CodeView/TypeRecord.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/DebugInfo/CodeView/TypeVisitorCallbacks.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Constants.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCExpr.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCSectionCOFF.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCSymbol.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/COFF.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ScopedPrinter.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetFrameLowering.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetRegisterInfo.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetSubtargetInfo.h"
35*9880d681SAndroid Build Coastguard Worker 
36*9880d681SAndroid Build Coastguard Worker using namespace llvm;
37*9880d681SAndroid Build Coastguard Worker using namespace llvm::codeview;
38*9880d681SAndroid Build Coastguard Worker 
CodeViewDebug(AsmPrinter * AP)39*9880d681SAndroid Build Coastguard Worker CodeViewDebug::CodeViewDebug(AsmPrinter *AP)
40*9880d681SAndroid Build Coastguard Worker     : DebugHandlerBase(AP), OS(*Asm->OutStreamer), CurFn(nullptr) {
41*9880d681SAndroid Build Coastguard Worker   // If module doesn't have named metadata anchors or COFF debug section
42*9880d681SAndroid Build Coastguard Worker   // is not available, skip any debug info related stuff.
43*9880d681SAndroid Build Coastguard Worker   if (!MMI->getModule()->getNamedMetadata("llvm.dbg.cu") ||
44*9880d681SAndroid Build Coastguard Worker       !AP->getObjFileLowering().getCOFFDebugSymbolsSection()) {
45*9880d681SAndroid Build Coastguard Worker     Asm = nullptr;
46*9880d681SAndroid Build Coastguard Worker     return;
47*9880d681SAndroid Build Coastguard Worker   }
48*9880d681SAndroid Build Coastguard Worker 
49*9880d681SAndroid Build Coastguard Worker   // Tell MMI that we have debug info.
50*9880d681SAndroid Build Coastguard Worker   MMI->setDebugInfoAvailability(true);
51*9880d681SAndroid Build Coastguard Worker }
52*9880d681SAndroid Build Coastguard Worker 
getFullFilepath(const DIFile * File)53*9880d681SAndroid Build Coastguard Worker StringRef CodeViewDebug::getFullFilepath(const DIFile *File) {
54*9880d681SAndroid Build Coastguard Worker   std::string &Filepath = FileToFilepathMap[File];
55*9880d681SAndroid Build Coastguard Worker   if (!Filepath.empty())
56*9880d681SAndroid Build Coastguard Worker     return Filepath;
57*9880d681SAndroid Build Coastguard Worker 
58*9880d681SAndroid Build Coastguard Worker   StringRef Dir = File->getDirectory(), Filename = File->getFilename();
59*9880d681SAndroid Build Coastguard Worker 
60*9880d681SAndroid Build Coastguard Worker   // Clang emits directory and relative filename info into the IR, but CodeView
61*9880d681SAndroid Build Coastguard Worker   // operates on full paths.  We could change Clang to emit full paths too, but
62*9880d681SAndroid Build Coastguard Worker   // that would increase the IR size and probably not needed for other users.
63*9880d681SAndroid Build Coastguard Worker   // For now, just concatenate and canonicalize the path here.
64*9880d681SAndroid Build Coastguard Worker   if (Filename.find(':') == 1)
65*9880d681SAndroid Build Coastguard Worker     Filepath = Filename;
66*9880d681SAndroid Build Coastguard Worker   else
67*9880d681SAndroid Build Coastguard Worker     Filepath = (Dir + "\\" + Filename).str();
68*9880d681SAndroid Build Coastguard Worker 
69*9880d681SAndroid Build Coastguard Worker   // Canonicalize the path.  We have to do it textually because we may no longer
70*9880d681SAndroid Build Coastguard Worker   // have access the file in the filesystem.
71*9880d681SAndroid Build Coastguard Worker   // First, replace all slashes with backslashes.
72*9880d681SAndroid Build Coastguard Worker   std::replace(Filepath.begin(), Filepath.end(), '/', '\\');
73*9880d681SAndroid Build Coastguard Worker 
74*9880d681SAndroid Build Coastguard Worker   // Remove all "\.\" with "\".
75*9880d681SAndroid Build Coastguard Worker   size_t Cursor = 0;
76*9880d681SAndroid Build Coastguard Worker   while ((Cursor = Filepath.find("\\.\\", Cursor)) != std::string::npos)
77*9880d681SAndroid Build Coastguard Worker     Filepath.erase(Cursor, 2);
78*9880d681SAndroid Build Coastguard Worker 
79*9880d681SAndroid Build Coastguard Worker   // Replace all "\XXX\..\" with "\".  Don't try too hard though as the original
80*9880d681SAndroid Build Coastguard Worker   // path should be well-formatted, e.g. start with a drive letter, etc.
81*9880d681SAndroid Build Coastguard Worker   Cursor = 0;
82*9880d681SAndroid Build Coastguard Worker   while ((Cursor = Filepath.find("\\..\\", Cursor)) != std::string::npos) {
83*9880d681SAndroid Build Coastguard Worker     // Something's wrong if the path starts with "\..\", abort.
84*9880d681SAndroid Build Coastguard Worker     if (Cursor == 0)
85*9880d681SAndroid Build Coastguard Worker       break;
86*9880d681SAndroid Build Coastguard Worker 
87*9880d681SAndroid Build Coastguard Worker     size_t PrevSlash = Filepath.rfind('\\', Cursor - 1);
88*9880d681SAndroid Build Coastguard Worker     if (PrevSlash == std::string::npos)
89*9880d681SAndroid Build Coastguard Worker       // Something's wrong, abort.
90*9880d681SAndroid Build Coastguard Worker       break;
91*9880d681SAndroid Build Coastguard Worker 
92*9880d681SAndroid Build Coastguard Worker     Filepath.erase(PrevSlash, Cursor + 3 - PrevSlash);
93*9880d681SAndroid Build Coastguard Worker     // The next ".." might be following the one we've just erased.
94*9880d681SAndroid Build Coastguard Worker     Cursor = PrevSlash;
95*9880d681SAndroid Build Coastguard Worker   }
96*9880d681SAndroid Build Coastguard Worker 
97*9880d681SAndroid Build Coastguard Worker   // Remove all duplicate backslashes.
98*9880d681SAndroid Build Coastguard Worker   Cursor = 0;
99*9880d681SAndroid Build Coastguard Worker   while ((Cursor = Filepath.find("\\\\", Cursor)) != std::string::npos)
100*9880d681SAndroid Build Coastguard Worker     Filepath.erase(Cursor, 1);
101*9880d681SAndroid Build Coastguard Worker 
102*9880d681SAndroid Build Coastguard Worker   return Filepath;
103*9880d681SAndroid Build Coastguard Worker }
104*9880d681SAndroid Build Coastguard Worker 
maybeRecordFile(const DIFile * F)105*9880d681SAndroid Build Coastguard Worker unsigned CodeViewDebug::maybeRecordFile(const DIFile *F) {
106*9880d681SAndroid Build Coastguard Worker   unsigned NextId = FileIdMap.size() + 1;
107*9880d681SAndroid Build Coastguard Worker   auto Insertion = FileIdMap.insert(std::make_pair(F, NextId));
108*9880d681SAndroid Build Coastguard Worker   if (Insertion.second) {
109*9880d681SAndroid Build Coastguard Worker     // We have to compute the full filepath and emit a .cv_file directive.
110*9880d681SAndroid Build Coastguard Worker     StringRef FullPath = getFullFilepath(F);
111*9880d681SAndroid Build Coastguard Worker     NextId = OS.EmitCVFileDirective(NextId, FullPath);
112*9880d681SAndroid Build Coastguard Worker     assert(NextId == FileIdMap.size() && ".cv_file directive failed");
113*9880d681SAndroid Build Coastguard Worker   }
114*9880d681SAndroid Build Coastguard Worker   return Insertion.first->second;
115*9880d681SAndroid Build Coastguard Worker }
116*9880d681SAndroid Build Coastguard Worker 
117*9880d681SAndroid Build Coastguard Worker CodeViewDebug::InlineSite &
getInlineSite(const DILocation * InlinedAt,const DISubprogram * Inlinee)118*9880d681SAndroid Build Coastguard Worker CodeViewDebug::getInlineSite(const DILocation *InlinedAt,
119*9880d681SAndroid Build Coastguard Worker                              const DISubprogram *Inlinee) {
120*9880d681SAndroid Build Coastguard Worker   auto SiteInsertion = CurFn->InlineSites.insert({InlinedAt, InlineSite()});
121*9880d681SAndroid Build Coastguard Worker   InlineSite *Site = &SiteInsertion.first->second;
122*9880d681SAndroid Build Coastguard Worker   if (SiteInsertion.second) {
123*9880d681SAndroid Build Coastguard Worker     Site->SiteFuncId = NextFuncId++;
124*9880d681SAndroid Build Coastguard Worker     Site->Inlinee = Inlinee;
125*9880d681SAndroid Build Coastguard Worker     InlinedSubprograms.insert(Inlinee);
126*9880d681SAndroid Build Coastguard Worker     getFuncIdForSubprogram(Inlinee);
127*9880d681SAndroid Build Coastguard Worker   }
128*9880d681SAndroid Build Coastguard Worker   return *Site;
129*9880d681SAndroid Build Coastguard Worker }
130*9880d681SAndroid Build Coastguard Worker 
getPrettyScopeName(const DIScope * Scope)131*9880d681SAndroid Build Coastguard Worker static StringRef getPrettyScopeName(const DIScope *Scope) {
132*9880d681SAndroid Build Coastguard Worker   StringRef ScopeName = Scope->getName();
133*9880d681SAndroid Build Coastguard Worker   if (!ScopeName.empty())
134*9880d681SAndroid Build Coastguard Worker     return ScopeName;
135*9880d681SAndroid Build Coastguard Worker 
136*9880d681SAndroid Build Coastguard Worker   switch (Scope->getTag()) {
137*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_TAG_enumeration_type:
138*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_TAG_class_type:
139*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_TAG_structure_type:
140*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_TAG_union_type:
141*9880d681SAndroid Build Coastguard Worker     return "<unnamed-tag>";
142*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_TAG_namespace:
143*9880d681SAndroid Build Coastguard Worker     return "`anonymous namespace'";
144*9880d681SAndroid Build Coastguard Worker   }
145*9880d681SAndroid Build Coastguard Worker 
146*9880d681SAndroid Build Coastguard Worker   return StringRef();
147*9880d681SAndroid Build Coastguard Worker }
148*9880d681SAndroid Build Coastguard Worker 
getQualifiedNameComponents(const DIScope * Scope,SmallVectorImpl<StringRef> & QualifiedNameComponents)149*9880d681SAndroid Build Coastguard Worker static const DISubprogram *getQualifiedNameComponents(
150*9880d681SAndroid Build Coastguard Worker     const DIScope *Scope, SmallVectorImpl<StringRef> &QualifiedNameComponents) {
151*9880d681SAndroid Build Coastguard Worker   const DISubprogram *ClosestSubprogram = nullptr;
152*9880d681SAndroid Build Coastguard Worker   while (Scope != nullptr) {
153*9880d681SAndroid Build Coastguard Worker     if (ClosestSubprogram == nullptr)
154*9880d681SAndroid Build Coastguard Worker       ClosestSubprogram = dyn_cast<DISubprogram>(Scope);
155*9880d681SAndroid Build Coastguard Worker     StringRef ScopeName = getPrettyScopeName(Scope);
156*9880d681SAndroid Build Coastguard Worker     if (!ScopeName.empty())
157*9880d681SAndroid Build Coastguard Worker       QualifiedNameComponents.push_back(ScopeName);
158*9880d681SAndroid Build Coastguard Worker     Scope = Scope->getScope().resolve();
159*9880d681SAndroid Build Coastguard Worker   }
160*9880d681SAndroid Build Coastguard Worker   return ClosestSubprogram;
161*9880d681SAndroid Build Coastguard Worker }
162*9880d681SAndroid Build Coastguard Worker 
getQualifiedName(ArrayRef<StringRef> QualifiedNameComponents,StringRef TypeName)163*9880d681SAndroid Build Coastguard Worker static std::string getQualifiedName(ArrayRef<StringRef> QualifiedNameComponents,
164*9880d681SAndroid Build Coastguard Worker                                     StringRef TypeName) {
165*9880d681SAndroid Build Coastguard Worker   std::string FullyQualifiedName;
166*9880d681SAndroid Build Coastguard Worker   for (StringRef QualifiedNameComponent : reverse(QualifiedNameComponents)) {
167*9880d681SAndroid Build Coastguard Worker     FullyQualifiedName.append(QualifiedNameComponent);
168*9880d681SAndroid Build Coastguard Worker     FullyQualifiedName.append("::");
169*9880d681SAndroid Build Coastguard Worker   }
170*9880d681SAndroid Build Coastguard Worker   FullyQualifiedName.append(TypeName);
171*9880d681SAndroid Build Coastguard Worker   return FullyQualifiedName;
172*9880d681SAndroid Build Coastguard Worker }
173*9880d681SAndroid Build Coastguard Worker 
getFullyQualifiedName(const DIScope * Scope,StringRef Name)174*9880d681SAndroid Build Coastguard Worker static std::string getFullyQualifiedName(const DIScope *Scope, StringRef Name) {
175*9880d681SAndroid Build Coastguard Worker   SmallVector<StringRef, 5> QualifiedNameComponents;
176*9880d681SAndroid Build Coastguard Worker   getQualifiedNameComponents(Scope, QualifiedNameComponents);
177*9880d681SAndroid Build Coastguard Worker   return getQualifiedName(QualifiedNameComponents, Name);
178*9880d681SAndroid Build Coastguard Worker }
179*9880d681SAndroid Build Coastguard Worker 
180*9880d681SAndroid Build Coastguard Worker struct CodeViewDebug::TypeLoweringScope {
TypeLoweringScopeCodeViewDebug::TypeLoweringScope181*9880d681SAndroid Build Coastguard Worker   TypeLoweringScope(CodeViewDebug &CVD) : CVD(CVD) { ++CVD.TypeEmissionLevel; }
~TypeLoweringScopeCodeViewDebug::TypeLoweringScope182*9880d681SAndroid Build Coastguard Worker   ~TypeLoweringScope() {
183*9880d681SAndroid Build Coastguard Worker     // Don't decrement TypeEmissionLevel until after emitting deferred types, so
184*9880d681SAndroid Build Coastguard Worker     // inner TypeLoweringScopes don't attempt to emit deferred types.
185*9880d681SAndroid Build Coastguard Worker     if (CVD.TypeEmissionLevel == 1)
186*9880d681SAndroid Build Coastguard Worker       CVD.emitDeferredCompleteTypes();
187*9880d681SAndroid Build Coastguard Worker     --CVD.TypeEmissionLevel;
188*9880d681SAndroid Build Coastguard Worker   }
189*9880d681SAndroid Build Coastguard Worker   CodeViewDebug &CVD;
190*9880d681SAndroid Build Coastguard Worker };
191*9880d681SAndroid Build Coastguard Worker 
getFullyQualifiedName(const DIScope * Ty)192*9880d681SAndroid Build Coastguard Worker static std::string getFullyQualifiedName(const DIScope *Ty) {
193*9880d681SAndroid Build Coastguard Worker   const DIScope *Scope = Ty->getScope().resolve();
194*9880d681SAndroid Build Coastguard Worker   return getFullyQualifiedName(Scope, getPrettyScopeName(Ty));
195*9880d681SAndroid Build Coastguard Worker }
196*9880d681SAndroid Build Coastguard Worker 
getScopeIndex(const DIScope * Scope)197*9880d681SAndroid Build Coastguard Worker TypeIndex CodeViewDebug::getScopeIndex(const DIScope *Scope) {
198*9880d681SAndroid Build Coastguard Worker   // No scope means global scope and that uses the zero index.
199*9880d681SAndroid Build Coastguard Worker   if (!Scope || isa<DIFile>(Scope))
200*9880d681SAndroid Build Coastguard Worker     return TypeIndex();
201*9880d681SAndroid Build Coastguard Worker 
202*9880d681SAndroid Build Coastguard Worker   assert(!isa<DIType>(Scope) && "shouldn't make a namespace scope for a type");
203*9880d681SAndroid Build Coastguard Worker 
204*9880d681SAndroid Build Coastguard Worker   // Check if we've already translated this scope.
205*9880d681SAndroid Build Coastguard Worker   auto I = TypeIndices.find({Scope, nullptr});
206*9880d681SAndroid Build Coastguard Worker   if (I != TypeIndices.end())
207*9880d681SAndroid Build Coastguard Worker     return I->second;
208*9880d681SAndroid Build Coastguard Worker 
209*9880d681SAndroid Build Coastguard Worker   // Build the fully qualified name of the scope.
210*9880d681SAndroid Build Coastguard Worker   std::string ScopeName = getFullyQualifiedName(Scope);
211*9880d681SAndroid Build Coastguard Worker   TypeIndex TI =
212*9880d681SAndroid Build Coastguard Worker       TypeTable.writeStringId(StringIdRecord(TypeIndex(), ScopeName));
213*9880d681SAndroid Build Coastguard Worker   return recordTypeIndexForDINode(Scope, TI);
214*9880d681SAndroid Build Coastguard Worker }
215*9880d681SAndroid Build Coastguard Worker 
getFuncIdForSubprogram(const DISubprogram * SP)216*9880d681SAndroid Build Coastguard Worker TypeIndex CodeViewDebug::getFuncIdForSubprogram(const DISubprogram *SP) {
217*9880d681SAndroid Build Coastguard Worker   // It's possible to ask for the FuncId of a function which doesn't have a
218*9880d681SAndroid Build Coastguard Worker   // subprogram: inlining a function with debug info into a function with none.
219*9880d681SAndroid Build Coastguard Worker   if (!SP)
220*9880d681SAndroid Build Coastguard Worker     return TypeIndex::None();
221*9880d681SAndroid Build Coastguard Worker 
222*9880d681SAndroid Build Coastguard Worker   // Check if we've already translated this subprogram.
223*9880d681SAndroid Build Coastguard Worker   auto I = TypeIndices.find({SP, nullptr});
224*9880d681SAndroid Build Coastguard Worker   if (I != TypeIndices.end())
225*9880d681SAndroid Build Coastguard Worker     return I->second;
226*9880d681SAndroid Build Coastguard Worker 
227*9880d681SAndroid Build Coastguard Worker   // The display name includes function template arguments. Drop them to match
228*9880d681SAndroid Build Coastguard Worker   // MSVC.
229*9880d681SAndroid Build Coastguard Worker   StringRef DisplayName = SP->getDisplayName().split('<').first;
230*9880d681SAndroid Build Coastguard Worker 
231*9880d681SAndroid Build Coastguard Worker   const DIScope *Scope = SP->getScope().resolve();
232*9880d681SAndroid Build Coastguard Worker   TypeIndex TI;
233*9880d681SAndroid Build Coastguard Worker   if (const auto *Class = dyn_cast_or_null<DICompositeType>(Scope)) {
234*9880d681SAndroid Build Coastguard Worker     // If the scope is a DICompositeType, then this must be a method. Member
235*9880d681SAndroid Build Coastguard Worker     // function types take some special handling, and require access to the
236*9880d681SAndroid Build Coastguard Worker     // subprogram.
237*9880d681SAndroid Build Coastguard Worker     TypeIndex ClassType = getTypeIndex(Class);
238*9880d681SAndroid Build Coastguard Worker     MemberFuncIdRecord MFuncId(ClassType, getMemberFunctionType(SP, Class),
239*9880d681SAndroid Build Coastguard Worker                                DisplayName);
240*9880d681SAndroid Build Coastguard Worker     TI = TypeTable.writeMemberFuncId(MFuncId);
241*9880d681SAndroid Build Coastguard Worker   } else {
242*9880d681SAndroid Build Coastguard Worker     // Otherwise, this must be a free function.
243*9880d681SAndroid Build Coastguard Worker     TypeIndex ParentScope = getScopeIndex(Scope);
244*9880d681SAndroid Build Coastguard Worker     FuncIdRecord FuncId(ParentScope, getTypeIndex(SP->getType()), DisplayName);
245*9880d681SAndroid Build Coastguard Worker     TI = TypeTable.writeFuncId(FuncId);
246*9880d681SAndroid Build Coastguard Worker   }
247*9880d681SAndroid Build Coastguard Worker 
248*9880d681SAndroid Build Coastguard Worker   return recordTypeIndexForDINode(SP, TI);
249*9880d681SAndroid Build Coastguard Worker }
250*9880d681SAndroid Build Coastguard Worker 
getMemberFunctionType(const DISubprogram * SP,const DICompositeType * Class)251*9880d681SAndroid Build Coastguard Worker TypeIndex CodeViewDebug::getMemberFunctionType(const DISubprogram *SP,
252*9880d681SAndroid Build Coastguard Worker                                                const DICompositeType *Class) {
253*9880d681SAndroid Build Coastguard Worker   // Always use the method declaration as the key for the function type. The
254*9880d681SAndroid Build Coastguard Worker   // method declaration contains the this adjustment.
255*9880d681SAndroid Build Coastguard Worker   if (SP->getDeclaration())
256*9880d681SAndroid Build Coastguard Worker     SP = SP->getDeclaration();
257*9880d681SAndroid Build Coastguard Worker   assert(!SP->getDeclaration() && "should use declaration as key");
258*9880d681SAndroid Build Coastguard Worker 
259*9880d681SAndroid Build Coastguard Worker   // Key the MemberFunctionRecord into the map as {SP, Class}. It won't collide
260*9880d681SAndroid Build Coastguard Worker   // with the MemberFuncIdRecord, which is keyed in as {SP, nullptr}.
261*9880d681SAndroid Build Coastguard Worker   auto I = TypeIndices.find({SP, Class});
262*9880d681SAndroid Build Coastguard Worker   if (I != TypeIndices.end())
263*9880d681SAndroid Build Coastguard Worker     return I->second;
264*9880d681SAndroid Build Coastguard Worker 
265*9880d681SAndroid Build Coastguard Worker   // Make sure complete type info for the class is emitted *after* the member
266*9880d681SAndroid Build Coastguard Worker   // function type, as the complete class type is likely to reference this
267*9880d681SAndroid Build Coastguard Worker   // member function type.
268*9880d681SAndroid Build Coastguard Worker   TypeLoweringScope S(*this);
269*9880d681SAndroid Build Coastguard Worker   TypeIndex TI =
270*9880d681SAndroid Build Coastguard Worker       lowerTypeMemberFunction(SP->getType(), Class, SP->getThisAdjustment());
271*9880d681SAndroid Build Coastguard Worker   return recordTypeIndexForDINode(SP, TI, Class);
272*9880d681SAndroid Build Coastguard Worker }
273*9880d681SAndroid Build Coastguard Worker 
recordTypeIndexForDINode(const DINode * Node,TypeIndex TI,const DIType * ClassTy)274*9880d681SAndroid Build Coastguard Worker TypeIndex CodeViewDebug::recordTypeIndexForDINode(const DINode *Node,
275*9880d681SAndroid Build Coastguard Worker                                                   TypeIndex TI,
276*9880d681SAndroid Build Coastguard Worker                                                   const DIType *ClassTy) {
277*9880d681SAndroid Build Coastguard Worker   auto InsertResult = TypeIndices.insert({{Node, ClassTy}, TI});
278*9880d681SAndroid Build Coastguard Worker   (void)InsertResult;
279*9880d681SAndroid Build Coastguard Worker   assert(InsertResult.second && "DINode was already assigned a type index");
280*9880d681SAndroid Build Coastguard Worker   return TI;
281*9880d681SAndroid Build Coastguard Worker }
282*9880d681SAndroid Build Coastguard Worker 
getPointerSizeInBytes()283*9880d681SAndroid Build Coastguard Worker unsigned CodeViewDebug::getPointerSizeInBytes() {
284*9880d681SAndroid Build Coastguard Worker   return MMI->getModule()->getDataLayout().getPointerSizeInBits() / 8;
285*9880d681SAndroid Build Coastguard Worker }
286*9880d681SAndroid Build Coastguard Worker 
recordLocalVariable(LocalVariable && Var,const DILocation * InlinedAt)287*9880d681SAndroid Build Coastguard Worker void CodeViewDebug::recordLocalVariable(LocalVariable &&Var,
288*9880d681SAndroid Build Coastguard Worker                                         const DILocation *InlinedAt) {
289*9880d681SAndroid Build Coastguard Worker   if (InlinedAt) {
290*9880d681SAndroid Build Coastguard Worker     // This variable was inlined. Associate it with the InlineSite.
291*9880d681SAndroid Build Coastguard Worker     const DISubprogram *Inlinee = Var.DIVar->getScope()->getSubprogram();
292*9880d681SAndroid Build Coastguard Worker     InlineSite &Site = getInlineSite(InlinedAt, Inlinee);
293*9880d681SAndroid Build Coastguard Worker     Site.InlinedLocals.emplace_back(Var);
294*9880d681SAndroid Build Coastguard Worker   } else {
295*9880d681SAndroid Build Coastguard Worker     // This variable goes in the main ProcSym.
296*9880d681SAndroid Build Coastguard Worker     CurFn->Locals.emplace_back(Var);
297*9880d681SAndroid Build Coastguard Worker   }
298*9880d681SAndroid Build Coastguard Worker }
299*9880d681SAndroid Build Coastguard Worker 
addLocIfNotPresent(SmallVectorImpl<const DILocation * > & Locs,const DILocation * Loc)300*9880d681SAndroid Build Coastguard Worker static void addLocIfNotPresent(SmallVectorImpl<const DILocation *> &Locs,
301*9880d681SAndroid Build Coastguard Worker                                const DILocation *Loc) {
302*9880d681SAndroid Build Coastguard Worker   auto B = Locs.begin(), E = Locs.end();
303*9880d681SAndroid Build Coastguard Worker   if (std::find(B, E, Loc) == E)
304*9880d681SAndroid Build Coastguard Worker     Locs.push_back(Loc);
305*9880d681SAndroid Build Coastguard Worker }
306*9880d681SAndroid Build Coastguard Worker 
maybeRecordLocation(const DebugLoc & DL,const MachineFunction * MF)307*9880d681SAndroid Build Coastguard Worker void CodeViewDebug::maybeRecordLocation(const DebugLoc &DL,
308*9880d681SAndroid Build Coastguard Worker                                         const MachineFunction *MF) {
309*9880d681SAndroid Build Coastguard Worker   // Skip this instruction if it has the same location as the previous one.
310*9880d681SAndroid Build Coastguard Worker   if (DL == CurFn->LastLoc)
311*9880d681SAndroid Build Coastguard Worker     return;
312*9880d681SAndroid Build Coastguard Worker 
313*9880d681SAndroid Build Coastguard Worker   const DIScope *Scope = DL.get()->getScope();
314*9880d681SAndroid Build Coastguard Worker   if (!Scope)
315*9880d681SAndroid Build Coastguard Worker     return;
316*9880d681SAndroid Build Coastguard Worker 
317*9880d681SAndroid Build Coastguard Worker   // Skip this line if it is longer than the maximum we can record.
318*9880d681SAndroid Build Coastguard Worker   LineInfo LI(DL.getLine(), DL.getLine(), /*IsStatement=*/true);
319*9880d681SAndroid Build Coastguard Worker   if (LI.getStartLine() != DL.getLine() || LI.isAlwaysStepInto() ||
320*9880d681SAndroid Build Coastguard Worker       LI.isNeverStepInto())
321*9880d681SAndroid Build Coastguard Worker     return;
322*9880d681SAndroid Build Coastguard Worker 
323*9880d681SAndroid Build Coastguard Worker   ColumnInfo CI(DL.getCol(), /*EndColumn=*/0);
324*9880d681SAndroid Build Coastguard Worker   if (CI.getStartColumn() != DL.getCol())
325*9880d681SAndroid Build Coastguard Worker     return;
326*9880d681SAndroid Build Coastguard Worker 
327*9880d681SAndroid Build Coastguard Worker   if (!CurFn->HaveLineInfo)
328*9880d681SAndroid Build Coastguard Worker     CurFn->HaveLineInfo = true;
329*9880d681SAndroid Build Coastguard Worker   unsigned FileId = 0;
330*9880d681SAndroid Build Coastguard Worker   if (CurFn->LastLoc.get() && CurFn->LastLoc->getFile() == DL->getFile())
331*9880d681SAndroid Build Coastguard Worker     FileId = CurFn->LastFileId;
332*9880d681SAndroid Build Coastguard Worker   else
333*9880d681SAndroid Build Coastguard Worker     FileId = CurFn->LastFileId = maybeRecordFile(DL->getFile());
334*9880d681SAndroid Build Coastguard Worker   CurFn->LastLoc = DL;
335*9880d681SAndroid Build Coastguard Worker 
336*9880d681SAndroid Build Coastguard Worker   unsigned FuncId = CurFn->FuncId;
337*9880d681SAndroid Build Coastguard Worker   if (const DILocation *SiteLoc = DL->getInlinedAt()) {
338*9880d681SAndroid Build Coastguard Worker     const DILocation *Loc = DL.get();
339*9880d681SAndroid Build Coastguard Worker 
340*9880d681SAndroid Build Coastguard Worker     // If this location was actually inlined from somewhere else, give it the ID
341*9880d681SAndroid Build Coastguard Worker     // of the inline call site.
342*9880d681SAndroid Build Coastguard Worker     FuncId =
343*9880d681SAndroid Build Coastguard Worker         getInlineSite(SiteLoc, Loc->getScope()->getSubprogram()).SiteFuncId;
344*9880d681SAndroid Build Coastguard Worker 
345*9880d681SAndroid Build Coastguard Worker     // Ensure we have links in the tree of inline call sites.
346*9880d681SAndroid Build Coastguard Worker     bool FirstLoc = true;
347*9880d681SAndroid Build Coastguard Worker     while ((SiteLoc = Loc->getInlinedAt())) {
348*9880d681SAndroid Build Coastguard Worker       InlineSite &Site =
349*9880d681SAndroid Build Coastguard Worker           getInlineSite(SiteLoc, Loc->getScope()->getSubprogram());
350*9880d681SAndroid Build Coastguard Worker       if (!FirstLoc)
351*9880d681SAndroid Build Coastguard Worker         addLocIfNotPresent(Site.ChildSites, Loc);
352*9880d681SAndroid Build Coastguard Worker       FirstLoc = false;
353*9880d681SAndroid Build Coastguard Worker       Loc = SiteLoc;
354*9880d681SAndroid Build Coastguard Worker     }
355*9880d681SAndroid Build Coastguard Worker     addLocIfNotPresent(CurFn->ChildSites, Loc);
356*9880d681SAndroid Build Coastguard Worker   }
357*9880d681SAndroid Build Coastguard Worker 
358*9880d681SAndroid Build Coastguard Worker   OS.EmitCVLocDirective(FuncId, FileId, DL.getLine(), DL.getCol(),
359*9880d681SAndroid Build Coastguard Worker                         /*PrologueEnd=*/false,
360*9880d681SAndroid Build Coastguard Worker                         /*IsStmt=*/false, DL->getFilename());
361*9880d681SAndroid Build Coastguard Worker }
362*9880d681SAndroid Build Coastguard Worker 
emitCodeViewMagicVersion()363*9880d681SAndroid Build Coastguard Worker void CodeViewDebug::emitCodeViewMagicVersion() {
364*9880d681SAndroid Build Coastguard Worker   OS.EmitValueToAlignment(4);
365*9880d681SAndroid Build Coastguard Worker   OS.AddComment("Debug section magic");
366*9880d681SAndroid Build Coastguard Worker   OS.EmitIntValue(COFF::DEBUG_SECTION_MAGIC, 4);
367*9880d681SAndroid Build Coastguard Worker }
368*9880d681SAndroid Build Coastguard Worker 
endModule()369*9880d681SAndroid Build Coastguard Worker void CodeViewDebug::endModule() {
370*9880d681SAndroid Build Coastguard Worker   if (!Asm || !MMI->hasDebugInfo())
371*9880d681SAndroid Build Coastguard Worker     return;
372*9880d681SAndroid Build Coastguard Worker 
373*9880d681SAndroid Build Coastguard Worker   assert(Asm != nullptr);
374*9880d681SAndroid Build Coastguard Worker 
375*9880d681SAndroid Build Coastguard Worker   // The COFF .debug$S section consists of several subsections, each starting
376*9880d681SAndroid Build Coastguard Worker   // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length
377*9880d681SAndroid Build Coastguard Worker   // of the payload followed by the payload itself.  The subsections are 4-byte
378*9880d681SAndroid Build Coastguard Worker   // aligned.
379*9880d681SAndroid Build Coastguard Worker 
380*9880d681SAndroid Build Coastguard Worker   // Use the generic .debug$S section, and make a subsection for all the inlined
381*9880d681SAndroid Build Coastguard Worker   // subprograms.
382*9880d681SAndroid Build Coastguard Worker   switchToDebugSectionForSymbol(nullptr);
383*9880d681SAndroid Build Coastguard Worker   emitInlineeLinesSubsection();
384*9880d681SAndroid Build Coastguard Worker 
385*9880d681SAndroid Build Coastguard Worker   // Emit per-function debug information.
386*9880d681SAndroid Build Coastguard Worker   for (auto &P : FnDebugInfo)
387*9880d681SAndroid Build Coastguard Worker     if (!P.first->isDeclarationForLinker())
388*9880d681SAndroid Build Coastguard Worker       emitDebugInfoForFunction(P.first, P.second);
389*9880d681SAndroid Build Coastguard Worker 
390*9880d681SAndroid Build Coastguard Worker   // Emit global variable debug information.
391*9880d681SAndroid Build Coastguard Worker   setCurrentSubprogram(nullptr);
392*9880d681SAndroid Build Coastguard Worker   emitDebugInfoForGlobals();
393*9880d681SAndroid Build Coastguard Worker 
394*9880d681SAndroid Build Coastguard Worker   // Emit retained types.
395*9880d681SAndroid Build Coastguard Worker   emitDebugInfoForRetainedTypes();
396*9880d681SAndroid Build Coastguard Worker 
397*9880d681SAndroid Build Coastguard Worker   // Switch back to the generic .debug$S section after potentially processing
398*9880d681SAndroid Build Coastguard Worker   // comdat symbol sections.
399*9880d681SAndroid Build Coastguard Worker   switchToDebugSectionForSymbol(nullptr);
400*9880d681SAndroid Build Coastguard Worker 
401*9880d681SAndroid Build Coastguard Worker   // Emit UDT records for any types used by global variables.
402*9880d681SAndroid Build Coastguard Worker   if (!GlobalUDTs.empty()) {
403*9880d681SAndroid Build Coastguard Worker     MCSymbol *SymbolsEnd = beginCVSubsection(ModuleSubstreamKind::Symbols);
404*9880d681SAndroid Build Coastguard Worker     emitDebugInfoForUDTs(GlobalUDTs);
405*9880d681SAndroid Build Coastguard Worker     endCVSubsection(SymbolsEnd);
406*9880d681SAndroid Build Coastguard Worker   }
407*9880d681SAndroid Build Coastguard Worker 
408*9880d681SAndroid Build Coastguard Worker   // This subsection holds a file index to offset in string table table.
409*9880d681SAndroid Build Coastguard Worker   OS.AddComment("File index to string table offset subsection");
410*9880d681SAndroid Build Coastguard Worker   OS.EmitCVFileChecksumsDirective();
411*9880d681SAndroid Build Coastguard Worker 
412*9880d681SAndroid Build Coastguard Worker   // This subsection holds the string table.
413*9880d681SAndroid Build Coastguard Worker   OS.AddComment("String table");
414*9880d681SAndroid Build Coastguard Worker   OS.EmitCVStringTableDirective();
415*9880d681SAndroid Build Coastguard Worker 
416*9880d681SAndroid Build Coastguard Worker   // Emit type information last, so that any types we translate while emitting
417*9880d681SAndroid Build Coastguard Worker   // function info are included.
418*9880d681SAndroid Build Coastguard Worker   emitTypeInformation();
419*9880d681SAndroid Build Coastguard Worker 
420*9880d681SAndroid Build Coastguard Worker   clear();
421*9880d681SAndroid Build Coastguard Worker }
422*9880d681SAndroid Build Coastguard Worker 
emitNullTerminatedSymbolName(MCStreamer & OS,StringRef S)423*9880d681SAndroid Build Coastguard Worker static void emitNullTerminatedSymbolName(MCStreamer &OS, StringRef S) {
424*9880d681SAndroid Build Coastguard Worker   // Microsoft's linker seems to have trouble with symbol names longer than
425*9880d681SAndroid Build Coastguard Worker   // 0xffd8 bytes.
426*9880d681SAndroid Build Coastguard Worker   S = S.substr(0, 0xffd8);
427*9880d681SAndroid Build Coastguard Worker   SmallString<32> NullTerminatedString(S);
428*9880d681SAndroid Build Coastguard Worker   NullTerminatedString.push_back('\0');
429*9880d681SAndroid Build Coastguard Worker   OS.EmitBytes(NullTerminatedString);
430*9880d681SAndroid Build Coastguard Worker }
431*9880d681SAndroid Build Coastguard Worker 
emitTypeInformation()432*9880d681SAndroid Build Coastguard Worker void CodeViewDebug::emitTypeInformation() {
433*9880d681SAndroid Build Coastguard Worker   // Do nothing if we have no debug info or if no non-trivial types were emitted
434*9880d681SAndroid Build Coastguard Worker   // to TypeTable during codegen.
435*9880d681SAndroid Build Coastguard Worker   NamedMDNode *CU_Nodes = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
436*9880d681SAndroid Build Coastguard Worker   if (!CU_Nodes)
437*9880d681SAndroid Build Coastguard Worker     return;
438*9880d681SAndroid Build Coastguard Worker   if (TypeTable.empty())
439*9880d681SAndroid Build Coastguard Worker     return;
440*9880d681SAndroid Build Coastguard Worker 
441*9880d681SAndroid Build Coastguard Worker   // Start the .debug$T section with 0x4.
442*9880d681SAndroid Build Coastguard Worker   OS.SwitchSection(Asm->getObjFileLowering().getCOFFDebugTypesSection());
443*9880d681SAndroid Build Coastguard Worker   emitCodeViewMagicVersion();
444*9880d681SAndroid Build Coastguard Worker 
445*9880d681SAndroid Build Coastguard Worker   SmallString<8> CommentPrefix;
446*9880d681SAndroid Build Coastguard Worker   if (OS.isVerboseAsm()) {
447*9880d681SAndroid Build Coastguard Worker     CommentPrefix += '\t';
448*9880d681SAndroid Build Coastguard Worker     CommentPrefix += Asm->MAI->getCommentString();
449*9880d681SAndroid Build Coastguard Worker     CommentPrefix += ' ';
450*9880d681SAndroid Build Coastguard Worker   }
451*9880d681SAndroid Build Coastguard Worker 
452*9880d681SAndroid Build Coastguard Worker   CVTypeDumper CVTD(nullptr, /*PrintRecordBytes=*/false);
453*9880d681SAndroid Build Coastguard Worker   TypeTable.ForEachRecord(
454*9880d681SAndroid Build Coastguard Worker       [&](TypeIndex Index, StringRef Record) {
455*9880d681SAndroid Build Coastguard Worker         if (OS.isVerboseAsm()) {
456*9880d681SAndroid Build Coastguard Worker           // Emit a block comment describing the type record for readability.
457*9880d681SAndroid Build Coastguard Worker           SmallString<512> CommentBlock;
458*9880d681SAndroid Build Coastguard Worker           raw_svector_ostream CommentOS(CommentBlock);
459*9880d681SAndroid Build Coastguard Worker           ScopedPrinter SP(CommentOS);
460*9880d681SAndroid Build Coastguard Worker           SP.setPrefix(CommentPrefix);
461*9880d681SAndroid Build Coastguard Worker           CVTD.setPrinter(&SP);
462*9880d681SAndroid Build Coastguard Worker           Error E = CVTD.dump({Record.bytes_begin(), Record.bytes_end()});
463*9880d681SAndroid Build Coastguard Worker           if (E) {
464*9880d681SAndroid Build Coastguard Worker             logAllUnhandledErrors(std::move(E), errs(), "error: ");
465*9880d681SAndroid Build Coastguard Worker             llvm_unreachable("produced malformed type record");
466*9880d681SAndroid Build Coastguard Worker           }
467*9880d681SAndroid Build Coastguard Worker           // emitRawComment will insert its own tab and comment string before
468*9880d681SAndroid Build Coastguard Worker           // the first line, so strip off our first one. It also prints its own
469*9880d681SAndroid Build Coastguard Worker           // newline.
470*9880d681SAndroid Build Coastguard Worker           OS.emitRawComment(
471*9880d681SAndroid Build Coastguard Worker               CommentOS.str().drop_front(CommentPrefix.size() - 1).rtrim());
472*9880d681SAndroid Build Coastguard Worker         } else {
473*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
474*9880d681SAndroid Build Coastguard Worker           // Assert that the type data is valid even if we aren't dumping
475*9880d681SAndroid Build Coastguard Worker           // comments. The MSVC linker doesn't do much type record validation,
476*9880d681SAndroid Build Coastguard Worker           // so the first link of an invalid type record can succeed while
477*9880d681SAndroid Build Coastguard Worker           // subsequent links will fail with LNK1285.
478*9880d681SAndroid Build Coastguard Worker           ByteStream<> Stream({Record.bytes_begin(), Record.bytes_end()});
479*9880d681SAndroid Build Coastguard Worker           CVTypeArray Types;
480*9880d681SAndroid Build Coastguard Worker           StreamReader Reader(Stream);
481*9880d681SAndroid Build Coastguard Worker           Error E = Reader.readArray(Types, Reader.getLength());
482*9880d681SAndroid Build Coastguard Worker           if (!E) {
483*9880d681SAndroid Build Coastguard Worker             TypeVisitorCallbacks C;
484*9880d681SAndroid Build Coastguard Worker             E = CVTypeVisitor(C).visitTypeStream(Types);
485*9880d681SAndroid Build Coastguard Worker           }
486*9880d681SAndroid Build Coastguard Worker           if (E) {
487*9880d681SAndroid Build Coastguard Worker             logAllUnhandledErrors(std::move(E), errs(), "error: ");
488*9880d681SAndroid Build Coastguard Worker             llvm_unreachable("produced malformed type record");
489*9880d681SAndroid Build Coastguard Worker           }
490*9880d681SAndroid Build Coastguard Worker #endif
491*9880d681SAndroid Build Coastguard Worker         }
492*9880d681SAndroid Build Coastguard Worker         OS.EmitBinaryData(Record);
493*9880d681SAndroid Build Coastguard Worker       });
494*9880d681SAndroid Build Coastguard Worker }
495*9880d681SAndroid Build Coastguard Worker 
emitInlineeLinesSubsection()496*9880d681SAndroid Build Coastguard Worker void CodeViewDebug::emitInlineeLinesSubsection() {
497*9880d681SAndroid Build Coastguard Worker   if (InlinedSubprograms.empty())
498*9880d681SAndroid Build Coastguard Worker     return;
499*9880d681SAndroid Build Coastguard Worker 
500*9880d681SAndroid Build Coastguard Worker   OS.AddComment("Inlinee lines subsection");
501*9880d681SAndroid Build Coastguard Worker   MCSymbol *InlineEnd = beginCVSubsection(ModuleSubstreamKind::InlineeLines);
502*9880d681SAndroid Build Coastguard Worker 
503*9880d681SAndroid Build Coastguard Worker   // We don't provide any extra file info.
504*9880d681SAndroid Build Coastguard Worker   // FIXME: Find out if debuggers use this info.
505*9880d681SAndroid Build Coastguard Worker   OS.AddComment("Inlinee lines signature");
506*9880d681SAndroid Build Coastguard Worker   OS.EmitIntValue(unsigned(InlineeLinesSignature::Normal), 4);
507*9880d681SAndroid Build Coastguard Worker 
508*9880d681SAndroid Build Coastguard Worker   for (const DISubprogram *SP : InlinedSubprograms) {
509*9880d681SAndroid Build Coastguard Worker     assert(TypeIndices.count({SP, nullptr}));
510*9880d681SAndroid Build Coastguard Worker     TypeIndex InlineeIdx = TypeIndices[{SP, nullptr}];
511*9880d681SAndroid Build Coastguard Worker 
512*9880d681SAndroid Build Coastguard Worker     OS.AddBlankLine();
513*9880d681SAndroid Build Coastguard Worker     unsigned FileId = maybeRecordFile(SP->getFile());
514*9880d681SAndroid Build Coastguard Worker     OS.AddComment("Inlined function " + SP->getDisplayName() + " starts at " +
515*9880d681SAndroid Build Coastguard Worker                   SP->getFilename() + Twine(':') + Twine(SP->getLine()));
516*9880d681SAndroid Build Coastguard Worker     OS.AddBlankLine();
517*9880d681SAndroid Build Coastguard Worker     // The filechecksum table uses 8 byte entries for now, and file ids start at
518*9880d681SAndroid Build Coastguard Worker     // 1.
519*9880d681SAndroid Build Coastguard Worker     unsigned FileOffset = (FileId - 1) * 8;
520*9880d681SAndroid Build Coastguard Worker     OS.AddComment("Type index of inlined function");
521*9880d681SAndroid Build Coastguard Worker     OS.EmitIntValue(InlineeIdx.getIndex(), 4);
522*9880d681SAndroid Build Coastguard Worker     OS.AddComment("Offset into filechecksum table");
523*9880d681SAndroid Build Coastguard Worker     OS.EmitIntValue(FileOffset, 4);
524*9880d681SAndroid Build Coastguard Worker     OS.AddComment("Starting line number");
525*9880d681SAndroid Build Coastguard Worker     OS.EmitIntValue(SP->getLine(), 4);
526*9880d681SAndroid Build Coastguard Worker   }
527*9880d681SAndroid Build Coastguard Worker 
528*9880d681SAndroid Build Coastguard Worker   endCVSubsection(InlineEnd);
529*9880d681SAndroid Build Coastguard Worker }
530*9880d681SAndroid Build Coastguard Worker 
collectInlineSiteChildren(SmallVectorImpl<unsigned> & Children,const FunctionInfo & FI,const InlineSite & Site)531*9880d681SAndroid Build Coastguard Worker void CodeViewDebug::collectInlineSiteChildren(
532*9880d681SAndroid Build Coastguard Worker     SmallVectorImpl<unsigned> &Children, const FunctionInfo &FI,
533*9880d681SAndroid Build Coastguard Worker     const InlineSite &Site) {
534*9880d681SAndroid Build Coastguard Worker   for (const DILocation *ChildSiteLoc : Site.ChildSites) {
535*9880d681SAndroid Build Coastguard Worker     auto I = FI.InlineSites.find(ChildSiteLoc);
536*9880d681SAndroid Build Coastguard Worker     const InlineSite &ChildSite = I->second;
537*9880d681SAndroid Build Coastguard Worker     Children.push_back(ChildSite.SiteFuncId);
538*9880d681SAndroid Build Coastguard Worker     collectInlineSiteChildren(Children, FI, ChildSite);
539*9880d681SAndroid Build Coastguard Worker   }
540*9880d681SAndroid Build Coastguard Worker }
541*9880d681SAndroid Build Coastguard Worker 
emitInlinedCallSite(const FunctionInfo & FI,const DILocation * InlinedAt,const InlineSite & Site)542*9880d681SAndroid Build Coastguard Worker void CodeViewDebug::emitInlinedCallSite(const FunctionInfo &FI,
543*9880d681SAndroid Build Coastguard Worker                                         const DILocation *InlinedAt,
544*9880d681SAndroid Build Coastguard Worker                                         const InlineSite &Site) {
545*9880d681SAndroid Build Coastguard Worker   MCSymbol *InlineBegin = MMI->getContext().createTempSymbol(),
546*9880d681SAndroid Build Coastguard Worker            *InlineEnd = MMI->getContext().createTempSymbol();
547*9880d681SAndroid Build Coastguard Worker 
548*9880d681SAndroid Build Coastguard Worker   assert(TypeIndices.count({Site.Inlinee, nullptr}));
549*9880d681SAndroid Build Coastguard Worker   TypeIndex InlineeIdx = TypeIndices[{Site.Inlinee, nullptr}];
550*9880d681SAndroid Build Coastguard Worker 
551*9880d681SAndroid Build Coastguard Worker   // SymbolRecord
552*9880d681SAndroid Build Coastguard Worker   OS.AddComment("Record length");
553*9880d681SAndroid Build Coastguard Worker   OS.emitAbsoluteSymbolDiff(InlineEnd, InlineBegin, 2);   // RecordLength
554*9880d681SAndroid Build Coastguard Worker   OS.EmitLabel(InlineBegin);
555*9880d681SAndroid Build Coastguard Worker   OS.AddComment("Record kind: S_INLINESITE");
556*9880d681SAndroid Build Coastguard Worker   OS.EmitIntValue(SymbolKind::S_INLINESITE, 2); // RecordKind
557*9880d681SAndroid Build Coastguard Worker 
558*9880d681SAndroid Build Coastguard Worker   OS.AddComment("PtrParent");
559*9880d681SAndroid Build Coastguard Worker   OS.EmitIntValue(0, 4);
560*9880d681SAndroid Build Coastguard Worker   OS.AddComment("PtrEnd");
561*9880d681SAndroid Build Coastguard Worker   OS.EmitIntValue(0, 4);
562*9880d681SAndroid Build Coastguard Worker   OS.AddComment("Inlinee type index");
563*9880d681SAndroid Build Coastguard Worker   OS.EmitIntValue(InlineeIdx.getIndex(), 4);
564*9880d681SAndroid Build Coastguard Worker 
565*9880d681SAndroid Build Coastguard Worker   unsigned FileId = maybeRecordFile(Site.Inlinee->getFile());
566*9880d681SAndroid Build Coastguard Worker   unsigned StartLineNum = Site.Inlinee->getLine();
567*9880d681SAndroid Build Coastguard Worker   SmallVector<unsigned, 3> SecondaryFuncIds;
568*9880d681SAndroid Build Coastguard Worker   collectInlineSiteChildren(SecondaryFuncIds, FI, Site);
569*9880d681SAndroid Build Coastguard Worker 
570*9880d681SAndroid Build Coastguard Worker   OS.EmitCVInlineLinetableDirective(Site.SiteFuncId, FileId, StartLineNum,
571*9880d681SAndroid Build Coastguard Worker                                     FI.Begin, FI.End, SecondaryFuncIds);
572*9880d681SAndroid Build Coastguard Worker 
573*9880d681SAndroid Build Coastguard Worker   OS.EmitLabel(InlineEnd);
574*9880d681SAndroid Build Coastguard Worker 
575*9880d681SAndroid Build Coastguard Worker   emitLocalVariableList(Site.InlinedLocals);
576*9880d681SAndroid Build Coastguard Worker 
577*9880d681SAndroid Build Coastguard Worker   // Recurse on child inlined call sites before closing the scope.
578*9880d681SAndroid Build Coastguard Worker   for (const DILocation *ChildSite : Site.ChildSites) {
579*9880d681SAndroid Build Coastguard Worker     auto I = FI.InlineSites.find(ChildSite);
580*9880d681SAndroid Build Coastguard Worker     assert(I != FI.InlineSites.end() &&
581*9880d681SAndroid Build Coastguard Worker            "child site not in function inline site map");
582*9880d681SAndroid Build Coastguard Worker     emitInlinedCallSite(FI, ChildSite, I->second);
583*9880d681SAndroid Build Coastguard Worker   }
584*9880d681SAndroid Build Coastguard Worker 
585*9880d681SAndroid Build Coastguard Worker   // Close the scope.
586*9880d681SAndroid Build Coastguard Worker   OS.AddComment("Record length");
587*9880d681SAndroid Build Coastguard Worker   OS.EmitIntValue(2, 2);                                  // RecordLength
588*9880d681SAndroid Build Coastguard Worker   OS.AddComment("Record kind: S_INLINESITE_END");
589*9880d681SAndroid Build Coastguard Worker   OS.EmitIntValue(SymbolKind::S_INLINESITE_END, 2); // RecordKind
590*9880d681SAndroid Build Coastguard Worker }
591*9880d681SAndroid Build Coastguard Worker 
switchToDebugSectionForSymbol(const MCSymbol * GVSym)592*9880d681SAndroid Build Coastguard Worker void CodeViewDebug::switchToDebugSectionForSymbol(const MCSymbol *GVSym) {
593*9880d681SAndroid Build Coastguard Worker   // If we have a symbol, it may be in a section that is COMDAT. If so, find the
594*9880d681SAndroid Build Coastguard Worker   // comdat key. A section may be comdat because of -ffunction-sections or
595*9880d681SAndroid Build Coastguard Worker   // because it is comdat in the IR.
596*9880d681SAndroid Build Coastguard Worker   MCSectionCOFF *GVSec =
597*9880d681SAndroid Build Coastguard Worker       GVSym ? dyn_cast<MCSectionCOFF>(&GVSym->getSection()) : nullptr;
598*9880d681SAndroid Build Coastguard Worker   const MCSymbol *KeySym = GVSec ? GVSec->getCOMDATSymbol() : nullptr;
599*9880d681SAndroid Build Coastguard Worker 
600*9880d681SAndroid Build Coastguard Worker   MCSectionCOFF *DebugSec = cast<MCSectionCOFF>(
601*9880d681SAndroid Build Coastguard Worker       Asm->getObjFileLowering().getCOFFDebugSymbolsSection());
602*9880d681SAndroid Build Coastguard Worker   DebugSec = OS.getContext().getAssociativeCOFFSection(DebugSec, KeySym);
603*9880d681SAndroid Build Coastguard Worker 
604*9880d681SAndroid Build Coastguard Worker   OS.SwitchSection(DebugSec);
605*9880d681SAndroid Build Coastguard Worker 
606*9880d681SAndroid Build Coastguard Worker   // Emit the magic version number if this is the first time we've switched to
607*9880d681SAndroid Build Coastguard Worker   // this section.
608*9880d681SAndroid Build Coastguard Worker   if (ComdatDebugSections.insert(DebugSec).second)
609*9880d681SAndroid Build Coastguard Worker     emitCodeViewMagicVersion();
610*9880d681SAndroid Build Coastguard Worker }
611*9880d681SAndroid Build Coastguard Worker 
emitDebugInfoForFunction(const Function * GV,FunctionInfo & FI)612*9880d681SAndroid Build Coastguard Worker void CodeViewDebug::emitDebugInfoForFunction(const Function *GV,
613*9880d681SAndroid Build Coastguard Worker                                              FunctionInfo &FI) {
614*9880d681SAndroid Build Coastguard Worker   // For each function there is a separate subsection
615*9880d681SAndroid Build Coastguard Worker   // which holds the PC to file:line table.
616*9880d681SAndroid Build Coastguard Worker   const MCSymbol *Fn = Asm->getSymbol(GV);
617*9880d681SAndroid Build Coastguard Worker   assert(Fn);
618*9880d681SAndroid Build Coastguard Worker 
619*9880d681SAndroid Build Coastguard Worker   // Switch to the to a comdat section, if appropriate.
620*9880d681SAndroid Build Coastguard Worker   switchToDebugSectionForSymbol(Fn);
621*9880d681SAndroid Build Coastguard Worker 
622*9880d681SAndroid Build Coastguard Worker   std::string FuncName;
623*9880d681SAndroid Build Coastguard Worker   auto *SP = GV->getSubprogram();
624*9880d681SAndroid Build Coastguard Worker   setCurrentSubprogram(SP);
625*9880d681SAndroid Build Coastguard Worker 
626*9880d681SAndroid Build Coastguard Worker   // If we have a display name, build the fully qualified name by walking the
627*9880d681SAndroid Build Coastguard Worker   // chain of scopes.
628*9880d681SAndroid Build Coastguard Worker   if (SP != nullptr && !SP->getDisplayName().empty())
629*9880d681SAndroid Build Coastguard Worker     FuncName =
630*9880d681SAndroid Build Coastguard Worker         getFullyQualifiedName(SP->getScope().resolve(), SP->getDisplayName());
631*9880d681SAndroid Build Coastguard Worker 
632*9880d681SAndroid Build Coastguard Worker   // If our DISubprogram name is empty, use the mangled name.
633*9880d681SAndroid Build Coastguard Worker   if (FuncName.empty())
634*9880d681SAndroid Build Coastguard Worker     FuncName = GlobalValue::getRealLinkageName(GV->getName());
635*9880d681SAndroid Build Coastguard Worker 
636*9880d681SAndroid Build Coastguard Worker   // Emit a symbol subsection, required by VS2012+ to find function boundaries.
637*9880d681SAndroid Build Coastguard Worker   OS.AddComment("Symbol subsection for " + Twine(FuncName));
638*9880d681SAndroid Build Coastguard Worker   MCSymbol *SymbolsEnd = beginCVSubsection(ModuleSubstreamKind::Symbols);
639*9880d681SAndroid Build Coastguard Worker   {
640*9880d681SAndroid Build Coastguard Worker     MCSymbol *ProcRecordBegin = MMI->getContext().createTempSymbol(),
641*9880d681SAndroid Build Coastguard Worker              *ProcRecordEnd = MMI->getContext().createTempSymbol();
642*9880d681SAndroid Build Coastguard Worker     OS.AddComment("Record length");
643*9880d681SAndroid Build Coastguard Worker     OS.emitAbsoluteSymbolDiff(ProcRecordEnd, ProcRecordBegin, 2);
644*9880d681SAndroid Build Coastguard Worker     OS.EmitLabel(ProcRecordBegin);
645*9880d681SAndroid Build Coastguard Worker 
646*9880d681SAndroid Build Coastguard Worker   if (GV->hasLocalLinkage()) {
647*9880d681SAndroid Build Coastguard Worker     OS.AddComment("Record kind: S_LPROC32_ID");
648*9880d681SAndroid Build Coastguard Worker     OS.EmitIntValue(unsigned(SymbolKind::S_LPROC32_ID), 2);
649*9880d681SAndroid Build Coastguard Worker   } else {
650*9880d681SAndroid Build Coastguard Worker     OS.AddComment("Record kind: S_GPROC32_ID");
651*9880d681SAndroid Build Coastguard Worker     OS.EmitIntValue(unsigned(SymbolKind::S_GPROC32_ID), 2);
652*9880d681SAndroid Build Coastguard Worker   }
653*9880d681SAndroid Build Coastguard Worker 
654*9880d681SAndroid Build Coastguard Worker     // These fields are filled in by tools like CVPACK which run after the fact.
655*9880d681SAndroid Build Coastguard Worker     OS.AddComment("PtrParent");
656*9880d681SAndroid Build Coastguard Worker     OS.EmitIntValue(0, 4);
657*9880d681SAndroid Build Coastguard Worker     OS.AddComment("PtrEnd");
658*9880d681SAndroid Build Coastguard Worker     OS.EmitIntValue(0, 4);
659*9880d681SAndroid Build Coastguard Worker     OS.AddComment("PtrNext");
660*9880d681SAndroid Build Coastguard Worker     OS.EmitIntValue(0, 4);
661*9880d681SAndroid Build Coastguard Worker     // This is the important bit that tells the debugger where the function
662*9880d681SAndroid Build Coastguard Worker     // code is located and what's its size:
663*9880d681SAndroid Build Coastguard Worker     OS.AddComment("Code size");
664*9880d681SAndroid Build Coastguard Worker     OS.emitAbsoluteSymbolDiff(FI.End, Fn, 4);
665*9880d681SAndroid Build Coastguard Worker     OS.AddComment("Offset after prologue");
666*9880d681SAndroid Build Coastguard Worker     OS.EmitIntValue(0, 4);
667*9880d681SAndroid Build Coastguard Worker     OS.AddComment("Offset before epilogue");
668*9880d681SAndroid Build Coastguard Worker     OS.EmitIntValue(0, 4);
669*9880d681SAndroid Build Coastguard Worker     OS.AddComment("Function type index");
670*9880d681SAndroid Build Coastguard Worker     OS.EmitIntValue(getFuncIdForSubprogram(GV->getSubprogram()).getIndex(), 4);
671*9880d681SAndroid Build Coastguard Worker     OS.AddComment("Function section relative address");
672*9880d681SAndroid Build Coastguard Worker     OS.EmitCOFFSecRel32(Fn);
673*9880d681SAndroid Build Coastguard Worker     OS.AddComment("Function section index");
674*9880d681SAndroid Build Coastguard Worker     OS.EmitCOFFSectionIndex(Fn);
675*9880d681SAndroid Build Coastguard Worker     OS.AddComment("Flags");
676*9880d681SAndroid Build Coastguard Worker     OS.EmitIntValue(0, 1);
677*9880d681SAndroid Build Coastguard Worker     // Emit the function display name as a null-terminated string.
678*9880d681SAndroid Build Coastguard Worker     OS.AddComment("Function name");
679*9880d681SAndroid Build Coastguard Worker     // Truncate the name so we won't overflow the record length field.
680*9880d681SAndroid Build Coastguard Worker     emitNullTerminatedSymbolName(OS, FuncName);
681*9880d681SAndroid Build Coastguard Worker     OS.EmitLabel(ProcRecordEnd);
682*9880d681SAndroid Build Coastguard Worker 
683*9880d681SAndroid Build Coastguard Worker     emitLocalVariableList(FI.Locals);
684*9880d681SAndroid Build Coastguard Worker 
685*9880d681SAndroid Build Coastguard Worker     // Emit inlined call site information. Only emit functions inlined directly
686*9880d681SAndroid Build Coastguard Worker     // into the parent function. We'll emit the other sites recursively as part
687*9880d681SAndroid Build Coastguard Worker     // of their parent inline site.
688*9880d681SAndroid Build Coastguard Worker     for (const DILocation *InlinedAt : FI.ChildSites) {
689*9880d681SAndroid Build Coastguard Worker       auto I = FI.InlineSites.find(InlinedAt);
690*9880d681SAndroid Build Coastguard Worker       assert(I != FI.InlineSites.end() &&
691*9880d681SAndroid Build Coastguard Worker              "child site not in function inline site map");
692*9880d681SAndroid Build Coastguard Worker       emitInlinedCallSite(FI, InlinedAt, I->second);
693*9880d681SAndroid Build Coastguard Worker     }
694*9880d681SAndroid Build Coastguard Worker 
695*9880d681SAndroid Build Coastguard Worker     if (SP != nullptr)
696*9880d681SAndroid Build Coastguard Worker       emitDebugInfoForUDTs(LocalUDTs);
697*9880d681SAndroid Build Coastguard Worker 
698*9880d681SAndroid Build Coastguard Worker     // We're done with this function.
699*9880d681SAndroid Build Coastguard Worker     OS.AddComment("Record length");
700*9880d681SAndroid Build Coastguard Worker     OS.EmitIntValue(0x0002, 2);
701*9880d681SAndroid Build Coastguard Worker     OS.AddComment("Record kind: S_PROC_ID_END");
702*9880d681SAndroid Build Coastguard Worker     OS.EmitIntValue(unsigned(SymbolKind::S_PROC_ID_END), 2);
703*9880d681SAndroid Build Coastguard Worker   }
704*9880d681SAndroid Build Coastguard Worker   endCVSubsection(SymbolsEnd);
705*9880d681SAndroid Build Coastguard Worker 
706*9880d681SAndroid Build Coastguard Worker   // We have an assembler directive that takes care of the whole line table.
707*9880d681SAndroid Build Coastguard Worker   OS.EmitCVLinetableDirective(FI.FuncId, Fn, FI.End);
708*9880d681SAndroid Build Coastguard Worker }
709*9880d681SAndroid Build Coastguard Worker 
710*9880d681SAndroid Build Coastguard Worker CodeViewDebug::LocalVarDefRange
createDefRangeMem(uint16_t CVRegister,int Offset)711*9880d681SAndroid Build Coastguard Worker CodeViewDebug::createDefRangeMem(uint16_t CVRegister, int Offset) {
712*9880d681SAndroid Build Coastguard Worker   LocalVarDefRange DR;
713*9880d681SAndroid Build Coastguard Worker   DR.InMemory = -1;
714*9880d681SAndroid Build Coastguard Worker   DR.DataOffset = Offset;
715*9880d681SAndroid Build Coastguard Worker   assert(DR.DataOffset == Offset && "truncation");
716*9880d681SAndroid Build Coastguard Worker   DR.StructOffset = 0;
717*9880d681SAndroid Build Coastguard Worker   DR.CVRegister = CVRegister;
718*9880d681SAndroid Build Coastguard Worker   return DR;
719*9880d681SAndroid Build Coastguard Worker }
720*9880d681SAndroid Build Coastguard Worker 
721*9880d681SAndroid Build Coastguard Worker CodeViewDebug::LocalVarDefRange
createDefRangeReg(uint16_t CVRegister)722*9880d681SAndroid Build Coastguard Worker CodeViewDebug::createDefRangeReg(uint16_t CVRegister) {
723*9880d681SAndroid Build Coastguard Worker   LocalVarDefRange DR;
724*9880d681SAndroid Build Coastguard Worker   DR.InMemory = 0;
725*9880d681SAndroid Build Coastguard Worker   DR.DataOffset = 0;
726*9880d681SAndroid Build Coastguard Worker   DR.StructOffset = 0;
727*9880d681SAndroid Build Coastguard Worker   DR.CVRegister = CVRegister;
728*9880d681SAndroid Build Coastguard Worker   return DR;
729*9880d681SAndroid Build Coastguard Worker }
730*9880d681SAndroid Build Coastguard Worker 
collectVariableInfoFromMMITable(DenseSet<InlinedVariable> & Processed)731*9880d681SAndroid Build Coastguard Worker void CodeViewDebug::collectVariableInfoFromMMITable(
732*9880d681SAndroid Build Coastguard Worker     DenseSet<InlinedVariable> &Processed) {
733*9880d681SAndroid Build Coastguard Worker   const TargetSubtargetInfo &TSI = Asm->MF->getSubtarget();
734*9880d681SAndroid Build Coastguard Worker   const TargetFrameLowering *TFI = TSI.getFrameLowering();
735*9880d681SAndroid Build Coastguard Worker   const TargetRegisterInfo *TRI = TSI.getRegisterInfo();
736*9880d681SAndroid Build Coastguard Worker 
737*9880d681SAndroid Build Coastguard Worker   for (const MachineModuleInfo::VariableDbgInfo &VI :
738*9880d681SAndroid Build Coastguard Worker        MMI->getVariableDbgInfo()) {
739*9880d681SAndroid Build Coastguard Worker     if (!VI.Var)
740*9880d681SAndroid Build Coastguard Worker       continue;
741*9880d681SAndroid Build Coastguard Worker     assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) &&
742*9880d681SAndroid Build Coastguard Worker            "Expected inlined-at fields to agree");
743*9880d681SAndroid Build Coastguard Worker 
744*9880d681SAndroid Build Coastguard Worker     Processed.insert(InlinedVariable(VI.Var, VI.Loc->getInlinedAt()));
745*9880d681SAndroid Build Coastguard Worker     LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc);
746*9880d681SAndroid Build Coastguard Worker 
747*9880d681SAndroid Build Coastguard Worker     // If variable scope is not found then skip this variable.
748*9880d681SAndroid Build Coastguard Worker     if (!Scope)
749*9880d681SAndroid Build Coastguard Worker       continue;
750*9880d681SAndroid Build Coastguard Worker 
751*9880d681SAndroid Build Coastguard Worker     // Get the frame register used and the offset.
752*9880d681SAndroid Build Coastguard Worker     unsigned FrameReg = 0;
753*9880d681SAndroid Build Coastguard Worker     int FrameOffset = TFI->getFrameIndexReference(*Asm->MF, VI.Slot, FrameReg);
754*9880d681SAndroid Build Coastguard Worker     uint16_t CVReg = TRI->getCodeViewRegNum(FrameReg);
755*9880d681SAndroid Build Coastguard Worker 
756*9880d681SAndroid Build Coastguard Worker     // Calculate the label ranges.
757*9880d681SAndroid Build Coastguard Worker     LocalVarDefRange DefRange = createDefRangeMem(CVReg, FrameOffset);
758*9880d681SAndroid Build Coastguard Worker     for (const InsnRange &Range : Scope->getRanges()) {
759*9880d681SAndroid Build Coastguard Worker       const MCSymbol *Begin = getLabelBeforeInsn(Range.first);
760*9880d681SAndroid Build Coastguard Worker       const MCSymbol *End = getLabelAfterInsn(Range.second);
761*9880d681SAndroid Build Coastguard Worker       End = End ? End : Asm->getFunctionEnd();
762*9880d681SAndroid Build Coastguard Worker       DefRange.Ranges.emplace_back(Begin, End);
763*9880d681SAndroid Build Coastguard Worker     }
764*9880d681SAndroid Build Coastguard Worker 
765*9880d681SAndroid Build Coastguard Worker     LocalVariable Var;
766*9880d681SAndroid Build Coastguard Worker     Var.DIVar = VI.Var;
767*9880d681SAndroid Build Coastguard Worker     Var.DefRanges.emplace_back(std::move(DefRange));
768*9880d681SAndroid Build Coastguard Worker     recordLocalVariable(std::move(Var), VI.Loc->getInlinedAt());
769*9880d681SAndroid Build Coastguard Worker   }
770*9880d681SAndroid Build Coastguard Worker }
771*9880d681SAndroid Build Coastguard Worker 
collectVariableInfo(const DISubprogram * SP)772*9880d681SAndroid Build Coastguard Worker void CodeViewDebug::collectVariableInfo(const DISubprogram *SP) {
773*9880d681SAndroid Build Coastguard Worker   DenseSet<InlinedVariable> Processed;
774*9880d681SAndroid Build Coastguard Worker   // Grab the variable info that was squirreled away in the MMI side-table.
775*9880d681SAndroid Build Coastguard Worker   collectVariableInfoFromMMITable(Processed);
776*9880d681SAndroid Build Coastguard Worker 
777*9880d681SAndroid Build Coastguard Worker   const TargetRegisterInfo *TRI = Asm->MF->getSubtarget().getRegisterInfo();
778*9880d681SAndroid Build Coastguard Worker 
779*9880d681SAndroid Build Coastguard Worker   for (const auto &I : DbgValues) {
780*9880d681SAndroid Build Coastguard Worker     InlinedVariable IV = I.first;
781*9880d681SAndroid Build Coastguard Worker     if (Processed.count(IV))
782*9880d681SAndroid Build Coastguard Worker       continue;
783*9880d681SAndroid Build Coastguard Worker     const DILocalVariable *DIVar = IV.first;
784*9880d681SAndroid Build Coastguard Worker     const DILocation *InlinedAt = IV.second;
785*9880d681SAndroid Build Coastguard Worker 
786*9880d681SAndroid Build Coastguard Worker     // Instruction ranges, specifying where IV is accessible.
787*9880d681SAndroid Build Coastguard Worker     const auto &Ranges = I.second;
788*9880d681SAndroid Build Coastguard Worker 
789*9880d681SAndroid Build Coastguard Worker     LexicalScope *Scope = nullptr;
790*9880d681SAndroid Build Coastguard Worker     if (InlinedAt)
791*9880d681SAndroid Build Coastguard Worker       Scope = LScopes.findInlinedScope(DIVar->getScope(), InlinedAt);
792*9880d681SAndroid Build Coastguard Worker     else
793*9880d681SAndroid Build Coastguard Worker       Scope = LScopes.findLexicalScope(DIVar->getScope());
794*9880d681SAndroid Build Coastguard Worker     // If variable scope is not found then skip this variable.
795*9880d681SAndroid Build Coastguard Worker     if (!Scope)
796*9880d681SAndroid Build Coastguard Worker       continue;
797*9880d681SAndroid Build Coastguard Worker 
798*9880d681SAndroid Build Coastguard Worker     LocalVariable Var;
799*9880d681SAndroid Build Coastguard Worker     Var.DIVar = DIVar;
800*9880d681SAndroid Build Coastguard Worker 
801*9880d681SAndroid Build Coastguard Worker     // Calculate the definition ranges.
802*9880d681SAndroid Build Coastguard Worker     for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) {
803*9880d681SAndroid Build Coastguard Worker       const InsnRange &Range = *I;
804*9880d681SAndroid Build Coastguard Worker       const MachineInstr *DVInst = Range.first;
805*9880d681SAndroid Build Coastguard Worker       assert(DVInst->isDebugValue() && "Invalid History entry");
806*9880d681SAndroid Build Coastguard Worker       const DIExpression *DIExpr = DVInst->getDebugExpression();
807*9880d681SAndroid Build Coastguard Worker 
808*9880d681SAndroid Build Coastguard Worker       // Bail if there is a complex DWARF expression for now.
809*9880d681SAndroid Build Coastguard Worker       if (DIExpr && DIExpr->getNumElements() > 0)
810*9880d681SAndroid Build Coastguard Worker         continue;
811*9880d681SAndroid Build Coastguard Worker 
812*9880d681SAndroid Build Coastguard Worker       // Bail if operand 0 is not a valid register. This means the variable is a
813*9880d681SAndroid Build Coastguard Worker       // simple constant, or is described by a complex expression.
814*9880d681SAndroid Build Coastguard Worker       // FIXME: Find a way to represent constant variables, since they are
815*9880d681SAndroid Build Coastguard Worker       // relatively common.
816*9880d681SAndroid Build Coastguard Worker       unsigned Reg =
817*9880d681SAndroid Build Coastguard Worker           DVInst->getOperand(0).isReg() ? DVInst->getOperand(0).getReg() : 0;
818*9880d681SAndroid Build Coastguard Worker       if (Reg == 0)
819*9880d681SAndroid Build Coastguard Worker         continue;
820*9880d681SAndroid Build Coastguard Worker 
821*9880d681SAndroid Build Coastguard Worker       // Handle the two cases we can handle: indirect in memory and in register.
822*9880d681SAndroid Build Coastguard Worker       bool IsIndirect = DVInst->getOperand(1).isImm();
823*9880d681SAndroid Build Coastguard Worker       unsigned CVReg = TRI->getCodeViewRegNum(DVInst->getOperand(0).getReg());
824*9880d681SAndroid Build Coastguard Worker       {
825*9880d681SAndroid Build Coastguard Worker         LocalVarDefRange DefRange;
826*9880d681SAndroid Build Coastguard Worker         if (IsIndirect) {
827*9880d681SAndroid Build Coastguard Worker           int64_t Offset = DVInst->getOperand(1).getImm();
828*9880d681SAndroid Build Coastguard Worker           DefRange = createDefRangeMem(CVReg, Offset);
829*9880d681SAndroid Build Coastguard Worker         } else {
830*9880d681SAndroid Build Coastguard Worker           DefRange = createDefRangeReg(CVReg);
831*9880d681SAndroid Build Coastguard Worker         }
832*9880d681SAndroid Build Coastguard Worker         if (Var.DefRanges.empty() ||
833*9880d681SAndroid Build Coastguard Worker             Var.DefRanges.back().isDifferentLocation(DefRange)) {
834*9880d681SAndroid Build Coastguard Worker           Var.DefRanges.emplace_back(std::move(DefRange));
835*9880d681SAndroid Build Coastguard Worker         }
836*9880d681SAndroid Build Coastguard Worker       }
837*9880d681SAndroid Build Coastguard Worker 
838*9880d681SAndroid Build Coastguard Worker       // Compute the label range.
839*9880d681SAndroid Build Coastguard Worker       const MCSymbol *Begin = getLabelBeforeInsn(Range.first);
840*9880d681SAndroid Build Coastguard Worker       const MCSymbol *End = getLabelAfterInsn(Range.second);
841*9880d681SAndroid Build Coastguard Worker       if (!End) {
842*9880d681SAndroid Build Coastguard Worker         if (std::next(I) != E)
843*9880d681SAndroid Build Coastguard Worker           End = getLabelBeforeInsn(std::next(I)->first);
844*9880d681SAndroid Build Coastguard Worker         else
845*9880d681SAndroid Build Coastguard Worker           End = Asm->getFunctionEnd();
846*9880d681SAndroid Build Coastguard Worker       }
847*9880d681SAndroid Build Coastguard Worker 
848*9880d681SAndroid Build Coastguard Worker       // If the last range end is our begin, just extend the last range.
849*9880d681SAndroid Build Coastguard Worker       // Otherwise make a new range.
850*9880d681SAndroid Build Coastguard Worker       SmallVectorImpl<std::pair<const MCSymbol *, const MCSymbol *>> &Ranges =
851*9880d681SAndroid Build Coastguard Worker           Var.DefRanges.back().Ranges;
852*9880d681SAndroid Build Coastguard Worker       if (!Ranges.empty() && Ranges.back().second == Begin)
853*9880d681SAndroid Build Coastguard Worker         Ranges.back().second = End;
854*9880d681SAndroid Build Coastguard Worker       else
855*9880d681SAndroid Build Coastguard Worker         Ranges.emplace_back(Begin, End);
856*9880d681SAndroid Build Coastguard Worker 
857*9880d681SAndroid Build Coastguard Worker       // FIXME: Do more range combining.
858*9880d681SAndroid Build Coastguard Worker     }
859*9880d681SAndroid Build Coastguard Worker 
860*9880d681SAndroid Build Coastguard Worker     recordLocalVariable(std::move(Var), InlinedAt);
861*9880d681SAndroid Build Coastguard Worker   }
862*9880d681SAndroid Build Coastguard Worker }
863*9880d681SAndroid Build Coastguard Worker 
beginFunction(const MachineFunction * MF)864*9880d681SAndroid Build Coastguard Worker void CodeViewDebug::beginFunction(const MachineFunction *MF) {
865*9880d681SAndroid Build Coastguard Worker   assert(!CurFn && "Can't process two functions at once!");
866*9880d681SAndroid Build Coastguard Worker 
867*9880d681SAndroid Build Coastguard Worker   if (!Asm || !MMI->hasDebugInfo())
868*9880d681SAndroid Build Coastguard Worker     return;
869*9880d681SAndroid Build Coastguard Worker 
870*9880d681SAndroid Build Coastguard Worker   DebugHandlerBase::beginFunction(MF);
871*9880d681SAndroid Build Coastguard Worker 
872*9880d681SAndroid Build Coastguard Worker   const Function *GV = MF->getFunction();
873*9880d681SAndroid Build Coastguard Worker   assert(FnDebugInfo.count(GV) == false);
874*9880d681SAndroid Build Coastguard Worker   CurFn = &FnDebugInfo[GV];
875*9880d681SAndroid Build Coastguard Worker   CurFn->FuncId = NextFuncId++;
876*9880d681SAndroid Build Coastguard Worker   CurFn->Begin = Asm->getFunctionBegin();
877*9880d681SAndroid Build Coastguard Worker 
878*9880d681SAndroid Build Coastguard Worker   // Find the end of the function prolog.  First known non-DBG_VALUE and
879*9880d681SAndroid Build Coastguard Worker   // non-frame setup location marks the beginning of the function body.
880*9880d681SAndroid Build Coastguard Worker   // FIXME: is there a simpler a way to do this? Can we just search
881*9880d681SAndroid Build Coastguard Worker   // for the first instruction of the function, not the last of the prolog?
882*9880d681SAndroid Build Coastguard Worker   DebugLoc PrologEndLoc;
883*9880d681SAndroid Build Coastguard Worker   bool EmptyPrologue = true;
884*9880d681SAndroid Build Coastguard Worker   for (const auto &MBB : *MF) {
885*9880d681SAndroid Build Coastguard Worker     for (const auto &MI : MBB) {
886*9880d681SAndroid Build Coastguard Worker       if (!MI.isDebugValue() && !MI.getFlag(MachineInstr::FrameSetup) &&
887*9880d681SAndroid Build Coastguard Worker           MI.getDebugLoc()) {
888*9880d681SAndroid Build Coastguard Worker         PrologEndLoc = MI.getDebugLoc();
889*9880d681SAndroid Build Coastguard Worker         break;
890*9880d681SAndroid Build Coastguard Worker       } else if (!MI.isDebugValue()) {
891*9880d681SAndroid Build Coastguard Worker         EmptyPrologue = false;
892*9880d681SAndroid Build Coastguard Worker       }
893*9880d681SAndroid Build Coastguard Worker     }
894*9880d681SAndroid Build Coastguard Worker   }
895*9880d681SAndroid Build Coastguard Worker 
896*9880d681SAndroid Build Coastguard Worker   // Record beginning of function if we have a non-empty prologue.
897*9880d681SAndroid Build Coastguard Worker   if (PrologEndLoc && !EmptyPrologue) {
898*9880d681SAndroid Build Coastguard Worker     DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc();
899*9880d681SAndroid Build Coastguard Worker     maybeRecordLocation(FnStartDL, MF);
900*9880d681SAndroid Build Coastguard Worker   }
901*9880d681SAndroid Build Coastguard Worker }
902*9880d681SAndroid Build Coastguard Worker 
addToUDTs(const DIType * Ty,TypeIndex TI)903*9880d681SAndroid Build Coastguard Worker void CodeViewDebug::addToUDTs(const DIType *Ty, TypeIndex TI) {
904*9880d681SAndroid Build Coastguard Worker   // Don't record empty UDTs.
905*9880d681SAndroid Build Coastguard Worker   if (Ty->getName().empty())
906*9880d681SAndroid Build Coastguard Worker     return;
907*9880d681SAndroid Build Coastguard Worker 
908*9880d681SAndroid Build Coastguard Worker   SmallVector<StringRef, 5> QualifiedNameComponents;
909*9880d681SAndroid Build Coastguard Worker   const DISubprogram *ClosestSubprogram = getQualifiedNameComponents(
910*9880d681SAndroid Build Coastguard Worker       Ty->getScope().resolve(), QualifiedNameComponents);
911*9880d681SAndroid Build Coastguard Worker 
912*9880d681SAndroid Build Coastguard Worker   std::string FullyQualifiedName =
913*9880d681SAndroid Build Coastguard Worker       getQualifiedName(QualifiedNameComponents, getPrettyScopeName(Ty));
914*9880d681SAndroid Build Coastguard Worker 
915*9880d681SAndroid Build Coastguard Worker   if (ClosestSubprogram == nullptr)
916*9880d681SAndroid Build Coastguard Worker     GlobalUDTs.emplace_back(std::move(FullyQualifiedName), TI);
917*9880d681SAndroid Build Coastguard Worker   else if (ClosestSubprogram == CurrentSubprogram)
918*9880d681SAndroid Build Coastguard Worker     LocalUDTs.emplace_back(std::move(FullyQualifiedName), TI);
919*9880d681SAndroid Build Coastguard Worker 
920*9880d681SAndroid Build Coastguard Worker   // TODO: What if the ClosestSubprogram is neither null or the current
921*9880d681SAndroid Build Coastguard Worker   // subprogram?  Currently, the UDT just gets dropped on the floor.
922*9880d681SAndroid Build Coastguard Worker   //
923*9880d681SAndroid Build Coastguard Worker   // The current behavior is not desirable.  To get maximal fidelity, we would
924*9880d681SAndroid Build Coastguard Worker   // need to perform all type translation before beginning emission of .debug$S
925*9880d681SAndroid Build Coastguard Worker   // and then make LocalUDTs a member of FunctionInfo
926*9880d681SAndroid Build Coastguard Worker }
927*9880d681SAndroid Build Coastguard Worker 
lowerType(const DIType * Ty,const DIType * ClassTy)928*9880d681SAndroid Build Coastguard Worker TypeIndex CodeViewDebug::lowerType(const DIType *Ty, const DIType *ClassTy) {
929*9880d681SAndroid Build Coastguard Worker   // Generic dispatch for lowering an unknown type.
930*9880d681SAndroid Build Coastguard Worker   switch (Ty->getTag()) {
931*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_TAG_array_type:
932*9880d681SAndroid Build Coastguard Worker     return lowerTypeArray(cast<DICompositeType>(Ty));
933*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_TAG_typedef:
934*9880d681SAndroid Build Coastguard Worker     return lowerTypeAlias(cast<DIDerivedType>(Ty));
935*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_TAG_base_type:
936*9880d681SAndroid Build Coastguard Worker     return lowerTypeBasic(cast<DIBasicType>(Ty));
937*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_TAG_pointer_type:
938*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_TAG_reference_type:
939*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_TAG_rvalue_reference_type:
940*9880d681SAndroid Build Coastguard Worker     return lowerTypePointer(cast<DIDerivedType>(Ty));
941*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_TAG_ptr_to_member_type:
942*9880d681SAndroid Build Coastguard Worker     return lowerTypeMemberPointer(cast<DIDerivedType>(Ty));
943*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_TAG_const_type:
944*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_TAG_volatile_type:
945*9880d681SAndroid Build Coastguard Worker     return lowerTypeModifier(cast<DIDerivedType>(Ty));
946*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_TAG_subroutine_type:
947*9880d681SAndroid Build Coastguard Worker     if (ClassTy) {
948*9880d681SAndroid Build Coastguard Worker       // The member function type of a member function pointer has no
949*9880d681SAndroid Build Coastguard Worker       // ThisAdjustment.
950*9880d681SAndroid Build Coastguard Worker       return lowerTypeMemberFunction(cast<DISubroutineType>(Ty), ClassTy,
951*9880d681SAndroid Build Coastguard Worker                                      /*ThisAdjustment=*/0);
952*9880d681SAndroid Build Coastguard Worker     }
953*9880d681SAndroid Build Coastguard Worker     return lowerTypeFunction(cast<DISubroutineType>(Ty));
954*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_TAG_enumeration_type:
955*9880d681SAndroid Build Coastguard Worker     return lowerTypeEnum(cast<DICompositeType>(Ty));
956*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_TAG_class_type:
957*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_TAG_structure_type:
958*9880d681SAndroid Build Coastguard Worker     return lowerTypeClass(cast<DICompositeType>(Ty));
959*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_TAG_union_type:
960*9880d681SAndroid Build Coastguard Worker     return lowerTypeUnion(cast<DICompositeType>(Ty));
961*9880d681SAndroid Build Coastguard Worker   default:
962*9880d681SAndroid Build Coastguard Worker     // Use the null type index.
963*9880d681SAndroid Build Coastguard Worker     return TypeIndex();
964*9880d681SAndroid Build Coastguard Worker   }
965*9880d681SAndroid Build Coastguard Worker }
966*9880d681SAndroid Build Coastguard Worker 
lowerTypeAlias(const DIDerivedType * Ty)967*9880d681SAndroid Build Coastguard Worker TypeIndex CodeViewDebug::lowerTypeAlias(const DIDerivedType *Ty) {
968*9880d681SAndroid Build Coastguard Worker   DITypeRef UnderlyingTypeRef = Ty->getBaseType();
969*9880d681SAndroid Build Coastguard Worker   TypeIndex UnderlyingTypeIndex = getTypeIndex(UnderlyingTypeRef);
970*9880d681SAndroid Build Coastguard Worker   StringRef TypeName = Ty->getName();
971*9880d681SAndroid Build Coastguard Worker 
972*9880d681SAndroid Build Coastguard Worker   addToUDTs(Ty, UnderlyingTypeIndex);
973*9880d681SAndroid Build Coastguard Worker 
974*9880d681SAndroid Build Coastguard Worker   if (UnderlyingTypeIndex == TypeIndex(SimpleTypeKind::Int32Long) &&
975*9880d681SAndroid Build Coastguard Worker       TypeName == "HRESULT")
976*9880d681SAndroid Build Coastguard Worker     return TypeIndex(SimpleTypeKind::HResult);
977*9880d681SAndroid Build Coastguard Worker   if (UnderlyingTypeIndex == TypeIndex(SimpleTypeKind::UInt16Short) &&
978*9880d681SAndroid Build Coastguard Worker       TypeName == "wchar_t")
979*9880d681SAndroid Build Coastguard Worker     return TypeIndex(SimpleTypeKind::WideCharacter);
980*9880d681SAndroid Build Coastguard Worker 
981*9880d681SAndroid Build Coastguard Worker   return UnderlyingTypeIndex;
982*9880d681SAndroid Build Coastguard Worker }
983*9880d681SAndroid Build Coastguard Worker 
lowerTypeArray(const DICompositeType * Ty)984*9880d681SAndroid Build Coastguard Worker TypeIndex CodeViewDebug::lowerTypeArray(const DICompositeType *Ty) {
985*9880d681SAndroid Build Coastguard Worker   DITypeRef ElementTypeRef = Ty->getBaseType();
986*9880d681SAndroid Build Coastguard Worker   TypeIndex ElementTypeIndex = getTypeIndex(ElementTypeRef);
987*9880d681SAndroid Build Coastguard Worker   // IndexType is size_t, which depends on the bitness of the target.
988*9880d681SAndroid Build Coastguard Worker   TypeIndex IndexType = Asm->MAI->getPointerSize() == 8
989*9880d681SAndroid Build Coastguard Worker                             ? TypeIndex(SimpleTypeKind::UInt64Quad)
990*9880d681SAndroid Build Coastguard Worker                             : TypeIndex(SimpleTypeKind::UInt32Long);
991*9880d681SAndroid Build Coastguard Worker 
992*9880d681SAndroid Build Coastguard Worker   uint64_t ElementSize = getBaseTypeSize(ElementTypeRef) / 8;
993*9880d681SAndroid Build Coastguard Worker 
994*9880d681SAndroid Build Coastguard Worker   bool UndefinedSubrange = false;
995*9880d681SAndroid Build Coastguard Worker 
996*9880d681SAndroid Build Coastguard Worker   // FIXME:
997*9880d681SAndroid Build Coastguard Worker   // There is a bug in the front-end where an array of a structure, which was
998*9880d681SAndroid Build Coastguard Worker   // declared as incomplete structure first, ends up not getting a size assigned
999*9880d681SAndroid Build Coastguard Worker   // to it. (PR28303)
1000*9880d681SAndroid Build Coastguard Worker   // Example:
1001*9880d681SAndroid Build Coastguard Worker   //   struct A(*p)[3];
1002*9880d681SAndroid Build Coastguard Worker   //   struct A { int f; } a[3];
1003*9880d681SAndroid Build Coastguard Worker   //
1004*9880d681SAndroid Build Coastguard Worker   // This needs to be fixed in the front-end, but in the meantime we don't want
1005*9880d681SAndroid Build Coastguard Worker   // to trigger an assertion because of this.
1006*9880d681SAndroid Build Coastguard Worker   if (Ty->getSizeInBits() == 0) {
1007*9880d681SAndroid Build Coastguard Worker     UndefinedSubrange = true;
1008*9880d681SAndroid Build Coastguard Worker   }
1009*9880d681SAndroid Build Coastguard Worker 
1010*9880d681SAndroid Build Coastguard Worker   // Add subranges to array type.
1011*9880d681SAndroid Build Coastguard Worker   DINodeArray Elements = Ty->getElements();
1012*9880d681SAndroid Build Coastguard Worker   for (int i = Elements.size() - 1; i >= 0; --i) {
1013*9880d681SAndroid Build Coastguard Worker     const DINode *Element = Elements[i];
1014*9880d681SAndroid Build Coastguard Worker     assert(Element->getTag() == dwarf::DW_TAG_subrange_type);
1015*9880d681SAndroid Build Coastguard Worker 
1016*9880d681SAndroid Build Coastguard Worker     const DISubrange *Subrange = cast<DISubrange>(Element);
1017*9880d681SAndroid Build Coastguard Worker     assert(Subrange->getLowerBound() == 0 &&
1018*9880d681SAndroid Build Coastguard Worker            "codeview doesn't support subranges with lower bounds");
1019*9880d681SAndroid Build Coastguard Worker     int64_t Count = Subrange->getCount();
1020*9880d681SAndroid Build Coastguard Worker 
1021*9880d681SAndroid Build Coastguard Worker     // Variable Length Array (VLA) has Count equal to '-1'.
1022*9880d681SAndroid Build Coastguard Worker     // Replace with Count '1', assume it is the minimum VLA length.
1023*9880d681SAndroid Build Coastguard Worker     // FIXME: Make front-end support VLA subrange and emit LF_DIMVARLU.
1024*9880d681SAndroid Build Coastguard Worker     if (Count == -1) {
1025*9880d681SAndroid Build Coastguard Worker       Count = 1;
1026*9880d681SAndroid Build Coastguard Worker       UndefinedSubrange = true;
1027*9880d681SAndroid Build Coastguard Worker     }
1028*9880d681SAndroid Build Coastguard Worker 
1029*9880d681SAndroid Build Coastguard Worker     StringRef Name = (i == 0) ? Ty->getName() : "";
1030*9880d681SAndroid Build Coastguard Worker     // Update the element size and element type index for subsequent subranges.
1031*9880d681SAndroid Build Coastguard Worker     ElementSize *= Count;
1032*9880d681SAndroid Build Coastguard Worker     ElementTypeIndex = TypeTable.writeArray(
1033*9880d681SAndroid Build Coastguard Worker         ArrayRecord(ElementTypeIndex, IndexType, ElementSize, Name));
1034*9880d681SAndroid Build Coastguard Worker   }
1035*9880d681SAndroid Build Coastguard Worker 
1036*9880d681SAndroid Build Coastguard Worker   (void)UndefinedSubrange;
1037*9880d681SAndroid Build Coastguard Worker   assert(UndefinedSubrange || ElementSize == (Ty->getSizeInBits() / 8));
1038*9880d681SAndroid Build Coastguard Worker 
1039*9880d681SAndroid Build Coastguard Worker   return ElementTypeIndex;
1040*9880d681SAndroid Build Coastguard Worker }
1041*9880d681SAndroid Build Coastguard Worker 
lowerTypeBasic(const DIBasicType * Ty)1042*9880d681SAndroid Build Coastguard Worker TypeIndex CodeViewDebug::lowerTypeBasic(const DIBasicType *Ty) {
1043*9880d681SAndroid Build Coastguard Worker   TypeIndex Index;
1044*9880d681SAndroid Build Coastguard Worker   dwarf::TypeKind Kind;
1045*9880d681SAndroid Build Coastguard Worker   uint32_t ByteSize;
1046*9880d681SAndroid Build Coastguard Worker 
1047*9880d681SAndroid Build Coastguard Worker   Kind = static_cast<dwarf::TypeKind>(Ty->getEncoding());
1048*9880d681SAndroid Build Coastguard Worker   ByteSize = Ty->getSizeInBits() / 8;
1049*9880d681SAndroid Build Coastguard Worker 
1050*9880d681SAndroid Build Coastguard Worker   SimpleTypeKind STK = SimpleTypeKind::None;
1051*9880d681SAndroid Build Coastguard Worker   switch (Kind) {
1052*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_ATE_address:
1053*9880d681SAndroid Build Coastguard Worker     // FIXME: Translate
1054*9880d681SAndroid Build Coastguard Worker     break;
1055*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_ATE_boolean:
1056*9880d681SAndroid Build Coastguard Worker     switch (ByteSize) {
1057*9880d681SAndroid Build Coastguard Worker     case 1:  STK = SimpleTypeKind::Boolean8;   break;
1058*9880d681SAndroid Build Coastguard Worker     case 2:  STK = SimpleTypeKind::Boolean16;  break;
1059*9880d681SAndroid Build Coastguard Worker     case 4:  STK = SimpleTypeKind::Boolean32;  break;
1060*9880d681SAndroid Build Coastguard Worker     case 8:  STK = SimpleTypeKind::Boolean64;  break;
1061*9880d681SAndroid Build Coastguard Worker     case 16: STK = SimpleTypeKind::Boolean128; break;
1062*9880d681SAndroid Build Coastguard Worker     }
1063*9880d681SAndroid Build Coastguard Worker     break;
1064*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_ATE_complex_float:
1065*9880d681SAndroid Build Coastguard Worker     switch (ByteSize) {
1066*9880d681SAndroid Build Coastguard Worker     case 2:  STK = SimpleTypeKind::Complex16;  break;
1067*9880d681SAndroid Build Coastguard Worker     case 4:  STK = SimpleTypeKind::Complex32;  break;
1068*9880d681SAndroid Build Coastguard Worker     case 8:  STK = SimpleTypeKind::Complex64;  break;
1069*9880d681SAndroid Build Coastguard Worker     case 10: STK = SimpleTypeKind::Complex80;  break;
1070*9880d681SAndroid Build Coastguard Worker     case 16: STK = SimpleTypeKind::Complex128; break;
1071*9880d681SAndroid Build Coastguard Worker     }
1072*9880d681SAndroid Build Coastguard Worker     break;
1073*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_ATE_float:
1074*9880d681SAndroid Build Coastguard Worker     switch (ByteSize) {
1075*9880d681SAndroid Build Coastguard Worker     case 2:  STK = SimpleTypeKind::Float16;  break;
1076*9880d681SAndroid Build Coastguard Worker     case 4:  STK = SimpleTypeKind::Float32;  break;
1077*9880d681SAndroid Build Coastguard Worker     case 6:  STK = SimpleTypeKind::Float48;  break;
1078*9880d681SAndroid Build Coastguard Worker     case 8:  STK = SimpleTypeKind::Float64;  break;
1079*9880d681SAndroid Build Coastguard Worker     case 10: STK = SimpleTypeKind::Float80;  break;
1080*9880d681SAndroid Build Coastguard Worker     case 16: STK = SimpleTypeKind::Float128; break;
1081*9880d681SAndroid Build Coastguard Worker     }
1082*9880d681SAndroid Build Coastguard Worker     break;
1083*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_ATE_signed:
1084*9880d681SAndroid Build Coastguard Worker     switch (ByteSize) {
1085*9880d681SAndroid Build Coastguard Worker     case 1:  STK = SimpleTypeKind::SByte;      break;
1086*9880d681SAndroid Build Coastguard Worker     case 2:  STK = SimpleTypeKind::Int16Short; break;
1087*9880d681SAndroid Build Coastguard Worker     case 4:  STK = SimpleTypeKind::Int32;      break;
1088*9880d681SAndroid Build Coastguard Worker     case 8:  STK = SimpleTypeKind::Int64Quad;  break;
1089*9880d681SAndroid Build Coastguard Worker     case 16: STK = SimpleTypeKind::Int128Oct;  break;
1090*9880d681SAndroid Build Coastguard Worker     }
1091*9880d681SAndroid Build Coastguard Worker     break;
1092*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_ATE_unsigned:
1093*9880d681SAndroid Build Coastguard Worker     switch (ByteSize) {
1094*9880d681SAndroid Build Coastguard Worker     case 1:  STK = SimpleTypeKind::Byte;        break;
1095*9880d681SAndroid Build Coastguard Worker     case 2:  STK = SimpleTypeKind::UInt16Short; break;
1096*9880d681SAndroid Build Coastguard Worker     case 4:  STK = SimpleTypeKind::UInt32;      break;
1097*9880d681SAndroid Build Coastguard Worker     case 8:  STK = SimpleTypeKind::UInt64Quad;  break;
1098*9880d681SAndroid Build Coastguard Worker     case 16: STK = SimpleTypeKind::UInt128Oct;  break;
1099*9880d681SAndroid Build Coastguard Worker     }
1100*9880d681SAndroid Build Coastguard Worker     break;
1101*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_ATE_UTF:
1102*9880d681SAndroid Build Coastguard Worker     switch (ByteSize) {
1103*9880d681SAndroid Build Coastguard Worker     case 2: STK = SimpleTypeKind::Character16; break;
1104*9880d681SAndroid Build Coastguard Worker     case 4: STK = SimpleTypeKind::Character32; break;
1105*9880d681SAndroid Build Coastguard Worker     }
1106*9880d681SAndroid Build Coastguard Worker     break;
1107*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_ATE_signed_char:
1108*9880d681SAndroid Build Coastguard Worker     if (ByteSize == 1)
1109*9880d681SAndroid Build Coastguard Worker       STK = SimpleTypeKind::SignedCharacter;
1110*9880d681SAndroid Build Coastguard Worker     break;
1111*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_ATE_unsigned_char:
1112*9880d681SAndroid Build Coastguard Worker     if (ByteSize == 1)
1113*9880d681SAndroid Build Coastguard Worker       STK = SimpleTypeKind::UnsignedCharacter;
1114*9880d681SAndroid Build Coastguard Worker     break;
1115*9880d681SAndroid Build Coastguard Worker   default:
1116*9880d681SAndroid Build Coastguard Worker     break;
1117*9880d681SAndroid Build Coastguard Worker   }
1118*9880d681SAndroid Build Coastguard Worker 
1119*9880d681SAndroid Build Coastguard Worker   // Apply some fixups based on the source-level type name.
1120*9880d681SAndroid Build Coastguard Worker   if (STK == SimpleTypeKind::Int32 && Ty->getName() == "long int")
1121*9880d681SAndroid Build Coastguard Worker     STK = SimpleTypeKind::Int32Long;
1122*9880d681SAndroid Build Coastguard Worker   if (STK == SimpleTypeKind::UInt32 && Ty->getName() == "long unsigned int")
1123*9880d681SAndroid Build Coastguard Worker     STK = SimpleTypeKind::UInt32Long;
1124*9880d681SAndroid Build Coastguard Worker   if (STK == SimpleTypeKind::UInt16Short &&
1125*9880d681SAndroid Build Coastguard Worker       (Ty->getName() == "wchar_t" || Ty->getName() == "__wchar_t"))
1126*9880d681SAndroid Build Coastguard Worker     STK = SimpleTypeKind::WideCharacter;
1127*9880d681SAndroid Build Coastguard Worker   if ((STK == SimpleTypeKind::SignedCharacter ||
1128*9880d681SAndroid Build Coastguard Worker        STK == SimpleTypeKind::UnsignedCharacter) &&
1129*9880d681SAndroid Build Coastguard Worker       Ty->getName() == "char")
1130*9880d681SAndroid Build Coastguard Worker     STK = SimpleTypeKind::NarrowCharacter;
1131*9880d681SAndroid Build Coastguard Worker 
1132*9880d681SAndroid Build Coastguard Worker   return TypeIndex(STK);
1133*9880d681SAndroid Build Coastguard Worker }
1134*9880d681SAndroid Build Coastguard Worker 
lowerTypePointer(const DIDerivedType * Ty)1135*9880d681SAndroid Build Coastguard Worker TypeIndex CodeViewDebug::lowerTypePointer(const DIDerivedType *Ty) {
1136*9880d681SAndroid Build Coastguard Worker   TypeIndex PointeeTI = getTypeIndex(Ty->getBaseType());
1137*9880d681SAndroid Build Coastguard Worker 
1138*9880d681SAndroid Build Coastguard Worker   // While processing the type being pointed to it is possible we already
1139*9880d681SAndroid Build Coastguard Worker   // created this pointer type.  If so, we check here and return the existing
1140*9880d681SAndroid Build Coastguard Worker   // pointer type.
1141*9880d681SAndroid Build Coastguard Worker   auto I = TypeIndices.find({Ty, nullptr});
1142*9880d681SAndroid Build Coastguard Worker   if (I != TypeIndices.end())
1143*9880d681SAndroid Build Coastguard Worker     return I->second;
1144*9880d681SAndroid Build Coastguard Worker 
1145*9880d681SAndroid Build Coastguard Worker   // Pointers to simple types can use SimpleTypeMode, rather than having a
1146*9880d681SAndroid Build Coastguard Worker   // dedicated pointer type record.
1147*9880d681SAndroid Build Coastguard Worker   if (PointeeTI.isSimple() &&
1148*9880d681SAndroid Build Coastguard Worker       PointeeTI.getSimpleMode() == SimpleTypeMode::Direct &&
1149*9880d681SAndroid Build Coastguard Worker       Ty->getTag() == dwarf::DW_TAG_pointer_type) {
1150*9880d681SAndroid Build Coastguard Worker     SimpleTypeMode Mode = Ty->getSizeInBits() == 64
1151*9880d681SAndroid Build Coastguard Worker                               ? SimpleTypeMode::NearPointer64
1152*9880d681SAndroid Build Coastguard Worker                               : SimpleTypeMode::NearPointer32;
1153*9880d681SAndroid Build Coastguard Worker     return TypeIndex(PointeeTI.getSimpleKind(), Mode);
1154*9880d681SAndroid Build Coastguard Worker   }
1155*9880d681SAndroid Build Coastguard Worker 
1156*9880d681SAndroid Build Coastguard Worker   PointerKind PK =
1157*9880d681SAndroid Build Coastguard Worker       Ty->getSizeInBits() == 64 ? PointerKind::Near64 : PointerKind::Near32;
1158*9880d681SAndroid Build Coastguard Worker   PointerMode PM = PointerMode::Pointer;
1159*9880d681SAndroid Build Coastguard Worker   switch (Ty->getTag()) {
1160*9880d681SAndroid Build Coastguard Worker   default: llvm_unreachable("not a pointer tag type");
1161*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_TAG_pointer_type:
1162*9880d681SAndroid Build Coastguard Worker     PM = PointerMode::Pointer;
1163*9880d681SAndroid Build Coastguard Worker     break;
1164*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_TAG_reference_type:
1165*9880d681SAndroid Build Coastguard Worker     PM = PointerMode::LValueReference;
1166*9880d681SAndroid Build Coastguard Worker     break;
1167*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_TAG_rvalue_reference_type:
1168*9880d681SAndroid Build Coastguard Worker     PM = PointerMode::RValueReference;
1169*9880d681SAndroid Build Coastguard Worker     break;
1170*9880d681SAndroid Build Coastguard Worker   }
1171*9880d681SAndroid Build Coastguard Worker   // FIXME: MSVC folds qualifiers into PointerOptions in the context of a method
1172*9880d681SAndroid Build Coastguard Worker   // 'this' pointer, but not normal contexts. Figure out what we're supposed to
1173*9880d681SAndroid Build Coastguard Worker   // do.
1174*9880d681SAndroid Build Coastguard Worker   PointerOptions PO = PointerOptions::None;
1175*9880d681SAndroid Build Coastguard Worker   PointerRecord PR(PointeeTI, PK, PM, PO, Ty->getSizeInBits() / 8);
1176*9880d681SAndroid Build Coastguard Worker   return TypeTable.writePointer(PR);
1177*9880d681SAndroid Build Coastguard Worker }
1178*9880d681SAndroid Build Coastguard Worker 
1179*9880d681SAndroid Build Coastguard Worker static PointerToMemberRepresentation
translatePtrToMemberRep(unsigned SizeInBytes,bool IsPMF,unsigned Flags)1180*9880d681SAndroid Build Coastguard Worker translatePtrToMemberRep(unsigned SizeInBytes, bool IsPMF, unsigned Flags) {
1181*9880d681SAndroid Build Coastguard Worker   // SizeInBytes being zero generally implies that the member pointer type was
1182*9880d681SAndroid Build Coastguard Worker   // incomplete, which can happen if it is part of a function prototype. In this
1183*9880d681SAndroid Build Coastguard Worker   // case, use the unknown model instead of the general model.
1184*9880d681SAndroid Build Coastguard Worker   if (IsPMF) {
1185*9880d681SAndroid Build Coastguard Worker     switch (Flags & DINode::FlagPtrToMemberRep) {
1186*9880d681SAndroid Build Coastguard Worker     case 0:
1187*9880d681SAndroid Build Coastguard Worker       return SizeInBytes == 0 ? PointerToMemberRepresentation::Unknown
1188*9880d681SAndroid Build Coastguard Worker                               : PointerToMemberRepresentation::GeneralFunction;
1189*9880d681SAndroid Build Coastguard Worker     case DINode::FlagSingleInheritance:
1190*9880d681SAndroid Build Coastguard Worker       return PointerToMemberRepresentation::SingleInheritanceFunction;
1191*9880d681SAndroid Build Coastguard Worker     case DINode::FlagMultipleInheritance:
1192*9880d681SAndroid Build Coastguard Worker       return PointerToMemberRepresentation::MultipleInheritanceFunction;
1193*9880d681SAndroid Build Coastguard Worker     case DINode::FlagVirtualInheritance:
1194*9880d681SAndroid Build Coastguard Worker       return PointerToMemberRepresentation::VirtualInheritanceFunction;
1195*9880d681SAndroid Build Coastguard Worker     }
1196*9880d681SAndroid Build Coastguard Worker   } else {
1197*9880d681SAndroid Build Coastguard Worker     switch (Flags & DINode::FlagPtrToMemberRep) {
1198*9880d681SAndroid Build Coastguard Worker     case 0:
1199*9880d681SAndroid Build Coastguard Worker       return SizeInBytes == 0 ? PointerToMemberRepresentation::Unknown
1200*9880d681SAndroid Build Coastguard Worker                               : PointerToMemberRepresentation::GeneralData;
1201*9880d681SAndroid Build Coastguard Worker     case DINode::FlagSingleInheritance:
1202*9880d681SAndroid Build Coastguard Worker       return PointerToMemberRepresentation::SingleInheritanceData;
1203*9880d681SAndroid Build Coastguard Worker     case DINode::FlagMultipleInheritance:
1204*9880d681SAndroid Build Coastguard Worker       return PointerToMemberRepresentation::MultipleInheritanceData;
1205*9880d681SAndroid Build Coastguard Worker     case DINode::FlagVirtualInheritance:
1206*9880d681SAndroid Build Coastguard Worker       return PointerToMemberRepresentation::VirtualInheritanceData;
1207*9880d681SAndroid Build Coastguard Worker     }
1208*9880d681SAndroid Build Coastguard Worker   }
1209*9880d681SAndroid Build Coastguard Worker   llvm_unreachable("invalid ptr to member representation");
1210*9880d681SAndroid Build Coastguard Worker }
1211*9880d681SAndroid Build Coastguard Worker 
lowerTypeMemberPointer(const DIDerivedType * Ty)1212*9880d681SAndroid Build Coastguard Worker TypeIndex CodeViewDebug::lowerTypeMemberPointer(const DIDerivedType *Ty) {
1213*9880d681SAndroid Build Coastguard Worker   assert(Ty->getTag() == dwarf::DW_TAG_ptr_to_member_type);
1214*9880d681SAndroid Build Coastguard Worker   TypeIndex ClassTI = getTypeIndex(Ty->getClassType());
1215*9880d681SAndroid Build Coastguard Worker   TypeIndex PointeeTI = getTypeIndex(Ty->getBaseType(), Ty->getClassType());
1216*9880d681SAndroid Build Coastguard Worker   PointerKind PK = Asm->MAI->getPointerSize() == 8 ? PointerKind::Near64
1217*9880d681SAndroid Build Coastguard Worker                                                    : PointerKind::Near32;
1218*9880d681SAndroid Build Coastguard Worker   bool IsPMF = isa<DISubroutineType>(Ty->getBaseType());
1219*9880d681SAndroid Build Coastguard Worker   PointerMode PM = IsPMF ? PointerMode::PointerToMemberFunction
1220*9880d681SAndroid Build Coastguard Worker                          : PointerMode::PointerToDataMember;
1221*9880d681SAndroid Build Coastguard Worker   PointerOptions PO = PointerOptions::None; // FIXME
1222*9880d681SAndroid Build Coastguard Worker   assert(Ty->getSizeInBits() / 8 <= 0xff && "pointer size too big");
1223*9880d681SAndroid Build Coastguard Worker   uint8_t SizeInBytes = Ty->getSizeInBits() / 8;
1224*9880d681SAndroid Build Coastguard Worker   MemberPointerInfo MPI(
1225*9880d681SAndroid Build Coastguard Worker       ClassTI, translatePtrToMemberRep(SizeInBytes, IsPMF, Ty->getFlags()));
1226*9880d681SAndroid Build Coastguard Worker   PointerRecord PR(PointeeTI, PK, PM, PO, SizeInBytes, MPI);
1227*9880d681SAndroid Build Coastguard Worker   return TypeTable.writePointer(PR);
1228*9880d681SAndroid Build Coastguard Worker }
1229*9880d681SAndroid Build Coastguard Worker 
1230*9880d681SAndroid Build Coastguard Worker /// Given a DWARF calling convention, get the CodeView equivalent. If we don't
1231*9880d681SAndroid Build Coastguard Worker /// have a translation, use the NearC convention.
dwarfCCToCodeView(unsigned DwarfCC)1232*9880d681SAndroid Build Coastguard Worker static CallingConvention dwarfCCToCodeView(unsigned DwarfCC) {
1233*9880d681SAndroid Build Coastguard Worker   switch (DwarfCC) {
1234*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_CC_normal:             return CallingConvention::NearC;
1235*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_CC_BORLAND_msfastcall: return CallingConvention::NearFast;
1236*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_CC_BORLAND_thiscall:   return CallingConvention::ThisCall;
1237*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_CC_BORLAND_stdcall:    return CallingConvention::NearStdCall;
1238*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_CC_BORLAND_pascal:     return CallingConvention::NearPascal;
1239*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_CC_LLVM_vectorcall:    return CallingConvention::NearVector;
1240*9880d681SAndroid Build Coastguard Worker   }
1241*9880d681SAndroid Build Coastguard Worker   return CallingConvention::NearC;
1242*9880d681SAndroid Build Coastguard Worker }
1243*9880d681SAndroid Build Coastguard Worker 
lowerTypeModifier(const DIDerivedType * Ty)1244*9880d681SAndroid Build Coastguard Worker TypeIndex CodeViewDebug::lowerTypeModifier(const DIDerivedType *Ty) {
1245*9880d681SAndroid Build Coastguard Worker   ModifierOptions Mods = ModifierOptions::None;
1246*9880d681SAndroid Build Coastguard Worker   bool IsModifier = true;
1247*9880d681SAndroid Build Coastguard Worker   const DIType *BaseTy = Ty;
1248*9880d681SAndroid Build Coastguard Worker   while (IsModifier && BaseTy) {
1249*9880d681SAndroid Build Coastguard Worker     // FIXME: Need to add DWARF tag for __unaligned.
1250*9880d681SAndroid Build Coastguard Worker     switch (BaseTy->getTag()) {
1251*9880d681SAndroid Build Coastguard Worker     case dwarf::DW_TAG_const_type:
1252*9880d681SAndroid Build Coastguard Worker       Mods |= ModifierOptions::Const;
1253*9880d681SAndroid Build Coastguard Worker       break;
1254*9880d681SAndroid Build Coastguard Worker     case dwarf::DW_TAG_volatile_type:
1255*9880d681SAndroid Build Coastguard Worker       Mods |= ModifierOptions::Volatile;
1256*9880d681SAndroid Build Coastguard Worker       break;
1257*9880d681SAndroid Build Coastguard Worker     default:
1258*9880d681SAndroid Build Coastguard Worker       IsModifier = false;
1259*9880d681SAndroid Build Coastguard Worker       break;
1260*9880d681SAndroid Build Coastguard Worker     }
1261*9880d681SAndroid Build Coastguard Worker     if (IsModifier)
1262*9880d681SAndroid Build Coastguard Worker       BaseTy = cast<DIDerivedType>(BaseTy)->getBaseType().resolve();
1263*9880d681SAndroid Build Coastguard Worker   }
1264*9880d681SAndroid Build Coastguard Worker   TypeIndex ModifiedTI = getTypeIndex(BaseTy);
1265*9880d681SAndroid Build Coastguard Worker 
1266*9880d681SAndroid Build Coastguard Worker   // While processing the type being pointed to, it is possible we already
1267*9880d681SAndroid Build Coastguard Worker   // created this modifier type.  If so, we check here and return the existing
1268*9880d681SAndroid Build Coastguard Worker   // modifier type.
1269*9880d681SAndroid Build Coastguard Worker   auto I = TypeIndices.find({Ty, nullptr});
1270*9880d681SAndroid Build Coastguard Worker   if (I != TypeIndices.end())
1271*9880d681SAndroid Build Coastguard Worker     return I->second;
1272*9880d681SAndroid Build Coastguard Worker 
1273*9880d681SAndroid Build Coastguard Worker   ModifierRecord MR(ModifiedTI, Mods);
1274*9880d681SAndroid Build Coastguard Worker   return TypeTable.writeModifier(MR);
1275*9880d681SAndroid Build Coastguard Worker }
1276*9880d681SAndroid Build Coastguard Worker 
lowerTypeFunction(const DISubroutineType * Ty)1277*9880d681SAndroid Build Coastguard Worker TypeIndex CodeViewDebug::lowerTypeFunction(const DISubroutineType *Ty) {
1278*9880d681SAndroid Build Coastguard Worker   SmallVector<TypeIndex, 8> ReturnAndArgTypeIndices;
1279*9880d681SAndroid Build Coastguard Worker   for (DITypeRef ArgTypeRef : Ty->getTypeArray())
1280*9880d681SAndroid Build Coastguard Worker     ReturnAndArgTypeIndices.push_back(getTypeIndex(ArgTypeRef));
1281*9880d681SAndroid Build Coastguard Worker 
1282*9880d681SAndroid Build Coastguard Worker   TypeIndex ReturnTypeIndex = TypeIndex::Void();
1283*9880d681SAndroid Build Coastguard Worker   ArrayRef<TypeIndex> ArgTypeIndices = None;
1284*9880d681SAndroid Build Coastguard Worker   if (!ReturnAndArgTypeIndices.empty()) {
1285*9880d681SAndroid Build Coastguard Worker     auto ReturnAndArgTypesRef = makeArrayRef(ReturnAndArgTypeIndices);
1286*9880d681SAndroid Build Coastguard Worker     ReturnTypeIndex = ReturnAndArgTypesRef.front();
1287*9880d681SAndroid Build Coastguard Worker     ArgTypeIndices = ReturnAndArgTypesRef.drop_front();
1288*9880d681SAndroid Build Coastguard Worker   }
1289*9880d681SAndroid Build Coastguard Worker 
1290*9880d681SAndroid Build Coastguard Worker   ArgListRecord ArgListRec(TypeRecordKind::ArgList, ArgTypeIndices);
1291*9880d681SAndroid Build Coastguard Worker   TypeIndex ArgListIndex = TypeTable.writeArgList(ArgListRec);
1292*9880d681SAndroid Build Coastguard Worker 
1293*9880d681SAndroid Build Coastguard Worker   CallingConvention CC = dwarfCCToCodeView(Ty->getCC());
1294*9880d681SAndroid Build Coastguard Worker 
1295*9880d681SAndroid Build Coastguard Worker   ProcedureRecord Procedure(ReturnTypeIndex, CC, FunctionOptions::None,
1296*9880d681SAndroid Build Coastguard Worker                             ArgTypeIndices.size(), ArgListIndex);
1297*9880d681SAndroid Build Coastguard Worker   return TypeTable.writeProcedure(Procedure);
1298*9880d681SAndroid Build Coastguard Worker }
1299*9880d681SAndroid Build Coastguard Worker 
lowerTypeMemberFunction(const DISubroutineType * Ty,const DIType * ClassTy,int ThisAdjustment)1300*9880d681SAndroid Build Coastguard Worker TypeIndex CodeViewDebug::lowerTypeMemberFunction(const DISubroutineType *Ty,
1301*9880d681SAndroid Build Coastguard Worker                                                  const DIType *ClassTy,
1302*9880d681SAndroid Build Coastguard Worker                                                  int ThisAdjustment) {
1303*9880d681SAndroid Build Coastguard Worker   // Lower the containing class type.
1304*9880d681SAndroid Build Coastguard Worker   TypeIndex ClassType = getTypeIndex(ClassTy);
1305*9880d681SAndroid Build Coastguard Worker 
1306*9880d681SAndroid Build Coastguard Worker   SmallVector<TypeIndex, 8> ReturnAndArgTypeIndices;
1307*9880d681SAndroid Build Coastguard Worker   for (DITypeRef ArgTypeRef : Ty->getTypeArray())
1308*9880d681SAndroid Build Coastguard Worker     ReturnAndArgTypeIndices.push_back(getTypeIndex(ArgTypeRef));
1309*9880d681SAndroid Build Coastguard Worker 
1310*9880d681SAndroid Build Coastguard Worker   TypeIndex ReturnTypeIndex = TypeIndex::Void();
1311*9880d681SAndroid Build Coastguard Worker   ArrayRef<TypeIndex> ArgTypeIndices = None;
1312*9880d681SAndroid Build Coastguard Worker   if (!ReturnAndArgTypeIndices.empty()) {
1313*9880d681SAndroid Build Coastguard Worker     auto ReturnAndArgTypesRef = makeArrayRef(ReturnAndArgTypeIndices);
1314*9880d681SAndroid Build Coastguard Worker     ReturnTypeIndex = ReturnAndArgTypesRef.front();
1315*9880d681SAndroid Build Coastguard Worker     ArgTypeIndices = ReturnAndArgTypesRef.drop_front();
1316*9880d681SAndroid Build Coastguard Worker   }
1317*9880d681SAndroid Build Coastguard Worker   TypeIndex ThisTypeIndex = TypeIndex::Void();
1318*9880d681SAndroid Build Coastguard Worker   if (!ArgTypeIndices.empty()) {
1319*9880d681SAndroid Build Coastguard Worker     ThisTypeIndex = ArgTypeIndices.front();
1320*9880d681SAndroid Build Coastguard Worker     ArgTypeIndices = ArgTypeIndices.drop_front();
1321*9880d681SAndroid Build Coastguard Worker   }
1322*9880d681SAndroid Build Coastguard Worker 
1323*9880d681SAndroid Build Coastguard Worker   ArgListRecord ArgListRec(TypeRecordKind::ArgList, ArgTypeIndices);
1324*9880d681SAndroid Build Coastguard Worker   TypeIndex ArgListIndex = TypeTable.writeArgList(ArgListRec);
1325*9880d681SAndroid Build Coastguard Worker 
1326*9880d681SAndroid Build Coastguard Worker   CallingConvention CC = dwarfCCToCodeView(Ty->getCC());
1327*9880d681SAndroid Build Coastguard Worker 
1328*9880d681SAndroid Build Coastguard Worker   // TODO: Need to use the correct values for:
1329*9880d681SAndroid Build Coastguard Worker   //       FunctionOptions
1330*9880d681SAndroid Build Coastguard Worker   //       ThisPointerAdjustment.
1331*9880d681SAndroid Build Coastguard Worker   TypeIndex TI = TypeTable.writeMemberFunction(MemberFunctionRecord(
1332*9880d681SAndroid Build Coastguard Worker       ReturnTypeIndex, ClassType, ThisTypeIndex, CC, FunctionOptions::None,
1333*9880d681SAndroid Build Coastguard Worker       ArgTypeIndices.size(), ArgListIndex, ThisAdjustment));
1334*9880d681SAndroid Build Coastguard Worker 
1335*9880d681SAndroid Build Coastguard Worker   return TI;
1336*9880d681SAndroid Build Coastguard Worker }
1337*9880d681SAndroid Build Coastguard Worker 
translateAccessFlags(unsigned RecordTag,unsigned Flags)1338*9880d681SAndroid Build Coastguard Worker static MemberAccess translateAccessFlags(unsigned RecordTag, unsigned Flags) {
1339*9880d681SAndroid Build Coastguard Worker   switch (Flags & DINode::FlagAccessibility) {
1340*9880d681SAndroid Build Coastguard Worker   case DINode::FlagPrivate:   return MemberAccess::Private;
1341*9880d681SAndroid Build Coastguard Worker   case DINode::FlagPublic:    return MemberAccess::Public;
1342*9880d681SAndroid Build Coastguard Worker   case DINode::FlagProtected: return MemberAccess::Protected;
1343*9880d681SAndroid Build Coastguard Worker   case 0:
1344*9880d681SAndroid Build Coastguard Worker     // If there was no explicit access control, provide the default for the tag.
1345*9880d681SAndroid Build Coastguard Worker     return RecordTag == dwarf::DW_TAG_class_type ? MemberAccess::Private
1346*9880d681SAndroid Build Coastguard Worker                                                  : MemberAccess::Public;
1347*9880d681SAndroid Build Coastguard Worker   }
1348*9880d681SAndroid Build Coastguard Worker   llvm_unreachable("access flags are exclusive");
1349*9880d681SAndroid Build Coastguard Worker }
1350*9880d681SAndroid Build Coastguard Worker 
translateMethodOptionFlags(const DISubprogram * SP)1351*9880d681SAndroid Build Coastguard Worker static MethodOptions translateMethodOptionFlags(const DISubprogram *SP) {
1352*9880d681SAndroid Build Coastguard Worker   if (SP->isArtificial())
1353*9880d681SAndroid Build Coastguard Worker     return MethodOptions::CompilerGenerated;
1354*9880d681SAndroid Build Coastguard Worker 
1355*9880d681SAndroid Build Coastguard Worker   // FIXME: Handle other MethodOptions.
1356*9880d681SAndroid Build Coastguard Worker 
1357*9880d681SAndroid Build Coastguard Worker   return MethodOptions::None;
1358*9880d681SAndroid Build Coastguard Worker }
1359*9880d681SAndroid Build Coastguard Worker 
translateMethodKindFlags(const DISubprogram * SP,bool Introduced)1360*9880d681SAndroid Build Coastguard Worker static MethodKind translateMethodKindFlags(const DISubprogram *SP,
1361*9880d681SAndroid Build Coastguard Worker                                            bool Introduced) {
1362*9880d681SAndroid Build Coastguard Worker   switch (SP->getVirtuality()) {
1363*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_VIRTUALITY_none:
1364*9880d681SAndroid Build Coastguard Worker     break;
1365*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_VIRTUALITY_virtual:
1366*9880d681SAndroid Build Coastguard Worker     return Introduced ? MethodKind::IntroducingVirtual : MethodKind::Virtual;
1367*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_VIRTUALITY_pure_virtual:
1368*9880d681SAndroid Build Coastguard Worker     return Introduced ? MethodKind::PureIntroducingVirtual
1369*9880d681SAndroid Build Coastguard Worker                       : MethodKind::PureVirtual;
1370*9880d681SAndroid Build Coastguard Worker   default:
1371*9880d681SAndroid Build Coastguard Worker     llvm_unreachable("unhandled virtuality case");
1372*9880d681SAndroid Build Coastguard Worker   }
1373*9880d681SAndroid Build Coastguard Worker 
1374*9880d681SAndroid Build Coastguard Worker   // FIXME: Get Clang to mark DISubprogram as static and do something with it.
1375*9880d681SAndroid Build Coastguard Worker 
1376*9880d681SAndroid Build Coastguard Worker   return MethodKind::Vanilla;
1377*9880d681SAndroid Build Coastguard Worker }
1378*9880d681SAndroid Build Coastguard Worker 
getRecordKind(const DICompositeType * Ty)1379*9880d681SAndroid Build Coastguard Worker static TypeRecordKind getRecordKind(const DICompositeType *Ty) {
1380*9880d681SAndroid Build Coastguard Worker   switch (Ty->getTag()) {
1381*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_TAG_class_type:     return TypeRecordKind::Class;
1382*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_TAG_structure_type: return TypeRecordKind::Struct;
1383*9880d681SAndroid Build Coastguard Worker   }
1384*9880d681SAndroid Build Coastguard Worker   llvm_unreachable("unexpected tag");
1385*9880d681SAndroid Build Coastguard Worker }
1386*9880d681SAndroid Build Coastguard Worker 
1387*9880d681SAndroid Build Coastguard Worker /// Return ClassOptions that should be present on both the forward declaration
1388*9880d681SAndroid Build Coastguard Worker /// and the defintion of a tag type.
getCommonClassOptions(const DICompositeType * Ty)1389*9880d681SAndroid Build Coastguard Worker static ClassOptions getCommonClassOptions(const DICompositeType *Ty) {
1390*9880d681SAndroid Build Coastguard Worker   ClassOptions CO = ClassOptions::None;
1391*9880d681SAndroid Build Coastguard Worker 
1392*9880d681SAndroid Build Coastguard Worker   // MSVC always sets this flag, even for local types. Clang doesn't always
1393*9880d681SAndroid Build Coastguard Worker   // appear to give every type a linkage name, which may be problematic for us.
1394*9880d681SAndroid Build Coastguard Worker   // FIXME: Investigate the consequences of not following them here.
1395*9880d681SAndroid Build Coastguard Worker   if (!Ty->getIdentifier().empty())
1396*9880d681SAndroid Build Coastguard Worker     CO |= ClassOptions::HasUniqueName;
1397*9880d681SAndroid Build Coastguard Worker 
1398*9880d681SAndroid Build Coastguard Worker   // Put the Nested flag on a type if it appears immediately inside a tag type.
1399*9880d681SAndroid Build Coastguard Worker   // Do not walk the scope chain. Do not attempt to compute ContainsNestedClass
1400*9880d681SAndroid Build Coastguard Worker   // here. That flag is only set on definitions, and not forward declarations.
1401*9880d681SAndroid Build Coastguard Worker   const DIScope *ImmediateScope = Ty->getScope().resolve();
1402*9880d681SAndroid Build Coastguard Worker   if (ImmediateScope && isa<DICompositeType>(ImmediateScope))
1403*9880d681SAndroid Build Coastguard Worker     CO |= ClassOptions::Nested;
1404*9880d681SAndroid Build Coastguard Worker 
1405*9880d681SAndroid Build Coastguard Worker   // Put the Scoped flag on function-local types.
1406*9880d681SAndroid Build Coastguard Worker   for (const DIScope *Scope = ImmediateScope; Scope != nullptr;
1407*9880d681SAndroid Build Coastguard Worker        Scope = Scope->getScope().resolve()) {
1408*9880d681SAndroid Build Coastguard Worker     if (isa<DISubprogram>(Scope)) {
1409*9880d681SAndroid Build Coastguard Worker       CO |= ClassOptions::Scoped;
1410*9880d681SAndroid Build Coastguard Worker       break;
1411*9880d681SAndroid Build Coastguard Worker     }
1412*9880d681SAndroid Build Coastguard Worker   }
1413*9880d681SAndroid Build Coastguard Worker 
1414*9880d681SAndroid Build Coastguard Worker   return CO;
1415*9880d681SAndroid Build Coastguard Worker }
1416*9880d681SAndroid Build Coastguard Worker 
lowerTypeEnum(const DICompositeType * Ty)1417*9880d681SAndroid Build Coastguard Worker TypeIndex CodeViewDebug::lowerTypeEnum(const DICompositeType *Ty) {
1418*9880d681SAndroid Build Coastguard Worker   ClassOptions CO = getCommonClassOptions(Ty);
1419*9880d681SAndroid Build Coastguard Worker   TypeIndex FTI;
1420*9880d681SAndroid Build Coastguard Worker   unsigned EnumeratorCount = 0;
1421*9880d681SAndroid Build Coastguard Worker 
1422*9880d681SAndroid Build Coastguard Worker   if (Ty->isForwardDecl()) {
1423*9880d681SAndroid Build Coastguard Worker     CO |= ClassOptions::ForwardReference;
1424*9880d681SAndroid Build Coastguard Worker   } else {
1425*9880d681SAndroid Build Coastguard Worker     FieldListRecordBuilder Fields;
1426*9880d681SAndroid Build Coastguard Worker     for (const DINode *Element : Ty->getElements()) {
1427*9880d681SAndroid Build Coastguard Worker       // We assume that the frontend provides all members in source declaration
1428*9880d681SAndroid Build Coastguard Worker       // order, which is what MSVC does.
1429*9880d681SAndroid Build Coastguard Worker       if (auto *Enumerator = dyn_cast_or_null<DIEnumerator>(Element)) {
1430*9880d681SAndroid Build Coastguard Worker         Fields.writeEnumerator(EnumeratorRecord(
1431*9880d681SAndroid Build Coastguard Worker             MemberAccess::Public, APSInt::getUnsigned(Enumerator->getValue()),
1432*9880d681SAndroid Build Coastguard Worker             Enumerator->getName()));
1433*9880d681SAndroid Build Coastguard Worker         EnumeratorCount++;
1434*9880d681SAndroid Build Coastguard Worker       }
1435*9880d681SAndroid Build Coastguard Worker     }
1436*9880d681SAndroid Build Coastguard Worker     FTI = TypeTable.writeFieldList(Fields);
1437*9880d681SAndroid Build Coastguard Worker   }
1438*9880d681SAndroid Build Coastguard Worker 
1439*9880d681SAndroid Build Coastguard Worker   std::string FullName = getFullyQualifiedName(Ty);
1440*9880d681SAndroid Build Coastguard Worker 
1441*9880d681SAndroid Build Coastguard Worker   return TypeTable.writeEnum(EnumRecord(EnumeratorCount, CO, FTI, FullName,
1442*9880d681SAndroid Build Coastguard Worker                                         Ty->getIdentifier(),
1443*9880d681SAndroid Build Coastguard Worker                                         getTypeIndex(Ty->getBaseType())));
1444*9880d681SAndroid Build Coastguard Worker }
1445*9880d681SAndroid Build Coastguard Worker 
1446*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
1447*9880d681SAndroid Build Coastguard Worker // ClassInfo
1448*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
1449*9880d681SAndroid Build Coastguard Worker 
1450*9880d681SAndroid Build Coastguard Worker struct llvm::ClassInfo {
1451*9880d681SAndroid Build Coastguard Worker   struct MemberInfo {
1452*9880d681SAndroid Build Coastguard Worker     const DIDerivedType *MemberTypeNode;
1453*9880d681SAndroid Build Coastguard Worker     uint64_t BaseOffset;
1454*9880d681SAndroid Build Coastguard Worker   };
1455*9880d681SAndroid Build Coastguard Worker   // [MemberInfo]
1456*9880d681SAndroid Build Coastguard Worker   typedef std::vector<MemberInfo> MemberList;
1457*9880d681SAndroid Build Coastguard Worker 
1458*9880d681SAndroid Build Coastguard Worker   typedef TinyPtrVector<const DISubprogram *> MethodsList;
1459*9880d681SAndroid Build Coastguard Worker   // MethodName -> MethodsList
1460*9880d681SAndroid Build Coastguard Worker   typedef MapVector<MDString *, MethodsList> MethodsMap;
1461*9880d681SAndroid Build Coastguard Worker 
1462*9880d681SAndroid Build Coastguard Worker   /// Base classes.
1463*9880d681SAndroid Build Coastguard Worker   std::vector<const DIDerivedType *> Inheritance;
1464*9880d681SAndroid Build Coastguard Worker 
1465*9880d681SAndroid Build Coastguard Worker   /// Direct members.
1466*9880d681SAndroid Build Coastguard Worker   MemberList Members;
1467*9880d681SAndroid Build Coastguard Worker   // Direct overloaded methods gathered by name.
1468*9880d681SAndroid Build Coastguard Worker   MethodsMap Methods;
1469*9880d681SAndroid Build Coastguard Worker 
1470*9880d681SAndroid Build Coastguard Worker   std::vector<const DICompositeType *> NestedClasses;
1471*9880d681SAndroid Build Coastguard Worker };
1472*9880d681SAndroid Build Coastguard Worker 
clear()1473*9880d681SAndroid Build Coastguard Worker void CodeViewDebug::clear() {
1474*9880d681SAndroid Build Coastguard Worker   assert(CurFn == nullptr);
1475*9880d681SAndroid Build Coastguard Worker   FileIdMap.clear();
1476*9880d681SAndroid Build Coastguard Worker   FnDebugInfo.clear();
1477*9880d681SAndroid Build Coastguard Worker   FileToFilepathMap.clear();
1478*9880d681SAndroid Build Coastguard Worker   LocalUDTs.clear();
1479*9880d681SAndroid Build Coastguard Worker   GlobalUDTs.clear();
1480*9880d681SAndroid Build Coastguard Worker   TypeIndices.clear();
1481*9880d681SAndroid Build Coastguard Worker   CompleteTypeIndices.clear();
1482*9880d681SAndroid Build Coastguard Worker }
1483*9880d681SAndroid Build Coastguard Worker 
collectMemberInfo(ClassInfo & Info,const DIDerivedType * DDTy)1484*9880d681SAndroid Build Coastguard Worker void CodeViewDebug::collectMemberInfo(ClassInfo &Info,
1485*9880d681SAndroid Build Coastguard Worker                                       const DIDerivedType *DDTy) {
1486*9880d681SAndroid Build Coastguard Worker   if (!DDTy->getName().empty()) {
1487*9880d681SAndroid Build Coastguard Worker     Info.Members.push_back({DDTy, 0});
1488*9880d681SAndroid Build Coastguard Worker     return;
1489*9880d681SAndroid Build Coastguard Worker   }
1490*9880d681SAndroid Build Coastguard Worker   // An unnamed member must represent a nested struct or union. Add all the
1491*9880d681SAndroid Build Coastguard Worker   // indirect fields to the current record.
1492*9880d681SAndroid Build Coastguard Worker   assert((DDTy->getOffsetInBits() % 8) == 0 && "Unnamed bitfield member!");
1493*9880d681SAndroid Build Coastguard Worker   uint64_t Offset = DDTy->getOffsetInBits();
1494*9880d681SAndroid Build Coastguard Worker   const DIType *Ty = DDTy->getBaseType().resolve();
1495*9880d681SAndroid Build Coastguard Worker   const DICompositeType *DCTy = cast<DICompositeType>(Ty);
1496*9880d681SAndroid Build Coastguard Worker   ClassInfo NestedInfo = collectClassInfo(DCTy);
1497*9880d681SAndroid Build Coastguard Worker   for (const ClassInfo::MemberInfo &IndirectField : NestedInfo.Members)
1498*9880d681SAndroid Build Coastguard Worker     Info.Members.push_back(
1499*9880d681SAndroid Build Coastguard Worker         {IndirectField.MemberTypeNode, IndirectField.BaseOffset + Offset});
1500*9880d681SAndroid Build Coastguard Worker }
1501*9880d681SAndroid Build Coastguard Worker 
collectClassInfo(const DICompositeType * Ty)1502*9880d681SAndroid Build Coastguard Worker ClassInfo CodeViewDebug::collectClassInfo(const DICompositeType *Ty) {
1503*9880d681SAndroid Build Coastguard Worker   ClassInfo Info;
1504*9880d681SAndroid Build Coastguard Worker   // Add elements to structure type.
1505*9880d681SAndroid Build Coastguard Worker   DINodeArray Elements = Ty->getElements();
1506*9880d681SAndroid Build Coastguard Worker   for (auto *Element : Elements) {
1507*9880d681SAndroid Build Coastguard Worker     // We assume that the frontend provides all members in source declaration
1508*9880d681SAndroid Build Coastguard Worker     // order, which is what MSVC does.
1509*9880d681SAndroid Build Coastguard Worker     if (!Element)
1510*9880d681SAndroid Build Coastguard Worker       continue;
1511*9880d681SAndroid Build Coastguard Worker     if (auto *SP = dyn_cast<DISubprogram>(Element)) {
1512*9880d681SAndroid Build Coastguard Worker       Info.Methods[SP->getRawName()].push_back(SP);
1513*9880d681SAndroid Build Coastguard Worker     } else if (auto *DDTy = dyn_cast<DIDerivedType>(Element)) {
1514*9880d681SAndroid Build Coastguard Worker       if (DDTy->getTag() == dwarf::DW_TAG_member) {
1515*9880d681SAndroid Build Coastguard Worker         collectMemberInfo(Info, DDTy);
1516*9880d681SAndroid Build Coastguard Worker       } else if (DDTy->getTag() == dwarf::DW_TAG_inheritance) {
1517*9880d681SAndroid Build Coastguard Worker         Info.Inheritance.push_back(DDTy);
1518*9880d681SAndroid Build Coastguard Worker       } else if (DDTy->getTag() == dwarf::DW_TAG_friend) {
1519*9880d681SAndroid Build Coastguard Worker         // Ignore friend members. It appears that MSVC emitted info about
1520*9880d681SAndroid Build Coastguard Worker         // friends in the past, but modern versions do not.
1521*9880d681SAndroid Build Coastguard Worker       }
1522*9880d681SAndroid Build Coastguard Worker       // FIXME: Get Clang to emit function virtual table here and handle it.
1523*9880d681SAndroid Build Coastguard Worker     } else if (auto *Composite = dyn_cast<DICompositeType>(Element)) {
1524*9880d681SAndroid Build Coastguard Worker       Info.NestedClasses.push_back(Composite);
1525*9880d681SAndroid Build Coastguard Worker     }
1526*9880d681SAndroid Build Coastguard Worker     // Skip other unrecognized kinds of elements.
1527*9880d681SAndroid Build Coastguard Worker   }
1528*9880d681SAndroid Build Coastguard Worker   return Info;
1529*9880d681SAndroid Build Coastguard Worker }
1530*9880d681SAndroid Build Coastguard Worker 
lowerTypeClass(const DICompositeType * Ty)1531*9880d681SAndroid Build Coastguard Worker TypeIndex CodeViewDebug::lowerTypeClass(const DICompositeType *Ty) {
1532*9880d681SAndroid Build Coastguard Worker   // First, construct the forward decl.  Don't look into Ty to compute the
1533*9880d681SAndroid Build Coastguard Worker   // forward decl options, since it might not be available in all TUs.
1534*9880d681SAndroid Build Coastguard Worker   TypeRecordKind Kind = getRecordKind(Ty);
1535*9880d681SAndroid Build Coastguard Worker   ClassOptions CO =
1536*9880d681SAndroid Build Coastguard Worker       ClassOptions::ForwardReference | getCommonClassOptions(Ty);
1537*9880d681SAndroid Build Coastguard Worker   std::string FullName = getFullyQualifiedName(Ty);
1538*9880d681SAndroid Build Coastguard Worker   TypeIndex FwdDeclTI = TypeTable.writeClass(ClassRecord(
1539*9880d681SAndroid Build Coastguard Worker       Kind, 0, CO, HfaKind::None, WindowsRTClassKind::None, TypeIndex(),
1540*9880d681SAndroid Build Coastguard Worker       TypeIndex(), TypeIndex(), 0, FullName, Ty->getIdentifier()));
1541*9880d681SAndroid Build Coastguard Worker   if (!Ty->isForwardDecl())
1542*9880d681SAndroid Build Coastguard Worker     DeferredCompleteTypes.push_back(Ty);
1543*9880d681SAndroid Build Coastguard Worker   return FwdDeclTI;
1544*9880d681SAndroid Build Coastguard Worker }
1545*9880d681SAndroid Build Coastguard Worker 
lowerCompleteTypeClass(const DICompositeType * Ty)1546*9880d681SAndroid Build Coastguard Worker TypeIndex CodeViewDebug::lowerCompleteTypeClass(const DICompositeType *Ty) {
1547*9880d681SAndroid Build Coastguard Worker   // Construct the field list and complete type record.
1548*9880d681SAndroid Build Coastguard Worker   TypeRecordKind Kind = getRecordKind(Ty);
1549*9880d681SAndroid Build Coastguard Worker   ClassOptions CO = getCommonClassOptions(Ty);
1550*9880d681SAndroid Build Coastguard Worker   TypeIndex FieldTI;
1551*9880d681SAndroid Build Coastguard Worker   TypeIndex VShapeTI;
1552*9880d681SAndroid Build Coastguard Worker   unsigned FieldCount;
1553*9880d681SAndroid Build Coastguard Worker   bool ContainsNestedClass;
1554*9880d681SAndroid Build Coastguard Worker   std::tie(FieldTI, VShapeTI, FieldCount, ContainsNestedClass) =
1555*9880d681SAndroid Build Coastguard Worker       lowerRecordFieldList(Ty);
1556*9880d681SAndroid Build Coastguard Worker 
1557*9880d681SAndroid Build Coastguard Worker   if (ContainsNestedClass)
1558*9880d681SAndroid Build Coastguard Worker     CO |= ClassOptions::ContainsNestedClass;
1559*9880d681SAndroid Build Coastguard Worker 
1560*9880d681SAndroid Build Coastguard Worker   std::string FullName = getFullyQualifiedName(Ty);
1561*9880d681SAndroid Build Coastguard Worker 
1562*9880d681SAndroid Build Coastguard Worker   uint64_t SizeInBytes = Ty->getSizeInBits() / 8;
1563*9880d681SAndroid Build Coastguard Worker 
1564*9880d681SAndroid Build Coastguard Worker   TypeIndex ClassTI = TypeTable.writeClass(ClassRecord(
1565*9880d681SAndroid Build Coastguard Worker       Kind, FieldCount, CO, HfaKind::None, WindowsRTClassKind::None, FieldTI,
1566*9880d681SAndroid Build Coastguard Worker       TypeIndex(), VShapeTI, SizeInBytes, FullName, Ty->getIdentifier()));
1567*9880d681SAndroid Build Coastguard Worker 
1568*9880d681SAndroid Build Coastguard Worker   TypeTable.writeUdtSourceLine(UdtSourceLineRecord(
1569*9880d681SAndroid Build Coastguard Worker       ClassTI, TypeTable.writeStringId(StringIdRecord(
1570*9880d681SAndroid Build Coastguard Worker                    TypeIndex(0x0), getFullFilepath(Ty->getFile()))),
1571*9880d681SAndroid Build Coastguard Worker       Ty->getLine()));
1572*9880d681SAndroid Build Coastguard Worker 
1573*9880d681SAndroid Build Coastguard Worker   addToUDTs(Ty, ClassTI);
1574*9880d681SAndroid Build Coastguard Worker 
1575*9880d681SAndroid Build Coastguard Worker   return ClassTI;
1576*9880d681SAndroid Build Coastguard Worker }
1577*9880d681SAndroid Build Coastguard Worker 
lowerTypeUnion(const DICompositeType * Ty)1578*9880d681SAndroid Build Coastguard Worker TypeIndex CodeViewDebug::lowerTypeUnion(const DICompositeType *Ty) {
1579*9880d681SAndroid Build Coastguard Worker   ClassOptions CO =
1580*9880d681SAndroid Build Coastguard Worker       ClassOptions::ForwardReference | getCommonClassOptions(Ty);
1581*9880d681SAndroid Build Coastguard Worker   std::string FullName = getFullyQualifiedName(Ty);
1582*9880d681SAndroid Build Coastguard Worker   TypeIndex FwdDeclTI =
1583*9880d681SAndroid Build Coastguard Worker       TypeTable.writeUnion(UnionRecord(0, CO, HfaKind::None, TypeIndex(), 0,
1584*9880d681SAndroid Build Coastguard Worker                                        FullName, Ty->getIdentifier()));
1585*9880d681SAndroid Build Coastguard Worker   if (!Ty->isForwardDecl())
1586*9880d681SAndroid Build Coastguard Worker     DeferredCompleteTypes.push_back(Ty);
1587*9880d681SAndroid Build Coastguard Worker   return FwdDeclTI;
1588*9880d681SAndroid Build Coastguard Worker }
1589*9880d681SAndroid Build Coastguard Worker 
lowerCompleteTypeUnion(const DICompositeType * Ty)1590*9880d681SAndroid Build Coastguard Worker TypeIndex CodeViewDebug::lowerCompleteTypeUnion(const DICompositeType *Ty) {
1591*9880d681SAndroid Build Coastguard Worker   ClassOptions CO = ClassOptions::Sealed | getCommonClassOptions(Ty);
1592*9880d681SAndroid Build Coastguard Worker   TypeIndex FieldTI;
1593*9880d681SAndroid Build Coastguard Worker   unsigned FieldCount;
1594*9880d681SAndroid Build Coastguard Worker   bool ContainsNestedClass;
1595*9880d681SAndroid Build Coastguard Worker   std::tie(FieldTI, std::ignore, FieldCount, ContainsNestedClass) =
1596*9880d681SAndroid Build Coastguard Worker       lowerRecordFieldList(Ty);
1597*9880d681SAndroid Build Coastguard Worker 
1598*9880d681SAndroid Build Coastguard Worker   if (ContainsNestedClass)
1599*9880d681SAndroid Build Coastguard Worker     CO |= ClassOptions::ContainsNestedClass;
1600*9880d681SAndroid Build Coastguard Worker 
1601*9880d681SAndroid Build Coastguard Worker   uint64_t SizeInBytes = Ty->getSizeInBits() / 8;
1602*9880d681SAndroid Build Coastguard Worker   std::string FullName = getFullyQualifiedName(Ty);
1603*9880d681SAndroid Build Coastguard Worker 
1604*9880d681SAndroid Build Coastguard Worker   TypeIndex UnionTI = TypeTable.writeUnion(
1605*9880d681SAndroid Build Coastguard Worker       UnionRecord(FieldCount, CO, HfaKind::None, FieldTI, SizeInBytes, FullName,
1606*9880d681SAndroid Build Coastguard Worker                   Ty->getIdentifier()));
1607*9880d681SAndroid Build Coastguard Worker 
1608*9880d681SAndroid Build Coastguard Worker   TypeTable.writeUdtSourceLine(UdtSourceLineRecord(
1609*9880d681SAndroid Build Coastguard Worker       UnionTI, TypeTable.writeStringId(StringIdRecord(
1610*9880d681SAndroid Build Coastguard Worker                    TypeIndex(0x0), getFullFilepath(Ty->getFile()))),
1611*9880d681SAndroid Build Coastguard Worker       Ty->getLine()));
1612*9880d681SAndroid Build Coastguard Worker 
1613*9880d681SAndroid Build Coastguard Worker   addToUDTs(Ty, UnionTI);
1614*9880d681SAndroid Build Coastguard Worker 
1615*9880d681SAndroid Build Coastguard Worker   return UnionTI;
1616*9880d681SAndroid Build Coastguard Worker }
1617*9880d681SAndroid Build Coastguard Worker 
1618*9880d681SAndroid Build Coastguard Worker std::tuple<TypeIndex, TypeIndex, unsigned, bool>
lowerRecordFieldList(const DICompositeType * Ty)1619*9880d681SAndroid Build Coastguard Worker CodeViewDebug::lowerRecordFieldList(const DICompositeType *Ty) {
1620*9880d681SAndroid Build Coastguard Worker   // Manually count members. MSVC appears to count everything that generates a
1621*9880d681SAndroid Build Coastguard Worker   // field list record. Each individual overload in a method overload group
1622*9880d681SAndroid Build Coastguard Worker   // contributes to this count, even though the overload group is a single field
1623*9880d681SAndroid Build Coastguard Worker   // list record.
1624*9880d681SAndroid Build Coastguard Worker   unsigned MemberCount = 0;
1625*9880d681SAndroid Build Coastguard Worker   ClassInfo Info = collectClassInfo(Ty);
1626*9880d681SAndroid Build Coastguard Worker   FieldListRecordBuilder Fields;
1627*9880d681SAndroid Build Coastguard Worker 
1628*9880d681SAndroid Build Coastguard Worker   // Create base classes.
1629*9880d681SAndroid Build Coastguard Worker   for (const DIDerivedType *I : Info.Inheritance) {
1630*9880d681SAndroid Build Coastguard Worker     if (I->getFlags() & DINode::FlagVirtual) {
1631*9880d681SAndroid Build Coastguard Worker       // Virtual base.
1632*9880d681SAndroid Build Coastguard Worker       // FIXME: Emit VBPtrOffset when the frontend provides it.
1633*9880d681SAndroid Build Coastguard Worker       unsigned VBPtrOffset = 0;
1634*9880d681SAndroid Build Coastguard Worker       // FIXME: Despite the accessor name, the offset is really in bytes.
1635*9880d681SAndroid Build Coastguard Worker       unsigned VBTableIndex = I->getOffsetInBits() / 4;
1636*9880d681SAndroid Build Coastguard Worker       Fields.writeVirtualBaseClass(VirtualBaseClassRecord(
1637*9880d681SAndroid Build Coastguard Worker           translateAccessFlags(Ty->getTag(), I->getFlags()),
1638*9880d681SAndroid Build Coastguard Worker           getTypeIndex(I->getBaseType()), getVBPTypeIndex(), VBPtrOffset,
1639*9880d681SAndroid Build Coastguard Worker           VBTableIndex));
1640*9880d681SAndroid Build Coastguard Worker     } else {
1641*9880d681SAndroid Build Coastguard Worker       assert(I->getOffsetInBits() % 8 == 0 &&
1642*9880d681SAndroid Build Coastguard Worker              "bases must be on byte boundaries");
1643*9880d681SAndroid Build Coastguard Worker       Fields.writeBaseClass(BaseClassRecord(
1644*9880d681SAndroid Build Coastguard Worker           translateAccessFlags(Ty->getTag(), I->getFlags()),
1645*9880d681SAndroid Build Coastguard Worker           getTypeIndex(I->getBaseType()), I->getOffsetInBits() / 8));
1646*9880d681SAndroid Build Coastguard Worker     }
1647*9880d681SAndroid Build Coastguard Worker   }
1648*9880d681SAndroid Build Coastguard Worker 
1649*9880d681SAndroid Build Coastguard Worker   // Create members.
1650*9880d681SAndroid Build Coastguard Worker   for (ClassInfo::MemberInfo &MemberInfo : Info.Members) {
1651*9880d681SAndroid Build Coastguard Worker     const DIDerivedType *Member = MemberInfo.MemberTypeNode;
1652*9880d681SAndroid Build Coastguard Worker     TypeIndex MemberBaseType = getTypeIndex(Member->getBaseType());
1653*9880d681SAndroid Build Coastguard Worker     StringRef MemberName = Member->getName();
1654*9880d681SAndroid Build Coastguard Worker     MemberAccess Access =
1655*9880d681SAndroid Build Coastguard Worker         translateAccessFlags(Ty->getTag(), Member->getFlags());
1656*9880d681SAndroid Build Coastguard Worker 
1657*9880d681SAndroid Build Coastguard Worker     if (Member->isStaticMember()) {
1658*9880d681SAndroid Build Coastguard Worker       Fields.writeStaticDataMember(
1659*9880d681SAndroid Build Coastguard Worker           StaticDataMemberRecord(Access, MemberBaseType, MemberName));
1660*9880d681SAndroid Build Coastguard Worker       MemberCount++;
1661*9880d681SAndroid Build Coastguard Worker       continue;
1662*9880d681SAndroid Build Coastguard Worker     }
1663*9880d681SAndroid Build Coastguard Worker 
1664*9880d681SAndroid Build Coastguard Worker     // Data member.
1665*9880d681SAndroid Build Coastguard Worker     uint64_t MemberOffsetInBits =
1666*9880d681SAndroid Build Coastguard Worker         Member->getOffsetInBits() + MemberInfo.BaseOffset;
1667*9880d681SAndroid Build Coastguard Worker     if (Member->isBitField()) {
1668*9880d681SAndroid Build Coastguard Worker       uint64_t StartBitOffset = MemberOffsetInBits;
1669*9880d681SAndroid Build Coastguard Worker       if (const auto *CI =
1670*9880d681SAndroid Build Coastguard Worker               dyn_cast_or_null<ConstantInt>(Member->getStorageOffsetInBits())) {
1671*9880d681SAndroid Build Coastguard Worker         MemberOffsetInBits = CI->getZExtValue() + MemberInfo.BaseOffset;
1672*9880d681SAndroid Build Coastguard Worker       }
1673*9880d681SAndroid Build Coastguard Worker       StartBitOffset -= MemberOffsetInBits;
1674*9880d681SAndroid Build Coastguard Worker       MemberBaseType = TypeTable.writeBitField(BitFieldRecord(
1675*9880d681SAndroid Build Coastguard Worker           MemberBaseType, Member->getSizeInBits(), StartBitOffset));
1676*9880d681SAndroid Build Coastguard Worker     }
1677*9880d681SAndroid Build Coastguard Worker     uint64_t MemberOffsetInBytes = MemberOffsetInBits / 8;
1678*9880d681SAndroid Build Coastguard Worker     Fields.writeDataMember(DataMemberRecord(Access, MemberBaseType,
1679*9880d681SAndroid Build Coastguard Worker                                             MemberOffsetInBytes, MemberName));
1680*9880d681SAndroid Build Coastguard Worker     MemberCount++;
1681*9880d681SAndroid Build Coastguard Worker   }
1682*9880d681SAndroid Build Coastguard Worker 
1683*9880d681SAndroid Build Coastguard Worker   // Create methods
1684*9880d681SAndroid Build Coastguard Worker   for (auto &MethodItr : Info.Methods) {
1685*9880d681SAndroid Build Coastguard Worker     StringRef Name = MethodItr.first->getString();
1686*9880d681SAndroid Build Coastguard Worker 
1687*9880d681SAndroid Build Coastguard Worker     std::vector<OneMethodRecord> Methods;
1688*9880d681SAndroid Build Coastguard Worker     for (const DISubprogram *SP : MethodItr.second) {
1689*9880d681SAndroid Build Coastguard Worker       TypeIndex MethodType = getMemberFunctionType(SP, Ty);
1690*9880d681SAndroid Build Coastguard Worker       bool Introduced = SP->getFlags() & DINode::FlagIntroducedVirtual;
1691*9880d681SAndroid Build Coastguard Worker 
1692*9880d681SAndroid Build Coastguard Worker       unsigned VFTableOffset = -1;
1693*9880d681SAndroid Build Coastguard Worker       if (Introduced)
1694*9880d681SAndroid Build Coastguard Worker         VFTableOffset = SP->getVirtualIndex() * getPointerSizeInBytes();
1695*9880d681SAndroid Build Coastguard Worker 
1696*9880d681SAndroid Build Coastguard Worker       Methods.push_back(
1697*9880d681SAndroid Build Coastguard Worker           OneMethodRecord(MethodType, translateMethodKindFlags(SP, Introduced),
1698*9880d681SAndroid Build Coastguard Worker                           translateMethodOptionFlags(SP),
1699*9880d681SAndroid Build Coastguard Worker                           translateAccessFlags(Ty->getTag(), SP->getFlags()),
1700*9880d681SAndroid Build Coastguard Worker                           VFTableOffset, Name));
1701*9880d681SAndroid Build Coastguard Worker       MemberCount++;
1702*9880d681SAndroid Build Coastguard Worker     }
1703*9880d681SAndroid Build Coastguard Worker     assert(Methods.size() > 0 && "Empty methods map entry");
1704*9880d681SAndroid Build Coastguard Worker     if (Methods.size() == 1)
1705*9880d681SAndroid Build Coastguard Worker       Fields.writeOneMethod(Methods[0]);
1706*9880d681SAndroid Build Coastguard Worker     else {
1707*9880d681SAndroid Build Coastguard Worker       TypeIndex MethodList =
1708*9880d681SAndroid Build Coastguard Worker           TypeTable.writeMethodOverloadList(MethodOverloadListRecord(Methods));
1709*9880d681SAndroid Build Coastguard Worker       Fields.writeOverloadedMethod(
1710*9880d681SAndroid Build Coastguard Worker           OverloadedMethodRecord(Methods.size(), MethodList, Name));
1711*9880d681SAndroid Build Coastguard Worker     }
1712*9880d681SAndroid Build Coastguard Worker   }
1713*9880d681SAndroid Build Coastguard Worker 
1714*9880d681SAndroid Build Coastguard Worker   // Create nested classes.
1715*9880d681SAndroid Build Coastguard Worker   for (const DICompositeType *Nested : Info.NestedClasses) {
1716*9880d681SAndroid Build Coastguard Worker     NestedTypeRecord R(getTypeIndex(DITypeRef(Nested)), Nested->getName());
1717*9880d681SAndroid Build Coastguard Worker     Fields.writeNestedType(R);
1718*9880d681SAndroid Build Coastguard Worker     MemberCount++;
1719*9880d681SAndroid Build Coastguard Worker   }
1720*9880d681SAndroid Build Coastguard Worker 
1721*9880d681SAndroid Build Coastguard Worker   TypeIndex FieldTI = TypeTable.writeFieldList(Fields);
1722*9880d681SAndroid Build Coastguard Worker   return std::make_tuple(FieldTI, TypeIndex(), MemberCount,
1723*9880d681SAndroid Build Coastguard Worker                          !Info.NestedClasses.empty());
1724*9880d681SAndroid Build Coastguard Worker }
1725*9880d681SAndroid Build Coastguard Worker 
getVBPTypeIndex()1726*9880d681SAndroid Build Coastguard Worker TypeIndex CodeViewDebug::getVBPTypeIndex() {
1727*9880d681SAndroid Build Coastguard Worker   if (!VBPType.getIndex()) {
1728*9880d681SAndroid Build Coastguard Worker     // Make a 'const int *' type.
1729*9880d681SAndroid Build Coastguard Worker     ModifierRecord MR(TypeIndex::Int32(), ModifierOptions::Const);
1730*9880d681SAndroid Build Coastguard Worker     TypeIndex ModifiedTI = TypeTable.writeModifier(MR);
1731*9880d681SAndroid Build Coastguard Worker 
1732*9880d681SAndroid Build Coastguard Worker     PointerKind PK = getPointerSizeInBytes() == 8 ? PointerKind::Near64
1733*9880d681SAndroid Build Coastguard Worker                                                   : PointerKind::Near32;
1734*9880d681SAndroid Build Coastguard Worker     PointerMode PM = PointerMode::Pointer;
1735*9880d681SAndroid Build Coastguard Worker     PointerOptions PO = PointerOptions::None;
1736*9880d681SAndroid Build Coastguard Worker     PointerRecord PR(ModifiedTI, PK, PM, PO, getPointerSizeInBytes());
1737*9880d681SAndroid Build Coastguard Worker 
1738*9880d681SAndroid Build Coastguard Worker     VBPType = TypeTable.writePointer(PR);
1739*9880d681SAndroid Build Coastguard Worker   }
1740*9880d681SAndroid Build Coastguard Worker 
1741*9880d681SAndroid Build Coastguard Worker   return VBPType;
1742*9880d681SAndroid Build Coastguard Worker }
1743*9880d681SAndroid Build Coastguard Worker 
getTypeIndex(DITypeRef TypeRef,DITypeRef ClassTyRef)1744*9880d681SAndroid Build Coastguard Worker TypeIndex CodeViewDebug::getTypeIndex(DITypeRef TypeRef, DITypeRef ClassTyRef) {
1745*9880d681SAndroid Build Coastguard Worker   const DIType *Ty = TypeRef.resolve();
1746*9880d681SAndroid Build Coastguard Worker   const DIType *ClassTy = ClassTyRef.resolve();
1747*9880d681SAndroid Build Coastguard Worker 
1748*9880d681SAndroid Build Coastguard Worker   // The null DIType is the void type. Don't try to hash it.
1749*9880d681SAndroid Build Coastguard Worker   if (!Ty)
1750*9880d681SAndroid Build Coastguard Worker     return TypeIndex::Void();
1751*9880d681SAndroid Build Coastguard Worker 
1752*9880d681SAndroid Build Coastguard Worker   // Check if we've already translated this type. Don't try to do a
1753*9880d681SAndroid Build Coastguard Worker   // get-or-create style insertion that caches the hash lookup across the
1754*9880d681SAndroid Build Coastguard Worker   // lowerType call. It will update the TypeIndices map.
1755*9880d681SAndroid Build Coastguard Worker   auto I = TypeIndices.find({Ty, ClassTy});
1756*9880d681SAndroid Build Coastguard Worker   if (I != TypeIndices.end())
1757*9880d681SAndroid Build Coastguard Worker     return I->second;
1758*9880d681SAndroid Build Coastguard Worker 
1759*9880d681SAndroid Build Coastguard Worker   TypeLoweringScope S(*this);
1760*9880d681SAndroid Build Coastguard Worker   TypeIndex TI = lowerType(Ty, ClassTy);
1761*9880d681SAndroid Build Coastguard Worker   return recordTypeIndexForDINode(Ty, TI, ClassTy);
1762*9880d681SAndroid Build Coastguard Worker }
1763*9880d681SAndroid Build Coastguard Worker 
getCompleteTypeIndex(DITypeRef TypeRef)1764*9880d681SAndroid Build Coastguard Worker TypeIndex CodeViewDebug::getCompleteTypeIndex(DITypeRef TypeRef) {
1765*9880d681SAndroid Build Coastguard Worker   const DIType *Ty = TypeRef.resolve();
1766*9880d681SAndroid Build Coastguard Worker 
1767*9880d681SAndroid Build Coastguard Worker   // The null DIType is the void type. Don't try to hash it.
1768*9880d681SAndroid Build Coastguard Worker   if (!Ty)
1769*9880d681SAndroid Build Coastguard Worker     return TypeIndex::Void();
1770*9880d681SAndroid Build Coastguard Worker 
1771*9880d681SAndroid Build Coastguard Worker   // If this is a non-record type, the complete type index is the same as the
1772*9880d681SAndroid Build Coastguard Worker   // normal type index. Just call getTypeIndex.
1773*9880d681SAndroid Build Coastguard Worker   switch (Ty->getTag()) {
1774*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_TAG_class_type:
1775*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_TAG_structure_type:
1776*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_TAG_union_type:
1777*9880d681SAndroid Build Coastguard Worker     break;
1778*9880d681SAndroid Build Coastguard Worker   default:
1779*9880d681SAndroid Build Coastguard Worker     return getTypeIndex(Ty);
1780*9880d681SAndroid Build Coastguard Worker   }
1781*9880d681SAndroid Build Coastguard Worker 
1782*9880d681SAndroid Build Coastguard Worker   // Check if we've already translated the complete record type.  Lowering a
1783*9880d681SAndroid Build Coastguard Worker   // complete type should never trigger lowering another complete type, so we
1784*9880d681SAndroid Build Coastguard Worker   // can reuse the hash table lookup result.
1785*9880d681SAndroid Build Coastguard Worker   const auto *CTy = cast<DICompositeType>(Ty);
1786*9880d681SAndroid Build Coastguard Worker   auto InsertResult = CompleteTypeIndices.insert({CTy, TypeIndex()});
1787*9880d681SAndroid Build Coastguard Worker   if (!InsertResult.second)
1788*9880d681SAndroid Build Coastguard Worker     return InsertResult.first->second;
1789*9880d681SAndroid Build Coastguard Worker 
1790*9880d681SAndroid Build Coastguard Worker   TypeLoweringScope S(*this);
1791*9880d681SAndroid Build Coastguard Worker 
1792*9880d681SAndroid Build Coastguard Worker   // Make sure the forward declaration is emitted first. It's unclear if this
1793*9880d681SAndroid Build Coastguard Worker   // is necessary, but MSVC does it, and we should follow suit until we can show
1794*9880d681SAndroid Build Coastguard Worker   // otherwise.
1795*9880d681SAndroid Build Coastguard Worker   TypeIndex FwdDeclTI = getTypeIndex(CTy);
1796*9880d681SAndroid Build Coastguard Worker 
1797*9880d681SAndroid Build Coastguard Worker   // Just use the forward decl if we don't have complete type info. This might
1798*9880d681SAndroid Build Coastguard Worker   // happen if the frontend is using modules and expects the complete definition
1799*9880d681SAndroid Build Coastguard Worker   // to be emitted elsewhere.
1800*9880d681SAndroid Build Coastguard Worker   if (CTy->isForwardDecl())
1801*9880d681SAndroid Build Coastguard Worker     return FwdDeclTI;
1802*9880d681SAndroid Build Coastguard Worker 
1803*9880d681SAndroid Build Coastguard Worker   TypeIndex TI;
1804*9880d681SAndroid Build Coastguard Worker   switch (CTy->getTag()) {
1805*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_TAG_class_type:
1806*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_TAG_structure_type:
1807*9880d681SAndroid Build Coastguard Worker     TI = lowerCompleteTypeClass(CTy);
1808*9880d681SAndroid Build Coastguard Worker     break;
1809*9880d681SAndroid Build Coastguard Worker   case dwarf::DW_TAG_union_type:
1810*9880d681SAndroid Build Coastguard Worker     TI = lowerCompleteTypeUnion(CTy);
1811*9880d681SAndroid Build Coastguard Worker     break;
1812*9880d681SAndroid Build Coastguard Worker   default:
1813*9880d681SAndroid Build Coastguard Worker     llvm_unreachable("not a record");
1814*9880d681SAndroid Build Coastguard Worker   }
1815*9880d681SAndroid Build Coastguard Worker 
1816*9880d681SAndroid Build Coastguard Worker   InsertResult.first->second = TI;
1817*9880d681SAndroid Build Coastguard Worker   return TI;
1818*9880d681SAndroid Build Coastguard Worker }
1819*9880d681SAndroid Build Coastguard Worker 
1820*9880d681SAndroid Build Coastguard Worker /// Emit all the deferred complete record types. Try to do this in FIFO order,
1821*9880d681SAndroid Build Coastguard Worker /// and do this until fixpoint, as each complete record type typically
1822*9880d681SAndroid Build Coastguard Worker /// references
1823*9880d681SAndroid Build Coastguard Worker /// many other record types.
emitDeferredCompleteTypes()1824*9880d681SAndroid Build Coastguard Worker void CodeViewDebug::emitDeferredCompleteTypes() {
1825*9880d681SAndroid Build Coastguard Worker   SmallVector<const DICompositeType *, 4> TypesToEmit;
1826*9880d681SAndroid Build Coastguard Worker   while (!DeferredCompleteTypes.empty()) {
1827*9880d681SAndroid Build Coastguard Worker     std::swap(DeferredCompleteTypes, TypesToEmit);
1828*9880d681SAndroid Build Coastguard Worker     for (const DICompositeType *RecordTy : TypesToEmit)
1829*9880d681SAndroid Build Coastguard Worker       getCompleteTypeIndex(RecordTy);
1830*9880d681SAndroid Build Coastguard Worker     TypesToEmit.clear();
1831*9880d681SAndroid Build Coastguard Worker   }
1832*9880d681SAndroid Build Coastguard Worker }
1833*9880d681SAndroid Build Coastguard Worker 
emitLocalVariableList(ArrayRef<LocalVariable> Locals)1834*9880d681SAndroid Build Coastguard Worker void CodeViewDebug::emitLocalVariableList(ArrayRef<LocalVariable> Locals) {
1835*9880d681SAndroid Build Coastguard Worker   // Get the sorted list of parameters and emit them first.
1836*9880d681SAndroid Build Coastguard Worker   SmallVector<const LocalVariable *, 6> Params;
1837*9880d681SAndroid Build Coastguard Worker   for (const LocalVariable &L : Locals)
1838*9880d681SAndroid Build Coastguard Worker     if (L.DIVar->isParameter())
1839*9880d681SAndroid Build Coastguard Worker       Params.push_back(&L);
1840*9880d681SAndroid Build Coastguard Worker   std::sort(Params.begin(), Params.end(),
1841*9880d681SAndroid Build Coastguard Worker             [](const LocalVariable *L, const LocalVariable *R) {
1842*9880d681SAndroid Build Coastguard Worker               return L->DIVar->getArg() < R->DIVar->getArg();
1843*9880d681SAndroid Build Coastguard Worker             });
1844*9880d681SAndroid Build Coastguard Worker   for (const LocalVariable *L : Params)
1845*9880d681SAndroid Build Coastguard Worker     emitLocalVariable(*L);
1846*9880d681SAndroid Build Coastguard Worker 
1847*9880d681SAndroid Build Coastguard Worker   // Next emit all non-parameters in the order that we found them.
1848*9880d681SAndroid Build Coastguard Worker   for (const LocalVariable &L : Locals)
1849*9880d681SAndroid Build Coastguard Worker     if (!L.DIVar->isParameter())
1850*9880d681SAndroid Build Coastguard Worker       emitLocalVariable(L);
1851*9880d681SAndroid Build Coastguard Worker }
1852*9880d681SAndroid Build Coastguard Worker 
emitLocalVariable(const LocalVariable & Var)1853*9880d681SAndroid Build Coastguard Worker void CodeViewDebug::emitLocalVariable(const LocalVariable &Var) {
1854*9880d681SAndroid Build Coastguard Worker   // LocalSym record, see SymbolRecord.h for more info.
1855*9880d681SAndroid Build Coastguard Worker   MCSymbol *LocalBegin = MMI->getContext().createTempSymbol(),
1856*9880d681SAndroid Build Coastguard Worker            *LocalEnd = MMI->getContext().createTempSymbol();
1857*9880d681SAndroid Build Coastguard Worker   OS.AddComment("Record length");
1858*9880d681SAndroid Build Coastguard Worker   OS.emitAbsoluteSymbolDiff(LocalEnd, LocalBegin, 2);
1859*9880d681SAndroid Build Coastguard Worker   OS.EmitLabel(LocalBegin);
1860*9880d681SAndroid Build Coastguard Worker 
1861*9880d681SAndroid Build Coastguard Worker   OS.AddComment("Record kind: S_LOCAL");
1862*9880d681SAndroid Build Coastguard Worker   OS.EmitIntValue(unsigned(SymbolKind::S_LOCAL), 2);
1863*9880d681SAndroid Build Coastguard Worker 
1864*9880d681SAndroid Build Coastguard Worker   LocalSymFlags Flags = LocalSymFlags::None;
1865*9880d681SAndroid Build Coastguard Worker   if (Var.DIVar->isParameter())
1866*9880d681SAndroid Build Coastguard Worker     Flags |= LocalSymFlags::IsParameter;
1867*9880d681SAndroid Build Coastguard Worker   if (Var.DefRanges.empty())
1868*9880d681SAndroid Build Coastguard Worker     Flags |= LocalSymFlags::IsOptimizedOut;
1869*9880d681SAndroid Build Coastguard Worker 
1870*9880d681SAndroid Build Coastguard Worker   OS.AddComment("TypeIndex");
1871*9880d681SAndroid Build Coastguard Worker   TypeIndex TI = getCompleteTypeIndex(Var.DIVar->getType());
1872*9880d681SAndroid Build Coastguard Worker   OS.EmitIntValue(TI.getIndex(), 4);
1873*9880d681SAndroid Build Coastguard Worker   OS.AddComment("Flags");
1874*9880d681SAndroid Build Coastguard Worker   OS.EmitIntValue(static_cast<uint16_t>(Flags), 2);
1875*9880d681SAndroid Build Coastguard Worker   // Truncate the name so we won't overflow the record length field.
1876*9880d681SAndroid Build Coastguard Worker   emitNullTerminatedSymbolName(OS, Var.DIVar->getName());
1877*9880d681SAndroid Build Coastguard Worker   OS.EmitLabel(LocalEnd);
1878*9880d681SAndroid Build Coastguard Worker 
1879*9880d681SAndroid Build Coastguard Worker   // Calculate the on disk prefix of the appropriate def range record. The
1880*9880d681SAndroid Build Coastguard Worker   // records and on disk formats are described in SymbolRecords.h. BytePrefix
1881*9880d681SAndroid Build Coastguard Worker   // should be big enough to hold all forms without memory allocation.
1882*9880d681SAndroid Build Coastguard Worker   SmallString<20> BytePrefix;
1883*9880d681SAndroid Build Coastguard Worker   for (const LocalVarDefRange &DefRange : Var.DefRanges) {
1884*9880d681SAndroid Build Coastguard Worker     BytePrefix.clear();
1885*9880d681SAndroid Build Coastguard Worker     // FIXME: Handle bitpieces.
1886*9880d681SAndroid Build Coastguard Worker     if (DefRange.StructOffset != 0)
1887*9880d681SAndroid Build Coastguard Worker       continue;
1888*9880d681SAndroid Build Coastguard Worker 
1889*9880d681SAndroid Build Coastguard Worker     if (DefRange.InMemory) {
1890*9880d681SAndroid Build Coastguard Worker       DefRangeRegisterRelSym Sym(DefRange.CVRegister, 0, DefRange.DataOffset, 0,
1891*9880d681SAndroid Build Coastguard Worker                                  0, 0, ArrayRef<LocalVariableAddrGap>());
1892*9880d681SAndroid Build Coastguard Worker       ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER_REL);
1893*9880d681SAndroid Build Coastguard Worker       BytePrefix +=
1894*9880d681SAndroid Build Coastguard Worker           StringRef(reinterpret_cast<const char *>(&SymKind), sizeof(SymKind));
1895*9880d681SAndroid Build Coastguard Worker       BytePrefix +=
1896*9880d681SAndroid Build Coastguard Worker           StringRef(reinterpret_cast<const char *>(&Sym.Header),
1897*9880d681SAndroid Build Coastguard Worker                     sizeof(Sym.Header) - sizeof(LocalVariableAddrRange));
1898*9880d681SAndroid Build Coastguard Worker     } else {
1899*9880d681SAndroid Build Coastguard Worker       assert(DefRange.DataOffset == 0 && "unexpected offset into register");
1900*9880d681SAndroid Build Coastguard Worker       // Unclear what matters here.
1901*9880d681SAndroid Build Coastguard Worker       DefRangeRegisterSym Sym(DefRange.CVRegister, 0, 0, 0, 0,
1902*9880d681SAndroid Build Coastguard Worker                               ArrayRef<LocalVariableAddrGap>());
1903*9880d681SAndroid Build Coastguard Worker       ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER);
1904*9880d681SAndroid Build Coastguard Worker       BytePrefix +=
1905*9880d681SAndroid Build Coastguard Worker           StringRef(reinterpret_cast<const char *>(&SymKind), sizeof(SymKind));
1906*9880d681SAndroid Build Coastguard Worker       BytePrefix +=
1907*9880d681SAndroid Build Coastguard Worker           StringRef(reinterpret_cast<const char *>(&Sym.Header),
1908*9880d681SAndroid Build Coastguard Worker                     sizeof(Sym.Header) - sizeof(LocalVariableAddrRange));
1909*9880d681SAndroid Build Coastguard Worker     }
1910*9880d681SAndroid Build Coastguard Worker     OS.EmitCVDefRangeDirective(DefRange.Ranges, BytePrefix);
1911*9880d681SAndroid Build Coastguard Worker   }
1912*9880d681SAndroid Build Coastguard Worker }
1913*9880d681SAndroid Build Coastguard Worker 
endFunction(const MachineFunction * MF)1914*9880d681SAndroid Build Coastguard Worker void CodeViewDebug::endFunction(const MachineFunction *MF) {
1915*9880d681SAndroid Build Coastguard Worker   if (!Asm || !CurFn)  // We haven't created any debug info for this function.
1916*9880d681SAndroid Build Coastguard Worker     return;
1917*9880d681SAndroid Build Coastguard Worker 
1918*9880d681SAndroid Build Coastguard Worker   const Function *GV = MF->getFunction();
1919*9880d681SAndroid Build Coastguard Worker   assert(FnDebugInfo.count(GV));
1920*9880d681SAndroid Build Coastguard Worker   assert(CurFn == &FnDebugInfo[GV]);
1921*9880d681SAndroid Build Coastguard Worker 
1922*9880d681SAndroid Build Coastguard Worker   collectVariableInfo(GV->getSubprogram());
1923*9880d681SAndroid Build Coastguard Worker 
1924*9880d681SAndroid Build Coastguard Worker   DebugHandlerBase::endFunction(MF);
1925*9880d681SAndroid Build Coastguard Worker 
1926*9880d681SAndroid Build Coastguard Worker   // Don't emit anything if we don't have any line tables.
1927*9880d681SAndroid Build Coastguard Worker   if (!CurFn->HaveLineInfo) {
1928*9880d681SAndroid Build Coastguard Worker     FnDebugInfo.erase(GV);
1929*9880d681SAndroid Build Coastguard Worker     CurFn = nullptr;
1930*9880d681SAndroid Build Coastguard Worker     return;
1931*9880d681SAndroid Build Coastguard Worker   }
1932*9880d681SAndroid Build Coastguard Worker 
1933*9880d681SAndroid Build Coastguard Worker   CurFn->End = Asm->getFunctionEnd();
1934*9880d681SAndroid Build Coastguard Worker 
1935*9880d681SAndroid Build Coastguard Worker   CurFn = nullptr;
1936*9880d681SAndroid Build Coastguard Worker }
1937*9880d681SAndroid Build Coastguard Worker 
beginInstruction(const MachineInstr * MI)1938*9880d681SAndroid Build Coastguard Worker void CodeViewDebug::beginInstruction(const MachineInstr *MI) {
1939*9880d681SAndroid Build Coastguard Worker   DebugHandlerBase::beginInstruction(MI);
1940*9880d681SAndroid Build Coastguard Worker 
1941*9880d681SAndroid Build Coastguard Worker   // Ignore DBG_VALUE locations and function prologue.
1942*9880d681SAndroid Build Coastguard Worker   if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup))
1943*9880d681SAndroid Build Coastguard Worker     return;
1944*9880d681SAndroid Build Coastguard Worker   DebugLoc DL = MI->getDebugLoc();
1945*9880d681SAndroid Build Coastguard Worker   if (DL == PrevInstLoc || !DL)
1946*9880d681SAndroid Build Coastguard Worker     return;
1947*9880d681SAndroid Build Coastguard Worker   maybeRecordLocation(DL, Asm->MF);
1948*9880d681SAndroid Build Coastguard Worker }
1949*9880d681SAndroid Build Coastguard Worker 
beginCVSubsection(ModuleSubstreamKind Kind)1950*9880d681SAndroid Build Coastguard Worker MCSymbol *CodeViewDebug::beginCVSubsection(ModuleSubstreamKind Kind) {
1951*9880d681SAndroid Build Coastguard Worker   MCSymbol *BeginLabel = MMI->getContext().createTempSymbol(),
1952*9880d681SAndroid Build Coastguard Worker            *EndLabel = MMI->getContext().createTempSymbol();
1953*9880d681SAndroid Build Coastguard Worker   OS.EmitIntValue(unsigned(Kind), 4);
1954*9880d681SAndroid Build Coastguard Worker   OS.AddComment("Subsection size");
1955*9880d681SAndroid Build Coastguard Worker   OS.emitAbsoluteSymbolDiff(EndLabel, BeginLabel, 4);
1956*9880d681SAndroid Build Coastguard Worker   OS.EmitLabel(BeginLabel);
1957*9880d681SAndroid Build Coastguard Worker   return EndLabel;
1958*9880d681SAndroid Build Coastguard Worker }
1959*9880d681SAndroid Build Coastguard Worker 
endCVSubsection(MCSymbol * EndLabel)1960*9880d681SAndroid Build Coastguard Worker void CodeViewDebug::endCVSubsection(MCSymbol *EndLabel) {
1961*9880d681SAndroid Build Coastguard Worker   OS.EmitLabel(EndLabel);
1962*9880d681SAndroid Build Coastguard Worker   // Every subsection must be aligned to a 4-byte boundary.
1963*9880d681SAndroid Build Coastguard Worker   OS.EmitValueToAlignment(4);
1964*9880d681SAndroid Build Coastguard Worker }
1965*9880d681SAndroid Build Coastguard Worker 
emitDebugInfoForUDTs(ArrayRef<std::pair<std::string,TypeIndex>> UDTs)1966*9880d681SAndroid Build Coastguard Worker void CodeViewDebug::emitDebugInfoForUDTs(
1967*9880d681SAndroid Build Coastguard Worker     ArrayRef<std::pair<std::string, TypeIndex>> UDTs) {
1968*9880d681SAndroid Build Coastguard Worker   for (const std::pair<std::string, codeview::TypeIndex> &UDT : UDTs) {
1969*9880d681SAndroid Build Coastguard Worker     MCSymbol *UDTRecordBegin = MMI->getContext().createTempSymbol(),
1970*9880d681SAndroid Build Coastguard Worker              *UDTRecordEnd = MMI->getContext().createTempSymbol();
1971*9880d681SAndroid Build Coastguard Worker     OS.AddComment("Record length");
1972*9880d681SAndroid Build Coastguard Worker     OS.emitAbsoluteSymbolDiff(UDTRecordEnd, UDTRecordBegin, 2);
1973*9880d681SAndroid Build Coastguard Worker     OS.EmitLabel(UDTRecordBegin);
1974*9880d681SAndroid Build Coastguard Worker 
1975*9880d681SAndroid Build Coastguard Worker     OS.AddComment("Record kind: S_UDT");
1976*9880d681SAndroid Build Coastguard Worker     OS.EmitIntValue(unsigned(SymbolKind::S_UDT), 2);
1977*9880d681SAndroid Build Coastguard Worker 
1978*9880d681SAndroid Build Coastguard Worker     OS.AddComment("Type");
1979*9880d681SAndroid Build Coastguard Worker     OS.EmitIntValue(UDT.second.getIndex(), 4);
1980*9880d681SAndroid Build Coastguard Worker 
1981*9880d681SAndroid Build Coastguard Worker     emitNullTerminatedSymbolName(OS, UDT.first);
1982*9880d681SAndroid Build Coastguard Worker     OS.EmitLabel(UDTRecordEnd);
1983*9880d681SAndroid Build Coastguard Worker   }
1984*9880d681SAndroid Build Coastguard Worker }
1985*9880d681SAndroid Build Coastguard Worker 
emitDebugInfoForGlobals()1986*9880d681SAndroid Build Coastguard Worker void CodeViewDebug::emitDebugInfoForGlobals() {
1987*9880d681SAndroid Build Coastguard Worker   NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
1988*9880d681SAndroid Build Coastguard Worker   for (const MDNode *Node : CUs->operands()) {
1989*9880d681SAndroid Build Coastguard Worker     const auto *CU = cast<DICompileUnit>(Node);
1990*9880d681SAndroid Build Coastguard Worker 
1991*9880d681SAndroid Build Coastguard Worker     // First, emit all globals that are not in a comdat in a single symbol
1992*9880d681SAndroid Build Coastguard Worker     // substream. MSVC doesn't like it if the substream is empty, so only open
1993*9880d681SAndroid Build Coastguard Worker     // it if we have at least one global to emit.
1994*9880d681SAndroid Build Coastguard Worker     switchToDebugSectionForSymbol(nullptr);
1995*9880d681SAndroid Build Coastguard Worker     MCSymbol *EndLabel = nullptr;
1996*9880d681SAndroid Build Coastguard Worker     for (const DIGlobalVariable *G : CU->getGlobalVariables()) {
1997*9880d681SAndroid Build Coastguard Worker       if (const auto *GV = dyn_cast_or_null<GlobalVariable>(G->getVariable())) {
1998*9880d681SAndroid Build Coastguard Worker         if (!GV->hasComdat() && !GV->isDeclarationForLinker()) {
1999*9880d681SAndroid Build Coastguard Worker           if (!EndLabel) {
2000*9880d681SAndroid Build Coastguard Worker             OS.AddComment("Symbol subsection for globals");
2001*9880d681SAndroid Build Coastguard Worker             EndLabel = beginCVSubsection(ModuleSubstreamKind::Symbols);
2002*9880d681SAndroid Build Coastguard Worker           }
2003*9880d681SAndroid Build Coastguard Worker           emitDebugInfoForGlobal(G, Asm->getSymbol(GV));
2004*9880d681SAndroid Build Coastguard Worker         }
2005*9880d681SAndroid Build Coastguard Worker       }
2006*9880d681SAndroid Build Coastguard Worker     }
2007*9880d681SAndroid Build Coastguard Worker     if (EndLabel)
2008*9880d681SAndroid Build Coastguard Worker       endCVSubsection(EndLabel);
2009*9880d681SAndroid Build Coastguard Worker 
2010*9880d681SAndroid Build Coastguard Worker     // Second, emit each global that is in a comdat into its own .debug$S
2011*9880d681SAndroid Build Coastguard Worker     // section along with its own symbol substream.
2012*9880d681SAndroid Build Coastguard Worker     for (const DIGlobalVariable *G : CU->getGlobalVariables()) {
2013*9880d681SAndroid Build Coastguard Worker       if (const auto *GV = dyn_cast_or_null<GlobalVariable>(G->getVariable())) {
2014*9880d681SAndroid Build Coastguard Worker         if (GV->hasComdat()) {
2015*9880d681SAndroid Build Coastguard Worker           MCSymbol *GVSym = Asm->getSymbol(GV);
2016*9880d681SAndroid Build Coastguard Worker           OS.AddComment("Symbol subsection for " +
2017*9880d681SAndroid Build Coastguard Worker                         Twine(GlobalValue::getRealLinkageName(GV->getName())));
2018*9880d681SAndroid Build Coastguard Worker           switchToDebugSectionForSymbol(GVSym);
2019*9880d681SAndroid Build Coastguard Worker           EndLabel = beginCVSubsection(ModuleSubstreamKind::Symbols);
2020*9880d681SAndroid Build Coastguard Worker           emitDebugInfoForGlobal(G, GVSym);
2021*9880d681SAndroid Build Coastguard Worker           endCVSubsection(EndLabel);
2022*9880d681SAndroid Build Coastguard Worker         }
2023*9880d681SAndroid Build Coastguard Worker       }
2024*9880d681SAndroid Build Coastguard Worker     }
2025*9880d681SAndroid Build Coastguard Worker   }
2026*9880d681SAndroid Build Coastguard Worker }
2027*9880d681SAndroid Build Coastguard Worker 
emitDebugInfoForRetainedTypes()2028*9880d681SAndroid Build Coastguard Worker void CodeViewDebug::emitDebugInfoForRetainedTypes() {
2029*9880d681SAndroid Build Coastguard Worker   NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
2030*9880d681SAndroid Build Coastguard Worker   for (const MDNode *Node : CUs->operands()) {
2031*9880d681SAndroid Build Coastguard Worker     for (auto *Ty : cast<DICompileUnit>(Node)->getRetainedTypes()) {
2032*9880d681SAndroid Build Coastguard Worker       if (DIType *RT = dyn_cast<DIType>(Ty)) {
2033*9880d681SAndroid Build Coastguard Worker         getTypeIndex(RT);
2034*9880d681SAndroid Build Coastguard Worker         // FIXME: Add to global/local DTU list.
2035*9880d681SAndroid Build Coastguard Worker       }
2036*9880d681SAndroid Build Coastguard Worker     }
2037*9880d681SAndroid Build Coastguard Worker   }
2038*9880d681SAndroid Build Coastguard Worker }
2039*9880d681SAndroid Build Coastguard Worker 
emitDebugInfoForGlobal(const DIGlobalVariable * DIGV,MCSymbol * GVSym)2040*9880d681SAndroid Build Coastguard Worker void CodeViewDebug::emitDebugInfoForGlobal(const DIGlobalVariable *DIGV,
2041*9880d681SAndroid Build Coastguard Worker                                            MCSymbol *GVSym) {
2042*9880d681SAndroid Build Coastguard Worker   // DataSym record, see SymbolRecord.h for more info.
2043*9880d681SAndroid Build Coastguard Worker   // FIXME: Thread local data, etc
2044*9880d681SAndroid Build Coastguard Worker   MCSymbol *DataBegin = MMI->getContext().createTempSymbol(),
2045*9880d681SAndroid Build Coastguard Worker            *DataEnd = MMI->getContext().createTempSymbol();
2046*9880d681SAndroid Build Coastguard Worker   OS.AddComment("Record length");
2047*9880d681SAndroid Build Coastguard Worker   OS.emitAbsoluteSymbolDiff(DataEnd, DataBegin, 2);
2048*9880d681SAndroid Build Coastguard Worker   OS.EmitLabel(DataBegin);
2049*9880d681SAndroid Build Coastguard Worker   const auto *GV = cast<GlobalVariable>(DIGV->getVariable());
2050*9880d681SAndroid Build Coastguard Worker   if (DIGV->isLocalToUnit()) {
2051*9880d681SAndroid Build Coastguard Worker     if (GV->isThreadLocal()) {
2052*9880d681SAndroid Build Coastguard Worker       OS.AddComment("Record kind: S_LTHREAD32");
2053*9880d681SAndroid Build Coastguard Worker       OS.EmitIntValue(unsigned(SymbolKind::S_LTHREAD32), 2);
2054*9880d681SAndroid Build Coastguard Worker     } else {
2055*9880d681SAndroid Build Coastguard Worker       OS.AddComment("Record kind: S_LDATA32");
2056*9880d681SAndroid Build Coastguard Worker       OS.EmitIntValue(unsigned(SymbolKind::S_LDATA32), 2);
2057*9880d681SAndroid Build Coastguard Worker     }
2058*9880d681SAndroid Build Coastguard Worker   } else {
2059*9880d681SAndroid Build Coastguard Worker     if (GV->isThreadLocal()) {
2060*9880d681SAndroid Build Coastguard Worker       OS.AddComment("Record kind: S_GTHREAD32");
2061*9880d681SAndroid Build Coastguard Worker       OS.EmitIntValue(unsigned(SymbolKind::S_GTHREAD32), 2);
2062*9880d681SAndroid Build Coastguard Worker     } else {
2063*9880d681SAndroid Build Coastguard Worker       OS.AddComment("Record kind: S_GDATA32");
2064*9880d681SAndroid Build Coastguard Worker       OS.EmitIntValue(unsigned(SymbolKind::S_GDATA32), 2);
2065*9880d681SAndroid Build Coastguard Worker     }
2066*9880d681SAndroid Build Coastguard Worker   }
2067*9880d681SAndroid Build Coastguard Worker   OS.AddComment("Type");
2068*9880d681SAndroid Build Coastguard Worker   OS.EmitIntValue(getCompleteTypeIndex(DIGV->getType()).getIndex(), 4);
2069*9880d681SAndroid Build Coastguard Worker   OS.AddComment("DataOffset");
2070*9880d681SAndroid Build Coastguard Worker   OS.EmitCOFFSecRel32(GVSym);
2071*9880d681SAndroid Build Coastguard Worker   OS.AddComment("Segment");
2072*9880d681SAndroid Build Coastguard Worker   OS.EmitCOFFSectionIndex(GVSym);
2073*9880d681SAndroid Build Coastguard Worker   OS.AddComment("Name");
2074*9880d681SAndroid Build Coastguard Worker   emitNullTerminatedSymbolName(OS, DIGV->getName());
2075*9880d681SAndroid Build Coastguard Worker   OS.EmitLabel(DataEnd);
2076*9880d681SAndroid Build Coastguard Worker }
2077