blob: 2256200c8d0a3a14f3a235e660007658539c89c1 [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)
David Blaikie7cbf58a2014-11-01 18:18:07 +000022 : DwarfUnit(UID, dwarf::DW_TAG_compile_unit, Node, A, DW, DWU),
Rafael Espindola063d7252015-03-10 16:58:10 +000023 Skeleton(nullptr), BaseAddress(nullptr) {
David Blaikie37c52312014-10-04 15:49:50 +000024 insertDIE(Node, &getUnitDie());
25}
26
27/// addLabelAddress - Add a dwarf label attribute data and value using
28/// DW_FORM_addr or DW_FORM_GNU_addr_index.
29///
30void DwarfCompileUnit::addLabelAddress(DIE &Die, dwarf::Attribute Attribute,
31 const MCSymbol *Label) {
32
33 // Don't use the address pool in non-fission or in the skeleton unit itself.
34 // FIXME: Once GDB supports this, it's probably worthwhile using the address
35 // pool from the skeleton - maybe even in non-fission (possibly fewer
36 // relocations by sharing them in the pool, but we have other ideas about how
37 // to reduce the number of relocations as well/instead).
38 if (!DD->useSplitDwarf() || !Skeleton)
39 return addLocalLabelAddress(Die, Attribute, Label);
40
41 if (Label)
42 DD->addArangeLabel(SymbolCU(this, Label));
43
44 unsigned idx = DD->getAddressPool().getIndex(Label);
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +000045 Die.addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, DIEInteger(idx));
David Blaikie37c52312014-10-04 15:49:50 +000046}
47
48void DwarfCompileUnit::addLocalLabelAddress(DIE &Die,
49 dwarf::Attribute Attribute,
50 const MCSymbol *Label) {
51 if (Label)
52 DD->addArangeLabel(SymbolCU(this, Label));
53
54 Die.addValue(Attribute, dwarf::DW_FORM_addr,
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +000055 Label ? DIEValue(DIELabel(Label)) : DIEValue(DIEInteger(0)));
David Blaikie37c52312014-10-04 15:49:50 +000056}
57
David Blaikie89452192014-10-04 16:00:26 +000058unsigned DwarfCompileUnit::getOrCreateSourceID(StringRef FileName,
59 StringRef DirName) {
David Blaikie37c52312014-10-04 15:49:50 +000060 // If we print assembly, we can't separate .file entries according to
61 // compile units. Thus all files will belong to the default compile unit.
62
63 // FIXME: add a better feature test than hasRawTextSupport. Even better,
64 // extend .file to support this.
Lang Hames9ff69c82015-04-24 19:11:51 +000065 return Asm->OutStreamer->EmitDwarfFileDirective(
David Blaikie37c52312014-10-04 15:49:50 +000066 0, DirName, FileName,
Lang Hames9ff69c82015-04-24 19:11:51 +000067 Asm->OutStreamer->hasRawTextSupport() ? 0 : getUniqueID());
David Blaikie37c52312014-10-04 15:49:50 +000068}
69
70// Return const expression if value is a GEP to access merged global
71// constant. e.g.
72// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
73static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
74 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
75 if (!CE || CE->getNumOperands() != 3 ||
76 CE->getOpcode() != Instruction::GetElementPtr)
77 return nullptr;
78
79 // First operand points to a global struct.
80 Value *Ptr = CE->getOperand(0);
81 if (!isa<GlobalValue>(Ptr) ||
82 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
83 return nullptr;
84
85 // Second operand is zero.
86 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
87 if (!CI || !CI->isZero())
88 return nullptr;
89
90 // Third operand is offset.
91 if (!isa<ConstantInt>(CE->getOperand(2)))
92 return nullptr;
93
94 return CE;
95}
96
97/// getOrCreateGlobalVariableDIE - get or create global variable DIE.
Duncan P. N. Exon Smith60635e32015-04-21 18:44:06 +000098DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000099 const DIGlobalVariable *GV) {
David Blaikie37c52312014-10-04 15:49:50 +0000100 // Check for pre-existence.
101 if (DIE *Die = getDIE(GV))
102 return Die;
103
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000104 assert(GV);
David Blaikie37c52312014-10-04 15:49:50 +0000105
Duncan P. N. Exon Smithbe9e4fe2015-04-20 18:32:29 +0000106 auto *GVContext = GV->getScope();
107 auto *GTy = DD->resolve(GV->getType());
David Blaikie37c52312014-10-04 15:49:50 +0000108
David Blaikie49cfc8c2014-10-23 19:12:43 +0000109 // Construct the context before querying for the existence of the DIE in
110 // case such construction creates the DIE.
111 DIE *ContextDIE = getOrCreateContextDIE(GVContext);
112
113 // Add to map.
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000114 DIE *VariableDIE = &createAndAddDIE(GV->getTag(), *ContextDIE, GV);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000115 DIScope *DeclContext;
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000116 if (auto *SDMDecl = GV->getStaticDataMemberDeclaration()) {
117 DeclContext = resolve(SDMDecl->getScope());
118 assert(SDMDecl->isStaticMember() && "Expected static member decl");
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000119 assert(GV->isDefinition());
David Blaikie37c52312014-10-04 15:49:50 +0000120 // We need the declaration DIE that is in the static member's class.
David Blaikie49cfc8c2014-10-23 19:12:43 +0000121 DIE *VariableSpecDIE = getOrCreateStaticMemberDIE(SDMDecl);
122 addDIEEntry(*VariableDIE, dwarf::DW_AT_specification, *VariableSpecDIE);
123 } else {
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000124 DeclContext = GV->getScope();
David Blaikie37c52312014-10-04 15:49:50 +0000125 // Add name and type.
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000126 addString(*VariableDIE, dwarf::DW_AT_name, GV->getDisplayName());
David Blaikie37c52312014-10-04 15:49:50 +0000127 addType(*VariableDIE, GTy);
128
129 // Add scoping info.
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000130 if (!GV->isLocalToUnit())
David Blaikie37c52312014-10-04 15:49:50 +0000131 addFlag(*VariableDIE, dwarf::DW_AT_external);
132
133 // Add line number info.
134 addSourceLine(*VariableDIE, GV);
135 }
136
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000137 if (!GV->isDefinition())
David Blaikie49cfc8c2014-10-23 19:12:43 +0000138 addFlag(*VariableDIE, dwarf::DW_AT_declaration);
David Blaikie877354a2015-04-14 18:08:25 +0000139 else
140 addGlobalName(GV->getName(), *VariableDIE, DeclContext);
David Blaikie49cfc8c2014-10-23 19:12:43 +0000141
David Blaikie37c52312014-10-04 15:49:50 +0000142 // Add location.
143 bool addToAccelTable = false;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000144 if (auto *Global = dyn_cast_or_null<GlobalVariable>(GV->getVariable())) {
David Blaikie37c52312014-10-04 15:49:50 +0000145 addToAccelTable = true;
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000146 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
Duncan P. N. Exon Smithcca5f682015-04-13 20:39:25 +0000147 const MCSymbol *Sym = Asm->getSymbol(Global);
148 if (Global->isThreadLocal()) {
David Blaikie37c52312014-10-04 15:49:50 +0000149 // FIXME: Make this work with -gsplit-dwarf.
150 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
151 assert((PointerSize == 4 || PointerSize == 8) &&
152 "Add support for other sizes if necessary");
153 // Based on GCC's support for TLS:
154 if (!DD->useSplitDwarf()) {
155 // 1) Start with a constNu of the appropriate pointer size
156 addUInt(*Loc, dwarf::DW_FORM_data1,
157 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
158 // 2) containing the (relocated) offset of the TLS variable
159 // within the module's TLS block.
160 addExpr(*Loc, dwarf::DW_FORM_udata,
161 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym));
162 } else {
163 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
164 addUInt(*Loc, dwarf::DW_FORM_udata,
165 DD->getAddressPool().getIndex(Sym, /* TLS */ true));
166 }
Paul Robinson78cc0822015-03-04 20:55:11 +0000167 // 3) followed by an OP to make the debugger do a TLS lookup.
168 addUInt(*Loc, dwarf::DW_FORM_data1,
169 DD->useGNUTLSOpcode() ? dwarf::DW_OP_GNU_push_tls_address
170 : dwarf::DW_OP_form_tls_address);
David Blaikie37c52312014-10-04 15:49:50 +0000171 } else {
172 DD->addArangeLabel(SymbolCU(this, Sym));
173 addOpAddress(*Loc, Sym);
174 }
David Blaikie49cfc8c2014-10-23 19:12:43 +0000175
176 addBlock(*VariableDIE, dwarf::DW_AT_location, Loc);
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000177 addLinkageName(*VariableDIE, GV->getLinkageName());
David Blaikie37c52312014-10-04 15:49:50 +0000178 } else if (const ConstantInt *CI =
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000179 dyn_cast_or_null<ConstantInt>(GV->getVariable())) {
David Blaikie49cfc8c2014-10-23 19:12:43 +0000180 addConstantValue(*VariableDIE, CI, GTy);
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000181 } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getVariable())) {
David Blaikie37c52312014-10-04 15:49:50 +0000182 addToAccelTable = true;
183 // GV is a merged global.
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000184 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
David Blaikie37c52312014-10-04 15:49:50 +0000185 Value *Ptr = CE->getOperand(0);
186 MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr));
187 DD->addArangeLabel(SymbolCU(this, Sym));
188 addOpAddress(*Loc, Sym);
189 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
190 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
191 addUInt(*Loc, dwarf::DW_FORM_udata,
192 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
193 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
194 addBlock(*VariableDIE, dwarf::DW_AT_location, Loc);
195 }
196
David Blaikie37c52312014-10-04 15:49:50 +0000197 if (addToAccelTable) {
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000198 DD->addAccelName(GV->getName(), *VariableDIE);
David Blaikie37c52312014-10-04 15:49:50 +0000199
200 // If the linkage name is different than the name, go ahead and output
201 // that as well into the name table.
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000202 if (GV->getLinkageName() != "" && GV->getName() != GV->getLinkageName())
203 DD->addAccelName(GV->getLinkageName(), *VariableDIE);
David Blaikie37c52312014-10-04 15:49:50 +0000204 }
205
David Blaikie49cfc8c2014-10-23 19:12:43 +0000206 return VariableDIE;
David Blaikie37c52312014-10-04 15:49:50 +0000207}
208
209void DwarfCompileUnit::addRange(RangeSpan Range) {
210 bool SameAsPrevCU = this == DD->getPrevCU();
211 DD->setPrevCU(this);
212 // If we have no current ranges just add the range and return, otherwise,
213 // check the current section and CU against the previous section and CU we
214 // emitted into and the subprogram was contained within. If these are the
215 // same then extend our current range, otherwise add this as a new range.
216 if (CURanges.empty() || !SameAsPrevCU ||
217 (&CURanges.back().getEnd()->getSection() !=
218 &Range.getEnd()->getSection())) {
219 CURanges.push_back(Range);
220 return;
221 }
222
223 CURanges.back().setEnd(Range.getEnd());
224}
225
David Blaikie6c0ee4e2014-10-08 22:46:27 +0000226void DwarfCompileUnit::addSectionLabel(DIE &Die, dwarf::Attribute Attribute,
227 const MCSymbol *Label,
228 const MCSymbol *Sec) {
229 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
230 addLabel(Die, Attribute,
231 DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
232 : dwarf::DW_FORM_data4,
233 Label);
234 else
235 addSectionDelta(Die, Attribute, Label, Sec);
236}
237
Rafael Espindola063d7252015-03-10 16:58:10 +0000238void DwarfCompileUnit::initStmtList() {
David Blaikie37c52312014-10-04 15:49:50 +0000239 // Define start line table label for each Compile Unit.
240 MCSymbol *LineTableStartSym =
Lang Hames9ff69c82015-04-24 19:11:51 +0000241 Asm->OutStreamer->getDwarfLineTableSymbol(getUniqueID());
David Blaikie37c52312014-10-04 15:49:50 +0000242
243 stmtListIndex = UnitDie.getValues().size();
244
245 // DW_AT_stmt_list is a offset of line number information for this
246 // compile unit in debug_line section. For split dwarf this is
247 // left in the skeleton CU and so not included.
248 // The line table entries are not always emitted in assembly, so it
249 // is not okay to use line_table_start here.
Rafael Espindola063d7252015-03-10 16:58:10 +0000250 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
David Blaikie6c0ee4e2014-10-08 22:46:27 +0000251 addSectionLabel(UnitDie, dwarf::DW_AT_stmt_list, LineTableStartSym,
Rafael Espindola063d7252015-03-10 16:58:10 +0000252 TLOF.getDwarfLineSection()->getBeginSymbol());
David Blaikie37c52312014-10-04 15:49:50 +0000253}
254
255void DwarfCompileUnit::applyStmtList(DIE &D) {
256 D.addValue(dwarf::DW_AT_stmt_list,
257 UnitDie.getAbbrev().getData()[stmtListIndex].getForm(),
258 UnitDie.getValues()[stmtListIndex]);
259}
260
David Blaikie14499a72014-10-04 15:58:47 +0000261void DwarfCompileUnit::attachLowHighPC(DIE &D, const MCSymbol *Begin,
262 const MCSymbol *End) {
263 assert(Begin && "Begin label should not be null!");
264 assert(End && "End label should not be null!");
265 assert(Begin->isDefined() && "Invalid starting label");
266 assert(End->isDefined() && "Invalid end label");
267
268 addLabelAddress(D, dwarf::DW_AT_low_pc, Begin);
269 if (DD->getDwarfVersion() < 4)
270 addLabelAddress(D, dwarf::DW_AT_high_pc, End);
271 else
272 addLabelDelta(D, dwarf::DW_AT_high_pc, End, Begin);
273}
274
David Blaikiecda2aa82014-10-04 16:24:00 +0000275// Find DIE for the given subprogram and attach appropriate DW_AT_low_pc
276// and DW_AT_high_pc attributes. If there are global variables in this
277// scope then create and insert DIEs for these variables.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000278DIE &DwarfCompileUnit::updateSubprogramScopeDIE(const DISubprogram *SP) {
David Blaikie3a443c22014-11-04 22:12:25 +0000279 DIE *SPDie = getOrCreateSubprogramDIE(SP, includeMinimalInlineScopes());
David Blaikiecda2aa82014-10-04 16:24:00 +0000280
Rafael Espindola07c03d32015-03-05 02:05:42 +0000281 attachLowHighPC(*SPDie, Asm->getFunctionBegin(), Asm->getFunctionEnd());
David Blaikiecda2aa82014-10-04 16:24:00 +0000282 if (!DD->getCurrentFunction()->getTarget().Options.DisableFramePointerElim(
283 *DD->getCurrentFunction()))
284 addFlag(*SPDie, dwarf::DW_AT_APPLE_omit_frame_ptr);
285
286 // Only include DW_AT_frame_base in full debug info
David Blaikie3a443c22014-11-04 22:12:25 +0000287 if (!includeMinimalInlineScopes()) {
Eric Christopherd4e723f2015-02-20 22:36:11 +0000288 const TargetRegisterInfo *RI = Asm->MF->getSubtarget().getRegisterInfo();
David Blaikiecda2aa82014-10-04 16:24:00 +0000289 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Adrian Prantl8efadbf2015-01-14 00:15:16 +0000290 if (RI->isPhysicalRegister(Location.getReg()))
291 addAddress(*SPDie, dwarf::DW_AT_frame_base, Location);
David Blaikiecda2aa82014-10-04 16:24:00 +0000292 }
293
294 // Add name to the name table, we do this here because we're guaranteed
295 // to have concrete versions of our DW_TAG_subprogram nodes.
296 DD->addSubprogramNames(SP, *SPDie);
297
298 return *SPDie;
299}
300
David Blaikie9c65b132014-10-08 22:20:02 +0000301// Construct a DIE for this scope.
302void DwarfCompileUnit::constructScopeDIE(
303 LexicalScope *Scope, SmallVectorImpl<std::unique_ptr<DIE>> &FinalChildren) {
304 if (!Scope || !Scope->getScopeNode())
305 return;
306
Duncan P. N. Exon Smithbe9e4fe2015-04-20 18:32:29 +0000307 auto *DS = Scope->getScopeNode();
David Blaikie9c65b132014-10-08 22:20:02 +0000308
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000309 assert((Scope->getInlinedAt() || !isa<DISubprogram>(DS)) &&
David Blaikie9c65b132014-10-08 22:20:02 +0000310 "Only handle inlined subprograms here, use "
311 "constructSubprogramScopeDIE for non-inlined "
312 "subprograms");
313
314 SmallVector<std::unique_ptr<DIE>, 8> Children;
315
316 // We try to create the scope DIE first, then the children DIEs. This will
317 // avoid creating un-used children then removing them later when we find out
318 // the scope DIE is null.
319 std::unique_ptr<DIE> ScopeDIE;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000320 if (Scope->getParent() && isa<DISubprogram>(DS)) {
David Blaikie01b48a82014-10-09 16:50:53 +0000321 ScopeDIE = constructInlinedScopeDIE(Scope);
David Blaikie9c65b132014-10-08 22:20:02 +0000322 if (!ScopeDIE)
323 return;
324 // We create children when the scope DIE is not null.
David Blaikie8b2fdb82014-10-09 18:24:28 +0000325 createScopeChildrenDIE(Scope, Children);
David Blaikie9c65b132014-10-08 22:20:02 +0000326 } else {
327 // Early exit when we know the scope DIE is going to be null.
328 if (DD->isLexicalScopeDIENull(Scope))
329 return;
330
331 unsigned ChildScopeCount;
332
333 // We create children here when we know the scope DIE is not going to be
334 // null and the children will be added to the scope DIE.
David Blaikie8b2fdb82014-10-09 18:24:28 +0000335 createScopeChildrenDIE(Scope, Children, &ChildScopeCount);
David Blaikie9c65b132014-10-08 22:20:02 +0000336
David Blaikie3a443c22014-11-04 22:12:25 +0000337 // Skip imported directives in gmlt-like data.
338 if (!includeMinimalInlineScopes()) {
339 // There is no need to emit empty lexical block DIE.
340 for (const auto &E : DD->findImportedEntitiesForScope(DS))
341 Children.push_back(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000342 constructImportedEntityDIE(cast<DIImportedEntity>(E.second)));
David Blaikie3a443c22014-11-04 22:12:25 +0000343 }
344
David Blaikie9c65b132014-10-08 22:20:02 +0000345 // If there are only other scopes as children, put them directly in the
346 // parent instead, as this scope would serve no purpose.
347 if (Children.size() == ChildScopeCount) {
348 FinalChildren.insert(FinalChildren.end(),
349 std::make_move_iterator(Children.begin()),
350 std::make_move_iterator(Children.end()));
351 return;
352 }
David Blaikie0fbf8bd2014-10-09 17:08:42 +0000353 ScopeDIE = constructLexicalScopeDIE(Scope);
David Blaikie9c65b132014-10-08 22:20:02 +0000354 assert(ScopeDIE && "Scope DIE should not be null.");
355 }
356
357 // Add children
358 for (auto &I : Children)
359 ScopeDIE->addChild(std::move(I));
360
361 FinalChildren.push_back(std::move(ScopeDIE));
362}
363
David Blaikiee5feec52014-10-08 23:30:05 +0000364void DwarfCompileUnit::addSectionDelta(DIE &Die, dwarf::Attribute Attribute,
365 const MCSymbol *Hi, const MCSymbol *Lo) {
David Blaikiee5feec52014-10-08 23:30:05 +0000366 Die.addValue(Attribute, DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
367 : dwarf::DW_FORM_data4,
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000368 new (DIEValueAllocator) DIEDelta(Hi, Lo));
David Blaikiee5feec52014-10-08 23:30:05 +0000369}
370
David Blaikie5b02a192014-11-03 23:10:59 +0000371void DwarfCompileUnit::addScopeRangeList(DIE &ScopeDIE,
372 SmallVector<RangeSpan, 2> Range) {
Rafael Espindola063d7252015-03-10 16:58:10 +0000373 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
374
David Blaikie52400202014-10-09 00:11:39 +0000375 // Emit offset in .debug_range as a relocatable label. emitDIE will handle
376 // emitting it appropriately.
Rafael Espindola063d7252015-03-10 16:58:10 +0000377 const MCSymbol *RangeSectionSym =
378 TLOF.getDwarfRangesSection()->getBeginSymbol();
David Blaikie52400202014-10-09 00:11:39 +0000379
Rafael Espindola9ab09232015-03-17 20:07:06 +0000380 RangeSpanList List(Asm->createTempSymbol("debug_ranges"), std::move(Range));
David Blaikie5b02a192014-11-03 23:10:59 +0000381
David Blaikie52400202014-10-09 00:11:39 +0000382 // Under fission, ranges are specified by constant offsets relative to the
383 // CU's DW_AT_GNU_ranges_base.
David Blaikie5b02a192014-11-03 23:10:59 +0000384 if (isDwoUnit())
385 addSectionDelta(ScopeDIE, dwarf::DW_AT_ranges, List.getSym(),
386 RangeSectionSym);
David Blaikie52400202014-10-09 00:11:39 +0000387 else
David Blaikie5b02a192014-11-03 23:10:59 +0000388 addSectionLabel(ScopeDIE, dwarf::DW_AT_ranges, List.getSym(),
389 RangeSectionSym);
David Blaikie52400202014-10-09 00:11:39 +0000390
391 // Add the range list to the set of ranges to be emitted.
David Blaikie542616d2014-11-03 21:52:56 +0000392 (Skeleton ? Skeleton : this)->CURangeLists.push_back(std::move(List));
David Blaikie52400202014-10-09 00:11:39 +0000393}
394
David Blaikiede123752014-10-09 00:21:42 +0000395void DwarfCompileUnit::attachRangesOrLowHighPC(
David Blaikie5b02a192014-11-03 23:10:59 +0000396 DIE &Die, SmallVector<RangeSpan, 2> Ranges) {
397 if (Ranges.size() == 1) {
398 const auto &single = Ranges.front();
399 attachLowHighPC(Die, single.getStart(), single.getEnd());
400 } else
401 addScopeRangeList(Die, std::move(Ranges));
402}
403
404void DwarfCompileUnit::attachRangesOrLowHighPC(
David Blaikiede123752014-10-09 00:21:42 +0000405 DIE &Die, const SmallVectorImpl<InsnRange> &Ranges) {
David Blaikie5b02a192014-11-03 23:10:59 +0000406 SmallVector<RangeSpan, 2> List;
407 List.reserve(Ranges.size());
408 for (const InsnRange &R : Ranges)
409 List.push_back(RangeSpan(DD->getLabelBeforeInsn(R.first),
410 DD->getLabelAfterInsn(R.second)));
411 attachRangesOrLowHighPC(Die, std::move(List));
David Blaikiede123752014-10-09 00:21:42 +0000412}
413
David Blaikie01b48a82014-10-09 16:50:53 +0000414// This scope represents inlined body of a function. Construct DIE to
415// represent this concrete inlined copy of the function.
416std::unique_ptr<DIE>
417DwarfCompileUnit::constructInlinedScopeDIE(LexicalScope *Scope) {
418 assert(Scope->getScopeNode());
Duncan P. N. Exon Smithbe9e4fe2015-04-20 18:32:29 +0000419 auto *DS = Scope->getScopeNode();
420 auto *InlinedSP = getDISubprogram(DS);
David Blaikie01b48a82014-10-09 16:50:53 +0000421 // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram
422 // was inlined from another compile unit.
David Blaikief6dac292014-11-01 17:21:26 +0000423 DIE *OriginDIE = DU->getAbstractSPDies()[InlinedSP];
David Blaikie01b48a82014-10-09 16:50:53 +0000424 assert(OriginDIE && "Unable to find original DIE for an inlined subprogram.");
425
426 auto ScopeDIE = make_unique<DIE>(dwarf::DW_TAG_inlined_subroutine);
427 addDIEEntry(*ScopeDIE, dwarf::DW_AT_abstract_origin, *OriginDIE);
428
429 attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges());
430
431 // Add the call site information to the DIE.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000432 const DILocation *IA = Scope->getInlinedAt();
David Blaikie01b48a82014-10-09 16:50:53 +0000433 addUInt(*ScopeDIE, dwarf::DW_AT_call_file, None,
Duncan P. N. Exon Smithb7e221b2015-04-14 01:35:55 +0000434 getOrCreateSourceID(IA->getFilename(), IA->getDirectory()));
435 addUInt(*ScopeDIE, dwarf::DW_AT_call_line, None, IA->getLine());
David Blaikie01b48a82014-10-09 16:50:53 +0000436
437 // Add name to the name table, we do this here because we're guaranteed
438 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
439 DD->addSubprogramNames(InlinedSP, *ScopeDIE);
440
441 return ScopeDIE;
442}
443
David Blaikie0fbf8bd2014-10-09 17:08:42 +0000444// Construct new DW_TAG_lexical_block for this scope and attach
445// DW_AT_low_pc/DW_AT_high_pc labels.
446std::unique_ptr<DIE>
447DwarfCompileUnit::constructLexicalScopeDIE(LexicalScope *Scope) {
448 if (DD->isLexicalScopeDIENull(Scope))
449 return nullptr;
450
451 auto ScopeDIE = make_unique<DIE>(dwarf::DW_TAG_lexical_block);
452 if (Scope->isAbstractScope())
453 return ScopeDIE;
454
455 attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges());
456
457 return ScopeDIE;
458}
459
David Blaikieee7df552014-10-09 17:56:36 +0000460/// constructVariableDIE - Construct a DIE for the given DbgVariable.
461std::unique_ptr<DIE> DwarfCompileUnit::constructVariableDIE(DbgVariable &DV,
462 bool Abstract) {
463 auto D = constructVariableDIEImpl(DV, Abstract);
464 DV.setDIE(*D);
465 return D;
466}
467
468std::unique_ptr<DIE>
469DwarfCompileUnit::constructVariableDIEImpl(const DbgVariable &DV,
470 bool Abstract) {
471 // Define variable debug information entry.
472 auto VariableDie = make_unique<DIE>(DV.getTag());
473
474 if (Abstract) {
475 applyVariableAttributes(DV, *VariableDie);
476 return VariableDie;
477 }
478
479 // Add variable address.
480
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +0000481 unsigned Offset = DV.getDebugLocListIndex();
David Blaikieee7df552014-10-09 17:56:36 +0000482 if (Offset != ~0U) {
483 addLocationList(*VariableDie, dwarf::DW_AT_location, Offset);
484 return VariableDie;
485 }
486
487 // Check if variable is described by a DBG_VALUE instruction.
488 if (const MachineInstr *DVInsn = DV.getMInsn()) {
489 assert(DVInsn->getNumOperands() == 4);
490 if (DVInsn->getOperand(0).isReg()) {
491 const MachineOperand RegOp = DVInsn->getOperand(0);
492 // If the second operand is an immediate, this is an indirect value.
493 if (DVInsn->getOperand(1).isImm()) {
494 MachineLocation Location(RegOp.getReg(),
495 DVInsn->getOperand(1).getImm());
496 addVariableAddress(DV, *VariableDie, Location);
497 } else if (RegOp.getReg())
498 addVariableAddress(DV, *VariableDie, MachineLocation(RegOp.getReg()));
499 } else if (DVInsn->getOperand(0).isImm())
500 addConstantValue(*VariableDie, DVInsn->getOperand(0), DV.getType());
501 else if (DVInsn->getOperand(0).isFPImm())
502 addConstantFPValue(*VariableDie, DVInsn->getOperand(0));
503 else if (DVInsn->getOperand(0).isCImm())
504 addConstantValue(*VariableDie, DVInsn->getOperand(0).getCImm(),
505 DV.getType());
506
507 return VariableDie;
508 }
509
510 // .. else use frame index.
Adrian Prantlca7e4702015-02-10 23:18:28 +0000511 if (DV.getFrameIndex().back() == ~0)
512 return VariableDie;
513
514 auto Expr = DV.getExpression().begin();
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000515 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
Adrian Prantlca7e4702015-02-10 23:18:28 +0000516 DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
517 for (auto FI : DV.getFrameIndex()) {
David Blaikieee7df552014-10-09 17:56:36 +0000518 unsigned FrameReg = 0;
Eric Christopherd4e723f2015-02-20 22:36:11 +0000519 const TargetFrameLowering *TFI = Asm->MF->getSubtarget().getFrameLowering();
David Blaikieee7df552014-10-09 17:56:36 +0000520 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
Duncan P. N. Exon Smith57bab0b2015-02-17 22:30:56 +0000521 assert(Expr != DV.getExpression().end() &&
522 "Wrong number of expressions");
Adrian Prantlca7e4702015-02-10 23:18:28 +0000523 DwarfExpr.AddMachineRegIndirect(FrameReg, Offset);
Duncan P. N. Exon Smith76c91842015-04-07 03:45:57 +0000524 DwarfExpr.AddExpression((*Expr)->expr_op_begin(), (*Expr)->expr_op_end());
Duncan P. N. Exon Smith57bab0b2015-02-17 22:30:56 +0000525 ++Expr;
David Blaikieee7df552014-10-09 17:56:36 +0000526 }
Adrian Prantlca7e4702015-02-10 23:18:28 +0000527 addBlock(*VariableDie, dwarf::DW_AT_location, Loc);
David Blaikieee7df552014-10-09 17:56:36 +0000528
529 return VariableDie;
530}
531
David Blaikie4a1a44e2014-10-09 17:56:39 +0000532std::unique_ptr<DIE> DwarfCompileUnit::constructVariableDIE(
533 DbgVariable &DV, const LexicalScope &Scope, DIE *&ObjectPointer) {
534 auto Var = constructVariableDIE(DV, Scope.isAbstractScope());
535 if (DV.isObjectPointer())
536 ObjectPointer = Var.get();
537 return Var;
538}
539
David Blaikie8b2fdb82014-10-09 18:24:28 +0000540DIE *DwarfCompileUnit::createScopeChildrenDIE(
541 LexicalScope *Scope, SmallVectorImpl<std::unique_ptr<DIE>> &Children,
542 unsigned *ChildScopeCount) {
543 DIE *ObjectPointer = nullptr;
544
David Blaikie80e5b1e2014-10-24 17:57:34 +0000545 for (DbgVariable *DV : DU->getScopeVariables().lookup(Scope))
David Blaikie8b2fdb82014-10-09 18:24:28 +0000546 Children.push_back(constructVariableDIE(*DV, *Scope, ObjectPointer));
547
548 unsigned ChildCountWithoutScopes = Children.size();
549
550 for (LexicalScope *LS : Scope->getChildren())
551 constructScopeDIE(LS, Children);
552
553 if (ChildScopeCount)
554 *ChildScopeCount = Children.size() - ChildCountWithoutScopes;
555
556 return ObjectPointer;
557}
558
David Blaikie1d072342014-10-09 20:21:36 +0000559void DwarfCompileUnit::constructSubprogramScopeDIE(LexicalScope *Scope) {
560 assert(Scope && Scope->getScopeNode());
561 assert(!Scope->getInlinedAt());
562 assert(!Scope->isAbstractScope());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000563 auto *Sub = cast<DISubprogram>(Scope->getScopeNode());
David Blaikie1d072342014-10-09 20:21:36 +0000564
565 DD->getProcessedSPNodes().insert(Sub);
566
567 DIE &ScopeDIE = updateSubprogramScopeDIE(Sub);
568
David Blaikie1d072342014-10-09 20:21:36 +0000569 // If this is a variadic function, add an unspecified parameter.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000570 DITypeRefArray FnArgs = Sub->getType()->getTypeArray();
David Blaikie1dd573d2014-10-23 22:27:50 +0000571
572 // Collect lexical scope children first.
573 // ObjectPointer might be a local (non-argument) local variable if it's a
574 // block's synthetic this pointer.
575 if (DIE *ObjectPointer = createAndAddScopeChildren(Scope, ScopeDIE))
576 addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer, *ObjectPointer);
577
David Blaikie1d072342014-10-09 20:21:36 +0000578 // If we have a single element of null, it is a function that returns void.
579 // If we have more than one elements and the last one is null, it is a
580 // variadic function.
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000581 if (FnArgs.size() > 1 && !FnArgs[FnArgs.size() - 1] &&
David Blaikie3a443c22014-11-04 22:12:25 +0000582 !includeMinimalInlineScopes())
David Blaikie1d072342014-10-09 20:21:36 +0000583 ScopeDIE.addChild(make_unique<DIE>(dwarf::DW_TAG_unspecified_parameters));
David Blaikie1d072342014-10-09 20:21:36 +0000584}
585
David Blaikie78b65b62014-10-09 20:26:15 +0000586DIE *DwarfCompileUnit::createAndAddScopeChildren(LexicalScope *Scope,
587 DIE &ScopeDIE) {
588 // We create children when the scope DIE is not null.
589 SmallVector<std::unique_ptr<DIE>, 8> Children;
590 DIE *ObjectPointer = createScopeChildrenDIE(Scope, Children);
591
592 // Add children
593 for (auto &I : Children)
594 ScopeDIE.addChild(std::move(I));
595
596 return ObjectPointer;
597}
598
David Blaikie49be5b32014-10-31 21:57:02 +0000599void
David Blaikie58410f22014-10-10 06:39:26 +0000600DwarfCompileUnit::constructAbstractSubprogramScopeDIE(LexicalScope *Scope) {
David Blaikief6dac292014-11-01 17:21:26 +0000601 DIE *&AbsDef = DU->getAbstractSPDies()[Scope->getScopeNode()];
David Blaikie49be5b32014-10-31 21:57:02 +0000602 if (AbsDef)
603 return;
604
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000605 auto *SP = cast<DISubprogram>(Scope->getScopeNode());
David Blaikie58410f22014-10-10 06:39:26 +0000606
607 DIE *ContextDIE;
608
David Blaikie3a443c22014-11-04 22:12:25 +0000609 if (includeMinimalInlineScopes())
610 ContextDIE = &getUnitDie();
David Blaikie58410f22014-10-10 06:39:26 +0000611 // Some of this is duplicated from DwarfUnit::getOrCreateSubprogramDIE, with
Duncan P. N. Exon Smith7c60f202015-04-18 00:35:36 +0000612 // the important distinction that the debug node is not associated with the
613 // DIE (since the debug node will be associated with the concrete DIE, if
David Blaikie58410f22014-10-10 06:39:26 +0000614 // any). It could be refactored to some common utility function.
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000615 else if (auto *SPDecl = SP->getDeclaration()) {
David Blaikie58410f22014-10-10 06:39:26 +0000616 ContextDIE = &getUnitDie();
617 getOrCreateSubprogramDIE(SPDecl);
618 } else
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000619 ContextDIE = getOrCreateContextDIE(resolve(SP->getScope()));
David Blaikie58410f22014-10-10 06:39:26 +0000620
Duncan P. N. Exon Smith7c60f202015-04-18 00:35:36 +0000621 // Passing null as the associated node because the abstract definition
David Blaikie58410f22014-10-10 06:39:26 +0000622 // shouldn't be found by lookup.
Duncan P. N. Exon Smith7c60f202015-04-18 00:35:36 +0000623 AbsDef = &createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, nullptr);
David Blaikie49be5b32014-10-31 21:57:02 +0000624 applySubprogramAttributesToDefinition(SP, *AbsDef);
David Blaikie58410f22014-10-10 06:39:26 +0000625
David Blaikie3a443c22014-11-04 22:12:25 +0000626 if (!includeMinimalInlineScopes())
David Blaikie49be5b32014-10-31 21:57:02 +0000627 addUInt(*AbsDef, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined);
628 if (DIE *ObjectPointer = createAndAddScopeChildren(Scope, *AbsDef))
629 addDIEEntry(*AbsDef, dwarf::DW_AT_object_pointer, *ObjectPointer);
David Blaikie58410f22014-10-10 06:39:26 +0000630}
631
Frederic Riss987fe222014-10-24 21:31:09 +0000632std::unique_ptr<DIE>
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000633DwarfCompileUnit::constructImportedEntityDIE(const DIImportedEntity *Module) {
Duncan P. N. Exon Smithde8e4272015-04-14 01:46:44 +0000634 std::unique_ptr<DIE> IMDie = make_unique<DIE>((dwarf::Tag)Module->getTag());
Frederic Riss987fe222014-10-24 21:31:09 +0000635 insertDIE(Module, IMDie.get());
636 DIE *EntityDie;
Duncan P. N. Exon Smithde8e4272015-04-14 01:46:44 +0000637 auto *Entity = resolve(Module->getEntity());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000638 if (auto *NS = dyn_cast<DINamespace>(Entity))
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000639 EntityDie = getOrCreateNameSpace(NS);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000640 else if (auto *SP = dyn_cast<DISubprogram>(Entity))
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000641 EntityDie = getOrCreateSubprogramDIE(SP);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000642 else if (auto *T = dyn_cast<DIType>(Entity))
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000643 EntityDie = getOrCreateTypeDIE(T);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000644 else if (auto *GV = dyn_cast<DIGlobalVariable>(Entity))
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000645 EntityDie = getOrCreateGlobalVariableDIE(GV);
Frederic Riss987fe222014-10-24 21:31:09 +0000646 else
647 EntityDie = getDIE(Entity);
648 assert(EntityDie);
Duncan P. N. Exon Smithde8e4272015-04-14 01:46:44 +0000649 addSourceLine(*IMDie, Module->getLine(), Module->getScope()->getFilename(),
650 Module->getScope()->getDirectory());
Frederic Riss987fe222014-10-24 21:31:09 +0000651 addDIEEntry(*IMDie, dwarf::DW_AT_import, *EntityDie);
Duncan P. N. Exon Smithde8e4272015-04-14 01:46:44 +0000652 StringRef Name = Module->getName();
Frederic Riss987fe222014-10-24 21:31:09 +0000653 if (!Name.empty())
654 addString(*IMDie, dwarf::DW_AT_name, Name);
655
656 return IMDie;
657}
658
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000659void DwarfCompileUnit::finishSubprogramDefinition(const DISubprogram *SP) {
David Blaikie4191cbc2014-10-10 06:39:29 +0000660 DIE *D = getDIE(SP);
David Blaikief6dac292014-11-01 17:21:26 +0000661 if (DIE *AbsSPDIE = DU->getAbstractSPDies().lookup(SP)) {
David Blaikie4191cbc2014-10-10 06:39:29 +0000662 if (D)
663 // If this subprogram has an abstract definition, reference that
664 addDIEEntry(*D, dwarf::DW_AT_abstract_origin, *AbsSPDIE);
665 } else {
David Blaikie3a443c22014-11-04 22:12:25 +0000666 if (!D && !includeMinimalInlineScopes())
David Blaikie4191cbc2014-10-10 06:39:29 +0000667 // Lazily construct the subprogram if we didn't see either concrete or
668 // inlined versions during codegen. (except in -gmlt ^ where we want
669 // to omit these entirely)
670 D = getOrCreateSubprogramDIE(SP);
671 if (D)
672 // And attach the attributes
673 applySubprogramAttributesToDefinition(SP, *D);
674 }
675}
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000676void DwarfCompileUnit::collectDeadVariables(const DISubprogram *SP) {
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000677 assert(SP && "CU's subprogram list contains a non-subprogram");
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000678 assert(SP->isDefinition() &&
David Blaikie1d96cc22014-10-31 22:30:30 +0000679 "CU's subprogram list contains a subprogram declaration");
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000680 auto Variables = SP->getVariables();
681 if (Variables.size() == 0)
David Blaikie1d96cc22014-10-31 22:30:30 +0000682 return;
683
David Blaikief6dac292014-11-01 17:21:26 +0000684 DIE *SPDIE = DU->getAbstractSPDies().lookup(SP);
David Blaikie1d96cc22014-10-31 22:30:30 +0000685 if (!SPDIE)
686 SPDIE = getDIE(SP);
687 assert(SPDIE);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000688 for (const DILocalVariable *DV : Variables) {
Duncan P. N. Exon Smith60635e32015-04-21 18:44:06 +0000689 DbgVariable NewVar(DV, /* IA */ nullptr, /* Expr */ nullptr, DD);
David Blaikie1d96cc22014-10-31 22:30:30 +0000690 auto VariableDie = constructVariableDIE(NewVar);
691 applyVariableAttributes(NewVar, *VariableDie);
692 SPDIE->addChild(std::move(VariableDie));
693 }
694}
David Blaikie4191cbc2014-10-10 06:39:29 +0000695
Rafael Espindola063d7252015-03-10 16:58:10 +0000696void DwarfCompileUnit::emitHeader(bool UseOffsets) {
David Blaikieb6726a92014-11-02 02:26:24 +0000697 // Don't bother labeling the .dwo unit, as its offset isn't used.
Rafael Espindola3c311142015-03-10 03:11:11 +0000698 if (!Skeleton) {
Rafael Espindola9ab09232015-03-17 20:07:06 +0000699 LabelBegin = Asm->createTempSymbol("cu_begin");
Lang Hames9ff69c82015-04-24 19:11:51 +0000700 Asm->OutStreamer->EmitLabel(LabelBegin);
Rafael Espindola3c311142015-03-10 03:11:11 +0000701 }
David Blaikieae57e662014-11-01 23:59:23 +0000702
Rafael Espindola063d7252015-03-10 16:58:10 +0000703 DwarfUnit::emitHeader(UseOffsets);
David Blaikieae57e662014-11-01 23:59:23 +0000704}
705
David Blaikie192b45c2014-11-02 06:16:39 +0000706/// addGlobalName - Add a new global name to the compile unit.
707void DwarfCompileUnit::addGlobalName(StringRef Name, DIE &Die,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000708 const DIScope *Context) {
David Blaikie3a443c22014-11-04 22:12:25 +0000709 if (includeMinimalInlineScopes())
David Blaikie192b45c2014-11-02 06:16:39 +0000710 return;
711 std::string FullName = getParentContextString(Context) + Name.str();
712 GlobalNames[FullName] = &Die;
713}
714
715/// Add a new global type to the unit.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000716void DwarfCompileUnit::addGlobalType(const DIType *Ty, const DIE &Die,
717 const DIScope *Context) {
David Blaikie3a443c22014-11-04 22:12:25 +0000718 if (includeMinimalInlineScopes())
David Blaikie192b45c2014-11-02 06:16:39 +0000719 return;
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000720 std::string FullName = getParentContextString(Context) + Ty->getName().str();
David Blaikie192b45c2014-11-02 06:16:39 +0000721 GlobalTypes[FullName] = &Die;
722}
David Blaikieae57e662014-11-01 23:59:23 +0000723
David Blaikie7d48be22014-11-02 06:37:23 +0000724/// addVariableAddress - Add DW_AT_location attribute for a
725/// DbgVariable based on provided MachineLocation.
726void DwarfCompileUnit::addVariableAddress(const DbgVariable &DV, DIE &Die,
727 MachineLocation Location) {
728 if (DV.variableHasComplexAddress())
729 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
730 else if (DV.isBlockByrefVariable())
731 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
732 else
Adrian Prantl5883af32015-01-19 17:57:29 +0000733 addAddress(Die, dwarf::DW_AT_location, Location);
David Blaikie7d48be22014-11-02 06:37:23 +0000734}
David Blaikief7435ee2014-11-02 06:46:40 +0000735
736/// Add an address attribute to a die based on the location provided.
737void DwarfCompileUnit::addAddress(DIE &Die, dwarf::Attribute Attribute,
Adrian Prantl5883af32015-01-19 17:57:29 +0000738 const MachineLocation &Location) {
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000739 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
David Blaikief7435ee2014-11-02 06:46:40 +0000740
Adrian Prantlab255fc2014-12-05 01:02:46 +0000741 bool validReg;
Adrian Prantl5883af32015-01-19 17:57:29 +0000742 if (Location.isReg())
Adrian Prantlab255fc2014-12-05 01:02:46 +0000743 validReg = addRegisterOpPiece(*Loc, Location.getReg());
744 else
745 validReg = addRegisterOffset(*Loc, Location.getReg(), Location.getOffset());
746
747 if (!validReg)
748 return;
749
David Blaikief7435ee2014-11-02 06:46:40 +0000750 // Now attach the location information to the DIE.
751 addBlock(Die, Attribute, Loc);
752}
David Blaikie77895fb2014-11-02 06:58:44 +0000753
754/// Start with the address based on the location provided, and generate the
755/// DWARF information necessary to find the actual variable given the extra
756/// address information encoded in the DbgVariable, starting from the starting
757/// location. Add the DWARF information to the die.
758void DwarfCompileUnit::addComplexAddress(const DbgVariable &DV, DIE &Die,
759 dwarf::Attribute Attribute,
760 const MachineLocation &Location) {
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000761 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
Adrian Prantl7813d9c2015-01-14 01:01:30 +0000762 DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
Adrian Prantlca7e4702015-02-10 23:18:28 +0000763 assert(DV.getExpression().size() == 1);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000764 const DIExpression *Expr = DV.getExpression().back();
Adrian Prantl40cb8192015-01-25 19:04:08 +0000765 bool ValidReg;
Adrian Prantl7813d9c2015-01-14 01:01:30 +0000766 if (Location.getOffset()) {
Adrian Prantl40cb8192015-01-25 19:04:08 +0000767 ValidReg = DwarfExpr.AddMachineRegIndirect(Location.getReg(),
768 Location.getOffset());
769 if (ValidReg)
Duncan P. N. Exon Smith76c91842015-04-07 03:45:57 +0000770 DwarfExpr.AddExpression(Expr->expr_op_begin(), Expr->expr_op_end());
Adrian Prantl5883af32015-01-19 17:57:29 +0000771 } else
Adrian Prantl40cb8192015-01-25 19:04:08 +0000772 ValidReg = DwarfExpr.AddMachineRegExpression(Expr, Location.getReg());
David Blaikie77895fb2014-11-02 06:58:44 +0000773
774 // Now attach the location information to the DIE.
Adrian Prantl40cb8192015-01-25 19:04:08 +0000775 if (ValidReg)
776 addBlock(Die, Attribute, Loc);
David Blaikie77895fb2014-11-02 06:58:44 +0000777}
David Blaikie4bc08812014-11-02 07:03:19 +0000778
779/// Add a Dwarf loclistptr attribute data and value.
780void DwarfCompileUnit::addLocationList(DIE &Die, dwarf::Attribute Attribute,
781 unsigned Index) {
David Blaikie4bc08812014-11-02 07:03:19 +0000782 dwarf::Form Form = DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
783 : dwarf::DW_FORM_data4;
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000784 Die.addValue(Attribute, Form, DIELocList(Index));
David Blaikie4bc08812014-11-02 07:03:19 +0000785}
David Blaikie02a63332014-11-02 07:06:51 +0000786
David Blaikie8c485b52014-11-02 07:08:12 +0000787void DwarfCompileUnit::applyVariableAttributes(const DbgVariable &Var,
788 DIE &VariableDie) {
David Blaikie02a63332014-11-02 07:06:51 +0000789 StringRef Name = Var.getName();
790 if (!Name.empty())
791 addString(VariableDie, dwarf::DW_AT_name, Name);
792 addSourceLine(VariableDie, Var.getVariable());
793 addType(VariableDie, Var.getType());
794 if (Var.isArtificial())
795 addFlag(VariableDie, dwarf::DW_AT_artificial);
796}
David Blaikie97802082014-11-02 07:11:55 +0000797
798/// Add a Dwarf expression attribute data and value.
799void DwarfCompileUnit::addExpr(DIELoc &Die, dwarf::Form Form,
800 const MCExpr *Expr) {
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000801 Die.addValue((dwarf::Attribute)0, Form, DIEExpr(Expr));
David Blaikie97802082014-11-02 07:11:55 +0000802}
David Blaikie3363a572014-11-02 08:09:09 +0000803
Duncan P. N. Exon Smith2fbe1352015-04-20 22:10:08 +0000804void DwarfCompileUnit::applySubprogramAttributesToDefinition(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000805 const DISubprogram *SP, DIE &SPDie) {
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000806 auto *SPDecl = SP->getDeclaration();
Duncan P. N. Exon Smithbe9e4fe2015-04-20 18:32:29 +0000807 auto *Context = resolve(SPDecl ? SPDecl->getScope() : SP->getScope());
David Blaikie3a443c22014-11-04 22:12:25 +0000808 applySubprogramAttributes(SP, SPDie, includeMinimalInlineScopes());
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000809 addGlobalName(SP->getName(), SPDie, Context);
David Blaikie3363a572014-11-02 08:09:09 +0000810}
David Blaikiecafd9622014-11-02 08:51:37 +0000811
812bool DwarfCompileUnit::isDwoUnit() const {
813 return DD->useSplitDwarf() && Skeleton;
814}
David Blaikie3a443c22014-11-04 22:12:25 +0000815
816bool DwarfCompileUnit::includeMinimalInlineScopes() const {
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +0000817 return getCUNode()->getEmissionKind() == DIBuilder::LineTablesOnly ||
David Blaikie3a443c22014-11-04 22:12:25 +0000818 (DD->useSplitDwarf() && !Skeleton);
819}
David Blaikie37c52312014-10-04 15:49:50 +0000820} // end llvm namespace