blob: 2638c25daa4f2a6f0a8f1e5b35f48079f95e4fcd [file] [log] [blame]
David Blaikie37c52312014-10-04 15:49:50 +00001#include "DwarfCompileUnit.h"
Adrian Prantl7813d9c2015-01-14 01:01:30 +00002#include "DwarfExpression.h"
David Blaikiecda2aa82014-10-04 16:24:00 +00003#include "llvm/CodeGen/MachineFunction.h"
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00004#include "llvm/IR/Constants.h"
David Blaikie37c52312014-10-04 15:49:50 +00005#include "llvm/IR/DataLayout.h"
6#include "llvm/IR/GlobalValue.h"
7#include "llvm/IR/GlobalVariable.h"
8#include "llvm/IR/Instruction.h"
9#include "llvm/MC/MCAsmInfo.h"
10#include "llvm/MC/MCStreamer.h"
David Blaikieee7df552014-10-09 17:56:36 +000011#include "llvm/Target/TargetFrameLowering.h"
David Blaikie37c52312014-10-04 15:49:50 +000012#include "llvm/Target/TargetLoweringObjectFile.h"
David Blaikiecda2aa82014-10-04 16:24:00 +000013#include "llvm/Target/TargetMachine.h"
David Blaikiecda2aa82014-10-04 16:24:00 +000014#include "llvm/Target/TargetRegisterInfo.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000015#include "llvm/Target/TargetSubtargetInfo.h"
David Blaikie37c52312014-10-04 15:49:50 +000016
17namespace llvm {
18
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000019DwarfCompileUnit::DwarfCompileUnit(unsigned UID, const DICompileUnit *Node,
David Blaikie37c52312014-10-04 15:49:50 +000020 AsmPrinter *A, DwarfDebug *DW,
21 DwarfFile *DWU)
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000022 : DwarfUnit(dwarf::DW_TAG_compile_unit, Node, A, DW, DWU), UniqueID(UID),
Rafael Espindola063d7252015-03-10 16:58:10 +000023 Skeleton(nullptr), BaseAddress(nullptr) {
David Blaikie37c52312014-10-04 15:49:50 +000024 insertDIE(Node, &getUnitDie());
Amjad Aboud8bbce8a2016-02-01 14:09:41 +000025 MacroLabelBegin = Asm->createTempSymbol("cu_macro_begin");
David Blaikie37c52312014-10-04 15:49:50 +000026}
27
28/// addLabelAddress - Add a dwarf label attribute data and value using
29/// DW_FORM_addr or DW_FORM_GNU_addr_index.
30///
31void DwarfCompileUnit::addLabelAddress(DIE &Die, dwarf::Attribute Attribute,
32 const MCSymbol *Label) {
33
34 // Don't use the address pool in non-fission or in the skeleton unit itself.
35 // FIXME: Once GDB supports this, it's probably worthwhile using the address
36 // pool from the skeleton - maybe even in non-fission (possibly fewer
37 // relocations by sharing them in the pool, but we have other ideas about how
38 // to reduce the number of relocations as well/instead).
39 if (!DD->useSplitDwarf() || !Skeleton)
40 return addLocalLabelAddress(Die, Attribute, Label);
41
42 if (Label)
43 DD->addArangeLabel(SymbolCU(this, Label));
44
45 unsigned idx = DD->getAddressPool().getIndex(Label);
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +000046 Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_GNU_addr_index,
47 DIEInteger(idx));
David Blaikie37c52312014-10-04 15:49:50 +000048}
49
50void DwarfCompileUnit::addLocalLabelAddress(DIE &Die,
51 dwarf::Attribute Attribute,
52 const MCSymbol *Label) {
53 if (Label)
54 DD->addArangeLabel(SymbolCU(this, Label));
55
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +000056 if (Label)
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +000057 Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_addr,
58 DIELabel(Label));
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +000059 else
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +000060 Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_addr,
61 DIEInteger(0));
David Blaikie37c52312014-10-04 15:49:50 +000062}
63
David Blaikie89452192014-10-04 16:00:26 +000064unsigned DwarfCompileUnit::getOrCreateSourceID(StringRef FileName,
65 StringRef DirName) {
David Blaikie37c52312014-10-04 15:49:50 +000066 // If we print assembly, we can't separate .file entries according to
67 // compile units. Thus all files will belong to the default compile unit.
68
69 // FIXME: add a better feature test than hasRawTextSupport. Even better,
70 // extend .file to support this.
Lang Hames9ff69c82015-04-24 19:11:51 +000071 return Asm->OutStreamer->EmitDwarfFileDirective(
David Blaikie37c52312014-10-04 15:49:50 +000072 0, DirName, FileName,
Lang Hames9ff69c82015-04-24 19:11:51 +000073 Asm->OutStreamer->hasRawTextSupport() ? 0 : getUniqueID());
David Blaikie37c52312014-10-04 15:49:50 +000074}
75
76// Return const expression if value is a GEP to access merged global
77// constant. e.g.
78// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
79static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
80 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
81 if (!CE || CE->getNumOperands() != 3 ||
82 CE->getOpcode() != Instruction::GetElementPtr)
83 return nullptr;
84
85 // First operand points to a global struct.
86 Value *Ptr = CE->getOperand(0);
Manuel Jacob5f6eaac2016-01-16 20:30:46 +000087 GlobalValue *GV = dyn_cast<GlobalValue>(Ptr);
88 if (!GV || !isa<StructType>(GV->getValueType()))
David Blaikie37c52312014-10-04 15:49:50 +000089 return nullptr;
90
91 // Second operand is zero.
92 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
93 if (!CI || !CI->isZero())
94 return nullptr;
95
96 // Third operand is offset.
97 if (!isa<ConstantInt>(CE->getOperand(2)))
98 return nullptr;
99
100 return CE;
101}
102
103/// getOrCreateGlobalVariableDIE - get or create global variable DIE.
Duncan P. N. Exon Smith60635e32015-04-21 18:44:06 +0000104DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE(
David Blaikied51dea62015-07-01 18:07:16 +0000105 const DIGlobalVariable *GV) {
David Blaikie37c52312014-10-04 15:49:50 +0000106 // Check for pre-existence.
107 if (DIE *Die = getDIE(GV))
108 return Die;
109
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000110 assert(GV);
David Blaikie37c52312014-10-04 15:49:50 +0000111
Duncan P. N. Exon Smithbe9e4fe2015-04-20 18:32:29 +0000112 auto *GVContext = GV->getScope();
113 auto *GTy = DD->resolve(GV->getType());
David Blaikie37c52312014-10-04 15:49:50 +0000114
David Blaikie49cfc8c2014-10-23 19:12:43 +0000115 // Construct the context before querying for the existence of the DIE in
116 // case such construction creates the DIE.
Amjad Aboud6ff7e102016-03-24 13:30:16 +0000117 // For Local Scope, do not construct context DIE.
118 bool IsLocalScope = GVContext && isa<DILocalScope>(GVContext);
119 DIE *ContextDIE = IsLocalScope ? nullptr : getOrCreateContextDIE(GVContext);
120 assert(ContextDIE || IsLocalScope);
David Blaikie49cfc8c2014-10-23 19:12:43 +0000121
Amjad Aboud6ff7e102016-03-24 13:30:16 +0000122 // Create new global variable and add to map.
123 DIE *VariableDIE = IsLocalScope
124 ? createDIE(GV->getTag(), GV)
125 : &createAndAddDIE(GV->getTag(), *ContextDIE, GV);
126
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000127 DIScope *DeclContext;
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000128 if (auto *SDMDecl = GV->getStaticDataMemberDeclaration()) {
129 DeclContext = resolve(SDMDecl->getScope());
130 assert(SDMDecl->isStaticMember() && "Expected static member decl");
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000131 assert(GV->isDefinition());
David Blaikie37c52312014-10-04 15:49:50 +0000132 // We need the declaration DIE that is in the static member's class.
David Blaikie49cfc8c2014-10-23 19:12:43 +0000133 DIE *VariableSpecDIE = getOrCreateStaticMemberDIE(SDMDecl);
134 addDIEEntry(*VariableDIE, dwarf::DW_AT_specification, *VariableSpecDIE);
135 } else {
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000136 DeclContext = GV->getScope();
David Blaikie37c52312014-10-04 15:49:50 +0000137 // Add name and type.
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000138 addString(*VariableDIE, dwarf::DW_AT_name, GV->getDisplayName());
David Blaikie37c52312014-10-04 15:49:50 +0000139 addType(*VariableDIE, GTy);
140
141 // Add scoping info.
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000142 if (!GV->isLocalToUnit())
David Blaikie37c52312014-10-04 15:49:50 +0000143 addFlag(*VariableDIE, dwarf::DW_AT_external);
144
145 // Add line number info.
146 addSourceLine(*VariableDIE, GV);
147 }
148
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000149 if (!GV->isDefinition())
David Blaikie49cfc8c2014-10-23 19:12:43 +0000150 addFlag(*VariableDIE, dwarf::DW_AT_declaration);
David Blaikie877354a2015-04-14 18:08:25 +0000151 else
152 addGlobalName(GV->getName(), *VariableDIE, DeclContext);
David Blaikie49cfc8c2014-10-23 19:12:43 +0000153
David Blaikie37c52312014-10-04 15:49:50 +0000154 // Add location.
155 bool addToAccelTable = false;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000156 if (auto *Global = dyn_cast_or_null<GlobalVariable>(GV->getVariable())) {
David Blaikie37c52312014-10-04 15:49:50 +0000157 addToAccelTable = true;
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000158 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
Duncan P. N. Exon Smithcca5f682015-04-13 20:39:25 +0000159 const MCSymbol *Sym = Asm->getSymbol(Global);
160 if (Global->isThreadLocal()) {
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +0000161 if (Asm->TM.Options.EmulatedTLS) {
162 // TODO: add debug info for emulated thread local mode.
David Blaikie37c52312014-10-04 15:49:50 +0000163 } else {
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +0000164 // FIXME: Make this work with -gsplit-dwarf.
165 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
166 assert((PointerSize == 4 || PointerSize == 8) &&
167 "Add support for other sizes if necessary");
168 // Based on GCC's support for TLS:
169 if (!DD->useSplitDwarf()) {
170 // 1) Start with a constNu of the appropriate pointer size
Dehao Chen54511352015-11-11 18:09:47 +0000171 addUInt(*Loc, dwarf::DW_FORM_data1, PointerSize == 4
172 ? dwarf::DW_OP_const4u
173 : dwarf::DW_OP_const8u);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +0000174 // 2) containing the (relocated) offset of the TLS variable
175 // within the module's TLS block.
176 addExpr(*Loc, dwarf::DW_FORM_udata,
177 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym));
178 } else {
179 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
180 addUInt(*Loc, dwarf::DW_FORM_udata,
181 DD->getAddressPool().getIndex(Sym, /* TLS */ true));
182 }
183 // 3) followed by an OP to make the debugger do a TLS lookup.
184 addUInt(*Loc, dwarf::DW_FORM_data1,
185 DD->useGNUTLSOpcode() ? dwarf::DW_OP_GNU_push_tls_address
186 : dwarf::DW_OP_form_tls_address);
David Blaikie37c52312014-10-04 15:49:50 +0000187 }
David Blaikie37c52312014-10-04 15:49:50 +0000188 } else {
189 DD->addArangeLabel(SymbolCU(this, Sym));
190 addOpAddress(*Loc, Sym);
191 }
David Blaikie49cfc8c2014-10-23 19:12:43 +0000192
193 addBlock(*VariableDIE, dwarf::DW_AT_location, Loc);
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000194 addLinkageName(*VariableDIE, GV->getLinkageName());
David Blaikie37c52312014-10-04 15:49:50 +0000195 } else if (const ConstantInt *CI =
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000196 dyn_cast_or_null<ConstantInt>(GV->getVariable())) {
David Blaikie49cfc8c2014-10-23 19:12:43 +0000197 addConstantValue(*VariableDIE, CI, GTy);
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000198 } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getVariable())) {
David Blaikie37c52312014-10-04 15:49:50 +0000199 addToAccelTable = true;
200 // GV is a merged global.
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000201 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
Eduard Burtescu68e7f492016-01-22 03:08:27 +0000202 auto *Ptr = cast<GlobalValue>(CE->getOperand(0));
203 MCSymbol *Sym = Asm->getSymbol(Ptr);
David Blaikie37c52312014-10-04 15:49:50 +0000204 DD->addArangeLabel(SymbolCU(this, Sym));
205 addOpAddress(*Loc, Sym);
206 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
207 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
208 addUInt(*Loc, dwarf::DW_FORM_udata,
Eduard Burtescu68e7f492016-01-22 03:08:27 +0000209 Asm->getDataLayout().getIndexedOffsetInType(Ptr->getValueType(), Idx));
David Blaikie37c52312014-10-04 15:49:50 +0000210 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
211 addBlock(*VariableDIE, dwarf::DW_AT_location, Loc);
212 }
213
David Blaikie37c52312014-10-04 15:49:50 +0000214 if (addToAccelTable) {
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000215 DD->addAccelName(GV->getName(), *VariableDIE);
David Blaikie37c52312014-10-04 15:49:50 +0000216
217 // If the linkage name is different than the name, go ahead and output
218 // that as well into the name table.
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000219 if (GV->getLinkageName() != "" && GV->getName() != GV->getLinkageName())
220 DD->addAccelName(GV->getLinkageName(), *VariableDIE);
David Blaikie37c52312014-10-04 15:49:50 +0000221 }
222
David Blaikie49cfc8c2014-10-23 19:12:43 +0000223 return VariableDIE;
David Blaikie37c52312014-10-04 15:49:50 +0000224}
225
226void DwarfCompileUnit::addRange(RangeSpan Range) {
227 bool SameAsPrevCU = this == DD->getPrevCU();
228 DD->setPrevCU(this);
229 // If we have no current ranges just add the range and return, otherwise,
230 // check the current section and CU against the previous section and CU we
231 // emitted into and the subprogram was contained within. If these are the
232 // same then extend our current range, otherwise add this as a new range.
233 if (CURanges.empty() || !SameAsPrevCU ||
234 (&CURanges.back().getEnd()->getSection() !=
235 &Range.getEnd()->getSection())) {
236 CURanges.push_back(Range);
237 return;
238 }
239
240 CURanges.back().setEnd(Range.getEnd());
241}
242
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000243DIE::value_iterator
244DwarfCompileUnit::addSectionLabel(DIE &Die, dwarf::Attribute Attribute,
245 const MCSymbol *Label, const MCSymbol *Sec) {
David Blaikie6c0ee4e2014-10-08 22:46:27 +0000246 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000247 return addLabel(Die, Attribute,
248 DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
249 : dwarf::DW_FORM_data4,
250 Label);
251 return addSectionDelta(Die, Attribute, Label, Sec);
David Blaikie6c0ee4e2014-10-08 22:46:27 +0000252}
253
Rafael Espindola063d7252015-03-10 16:58:10 +0000254void DwarfCompileUnit::initStmtList() {
David Blaikie37c52312014-10-04 15:49:50 +0000255 // Define start line table label for each Compile Unit.
256 MCSymbol *LineTableStartSym =
Lang Hames9ff69c82015-04-24 19:11:51 +0000257 Asm->OutStreamer->getDwarfLineTableSymbol(getUniqueID());
David Blaikie37c52312014-10-04 15:49:50 +0000258
David Blaikie37c52312014-10-04 15:49:50 +0000259 // DW_AT_stmt_list is a offset of line number information for this
260 // compile unit in debug_line section. For split dwarf this is
261 // left in the skeleton CU and so not included.
262 // The line table entries are not always emitted in assembly, so it
263 // is not okay to use line_table_start here.
Rafael Espindola063d7252015-03-10 16:58:10 +0000264 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000265 StmtListValue =
266 addSectionLabel(UnitDie, dwarf::DW_AT_stmt_list, LineTableStartSym,
267 TLOF.getDwarfLineSection()->getBeginSymbol());
David Blaikie37c52312014-10-04 15:49:50 +0000268}
269
270void DwarfCompileUnit::applyStmtList(DIE &D) {
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000271 D.addValue(DIEValueAllocator, *StmtListValue);
David Blaikie37c52312014-10-04 15:49:50 +0000272}
273
David Blaikie14499a72014-10-04 15:58:47 +0000274void DwarfCompileUnit::attachLowHighPC(DIE &D, const MCSymbol *Begin,
275 const MCSymbol *End) {
276 assert(Begin && "Begin label should not be null!");
277 assert(End && "End label should not be null!");
278 assert(Begin->isDefined() && "Invalid starting label");
279 assert(End->isDefined() && "Invalid end label");
280
281 addLabelAddress(D, dwarf::DW_AT_low_pc, Begin);
282 if (DD->getDwarfVersion() < 4)
283 addLabelAddress(D, dwarf::DW_AT_high_pc, End);
284 else
285 addLabelDelta(D, dwarf::DW_AT_high_pc, End, Begin);
286}
287
David Blaikiecda2aa82014-10-04 16:24:00 +0000288// Find DIE for the given subprogram and attach appropriate DW_AT_low_pc
289// and DW_AT_high_pc attributes. If there are global variables in this
290// scope then create and insert DIEs for these variables.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000291DIE &DwarfCompileUnit::updateSubprogramScopeDIE(const DISubprogram *SP) {
David Blaikie3a443c22014-11-04 22:12:25 +0000292 DIE *SPDie = getOrCreateSubprogramDIE(SP, includeMinimalInlineScopes());
David Blaikiecda2aa82014-10-04 16:24:00 +0000293
Rafael Espindola07c03d32015-03-05 02:05:42 +0000294 attachLowHighPC(*SPDie, Asm->getFunctionBegin(), Asm->getFunctionEnd());
David Blaikiecda2aa82014-10-04 16:24:00 +0000295 if (!DD->getCurrentFunction()->getTarget().Options.DisableFramePointerElim(
296 *DD->getCurrentFunction()))
297 addFlag(*SPDie, dwarf::DW_AT_APPLE_omit_frame_ptr);
298
299 // Only include DW_AT_frame_base in full debug info
David Blaikie3a443c22014-11-04 22:12:25 +0000300 if (!includeMinimalInlineScopes()) {
Eric Christopherd4e723f2015-02-20 22:36:11 +0000301 const TargetRegisterInfo *RI = Asm->MF->getSubtarget().getRegisterInfo();
David Blaikiecda2aa82014-10-04 16:24:00 +0000302 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Adrian Prantl8efadbf2015-01-14 00:15:16 +0000303 if (RI->isPhysicalRegister(Location.getReg()))
304 addAddress(*SPDie, dwarf::DW_AT_frame_base, Location);
David Blaikiecda2aa82014-10-04 16:24:00 +0000305 }
306
307 // Add name to the name table, we do this here because we're guaranteed
308 // to have concrete versions of our DW_TAG_subprogram nodes.
309 DD->addSubprogramNames(SP, *SPDie);
310
311 return *SPDie;
312}
313
David Blaikie9c65b132014-10-08 22:20:02 +0000314// Construct a DIE for this scope.
315void DwarfCompileUnit::constructScopeDIE(
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000316 LexicalScope *Scope, SmallVectorImpl<DIE *> &FinalChildren) {
David Blaikie9c65b132014-10-08 22:20:02 +0000317 if (!Scope || !Scope->getScopeNode())
318 return;
319
Duncan P. N. Exon Smithbe9e4fe2015-04-20 18:32:29 +0000320 auto *DS = Scope->getScopeNode();
David Blaikie9c65b132014-10-08 22:20:02 +0000321
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000322 assert((Scope->getInlinedAt() || !isa<DISubprogram>(DS)) &&
David Blaikie9c65b132014-10-08 22:20:02 +0000323 "Only handle inlined subprograms here, use "
324 "constructSubprogramScopeDIE for non-inlined "
325 "subprograms");
326
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000327 SmallVector<DIE *, 8> Children;
David Blaikie9c65b132014-10-08 22:20:02 +0000328
329 // We try to create the scope DIE first, then the children DIEs. This will
330 // avoid creating un-used children then removing them later when we find out
331 // the scope DIE is null.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000332 DIE *ScopeDIE;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000333 if (Scope->getParent() && isa<DISubprogram>(DS)) {
David Blaikie01b48a82014-10-09 16:50:53 +0000334 ScopeDIE = constructInlinedScopeDIE(Scope);
David Blaikie9c65b132014-10-08 22:20:02 +0000335 if (!ScopeDIE)
336 return;
337 // We create children when the scope DIE is not null.
David Blaikie8b2fdb82014-10-09 18:24:28 +0000338 createScopeChildrenDIE(Scope, Children);
David Blaikie9c65b132014-10-08 22:20:02 +0000339 } else {
340 // Early exit when we know the scope DIE is going to be null.
341 if (DD->isLexicalScopeDIENull(Scope))
342 return;
343
Amjad Aboud6ff7e102016-03-24 13:30:16 +0000344 bool HasNonScopeChildren;
David Blaikie9c65b132014-10-08 22:20:02 +0000345
346 // We create children here when we know the scope DIE is not going to be
347 // null and the children will be added to the scope DIE.
Amjad Aboud6ff7e102016-03-24 13:30:16 +0000348 createScopeChildrenDIE(Scope, Children, &HasNonScopeChildren);
David Blaikie3a443c22014-11-04 22:12:25 +0000349
David Blaikie9c65b132014-10-08 22:20:02 +0000350 // If there are only other scopes as children, put them directly in the
351 // parent instead, as this scope would serve no purpose.
Amjad Aboud6ff7e102016-03-24 13:30:16 +0000352 if (!HasNonScopeChildren) {
David Blaikie9c65b132014-10-08 22:20:02 +0000353 FinalChildren.insert(FinalChildren.end(),
354 std::make_move_iterator(Children.begin()),
355 std::make_move_iterator(Children.end()));
356 return;
357 }
David Blaikie0fbf8bd2014-10-09 17:08:42 +0000358 ScopeDIE = constructLexicalScopeDIE(Scope);
David Blaikie9c65b132014-10-08 22:20:02 +0000359 assert(ScopeDIE && "Scope DIE should not be null.");
360 }
361
362 // Add children
363 for (auto &I : Children)
364 ScopeDIE->addChild(std::move(I));
365
366 FinalChildren.push_back(std::move(ScopeDIE));
Amjad Aboud6ff7e102016-03-24 13:30:16 +0000367 addLocalScopeDieToLexicalScope(Scope, ScopeDIE);
David Blaikie9c65b132014-10-08 22:20:02 +0000368}
369
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000370DIE::value_iterator
371DwarfCompileUnit::addSectionDelta(DIE &Die, dwarf::Attribute Attribute,
372 const MCSymbol *Hi, const MCSymbol *Lo) {
373 return Die.addValue(DIEValueAllocator, Attribute,
374 DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
375 : dwarf::DW_FORM_data4,
376 new (DIEValueAllocator) DIEDelta(Hi, Lo));
David Blaikiee5feec52014-10-08 23:30:05 +0000377}
378
David Blaikie5b02a192014-11-03 23:10:59 +0000379void DwarfCompileUnit::addScopeRangeList(DIE &ScopeDIE,
380 SmallVector<RangeSpan, 2> Range) {
Rafael Espindola063d7252015-03-10 16:58:10 +0000381 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
382
David Blaikie52400202014-10-09 00:11:39 +0000383 // Emit offset in .debug_range as a relocatable label. emitDIE will handle
384 // emitting it appropriately.
Rafael Espindola063d7252015-03-10 16:58:10 +0000385 const MCSymbol *RangeSectionSym =
386 TLOF.getDwarfRangesSection()->getBeginSymbol();
David Blaikie52400202014-10-09 00:11:39 +0000387
Rafael Espindola9ab09232015-03-17 20:07:06 +0000388 RangeSpanList List(Asm->createTempSymbol("debug_ranges"), std::move(Range));
David Blaikie5b02a192014-11-03 23:10:59 +0000389
David Blaikie52400202014-10-09 00:11:39 +0000390 // Under fission, ranges are specified by constant offsets relative to the
391 // CU's DW_AT_GNU_ranges_base.
David Blaikie5b02a192014-11-03 23:10:59 +0000392 if (isDwoUnit())
393 addSectionDelta(ScopeDIE, dwarf::DW_AT_ranges, List.getSym(),
394 RangeSectionSym);
David Blaikie52400202014-10-09 00:11:39 +0000395 else
David Blaikie5b02a192014-11-03 23:10:59 +0000396 addSectionLabel(ScopeDIE, dwarf::DW_AT_ranges, List.getSym(),
397 RangeSectionSym);
David Blaikie52400202014-10-09 00:11:39 +0000398
399 // Add the range list to the set of ranges to be emitted.
David Blaikie542616d2014-11-03 21:52:56 +0000400 (Skeleton ? Skeleton : this)->CURangeLists.push_back(std::move(List));
David Blaikie52400202014-10-09 00:11:39 +0000401}
402
David Blaikiede123752014-10-09 00:21:42 +0000403void DwarfCompileUnit::attachRangesOrLowHighPC(
David Blaikie5b02a192014-11-03 23:10:59 +0000404 DIE &Die, SmallVector<RangeSpan, 2> Ranges) {
405 if (Ranges.size() == 1) {
406 const auto &single = Ranges.front();
407 attachLowHighPC(Die, single.getStart(), single.getEnd());
408 } else
409 addScopeRangeList(Die, std::move(Ranges));
410}
411
412void DwarfCompileUnit::attachRangesOrLowHighPC(
David Blaikiede123752014-10-09 00:21:42 +0000413 DIE &Die, const SmallVectorImpl<InsnRange> &Ranges) {
David Blaikie5b02a192014-11-03 23:10:59 +0000414 SmallVector<RangeSpan, 2> List;
415 List.reserve(Ranges.size());
416 for (const InsnRange &R : Ranges)
417 List.push_back(RangeSpan(DD->getLabelBeforeInsn(R.first),
418 DD->getLabelAfterInsn(R.second)));
419 attachRangesOrLowHighPC(Die, std::move(List));
David Blaikiede123752014-10-09 00:21:42 +0000420}
421
David Blaikie01b48a82014-10-09 16:50:53 +0000422// This scope represents inlined body of a function. Construct DIE to
423// represent this concrete inlined copy of the function.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000424DIE *DwarfCompileUnit::constructInlinedScopeDIE(LexicalScope *Scope) {
David Blaikie01b48a82014-10-09 16:50:53 +0000425 assert(Scope->getScopeNode());
Duncan P. N. Exon Smithbe9e4fe2015-04-20 18:32:29 +0000426 auto *DS = Scope->getScopeNode();
427 auto *InlinedSP = getDISubprogram(DS);
David Blaikie01b48a82014-10-09 16:50:53 +0000428 // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram
429 // was inlined from another compile unit.
David Blaikief6dac292014-11-01 17:21:26 +0000430 DIE *OriginDIE = DU->getAbstractSPDies()[InlinedSP];
David Blaikie01b48a82014-10-09 16:50:53 +0000431 assert(OriginDIE && "Unable to find original DIE for an inlined subprogram.");
432
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000433 auto ScopeDIE = DIE::get(DIEValueAllocator, dwarf::DW_TAG_inlined_subroutine);
David Blaikie01b48a82014-10-09 16:50:53 +0000434 addDIEEntry(*ScopeDIE, dwarf::DW_AT_abstract_origin, *OriginDIE);
435
436 attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges());
437
438 // Add the call site information to the DIE.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000439 const DILocation *IA = Scope->getInlinedAt();
David Blaikie01b48a82014-10-09 16:50:53 +0000440 addUInt(*ScopeDIE, dwarf::DW_AT_call_file, None,
Duncan P. N. Exon Smithb7e221b2015-04-14 01:35:55 +0000441 getOrCreateSourceID(IA->getFilename(), IA->getDirectory()));
442 addUInt(*ScopeDIE, dwarf::DW_AT_call_line, None, IA->getLine());
Dehao Chen72fdf442015-11-11 18:08:18 +0000443 if (IA->getDiscriminator())
Dehao Chen54511352015-11-11 18:09:47 +0000444 addUInt(*ScopeDIE, dwarf::DW_AT_GNU_discriminator, None,
445 IA->getDiscriminator());
David Blaikie01b48a82014-10-09 16:50:53 +0000446
447 // Add name to the name table, we do this here because we're guaranteed
448 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
449 DD->addSubprogramNames(InlinedSP, *ScopeDIE);
450
451 return ScopeDIE;
452}
453
David Blaikie0fbf8bd2014-10-09 17:08:42 +0000454// Construct new DW_TAG_lexical_block for this scope and attach
455// DW_AT_low_pc/DW_AT_high_pc labels.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000456DIE *DwarfCompileUnit::constructLexicalScopeDIE(LexicalScope *Scope) {
David Blaikie0fbf8bd2014-10-09 17:08:42 +0000457 if (DD->isLexicalScopeDIENull(Scope))
458 return nullptr;
459
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000460 auto ScopeDIE = DIE::get(DIEValueAllocator, dwarf::DW_TAG_lexical_block);
David Blaikie0fbf8bd2014-10-09 17:08:42 +0000461 if (Scope->isAbstractScope())
462 return ScopeDIE;
463
464 attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges());
465
466 return ScopeDIE;
467}
468
David Blaikieee7df552014-10-09 17:56:36 +0000469/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000470DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV, bool Abstract) {
David Blaikieee7df552014-10-09 17:56:36 +0000471 auto D = constructVariableDIEImpl(DV, Abstract);
472 DV.setDIE(*D);
473 return D;
474}
475
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000476DIE *DwarfCompileUnit::constructVariableDIEImpl(const DbgVariable &DV,
477 bool Abstract) {
David Blaikieee7df552014-10-09 17:56:36 +0000478 // Define variable debug information entry.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000479 auto VariableDie = DIE::get(DIEValueAllocator, DV.getTag());
David Blaikieee7df552014-10-09 17:56:36 +0000480
481 if (Abstract) {
482 applyVariableAttributes(DV, *VariableDie);
483 return VariableDie;
484 }
485
486 // Add variable address.
487
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +0000488 unsigned Offset = DV.getDebugLocListIndex();
David Blaikieee7df552014-10-09 17:56:36 +0000489 if (Offset != ~0U) {
490 addLocationList(*VariableDie, dwarf::DW_AT_location, Offset);
491 return VariableDie;
492 }
493
494 // Check if variable is described by a DBG_VALUE instruction.
495 if (const MachineInstr *DVInsn = DV.getMInsn()) {
496 assert(DVInsn->getNumOperands() == 4);
497 if (DVInsn->getOperand(0).isReg()) {
498 const MachineOperand RegOp = DVInsn->getOperand(0);
499 // If the second operand is an immediate, this is an indirect value.
500 if (DVInsn->getOperand(1).isImm()) {
501 MachineLocation Location(RegOp.getReg(),
502 DVInsn->getOperand(1).getImm());
503 addVariableAddress(DV, *VariableDie, Location);
504 } else if (RegOp.getReg())
505 addVariableAddress(DV, *VariableDie, MachineLocation(RegOp.getReg()));
Adrian Prantl3b89e662016-02-17 22:20:08 +0000506 } else if (DVInsn->getOperand(0).isImm()) {
507 // This variable is described by a single constant.
508 // Check whether it has a DIExpression.
509 auto *Expr = DV.getSingleExpression();
510 if (Expr && Expr->getNumElements()) {
511 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
512 DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
513 // If there is an expression, emit raw unsigned bytes.
514 DwarfExpr.AddUnsignedConstant(DVInsn->getOperand(0).getImm());
515 DwarfExpr.AddExpression(Expr->expr_op_begin(), Expr->expr_op_end());
516 addBlock(*VariableDie, dwarf::DW_AT_location, Loc);
517 } else
518 addConstantValue(*VariableDie, DVInsn->getOperand(0), DV.getType());
519 } else if (DVInsn->getOperand(0).isFPImm())
David Blaikieee7df552014-10-09 17:56:36 +0000520 addConstantFPValue(*VariableDie, DVInsn->getOperand(0));
521 else if (DVInsn->getOperand(0).isCImm())
522 addConstantValue(*VariableDie, DVInsn->getOperand(0).getCImm(),
523 DV.getType());
524
525 return VariableDie;
526 }
527
528 // .. else use frame index.
Duncan P. N. Exon Smithe6cc5312015-06-21 16:50:43 +0000529 if (DV.getFrameIndex().empty())
Adrian Prantlca7e4702015-02-10 23:18:28 +0000530 return VariableDie;
531
532 auto Expr = DV.getExpression().begin();
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000533 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
Adrian Prantlca7e4702015-02-10 23:18:28 +0000534 DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
535 for (auto FI : DV.getFrameIndex()) {
David Blaikieee7df552014-10-09 17:56:36 +0000536 unsigned FrameReg = 0;
Eric Christopherd4e723f2015-02-20 22:36:11 +0000537 const TargetFrameLowering *TFI = Asm->MF->getSubtarget().getFrameLowering();
David Blaikieee7df552014-10-09 17:56:36 +0000538 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
Dehao Chen54511352015-11-11 18:09:47 +0000539 assert(Expr != DV.getExpression().end() && "Wrong number of expressions");
Adrian Prantlca7e4702015-02-10 23:18:28 +0000540 DwarfExpr.AddMachineRegIndirect(FrameReg, Offset);
Duncan P. N. Exon Smith76c91842015-04-07 03:45:57 +0000541 DwarfExpr.AddExpression((*Expr)->expr_op_begin(), (*Expr)->expr_op_end());
Duncan P. N. Exon Smith57bab0b2015-02-17 22:30:56 +0000542 ++Expr;
David Blaikieee7df552014-10-09 17:56:36 +0000543 }
Adrian Prantlca7e4702015-02-10 23:18:28 +0000544 addBlock(*VariableDie, dwarf::DW_AT_location, Loc);
David Blaikieee7df552014-10-09 17:56:36 +0000545
546 return VariableDie;
547}
548
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000549DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV,
550 const LexicalScope &Scope,
551 DIE *&ObjectPointer) {
David Blaikie4a1a44e2014-10-09 17:56:39 +0000552 auto Var = constructVariableDIE(DV, Scope.isAbstractScope());
553 if (DV.isObjectPointer())
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000554 ObjectPointer = Var;
David Blaikie4a1a44e2014-10-09 17:56:39 +0000555 return Var;
556}
557
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000558DIE *DwarfCompileUnit::createScopeChildrenDIE(LexicalScope *Scope,
559 SmallVectorImpl<DIE *> &Children,
Amjad Aboud6ff7e102016-03-24 13:30:16 +0000560 bool *HasNonScopeChildren) {
David Blaikie8b2fdb82014-10-09 18:24:28 +0000561 DIE *ObjectPointer = nullptr;
Amjad Aboud6ff7e102016-03-24 13:30:16 +0000562 bool HasLocalDclDie = false;
563 auto *DS = Scope->getScopeNode();
David Blaikie8b2fdb82014-10-09 18:24:28 +0000564
David Blaikie80e5b1e2014-10-24 17:57:34 +0000565 for (DbgVariable *DV : DU->getScopeVariables().lookup(Scope))
David Blaikie8b2fdb82014-10-09 18:24:28 +0000566 Children.push_back(constructVariableDIE(*DV, *Scope, ObjectPointer));
567
Amjad Aboud6ff7e102016-03-24 13:30:16 +0000568 // Skip local declarations in gmlt-like data.
569 if (!includeMinimalInlineScopes()) {
570 for (const auto *DI : LocalDeclNodes[DS]) {
571 DIE *D = nullptr;
572 if (auto *IE = dyn_cast<DIImportedEntity>(DI))
573 D = getOrCreateImportedEntityDIE(IE);
574 else if (auto *GV = dyn_cast<DIGlobalVariable>(DI))
575 D = getOrCreateGlobalVariableDIE(GV);
576 else if (auto *RT = dyn_cast<DIType>(DI))
577 D = getOrCreateTypeDIE(RT);
578 else
579 llvm_unreachable("Unexpected DI node!");
580 addLocalDclDieToLexicalScope(Scope, D);
581 HasLocalDclDie = true;
582 }
583 }
584
585 if (HasNonScopeChildren)
586 *HasNonScopeChildren = !Children.empty() || HasLocalDclDie;
David Blaikie8b2fdb82014-10-09 18:24:28 +0000587
588 for (LexicalScope *LS : Scope->getChildren())
589 constructScopeDIE(LS, Children);
590
David Blaikie8b2fdb82014-10-09 18:24:28 +0000591 return ObjectPointer;
592}
593
David Blaikie1d072342014-10-09 20:21:36 +0000594void DwarfCompileUnit::constructSubprogramScopeDIE(LexicalScope *Scope) {
595 assert(Scope && Scope->getScopeNode());
596 assert(!Scope->getInlinedAt());
597 assert(!Scope->isAbstractScope());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000598 auto *Sub = cast<DISubprogram>(Scope->getScopeNode());
David Blaikie1d072342014-10-09 20:21:36 +0000599
600 DD->getProcessedSPNodes().insert(Sub);
601
602 DIE &ScopeDIE = updateSubprogramScopeDIE(Sub);
603
David Blaikie1d072342014-10-09 20:21:36 +0000604 // If this is a variadic function, add an unspecified parameter.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000605 DITypeRefArray FnArgs = Sub->getType()->getTypeArray();
David Blaikie1dd573d2014-10-23 22:27:50 +0000606
607 // Collect lexical scope children first.
608 // ObjectPointer might be a local (non-argument) local variable if it's a
609 // block's synthetic this pointer.
610 if (DIE *ObjectPointer = createAndAddScopeChildren(Scope, ScopeDIE))
611 addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer, *ObjectPointer);
612
David Blaikie1d072342014-10-09 20:21:36 +0000613 // If we have a single element of null, it is a function that returns void.
614 // If we have more than one elements and the last one is null, it is a
615 // variadic function.
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000616 if (FnArgs.size() > 1 && !FnArgs[FnArgs.size() - 1] &&
David Blaikie3a443c22014-11-04 22:12:25 +0000617 !includeMinimalInlineScopes())
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000618 ScopeDIE.addChild(
619 DIE::get(DIEValueAllocator, dwarf::DW_TAG_unspecified_parameters));
David Blaikie1d072342014-10-09 20:21:36 +0000620}
621
David Blaikie78b65b62014-10-09 20:26:15 +0000622DIE *DwarfCompileUnit::createAndAddScopeChildren(LexicalScope *Scope,
623 DIE &ScopeDIE) {
624 // We create children when the scope DIE is not null.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000625 SmallVector<DIE *, 8> Children;
David Blaikie78b65b62014-10-09 20:26:15 +0000626 DIE *ObjectPointer = createScopeChildrenDIE(Scope, Children);
627
628 // Add children
629 for (auto &I : Children)
630 ScopeDIE.addChild(std::move(I));
631
Amjad Aboud6ff7e102016-03-24 13:30:16 +0000632 addLocalScopeDieToLexicalScope(Scope, &ScopeDIE);
633
David Blaikie78b65b62014-10-09 20:26:15 +0000634 return ObjectPointer;
635}
636
Dehao Chen54511352015-11-11 18:09:47 +0000637void DwarfCompileUnit::constructAbstractSubprogramScopeDIE(
638 LexicalScope *Scope) {
David Blaikief6dac292014-11-01 17:21:26 +0000639 DIE *&AbsDef = DU->getAbstractSPDies()[Scope->getScopeNode()];
David Blaikie49be5b32014-10-31 21:57:02 +0000640 if (AbsDef)
641 return;
642
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000643 auto *SP = cast<DISubprogram>(Scope->getScopeNode());
David Blaikie58410f22014-10-10 06:39:26 +0000644
645 DIE *ContextDIE;
646
David Blaikie3a443c22014-11-04 22:12:25 +0000647 if (includeMinimalInlineScopes())
648 ContextDIE = &getUnitDie();
David Blaikie58410f22014-10-10 06:39:26 +0000649 // Some of this is duplicated from DwarfUnit::getOrCreateSubprogramDIE, with
Duncan P. N. Exon Smith7c60f202015-04-18 00:35:36 +0000650 // the important distinction that the debug node is not associated with the
651 // DIE (since the debug node will be associated with the concrete DIE, if
David Blaikie58410f22014-10-10 06:39:26 +0000652 // any). It could be refactored to some common utility function.
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000653 else if (auto *SPDecl = SP->getDeclaration()) {
David Blaikie58410f22014-10-10 06:39:26 +0000654 ContextDIE = &getUnitDie();
655 getOrCreateSubprogramDIE(SPDecl);
656 } else
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000657 ContextDIE = getOrCreateContextDIE(resolve(SP->getScope()));
David Blaikie58410f22014-10-10 06:39:26 +0000658
Duncan P. N. Exon Smith7c60f202015-04-18 00:35:36 +0000659 // Passing null as the associated node because the abstract definition
David Blaikie58410f22014-10-10 06:39:26 +0000660 // shouldn't be found by lookup.
Duncan P. N. Exon Smith7c60f202015-04-18 00:35:36 +0000661 AbsDef = &createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, nullptr);
David Blaikie49be5b32014-10-31 21:57:02 +0000662 applySubprogramAttributesToDefinition(SP, *AbsDef);
David Blaikie58410f22014-10-10 06:39:26 +0000663
David Blaikie3a443c22014-11-04 22:12:25 +0000664 if (!includeMinimalInlineScopes())
David Blaikie49be5b32014-10-31 21:57:02 +0000665 addUInt(*AbsDef, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined);
666 if (DIE *ObjectPointer = createAndAddScopeChildren(Scope, *AbsDef))
667 addDIEEntry(*AbsDef, dwarf::DW_AT_object_pointer, *ObjectPointer);
David Blaikie58410f22014-10-10 06:39:26 +0000668}
669
Amjad Aboud6ff7e102016-03-24 13:30:16 +0000670DIE *DwarfCompileUnit::getOrCreateImportedEntityDIE(
671 const DIImportedEntity *Module) {
672 if (DIE *Die = getDIE(Module))
673 return Die;
674
675 return constructImportedEntityDIE(Module);
676}
677
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000678DIE *DwarfCompileUnit::constructImportedEntityDIE(
679 const DIImportedEntity *Module) {
Amjad Aboud6ff7e102016-03-24 13:30:16 +0000680
681 assert(!getDIE(Module));
682
683 DIE *IMDie = createDIE(Module->getTag(), Module);
Frederic Riss987fe222014-10-24 21:31:09 +0000684 DIE *EntityDie;
Duncan P. N. Exon Smithde8e4272015-04-14 01:46:44 +0000685 auto *Entity = resolve(Module->getEntity());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000686 if (auto *NS = dyn_cast<DINamespace>(Entity))
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000687 EntityDie = getOrCreateNameSpace(NS);
Adrian Prantl08a388b2015-06-30 02:13:04 +0000688 else if (auto *M = dyn_cast<DIModule>(Entity))
689 EntityDie = getOrCreateModule(M);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000690 else if (auto *SP = dyn_cast<DISubprogram>(Entity))
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000691 EntityDie = getOrCreateSubprogramDIE(SP);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000692 else if (auto *T = dyn_cast<DIType>(Entity))
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000693 EntityDie = getOrCreateTypeDIE(T);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000694 else if (auto *GV = dyn_cast<DIGlobalVariable>(Entity))
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000695 EntityDie = getOrCreateGlobalVariableDIE(GV);
Frederic Riss987fe222014-10-24 21:31:09 +0000696 else
697 EntityDie = getDIE(Entity);
698 assert(EntityDie);
Duncan P. N. Exon Smithde8e4272015-04-14 01:46:44 +0000699 addSourceLine(*IMDie, Module->getLine(), Module->getScope()->getFilename(),
700 Module->getScope()->getDirectory());
Frederic Riss987fe222014-10-24 21:31:09 +0000701 addDIEEntry(*IMDie, dwarf::DW_AT_import, *EntityDie);
Duncan P. N. Exon Smithde8e4272015-04-14 01:46:44 +0000702 StringRef Name = Module->getName();
Frederic Riss987fe222014-10-24 21:31:09 +0000703 if (!Name.empty())
704 addString(*IMDie, dwarf::DW_AT_name, Name);
705
706 return IMDie;
707}
708
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000709void DwarfCompileUnit::finishSubprogramDefinition(const DISubprogram *SP) {
Amjad Aboud6ff7e102016-03-24 13:30:16 +0000710 if (DIE *D = getDIE(SP)) {
711 if (DIE *AbsSPDIE = DU->getAbstractSPDies().lookup(SP))
David Blaikie4191cbc2014-10-10 06:39:29 +0000712 // If this subprogram has an abstract definition, reference that
713 addDIEEntry(*D, dwarf::DW_AT_abstract_origin, *AbsSPDIE);
Amjad Aboud6ff7e102016-03-24 13:30:16 +0000714 else
David Blaikie4191cbc2014-10-10 06:39:29 +0000715 // And attach the attributes
716 applySubprogramAttributesToDefinition(SP, *D);
717 }
718}
Amjad Aboud6ff7e102016-03-24 13:30:16 +0000719
720void DwarfCompileUnit::finishLocalScopeDefinitions() {
721 for (const auto &I : getLSDieInfoMap()) {
722 auto LSInfo = I.second;
723 // Attach all local dcl DIEs to abstract local scope if available,
724 // otherwise attach it to concrete local scope.
725 DIE *LBDie =
726 LSInfo.AbstractLSDie ? LSInfo.AbstractLSDie : LSInfo.ConcreteLSDie;
727 assert(LBDie || LSInfo.LocalDclDies.empty());
728 for (auto &D : LSInfo.LocalDclDies)
729 LBDie->addChild(std::move(D));
730
731 if (isa<DISubprogram>(I.first))
732 // For function scope there is nothing else to do.
733 // "abstract_origin" dwarf attribute was added somewhere else.
734 continue;
735
736 if (LSInfo.AbstractLSDie) {
737 // Add "abstract_origin" dwarf attribute to concrete local scope pointing
738 // to the corresponding abstract local scope.
739 if (LSInfo.ConcreteLSDie)
740 addDIEEntry(*LSInfo.ConcreteLSDie, dwarf::DW_AT_abstract_origin,
741 *LSInfo.AbstractLSDie);
742 // Add "abstract_origin" dwarf attribute to inline local scope pointing
743 // to the corresponding abstract local scope.
744 for (auto &L : LSInfo.InlineLSDies)
745 addDIEEntry(*L, dwarf::DW_AT_abstract_origin, *LSInfo.AbstractLSDie);
746 }
747 }
748}
749
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000750void DwarfCompileUnit::collectDeadVariables(const DISubprogram *SP) {
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000751 assert(SP && "CU's subprogram list contains a non-subprogram");
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000752 assert(SP->isDefinition() &&
David Blaikie1d96cc22014-10-31 22:30:30 +0000753 "CU's subprogram list contains a subprogram declaration");
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000754 auto Variables = SP->getVariables();
755 if (Variables.size() == 0)
David Blaikie1d96cc22014-10-31 22:30:30 +0000756 return;
757
David Blaikief6dac292014-11-01 17:21:26 +0000758 DIE *SPDIE = DU->getAbstractSPDies().lookup(SP);
David Blaikie1d96cc22014-10-31 22:30:30 +0000759 if (!SPDIE)
Amjad Aboud6ff7e102016-03-24 13:30:16 +0000760 return;
David Blaikie1d96cc22014-10-31 22:30:30 +0000761 assert(SPDIE);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000762 for (const DILocalVariable *DV : Variables) {
Duncan P. N. Exon Smithe6cc5312015-06-21 16:50:43 +0000763 DbgVariable NewVar(DV, /* IA */ nullptr, DD);
David Blaikie1d96cc22014-10-31 22:30:30 +0000764 auto VariableDie = constructVariableDIE(NewVar);
765 applyVariableAttributes(NewVar, *VariableDie);
766 SPDIE->addChild(std::move(VariableDie));
767 }
768}
David Blaikie4191cbc2014-10-10 06:39:29 +0000769
Rafael Espindola063d7252015-03-10 16:58:10 +0000770void DwarfCompileUnit::emitHeader(bool UseOffsets) {
David Blaikieb6726a92014-11-02 02:26:24 +0000771 // Don't bother labeling the .dwo unit, as its offset isn't used.
Rafael Espindola3c311142015-03-10 03:11:11 +0000772 if (!Skeleton) {
Rafael Espindola9ab09232015-03-17 20:07:06 +0000773 LabelBegin = Asm->createTempSymbol("cu_begin");
Lang Hames9ff69c82015-04-24 19:11:51 +0000774 Asm->OutStreamer->EmitLabel(LabelBegin);
Rafael Espindola3c311142015-03-10 03:11:11 +0000775 }
David Blaikieae57e662014-11-01 23:59:23 +0000776
Rafael Espindola063d7252015-03-10 16:58:10 +0000777 DwarfUnit::emitHeader(UseOffsets);
David Blaikieae57e662014-11-01 23:59:23 +0000778}
779
David Blaikie192b45c2014-11-02 06:16:39 +0000780/// addGlobalName - Add a new global name to the compile unit.
781void DwarfCompileUnit::addGlobalName(StringRef Name, DIE &Die,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000782 const DIScope *Context) {
David Blaikie3a443c22014-11-04 22:12:25 +0000783 if (includeMinimalInlineScopes())
David Blaikie192b45c2014-11-02 06:16:39 +0000784 return;
785 std::string FullName = getParentContextString(Context) + Name.str();
786 GlobalNames[FullName] = &Die;
787}
788
789/// Add a new global type to the unit.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000790void DwarfCompileUnit::addGlobalType(const DIType *Ty, const DIE &Die,
791 const DIScope *Context) {
David Blaikie3a443c22014-11-04 22:12:25 +0000792 if (includeMinimalInlineScopes())
David Blaikie192b45c2014-11-02 06:16:39 +0000793 return;
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000794 std::string FullName = getParentContextString(Context) + Ty->getName().str();
David Blaikie192b45c2014-11-02 06:16:39 +0000795 GlobalTypes[FullName] = &Die;
796}
David Blaikieae57e662014-11-01 23:59:23 +0000797
David Blaikie7d48be22014-11-02 06:37:23 +0000798/// addVariableAddress - Add DW_AT_location attribute for a
799/// DbgVariable based on provided MachineLocation.
800void DwarfCompileUnit::addVariableAddress(const DbgVariable &DV, DIE &Die,
801 MachineLocation Location) {
Duncan P. N. Exon Smithe6cc5312015-06-21 16:50:43 +0000802 if (DV.hasComplexAddress())
David Blaikie7d48be22014-11-02 06:37:23 +0000803 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
804 else if (DV.isBlockByrefVariable())
805 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
806 else
Adrian Prantl5883af32015-01-19 17:57:29 +0000807 addAddress(Die, dwarf::DW_AT_location, Location);
David Blaikie7d48be22014-11-02 06:37:23 +0000808}
David Blaikief7435ee2014-11-02 06:46:40 +0000809
810/// Add an address attribute to a die based on the location provided.
811void DwarfCompileUnit::addAddress(DIE &Die, dwarf::Attribute Attribute,
Adrian Prantl5883af32015-01-19 17:57:29 +0000812 const MachineLocation &Location) {
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000813 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
David Blaikief7435ee2014-11-02 06:46:40 +0000814
Adrian Prantlab255fc2014-12-05 01:02:46 +0000815 bool validReg;
Adrian Prantl5883af32015-01-19 17:57:29 +0000816 if (Location.isReg())
Adrian Prantlab255fc2014-12-05 01:02:46 +0000817 validReg = addRegisterOpPiece(*Loc, Location.getReg());
818 else
819 validReg = addRegisterOffset(*Loc, Location.getReg(), Location.getOffset());
820
821 if (!validReg)
822 return;
823
David Blaikief7435ee2014-11-02 06:46:40 +0000824 // Now attach the location information to the DIE.
825 addBlock(Die, Attribute, Loc);
826}
David Blaikie77895fb2014-11-02 06:58:44 +0000827
828/// Start with the address based on the location provided, and generate the
829/// DWARF information necessary to find the actual variable given the extra
830/// address information encoded in the DbgVariable, starting from the starting
831/// location. Add the DWARF information to the die.
832void DwarfCompileUnit::addComplexAddress(const DbgVariable &DV, DIE &Die,
833 dwarf::Attribute Attribute,
834 const MachineLocation &Location) {
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000835 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
Adrian Prantl7813d9c2015-01-14 01:01:30 +0000836 DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
Adrian Prantl6f4746b12016-02-17 22:19:59 +0000837 const DIExpression *Expr = DV.getSingleExpression();
Adrian Prantl40cb8192015-01-25 19:04:08 +0000838 bool ValidReg;
Adrian Prantl7813d9c2015-01-14 01:01:30 +0000839 if (Location.getOffset()) {
Adrian Prantl40cb8192015-01-25 19:04:08 +0000840 ValidReg = DwarfExpr.AddMachineRegIndirect(Location.getReg(),
841 Location.getOffset());
842 if (ValidReg)
Duncan P. N. Exon Smith76c91842015-04-07 03:45:57 +0000843 DwarfExpr.AddExpression(Expr->expr_op_begin(), Expr->expr_op_end());
Adrian Prantl5883af32015-01-19 17:57:29 +0000844 } else
Adrian Prantl40cb8192015-01-25 19:04:08 +0000845 ValidReg = DwarfExpr.AddMachineRegExpression(Expr, Location.getReg());
David Blaikie77895fb2014-11-02 06:58:44 +0000846
847 // Now attach the location information to the DIE.
Adrian Prantl40cb8192015-01-25 19:04:08 +0000848 if (ValidReg)
849 addBlock(Die, Attribute, Loc);
David Blaikie77895fb2014-11-02 06:58:44 +0000850}
David Blaikie4bc08812014-11-02 07:03:19 +0000851
852/// Add a Dwarf loclistptr attribute data and value.
853void DwarfCompileUnit::addLocationList(DIE &Die, dwarf::Attribute Attribute,
854 unsigned Index) {
David Blaikie4bc08812014-11-02 07:03:19 +0000855 dwarf::Form Form = DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
856 : dwarf::DW_FORM_data4;
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000857 Die.addValue(DIEValueAllocator, Attribute, Form, DIELocList(Index));
David Blaikie4bc08812014-11-02 07:03:19 +0000858}
David Blaikie02a63332014-11-02 07:06:51 +0000859
David Blaikie8c485b52014-11-02 07:08:12 +0000860void DwarfCompileUnit::applyVariableAttributes(const DbgVariable &Var,
861 DIE &VariableDie) {
David Blaikie02a63332014-11-02 07:06:51 +0000862 StringRef Name = Var.getName();
863 if (!Name.empty())
864 addString(VariableDie, dwarf::DW_AT_name, Name);
865 addSourceLine(VariableDie, Var.getVariable());
866 addType(VariableDie, Var.getType());
867 if (Var.isArtificial())
868 addFlag(VariableDie, dwarf::DW_AT_artificial);
869}
David Blaikie97802082014-11-02 07:11:55 +0000870
871/// Add a Dwarf expression attribute data and value.
872void DwarfCompileUnit::addExpr(DIELoc &Die, dwarf::Form Form,
873 const MCExpr *Expr) {
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000874 Die.addValue(DIEValueAllocator, (dwarf::Attribute)0, Form, DIEExpr(Expr));
David Blaikie97802082014-11-02 07:11:55 +0000875}
David Blaikie3363a572014-11-02 08:09:09 +0000876
Duncan P. N. Exon Smith2fbe1352015-04-20 22:10:08 +0000877void DwarfCompileUnit::applySubprogramAttributesToDefinition(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000878 const DISubprogram *SP, DIE &SPDie) {
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000879 auto *SPDecl = SP->getDeclaration();
Duncan P. N. Exon Smithbe9e4fe2015-04-20 18:32:29 +0000880 auto *Context = resolve(SPDecl ? SPDecl->getScope() : SP->getScope());
David Blaikie3a443c22014-11-04 22:12:25 +0000881 applySubprogramAttributes(SP, SPDie, includeMinimalInlineScopes());
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000882 addGlobalName(SP->getName(), SPDie, Context);
David Blaikie3363a572014-11-02 08:09:09 +0000883}
David Blaikiecafd9622014-11-02 08:51:37 +0000884
Amjad Aboud6ff7e102016-03-24 13:30:16 +0000885void DwarfCompileUnit::addLocalScopeDieToLexicalScope(LexicalScope *LS,
886 DIE *D) {
887 const DILocalScope *Scope = LS->getScopeNode();
888 assert(!isa<DILexicalBlockFile>(Scope) && "Don't expect Lexical Block File!");
889 auto &LSInfo = getLSDieInfoMap()[Scope];
890 if (LS->isAbstractScope()) {
891 assert(!LSInfo.AbstractLSDie && "Adding abstract LS DIE twice.");
892 LSInfo.AbstractLSDie = D;
893 return;
894 }
895 if (LS->getInlinedAt()) {
896 assert(!LSInfo.InlineLSDies.count(D) && "Adding inline LS DIE twice.");
897 LSInfo.InlineLSDies.insert(D);
898 return;
899 }
900 assert(!LSInfo.ConcreteLSDie && "Adding cocncrete LS DIE twice.");
901 LSInfo.ConcreteLSDie = D;
902 return;
903}
904
905void DwarfCompileUnit::addLocalDclDieToLexicalScope(LexicalScope *LS, DIE *D) {
906 const DILocalScope *Scope = LS->getScopeNode();
907 assert(!isa<DILexicalBlockFile>(Scope) && "Don't expect Lexical Block File!");
908 auto &LSInfo = getLSDieInfoMap()[Scope];
909 LSInfo.LocalDclDies.insert(D);
910 return;
911}
912
David Blaikiecafd9622014-11-02 08:51:37 +0000913bool DwarfCompileUnit::isDwoUnit() const {
914 return DD->useSplitDwarf() && Skeleton;
915}
David Blaikie3a443c22014-11-04 22:12:25 +0000916
917bool DwarfCompileUnit::includeMinimalInlineScopes() const {
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +0000918 return getCUNode()->getEmissionKind() == DIBuilder::LineTablesOnly ||
David Blaikie3a443c22014-11-04 22:12:25 +0000919 (DD->useSplitDwarf() && !Skeleton);
920}
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000921} // end llvm namespace