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