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