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