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