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