blob: 0d501eaa00cdb23407e30d3b8f911fe972372bc2 [file] [log] [blame]
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +00001//===- llvm/CodeGen/DwarfCompileUnit.cpp - Dwarf Compile Units ------------===//
Paul Robinson34435752017-03-01 00:14:42 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Paul Robinson34435752017-03-01 00:14:42 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains support for constructing a dwarf compile unit.
10//
11//===----------------------------------------------------------------------===//
12
David Blaikie37c52312014-10-04 15:49:50 +000013#include "DwarfCompileUnit.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000014#include "AddressPool.h"
15#include "DwarfDebug.h"
Adrian Prantl7813d9c2015-01-14 01:01:30 +000016#include "DwarfExpression.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000017#include "DwarfUnit.h"
18#include "llvm/ADT/None.h"
19#include "llvm/ADT/STLExtras.h"
Markus Lavinb86ce212019-03-19 13:16:28 +000020#include "llvm/ADT/SmallString.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000021#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/BinaryFormat/Dwarf.h"
24#include "llvm/CodeGen/AsmPrinter.h"
25#include "llvm/CodeGen/DIE.h"
26#include "llvm/CodeGen/LexicalScopes.h"
David Blaikiecda2aa82014-10-04 16:24:00 +000027#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000028#include "llvm/CodeGen/MachineInstr.h"
29#include "llvm/CodeGen/MachineOperand.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000030#include "llvm/CodeGen/TargetFrameLowering.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000031#include "llvm/CodeGen/TargetRegisterInfo.h"
32#include "llvm/CodeGen/TargetSubtargetInfo.h"
David Blaikie37c52312014-10-04 15:49:50 +000033#include "llvm/IR/DataLayout.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000034#include "llvm/IR/DebugInfo.h"
35#include "llvm/IR/DebugInfoMetadata.h"
David Blaikie37c52312014-10-04 15:49:50 +000036#include "llvm/IR/GlobalVariable.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000037#include "llvm/MC/MCSection.h"
David Blaikie37c52312014-10-04 15:49:50 +000038#include "llvm/MC/MCStreamer.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000039#include "llvm/MC/MCSymbol.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000040#include "llvm/MC/MachineLocation.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000041#include "llvm/Support/Casting.h"
David Blaikie6054e652018-03-23 23:58:19 +000042#include "llvm/Target/TargetLoweringObjectFile.h"
David Blaikiecda2aa82014-10-04 16:24:00 +000043#include "llvm/Target/TargetMachine.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000044#include "llvm/Target/TargetOptions.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000045#include <algorithm>
46#include <cassert>
47#include <cstdint>
48#include <iterator>
49#include <memory>
50#include <string>
51#include <utility>
David Blaikie37c52312014-10-04 15:49:50 +000052
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000053using namespace llvm;
David Blaikie37c52312014-10-04 15:49:50 +000054
Alexey Lapshin789e2572019-11-30 23:48:32 +030055static dwarf::Tag GetCompileUnitType(UnitKind Kind, DwarfDebug *DW) {
56
57 // According to DWARF Debugging Information Format Version 5,
58 // 3.1.2 Skeleton Compilation Unit Entries:
59 // "When generating a split DWARF object file (see Section 7.3.2
60 // on page 187), the compilation unit in the .debug_info section
61 // is a "skeleton" compilation unit with the tag DW_TAG_skeleton_unit"
62 if (DW->getDwarfVersion() >= 5 && Kind == UnitKind::Skeleton)
63 return dwarf::DW_TAG_skeleton_unit;
64
65 return dwarf::DW_TAG_compile_unit;
66}
67
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000068DwarfCompileUnit::DwarfCompileUnit(unsigned UID, const DICompileUnit *Node,
David Blaikie37c52312014-10-04 15:49:50 +000069 AsmPrinter *A, DwarfDebug *DW,
Alexey Lapshin789e2572019-11-30 23:48:32 +030070 DwarfFile *DWU, UnitKind Kind)
71 : DwarfUnit(GetCompileUnitType(Kind, DW), Node, A, DW, DWU), UniqueID(UID) {
David Blaikie37c52312014-10-04 15:49:50 +000072 insertDIE(Node, &getUnitDie());
Amjad Aboud8bbce8a2016-02-01 14:09:41 +000073 MacroLabelBegin = Asm->createTempSymbol("cu_macro_begin");
David Blaikie37c52312014-10-04 15:49:50 +000074}
75
76/// addLabelAddress - Add a dwarf label attribute data and value using
77/// DW_FORM_addr or DW_FORM_GNU_addr_index.
David Blaikie37c52312014-10-04 15:49:50 +000078void DwarfCompileUnit::addLabelAddress(DIE &Die, dwarf::Attribute Attribute,
79 const MCSymbol *Label) {
David Blaikie37c52312014-10-04 15:49:50 +000080 // Don't use the address pool in non-fission or in the skeleton unit itself.
David Blaikie161dd3c2018-10-20 06:02:15 +000081 if ((!DD->useSplitDwarf() || !Skeleton) && DD->getDwarfVersion() < 5)
David Blaikie37c52312014-10-04 15:49:50 +000082 return addLocalLabelAddress(Die, Attribute, Label);
83
84 if (Label)
85 DD->addArangeLabel(SymbolCU(this, Label));
86
87 unsigned idx = DD->getAddressPool().getIndex(Label);
David Blaikie161dd3c2018-10-20 06:02:15 +000088 Die.addValue(DIEValueAllocator, Attribute,
89 DD->getDwarfVersion() >= 5 ? dwarf::DW_FORM_addrx
90 : dwarf::DW_FORM_GNU_addr_index,
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +000091 DIEInteger(idx));
David Blaikie37c52312014-10-04 15:49:50 +000092}
93
94void DwarfCompileUnit::addLocalLabelAddress(DIE &Die,
95 dwarf::Attribute Attribute,
96 const MCSymbol *Label) {
97 if (Label)
98 DD->addArangeLabel(SymbolCU(this, Label));
99
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +0000100 if (Label)
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000101 Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_addr,
102 DIELabel(Label));
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +0000103 else
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000104 Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_addr,
105 DIEInteger(0));
David Blaikie37c52312014-10-04 15:49:50 +0000106}
107
Paul Robinson612e89d2018-01-12 19:17:50 +0000108unsigned DwarfCompileUnit::getOrCreateSourceID(const DIFile *File) {
David Blaikie37c52312014-10-04 15:49:50 +0000109 // If we print assembly, we can't separate .file entries according to
110 // compile units. Thus all files will belong to the default compile unit.
111
112 // FIXME: add a better feature test than hasRawTextSupport. Even better,
113 // extend .file to support this.
Paul Robinson612e89d2018-01-12 19:17:50 +0000114 unsigned CUID = Asm->OutStreamer->hasRawTextSupport() ? 0 : getUniqueID();
115 if (!File)
Eric Christopher798e83b2019-04-04 23:34:38 +0000116 return Asm->OutStreamer->EmitDwarfFileDirective(0, "", "", None, None, CUID);
Lang Hames9ff69c82015-04-24 19:11:51 +0000117 return Asm->OutStreamer->EmitDwarfFileDirective(
Scott Linder16c7bda2018-02-23 23:01:06 +0000118 0, File->getDirectory(), File->getFilename(), getMD5AsBytes(File),
119 File->getSource(), CUID);
David Blaikie37c52312014-10-04 15:49:50 +0000120}
121
Duncan P. N. Exon Smith60635e32015-04-21 18:44:06 +0000122DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE(
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000123 const DIGlobalVariable *GV, ArrayRef<GlobalExpr> GlobalExprs) {
David Blaikie37c52312014-10-04 15:49:50 +0000124 // Check for pre-existence.
125 if (DIE *Die = getDIE(GV))
126 return Die;
127
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000128 assert(GV);
David Blaikie37c52312014-10-04 15:49:50 +0000129
Duncan P. N. Exon Smithbe9e4fe2015-04-20 18:32:29 +0000130 auto *GVContext = GV->getScope();
Fangrui Songda82ce92019-05-07 02:06:37 +0000131 const DIType *GTy = GV->getType();
David Blaikie37c52312014-10-04 15:49:50 +0000132
David Blaikie49cfc8c2014-10-23 19:12:43 +0000133 // Construct the context before querying for the existence of the DIE in
134 // case such construction creates the DIE.
Adrian Prantl6ed57062019-04-08 19:13:55 +0000135 auto *CB = GVContext ? dyn_cast<DICommonBlock>(GVContext) : nullptr;
136 DIE *ContextDIE = CB ? getOrCreateCommonBlock(CB, GlobalExprs)
137 : getOrCreateContextDIE(GVContext);
David Blaikie49cfc8c2014-10-23 19:12:43 +0000138
Amjad Aboud72da9392016-04-30 01:44:07 +0000139 // Add to map.
140 DIE *VariableDIE = &createAndAddDIE(GV->getTag(), *ContextDIE, GV);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000141 DIScope *DeclContext;
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000142 if (auto *SDMDecl = GV->getStaticDataMemberDeclaration()) {
Fangrui Songda82ce92019-05-07 02:06:37 +0000143 DeclContext = SDMDecl->getScope();
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000144 assert(SDMDecl->isStaticMember() && "Expected static member decl");
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000145 assert(GV->isDefinition());
David Blaikie37c52312014-10-04 15:49:50 +0000146 // We need the declaration DIE that is in the static member's class.
David Blaikie49cfc8c2014-10-23 19:12:43 +0000147 DIE *VariableSpecDIE = getOrCreateStaticMemberDIE(SDMDecl);
148 addDIEEntry(*VariableDIE, dwarf::DW_AT_specification, *VariableSpecDIE);
Adrian Prantl3502f202016-11-08 22:11:38 +0000149 // If the global variable's type is different from the one in the class
150 // member type, assume that it's more specific and also emit it.
Fangrui Songda82ce92019-05-07 02:06:37 +0000151 if (GTy != SDMDecl->getBaseType())
Adrian Prantl3502f202016-11-08 22:11:38 +0000152 addType(*VariableDIE, GTy);
David Blaikie49cfc8c2014-10-23 19:12:43 +0000153 } else {
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000154 DeclContext = GV->getScope();
David Blaikie37c52312014-10-04 15:49:50 +0000155 // Add name and type.
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000156 addString(*VariableDIE, dwarf::DW_AT_name, GV->getDisplayName());
David Blaikie37c52312014-10-04 15:49:50 +0000157 addType(*VariableDIE, GTy);
158
159 // Add scoping info.
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000160 if (!GV->isLocalToUnit())
David Blaikie37c52312014-10-04 15:49:50 +0000161 addFlag(*VariableDIE, dwarf::DW_AT_external);
162
163 // Add line number info.
164 addSourceLine(*VariableDIE, GV);
165 }
166
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000167 if (!GV->isDefinition())
David Blaikie49cfc8c2014-10-23 19:12:43 +0000168 addFlag(*VariableDIE, dwarf::DW_AT_declaration);
David Blaikie877354a2015-04-14 18:08:25 +0000169 else
170 addGlobalName(GV->getName(), *VariableDIE, DeclContext);
David Blaikie49cfc8c2014-10-23 19:12:43 +0000171
Victor Leschuk3c989982016-10-26 11:59:03 +0000172 if (uint32_t AlignInBytes = GV->getAlignInBytes())
173 addUInt(*VariableDIE, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
174 AlignInBytes);
175
Matthew Vossf8ab35a2018-10-03 18:44:53 +0000176 if (MDTuple *TP = GV->getTemplateParams())
177 addTemplateParams(*VariableDIE, DINodeArray(TP));
178
David Blaikie37c52312014-10-04 15:49:50 +0000179 // Add location.
Adrian Prantl6ed57062019-04-08 19:13:55 +0000180 addLocationAttribute(VariableDIE, GV, GlobalExprs);
181
182 return VariableDIE;
183}
184
185void DwarfCompileUnit::addLocationAttribute(
186 DIE *VariableDIE, const DIGlobalVariable *GV, ArrayRef<GlobalExpr> GlobalExprs) {
David Blaikie37c52312014-10-04 15:49:50 +0000187 bool addToAccelTable = false;
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000188 DIELoc *Loc = nullptr;
Alexey Bataevf3a91502019-02-05 19:33:47 +0000189 Optional<unsigned> NVPTXAddressSpace;
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000190 std::unique_ptr<DIEDwarfExpression> DwarfExpr;
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000191 for (const auto &GE : GlobalExprs) {
192 const GlobalVariable *Global = GE.Var;
193 const DIExpression *Expr = GE.Expr;
Adrian Prantlb216e3e2017-03-23 23:35:09 +0000194
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000195 // For compatibility with DWARF 3 and earlier,
196 // DW_AT_location(DW_OP_constu, X, DW_OP_stack_value) becomes
197 // DW_AT_const_value(X).
198 if (GlobalExprs.size() == 1 && Expr && Expr->isConstant()) {
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000199 addToAccelTable = true;
Adrian Prantlb216e3e2017-03-23 23:35:09 +0000200 addConstantValue(*VariableDIE, /*Unsigned=*/true, Expr->getElement(1));
201 break;
202 }
203
204 // We cannot describe the location of dllimport'd variables: the
205 // computation of their address requires loads from the IAT.
206 if (Global && Global->hasDLLImportStorageClass())
207 continue;
208
209 // Nothing to describe without address or constant.
210 if (!Global && (!Expr || !Expr->isConstant()))
211 continue;
212
Lei Liu8e422b82018-08-01 23:46:49 +0000213 if (Global && Global->isThreadLocal() &&
214 !Asm->getObjFileLowering().supportDebugThreadLocalLocation())
215 continue;
216
Adrian Prantlb216e3e2017-03-23 23:35:09 +0000217 if (!Loc) {
218 addToAccelTable = true;
219 Loc = new (DIEValueAllocator) DIELoc;
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000220 DwarfExpr = std::make_unique<DIEDwarfExpression>(*Asm, *this, *Loc);
Adrian Prantlb216e3e2017-03-23 23:35:09 +0000221 }
222
Alexey Bataevf3a91502019-02-05 19:33:47 +0000223 if (Expr) {
224 // According to
225 // https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf
226 // cuda-gdb requires DW_AT_address_class for all variables to be able to
227 // correctly interpret address space of the variable address.
228 // Decode DW_OP_constu <DWARF Address Space> DW_OP_swap DW_OP_xderef
229 // sequence for the NVPTX + gdb target.
230 unsigned LocalNVPTXAddressSpace;
231 if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) {
232 const DIExpression *NewExpr =
233 DIExpression::extractAddressClass(Expr, LocalNVPTXAddressSpace);
234 if (NewExpr != Expr) {
235 Expr = NewExpr;
236 NVPTXAddressSpace = LocalNVPTXAddressSpace;
237 }
238 }
Mikael Holmena9e31532018-01-29 12:37:30 +0000239 DwarfExpr->addFragmentOffset(Expr);
Alexey Bataevf3a91502019-02-05 19:33:47 +0000240 }
Mikael Holmena9e31532018-01-29 12:37:30 +0000241
Adrian Prantlb216e3e2017-03-23 23:35:09 +0000242 if (Global) {
243 const MCSymbol *Sym = Asm->getSymbol(Global);
244 if (Global->isThreadLocal()) {
Chih-Hung Hsieh9f9e4682018-02-28 17:48:55 +0000245 if (Asm->TM.useEmulatedTLS()) {
Adrian Prantlb216e3e2017-03-23 23:35:09 +0000246 // TODO: add debug info for emulated thread local mode.
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000247 } else {
Adrian Prantlb216e3e2017-03-23 23:35:09 +0000248 // FIXME: Make this work with -gsplit-dwarf.
249 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
250 assert((PointerSize == 4 || PointerSize == 8) &&
251 "Add support for other sizes if necessary");
252 // Based on GCC's support for TLS:
253 if (!DD->useSplitDwarf()) {
254 // 1) Start with a constNu of the appropriate pointer size
255 addUInt(*Loc, dwarf::DW_FORM_data1,
256 PointerSize == 4 ? dwarf::DW_OP_const4u
257 : dwarf::DW_OP_const8u);
258 // 2) containing the (relocated) offset of the TLS variable
259 // within the module's TLS block.
260 addExpr(*Loc, dwarf::DW_FORM_udata,
261 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym));
262 } else {
263 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
264 addUInt(*Loc, dwarf::DW_FORM_udata,
265 DD->getAddressPool().getIndex(Sym, /* TLS */ true));
266 }
267 // 3) followed by an OP to make the debugger do a TLS lookup.
268 addUInt(*Loc, dwarf::DW_FORM_data1,
269 DD->useGNUTLSOpcode() ? dwarf::DW_OP_GNU_push_tls_address
270 : dwarf::DW_OP_form_tls_address);
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000271 }
Adrian Prantlb216e3e2017-03-23 23:35:09 +0000272 } else {
273 DD->addArangeLabel(SymbolCU(this, Sym));
274 addOpAddress(*Loc, Sym);
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000275 }
Adrian Prantlb216e3e2017-03-23 23:35:09 +0000276 }
Adrian Prantl4b542c62018-04-27 22:05:31 +0000277 // Global variables attached to symbols are memory locations.
278 // It would be better if this were unconditional, but malformed input that
279 // mixes non-fragments and fragments for the same variable is too expensive
280 // to detect in the verifier.
Adrian Prantl3edc63a2018-05-04 16:10:43 +0000281 if (DwarfExpr->isUnknownLocation())
Adrian Prantl4b542c62018-04-27 22:05:31 +0000282 DwarfExpr->setMemoryLocationKind();
283 DwarfExpr->addExpression(Expr);
Adrian Prantl73ec0652016-12-16 19:39:01 +0000284 }
Alexey Bataevf3a91502019-02-05 19:33:47 +0000285 if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) {
286 // According to
287 // https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf
288 // cuda-gdb requires DW_AT_address_class for all variables to be able to
289 // correctly interpret address space of the variable address.
290 const unsigned NVPTX_ADDR_global_space = 5;
291 addUInt(*VariableDIE, dwarf::DW_AT_address_class, dwarf::DW_FORM_data1,
292 NVPTXAddressSpace ? *NVPTXAddressSpace : NVPTX_ADDR_global_space);
293 }
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000294 if (Loc)
295 addBlock(*VariableDIE, dwarf::DW_AT_location, DwarfExpr->finalize());
296
297 if (DD->useAllLinkageNames())
298 addLinkageName(*VariableDIE, GV->getLinkageName());
David Blaikie37c52312014-10-04 15:49:50 +0000299
David Blaikie37c52312014-10-04 15:49:50 +0000300 if (addToAccelTable) {
David Blaikie66cf14d2018-08-16 21:29:55 +0000301 DD->addAccelName(*CUNode, GV->getName(), *VariableDIE);
David Blaikie37c52312014-10-04 15:49:50 +0000302
303 // If the linkage name is different than the name, go ahead and output
304 // that as well into the name table.
Pavel Labath2a6afe52018-05-14 14:13:20 +0000305 if (GV->getLinkageName() != "" && GV->getName() != GV->getLinkageName() &&
306 DD->useAllLinkageNames())
David Blaikie66cf14d2018-08-16 21:29:55 +0000307 DD->addAccelName(*CUNode, GV->getLinkageName(), *VariableDIE);
David Blaikie37c52312014-10-04 15:49:50 +0000308 }
Adrian Prantl6ed57062019-04-08 19:13:55 +0000309}
David Blaikie37c52312014-10-04 15:49:50 +0000310
Adrian Prantl6ed57062019-04-08 19:13:55 +0000311DIE *DwarfCompileUnit::getOrCreateCommonBlock(
312 const DICommonBlock *CB, ArrayRef<GlobalExpr> GlobalExprs) {
313 // Construct the context before querying for the existence of the DIE in case
314 // such construction creates the DIE.
315 DIE *ContextDIE = getOrCreateContextDIE(CB->getScope());
316
317 if (DIE *NDie = getDIE(CB))
318 return NDie;
319 DIE &NDie = createAndAddDIE(dwarf::DW_TAG_common_block, *ContextDIE, CB);
320 StringRef Name = CB->getName().empty() ? "_BLNK_" : CB->getName();
321 addString(NDie, dwarf::DW_AT_name, Name);
322 addGlobalName(Name, NDie, CB->getScope());
323 if (CB->getFile())
324 addSourceLine(NDie, CB->getLineNo(), CB->getFile());
325 if (DIGlobalVariable *V = CB->getDecl())
326 getCU().addLocationAttribute(&NDie, V, GlobalExprs);
327 return &NDie;
David Blaikie37c52312014-10-04 15:49:50 +0000328}
329
330void DwarfCompileUnit::addRange(RangeSpan Range) {
David Blaikie58b10df2020-01-17 18:12:34 -0800331 DD->insertSectionLabel(Range.Begin);
332
David Blaikie37c52312014-10-04 15:49:50 +0000333 bool SameAsPrevCU = this == DD->getPrevCU();
334 DD->setPrevCU(this);
335 // If we have no current ranges just add the range and return, otherwise,
336 // check the current section and CU against the previous section and CU we
337 // emitted into and the subprogram was contained within. If these are the
338 // same then extend our current range, otherwise add this as a new range.
339 if (CURanges.empty() || !SameAsPrevCU ||
David Blaikieb677cb8d2019-10-02 22:27:24 +0000340 (&CURanges.back().End->getSection() !=
341 &Range.End->getSection())) {
David Blaikie37c52312014-10-04 15:49:50 +0000342 CURanges.push_back(Range);
343 return;
344 }
345
David Blaikieb677cb8d2019-10-02 22:27:24 +0000346 CURanges.back().End = Range.End;
David Blaikie37c52312014-10-04 15:49:50 +0000347}
348
Rafael Espindola063d7252015-03-10 16:58:10 +0000349void DwarfCompileUnit::initStmtList() {
Alexey Bataevd4dd7212018-08-01 19:38:20 +0000350 if (CUNode->isDebugDirectivesOnly())
351 return;
352
David Blaikie37c52312014-10-04 15:49:50 +0000353 // Define start line table label for each Compile Unit.
Alexey Bataevbff36082018-03-23 13:35:54 +0000354 MCSymbol *LineTableStartSym;
355 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
356 if (DD->useSectionsAsReferences()) {
357 LineTableStartSym = TLOF.getDwarfLineSection()->getBeginSymbol();
358 } else {
359 LineTableStartSym =
360 Asm->OutStreamer->getDwarfLineTableSymbol(getUniqueID());
361 }
David Blaikie37c52312014-10-04 15:49:50 +0000362
David Blaikie37c52312014-10-04 15:49:50 +0000363 // DW_AT_stmt_list is a offset of line number information for this
364 // compile unit in debug_line section. For split dwarf this is
365 // left in the skeleton CU and so not included.
366 // The line table entries are not always emitted in assembly, so it
367 // is not okay to use line_table_start here.
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000368 StmtListValue =
Greg Clayton35630c32016-12-01 18:56:29 +0000369 addSectionLabel(getUnitDie(), dwarf::DW_AT_stmt_list, LineTableStartSym,
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000370 TLOF.getDwarfLineSection()->getBeginSymbol());
David Blaikie37c52312014-10-04 15:49:50 +0000371}
372
373void DwarfCompileUnit::applyStmtList(DIE &D) {
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000374 D.addValue(DIEValueAllocator, *StmtListValue);
David Blaikie37c52312014-10-04 15:49:50 +0000375}
376
David Blaikie14499a72014-10-04 15:58:47 +0000377void DwarfCompileUnit::attachLowHighPC(DIE &D, const MCSymbol *Begin,
378 const MCSymbol *End) {
379 assert(Begin && "Begin label should not be null!");
380 assert(End && "End label should not be null!");
381 assert(Begin->isDefined() && "Invalid starting label");
382 assert(End->isDefined() && "Invalid end label");
383
384 addLabelAddress(D, dwarf::DW_AT_low_pc, Begin);
385 if (DD->getDwarfVersion() < 4)
386 addLabelAddress(D, dwarf::DW_AT_high_pc, End);
387 else
388 addLabelDelta(D, dwarf::DW_AT_high_pc, End, Begin);
389}
390
David Blaikiecda2aa82014-10-04 16:24:00 +0000391// Find DIE for the given subprogram and attach appropriate DW_AT_low_pc
392// and DW_AT_high_pc attributes. If there are global variables in this
393// scope then create and insert DIEs for these variables.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000394DIE &DwarfCompileUnit::updateSubprogramScopeDIE(const DISubprogram *SP) {
David Blaikie3a443c22014-11-04 22:12:25 +0000395 DIE *SPDie = getOrCreateSubprogramDIE(SP, includeMinimalInlineScopes());
David Blaikiecda2aa82014-10-04 16:24:00 +0000396
Rafael Espindola07c03d32015-03-05 02:05:42 +0000397 attachLowHighPC(*SPDie, Asm->getFunctionBegin(), Asm->getFunctionEnd());
David Blaikiec53e18d2016-05-24 21:19:28 +0000398 if (DD->useAppleExtensionAttributes() &&
399 !DD->getCurrentFunction()->getTarget().Options.DisableFramePointerElim(
David Blaikiecda2aa82014-10-04 16:24:00 +0000400 *DD->getCurrentFunction()))
401 addFlag(*SPDie, dwarf::DW_AT_APPLE_omit_frame_ptr);
402
403 // Only include DW_AT_frame_base in full debug info
David Blaikie3a443c22014-11-04 22:12:25 +0000404 if (!includeMinimalInlineScopes()) {
Derek Schuffff171ac2019-12-18 14:50:19 -0800405 const TargetFrameLowering *TFI = Asm->MF->getSubtarget().getFrameLowering();
406 TargetFrameLowering::DwarfFrameBase FrameBase =
407 TFI->getDwarfFrameBase(*Asm->MF);
408 switch (FrameBase.Kind) {
409 case TargetFrameLowering::DwarfFrameBase::Register: {
410 if (Register::isPhysicalRegister(FrameBase.Location.Reg)) {
411 MachineLocation Location(FrameBase.Location.Reg);
412 addAddress(*SPDie, dwarf::DW_AT_frame_base, Location);
413 }
414 break;
415 }
416 case TargetFrameLowering::DwarfFrameBase::CFA: {
Alexey Bataev7ae86fe2018-07-26 16:10:05 +0000417 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
418 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_call_frame_cfa);
419 addBlock(*SPDie, dwarf::DW_AT_frame_base, Loc);
Derek Schuffff171ac2019-12-18 14:50:19 -0800420 break;
421 }
422 case TargetFrameLowering::DwarfFrameBase::WasmFrameBase: {
423 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
424 DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
425 DIExpressionCursor Cursor({});
426 DwarfExpr.addWasmLocation(FrameBase.Location.WasmLoc.Kind,
427 FrameBase.Location.WasmLoc.Index);
428 DwarfExpr.addExpression(std::move(Cursor));
429 addBlock(*SPDie, dwarf::DW_AT_frame_base, DwarfExpr.finalize());
430 break;
431 }
Alexey Bataev7ae86fe2018-07-26 16:10:05 +0000432 }
David Blaikiecda2aa82014-10-04 16:24:00 +0000433 }
434
435 // Add name to the name table, we do this here because we're guaranteed
436 // to have concrete versions of our DW_TAG_subprogram nodes.
David Blaikie66cf14d2018-08-16 21:29:55 +0000437 DD->addSubprogramNames(*CUNode, SP, *SPDie);
David Blaikiecda2aa82014-10-04 16:24:00 +0000438
439 return *SPDie;
440}
441
David Blaikie9c65b132014-10-08 22:20:02 +0000442// Construct a DIE for this scope.
443void DwarfCompileUnit::constructScopeDIE(
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000444 LexicalScope *Scope, SmallVectorImpl<DIE *> &FinalChildren) {
David Blaikie9c65b132014-10-08 22:20:02 +0000445 if (!Scope || !Scope->getScopeNode())
446 return;
447
Duncan P. N. Exon Smithbe9e4fe2015-04-20 18:32:29 +0000448 auto *DS = Scope->getScopeNode();
David Blaikie9c65b132014-10-08 22:20:02 +0000449
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000450 assert((Scope->getInlinedAt() || !isa<DISubprogram>(DS)) &&
David Blaikie9c65b132014-10-08 22:20:02 +0000451 "Only handle inlined subprograms here, use "
452 "constructSubprogramScopeDIE for non-inlined "
453 "subprograms");
454
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000455 SmallVector<DIE *, 8> Children;
David Blaikie9c65b132014-10-08 22:20:02 +0000456
457 // We try to create the scope DIE first, then the children DIEs. This will
458 // avoid creating un-used children then removing them later when we find out
459 // the scope DIE is null.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000460 DIE *ScopeDIE;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000461 if (Scope->getParent() && isa<DISubprogram>(DS)) {
David Blaikie01b48a82014-10-09 16:50:53 +0000462 ScopeDIE = constructInlinedScopeDIE(Scope);
David Blaikie9c65b132014-10-08 22:20:02 +0000463 if (!ScopeDIE)
464 return;
465 // We create children when the scope DIE is not null.
David Blaikie8b2fdb82014-10-09 18:24:28 +0000466 createScopeChildrenDIE(Scope, Children);
David Blaikie9c65b132014-10-08 22:20:02 +0000467 } else {
468 // Early exit when we know the scope DIE is going to be null.
469 if (DD->isLexicalScopeDIENull(Scope))
470 return;
471
David Blaikie2195e132017-07-27 00:06:53 +0000472 bool HasNonScopeChildren = false;
David Blaikie9c65b132014-10-08 22:20:02 +0000473
474 // We create children here when we know the scope DIE is not going to be
475 // null and the children will be added to the scope DIE.
David Blaikie2195e132017-07-27 00:06:53 +0000476 createScopeChildrenDIE(Scope, Children, &HasNonScopeChildren);
David Blaikie3a443c22014-11-04 22:12:25 +0000477
David Blaikie9c65b132014-10-08 22:20:02 +0000478 // If there are only other scopes as children, put them directly in the
479 // parent instead, as this scope would serve no purpose.
David Blaikie2195e132017-07-27 00:06:53 +0000480 if (!HasNonScopeChildren) {
David Blaikie9c65b132014-10-08 22:20:02 +0000481 FinalChildren.insert(FinalChildren.end(),
482 std::make_move_iterator(Children.begin()),
483 std::make_move_iterator(Children.end()));
484 return;
485 }
David Blaikie0fbf8bd2014-10-09 17:08:42 +0000486 ScopeDIE = constructLexicalScopeDIE(Scope);
David Blaikie9c65b132014-10-08 22:20:02 +0000487 assert(ScopeDIE && "Scope DIE should not be null.");
488 }
489
490 // Add children
491 for (auto &I : Children)
492 ScopeDIE->addChild(std::move(I));
493
494 FinalChildren.push_back(std::move(ScopeDIE));
495}
496
David Blaikie5b02a192014-11-03 23:10:59 +0000497void DwarfCompileUnit::addScopeRangeList(DIE &ScopeDIE,
498 SmallVector<RangeSpan, 2> Range) {
David Blaikie52400202014-10-09 00:11:39 +0000499
David Blaikiec4af8bf2018-10-20 07:36:39 +0000500 HasRangeLists = true;
501
502 // Add the range list to the set of ranges to be emitted.
503 auto IndexAndList =
504 (DD->getDwarfVersion() < 5 && Skeleton ? Skeleton->DU : DU)
David Blaikiec8f7e6c2018-11-08 00:35:54 +0000505 ->addRange(*(Skeleton ? Skeleton : this), std::move(Range));
David Blaikiec4af8bf2018-10-20 07:36:39 +0000506
507 uint32_t Index = IndexAndList.first;
508 auto &List = *IndexAndList.second;
David Blaikie5b02a192014-11-03 23:10:59 +0000509
David Blaikie52400202014-10-09 00:11:39 +0000510 // Under fission, ranges are specified by constant offsets relative to the
511 // CU's DW_AT_GNU_ranges_base.
Wolfgang Piebfcf38102018-07-12 18:18:21 +0000512 // FIXME: For DWARF v5, do not generate the DW_AT_ranges attribute under
513 // fission until we support the forms using the .debug_addr section
514 // (DW_RLE_startx_endx etc.).
David Blaikie32e09de2018-10-20 08:12:36 +0000515 if (DD->getDwarfVersion() >= 5)
516 addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_rnglistx, Index);
David Blaikiede9aa372019-10-11 23:51:24 +0000517 else {
518 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
519 const MCSymbol *RangeSectionSym =
520 TLOF.getDwarfRangesSection()->getBeginSymbol();
521 if (isDwoUnit())
David Blaikie64fa76e2019-12-16 15:19:25 -0800522 addSectionDelta(ScopeDIE, dwarf::DW_AT_ranges, List.Label,
David Blaikiede9aa372019-10-11 23:51:24 +0000523 RangeSectionSym);
524 else
David Blaikie64fa76e2019-12-16 15:19:25 -0800525 addSectionLabel(ScopeDIE, dwarf::DW_AT_ranges, List.Label,
David Blaikiede9aa372019-10-11 23:51:24 +0000526 RangeSectionSym);
527 }
David Blaikie52400202014-10-09 00:11:39 +0000528}
529
David Blaikiede123752014-10-09 00:21:42 +0000530void DwarfCompileUnit::attachRangesOrLowHighPC(
David Blaikie5b02a192014-11-03 23:10:59 +0000531 DIE &Die, SmallVector<RangeSpan, 2> Ranges) {
Alexey Bataev858a7dd2018-03-20 20:21:38 +0000532 if (Ranges.size() == 1 || !DD->useRangesSection()) {
533 const RangeSpan &Front = Ranges.front();
534 const RangeSpan &Back = Ranges.back();
David Blaikieb677cb8d2019-10-02 22:27:24 +0000535 attachLowHighPC(Die, Front.Begin, Back.End);
David Blaikie5b02a192014-11-03 23:10:59 +0000536 } else
537 addScopeRangeList(Die, std::move(Ranges));
538}
539
540void DwarfCompileUnit::attachRangesOrLowHighPC(
David Blaikiede123752014-10-09 00:21:42 +0000541 DIE &Die, const SmallVectorImpl<InsnRange> &Ranges) {
David Blaikie5b02a192014-11-03 23:10:59 +0000542 SmallVector<RangeSpan, 2> List;
543 List.reserve(Ranges.size());
544 for (const InsnRange &R : Ranges)
David Blaikieb677cb8d2019-10-02 22:27:24 +0000545 List.push_back(
546 {DD->getLabelBeforeInsn(R.first), DD->getLabelAfterInsn(R.second)});
David Blaikie5b02a192014-11-03 23:10:59 +0000547 attachRangesOrLowHighPC(Die, std::move(List));
David Blaikiede123752014-10-09 00:21:42 +0000548}
549
David Blaikie01b48a82014-10-09 16:50:53 +0000550// This scope represents inlined body of a function. Construct DIE to
551// represent this concrete inlined copy of the function.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000552DIE *DwarfCompileUnit::constructInlinedScopeDIE(LexicalScope *Scope) {
David Blaikie01b48a82014-10-09 16:50:53 +0000553 assert(Scope->getScopeNode());
Duncan P. N. Exon Smithbe9e4fe2015-04-20 18:32:29 +0000554 auto *DS = Scope->getScopeNode();
555 auto *InlinedSP = getDISubprogram(DS);
David Blaikie01b48a82014-10-09 16:50:53 +0000556 // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram
557 // was inlined from another compile unit.
David Blaikie488393f2017-05-12 01:13:45 +0000558 DIE *OriginDIE = getAbstractSPDies()[InlinedSP];
David Blaikie01b48a82014-10-09 16:50:53 +0000559 assert(OriginDIE && "Unable to find original DIE for an inlined subprogram.");
560
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000561 auto ScopeDIE = DIE::get(DIEValueAllocator, dwarf::DW_TAG_inlined_subroutine);
David Blaikie01b48a82014-10-09 16:50:53 +0000562 addDIEEntry(*ScopeDIE, dwarf::DW_AT_abstract_origin, *OriginDIE);
563
564 attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges());
565
566 // Add the call site information to the DIE.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000567 const DILocation *IA = Scope->getInlinedAt();
David Blaikie01b48a82014-10-09 16:50:53 +0000568 addUInt(*ScopeDIE, dwarf::DW_AT_call_file, None,
Paul Robinson612e89d2018-01-12 19:17:50 +0000569 getOrCreateSourceID(IA->getFile()));
Duncan P. N. Exon Smithb7e221b2015-04-14 01:35:55 +0000570 addUInt(*ScopeDIE, dwarf::DW_AT_call_line, None, IA->getLine());
David Blaikie40580d32019-07-16 21:15:19 +0000571 if (IA->getColumn())
572 addUInt(*ScopeDIE, dwarf::DW_AT_call_column, None, IA->getColumn());
Dehao Chen6e0c8442016-10-07 15:21:31 +0000573 if (IA->getDiscriminator() && DD->getDwarfVersion() >= 4)
Dehao Chen54511352015-11-11 18:09:47 +0000574 addUInt(*ScopeDIE, dwarf::DW_AT_GNU_discriminator, None,
575 IA->getDiscriminator());
David Blaikie01b48a82014-10-09 16:50:53 +0000576
577 // Add name to the name table, we do this here because we're guaranteed
578 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
David Blaikie66cf14d2018-08-16 21:29:55 +0000579 DD->addSubprogramNames(*CUNode, InlinedSP, *ScopeDIE);
David Blaikie01b48a82014-10-09 16:50:53 +0000580
581 return ScopeDIE;
582}
583
David Blaikie0fbf8bd2014-10-09 17:08:42 +0000584// Construct new DW_TAG_lexical_block for this scope and attach
585// DW_AT_low_pc/DW_AT_high_pc labels.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000586DIE *DwarfCompileUnit::constructLexicalScopeDIE(LexicalScope *Scope) {
David Blaikie0fbf8bd2014-10-09 17:08:42 +0000587 if (DD->isLexicalScopeDIENull(Scope))
588 return nullptr;
589
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000590 auto ScopeDIE = DIE::get(DIEValueAllocator, dwarf::DW_TAG_lexical_block);
David Blaikie0fbf8bd2014-10-09 17:08:42 +0000591 if (Scope->isAbstractScope())
592 return ScopeDIE;
593
594 attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges());
595
596 return ScopeDIE;
597}
598
David Blaikieee7df552014-10-09 17:56:36 +0000599/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000600DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV, bool Abstract) {
David Blaikieee7df552014-10-09 17:56:36 +0000601 auto D = constructVariableDIEImpl(DV, Abstract);
602 DV.setDIE(*D);
603 return D;
604}
605
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000606DIE *DwarfCompileUnit::constructLabelDIE(DbgLabel &DL,
607 const LexicalScope &Scope) {
608 auto LabelDie = DIE::get(DIEValueAllocator, DL.getTag());
609 insertDIE(DL.getLabel(), LabelDie);
610 DL.setDIE(*LabelDie);
611
612 if (Scope.isAbstractScope())
613 applyLabelAttributes(DL, *LabelDie);
614
615 return LabelDie;
616}
617
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000618DIE *DwarfCompileUnit::constructVariableDIEImpl(const DbgVariable &DV,
619 bool Abstract) {
David Blaikieee7df552014-10-09 17:56:36 +0000620 // Define variable debug information entry.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000621 auto VariableDie = DIE::get(DIEValueAllocator, DV.getTag());
Sander de Smalendc00bec2018-01-24 13:35:54 +0000622 insertDIE(DV.getVariable(), VariableDie);
David Blaikieee7df552014-10-09 17:56:36 +0000623
624 if (Abstract) {
625 applyVariableAttributes(DV, *VariableDie);
626 return VariableDie;
627 }
628
629 // Add variable address.
630
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +0000631 unsigned Offset = DV.getDebugLocListIndex();
David Blaikieee7df552014-10-09 17:56:36 +0000632 if (Offset != ~0U) {
633 addLocationList(*VariableDie, dwarf::DW_AT_location, Offset);
Evgenii Stepanovdabd2622019-11-25 17:47:30 -0800634 auto TagOffset = DV.getDebugLocListTagOffset();
635 if (TagOffset)
636 addUInt(*VariableDie, dwarf::DW_AT_LLVM_tag_offset, dwarf::DW_FORM_data1,
637 *TagOffset);
David Blaikieee7df552014-10-09 17:56:36 +0000638 return VariableDie;
639 }
640
Nikola Pricaabc1dff2019-06-10 08:41:06 +0000641 // Check if variable has a single location description.
642 if (auto *DVal = DV.getValueLoc()) {
643 if (DVal->isLocation())
644 addVariableAddress(DV, *VariableDie, DVal->getLoc());
645 else if (DVal->isInt()) {
Adrian Prantl3b89e662016-02-17 22:20:08 +0000646 auto *Expr = DV.getSingleExpression();
647 if (Expr && Expr->getNumElements()) {
648 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
649 DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
650 // If there is an expression, emit raw unsigned bytes.
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000651 DwarfExpr.addFragmentOffset(Expr);
Nikola Pricaabc1dff2019-06-10 08:41:06 +0000652 DwarfExpr.addUnsignedConstant(DVal->getInt());
Adrian Prantla63b8e82017-03-16 17:42:45 +0000653 DwarfExpr.addExpression(Expr);
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000654 addBlock(*VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize());
Evgenii Stepanovdabd2622019-11-25 17:47:30 -0800655 if (DwarfExpr.TagOffset)
656 addUInt(*VariableDie, dwarf::DW_AT_LLVM_tag_offset,
657 dwarf::DW_FORM_data1, *DwarfExpr.TagOffset);
658
Adrian Prantl3b89e662016-02-17 22:20:08 +0000659 } else
Nikola Pricaabc1dff2019-06-10 08:41:06 +0000660 addConstantValue(*VariableDie, DVal->getInt(), DV.getType());
661 } else if (DVal->isConstantFP()) {
662 addConstantFPValue(*VariableDie, DVal->getConstantFP());
663 } else if (DVal->isConstantInt()) {
664 addConstantValue(*VariableDie, DVal->getConstantInt(), DV.getType());
665 }
David Blaikieee7df552014-10-09 17:56:36 +0000666 return VariableDie;
667 }
668
669 // .. else use frame index.
Adrian Prantl67c24422017-02-17 19:42:32 +0000670 if (!DV.hasFrameIndexExprs())
Adrian Prantlca7e4702015-02-10 23:18:28 +0000671 return VariableDie;
672
Alexey Bataevf3a91502019-02-05 19:33:47 +0000673 Optional<unsigned> NVPTXAddressSpace;
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000674 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
Adrian Prantlca7e4702015-02-10 23:18:28 +0000675 DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
Adrian Prantl67c24422017-02-17 19:42:32 +0000676 for (auto &Fragment : DV.getFrameIndexExprs()) {
David Blaikieee7df552014-10-09 17:56:36 +0000677 unsigned FrameReg = 0;
Adrian Prantl6825fb62017-04-18 01:21:53 +0000678 const DIExpression *Expr = Fragment.Expr;
Eric Christopherd4e723f2015-02-20 22:36:11 +0000679 const TargetFrameLowering *TFI = Asm->MF->getSubtarget().getFrameLowering();
Adrian Prantl67c24422017-02-17 19:42:32 +0000680 int Offset = TFI->getFrameIndexReference(*Asm->MF, Fragment.FI, FrameReg);
Adrian Prantl6825fb62017-04-18 01:21:53 +0000681 DwarfExpr.addFragmentOffset(Expr);
Adrian Prantl956484b2017-03-20 21:35:09 +0000682 SmallVector<uint64_t, 8> Ops;
Bjorn Pettersson2b698a12019-09-07 11:40:10 +0000683 DIExpression::appendOffset(Ops, Offset);
Alexey Bataevf3a91502019-02-05 19:33:47 +0000684 // According to
685 // https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf
686 // cuda-gdb requires DW_AT_address_class for all variables to be able to
687 // correctly interpret address space of the variable address.
688 // Decode DW_OP_constu <DWARF Address Space> DW_OP_swap DW_OP_xderef
689 // sequence for the NVPTX + gdb target.
690 unsigned LocalNVPTXAddressSpace;
691 if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) {
692 const DIExpression *NewExpr =
693 DIExpression::extractAddressClass(Expr, LocalNVPTXAddressSpace);
694 if (NewExpr != Expr) {
695 Expr = NewExpr;
696 NVPTXAddressSpace = LocalNVPTXAddressSpace;
697 }
698 }
699 if (Expr)
700 Ops.append(Expr->elements_begin(), Expr->elements_end());
Adrian Prantl6825fb62017-04-18 01:21:53 +0000701 DIExpressionCursor Cursor(Ops);
Adrian Prantlc12cee32017-04-19 23:42:25 +0000702 DwarfExpr.setMemoryLocationKind();
Alexey Bataev4dd75582018-07-26 16:29:52 +0000703 if (const MCSymbol *FrameSymbol = Asm->getFunctionFrameSymbol())
704 addOpAddress(*Loc, FrameSymbol);
705 else
706 DwarfExpr.addMachineRegExpression(
707 *Asm->MF->getSubtarget().getRegisterInfo(), Cursor, FrameReg);
Adrian Prantl6825fb62017-04-18 01:21:53 +0000708 DwarfExpr.addExpression(std::move(Cursor));
David Blaikieee7df552014-10-09 17:56:36 +0000709 }
Alexey Bataevf3a91502019-02-05 19:33:47 +0000710 if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) {
711 // According to
712 // https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf
713 // cuda-gdb requires DW_AT_address_class for all variables to be able to
714 // correctly interpret address space of the variable address.
715 const unsigned NVPTX_ADDR_local_space = 6;
716 addUInt(*VariableDie, dwarf::DW_AT_address_class, dwarf::DW_FORM_data1,
717 NVPTXAddressSpace ? *NVPTXAddressSpace : NVPTX_ADDR_local_space);
718 }
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000719 addBlock(*VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize());
Peter Collingbournefb9ce102019-06-17 23:39:41 +0000720 if (DwarfExpr.TagOffset)
721 addUInt(*VariableDie, dwarf::DW_AT_LLVM_tag_offset, dwarf::DW_FORM_data1,
722 *DwarfExpr.TagOffset);
David Blaikieee7df552014-10-09 17:56:36 +0000723
724 return VariableDie;
725}
726
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000727DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV,
728 const LexicalScope &Scope,
729 DIE *&ObjectPointer) {
David Blaikie4a1a44e2014-10-09 17:56:39 +0000730 auto Var = constructVariableDIE(DV, Scope.isAbstractScope());
731 if (DV.isObjectPointer())
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000732 ObjectPointer = Var;
David Blaikie4a1a44e2014-10-09 17:56:39 +0000733 return Var;
734}
735
Adrian Prantl03dd6f52018-02-08 23:21:15 +0000736/// Return all DIVariables that appear in count: expressions.
737static SmallVector<const DIVariable *, 2> dependencies(DbgVariable *Var) {
738 SmallVector<const DIVariable *, 2> Result;
739 auto *Array = dyn_cast<DICompositeType>(Var->getType());
Adrian Prantlc929f7a2018-02-06 22:17:45 +0000740 if (!Array || Array->getTag() != dwarf::DW_TAG_array_type)
Adrian Prantl03dd6f52018-02-08 23:21:15 +0000741 return Result;
742 for (auto *El : Array->getElements()) {
Adrian Prantlc929f7a2018-02-06 22:17:45 +0000743 if (auto *Subrange = dyn_cast<DISubrange>(El)) {
744 auto Count = Subrange->getCount();
Adrian Prantl03dd6f52018-02-08 23:21:15 +0000745 if (auto *Dependency = Count.dyn_cast<DIVariable *>())
746 Result.push_back(Dependency);
Adrian Prantlc929f7a2018-02-06 22:17:45 +0000747 }
Adrian Prantl03dd6f52018-02-08 23:21:15 +0000748 }
749 return Result;
Adrian Prantlc929f7a2018-02-06 22:17:45 +0000750}
751
752/// Sort local variables so that variables appearing inside of helper
753/// expressions come first.
Adrian Prantl03dd6f52018-02-08 23:21:15 +0000754static SmallVector<DbgVariable *, 8>
755sortLocalVars(SmallVectorImpl<DbgVariable *> &Input) {
756 SmallVector<DbgVariable *, 8> Result;
757 SmallVector<PointerIntPair<DbgVariable *, 1>, 8> WorkList;
758 // Map back from a DIVariable to its containing DbgVariable.
759 SmallDenseMap<const DILocalVariable *, DbgVariable *> DbgVar;
760 // Set of DbgVariables in Result.
761 SmallDenseSet<DbgVariable *, 8> Visited;
762 // For cycle detection.
763 SmallDenseSet<DbgVariable *, 8> Visiting;
764
765 // Initialize the worklist and the DIVariable lookup table.
766 for (auto Var : reverse(Input)) {
767 DbgVar.insert({Var->getVariable(), Var});
768 WorkList.push_back({Var, 0});
769 }
770
771 // Perform a stable topological sort by doing a DFS.
772 while (!WorkList.empty()) {
773 auto Item = WorkList.back();
774 DbgVariable *Var = Item.getPointer();
775 bool visitedAllDependencies = Item.getInt();
776 WorkList.pop_back();
777
778 // Dependency is in a different lexical scope or a global.
779 if (!Var)
780 continue;
781
782 // Already handled.
783 if (Visited.count(Var))
784 continue;
785
786 // Add to Result if all dependencies are visited.
787 if (visitedAllDependencies) {
788 Visited.insert(Var);
789 Result.push_back(Var);
790 continue;
791 }
792
793 // Detect cycles.
794 auto Res = Visiting.insert(Var);
795 if (!Res.second) {
796 assert(false && "dependency cycle in local variables");
797 return Result;
798 }
799
800 // Push dependencies and this node onto the worklist, so that this node is
801 // visited again after all of its dependencies are handled.
802 WorkList.push_back({Var, 1});
803 for (auto *Dependency : dependencies(Var)) {
804 auto Dep = dyn_cast_or_null<const DILocalVariable>(Dependency);
805 WorkList.push_back({DbgVar[Dep], 0});
806 }
807 }
808 return Result;
Adrian Prantlc929f7a2018-02-06 22:17:45 +0000809}
810
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000811DIE *DwarfCompileUnit::createScopeChildrenDIE(LexicalScope *Scope,
812 SmallVectorImpl<DIE *> &Children,
David Blaikie2195e132017-07-27 00:06:53 +0000813 bool *HasNonScopeChildren) {
814 assert(Children.empty());
David Blaikie8b2fdb82014-10-09 18:24:28 +0000815 DIE *ObjectPointer = nullptr;
816
Adrian Prantlc929f7a2018-02-06 22:17:45 +0000817 // Emit function arguments (order is significant).
818 auto Vars = DU->getScopeVariables().lookup(Scope);
819 for (auto &DV : Vars.Args)
820 Children.push_back(constructVariableDIE(*DV.second, *Scope, ObjectPointer));
821
822 // Emit local variables.
Adrian Prantl03dd6f52018-02-08 23:21:15 +0000823 auto Locals = sortLocalVars(Vars.Locals);
824 for (DbgVariable *DV : Locals)
David Blaikie8b2fdb82014-10-09 18:24:28 +0000825 Children.push_back(constructVariableDIE(*DV, *Scope, ObjectPointer));
826
David Blaikie2195e132017-07-27 00:06:53 +0000827 // Skip imported directives in gmlt-like data.
828 if (!includeMinimalInlineScopes()) {
829 // There is no need to emit empty lexical block DIE.
830 for (const auto *IE : ImportedEntities[Scope->getScopeNode()])
831 Children.push_back(
832 constructImportedEntityDIE(cast<DIImportedEntity>(IE)));
833 }
834
835 if (HasNonScopeChildren)
836 *HasNonScopeChildren = !Children.empty();
David Blaikie8b2fdb82014-10-09 18:24:28 +0000837
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000838 for (DbgLabel *DL : DU->getScopeLabels().lookup(Scope))
839 Children.push_back(constructLabelDIE(*DL, *Scope));
840
David Blaikie8b2fdb82014-10-09 18:24:28 +0000841 for (LexicalScope *LS : Scope->getChildren())
842 constructScopeDIE(LS, Children);
843
David Blaikie8b2fdb82014-10-09 18:24:28 +0000844 return ObjectPointer;
845}
846
Vedant Kumar5931b4e2018-10-05 20:37:17 +0000847DIE &DwarfCompileUnit::constructSubprogramScopeDIE(const DISubprogram *Sub,
848 LexicalScope *Scope) {
David Blaikie1d072342014-10-09 20:21:36 +0000849 DIE &ScopeDIE = updateSubprogramScopeDIE(Sub);
850
David Blaikie3e3eb332016-12-15 23:17:52 +0000851 if (Scope) {
852 assert(!Scope->getInlinedAt());
853 assert(!Scope->isAbstractScope());
854 // Collect lexical scope children first.
855 // ObjectPointer might be a local (non-argument) local variable if it's a
856 // block's synthetic this pointer.
857 if (DIE *ObjectPointer = createAndAddScopeChildren(Scope, ScopeDIE))
858 addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer, *ObjectPointer);
859 }
860
David Blaikie1d072342014-10-09 20:21:36 +0000861 // If this is a variadic function, add an unspecified parameter.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000862 DITypeRefArray FnArgs = Sub->getType()->getTypeArray();
David Blaikie1dd573d2014-10-23 22:27:50 +0000863
David Blaikie1d072342014-10-09 20:21:36 +0000864 // If we have a single element of null, it is a function that returns void.
865 // If we have more than one elements and the last one is null, it is a
866 // variadic function.
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000867 if (FnArgs.size() > 1 && !FnArgs[FnArgs.size() - 1] &&
David Blaikie3a443c22014-11-04 22:12:25 +0000868 !includeMinimalInlineScopes())
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000869 ScopeDIE.addChild(
870 DIE::get(DIEValueAllocator, dwarf::DW_TAG_unspecified_parameters));
Vedant Kumar5931b4e2018-10-05 20:37:17 +0000871
872 return ScopeDIE;
David Blaikie1d072342014-10-09 20:21:36 +0000873}
874
David Blaikie78b65b62014-10-09 20:26:15 +0000875DIE *DwarfCompileUnit::createAndAddScopeChildren(LexicalScope *Scope,
876 DIE &ScopeDIE) {
877 // We create children when the scope DIE is not null.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000878 SmallVector<DIE *, 8> Children;
David Blaikie78b65b62014-10-09 20:26:15 +0000879 DIE *ObjectPointer = createScopeChildrenDIE(Scope, Children);
880
881 // Add children
882 for (auto &I : Children)
883 ScopeDIE.addChild(std::move(I));
884
885 return ObjectPointer;
886}
887
Dehao Chen54511352015-11-11 18:09:47 +0000888void DwarfCompileUnit::constructAbstractSubprogramScopeDIE(
889 LexicalScope *Scope) {
David Blaikie488393f2017-05-12 01:13:45 +0000890 DIE *&AbsDef = getAbstractSPDies()[Scope->getScopeNode()];
David Blaikie49be5b32014-10-31 21:57:02 +0000891 if (AbsDef)
892 return;
893
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000894 auto *SP = cast<DISubprogram>(Scope->getScopeNode());
David Blaikie58410f22014-10-10 06:39:26 +0000895
896 DIE *ContextDIE;
Jonas Devlieghere294e6892017-11-15 10:57:05 +0000897 DwarfCompileUnit *ContextCU = this;
David Blaikie58410f22014-10-10 06:39:26 +0000898
David Blaikie3a443c22014-11-04 22:12:25 +0000899 if (includeMinimalInlineScopes())
900 ContextDIE = &getUnitDie();
David Blaikie58410f22014-10-10 06:39:26 +0000901 // Some of this is duplicated from DwarfUnit::getOrCreateSubprogramDIE, with
Duncan P. N. Exon Smith7c60f202015-04-18 00:35:36 +0000902 // the important distinction that the debug node is not associated with the
903 // DIE (since the debug node will be associated with the concrete DIE, if
David Blaikie58410f22014-10-10 06:39:26 +0000904 // any). It could be refactored to some common utility function.
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000905 else if (auto *SPDecl = SP->getDeclaration()) {
David Blaikie58410f22014-10-10 06:39:26 +0000906 ContextDIE = &getUnitDie();
907 getOrCreateSubprogramDIE(SPDecl);
Jonas Devlieghere294e6892017-11-15 10:57:05 +0000908 } else {
Fangrui Songda82ce92019-05-07 02:06:37 +0000909 ContextDIE = getOrCreateContextDIE(SP->getScope());
Jonas Devlieghere294e6892017-11-15 10:57:05 +0000910 // The scope may be shared with a subprogram that has already been
911 // constructed in another CU, in which case we need to construct this
912 // subprogram in the same CU.
913 ContextCU = DD->lookupCU(ContextDIE->getUnitDie());
914 }
David Blaikie58410f22014-10-10 06:39:26 +0000915
Duncan P. N. Exon Smith7c60f202015-04-18 00:35:36 +0000916 // Passing null as the associated node because the abstract definition
David Blaikie58410f22014-10-10 06:39:26 +0000917 // shouldn't be found by lookup.
Jonas Devlieghere294e6892017-11-15 10:57:05 +0000918 AbsDef = &ContextCU->createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, nullptr);
919 ContextCU->applySubprogramAttributesToDefinition(SP, *AbsDef);
David Blaikie58410f22014-10-10 06:39:26 +0000920
Jonas Devlieghere294e6892017-11-15 10:57:05 +0000921 if (!ContextCU->includeMinimalInlineScopes())
922 ContextCU->addUInt(*AbsDef, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined);
923 if (DIE *ObjectPointer = ContextCU->createAndAddScopeChildren(Scope, *AbsDef))
924 ContextCU->addDIEEntry(*AbsDef, dwarf::DW_AT_object_pointer, *ObjectPointer);
David Blaikie58410f22014-10-10 06:39:26 +0000925}
926
Vedant Kumar533dd022019-08-26 20:53:12 +0000927/// Whether to use the GNU analog for a DWARF5 tag, attribute, or location atom.
928static bool useGNUAnalogForDwarf5Feature(DwarfDebug *DD) {
929 return DD->getDwarfVersion() == 4 && DD->tuneForGDB();
930}
931
Vedant Kumar58a07142019-08-26 20:53:34 +0000932dwarf::Tag DwarfCompileUnit::getDwarf5OrGNUTag(dwarf::Tag Tag) const {
Vedant Kumar533dd022019-08-26 20:53:12 +0000933 if (!useGNUAnalogForDwarf5Feature(DD))
Djordje Todorovicb9973f82019-07-31 16:51:28 +0000934 return Tag;
935 switch (Tag) {
936 case dwarf::DW_TAG_call_site:
937 return dwarf::DW_TAG_GNU_call_site;
938 case dwarf::DW_TAG_call_site_parameter:
939 return dwarf::DW_TAG_GNU_call_site_parameter;
940 default:
Vedant Kumar58a07142019-08-26 20:53:34 +0000941 llvm_unreachable("DWARF5 tag with no GNU analog");
Djordje Todorovicb9973f82019-07-31 16:51:28 +0000942 }
943}
944
945dwarf::Attribute
Vedant Kumar58a07142019-08-26 20:53:34 +0000946DwarfCompileUnit::getDwarf5OrGNUAttr(dwarf::Attribute Attr) const {
Vedant Kumar533dd022019-08-26 20:53:12 +0000947 if (!useGNUAnalogForDwarf5Feature(DD))
Djordje Todorovicb9973f82019-07-31 16:51:28 +0000948 return Attr;
949 switch (Attr) {
950 case dwarf::DW_AT_call_all_calls:
951 return dwarf::DW_AT_GNU_all_call_sites;
952 case dwarf::DW_AT_call_target:
953 return dwarf::DW_AT_GNU_call_site_target;
954 case dwarf::DW_AT_call_origin:
955 return dwarf::DW_AT_abstract_origin;
Vedant Kumar43464502020-01-15 14:15:45 -0800956 case dwarf::DW_AT_call_return_pc:
Djordje Todorovicb9973f82019-07-31 16:51:28 +0000957 return dwarf::DW_AT_low_pc;
958 case dwarf::DW_AT_call_value:
959 return dwarf::DW_AT_GNU_call_site_value;
960 case dwarf::DW_AT_call_tail_call:
961 return dwarf::DW_AT_GNU_tail_call;
962 default:
Vedant Kumar58a07142019-08-26 20:53:34 +0000963 llvm_unreachable("DWARF5 attribute with no GNU analog");
Djordje Todorovicb9973f82019-07-31 16:51:28 +0000964 }
965}
966
Vedant Kumar533dd022019-08-26 20:53:12 +0000967dwarf::LocationAtom
968DwarfCompileUnit::getDwarf5OrGNULocationAtom(dwarf::LocationAtom Loc) const {
969 if (!useGNUAnalogForDwarf5Feature(DD))
970 return Loc;
971 switch (Loc) {
972 case dwarf::DW_OP_entry_value:
973 return dwarf::DW_OP_GNU_entry_value;
974 default:
975 llvm_unreachable("DWARF5 location atom with no GNU analog");
976 }
977}
978
Vedant Kumarf0120552020-01-09 18:00:33 -0800979DIE &DwarfCompileUnit::constructCallSiteEntryDIE(DIE &ScopeDIE,
Vedant Kumare08f2052020-01-25 12:52:47 -0800980 DIE *CalleeDIE,
Vedant Kumarf0120552020-01-09 18:00:33 -0800981 bool IsTail,
982 const MCSymbol *PCAddr,
983 unsigned CallReg) {
Vedant Kumar5931b4e2018-10-05 20:37:17 +0000984 // Insert a call site entry DIE within ScopeDIE.
Vedant Kumar58a07142019-08-26 20:53:34 +0000985 DIE &CallSiteDIE = createAndAddDIE(getDwarf5OrGNUTag(dwarf::DW_TAG_call_site),
986 ScopeDIE, nullptr);
Vedant Kumar5931b4e2018-10-05 20:37:17 +0000987
Djordje Todorovicb9973f82019-07-31 16:51:28 +0000988 if (CallReg) {
989 // Indirect call.
Vedant Kumar58a07142019-08-26 20:53:34 +0000990 addAddress(CallSiteDIE, getDwarf5OrGNUAttr(dwarf::DW_AT_call_target),
Djordje Todorovicb9973f82019-07-31 16:51:28 +0000991 MachineLocation(CallReg));
Djordje Todorovic0739ccd2019-07-12 09:45:12 +0000992 } else {
Vedant Kumare08f2052020-01-25 12:52:47 -0800993 assert(CalleeDIE && "No DIE for call site entry origin");
Vedant Kumar58a07142019-08-26 20:53:34 +0000994 addDIEEntry(CallSiteDIE, getDwarf5OrGNUAttr(dwarf::DW_AT_call_origin),
Djordje Todorovicb9973f82019-07-31 16:51:28 +0000995 *CalleeDIE);
996 }
997
998 if (IsTail)
999 // Attach DW_AT_call_tail_call to tail calls for standards compliance.
Vedant Kumar58a07142019-08-26 20:53:34 +00001000 addFlag(CallSiteDIE, getDwarf5OrGNUAttr(dwarf::DW_AT_call_tail_call));
Djordje Todorovicb9973f82019-07-31 16:51:28 +00001001
1002 // Attach the return PC to allow the debugger to disambiguate call paths
1003 // from one function to another.
Vedant Kumar43464502020-01-15 14:15:45 -08001004 //
1005 // The return PC is only really needed when the call /isn't/ a tail call, but
1006 // for some reason GDB always expects it.
1007 if (!IsTail || DD->tuneForGDB()) {
Vedant Kumarf0120552020-01-09 18:00:33 -08001008 assert(PCAddr && "Missing return PC information for a call");
Vedant Kumar43464502020-01-15 14:15:45 -08001009 addLabelAddress(CallSiteDIE,
1010 getDwarf5OrGNUAttr(dwarf::DW_AT_call_return_pc), PCAddr);
Vedant Kumar5931b4e2018-10-05 20:37:17 +00001011 }
Djordje Todorovicb9973f82019-07-31 16:51:28 +00001012
Vedant Kumar5931b4e2018-10-05 20:37:17 +00001013 return CallSiteDIE;
1014}
1015
Djordje Todorovicb9973f82019-07-31 16:51:28 +00001016void DwarfCompileUnit::constructCallSiteParmEntryDIEs(
1017 DIE &CallSiteDIE, SmallVector<DbgCallSiteParam, 4> &Params) {
1018 for (const auto &Param : Params) {
1019 unsigned Register = Param.getRegister();
1020 auto CallSiteDieParam =
1021 DIE::get(DIEValueAllocator,
Vedant Kumar58a07142019-08-26 20:53:34 +00001022 getDwarf5OrGNUTag(dwarf::DW_TAG_call_site_parameter));
Djordje Todorovicb9973f82019-07-31 16:51:28 +00001023 insertDIE(CallSiteDieParam);
1024 addAddress(*CallSiteDieParam, dwarf::DW_AT_location,
1025 MachineLocation(Register));
1026
1027 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1028 DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
1029 DwarfExpr.setCallSiteParamValueFlag();
1030
1031 DwarfDebug::emitDebugLocValue(*Asm, nullptr, Param.getValue(), DwarfExpr);
1032
Vedant Kumar58a07142019-08-26 20:53:34 +00001033 addBlock(*CallSiteDieParam, getDwarf5OrGNUAttr(dwarf::DW_AT_call_value),
Djordje Todorovicb9973f82019-07-31 16:51:28 +00001034 DwarfExpr.finalize());
1035
1036 CallSiteDIE.addChild(CallSiteDieParam);
1037 }
1038}
1039
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +00001040DIE *DwarfCompileUnit::constructImportedEntityDIE(
1041 const DIImportedEntity *Module) {
Amjad Aboud72da9392016-04-30 01:44:07 +00001042 DIE *IMDie = DIE::get(DIEValueAllocator, (dwarf::Tag)Module->getTag());
1043 insertDIE(Module, IMDie);
Frederic Riss987fe222014-10-24 21:31:09 +00001044 DIE *EntityDie;
Fangrui Songda82ce92019-05-07 02:06:37 +00001045 auto *Entity = Module->getEntity();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001046 if (auto *NS = dyn_cast<DINamespace>(Entity))
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +00001047 EntityDie = getOrCreateNameSpace(NS);
Adrian Prantl08a388b2015-06-30 02:13:04 +00001048 else if (auto *M = dyn_cast<DIModule>(Entity))
1049 EntityDie = getOrCreateModule(M);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001050 else if (auto *SP = dyn_cast<DISubprogram>(Entity))
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +00001051 EntityDie = getOrCreateSubprogramDIE(SP);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001052 else if (auto *T = dyn_cast<DIType>(Entity))
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +00001053 EntityDie = getOrCreateTypeDIE(T);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001054 else if (auto *GV = dyn_cast<DIGlobalVariable>(Entity))
Adrian Prantlbceaaa92016-12-20 02:09:43 +00001055 EntityDie = getOrCreateGlobalVariableDIE(GV, {});
Frederic Riss987fe222014-10-24 21:31:09 +00001056 else
1057 EntityDie = getDIE(Entity);
1058 assert(EntityDie);
Paul Robinson612e89d2018-01-12 19:17:50 +00001059 addSourceLine(*IMDie, Module->getLine(), Module->getFile());
Frederic Riss987fe222014-10-24 21:31:09 +00001060 addDIEEntry(*IMDie, dwarf::DW_AT_import, *EntityDie);
Duncan P. N. Exon Smithde8e4272015-04-14 01:46:44 +00001061 StringRef Name = Module->getName();
Frederic Riss987fe222014-10-24 21:31:09 +00001062 if (!Name.empty())
1063 addString(*IMDie, dwarf::DW_AT_name, Name);
1064
1065 return IMDie;
1066}
1067
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001068void DwarfCompileUnit::finishSubprogramDefinition(const DISubprogram *SP) {
Amjad Aboud72da9392016-04-30 01:44:07 +00001069 DIE *D = getDIE(SP);
David Blaikie488393f2017-05-12 01:13:45 +00001070 if (DIE *AbsSPDIE = getAbstractSPDies().lookup(SP)) {
Amjad Aboud72da9392016-04-30 01:44:07 +00001071 if (D)
David Blaikie4191cbc2014-10-10 06:39:29 +00001072 // If this subprogram has an abstract definition, reference that
1073 addDIEEntry(*D, dwarf::DW_AT_abstract_origin, *AbsSPDIE);
Amjad Aboud72da9392016-04-30 01:44:07 +00001074 } else {
David Blaikie3e3eb332016-12-15 23:17:52 +00001075 assert(D || includeMinimalInlineScopes());
Amjad Aboud72da9392016-04-30 01:44:07 +00001076 if (D)
David Blaikie4191cbc2014-10-10 06:39:29 +00001077 // And attach the attributes
1078 applySubprogramAttributesToDefinition(SP, *D);
1079 }
1080}
1081
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001082void DwarfCompileUnit::finishEntityDefinition(const DbgEntity *Entity) {
1083 DbgEntity *AbsEntity = getExistingAbstractEntity(Entity->getEntity());
1084
1085 auto *Die = Entity->getDIE();
1086 /// Label may be used to generate DW_AT_low_pc, so put it outside
1087 /// if/else block.
1088 const DbgLabel *Label = nullptr;
1089 if (AbsEntity && AbsEntity->getDIE()) {
1090 addDIEEntry(*Die, dwarf::DW_AT_abstract_origin, *AbsEntity->getDIE());
1091 Label = dyn_cast<const DbgLabel>(Entity);
1092 } else {
1093 if (const DbgVariable *Var = dyn_cast<const DbgVariable>(Entity))
1094 applyVariableAttributes(*Var, *Die);
1095 else if ((Label = dyn_cast<const DbgLabel>(Entity)))
1096 applyLabelAttributes(*Label, *Die);
1097 else
1098 llvm_unreachable("DbgEntity must be DbgVariable or DbgLabel.");
1099 }
1100
Hsiangkai Wang55321d82018-09-26 04:19:23 +00001101 if (Label)
1102 if (const auto *Sym = Label->getSymbol())
1103 addLabelAddress(*Die, dwarf::DW_AT_low_pc, Sym);
David Blaikie488393f2017-05-12 01:13:45 +00001104}
1105
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001106DbgEntity *DwarfCompileUnit::getExistingAbstractEntity(const DINode *Node) {
1107 auto &AbstractEntities = getAbstractEntities();
1108 auto I = AbstractEntities.find(Node);
1109 if (I != AbstractEntities.end())
David Blaikie488393f2017-05-12 01:13:45 +00001110 return I->second.get();
1111 return nullptr;
1112}
1113
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001114void DwarfCompileUnit::createAbstractEntity(const DINode *Node,
1115 LexicalScope *Scope) {
David Blaikie488393f2017-05-12 01:13:45 +00001116 assert(Scope && Scope->isAbstractScope());
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001117 auto &Entity = getAbstractEntities()[Node];
1118 if (isa<const DILocalVariable>(Node)) {
Jonas Devlieghere0eaee542019-08-15 15:54:37 +00001119 Entity = std::make_unique<DbgVariable>(
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001120 cast<const DILocalVariable>(Node), nullptr /* IA */);;
1121 DU->addScopeVariable(Scope, cast<DbgVariable>(Entity.get()));
1122 } else if (isa<const DILabel>(Node)) {
Jonas Devlieghere0eaee542019-08-15 15:54:37 +00001123 Entity = std::make_unique<DbgLabel>(
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001124 cast<const DILabel>(Node), nullptr /* IA */);
1125 DU->addScopeLabel(Scope, cast<DbgLabel>(Entity.get()));
1126 }
David Blaikie488393f2017-05-12 01:13:45 +00001127}
1128
Rafael Espindola063d7252015-03-10 16:58:10 +00001129void DwarfCompileUnit::emitHeader(bool UseOffsets) {
David Blaikieb6726a92014-11-02 02:26:24 +00001130 // Don't bother labeling the .dwo unit, as its offset isn't used.
Alexey Bataevbff36082018-03-23 13:35:54 +00001131 if (!Skeleton && !DD->useSectionsAsReferences()) {
Rafael Espindola9ab09232015-03-17 20:07:06 +00001132 LabelBegin = Asm->createTempSymbol("cu_begin");
Lang Hames9ff69c82015-04-24 19:11:51 +00001133 Asm->OutStreamer->EmitLabel(LabelBegin);
Rafael Espindola3c311142015-03-10 03:11:11 +00001134 }
David Blaikieae57e662014-11-01 23:59:23 +00001135
Paul Robinsoncddd6042017-02-28 20:24:55 +00001136 dwarf::UnitType UT = Skeleton ? dwarf::DW_UT_split_compile
1137 : DD->useSplitDwarf() ? dwarf::DW_UT_skeleton
1138 : dwarf::DW_UT_compile;
1139 DwarfUnit::emitCommonHeader(UseOffsets, UT);
Paul Robinson543c0e12018-05-22 17:27:31 +00001140 if (DD->getDwarfVersion() >= 5 && UT != dwarf::DW_UT_compile)
1141 Asm->emitInt64(getDWOId());
David Blaikieae57e662014-11-01 23:59:23 +00001142}
1143
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001144bool DwarfCompileUnit::hasDwarfPubSections() const {
David Blaikie66cf14d2018-08-16 21:29:55 +00001145 switch (CUNode->getNameTableKind()) {
1146 case DICompileUnit::DebugNameTableKind::None:
1147 return false;
1148 // Opting in to GNU Pubnames/types overrides the default to ensure these are
1149 // generated for things like Gold's gdb_index generation.
1150 case DICompileUnit::DebugNameTableKind::GNU:
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001151 return true;
David Blaikie66cf14d2018-08-16 21:29:55 +00001152 case DICompileUnit::DebugNameTableKind::Default:
David Blaikie0e030472018-08-16 23:57:15 +00001153 return DD->tuneForGDB() && !includeMinimalInlineScopes() &&
David Blaikie2f511762019-04-23 19:00:45 +00001154 !CUNode->isDebugDirectivesOnly() &&
1155 DD->getAccelTableKind() != AccelTableKind::Apple &&
1156 DD->getDwarfVersion() < 5;
David Blaikie66cf14d2018-08-16 21:29:55 +00001157 }
Simon Pilgrim22d580f22018-08-17 09:47:52 +00001158 llvm_unreachable("Unhandled DICompileUnit::DebugNameTableKind enum");
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001159}
1160
David Blaikie192b45c2014-11-02 06:16:39 +00001161/// addGlobalName - Add a new global name to the compile unit.
David Blaikiea0e3c752017-02-03 00:44:18 +00001162void DwarfCompileUnit::addGlobalName(StringRef Name, const DIE &Die,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001163 const DIScope *Context) {
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001164 if (!hasDwarfPubSections())
David Blaikie192b45c2014-11-02 06:16:39 +00001165 return;
1166 std::string FullName = getParentContextString(Context) + Name.str();
1167 GlobalNames[FullName] = &Die;
1168}
1169
David Blaikiea0e3c752017-02-03 00:44:18 +00001170void DwarfCompileUnit::addGlobalNameForTypeUnit(StringRef Name,
1171 const DIScope *Context) {
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001172 if (!hasDwarfPubSections())
David Blaikiea0e3c752017-02-03 00:44:18 +00001173 return;
1174 std::string FullName = getParentContextString(Context) + Name.str();
1175 // Insert, allowing the entry to remain as-is if it's already present
1176 // This way the CU-level type DIE is preferred over the "can't describe this
1177 // type as a unit offset because it's not really in the CU at all, it's only
1178 // in a type unit"
1179 GlobalNames.insert(std::make_pair(std::move(FullName), &getUnitDie()));
1180}
1181
David Blaikie192b45c2014-11-02 06:16:39 +00001182/// Add a new global type to the unit.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001183void DwarfCompileUnit::addGlobalType(const DIType *Ty, const DIE &Die,
1184 const DIScope *Context) {
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001185 if (!hasDwarfPubSections())
David Blaikie192b45c2014-11-02 06:16:39 +00001186 return;
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +00001187 std::string FullName = getParentContextString(Context) + Ty->getName().str();
David Blaikie192b45c2014-11-02 06:16:39 +00001188 GlobalTypes[FullName] = &Die;
1189}
David Blaikieae57e662014-11-01 23:59:23 +00001190
David Blaikiea0e3c752017-02-03 00:44:18 +00001191void DwarfCompileUnit::addGlobalTypeUnitType(const DIType *Ty,
1192 const DIScope *Context) {
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001193 if (!hasDwarfPubSections())
David Blaikiea0e3c752017-02-03 00:44:18 +00001194 return;
1195 std::string FullName = getParentContextString(Context) + Ty->getName().str();
1196 // Insert, allowing the entry to remain as-is if it's already present
1197 // This way the CU-level type DIE is preferred over the "can't describe this
1198 // type as a unit offset because it's not really in the CU at all, it's only
1199 // in a type unit"
1200 GlobalTypes.insert(std::make_pair(std::move(FullName), &getUnitDie()));
1201}
1202
David Blaikie7d48be22014-11-02 06:37:23 +00001203void DwarfCompileUnit::addVariableAddress(const DbgVariable &DV, DIE &Die,
1204 MachineLocation Location) {
Duncan P. N. Exon Smithe6cc5312015-06-21 16:50:43 +00001205 if (DV.hasComplexAddress())
David Blaikie7d48be22014-11-02 06:37:23 +00001206 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
David Blaikie7d48be22014-11-02 06:37:23 +00001207 else
Adrian Prantl5883af32015-01-19 17:57:29 +00001208 addAddress(Die, dwarf::DW_AT_location, Location);
David Blaikie7d48be22014-11-02 06:37:23 +00001209}
David Blaikief7435ee2014-11-02 06:46:40 +00001210
1211/// Add an address attribute to a die based on the location provided.
1212void DwarfCompileUnit::addAddress(DIE &Die, dwarf::Attribute Attribute,
Adrian Prantl5883af32015-01-19 17:57:29 +00001213 const MachineLocation &Location) {
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +00001214 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
Adrian Prantl956484b2017-03-20 21:35:09 +00001215 DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
Adrian Prantlc12cee32017-04-19 23:42:25 +00001216 if (Location.isIndirect())
1217 DwarfExpr.setMemoryLocationKind();
David Blaikief7435ee2014-11-02 06:46:40 +00001218
Adrian Prantl2049c0d2017-08-02 15:22:17 +00001219 DIExpressionCursor Cursor({});
Adrian Prantl956484b2017-03-20 21:35:09 +00001220 const TargetRegisterInfo &TRI = *Asm->MF->getSubtarget().getRegisterInfo();
Adrian Prantlc12cee32017-04-19 23:42:25 +00001221 if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg()))
Adrian Prantlab255fc2014-12-05 01:02:46 +00001222 return;
Adrian Prantl956484b2017-03-20 21:35:09 +00001223 DwarfExpr.addExpression(std::move(Cursor));
Adrian Prantlab255fc2014-12-05 01:02:46 +00001224
David Blaikief7435ee2014-11-02 06:46:40 +00001225 // Now attach the location information to the DIE.
Adrian Prantl956484b2017-03-20 21:35:09 +00001226 addBlock(Die, Attribute, DwarfExpr.finalize());
Evgenii Stepanovdabd2622019-11-25 17:47:30 -08001227
1228 if (DwarfExpr.TagOffset)
1229 addUInt(Die, dwarf::DW_AT_LLVM_tag_offset, dwarf::DW_FORM_data1,
1230 *DwarfExpr.TagOffset);
David Blaikief7435ee2014-11-02 06:46:40 +00001231}
David Blaikie77895fb2014-11-02 06:58:44 +00001232
1233/// Start with the address based on the location provided, and generate the
1234/// DWARF information necessary to find the actual variable given the extra
1235/// address information encoded in the DbgVariable, starting from the starting
1236/// location. Add the DWARF information to the die.
1237void DwarfCompileUnit::addComplexAddress(const DbgVariable &DV, DIE &Die,
1238 dwarf::Attribute Attribute,
1239 const MachineLocation &Location) {
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +00001240 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
Adrian Prantl7813d9c2015-01-14 01:01:30 +00001241 DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
Adrian Prantl956484b2017-03-20 21:35:09 +00001242 const DIExpression *DIExpr = DV.getSingleExpression();
1243 DwarfExpr.addFragmentOffset(DIExpr);
Adrian Prantlc12cee32017-04-19 23:42:25 +00001244 if (Location.isIndirect())
1245 DwarfExpr.setMemoryLocationKind();
Adrian Prantl956484b2017-03-20 21:35:09 +00001246
Adrian Prantl2049c0d2017-08-02 15:22:17 +00001247 DIExpressionCursor Cursor(DIExpr);
Djordje Todorovic12aca5d2019-07-09 08:36:34 +00001248
1249 if (DIExpr->isEntryValue()) {
1250 DwarfExpr.setEntryValueFlag();
David Stenberg1ae2d9a2019-10-15 11:31:21 +00001251 DwarfExpr.beginEntryValueExpression(Cursor);
Djordje Todorovic12aca5d2019-07-09 08:36:34 +00001252 }
1253
Peter Collingbourne96c9ae62016-05-20 19:35:17 +00001254 const TargetRegisterInfo &TRI = *Asm->MF->getSubtarget().getRegisterInfo();
Adrian Prantlc12cee32017-04-19 23:42:25 +00001255 if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg()))
Adrian Prantl54286bd2016-11-02 16:12:20 +00001256 return;
Adrian Prantl956484b2017-03-20 21:35:09 +00001257 DwarfExpr.addExpression(std::move(Cursor));
David Blaikie77895fb2014-11-02 06:58:44 +00001258
1259 // Now attach the location information to the DIE.
Adrian Prantl956484b2017-03-20 21:35:09 +00001260 addBlock(Die, Attribute, DwarfExpr.finalize());
Evgenii Stepanovdabd2622019-11-25 17:47:30 -08001261
1262 if (DwarfExpr.TagOffset)
1263 addUInt(Die, dwarf::DW_AT_LLVM_tag_offset, dwarf::DW_FORM_data1,
1264 *DwarfExpr.TagOffset);
David Blaikie77895fb2014-11-02 06:58:44 +00001265}
David Blaikie4bc08812014-11-02 07:03:19 +00001266
1267/// Add a Dwarf loclistptr attribute data and value.
1268void DwarfCompileUnit::addLocationList(DIE &Die, dwarf::Attribute Attribute,
1269 unsigned Index) {
David Blaikie77cfcd72019-11-12 14:15:37 -08001270 dwarf::Form Form = dwarf::DW_FORM_data4;
1271 if (DD->getDwarfVersion() == 4)
1272 Form =dwarf::DW_FORM_sec_offset;
1273 if (DD->getDwarfVersion() >= 5)
1274 Form =dwarf::DW_FORM_loclistx;
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +00001275 Die.addValue(DIEValueAllocator, Attribute, Form, DIELocList(Index));
David Blaikie4bc08812014-11-02 07:03:19 +00001276}
David Blaikie02a63332014-11-02 07:06:51 +00001277
David Blaikie8c485b52014-11-02 07:08:12 +00001278void DwarfCompileUnit::applyVariableAttributes(const DbgVariable &Var,
1279 DIE &VariableDie) {
David Blaikie02a63332014-11-02 07:06:51 +00001280 StringRef Name = Var.getName();
1281 if (!Name.empty())
1282 addString(VariableDie, dwarf::DW_AT_name, Name);
Victor Leschuk3c989982016-10-26 11:59:03 +00001283 const auto *DIVar = Var.getVariable();
1284 if (DIVar)
1285 if (uint32_t AlignInBytes = DIVar->getAlignInBytes())
1286 addUInt(VariableDie, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
1287 AlignInBytes);
1288
1289 addSourceLine(VariableDie, DIVar);
David Blaikie02a63332014-11-02 07:06:51 +00001290 addType(VariableDie, Var.getType());
1291 if (Var.isArtificial())
1292 addFlag(VariableDie, dwarf::DW_AT_artificial);
1293}
David Blaikie97802082014-11-02 07:11:55 +00001294
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001295void DwarfCompileUnit::applyLabelAttributes(const DbgLabel &Label,
1296 DIE &LabelDie) {
1297 StringRef Name = Label.getName();
1298 if (!Name.empty())
1299 addString(LabelDie, dwarf::DW_AT_name, Name);
1300 const auto *DILabel = Label.getLabel();
1301 addSourceLine(LabelDie, DILabel);
1302}
1303
David Blaikie97802082014-11-02 07:11:55 +00001304/// Add a Dwarf expression attribute data and value.
1305void DwarfCompileUnit::addExpr(DIELoc &Die, dwarf::Form Form,
1306 const MCExpr *Expr) {
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +00001307 Die.addValue(DIEValueAllocator, (dwarf::Attribute)0, Form, DIEExpr(Expr));
David Blaikie97802082014-11-02 07:11:55 +00001308}
David Blaikie3363a572014-11-02 08:09:09 +00001309
Duncan P. N. Exon Smith2fbe1352015-04-20 22:10:08 +00001310void DwarfCompileUnit::applySubprogramAttributesToDefinition(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001311 const DISubprogram *SP, DIE &SPDie) {
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +00001312 auto *SPDecl = SP->getDeclaration();
Fangrui Songda82ce92019-05-07 02:06:37 +00001313 auto *Context = SPDecl ? SPDecl->getScope() : SP->getScope();
David Blaikie3a443c22014-11-04 22:12:25 +00001314 applySubprogramAttributes(SP, SPDie, includeMinimalInlineScopes());
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +00001315 addGlobalName(SP->getName(), SPDie, Context);
David Blaikie3363a572014-11-02 08:09:09 +00001316}
David Blaikiecafd9622014-11-02 08:51:37 +00001317
1318bool DwarfCompileUnit::isDwoUnit() const {
1319 return DD->useSplitDwarf() && Skeleton;
1320}
David Blaikie3a443c22014-11-04 22:12:25 +00001321
David Blaikie832c7d92019-04-24 18:09:44 +00001322void DwarfCompileUnit::finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) {
1323 constructTypeDIE(D, CTy);
1324}
1325
David Blaikie3a443c22014-11-04 22:12:25 +00001326bool DwarfCompileUnit::includeMinimalInlineScopes() const {
Adrian Prantlb939a252016-03-31 23:56:58 +00001327 return getCUNode()->getEmissionKind() == DICompileUnit::LineTablesOnly ||
David Blaikie3a443c22014-11-04 22:12:25 +00001328 (DD->useSplitDwarf() && !Skeleton);
1329}
David Blaikie61c127c2018-12-14 22:34:03 +00001330
1331void DwarfCompileUnit::addAddrTableBase() {
1332 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
1333 MCSymbol *Label = DD->getAddressPool().getLabel();
1334 addSectionLabel(getUnitDie(),
1335 getDwarfVersion() >= 5 ? dwarf::DW_AT_addr_base
1336 : dwarf::DW_AT_GNU_addr_base,
1337 Label, TLOF.getDwarfAddrSection()->getBeginSymbol());
1338}
Markus Lavinb86ce212019-03-19 13:16:28 +00001339
1340void DwarfCompileUnit::addBaseTypeRef(DIEValueList &Die, int64_t Idx) {
1341 Die.addValue(DIEValueAllocator, (dwarf::Attribute)0, dwarf::DW_FORM_udata,
1342 new (DIEValueAllocator) DIEBaseTypeRef(this, Idx));
1343}
1344
1345void DwarfCompileUnit::createBaseTypeDIEs() {
1346 // Insert the base_type DIEs directly after the CU so that their offsets will
1347 // fit in the fixed size ULEB128 used inside the location expressions.
1348 // Maintain order by iterating backwards and inserting to the front of CU
1349 // child list.
1350 for (auto &Btr : reverse(ExprRefedBaseTypes)) {
1351 DIE &Die = getUnitDie().addChildFront(
1352 DIE::get(DIEValueAllocator, dwarf::DW_TAG_base_type));
1353 SmallString<32> Str;
1354 addString(Die, dwarf::DW_AT_name,
1355 Twine(dwarf::AttributeEncodingString(Btr.Encoding) +
1356 "_" + Twine(Btr.BitSize)).toStringRef(Str));
1357 addUInt(Die, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, Btr.Encoding);
1358 addUInt(Die, dwarf::DW_AT_byte_size, None, Btr.BitSize / 8);
1359
1360 Btr.Die = &Die;
1361 }
1362}