blob: ff70f6ca0445a7f7469857c314eb5da0d33b813f [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 Aboud72da9392016-04-30 01:44:07 +0000117 DIE *ContextDIE = getOrCreateContextDIE(GVContext);
David Blaikie49cfc8c2014-10-23 19:12:43 +0000118
Amjad Aboud72da9392016-04-30 01:44:07 +0000119 // Add to map.
120 DIE *VariableDIE = &createAndAddDIE(GV->getTag(), *ContextDIE, GV);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000121 DIScope *DeclContext;
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000122 if (auto *SDMDecl = GV->getStaticDataMemberDeclaration()) {
123 DeclContext = resolve(SDMDecl->getScope());
124 assert(SDMDecl->isStaticMember() && "Expected static member decl");
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000125 assert(GV->isDefinition());
David Blaikie37c52312014-10-04 15:49:50 +0000126 // We need the declaration DIE that is in the static member's class.
David Blaikie49cfc8c2014-10-23 19:12:43 +0000127 DIE *VariableSpecDIE = getOrCreateStaticMemberDIE(SDMDecl);
128 addDIEEntry(*VariableDIE, dwarf::DW_AT_specification, *VariableSpecDIE);
129 } else {
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000130 DeclContext = GV->getScope();
David Blaikie37c52312014-10-04 15:49:50 +0000131 // Add name and type.
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000132 addString(*VariableDIE, dwarf::DW_AT_name, GV->getDisplayName());
David Blaikie37c52312014-10-04 15:49:50 +0000133 addType(*VariableDIE, GTy);
134
135 // Add scoping info.
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000136 if (!GV->isLocalToUnit())
David Blaikie37c52312014-10-04 15:49:50 +0000137 addFlag(*VariableDIE, dwarf::DW_AT_external);
138
139 // Add line number info.
140 addSourceLine(*VariableDIE, GV);
141 }
142
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000143 if (!GV->isDefinition())
David Blaikie49cfc8c2014-10-23 19:12:43 +0000144 addFlag(*VariableDIE, dwarf::DW_AT_declaration);
David Blaikie877354a2015-04-14 18:08:25 +0000145 else
146 addGlobalName(GV->getName(), *VariableDIE, DeclContext);
David Blaikie49cfc8c2014-10-23 19:12:43 +0000147
David Blaikie37c52312014-10-04 15:49:50 +0000148 // Add location.
149 bool addToAccelTable = false;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000150 if (auto *Global = dyn_cast_or_null<GlobalVariable>(GV->getVariable())) {
David Blaikie37c52312014-10-04 15:49:50 +0000151 addToAccelTable = true;
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000152 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
Duncan P. N. Exon Smithcca5f682015-04-13 20:39:25 +0000153 const MCSymbol *Sym = Asm->getSymbol(Global);
154 if (Global->isThreadLocal()) {
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +0000155 if (Asm->TM.Options.EmulatedTLS) {
156 // TODO: add debug info for emulated thread local mode.
David Blaikie37c52312014-10-04 15:49:50 +0000157 } else {
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +0000158 // FIXME: Make this work with -gsplit-dwarf.
159 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
160 assert((PointerSize == 4 || PointerSize == 8) &&
161 "Add support for other sizes if necessary");
162 // Based on GCC's support for TLS:
163 if (!DD->useSplitDwarf()) {
164 // 1) Start with a constNu of the appropriate pointer size
Dehao Chen54511352015-11-11 18:09:47 +0000165 addUInt(*Loc, dwarf::DW_FORM_data1, PointerSize == 4
166 ? dwarf::DW_OP_const4u
167 : dwarf::DW_OP_const8u);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +0000168 // 2) containing the (relocated) offset of the TLS variable
169 // within the module's TLS block.
170 addExpr(*Loc, dwarf::DW_FORM_udata,
171 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym));
172 } else {
173 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
174 addUInt(*Loc, dwarf::DW_FORM_udata,
175 DD->getAddressPool().getIndex(Sym, /* TLS */ true));
176 }
177 // 3) followed by an OP to make the debugger do a TLS lookup.
178 addUInt(*Loc, dwarf::DW_FORM_data1,
179 DD->useGNUTLSOpcode() ? dwarf::DW_OP_GNU_push_tls_address
180 : dwarf::DW_OP_form_tls_address);
David Blaikie37c52312014-10-04 15:49:50 +0000181 }
David Blaikie37c52312014-10-04 15:49:50 +0000182 } else {
183 DD->addArangeLabel(SymbolCU(this, Sym));
184 addOpAddress(*Loc, Sym);
185 }
David Blaikie49cfc8c2014-10-23 19:12:43 +0000186
187 addBlock(*VariableDIE, dwarf::DW_AT_location, Loc);
Paul Robinson43d1e452016-04-18 22:41:41 +0000188 if (DD->useAllLinkageNames())
189 addLinkageName(*VariableDIE, GV->getLinkageName());
David Blaikie37c52312014-10-04 15:49:50 +0000190 } else if (const ConstantInt *CI =
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000191 dyn_cast_or_null<ConstantInt>(GV->getVariable())) {
David Blaikie49cfc8c2014-10-23 19:12:43 +0000192 addConstantValue(*VariableDIE, CI, GTy);
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000193 } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getVariable())) {
David Blaikie37c52312014-10-04 15:49:50 +0000194 addToAccelTable = true;
195 // GV is a merged global.
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000196 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
Eduard Burtescu68e7f492016-01-22 03:08:27 +0000197 auto *Ptr = cast<GlobalValue>(CE->getOperand(0));
198 MCSymbol *Sym = Asm->getSymbol(Ptr);
David Blaikie37c52312014-10-04 15:49:50 +0000199 DD->addArangeLabel(SymbolCU(this, Sym));
200 addOpAddress(*Loc, Sym);
201 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
202 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
203 addUInt(*Loc, dwarf::DW_FORM_udata,
Eduard Burtescu68e7f492016-01-22 03:08:27 +0000204 Asm->getDataLayout().getIndexedOffsetInType(Ptr->getValueType(), Idx));
David Blaikie37c52312014-10-04 15:49:50 +0000205 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
206 addBlock(*VariableDIE, dwarf::DW_AT_location, Loc);
207 }
208
David Blaikie37c52312014-10-04 15:49:50 +0000209 if (addToAccelTable) {
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000210 DD->addAccelName(GV->getName(), *VariableDIE);
David Blaikie37c52312014-10-04 15:49:50 +0000211
212 // If the linkage name is different than the name, go ahead and output
213 // that as well into the name table.
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000214 if (GV->getLinkageName() != "" && GV->getName() != GV->getLinkageName())
215 DD->addAccelName(GV->getLinkageName(), *VariableDIE);
David Blaikie37c52312014-10-04 15:49:50 +0000216 }
217
David Blaikie49cfc8c2014-10-23 19:12:43 +0000218 return VariableDIE;
David Blaikie37c52312014-10-04 15:49:50 +0000219}
220
221void DwarfCompileUnit::addRange(RangeSpan Range) {
222 bool SameAsPrevCU = this == DD->getPrevCU();
223 DD->setPrevCU(this);
224 // If we have no current ranges just add the range and return, otherwise,
225 // check the current section and CU against the previous section and CU we
226 // emitted into and the subprogram was contained within. If these are the
227 // same then extend our current range, otherwise add this as a new range.
228 if (CURanges.empty() || !SameAsPrevCU ||
229 (&CURanges.back().getEnd()->getSection() !=
230 &Range.getEnd()->getSection())) {
231 CURanges.push_back(Range);
232 return;
233 }
234
235 CURanges.back().setEnd(Range.getEnd());
236}
237
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000238DIE::value_iterator
239DwarfCompileUnit::addSectionLabel(DIE &Die, dwarf::Attribute Attribute,
240 const MCSymbol *Label, const MCSymbol *Sec) {
David Blaikie6c0ee4e2014-10-08 22:46:27 +0000241 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000242 return addLabel(Die, Attribute,
243 DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
244 : dwarf::DW_FORM_data4,
245 Label);
246 return addSectionDelta(Die, Attribute, Label, Sec);
David Blaikie6c0ee4e2014-10-08 22:46:27 +0000247}
248
Rafael Espindola063d7252015-03-10 16:58:10 +0000249void DwarfCompileUnit::initStmtList() {
David Blaikie37c52312014-10-04 15:49:50 +0000250 // Define start line table label for each Compile Unit.
251 MCSymbol *LineTableStartSym =
Lang Hames9ff69c82015-04-24 19:11:51 +0000252 Asm->OutStreamer->getDwarfLineTableSymbol(getUniqueID());
David Blaikie37c52312014-10-04 15:49:50 +0000253
David Blaikie37c52312014-10-04 15:49:50 +0000254 // DW_AT_stmt_list is a offset of line number information for this
255 // compile unit in debug_line section. For split dwarf this is
256 // left in the skeleton CU and so not included.
257 // The line table entries are not always emitted in assembly, so it
258 // is not okay to use line_table_start here.
Rafael Espindola063d7252015-03-10 16:58:10 +0000259 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000260 StmtListValue =
261 addSectionLabel(UnitDie, dwarf::DW_AT_stmt_list, LineTableStartSym,
262 TLOF.getDwarfLineSection()->getBeginSymbol());
David Blaikie37c52312014-10-04 15:49:50 +0000263}
264
265void DwarfCompileUnit::applyStmtList(DIE &D) {
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000266 D.addValue(DIEValueAllocator, *StmtListValue);
David Blaikie37c52312014-10-04 15:49:50 +0000267}
268
David Blaikie14499a72014-10-04 15:58:47 +0000269void DwarfCompileUnit::attachLowHighPC(DIE &D, const MCSymbol *Begin,
270 const MCSymbol *End) {
271 assert(Begin && "Begin label should not be null!");
272 assert(End && "End label should not be null!");
273 assert(Begin->isDefined() && "Invalid starting label");
274 assert(End->isDefined() && "Invalid end label");
275
276 addLabelAddress(D, dwarf::DW_AT_low_pc, Begin);
277 if (DD->getDwarfVersion() < 4)
278 addLabelAddress(D, dwarf::DW_AT_high_pc, End);
279 else
280 addLabelDelta(D, dwarf::DW_AT_high_pc, End, Begin);
281}
282
David Blaikiecda2aa82014-10-04 16:24:00 +0000283// Find DIE for the given subprogram and attach appropriate DW_AT_low_pc
284// and DW_AT_high_pc attributes. If there are global variables in this
285// scope then create and insert DIEs for these variables.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000286DIE &DwarfCompileUnit::updateSubprogramScopeDIE(const DISubprogram *SP) {
David Blaikie3a443c22014-11-04 22:12:25 +0000287 DIE *SPDie = getOrCreateSubprogramDIE(SP, includeMinimalInlineScopes());
David Blaikiecda2aa82014-10-04 16:24:00 +0000288
Rafael Espindola07c03d32015-03-05 02:05:42 +0000289 attachLowHighPC(*SPDie, Asm->getFunctionBegin(), Asm->getFunctionEnd());
David Blaikiecda2aa82014-10-04 16:24:00 +0000290 if (!DD->getCurrentFunction()->getTarget().Options.DisableFramePointerElim(
291 *DD->getCurrentFunction()))
292 addFlag(*SPDie, dwarf::DW_AT_APPLE_omit_frame_ptr);
293
294 // Only include DW_AT_frame_base in full debug info
David Blaikie3a443c22014-11-04 22:12:25 +0000295 if (!includeMinimalInlineScopes()) {
Eric Christopherd4e723f2015-02-20 22:36:11 +0000296 const TargetRegisterInfo *RI = Asm->MF->getSubtarget().getRegisterInfo();
David Blaikiecda2aa82014-10-04 16:24:00 +0000297 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Adrian Prantl8efadbf2015-01-14 00:15:16 +0000298 if (RI->isPhysicalRegister(Location.getReg()))
299 addAddress(*SPDie, dwarf::DW_AT_frame_base, Location);
David Blaikiecda2aa82014-10-04 16:24:00 +0000300 }
301
302 // Add name to the name table, we do this here because we're guaranteed
303 // to have concrete versions of our DW_TAG_subprogram nodes.
304 DD->addSubprogramNames(SP, *SPDie);
305
306 return *SPDie;
307}
308
David Blaikie9c65b132014-10-08 22:20:02 +0000309// Construct a DIE for this scope.
310void DwarfCompileUnit::constructScopeDIE(
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000311 LexicalScope *Scope, SmallVectorImpl<DIE *> &FinalChildren) {
David Blaikie9c65b132014-10-08 22:20:02 +0000312 if (!Scope || !Scope->getScopeNode())
313 return;
314
Duncan P. N. Exon Smithbe9e4fe2015-04-20 18:32:29 +0000315 auto *DS = Scope->getScopeNode();
David Blaikie9c65b132014-10-08 22:20:02 +0000316
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000317 assert((Scope->getInlinedAt() || !isa<DISubprogram>(DS)) &&
David Blaikie9c65b132014-10-08 22:20:02 +0000318 "Only handle inlined subprograms here, use "
319 "constructSubprogramScopeDIE for non-inlined "
320 "subprograms");
321
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000322 SmallVector<DIE *, 8> Children;
David Blaikie9c65b132014-10-08 22:20:02 +0000323
324 // We try to create the scope DIE first, then the children DIEs. This will
325 // avoid creating un-used children then removing them later when we find out
326 // the scope DIE is null.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000327 DIE *ScopeDIE;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000328 if (Scope->getParent() && isa<DISubprogram>(DS)) {
David Blaikie01b48a82014-10-09 16:50:53 +0000329 ScopeDIE = constructInlinedScopeDIE(Scope);
David Blaikie9c65b132014-10-08 22:20:02 +0000330 if (!ScopeDIE)
331 return;
332 // We create children when the scope DIE is not null.
David Blaikie8b2fdb82014-10-09 18:24:28 +0000333 createScopeChildrenDIE(Scope, Children);
David Blaikie9c65b132014-10-08 22:20:02 +0000334 } else {
335 // Early exit when we know the scope DIE is going to be null.
336 if (DD->isLexicalScopeDIENull(Scope))
337 return;
338
Amjad Aboud72da9392016-04-30 01:44:07 +0000339 unsigned ChildScopeCount;
David Blaikie9c65b132014-10-08 22:20:02 +0000340
341 // We create children here when we know the scope DIE is not going to be
342 // null and the children will be added to the scope DIE.
Amjad Aboud72da9392016-04-30 01:44:07 +0000343 createScopeChildrenDIE(Scope, Children, &ChildScopeCount);
344
345 // Skip imported directives in gmlt-like data.
346 if (!includeMinimalInlineScopes()) {
347 // There is no need to emit empty lexical block DIE.
348 for (const auto *IE : ImportedEntities[DS])
349 Children.push_back(
350 constructImportedEntityDIE(cast<DIImportedEntity>(IE)));
351 }
David Blaikie3a443c22014-11-04 22:12:25 +0000352
David Blaikie9c65b132014-10-08 22:20:02 +0000353 // If there are only other scopes as children, put them directly in the
354 // parent instead, as this scope would serve no purpose.
Amjad Aboud72da9392016-04-30 01:44:07 +0000355 if (Children.size() == ChildScopeCount) {
David Blaikie9c65b132014-10-08 22:20:02 +0000356 FinalChildren.insert(FinalChildren.end(),
357 std::make_move_iterator(Children.begin()),
358 std::make_move_iterator(Children.end()));
359 return;
360 }
David Blaikie0fbf8bd2014-10-09 17:08:42 +0000361 ScopeDIE = constructLexicalScopeDIE(Scope);
David Blaikie9c65b132014-10-08 22:20:02 +0000362 assert(ScopeDIE && "Scope DIE should not be null.");
363 }
364
365 // Add children
366 for (auto &I : Children)
367 ScopeDIE->addChild(std::move(I));
368
369 FinalChildren.push_back(std::move(ScopeDIE));
370}
371
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000372DIE::value_iterator
373DwarfCompileUnit::addSectionDelta(DIE &Die, dwarf::Attribute Attribute,
374 const MCSymbol *Hi, const MCSymbol *Lo) {
375 return Die.addValue(DIEValueAllocator, Attribute,
376 DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
377 : dwarf::DW_FORM_data4,
378 new (DIEValueAllocator) DIEDelta(Hi, Lo));
David Blaikiee5feec52014-10-08 23:30:05 +0000379}
380
David Blaikie5b02a192014-11-03 23:10:59 +0000381void DwarfCompileUnit::addScopeRangeList(DIE &ScopeDIE,
382 SmallVector<RangeSpan, 2> Range) {
Rafael Espindola063d7252015-03-10 16:58:10 +0000383 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
384
David Blaikie52400202014-10-09 00:11:39 +0000385 // Emit offset in .debug_range as a relocatable label. emitDIE will handle
386 // emitting it appropriately.
Rafael Espindola063d7252015-03-10 16:58:10 +0000387 const MCSymbol *RangeSectionSym =
388 TLOF.getDwarfRangesSection()->getBeginSymbol();
David Blaikie52400202014-10-09 00:11:39 +0000389
Rafael Espindola9ab09232015-03-17 20:07:06 +0000390 RangeSpanList List(Asm->createTempSymbol("debug_ranges"), std::move(Range));
David Blaikie5b02a192014-11-03 23:10:59 +0000391
David Blaikie52400202014-10-09 00:11:39 +0000392 // Under fission, ranges are specified by constant offsets relative to the
393 // CU's DW_AT_GNU_ranges_base.
David Blaikie5b02a192014-11-03 23:10:59 +0000394 if (isDwoUnit())
395 addSectionDelta(ScopeDIE, dwarf::DW_AT_ranges, List.getSym(),
396 RangeSectionSym);
David Blaikie52400202014-10-09 00:11:39 +0000397 else
David Blaikie5b02a192014-11-03 23:10:59 +0000398 addSectionLabel(ScopeDIE, dwarf::DW_AT_ranges, List.getSym(),
399 RangeSectionSym);
David Blaikie52400202014-10-09 00:11:39 +0000400
401 // Add the range list to the set of ranges to be emitted.
David Blaikie542616d2014-11-03 21:52:56 +0000402 (Skeleton ? Skeleton : this)->CURangeLists.push_back(std::move(List));
David Blaikie52400202014-10-09 00:11:39 +0000403}
404
David Blaikiede123752014-10-09 00:21:42 +0000405void DwarfCompileUnit::attachRangesOrLowHighPC(
David Blaikie5b02a192014-11-03 23:10:59 +0000406 DIE &Die, SmallVector<RangeSpan, 2> Ranges) {
407 if (Ranges.size() == 1) {
408 const auto &single = Ranges.front();
409 attachLowHighPC(Die, single.getStart(), single.getEnd());
410 } else
411 addScopeRangeList(Die, std::move(Ranges));
412}
413
414void DwarfCompileUnit::attachRangesOrLowHighPC(
David Blaikiede123752014-10-09 00:21:42 +0000415 DIE &Die, const SmallVectorImpl<InsnRange> &Ranges) {
David Blaikie5b02a192014-11-03 23:10:59 +0000416 SmallVector<RangeSpan, 2> List;
417 List.reserve(Ranges.size());
418 for (const InsnRange &R : Ranges)
419 List.push_back(RangeSpan(DD->getLabelBeforeInsn(R.first),
420 DD->getLabelAfterInsn(R.second)));
421 attachRangesOrLowHighPC(Die, std::move(List));
David Blaikiede123752014-10-09 00:21:42 +0000422}
423
David Blaikie01b48a82014-10-09 16:50:53 +0000424// This scope represents inlined body of a function. Construct DIE to
425// represent this concrete inlined copy of the function.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000426DIE *DwarfCompileUnit::constructInlinedScopeDIE(LexicalScope *Scope) {
David Blaikie01b48a82014-10-09 16:50:53 +0000427 assert(Scope->getScopeNode());
Duncan P. N. Exon Smithbe9e4fe2015-04-20 18:32:29 +0000428 auto *DS = Scope->getScopeNode();
429 auto *InlinedSP = getDISubprogram(DS);
David Blaikie01b48a82014-10-09 16:50:53 +0000430 // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram
431 // was inlined from another compile unit.
David Blaikief6dac292014-11-01 17:21:26 +0000432 DIE *OriginDIE = DU->getAbstractSPDies()[InlinedSP];
David Blaikie01b48a82014-10-09 16:50:53 +0000433 assert(OriginDIE && "Unable to find original DIE for an inlined subprogram.");
434
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000435 auto ScopeDIE = DIE::get(DIEValueAllocator, dwarf::DW_TAG_inlined_subroutine);
David Blaikie01b48a82014-10-09 16:50:53 +0000436 addDIEEntry(*ScopeDIE, dwarf::DW_AT_abstract_origin, *OriginDIE);
437
438 attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges());
439
440 // Add the call site information to the DIE.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000441 const DILocation *IA = Scope->getInlinedAt();
David Blaikie01b48a82014-10-09 16:50:53 +0000442 addUInt(*ScopeDIE, dwarf::DW_AT_call_file, None,
Duncan P. N. Exon Smithb7e221b2015-04-14 01:35:55 +0000443 getOrCreateSourceID(IA->getFilename(), IA->getDirectory()));
444 addUInt(*ScopeDIE, dwarf::DW_AT_call_line, None, IA->getLine());
Dehao Chen72fdf442015-11-11 18:08:18 +0000445 if (IA->getDiscriminator())
Dehao Chen54511352015-11-11 18:09:47 +0000446 addUInt(*ScopeDIE, dwarf::DW_AT_GNU_discriminator, None,
447 IA->getDiscriminator());
David Blaikie01b48a82014-10-09 16:50:53 +0000448
449 // Add name to the name table, we do this here because we're guaranteed
450 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
451 DD->addSubprogramNames(InlinedSP, *ScopeDIE);
452
453 return ScopeDIE;
454}
455
David Blaikie0fbf8bd2014-10-09 17:08:42 +0000456// Construct new DW_TAG_lexical_block for this scope and attach
457// DW_AT_low_pc/DW_AT_high_pc labels.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000458DIE *DwarfCompileUnit::constructLexicalScopeDIE(LexicalScope *Scope) {
David Blaikie0fbf8bd2014-10-09 17:08:42 +0000459 if (DD->isLexicalScopeDIENull(Scope))
460 return nullptr;
461
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000462 auto ScopeDIE = DIE::get(DIEValueAllocator, dwarf::DW_TAG_lexical_block);
David Blaikie0fbf8bd2014-10-09 17:08:42 +0000463 if (Scope->isAbstractScope())
464 return ScopeDIE;
465
466 attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges());
467
468 return ScopeDIE;
469}
470
David Blaikieee7df552014-10-09 17:56:36 +0000471/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000472DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV, bool Abstract) {
David Blaikieee7df552014-10-09 17:56:36 +0000473 auto D = constructVariableDIEImpl(DV, Abstract);
474 DV.setDIE(*D);
475 return D;
476}
477
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000478DIE *DwarfCompileUnit::constructVariableDIEImpl(const DbgVariable &DV,
479 bool Abstract) {
David Blaikieee7df552014-10-09 17:56:36 +0000480 // Define variable debug information entry.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000481 auto VariableDie = DIE::get(DIEValueAllocator, DV.getTag());
David Blaikieee7df552014-10-09 17:56:36 +0000482
483 if (Abstract) {
484 applyVariableAttributes(DV, *VariableDie);
485 return VariableDie;
486 }
487
488 // Add variable address.
489
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +0000490 unsigned Offset = DV.getDebugLocListIndex();
David Blaikieee7df552014-10-09 17:56:36 +0000491 if (Offset != ~0U) {
492 addLocationList(*VariableDie, dwarf::DW_AT_location, Offset);
493 return VariableDie;
494 }
495
496 // Check if variable is described by a DBG_VALUE instruction.
497 if (const MachineInstr *DVInsn = DV.getMInsn()) {
498 assert(DVInsn->getNumOperands() == 4);
499 if (DVInsn->getOperand(0).isReg()) {
500 const MachineOperand RegOp = DVInsn->getOperand(0);
501 // If the second operand is an immediate, this is an indirect value.
502 if (DVInsn->getOperand(1).isImm()) {
503 MachineLocation Location(RegOp.getReg(),
504 DVInsn->getOperand(1).getImm());
505 addVariableAddress(DV, *VariableDie, Location);
506 } else if (RegOp.getReg())
507 addVariableAddress(DV, *VariableDie, MachineLocation(RegOp.getReg()));
Adrian Prantl3b89e662016-02-17 22:20:08 +0000508 } else if (DVInsn->getOperand(0).isImm()) {
509 // This variable is described by a single constant.
510 // Check whether it has a DIExpression.
511 auto *Expr = DV.getSingleExpression();
512 if (Expr && Expr->getNumElements()) {
513 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
514 DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
515 // If there is an expression, emit raw unsigned bytes.
516 DwarfExpr.AddUnsignedConstant(DVInsn->getOperand(0).getImm());
517 DwarfExpr.AddExpression(Expr->expr_op_begin(), Expr->expr_op_end());
518 addBlock(*VariableDie, dwarf::DW_AT_location, Loc);
519 } else
520 addConstantValue(*VariableDie, DVInsn->getOperand(0), DV.getType());
521 } else if (DVInsn->getOperand(0).isFPImm())
David Blaikieee7df552014-10-09 17:56:36 +0000522 addConstantFPValue(*VariableDie, DVInsn->getOperand(0));
523 else if (DVInsn->getOperand(0).isCImm())
524 addConstantValue(*VariableDie, DVInsn->getOperand(0).getCImm(),
525 DV.getType());
526
527 return VariableDie;
528 }
529
530 // .. else use frame index.
Duncan P. N. Exon Smithe6cc5312015-06-21 16:50:43 +0000531 if (DV.getFrameIndex().empty())
Adrian Prantlca7e4702015-02-10 23:18:28 +0000532 return VariableDie;
533
534 auto Expr = DV.getExpression().begin();
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000535 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
Adrian Prantlca7e4702015-02-10 23:18:28 +0000536 DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
537 for (auto FI : DV.getFrameIndex()) {
David Blaikieee7df552014-10-09 17:56:36 +0000538 unsigned FrameReg = 0;
Eric Christopherd4e723f2015-02-20 22:36:11 +0000539 const TargetFrameLowering *TFI = Asm->MF->getSubtarget().getFrameLowering();
David Blaikieee7df552014-10-09 17:56:36 +0000540 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
Dehao Chen54511352015-11-11 18:09:47 +0000541 assert(Expr != DV.getExpression().end() && "Wrong number of expressions");
Adrian Prantlca7e4702015-02-10 23:18:28 +0000542 DwarfExpr.AddMachineRegIndirect(FrameReg, Offset);
Duncan P. N. Exon Smith76c91842015-04-07 03:45:57 +0000543 DwarfExpr.AddExpression((*Expr)->expr_op_begin(), (*Expr)->expr_op_end());
Duncan P. N. Exon Smith57bab0b2015-02-17 22:30:56 +0000544 ++Expr;
David Blaikieee7df552014-10-09 17:56:36 +0000545 }
Adrian Prantlca7e4702015-02-10 23:18:28 +0000546 addBlock(*VariableDie, dwarf::DW_AT_location, Loc);
David Blaikieee7df552014-10-09 17:56:36 +0000547
548 return VariableDie;
549}
550
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000551DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV,
552 const LexicalScope &Scope,
553 DIE *&ObjectPointer) {
David Blaikie4a1a44e2014-10-09 17:56:39 +0000554 auto Var = constructVariableDIE(DV, Scope.isAbstractScope());
555 if (DV.isObjectPointer())
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000556 ObjectPointer = Var;
David Blaikie4a1a44e2014-10-09 17:56:39 +0000557 return Var;
558}
559
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000560DIE *DwarfCompileUnit::createScopeChildrenDIE(LexicalScope *Scope,
561 SmallVectorImpl<DIE *> &Children,
Amjad Aboud72da9392016-04-30 01:44:07 +0000562 unsigned *ChildScopeCount) {
David Blaikie8b2fdb82014-10-09 18:24:28 +0000563 DIE *ObjectPointer = nullptr;
564
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 Aboud72da9392016-04-30 01:44:07 +0000568 unsigned ChildCountWithoutScopes = Children.size();
David Blaikie8b2fdb82014-10-09 18:24:28 +0000569
570 for (LexicalScope *LS : Scope->getChildren())
571 constructScopeDIE(LS, Children);
572
Amjad Aboud72da9392016-04-30 01:44:07 +0000573 if (ChildScopeCount)
574 *ChildScopeCount = Children.size() - ChildCountWithoutScopes;
575
David Blaikie8b2fdb82014-10-09 18:24:28 +0000576 return ObjectPointer;
577}
578
David Blaikie1d072342014-10-09 20:21:36 +0000579void DwarfCompileUnit::constructSubprogramScopeDIE(LexicalScope *Scope) {
580 assert(Scope && Scope->getScopeNode());
581 assert(!Scope->getInlinedAt());
582 assert(!Scope->isAbstractScope());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000583 auto *Sub = cast<DISubprogram>(Scope->getScopeNode());
David Blaikie1d072342014-10-09 20:21:36 +0000584
585 DD->getProcessedSPNodes().insert(Sub);
586
587 DIE &ScopeDIE = updateSubprogramScopeDIE(Sub);
588
David Blaikie1d072342014-10-09 20:21:36 +0000589 // If this is a variadic function, add an unspecified parameter.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000590 DITypeRefArray FnArgs = Sub->getType()->getTypeArray();
David Blaikie1dd573d2014-10-23 22:27:50 +0000591
592 // Collect lexical scope children first.
593 // ObjectPointer might be a local (non-argument) local variable if it's a
594 // block's synthetic this pointer.
595 if (DIE *ObjectPointer = createAndAddScopeChildren(Scope, ScopeDIE))
596 addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer, *ObjectPointer);
597
David Blaikie1d072342014-10-09 20:21:36 +0000598 // If we have a single element of null, it is a function that returns void.
599 // If we have more than one elements and the last one is null, it is a
600 // variadic function.
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000601 if (FnArgs.size() > 1 && !FnArgs[FnArgs.size() - 1] &&
David Blaikie3a443c22014-11-04 22:12:25 +0000602 !includeMinimalInlineScopes())
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000603 ScopeDIE.addChild(
604 DIE::get(DIEValueAllocator, dwarf::DW_TAG_unspecified_parameters));
David Blaikie1d072342014-10-09 20:21:36 +0000605}
606
David Blaikie78b65b62014-10-09 20:26:15 +0000607DIE *DwarfCompileUnit::createAndAddScopeChildren(LexicalScope *Scope,
608 DIE &ScopeDIE) {
609 // We create children when the scope DIE is not null.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000610 SmallVector<DIE *, 8> Children;
David Blaikie78b65b62014-10-09 20:26:15 +0000611 DIE *ObjectPointer = createScopeChildrenDIE(Scope, Children);
612
613 // Add children
614 for (auto &I : Children)
615 ScopeDIE.addChild(std::move(I));
616
617 return ObjectPointer;
618}
619
Dehao Chen54511352015-11-11 18:09:47 +0000620void DwarfCompileUnit::constructAbstractSubprogramScopeDIE(
621 LexicalScope *Scope) {
David Blaikief6dac292014-11-01 17:21:26 +0000622 DIE *&AbsDef = DU->getAbstractSPDies()[Scope->getScopeNode()];
David Blaikie49be5b32014-10-31 21:57:02 +0000623 if (AbsDef)
624 return;
625
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000626 auto *SP = cast<DISubprogram>(Scope->getScopeNode());
David Blaikie58410f22014-10-10 06:39:26 +0000627
628 DIE *ContextDIE;
629
David Blaikie3a443c22014-11-04 22:12:25 +0000630 if (includeMinimalInlineScopes())
631 ContextDIE = &getUnitDie();
David Blaikie58410f22014-10-10 06:39:26 +0000632 // Some of this is duplicated from DwarfUnit::getOrCreateSubprogramDIE, with
Duncan P. N. Exon Smith7c60f202015-04-18 00:35:36 +0000633 // the important distinction that the debug node is not associated with the
634 // DIE (since the debug node will be associated with the concrete DIE, if
David Blaikie58410f22014-10-10 06:39:26 +0000635 // any). It could be refactored to some common utility function.
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000636 else if (auto *SPDecl = SP->getDeclaration()) {
David Blaikie58410f22014-10-10 06:39:26 +0000637 ContextDIE = &getUnitDie();
638 getOrCreateSubprogramDIE(SPDecl);
639 } else
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000640 ContextDIE = getOrCreateContextDIE(resolve(SP->getScope()));
David Blaikie58410f22014-10-10 06:39:26 +0000641
Duncan P. N. Exon Smith7c60f202015-04-18 00:35:36 +0000642 // Passing null as the associated node because the abstract definition
David Blaikie58410f22014-10-10 06:39:26 +0000643 // shouldn't be found by lookup.
Duncan P. N. Exon Smith7c60f202015-04-18 00:35:36 +0000644 AbsDef = &createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, nullptr);
David Blaikie49be5b32014-10-31 21:57:02 +0000645 applySubprogramAttributesToDefinition(SP, *AbsDef);
David Blaikie58410f22014-10-10 06:39:26 +0000646
David Blaikie3a443c22014-11-04 22:12:25 +0000647 if (!includeMinimalInlineScopes())
David Blaikie49be5b32014-10-31 21:57:02 +0000648 addUInt(*AbsDef, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined);
649 if (DIE *ObjectPointer = createAndAddScopeChildren(Scope, *AbsDef))
650 addDIEEntry(*AbsDef, dwarf::DW_AT_object_pointer, *ObjectPointer);
David Blaikie58410f22014-10-10 06:39:26 +0000651}
652
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000653DIE *DwarfCompileUnit::constructImportedEntityDIE(
654 const DIImportedEntity *Module) {
Amjad Aboud72da9392016-04-30 01:44:07 +0000655 DIE *IMDie = DIE::get(DIEValueAllocator, (dwarf::Tag)Module->getTag());
656 insertDIE(Module, IMDie);
Frederic Riss987fe222014-10-24 21:31:09 +0000657 DIE *EntityDie;
Duncan P. N. Exon Smithde8e4272015-04-14 01:46:44 +0000658 auto *Entity = resolve(Module->getEntity());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000659 if (auto *NS = dyn_cast<DINamespace>(Entity))
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000660 EntityDie = getOrCreateNameSpace(NS);
Adrian Prantl08a388b2015-06-30 02:13:04 +0000661 else if (auto *M = dyn_cast<DIModule>(Entity))
662 EntityDie = getOrCreateModule(M);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000663 else if (auto *SP = dyn_cast<DISubprogram>(Entity))
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000664 EntityDie = getOrCreateSubprogramDIE(SP);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000665 else if (auto *T = dyn_cast<DIType>(Entity))
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000666 EntityDie = getOrCreateTypeDIE(T);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000667 else if (auto *GV = dyn_cast<DIGlobalVariable>(Entity))
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000668 EntityDie = getOrCreateGlobalVariableDIE(GV);
Frederic Riss987fe222014-10-24 21:31:09 +0000669 else
670 EntityDie = getDIE(Entity);
671 assert(EntityDie);
Duncan P. N. Exon Smithde8e4272015-04-14 01:46:44 +0000672 addSourceLine(*IMDie, Module->getLine(), Module->getScope()->getFilename(),
673 Module->getScope()->getDirectory());
Frederic Riss987fe222014-10-24 21:31:09 +0000674 addDIEEntry(*IMDie, dwarf::DW_AT_import, *EntityDie);
Duncan P. N. Exon Smithde8e4272015-04-14 01:46:44 +0000675 StringRef Name = Module->getName();
Frederic Riss987fe222014-10-24 21:31:09 +0000676 if (!Name.empty())
677 addString(*IMDie, dwarf::DW_AT_name, Name);
678
679 return IMDie;
680}
681
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000682void DwarfCompileUnit::finishSubprogramDefinition(const DISubprogram *SP) {
Amjad Aboud72da9392016-04-30 01:44:07 +0000683 DIE *D = getDIE(SP);
684 if (DIE *AbsSPDIE = DU->getAbstractSPDies().lookup(SP)) {
685 if (D)
David Blaikie4191cbc2014-10-10 06:39:29 +0000686 // If this subprogram has an abstract definition, reference that
687 addDIEEntry(*D, dwarf::DW_AT_abstract_origin, *AbsSPDIE);
Amjad Aboud72da9392016-04-30 01:44:07 +0000688 } else {
689 if (!D && !includeMinimalInlineScopes())
690 // Lazily construct the subprogram if we didn't see either concrete or
691 // inlined versions during codegen. (except in -gmlt ^ where we want
692 // to omit these entirely)
693 D = getOrCreateSubprogramDIE(SP);
694 if (D)
David Blaikie4191cbc2014-10-10 06:39:29 +0000695 // And attach the attributes
696 applySubprogramAttributesToDefinition(SP, *D);
697 }
698}
699
Rafael Espindola063d7252015-03-10 16:58:10 +0000700void DwarfCompileUnit::emitHeader(bool UseOffsets) {
David Blaikieb6726a92014-11-02 02:26:24 +0000701 // Don't bother labeling the .dwo unit, as its offset isn't used.
Rafael Espindola3c311142015-03-10 03:11:11 +0000702 if (!Skeleton) {
Rafael Espindola9ab09232015-03-17 20:07:06 +0000703 LabelBegin = Asm->createTempSymbol("cu_begin");
Lang Hames9ff69c82015-04-24 19:11:51 +0000704 Asm->OutStreamer->EmitLabel(LabelBegin);
Rafael Espindola3c311142015-03-10 03:11:11 +0000705 }
David Blaikieae57e662014-11-01 23:59:23 +0000706
Rafael Espindola063d7252015-03-10 16:58:10 +0000707 DwarfUnit::emitHeader(UseOffsets);
David Blaikieae57e662014-11-01 23:59:23 +0000708}
709
David Blaikie192b45c2014-11-02 06:16:39 +0000710/// addGlobalName - Add a new global name to the compile unit.
711void DwarfCompileUnit::addGlobalName(StringRef Name, DIE &Die,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000712 const DIScope *Context) {
David Blaikie3a443c22014-11-04 22:12:25 +0000713 if (includeMinimalInlineScopes())
David Blaikie192b45c2014-11-02 06:16:39 +0000714 return;
715 std::string FullName = getParentContextString(Context) + Name.str();
716 GlobalNames[FullName] = &Die;
717}
718
719/// Add a new global type to the unit.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000720void DwarfCompileUnit::addGlobalType(const DIType *Ty, const DIE &Die,
721 const DIScope *Context) {
David Blaikie3a443c22014-11-04 22:12:25 +0000722 if (includeMinimalInlineScopes())
David Blaikie192b45c2014-11-02 06:16:39 +0000723 return;
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000724 std::string FullName = getParentContextString(Context) + Ty->getName().str();
David Blaikie192b45c2014-11-02 06:16:39 +0000725 GlobalTypes[FullName] = &Die;
726}
David Blaikieae57e662014-11-01 23:59:23 +0000727
David Blaikie7d48be22014-11-02 06:37:23 +0000728/// addVariableAddress - Add DW_AT_location attribute for a
729/// DbgVariable based on provided MachineLocation.
730void DwarfCompileUnit::addVariableAddress(const DbgVariable &DV, DIE &Die,
731 MachineLocation Location) {
Duncan P. N. Exon Smithe6cc5312015-06-21 16:50:43 +0000732 if (DV.hasComplexAddress())
David Blaikie7d48be22014-11-02 06:37:23 +0000733 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
734 else if (DV.isBlockByrefVariable())
735 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
736 else
Adrian Prantl5883af32015-01-19 17:57:29 +0000737 addAddress(Die, dwarf::DW_AT_location, Location);
David Blaikie7d48be22014-11-02 06:37:23 +0000738}
David Blaikief7435ee2014-11-02 06:46:40 +0000739
740/// Add an address attribute to a die based on the location provided.
741void DwarfCompileUnit::addAddress(DIE &Die, dwarf::Attribute Attribute,
Adrian Prantl5883af32015-01-19 17:57:29 +0000742 const MachineLocation &Location) {
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000743 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
David Blaikief7435ee2014-11-02 06:46:40 +0000744
Adrian Prantlab255fc2014-12-05 01:02:46 +0000745 bool validReg;
Adrian Prantl5883af32015-01-19 17:57:29 +0000746 if (Location.isReg())
Adrian Prantlab255fc2014-12-05 01:02:46 +0000747 validReg = addRegisterOpPiece(*Loc, Location.getReg());
748 else
749 validReg = addRegisterOffset(*Loc, Location.getReg(), Location.getOffset());
750
751 if (!validReg)
752 return;
753
David Blaikief7435ee2014-11-02 06:46:40 +0000754 // Now attach the location information to the DIE.
755 addBlock(Die, Attribute, Loc);
756}
David Blaikie77895fb2014-11-02 06:58:44 +0000757
758/// Start with the address based on the location provided, and generate the
759/// DWARF information necessary to find the actual variable given the extra
760/// address information encoded in the DbgVariable, starting from the starting
761/// location. Add the DWARF information to the die.
762void DwarfCompileUnit::addComplexAddress(const DbgVariable &DV, DIE &Die,
763 dwarf::Attribute Attribute,
764 const MachineLocation &Location) {
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000765 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
Adrian Prantl7813d9c2015-01-14 01:01:30 +0000766 DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
Adrian Prantl6f4746b12016-02-17 22:19:59 +0000767 const DIExpression *Expr = DV.getSingleExpression();
Adrian Prantl40cb8192015-01-25 19:04:08 +0000768 bool ValidReg;
Adrian Prantl7813d9c2015-01-14 01:01:30 +0000769 if (Location.getOffset()) {
Adrian Prantl40cb8192015-01-25 19:04:08 +0000770 ValidReg = DwarfExpr.AddMachineRegIndirect(Location.getReg(),
771 Location.getOffset());
772 if (ValidReg)
Duncan P. N. Exon Smith76c91842015-04-07 03:45:57 +0000773 DwarfExpr.AddExpression(Expr->expr_op_begin(), Expr->expr_op_end());
Adrian Prantl5883af32015-01-19 17:57:29 +0000774 } else
Adrian Prantl40cb8192015-01-25 19:04:08 +0000775 ValidReg = DwarfExpr.AddMachineRegExpression(Expr, Location.getReg());
David Blaikie77895fb2014-11-02 06:58:44 +0000776
777 // Now attach the location information to the DIE.
Adrian Prantl40cb8192015-01-25 19:04:08 +0000778 if (ValidReg)
779 addBlock(Die, Attribute, Loc);
David Blaikie77895fb2014-11-02 06:58:44 +0000780}
David Blaikie4bc08812014-11-02 07:03:19 +0000781
782/// Add a Dwarf loclistptr attribute data and value.
783void DwarfCompileUnit::addLocationList(DIE &Die, dwarf::Attribute Attribute,
784 unsigned Index) {
David Blaikie4bc08812014-11-02 07:03:19 +0000785 dwarf::Form Form = DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
786 : dwarf::DW_FORM_data4;
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000787 Die.addValue(DIEValueAllocator, Attribute, Form, DIELocList(Index));
David Blaikie4bc08812014-11-02 07:03:19 +0000788}
David Blaikie02a63332014-11-02 07:06:51 +0000789
David Blaikie8c485b52014-11-02 07:08:12 +0000790void DwarfCompileUnit::applyVariableAttributes(const DbgVariable &Var,
791 DIE &VariableDie) {
David Blaikie02a63332014-11-02 07:06:51 +0000792 StringRef Name = Var.getName();
793 if (!Name.empty())
794 addString(VariableDie, dwarf::DW_AT_name, Name);
795 addSourceLine(VariableDie, Var.getVariable());
796 addType(VariableDie, Var.getType());
797 if (Var.isArtificial())
798 addFlag(VariableDie, dwarf::DW_AT_artificial);
799}
David Blaikie97802082014-11-02 07:11:55 +0000800
801/// Add a Dwarf expression attribute data and value.
802void DwarfCompileUnit::addExpr(DIELoc &Die, dwarf::Form Form,
803 const MCExpr *Expr) {
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000804 Die.addValue(DIEValueAllocator, (dwarf::Attribute)0, Form, DIEExpr(Expr));
David Blaikie97802082014-11-02 07:11:55 +0000805}
David Blaikie3363a572014-11-02 08:09:09 +0000806
Duncan P. N. Exon Smith2fbe1352015-04-20 22:10:08 +0000807void DwarfCompileUnit::applySubprogramAttributesToDefinition(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000808 const DISubprogram *SP, DIE &SPDie) {
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000809 auto *SPDecl = SP->getDeclaration();
Duncan P. N. Exon Smithbe9e4fe2015-04-20 18:32:29 +0000810 auto *Context = resolve(SPDecl ? SPDecl->getScope() : SP->getScope());
David Blaikie3a443c22014-11-04 22:12:25 +0000811 applySubprogramAttributes(SP, SPDie, includeMinimalInlineScopes());
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000812 addGlobalName(SP->getName(), SPDie, Context);
David Blaikie3363a572014-11-02 08:09:09 +0000813}
David Blaikiecafd9622014-11-02 08:51:37 +0000814
815bool DwarfCompileUnit::isDwoUnit() const {
816 return DD->useSplitDwarf() && Skeleton;
817}
David Blaikie3a443c22014-11-04 22:12:25 +0000818
819bool DwarfCompileUnit::includeMinimalInlineScopes() const {
Adrian Prantlb939a252016-03-31 23:56:58 +0000820 return getCUNode()->getEmissionKind() == DICompileUnit::LineTablesOnly ||
David Blaikie3a443c22014-11-04 22:12:25 +0000821 (DD->useSplitDwarf() && !Skeleton);
822}
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000823} // end llvm namespace