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