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