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