LLVM 22.0.0git
DwarfUnit.cpp
Go to the documentation of this file.
1//===-- llvm/CodeGen/DwarfUnit.cpp - Dwarf Type and Compile Units ---------===//
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 constructing a dwarf compile unit.
10//
11//===----------------------------------------------------------------------===//
12
13#include "DwarfUnit.h"
14#include "AddressPool.h"
15#include "DwarfCompileUnit.h"
16#include "DwarfExpression.h"
17#include "llvm/ADT/APFloat.h"
18#include "llvm/ADT/APInt.h"
20#include "llvm/IR/Constants.h"
21#include "llvm/IR/DataLayout.h"
22#include "llvm/IR/GlobalValue.h"
23#include "llvm/IR/Metadata.h"
24#include "llvm/MC/MCAsmInfo.h"
25#include "llvm/MC/MCContext.h"
26#include "llvm/MC/MCDwarf.h"
27#include "llvm/MC/MCSection.h"
28#include "llvm/MC/MCStreamer.h"
31#include <cassert>
32#include <cstdint>
33#include <limits>
34#include <string>
35#include <utility>
36
37using namespace llvm;
38
39#define DEBUG_TYPE "dwarfdebug"
40
43 : DwarfExpression(AP.getDwarfVersion(), CU), AP(AP), OutDIE(DIE) {}
44
45void DIEDwarfExpression::emitOp(uint8_t Op, const char* Comment) {
46 CU.addUInt(getActiveDIE(), dwarf::DW_FORM_data1, Op);
47}
48
49void DIEDwarfExpression::emitSigned(int64_t Value) {
50 CU.addSInt(getActiveDIE(), dwarf::DW_FORM_sdata, Value);
51}
52
53void DIEDwarfExpression::emitUnsigned(uint64_t Value) {
54 CU.addUInt(getActiveDIE(), dwarf::DW_FORM_udata, Value);
55}
56
57void DIEDwarfExpression::emitData1(uint8_t Value) {
58 CU.addUInt(getActiveDIE(), dwarf::DW_FORM_data1, Value);
59}
60
61void DIEDwarfExpression::emitBaseTypeRef(uint64_t Idx) {
62 CU.addBaseTypeRef(getActiveDIE(), Idx);
63}
64
66 assert(!IsBuffering && "Already buffering?");
67 IsBuffering = true;
68}
69
70void DIEDwarfExpression::disableTemporaryBuffer() { IsBuffering = false; }
71
73 return TmpDIE.computeSize(AP.getDwarfFormParams());
74}
75
76void DIEDwarfExpression::commitTemporaryBuffer() { OutDIE.takeValues(TmpDIE); }
77
78bool DIEDwarfExpression::isFrameRegister(const TargetRegisterInfo &TRI,
79 llvm::Register MachineReg) {
80 return MachineReg == TRI.getFrameRegister(*AP.MF);
81}
82
84 AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU,
85 unsigned UniqueID)
86 : DIEUnit(UnitTag), UniqueID(UniqueID), CUNode(Node), Asm(A), DD(DW),
87 DU(DWU) {}
88
90 DwarfDebug *DW, DwarfFile *DWU, unsigned UniqueID,
91 MCDwarfDwoLineTable *SplitLineTable)
92 : DwarfUnit(dwarf::DW_TAG_type_unit, CU.getCUNode(), A, DW, DWU, UniqueID),
93 CU(CU), SplitLineTable(SplitLineTable) {}
94
96 for (DIEBlock *B : DIEBlocks)
97 B->~DIEBlock();
98 for (DIELoc *L : DIELocs)
99 L->~DIELoc();
100}
101
102int64_t DwarfUnit::getDefaultLowerBound() const {
103 switch (getSourceLanguage()) {
104 default:
105 break;
106
107 // The languages below have valid values in all DWARF versions.
108 case dwarf::DW_LANG_C:
109 case dwarf::DW_LANG_C89:
110 case dwarf::DW_LANG_C_plus_plus:
111 return 0;
112
113 case dwarf::DW_LANG_Fortran77:
114 case dwarf::DW_LANG_Fortran90:
115 return 1;
116
117 // The languages below have valid values only if the DWARF version >= 3.
118 case dwarf::DW_LANG_C99:
119 case dwarf::DW_LANG_ObjC:
120 case dwarf::DW_LANG_ObjC_plus_plus:
121 if (DD->getDwarfVersion() >= 3)
122 return 0;
123 break;
124
125 case dwarf::DW_LANG_Fortran95:
126 if (DD->getDwarfVersion() >= 3)
127 return 1;
128 break;
129
130 // Starting with DWARF v4, all defined languages have valid values.
131 case dwarf::DW_LANG_D:
132 case dwarf::DW_LANG_Java:
133 case dwarf::DW_LANG_Python:
134 case dwarf::DW_LANG_UPC:
135 if (DD->getDwarfVersion() >= 4)
136 return 0;
137 break;
138
139 case dwarf::DW_LANG_Ada83:
140 case dwarf::DW_LANG_Ada95:
141 case dwarf::DW_LANG_Cobol74:
142 case dwarf::DW_LANG_Cobol85:
143 case dwarf::DW_LANG_Modula2:
144 case dwarf::DW_LANG_Pascal83:
145 case dwarf::DW_LANG_PLI:
146 if (DD->getDwarfVersion() >= 4)
147 return 1;
148 break;
149
150 // The languages below are new in DWARF v5.
151 case dwarf::DW_LANG_BLISS:
152 case dwarf::DW_LANG_C11:
153 case dwarf::DW_LANG_C_plus_plus_03:
154 case dwarf::DW_LANG_C_plus_plus_11:
155 case dwarf::DW_LANG_C_plus_plus_14:
156 case dwarf::DW_LANG_Dylan:
157 case dwarf::DW_LANG_Go:
158 case dwarf::DW_LANG_Haskell:
159 case dwarf::DW_LANG_OCaml:
160 case dwarf::DW_LANG_OpenCL:
161 case dwarf::DW_LANG_RenderScript:
162 case dwarf::DW_LANG_Rust:
163 case dwarf::DW_LANG_Swift:
164 if (DD->getDwarfVersion() >= 5)
165 return 0;
166 break;
167
168 case dwarf::DW_LANG_Fortran03:
169 case dwarf::DW_LANG_Fortran08:
170 case dwarf::DW_LANG_Julia:
171 case dwarf::DW_LANG_Modula3:
172 if (DD->getDwarfVersion() >= 5)
173 return 1;
174 break;
175 }
176
177 return -1;
178}
179
180/// Check whether the DIE for this MDNode can be shared across CUs.
182 // When the MDNode can be part of the type system, the DIE can be shared
183 // across CUs.
184 // Combining type units and cross-CU DIE sharing is lower value (since
185 // cross-CU DIE sharing is used in LTO and removes type redundancy at that
186 // level already) but may be implementable for some value in projects
187 // building multiple independent libraries with LTO and then linking those
188 // together.
189 if (isDwoUnit() && !DD->shareAcrossDWOCUs())
190 return false;
191 return (isa<DIType>(D) ||
192 (isa<DISubprogram>(D) && !cast<DISubprogram>(D)->isDefinition())) &&
193 !DD->generateTypeUnits();
194}
195
198 return DU->getDIE(D);
199 return MDNodeToDieMap.lookup(D);
200}
201
204 DU->insertDIE(Desc, D);
205 return;
206 }
207 MDNodeToDieMap.insert(std::make_pair(Desc, D));
208}
209
211 MDNodeToDieMap.insert(std::make_pair(nullptr, D));
212}
213
215 if (DD->getDwarfVersion() >= 4)
216 addAttribute(Die, Attribute, dwarf::DW_FORM_flag_present, DIEInteger(1));
217 else
218 addAttribute(Die, Attribute, dwarf::DW_FORM_flag, DIEInteger(1));
219}
220
222 std::optional<dwarf::Form> Form, uint64_t Integer) {
223 if (!Form)
224 Form = DIEInteger::BestForm(false, Integer);
225 assert(Form != dwarf::DW_FORM_implicit_const &&
226 "DW_FORM_implicit_const is used only for signed integers");
228}
229
234
235void DwarfUnit::addIntAsBlock(DIE &Die, dwarf::Attribute Attribute, const APInt &Val) {
237
238 // Get the raw data form of the large APInt.
239 const uint64_t *Ptr64 = Val.getRawData();
240
241 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
242 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
243
244 // Output the constant to DWARF one byte at a time.
245 for (int i = 0; i < NumBytes; i++) {
246 uint8_t c;
247 if (LittleEndian)
248 c = Ptr64[i / 8] >> (8 * (i & 7));
249 else
250 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
251 addUInt(*Block, dwarf::DW_FORM_data1, c);
252 }
253
254 addBlock(Die, Attribute, Block);
255}
256
258 const APInt &Val, bool Unsigned) {
259 unsigned CIBitWidth = Val.getBitWidth();
260 if (CIBitWidth <= 64) {
261 if (Unsigned)
262 addUInt(Die, Attribute, std::nullopt, Val.getZExtValue());
263 else
264 addSInt(Die, Attribute, std::nullopt, Val.getSExtValue());
265 return;
266 }
267
268 addIntAsBlock(Die, Attribute, Val);
269}
270
272 std::optional<dwarf::Form> Form, int64_t Integer) {
273 if (!Form)
274 Form = DIEInteger::BestForm(true, Integer);
276}
277
278void DwarfUnit::addSInt(DIEValueList &Die, std::optional<dwarf::Form> Form,
279 int64_t Integer) {
280 addSInt(Die, (dwarf::Attribute)0, Form, Integer);
281}
282
285 if (CUNode->isDebugDirectivesOnly())
286 return;
287
288 if (DD->useInlineStrings()) {
289 addAttribute(Die, Attribute, dwarf::DW_FORM_string,
292 return;
293 }
294 dwarf::Form IxForm =
295 isDwoUnit() ? dwarf::DW_FORM_GNU_str_index : dwarf::DW_FORM_strp;
296
297 auto StringPoolEntry =
298 useSegmentedStringOffsetsTable() || IxForm == dwarf::DW_FORM_GNU_str_index
299 ? DU->getStringPool().getIndexedEntry(*Asm, String)
300 : DU->getStringPool().getEntry(*Asm, String);
301
302 // For DWARF v5 and beyond, use the smallest strx? form possible.
304 IxForm = dwarf::DW_FORM_strx1;
305 unsigned Index = StringPoolEntry.getIndex();
306 if (Index > 0xffffff)
307 IxForm = dwarf::DW_FORM_strx4;
308 else if (Index > 0xffff)
309 IxForm = dwarf::DW_FORM_strx3;
310 else if (Index > 0xff)
311 IxForm = dwarf::DW_FORM_strx2;
312 }
313 addAttribute(Die, Attribute, IxForm, DIEString(StringPoolEntry));
314}
315
317 dwarf::Form Form, const MCSymbol *Label) {
318 addAttribute(Die, Attribute, Form, DIELabel(Label));
319}
320
321void DwarfUnit::addLabel(DIELoc &Die, dwarf::Form Form, const MCSymbol *Label) {
322 addLabel(Die, (dwarf::Attribute)0, Form, Label);
323}
324
327 addUInt(Die, Attribute, DD->getDwarfSectionOffsetForm(), Integer);
328}
329
330unsigned DwarfTypeUnit::getOrCreateSourceID(const DIFile *File) {
331 if (!SplitLineTable)
332 return getCU().getOrCreateSourceID(File);
333 if (!UsedLineTable) {
334 UsedLineTable = true;
335 // This is a split type unit that needs a line table.
336 addSectionOffset(getUnitDie(), dwarf::DW_AT_stmt_list, 0);
337 }
338 return SplitLineTable->getFile(
339 File->getDirectory(), File->getFilename(), DD->getMD5AsBytes(File),
340 Asm->OutContext.getDwarfVersion(), File->getSource());
341}
342
344 bool UseAddrOffsetFormOrExpressions =
345 DD->useAddrOffsetForm() || DD->useAddrOffsetExpressions();
346
347 const MCSymbol *Base = nullptr;
348 if (Label->isInSection() && UseAddrOffsetFormOrExpressions)
349 Base = DD->getSectionLabel(&Label->getSection());
350
351 uint32_t Index = DD->getAddressPool().getIndex(Base ? Base : Label);
352
353 if (DD->getDwarfVersion() >= 5) {
354 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addrx);
355 addUInt(Die, dwarf::DW_FORM_addrx, Index);
356 } else {
357 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
358 addUInt(Die, dwarf::DW_FORM_GNU_addr_index, Index);
359 }
360
361 if (Base && Base != Label) {
362 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_const4u);
363 addLabelDelta(Die, (dwarf::Attribute)0, Label, Base);
364 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
365 }
366}
367
369 if (DD->getDwarfVersion() >= 5) {
370 addPoolOpAddress(Die, Sym);
371 return;
372 }
373
374 if (DD->useSplitDwarf()) {
375 addPoolOpAddress(Die, Sym);
376 return;
377 }
378
379 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
380 addLabel(Die, dwarf::DW_FORM_addr, Sym);
381}
382
384 const MCSymbol *Hi, const MCSymbol *Lo) {
385 addAttribute(Die, Attribute, dwarf::DW_FORM_data4,
387}
388
392
394 // Flag the type unit reference as a declaration so that if it contains
395 // members (implicit special members, static data member definitions, member
396 // declarations for definitions in this CU, etc) consumers don't get confused
397 // and think this is a full definition.
398 addFlag(Die, dwarf::DW_AT_declaration);
399
400 addAttribute(Die, dwarf::DW_AT_signature, dwarf::DW_FORM_ref_sig8,
401 DIEInteger(Signature));
402}
403
405 DIEEntry Entry) {
406 const DIEUnit *CU = Die.getUnit();
407 const DIEUnit *EntryCU = Entry.getEntry().getUnit();
408 if (!CU)
409 // We assume that Die belongs to this CU, if it is not linked to any CU yet.
410 CU = getUnitDie().getUnit();
411 if (!EntryCU)
412 EntryCU = getUnitDie().getUnit();
413 assert(EntryCU == CU || !DD->useSplitDwarf() || DD->shareAcrossDWOCUs() ||
414 !static_cast<const DwarfUnit*>(CU)->isDwoUnit());
416 EntryCU == CU ? dwarf::DW_FORM_ref4 : dwarf::DW_FORM_ref_addr,
417 Entry);
418}
419
421 DIE &Die = Parent.addChild(DIE::get(DIEValueAllocator, Tag));
422 if (N)
423 insertDIE(N, &Die);
424 return Die;
425}
426
428 Loc->computeSize(Asm->getDwarfFormParams());
429 DIELocs.push_back(Loc); // Memoize so we can call the destructor later on.
430 addAttribute(Die, Attribute, Loc->BestForm(DD->getDwarfVersion()), Loc);
431}
432
434 DIEBlock *Block) {
435 Block->computeSize(Asm->getDwarfFormParams());
436 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
437 addAttribute(Die, Attribute, Form, Block);
438}
439
444
445void DwarfUnit::addSourceLine(DIE &Die, unsigned Line, unsigned Column,
446 const DIFile *File) {
447 if (Line == 0)
448 return;
449
450 unsigned FileID = getOrCreateSourceID(File);
451 addUInt(Die, dwarf::DW_AT_decl_file, std::nullopt, FileID);
452 addUInt(Die, dwarf::DW_AT_decl_line, std::nullopt, Line);
453
454 if (Column != 0)
455 addUInt(Die, dwarf::DW_AT_decl_column, std::nullopt, Column);
456}
457
459 assert(V);
460
461 addSourceLine(Die, V->getLine(), /*Column*/ 0, V->getFile());
462}
463
465 assert(G);
466
467 addSourceLine(Die, G->getLine(), /*Column*/ 0, G->getFile());
468}
469
471 assert(SP);
472
473 addSourceLine(Die, SP->getLine(), /*Column*/ 0, SP->getFile());
474}
475
477 assert(L);
478
479 addSourceLine(Die, L->getLine(), L->getColumn(), L->getFile());
480}
481
482void DwarfUnit::addSourceLine(DIE &Die, const DIType *Ty) {
483 assert(Ty);
484
485 addSourceLine(Die, Ty->getLine(), /*Column*/ 0, Ty->getFile());
486}
487
489 assert(Ty);
490
491 addSourceLine(Die, Ty->getLine(), /*Column*/ 0, Ty->getFile());
492}
493
495 // Pass this down to addConstantValue as an unsigned bag of bits.
496 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
497}
498
500 const DIType *Ty) {
501 addConstantValue(Die, CI->getValue(), Ty);
502}
503
504void DwarfUnit::addConstantValue(DIE &Die, uint64_t Val, const DIType *Ty) {
505 addConstantValue(Die, DD->isUnsignedDIType(Ty), Val);
506}
507
509 // FIXME: This is a bit conservative/simple - it emits negative values always
510 // sign extended to 64 bits rather than minimizing the number of bytes.
511 addUInt(Die, dwarf::DW_AT_const_value,
512 Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata, Val);
513}
514
515void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, const DIType *Ty) {
516 addConstantValue(Die, Val, DD->isUnsignedDIType(Ty));
517}
518
519void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, bool Unsigned) {
520 unsigned CIBitWidth = Val.getBitWidth();
521 if (CIBitWidth <= 64) {
523 Unsigned ? Val.getZExtValue() : Val.getSExtValue());
524 return;
525 }
526
527 addIntAsBlock(Die, dwarf::DW_AT_const_value, Val);
528}
529
531 if (!LinkageName.empty())
532 addString(Die,
533 DD->getDwarfVersion() >= 4 ? dwarf::DW_AT_linkage_name
534 : dwarf::DW_AT_MIPS_linkage_name,
536}
537
538void DwarfUnit::addTemplateParams(DIE &Buffer, DINodeArray TParams) {
539 // Add template parameters.
540 for (const auto *Element : TParams) {
541 if (auto *TTP = dyn_cast<DITemplateTypeParameter>(Element))
542 constructTemplateTypeParameterDIE(Buffer, TTP);
543 else if (auto *TVP = dyn_cast<DITemplateValueParameter>(Element))
544 constructTemplateValueParameterDIE(Buffer, TVP);
545 }
546}
547
548/// Add thrown types.
549void DwarfUnit::addThrownTypes(DIE &Die, DINodeArray ThrownTypes) {
550 for (const auto *Ty : ThrownTypes) {
551 DIE &TT = createAndAddDIE(dwarf::DW_TAG_thrown_type, Die);
552 addType(TT, cast<DIType>(Ty));
553 }
554}
555
557 if ((Flags & DINode::FlagAccessibility) == DINode::FlagProtected)
558 addUInt(Die, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
560 else if ((Flags & DINode::FlagAccessibility) == DINode::FlagPrivate)
561 addUInt(Die, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
563 else if ((Flags & DINode::FlagAccessibility) == DINode::FlagPublic)
564 addUInt(Die, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
566}
567
569 if (!Context || isa<DIFile>(Context) || isa<DICompileUnit>(Context))
570 return &getUnitDie();
571 if (auto *T = dyn_cast<DIType>(Context))
572 return getOrCreateTypeDIE(T);
573 if (auto *NS = dyn_cast<DINamespace>(Context))
574 return getOrCreateNameSpace(NS);
575 if (auto *SP = dyn_cast<DISubprogram>(Context))
576 return getOrCreateSubprogramDIE(SP, nullptr);
577 if (auto *M = dyn_cast<DIModule>(Context))
578 return getOrCreateModule(M);
579 return getDIE(Context);
580}
581
583 auto *Context = Ty->getScope();
584 DIE *ContextDIE = getOrCreateContextDIE(Context);
585
586 if (DIE *TyDIE = getDIE(Ty))
587 return TyDIE;
588
589 // Create new type.
590 DIE &TyDIE = createAndAddDIE(Ty->getTag(), *ContextDIE, Ty);
591
593
594 updateAcceleratorTables(Context, Ty, TyDIE);
595 return &TyDIE;
596}
597
598DIE *DwarfUnit::createTypeDIE(const DIScope *Context, DIE &ContextDIE,
599 const DIType *Ty) {
600 // Create new type.
601 DIE &TyDIE = createAndAddDIE(Ty->getTag(), ContextDIE, Ty);
602
603 auto construct = [&](const auto *Ty) {
604 updateAcceleratorTables(Context, Ty, TyDIE);
605 constructTypeDIE(TyDIE, Ty);
606 };
607
608 if (auto *CTy = dyn_cast<DICompositeType>(Ty)) {
609 if (DD->generateTypeUnits() && !Ty->isForwardDecl() &&
610 (Ty->getRawName() || CTy->getRawIdentifier())) {
611 // Skip updating the accelerator tables since this is not the full type.
612 if (MDString *TypeId = CTy->getRawIdentifier()) {
613 addGlobalType(Ty, TyDIE, Context);
614 DD->addDwarfTypeUnitType(getCU(), TypeId->getString(), TyDIE, CTy);
615 } else {
616 updateAcceleratorTables(Context, Ty, TyDIE);
617 finishNonUnitTypeDIE(TyDIE, CTy);
618 }
619 return &TyDIE;
620 }
621 construct(CTy);
622 } else if (auto *FPT = dyn_cast<DIFixedPointType>(Ty))
623 construct(FPT);
624 else if (auto *BT = dyn_cast<DIBasicType>(Ty))
625 construct(BT);
626 else if (auto *ST = dyn_cast<DIStringType>(Ty))
627 construct(ST);
628 else if (auto *STy = dyn_cast<DISubroutineType>(Ty))
629 construct(STy);
630 else if (auto *SRTy = dyn_cast<DISubrangeType>(Ty))
631 constructSubrangeDIE(TyDIE, SRTy);
632 else
633 construct(cast<DIDerivedType>(Ty));
634
635 return &TyDIE;
636}
637
639 if (!TyNode)
640 return nullptr;
641
642 auto *Ty = cast<DIType>(TyNode);
643
644 // DW_TAG_restrict_type is not supported in DWARF2
645 if (Ty->getTag() == dwarf::DW_TAG_restrict_type && DD->getDwarfVersion() <= 2)
647
648 // DW_TAG_atomic_type is not supported in DWARF < 5
649 if (Ty->getTag() == dwarf::DW_TAG_atomic_type && DD->getDwarfVersion() < 5)
651
652 // Construct the context before querying for the existence of the DIE in case
653 // such construction creates the DIE.
654 auto *Context = Ty->getScope();
655 DIE *ContextDIE = getOrCreateContextDIE(Context);
656 assert(ContextDIE);
657
658 if (DIE *TyDIE = getDIE(Ty))
659 return TyDIE;
660
661 return static_cast<DwarfUnit *>(ContextDIE->getUnit())
662 ->createTypeDIE(Context, *ContextDIE, Ty);
663}
664
666 const DIType *Ty, const DIE &TyDIE) {
667 if (Ty->getName().empty())
668 return;
669 if (Ty->isForwardDecl())
670 return;
671
672 // add temporary record for this type to be added later
673
674 unsigned Flags = 0;
675 if (auto *CT = dyn_cast<DICompositeType>(Ty)) {
676 // A runtime language of 0 actually means C/C++ and that any
677 // non-negative value is some version of Objective-C/C++.
678 if (CT->getRuntimeLang() == 0 || CT->isObjcClassComplete())
680 }
681
682 DD->addAccelType(*this, CUNode->getNameTableKind(), Ty->getName(), TyDIE,
683 Flags);
684
685 if (auto *CT = dyn_cast<DICompositeType>(Ty))
686 if (Ty->getName() != CT->getIdentifier() &&
687 CT->getRuntimeLang() == dwarf::DW_LANG_Swift)
688 DD->addAccelType(*this, CUNode->getNameTableKind(), CT->getIdentifier(),
689 TyDIE, Flags);
690
691 addGlobalType(Ty, TyDIE, Context);
692}
693
694void DwarfUnit::addGlobalType(const DIType *Ty, const DIE &TyDIE,
695 const DIScope *Context) {
696 if (!Context || isa<DICompileUnit>(Context) || isa<DIFile>(Context) ||
697 isa<DINamespace>(Context) || isa<DICommonBlock>(Context))
698 addGlobalTypeImpl(Ty, TyDIE, Context);
699}
700
701void DwarfUnit::addType(DIE &Entity, const DIType *Ty,
703 assert(Ty && "Trying to add a type that doesn't exist?");
705}
706
707// FIXME: change callsites to use the new DW_LNAME_ language codes.
709 const auto &Lang = getLanguage();
710
711 if (!Lang.hasVersionedName())
712 return static_cast<llvm::dwarf::SourceLanguage>(Lang.getName());
713
715 static_cast<llvm::dwarf::SourceLanguageName>(Lang.getName()),
716 Lang.getVersion())
718}
719
720std::string DwarfUnit::getParentContextString(const DIScope *Context) const {
721 if (!Context)
722 return "";
723
724 // FIXME: Decide whether to implement this for non-C++ languages.
726 return "";
727
728 std::string CS;
730 while (!isa<DICompileUnit>(Context)) {
731 Parents.push_back(Context);
732 if (const DIScope *S = Context->getScope())
733 Context = S;
734 else
735 // Structure, etc types will have a NULL context if they're at the top
736 // level.
737 break;
738 }
739
740 // Reverse iterate over our list to go from the outermost construct to the
741 // innermost.
742 for (const DIScope *Ctx : llvm::reverse(Parents)) {
743 StringRef Name = Ctx->getName();
744 if (Name.empty() && isa<DINamespace>(Ctx))
745 Name = "(anonymous namespace)";
746 if (!Name.empty()) {
747 CS += Name;
748 CS += "::";
749 }
750 }
751 return CS;
752}
753
754void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIBasicType *BTy) {
755 // Get core information.
756 StringRef Name = BTy->getName();
757 // Add name if not anonymous or intermediate type.
758 if (!Name.empty())
759 addString(Buffer, dwarf::DW_AT_name, Name);
760
761 // An unspecified type only has a name attribute.
762 if (BTy->getTag() == dwarf::DW_TAG_unspecified_type)
763 return;
764
765 if (BTy->getTag() != dwarf::DW_TAG_string_type)
766 addUInt(Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
767 BTy->getEncoding());
768
769 uint64_t SizeInBytes = divideCeil(BTy->getSizeInBits(), 8);
770 addUInt(Buffer, dwarf::DW_AT_byte_size, std::nullopt, SizeInBytes);
771 if (BTy->getTag() == dwarf::Tag::DW_TAG_base_type) {
772 // DW_TAG_base_type:
773 // If the value of an object of the given type does not fully occupy the
774 // storage described by a byte size attribute, the base type entry may also
775 // have a DW_AT_bit_size [...] attribute.
776 // TODO: Do big endian targets need DW_AT_data_bit_offset? See discussion in
777 // pull request #164372.
778 if (uint64_t DataSizeInBits = BTy->getDataSizeInBits();
779 DataSizeInBits && DataSizeInBits != SizeInBytes * 8)
780 addUInt(Buffer, dwarf::DW_AT_bit_size, std::nullopt, DataSizeInBits);
781 }
782
783 if (BTy->isBigEndian())
784 addUInt(Buffer, dwarf::DW_AT_endianity, std::nullopt, dwarf::DW_END_big);
785 else if (BTy->isLittleEndian())
786 addUInt(Buffer, dwarf::DW_AT_endianity, std::nullopt, dwarf::DW_END_little);
787
788 if (uint32_t NumExtraInhabitants = BTy->getNumExtraInhabitants())
789 addUInt(Buffer, dwarf::DW_AT_LLVM_num_extra_inhabitants, std::nullopt,
790 NumExtraInhabitants);
791}
792
793void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIFixedPointType *BTy) {
794 // Base type handling.
795 constructTypeDIE(Buffer, static_cast<const DIBasicType *>(BTy));
796
797 if (BTy->isBinary())
798 addSInt(Buffer, dwarf::DW_AT_binary_scale, dwarf::DW_FORM_sdata,
799 BTy->getFactor());
800 else if (BTy->isDecimal())
801 addSInt(Buffer, dwarf::DW_AT_decimal_scale, dwarf::DW_FORM_sdata,
802 BTy->getFactor());
803 else {
804 assert(BTy->isRational());
805 DIE *ContextDIE = getOrCreateContextDIE(BTy->getScope());
806 DIE &Constant = createAndAddDIE(dwarf::DW_TAG_constant, *ContextDIE);
807
808 addInt(Constant, dwarf::DW_AT_GNU_numerator, BTy->getNumerator(),
809 !BTy->isSigned());
810 addInt(Constant, dwarf::DW_AT_GNU_denominator, BTy->getDenominator(),
811 !BTy->isSigned());
812
813 addDIEEntry(Buffer, dwarf::DW_AT_small, Constant);
814 }
815}
816
817void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIStringType *STy) {
818 // Get core information.
819 StringRef Name = STy->getName();
820 // Add name if not anonymous or intermediate type.
821 if (!Name.empty())
822 addString(Buffer, dwarf::DW_AT_name, Name);
823
824 if (DIVariable *Var = STy->getStringLength()) {
825 if (auto *VarDIE = getDIE(Var))
826 addDIEEntry(Buffer, dwarf::DW_AT_string_length, *VarDIE);
827 } else if (DIExpression *Expr = STy->getStringLengthExp()) {
828 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
829 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
830 // This is to describe the memory location of the
831 // length of a Fortran deferred length string, so
832 // lock it down as such.
833 DwarfExpr.setMemoryLocationKind();
834 DwarfExpr.addExpression(Expr);
835 addBlock(Buffer, dwarf::DW_AT_string_length, DwarfExpr.finalize());
836 } else {
837 uint64_t Size = STy->getSizeInBits() >> 3;
838 addUInt(Buffer, dwarf::DW_AT_byte_size, std::nullopt, Size);
839 }
840
841 if (DIExpression *Expr = STy->getStringLocationExp()) {
842 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
843 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
844 // This is to describe the memory location of the
845 // string, so lock it down as such.
846 DwarfExpr.setMemoryLocationKind();
847 DwarfExpr.addExpression(Expr);
848 addBlock(Buffer, dwarf::DW_AT_data_location, DwarfExpr.finalize());
849 }
850
851 if (STy->getEncoding()) {
852 // For eventual Unicode support.
853 addUInt(Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
854 STy->getEncoding());
855 }
856}
857
858void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy) {
859 // Get core information.
860 StringRef Name = DTy->getName();
861 uint64_t Size = DTy->getSizeInBits() >> 3;
862 uint16_t Tag = Buffer.getTag();
863
864 // Map to main type, void will not have a type.
865 const DIType *FromTy = DTy->getBaseType();
866 if (FromTy)
867 addType(Buffer, FromTy);
868
869 // Add name if not anonymous or intermediate type.
870 if (!Name.empty())
871 addString(Buffer, dwarf::DW_AT_name, Name);
872
873 addAnnotation(Buffer, DTy->getAnnotations());
874
875 // If alignment is specified for a typedef , create and insert DW_AT_alignment
876 // attribute in DW_TAG_typedef DIE.
877 if (Tag == dwarf::DW_TAG_typedef && DD->getDwarfVersion() >= 5) {
878 uint32_t AlignInBytes = DTy->getAlignInBytes();
879 if (AlignInBytes > 0)
880 addUInt(Buffer, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
881 AlignInBytes);
882 }
883
884 // Add size if non-zero (derived types might be zero-sized.)
885 if (Size && Tag != dwarf::DW_TAG_pointer_type
886 && Tag != dwarf::DW_TAG_ptr_to_member_type
887 && Tag != dwarf::DW_TAG_reference_type
888 && Tag != dwarf::DW_TAG_rvalue_reference_type)
889 addUInt(Buffer, dwarf::DW_AT_byte_size, std::nullopt, Size);
890
891 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
892 addDIEEntry(Buffer, dwarf::DW_AT_containing_type,
893 *getOrCreateTypeDIE(cast<DIDerivedType>(DTy)->getClassType()));
894
895 addAccess(Buffer, DTy->getFlags());
896
897 // Add source line info if available and TyDesc is not a forward declaration.
898 if (!DTy->isForwardDecl())
899 addSourceLine(Buffer, DTy);
900
901 // If DWARF address space value is other than None, add it. The IR
902 // verifier checks that DWARF address space only exists for pointer
903 // or reference types.
904 if (DTy->getDWARFAddressSpace())
905 addUInt(Buffer, dwarf::DW_AT_address_class, dwarf::DW_FORM_data4,
906 *DTy->getDWARFAddressSpace());
907
908 // Add template alias template parameters.
909 if (Tag == dwarf::DW_TAG_template_alias)
910 addTemplateParams(Buffer, DTy->getTemplateParams());
911
912 if (auto PtrAuthData = DTy->getPtrAuthData()) {
913 addUInt(Buffer, dwarf::DW_AT_LLVM_ptrauth_key, dwarf::DW_FORM_data1,
914 PtrAuthData->key());
915 if (PtrAuthData->isAddressDiscriminated())
916 addFlag(Buffer, dwarf::DW_AT_LLVM_ptrauth_address_discriminated);
917 addUInt(Buffer, dwarf::DW_AT_LLVM_ptrauth_extra_discriminator,
918 dwarf::DW_FORM_data2, PtrAuthData->extraDiscriminator());
919 if (PtrAuthData->isaPointer())
920 addFlag(Buffer, dwarf::DW_AT_LLVM_ptrauth_isa_pointer);
921 if (PtrAuthData->authenticatesNullValues())
922 addFlag(Buffer, dwarf::DW_AT_LLVM_ptrauth_authenticates_null_values);
923 }
924}
925
926std::optional<unsigned>
928 // Args[0] is the return type.
929 std::optional<unsigned> ObjectPointerIndex;
930 for (unsigned i = 1, N = Args.size(); i < N; ++i) {
931 const DIType *Ty = Args[i];
932 if (!Ty) {
933 assert(i == N-1 && "Unspecified parameter must be the last argument");
934 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
935 } else {
936 DIE &Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
937 addType(Arg, Ty);
938 if (Ty->isArtificial())
939 addFlag(Arg, dwarf::DW_AT_artificial);
940
941 if (Ty->isObjectPointer()) {
942 assert(!ObjectPointerIndex &&
943 "Can't have more than one object pointer");
944 ObjectPointerIndex = i;
945 }
946 }
947 }
948
949 return ObjectPointerIndex;
950}
951
952void DwarfUnit::constructTypeDIE(DIE &Buffer, const DISubroutineType *CTy) {
953 // Add return type. A void return won't have a type.
954 auto Elements = cast<DISubroutineType>(CTy)->getTypeArray();
955 if (Elements.size())
956 if (auto RTy = Elements[0])
957 addType(Buffer, RTy);
958
959 bool isPrototyped = true;
960 if (Elements.size() == 2 && !Elements[1])
961 isPrototyped = false;
962
963 constructSubprogramArguments(Buffer, Elements);
964
965 // Add prototype flag if we're dealing with a C language and the function has
966 // been prototyped.
967 if (isPrototyped && dwarf::isC(getSourceLanguage()))
968 addFlag(Buffer, dwarf::DW_AT_prototyped);
969
970 // Add a DW_AT_calling_convention if this has an explicit convention.
971 if (CTy->getCC() && CTy->getCC() != dwarf::DW_CC_normal)
972 addUInt(Buffer, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1,
973 CTy->getCC());
974
975 if (CTy->isLValueReference())
976 addFlag(Buffer, dwarf::DW_AT_reference);
977
978 if (CTy->isRValueReference())
979 addFlag(Buffer, dwarf::DW_AT_rvalue_reference);
980}
981
982void DwarfUnit::addAnnotation(DIE &Buffer, DINodeArray Annotations) {
983 if (!Annotations)
984 return;
985
986 for (const Metadata *Annotation : Annotations->operands()) {
987 const MDNode *MD = cast<MDNode>(Annotation);
988 const MDString *Name = cast<MDString>(MD->getOperand(0));
989 const auto &Value = MD->getOperand(1);
990
991 DIE &AnnotationDie = createAndAddDIE(dwarf::DW_TAG_LLVM_annotation, Buffer);
992 addString(AnnotationDie, dwarf::DW_AT_name, Name->getString());
993 if (const auto *Data = dyn_cast<MDString>(Value))
994 addString(AnnotationDie, dwarf::DW_AT_const_value, Data->getString());
995 else if (const auto *Data = dyn_cast<ConstantAsMetadata>(Value))
996 addConstantValue(AnnotationDie, Data->getValue()->getUniqueInteger(),
997 /*Unsigned=*/true);
998 else
999 assert(false && "Unsupported annotation value type");
1000 }
1001}
1002
1003void DwarfUnit::addDiscriminant(DIE &Variant, Constant *Discriminant,
1004 bool IsUnsigned) {
1005 if (const auto *CI = dyn_cast_or_null<ConstantInt>(Discriminant)) {
1006 addInt(Variant, dwarf::DW_AT_discr_value, CI->getValue(), IsUnsigned);
1007 } else if (const auto *CA =
1009 // Must have an even number of operands.
1010 unsigned NElems = CA->getNumElements();
1011 if (NElems % 2 != 0) {
1012 return;
1013 }
1014
1015 DIEBlock *Block = new (DIEValueAllocator) DIEBlock;
1016
1017 auto AddInt = [&](const APInt &Val) {
1018 if (IsUnsigned)
1019 addUInt(*Block, dwarf::DW_FORM_udata, Val.getZExtValue());
1020 else
1021 addSInt(*Block, dwarf::DW_FORM_sdata, Val.getSExtValue());
1022 };
1023
1024 for (unsigned I = 0; I < NElems; I += 2) {
1025 APInt LV = CA->getElementAsAPInt(I);
1026 APInt HV = CA->getElementAsAPInt(I + 1);
1027 if (LV == HV) {
1028 addUInt(*Block, dwarf::DW_FORM_data1, dwarf::DW_DSC_label);
1029 AddInt(LV);
1030 } else {
1031 addUInt(*Block, dwarf::DW_FORM_data1, dwarf::DW_DSC_range);
1032 AddInt(LV);
1033 AddInt(HV);
1034 }
1035 }
1036 addBlock(Variant, dwarf::DW_AT_discr_list, Block);
1037 }
1038}
1039
1041 // Add name if not anonymous or intermediate type.
1042 StringRef Name = CTy->getName();
1043
1044 uint16_t Tag = Buffer.getTag();
1045
1046 switch (Tag) {
1047 case dwarf::DW_TAG_array_type:
1048 constructArrayTypeDIE(Buffer, CTy);
1049 break;
1050 case dwarf::DW_TAG_enumeration_type:
1051 constructEnumTypeDIE(Buffer, CTy);
1052 break;
1053 case dwarf::DW_TAG_variant_part:
1054 case dwarf::DW_TAG_variant:
1055 case dwarf::DW_TAG_structure_type:
1056 case dwarf::DW_TAG_union_type:
1057 case dwarf::DW_TAG_class_type:
1058 case dwarf::DW_TAG_namelist: {
1059 // Emit the discriminator for a variant part.
1060 DIDerivedType *Discriminator = nullptr;
1061 if (Tag == dwarf::DW_TAG_variant_part) {
1062 Discriminator = CTy->getDiscriminator();
1063 if (Discriminator) {
1064 // DWARF says:
1065 // If the variant part has a discriminant, the discriminant is
1066 // represented by a separate debugging information entry which is
1067 // a child of the variant part entry.
1068 // However, for a language like Ada, this yields a weird
1069 // result: a discriminant field would have to be emitted
1070 // multiple times, once per variant part. Instead, this DWARF
1071 // restriction was lifted for DWARF 6 (see
1072 // https://dwarfstd.org/issues/180123.1.html) and so we allow
1073 // this here.
1074 DIE *DiscDIE = getDIE(Discriminator);
1075 if (DiscDIE == nullptr) {
1076 DiscDIE = &constructMemberDIE(Buffer, Discriminator);
1077 }
1078 addDIEEntry(Buffer, dwarf::DW_AT_discr, *DiscDIE);
1079 }
1080 }
1081
1082 // Add template parameters to a class, structure or union types.
1083 if (Tag == dwarf::DW_TAG_class_type ||
1084 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
1085 addTemplateParams(Buffer, CTy->getTemplateParams());
1086
1087 // Add elements to structure type.
1088 DINodeArray Elements = CTy->getElements();
1089 for (const auto *Element : Elements) {
1090 if (!Element)
1091 continue;
1092 if (auto *SP = dyn_cast<DISubprogram>(Element))
1093 getOrCreateSubprogramDIE(SP, nullptr);
1094 else if (auto *DDTy = dyn_cast<DIDerivedType>(Element)) {
1095 if (DDTy->getTag() == dwarf::DW_TAG_friend) {
1096 DIE &ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
1097 addType(ElemDie, DDTy->getBaseType(), dwarf::DW_AT_friend);
1098 } else if (DDTy->isStaticMember()) {
1100 } else if (Tag == dwarf::DW_TAG_variant_part) {
1101 // When emitting a variant part, wrap each member in
1102 // DW_TAG_variant.
1103 DIE &Variant = createAndAddDIE(dwarf::DW_TAG_variant, Buffer);
1104 if (Constant *CI = DDTy->getDiscriminantValue()) {
1105 addDiscriminant(Variant, CI,
1106 DD->isUnsignedDIType(Discriminator->getBaseType()));
1107 }
1108 // If the variant holds a composite type with tag
1109 // DW_TAG_variant, inline those members into the variant
1110 // DIE.
1111 if (auto *Composite =
1112 dyn_cast_or_null<DICompositeType>(DDTy->getBaseType());
1113 Composite != nullptr &&
1114 Composite->getTag() == dwarf::DW_TAG_variant) {
1115 constructTypeDIE(Variant, Composite);
1116 } else {
1117 constructMemberDIE(Variant, DDTy);
1118 }
1119 } else {
1120 constructMemberDIE(Buffer, DDTy);
1121 }
1122 } else if (auto *Property = dyn_cast<DIObjCProperty>(Element)) {
1123 DIE &ElemDie = createAndAddDIE(Property->getTag(), Buffer);
1124 StringRef PropertyName = Property->getName();
1125 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
1126 if (Property->getType())
1127 addType(ElemDie, Property->getType());
1128 addSourceLine(ElemDie, Property);
1129 StringRef GetterName = Property->getGetterName();
1130 if (!GetterName.empty())
1131 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
1132 StringRef SetterName = Property->getSetterName();
1133 if (!SetterName.empty())
1134 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
1135 if (unsigned PropertyAttributes = Property->getAttributes())
1136 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, std::nullopt,
1137 PropertyAttributes);
1138 } else if (auto *Composite = dyn_cast<DICompositeType>(Element)) {
1139 if (Composite->getTag() == dwarf::DW_TAG_variant_part) {
1140 DIE &VariantPart = createAndAddDIE(Composite->getTag(), Buffer);
1141 constructTypeDIE(VariantPart, Composite);
1142 }
1143 } else if (Tag == dwarf::DW_TAG_namelist) {
1144 auto *Var = dyn_cast<DINode>(Element);
1145 auto *VarDIE = getDIE(Var);
1146 if (VarDIE) {
1147 DIE &ItemDie = createAndAddDIE(dwarf::DW_TAG_namelist_item, Buffer);
1148 addDIEEntry(ItemDie, dwarf::DW_AT_namelist_item, *VarDIE);
1149 }
1150 }
1151 }
1152
1153 if (CTy->isAppleBlockExtension())
1154 addFlag(Buffer, dwarf::DW_AT_APPLE_block);
1155
1156 if (CTy->getExportSymbols())
1157 addFlag(Buffer, dwarf::DW_AT_export_symbols);
1158
1159 // This is outside the DWARF spec, but GDB expects a DW_AT_containing_type
1160 // inside C++ composite types to point to the base class with the vtable.
1161 // Rust uses DW_AT_containing_type to link a vtable to the type
1162 // for which it was created.
1163 if (auto *ContainingType = CTy->getVTableHolder())
1164 addDIEEntry(Buffer, dwarf::DW_AT_containing_type,
1165 *getOrCreateTypeDIE(ContainingType));
1166
1167 if (CTy->isObjcClassComplete())
1168 addFlag(Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
1169
1170 // Add the type's non-standard calling convention.
1171 // DW_CC_pass_by_value/DW_CC_pass_by_reference are introduced in DWARF 5.
1172 if (!Asm->TM.Options.DebugStrictDwarf || DD->getDwarfVersion() >= 5) {
1173 uint8_t CC = 0;
1174 if (CTy->isTypePassByValue())
1175 CC = dwarf::DW_CC_pass_by_value;
1176 else if (CTy->isTypePassByReference())
1177 CC = dwarf::DW_CC_pass_by_reference;
1178 if (CC)
1179 addUInt(Buffer, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1,
1180 CC);
1181 }
1182
1183 if (auto *SpecifiedFrom = CTy->getSpecification())
1184 addDIEEntry(Buffer, dwarf::DW_AT_specification,
1185 *getOrCreateContextDIE(SpecifiedFrom));
1186
1187 break;
1188 }
1189 default:
1190 break;
1191 }
1192
1193 // Add name if not anonymous or intermediate type.
1194 if (!Name.empty())
1195 addString(Buffer, dwarf::DW_AT_name, Name);
1196
1197 // For Swift, mangled names are put into DW_AT_linkage_name.
1198 if (CTy->getRuntimeLang() == dwarf::DW_LANG_Swift && CTy->getRawIdentifier())
1199 addString(Buffer, dwarf::DW_AT_linkage_name, CTy->getIdentifier());
1200
1201 addAnnotation(Buffer, CTy->getAnnotations());
1202
1203 if (Tag == dwarf::DW_TAG_enumeration_type ||
1204 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
1205 Tag == dwarf::DW_TAG_union_type) {
1206 if (auto *Var = dyn_cast_or_null<DIVariable>(CTy->getRawSizeInBits())) {
1207 if (auto *VarDIE = getDIE(Var))
1208 addDIEEntry(Buffer, dwarf::DW_AT_bit_size, *VarDIE);
1209 } else if (auto *Exp =
1212 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1213 DwarfExpr.setMemoryLocationKind();
1214 DwarfExpr.addExpression(Exp);
1215 addBlock(Buffer, dwarf::DW_AT_bit_size, DwarfExpr.finalize());
1216 } else {
1217 uint64_t Size = CTy->getSizeInBits() >> 3;
1218 // Add size if non-zero (derived types might be zero-sized.)
1219 // Ignore the size if it's a non-enum forward decl.
1220 // TODO: Do we care about size for enum forward declarations?
1221 if (Size &&
1222 (!CTy->isForwardDecl() || Tag == dwarf::DW_TAG_enumeration_type))
1223 addUInt(Buffer, dwarf::DW_AT_byte_size, std::nullopt, Size);
1224 else if (!CTy->isForwardDecl())
1225 // Add zero size if it is not a forward declaration.
1226 addUInt(Buffer, dwarf::DW_AT_byte_size, std::nullopt, 0);
1227 }
1228
1229 // If we're a forward decl, say so.
1230 if (CTy->isForwardDecl())
1231 addFlag(Buffer, dwarf::DW_AT_declaration);
1232
1233 // Add accessibility info if available.
1234 addAccess(Buffer, CTy->getFlags());
1235
1236 // Add source line info if available.
1237 if (!CTy->isForwardDecl())
1238 addSourceLine(Buffer, CTy);
1239
1240 // No harm in adding the runtime language to the declaration.
1241 unsigned RLang = CTy->getRuntimeLang();
1242 if (RLang)
1243 addUInt(Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
1244 RLang);
1245
1246 // Add align info if available.
1247 if (uint32_t AlignInBytes = CTy->getAlignInBytes())
1248 addUInt(Buffer, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
1249 AlignInBytes);
1250
1251 if (uint32_t NumExtraInhabitants = CTy->getNumExtraInhabitants())
1252 addUInt(Buffer, dwarf::DW_AT_LLVM_num_extra_inhabitants, std::nullopt,
1253 NumExtraInhabitants);
1254 }
1255}
1256
1257void DwarfUnit::constructTemplateTypeParameterDIE(
1258 DIE &Buffer, const DITemplateTypeParameter *TP) {
1259 DIE &ParamDIE =
1260 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
1261 // Add the type if it exists, it could be void and therefore no type.
1262 if (TP->getType())
1263 addType(ParamDIE, TP->getType());
1264 if (!TP->getName().empty())
1265 addString(ParamDIE, dwarf::DW_AT_name, TP->getName());
1266 if (TP->isDefault() && isCompatibleWithVersion(5))
1267 addFlag(ParamDIE, dwarf::DW_AT_default_value);
1268}
1269
1270void DwarfUnit::constructTemplateValueParameterDIE(
1271 DIE &Buffer, const DITemplateValueParameter *VP) {
1272 DIE &ParamDIE = createAndAddDIE(VP->getTag(), Buffer);
1273
1274 // Add the type if there is one, template template and template parameter
1275 // packs will not have a type.
1276 if (VP->getTag() == dwarf::DW_TAG_template_value_parameter)
1277 addType(ParamDIE, VP->getType());
1278 if (!VP->getName().empty())
1279 addString(ParamDIE, dwarf::DW_AT_name, VP->getName());
1280 if (VP->isDefault() && isCompatibleWithVersion(5))
1281 addFlag(ParamDIE, dwarf::DW_AT_default_value);
1282 if (Metadata *Val = VP->getValue()) {
1284 addConstantValue(ParamDIE, CI, VP->getType());
1285 else if (ConstantFP *CF = mdconst::dyn_extract<ConstantFP>(Val))
1286 addConstantFPValue(ParamDIE, CF);
1287 else if (GlobalValue *GV = mdconst::dyn_extract<GlobalValue>(Val)) {
1288 // We cannot describe the location of dllimport'd entities: the
1289 // computation of their address requires loads from the IAT.
1290 if (!GV->hasDLLImportStorageClass()) {
1291 // For declaration non-type template parameters (such as global values
1292 // and functions)
1294 addOpAddress(*Loc, Asm->getSymbol(GV));
1295 // Emit DW_OP_stack_value to use the address as the immediate value of
1296 // the parameter, rather than a pointer to it.
1297 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1298 addBlock(ParamDIE, dwarf::DW_AT_location, Loc);
1299 }
1300 } else if (VP->getTag() == dwarf::DW_TAG_GNU_template_template_param) {
1301 assert(isa<MDString>(Val));
1302 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1303 cast<MDString>(Val)->getString());
1304 } else if (VP->getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
1305 addTemplateParams(ParamDIE, cast<MDTuple>(Val));
1306 }
1307 }
1308}
1309
1311 // Construct the context before querying for the existence of the DIE in case
1312 // such construction creates the DIE.
1313 DIE *ContextDIE = getOrCreateContextDIE(NS->getScope());
1314
1315 if (DIE *NDie = getDIE(NS))
1316 return NDie;
1317 DIE &NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
1318
1319 StringRef Name = NS->getName();
1320 if (!Name.empty())
1321 addString(NDie, dwarf::DW_AT_name, NS->getName());
1322 else
1323 Name = "(anonymous namespace)";
1324 DD->addAccelNamespace(*this, CUNode->getNameTableKind(), Name, NDie);
1325 addGlobalName(Name, NDie, NS->getScope());
1326 if (NS->getExportSymbols())
1327 addFlag(NDie, dwarf::DW_AT_export_symbols);
1328 return &NDie;
1329}
1330
1332 // Construct the context before querying for the existence of the DIE in case
1333 // such construction creates the DIE.
1334 DIE *ContextDIE = getOrCreateContextDIE(M->getScope());
1335
1336 if (DIE *MDie = getDIE(M))
1337 return MDie;
1338 DIE &MDie = createAndAddDIE(dwarf::DW_TAG_module, *ContextDIE, M);
1339
1340 if (!M->getName().empty()) {
1341 addString(MDie, dwarf::DW_AT_name, M->getName());
1342 addGlobalName(M->getName(), MDie, M->getScope());
1343 }
1344 if (!M->getConfigurationMacros().empty())
1345 addString(MDie, dwarf::DW_AT_LLVM_config_macros,
1346 M->getConfigurationMacros());
1347 if (!M->getIncludePath().empty())
1348 addString(MDie, dwarf::DW_AT_LLVM_include_path, M->getIncludePath());
1349 if (!M->getAPINotesFile().empty())
1350 addString(MDie, dwarf::DW_AT_LLVM_apinotes, M->getAPINotesFile());
1351 if (M->getFile())
1352 addUInt(MDie, dwarf::DW_AT_decl_file, std::nullopt,
1353 getOrCreateSourceID(M->getFile()));
1354 if (M->getLineNo())
1355 addUInt(MDie, dwarf::DW_AT_decl_line, std::nullopt, M->getLineNo());
1356 if (M->getIsDecl())
1357 addFlag(MDie, dwarf::DW_AT_declaration);
1358
1359 return &MDie;
1360}
1361
1363 const Function *FnHint, bool Minimal) {
1364 // Construct the context before querying for the existence of the DIE in case
1365 // such construction creates the DIE (as is the case for member function
1366 // declarations).
1367 DIE *ContextDIE =
1369
1370 if (DIE *SPDie = getDIE(SP))
1371 return SPDie;
1372
1373 if (auto *SPDecl = SP->getDeclaration()) {
1374 if (!Minimal) {
1375 // Build the decl now to ensure it precedes the definition.
1376 getOrCreateSubprogramDIE(SPDecl, nullptr);
1377 // Check whether the DIE for SP has already been created after the call
1378 // above.
1379 // FIXME: Should the creation of definition subprogram DIE during
1380 // the creation of declaration subprogram DIE be allowed?
1381 // See https://github.com/llvm/llvm-project/pull/154636.
1382 if (DIE *SPDie = getDIE(SP))
1383 return SPDie;
1384 }
1385 }
1386
1387 // DW_TAG_inlined_subroutine may refer to this DIE.
1388 DIE &SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
1389
1390 // Stop here and fill this in later, depending on whether or not this
1391 // subprogram turns out to have inlined instances or not.
1392 if (SP->isDefinition())
1393 return &SPDie;
1394
1395 static_cast<DwarfUnit *>(SPDie.getUnit())
1396 ->applySubprogramAttributes(SP, SPDie);
1397 return &SPDie;
1398}
1399
1401 DIE &SPDie, bool Minimal) {
1402 DIE *DeclDie = nullptr;
1403 StringRef DeclLinkageName;
1404 if (auto *SPDecl = SP->getDeclaration()) {
1405 if (!Minimal) {
1406 DITypeRefArray DeclArgs, DefinitionArgs;
1407 DeclArgs = SPDecl->getType()->getTypeArray();
1408 DefinitionArgs = SP->getType()->getTypeArray();
1409
1410 if (DeclArgs.size() && DefinitionArgs.size())
1411 if (DefinitionArgs[0] != nullptr && DeclArgs[0] != DefinitionArgs[0])
1412 addType(SPDie, DefinitionArgs[0]);
1413
1414 DeclDie = getDIE(SPDecl);
1415 assert(DeclDie && "This DIE should've already been constructed when the "
1416 "definition DIE was created in "
1417 "getOrCreateSubprogramDIE");
1418 // Look at the Decl's linkage name only if we emitted it.
1419 if (DD->useAllLinkageNames())
1420 DeclLinkageName = SPDecl->getLinkageName();
1421 unsigned DeclID = getOrCreateSourceID(SPDecl->getFile());
1422 unsigned DefID = getOrCreateSourceID(SP->getFile());
1423 if (DeclID != DefID)
1424 addUInt(SPDie, dwarf::DW_AT_decl_file, std::nullopt, DefID);
1425
1426 if (SP->getLine() != SPDecl->getLine())
1427 addUInt(SPDie, dwarf::DW_AT_decl_line, std::nullopt, SP->getLine());
1428 }
1429 }
1430
1431 // Add function template parameters.
1432 addTemplateParams(SPDie, SP->getTemplateParams());
1433
1434 // Add the linkage name if we have one and it isn't in the Decl.
1435 StringRef LinkageName = SP->getLinkageName();
1436 // Always emit linkage name for abstract subprograms.
1437 if (DeclLinkageName != LinkageName &&
1438 (DD->useAllLinkageNames() || DU->getAbstractScopeDIEs().lookup(SP)))
1440
1441 if (!DeclDie)
1442 return false;
1443
1444 // Refer to the function declaration where all the other attributes will be
1445 // found.
1446 addDIEEntry(SPDie, dwarf::DW_AT_specification, *DeclDie);
1447 return true;
1448}
1449
1451 bool SkipSPAttributes) {
1452 // If -fdebug-info-for-profiling is enabled, need to emit the subprogram
1453 // and its source location.
1454 bool SkipSPSourceLocation = SkipSPAttributes &&
1455 !CUNode->getDebugInfoForProfiling();
1456 if (!SkipSPSourceLocation)
1457 if (applySubprogramDefinitionAttributes(SP, SPDie, SkipSPAttributes))
1458 return;
1459
1460 // Constructors and operators for anonymous aggregates do not have names.
1461 if (!SP->getName().empty())
1462 addString(SPDie, dwarf::DW_AT_name, SP->getName());
1463
1464 addAnnotation(SPDie, SP->getAnnotations());
1465
1466 if (!SkipSPSourceLocation)
1467 addSourceLine(SPDie, SP);
1468
1469 // Skip the rest of the attributes under -gmlt to save space.
1470 if (SkipSPAttributes)
1471 return;
1472
1473 // Add the prototype if we have a prototype and we have a C like
1474 // language.
1475 if (SP->isPrototyped() && dwarf::isC(getSourceLanguage()))
1476 addFlag(SPDie, dwarf::DW_AT_prototyped);
1477
1478 if (SP->isObjCDirect())
1479 addFlag(SPDie, dwarf::DW_AT_APPLE_objc_direct);
1480
1481 unsigned CC = 0;
1482 DITypeRefArray Args;
1483 if (const DISubroutineType *SPTy = SP->getType()) {
1484 Args = SPTy->getTypeArray();
1485 CC = SPTy->getCC();
1486 }
1487
1488 // Add a DW_AT_calling_convention if this has an explicit convention.
1489 if (CC && CC != dwarf::DW_CC_normal)
1490 addUInt(SPDie, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1, CC);
1491
1492 // Add a return type. If this is a type like a C/C++ void type we don't add a
1493 // return type.
1494 if (Args.size())
1495 if (auto Ty = Args[0])
1496 addType(SPDie, Ty);
1497
1498 unsigned VK = SP->getVirtuality();
1499 if (VK) {
1500 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
1501 if (SP->getVirtualIndex() != -1u) {
1502 DIELoc *Block = getDIELoc();
1503 addUInt(*Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1504 addUInt(*Block, dwarf::DW_FORM_udata, SP->getVirtualIndex());
1505 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
1506 }
1507 ContainingTypeMap.insert(std::make_pair(&SPDie, SP->getContainingType()));
1508 }
1509
1510 if (!SP->isDefinition()) {
1511 addFlag(SPDie, dwarf::DW_AT_declaration);
1512
1513 // Add arguments. Do not add arguments for subprogram definition. They will
1514 // be handled while processing variables.
1515 //
1516 // Encode the object pointer as an index instead of a DIE reference in order
1517 // to minimize the affect on the .debug_info size.
1518 if (std::optional<unsigned> ObjectPointerIndex =
1519 constructSubprogramArguments(SPDie, Args)) {
1520 if (getDwarfDebug().tuneForLLDB() &&
1521 getDwarfDebug().getDwarfVersion() >= 5) {
1522 // 0th index in Args is the return type, hence adjust by 1. In DWARF
1523 // we want the first parameter to be at index 0.
1524 assert(*ObjectPointerIndex > 0);
1525 addSInt(SPDie, dwarf::DW_AT_object_pointer,
1526 dwarf::DW_FORM_implicit_const, *ObjectPointerIndex - 1);
1527 }
1528 }
1529 }
1530
1531 addThrownTypes(SPDie, SP->getThrownTypes());
1532
1533 if (SP->isArtificial())
1534 addFlag(SPDie, dwarf::DW_AT_artificial);
1535
1536 if (!SP->isLocalToUnit())
1537 addFlag(SPDie, dwarf::DW_AT_external);
1538
1539 if (DD->useAppleExtensionAttributes()) {
1540 if (SP->isOptimized())
1541 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
1542
1543 if (unsigned isa = Asm->getISAEncoding())
1544 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1545 }
1546
1547 if (SP->isLValueReference())
1548 addFlag(SPDie, dwarf::DW_AT_reference);
1549
1550 if (SP->isRValueReference())
1551 addFlag(SPDie, dwarf::DW_AT_rvalue_reference);
1552
1553 if (SP->isNoReturn())
1554 addFlag(SPDie, dwarf::DW_AT_noreturn);
1555
1556 addAccess(SPDie, SP->getFlags());
1557
1558 if (SP->isExplicit())
1559 addFlag(SPDie, dwarf::DW_AT_explicit);
1560
1561 if (SP->isMainSubprogram())
1562 addFlag(SPDie, dwarf::DW_AT_main_subprogram);
1563 if (SP->isPure())
1564 addFlag(SPDie, dwarf::DW_AT_pure);
1565 if (SP->isElemental())
1566 addFlag(SPDie, dwarf::DW_AT_elemental);
1567 if (SP->isRecursive())
1568 addFlag(SPDie, dwarf::DW_AT_recursive);
1569
1570 if (!SP->getTargetFuncName().empty())
1571 addString(SPDie, dwarf::DW_AT_trampoline, SP->getTargetFuncName());
1572
1573 if (DD->getDwarfVersion() >= 5 && SP->isDeleted())
1574 addFlag(SPDie, dwarf::DW_AT_deleted);
1575}
1576
1577void DwarfUnit::constructSubrangeDIE(DIE &DW_Subrange, const DISubrangeType *SR,
1578 bool ForArray) {
1579 StringRef Name = SR->getName();
1580 if (!Name.empty())
1581 addString(DW_Subrange, dwarf::DW_AT_name, Name);
1582
1583 if (SR->getBaseType())
1584 addType(DW_Subrange, SR->getBaseType());
1585
1586 addSourceLine(DW_Subrange, SR);
1587
1588 if (uint64_t Size = SR->getSizeInBits())
1589 addUInt(DW_Subrange, dwarf::DW_AT_byte_size, std::nullopt, Size >> 3);
1590 if (uint32_t AlignInBytes = SR->getAlignInBytes())
1591 addUInt(DW_Subrange, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
1592 AlignInBytes);
1593
1594 if (SR->isBigEndian())
1595 addUInt(DW_Subrange, dwarf::DW_AT_endianity, std::nullopt,
1596 dwarf::DW_END_big);
1597 else if (SR->isLittleEndian())
1598 addUInt(DW_Subrange, dwarf::DW_AT_endianity, std::nullopt,
1599 dwarf::DW_END_little);
1600
1601 // The LowerBound value defines the lower bounds which is typically
1602 // zero for C/C++. Values are 64 bit.
1603 int64_t DefaultLowerBound = getDefaultLowerBound();
1604
1605 auto AddBoundTypeEntry = [&](dwarf::Attribute Attr,
1606 DISubrangeType::BoundType Bound) -> void {
1607 if (auto *BV = dyn_cast_if_present<DIVariable *>(Bound)) {
1608 if (auto *VarDIE = getDIE(BV))
1609 addDIEEntry(DW_Subrange, Attr, *VarDIE);
1610 } else if (auto *BE = dyn_cast_if_present<DIExpression *>(Bound)) {
1612 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1613 DwarfExpr.setMemoryLocationKind();
1614 DwarfExpr.addExpression(BE);
1615 addBlock(DW_Subrange, Attr, DwarfExpr.finalize());
1616 } else if (auto *BI = dyn_cast_if_present<ConstantInt *>(Bound)) {
1617 if (Attr == dwarf::DW_AT_GNU_bias) {
1618 if (BI->getSExtValue() != 0)
1619 addUInt(DW_Subrange, Attr, dwarf::DW_FORM_sdata, BI->getSExtValue());
1620 } else if (Attr != dwarf::DW_AT_lower_bound || DefaultLowerBound == -1 ||
1621 BI->getSExtValue() != DefaultLowerBound || !ForArray)
1622 addSInt(DW_Subrange, Attr, dwarf::DW_FORM_sdata, BI->getSExtValue());
1623 }
1624 };
1625
1626 AddBoundTypeEntry(dwarf::DW_AT_lower_bound, SR->getLowerBound());
1627
1628 AddBoundTypeEntry(dwarf::DW_AT_upper_bound, SR->getUpperBound());
1629
1630 AddBoundTypeEntry(dwarf::DW_AT_bit_stride, SR->getStride());
1631
1632 AddBoundTypeEntry(dwarf::DW_AT_GNU_bias, SR->getBias());
1633}
1634
1635void DwarfUnit::constructSubrangeDIE(DIE &Buffer, const DISubrange *SR) {
1636 DIE &DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
1637
1638 DIE *IdxTy = getIndexTyDie();
1639 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, *IdxTy);
1640
1641 // The LowerBound value defines the lower bounds which is typically zero for
1642 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1643 // Count == -1 then the array is unbounded and we do not emit
1644 // DW_AT_lower_bound and DW_AT_count attributes.
1645 int64_t DefaultLowerBound = getDefaultLowerBound();
1646
1647 auto AddBoundTypeEntry = [&](dwarf::Attribute Attr,
1648 DISubrange::BoundType Bound) -> void {
1649 if (auto *BV = dyn_cast_if_present<DIVariable *>(Bound)) {
1650 if (auto *VarDIE = getDIE(BV))
1651 addDIEEntry(DW_Subrange, Attr, *VarDIE);
1652 } else if (auto *BE = dyn_cast_if_present<DIExpression *>(Bound)) {
1653 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1654 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1655 DwarfExpr.setMemoryLocationKind();
1656 DwarfExpr.addExpression(BE);
1657 addBlock(DW_Subrange, Attr, DwarfExpr.finalize());
1658 } else if (auto *BI = dyn_cast_if_present<ConstantInt *>(Bound)) {
1659 if (Attr == dwarf::DW_AT_count) {
1660 if (BI->getSExtValue() != -1)
1661 addUInt(DW_Subrange, Attr, std::nullopt, BI->getSExtValue());
1662 } else if (Attr != dwarf::DW_AT_lower_bound || DefaultLowerBound == -1 ||
1663 BI->getSExtValue() != DefaultLowerBound)
1664 addSInt(DW_Subrange, Attr, dwarf::DW_FORM_sdata, BI->getSExtValue());
1665 }
1666 };
1667
1668 AddBoundTypeEntry(dwarf::DW_AT_lower_bound, SR->getLowerBound());
1669
1670 AddBoundTypeEntry(dwarf::DW_AT_count, SR->getCount());
1671
1672 AddBoundTypeEntry(dwarf::DW_AT_upper_bound, SR->getUpperBound());
1673
1674 AddBoundTypeEntry(dwarf::DW_AT_byte_stride, SR->getStride());
1675}
1676
1677void DwarfUnit::constructGenericSubrangeDIE(DIE &Buffer,
1678 const DIGenericSubrange *GSR) {
1679 DIE &DwGenericSubrange =
1680 createAndAddDIE(dwarf::DW_TAG_generic_subrange, Buffer);
1681 // Get an anonymous type for index type.
1682 // FIXME: This type should be passed down from the front end
1683 // as different languages may have different sizes for indexes.
1684 DIE *IdxTy = getIndexTyDie();
1685 addDIEEntry(DwGenericSubrange, dwarf::DW_AT_type, *IdxTy);
1686
1687 int64_t DefaultLowerBound = getDefaultLowerBound();
1688
1689 auto AddBoundTypeEntry = [&](dwarf::Attribute Attr,
1690 DIGenericSubrange::BoundType Bound) -> void {
1691 if (auto *BV = dyn_cast_if_present<DIVariable *>(Bound)) {
1692 if (auto *VarDIE = getDIE(BV))
1693 addDIEEntry(DwGenericSubrange, Attr, *VarDIE);
1694 } else if (auto *BE = dyn_cast_if_present<DIExpression *>(Bound)) {
1695 if (BE->isConstant() &&
1697 *BE->isConstant()) {
1698 if (Attr != dwarf::DW_AT_lower_bound || DefaultLowerBound == -1 ||
1699 static_cast<int64_t>(BE->getElement(1)) != DefaultLowerBound)
1700 addSInt(DwGenericSubrange, Attr, dwarf::DW_FORM_sdata,
1701 BE->getElement(1));
1702 } else {
1703 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1704 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1705 DwarfExpr.setMemoryLocationKind();
1706 DwarfExpr.addExpression(BE);
1707 addBlock(DwGenericSubrange, Attr, DwarfExpr.finalize());
1708 }
1709 }
1710 };
1711
1712 AddBoundTypeEntry(dwarf::DW_AT_lower_bound, GSR->getLowerBound());
1713 AddBoundTypeEntry(dwarf::DW_AT_count, GSR->getCount());
1714 AddBoundTypeEntry(dwarf::DW_AT_upper_bound, GSR->getUpperBound());
1715 AddBoundTypeEntry(dwarf::DW_AT_byte_stride, GSR->getStride());
1716}
1717
1718DIE *DwarfUnit::getIndexTyDie() {
1719 if (IndexTyDie)
1720 return IndexTyDie;
1721 // Construct an integer type to use for indexes.
1722 IndexTyDie = &createAndAddDIE(dwarf::DW_TAG_base_type, getUnitDie());
1723 StringRef Name = "__ARRAY_SIZE_TYPE__";
1724 addString(*IndexTyDie, dwarf::DW_AT_name, Name);
1725 addUInt(*IndexTyDie, dwarf::DW_AT_byte_size, std::nullopt, sizeof(int64_t));
1726 addUInt(*IndexTyDie, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1728 DD->addAccelType(*this, CUNode->getNameTableKind(), Name, *IndexTyDie,
1729 /*Flags*/ 0);
1730 return IndexTyDie;
1731}
1732
1733/// Returns true if the vector's size differs from the sum of sizes of elements
1734/// the user specified. This can occur if the vector has been rounded up to
1735/// fit memory alignment constraints.
1736static bool hasVectorBeenPadded(const DICompositeType *CTy) {
1737 assert(CTy && CTy->isVector() && "Composite type is not a vector");
1738 const uint64_t ActualSize = CTy->getSizeInBits();
1739
1740 // Obtain the size of each element in the vector.
1741 DIType *BaseTy = CTy->getBaseType();
1742 assert(BaseTy && "Unknown vector element type.");
1743 const uint64_t ElementSize = BaseTy->getSizeInBits();
1744
1745 // Locate the number of elements in the vector.
1746 const DINodeArray Elements = CTy->getElements();
1747 assert(Elements.size() == 1 &&
1748 Elements[0]->getTag() == dwarf::DW_TAG_subrange_type &&
1749 "Invalid vector element array, expected one element of type subrange");
1750 const auto Subrange = cast<DISubrange>(Elements[0]);
1751 const auto NumVecElements =
1752 Subrange->getCount()
1753 ? cast<ConstantInt *>(Subrange->getCount())->getSExtValue()
1754 : 0;
1755
1756 // Ensure we found the element count and that the actual size is wide
1757 // enough to contain the requested size.
1758 assert(ActualSize >= (NumVecElements * ElementSize) && "Invalid vector size");
1759 return ActualSize != (NumVecElements * ElementSize);
1760}
1761
1762void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
1763 if (CTy->isVector()) {
1764 addFlag(Buffer, dwarf::DW_AT_GNU_vector);
1765 if (hasVectorBeenPadded(CTy))
1766 addUInt(Buffer, dwarf::DW_AT_byte_size, std::nullopt,
1767 CTy->getSizeInBits() / CHAR_BIT);
1768 }
1769
1770 if (DIVariable *Var = CTy->getDataLocation()) {
1771 if (auto *VarDIE = getDIE(Var))
1772 addDIEEntry(Buffer, dwarf::DW_AT_data_location, *VarDIE);
1773 } else if (DIExpression *Expr = CTy->getDataLocationExp()) {
1774 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1775 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1776 DwarfExpr.setMemoryLocationKind();
1777 DwarfExpr.addExpression(Expr);
1778 addBlock(Buffer, dwarf::DW_AT_data_location, DwarfExpr.finalize());
1779 }
1780
1781 if (DIVariable *Var = CTy->getAssociated()) {
1782 if (auto *VarDIE = getDIE(Var))
1783 addDIEEntry(Buffer, dwarf::DW_AT_associated, *VarDIE);
1784 } else if (DIExpression *Expr = CTy->getAssociatedExp()) {
1785 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1786 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1787 DwarfExpr.setMemoryLocationKind();
1788 DwarfExpr.addExpression(Expr);
1789 addBlock(Buffer, dwarf::DW_AT_associated, DwarfExpr.finalize());
1790 }
1791
1792 if (DIVariable *Var = CTy->getAllocated()) {
1793 if (auto *VarDIE = getDIE(Var))
1794 addDIEEntry(Buffer, dwarf::DW_AT_allocated, *VarDIE);
1795 } else if (DIExpression *Expr = CTy->getAllocatedExp()) {
1796 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1797 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1798 DwarfExpr.setMemoryLocationKind();
1799 DwarfExpr.addExpression(Expr);
1800 addBlock(Buffer, dwarf::DW_AT_allocated, DwarfExpr.finalize());
1801 }
1802
1803 if (auto *RankConst = CTy->getRankConst()) {
1804 addSInt(Buffer, dwarf::DW_AT_rank, dwarf::DW_FORM_sdata,
1805 RankConst->getSExtValue());
1806 } else if (auto *RankExpr = CTy->getRankExp()) {
1807 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1808 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1809 DwarfExpr.setMemoryLocationKind();
1810 DwarfExpr.addExpression(RankExpr);
1811 addBlock(Buffer, dwarf::DW_AT_rank, DwarfExpr.finalize());
1812 }
1813
1814 if (auto *BitStride = CTy->getBitStrideConst()) {
1815 addUInt(Buffer, dwarf::DW_AT_bit_stride, {}, BitStride->getZExtValue());
1816 }
1817
1818 // Emit the element type.
1819 addType(Buffer, CTy->getBaseType());
1820
1821 // Add subranges to array type.
1822 DINodeArray Elements = CTy->getElements();
1823 for (DINode *E : Elements) {
1824 if (auto *Element = dyn_cast_or_null<DISubrangeType>(E)) {
1825 DIE &TyDIE = createAndAddDIE(Element->getTag(), Buffer, CTy);
1826 constructSubrangeDIE(TyDIE, Element, true);
1827 } else if (auto *Element = dyn_cast_or_null<DISubrange>(E))
1828 constructSubrangeDIE(Buffer, Element);
1829 else if (auto *Element = dyn_cast_or_null<DIGenericSubrange>(E))
1830 constructGenericSubrangeDIE(Buffer, Element);
1831 }
1832}
1833
1834void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
1835 const DIType *DTy = CTy->getBaseType();
1836 bool IsUnsigned = DTy && DD->isUnsignedDIType(DTy);
1837 if (DTy) {
1838 if (!Asm->TM.Options.DebugStrictDwarf || DD->getDwarfVersion() >= 3)
1839 addType(Buffer, DTy);
1840 if (DD->getDwarfVersion() >= 4 && (CTy->getFlags() & DINode::FlagEnumClass))
1841 addFlag(Buffer, dwarf::DW_AT_enum_class);
1842 }
1843
1844 if (auto Kind = CTy->getEnumKind())
1845 addUInt(Buffer, dwarf::DW_AT_APPLE_enum_kind, dwarf::DW_FORM_data1, *Kind);
1846
1847 auto *Context = CTy->getScope();
1848 bool IndexEnumerators = !Context || isa<DICompileUnit>(Context) || isa<DIFile>(Context) ||
1850 DINodeArray Elements = CTy->getElements();
1851
1852 // Add enumerators to enumeration type.
1853 for (const DINode *E : Elements) {
1855 if (Enum) {
1856 DIE &Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
1857 StringRef Name = Enum->getName();
1858 addString(Enumerator, dwarf::DW_AT_name, Name);
1859 addConstantValue(Enumerator, Enum->getValue(), IsUnsigned);
1860 if (IndexEnumerators)
1862 }
1863 }
1864}
1865
1867 for (auto &P : ContainingTypeMap) {
1868 DIE &SPDie = *P.first;
1869 const DINode *D = P.second;
1870 if (!D)
1871 continue;
1872 DIE *NDie = getDIE(D);
1873 if (!NDie)
1874 continue;
1875 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, *NDie);
1876 }
1877}
1878
1879DIE &DwarfUnit::constructMemberDIE(DIE &Buffer, const DIDerivedType *DT) {
1880 DIE &MemberDie = createAndAddDIE(DT->getTag(), Buffer, DT);
1881 StringRef Name = DT->getName();
1882 if (!Name.empty())
1883 addString(MemberDie, dwarf::DW_AT_name, Name);
1884
1885 addAnnotation(MemberDie, DT->getAnnotations());
1886
1887 if (DIType *Resolved = DT->getBaseType())
1888 addType(MemberDie, Resolved);
1889
1890 addSourceLine(MemberDie, DT);
1891
1892 if (DT->getTag() == dwarf::DW_TAG_inheritance && DT->isVirtual()) {
1893
1894 // For C++, virtual base classes are not at fixed offset. Use following
1895 // expression to extract appropriate offset from vtable.
1896 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1897
1898 DIELoc *VBaseLocationDie = new (DIEValueAllocator) DIELoc;
1899 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1900 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1901 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1902 addUInt(*VBaseLocationDie, dwarf::DW_FORM_udata, DT->getOffsetInBits());
1903 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1904 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1905 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1906
1907 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
1908 } else {
1909 uint64_t Size = 0;
1910 uint64_t FieldSize = 0;
1911
1912 bool IsBitfield = DT->isBitField();
1913
1914 // Handle the size.
1915 if (DT->getRawSizeInBits() == nullptr) {
1916 // No size, just ignore.
1917 } else if (auto *Var = dyn_cast<DIVariable>(DT->getRawSizeInBits())) {
1918 if (auto *VarDIE = getDIE(Var))
1919 addDIEEntry(MemberDie, dwarf::DW_AT_bit_size, *VarDIE);
1920 } else if (auto *Exp = dyn_cast<DIExpression>(DT->getRawSizeInBits())) {
1921 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1922 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1923 DwarfExpr.setMemoryLocationKind();
1924 DwarfExpr.addExpression(Exp);
1925 addBlock(MemberDie, dwarf::DW_AT_bit_size, DwarfExpr.finalize());
1926 } else {
1927 Size = DT->getSizeInBits();
1928 FieldSize = DD->getBaseTypeSize(DT);
1929 if (IsBitfield) {
1930 // Handle bitfield, assume bytes are 8 bits.
1931 if (DD->useDWARF2Bitfields())
1932 addUInt(MemberDie, dwarf::DW_AT_byte_size, std::nullopt,
1933 FieldSize / 8);
1934 addUInt(MemberDie, dwarf::DW_AT_bit_size, std::nullopt, Size);
1935 }
1936 }
1937
1938 // Handle the location. DW_AT_data_bit_offset won't allow an
1939 // expression until DWARF 6, but it can be used as an extension.
1940 // See https://dwarfstd.org/issues/250501.1.html
1941 if (auto *Var = dyn_cast_or_null<DIVariable>(DT->getRawOffsetInBits())) {
1942 if (!Asm->TM.Options.DebugStrictDwarf || DD->getDwarfVersion() >= 6) {
1943 if (auto *VarDIE = getDIE(Var))
1944 addDIEEntry(MemberDie, dwarf::DW_AT_data_bit_offset, *VarDIE);
1945 }
1946 } else if (auto *Expr =
1948 if (!Asm->TM.Options.DebugStrictDwarf || DD->getDwarfVersion() >= 6) {
1949 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1950 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1951 DwarfExpr.setMemoryLocationKind();
1952 DwarfExpr.addExpression(Expr);
1953 addBlock(MemberDie, dwarf::DW_AT_data_bit_offset, DwarfExpr.finalize());
1954 }
1955 } else {
1956 uint32_t AlignInBytes = DT->getAlignInBytes();
1957 uint64_t OffsetInBytes;
1958
1959 if (IsBitfield) {
1960 assert(DT->getOffsetInBits() <=
1961 (uint64_t)std::numeric_limits<int64_t>::max());
1962 int64_t Offset = DT->getOffsetInBits();
1963 // We can't use DT->getAlignInBits() here: AlignInBits for member type
1964 // is non-zero if and only if alignment was forced (e.g. _Alignas()),
1965 // which can't be done with bitfields. Thus we use FieldSize here.
1966 uint32_t AlignInBits = FieldSize;
1967 uint32_t AlignMask = ~(AlignInBits - 1);
1968 // The bits from the start of the storage unit to the start of the
1969 // field.
1970 uint64_t StartBitOffset = Offset - (Offset & AlignMask);
1971 // The byte offset of the field's aligned storage unit inside the
1972 // struct.
1973 OffsetInBytes = (Offset - StartBitOffset) / 8;
1974
1975 if (DD->useDWARF2Bitfields()) {
1976 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1977 uint64_t FieldOffset = (HiMark - FieldSize);
1978 Offset -= FieldOffset;
1979
1980 // Maybe we need to work from the other end.
1981 if (Asm->getDataLayout().isLittleEndian())
1982 Offset = FieldSize - (Offset + Size);
1983
1984 if (Offset < 0)
1985 addSInt(MemberDie, dwarf::DW_AT_bit_offset, dwarf::DW_FORM_sdata,
1986 Offset);
1987 else
1988 addUInt(MemberDie, dwarf::DW_AT_bit_offset, std::nullopt,
1989 (uint64_t)Offset);
1990 OffsetInBytes = FieldOffset >> 3;
1991 } else {
1992 addUInt(MemberDie, dwarf::DW_AT_data_bit_offset, std::nullopt,
1993 Offset);
1994 }
1995 } else {
1996 // This is not a bitfield.
1997 OffsetInBytes = DT->getOffsetInBits() / 8;
1998 if (AlignInBytes)
1999 addUInt(MemberDie, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
2000 AlignInBytes);
2001 }
2002
2003 if (DD->getDwarfVersion() <= 2) {
2004 DIELoc *MemLocationDie = new (DIEValueAllocator) DIELoc;
2005 addUInt(*MemLocationDie, dwarf::DW_FORM_data1,
2006 dwarf::DW_OP_plus_uconst);
2007 addUInt(*MemLocationDie, dwarf::DW_FORM_udata, OffsetInBytes);
2008 addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie);
2009 } else if (!IsBitfield || DD->useDWARF2Bitfields()) {
2010 // In DWARF v3, DW_FORM_data4/8 in DW_AT_data_member_location are
2011 // interpreted as location-list pointers. Interpreting constants as
2012 // pointers is not expected, so we use DW_FORM_udata to encode the
2013 // constants here.
2014 if (DD->getDwarfVersion() == 3)
2015 addUInt(MemberDie, dwarf::DW_AT_data_member_location,
2016 dwarf::DW_FORM_udata, OffsetInBytes);
2017 else
2018 addUInt(MemberDie, dwarf::DW_AT_data_member_location, std::nullopt,
2019 OffsetInBytes);
2020 }
2021 }
2022 }
2023
2024 addAccess(MemberDie, DT->getFlags());
2025
2026 if (DT->isVirtual())
2027 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
2028 dwarf::DW_VIRTUALITY_virtual);
2029
2030 // Objective-C properties.
2031 if (DINode *PNode = DT->getObjCProperty())
2032 if (DIE *PDie = getDIE(PNode))
2033 addAttribute(MemberDie, dwarf::DW_AT_APPLE_property,
2034 dwarf::DW_FORM_ref4, DIEEntry(*PDie));
2035
2036 if (DT->isArtificial())
2037 addFlag(MemberDie, dwarf::DW_AT_artificial);
2038
2039 return MemberDie;
2040}
2041
2043 if (!DT)
2044 return nullptr;
2045
2046 // Construct the context before querying for the existence of the DIE in case
2047 // such construction creates the DIE.
2048 DIE *ContextDIE = getOrCreateContextDIE(DT->getScope());
2049 assert(dwarf::isType(ContextDIE->getTag()) &&
2050 "Static member should belong to a type.");
2051
2052 if (DIE *StaticMemberDIE = getDIE(DT))
2053 return StaticMemberDIE;
2054
2055 DwarfUnit *ContextUnit = static_cast<DwarfUnit *>(ContextDIE->getUnit());
2056 DIE &StaticMemberDIE = createAndAddDIE(DT->getTag(), *ContextDIE, DT);
2057
2058 const DIType *Ty = DT->getBaseType();
2059
2060 addString(StaticMemberDIE, dwarf::DW_AT_name, DT->getName());
2061 addType(StaticMemberDIE, Ty);
2062 ContextUnit->addSourceLine(StaticMemberDIE, DT);
2063 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
2064 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
2065
2066 // Consider the case when the static member was created by the compiler.
2067 if (DT->isArtificial())
2068 addFlag(StaticMemberDIE, dwarf::DW_AT_artificial);
2069
2070 // FIXME: We could omit private if the parent is a class_type, and
2071 // public if the parent is something else.
2072 addAccess(StaticMemberDIE, DT->getFlags());
2073
2075 addConstantValue(StaticMemberDIE, CI, Ty);
2076 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT->getConstant()))
2077 addConstantFPValue(StaticMemberDIE, CFP);
2078
2079 if (uint32_t AlignInBytes = DT->getAlignInBytes())
2080 addUInt(StaticMemberDIE, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
2081 AlignInBytes);
2082
2083 return &StaticMemberDIE;
2084}
2085
2087 // Emit size of content not including length itself
2088 if (!DD->useSectionsAsReferences())
2089 EndLabel = Asm->emitDwarfUnitLength(
2090 isDwoUnit() ? "debug_info_dwo" : "debug_info", "Length of Unit");
2091 else
2092 Asm->emitDwarfUnitLength(getHeaderSize() + getUnitDie().getSize(),
2093 "Length of Unit");
2094
2095 Asm->OutStreamer->AddComment("DWARF version number");
2096 unsigned Version = DD->getDwarfVersion();
2097 Asm->emitInt16(Version);
2098
2099 // DWARF v5 reorders the address size and adds a unit type.
2100 if (Version >= 5) {
2101 Asm->OutStreamer->AddComment("DWARF Unit Type");
2102 Asm->emitInt8(UT);
2103 Asm->OutStreamer->AddComment("Address Size (in bytes)");
2104 Asm->emitInt8(Asm->MAI->getCodePointerSize());
2105 }
2106
2107 // We share one abbreviations table across all units so it's always at the
2108 // start of the section. Use a relocatable offset where needed to ensure
2109 // linking doesn't invalidate that offset.
2110 Asm->OutStreamer->AddComment("Offset Into Abbrev. Section");
2111 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
2112 if (UseOffsets)
2113 Asm->emitDwarfLengthOrOffset(0);
2114 else
2115 Asm->emitDwarfSymbolReference(
2116 TLOF.getDwarfAbbrevSection()->getBeginSymbol(), false);
2117
2118 if (Version <= 4) {
2119 Asm->OutStreamer->AddComment("Address Size (in bytes)");
2120 Asm->emitInt8(Asm->MAI->getCodePointerSize());
2121 }
2122}
2123
2124void DwarfTypeUnit::emitHeader(bool UseOffsets) {
2125 if (!DD->useSplitDwarf()) {
2126 LabelBegin = Asm->createTempSymbol("tu_begin");
2127 Asm->OutStreamer->emitLabel(LabelBegin);
2128 }
2129 DwarfUnit::emitCommonHeader(UseOffsets,
2130 DD->useSplitDwarf() ? dwarf::DW_UT_split_type
2131 : dwarf::DW_UT_type);
2132 Asm->OutStreamer->AddComment("Type Signature");
2133 Asm->OutStreamer->emitIntValue(TypeSignature, sizeof(TypeSignature));
2134 Asm->OutStreamer->AddComment("Type DIE Offset");
2135 // In a skeleton type unit there is no type DIE so emit a zero offset.
2136 Asm->emitDwarfLengthOrOffset(Ty ? Ty->getOffset() : 0);
2137}
2138
2140 const MCSymbol *Hi, const MCSymbol *Lo) {
2141 addAttribute(Die, Attribute, DD->getDwarfSectionOffsetForm(),
2143}
2144
2146 const MCSymbol *Label, const MCSymbol *Sec) {
2147 if (Asm->doesDwarfUseRelocationsAcrossSections())
2148 addLabel(Die, Attribute, DD->getDwarfSectionOffsetForm(), Label);
2149 else
2150 addSectionDelta(Die, Attribute, Label, Sec);
2151}
2152
2153bool DwarfTypeUnit::isDwoUnit() const {
2154 // Since there are no skeleton type units, all type units are dwo type units
2155 // when split DWARF is being used.
2156 return DD->useSplitDwarf();
2157}
2158
2160 const DIScope *Context) {
2161 getCU().addGlobalNameForTypeUnit(Name, Context);
2162}
2163
2165 const DIScope *Context) {
2166 getCU().addGlobalTypeUnitType(Ty, Context);
2167}
2168
2171 return nullptr;
2172 if (isDwoUnit())
2173 return nullptr;
2174 return getSection()->getBeginSymbol();
2175}
2176
2178 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
2179 addSectionLabel(getUnitDie(), dwarf::DW_AT_str_offsets_base,
2180 DU->getStringOffsetsStartSym(),
2182}
2183
2185 assert(DD->getDwarfVersion() >= 5 &&
2186 "DW_AT_rnglists_base requires DWARF version 5 or later");
2187 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
2188 addSectionLabel(getUnitDie(), dwarf::DW_AT_rnglists_base,
2189 DU->getRnglistsTableBaseSym(),
2191}
2192
2193void DwarfTypeUnit::finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) {
2195}
2196
2197bool DwarfUnit::isCompatibleWithVersion(uint16_t Version) const {
2198 return !Asm->TM.Options.DebugStrictDwarf || DD->getDwarfVersion() >= Version;
2199}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
BitTracker BT
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static bool hasVectorBeenPadded(const DICompositeType *CTy)
Returns true if the vector's size differs from the sum of sizes of elements the user specified.
#define I(x, y, z)
Definition MD5.cpp:58
#define G(x, y, z)
Definition MD5.cpp:56
Register const TargetRegisterInfo * TRI
This file contains the declarations for metadata subclasses.
#define T
#define P(N)
static enum BaseType getBaseType(const Value *Val)
Return the baseType for Val which states whether Val is exclusively derived from constant/null,...
static unsigned getSize(unsigned Kind)
APInt bitcastToAPInt() const
Definition APFloat.h:1335
Class for arbitrary precision integers.
Definition APInt.h:78
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1540
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1488
const uint64_t * getRawData() const
This function returns a pointer to the internal storage of the APInt.
Definition APInt.h:569
int64_t getSExtValue() const
Get sign extended value.
Definition APInt.h:1562
void resetUsedFlag(bool HasBeenUsed=false)
Definition AddressPool.h:51
Annotations lets you mark points and ranges inside source code, for tests:
Definition Annotations.h:53
This class is intended to be used as a driving class for all asm writers.
Definition AsmPrinter.h:96
MCSymbol * getSymbol(const GlobalValue *GV) const
TargetMachine & TM
Target machine description.
Definition AsmPrinter.h:99
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition AsmPrinter.h:106
const DataLayout & getDataLayout() const
Return information about data layout.
bool doesDwarfUseRelocationsAcrossSections() const
Definition AsmPrinter.h:372
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:69
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:277
const APFloat & getValueAPF() const
Definition Constants.h:320
This is the shared class of boolean and integer constants.
Definition Constants.h:87
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:154
This is an important base class in LLVM.
Definition Constant.h:43
Basic type, like 'int' or 'float'.
uint32_t getDataSizeInBits() const
unsigned getEncoding() const
DIExpression * getRankExp() const
DIExpression * getAssociatedExp() const
DIVariable * getAllocated() const
DIExpression * getDataLocationExp() const
DIVariable * getAssociated() const
DIDerivedType * getDiscriminator() const
DIVariable * getDataLocation() const
unsigned getRuntimeLang() const
DIType * getSpecification() const
StringRef getIdentifier() const
DINodeArray getElements() const
DITemplateParameterArray getTemplateParams() const
DIExpression * getAllocatedExp() const
ConstantInt * getBitStrideConst() const
std::optional< uint32_t > getEnumKind() const
DIType * getVTableHolder() const
DINodeArray getAnnotations() const
ConstantInt * getRankConst() const
MDString * getRawIdentifier() const
DIType * getBaseType() const
DINodeArray getAnnotations() const
Get annotations associated with this derived type.
DITemplateParameterArray getTemplateParams() const
Get the template parameters from a template alias.
DIObjCProperty * getObjCProperty() const
LLVM_ABI Constant * getConstant() const
DIEBlock - Represents a block of values.
Definition DIE.h:1056
A simple label difference DIE.
Definition DIE.h:265
DwarfExpression implementation for singular DW_AT_location.
DIEDwarfExpression(const AsmPrinter &AP, DwarfCompileUnit &CU, DIELoc &DIE)
Definition DwarfUnit.cpp:41
A pointer to another debug information entry.
Definition DIE.h:325
A container for inline string values.
Definition DIE.h:303
An integer value DIE.
Definition DIE.h:169
static dwarf::Form BestForm(bool IsSigned, uint64_t Int)
Choose the best form for integer.
Definition DIE.h:176
A label DIE.
Definition DIE.h:226
DIELoc - Represents an expression location.
Definition DIE.h:1020
A container for string pool string values.
Definition DIE.h:283
virtual const MCSymbol * getCrossSectionRelativeBaseAddress() const
Definition DIE.h:999
LLVM_ABI DIEUnit(dwarf::Tag UnitTag)
Definition DIE.cpp:306
MCSection * getSection() const
Return the section that this DIEUnit will be emitted into.
Definition DIE.h:1006
DIE & getUnitDie()
Definition DIE.h:1009
A list of DIE values.
Definition DIE.h:698
A structured debug information entry.
Definition DIE.h:828
DIE & addChild(DIE *Child)
Add a child to the DIE.
Definition DIE.h:944
static DIE * get(BumpPtrAllocator &Alloc, dwarf::Tag Tag)
Definition DIE.h:858
LLVM_ABI DIEUnit * getUnit() const
Climb up the parent chain to get the compile unit or type unit that this DIE belongs to.
Definition DIE.cpp:203
dwarf::Tag getTag() const
Definition DIE.h:864
const APInt & getDenominator() const
LLVM_ABI bool isSigned() const
const APInt & getNumerator() const
LLVM_ABI BoundType getLowerBound() const
LLVM_ABI BoundType getCount() const
LLVM_ABI BoundType getUpperBound() const
PointerUnion< DIVariable *, DIExpression * > BoundType
LLVM_ABI BoundType getStride() const
Represents a module in the programming language, for example, a Clang module, or a Fortran module.
Debug lexical block.
DIScope * getScope() const
StringRef getName() const
bool getExportSymbols() const
Tagged DWARF-like metadata node.
LLVM_ABI dwarf::Tag getTag() const
DIFlags
Debug info flags.
Base class for scope-like contexts.
LLVM_ABI DIScope * getScope() const
String type, Fortran CHARACTER(n)
unsigned getEncoding() const
DIExpression * getStringLengthExp() const
DIVariable * getStringLength() const
DIExpression * getStringLocationExp() const
Subprogram description. Uses SubclassData1.
BoundType getLowerBound() const
PointerUnion< ConstantInt *, DIVariable *, DIExpression * > BoundType
BoundType getBias() const
BoundType getUpperBound() const
DIType * getBaseType() const
Get the base type this is derived from.
BoundType getStride() const
Array subrange.
LLVM_ABI BoundType getUpperBound() const
LLVM_ABI BoundType getStride() const
LLVM_ABI BoundType getLowerBound() const
LLVM_ABI BoundType getCount() const
Type array for a subprogram.
Base class for types.
bool isLittleEndian() const
uint32_t getNumExtraInhabitants() const
bool isBigEndian() const
bool isLValueReference() const
bool isBitField() const
bool isVirtual() const
bool isObjcClassComplete() const
bool isAppleBlockExtension() const
uint64_t getOffsetInBits() const
bool isVector() const
DIFlags getFlags() const
StringRef getName() const
bool isForwardDecl() const
bool isTypePassByValue() const
uint64_t getSizeInBits() const
uint32_t getAlignInBytes() const
Metadata * getRawSizeInBits() const
bool isRValueReference() const
bool isArtificial() const
bool getExportSymbols() const
DIScope * getScope() const
bool isTypePassByReference() const
Metadata * getRawOffsetInBits() const
bool isLittleEndian() const
Layout endianness...
Definition DataLayout.h:207
void addGlobalNameForTypeUnit(StringRef Name, const DIScope *Context)
Add a new global name present in a type unit to this compile unit.
unsigned getOrCreateSourceID(const DIFile *File) override
Look up the source ID for the given file.
void addGlobalTypeUnitType(const DIType *Ty, const DIScope *Context)
Add a new global type present in a type unit to this compile unit.
Collects and handles dwarf debug information.
Definition DwarfDebug.h:351
std::optional< MD5::MD5Result > getMD5AsBytes(const DIFile *File) const
If the File has an MD5 checksum, return it as an MD5Result allocated in the MCContext.
uint16_t getDwarfVersion() const
Returns the Dwarf Version.
AddressPool & getAddressPool()
Definition DwarfDebug.h:879
bool useSplitDwarf() const
Returns whether or not to change the current debug info for the split dwarf proposal support.
Definition DwarfDebug.h:824
virtual void disableTemporaryBuffer()=0
Disable emission to the temporary buffer.
virtual unsigned getTemporaryBufferSize()=0
Return the emitted size, in number of bytes, for the data stored in the temporary buffer.
void setMemoryLocationKind()
Lock this down to become a memory location description.
DwarfCompileUnit & CU
void addExpression(DIExpressionCursor &&Expr)
Emit all remaining operations in the DIExpressionCursor.
virtual void commitTemporaryBuffer()=0
Commit the data stored in the temporary buffer to the main output.
DwarfExpression(unsigned DwarfVersion, DwarfCompileUnit &CU)
virtual void enableTemporaryBuffer()=0
Start emitting data to the temporary buffer.
DwarfTypeUnit(DwarfCompileUnit &CU, AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU, unsigned UniqueID, MCDwarfDwoLineTable *SplitLineTable=nullptr)
Definition DwarfUnit.cpp:89
void addGlobalTypeImpl(const DIType *Ty, const DIE &Die, const DIScope *Context) override
Add a new global type to the compile unit.
DwarfCompileUnit & getCU() override
Definition DwarfUnit.h:442
void emitHeader(bool UseOffsets) override
Emit the header for this unit, not including the initial length field.
void addGlobalName(StringRef Name, const DIE &Die, const DIScope *Context) override
Add a new global name to the compile unit.
virtual DIE * getOrCreateTypeDIE(const MDNode *TyNode)
Find existing DIE or create new DIE for the given type.
void addInt(DIE &Die, dwarf::Attribute Attribute, const APInt &Integer, bool Unsigned)
Add an integer attribute data and value; value may be any width.
DwarfDebug & getDwarfDebug() const
Definition DwarfUnit.h:113
void addThrownTypes(DIE &Die, DINodeArray ThrownTypes)
Add thrown types.
void addStringOffsetsStart()
Add the DW_AT_str_offsets_base attribute to the unit DIE.
void addAnnotation(DIE &Buffer, DINodeArray Annotations)
Add DW_TAG_LLVM_annotation.
std::vector< DIEBlock * > DIEBlocks
A list of all the DIEBlocks in use.
Definition DwarfUnit.h:67
std::vector< DIELoc * > DIELocs
A list of all the DIELocs in use.
Definition DwarfUnit.h:70
void addBlock(DIE &Die, dwarf::Attribute Attribute, DIELoc *Loc)
Add block data.
void addTemplateParams(DIE &Buffer, DINodeArray TParams)
Add template parameters in buffer.
virtual DIE * getOrCreateContextDIE(const DIScope *Context)
Get context owner's DIE.
bool useSegmentedStringOffsetsTable() const
Definition DwarfUnit.h:290
bool applySubprogramDefinitionAttributes(const DISubprogram *SP, DIE &SPDie, bool Minimal)
DIELoc * getDIELoc()
Returns a fresh newly allocated DIELoc.
Definition DwarfUnit.h:145
void updateAcceleratorTables(const DIScope *Context, const DIType *Ty, const DIE &TyDIE)
If this is a named finished type then include it in the list of types for the accelerator tables.
void addAttribute(DIEValueList &Die, dwarf::Attribute Attribute, dwarf::Form Form, T &&Value)
Definition DwarfUnit.h:85
void addOpAddress(DIELoc &Die, const MCSymbol *Sym)
Add a dwarf op address data and value using the form given and an op of either DW_FORM_addr or DW_FOR...
void addUInt(DIEValueList &Die, dwarf::Attribute Attribute, std::optional< dwarf::Form > Form, uint64_t Integer)
Add an unsigned integer attribute data and value.
void addString(DIE &Die, dwarf::Attribute Attribute, StringRef Str)
Add a string attribute data and value.
void addConstantValue(DIE &Die, const ConstantInt *CI, const DIType *Ty)
Add constant value entry in variable DIE.
DIE * getOrCreateNameSpace(const DINamespace *NS)
void insertDIE(const DINode *Desc, DIE *D)
Insert DIE into the map.
void addAccess(DIE &Die, DINode::DIFlags Flags)
Add the accessibility attribute.
void addSectionDelta(DIE &Die, dwarf::Attribute Attribute, const MCSymbol *Hi, const MCSymbol *Lo)
addSectionDelta - Add a label delta attribute data and value.
void addGlobalType(const DIType *Ty, const DIE &Die, const DIScope *Context)
DIE * createTypeDIE(const DIScope *Context, DIE &ContextDIE, const DIType *Ty)
Creates type DIE with specific context.
bool shouldPlaceInUnitDIE(const DISubprogram *SP, bool Minimal)
Definition DwarfUnit.h:349
DwarfDebug * DD
Definition DwarfUnit.h:56
DenseMap< DIE *, const DINode * > ContainingTypeMap
This map is used to keep track of subprogram DIEs that need DW_AT_containing_type attribute.
Definition DwarfUnit.h:75
const DICompileUnit * CUNode
MDNode for the compile unit.
Definition DwarfUnit.h:41
virtual unsigned getOrCreateSourceID(const DIFile *File)=0
Look up the source ID for the given file.
virtual DIE * getOrCreateSubprogramDIE(const DISubprogram *SP, const Function *FnHint, bool Minimal=false)
DIE * getOrCreateSubprogramContextDIE(const DISubprogram *SP, bool IgnoreScope)
Definition DwarfUnit.h:354
virtual void addGlobalTypeImpl(const DIType *Ty, const DIE &Die, const DIScope *Context)=0
Add a new global type to the compile unit.
void addDIETypeSignature(DIE &Die, uint64_t Signature)
Add a type's DW_AT_signature and set the declaration flag.
std::optional< unsigned > constructSubprogramArguments(DIE &Buffer, DITypeRefArray Args)
Construct function argument DIEs.
virtual DwarfCompileUnit & getCU()=0
DIE * getDIE(const DINode *D) const
Returns the DIE map slot for the specified debug variable.
virtual unsigned getHeaderSize() const
Compute the size of a header for this unit, not including the initial length field.
Definition DwarfUnit.h:296
MCSymbol * LabelBegin
The start of the unit within its section.
Definition DwarfUnit.h:50
void addSInt(DIEValueList &Die, dwarf::Attribute Attribute, std::optional< dwarf::Form > Form, int64_t Integer)
Add an signed integer attribute data and value.
DwarfUnit(dwarf::Tag, const DICompileUnit *Node, AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU, unsigned UniqueID=0)
Definition DwarfUnit.cpp:83
void addLabelDelta(DIEValueList &Die, dwarf::Attribute Attribute, const MCSymbol *Hi, const MCSymbol *Lo)
Add a label delta attribute data and value.
void addLinkageName(DIE &Die, StringRef LinkageName)
Add a linkage name, if it isn't empty.
std::string getParentContextString(const DIScope *Context) const
Get string containing language specific context for a global name.
void addSourceLine(DIE &Die, unsigned Line, unsigned Column, const DIFile *File)
Add location information to specified debug information entry.
void emitCommonHeader(bool UseOffsets, dwarf::UnitType UT)
Emit the common part of the header for this unit.
BumpPtrAllocator DIEValueAllocator
Definition DwarfUnit.h:44
DIE * IndexTyDie
An anonymous type for index type. Owned by DIEUnit.
Definition DwarfUnit.h:60
void addRnglistsBase()
Add the DW_AT_rnglists_base attribute to the unit DIE.
DIE * getOrCreateModule(const DIModule *M)
const DICompileUnit * getCUNode() const
Definition DwarfUnit.h:112
DIE & createAndAddDIE(dwarf::Tag Tag, DIE &Parent, const DINode *N=nullptr)
Create a DIE with the given Tag, add the DIE to its parent, and call insertDIE if MD is not null.
DwarfFile * DU
Definition DwarfUnit.h:57
void addSectionOffset(DIE &Die, dwarf::Attribute Attribute, uint64_t Integer)
Add an offset into a section attribute data and value.
DIE * getOrCreateStaticMemberDIE(const DIDerivedType *DT)
Create new static data member DIE.
void addLabel(DIEValueList &Die, dwarf::Attribute Attribute, dwarf::Form Form, const MCSymbol *Label)
Add a Dwarf label attribute data and value.
void addConstantFPValue(DIE &Die, const ConstantFP *CFP)
Add constant value entry in variable DIE.
void constructContainingTypeDIEs()
Construct DIEs for types that contain vtables.
unsigned UniqueID
A numeric ID unique among all CUs in the module.
Definition DwarfUnit.h:39
void addSectionLabel(DIE &Die, dwarf::Attribute Attribute, const MCSymbol *Label, const MCSymbol *Sec)
Add a Dwarf section label attribute data and value.
bool isShareableAcrossCUs(const DINode *D) const
Check whether the DIE for this MDNode can be shared across CUs.
llvm::dwarf::SourceLanguage getSourceLanguage() const
void addPoolOpAddress(DIEValueList &Die, const MCSymbol *Label)
~DwarfUnit() override
Definition DwarfUnit.cpp:95
DenseMap< const MDNode *, DIE * > MDNodeToDieMap
Tracks the mapping of unit level debug information variables to debug information entries.
Definition DwarfUnit.h:64
void constructTypeDIE(DIE &Buffer, const DICompositeType *CTy)
virtual void addGlobalName(StringRef Name, const DIE &Die, const DIScope *Context)=0
Add a new global name to the compile unit.
MCSymbol * EndLabel
Emitted at the end of the CU and used to compute the CU Length field.
Definition DwarfUnit.h:53
void addFlag(DIE &Die, dwarf::Attribute Attribute)
Add a flag that is true to the DIE.
AsmPrinter * Asm
Target of Dwarf emission.
Definition DwarfUnit.h:47
void addType(DIE &Entity, const DIType *Ty, dwarf::Attribute Attribute=dwarf::DW_AT_type)
Add a new type attribute to the specified entity.
void applySubprogramAttributes(const DISubprogram *SP, DIE &SPDie, bool SkipSPAttributes=false)
void addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIE &Entry)
Add a DIE attribute data and value.
static StringRef dropLLVMManglingEscape(StringRef Name)
If the given string begins with the GlobalValue name mangling escape character '\1',...
uint16_t getDwarfVersion() const
Definition MCContext.h:817
unsigned getFile(StringRef Directory, StringRef FileName, std::optional< MD5::MD5Result > Checksum, uint16_t DwarfVersion, std::optional< StringRef > Source)
Definition MCDwarf.h:357
MCSection * getDwarfStrOffSection() const
MCSection * getDwarfRnglistsSection() const
MCSection * getDwarfAbbrevSection() const
MCSymbol * getBeginSymbol()
Definition MCSection.h:593
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
Metadata node.
Definition Metadata.h:1078
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1442
A single uniqued string.
Definition Metadata.h:721
Root of the metadata hierarchy.
Definition Metadata.h:64
Wrapper class representing virtual and physical registers.
Definition Register.h:19
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:143
TargetOptions Options
unsigned DebugStrictDwarf
When set to true, don't use DWARF extensions in later DWARF versions.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
LLVM Value Representation.
Definition Value.h:75
Calculates the starting offsets for various sections within the .debug_names section.
Definition Dwarf.h:35
@ DW_ACCESS_private
Definition Dwarf.h:187
@ DW_ACCESS_protected
Definition Dwarf.h:186
@ DW_ACCESS_public
Definition Dwarf.h:185
Attribute
Attributes.
Definition Dwarf.h:125
SourceLanguageName
Definition Dwarf.h:223
UnitType
Constants for unit types in DWARF v5.
Definition Dwarf.h:896
bool isType(Tag T)
Definition Dwarf.h:113
@ DW_LANG_hi_user
Definition Dwarf.h:220
std::optional< SourceLanguage > toDW_LANG(SourceLanguageName name, uint32_t version)
Convert a DWARF 6 pair of language name and version to a DWARF 5 DW_LANG.
Definition Dwarf.h:231
bool isCPlusPlus(SourceLanguage S)
Definition Dwarf.h:512
TypeKind getArrayIndexTypeEncoding(SourceLanguage S)
Definition Dwarf.h:748
@ DW_DSC_range
Definition Dwarf.h:791
@ DW_DSC_label
Definition Dwarf.h:790
@ DW_FLAG_type_implementation
Definition Dwarf.h:953
bool isC(SourceLanguage S)
Definition Dwarf.h:672
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > dyn_extract(Y &&MD)
Extract a Value from Metadata, if any.
Definition Metadata.h:695
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
Definition Casting.h:732
Op::Description Desc
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
FunctionAddr VTableAddr uintptr_t uintptr_t Version
Definition InstrProf.h:302
auto reverse(ContainerTy &&C)
Definition STLExtras.h:406
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
constexpr T divideCeil(U Numerator, V Denominator)
Returns the integer ceil(Numerator / Denominator).
Definition MathExtras.h:394
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
#define N