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