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