blob: 56856c7ee3f35c43f5d94a2044b0bb4cb6a16455 [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.
David Blaikied51dea62015-07-01 18:07:16 +0000117 DIE *ContextDIE = getOrCreateContextDIE(GVContext);
David Blaikie49cfc8c2014-10-23 19:12:43 +0000118
119 // Add to map.
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000120 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);
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000188 addLinkageName(*VariableDIE, GV->getLinkageName());
David Blaikie37c52312014-10-04 15:49:50 +0000189 } else if (const ConstantInt *CI =
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000190 dyn_cast_or_null<ConstantInt>(GV->getVariable())) {
David Blaikie49cfc8c2014-10-23 19:12:43 +0000191 addConstantValue(*VariableDIE, CI, GTy);
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000192 } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getVariable())) {
David Blaikie37c52312014-10-04 15:49:50 +0000193 addToAccelTable = true;
194 // GV is a merged global.
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000195 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
Eduard Burtescu68e7f492016-01-22 03:08:27 +0000196 auto *Ptr = cast<GlobalValue>(CE->getOperand(0));
197 MCSymbol *Sym = Asm->getSymbol(Ptr);
David Blaikie37c52312014-10-04 15:49:50 +0000198 DD->addArangeLabel(SymbolCU(this, Sym));
199 addOpAddress(*Loc, Sym);
200 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
201 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
202 addUInt(*Loc, dwarf::DW_FORM_udata,
Eduard Burtescu68e7f492016-01-22 03:08:27 +0000203 Asm->getDataLayout().getIndexedOffsetInType(Ptr->getValueType(), Idx));
David Blaikie37c52312014-10-04 15:49:50 +0000204 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
205 addBlock(*VariableDIE, dwarf::DW_AT_location, Loc);
206 }
207
David Blaikie37c52312014-10-04 15:49:50 +0000208 if (addToAccelTable) {
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000209 DD->addAccelName(GV->getName(), *VariableDIE);
David Blaikie37c52312014-10-04 15:49:50 +0000210
211 // If the linkage name is different than the name, go ahead and output
212 // that as well into the name table.
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000213 if (GV->getLinkageName() != "" && GV->getName() != GV->getLinkageName())
214 DD->addAccelName(GV->getLinkageName(), *VariableDIE);
David Blaikie37c52312014-10-04 15:49:50 +0000215 }
216
David Blaikie49cfc8c2014-10-23 19:12:43 +0000217 return VariableDIE;
David Blaikie37c52312014-10-04 15:49:50 +0000218}
219
220void DwarfCompileUnit::addRange(RangeSpan Range) {
221 bool SameAsPrevCU = this == DD->getPrevCU();
222 DD->setPrevCU(this);
223 // If we have no current ranges just add the range and return, otherwise,
224 // check the current section and CU against the previous section and CU we
225 // emitted into and the subprogram was contained within. If these are the
226 // same then extend our current range, otherwise add this as a new range.
227 if (CURanges.empty() || !SameAsPrevCU ||
228 (&CURanges.back().getEnd()->getSection() !=
229 &Range.getEnd()->getSection())) {
230 CURanges.push_back(Range);
231 return;
232 }
233
234 CURanges.back().setEnd(Range.getEnd());
235}
236
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000237DIE::value_iterator
238DwarfCompileUnit::addSectionLabel(DIE &Die, dwarf::Attribute Attribute,
239 const MCSymbol *Label, const MCSymbol *Sec) {
David Blaikie6c0ee4e2014-10-08 22:46:27 +0000240 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000241 return addLabel(Die, Attribute,
242 DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
243 : dwarf::DW_FORM_data4,
244 Label);
245 return addSectionDelta(Die, Attribute, Label, Sec);
David Blaikie6c0ee4e2014-10-08 22:46:27 +0000246}
247
Rafael Espindola063d7252015-03-10 16:58:10 +0000248void DwarfCompileUnit::initStmtList() {
David Blaikie37c52312014-10-04 15:49:50 +0000249 // Define start line table label for each Compile Unit.
250 MCSymbol *LineTableStartSym =
Lang Hames9ff69c82015-04-24 19:11:51 +0000251 Asm->OutStreamer->getDwarfLineTableSymbol(getUniqueID());
David Blaikie37c52312014-10-04 15:49:50 +0000252
David Blaikie37c52312014-10-04 15:49:50 +0000253 // DW_AT_stmt_list is a offset of line number information for this
254 // compile unit in debug_line section. For split dwarf this is
255 // left in the skeleton CU and so not included.
256 // The line table entries are not always emitted in assembly, so it
257 // is not okay to use line_table_start here.
Rafael Espindola063d7252015-03-10 16:58:10 +0000258 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000259 StmtListValue =
260 addSectionLabel(UnitDie, dwarf::DW_AT_stmt_list, LineTableStartSym,
261 TLOF.getDwarfLineSection()->getBeginSymbol());
David Blaikie37c52312014-10-04 15:49:50 +0000262}
263
264void DwarfCompileUnit::applyStmtList(DIE &D) {
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000265 D.addValue(DIEValueAllocator, *StmtListValue);
David Blaikie37c52312014-10-04 15:49:50 +0000266}
267
David Blaikie14499a72014-10-04 15:58:47 +0000268void DwarfCompileUnit::attachLowHighPC(DIE &D, const MCSymbol *Begin,
269 const MCSymbol *End) {
270 assert(Begin && "Begin label should not be null!");
271 assert(End && "End label should not be null!");
272 assert(Begin->isDefined() && "Invalid starting label");
273 assert(End->isDefined() && "Invalid end label");
274
275 addLabelAddress(D, dwarf::DW_AT_low_pc, Begin);
276 if (DD->getDwarfVersion() < 4)
277 addLabelAddress(D, dwarf::DW_AT_high_pc, End);
278 else
279 addLabelDelta(D, dwarf::DW_AT_high_pc, End, Begin);
280}
281
David Blaikiecda2aa82014-10-04 16:24:00 +0000282// Find DIE for the given subprogram and attach appropriate DW_AT_low_pc
283// and DW_AT_high_pc attributes. If there are global variables in this
284// scope then create and insert DIEs for these variables.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000285DIE &DwarfCompileUnit::updateSubprogramScopeDIE(const DISubprogram *SP) {
David Blaikie3a443c22014-11-04 22:12:25 +0000286 DIE *SPDie = getOrCreateSubprogramDIE(SP, includeMinimalInlineScopes());
David Blaikiecda2aa82014-10-04 16:24:00 +0000287
Rafael Espindola07c03d32015-03-05 02:05:42 +0000288 attachLowHighPC(*SPDie, Asm->getFunctionBegin(), Asm->getFunctionEnd());
David Blaikiecda2aa82014-10-04 16:24:00 +0000289 if (!DD->getCurrentFunction()->getTarget().Options.DisableFramePointerElim(
290 *DD->getCurrentFunction()))
291 addFlag(*SPDie, dwarf::DW_AT_APPLE_omit_frame_ptr);
292
293 // Only include DW_AT_frame_base in full debug info
David Blaikie3a443c22014-11-04 22:12:25 +0000294 if (!includeMinimalInlineScopes()) {
Eric Christopherd4e723f2015-02-20 22:36:11 +0000295 const TargetRegisterInfo *RI = Asm->MF->getSubtarget().getRegisterInfo();
David Blaikiecda2aa82014-10-04 16:24:00 +0000296 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Adrian Prantl8efadbf2015-01-14 00:15:16 +0000297 if (RI->isPhysicalRegister(Location.getReg()))
298 addAddress(*SPDie, dwarf::DW_AT_frame_base, Location);
David Blaikiecda2aa82014-10-04 16:24:00 +0000299 }
300
301 // Add name to the name table, we do this here because we're guaranteed
302 // to have concrete versions of our DW_TAG_subprogram nodes.
303 DD->addSubprogramNames(SP, *SPDie);
304
305 return *SPDie;
306}
307
David Blaikie9c65b132014-10-08 22:20:02 +0000308// Construct a DIE for this scope.
309void DwarfCompileUnit::constructScopeDIE(
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000310 LexicalScope *Scope, SmallVectorImpl<DIE *> &FinalChildren) {
David Blaikie9c65b132014-10-08 22:20:02 +0000311 if (!Scope || !Scope->getScopeNode())
312 return;
313
Duncan P. N. Exon Smithbe9e4fe2015-04-20 18:32:29 +0000314 auto *DS = Scope->getScopeNode();
David Blaikie9c65b132014-10-08 22:20:02 +0000315
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000316 assert((Scope->getInlinedAt() || !isa<DISubprogram>(DS)) &&
David Blaikie9c65b132014-10-08 22:20:02 +0000317 "Only handle inlined subprograms here, use "
318 "constructSubprogramScopeDIE for non-inlined "
319 "subprograms");
320
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000321 SmallVector<DIE *, 8> Children;
David Blaikie9c65b132014-10-08 22:20:02 +0000322
323 // We try to create the scope DIE first, then the children DIEs. This will
324 // avoid creating un-used children then removing them later when we find out
325 // the scope DIE is null.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000326 DIE *ScopeDIE;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000327 if (Scope->getParent() && isa<DISubprogram>(DS)) {
David Blaikie01b48a82014-10-09 16:50:53 +0000328 ScopeDIE = constructInlinedScopeDIE(Scope);
David Blaikie9c65b132014-10-08 22:20:02 +0000329 if (!ScopeDIE)
330 return;
331 // We create children when the scope DIE is not null.
David Blaikie8b2fdb82014-10-09 18:24:28 +0000332 createScopeChildrenDIE(Scope, Children);
David Blaikie9c65b132014-10-08 22:20:02 +0000333 } else {
334 // Early exit when we know the scope DIE is going to be null.
335 if (DD->isLexicalScopeDIENull(Scope))
336 return;
337
338 unsigned ChildScopeCount;
339
340 // We create children here when we know the scope DIE is not going to be
341 // null and the children will be added to the scope DIE.
David Blaikie8b2fdb82014-10-09 18:24:28 +0000342 createScopeChildrenDIE(Scope, Children, &ChildScopeCount);
David Blaikie9c65b132014-10-08 22:20:02 +0000343
David Blaikied51dea62015-07-01 18:07:16 +0000344 // Skip imported directives in gmlt-like data.
345 if (!includeMinimalInlineScopes()) {
346 // There is no need to emit empty lexical block DIE.
Ivan Krasin298639a2015-10-26 21:36:35 +0000347 for (const auto *IE : ImportedEntities[DS])
Ivan Krasin465fbe22015-10-26 22:35:40 +0000348 Children.push_back(
349 constructImportedEntityDIE(cast<DIImportedEntity>(IE)));
David Blaikied51dea62015-07-01 18:07:16 +0000350 }
David Blaikie3a443c22014-11-04 22:12:25 +0000351
David Blaikie9c65b132014-10-08 22:20:02 +0000352 // If there are only other scopes as children, put them directly in the
353 // parent instead, as this scope would serve no purpose.
David Blaikied51dea62015-07-01 18:07:16 +0000354 if (Children.size() == ChildScopeCount) {
David Blaikie9c65b132014-10-08 22:20:02 +0000355 FinalChildren.insert(FinalChildren.end(),
356 std::make_move_iterator(Children.begin()),
357 std::make_move_iterator(Children.end()));
358 return;
359 }
David Blaikie0fbf8bd2014-10-09 17:08:42 +0000360 ScopeDIE = constructLexicalScopeDIE(Scope);
David Blaikie9c65b132014-10-08 22:20:02 +0000361 assert(ScopeDIE && "Scope DIE should not be null.");
362 }
363
364 // Add children
365 for (auto &I : Children)
366 ScopeDIE->addChild(std::move(I));
367
368 FinalChildren.push_back(std::move(ScopeDIE));
369}
370
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000371DIE::value_iterator
372DwarfCompileUnit::addSectionDelta(DIE &Die, dwarf::Attribute Attribute,
373 const MCSymbol *Hi, const MCSymbol *Lo) {
374 return Die.addValue(DIEValueAllocator, Attribute,
375 DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
376 : dwarf::DW_FORM_data4,
377 new (DIEValueAllocator) DIEDelta(Hi, Lo));
David Blaikiee5feec52014-10-08 23:30:05 +0000378}
379
David Blaikie5b02a192014-11-03 23:10:59 +0000380void DwarfCompileUnit::addScopeRangeList(DIE &ScopeDIE,
381 SmallVector<RangeSpan, 2> Range) {
Rafael Espindola063d7252015-03-10 16:58:10 +0000382 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
383
David Blaikie52400202014-10-09 00:11:39 +0000384 // Emit offset in .debug_range as a relocatable label. emitDIE will handle
385 // emitting it appropriately.
Rafael Espindola063d7252015-03-10 16:58:10 +0000386 const MCSymbol *RangeSectionSym =
387 TLOF.getDwarfRangesSection()->getBeginSymbol();
David Blaikie52400202014-10-09 00:11:39 +0000388
Rafael Espindola9ab09232015-03-17 20:07:06 +0000389 RangeSpanList List(Asm->createTempSymbol("debug_ranges"), std::move(Range));
David Blaikie5b02a192014-11-03 23:10:59 +0000390
David Blaikie52400202014-10-09 00:11:39 +0000391 // Under fission, ranges are specified by constant offsets relative to the
392 // CU's DW_AT_GNU_ranges_base.
David Blaikie5b02a192014-11-03 23:10:59 +0000393 if (isDwoUnit())
394 addSectionDelta(ScopeDIE, dwarf::DW_AT_ranges, List.getSym(),
395 RangeSectionSym);
David Blaikie52400202014-10-09 00:11:39 +0000396 else
David Blaikie5b02a192014-11-03 23:10:59 +0000397 addSectionLabel(ScopeDIE, dwarf::DW_AT_ranges, List.getSym(),
398 RangeSectionSym);
David Blaikie52400202014-10-09 00:11:39 +0000399
400 // Add the range list to the set of ranges to be emitted.
David Blaikie542616d2014-11-03 21:52:56 +0000401 (Skeleton ? Skeleton : this)->CURangeLists.push_back(std::move(List));
David Blaikie52400202014-10-09 00:11:39 +0000402}
403
David Blaikiede123752014-10-09 00:21:42 +0000404void DwarfCompileUnit::attachRangesOrLowHighPC(
David Blaikie5b02a192014-11-03 23:10:59 +0000405 DIE &Die, SmallVector<RangeSpan, 2> Ranges) {
406 if (Ranges.size() == 1) {
407 const auto &single = Ranges.front();
408 attachLowHighPC(Die, single.getStart(), single.getEnd());
409 } else
410 addScopeRangeList(Die, std::move(Ranges));
411}
412
413void DwarfCompileUnit::attachRangesOrLowHighPC(
David Blaikiede123752014-10-09 00:21:42 +0000414 DIE &Die, const SmallVectorImpl<InsnRange> &Ranges) {
David Blaikie5b02a192014-11-03 23:10:59 +0000415 SmallVector<RangeSpan, 2> List;
416 List.reserve(Ranges.size());
417 for (const InsnRange &R : Ranges)
418 List.push_back(RangeSpan(DD->getLabelBeforeInsn(R.first),
419 DD->getLabelAfterInsn(R.second)));
420 attachRangesOrLowHighPC(Die, std::move(List));
David Blaikiede123752014-10-09 00:21:42 +0000421}
422
David Blaikie01b48a82014-10-09 16:50:53 +0000423// This scope represents inlined body of a function. Construct DIE to
424// represent this concrete inlined copy of the function.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000425DIE *DwarfCompileUnit::constructInlinedScopeDIE(LexicalScope *Scope) {
David Blaikie01b48a82014-10-09 16:50:53 +0000426 assert(Scope->getScopeNode());
Duncan P. N. Exon Smithbe9e4fe2015-04-20 18:32:29 +0000427 auto *DS = Scope->getScopeNode();
428 auto *InlinedSP = getDISubprogram(DS);
David Blaikie01b48a82014-10-09 16:50:53 +0000429 // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram
430 // was inlined from another compile unit.
David Blaikief6dac292014-11-01 17:21:26 +0000431 DIE *OriginDIE = DU->getAbstractSPDies()[InlinedSP];
David Blaikie01b48a82014-10-09 16:50:53 +0000432 assert(OriginDIE && "Unable to find original DIE for an inlined subprogram.");
433
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000434 auto ScopeDIE = DIE::get(DIEValueAllocator, dwarf::DW_TAG_inlined_subroutine);
David Blaikie01b48a82014-10-09 16:50:53 +0000435 addDIEEntry(*ScopeDIE, dwarf::DW_AT_abstract_origin, *OriginDIE);
436
437 attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges());
438
439 // Add the call site information to the DIE.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000440 const DILocation *IA = Scope->getInlinedAt();
David Blaikie01b48a82014-10-09 16:50:53 +0000441 addUInt(*ScopeDIE, dwarf::DW_AT_call_file, None,
Duncan P. N. Exon Smithb7e221b2015-04-14 01:35:55 +0000442 getOrCreateSourceID(IA->getFilename(), IA->getDirectory()));
443 addUInt(*ScopeDIE, dwarf::DW_AT_call_line, None, IA->getLine());
Dehao Chen72fdf442015-11-11 18:08:18 +0000444 if (IA->getDiscriminator())
Dehao Chen54511352015-11-11 18:09:47 +0000445 addUInt(*ScopeDIE, dwarf::DW_AT_GNU_discriminator, None,
446 IA->getDiscriminator());
David Blaikie01b48a82014-10-09 16:50:53 +0000447
448 // Add name to the name table, we do this here because we're guaranteed
449 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
450 DD->addSubprogramNames(InlinedSP, *ScopeDIE);
451
452 return ScopeDIE;
453}
454
David Blaikie0fbf8bd2014-10-09 17:08:42 +0000455// Construct new DW_TAG_lexical_block for this scope and attach
456// DW_AT_low_pc/DW_AT_high_pc labels.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000457DIE *DwarfCompileUnit::constructLexicalScopeDIE(LexicalScope *Scope) {
David Blaikie0fbf8bd2014-10-09 17:08:42 +0000458 if (DD->isLexicalScopeDIENull(Scope))
459 return nullptr;
460
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000461 auto ScopeDIE = DIE::get(DIEValueAllocator, dwarf::DW_TAG_lexical_block);
David Blaikie0fbf8bd2014-10-09 17:08:42 +0000462 if (Scope->isAbstractScope())
463 return ScopeDIE;
464
465 attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges());
466
467 return ScopeDIE;
468}
469
David Blaikieee7df552014-10-09 17:56:36 +0000470/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000471DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV, bool Abstract) {
David Blaikieee7df552014-10-09 17:56:36 +0000472 auto D = constructVariableDIEImpl(DV, Abstract);
473 DV.setDIE(*D);
474 return D;
475}
476
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000477DIE *DwarfCompileUnit::constructVariableDIEImpl(const DbgVariable &DV,
478 bool Abstract) {
David Blaikieee7df552014-10-09 17:56:36 +0000479 // Define variable debug information entry.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000480 auto VariableDie = DIE::get(DIEValueAllocator, DV.getTag());
David Blaikieee7df552014-10-09 17:56:36 +0000481
482 if (Abstract) {
483 applyVariableAttributes(DV, *VariableDie);
484 return VariableDie;
485 }
486
487 // Add variable address.
488
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +0000489 unsigned Offset = DV.getDebugLocListIndex();
David Blaikieee7df552014-10-09 17:56:36 +0000490 if (Offset != ~0U) {
491 addLocationList(*VariableDie, dwarf::DW_AT_location, Offset);
492 return VariableDie;
493 }
494
495 // Check if variable is described by a DBG_VALUE instruction.
496 if (const MachineInstr *DVInsn = DV.getMInsn()) {
497 assert(DVInsn->getNumOperands() == 4);
498 if (DVInsn->getOperand(0).isReg()) {
499 const MachineOperand RegOp = DVInsn->getOperand(0);
500 // If the second operand is an immediate, this is an indirect value.
501 if (DVInsn->getOperand(1).isImm()) {
502 MachineLocation Location(RegOp.getReg(),
503 DVInsn->getOperand(1).getImm());
504 addVariableAddress(DV, *VariableDie, Location);
505 } else if (RegOp.getReg())
506 addVariableAddress(DV, *VariableDie, MachineLocation(RegOp.getReg()));
Adrian Prantl3b89e662016-02-17 22:20:08 +0000507 } else if (DVInsn->getOperand(0).isImm()) {
508 // This variable is described by a single constant.
509 // Check whether it has a DIExpression.
510 auto *Expr = DV.getSingleExpression();
511 if (Expr && Expr->getNumElements()) {
512 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
513 DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
514 // If there is an expression, emit raw unsigned bytes.
515 DwarfExpr.AddUnsignedConstant(DVInsn->getOperand(0).getImm());
516 DwarfExpr.AddExpression(Expr->expr_op_begin(), Expr->expr_op_end());
517 addBlock(*VariableDie, dwarf::DW_AT_location, Loc);
518 } else
519 addConstantValue(*VariableDie, DVInsn->getOperand(0), DV.getType());
520 } else if (DVInsn->getOperand(0).isFPImm())
David Blaikieee7df552014-10-09 17:56:36 +0000521 addConstantFPValue(*VariableDie, DVInsn->getOperand(0));
522 else if (DVInsn->getOperand(0).isCImm())
523 addConstantValue(*VariableDie, DVInsn->getOperand(0).getCImm(),
524 DV.getType());
525
526 return VariableDie;
527 }
528
529 // .. else use frame index.
Duncan P. N. Exon Smithe6cc5312015-06-21 16:50:43 +0000530 if (DV.getFrameIndex().empty())
Adrian Prantlca7e4702015-02-10 23:18:28 +0000531 return VariableDie;
532
533 auto Expr = DV.getExpression().begin();
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000534 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
Adrian Prantlca7e4702015-02-10 23:18:28 +0000535 DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
536 for (auto FI : DV.getFrameIndex()) {
David Blaikieee7df552014-10-09 17:56:36 +0000537 unsigned FrameReg = 0;
Eric Christopherd4e723f2015-02-20 22:36:11 +0000538 const TargetFrameLowering *TFI = Asm->MF->getSubtarget().getFrameLowering();
David Blaikieee7df552014-10-09 17:56:36 +0000539 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
Dehao Chen54511352015-11-11 18:09:47 +0000540 assert(Expr != DV.getExpression().end() && "Wrong number of expressions");
Adrian Prantlca7e4702015-02-10 23:18:28 +0000541 DwarfExpr.AddMachineRegIndirect(FrameReg, Offset);
Duncan P. N. Exon Smith76c91842015-04-07 03:45:57 +0000542 DwarfExpr.AddExpression((*Expr)->expr_op_begin(), (*Expr)->expr_op_end());
Duncan P. N. Exon Smith57bab0b2015-02-17 22:30:56 +0000543 ++Expr;
David Blaikieee7df552014-10-09 17:56:36 +0000544 }
Adrian Prantlca7e4702015-02-10 23:18:28 +0000545 addBlock(*VariableDie, dwarf::DW_AT_location, Loc);
David Blaikieee7df552014-10-09 17:56:36 +0000546
547 return VariableDie;
548}
549
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000550DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV,
551 const LexicalScope &Scope,
552 DIE *&ObjectPointer) {
David Blaikie4a1a44e2014-10-09 17:56:39 +0000553 auto Var = constructVariableDIE(DV, Scope.isAbstractScope());
554 if (DV.isObjectPointer())
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000555 ObjectPointer = Var;
David Blaikie4a1a44e2014-10-09 17:56:39 +0000556 return Var;
557}
558
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000559DIE *DwarfCompileUnit::createScopeChildrenDIE(LexicalScope *Scope,
560 SmallVectorImpl<DIE *> &Children,
561 unsigned *ChildScopeCount) {
David Blaikie8b2fdb82014-10-09 18:24:28 +0000562 DIE *ObjectPointer = nullptr;
563
David Blaikie80e5b1e2014-10-24 17:57:34 +0000564 for (DbgVariable *DV : DU->getScopeVariables().lookup(Scope))
David Blaikie8b2fdb82014-10-09 18:24:28 +0000565 Children.push_back(constructVariableDIE(*DV, *Scope, ObjectPointer));
566
567 unsigned ChildCountWithoutScopes = Children.size();
568
569 for (LexicalScope *LS : Scope->getChildren())
570 constructScopeDIE(LS, Children);
571
572 if (ChildScopeCount)
573 *ChildScopeCount = Children.size() - ChildCountWithoutScopes;
574
575 return ObjectPointer;
576}
577
David Blaikie1d072342014-10-09 20:21:36 +0000578void DwarfCompileUnit::constructSubprogramScopeDIE(LexicalScope *Scope) {
579 assert(Scope && Scope->getScopeNode());
580 assert(!Scope->getInlinedAt());
581 assert(!Scope->isAbstractScope());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000582 auto *Sub = cast<DISubprogram>(Scope->getScopeNode());
David Blaikie1d072342014-10-09 20:21:36 +0000583
584 DD->getProcessedSPNodes().insert(Sub);
585
586 DIE &ScopeDIE = updateSubprogramScopeDIE(Sub);
587
David Blaikie1d072342014-10-09 20:21:36 +0000588 // If this is a variadic function, add an unspecified parameter.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000589 DITypeRefArray FnArgs = Sub->getType()->getTypeArray();
David Blaikie1dd573d2014-10-23 22:27:50 +0000590
591 // Collect lexical scope children first.
592 // ObjectPointer might be a local (non-argument) local variable if it's a
593 // block's synthetic this pointer.
594 if (DIE *ObjectPointer = createAndAddScopeChildren(Scope, ScopeDIE))
595 addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer, *ObjectPointer);
596
David Blaikie1d072342014-10-09 20:21:36 +0000597 // If we have a single element of null, it is a function that returns void.
598 // If we have more than one elements and the last one is null, it is a
599 // variadic function.
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000600 if (FnArgs.size() > 1 && !FnArgs[FnArgs.size() - 1] &&
David Blaikie3a443c22014-11-04 22:12:25 +0000601 !includeMinimalInlineScopes())
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000602 ScopeDIE.addChild(
603 DIE::get(DIEValueAllocator, dwarf::DW_TAG_unspecified_parameters));
David Blaikie1d072342014-10-09 20:21:36 +0000604}
605
David Blaikie78b65b62014-10-09 20:26:15 +0000606DIE *DwarfCompileUnit::createAndAddScopeChildren(LexicalScope *Scope,
607 DIE &ScopeDIE) {
608 // We create children when the scope DIE is not null.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000609 SmallVector<DIE *, 8> Children;
David Blaikie78b65b62014-10-09 20:26:15 +0000610 DIE *ObjectPointer = createScopeChildrenDIE(Scope, Children);
611
612 // Add children
613 for (auto &I : Children)
614 ScopeDIE.addChild(std::move(I));
615
616 return ObjectPointer;
617}
618
Dehao Chen54511352015-11-11 18:09:47 +0000619void DwarfCompileUnit::constructAbstractSubprogramScopeDIE(
620 LexicalScope *Scope) {
David Blaikief6dac292014-11-01 17:21:26 +0000621 DIE *&AbsDef = DU->getAbstractSPDies()[Scope->getScopeNode()];
David Blaikie49be5b32014-10-31 21:57:02 +0000622 if (AbsDef)
623 return;
624
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000625 auto *SP = cast<DISubprogram>(Scope->getScopeNode());
David Blaikie58410f22014-10-10 06:39:26 +0000626
627 DIE *ContextDIE;
628
David Blaikie3a443c22014-11-04 22:12:25 +0000629 if (includeMinimalInlineScopes())
630 ContextDIE = &getUnitDie();
David Blaikie58410f22014-10-10 06:39:26 +0000631 // Some of this is duplicated from DwarfUnit::getOrCreateSubprogramDIE, with
Duncan P. N. Exon Smith7c60f202015-04-18 00:35:36 +0000632 // the important distinction that the debug node is not associated with the
633 // DIE (since the debug node will be associated with the concrete DIE, if
David Blaikie58410f22014-10-10 06:39:26 +0000634 // any). It could be refactored to some common utility function.
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000635 else if (auto *SPDecl = SP->getDeclaration()) {
David Blaikie58410f22014-10-10 06:39:26 +0000636 ContextDIE = &getUnitDie();
637 getOrCreateSubprogramDIE(SPDecl);
638 } else
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000639 ContextDIE = getOrCreateContextDIE(resolve(SP->getScope()));
David Blaikie58410f22014-10-10 06:39:26 +0000640
Duncan P. N. Exon Smith7c60f202015-04-18 00:35:36 +0000641 // Passing null as the associated node because the abstract definition
David Blaikie58410f22014-10-10 06:39:26 +0000642 // shouldn't be found by lookup.
Duncan P. N. Exon Smith7c60f202015-04-18 00:35:36 +0000643 AbsDef = &createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, nullptr);
David Blaikie49be5b32014-10-31 21:57:02 +0000644 applySubprogramAttributesToDefinition(SP, *AbsDef);
David Blaikie58410f22014-10-10 06:39:26 +0000645
David Blaikie3a443c22014-11-04 22:12:25 +0000646 if (!includeMinimalInlineScopes())
David Blaikie49be5b32014-10-31 21:57:02 +0000647 addUInt(*AbsDef, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined);
648 if (DIE *ObjectPointer = createAndAddScopeChildren(Scope, *AbsDef))
649 addDIEEntry(*AbsDef, dwarf::DW_AT_object_pointer, *ObjectPointer);
David Blaikie58410f22014-10-10 06:39:26 +0000650}
651
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000652DIE *DwarfCompileUnit::constructImportedEntityDIE(
653 const DIImportedEntity *Module) {
654 DIE *IMDie = DIE::get(DIEValueAllocator, (dwarf::Tag)Module->getTag());
655 insertDIE(Module, IMDie);
Frederic Riss987fe222014-10-24 21:31:09 +0000656 DIE *EntityDie;
Duncan P. N. Exon Smithde8e4272015-04-14 01:46:44 +0000657 auto *Entity = resolve(Module->getEntity());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000658 if (auto *NS = dyn_cast<DINamespace>(Entity))
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000659 EntityDie = getOrCreateNameSpace(NS);
Adrian Prantl08a388b2015-06-30 02:13:04 +0000660 else if (auto *M = dyn_cast<DIModule>(Entity))
661 EntityDie = getOrCreateModule(M);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000662 else if (auto *SP = dyn_cast<DISubprogram>(Entity))
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000663 EntityDie = getOrCreateSubprogramDIE(SP);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000664 else if (auto *T = dyn_cast<DIType>(Entity))
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000665 EntityDie = getOrCreateTypeDIE(T);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000666 else if (auto *GV = dyn_cast<DIGlobalVariable>(Entity))
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000667 EntityDie = getOrCreateGlobalVariableDIE(GV);
Frederic Riss987fe222014-10-24 21:31:09 +0000668 else
669 EntityDie = getDIE(Entity);
670 assert(EntityDie);
Duncan P. N. Exon Smithde8e4272015-04-14 01:46:44 +0000671 addSourceLine(*IMDie, Module->getLine(), Module->getScope()->getFilename(),
672 Module->getScope()->getDirectory());
Frederic Riss987fe222014-10-24 21:31:09 +0000673 addDIEEntry(*IMDie, dwarf::DW_AT_import, *EntityDie);
Duncan P. N. Exon Smithde8e4272015-04-14 01:46:44 +0000674 StringRef Name = Module->getName();
Frederic Riss987fe222014-10-24 21:31:09 +0000675 if (!Name.empty())
676 addString(*IMDie, dwarf::DW_AT_name, Name);
677
678 return IMDie;
679}
680
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000681void DwarfCompileUnit::finishSubprogramDefinition(const DISubprogram *SP) {
David Blaikie4191cbc2014-10-10 06:39:29 +0000682 DIE *D = getDIE(SP);
David Blaikief6dac292014-11-01 17:21:26 +0000683 if (DIE *AbsSPDIE = DU->getAbstractSPDies().lookup(SP)) {
David Blaikie4191cbc2014-10-10 06:39:29 +0000684 if (D)
685 // If this subprogram has an abstract definition, reference that
686 addDIEEntry(*D, dwarf::DW_AT_abstract_origin, *AbsSPDIE);
687 } else {
David Blaikie3a443c22014-11-04 22:12:25 +0000688 if (!D && !includeMinimalInlineScopes())
David Blaikie4191cbc2014-10-10 06:39:29 +0000689 // Lazily construct the subprogram if we didn't see either concrete or
690 // inlined versions during codegen. (except in -gmlt ^ where we want
691 // to omit these entirely)
692 D = getOrCreateSubprogramDIE(SP);
693 if (D)
694 // And attach the attributes
695 applySubprogramAttributesToDefinition(SP, *D);
696 }
697}
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000698void DwarfCompileUnit::collectDeadVariables(const DISubprogram *SP) {
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000699 assert(SP && "CU's subprogram list contains a non-subprogram");
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000700 assert(SP->isDefinition() &&
David Blaikie1d96cc22014-10-31 22:30:30 +0000701 "CU's subprogram list contains a subprogram declaration");
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000702 auto Variables = SP->getVariables();
703 if (Variables.size() == 0)
David Blaikie1d96cc22014-10-31 22:30:30 +0000704 return;
705
David Blaikief6dac292014-11-01 17:21:26 +0000706 DIE *SPDIE = DU->getAbstractSPDies().lookup(SP);
David Blaikie1d96cc22014-10-31 22:30:30 +0000707 if (!SPDIE)
708 SPDIE = getDIE(SP);
709 assert(SPDIE);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000710 for (const DILocalVariable *DV : Variables) {
Duncan P. N. Exon Smithe6cc5312015-06-21 16:50:43 +0000711 DbgVariable NewVar(DV, /* IA */ nullptr, DD);
David Blaikie1d96cc22014-10-31 22:30:30 +0000712 auto VariableDie = constructVariableDIE(NewVar);
713 applyVariableAttributes(NewVar, *VariableDie);
714 SPDIE->addChild(std::move(VariableDie));
715 }
716}
David Blaikie4191cbc2014-10-10 06:39:29 +0000717
Rafael Espindola063d7252015-03-10 16:58:10 +0000718void DwarfCompileUnit::emitHeader(bool UseOffsets) {
David Blaikieb6726a92014-11-02 02:26:24 +0000719 // Don't bother labeling the .dwo unit, as its offset isn't used.
Rafael Espindola3c311142015-03-10 03:11:11 +0000720 if (!Skeleton) {
Rafael Espindola9ab09232015-03-17 20:07:06 +0000721 LabelBegin = Asm->createTempSymbol("cu_begin");
Lang Hames9ff69c82015-04-24 19:11:51 +0000722 Asm->OutStreamer->EmitLabel(LabelBegin);
Rafael Espindola3c311142015-03-10 03:11:11 +0000723 }
David Blaikieae57e662014-11-01 23:59:23 +0000724
Rafael Espindola063d7252015-03-10 16:58:10 +0000725 DwarfUnit::emitHeader(UseOffsets);
David Blaikieae57e662014-11-01 23:59:23 +0000726}
727
David Blaikie192b45c2014-11-02 06:16:39 +0000728/// addGlobalName - Add a new global name to the compile unit.
729void DwarfCompileUnit::addGlobalName(StringRef Name, DIE &Die,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000730 const DIScope *Context) {
David Blaikie3a443c22014-11-04 22:12:25 +0000731 if (includeMinimalInlineScopes())
David Blaikie192b45c2014-11-02 06:16:39 +0000732 return;
733 std::string FullName = getParentContextString(Context) + Name.str();
734 GlobalNames[FullName] = &Die;
735}
736
737/// Add a new global type to the unit.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000738void DwarfCompileUnit::addGlobalType(const DIType *Ty, const DIE &Die,
739 const DIScope *Context) {
David Blaikie3a443c22014-11-04 22:12:25 +0000740 if (includeMinimalInlineScopes())
David Blaikie192b45c2014-11-02 06:16:39 +0000741 return;
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000742 std::string FullName = getParentContextString(Context) + Ty->getName().str();
David Blaikie192b45c2014-11-02 06:16:39 +0000743 GlobalTypes[FullName] = &Die;
744}
David Blaikieae57e662014-11-01 23:59:23 +0000745
David Blaikie7d48be22014-11-02 06:37:23 +0000746/// addVariableAddress - Add DW_AT_location attribute for a
747/// DbgVariable based on provided MachineLocation.
748void DwarfCompileUnit::addVariableAddress(const DbgVariable &DV, DIE &Die,
749 MachineLocation Location) {
Duncan P. N. Exon Smithe6cc5312015-06-21 16:50:43 +0000750 if (DV.hasComplexAddress())
David Blaikie7d48be22014-11-02 06:37:23 +0000751 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
752 else if (DV.isBlockByrefVariable())
753 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
754 else
Adrian Prantl5883af32015-01-19 17:57:29 +0000755 addAddress(Die, dwarf::DW_AT_location, Location);
David Blaikie7d48be22014-11-02 06:37:23 +0000756}
David Blaikief7435ee2014-11-02 06:46:40 +0000757
758/// Add an address attribute to a die based on the location provided.
759void DwarfCompileUnit::addAddress(DIE &Die, dwarf::Attribute Attribute,
Adrian Prantl5883af32015-01-19 17:57:29 +0000760 const MachineLocation &Location) {
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000761 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
David Blaikief7435ee2014-11-02 06:46:40 +0000762
Adrian Prantlab255fc2014-12-05 01:02:46 +0000763 bool validReg;
Adrian Prantl5883af32015-01-19 17:57:29 +0000764 if (Location.isReg())
Adrian Prantlab255fc2014-12-05 01:02:46 +0000765 validReg = addRegisterOpPiece(*Loc, Location.getReg());
766 else
767 validReg = addRegisterOffset(*Loc, Location.getReg(), Location.getOffset());
768
769 if (!validReg)
770 return;
771
David Blaikief7435ee2014-11-02 06:46:40 +0000772 // Now attach the location information to the DIE.
773 addBlock(Die, Attribute, Loc);
774}
David Blaikie77895fb2014-11-02 06:58:44 +0000775
776/// Start with the address based on the location provided, and generate the
777/// DWARF information necessary to find the actual variable given the extra
778/// address information encoded in the DbgVariable, starting from the starting
779/// location. Add the DWARF information to the die.
780void DwarfCompileUnit::addComplexAddress(const DbgVariable &DV, DIE &Die,
781 dwarf::Attribute Attribute,
782 const MachineLocation &Location) {
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000783 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
Adrian Prantl7813d9c2015-01-14 01:01:30 +0000784 DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
Adrian Prantl6f4746b12016-02-17 22:19:59 +0000785 const DIExpression *Expr = DV.getSingleExpression();
Adrian Prantl40cb8192015-01-25 19:04:08 +0000786 bool ValidReg;
Adrian Prantl7813d9c2015-01-14 01:01:30 +0000787 if (Location.getOffset()) {
Adrian Prantl40cb8192015-01-25 19:04:08 +0000788 ValidReg = DwarfExpr.AddMachineRegIndirect(Location.getReg(),
789 Location.getOffset());
790 if (ValidReg)
Duncan P. N. Exon Smith76c91842015-04-07 03:45:57 +0000791 DwarfExpr.AddExpression(Expr->expr_op_begin(), Expr->expr_op_end());
Adrian Prantl5883af32015-01-19 17:57:29 +0000792 } else
Adrian Prantl40cb8192015-01-25 19:04:08 +0000793 ValidReg = DwarfExpr.AddMachineRegExpression(Expr, Location.getReg());
David Blaikie77895fb2014-11-02 06:58:44 +0000794
795 // Now attach the location information to the DIE.
Adrian Prantl40cb8192015-01-25 19:04:08 +0000796 if (ValidReg)
797 addBlock(Die, Attribute, Loc);
David Blaikie77895fb2014-11-02 06:58:44 +0000798}
David Blaikie4bc08812014-11-02 07:03:19 +0000799
800/// Add a Dwarf loclistptr attribute data and value.
801void DwarfCompileUnit::addLocationList(DIE &Die, dwarf::Attribute Attribute,
802 unsigned Index) {
David Blaikie4bc08812014-11-02 07:03:19 +0000803 dwarf::Form Form = DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
804 : dwarf::DW_FORM_data4;
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000805 Die.addValue(DIEValueAllocator, Attribute, Form, DIELocList(Index));
David Blaikie4bc08812014-11-02 07:03:19 +0000806}
David Blaikie02a63332014-11-02 07:06:51 +0000807
David Blaikie8c485b52014-11-02 07:08:12 +0000808void DwarfCompileUnit::applyVariableAttributes(const DbgVariable &Var,
809 DIE &VariableDie) {
David Blaikie02a63332014-11-02 07:06:51 +0000810 StringRef Name = Var.getName();
811 if (!Name.empty())
812 addString(VariableDie, dwarf::DW_AT_name, Name);
813 addSourceLine(VariableDie, Var.getVariable());
814 addType(VariableDie, Var.getType());
815 if (Var.isArtificial())
816 addFlag(VariableDie, dwarf::DW_AT_artificial);
817}
David Blaikie97802082014-11-02 07:11:55 +0000818
819/// Add a Dwarf expression attribute data and value.
820void DwarfCompileUnit::addExpr(DIELoc &Die, dwarf::Form Form,
821 const MCExpr *Expr) {
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +0000822 Die.addValue(DIEValueAllocator, (dwarf::Attribute)0, Form, DIEExpr(Expr));
David Blaikie97802082014-11-02 07:11:55 +0000823}
David Blaikie3363a572014-11-02 08:09:09 +0000824
Duncan P. N. Exon Smith2fbe1352015-04-20 22:10:08 +0000825void DwarfCompileUnit::applySubprogramAttributesToDefinition(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000826 const DISubprogram *SP, DIE &SPDie) {
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000827 auto *SPDecl = SP->getDeclaration();
Duncan P. N. Exon Smithbe9e4fe2015-04-20 18:32:29 +0000828 auto *Context = resolve(SPDecl ? SPDecl->getScope() : SP->getScope());
David Blaikie3a443c22014-11-04 22:12:25 +0000829 applySubprogramAttributes(SP, SPDie, includeMinimalInlineScopes());
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000830 addGlobalName(SP->getName(), SPDie, Context);
David Blaikie3363a572014-11-02 08:09:09 +0000831}
David Blaikiecafd9622014-11-02 08:51:37 +0000832
833bool DwarfCompileUnit::isDwoUnit() const {
834 return DD->useSplitDwarf() && Skeleton;
835}
David Blaikie3a443c22014-11-04 22:12:25 +0000836
837bool DwarfCompileUnit::includeMinimalInlineScopes() const {
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +0000838 return getCUNode()->getEmissionKind() == DIBuilder::LineTablesOnly ||
David Blaikie3a443c22014-11-04 22:12:25 +0000839 (DD->useSplitDwarf() && !Skeleton);
840}
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000841} // end llvm namespace