LLVM 17.0.0git
CodeViewDebug.cpp
Go to the documentation of this file.
1//===- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp ----------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains support for writing Microsoft CodeView debug info.
10//
11//===----------------------------------------------------------------------===//
12
13#include "CodeViewDebug.h"
14#include "llvm/ADT/APSInt.h"
15#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/StringRef.h"
19#include "llvm/ADT/Triple.h"
20#include "llvm/ADT/Twine.h"
32#include "llvm/Config/llvm-config.h"
43#include "llvm/IR/Constants.h"
44#include "llvm/IR/DataLayout.h"
46#include "llvm/IR/Function.h"
47#include "llvm/IR/GlobalValue.h"
49#include "llvm/IR/Metadata.h"
50#include "llvm/IR/Module.h"
51#include "llvm/MC/MCAsmInfo.h"
52#include "llvm/MC/MCContext.h"
54#include "llvm/MC/MCStreamer.h"
55#include "llvm/MC/MCSymbol.h"
58#include "llvm/Support/Endian.h"
59#include "llvm/Support/Error.h"
62#include "llvm/Support/Path.h"
64#include "llvm/Support/SMLoc.h"
68#include <algorithm>
69#include <cassert>
70#include <cctype>
71#include <cstddef>
72#include <iterator>
73#include <limits>
74
75using namespace llvm;
76using namespace llvm::codeview;
77
78namespace {
79class CVMCAdapter : public CodeViewRecordStreamer {
80public:
81 CVMCAdapter(MCStreamer &OS, TypeCollection &TypeTable)
82 : OS(&OS), TypeTable(TypeTable) {}
83
84 void emitBytes(StringRef Data) override { OS->emitBytes(Data); }
85
86 void emitIntValue(uint64_t Value, unsigned Size) override {
87 OS->emitIntValueInHex(Value, Size);
88 }
89
90 void emitBinaryData(StringRef Data) override { OS->emitBinaryData(Data); }
91
92 void AddComment(const Twine &T) override { OS->AddComment(T); }
93
94 void AddRawComment(const Twine &T) override { OS->emitRawComment(T); }
95
96 bool isVerboseAsm() override { return OS->isVerboseAsm(); }
97
98 std::string getTypeName(TypeIndex TI) override {
99 std::string TypeName;
100 if (!TI.isNoneType()) {
101 if (TI.isSimple())
102 TypeName = std::string(TypeIndex::simpleTypeName(TI));
103 else
104 TypeName = std::string(TypeTable.getTypeName(TI));
105 }
106 return TypeName;
107 }
108
109private:
110 MCStreamer *OS = nullptr;
111 TypeCollection &TypeTable;
112};
113} // namespace
114
116 switch (Type) {
117 case Triple::ArchType::x86:
118 return CPUType::Pentium3;
119 case Triple::ArchType::x86_64:
120 return CPUType::X64;
121 case Triple::ArchType::thumb:
122 // LLVM currently doesn't support Windows CE and so thumb
123 // here is indiscriminately mapped to ARMNT specifically.
124 return CPUType::ARMNT;
125 case Triple::ArchType::aarch64:
126 return CPUType::ARM64;
127 default:
128 report_fatal_error("target architecture doesn't map to a CodeView CPUType");
129 }
130}
131
133 : DebugHandlerBase(AP), OS(*Asm->OutStreamer), TypeTable(Allocator) {}
134
135StringRef CodeViewDebug::getFullFilepath(const DIFile *File) {
136 std::string &Filepath = FileToFilepathMap[File];
137 if (!Filepath.empty())
138 return Filepath;
139
140 StringRef Dir = File->getDirectory(), Filename = File->getFilename();
141
142 // If this is a Unix-style path, just use it as is. Don't try to canonicalize
143 // it textually because one of the path components could be a symlink.
144 if (Dir.startswith("/") || Filename.startswith("/")) {
146 return Filename;
147 Filepath = std::string(Dir);
148 if (Dir.back() != '/')
149 Filepath += '/';
150 Filepath += Filename;
151 return Filepath;
152 }
153
154 // Clang emits directory and relative filename info into the IR, but CodeView
155 // operates on full paths. We could change Clang to emit full paths too, but
156 // that would increase the IR size and probably not needed for other users.
157 // For now, just concatenate and canonicalize the path here.
158 if (Filename.find(':') == 1)
159 Filepath = std::string(Filename);
160 else
161 Filepath = (Dir + "\\" + Filename).str();
162
163 // Canonicalize the path. We have to do it textually because we may no longer
164 // have access the file in the filesystem.
165 // First, replace all slashes with backslashes.
166 std::replace(Filepath.begin(), Filepath.end(), '/', '\\');
167
168 // Remove all "\.\" with "\".
169 size_t Cursor = 0;
170 while ((Cursor = Filepath.find("\\.\\", Cursor)) != std::string::npos)
171 Filepath.erase(Cursor, 2);
172
173 // Replace all "\XXX\..\" with "\". Don't try too hard though as the original
174 // path should be well-formatted, e.g. start with a drive letter, etc.
175 Cursor = 0;
176 while ((Cursor = Filepath.find("\\..\\", Cursor)) != std::string::npos) {
177 // Something's wrong if the path starts with "\..\", abort.
178 if (Cursor == 0)
179 break;
180
181 size_t PrevSlash = Filepath.rfind('\\', Cursor - 1);
182 if (PrevSlash == std::string::npos)
183 // Something's wrong, abort.
184 break;
185
186 Filepath.erase(PrevSlash, Cursor + 3 - PrevSlash);
187 // The next ".." might be following the one we've just erased.
188 Cursor = PrevSlash;
189 }
190
191 // Remove all duplicate backslashes.
192 Cursor = 0;
193 while ((Cursor = Filepath.find("\\\\", Cursor)) != std::string::npos)
194 Filepath.erase(Cursor, 1);
195
196 return Filepath;
197}
198
199unsigned CodeViewDebug::maybeRecordFile(const DIFile *F) {
200 StringRef FullPath = getFullFilepath(F);
201 unsigned NextId = FileIdMap.size() + 1;
202 auto Insertion = FileIdMap.insert(std::make_pair(FullPath, NextId));
203 if (Insertion.second) {
204 // We have to compute the full filepath and emit a .cv_file directive.
205 ArrayRef<uint8_t> ChecksumAsBytes;
206 FileChecksumKind CSKind = FileChecksumKind::None;
207 if (F->getChecksum()) {
208 std::string Checksum = fromHex(F->getChecksum()->Value);
209 void *CKMem = OS.getContext().allocate(Checksum.size(), 1);
210 memcpy(CKMem, Checksum.data(), Checksum.size());
211 ChecksumAsBytes = ArrayRef<uint8_t>(
212 reinterpret_cast<const uint8_t *>(CKMem), Checksum.size());
213 switch (F->getChecksum()->Kind) {
214 case DIFile::CSK_MD5:
215 CSKind = FileChecksumKind::MD5;
216 break;
217 case DIFile::CSK_SHA1:
218 CSKind = FileChecksumKind::SHA1;
219 break;
221 CSKind = FileChecksumKind::SHA256;
222 break;
223 }
224 }
225 bool Success = OS.emitCVFileDirective(NextId, FullPath, ChecksumAsBytes,
226 static_cast<unsigned>(CSKind));
227 (void)Success;
228 assert(Success && ".cv_file directive failed");
229 }
230 return Insertion.first->second;
231}
232
233CodeViewDebug::InlineSite &
234CodeViewDebug::getInlineSite(const DILocation *InlinedAt,
235 const DISubprogram *Inlinee) {
236 auto SiteInsertion = CurFn->InlineSites.insert({InlinedAt, InlineSite()});
237 InlineSite *Site = &SiteInsertion.first->second;
238 if (SiteInsertion.second) {
239 unsigned ParentFuncId = CurFn->FuncId;
240 if (const DILocation *OuterIA = InlinedAt->getInlinedAt())
241 ParentFuncId =
242 getInlineSite(OuterIA, InlinedAt->getScope()->getSubprogram())
243 .SiteFuncId;
244
245 Site->SiteFuncId = NextFuncId++;
247 Site->SiteFuncId, ParentFuncId, maybeRecordFile(InlinedAt->getFile()),
248 InlinedAt->getLine(), InlinedAt->getColumn(), SMLoc());
249 Site->Inlinee = Inlinee;
250 InlinedSubprograms.insert(Inlinee);
251 getFuncIdForSubprogram(Inlinee);
252 }
253 return *Site;
254}
255
257 StringRef ScopeName = Scope->getName();
258 if (!ScopeName.empty())
259 return ScopeName;
260
261 switch (Scope->getTag()) {
262 case dwarf::DW_TAG_enumeration_type:
263 case dwarf::DW_TAG_class_type:
264 case dwarf::DW_TAG_structure_type:
265 case dwarf::DW_TAG_union_type:
266 return "<unnamed-tag>";
267 case dwarf::DW_TAG_namespace:
268 return "`anonymous namespace'";
269 default:
270 return StringRef();
271 }
272}
273
274const DISubprogram *CodeViewDebug::collectParentScopeNames(
275 const DIScope *Scope, SmallVectorImpl<StringRef> &QualifiedNameComponents) {
276 const DISubprogram *ClosestSubprogram = nullptr;
277 while (Scope != nullptr) {
278 if (ClosestSubprogram == nullptr)
279 ClosestSubprogram = dyn_cast<DISubprogram>(Scope);
280
281 // If a type appears in a scope chain, make sure it gets emitted. The
282 // frontend will be responsible for deciding if this should be a forward
283 // declaration or a complete type.
284 if (const auto *Ty = dyn_cast<DICompositeType>(Scope))
285 DeferredCompleteTypes.push_back(Ty);
286
287 StringRef ScopeName = getPrettyScopeName(Scope);
288 if (!ScopeName.empty())
289 QualifiedNameComponents.push_back(ScopeName);
290 Scope = Scope->getScope();
291 }
292 return ClosestSubprogram;
293}
294
295static std::string formatNestedName(ArrayRef<StringRef> QualifiedNameComponents,
296 StringRef TypeName) {
297 std::string FullyQualifiedName;
298 for (StringRef QualifiedNameComponent :
299 llvm::reverse(QualifiedNameComponents)) {
300 FullyQualifiedName.append(std::string(QualifiedNameComponent));
301 FullyQualifiedName.append("::");
302 }
303 FullyQualifiedName.append(std::string(TypeName));
304 return FullyQualifiedName;
305}
306
308 TypeLoweringScope(CodeViewDebug &CVD) : CVD(CVD) { ++CVD.TypeEmissionLevel; }
310 // Don't decrement TypeEmissionLevel until after emitting deferred types, so
311 // inner TypeLoweringScopes don't attempt to emit deferred types.
312 if (CVD.TypeEmissionLevel == 1)
313 CVD.emitDeferredCompleteTypes();
314 --CVD.TypeEmissionLevel;
315 }
317};
318
319std::string CodeViewDebug::getFullyQualifiedName(const DIScope *Scope,
320 StringRef Name) {
321 // Ensure types in the scope chain are emitted as soon as possible.
322 // This can create otherwise a situation where S_UDTs are emitted while
323 // looping in emitDebugInfoForUDTs.
324 TypeLoweringScope S(*this);
325 SmallVector<StringRef, 5> QualifiedNameComponents;
326 collectParentScopeNames(Scope, QualifiedNameComponents);
327 return formatNestedName(QualifiedNameComponents, Name);
328}
329
330std::string CodeViewDebug::getFullyQualifiedName(const DIScope *Ty) {
331 const DIScope *Scope = Ty->getScope();
332 return getFullyQualifiedName(Scope, getPrettyScopeName(Ty));
333}
334
335TypeIndex CodeViewDebug::getScopeIndex(const DIScope *Scope) {
336 // No scope means global scope and that uses the zero index.
337 //
338 // We also use zero index when the scope is a DISubprogram
339 // to suppress the emission of LF_STRING_ID for the function,
340 // which can trigger a link-time error with the linker in
341 // VS2019 version 16.11.2 or newer.
342 // Note, however, skipping the debug info emission for the DISubprogram
343 // is a temporary fix. The root issue here is that we need to figure out
344 // the proper way to encode a function nested in another function
345 // (as introduced by the Fortran 'contains' keyword) in CodeView.
346 if (!Scope || isa<DIFile>(Scope) || isa<DISubprogram>(Scope))
347 return TypeIndex();
348
349 assert(!isa<DIType>(Scope) && "shouldn't make a namespace scope for a type");
350
351 // Check if we've already translated this scope.
352 auto I = TypeIndices.find({Scope, nullptr});
353 if (I != TypeIndices.end())
354 return I->second;
355
356 // Build the fully qualified name of the scope.
357 std::string ScopeName = getFullyQualifiedName(Scope);
358 StringIdRecord SID(TypeIndex(), ScopeName);
359 auto TI = TypeTable.writeLeafType(SID);
360 return recordTypeIndexForDINode(Scope, TI);
361}
362
364 // Remove template args from the display name. Assume that the template args
365 // are the last thing in the name.
366 if (Name.empty() || Name.back() != '>')
367 return Name;
368
369 int OpenBrackets = 0;
370 for (int i = Name.size() - 1; i >= 0; --i) {
371 if (Name[i] == '>')
372 ++OpenBrackets;
373 else if (Name[i] == '<') {
374 --OpenBrackets;
375 if (OpenBrackets == 0)
376 return Name.substr(0, i);
377 }
378 }
379 return Name;
380}
381
382TypeIndex CodeViewDebug::getFuncIdForSubprogram(const DISubprogram *SP) {
383 assert(SP);
384
385 // Check if we've already translated this subprogram.
386 auto I = TypeIndices.find({SP, nullptr});
387 if (I != TypeIndices.end())
388 return I->second;
389
390 // The display name includes function template arguments. Drop them to match
391 // MSVC. We need to have the template arguments in the DISubprogram name
392 // because they are used in other symbol records, such as S_GPROC32_IDs.
393 StringRef DisplayName = removeTemplateArgs(SP->getName());
394
395 const DIScope *Scope = SP->getScope();
396 TypeIndex TI;
397 if (const auto *Class = dyn_cast_or_null<DICompositeType>(Scope)) {
398 // If the scope is a DICompositeType, then this must be a method. Member
399 // function types take some special handling, and require access to the
400 // subprogram.
401 TypeIndex ClassType = getTypeIndex(Class);
402 MemberFuncIdRecord MFuncId(ClassType, getMemberFunctionType(SP, Class),
403 DisplayName);
404 TI = TypeTable.writeLeafType(MFuncId);
405 } else {
406 // Otherwise, this must be a free function.
407 TypeIndex ParentScope = getScopeIndex(Scope);
408 FuncIdRecord FuncId(ParentScope, getTypeIndex(SP->getType()), DisplayName);
409 TI = TypeTable.writeLeafType(FuncId);
410 }
411
412 return recordTypeIndexForDINode(SP, TI);
413}
414
415static bool isNonTrivial(const DICompositeType *DCTy) {
416 return ((DCTy->getFlags() & DINode::FlagNonTrivial) == DINode::FlagNonTrivial);
417}
418
419static FunctionOptions
421 const DICompositeType *ClassTy = nullptr,
422 StringRef SPName = StringRef("")) {
423 FunctionOptions FO = FunctionOptions::None;
424 const DIType *ReturnTy = nullptr;
425 if (auto TypeArray = Ty->getTypeArray()) {
426 if (TypeArray.size())
427 ReturnTy = TypeArray[0];
428 }
429
430 // Add CxxReturnUdt option to functions that return nontrivial record types
431 // or methods that return record types.
432 if (auto *ReturnDCTy = dyn_cast_or_null<DICompositeType>(ReturnTy))
433 if (isNonTrivial(ReturnDCTy) || ClassTy)
434 FO |= FunctionOptions::CxxReturnUdt;
435
436 // DISubroutineType is unnamed. Use DISubprogram's i.e. SPName in comparison.
437 if (ClassTy && isNonTrivial(ClassTy) && SPName == ClassTy->getName()) {
438 FO |= FunctionOptions::Constructor;
439
440 // TODO: put the FunctionOptions::ConstructorWithVirtualBases flag.
441
442 }
443 return FO;
444}
445
446TypeIndex CodeViewDebug::getMemberFunctionType(const DISubprogram *SP,
447 const DICompositeType *Class) {
448 // Always use the method declaration as the key for the function type. The
449 // method declaration contains the this adjustment.
450 if (SP->getDeclaration())
451 SP = SP->getDeclaration();
452 assert(!SP->getDeclaration() && "should use declaration as key");
453
454 // Key the MemberFunctionRecord into the map as {SP, Class}. It won't collide
455 // with the MemberFuncIdRecord, which is keyed in as {SP, nullptr}.
456 auto I = TypeIndices.find({SP, Class});
457 if (I != TypeIndices.end())
458 return I->second;
459
460 // Make sure complete type info for the class is emitted *after* the member
461 // function type, as the complete class type is likely to reference this
462 // member function type.
463 TypeLoweringScope S(*this);
464 const bool IsStaticMethod = (SP->getFlags() & DINode::FlagStaticMember) != 0;
465
466 FunctionOptions FO = getFunctionOptions(SP->getType(), Class, SP->getName());
467 TypeIndex TI = lowerTypeMemberFunction(
468 SP->getType(), Class, SP->getThisAdjustment(), IsStaticMethod, FO);
469 return recordTypeIndexForDINode(SP, TI, Class);
470}
471
472TypeIndex CodeViewDebug::recordTypeIndexForDINode(const DINode *Node,
473 TypeIndex TI,
474 const DIType *ClassTy) {
475 auto InsertResult = TypeIndices.insert({{Node, ClassTy}, TI});
476 (void)InsertResult;
477 assert(InsertResult.second && "DINode was already assigned a type index");
478 return TI;
479}
480
481unsigned CodeViewDebug::getPointerSizeInBytes() {
483}
484
485void CodeViewDebug::recordLocalVariable(LocalVariable &&Var,
486 const LexicalScope *LS) {
487 if (const DILocation *InlinedAt = LS->getInlinedAt()) {
488 // This variable was inlined. Associate it with the InlineSite.
489 const DISubprogram *Inlinee = Var.DIVar->getScope()->getSubprogram();
490 InlineSite &Site = getInlineSite(InlinedAt, Inlinee);
491 Site.InlinedLocals.emplace_back(std::move(Var));
492 } else {
493 // This variable goes into the corresponding lexical scope.
494 ScopeVariables[LS].emplace_back(std::move(Var));
495 }
496}
497
499 const DILocation *Loc) {
500 if (!llvm::is_contained(Locs, Loc))
501 Locs.push_back(Loc);
502}
503
504void CodeViewDebug::maybeRecordLocation(const DebugLoc &DL,
505 const MachineFunction *MF) {
506 // Skip this instruction if it has the same location as the previous one.
507 if (!DL || DL == PrevInstLoc)
508 return;
509
510 const DIScope *Scope = DL->getScope();
511 if (!Scope)
512 return;
513
514 // Skip this line if it is longer than the maximum we can record.
515 LineInfo LI(DL.getLine(), DL.getLine(), /*IsStatement=*/true);
516 if (LI.getStartLine() != DL.getLine() || LI.isAlwaysStepInto() ||
517 LI.isNeverStepInto())
518 return;
519
520 ColumnInfo CI(DL.getCol(), /*EndColumn=*/0);
521 if (CI.getStartColumn() != DL.getCol())
522 return;
523
524 if (!CurFn->HaveLineInfo)
525 CurFn->HaveLineInfo = true;
526 unsigned FileId = 0;
527 if (PrevInstLoc.get() && PrevInstLoc->getFile() == DL->getFile())
528 FileId = CurFn->LastFileId;
529 else
530 FileId = CurFn->LastFileId = maybeRecordFile(DL->getFile());
531 PrevInstLoc = DL;
532
533 unsigned FuncId = CurFn->FuncId;
534 if (const DILocation *SiteLoc = DL->getInlinedAt()) {
535 const DILocation *Loc = DL.get();
536
537 // If this location was actually inlined from somewhere else, give it the ID
538 // of the inline call site.
539 FuncId =
540 getInlineSite(SiteLoc, Loc->getScope()->getSubprogram()).SiteFuncId;
541
542 // Ensure we have links in the tree of inline call sites.
543 bool FirstLoc = true;
544 while ((SiteLoc = Loc->getInlinedAt())) {
545 InlineSite &Site =
546 getInlineSite(SiteLoc, Loc->getScope()->getSubprogram());
547 if (!FirstLoc)
548 addLocIfNotPresent(Site.ChildSites, Loc);
549 FirstLoc = false;
550 Loc = SiteLoc;
551 }
552 addLocIfNotPresent(CurFn->ChildSites, Loc);
553 }
554
555 OS.emitCVLocDirective(FuncId, FileId, DL.getLine(), DL.getCol(),
556 /*PrologueEnd=*/false, /*IsStmt=*/false,
557 DL->getFilename(), SMLoc());
558}
559
560void CodeViewDebug::emitCodeViewMagicVersion() {
562 OS.AddComment("Debug section magic");
564}
565
566static SourceLanguage MapDWLangToCVLang(unsigned DWLang) {
567 switch (DWLang) {
568 case dwarf::DW_LANG_C:
569 case dwarf::DW_LANG_C89:
570 case dwarf::DW_LANG_C99:
571 case dwarf::DW_LANG_C11:
572 case dwarf::DW_LANG_ObjC:
573 return SourceLanguage::C;
574 case dwarf::DW_LANG_C_plus_plus:
575 case dwarf::DW_LANG_C_plus_plus_03:
576 case dwarf::DW_LANG_C_plus_plus_11:
577 case dwarf::DW_LANG_C_plus_plus_14:
578 return SourceLanguage::Cpp;
579 case dwarf::DW_LANG_Fortran77:
580 case dwarf::DW_LANG_Fortran90:
581 case dwarf::DW_LANG_Fortran95:
582 case dwarf::DW_LANG_Fortran03:
583 case dwarf::DW_LANG_Fortran08:
584 return SourceLanguage::Fortran;
585 case dwarf::DW_LANG_Pascal83:
586 return SourceLanguage::Pascal;
587 case dwarf::DW_LANG_Cobol74:
588 case dwarf::DW_LANG_Cobol85:
589 return SourceLanguage::Cobol;
590 case dwarf::DW_LANG_Java:
591 return SourceLanguage::Java;
592 case dwarf::DW_LANG_D:
593 return SourceLanguage::D;
594 case dwarf::DW_LANG_Swift:
595 return SourceLanguage::Swift;
596 case dwarf::DW_LANG_Rust:
597 return SourceLanguage::Rust;
598 default:
599 // There's no CodeView representation for this language, and CV doesn't
600 // have an "unknown" option for the language field, so we'll use MASM,
601 // as it's very low level.
602 return SourceLanguage::Masm;
603 }
604}
605
607 // If module doesn't have named metadata anchors or COFF debug section
608 // is not available, skip any debug info related stuff.
609 if (!MMI->hasDebugInfo() ||
611 Asm = nullptr;
612 return;
613 }
614
615 TheCPU = mapArchToCVCPUType(Triple(M->getTargetTriple()).getArch());
616
617 // Get the current source language.
618 const MDNode *Node = *M->debug_compile_units_begin();
619 const auto *CU = cast<DICompileUnit>(Node);
620
621 CurrentSourceLanguage = MapDWLangToCVLang(CU->getSourceLanguage());
622
623 collectGlobalVariableInfo();
624
625 // Check if we should emit type record hashes.
626 ConstantInt *GH =
627 mdconst::extract_or_null<ConstantInt>(M->getModuleFlag("CodeViewGHash"));
628 EmitDebugGlobalHashes = GH && !GH->isZero();
629}
630
632 if (!Asm || !MMI->hasDebugInfo())
633 return;
634
635 // The COFF .debug$S section consists of several subsections, each starting
636 // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length
637 // of the payload followed by the payload itself. The subsections are 4-byte
638 // aligned.
639
640 // Use the generic .debug$S section, and make a subsection for all the inlined
641 // subprograms.
642 switchToDebugSectionForSymbol(nullptr);
643
644 MCSymbol *CompilerInfo = beginCVSubsection(DebugSubsectionKind::Symbols);
645 emitObjName();
646 emitCompilerInformation();
647 endCVSubsection(CompilerInfo);
648
649 emitInlineeLinesSubsection();
650
651 // Emit per-function debug information.
652 for (auto &P : FnDebugInfo)
653 if (!P.first->isDeclarationForLinker())
654 emitDebugInfoForFunction(P.first, *P.second);
655
656 // Get types used by globals without emitting anything.
657 // This is meant to collect all static const data members so they can be
658 // emitted as globals.
659 collectDebugInfoForGlobals();
660
661 // Emit retained types.
662 emitDebugInfoForRetainedTypes();
663
664 // Emit global variable debug information.
665 setCurrentSubprogram(nullptr);
666 emitDebugInfoForGlobals();
667
668 // Switch back to the generic .debug$S section after potentially processing
669 // comdat symbol sections.
670 switchToDebugSectionForSymbol(nullptr);
671
672 // Emit UDT records for any types used by global variables.
673 if (!GlobalUDTs.empty()) {
674 MCSymbol *SymbolsEnd = beginCVSubsection(DebugSubsectionKind::Symbols);
675 emitDebugInfoForUDTs(GlobalUDTs);
676 endCVSubsection(SymbolsEnd);
677 }
678
679 // This subsection holds a file index to offset in string table table.
680 OS.AddComment("File index to string table offset subsection");
682
683 // This subsection holds the string table.
684 OS.AddComment("String table");
686
687 // Emit S_BUILDINFO, which points to LF_BUILDINFO. Put this in its own symbol
688 // subsection in the generic .debug$S section at the end. There is no
689 // particular reason for this ordering other than to match MSVC.
690 emitBuildInfo();
691
692 // Emit type information and hashes last, so that any types we translate while
693 // emitting function info are included.
694 emitTypeInformation();
695
696 if (EmitDebugGlobalHashes)
697 emitTypeGlobalHashes();
698
699 clear();
700}
701
702static void
704 unsigned MaxFixedRecordLength = 0xF00) {
705 // The maximum CV record length is 0xFF00. Most of the strings we emit appear
706 // after a fixed length portion of the record. The fixed length portion should
707 // always be less than 0xF00 (3840) bytes, so truncate the string so that the
708 // overall record size is less than the maximum allowed.
709 SmallString<32> NullTerminatedString(
710 S.take_front(MaxRecordLength - MaxFixedRecordLength - 1));
711 NullTerminatedString.push_back('\0');
712 OS.emitBytes(NullTerminatedString);
713}
714
715void CodeViewDebug::emitTypeInformation() {
716 if (TypeTable.empty())
717 return;
718
719 // Start the .debug$T or .debug$P section with 0x4.
721 emitCodeViewMagicVersion();
722
723 TypeTableCollection Table(TypeTable.records());
725
726 // To emit type record using Codeview MCStreamer adapter
727 CVMCAdapter CVMCOS(OS, Table);
728 TypeRecordMapping typeMapping(CVMCOS);
729 Pipeline.addCallbackToPipeline(typeMapping);
730
731 std::optional<TypeIndex> B = Table.getFirst();
732 while (B) {
733 // This will fail if the record data is invalid.
734 CVType Record = Table.getType(*B);
735
737
738 if (E) {
739 logAllUnhandledErrors(std::move(E), errs(), "error: ");
740 llvm_unreachable("produced malformed type record");
741 }
742
743 B = Table.getNext(*B);
744 }
745}
746
747void CodeViewDebug::emitTypeGlobalHashes() {
748 if (TypeTable.empty())
749 return;
750
751 // Start the .debug$H section with the version and hash algorithm, currently
752 // hardcoded to version 0, SHA1.
754
756 OS.AddComment("Magic");
758 OS.AddComment("Section Version");
759 OS.emitInt16(0);
760 OS.AddComment("Hash Algorithm");
761 OS.emitInt16(uint16_t(GlobalTypeHashAlg::BLAKE3));
762
764 for (const auto &GHR : TypeTable.hashes()) {
765 if (OS.isVerboseAsm()) {
766 // Emit an EOL-comment describing which TypeIndex this hash corresponds
767 // to, as well as the stringified SHA1 hash.
769 raw_svector_ostream CommentOS(Comment);
770 CommentOS << formatv("{0:X+} [{1}]", TI.getIndex(), GHR);
771 OS.AddComment(Comment);
772 ++TI;
773 }
774 assert(GHR.Hash.size() == 8);
775 StringRef S(reinterpret_cast<const char *>(GHR.Hash.data()),
776 GHR.Hash.size());
777 OS.emitBinaryData(S);
778 }
779}
780
781void CodeViewDebug::emitObjName() {
782 MCSymbol *CompilerEnd = beginSymbolRecord(SymbolKind::S_OBJNAME);
783
785 llvm::SmallString<256> PathStore(PathRef);
786
787 if (PathRef.empty() || PathRef == "-") {
788 // Don't emit the filename if we're writing to stdout or to /dev/null.
789 PathRef = {};
790 } else {
791 llvm::sys::path::remove_dots(PathStore, /*remove_dot_dot=*/true);
792 PathRef = PathStore;
793 }
794
795 OS.AddComment("Signature");
796 OS.emitIntValue(0, 4);
797
798 OS.AddComment("Object name");
799 emitNullTerminatedSymbolName(OS, PathRef);
800
801 endSymbolRecord(CompilerEnd);
802}
803
804namespace {
805struct Version {
806 int Part[4];
807};
808} // end anonymous namespace
809
810// Takes a StringRef like "clang 4.0.0.0 (other nonsense 123)" and parses out
811// the version number.
812static Version parseVersion(StringRef Name) {
813 Version V = {{0}};
814 int N = 0;
815 for (const char C : Name) {
816 if (isdigit(C)) {
817 V.Part[N] *= 10;
818 V.Part[N] += C - '0';
819 V.Part[N] =
820 std::min<int>(V.Part[N], std::numeric_limits<uint16_t>::max());
821 } else if (C == '.') {
822 ++N;
823 if (N >= 4)
824 return V;
825 } else if (N > 0)
826 return V;
827 }
828 return V;
829}
830
831void CodeViewDebug::emitCompilerInformation() {
832 MCSymbol *CompilerEnd = beginSymbolRecord(SymbolKind::S_COMPILE3);
833 uint32_t Flags = 0;
834
835 // The low byte of the flags indicates the source language.
836 Flags = CurrentSourceLanguage;
837 // TODO: Figure out which other flags need to be set.
838 if (MMI->getModule()->getProfileSummary(/*IsCS*/ false) != nullptr) {
839 Flags |= static_cast<uint32_t>(CompileSym3Flags::PGO);
840 }
841 using ArchType = llvm::Triple::ArchType;
842 ArchType Arch = Triple(MMI->getModule()->getTargetTriple()).getArch();
843 if (Asm->TM.Options.Hotpatch || Arch == ArchType::thumb ||
844 Arch == ArchType::aarch64) {
845 Flags |= static_cast<uint32_t>(CompileSym3Flags::HotPatch);
846 }
847
848 OS.AddComment("Flags and language");
849 OS.emitInt32(Flags);
850
851 OS.AddComment("CPUType");
852 OS.emitInt16(static_cast<uint64_t>(TheCPU));
853
854 NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
855 const MDNode *Node = *CUs->operands().begin();
856 const auto *CU = cast<DICompileUnit>(Node);
857
858 StringRef CompilerVersion = CU->getProducer();
859 Version FrontVer = parseVersion(CompilerVersion);
860 OS.AddComment("Frontend version");
861 for (int N : FrontVer.Part) {
862 OS.emitInt16(N);
863 }
864
865 // Some Microsoft tools, like Binscope, expect a backend version number of at
866 // least 8.something, so we'll coerce the LLVM version into a form that
867 // guarantees it'll be big enough without really lying about the version.
868 int Major = 1000 * LLVM_VERSION_MAJOR +
869 10 * LLVM_VERSION_MINOR +
870 LLVM_VERSION_PATCH;
871 // Clamp it for builds that use unusually large version numbers.
872 Major = std::min<int>(Major, std::numeric_limits<uint16_t>::max());
873 Version BackVer = {{ Major, 0, 0, 0 }};
874 OS.AddComment("Backend version");
875 for (int N : BackVer.Part)
876 OS.emitInt16(N);
877
878 OS.AddComment("Null-terminated compiler version string");
879 emitNullTerminatedSymbolName(OS, CompilerVersion);
880
881 endSymbolRecord(CompilerEnd);
882}
883
885 StringRef S) {
886 StringIdRecord SIR(TypeIndex(0x0), S);
887 return TypeTable.writeLeafType(SIR);
888}
889
891 StringRef MainFilename) {
892 std::string FlatCmdLine;
893 raw_string_ostream OS(FlatCmdLine);
894 bool PrintedOneArg = false;
895 if (!StringRef(Args[0]).contains("-cc1")) {
896 llvm::sys::printArg(OS, "-cc1", /*Quote=*/true);
897 PrintedOneArg = true;
898 }
899 for (unsigned i = 0; i < Args.size(); i++) {
900 StringRef Arg = Args[i];
901 if (Arg.empty())
902 continue;
903 if (Arg == "-main-file-name" || Arg == "-o") {
904 i++; // Skip this argument and next one.
905 continue;
906 }
907 if (Arg.startswith("-object-file-name") || Arg == MainFilename)
908 continue;
909 // Skip fmessage-length for reproduciability.
910 if (Arg.startswith("-fmessage-length"))
911 continue;
912 if (PrintedOneArg)
913 OS << " ";
914 llvm::sys::printArg(OS, Arg, /*Quote=*/true);
915 PrintedOneArg = true;
916 }
917 OS.flush();
918 return FlatCmdLine;
919}
920
921void CodeViewDebug::emitBuildInfo() {
922 // First, make LF_BUILDINFO. It's a sequence of strings with various bits of
923 // build info. The known prefix is:
924 // - Absolute path of current directory
925 // - Compiler path
926 // - Main source file path, relative to CWD or absolute
927 // - Type server PDB file
928 // - Canonical compiler command line
929 // If frontend and backend compilation are separated (think llc or LTO), it's
930 // not clear if the compiler path should refer to the executable for the
931 // frontend or the backend. Leave it blank for now.
932 TypeIndex BuildInfoArgs[BuildInfoRecord::MaxArgs] = {};
933 NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
934 const MDNode *Node = *CUs->operands().begin(); // FIXME: Multiple CUs.
935 const auto *CU = cast<DICompileUnit>(Node);
936 const DIFile *MainSourceFile = CU->getFile();
937 BuildInfoArgs[BuildInfoRecord::CurrentDirectory] =
938 getStringIdTypeIdx(TypeTable, MainSourceFile->getDirectory());
939 BuildInfoArgs[BuildInfoRecord::SourceFile] =
940 getStringIdTypeIdx(TypeTable, MainSourceFile->getFilename());
941 // FIXME: PDB is intentionally blank unless we implement /Zi type servers.
942 BuildInfoArgs[BuildInfoRecord::TypeServerPDB] =
943 getStringIdTypeIdx(TypeTable, "");
944 if (Asm->TM.Options.MCOptions.Argv0 != nullptr) {
945 BuildInfoArgs[BuildInfoRecord::BuildTool] =
949 MainSourceFile->getFilename()));
950 }
951 BuildInfoRecord BIR(BuildInfoArgs);
952 TypeIndex BuildInfoIndex = TypeTable.writeLeafType(BIR);
953
954 // Make a new .debug$S subsection for the S_BUILDINFO record, which points
955 // from the module symbols into the type stream.
956 MCSymbol *BISubsecEnd = beginCVSubsection(DebugSubsectionKind::Symbols);
957 MCSymbol *BIEnd = beginSymbolRecord(SymbolKind::S_BUILDINFO);
958 OS.AddComment("LF_BUILDINFO index");
959 OS.emitInt32(BuildInfoIndex.getIndex());
960 endSymbolRecord(BIEnd);
961 endCVSubsection(BISubsecEnd);
962}
963
964void CodeViewDebug::emitInlineeLinesSubsection() {
965 if (InlinedSubprograms.empty())
966 return;
967
968 OS.AddComment("Inlinee lines subsection");
969 MCSymbol *InlineEnd = beginCVSubsection(DebugSubsectionKind::InlineeLines);
970
971 // We emit the checksum info for files. This is used by debuggers to
972 // determine if a pdb matches the source before loading it. Visual Studio,
973 // for instance, will display a warning that the breakpoints are not valid if
974 // the pdb does not match the source.
975 OS.AddComment("Inlinee lines signature");
976 OS.emitInt32(unsigned(InlineeLinesSignature::Normal));
977
978 for (const DISubprogram *SP : InlinedSubprograms) {
979 assert(TypeIndices.count({SP, nullptr}));
980 TypeIndex InlineeIdx = TypeIndices[{SP, nullptr}];
981
982 OS.addBlankLine();
983 unsigned FileId = maybeRecordFile(SP->getFile());
984 OS.AddComment("Inlined function " + SP->getName() + " starts at " +
985 SP->getFilename() + Twine(':') + Twine(SP->getLine()));
986 OS.addBlankLine();
987 OS.AddComment("Type index of inlined function");
988 OS.emitInt32(InlineeIdx.getIndex());
989 OS.AddComment("Offset into filechecksum table");
991 OS.AddComment("Starting line number");
992 OS.emitInt32(SP->getLine());
993 }
994
995 endCVSubsection(InlineEnd);
996}
997
998void CodeViewDebug::emitInlinedCallSite(const FunctionInfo &FI,
999 const DILocation *InlinedAt,
1000 const InlineSite &Site) {
1001 assert(TypeIndices.count({Site.Inlinee, nullptr}));
1002 TypeIndex InlineeIdx = TypeIndices[{Site.Inlinee, nullptr}];
1003
1004 // SymbolRecord
1005 MCSymbol *InlineEnd = beginSymbolRecord(SymbolKind::S_INLINESITE);
1006
1007 OS.AddComment("PtrParent");
1008 OS.emitInt32(0);
1009 OS.AddComment("PtrEnd");
1010 OS.emitInt32(0);
1011 OS.AddComment("Inlinee type index");
1012 OS.emitInt32(InlineeIdx.getIndex());
1013
1014 unsigned FileId = maybeRecordFile(Site.Inlinee->getFile());
1015 unsigned StartLineNum = Site.Inlinee->getLine();
1016
1017 OS.emitCVInlineLinetableDirective(Site.SiteFuncId, FileId, StartLineNum,
1018 FI.Begin, FI.End);
1019
1020 endSymbolRecord(InlineEnd);
1021
1022 emitLocalVariableList(FI, Site.InlinedLocals);
1023
1024 // Recurse on child inlined call sites before closing the scope.
1025 for (const DILocation *ChildSite : Site.ChildSites) {
1026 auto I = FI.InlineSites.find(ChildSite);
1027 assert(I != FI.InlineSites.end() &&
1028 "child site not in function inline site map");
1029 emitInlinedCallSite(FI, ChildSite, I->second);
1030 }
1031
1032 // Close the scope.
1033 emitEndSymbolRecord(SymbolKind::S_INLINESITE_END);
1034}
1035
1036void CodeViewDebug::switchToDebugSectionForSymbol(const MCSymbol *GVSym) {
1037 // If we have a symbol, it may be in a section that is COMDAT. If so, find the
1038 // comdat key. A section may be comdat because of -ffunction-sections or
1039 // because it is comdat in the IR.
1040 MCSectionCOFF *GVSec =
1041 GVSym ? dyn_cast<MCSectionCOFF>(&GVSym->getSection()) : nullptr;
1042 const MCSymbol *KeySym = GVSec ? GVSec->getCOMDATSymbol() : nullptr;
1043
1044 MCSectionCOFF *DebugSec = cast<MCSectionCOFF>(
1046 DebugSec = OS.getContext().getAssociativeCOFFSection(DebugSec, KeySym);
1047
1048 OS.switchSection(DebugSec);
1049
1050 // Emit the magic version number if this is the first time we've switched to
1051 // this section.
1052 if (ComdatDebugSections.insert(DebugSec).second)
1053 emitCodeViewMagicVersion();
1054}
1055
1056// Emit an S_THUNK32/S_END symbol pair for a thunk routine.
1057// The only supported thunk ordinal is currently the standard type.
1058void CodeViewDebug::emitDebugInfoForThunk(const Function *GV,
1059 FunctionInfo &FI,
1060 const MCSymbol *Fn) {
1061 std::string FuncName =
1062 std::string(GlobalValue::dropLLVMManglingEscape(GV->getName()));
1063 const ThunkOrdinal ordinal = ThunkOrdinal::Standard; // Only supported kind.
1064
1065 OS.AddComment("Symbol subsection for " + Twine(FuncName));
1066 MCSymbol *SymbolsEnd = beginCVSubsection(DebugSubsectionKind::Symbols);
1067
1068 // Emit S_THUNK32
1069 MCSymbol *ThunkRecordEnd = beginSymbolRecord(SymbolKind::S_THUNK32);
1070 OS.AddComment("PtrParent");
1071 OS.emitInt32(0);
1072 OS.AddComment("PtrEnd");
1073 OS.emitInt32(0);
1074 OS.AddComment("PtrNext");
1075 OS.emitInt32(0);
1076 OS.AddComment("Thunk section relative address");
1077 OS.emitCOFFSecRel32(Fn, /*Offset=*/0);
1078 OS.AddComment("Thunk section index");
1079 OS.emitCOFFSectionIndex(Fn);
1080 OS.AddComment("Code size");
1081 OS.emitAbsoluteSymbolDiff(FI.End, Fn, 2);
1082 OS.AddComment("Ordinal");
1083 OS.emitInt8(unsigned(ordinal));
1084 OS.AddComment("Function name");
1085 emitNullTerminatedSymbolName(OS, FuncName);
1086 // Additional fields specific to the thunk ordinal would go here.
1087 endSymbolRecord(ThunkRecordEnd);
1088
1089 // Local variables/inlined routines are purposely omitted here. The point of
1090 // marking this as a thunk is so Visual Studio will NOT stop in this routine.
1091
1092 // Emit S_PROC_ID_END
1093 emitEndSymbolRecord(SymbolKind::S_PROC_ID_END);
1094
1095 endCVSubsection(SymbolsEnd);
1096}
1097
1098void CodeViewDebug::emitDebugInfoForFunction(const Function *GV,
1099 FunctionInfo &FI) {
1100 // For each function there is a separate subsection which holds the PC to
1101 // file:line table.
1102 const MCSymbol *Fn = Asm->getSymbol(GV);
1103 assert(Fn);
1104
1105 // Switch to the to a comdat section, if appropriate.
1106 switchToDebugSectionForSymbol(Fn);
1107
1108 std::string FuncName;
1109 auto *SP = GV->getSubprogram();
1110 assert(SP);
1111 setCurrentSubprogram(SP);
1112
1113 if (SP->isThunk()) {
1114 emitDebugInfoForThunk(GV, FI, Fn);
1115 return;
1116 }
1117
1118 // If we have a display name, build the fully qualified name by walking the
1119 // chain of scopes.
1120 if (!SP->getName().empty())
1121 FuncName = getFullyQualifiedName(SP->getScope(), SP->getName());
1122
1123 // If our DISubprogram name is empty, use the mangled name.
1124 if (FuncName.empty())
1125 FuncName = std::string(GlobalValue::dropLLVMManglingEscape(GV->getName()));
1126
1127 // Emit FPO data, but only on 32-bit x86. No other platforms use it.
1129 OS.emitCVFPOData(Fn);
1130
1131 // Emit a symbol subsection, required by VS2012+ to find function boundaries.
1132 OS.AddComment("Symbol subsection for " + Twine(FuncName));
1133 MCSymbol *SymbolsEnd = beginCVSubsection(DebugSubsectionKind::Symbols);
1134 {
1135 SymbolKind ProcKind = GV->hasLocalLinkage() ? SymbolKind::S_LPROC32_ID
1136 : SymbolKind::S_GPROC32_ID;
1137 MCSymbol *ProcRecordEnd = beginSymbolRecord(ProcKind);
1138
1139 // These fields are filled in by tools like CVPACK which run after the fact.
1140 OS.AddComment("PtrParent");
1141 OS.emitInt32(0);
1142 OS.AddComment("PtrEnd");
1143 OS.emitInt32(0);
1144 OS.AddComment("PtrNext");
1145 OS.emitInt32(0);
1146 // This is the important bit that tells the debugger where the function
1147 // code is located and what's its size:
1148 OS.AddComment("Code size");
1149 OS.emitAbsoluteSymbolDiff(FI.End, Fn, 4);
1150 OS.AddComment("Offset after prologue");
1151 OS.emitInt32(0);
1152 OS.AddComment("Offset before epilogue");
1153 OS.emitInt32(0);
1154 OS.AddComment("Function type index");
1155 OS.emitInt32(getFuncIdForSubprogram(GV->getSubprogram()).getIndex());
1156 OS.AddComment("Function section relative address");
1157 OS.emitCOFFSecRel32(Fn, /*Offset=*/0);
1158 OS.AddComment("Function section index");
1159 OS.emitCOFFSectionIndex(Fn);
1160 OS.AddComment("Flags");
1161 OS.emitInt8(0);
1162 // Emit the function display name as a null-terminated string.
1163 OS.AddComment("Function name");
1164 // Truncate the name so we won't overflow the record length field.
1165 emitNullTerminatedSymbolName(OS, FuncName);
1166 endSymbolRecord(ProcRecordEnd);
1167
1168 MCSymbol *FrameProcEnd = beginSymbolRecord(SymbolKind::S_FRAMEPROC);
1169 // Subtract out the CSR size since MSVC excludes that and we include it.
1170 OS.AddComment("FrameSize");
1171 OS.emitInt32(FI.FrameSize - FI.CSRSize);
1172 OS.AddComment("Padding");
1173 OS.emitInt32(0);
1174 OS.AddComment("Offset of padding");
1175 OS.emitInt32(0);
1176 OS.AddComment("Bytes of callee saved registers");
1177 OS.emitInt32(FI.CSRSize);
1178 OS.AddComment("Exception handler offset");
1179 OS.emitInt32(0);
1180 OS.AddComment("Exception handler section");
1181 OS.emitInt16(0);
1182 OS.AddComment("Flags (defines frame register)");
1183 OS.emitInt32(uint32_t(FI.FrameProcOpts));
1184 endSymbolRecord(FrameProcEnd);
1185
1186 emitLocalVariableList(FI, FI.Locals);
1187 emitGlobalVariableList(FI.Globals);
1188 emitLexicalBlockList(FI.ChildBlocks, FI);
1189
1190 // Emit inlined call site information. Only emit functions inlined directly
1191 // into the parent function. We'll emit the other sites recursively as part
1192 // of their parent inline site.
1193 for (const DILocation *InlinedAt : FI.ChildSites) {
1194 auto I = FI.InlineSites.find(InlinedAt);
1195 assert(I != FI.InlineSites.end() &&
1196 "child site not in function inline site map");
1197 emitInlinedCallSite(FI, InlinedAt, I->second);
1198 }
1199
1200 for (auto Annot : FI.Annotations) {
1201 MCSymbol *Label = Annot.first;
1202 MDTuple *Strs = cast<MDTuple>(Annot.second);
1203 MCSymbol *AnnotEnd = beginSymbolRecord(SymbolKind::S_ANNOTATION);
1204 OS.emitCOFFSecRel32(Label, /*Offset=*/0);
1205 // FIXME: Make sure we don't overflow the max record size.
1206 OS.emitCOFFSectionIndex(Label);
1207 OS.emitInt16(Strs->getNumOperands());
1208 for (Metadata *MD : Strs->operands()) {
1209 // MDStrings are null terminated, so we can do EmitBytes and get the
1210 // nice .asciz directive.
1211 StringRef Str = cast<MDString>(MD)->getString();
1212 assert(Str.data()[Str.size()] == '\0' && "non-nullterminated MDString");
1213 OS.emitBytes(StringRef(Str.data(), Str.size() + 1));
1214 }
1215 endSymbolRecord(AnnotEnd);
1216 }
1217
1218 for (auto HeapAllocSite : FI.HeapAllocSites) {
1219 const MCSymbol *BeginLabel = std::get<0>(HeapAllocSite);
1220 const MCSymbol *EndLabel = std::get<1>(HeapAllocSite);
1221 const DIType *DITy = std::get<2>(HeapAllocSite);
1222 MCSymbol *HeapAllocEnd = beginSymbolRecord(SymbolKind::S_HEAPALLOCSITE);
1223 OS.AddComment("Call site offset");
1224 OS.emitCOFFSecRel32(BeginLabel, /*Offset=*/0);
1225 OS.AddComment("Call site section index");
1226 OS.emitCOFFSectionIndex(BeginLabel);
1227 OS.AddComment("Call instruction length");
1228 OS.emitAbsoluteSymbolDiff(EndLabel, BeginLabel, 2);
1229 OS.AddComment("Type index");
1230 OS.emitInt32(getCompleteTypeIndex(DITy).getIndex());
1231 endSymbolRecord(HeapAllocEnd);
1232 }
1233
1234 if (SP != nullptr)
1235 emitDebugInfoForUDTs(LocalUDTs);
1236
1237 // We're done with this function.
1238 emitEndSymbolRecord(SymbolKind::S_PROC_ID_END);
1239 }
1240 endCVSubsection(SymbolsEnd);
1241
1242 // We have an assembler directive that takes care of the whole line table.
1243 OS.emitCVLinetableDirective(FI.FuncId, Fn, FI.End);
1244}
1245
1247CodeViewDebug::createDefRangeMem(uint16_t CVRegister, int Offset) {
1248 LocalVarDef DR;
1249 DR.InMemory = -1;
1250 DR.DataOffset = Offset;
1251 assert(DR.DataOffset == Offset && "truncation");
1252 DR.IsSubfield = 0;
1253 DR.StructOffset = 0;
1254 DR.CVRegister = CVRegister;
1255 return DR;
1256}
1257
1258void CodeViewDebug::collectVariableInfoFromMFTable(
1259 DenseSet<InlinedEntity> &Processed) {
1260 const MachineFunction &MF = *Asm->MF;
1261 const TargetSubtargetInfo &TSI = MF.getSubtarget();
1262 const TargetFrameLowering *TFI = TSI.getFrameLowering();
1263 const TargetRegisterInfo *TRI = TSI.getRegisterInfo();
1264
1266 if (!VI.Var)
1267 continue;
1268 assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) &&
1269 "Expected inlined-at fields to agree");
1270
1271 Processed.insert(InlinedEntity(VI.Var, VI.Loc->getInlinedAt()));
1273
1274 // If variable scope is not found then skip this variable.
1275 if (!Scope)
1276 continue;
1277
1278 // If the variable has an attached offset expression, extract it.
1279 // FIXME: Try to handle DW_OP_deref as well.
1280 int64_t ExprOffset = 0;
1281 bool Deref = false;
1282 if (VI.Expr) {
1283 // If there is one DW_OP_deref element, use offset of 0 and keep going.
1284 if (VI.Expr->getNumElements() == 1 &&
1285 VI.Expr->getElement(0) == llvm::dwarf::DW_OP_deref)
1286 Deref = true;
1287 else if (!VI.Expr->extractIfOffset(ExprOffset))
1288 continue;
1289 }
1290
1291 // Get the frame register used and the offset.
1292 Register FrameReg;
1293 StackOffset FrameOffset = TFI->getFrameIndexReference(*Asm->MF, VI.Slot, FrameReg);
1294 uint16_t CVReg = TRI->getCodeViewRegNum(FrameReg);
1295
1296 assert(!FrameOffset.getScalable() &&
1297 "Frame offsets with a scalable component are not supported");
1298
1299 // Calculate the label ranges.
1300 LocalVarDef DefRange =
1301 createDefRangeMem(CVReg, FrameOffset.getFixed() + ExprOffset);
1302
1303 LocalVariable Var;
1304 Var.DIVar = VI.Var;
1305
1306 for (const InsnRange &Range : Scope->getRanges()) {
1307 const MCSymbol *Begin = getLabelBeforeInsn(Range.first);
1308 const MCSymbol *End = getLabelAfterInsn(Range.second);
1309 End = End ? End : Asm->getFunctionEnd();
1310 Var.DefRanges[DefRange].emplace_back(Begin, End);
1311 }
1312
1313 if (Deref)
1314 Var.UseReferenceType = true;
1315
1316 recordLocalVariable(std::move(Var), Scope);
1317 }
1318}
1319
1321 return !Loc.LoadChain.empty() && Loc.LoadChain.back() == 0;
1322}
1323
1325 return Loc.LoadChain.size() == 2 && Loc.LoadChain.back() == 0;
1326}
1327
1328void CodeViewDebug::calculateRanges(
1329 LocalVariable &Var, const DbgValueHistoryMap::Entries &Entries) {
1331
1332 // Calculate the definition ranges.
1333 for (auto I = Entries.begin(), E = Entries.end(); I != E; ++I) {
1334 const auto &Entry = *I;
1335 if (!Entry.isDbgValue())
1336 continue;
1337 const MachineInstr *DVInst = Entry.getInstr();
1338 assert(DVInst->isDebugValue() && "Invalid History entry");
1339 // FIXME: Find a way to represent constant variables, since they are
1340 // relatively common.
1341 std::optional<DbgVariableLocation> Location =
1343 if (!Location)
1344 {
1345 // When we don't have a location this is usually because LLVM has
1346 // transformed it into a constant and we only have an llvm.dbg.value. We
1347 // can't represent these well in CodeView since S_LOCAL only works on
1348 // registers and memory locations. Instead, we will pretend this to be a
1349 // constant value to at least have it show up in the debugger.
1350 auto Op = DVInst->getDebugOperand(0);
1351 if (Op.isImm())
1352 Var.ConstantValue = APSInt(APInt(64, Op.getImm()), false);
1353 continue;
1354 }
1355
1356 // CodeView can only express variables in register and variables in memory
1357 // at a constant offset from a register. However, for variables passed
1358 // indirectly by pointer, it is common for that pointer to be spilled to a
1359 // stack location. For the special case of one offseted load followed by a
1360 // zero offset load (a pointer spilled to the stack), we change the type of
1361 // the local variable from a value type to a reference type. This tricks the
1362 // debugger into doing the load for us.
1363 if (Var.UseReferenceType) {
1364 // We're using a reference type. Drop the last zero offset load.
1365 if (canUseReferenceType(*Location))
1366 Location->LoadChain.pop_back();
1367 else
1368 continue;
1369 } else if (needsReferenceType(*Location)) {
1370 // This location can't be expressed without switching to a reference type.
1371 // Start over using that.
1372 Var.UseReferenceType = true;
1373 Var.DefRanges.clear();
1374 calculateRanges(Var, Entries);
1375 return;
1376 }
1377
1378 // We can only handle a register or an offseted load of a register.
1379 if (Location->Register == 0 || Location->LoadChain.size() > 1)
1380 continue;
1381
1382 LocalVarDef DR;
1383 DR.CVRegister = TRI->getCodeViewRegNum(Location->Register);
1384 DR.InMemory = !Location->LoadChain.empty();
1385 DR.DataOffset =
1386 !Location->LoadChain.empty() ? Location->LoadChain.back() : 0;
1387 if (Location->FragmentInfo) {
1388 DR.IsSubfield = true;
1389 DR.StructOffset = Location->FragmentInfo->OffsetInBits / 8;
1390 } else {
1391 DR.IsSubfield = false;
1392 DR.StructOffset = 0;
1393 }
1394
1395 // Compute the label range.
1396 const MCSymbol *Begin = getLabelBeforeInsn(Entry.getInstr());
1397 const MCSymbol *End;
1398 if (Entry.getEndIndex() != DbgValueHistoryMap::NoEntry) {
1399 auto &EndingEntry = Entries[Entry.getEndIndex()];
1400 End = EndingEntry.isDbgValue()
1401 ? getLabelBeforeInsn(EndingEntry.getInstr())
1402 : getLabelAfterInsn(EndingEntry.getInstr());
1403 } else
1404 End = Asm->getFunctionEnd();
1405
1406 // If the last range end is our begin, just extend the last range.
1407 // Otherwise make a new range.
1409 Var.DefRanges[DR];
1410 if (!R.empty() && R.back().second == Begin)
1411 R.back().second = End;
1412 else
1413 R.emplace_back(Begin, End);
1414
1415 // FIXME: Do more range combining.
1416 }
1417}
1418
1419void CodeViewDebug::collectVariableInfo(const DISubprogram *SP) {
1420 DenseSet<InlinedEntity> Processed;
1421 // Grab the variable info that was squirreled away in the MMI side-table.
1422 collectVariableInfoFromMFTable(Processed);
1423
1424 for (const auto &I : DbgValues) {
1425 InlinedEntity IV = I.first;
1426 if (Processed.count(IV))
1427 continue;
1428 const DILocalVariable *DIVar = cast<DILocalVariable>(IV.first);
1429 const DILocation *InlinedAt = IV.second;
1430
1431 // Instruction ranges, specifying where IV is accessible.
1432 const auto &Entries = I.second;
1433
1434 LexicalScope *Scope = nullptr;
1435 if (InlinedAt)
1436 Scope = LScopes.findInlinedScope(DIVar->getScope(), InlinedAt);
1437 else
1439 // If variable scope is not found then skip this variable.
1440 if (!Scope)
1441 continue;
1442
1443 LocalVariable Var;
1444 Var.DIVar = DIVar;
1445
1446 calculateRanges(Var, Entries);
1447 recordLocalVariable(std::move(Var), Scope);
1448 }
1449}
1450
1452 const TargetSubtargetInfo &TSI = MF->getSubtarget();
1453 const TargetRegisterInfo *TRI = TSI.getRegisterInfo();
1454 const MachineFrameInfo &MFI = MF->getFrameInfo();
1455 const Function &GV = MF->getFunction();
1456 auto Insertion = FnDebugInfo.insert({&GV, std::make_unique<FunctionInfo>()});
1457 assert(Insertion.second && "function already has info");
1458 CurFn = Insertion.first->second.get();
1459 CurFn->FuncId = NextFuncId++;
1460 CurFn->Begin = Asm->getFunctionBegin();
1461
1462 // The S_FRAMEPROC record reports the stack size, and how many bytes of
1463 // callee-saved registers were used. For targets that don't use a PUSH
1464 // instruction (AArch64), this will be zero.
1465 CurFn->CSRSize = MFI.getCVBytesOfCalleeSavedRegisters();
1466 CurFn->FrameSize = MFI.getStackSize();
1467 CurFn->OffsetAdjustment = MFI.getOffsetAdjustment();
1468 CurFn->HasStackRealignment = TRI->hasStackRealignment(*MF);
1469
1470 // For this function S_FRAMEPROC record, figure out which codeview register
1471 // will be the frame pointer.
1472 CurFn->EncodedParamFramePtrReg = EncodedFramePtrReg::None; // None.
1473 CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::None; // None.
1474 if (CurFn->FrameSize > 0) {
1475 if (!TSI.getFrameLowering()->hasFP(*MF)) {
1476 CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::StackPtr;
1477 CurFn->EncodedParamFramePtrReg = EncodedFramePtrReg::StackPtr;
1478 } else {
1479 // If there is an FP, parameters are always relative to it.
1480 CurFn->EncodedParamFramePtrReg = EncodedFramePtrReg::FramePtr;
1481 if (CurFn->HasStackRealignment) {
1482 // If the stack needs realignment, locals are relative to SP or VFRAME.
1483 CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::StackPtr;
1484 } else {
1485 // Otherwise, locals are relative to EBP, and we probably have VLAs or
1486 // other stack adjustments.
1487 CurFn->EncodedLocalFramePtrReg = EncodedFramePtrReg::FramePtr;
1488 }
1489 }
1490 }
1491
1492 // Compute other frame procedure options.
1493 FrameProcedureOptions FPO = FrameProcedureOptions::None;
1494 if (MFI.hasVarSizedObjects())
1495 FPO |= FrameProcedureOptions::HasAlloca;
1496 if (MF->exposesReturnsTwice())
1497 FPO |= FrameProcedureOptions::HasSetJmp;
1498 // FIXME: Set HasLongJmp if we ever track that info.
1499 if (MF->hasInlineAsm())
1500 FPO |= FrameProcedureOptions::HasInlineAssembly;
1501 if (GV.hasPersonalityFn()) {
1504 FPO |= FrameProcedureOptions::HasStructuredExceptionHandling;
1505 else
1506 FPO |= FrameProcedureOptions::HasExceptionHandling;
1507 }
1508 if (GV.hasFnAttribute(Attribute::InlineHint))
1509 FPO |= FrameProcedureOptions::MarkedInline;
1510 if (GV.hasFnAttribute(Attribute::Naked))
1511 FPO |= FrameProcedureOptions::Naked;
1512 if (MFI.hasStackProtectorIndex()) {
1513 FPO |= FrameProcedureOptions::SecurityChecks;
1514 if (GV.hasFnAttribute(Attribute::StackProtectStrong) ||
1515 GV.hasFnAttribute(Attribute::StackProtectReq)) {
1516 FPO |= FrameProcedureOptions::StrictSecurityChecks;
1517 }
1518 } else if (!GV.hasStackProtectorFnAttr()) {
1519 // __declspec(safebuffers) disables stack guards.
1520 FPO |= FrameProcedureOptions::SafeBuffers;
1521 }
1522 FPO |= FrameProcedureOptions(uint32_t(CurFn->EncodedLocalFramePtrReg) << 14U);
1523 FPO |= FrameProcedureOptions(uint32_t(CurFn->EncodedParamFramePtrReg) << 16U);
1524 if (Asm->TM.getOptLevel() != CodeGenOpt::None &&
1525 !GV.hasOptSize() && !GV.hasOptNone())
1526 FPO |= FrameProcedureOptions::OptimizedForSpeed;
1527 if (GV.hasProfileData()) {
1528 FPO |= FrameProcedureOptions::ValidProfileCounts;
1529 FPO |= FrameProcedureOptions::ProfileGuidedOptimization;
1530 }
1531 // FIXME: Set GuardCfg when it is implemented.
1532 CurFn->FrameProcOpts = FPO;
1533
1534 OS.emitCVFuncIdDirective(CurFn->FuncId);
1535
1536 // Find the end of the function prolog. First known non-DBG_VALUE and
1537 // non-frame setup location marks the beginning of the function body.
1538 // FIXME: is there a simpler a way to do this? Can we just search
1539 // for the first instruction of the function, not the last of the prolog?
1541 bool EmptyPrologue = true;
1542 for (const auto &MBB : *MF) {
1543 for (const auto &MI : MBB) {
1544 if (!MI.isMetaInstruction() && !MI.getFlag(MachineInstr::FrameSetup) &&
1545 MI.getDebugLoc()) {
1546 PrologEndLoc = MI.getDebugLoc();
1547 break;
1548 } else if (!MI.isMetaInstruction()) {
1549 EmptyPrologue = false;
1550 }
1551 }
1552 }
1553
1554 // Record beginning of function if we have a non-empty prologue.
1555 if (PrologEndLoc && !EmptyPrologue) {
1556 DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc();
1557 maybeRecordLocation(FnStartDL, MF);
1558 }
1559
1560 // Find heap alloc sites and emit labels around them.
1561 for (const auto &MBB : *MF) {
1562 for (const auto &MI : MBB) {
1563 if (MI.getHeapAllocMarker()) {
1566 }
1567 }
1568 }
1569}
1570
1571static bool shouldEmitUdt(const DIType *T) {
1572 if (!T)
1573 return false;
1574
1575 // MSVC does not emit UDTs for typedefs that are scoped to classes.
1576 if (T->getTag() == dwarf::DW_TAG_typedef) {
1577 if (DIScope *Scope = T->getScope()) {
1578 switch (Scope->getTag()) {
1579 case dwarf::DW_TAG_structure_type:
1580 case dwarf::DW_TAG_class_type:
1581 case dwarf::DW_TAG_union_type:
1582 return false;
1583 default:
1584 // do nothing.
1585 ;
1586 }
1587 }
1588 }
1589
1590 while (true) {
1591 if (!T || T->isForwardDecl())
1592 return false;
1593
1594 const DIDerivedType *DT = dyn_cast<DIDerivedType>(T);
1595 if (!DT)
1596 return true;
1597 T = DT->getBaseType();
1598 }
1599 return true;
1600}
1601
1602void CodeViewDebug::addToUDTs(const DIType *Ty) {
1603 // Don't record empty UDTs.
1604 if (Ty->getName().empty())
1605 return;
1606 if (!shouldEmitUdt(Ty))
1607 return;
1608
1609 SmallVector<StringRef, 5> ParentScopeNames;
1610 const DISubprogram *ClosestSubprogram =
1611 collectParentScopeNames(Ty->getScope(), ParentScopeNames);
1612
1613 std::string FullyQualifiedName =
1614 formatNestedName(ParentScopeNames, getPrettyScopeName(Ty));
1615
1616 if (ClosestSubprogram == nullptr) {
1617 GlobalUDTs.emplace_back(std::move(FullyQualifiedName), Ty);
1618 } else if (ClosestSubprogram == CurrentSubprogram) {
1619 LocalUDTs.emplace_back(std::move(FullyQualifiedName), Ty);
1620 }
1621
1622 // TODO: What if the ClosestSubprogram is neither null or the current
1623 // subprogram? Currently, the UDT just gets dropped on the floor.
1624 //
1625 // The current behavior is not desirable. To get maximal fidelity, we would
1626 // need to perform all type translation before beginning emission of .debug$S
1627 // and then make LocalUDTs a member of FunctionInfo
1628}
1629
1630TypeIndex CodeViewDebug::lowerType(const DIType *Ty, const DIType *ClassTy) {
1631 // Generic dispatch for lowering an unknown type.
1632 switch (Ty->getTag()) {
1633 case dwarf::DW_TAG_array_type:
1634 return lowerTypeArray(cast<DICompositeType>(Ty));
1635 case dwarf::DW_TAG_typedef:
1636 return lowerTypeAlias(cast<DIDerivedType>(Ty));
1637 case dwarf::DW_TAG_base_type:
1638 return lowerTypeBasic(cast<DIBasicType>(Ty));
1639 case dwarf::DW_TAG_pointer_type:
1640 if (cast<DIDerivedType>(Ty)->getName() == "__vtbl_ptr_type")
1641 return lowerTypeVFTableShape(cast<DIDerivedType>(Ty));
1642 [[fallthrough]];
1643 case dwarf::DW_TAG_reference_type:
1644 case dwarf::DW_TAG_rvalue_reference_type:
1645 return lowerTypePointer(cast<DIDerivedType>(Ty));
1646 case dwarf::DW_TAG_ptr_to_member_type:
1647 return lowerTypeMemberPointer(cast<DIDerivedType>(Ty));
1648 case dwarf::DW_TAG_restrict_type:
1649 case dwarf::DW_TAG_const_type:
1650 case dwarf::DW_TAG_volatile_type:
1651 // TODO: add support for DW_TAG_atomic_type here
1652 return lowerTypeModifier(cast<DIDerivedType>(Ty));
1653 case dwarf::DW_TAG_subroutine_type:
1654 if (ClassTy) {
1655 // The member function type of a member function pointer has no
1656 // ThisAdjustment.
1657 return lowerTypeMemberFunction(cast<DISubroutineType>(Ty), ClassTy,
1658 /*ThisAdjustment=*/0,
1659 /*IsStaticMethod=*/false);
1660 }
1661 return lowerTypeFunction(cast<DISubroutineType>(Ty));
1662 case dwarf::DW_TAG_enumeration_type:
1663 return lowerTypeEnum(cast<DICompositeType>(Ty));
1664 case dwarf::DW_TAG_class_type:
1665 case dwarf::DW_TAG_structure_type:
1666 return lowerTypeClass(cast<DICompositeType>(Ty));
1667 case dwarf::DW_TAG_union_type:
1668 return lowerTypeUnion(cast<DICompositeType>(Ty));
1669 case dwarf::DW_TAG_string_type:
1670 return lowerTypeString(cast<DIStringType>(Ty));
1671 case dwarf::DW_TAG_unspecified_type:
1672 if (Ty->getName() == "decltype(nullptr)")
1673 return TypeIndex::NullptrT();
1674 return TypeIndex::None();
1675 default:
1676 // Use the null type index.
1677 return TypeIndex();
1678 }
1679}
1680
1681TypeIndex CodeViewDebug::lowerTypeAlias(const DIDerivedType *Ty) {
1682 TypeIndex UnderlyingTypeIndex = getTypeIndex(Ty->getBaseType());
1683 StringRef TypeName = Ty->getName();
1684
1685 addToUDTs(Ty);
1686
1687 if (UnderlyingTypeIndex == TypeIndex(SimpleTypeKind::Int32Long) &&
1688 TypeName == "HRESULT")
1689 return TypeIndex(SimpleTypeKind::HResult);
1690 if (UnderlyingTypeIndex == TypeIndex(SimpleTypeKind::UInt16Short) &&
1691 TypeName == "wchar_t")
1692 return TypeIndex(SimpleTypeKind::WideCharacter);
1693
1694 return UnderlyingTypeIndex;
1695}
1696
1697TypeIndex CodeViewDebug::lowerTypeArray(const DICompositeType *Ty) {
1698 const DIType *ElementType = Ty->getBaseType();
1699 TypeIndex ElementTypeIndex = getTypeIndex(ElementType);
1700 // IndexType is size_t, which depends on the bitness of the target.
1701 TypeIndex IndexType = getPointerSizeInBytes() == 8
1702 ? TypeIndex(SimpleTypeKind::UInt64Quad)
1704
1705 uint64_t ElementSize = getBaseTypeSize(ElementType) / 8;
1706
1707 // Add subranges to array type.
1708 DINodeArray Elements = Ty->getElements();
1709 for (int i = Elements.size() - 1; i >= 0; --i) {
1710 const DINode *Element = Elements[i];
1711 assert(Element->getTag() == dwarf::DW_TAG_subrange_type);
1712
1713 const DISubrange *Subrange = cast<DISubrange>(Element);
1714 int64_t Count = -1;
1715
1716 // If Subrange has a Count field, use it.
1717 // Otherwise, if it has an upperboud, use (upperbound - lowerbound + 1),
1718 // where lowerbound is from the LowerBound field of the Subrange,
1719 // or the language default lowerbound if that field is unspecified.
1720 if (auto *CI = Subrange->getCount().dyn_cast<ConstantInt *>())
1721 Count = CI->getSExtValue();
1722 else if (auto *UI = Subrange->getUpperBound().dyn_cast<ConstantInt *>()) {
1723 // Fortran uses 1 as the default lowerbound; other languages use 0.
1724 int64_t Lowerbound = (moduleIsInFortran()) ? 1 : 0;
1725 auto *LI = Subrange->getLowerBound().dyn_cast<ConstantInt *>();
1726 Lowerbound = (LI) ? LI->getSExtValue() : Lowerbound;
1727 Count = UI->getSExtValue() - Lowerbound + 1;
1728 }
1729
1730 // Forward declarations of arrays without a size and VLAs use a count of -1.
1731 // Emit a count of zero in these cases to match what MSVC does for arrays
1732 // without a size. MSVC doesn't support VLAs, so it's not clear what we
1733 // should do for them even if we could distinguish them.
1734 if (Count == -1)
1735 Count = 0;
1736
1737 // Update the element size and element type index for subsequent subranges.
1738 ElementSize *= Count;
1739
1740 // If this is the outermost array, use the size from the array. It will be
1741 // more accurate if we had a VLA or an incomplete element type size.
1742 uint64_t ArraySize =
1743 (i == 0 && ElementSize == 0) ? Ty->getSizeInBits() / 8 : ElementSize;
1744
1745 StringRef Name = (i == 0) ? Ty->getName() : "";
1746 ArrayRecord AR(ElementTypeIndex, IndexType, ArraySize, Name);
1747 ElementTypeIndex = TypeTable.writeLeafType(AR);
1748 }
1749
1750 return ElementTypeIndex;
1751}
1752
1753// This function lowers a Fortran character type (DIStringType).
1754// Note that it handles only the character*n variant (using SizeInBits
1755// field in DIString to describe the type size) at the moment.
1756// Other variants (leveraging the StringLength and StringLengthExp
1757// fields in DIStringType) remain TBD.
1758TypeIndex CodeViewDebug::lowerTypeString(const DIStringType *Ty) {
1759 TypeIndex CharType = TypeIndex(SimpleTypeKind::NarrowCharacter);
1760 uint64_t ArraySize = Ty->getSizeInBits() >> 3;
1761 StringRef Name = Ty->getName();
1762 // IndexType is size_t, which depends on the bitness of the target.
1763 TypeIndex IndexType = getPointerSizeInBytes() == 8
1764 ? TypeIndex(SimpleTypeKind::UInt64Quad)
1766
1767 // Create a type of character array of ArraySize.
1768 ArrayRecord AR(CharType, IndexType, ArraySize, Name);
1769
1770 return TypeTable.writeLeafType(AR);
1771}
1772
1773TypeIndex CodeViewDebug::lowerTypeBasic(const DIBasicType *Ty) {
1776 uint32_t ByteSize;
1777
1778 Kind = static_cast<dwarf::TypeKind>(Ty->getEncoding());
1779 ByteSize = Ty->getSizeInBits() / 8;
1780
1781 SimpleTypeKind STK = SimpleTypeKind::None;
1782 switch (Kind) {
1783 case dwarf::DW_ATE_address:
1784 // FIXME: Translate
1785 break;
1786 case dwarf::DW_ATE_boolean:
1787 switch (ByteSize) {
1788 case 1: STK = SimpleTypeKind::Boolean8; break;
1789 case 2: STK = SimpleTypeKind::Boolean16; break;
1790 case 4: STK = SimpleTypeKind::Boolean32; break;
1791 case 8: STK = SimpleTypeKind::Boolean64; break;
1792 case 16: STK = SimpleTypeKind::Boolean128; break;
1793 }
1794 break;
1795 case dwarf::DW_ATE_complex_float:
1796 switch (ByteSize) {
1797 case 2: STK = SimpleTypeKind::Complex16; break;
1798 case 4: STK = SimpleTypeKind::Complex32; break;
1799 case 8: STK = SimpleTypeKind::Complex64; break;
1800 case 10: STK = SimpleTypeKind::Complex80; break;
1801 case 16: STK = SimpleTypeKind::Complex128; break;
1802 }
1803 break;
1804 case dwarf::DW_ATE_float:
1805 switch (ByteSize) {
1806 case 2: STK = SimpleTypeKind::Float16; break;
1807 case 4: STK = SimpleTypeKind::Float32; break;
1808 case 6: STK = SimpleTypeKind::Float48; break;
1809 case 8: STK = SimpleTypeKind::Float64; break;
1810 case 10: STK = SimpleTypeKind::Float80; break;
1811 case 16: STK = SimpleTypeKind::Float128; break;
1812 }
1813 break;
1814 case dwarf::DW_ATE_signed:
1815 switch (ByteSize) {
1816 case 1: STK = SimpleTypeKind::SignedCharacter; break;
1817 case 2: STK = SimpleTypeKind::Int16Short; break;
1818 case 4: STK = SimpleTypeKind::Int32; break;
1819 case 8: STK = SimpleTypeKind::Int64Quad; break;
1820 case 16: STK = SimpleTypeKind::Int128Oct; break;
1821 }
1822 break;
1823 case dwarf::DW_ATE_unsigned:
1824 switch (ByteSize) {
1825 case 1: STK = SimpleTypeKind::UnsignedCharacter; break;
1826 case 2: STK = SimpleTypeKind::UInt16Short; break;
1827 case 4: STK = SimpleTypeKind::UInt32; break;
1828 case 8: STK = SimpleTypeKind::UInt64Quad; break;
1829 case 16: STK = SimpleTypeKind::UInt128Oct; break;
1830 }
1831 break;
1832 case dwarf::DW_ATE_UTF:
1833 switch (ByteSize) {
1834 case 1: STK = SimpleTypeKind::Character8; break;
1835 case 2: STK = SimpleTypeKind::Character16; break;
1836 case 4: STK = SimpleTypeKind::Character32; break;
1837 }
1838 break;
1839 case dwarf::DW_ATE_signed_char:
1840 if (ByteSize == 1)
1841 STK = SimpleTypeKind::SignedCharacter;
1842 break;
1843 case dwarf::DW_ATE_unsigned_char:
1844 if (ByteSize == 1)
1845 STK = SimpleTypeKind::UnsignedCharacter;
1846 break;
1847 default:
1848 break;
1849 }
1850
1851 // Apply some fixups based on the source-level type name.
1852 // Include some amount of canonicalization from an old naming scheme Clang
1853 // used to use for integer types (in an outdated effort to be compatible with
1854 // GCC's debug info/GDB's behavior, which has since been addressed).
1855 if (STK == SimpleTypeKind::Int32 &&
1856 (Ty->getName() == "long int" || Ty->getName() == "long"))
1857 STK = SimpleTypeKind::Int32Long;
1858 if (STK == SimpleTypeKind::UInt32 && (Ty->getName() == "long unsigned int" ||
1859 Ty->getName() == "unsigned long"))
1860 STK = SimpleTypeKind::UInt32Long;
1861 if (STK == SimpleTypeKind::UInt16Short &&
1862 (Ty->getName() == "wchar_t" || Ty->getName() == "__wchar_t"))
1863 STK = SimpleTypeKind::WideCharacter;
1864 if ((STK == SimpleTypeKind::SignedCharacter ||
1865 STK == SimpleTypeKind::UnsignedCharacter) &&
1866 Ty->getName() == "char")
1867 STK = SimpleTypeKind::NarrowCharacter;
1868
1869 return TypeIndex(STK);
1870}
1871
1872TypeIndex CodeViewDebug::lowerTypePointer(const DIDerivedType *Ty,
1873 PointerOptions PO) {
1874 TypeIndex PointeeTI = getTypeIndex(Ty->getBaseType());
1875
1876 // Pointers to simple types without any options can use SimpleTypeMode, rather
1877 // than having a dedicated pointer type record.
1878 if (PointeeTI.isSimple() && PO == PointerOptions::None &&
1879 PointeeTI.getSimpleMode() == SimpleTypeMode::Direct &&
1880 Ty->getTag() == dwarf::DW_TAG_pointer_type) {
1881 SimpleTypeMode Mode = Ty->getSizeInBits() == 64
1882 ? SimpleTypeMode::NearPointer64
1883 : SimpleTypeMode::NearPointer32;
1884 return TypeIndex(PointeeTI.getSimpleKind(), Mode);
1885 }
1886
1887 PointerKind PK =
1888 Ty->getSizeInBits() == 64 ? PointerKind::Near64 : PointerKind::Near32;
1889 PointerMode PM = PointerMode::Pointer;
1890 switch (Ty->getTag()) {
1891 default: llvm_unreachable("not a pointer tag type");
1892 case dwarf::DW_TAG_pointer_type:
1893 PM = PointerMode::Pointer;
1894 break;
1895 case dwarf::DW_TAG_reference_type:
1896 PM = PointerMode::LValueReference;
1897 break;
1898 case dwarf::DW_TAG_rvalue_reference_type:
1899 PM = PointerMode::RValueReference;
1900 break;
1901 }
1902
1903 if (Ty->isObjectPointer())
1904 PO |= PointerOptions::Const;
1905
1906 PointerRecord PR(PointeeTI, PK, PM, PO, Ty->getSizeInBits() / 8);
1907 return TypeTable.writeLeafType(PR);
1908}
1909
1911translatePtrToMemberRep(unsigned SizeInBytes, bool IsPMF, unsigned Flags) {
1912 // SizeInBytes being zero generally implies that the member pointer type was
1913 // incomplete, which can happen if it is part of a function prototype. In this
1914 // case, use the unknown model instead of the general model.
1915 if (IsPMF) {
1916 switch (Flags & DINode::FlagPtrToMemberRep) {
1917 case 0:
1918 return SizeInBytes == 0 ? PointerToMemberRepresentation::Unknown
1919 : PointerToMemberRepresentation::GeneralFunction;
1920 case DINode::FlagSingleInheritance:
1921 return PointerToMemberRepresentation::SingleInheritanceFunction;
1922 case DINode::FlagMultipleInheritance:
1923 return PointerToMemberRepresentation::MultipleInheritanceFunction;
1924 case DINode::FlagVirtualInheritance:
1925 return PointerToMemberRepresentation::VirtualInheritanceFunction;
1926 }
1927 } else {
1928 switch (Flags & DINode::FlagPtrToMemberRep) {
1929 case 0:
1930 return SizeInBytes == 0 ? PointerToMemberRepresentation::Unknown
1931 : PointerToMemberRepresentation::GeneralData;
1932 case DINode::FlagSingleInheritance:
1933 return PointerToMemberRepresentation::SingleInheritanceData;
1934 case DINode::FlagMultipleInheritance:
1935 return PointerToMemberRepresentation::MultipleInheritanceData;
1936 case DINode::FlagVirtualInheritance:
1937 return PointerToMemberRepresentation::VirtualInheritanceData;
1938 }
1939 }
1940 llvm_unreachable("invalid ptr to member representation");
1941}
1942
1943TypeIndex CodeViewDebug::lowerTypeMemberPointer(const DIDerivedType *Ty,
1944 PointerOptions PO) {
1945 assert(Ty->getTag() == dwarf::DW_TAG_ptr_to_member_type);
1946 bool IsPMF = isa<DISubroutineType>(Ty->getBaseType());
1947 TypeIndex ClassTI = getTypeIndex(Ty->getClassType());
1948 TypeIndex PointeeTI =
1949 getTypeIndex(Ty->getBaseType(), IsPMF ? Ty->getClassType() : nullptr);
1950 PointerKind PK = getPointerSizeInBytes() == 8 ? PointerKind::Near64
1951 : PointerKind::Near32;
1952 PointerMode PM = IsPMF ? PointerMode::PointerToMemberFunction
1953 : PointerMode::PointerToDataMember;
1954
1955 assert(Ty->getSizeInBits() / 8 <= 0xff && "pointer size too big");
1956 uint8_t SizeInBytes = Ty->getSizeInBits() / 8;
1958 ClassTI, translatePtrToMemberRep(SizeInBytes, IsPMF, Ty->getFlags()));
1959 PointerRecord PR(PointeeTI, PK, PM, PO, SizeInBytes, MPI);
1960 return TypeTable.writeLeafType(PR);
1961}
1962
1963/// Given a DWARF calling convention, get the CodeView equivalent. If we don't
1964/// have a translation, use the NearC convention.
1965static CallingConvention dwarfCCToCodeView(unsigned DwarfCC) {
1966 switch (DwarfCC) {
1967 case dwarf::DW_CC_normal: return CallingConvention::NearC;
1968 case dwarf::DW_CC_BORLAND_msfastcall: return CallingConvention::NearFast;
1969 case dwarf::DW_CC_BORLAND_thiscall: return CallingConvention::ThisCall;
1970 case dwarf::DW_CC_BORLAND_stdcall: return CallingConvention::NearStdCall;
1971 case dwarf::DW_CC_BORLAND_pascal: return CallingConvention::NearPascal;
1972 case dwarf::DW_CC_LLVM_vectorcall: return CallingConvention::NearVector;
1973 }
1974 return CallingConvention::NearC;
1975}
1976
1977TypeIndex CodeViewDebug::lowerTypeModifier(const DIDerivedType *Ty) {
1978 ModifierOptions Mods = ModifierOptions::None;
1979 PointerOptions PO = PointerOptions::None;
1980 bool IsModifier = true;
1981 const DIType *BaseTy = Ty;
1982 while (IsModifier && BaseTy) {
1983 // FIXME: Need to add DWARF tags for __unaligned and _Atomic
1984 switch (BaseTy->getTag()) {
1985 case dwarf::DW_TAG_const_type:
1986 Mods |= ModifierOptions::Const;
1987 PO |= PointerOptions::Const;
1988 break;
1989 case dwarf::DW_TAG_volatile_type:
1990 Mods |= ModifierOptions::Volatile;
1991 PO |= PointerOptions::Volatile;
1992 break;
1993 case dwarf::DW_TAG_restrict_type:
1994 // Only pointer types be marked with __restrict. There is no known flag
1995 // for __restrict in LF_MODIFIER records.
1996 PO |= PointerOptions::Restrict;
1997 break;
1998 default:
1999 IsModifier = false;
2000 break;
2001 }
2002 if (IsModifier)
2003 BaseTy = cast<DIDerivedType>(BaseTy)->getBaseType();
2004 }
2005
2006 // Check if the inner type will use an LF_POINTER record. If so, the
2007 // qualifiers will go in the LF_POINTER record. This comes up for types like
2008 // 'int *const' and 'int *__restrict', not the more common cases like 'const
2009 // char *'.
2010 if (BaseTy) {
2011 switch (BaseTy->getTag()) {
2012 case dwarf::DW_TAG_pointer_type:
2013 case dwarf::DW_TAG_reference_type:
2014 case dwarf::DW_TAG_rvalue_reference_type:
2015 return lowerTypePointer(cast<DIDerivedType>(BaseTy), PO);
2016 case dwarf::DW_TAG_ptr_to_member_type:
2017 return lowerTypeMemberPointer(cast<DIDerivedType>(BaseTy), PO);
2018 default:
2019 break;
2020 }
2021 }
2022
2023 TypeIndex ModifiedTI = getTypeIndex(BaseTy);
2024
2025 // Return the base type index if there aren't any modifiers. For example, the
2026 // metadata could contain restrict wrappers around non-pointer types.
2027 if (Mods == ModifierOptions::None)
2028 return ModifiedTI;
2029
2030 ModifierRecord MR(ModifiedTI, Mods);
2031 return TypeTable.writeLeafType(MR);
2032}
2033
2034TypeIndex CodeViewDebug::lowerTypeFunction(const DISubroutineType *Ty) {
2035 SmallVector<TypeIndex, 8> ReturnAndArgTypeIndices;
2036 for (const DIType *ArgType : Ty->getTypeArray())
2037 ReturnAndArgTypeIndices.push_back(getTypeIndex(ArgType));
2038
2039 // MSVC uses type none for variadic argument.
2040 if (ReturnAndArgTypeIndices.size() > 1 &&
2041 ReturnAndArgTypeIndices.back() == TypeIndex::Void()) {
2042 ReturnAndArgTypeIndices.back() = TypeIndex::None();
2043 }
2044 TypeIndex ReturnTypeIndex = TypeIndex::Void();
2045 ArrayRef<TypeIndex> ArgTypeIndices = std::nullopt;
2046 if (!ReturnAndArgTypeIndices.empty()) {
2047 auto ReturnAndArgTypesRef = ArrayRef(ReturnAndArgTypeIndices);
2048 ReturnTypeIndex = ReturnAndArgTypesRef.front();
2049 ArgTypeIndices = ReturnAndArgTypesRef.drop_front();
2050 }
2051
2052 ArgListRecord ArgListRec(TypeRecordKind::ArgList, ArgTypeIndices);
2053 TypeIndex ArgListIndex = TypeTable.writeLeafType(ArgListRec);
2054
2056
2058 ProcedureRecord Procedure(ReturnTypeIndex, CC, FO, ArgTypeIndices.size(),
2059 ArgListIndex);
2060 return TypeTable.writeLeafType(Procedure);
2061}
2062
2063TypeIndex CodeViewDebug::lowerTypeMemberFunction(const DISubroutineType *Ty,
2064 const DIType *ClassTy,
2065 int ThisAdjustment,
2066 bool IsStaticMethod,
2067 FunctionOptions FO) {
2068 // Lower the containing class type.
2069 TypeIndex ClassType = getTypeIndex(ClassTy);
2070
2071 DITypeRefArray ReturnAndArgs = Ty->getTypeArray();
2072
2073 unsigned Index = 0;
2074 SmallVector<TypeIndex, 8> ArgTypeIndices;
2075 TypeIndex ReturnTypeIndex = TypeIndex::Void();
2076 if (ReturnAndArgs.size() > Index) {
2077 ReturnTypeIndex = getTypeIndex(ReturnAndArgs[Index++]);
2078 }
2079
2080 // If the first argument is a pointer type and this isn't a static method,
2081 // treat it as the special 'this' parameter, which is encoded separately from
2082 // the arguments.
2083 TypeIndex ThisTypeIndex;
2084 if (!IsStaticMethod && ReturnAndArgs.size() > Index) {
2085 if (const DIDerivedType *PtrTy =
2086 dyn_cast_or_null<DIDerivedType>(ReturnAndArgs[Index])) {
2087 if (PtrTy->getTag() == dwarf::DW_TAG_pointer_type) {
2088 ThisTypeIndex = getTypeIndexForThisPtr(PtrTy, Ty);
2089 Index++;
2090 }
2091 }
2092 }
2093
2094 while (Index < ReturnAndArgs.size())
2095 ArgTypeIndices.push_back(getTypeIndex(ReturnAndArgs[Index++]));
2096
2097 // MSVC uses type none for variadic argument.
2098 if (!ArgTypeIndices.empty() && ArgTypeIndices.back() == TypeIndex::Void())
2099 ArgTypeIndices.back() = TypeIndex::None();
2100
2101 ArgListRecord ArgListRec(TypeRecordKind::ArgList, ArgTypeIndices);
2102 TypeIndex ArgListIndex = TypeTable.writeLeafType(ArgListRec);
2103
2105
2106 MemberFunctionRecord MFR(ReturnTypeIndex, ClassType, ThisTypeIndex, CC, FO,
2107 ArgTypeIndices.size(), ArgListIndex, ThisAdjustment);
2108 return TypeTable.writeLeafType(MFR);
2109}
2110
2111TypeIndex CodeViewDebug::lowerTypeVFTableShape(const DIDerivedType *Ty) {
2112 unsigned VSlotCount =
2113 Ty->getSizeInBits() / (8 * Asm->MAI->getCodePointerSize());
2114 SmallVector<VFTableSlotKind, 4> Slots(VSlotCount, VFTableSlotKind::Near);
2115
2116 VFTableShapeRecord VFTSR(Slots);
2117 return TypeTable.writeLeafType(VFTSR);
2118}
2119
2120static MemberAccess translateAccessFlags(unsigned RecordTag, unsigned Flags) {
2121 switch (Flags & DINode::FlagAccessibility) {
2122 case DINode::FlagPrivate: return MemberAccess::Private;
2123 case DINode::FlagPublic: return MemberAccess::Public;
2124 case DINode::FlagProtected: return MemberAccess::Protected;
2125 case 0:
2126 // If there was no explicit access control, provide the default for the tag.
2127 return RecordTag == dwarf::DW_TAG_class_type ? MemberAccess::Private
2128 : MemberAccess::Public;
2129 }
2130 llvm_unreachable("access flags are exclusive");
2131}
2132
2134 if (SP->isArtificial())
2135 return MethodOptions::CompilerGenerated;
2136
2137 // FIXME: Handle other MethodOptions.
2138
2139 return MethodOptions::None;
2140}
2141
2143 bool Introduced) {
2144 if (SP->getFlags() & DINode::FlagStaticMember)
2145 return MethodKind::Static;
2146
2147 switch (SP->getVirtuality()) {
2148 case dwarf::DW_VIRTUALITY_none:
2149 break;
2150 case dwarf::DW_VIRTUALITY_virtual:
2151 return Introduced ? MethodKind::IntroducingVirtual : MethodKind::Virtual;
2152 case dwarf::DW_VIRTUALITY_pure_virtual:
2153 return Introduced ? MethodKind::PureIntroducingVirtual
2154 : MethodKind::PureVirtual;
2155 default:
2156 llvm_unreachable("unhandled virtuality case");
2157 }
2158
2159 return MethodKind::Vanilla;
2160}
2161
2163 switch (Ty->getTag()) {
2164 case dwarf::DW_TAG_class_type:
2165 return TypeRecordKind::Class;
2166 case dwarf::DW_TAG_structure_type:
2167 return TypeRecordKind::Struct;
2168 default:
2169 llvm_unreachable("unexpected tag");
2170 }
2171}
2172
2173/// Return ClassOptions that should be present on both the forward declaration
2174/// and the defintion of a tag type.
2176 ClassOptions CO = ClassOptions::None;
2177
2178 // MSVC always sets this flag, even for local types. Clang doesn't always
2179 // appear to give every type a linkage name, which may be problematic for us.
2180 // FIXME: Investigate the consequences of not following them here.
2181 if (!Ty->getIdentifier().empty())
2182 CO |= ClassOptions::HasUniqueName;
2183
2184 // Put the Nested flag on a type if it appears immediately inside a tag type.
2185 // Do not walk the scope chain. Do not attempt to compute ContainsNestedClass
2186 // here. That flag is only set on definitions, and not forward declarations.
2187 const DIScope *ImmediateScope = Ty->getScope();
2188 if (ImmediateScope && isa<DICompositeType>(ImmediateScope))
2189 CO |= ClassOptions::Nested;
2190
2191 // Put the Scoped flag on function-local types. MSVC puts this flag for enum
2192 // type only when it has an immediate function scope. Clang never puts enums
2193 // inside DILexicalBlock scopes. Enum types, as generated by clang, are
2194 // always in function, class, or file scopes.
2195 if (Ty->getTag() == dwarf::DW_TAG_enumeration_type) {
2196 if (ImmediateScope && isa<DISubprogram>(ImmediateScope))
2197 CO |= ClassOptions::Scoped;
2198 } else {
2199 for (const DIScope *Scope = ImmediateScope; Scope != nullptr;
2200 Scope = Scope->getScope()) {
2201 if (isa<DISubprogram>(Scope)) {
2202 CO |= ClassOptions::Scoped;
2203 break;
2204 }
2205 }
2206 }
2207
2208 return CO;
2209}
2210
2211void CodeViewDebug::addUDTSrcLine(const DIType *Ty, TypeIndex TI) {
2212 switch (Ty->getTag()) {
2213 case dwarf::DW_TAG_class_type:
2214 case dwarf::DW_TAG_structure_type:
2215 case dwarf::DW_TAG_union_type:
2216 case dwarf::DW_TAG_enumeration_type:
2217 break;
2218 default:
2219 return;
2220 }
2221
2222 if (const auto *File = Ty->getFile()) {
2223 StringIdRecord SIDR(TypeIndex(0x0), getFullFilepath(File));
2224 TypeIndex SIDI = TypeTable.writeLeafType(SIDR);
2225
2226 UdtSourceLineRecord USLR(TI, SIDI, Ty->getLine());
2227 TypeTable.writeLeafType(USLR);
2228 }
2229}
2230
2231TypeIndex CodeViewDebug::lowerTypeEnum(const DICompositeType *Ty) {
2233 TypeIndex FTI;
2234 unsigned EnumeratorCount = 0;
2235
2236 if (Ty->isForwardDecl()) {
2237 CO |= ClassOptions::ForwardReference;
2238 } else {
2239 ContinuationRecordBuilder ContinuationBuilder;
2240 ContinuationBuilder.begin(ContinuationRecordKind::FieldList);
2241 for (const DINode *Element : Ty->getElements()) {
2242 // We assume that the frontend provides all members in source declaration
2243 // order, which is what MSVC does.
2244 if (auto *Enumerator = dyn_cast_or_null<DIEnumerator>(Element)) {
2245 // FIXME: Is it correct to always emit these as unsigned here?
2246 EnumeratorRecord ER(MemberAccess::Public,
2247 APSInt(Enumerator->getValue(), true),
2248 Enumerator->getName());
2249 ContinuationBuilder.writeMemberType(ER);
2250 EnumeratorCount++;
2251 }
2252 }
2253 FTI = TypeTable.insertRecord(ContinuationBuilder);
2254 }
2255
2256 std::string FullName = getFullyQualifiedName(Ty);
2257
2258 EnumRecord ER(EnumeratorCount, CO, FTI, FullName, Ty->getIdentifier(),
2259 getTypeIndex(Ty->getBaseType()));
2260 TypeIndex EnumTI = TypeTable.writeLeafType(ER);
2261
2262 addUDTSrcLine(Ty, EnumTI);
2263
2264 return EnumTI;
2265}
2266
2267//===----------------------------------------------------------------------===//
2268// ClassInfo
2269//===----------------------------------------------------------------------===//
2270
2272 struct MemberInfo {
2275 };
2276 // [MemberInfo]
2277 using MemberList = std::vector<MemberInfo>;
2278
2280 // MethodName -> MethodsList
2282
2283 /// Base classes.
2284 std::vector<const DIDerivedType *> Inheritance;
2285
2286 /// Direct members.
2288 // Direct overloaded methods gathered by name.
2290
2292
2293 std::vector<const DIType *> NestedTypes;
2294};
2295
2296void CodeViewDebug::clear() {
2297 assert(CurFn == nullptr);
2298 FileIdMap.clear();
2299 FnDebugInfo.clear();
2300 FileToFilepathMap.clear();
2301 LocalUDTs.clear();
2302 GlobalUDTs.clear();
2303 TypeIndices.clear();
2304 CompleteTypeIndices.clear();
2305 ScopeGlobals.clear();
2306 CVGlobalVariableOffsets.clear();
2307}
2308
2309void CodeViewDebug::collectMemberInfo(ClassInfo &Info,
2310 const DIDerivedType *DDTy) {
2311 if (!DDTy->getName().empty()) {
2312 Info.Members.push_back({DDTy, 0});
2313
2314 // Collect static const data members with values.
2315 if ((DDTy->getFlags() & DINode::FlagStaticMember) ==
2316 DINode::FlagStaticMember) {
2317 if (DDTy->getConstant() && (isa<ConstantInt>(DDTy->getConstant()) ||
2318 isa<ConstantFP>(DDTy->getConstant())))
2319 StaticConstMembers.push_back(DDTy);
2320 }
2321
2322 return;
2323 }
2324
2325 // An unnamed member may represent a nested struct or union. Attempt to
2326 // interpret the unnamed member as a DICompositeType possibly wrapped in
2327 // qualifier types. Add all the indirect fields to the current record if that
2328 // succeeds, and drop the member if that fails.
2329 assert((DDTy->getOffsetInBits() % 8) == 0 && "Unnamed bitfield member!");
2331 const DIType *Ty = DDTy->getBaseType();
2332 bool FullyResolved = false;
2333 while (!FullyResolved) {
2334 switch (Ty->getTag()) {
2335 case dwarf::DW_TAG_const_type:
2336 case dwarf::DW_TAG_volatile_type:
2337 // FIXME: we should apply the qualifier types to the indirect fields
2338 // rather than dropping them.
2339 Ty = cast<DIDerivedType>(Ty)->getBaseType();
2340 break;
2341 default:
2342 FullyResolved = true;
2343 break;
2344 }
2345 }
2346
2347 const DICompositeType *DCTy = dyn_cast<DICompositeType>(Ty);
2348 if (!DCTy)
2349 return;
2350
2351 ClassInfo NestedInfo = collectClassInfo(DCTy);
2352 for (const ClassInfo::MemberInfo &IndirectField : NestedInfo.Members)
2353 Info.Members.push_back(
2354 {IndirectField.MemberTypeNode, IndirectField.BaseOffset + Offset});
2355}
2356
2357ClassInfo CodeViewDebug::collectClassInfo(const DICompositeType *Ty) {
2359 // Add elements to structure type.
2360 DINodeArray Elements = Ty->getElements();
2361 for (auto *Element : Elements) {
2362 // We assume that the frontend provides all members in source declaration
2363 // order, which is what MSVC does.
2364 if (!Element)
2365 continue;
2366 if (auto *SP = dyn_cast<DISubprogram>(Element)) {
2367 Info.Methods[SP->getRawName()].push_back(SP);
2368 } else if (auto *DDTy = dyn_cast<DIDerivedType>(Element)) {
2369 if (DDTy->getTag() == dwarf::DW_TAG_member) {
2370 collectMemberInfo(Info, DDTy);
2371 } else if (DDTy->getTag() == dwarf::DW_TAG_inheritance) {
2372 Info.Inheritance.push_back(DDTy);
2373 } else if (DDTy->getTag() == dwarf::DW_TAG_pointer_type &&
2374 DDTy->getName() == "__vtbl_ptr_type") {
2375 Info.VShapeTI = getTypeIndex(DDTy);
2376 } else if (DDTy->getTag() == dwarf::DW_TAG_typedef) {
2377 Info.NestedTypes.push_back(DDTy);
2378 } else if (DDTy->getTag() == dwarf::DW_TAG_friend) {
2379 // Ignore friend members. It appears that MSVC emitted info about
2380 // friends in the past, but modern versions do not.
2381 }
2382 } else if (auto *Composite = dyn_cast<DICompositeType>(Element)) {
2383 Info.NestedTypes.push_back(Composite);
2384 }
2385 // Skip other unrecognized kinds of elements.
2386 }
2387 return Info;
2388}
2389
2391 // This routine is used by lowerTypeClass and lowerTypeUnion to determine
2392 // if a complete type should be emitted instead of a forward reference.
2393 return Ty->getName().empty() && Ty->getIdentifier().empty() &&
2394 !Ty->isForwardDecl();
2395}
2396
2397TypeIndex CodeViewDebug::lowerTypeClass(const DICompositeType *Ty) {
2398 // Emit the complete type for unnamed structs. C++ classes with methods
2399 // which have a circular reference back to the class type are expected to
2400 // be named by the front-end and should not be "unnamed". C unnamed
2401 // structs should not have circular references.
2403 // If this unnamed complete type is already in the process of being defined
2404 // then the description of the type is malformed and cannot be emitted
2405 // into CodeView correctly so report a fatal error.
2406 auto I = CompleteTypeIndices.find(Ty);
2407 if (I != CompleteTypeIndices.end() && I->second == TypeIndex())
2408 report_fatal_error("cannot debug circular reference to unnamed type");
2409 return getCompleteTypeIndex(Ty);
2410 }
2411
2412 // First, construct the forward decl. Don't look into Ty to compute the
2413 // forward decl options, since it might not be available in all TUs.
2415 ClassOptions CO =
2416 ClassOptions::ForwardReference | getCommonClassOptions(Ty);
2417 std::string FullName = getFullyQualifiedName(Ty);
2418 ClassRecord CR(Kind, 0, CO, TypeIndex(), TypeIndex(), TypeIndex(), 0,
2419 FullName, Ty->getIdentifier());
2420 TypeIndex FwdDeclTI = TypeTable.writeLeafType(CR);
2421 if (!Ty->isForwardDecl())
2422 DeferredCompleteTypes.push_back(Ty);
2423 return FwdDeclTI;
2424}
2425
2426TypeIndex CodeViewDebug::lowerCompleteTypeClass(const DICompositeType *Ty) {
2427 // Construct the field list and complete type record.
2430 TypeIndex FieldTI;
2431 TypeIndex VShapeTI;
2432 unsigned FieldCount;
2434 std::tie(FieldTI, VShapeTI, FieldCount, ContainsNestedClass) =
2435 lowerRecordFieldList(Ty);
2436
2438 CO |= ClassOptions::ContainsNestedClass;
2439
2440 // MSVC appears to set this flag by searching any destructor or method with
2441 // FunctionOptions::Constructor among the emitted members. Clang AST has all
2442 // the members, however special member functions are not yet emitted into
2443 // debug information. For now checking a class's non-triviality seems enough.
2444 // FIXME: not true for a nested unnamed struct.
2445 if (isNonTrivial(Ty))
2446 CO |= ClassOptions::HasConstructorOrDestructor;
2447
2448 std::string FullName = getFullyQualifiedName(Ty);
2449
2450 uint64_t SizeInBytes = Ty->getSizeInBits() / 8;
2451
2452 ClassRecord CR(Kind, FieldCount, CO, FieldTI, TypeIndex(), VShapeTI,
2453 SizeInBytes, FullName, Ty->getIdentifier());
2454 TypeIndex ClassTI = TypeTable.writeLeafType(CR);
2455
2456 addUDTSrcLine(Ty, ClassTI);
2457
2458 addToUDTs(Ty);
2459
2460 return ClassTI;
2461}
2462
2463TypeIndex CodeViewDebug::lowerTypeUnion(const DICompositeType *Ty) {
2464 // Emit the complete type for unnamed unions.
2466 return getCompleteTypeIndex(Ty);
2467
2468 ClassOptions CO =
2469 ClassOptions::ForwardReference | getCommonClassOptions(Ty);
2470 std::string FullName = getFullyQualifiedName(Ty);
2471 UnionRecord UR(0, CO, TypeIndex(), 0, FullName, Ty->getIdentifier());
2472 TypeIndex FwdDeclTI = TypeTable.writeLeafType(UR);
2473 if (!Ty->isForwardDecl())
2474 DeferredCompleteTypes.push_back(Ty);
2475 return FwdDeclTI;
2476}
2477
2478TypeIndex CodeViewDebug::lowerCompleteTypeUnion(const DICompositeType *Ty) {
2479 ClassOptions CO = ClassOptions::Sealed | getCommonClassOptions(Ty);
2480 TypeIndex FieldTI;
2481 unsigned FieldCount;
2483 std::tie(FieldTI, std::ignore, FieldCount, ContainsNestedClass) =
2484 lowerRecordFieldList(Ty);
2485
2487 CO |= ClassOptions::ContainsNestedClass;
2488
2489 uint64_t SizeInBytes = Ty->getSizeInBits() / 8;
2490 std::string FullName = getFullyQualifiedName(Ty);
2491
2492 UnionRecord UR(FieldCount, CO, FieldTI, SizeInBytes, FullName,
2493 Ty->getIdentifier());
2494 TypeIndex UnionTI = TypeTable.writeLeafType(UR);
2495
2496 addUDTSrcLine(Ty, UnionTI);
2497
2498 addToUDTs(Ty);
2499
2500 return UnionTI;
2501}
2502
2503std::tuple<TypeIndex, TypeIndex, unsigned, bool>
2504CodeViewDebug::lowerRecordFieldList(const DICompositeType *Ty) {
2505 // Manually count members. MSVC appears to count everything that generates a
2506 // field list record. Each individual overload in a method overload group
2507 // contributes to this count, even though the overload group is a single field
2508 // list record.
2509 unsigned MemberCount = 0;
2510 ClassInfo Info = collectClassInfo(Ty);
2511 ContinuationRecordBuilder ContinuationBuilder;
2512 ContinuationBuilder.begin(ContinuationRecordKind::FieldList);
2513
2514 // Create base classes.
2515 for (const DIDerivedType *I : Info.Inheritance) {
2516 if (I->getFlags() & DINode::FlagVirtual) {
2517 // Virtual base.
2518 unsigned VBPtrOffset = I->getVBPtrOffset();
2519 // FIXME: Despite the accessor name, the offset is really in bytes.
2520 unsigned VBTableIndex = I->getOffsetInBits() / 4;
2521 auto RecordKind = (I->getFlags() & DINode::FlagIndirectVirtualBase) == DINode::FlagIndirectVirtualBase
2522 ? TypeRecordKind::IndirectVirtualBaseClass
2523 : TypeRecordKind::VirtualBaseClass;
2525 RecordKind, translateAccessFlags(Ty->getTag(), I->getFlags()),
2526 getTypeIndex(I->getBaseType()), getVBPTypeIndex(), VBPtrOffset,
2527 VBTableIndex);
2528
2529 ContinuationBuilder.writeMemberType(VBCR);
2530 MemberCount++;
2531 } else {
2532 assert(I->getOffsetInBits() % 8 == 0 &&
2533 "bases must be on byte boundaries");
2534 BaseClassRecord BCR(translateAccessFlags(Ty->getTag(), I->getFlags()),
2535 getTypeIndex(I->getBaseType()),
2536 I->getOffsetInBits() / 8);
2537 ContinuationBuilder.writeMemberType(BCR);
2538 MemberCount++;
2539 }
2540 }
2541
2542 // Create members.
2543 for (ClassInfo::MemberInfo &MemberInfo : Info.Members) {
2544 const DIDerivedType *Member = MemberInfo.MemberTypeNode;
2545 TypeIndex MemberBaseType = getTypeIndex(Member->getBaseType());
2546 StringRef MemberName = Member->getName();
2547 MemberAccess Access =
2548 translateAccessFlags(Ty->getTag(), Member->getFlags());
2549
2550 if (Member->isStaticMember()) {
2551 StaticDataMemberRecord SDMR(Access, MemberBaseType, MemberName);
2552 ContinuationBuilder.writeMemberType(SDMR);
2553 MemberCount++;
2554 continue;
2555 }
2556
2557 // Virtual function pointer member.
2558 if ((Member->getFlags() & DINode::FlagArtificial) &&
2559 Member->getName().startswith("_vptr$")) {
2560 VFPtrRecord VFPR(getTypeIndex(Member->getBaseType()));
2561 ContinuationBuilder.writeMemberType(VFPR);
2562 MemberCount++;
2563 continue;
2564 }
2565
2566 // Data member.
2567 uint64_t MemberOffsetInBits =
2568 Member->getOffsetInBits() + MemberInfo.BaseOffset;
2569 if (Member->isBitField()) {
2570 uint64_t StartBitOffset = MemberOffsetInBits;
2571 if (const auto *CI =
2572 dyn_cast_or_null<ConstantInt>(Member->getStorageOffsetInBits())) {
2573 MemberOffsetInBits = CI->getZExtValue() + MemberInfo.BaseOffset;
2574 }
2575 StartBitOffset -= MemberOffsetInBits;
2576 BitFieldRecord BFR(MemberBaseType, Member->getSizeInBits(),
2577 StartBitOffset);
2578 MemberBaseType = TypeTable.writeLeafType(BFR);
2579 }
2580 uint64_t MemberOffsetInBytes = MemberOffsetInBits / 8;
2581 DataMemberRecord DMR(Access, MemberBaseType, MemberOffsetInBytes,
2582 MemberName);
2583 ContinuationBuilder.writeMemberType(DMR);
2584 MemberCount++;
2585 }
2586
2587 // Create methods
2588 for (auto &MethodItr : Info.Methods) {
2589 StringRef Name = MethodItr.first->getString();
2590
2591 std::vector<OneMethodRecord> Methods;
2592 for (const DISubprogram *SP : MethodItr.second) {
2593 TypeIndex MethodType = getMemberFunctionType(SP, Ty);
2594 bool Introduced = SP->getFlags() & DINode::FlagIntroducedVirtual;
2595
2596 unsigned VFTableOffset = -1;
2597 if (Introduced)
2598 VFTableOffset = SP->getVirtualIndex() * getPointerSizeInBytes();
2599
2600 Methods.push_back(OneMethodRecord(
2601 MethodType, translateAccessFlags(Ty->getTag(), SP->getFlags()),
2602 translateMethodKindFlags(SP, Introduced),
2603 translateMethodOptionFlags(SP), VFTableOffset, Name));
2604 MemberCount++;
2605 }
2606 assert(!Methods.empty() && "Empty methods map entry");
2607 if (Methods.size() == 1)
2608 ContinuationBuilder.writeMemberType(Methods[0]);
2609 else {
2610 // FIXME: Make this use its own ContinuationBuilder so that
2611 // MethodOverloadList can be split correctly.
2612 MethodOverloadListRecord MOLR(Methods);
2613 TypeIndex MethodList = TypeTable.writeLeafType(MOLR);
2614
2615 OverloadedMethodRecord OMR(Methods.size(), MethodList, Name);
2616 ContinuationBuilder.writeMemberType(OMR);
2617 }
2618 }
2619
2620 // Create nested classes.
2621 for (const DIType *Nested : Info.NestedTypes) {
2622 NestedTypeRecord R(getTypeIndex(Nested), Nested->getName());
2623 ContinuationBuilder.writeMemberType(R);
2624 MemberCount++;
2625 }
2626
2627 TypeIndex FieldTI = TypeTable.insertRecord(ContinuationBuilder);
2628 return std::make_tuple(FieldTI, Info.VShapeTI, MemberCount,
2629 !Info.NestedTypes.empty());
2630}
2631
2632TypeIndex CodeViewDebug::getVBPTypeIndex() {
2633 if (!VBPType.getIndex()) {
2634 // Make a 'const int *' type.
2635 ModifierRecord MR(TypeIndex::Int32(), ModifierOptions::Const);
2636 TypeIndex ModifiedTI = TypeTable.writeLeafType(MR);
2637
2638 PointerKind PK = getPointerSizeInBytes() == 8 ? PointerKind::Near64
2639 : PointerKind::Near32;
2640 PointerMode PM = PointerMode::Pointer;
2641 PointerOptions PO = PointerOptions::None;
2642 PointerRecord PR(ModifiedTI, PK, PM, PO, getPointerSizeInBytes());
2643 VBPType = TypeTable.writeLeafType(PR);
2644 }
2645
2646 return VBPType;
2647}
2648
2649TypeIndex CodeViewDebug::getTypeIndex(const DIType *Ty, const DIType *ClassTy) {
2650 // The null DIType is the void type. Don't try to hash it.
2651 if (!Ty)
2652 return TypeIndex::Void();
2653
2654 // Check if we've already translated this type. Don't try to do a
2655 // get-or-create style insertion that caches the hash lookup across the
2656 // lowerType call. It will update the TypeIndices map.
2657 auto I = TypeIndices.find({Ty, ClassTy});
2658 if (I != TypeIndices.end())
2659 return I->second;
2660
2661 TypeLoweringScope S(*this);
2662 TypeIndex TI = lowerType(Ty, ClassTy);
2663 return recordTypeIndexForDINode(Ty, TI, ClassTy);
2664}
2665
2667CodeViewDebug::getTypeIndexForThisPtr(const DIDerivedType *PtrTy,
2668 const DISubroutineType *SubroutineTy) {
2669 assert(PtrTy->getTag() == dwarf::DW_TAG_pointer_type &&
2670 "this type must be a pointer type");
2671
2672 PointerOptions Options = PointerOptions::None;
2673 if (SubroutineTy->getFlags() & DINode::DIFlags::FlagLValueReference)
2674 Options = PointerOptions::LValueRefThisPointer;
2675 else if (SubroutineTy->getFlags() & DINode::DIFlags::FlagRValueReference)
2676 Options = PointerOptions::RValueRefThisPointer;
2677
2678 // Check if we've already translated this type. If there is no ref qualifier
2679 // on the function then we look up this pointer type with no associated class
2680 // so that the TypeIndex for the this pointer can be shared with the type
2681 // index for other pointers to this class type. If there is a ref qualifier
2682 // then we lookup the pointer using the subroutine as the parent type.
2683 auto I = TypeIndices.find({PtrTy, SubroutineTy});
2684 if (I != TypeIndices.end())
2685 return I->second;
2686
2687 TypeLoweringScope S(*this);
2688 TypeIndex TI = lowerTypePointer(PtrTy, Options);
2689 return recordTypeIndexForDINode(PtrTy, TI, SubroutineTy);
2690}
2691
2692TypeIndex CodeViewDebug::getTypeIndexForReferenceTo(const DIType *Ty) {
2693 PointerRecord PR(getTypeIndex(Ty),
2694 getPointerSizeInBytes() == 8 ? PointerKind::Near64
2695 : PointerKind::Near32,
2696 PointerMode::LValueReference, PointerOptions::None,
2697 Ty->getSizeInBits() / 8);
2698 return TypeTable.writeLeafType(PR);
2699}
2700
2701TypeIndex CodeViewDebug::getCompleteTypeIndex(const DIType *Ty) {
2702 // The null DIType is the void type. Don't try to hash it.
2703 if (!Ty)
2704 return TypeIndex::Void();
2705
2706 // Look through typedefs when getting the complete type index. Call
2707 // getTypeIndex on the typdef to ensure that any UDTs are accumulated and are
2708 // emitted only once.
2709 if (Ty->getTag() == dwarf::DW_TAG_typedef)
2710 (void)getTypeIndex(Ty);
2711 while (Ty->getTag() == dwarf::DW_TAG_typedef)
2712 Ty = cast<DIDerivedType>(Ty)->getBaseType();
2713
2714 // If this is a non-record type, the complete type index is the same as the
2715 // normal type index. Just call getTypeIndex.
2716 switch (Ty->getTag()) {
2717 case dwarf::DW_TAG_class_type:
2718 case dwarf::DW_TAG_structure_type:
2719 case dwarf::DW_TAG_union_type:
2720 break;
2721 default:
2722 return getTypeIndex(Ty);
2723 }
2724
2725 const auto *CTy = cast<DICompositeType>(Ty);
2726
2727 TypeLoweringScope S(*this);
2728
2729 // Make sure the forward declaration is emitted first. It's unclear if this
2730 // is necessary, but MSVC does it, and we should follow suit until we can show
2731 // otherwise.
2732 // We only emit a forward declaration for named types.
2733 if (!CTy->getName().empty() || !CTy->getIdentifier().empty()) {
2734 TypeIndex FwdDeclTI = getTypeIndex(CTy);
2735
2736 // Just use the forward decl if we don't have complete type info. This
2737 // might happen if the frontend is using modules and expects the complete
2738 // definition to be emitted elsewhere.
2739 if (CTy->isForwardDecl())
2740 return FwdDeclTI;
2741 }
2742
2743 // Check if we've already translated the complete record type.
2744 // Insert the type with a null TypeIndex to signify that the type is currently
2745 // being lowered.
2746 auto InsertResult = CompleteTypeIndices.insert({CTy, TypeIndex()});
2747 if (!InsertResult.second)
2748 return InsertResult.first->second;
2749
2750 TypeIndex TI;
2751 switch (CTy->getTag()) {
2752 case dwarf::DW_TAG_class_type:
2753 case dwarf::DW_TAG_structure_type:
2754 TI = lowerCompleteTypeClass(CTy);
2755 break;
2756 case dwarf::DW_TAG_union_type:
2757 TI = lowerCompleteTypeUnion(CTy);
2758 break;
2759 default:
2760 llvm_unreachable("not a record");
2761 }
2762
2763 // Update the type index associated with this CompositeType. This cannot
2764 // use the 'InsertResult' iterator above because it is potentially
2765 // invalidated by map insertions which can occur while lowering the class
2766 // type above.
2767 CompleteTypeIndices[CTy] = TI;
2768 return TI;
2769}
2770
2771/// Emit all the deferred complete record types. Try to do this in FIFO order,
2772/// and do this until fixpoint, as each complete record type typically
2773/// references
2774/// many other record types.
2775void CodeViewDebug::emitDeferredCompleteTypes() {
2777 while (!DeferredCompleteTypes.empty()) {
2778 std::swap(DeferredCompleteTypes, TypesToEmit);
2779 for (const DICompositeType *RecordTy : TypesToEmit)
2780 getCompleteTypeIndex(RecordTy);
2781 TypesToEmit.clear();
2782 }
2783}
2784
2785void CodeViewDebug::emitLocalVariableList(const FunctionInfo &FI,
2786 ArrayRef<LocalVariable> Locals) {
2787 // Get the sorted list of parameters and emit them first.
2789 for (const LocalVariable &L : Locals)
2790 if (L.DIVar->isParameter())
2791 Params.push_back(&L);
2792 llvm::sort(Params, [](const LocalVariable *L, const LocalVariable *R) {
2793 return L->DIVar->getArg() < R->DIVar->getArg();
2794 });
2795 for (const LocalVariable *L : Params)
2796 emitLocalVariable(FI, *L);
2797
2798 // Next emit all non-parameters in the order that we found them.
2799 for (const LocalVariable &L : Locals) {
2800 if (!L.DIVar->isParameter()) {
2801 if (L.ConstantValue) {
2802 // If ConstantValue is set we will emit it as a S_CONSTANT instead of a
2803 // S_LOCAL in order to be able to represent it at all.
2804 const DIType *Ty = L.DIVar->getType();
2805 APSInt Val(*L.ConstantValue);
2806 emitConstantSymbolRecord(Ty, Val, std::string(L.DIVar->getName()));
2807 } else {
2808 emitLocalVariable(FI, L);
2809 }
2810 }
2811 }
2812}
2813
2814void CodeViewDebug::emitLocalVariable(const FunctionInfo &FI,
2815 const LocalVariable &Var) {
2816 // LocalSym record, see SymbolRecord.h for more info.
2817 MCSymbol *LocalEnd = beginSymbolRecord(SymbolKind::S_LOCAL);
2818
2819 LocalSymFlags Flags = LocalSymFlags::None;
2820 if (Var.DIVar->isParameter())
2821 Flags |= LocalSymFlags::IsParameter;
2822 if (Var.DefRanges.empty())
2823 Flags |= LocalSymFlags::IsOptimizedOut;
2824
2825 OS.AddComment("TypeIndex");
2826 TypeIndex TI = Var.UseReferenceType
2827 ? getTypeIndexForReferenceTo(Var.DIVar->getType())
2828 : getCompleteTypeIndex(Var.DIVar->getType());
2829 OS.emitInt32(TI.getIndex());
2830 OS.AddComment("Flags");
2831 OS.emitInt16(static_cast<uint16_t>(Flags));
2832 // Truncate the name so we won't overflow the record length field.
2833 emitNullTerminatedSymbolName(OS, Var.DIVar->getName());
2834 endSymbolRecord(LocalEnd);
2835
2836 // Calculate the on disk prefix of the appropriate def range record. The
2837 // records and on disk formats are described in SymbolRecords.h. BytePrefix
2838 // should be big enough to hold all forms without memory allocation.
2839 SmallString<20> BytePrefix;
2840 for (const auto &Pair : Var.DefRanges) {
2841 LocalVarDef DefRange = Pair.first;
2842 const auto &Ranges = Pair.second;
2843 BytePrefix.clear();
2844 if (DefRange.InMemory) {
2845 int Offset = DefRange.DataOffset;
2846 unsigned Reg = DefRange.CVRegister;
2847
2848 // 32-bit x86 call sequences often use PUSH instructions, which disrupt
2849 // ESP-relative offsets. Use the virtual frame pointer, VFRAME or $T0,
2850 // instead. In frames without stack realignment, $T0 will be the CFA.
2851 if (RegisterId(Reg) == RegisterId::ESP) {
2852 Reg = unsigned(RegisterId::VFRAME);
2853 Offset += FI.OffsetAdjustment;
2854 }
2855
2856 // If we can use the chosen frame pointer for the frame and this isn't a
2857 // sliced aggregate, use the smaller S_DEFRANGE_FRAMEPOINTER_REL record.
2858 // Otherwise, use S_DEFRANGE_REGISTER_REL.
2859 EncodedFramePtrReg EncFP = encodeFramePtrReg(RegisterId(Reg), TheCPU);
2860 if (!DefRange.IsSubfield && EncFP != EncodedFramePtrReg::None &&
2861 (bool(Flags & LocalSymFlags::IsParameter)
2862 ? (EncFP == FI.EncodedParamFramePtrReg)
2863 : (EncFP == FI.EncodedLocalFramePtrReg))) {
2865 DRHdr.Offset = Offset;
2866 OS.emitCVDefRangeDirective(Ranges, DRHdr);
2867 } else {
2868 uint16_t RegRelFlags = 0;
2869 if (DefRange.IsSubfield) {
2871 (DefRange.StructOffset
2873 }
2875 DRHdr.Register = Reg;
2876 DRHdr.Flags = RegRelFlags;
2877 DRHdr.BasePointerOffset = Offset;
2878 OS.emitCVDefRangeDirective(Ranges, DRHdr);
2879 }
2880 } else {
2881 assert(DefRange.DataOffset == 0 && "unexpected offset into register");
2882 if (DefRange.IsSubfield) {
2884 DRHdr.Register = DefRange.CVRegister;
2885 DRHdr.MayHaveNoName = 0;
2886 DRHdr.OffsetInParent = DefRange.StructOffset;
2887 OS.emitCVDefRangeDirective(Ranges, DRHdr);
2888 } else {
2890 DRHdr.Register = DefRange.CVRegister;
2891 DRHdr.MayHaveNoName = 0;
2892 OS.emitCVDefRangeDirective(Ranges, DRHdr);
2893 }
2894 }
2895 }
2896}
2897
2898void CodeViewDebug::emitLexicalBlockList(ArrayRef<LexicalBlock *> Blocks,
2899 const FunctionInfo& FI) {
2900 for (LexicalBlock *Block : Blocks)
2901 emitLexicalBlock(*Block, FI);
2902}
2903
2904/// Emit an S_BLOCK32 and S_END record pair delimiting the contents of a
2905/// lexical block scope.
2906void CodeViewDebug::emitLexicalBlock(const LexicalBlock &Block,
2907 const FunctionInfo& FI) {
2908 MCSymbol *RecordEnd = beginSymbolRecord(SymbolKind::S_BLOCK32);
2909 OS.AddComment("PtrParent");
2910 OS.emitInt32(0); // PtrParent
2911 OS.AddComment("PtrEnd");
2912 OS.emitInt32(0); // PtrEnd
2913 OS.AddComment("Code size");
2914 OS.emitAbsoluteSymbolDiff(Block.End, Block.Begin, 4); // Code Size
2915 OS.AddComment("Function section relative address");
2916 OS.emitCOFFSecRel32(Block.Begin, /*Offset=*/0); // Func Offset
2917 OS.AddComment("Function section index");
2918 OS.emitCOFFSectionIndex(FI.Begin); // Func Symbol
2919 OS.AddComment("Lexical block name");
2920 emitNullTerminatedSymbolName(OS, Block.Name); // Name
2921 endSymbolRecord(RecordEnd);
2922
2923 // Emit variables local to this lexical block.
2924 emitLocalVariableList(FI, Block.Locals);
2925 emitGlobalVariableList(Block.Globals);
2926
2927 // Emit lexical blocks contained within this block.
2928 emitLexicalBlockList(Block.Children, FI);
2929
2930 // Close the lexical block scope.
2931 emitEndSymbolRecord(SymbolKind::S_END);
2932}
2933
2934/// Convenience routine for collecting lexical block information for a list
2935/// of lexical scopes.
2936void CodeViewDebug::collectLexicalBlockInfo(
2941 for (LexicalScope *Scope : Scopes)
2942 collectLexicalBlockInfo(*Scope, Blocks, Locals, Globals);
2943}
2944
2945/// Populate the lexical blocks and local variable lists of the parent with
2946/// information about the specified lexical scope.
2947void CodeViewDebug::collectLexicalBlockInfo(
2948 LexicalScope &Scope,
2949 SmallVectorImpl<LexicalBlock *> &ParentBlocks,
2950 SmallVectorImpl<LocalVariable> &ParentLocals,
2951 SmallVectorImpl<CVGlobalVariable> &ParentGlobals) {
2952 if (Scope.isAbstractScope())
2953 return;
2954
2955 // Gather information about the lexical scope including local variables,
2956 // global variables, and address ranges.
2957 bool IgnoreScope = false;
2958 auto LI = ScopeVariables.find(&Scope);
2960 LI != ScopeVariables.end() ? &LI->second : nullptr;
2961 auto GI = ScopeGlobals.find(Scope.getScopeNode());
2963 GI != ScopeGlobals.end() ? GI->second.get() : nullptr;
2964 const DILexicalBlock *DILB = dyn_cast<DILexicalBlock>(Scope.getScopeNode());
2965 const SmallVectorImpl<InsnRange> &Ranges = Scope.getRanges();
2966
2967 // Ignore lexical scopes which do not contain variables.
2968 if (!Locals && !Globals)
2969 IgnoreScope = true;
2970
2971 // Ignore lexical scopes which are not lexical blocks.
2972 if (!DILB)
2973 IgnoreScope = true;
2974
2975 // Ignore scopes which have too many address ranges to represent in the
2976 // current CodeView format or do not have a valid address range.
2977 //
2978 // For lexical scopes with multiple address ranges you may be tempted to
2979 // construct a single range covering every instruction where the block is
2980 // live and everything in between. Unfortunately, Visual Studio only
2981 // displays variables from the first matching lexical block scope. If the
2982 // first lexical block contains exception handling code or cold code which
2983 // is moved to the bottom of the routine creating a single range covering
2984 // nearly the entire routine, then it will hide all other lexical blocks
2985 // and the variables they contain.
2986 if (Ranges.size() != 1 || !getLabelAfterInsn(Ranges.front().second))
2987 IgnoreScope = true;
2988
2989 if (IgnoreScope) {
2990 // This scope can be safely ignored and eliminating it will reduce the
2991 // size of the debug information. Be sure to collect any variable and scope
2992 // information from the this scope or any of its children and collapse them
2993 // into the parent scope.
2994 if (Locals)
2995 ParentLocals.append(Locals->begin(), Locals->end());
2996 if (Globals)
2997 ParentGlobals.append(Globals->begin(), Globals->end());
2998 collectLexicalBlockInfo(Scope.getChildren(),
2999 ParentBlocks,
3000 ParentLocals,
3001 ParentGlobals);
3002 return;
3003 }
3004
3005 // Create a new CodeView lexical block for this lexical scope. If we've
3006 // seen this DILexicalBlock before then the scope tree is malformed and
3007 // we can handle this gracefully by not processing it a second time.
3008 auto BlockInsertion = CurFn->LexicalBlocks.insert({DILB, LexicalBlock()});
3009 if (!BlockInsertion.second)
3010 return;
3011
3012 // Create a lexical block containing the variables and collect the the
3013 // lexical block information for the children.
3014 const InsnRange &Range = Ranges.front();
3015 assert(Range.first && Range.second);
3016 LexicalBlock &Block = BlockInsertion.first->second;
3017 Block.Begin = getLabelBeforeInsn(Range.first);
3018 Block.End = getLabelAfterInsn(Range.second);
3019 assert(Block.Begin && "missing label for scope begin");
3020 assert(Block.End && "missing label for scope end");
3021 Block.Name = DILB->getName();
3022 if (Locals)
3023 Block.Locals = std::move(*Locals);
3024 if (Globals)
3025 Block.Globals = std::move(*Globals);
3026 ParentBlocks.push_back(&Block);
3027 collectLexicalBlockInfo(Scope.getChildren(),
3028 Block.Children,
3029 Block.Locals,
3030 Block.Globals);
3031}
3032
3034 const Function &GV = MF->getFunction();
3035 assert(FnDebugInfo.count(&GV));
3036 assert(CurFn == FnDebugInfo[&GV].get());
3037
3038 collectVariableInfo(GV.getSubprogram());
3039
3040 // Build the lexical block structure to emit for this routine.
3042 collectLexicalBlockInfo(*CFS,
3043 CurFn->ChildBlocks,
3044 CurFn->Locals,
3045 CurFn->Globals);
3046
3047 // Clear the scope and variable information from the map which will not be
3048 // valid after we have finished processing this routine. This also prepares
3049 // the map for the subsequent routine.
3050 ScopeVariables.clear();
3051
3052 // Don't emit anything if we don't have any line tables.
3053 // Thunks are compiler-generated and probably won't have source correlation.
3054 if (!CurFn->HaveLineInfo && !GV.getSubprogram()->isThunk()) {
3055 FnDebugInfo.erase(&GV);
3056 CurFn = nullptr;
3057 return;
3058 }
3059
3060 // Find heap alloc sites and add to list.
3061 for (const auto &MBB : *MF) {
3062 for (const auto &MI : MBB) {
3063 if (MDNode *MD = MI.getHeapAllocMarker()) {
3064 CurFn->HeapAllocSites.push_back(std::make_tuple(getLabelBeforeInsn(&MI),
3066 dyn_cast<DIType>(MD)));
3067 }
3068 }
3069 }
3070
3071 CurFn->Annotations = MF->getCodeViewAnnotations();
3072
3073 CurFn->End = Asm->getFunctionEnd();
3074
3075 CurFn = nullptr;
3076}
3077
3078// Usable locations are valid with non-zero line numbers. A line number of zero
3079// corresponds to optimized code that doesn't have a distinct source location.
3080// In this case, we try to use the previous or next source location depending on
3081// the context.
3083 return DL && DL.getLine() != 0;
3084}
3085
3088
3089 // Ignore DBG_VALUE and DBG_LABEL locations and function prologue.
3090 if (!Asm || !CurFn || MI->isDebugInstr() ||
3091 MI->getFlag(MachineInstr::FrameSetup))
3092 return;
3093
3094 // If the first instruction of a new MBB has no location, find the first
3095 // instruction with a location and use that.
3096 DebugLoc DL = MI->getDebugLoc();
3097 if (!isUsableDebugLoc(DL) && MI->getParent() != PrevInstBB) {
3098 for (const auto &NextMI : *MI->getParent()) {
3099 if (NextMI.isDebugInstr())
3100 continue;
3101 DL = NextMI.getDebugLoc();
3102 if (isUsableDebugLoc(DL))
3103 break;
3104 }
3105 // FIXME: Handle the case where the BB has no valid locations. This would
3106 // probably require doing a real dataflow analysis.
3107 }
3108 PrevInstBB = MI->getParent();
3109
3110 // If we still don't have a debug location, don't record a location.
3111 if (!isUsableDebugLoc(DL))
3112 return;
3113
3114 maybeRecordLocation(DL, Asm->MF);
3115}
3116
3117MCSymbol *CodeViewDebug::beginCVSubsection(DebugSubsectionKind Kind) {
3118 MCSymbol *BeginLabel = MMI->getContext().createTempSymbol(),
3119 *EndLabel = MMI->getContext().createTempSymbol();
3120 OS.emitInt32(unsigned(Kind));
3121 OS.AddComment("Subsection size");
3122 OS.emitAbsoluteSymbolDiff(EndLabel, BeginLabel, 4);
3123 OS.emitLabel(BeginLabel);
3124 return EndLabel;
3125}
3126
3127void CodeViewDebug::endCVSubsection(MCSymbol *EndLabel) {
3128 OS.emitLabel(EndLabel);
3129 // Every subsection must be aligned to a 4-byte boundary.
3131}
3132
3134 for (const EnumEntry<SymbolKind> &EE : getSymbolTypeNames())
3135 if (EE.Value == SymKind)
3136 return EE.Name;
3137 return "";
3138}
3139
3140MCSymbol *CodeViewDebug::beginSymbolRecord(SymbolKind SymKind) {
3141 MCSymbol *BeginLabel = MMI->getContext().createTempSymbol(),
3142 *EndLabel = MMI->getContext().createTempSymbol();
3143 OS.AddComment("Record length");
3144 OS.emitAbsoluteSymbolDiff(EndLabel, BeginLabel, 2);
3145 OS.emitLabel(BeginLabel);
3146 if (OS.isVerboseAsm())
3147 OS.AddComment("Record kind: " + getSymbolName(SymKind));
3148 OS.emitInt16(unsigned(SymKind));
3149 return EndLabel;
3150}
3151
3152void CodeViewDebug::endSymbolRecord(MCSymbol *SymEnd) {
3153 // MSVC does not pad out symbol records to four bytes, but LLVM does to avoid
3154 // an extra copy of every symbol record in LLD. This increases object file
3155 // size by less than 1% in the clang build, and is compatible with the Visual
3156 // C++ linker.
3158 OS.emitLabel(SymEnd);
3159}
3160
3161void CodeViewDebug::emitEndSymbolRecord(SymbolKind EndKind) {
3162 OS.AddComment("Record length");
3163 OS.emitInt16(2);
3164 if (OS.isVerboseAsm())
3165 OS.AddComment("Record kind: " + getSymbolName(EndKind));
3166 OS.emitInt16(uint16_t(EndKind)); // Record Kind
3167}
3168
3169void CodeViewDebug::emitDebugInfoForUDTs(
3170 const std::vector<std::pair<std::string, const DIType *>> &UDTs) {
3171#ifndef NDEBUG
3172 size_t OriginalSize = UDTs.size();
3173#endif
3174 for (const auto &UDT : UDTs) {
3175 const DIType *T = UDT.second;
3177 MCSymbol *UDTRecordEnd = beginSymbolRecord(SymbolKind::S_UDT);
3178 OS.AddComment("Type");
3179 OS.emitInt32(getCompleteTypeIndex(T).getIndex());
3180 assert(OriginalSize == UDTs.size() &&
3181 "getCompleteTypeIndex found new UDTs!");
3183 endSymbolRecord(UDTRecordEnd);
3184 }
3185}
3186
3187void CodeViewDebug::collectGlobalVariableInfo() {
3189 GlobalMap;
3190 for (const GlobalVariable &GV : MMI->getModule()->globals()) {
3192 GV.getDebugInfo(GVEs);
3193 for (const auto *GVE : GVEs)
3194 GlobalMap[GVE] = &GV;
3195 }
3196
3197 NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
3198 for (const MDNode *Node : CUs->operands()) {
3199 const auto *CU = cast<DICompileUnit>(Node);
3200 for (const auto *GVE : CU->getGlobalVariables()) {
3201 const DIGlobalVariable *DIGV = GVE->getVariable();
3202 const DIExpression *DIE = GVE->getExpression();
3203 // Don't emit string literals in CodeView, as the only useful parts are
3204 // generally the filename and line number, which isn't possible to output
3205 // in CodeView. String literals should be the only unnamed GlobalVariable
3206 // with debug info.
3207 if (DIGV->getName().empty()) continue;
3208
3209 if ((DIE->getNumElements() == 2) &&
3210 (DIE->getElement(0) == dwarf::DW_OP_plus_uconst))
3211 // Record the constant offset for the variable.
3212 //
3213 // A Fortran common block uses this idiom to encode the offset
3214 // of a variable from the common block's starting address.
3215 CVGlobalVariableOffsets.insert(
3216 std::make_pair(DIGV, DIE->getElement(1)));
3217
3218 // Emit constant global variables in a global symbol section.
3219 if (GlobalMap.count(GVE) == 0 && DIE->isConstant()) {
3220 CVGlobalVariable CVGV = {DIGV, DIE};
3221 GlobalVariables.emplace_back(std::move(CVGV));
3222 }
3223
3224 const auto *GV = GlobalMap.lookup(GVE);
3225 if (!GV || GV->isDeclarationForLinker())
3226 continue;
3227
3228 DIScope *Scope = DIGV->getScope();
3230 if (Scope && isa<DILocalScope>(Scope)) {
3231 // Locate a global variable list for this scope, creating one if
3232 // necessary.
3233 auto Insertion = ScopeGlobals.insert(
3234 {Scope, std::unique_ptr<GlobalVariableList>()});
3235 if (Insertion.second)
3236 Insertion.first->second = std::make_unique<GlobalVariableList>();
3237 VariableList = Insertion.first->second.get();
3238 } else if (GV->hasComdat())
3239 // Emit this global variable into a COMDAT section.
3240 VariableList = &ComdatVariables;
3241 else
3242 // Emit this global variable in a single global symbol section.
3243 VariableList = &GlobalVariables;
3244 CVGlobalVariable CVGV = {DIGV, GV};
3245 VariableList->emplace_back(std::move(CVGV));
3246 }
3247 }
3248}
3249
3250void CodeViewDebug::collectDebugInfoForGlobals() {
3251 for (const CVGlobalVariable &CVGV : GlobalVariables) {
3252 const DIGlobalVariable *DIGV = CVGV.DIGV;
3253 const DIScope *Scope = DIGV->getScope();
3254 getCompleteTypeIndex(DIGV->getType());
3255 getFullyQualifiedName(Scope, DIGV->getName());
3256 }
3257
3258 for (const CVGlobalVariable &CVGV : ComdatVariables) {
3259 const DIGlobalVariable *DIGV = CVGV.DIGV;
3260 const DIScope *Scope = DIGV->getScope();
3261 getCompleteTypeIndex(DIGV->getType());
3262 getFullyQualifiedName(Scope, DIGV->getName());
3263 }
3264}
3265
3266void CodeViewDebug::emitDebugInfoForGlobals() {
3267 // First, emit all globals that are not in a comdat in a single symbol
3268 // substream. MSVC doesn't like it if the substream is empty, so only open
3269 // it if we have at least one global to emit.
3270 switchToDebugSectionForSymbol(nullptr);
3271 if (!GlobalVariables.empty() || !StaticConstMembers.empty()) {
3272 OS.AddComment("Symbol subsection for globals");
3273 MCSymbol *EndLabel = beginCVSubsection(DebugSubsectionKind::Symbols);
3274 emitGlobalVariableList(GlobalVariables);
3275 emitStaticConstMemberList();
3276 endCVSubsection(EndLabel);
3277 }
3278
3279 // Second, emit each global that is in a comdat into its own .debug$S
3280 // section along with its own symbol substream.
3281 for (const CVGlobalVariable &CVGV : ComdatVariables) {
3282 const GlobalVariable *GV = CVGV.GVInfo.get<const GlobalVariable *>();
3283 MCSymbol *GVSym = Asm->getSymbol(GV);
3284 OS.AddComment("Symbol subsection for " +
3286 switchToDebugSectionForSymbol(GVSym);
3287 MCSymbol *EndLabel = beginCVSubsection(DebugSubsectionKind::Symbols);
3288 // FIXME: emitDebugInfoForGlobal() doesn't handle DIExpressions.
3289 emitDebugInfoForGlobal(CVGV);
3290 endCVSubsection(EndLabel);
3291 }
3292}
3293
3294void CodeViewDebug::emitDebugInfoForRetainedTypes() {
3295 NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
3296 for (const MDNode *Node : CUs->operands()) {
3297 for (auto *Ty : cast<DICompileUnit>(Node)->getRetainedTypes()) {
3298 if (DIType *RT = dyn_cast<DIType>(Ty)) {
3299 getTypeIndex(RT);
3300 // FIXME: Add to global/local DTU list.
3301 }
3302 }
3303 }
3304}
3305
3306// Emit each global variable in the specified array.
3307void CodeViewDebug::emitGlobalVariableList(ArrayRef<CVGlobalVariable> Globals) {
3308 for (const CVGlobalVariable &CVGV : Globals) {
3309 // FIXME: emitDebugInfoForGlobal() doesn't handle DIExpressions.
3310 emitDebugInfoForGlobal(CVGV);
3311 }
3312}
3313
3314void CodeViewDebug::emitConstantSymbolRecord(const DIType *DTy, APSInt &Value,
3315 const std::string &QualifiedName) {
3316 MCSymbol *SConstantEnd = beginSymbolRecord(SymbolKind::S_CONSTANT);
3317 OS.AddComment("Type");
3318 OS.emitInt32(getTypeIndex(DTy).getIndex());
3319
3320 OS.AddComment("Value");
3321
3322 // Encoded integers shouldn't need more than 10 bytes.
3323 uint8_t Data[10];
3324 BinaryStreamWriter Writer(Data, llvm::support::endianness::little);
3325 CodeViewRecordIO IO(Writer);
3326 cantFail(IO.mapEncodedInteger(Value));
3327 StringRef SRef((char *)Data, Writer.getOffset());
3328 OS.emitBinaryData(SRef);
3329
3330 OS.AddComment("Name");
3332 endSymbolRecord(SConstantEnd);
3333}
3334
3335void CodeViewDebug::emitStaticConstMemberList() {
3336 for (const DIDerivedType *DTy : StaticConstMembers) {
3337 const DIScope *Scope = DTy->getScope();
3338
3339 APSInt Value;
3340 if (const ConstantInt *CI =
3341 dyn_cast_or_null<ConstantInt>(DTy->getConstant()))
3342 Value = APSInt(CI->getValue(),
3343 DebugHandlerBase::isUnsignedDIType(DTy->getBaseType()));
3344 else if (const ConstantFP *CFP =
3345 dyn_cast_or_null<ConstantFP>(DTy->getConstant()))
3346 Value = APSInt(CFP->getValueAPF().bitcastToAPInt(), true);
3347 else
3348 llvm_unreachable("cannot emit a constant without a value");
3349
3350 emitConstantSymbolRecord(DTy->getBaseType(), Value,
3351 getFullyQualifiedName(Scope, DTy->getName()));
3352 }
3353}
3354
3355static bool isFloatDIType(const DIType *Ty) {
3356 if (isa<DICompositeType>(Ty))
3357 return false;
3358
3359 if (auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
3360 dwarf::Tag T = (dwarf::Tag)Ty->getTag();
3361 if (T == dwarf::DW_TAG_pointer_type ||
3362 T == dwarf::DW_TAG_ptr_to_member_type ||
3363 T == dwarf::DW_TAG_reference_type ||
3364 T == dwarf::DW_TAG_rvalue_reference_type)
3365 return false;
3366 assert(DTy->getBaseType() && "Expected valid base type");
3367 return isFloatDIType(DTy->getBaseType());
3368 }
3369
3370 auto *BTy = cast<DIBasicType>(Ty);
3371 return (BTy->getEncoding() == dwarf::DW_ATE_float);
3372}
3373
3374void CodeViewDebug::emitDebugInfoForGlobal(const CVGlobalVariable &CVGV) {
3375 const DIGlobalVariable *DIGV = CVGV.DIGV;
3376
3377 const DIScope *Scope = DIGV->getScope();
3378 // For static data members, get the scope from the declaration.
3379 if (const auto *MemberDecl = dyn_cast_or_null<DIDerivedType>(
3381 Scope = MemberDecl->getScope();
3382 // For static local variables and Fortran, the scoping portion is elided
3383 // in its name so that we can reference the variable in the command line
3384 // of the VS debugger.
3385 std::string QualifiedName =
3386 (moduleIsInFortran() || (Scope && isa<DILocalScope>(Scope)))
3387 ? std::string(DIGV->getName())
3388 : getFullyQualifiedName(Scope, DIGV->getName());
3389
3390 if (const GlobalVariable *GV =
3391 CVGV.GVInfo.dyn_cast<const GlobalVariable *>()) {
3392 // DataSym record, see SymbolRecord.h for more info. Thread local data
3393 // happens to have the same format as global data.
3394 MCSymbol *GVSym = Asm->getSymbol(GV);
3396 ? (DIGV->isLocalToUnit() ? SymbolKind::S_LTHREAD32
3397 : SymbolKind::S_GTHREAD32)
3398 : (DIGV->isLocalToUnit() ? SymbolKind::S_LDATA32
3399 : SymbolKind::S_GDATA32);
3400 MCSymbol *DataEnd = beginSymbolRecord(DataSym);
3401 OS.AddComment("Type");
3402 OS.emitInt32(getCompleteTypeIndex(DIGV->getType()).getIndex());
3403 OS.AddComment("DataOffset");
3404
3405 uint64_t Offset = 0;
3406 if (CVGlobalVariableOffsets.find(DIGV) != CVGlobalVariableOffsets.end())
3407 // Use the offset seen while collecting info on globals.
3408 Offset = CVGlobalVariableOffsets[DIGV];
3409 OS.emitCOFFSecRel32(GVSym, Offset);
3410
3411 OS.AddComment("Segment");
3412 OS.emitCOFFSectionIndex(GVSym);
3413 OS.AddComment("Name");
3414 const unsigned LengthOfDataRecord = 12;
3415 emitNullTerminatedSymbolName(OS, QualifiedName, LengthOfDataRecord);
3416 endSymbolRecord(DataEnd);
3417 } else {
3418 const DIExpression *DIE = CVGV.GVInfo.get<const DIExpression *>();
3419 assert(DIE->isConstant() &&
3420 "Global constant variables must contain a constant expression.");
3421
3422 // Use unsigned for floats.
3423 bool isUnsigned = isFloatDIType(DIGV->getType())
3424 ? true
3426 APSInt Value(APInt(/*BitWidth=*/64, DIE->getElement(1)), isUnsigned);
3427 emitConstantSymbolRecord(DIGV->getType(), Value, QualifiedName);
3428 }
3429}
#define Success
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This header is deprecated in favour of llvm/TargetParser/Triple.h.
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
basic Basic Alias true
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
static bool canUseReferenceType(const DbgVariableLocation &Loc)
static SourceLanguage MapDWLangToCVLang(unsigned DWLang)
static MethodKind translateMethodKindFlags(const DISubprogram *SP, bool Introduced)
static bool isUsableDebugLoc(DebugLoc DL)
static TypeIndex getStringIdTypeIdx(GlobalTypeTableBuilder &TypeTable, StringRef S)
static CPUType mapArchToCVCPUType(Triple::ArchType Type)
static void emitNullTerminatedSymbolName(MCStreamer &OS, StringRef S, unsigned MaxFixedRecordLength=0xF00)
static Version parseVersion(StringRef Name)
static MethodOptions translateMethodOptionFlags(const DISubprogram *SP)
static bool isNonTrivial(const DICompositeType *DCTy)
static std::string formatNestedName(ArrayRef< StringRef > QualifiedNameComponents, StringRef TypeName)
static ClassOptions getCommonClassOptions(const DICompositeType *Ty)
Return ClassOptions that should be present on both the forward declaration and the defintion of a tag...
static PointerToMemberRepresentation translatePtrToMemberRep(unsigned SizeInBytes, bool IsPMF, unsigned Flags)
static FunctionOptions getFunctionOptions(const DISubroutineType *Ty, const DICompositeType *ClassTy=nullptr, StringRef SPName=StringRef(""))
static StringRef removeTemplateArgs(StringRef Name)
static TypeRecordKind getRecordKind(const DICompositeType *Ty)
static CallingConvention dwarfCCToCodeView(unsigned DwarfCC)
Given a DWARF calling convention, get the CodeView equivalent.
static bool isFloatDIType(const DIType *Ty)
static void addLocIfNotPresent(SmallVectorImpl< const DILocation * > &Locs, const DILocation *Loc)
static bool shouldEmitUdt(const DIType *T)
static StringRef getPrettyScopeName(const DIScope *Scope)
static std::string flattenCommandLine(ArrayRef< std::string > Args, StringRef MainFilename)
static bool shouldAlwaysEmitCompleteClassType(const DICompositeType *Ty)
static bool needsReferenceType(const DbgVariableLocation &Loc)
static MemberAccess translateAccessFlags(unsigned RecordTag, unsigned Flags)
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file contains constants used for implementing Dwarf debug support.
std::string Name
uint64_t Size
IRTranslator LLVM IR MI
static LVOptions Options
Definition: LVOptions.cpp:25
static M68kRelType getType(unsigned Kind, MCSymbolRefExpr::VariantKind &Modifier, bool &IsPCRel)
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
unsigned const TargetRegisterInfo * TRI
This file contains the declarations for metadata subclasses.
Module.h This file contains the declarations for the Module class.
#define P(N)
Profile::FuncID FuncId
Definition: Profile.cpp:321
static StringRef getName(Value *V)
Basic Register Allocator
@ VI
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallString class.
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition: Value.cpp:467
static const uint32_t IV[8]
Definition: blake3_impl.h:85
Class for arbitrary precision integers.
Definition: APInt.h:75
An arbitrary precision integer that knows its signedness.
Definition: APSInt.h:23
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition: ArrayRef.h:202
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:163
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:84
const TargetLoweringObjectFile & getObjFileLowering() const
Return information about object file lowering.
Definition: AsmPrinter.cpp:371
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:643
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:87
MCSymbol * getFunctionBegin() const
Definition: AsmPrinter.h:274
const MCAsmInfo * MAI
Target Asm Printer information.
Definition: AsmPrinter.h:90
MachineFunction * MF
The current machine function.
Definition: AsmPrinter.h:102
MCSymbol * getFunctionEnd() const
Definition: AsmPrinter.h:275
Provides write only access to a subclass of WritableBinaryStream.
Collects and handles line tables information in a CodeView format.
Definition: CodeViewDebug.h:52
CodeViewDebug(AsmPrinter *AP)
void beginModule(Module *M) override
bool moduleIsInFortran()
Check if the current module is in Fortran.
void endFunctionImpl(const MachineFunction *) override
Gather post-function debug information.
void endModule() override
Emit the COFF section that holds the line table information.
void beginInstruction(const MachineInstr *MI) override
Process beginning of an instruction.
void beginFunctionImpl(const MachineFunction *MF) override
Gather pre-function debug information.
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:256
This is the shared class of boolean and integer constants.
Definition: Constants.h:78
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition: Constants.h:193
int64_t getSExtValue() const
Return the constant as a 64-bit integer value after it has been sign extended as appropriate for the ...
Definition: Constants.h:147
Basic type, like 'int' or 'float'.
unsigned getEncoding() const
StringRef getIdentifier() const
DINodeArray getElements() const
DIType * getBaseType() const
Constant * getConstant() const
A structured debug information entry.
Definition: DIE.h:739
static DIE * get(BumpPtrAllocator &Alloc, dwarf::Tag Tag)
Definition: DIE.h:769
DWARF expression.
Metadata * getRawStaticDataMemberDeclaration() const
DILocalScope * getScope() const
Get the local scope for this variable.
Debug location.
Tagged DWARF-like metadata node.
dwarf::Tag getTag() const
Base class for scope-like contexts.
StringRef getFilename() const
StringRef getName() const
DIFile * getFile() const
StringRef getDirectory() const
DIScope * getScope() const
String type, Fortran CHARACTER(n)
Subprogram description.
Array subrange.
Type array for a subprogram.
DITypeRefArray getTypeArray() const
unsigned size() const
Base class for types.
uint64_t getOffsetInBits() const
bool isObjectPointer() const
DIFlags getFlags() const
StringRef getName() const
bool isForwardDecl() const
uint64_t getSizeInBits() const
unsigned getLine() const
DIScope * getScope() const
DIScope * getScope() const
DIType * getType() const
StringRef getName() const
unsigned getPointerSizeInBits(unsigned AS=0) const
Layout pointer size, in bits FIXME: The defaults need to be removed once all of the backends/clients ...
Definition: DataLayout.h:413
static const EntryIndex NoEntry
Special value to indicate that an entry is valid until the end of the function.
Base class for debug information backends.
static bool isUnsignedDIType(const DIType *Ty)
Return true if type encoding is unsigned.
AsmPrinter * Asm
Target of debug info emission.
MCSymbol * getLabelBeforeInsn(const MachineInstr *MI)
Return Label preceding the instruction.
MachineModuleInfo * MMI
Collected machine module information.
DebugLoc PrevInstLoc
Previous instruction's location information.
DebugLoc PrologEndLoc
This location indicates end of function prologue and beginning of function body.
MCSymbol * getLabelAfterInsn(const MachineInstr *MI)
Return Label immediately following the instruction.
void beginInstruction(const MachineInstr *MI) override
Process beginning of an instruction.
const MachineBasicBlock * PrevInstBB
void requestLabelAfterInsn(const MachineInstr *MI)
Ensure that a label will be emitted after MI.
DbgValueHistoryMap DbgValues
History of DBG_VALUE and clobber instructions for each user variable.
void requestLabelBeforeInsn(const MachineInstr *MI)
Ensure that a label will be emitted before MI.
static uint64_t getBaseTypeSize(const DIType *Ty)
If this type is derived from a base type then return base type size.
A debug info location.
Definition: DebugLoc.h:33
DebugLoc getFnDebugLoc() const
Find the debug info location for the start of the function.
Definition: DebugLoc.cpp:48
DILocation * get() const
Get the underlying DILocation.
Definition: DebugLoc.cpp:20
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: DenseMap.h:197
unsigned size() const
Definition: DenseMap.h:99
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:145
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:207
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
bool hasOptSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
Definition: Function.h:644
bool hasStackProtectorFnAttr() const
Returns true if the function has ssp, sspstrong, or sspreq fn attrs.
Definition: Function.cpp:731
DISubprogram * getSubprogram() const
Get the attached subprogram.
Definition: Metadata.cpp:1625
bool hasPersonalityFn() const
Check whether this function has a personality function.
Definition: Function.h:803
Constant * getPersonalityFn() const
Get the personality function associated with this function.
Definition: Function.cpp:1995
bool hasProfileData(bool IncludeSynthetic=false) const
Return true if the function is annotated with profile data.
Definition: Function.h:289
bool hasOptNone() const
Do not optimize this function (-O0).
Definition: Function.h:638
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.cpp:640
bool hasComdat() const
Definition: GlobalObject.h:127
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
Definition: GlobalValue.h:259
bool hasLocalLinkage() const
Definition: GlobalValue.h:523
static StringRef dropLLVMManglingEscape(StringRef Name)
If the given string begins with the GlobalValue name mangling escape character '\1',...
Definition: GlobalValue.h:562
bool isDeclarationForLinker() const
Definition: GlobalValue.h:614
LexicalScope - This class is used to track scope information.
Definition: LexicalScopes.h:44
LexicalScope * findLexicalScope(const DILocation *DL)
findLexicalScope - Find lexical scope, either regular or inlined, for the given DebugLoc.
LexicalScope * findInlinedScope(const DILocalScope *N, const DILocation *IA)
findInlinedScope - Find an inlined scope for the given scope/inlined-at.
LexicalScope * getCurrentFunctionScope() const
getCurrentFunctionScope - Return lexical scope for the current function.
unsigned getCodePointerSize() const
Get the code pointer size in bytes.
Definition: MCAsmInfo.h:550
void * allocate(unsigned Size, unsigned Align=8)
Definition: MCContext.h:842
MCSymbol * createTempSymbol()
Create a temporary symbol with a unique name.
Definition: MCContext.cpp:318
MCSectionCOFF * getAssociativeCOFFSection(MCSectionCOFF *Sec, const MCSymbol *KeySym, unsigned UniqueID=GenericSectionID)
Gets or creates a section equivalent to Sec that is associated with the section containing KeySym.
Definition: MCContext.cpp:700
MCSection * getCOFFDebugSymbolsSection() const
MCSection * getCOFFDebugTypesSection() const
MCSection * getCOFFGlobalTypeHashesSection() const
This represents a section on Windows.
Definition: MCSectionCOFF.h:26
MCSymbol * getCOMDATSymbol() const
Definition: MCSectionCOFF.h:67
Streaming machine code generation interface.
Definition: MCStreamer.h:212
virtual void addBlankLine()
Emit a blank line to a .s file to pretty it up.
Definition: MCStreamer.h:380
virtual bool emitCVFuncIdDirective(unsigned FunctionId)
Introduces a function id for use with .cv_loc.
Definition: MCStreamer.cpp:301
virtual void emitBinaryData(StringRef Data)
Functionally identical to EmitBytes.
virtual bool emitCVFileDirective(unsigned FileNo, StringRef Filename, ArrayRef< uint8_t > Checksum, unsigned ChecksumKind)
Associate a filename with a specified logical file number, and also specify that file's checksum info...
Definition: MCStreamer.cpp:294
virtual void emitCVStringTableDirective()
This implements the CodeView '.cv_stringtable' assembler directive.
Definition: MCStreamer.h:1002
virtual bool isVerboseAsm() const
Return true if this streamer supports verbose assembly and if it is enabled.
Definition: MCStreamer.h:336
virtual void emitCVFileChecksumOffsetDirective(unsigned FileNo)
This implements the CodeView '.cv_filechecksumoffset' assembler directive.
Definition: MCStreamer.h:1009
MCContext & getContext() const
Definition: MCStreamer.h:297
virtual void AddComment(const Twine &T, bool EOL=true)
Add a textual comment.
Definition: MCStreamer.h:359
virtual void emitCVFPOData(const MCSymbol *ProcSym, SMLoc Loc={})
This implements the CodeView '.cv_fpo_data' assembler directive.
Definition: MCStreamer.h:1012
virtual void emitCOFFSecRel32(MCSymbol const *Symbol, uint64_t Offset)
Emits a COFF section relative relocation.
Definition: MCStreamer.cpp:980
virtual void emitCVFileChecksumsDirective()
This implements the CodeView '.cv_filechecksums' assembler directive.
Definition: MCStreamer.h:1005
virtual void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo, unsigned Size)
Emit the absolute difference between two symbols.
virtual void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:423
virtual void emitCOFFSectionIndex(MCSymbol const *Symbol)
Emits a COFF section index.
Definition: MCStreamer.cpp:978
virtual void emitCVLinetableDirective(unsigned FunctionId, const MCSymbol *FnStart, const MCSymbol *FnEnd)
This implements the CodeView '.cv_linetable' assembler directive.
Definition: MCStreamer.cpp:346
virtual void emitValueToAlignment(Align Alignment, int64_t Value=0, unsigned ValueSize=1, unsigned MaxBytesToEmit=0)
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
virtual void emitIntValue(uint64_t Value, unsigned Size)
Special case of EmitValue that avoids the client having to pass in a MCExpr for constant integers.
Definition: MCStreamer.cpp:134
void emitInt16(uint64_t Value)
Definition: MCStreamer.h:747
virtual void switchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
virtual void emitCVDefRangeDirective(ArrayRef< std::pair< const MCSymbol *, const MCSymbol * > > Ranges, StringRef FixedSizePortion)
This implements the CodeView '.cv_def_range' assembler directive.
Definition: MCStreamer.cpp:368
void emitInt32(uint64_t Value)
Definition: MCStreamer.h:748
virtual void emitCVLocDirective(unsigned FunctionId, unsigned FileNo, unsigned Line, unsigned Column, bool PrologueEnd, bool IsStmt, StringRef FileName, SMLoc Loc)
This implements the CodeView '.cv_loc' assembler directive.
Definition: MCStreamer.cpp:319
virtual bool emitCVInlineSiteIdDirective(unsigned FunctionId, unsigned IAFunc, unsigned IAFile, unsigned IALine, unsigned IACol, SMLoc Loc)
Introduces an inline call site id for use with .cv_loc.
Definition: MCStreamer.cpp:305
void emitInt8(uint64_t Value)
Definition: MCStreamer.h:746
virtual void emitCVInlineLinetableDirective(unsigned PrimaryFunctionId, unsigned SourceFileId, unsigned SourceLineNum, const MCSymbol *FnStartSym, const MCSymbol *FnEndSym)
This implements the CodeView '.cv_inline_linetable' assembler directive.
Definition: MCStreamer.cpp:350
virtual void emitBytes(StringRef Data)
Emit the bytes in Data into the output.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
MCSection & getSection() const
Get the section associated with a defined, non-absolute symbol.
Definition: MCSymbol.h:267
ArrayRef< std::string > CommandLineArgs
Metadata node.
Definition: Metadata.h:943
ArrayRef< MDOperand > operands() const
Definition: Metadata.h:1289
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1297
Tuple of metadata.
Definition: Metadata.h:1328
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects.
int getOffsetAdjustment() const
Return the correction for frame offsets.
unsigned getCVBytesOfCalleeSavedRegisters() const
Returns how many bytes of callee-saved registers the target pushed in the prologue.
bool hasStackProtectorIndex() const
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
bool hasInlineAsm() const
Returns true if the function contains any inline assembly.
bool exposesReturnsTwice() const
exposesReturnsTwice - Returns true if the function calls setjmp or any other similar functions with a...
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Function & getFunction()
Return the LLVM function that this machine code represents.
VariableDbgInfoMapTy & getVariableDbgInfo()
ArrayRef< std::pair< MCSymbol *, MDNode * > > getCodeViewAnnotations() const
Representation of each machine instruction.
Definition: MachineInstr.h:68
bool isDebugValue() const
MachineOperand & getDebugOperand(unsigned Index)
Definition: MachineInstr.h:535
const MCContext & getContext() const
const Module * getModule() const
bool hasDebugInfo() const
Returns true if valid debug info is present.
Root of the metadata hierarchy.
Definition: Metadata.h:61
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
NamedMDNode * getNamedMetadata(const Twine &Name) const
Return the first NamedMDNode in the module with the specified name.
Definition: Module.cpp:251
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:258
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:398
iterator_range< global_iterator > globals()
Definition: Module.h:614
Metadata * getProfileSummary(bool IsCS) const
Returns profile summary metadata.
Definition: Module.cpp:643
A tuple of MDNodes.
Definition: Metadata.h:1587
iterator_range< op_iterator > operands()
Definition: Metadata.h:1683
RecordRecTy * getType()
Definition: Record.cpp:2530
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
Represents a location in source code.
Definition: SMLoc.h:23
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
bool empty() const
Definition: SmallVector.h:94
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:577
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:941
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:687
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StackOffset holds a fixed and a scalable offset in bytes.
Definition: TypeSize.h:36
static StackOffset getScalable(int64_t Scalable)
Definition: TypeSize.h:46
static StackOffset getFixed(int64_t Fixed)
Definition: TypeSize.h:45
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
char back() const
back - Get the last character in the string.
Definition: StringRef.h:146
bool startswith(StringRef Prefix) const
Definition: StringRef.h:261
StringRef take_front(size_t N=1) const
Return a StringRef equal to 'this' but with only the first N elements remaining.
Definition: StringRef.h:568
Information about stack frame layout on the target.
virtual bool hasFP(const MachineFunction &MF) const =0
hasFP - Return true if the specified function should have a dedicated frame pointer register.
virtual StackOffset getFrameIndexReference(const MachineFunction &MF, int FI, Register &FrameReg) const
getFrameIndexReference - This method should return the base register and offset used to reference a f...
CodeGenOpt::Level getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
TargetOptions Options
MCTargetOptions MCOptions
Machine level options.
std::string ObjectFilenameForDebug
Stores the filename/path of the final .o/.obj file, to be written in the debug information.
unsigned Hotpatch
Emit the hotpatch flag in CodeView debug.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
TargetSubtargetInfo - Generic base class for all target subtargets.
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
virtual const TargetFrameLowering * getFrameLowering() const
TinyPtrVector - This class is specialized for cases where there are normally 0 or 1 element in a vect...
Definition: TinyPtrVector.h:29
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition: Triple.h:355
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:74
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:308
@ CurrentDirectory
Absolute CWD path.
Definition: TypeRecord.h:679
@ SourceFile
Path to main source file, relative or absolute.
Definition: TypeRecord.h:681
@ BuildTool
Absolute compiler path.
Definition: TypeRecord.h:680
@ CommandLine
Full canonical command line (maybe -cc1)
Definition: TypeRecord.h:683
@ TypeServerPDB
Absolute path of type server PDB (/Fd)
Definition: TypeRecord.h:682
virtual void emitBytes(StringRef Data)=0
virtual void AddComment(const Twine &T)=0
virtual void emitIntValue(uint64_t Value, unsigned Size)=0
virtual void emitBinaryData(StringRef Data)=0
virtual void AddRawComment(const Twine &T)=0
void begin(ContinuationRecordKind RecordKind)
ArrayRef< ArrayRef< uint8_t > > records() const
TypeIndex insertRecord(ContinuationRecordBuilder &Builder)
ArrayRef< GloballyHashedType > hashes() const
For method overload sets. LF_METHOD.
Definition: TypeRecord.h:765
A 32-bit type reference.
Definition: TypeIndex.h:96
SimpleTypeKind getSimpleKind() const
Definition: TypeIndex.h:136
static TypeIndex None()
Definition: TypeIndex.h:148
SimpleTypeMode getSimpleMode() const
Definition: TypeIndex.h:141
static const uint32_t FirstNonSimpleIndex
Definition: TypeIndex.h:98
static StringRef simpleTypeName(TypeIndex TI)
Definition: TypeIndex.cpp:71
static TypeIndex Void()
Definition: TypeIndex.h:149
uint32_t getIndex() const
Definition: TypeIndex.h:111
bool isNoneType() const
Definition: TypeIndex.h:116
static TypeIndex NullptrT()
Definition: TypeIndex.h:157
static TypeIndex Int32()
Definition: TypeIndex.h:182
void addCallbackToPipeline(TypeVisitorCallbacks &Callbacks)
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition: DenseSet.h:97
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:642
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:672
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char TypeName[]
Key for Kernel::Arg::Metadata::mTypeName.
@ DEBUG_HASHES_SECTION_MAGIC
Definition: COFF.h:772
@ DEBUG_SECTION_MAGIC
Definition: COFF.h:771
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
const uint64_t Version
Definition: InstrProf.h:1058
Reg
All possible values of the reg field in the ModR/M byte.
PointerMode
Equivalent to CV_ptrmode_e.
Definition: CodeView.h:353
PointerOptions
Equivalent to misc lfPointerAttr bitfields.
Definition: CodeView.h:362
LocalSymFlags
Corresponds to CV_LVARFLAGS bitfield.
Definition: CodeView.h:406
MethodKind
Part of member attribute flags. (CV_methodprop_e)
Definition: CodeView.h:270
PointerKind
Equivalent to CV_ptrtype_e.
Definition: CodeView.h:336
CPUType
These values correspond to the CV_CPU_TYPE_e enumeration, and are documented here: https://msdn....
Definition: CodeView.h:75
PointerToMemberRepresentation
Equivalent to CV_pmtype_e.
Definition: CodeView.h:376
MethodOptions
Equivalent to CV_fldattr_t bitfield.
Definition: CodeView.h:281
ArrayRef< EnumEntry< SymbolKind > > getSymbolTypeNames()
Definition: EnumTables.cpp:439
MemberAccess
Source-level access specifier. (CV_access_e)
Definition: CodeView.h:262
ThunkOrdinal
These values correspond to the THUNK_ORDINAL enumeration.
Definition: CodeView.h:553
EncodedFramePtrReg
Two-bit value indicating which register is the designated frame pointer register.
Definition: CodeView.h:541
TypeRecordKind
Distinguishes individual records in .debug$T or .debug$P section or PDB type stream.
Definition: CodeView.h:26
SymbolKind
Duplicate copy of the above enum, but using the official CV names.
Definition: CodeView.h:47
ModifierOptions
Equivalent to CV_modifier_t.
Definition: CodeView.h:301
StringRef getSymbolName(CVSymbol Sym)
Definition: RecordName.cpp:323
EncodedFramePtrReg encodeFramePtrReg(RegisterId Reg, CPUType CPU)
Error visitTypeRecord(CVType &Record, TypeIndex Index, TypeVisitorCallbacks &Callbacks, VisitorDataSource Source=VDS_BytesPresent)
CallingConvention
Definition: Dwarf.h:413
SourceLanguage
Definition: Dwarf.h:199
bool remove_dots(SmallVectorImpl< char > &path, bool remove_dot_dot=false, Style style=Style::native)
In-place remove any '.
Definition: Path.cpp:715
bool is_absolute(const Twine &path, Style style=Style::native)
Is path absolute?
Definition: Path.cpp:671
void printArg(llvm::raw_ostream &OS, StringRef Arg, bool Quote)
Print a command argument, and optionally quote it.
Definition: Program.cpp:83
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:406
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
Definition: Error.cpp:63
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
std::tuple< uint64_t, uint32_t > InlineSite
Definition: MCPseudoProbe.h:96
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:484
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1683
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:145
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
StringRef getTypeName()
We provide a function which tries to compute the (demangled) name of a type statically.
Definition: TypeName.h:27
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:745
bool isAsynchronousEHPersonality(EHPersonality Pers)
Returns true if this personality function catches asynchronous exceptions.
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.
Definition: STLExtras.h:1869
std::pair< const MachineInstr *, const MachineInstr * > InsnRange
InsnRange - This is used to track range of instructions with identical lexical scope.
Definition: LexicalScopes.h:39
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:853
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
const DIDerivedType * MemberTypeNode
std::vector< MemberInfo > MemberList
MemberList Members
Direct members.
std::vector< const DIType * > NestedTypes
std::vector< const DIDerivedType * > Inheritance
Base classes.
int InMemory
Indicates that variable data is stored in memory relative to the specified register.
Definition: CodeViewDebug.h:57
Represents the location at which a variable is stored.
static std::optional< DbgVariableLocation > extractFromMachineInstruction(const MachineInstr &Instruction)
Extract a VariableLocation from a MachineInstr.
SmallVector< int64_t, 1 > LoadChain
Chain of offsetted loads necessary to load the value if it lives in memory.