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