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