blob: f42a33eb774958c8e26877457f2670d653912729 [file] [log] [blame]
Devang Patel161b2f42011-04-12 23:21:44 +00001//===-- llvm/CodeGen/DwarfCompileUnit.cpp - Dwarf Compile Unit ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Eric Christopher443c9ed2012-08-14 05:13:29 +000010// This file contains support for constructing a dwarf compile unit.
Devang Patel161b2f42011-04-12 23:21:44 +000011//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "dwarfdebug"
15
16#include "DwarfCompileUnit.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "DwarfAccelTable.h"
Devang Patel161b2f42011-04-12 23:21:44 +000018#include "DwarfDebug.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/ADT/APFloat.h"
Bill Wendling16eeb6f2012-06-29 08:32:07 +000020#include "llvm/DIBuilder.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000021#include "llvm/IR/Constants.h"
22#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/GlobalVariable.h"
24#include "llvm/IR/Instructions.h"
Eric Christopherd61c34b2011-11-11 03:16:32 +000025#include "llvm/Support/Debug.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000026#include "llvm/Support/ErrorHandling.h"
Devang Patel6f9d8ff2011-08-15 17:57:41 +000027#include "llvm/Target/Mangler.h"
Devang Patel161b2f42011-04-12 23:21:44 +000028#include "llvm/Target/TargetFrameLowering.h"
29#include "llvm/Target/TargetMachine.h"
David Blaikie59eaa382013-06-28 20:05:11 +000030#include "llvm/Target/TargetLoweringObjectFile.h"
Devang Patel161b2f42011-04-12 23:21:44 +000031#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel161b2f42011-04-12 23:21:44 +000032
33using namespace llvm;
34
35/// CompileUnit - Compile unit constructor.
Eric Christopher9c57ad22013-05-08 00:58:51 +000036CompileUnit::CompileUnit(unsigned UID, unsigned L, DIE *D, const MDNode *N,
Eric Christopher5592ba42013-05-30 00:43:32 +000037 AsmPrinter *A, DwarfDebug *DW, DwarfUnits *DWU)
Eric Christopher2e5d8702012-12-20 21:58:36 +000038 : UniqueID(UID), Language(L), CUDie(D), Asm(A), DD(DW), DU(DWU),
Manman Renbc3e96f2013-03-12 18:27:15 +000039 IndexTyDie(0), DebugInfoOffset(0) {
Devang Patel161b2f42011-04-12 23:21:44 +000040 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
Eric Christopher9c57ad22013-05-08 00:58:51 +000041 insertDIE(N, D);
Devang Patel161b2f42011-04-12 23:21:44 +000042}
43
44/// ~CompileUnit - Destructor for compile unit.
45CompileUnit::~CompileUnit() {
46 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
47 DIEBlocks[j]->~DIEBlock();
48}
49
50/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
51/// information entry.
52DIEEntry *CompileUnit::createDIEEntry(DIE *Entry) {
53 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
54 return Value;
55}
56
Bill Wendling6afe4782012-12-06 07:55:19 +000057/// getDefaultLowerBound - Return the default lower bound for an array. If the
Bill Wendling222c2fd2012-12-06 07:38:10 +000058/// DWARF version doesn't handle the language, return -1.
Bill Wendling6afe4782012-12-06 07:55:19 +000059int64_t CompileUnit::getDefaultLowerBound() const {
Bill Wendling222c2fd2012-12-06 07:38:10 +000060 switch (Language) {
61 default:
62 break;
63
64 case dwarf::DW_LANG_C89:
65 case dwarf::DW_LANG_C99:
66 case dwarf::DW_LANG_C:
67 case dwarf::DW_LANG_C_plus_plus:
68 case dwarf::DW_LANG_ObjC:
69 case dwarf::DW_LANG_ObjC_plus_plus:
70 return 0;
71
72 case dwarf::DW_LANG_Fortran77:
73 case dwarf::DW_LANG_Fortran90:
74 case dwarf::DW_LANG_Fortran95:
75 return 1;
76
77 // The languages below have valid values only if the DWARF version >= 4.
78 case dwarf::DW_LANG_Java:
79 case dwarf::DW_LANG_Python:
80 case dwarf::DW_LANG_UPC:
81 case dwarf::DW_LANG_D:
82 if (dwarf::DWARF_VERSION >= 4)
83 return 0;
84 break;
85
86 case dwarf::DW_LANG_Ada83:
87 case dwarf::DW_LANG_Ada95:
88 case dwarf::DW_LANG_Cobol74:
89 case dwarf::DW_LANG_Cobol85:
90 case dwarf::DW_LANG_Modula2:
91 case dwarf::DW_LANG_Pascal83:
92 case dwarf::DW_LANG_PLI:
93 if (dwarf::DWARF_VERSION >= 4)
94 return 1;
95 break;
96 }
97
98 return -1;
99}
100
Eric Christopher873cf0a2012-08-24 01:14:27 +0000101/// addFlag - Add a flag that is true.
102void CompileUnit::addFlag(DIE *Die, unsigned Attribute) {
103 if (!DD->useDarwinGDBCompat())
104 Die->addValue(Attribute, dwarf::DW_FORM_flag_present,
105 DIEIntegerOne);
106 else
107 addUInt(Die, Attribute, dwarf::DW_FORM_flag, 1);
108}
109
Devang Patel161b2f42011-04-12 23:21:44 +0000110/// addUInt - Add an unsigned integer attribute data and value.
111///
112void CompileUnit::addUInt(DIE *Die, unsigned Attribute,
113 unsigned Form, uint64_t Integer) {
114 if (!Form) Form = DIEInteger::BestForm(false, Integer);
115 DIEValue *Value = Integer == 1 ?
116 DIEIntegerOne : new (DIEValueAllocator) DIEInteger(Integer);
117 Die->addValue(Attribute, Form, Value);
118}
119
120/// addSInt - Add an signed integer attribute data and value.
121///
122void CompileUnit::addSInt(DIE *Die, unsigned Attribute,
123 unsigned Form, int64_t Integer) {
124 if (!Form) Form = DIEInteger::BestForm(true, Integer);
125 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
126 Die->addValue(Attribute, Form, Value);
127}
128
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000129/// addString - Add a string attribute data and value. We always emit a
130/// reference to the string pool instead of immediate strings so that DIEs have
Eric Christopher3cc42202013-01-07 19:32:45 +0000131/// more predictable sizes. In the case of split dwarf we emit an index
132/// into another table which gets us the static offset into the string
133/// table.
Nick Lewycky390c40d2011-10-27 06:44:11 +0000134void CompileUnit::addString(DIE *Die, unsigned Attribute, StringRef String) {
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000135 if (!DD->useSplitDwarf()) {
136 MCSymbol *Symb = DU->getStringPoolEntry(String);
137 DIEValue *Value;
138 if (Asm->needsRelocationsForDwarfStringPool())
David Blaikie95e72c92013-06-28 20:05:04 +0000139 Value = new (DIEValueAllocator) DIELabel(Symb, Asm->OutContext);
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000140 else {
141 MCSymbol *StringPool = DU->getStringPoolSym();
142 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
143 }
144 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value);
145 } else {
146 unsigned idx = DU->getStringPoolIndex(String);
147 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
148 Die->addValue(Attribute, dwarf::DW_FORM_GNU_str_index, Value);
149 }
150}
151
152/// addLocalString - Add a string attribute data and value. This is guaranteed
153/// to be in the local string pool instead of indirected.
154void CompileUnit::addLocalString(DIE *Die, unsigned Attribute,
155 StringRef String) {
Eric Christopher2e5d8702012-12-20 21:58:36 +0000156 MCSymbol *Symb = DU->getStringPoolEntry(String);
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000157 DIEValue *Value;
158 if (Asm->needsRelocationsForDwarfStringPool())
David Blaikie95e72c92013-06-28 20:05:04 +0000159 Value = new (DIEValueAllocator) DIELabel(Symb, Asm->OutContext);
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000160 else {
Eric Christopher2e5d8702012-12-20 21:58:36 +0000161 MCSymbol *StringPool = DU->getStringPoolSym();
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000162 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
Nick Lewycky390c40d2011-10-27 06:44:11 +0000163 }
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000164 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value);
Devang Patel161b2f42011-04-12 23:21:44 +0000165}
166
167/// addLabel - Add a Dwarf label attribute data and value.
168///
169void CompileUnit::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
David Blaikie95e72c92013-06-28 20:05:04 +0000170 const MCSymbolRefExpr *Label) {
Devang Patel161b2f42011-04-12 23:21:44 +0000171 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
172 Die->addValue(Attribute, Form, Value);
173}
174
David Blaikie95e72c92013-06-28 20:05:04 +0000175void CompileUnit::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
176 const MCSymbol *Label) {
177 addLabel(Die, Attribute, Form, MCSymbolRefExpr::Create(Label, Asm->OutContext));
178}
179
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000180/// addLabelAddress - Add a dwarf label attribute data and value using
181/// DW_FORM_addr or DW_FORM_GNU_addr_index.
182///
183void CompileUnit::addLabelAddress(DIE *Die, unsigned Attribute,
184 MCSymbol *Label) {
185 if (!DD->useSplitDwarf()) {
186 if (Label != NULL) {
David Blaikie95e72c92013-06-28 20:05:04 +0000187 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label, Asm->OutContext);
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000188 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
189 } else {
190 DIEValue *Value = new (DIEValueAllocator) DIEInteger(0);
191 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
192 }
193 } else {
194 unsigned idx = DU->getAddrPoolIndex(Label);
195 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
196 Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
197 }
198}
199
Eric Christopher0969ddf2013-01-18 22:11:33 +0000200/// addOpAddress - Add a dwarf op address data and value using the
201/// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
202///
David Blaikie95e72c92013-06-28 20:05:04 +0000203void CompileUnit::addOpAddress(DIE *Die, const MCSymbol *Sym) {
Eric Christopher0969ddf2013-01-18 22:11:33 +0000204 if (!DD->useSplitDwarf()) {
205 addUInt(Die, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
206 addLabel(Die, 0, dwarf::DW_FORM_udata, Sym);
207 } else {
Eric Christopher0969ddf2013-01-18 22:11:33 +0000208 addUInt(Die, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
David Blaikie8fed05e2013-07-01 23:55:52 +0000209 addUInt(Die, 0, dwarf::DW_FORM_GNU_addr_index, DU->getAddrPoolIndex(Sym));
Eric Christopher0969ddf2013-01-18 22:11:33 +0000210 }
211}
212
Devang Patel161b2f42011-04-12 23:21:44 +0000213/// addDelta - Add a label delta attribute data and value.
214///
215void CompileUnit::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
216 const MCSymbol *Hi, const MCSymbol *Lo) {
217 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
218 Die->addValue(Attribute, Form, Value);
219}
220
221/// addDIEEntry - Add a DIE attribute data and value.
222///
223void CompileUnit::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
224 DIE *Entry) {
225 Die->addValue(Attribute, Form, createDIEEntry(Entry));
226}
227
Devang Patel161b2f42011-04-12 23:21:44 +0000228/// addBlock - Add block data.
229///
230void CompileUnit::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
231 DIEBlock *Block) {
232 Block->ComputeSize(Asm);
233 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
234 Die->addValue(Attribute, Block->BestForm(), Block);
235}
236
237/// addSourceLine - Add location information to specified debug information
238/// entry.
239void CompileUnit::addSourceLine(DIE *Die, DIVariable V) {
240 // Verify variable.
241 if (!V.Verify())
242 return;
Eric Christopher8b4310b2012-11-21 00:34:38 +0000243
Devang Patel161b2f42011-04-12 23:21:44 +0000244 unsigned Line = V.getLineNumber();
245 if (Line == 0)
246 return;
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000247 unsigned FileID = DD->getOrCreateSourceID(V.getContext().getFilename(),
Manman Ren3de61b42013-03-07 01:42:00 +0000248 V.getContext().getDirectory(),
249 getUniqueID());
Devang Patel161b2f42011-04-12 23:21:44 +0000250 assert(FileID && "Invalid file id");
251 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
252 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
253}
254
255/// addSourceLine - Add location information to specified debug information
256/// entry.
257void CompileUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
258 // Verify global variable.
259 if (!G.Verify())
260 return;
261
262 unsigned Line = G.getLineNumber();
263 if (Line == 0)
264 return;
Manman Ren3de61b42013-03-07 01:42:00 +0000265 unsigned FileID = DD->getOrCreateSourceID(G.getFilename(), G.getDirectory(),
266 getUniqueID());
Devang Patel161b2f42011-04-12 23:21:44 +0000267 assert(FileID && "Invalid file id");
268 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
269 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
270}
271
272/// addSourceLine - Add location information to specified debug information
273/// entry.
274void CompileUnit::addSourceLine(DIE *Die, DISubprogram SP) {
275 // Verify subprogram.
276 if (!SP.Verify())
277 return;
Eric Christopher2125d5a2012-03-15 23:55:40 +0000278
Devang Patel161b2f42011-04-12 23:21:44 +0000279 // If the line number is 0, don't add it.
Eric Christopher2125d5a2012-03-15 23:55:40 +0000280 unsigned Line = SP.getLineNumber();
281 if (Line == 0)
Devang Patel161b2f42011-04-12 23:21:44 +0000282 return;
283
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000284 unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(),
Manman Ren3de61b42013-03-07 01:42:00 +0000285 SP.getDirectory(), getUniqueID());
Devang Patel161b2f42011-04-12 23:21:44 +0000286 assert(FileID && "Invalid file id");
287 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
288 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
289}
290
291/// addSourceLine - Add location information to specified debug information
292/// entry.
293void CompileUnit::addSourceLine(DIE *Die, DIType Ty) {
294 // Verify type.
295 if (!Ty.Verify())
296 return;
297
298 unsigned Line = Ty.getLineNumber();
Eric Christopher2125d5a2012-03-15 23:55:40 +0000299 if (Line == 0)
Devang Patel161b2f42011-04-12 23:21:44 +0000300 return;
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000301 unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(),
Manman Ren3de61b42013-03-07 01:42:00 +0000302 Ty.getDirectory(), getUniqueID());
Devang Patel161b2f42011-04-12 23:21:44 +0000303 assert(FileID && "Invalid file id");
304 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
305 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
306}
307
308/// addSourceLine - Add location information to specified debug information
309/// entry.
Eric Christopherb8ca9882012-03-29 08:42:56 +0000310void CompileUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
311 // Verify type.
312 if (!Ty.Verify())
313 return;
314
315 unsigned Line = Ty.getLineNumber();
316 if (Line == 0)
317 return;
318 DIFile File = Ty.getFile();
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000319 unsigned FileID = DD->getOrCreateSourceID(File.getFilename(),
Manman Ren3de61b42013-03-07 01:42:00 +0000320 File.getDirectory(), getUniqueID());
Eric Christopherb8ca9882012-03-29 08:42:56 +0000321 assert(FileID && "Invalid file id");
322 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
323 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
324}
325
326/// addSourceLine - Add location information to specified debug information
327/// entry.
Devang Patel161b2f42011-04-12 23:21:44 +0000328void CompileUnit::addSourceLine(DIE *Die, DINameSpace NS) {
329 // Verify namespace.
330 if (!NS.Verify())
331 return;
332
333 unsigned Line = NS.getLineNumber();
334 if (Line == 0)
335 return;
336 StringRef FN = NS.getFilename();
337
Manman Ren3de61b42013-03-07 01:42:00 +0000338 unsigned FileID = DD->getOrCreateSourceID(FN, NS.getDirectory(),
339 getUniqueID());
Devang Patel161b2f42011-04-12 23:21:44 +0000340 assert(FileID && "Invalid file id");
341 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
342 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
343}
344
Eric Christopher8b4310b2012-11-21 00:34:38 +0000345/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patele1cdf842011-04-27 22:45:24 +0000346/// DbgVariable based on provided MachineLocation.
Eric Christopherf61dbc12013-06-24 21:07:27 +0000347void CompileUnit::addVariableAddress(const DbgVariable &DV, DIE *Die,
Devang Patele1cdf842011-04-27 22:45:24 +0000348 MachineLocation Location) {
Eric Christopherf61dbc12013-06-24 21:07:27 +0000349 if (DV.variableHasComplexAddress())
Devang Patel161b2f42011-04-12 23:21:44 +0000350 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
Eric Christopherf61dbc12013-06-24 21:07:27 +0000351 else if (DV.isBlockByrefVariable())
Devang Patel161b2f42011-04-12 23:21:44 +0000352 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
353 else
David Blaikie4532c282013-06-20 00:25:24 +0000354 addAddress(Die, dwarf::DW_AT_location, Location,
Eric Christopherf61dbc12013-06-24 21:07:27 +0000355 DV.getVariable().isIndirect());
Devang Patel161b2f42011-04-12 23:21:44 +0000356}
357
Devang Patel116da2f2011-04-26 19:06:18 +0000358/// addRegisterOp - Add register operand.
359void CompileUnit::addRegisterOp(DIE *TheDie, unsigned Reg) {
360 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
361 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
362 if (DWReg < 32)
363 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
364 else {
365 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
366 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
367 }
368}
369
370/// addRegisterOffset - Add register offset.
371void CompileUnit::addRegisterOffset(DIE *TheDie, unsigned Reg,
372 int64_t Offset) {
373 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
374 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
375 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
376 if (Reg == TRI->getFrameRegister(*Asm->MF))
377 // If variable offset is based in frame register then use fbreg.
378 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
379 else if (DWReg < 32)
380 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
381 else {
382 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
383 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
384 }
385 addSInt(TheDie, 0, dwarf::DW_FORM_sdata, Offset);
386}
387
388/// addAddress - Add an address attribute to a die based on the location
389/// provided.
390void CompileUnit::addAddress(DIE *Die, unsigned Attribute,
David Blaikie4532c282013-06-20 00:25:24 +0000391 const MachineLocation &Location, bool Indirect) {
Devang Patel116da2f2011-04-26 19:06:18 +0000392 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
393
David Blaikie4532c282013-06-20 00:25:24 +0000394 if (Location.isReg() && !Indirect)
Devang Patel116da2f2011-04-26 19:06:18 +0000395 addRegisterOp(Block, Location.getReg());
David Blaikie4532c282013-06-20 00:25:24 +0000396 else {
Devang Patel116da2f2011-04-26 19:06:18 +0000397 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
David Blaikie4532c282013-06-20 00:25:24 +0000398 if (Indirect && !Location.isReg()) {
399 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
400 }
401 }
Devang Patel116da2f2011-04-26 19:06:18 +0000402
403 // Now attach the location information to the DIE.
404 addBlock(Die, Attribute, 0, Block);
405}
406
Devang Patel161b2f42011-04-12 23:21:44 +0000407/// addComplexAddress - Start with the address based on the location provided,
408/// and generate the DWARF information necessary to find the actual variable
409/// given the extra address information encoded in the DIVariable, starting from
410/// the starting location. Add the DWARF information to the die.
411///
Eric Christopherf61dbc12013-06-24 21:07:27 +0000412void CompileUnit::addComplexAddress(const DbgVariable &DV, DIE *Die,
Devang Patel161b2f42011-04-12 23:21:44 +0000413 unsigned Attribute,
414 const MachineLocation &Location) {
Devang Patel161b2f42011-04-12 23:21:44 +0000415 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Eric Christopherf61dbc12013-06-24 21:07:27 +0000416 unsigned N = DV.getNumAddrElements();
Devang Patelc26f5442011-04-28 02:22:40 +0000417 unsigned i = 0;
418 if (Location.isReg()) {
Eric Christopherf61dbc12013-06-24 21:07:27 +0000419 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Devang Patelc26f5442011-04-28 02:22:40 +0000420 // If first address element is OpPlus then emit
421 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Eric Christopherf61dbc12013-06-24 21:07:27 +0000422 addRegisterOffset(Block, Location.getReg(), DV.getAddrElement(1));
Devang Patelc26f5442011-04-28 02:22:40 +0000423 i = 2;
424 } else
425 addRegisterOp(Block, Location.getReg());
426 }
Devang Patel116da2f2011-04-26 19:06:18 +0000427 else
428 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel161b2f42011-04-12 23:21:44 +0000429
Devang Patelc26f5442011-04-28 02:22:40 +0000430 for (;i < N; ++i) {
Eric Christopherf61dbc12013-06-24 21:07:27 +0000431 uint64_t Element = DV.getAddrElement(i);
Devang Patel161b2f42011-04-12 23:21:44 +0000432 if (Element == DIBuilder::OpPlus) {
433 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Eric Christopherf61dbc12013-06-24 21:07:27 +0000434 addUInt(Block, 0, dwarf::DW_FORM_udata, DV.getAddrElement(++i));
Devang Patel161b2f42011-04-12 23:21:44 +0000435 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher50120762012-05-08 18:56:00 +0000436 if (!Location.isReg())
437 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel161b2f42011-04-12 23:21:44 +0000438 } else llvm_unreachable("unknown DIBuilder Opcode");
439 }
440
441 // Now attach the location information to the DIE.
442 addBlock(Die, Attribute, 0, Block);
443}
444
445/* Byref variables, in Blocks, are declared by the programmer as "SomeType
446 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
447 gives the variable VarName either the struct, or a pointer to the struct, as
448 its type. This is necessary for various behind-the-scenes things the
449 compiler needs to do with by-reference variables in Blocks.
450
451 However, as far as the original *programmer* is concerned, the variable
452 should still have type 'SomeType', as originally declared.
453
454 The function getBlockByrefType dives into the __Block_byref_x_VarName
455 struct to find the original type of the variable, which is then assigned to
456 the variable's Debug Information Entry as its real type. So far, so good.
457 However now the debugger will expect the variable VarName to have the type
458 SomeType. So we need the location attribute for the variable to be an
459 expression that explains to the debugger how to navigate through the
460 pointers and struct to find the actual variable of type SomeType.
461
462 The following function does just that. We start by getting
463 the "normal" location for the variable. This will be the location
464 of either the struct __Block_byref_x_VarName or the pointer to the
465 struct __Block_byref_x_VarName.
466
467 The struct will look something like:
468
469 struct __Block_byref_x_VarName {
470 ... <various fields>
471 struct __Block_byref_x_VarName *forwarding;
472 ... <various other fields>
473 SomeType VarName;
474 ... <maybe more fields>
475 };
476
477 If we are given the struct directly (as our starting point) we
478 need to tell the debugger to:
479
480 1). Add the offset of the forwarding field.
481
482 2). Follow that pointer to get the real __Block_byref_x_VarName
483 struct to use (the real one may have been copied onto the heap).
484
485 3). Add the offset for the field VarName, to find the actual variable.
486
487 If we started with a pointer to the struct, then we need to
488 dereference that pointer first, before the other steps.
489 Translating this into DWARF ops, we will need to append the following
490 to the current location description for the variable:
491
492 DW_OP_deref -- optional, if we start with a pointer
493 DW_OP_plus_uconst <forward_fld_offset>
494 DW_OP_deref
495 DW_OP_plus_uconst <varName_fld_offset>
496
497 That is what this function does. */
498
499/// addBlockByrefAddress - Start with the address based on the location
500/// provided, and generate the DWARF information necessary to find the
501/// actual Block variable (navigating the Block struct) based on the
502/// starting location. Add the DWARF information to the die. For
503/// more information, read large comment just above here.
504///
Eric Christopherf61dbc12013-06-24 21:07:27 +0000505void CompileUnit::addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
Devang Patel161b2f42011-04-12 23:21:44 +0000506 unsigned Attribute,
507 const MachineLocation &Location) {
Eric Christopherf61dbc12013-06-24 21:07:27 +0000508 DIType Ty = DV.getType();
Devang Patel161b2f42011-04-12 23:21:44 +0000509 DIType TmpTy = Ty;
510 unsigned Tag = Ty.getTag();
511 bool isPointer = false;
512
Eric Christopherf61dbc12013-06-24 21:07:27 +0000513 StringRef varName = DV.getName();
Devang Patel161b2f42011-04-12 23:21:44 +0000514
515 if (Tag == dwarf::DW_TAG_pointer_type) {
516 DIDerivedType DTy = DIDerivedType(Ty);
517 TmpTy = DTy.getTypeDerivedFrom();
518 isPointer = true;
519 }
520
521 DICompositeType blockStruct = DICompositeType(TmpTy);
522
523 // Find the __forwarding field and the variable field in the __Block_byref
524 // struct.
525 DIArray Fields = blockStruct.getTypeArray();
526 DIDescriptor varField = DIDescriptor();
527 DIDescriptor forwardingField = DIDescriptor();
528
529 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
530 DIDescriptor Element = Fields.getElement(i);
531 DIDerivedType DT = DIDerivedType(Element);
532 StringRef fieldName = DT.getName();
533 if (fieldName == "__forwarding")
534 forwardingField = Element;
535 else if (fieldName == varName)
536 varField = Element;
537 }
538
539 // Get the offsets for the forwarding field and the variable field.
540 unsigned forwardingFieldOffset =
541 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
542 unsigned varFieldOffset =
543 DIDerivedType(varField).getOffsetInBits() >> 3;
544
545 // Decode the original location, and use that as the start of the byref
546 // variable's location.
Devang Patel161b2f42011-04-12 23:21:44 +0000547 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
548
Eric Christophercaba2632012-07-04 02:02:18 +0000549 if (Location.isReg())
550 addRegisterOp(Block, Location.getReg());
551 else
552 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel161b2f42011-04-12 23:21:44 +0000553
554 // If we started with a pointer to the __Block_byref... struct, then
555 // the first thing we need to do is dereference the pointer (DW_OP_deref).
556 if (isPointer)
557 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
558
559 // Next add the offset for the '__forwarding' field:
560 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
561 // adding the offset if it's 0.
562 if (forwardingFieldOffset > 0) {
563 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
564 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
565 }
566
567 // Now dereference the __forwarding field to get to the real __Block_byref
568 // struct: DW_OP_deref.
569 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
570
571 // Now that we've got the real __Block_byref... struct, add the offset
572 // for the variable's field to get to the location of the actual variable:
573 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
574 if (varFieldOffset > 0) {
575 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
576 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
577 }
578
579 // Now attach the location information to the DIE.
580 addBlock(Die, Attribute, 0, Block);
581}
582
Devang Patel4ec14b02011-07-20 21:57:04 +0000583/// isTypeSigned - Return true if the type is signed.
584static bool isTypeSigned(DIType Ty, int *SizeInBits) {
585 if (Ty.isDerivedType())
586 return isTypeSigned(DIDerivedType(Ty).getTypeDerivedFrom(), SizeInBits);
587 if (Ty.isBasicType())
588 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed
589 || DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
590 *SizeInBits = Ty.getSizeInBits();
591 return true;
592 }
593 return false;
594}
595
Devang Patel161b2f42011-04-12 23:21:44 +0000596/// addConstantValue - Add constant value entry in variable DIE.
Devang Patelb58128e2011-05-27 16:45:18 +0000597bool CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
598 DIType Ty) {
David Blaikie4de9d722013-05-10 21:52:07 +0000599 // FIXME: This is a bit conservative/simple - it emits negative values at
600 // their maximum bit width which is a bit unfortunate (& doesn't prefer
601 // udata/sdata over dataN as suggested by the DWARF spec)
Nick Lewycky746cb672011-10-26 22:55:33 +0000602 assert(MO.isImm() && "Invalid machine operand!");
Devang Patel161b2f42011-04-12 23:21:44 +0000603 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel4ec14b02011-07-20 21:57:04 +0000604 int SizeInBits = -1;
605 bool SignedConstant = isTypeSigned(Ty, &SizeInBits);
606 unsigned Form = SignedConstant ? dwarf::DW_FORM_sdata : dwarf::DW_FORM_udata;
607 switch (SizeInBits) {
608 case 8: Form = dwarf::DW_FORM_data1; break;
609 case 16: Form = dwarf::DW_FORM_data2; break;
610 case 32: Form = dwarf::DW_FORM_data4; break;
611 case 64: Form = dwarf::DW_FORM_data8; break;
Devang Patel045c1d42011-05-27 19:13:26 +0000612 default: break;
613 }
Eric Christopher8b4310b2012-11-21 00:34:38 +0000614 SignedConstant ? addSInt(Block, 0, Form, MO.getImm())
Devang Patel4ec14b02011-07-20 21:57:04 +0000615 : addUInt(Block, 0, Form, MO.getImm());
Devang Patel72f0d9c2011-05-27 18:15:52 +0000616
Devang Patel161b2f42011-04-12 23:21:44 +0000617 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
618 return true;
619}
620
621/// addConstantFPValue - Add constant value entry in variable DIE.
622bool CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Nick Lewycky390c40d2011-10-27 06:44:11 +0000623 assert (MO.isFPImm() && "Invalid machine operand!");
Devang Patel161b2f42011-04-12 23:21:44 +0000624 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
625 APFloat FPImm = MO.getFPImm()->getValueAPF();
626
627 // Get the raw data form of the floating point.
628 const APInt FltVal = FPImm.bitcastToAPInt();
629 const char *FltPtr = (const char*)FltVal.getRawData();
630
631 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmow3574eca2012-10-08 16:38:25 +0000632 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel161b2f42011-04-12 23:21:44 +0000633 int Incr = (LittleEndian ? 1 : -1);
634 int Start = (LittleEndian ? 0 : NumBytes - 1);
635 int Stop = (LittleEndian ? NumBytes : -1);
636
637 // Output the constant to DWARF one byte at a time.
638 for (; Start != Stop; Start += Incr)
639 addUInt(Block, 0, dwarf::DW_FORM_data1,
640 (unsigned char)0xFF & FltPtr[Start]);
641
642 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
643 return true;
644}
645
David Blaikie14268412013-01-20 01:18:01 +0000646/// addConstantFPValue - Add constant value entry in variable DIE.
647bool CompileUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) {
648 return addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), false);
649}
650
Devang Patel161b2f42011-04-12 23:21:44 +0000651/// addConstantValue - Add constant value entry in variable DIE.
Devang Patel8594d422011-06-24 20:46:11 +0000652bool CompileUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
Devang Patel161b2f42011-04-12 23:21:44 +0000653 bool Unsigned) {
David Blaikie14268412013-01-20 01:18:01 +0000654 return addConstantValue(Die, CI->getValue(), Unsigned);
655}
656
657// addConstantValue - Add constant value entry in variable DIE.
658bool CompileUnit::addConstantValue(DIE *Die, const APInt &Val,
659 bool Unsigned) {
660 unsigned CIBitWidth = Val.getBitWidth();
Devang Pateld6a81362011-05-28 00:39:18 +0000661 if (CIBitWidth <= 64) {
662 unsigned form = 0;
663 switch (CIBitWidth) {
664 case 8: form = dwarf::DW_FORM_data1; break;
665 case 16: form = dwarf::DW_FORM_data2; break;
666 case 32: form = dwarf::DW_FORM_data4; break;
667 case 64: form = dwarf::DW_FORM_data8; break;
Eric Christopher8b4310b2012-11-21 00:34:38 +0000668 default:
Devang Pateld6a81362011-05-28 00:39:18 +0000669 form = Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata;
670 }
Devang Patel161b2f42011-04-12 23:21:44 +0000671 if (Unsigned)
David Blaikie14268412013-01-20 01:18:01 +0000672 addUInt(Die, dwarf::DW_AT_const_value, form, Val.getZExtValue());
Devang Patel161b2f42011-04-12 23:21:44 +0000673 else
David Blaikie14268412013-01-20 01:18:01 +0000674 addSInt(Die, dwarf::DW_AT_const_value, form, Val.getSExtValue());
Devang Patel161b2f42011-04-12 23:21:44 +0000675 return true;
676 }
677
678 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
679
680 // Get the raw data form of the large APInt.
NAKAMURA Takumic3e48c32011-10-28 14:12:22 +0000681 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel161b2f42011-04-12 23:21:44 +0000682
683 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmow3574eca2012-10-08 16:38:25 +0000684 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel161b2f42011-04-12 23:21:44 +0000685
686 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumic3e48c32011-10-28 14:12:22 +0000687 for (int i = 0; i < NumBytes; i++) {
688 uint8_t c;
689 if (LittleEndian)
690 c = Ptr64[i / 8] >> (8 * (i & 7));
691 else
692 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
693 addUInt(Block, 0, dwarf::DW_FORM_data1, c);
694 }
Devang Patel161b2f42011-04-12 23:21:44 +0000695
696 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
697 return true;
698}
699
Eric Christopher6c3bb942013-04-22 07:47:40 +0000700/// addTemplateParams - Add template parameters into buffer.
Devang Patel161b2f42011-04-12 23:21:44 +0000701void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
702 // Add template parameters.
703 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
704 DIDescriptor Element = TParams.getElement(i);
705 if (Element.isTemplateTypeParameter())
706 Buffer.addChild(getOrCreateTemplateTypeParameterDIE(
707 DITemplateTypeParameter(Element)));
708 else if (Element.isTemplateValueParameter())
709 Buffer.addChild(getOrCreateTemplateValueParameterDIE(
710 DITemplateValueParameter(Element)));
711 }
Devang Patel161b2f42011-04-12 23:21:44 +0000712}
Nick Lewycky746cb672011-10-26 22:55:33 +0000713
Eric Christopher6b6061f2013-01-16 01:22:23 +0000714/// getOrCreateContextDIE - Get context owner's DIE.
715DIE *CompileUnit::getOrCreateContextDIE(DIDescriptor Context) {
716 if (Context.isType())
717 return getOrCreateTypeDIE(DIType(Context));
718 else if (Context.isNameSpace())
719 return getOrCreateNameSpace(DINameSpace(Context));
720 else if (Context.isSubprogram())
721 return getOrCreateSubprogramDIE(DISubprogram(Context));
Eric Christopher6c3bb942013-04-22 07:47:40 +0000722 else
Eric Christopher6b6061f2013-01-16 01:22:23 +0000723 return getDIE(Context);
724}
725
Devang Patel161b2f42011-04-12 23:21:44 +0000726/// addToContextOwner - Add Die into the list of its context owner's children.
727void CompileUnit::addToContextOwner(DIE *Die, DIDescriptor Context) {
Eric Christopher6b6061f2013-01-16 01:22:23 +0000728 if (DIE *ContextDIE = getOrCreateContextDIE(Context))
Devang Patel161b2f42011-04-12 23:21:44 +0000729 ContextDIE->addChild(Die);
730 else
731 addDie(Die);
732}
733
734/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
735/// given DIType.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000736DIE *CompileUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
737 DIType Ty(TyNode);
738 if (!Ty.Verify())
739 return NULL;
Devang Patel161b2f42011-04-12 23:21:44 +0000740 DIE *TyDIE = getDIE(Ty);
741 if (TyDIE)
742 return TyDIE;
743
744 // Create new type.
745 TyDIE = new DIE(dwarf::DW_TAG_base_type);
746 insertDIE(Ty, TyDIE);
747 if (Ty.isBasicType())
748 constructTypeDIE(*TyDIE, DIBasicType(Ty));
749 else if (Ty.isCompositeType())
750 constructTypeDIE(*TyDIE, DICompositeType(Ty));
751 else {
752 assert(Ty.isDerivedType() && "Unknown kind of DIType");
753 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
754 }
Eric Christopher1b3f9192011-11-10 19:52:58 +0000755 // If this is a named finished type then include it in the list of types
756 // for the accelerator tables.
Eric Christopherc36145f2012-01-06 04:35:23 +0000757 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
758 bool IsImplementation = 0;
759 if (Ty.isCompositeType()) {
760 DICompositeType CT(Ty);
Eric Christophere0167892012-01-06 23:03:37 +0000761 // A runtime language of 0 actually means C/C++ and that any
762 // non-negative value is some version of Objective-C/C++.
Eric Christopherc36145f2012-01-06 04:35:23 +0000763 IsImplementation = (CT.getRunTimeLang() == 0) ||
Eric Christophere2dc9332012-02-22 08:46:02 +0000764 CT.isObjcClassComplete();
Eric Christopherc36145f2012-01-06 04:35:23 +0000765 }
Eric Christophere0167892012-01-06 23:03:37 +0000766 unsigned Flags = IsImplementation ?
767 DwarfAccelTable::eTypeFlagClassIsImplementation : 0;
768 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
Eric Christopherc36145f2012-01-06 04:35:23 +0000769 }
Eric Christopher8b4310b2012-11-21 00:34:38 +0000770
Devang Patel161b2f42011-04-12 23:21:44 +0000771 addToContextOwner(TyDIE, Ty.getContext());
772 return TyDIE;
773}
774
775/// addType - Add a new type attribute to the specified entity.
Eric Christopher4d069bf2012-05-22 18:45:24 +0000776void CompileUnit::addType(DIE *Entity, DIType Ty, unsigned Attribute) {
Devang Patel161b2f42011-04-12 23:21:44 +0000777 if (!Ty.Verify())
778 return;
779
780 // Check for pre-existence.
781 DIEEntry *Entry = getDIEEntry(Ty);
782 // If it exists then use the existing value.
783 if (Entry) {
Eric Christopher663e0cf2012-03-28 07:34:31 +0000784 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
Devang Patel161b2f42011-04-12 23:21:44 +0000785 return;
786 }
787
788 // Construct type.
789 DIE *Buffer = getOrCreateTypeDIE(Ty);
790
791 // Set up proxy.
792 Entry = createDIEEntry(Buffer);
793 insertDIEEntry(Ty, Entry);
Eric Christopher663e0cf2012-03-28 07:34:31 +0000794 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
Devang Patele9ae06c2011-05-31 22:56:51 +0000795
796 // If this is a complete composite type then include it in the
797 // list of global types.
Devang Patelc20bdf12011-06-01 00:23:24 +0000798 addGlobalType(Ty);
Devang Patel66658e42011-05-31 23:30:30 +0000799}
800
801/// addGlobalType - Add a new global type to the compile unit.
802///
Devang Patelc20bdf12011-06-01 00:23:24 +0000803void CompileUnit::addGlobalType(DIType Ty) {
Devang Patele9ae06c2011-05-31 22:56:51 +0000804 DIDescriptor Context = Ty.getContext();
Eric Christopher8b4310b2012-11-21 00:34:38 +0000805 if (Ty.isCompositeType() && !Ty.getName().empty() && !Ty.isForwardDecl()
806 && (!Context || Context.isCompileUnit() || Context.isFile()
Devang Patel94c7ddb2011-08-16 22:09:43 +0000807 || Context.isNameSpace()))
Devang Patelc20bdf12011-06-01 00:23:24 +0000808 if (DIEEntry *Entry = getDIEEntry(Ty))
809 GlobalTypes[Ty.getName()] = Entry->getEntry();
Devang Patel161b2f42011-04-12 23:21:44 +0000810}
811
Devang Patel31c5d052011-05-06 16:57:54 +0000812/// addPubTypes - Add type for pubtypes section.
813void CompileUnit::addPubTypes(DISubprogram SP) {
814 DICompositeType SPTy = SP.getType();
815 unsigned SPTag = SPTy.getTag();
816 if (SPTag != dwarf::DW_TAG_subroutine_type)
817 return;
818
819 DIArray Args = SPTy.getTypeArray();
820 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
821 DIType ATy(Args.getElement(i));
822 if (!ATy.Verify())
823 continue;
Devang Patelc20bdf12011-06-01 00:23:24 +0000824 addGlobalType(ATy);
Devang Patel31c5d052011-05-06 16:57:54 +0000825 }
826}
827
Devang Patel161b2f42011-04-12 23:21:44 +0000828/// constructTypeDIE - Construct basic type die from DIBasicType.
829void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
830 // Get core information.
831 StringRef Name = BTy.getName();
Devang Patel161b2f42011-04-12 23:21:44 +0000832 // Add name if not anonymous or intermediate type.
833 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000834 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel734a67c2011-09-14 23:13:28 +0000835
836 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type) {
837 Buffer.setTag(dwarf::DW_TAG_unspecified_type);
838 // Unspecified types has only name, nothing else.
839 return;
840 }
841
842 Buffer.setTag(dwarf::DW_TAG_base_type);
Nick Lewycky746cb672011-10-26 22:55:33 +0000843 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel30d409c2012-02-07 23:33:58 +0000844 BTy.getEncoding());
Devang Patel734a67c2011-09-14 23:13:28 +0000845
Devang Patel161b2f42011-04-12 23:21:44 +0000846 uint64_t Size = BTy.getSizeInBits() >> 3;
847 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
848}
849
850/// constructTypeDIE - Construct derived type die from DIDerivedType.
851void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
852 // Get core information.
853 StringRef Name = DTy.getName();
854 uint64_t Size = DTy.getSizeInBits() >> 3;
855 unsigned Tag = DTy.getTag();
856
857 // FIXME - Workaround for templates.
858 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
859
860 Buffer.setTag(Tag);
861
862 // Map to main type, void will not have a type.
863 DIType FromTy = DTy.getTypeDerivedFrom();
864 addType(&Buffer, FromTy);
865
866 // Add name if not anonymous or intermediate type.
867 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000868 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +0000869
870 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher35f225a2012-02-21 22:25:53 +0000871 if (Size && Tag != dwarf::DW_TAG_pointer_type)
Devang Patel161b2f42011-04-12 23:21:44 +0000872 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
873
David Blaikie62fdfb52013-01-07 05:51:15 +0000874 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
875 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
876 getOrCreateTypeDIE(DTy.getClassType()));
Devang Patel161b2f42011-04-12 23:21:44 +0000877 // Add source line info if available and TyDesc is not a forward declaration.
878 if (!DTy.isForwardDecl())
879 addSourceLine(&Buffer, DTy);
880}
881
882/// constructTypeDIE - Construct type DIE from DICompositeType.
883void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
884 // Get core information.
885 StringRef Name = CTy.getName();
886
887 uint64_t Size = CTy.getSizeInBits() >> 3;
888 unsigned Tag = CTy.getTag();
889 Buffer.setTag(Tag);
890
891 switch (Tag) {
Devang Patel161b2f42011-04-12 23:21:44 +0000892 case dwarf::DW_TAG_array_type:
893 constructArrayTypeDIE(Buffer, &CTy);
894 break;
895 case dwarf::DW_TAG_enumeration_type: {
896 DIArray Elements = CTy.getTypeArray();
897
898 // Add enumerators to enumeration type.
899 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
900 DIE *ElemDie = NULL;
901 DIDescriptor Enum(Elements.getElement(i));
902 if (Enum.isEnumerator()) {
903 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
904 Buffer.addChild(ElemDie);
905 }
906 }
Eric Christopherbb0f6ea2012-05-23 00:09:20 +0000907 DIType DTy = CTy.getTypeDerivedFrom();
908 if (DTy.Verify()) {
909 addType(&Buffer, DTy);
910 addUInt(&Buffer, dwarf::DW_AT_enum_class, dwarf::DW_FORM_flag, 1);
911 }
Devang Patel161b2f42011-04-12 23:21:44 +0000912 }
913 break;
914 case dwarf::DW_TAG_subroutine_type: {
915 // Add return type.
916 DIArray Elements = CTy.getTypeArray();
917 DIDescriptor RTy = Elements.getElement(0);
918 addType(&Buffer, DIType(RTy));
919
920 bool isPrototyped = true;
921 // Add arguments.
922 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
923 DIDescriptor Ty = Elements.getElement(i);
924 if (Ty.isUnspecifiedParameter()) {
925 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters);
926 Buffer.addChild(Arg);
927 isPrototyped = false;
928 } else {
929 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
930 addType(Arg, DIType(Ty));
David Blaikieaaf2e632013-01-29 19:35:24 +0000931 if (DIType(Ty).isArtificial())
932 addFlag(Arg, dwarf::DW_AT_artificial);
Devang Patel161b2f42011-04-12 23:21:44 +0000933 Buffer.addChild(Arg);
934 }
935 }
Eric Christopher8b6fe6b2012-02-22 08:46:21 +0000936 // Add prototype flag if we're dealing with a C language and the
937 // function has been prototyped.
938 if (isPrototyped &&
Eric Christopher4d069bf2012-05-22 18:45:24 +0000939 (Language == dwarf::DW_LANG_C89 ||
940 Language == dwarf::DW_LANG_C99 ||
941 Language == dwarf::DW_LANG_ObjC))
Eric Christopher873cf0a2012-08-24 01:14:27 +0000942 addFlag(&Buffer, dwarf::DW_AT_prototyped);
Devang Patel161b2f42011-04-12 23:21:44 +0000943 }
944 break;
945 case dwarf::DW_TAG_structure_type:
946 case dwarf::DW_TAG_union_type:
947 case dwarf::DW_TAG_class_type: {
948 // Add elements to structure type.
949 DIArray Elements = CTy.getTypeArray();
950
951 // A forward struct declared type may not have elements available.
952 unsigned N = Elements.getNumElements();
953 if (N == 0)
954 break;
955
956 // Add elements to structure type.
957 for (unsigned i = 0; i < N; ++i) {
958 DIDescriptor Element = Elements.getElement(i);
959 DIE *ElemDie = NULL;
960 if (Element.isSubprogram()) {
961 DISubprogram SP(Element);
Devang Pateldbc64af2011-08-15 17:24:54 +0000962 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
Devang Patel161b2f42011-04-12 23:21:44 +0000963 if (SP.isProtected())
Nick Lewycky13aaca52011-12-13 05:09:11 +0000964 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000965 dwarf::DW_ACCESS_protected);
966 else if (SP.isPrivate())
Nick Lewycky13aaca52011-12-13 05:09:11 +0000967 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000968 dwarf::DW_ACCESS_private);
Eric Christopher8b4310b2012-11-21 00:34:38 +0000969 else
Nick Lewycky13aaca52011-12-13 05:09:11 +0000970 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000971 dwarf::DW_ACCESS_public);
972 if (SP.isExplicit())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000973 addFlag(ElemDie, dwarf::DW_AT_explicit);
Eric Christopher663e0cf2012-03-28 07:34:31 +0000974 } else if (Element.isDerivedType()) {
Eric Christopher4d069bf2012-05-22 18:45:24 +0000975 DIDerivedType DDTy(Element);
976 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
977 ElemDie = new DIE(dwarf::DW_TAG_friend);
978 addType(ElemDie, DDTy.getTypeDerivedFrom(), dwarf::DW_AT_friend);
Eric Christopher6b6061f2013-01-16 01:22:23 +0000979 } else if (DDTy.isStaticMember())
980 ElemDie = createStaticMemberDIE(DDTy);
981 else
982 ElemDie = createMemberDIE(DDTy);
Eric Christopher663e0cf2012-03-28 07:34:31 +0000983 } else if (Element.isObjCProperty()) {
Devang Patel30d409c2012-02-07 23:33:58 +0000984 DIObjCProperty Property(Element);
985 ElemDie = new DIE(Property.getTag());
986 StringRef PropertyName = Property.getObjCPropertyName();
987 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Eric Christopher4d069bf2012-05-22 18:45:24 +0000988 addType(ElemDie, Property.getType());
989 addSourceLine(ElemDie, Property);
Devang Patel30d409c2012-02-07 23:33:58 +0000990 StringRef GetterName = Property.getObjCPropertyGetterName();
991 if (!GetterName.empty())
992 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
993 StringRef SetterName = Property.getObjCPropertySetterName();
994 if (!SetterName.empty())
995 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
996 unsigned PropertyAttributes = 0;
Devang Patel9e11eb12012-02-04 01:30:32 +0000997 if (Property.isReadOnlyObjCProperty())
998 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
999 if (Property.isReadWriteObjCProperty())
1000 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1001 if (Property.isAssignObjCProperty())
1002 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1003 if (Property.isRetainObjCProperty())
1004 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1005 if (Property.isCopyObjCProperty())
1006 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1007 if (Property.isNonAtomicObjCProperty())
1008 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1009 if (PropertyAttributes)
Eric Christopher8b4310b2012-11-21 00:34:38 +00001010 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, 0,
Devang Patel9e11eb12012-02-04 01:30:32 +00001011 PropertyAttributes);
Devang Patel6588abf2012-02-06 17:49:43 +00001012
Devang Patel30d409c2012-02-07 23:33:58 +00001013 DIEEntry *Entry = getDIEEntry(Element);
1014 if (!Entry) {
1015 Entry = createDIEEntry(ElemDie);
1016 insertDIEEntry(Element, Entry);
1017 }
Devang Patel9e11eb12012-02-04 01:30:32 +00001018 } else
Devang Patel161b2f42011-04-12 23:21:44 +00001019 continue;
1020 Buffer.addChild(ElemDie);
1021 }
1022
1023 if (CTy.isAppleBlockExtension())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001024 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel161b2f42011-04-12 23:21:44 +00001025
Devang Patel161b2f42011-04-12 23:21:44 +00001026 DICompositeType ContainingType = CTy.getContainingType();
1027 if (DIDescriptor(ContainingType).isCompositeType())
1028 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
1029 getOrCreateTypeDIE(DIType(ContainingType)));
1030 else {
1031 DIDescriptor Context = CTy.getContext();
1032 addToContextOwner(&Buffer, Context);
1033 }
1034
Devang Patel201e6cd2011-05-12 21:29:42 +00001035 if (CTy.isObjcClassComplete())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001036 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patelb11f80e2011-05-12 19:06:16 +00001037
Eric Christopher1a8e8862011-12-16 23:42:42 +00001038 // Add template parameters to a class, structure or union types.
1039 // FIXME: The support isn't in the metadata for this yet.
1040 if (Tag == dwarf::DW_TAG_class_type ||
1041 Tag == dwarf::DW_TAG_structure_type ||
1042 Tag == dwarf::DW_TAG_union_type)
Devang Patel161b2f42011-04-12 23:21:44 +00001043 addTemplateParams(Buffer, CTy.getTemplateParams());
1044
1045 break;
1046 }
1047 default:
1048 break;
1049 }
1050
1051 // Add name if not anonymous or intermediate type.
1052 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001053 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001054
Eric Christopher4a5d8392012-05-22 18:45:18 +00001055 if (Tag == dwarf::DW_TAG_enumeration_type ||
1056 Tag == dwarf::DW_TAG_class_type ||
1057 Tag == dwarf::DW_TAG_structure_type ||
1058 Tag == dwarf::DW_TAG_union_type) {
Devang Patel161b2f42011-04-12 23:21:44 +00001059 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopherfc4199b2012-06-01 00:22:32 +00001060 // TODO: Do we care about size for enum forward declarations?
Devang Patel161b2f42011-04-12 23:21:44 +00001061 if (Size)
1062 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Eric Christopherfc4199b2012-06-01 00:22:32 +00001063 else if (!CTy.isForwardDecl())
Devang Patel161b2f42011-04-12 23:21:44 +00001064 // Add zero size if it is not a forward declaration.
Eric Christopherfc4199b2012-06-01 00:22:32 +00001065 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
1066
1067 // If we're a forward decl, say so.
1068 if (CTy.isForwardDecl())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001069 addFlag(&Buffer, dwarf::DW_AT_declaration);
Devang Patel161b2f42011-04-12 23:21:44 +00001070
1071 // Add source line info if available.
1072 if (!CTy.isForwardDecl())
1073 addSourceLine(&Buffer, CTy);
Eric Christopher89388952012-03-07 00:15:19 +00001074
1075 // No harm in adding the runtime language to the declaration.
1076 unsigned RLang = CTy.getRunTimeLang();
1077 if (RLang)
1078 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
1079 dwarf::DW_FORM_data1, RLang);
Devang Patel161b2f42011-04-12 23:21:44 +00001080 }
1081}
1082
Eric Christopher8b4310b2012-11-21 00:34:38 +00001083/// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
Devang Patel161b2f42011-04-12 23:21:44 +00001084/// for the given DITemplateTypeParameter.
1085DIE *
1086CompileUnit::getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP) {
1087 DIE *ParamDIE = getDIE(TP);
1088 if (ParamDIE)
1089 return ParamDIE;
1090
1091 ParamDIE = new DIE(dwarf::DW_TAG_template_type_parameter);
1092 addType(ParamDIE, TP.getType());
David Blaikiee88939c2013-06-22 18:59:11 +00001093 if (!TP.getName().empty())
1094 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel161b2f42011-04-12 23:21:44 +00001095 return ParamDIE;
1096}
1097
Eric Christopher8b4310b2012-11-21 00:34:38 +00001098/// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE
Devang Patel161b2f42011-04-12 23:21:44 +00001099/// for the given DITemplateValueParameter.
1100DIE *
Eric Christopher4d069bf2012-05-22 18:45:24 +00001101CompileUnit::getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TPV){
Devang Patel161b2f42011-04-12 23:21:44 +00001102 DIE *ParamDIE = getDIE(TPV);
1103 if (ParamDIE)
1104 return ParamDIE;
1105
David Blaikiee88939c2013-06-22 18:59:11 +00001106 ParamDIE = new DIE(TPV.getTag());
Devang Patel161b2f42011-04-12 23:21:44 +00001107 addType(ParamDIE, TPV.getType());
1108 if (!TPV.getName().empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001109 addString(ParamDIE, dwarf::DW_AT_name, TPV.getName());
David Blaikie4de9d722013-05-10 21:52:07 +00001110 if (Value *Val = TPV.getValue()) {
1111 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val))
1112 addConstantValue(ParamDIE, CI, TPV.getType().isUnsignedDIType());
1113 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) {
1114 // For declaration non-type template parameters (such as global values and
1115 // functions)
1116 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1117 addOpAddress(Block, Asm->Mang->getSymbol(GV));
1118 // Emit DW_OP_stack_value to use the address as the immediate value of the
1119 // parameter, rather than a pointer to it.
1120 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1121 addBlock(ParamDIE, dwarf::DW_AT_location, 0, Block);
David Blaikiee88939c2013-06-22 18:59:11 +00001122 } else if (TPV.getTag() == dwarf::DW_TAG_GNU_template_template_param) {
1123 assert(isa<MDString>(Val));
1124 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1125 cast<MDString>(Val)->getString());
1126 } else if (TPV.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
1127 assert(isa<MDNode>(Val));
1128 DIArray A(cast<MDNode>(Val));
1129 addTemplateParams(*ParamDIE, A);
David Blaikie4de9d722013-05-10 21:52:07 +00001130 }
1131 }
1132
Devang Patel161b2f42011-04-12 23:21:44 +00001133 return ParamDIE;
1134}
1135
Devang Patel31c5d052011-05-06 16:57:54 +00001136/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1137DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) {
1138 DIE *NDie = getDIE(NS);
1139 if (NDie)
1140 return NDie;
1141 NDie = new DIE(dwarf::DW_TAG_namespace);
1142 insertDIE(NS, NDie);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001143 if (!NS.getName().empty()) {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001144 addString(NDie, dwarf::DW_AT_name, NS.getName());
Eric Christopher09ac3d82011-11-07 09:24:32 +00001145 addAccelNamespace(NS.getName(), NDie);
1146 } else
1147 addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel31c5d052011-05-06 16:57:54 +00001148 addSourceLine(NDie, NS);
1149 addToContextOwner(NDie, NS.getContext());
1150 return NDie;
1151}
1152
Devang Pateldbc64af2011-08-15 17:24:54 +00001153/// getOrCreateSubprogramDIE - Create new DIE using SP.
1154DIE *CompileUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
1155 DIE *SPDie = getDIE(SP);
1156 if (SPDie)
1157 return SPDie;
1158
Peter Collingbourne27302f02012-05-27 18:36:44 +00001159 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1160
1161 // DW_TAG_inlined_subroutine may refer to this DIE.
1162 insertDIE(SP, SPDie);
1163
Rafael Espindola01b55b42011-11-10 22:34:29 +00001164 DISubprogram SPDecl = SP.getFunctionDeclaration();
1165 DIE *DeclDie = NULL;
1166 if (SPDecl.isSubprogram()) {
1167 DeclDie = getOrCreateSubprogramDIE(SPDecl);
1168 }
1169
Devang Pateldbc64af2011-08-15 17:24:54 +00001170 // Add to context owner.
1171 addToContextOwner(SPDie, SP.getContext());
1172
1173 // Add function template parameters.
1174 addTemplateParams(*SPDie, SP.getTemplateParams());
1175
Eric Christopherc4968752013-05-09 00:42:33 +00001176 // Unfortunately this code needs to stay here instead of below the
1177 // AT_specification code in order to work around a bug in older
1178 // gdbs that requires the linkage name to resolve multiple template
1179 // functions.
1180 // TODO: Remove this set of code when we get rid of the old gdb
1181 // compatibility.
Eric Christopher8d101c32012-03-15 08:19:33 +00001182 StringRef LinkageName = SP.getLinkageName();
Eric Christopherc4968752013-05-09 00:42:33 +00001183 if (!LinkageName.empty() && DD->useDarwinGDBCompat())
1184 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c2b4be2013-06-01 17:51:14 +00001185 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopher8d101c32012-03-15 08:19:33 +00001186
Devang Pateldbc64af2011-08-15 17:24:54 +00001187 // If this DIE is going to refer declaration info using AT_specification
Eric Christopherc4968752013-05-09 00:42:33 +00001188 // then there is no need to add other attributes.
Rafael Espindola01b55b42011-11-10 22:34:29 +00001189 if (DeclDie) {
1190 // Refer function declaration directly.
1191 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1192 DeclDie);
1193
Devang Pateldbc64af2011-08-15 17:24:54 +00001194 return SPDie;
Rafael Espindola01b55b42011-11-10 22:34:29 +00001195 }
Devang Pateldbc64af2011-08-15 17:24:54 +00001196
Eric Christophercbbd5b12012-08-23 22:52:55 +00001197 // Add the linkage name if we have one.
Eric Christopherc4968752013-05-09 00:42:33 +00001198 if (!LinkageName.empty() && !DD->useDarwinGDBCompat())
Eric Christophercbbd5b12012-08-23 22:52:55 +00001199 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c2b4be2013-06-01 17:51:14 +00001200 GlobalValue::getRealLinkageName(LinkageName));
Eric Christophercbbd5b12012-08-23 22:52:55 +00001201
Devang Pateldbc64af2011-08-15 17:24:54 +00001202 // Constructors and operators for anonymous aggregates do not have names.
1203 if (!SP.getName().empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001204 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Pateldbc64af2011-08-15 17:24:54 +00001205
1206 addSourceLine(SPDie, SP);
1207
Eric Christopher8b6fe6b2012-02-22 08:46:21 +00001208 // Add the prototype if we have a prototype and we have a C like
1209 // language.
1210 if (SP.isPrototyped() &&
1211 (Language == dwarf::DW_LANG_C89 ||
1212 Language == dwarf::DW_LANG_C99 ||
1213 Language == dwarf::DW_LANG_ObjC))
Eric Christopher873cf0a2012-08-24 01:14:27 +00001214 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Pateldbc64af2011-08-15 17:24:54 +00001215
1216 // Add Return Type.
1217 DICompositeType SPTy = SP.getType();
David Blaikie3d331842013-05-22 23:22:18 +00001218 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type &&
1219 "the type of a subprogram should be a subroutine");
Devang Pateldbc64af2011-08-15 17:24:54 +00001220
David Blaikie3d331842013-05-22 23:22:18 +00001221 DIArray Args = SPTy.getTypeArray();
1222 addType(SPDie, DIType(Args.getElement(0)));
Devang Pateldbc64af2011-08-15 17:24:54 +00001223
1224 unsigned VK = SP.getVirtuality();
1225 if (VK) {
Nick Lewycky798313d2011-12-14 00:56:07 +00001226 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Devang Pateldbc64af2011-08-15 17:24:54 +00001227 DIEBlock *Block = getDIEBlock();
1228 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1229 addUInt(Block, 0, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1230 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
1231 ContainingTypeMap.insert(std::make_pair(SPDie,
1232 SP.getContainingType()));
1233 }
1234
1235 if (!SP.isDefinition()) {
Eric Christopher873cf0a2012-08-24 01:14:27 +00001236 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher8b4310b2012-11-21 00:34:38 +00001237
Devang Pateldbc64af2011-08-15 17:24:54 +00001238 // Add arguments. Do not add arguments for subprogram definition. They will
1239 // be handled while processing variables.
David Blaikie3d331842013-05-22 23:22:18 +00001240 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1241 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1242 DIType ATy = DIType(Args.getElement(i));
1243 addType(Arg, ATy);
1244 if (ATy.isArtificial())
1245 addFlag(Arg, dwarf::DW_AT_artificial);
1246 SPDie->addChild(Arg);
1247 }
Devang Pateldbc64af2011-08-15 17:24:54 +00001248 }
1249
1250 if (SP.isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001251 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Pateldbc64af2011-08-15 17:24:54 +00001252
1253 if (!SP.isLocalToUnit())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001254 addFlag(SPDie, dwarf::DW_AT_external);
Devang Pateldbc64af2011-08-15 17:24:54 +00001255
1256 if (SP.isOptimized())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001257 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Pateldbc64af2011-08-15 17:24:54 +00001258
1259 if (unsigned isa = Asm->getISAEncoding()) {
1260 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1261 }
1262
1263 return SPDie;
1264}
1265
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001266// Return const expression if value is a GEP to access merged global
1267// constant. e.g.
1268// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1269static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1270 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1271 if (!CE || CE->getNumOperands() != 3 ||
1272 CE->getOpcode() != Instruction::GetElementPtr)
1273 return NULL;
1274
1275 // First operand points to a global struct.
1276 Value *Ptr = CE->getOperand(0);
1277 if (!isa<GlobalValue>(Ptr) ||
1278 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1279 return NULL;
1280
1281 // Second operand is zero.
1282 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1283 if (!CI || !CI->isZero())
1284 return NULL;
1285
1286 // Third operand is offset.
1287 if (!isa<ConstantInt>(CE->getOperand(2)))
1288 return NULL;
1289
1290 return CE;
1291}
1292
1293/// createGlobalVariableDIE - create global variable DIE.
1294void CompileUnit::createGlobalVariableDIE(const MDNode *N) {
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001295 // Check for pre-existence.
Devang Patel49e2f032011-08-18 22:21:50 +00001296 if (getDIE(N))
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001297 return;
1298
Devang Patel49e2f032011-08-18 22:21:50 +00001299 DIGlobalVariable GV(N);
Devang Patel28bea082011-08-18 23:17:55 +00001300 if (!GV.Verify())
1301 return;
1302
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001303 DIDescriptor GVContext = GV.getContext();
Eric Christopher6b6061f2013-01-16 01:22:23 +00001304 DIType GTy = GV.getType();
1305
1306 // If this is a static data member definition, some attributes belong
1307 // to the declaration DIE.
1308 DIE *VariableDIE = NULL;
Manman Ren945e8282013-02-01 23:54:37 +00001309 bool IsStaticMember = false;
Eric Christopher6b6061f2013-01-16 01:22:23 +00001310 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1311 if (SDMDecl.Verify()) {
1312 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1313 // We need the declaration DIE that is in the static member's class.
1314 // But that class might not exist in the DWARF yet.
1315 // Creating the class will create the static member decl DIE.
1316 getOrCreateContextDIE(SDMDecl.getContext());
1317 VariableDIE = getDIE(SDMDecl);
1318 assert(VariableDIE && "Static member decl has no context?");
Manman Ren945e8282013-02-01 23:54:37 +00001319 IsStaticMember = true;
Eric Christopher6b6061f2013-01-16 01:22:23 +00001320 }
1321
1322 // If this is not a static data member definition, create the variable
1323 // DIE and add the initial set of attributes to it.
1324 if (!VariableDIE) {
1325 VariableDIE = new DIE(GV.getTag());
1326 // Add to map.
1327 insertDIE(N, VariableDIE);
1328
1329 // Add name and type.
1330 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1331 addType(VariableDIE, GTy);
1332
1333 // Add scoping info.
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001334 if (!GV.isLocalToUnit()) {
Eric Christopher6b6061f2013-01-16 01:22:23 +00001335 addFlag(VariableDIE, dwarf::DW_AT_external);
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001336 addGlobalName(GV.getName(), VariableDIE);
1337 }
Eric Christopher6b6061f2013-01-16 01:22:23 +00001338
1339 // Add line number info.
1340 addSourceLine(VariableDIE, GV);
1341 // Add to context owner.
1342 addToContextOwner(VariableDIE, GVContext);
1343 }
1344
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001345 // Add location.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001346 bool addToAccelTable = false;
Eric Christopherd61c34b2011-11-11 03:16:32 +00001347 DIE *VariableSpecDIE = NULL;
Eric Christopher6b6061f2013-01-16 01:22:23 +00001348 bool isGlobalVariable = GV.getGlobal() != NULL;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001349 if (isGlobalVariable) {
Eric Christopher09ac3d82011-11-07 09:24:32 +00001350 addToAccelTable = true;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001351 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
David Blaikie59eaa382013-06-28 20:05:11 +00001352 const MCSymbol *Sym = Asm->Mang->getSymbol(GV.getGlobal());
1353 if (GV.getGlobal()->isThreadLocal()) {
1354 // FIXME: Make this work with -gsplit-dwarf.
1355 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
1356 assert((PointerSize == 4 || PointerSize == 8) &&
1357 "Add support for other sizes if necessary");
David Blaikie8fed05e2013-07-01 23:55:52 +00001358 const MCSymbolRefExpr *Ref =
1359 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym);
David Blaikie59eaa382013-06-28 20:05:11 +00001360 // Based on GCC's support for TLS:
David Blaikie8fed05e2013-07-01 23:55:52 +00001361 if (!DD->useSplitDwarf()) {
1362 // 1) Start with a constNu of the appropriate pointer size
1363 addUInt(Block, 0, dwarf::DW_FORM_data1,
1364 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
1365 // 2) containing the (relocated) address of the TLS variable
1366 addLabel(Block, 0, dwarf::DW_FORM_udata, Ref);
1367 } else {
1368 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
1369 addUInt(Block, 0, dwarf::DW_FORM_udata, DU->getAddrPoolIndex(Ref));
1370 }
David Blaikie59eaa382013-06-28 20:05:11 +00001371 // 3) followed by a custom OP to tell the debugger about TLS (presumably)
1372 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_lo_user);
1373 } else
1374 addOpAddress(Block, Sym);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001375 // Do not create specification DIE if context is either compile unit
1376 // or a subprogram.
Devang Patel1dd4e562011-09-21 23:41:11 +00001377 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001378 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
1379 // Create specification DIE.
Eric Christopherd117fbb2011-11-11 01:55:22 +00001380 VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001381 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1382 dwarf::DW_FORM_ref4, VariableDIE);
1383 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopher6b6061f2013-01-16 01:22:23 +00001384 // A static member's declaration is already flagged as such.
1385 if (!SDMDecl.Verify())
1386 addFlag(VariableDIE, dwarf::DW_AT_declaration);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001387 addDie(VariableSpecDIE);
1388 } else {
1389 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001390 }
Eric Christopher6b6061f2013-01-16 01:22:23 +00001391 // Add linkage name.
1392 StringRef LinkageName = GV.getLinkageName();
Eric Christophera8ada252013-02-27 23:49:50 +00001393 if (!LinkageName.empty()) {
Eric Christopher8d45a982013-02-27 23:49:47 +00001394 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1395 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1396 // TAG_variable.
Manman Ren21a08a12013-02-27 23:21:02 +00001397 addString(IsStaticMember && VariableSpecDIE ?
1398 VariableSpecDIE : VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c2b4be2013-06-01 17:51:14 +00001399 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopher8d45a982013-02-27 23:49:47 +00001400 // In compatibility mode with older gdbs we put the linkage name on both
1401 // the TAG_variable DIE and on the TAG_member DIE.
Manman Ren21a08a12013-02-27 23:21:02 +00001402 if (IsStaticMember && VariableSpecDIE && DD->useDarwinGDBCompat())
1403 addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c2b4be2013-06-01 17:51:14 +00001404 GlobalValue::getRealLinkageName(LinkageName));
Manman Ren06df83c2013-02-27 00:02:32 +00001405 }
Eric Christopher8b4310b2012-11-21 00:34:38 +00001406 } else if (const ConstantInt *CI =
Manman Ren945e8282013-02-01 23:54:37 +00001407 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
Adrian Prantl2e892e42013-04-04 22:56:49 +00001408 // AT_const_value was added when the static member was created. To avoid
Manman Ren945e8282013-02-01 23:54:37 +00001409 // emitting AT_const_value multiple times, we only add AT_const_value when
1410 // it is not a static member.
1411 if (!IsStaticMember)
1412 addConstantValue(VariableDIE, CI, GTy.isUnsignedDIType());
1413 } else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) {
Eric Christopher09ac3d82011-11-07 09:24:32 +00001414 addToAccelTable = true;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001415 // GV is a merged global.
1416 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1417 Value *Ptr = CE->getOperand(0);
Eric Christopher0969ddf2013-01-18 22:11:33 +00001418 addOpAddress(Block, Asm->Mang->getSymbol(cast<GlobalValue>(Ptr)));
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001419 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1420 SmallVector<Value*, 3> Idx(CE->op_begin()+1, CE->op_end());
Eric Christopher8b4310b2012-11-21 00:34:38 +00001421 addUInt(Block, 0, dwarf::DW_FORM_udata,
Micah Villmow3574eca2012-10-08 16:38:25 +00001422 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001423 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1424 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1425 }
1426
Eric Christopherd117fbb2011-11-11 01:55:22 +00001427 if (addToAccelTable) {
1428 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1429 addAccelName(GV.getName(), AddrDIE);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001430
Eric Christopherd117fbb2011-11-11 01:55:22 +00001431 // If the linkage name is different than the name, go ahead and output
1432 // that as well into the name table.
1433 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1434 addAccelName(GV.getLinkageName(), AddrDIE);
1435 }
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001436}
1437
Devang Patel161b2f42011-04-12 23:21:44 +00001438/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Eric Christopher4d069bf2012-05-22 18:45:24 +00001439void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR,
1440 DIE *IndexTy) {
Devang Patel161b2f42011-04-12 23:21:44 +00001441 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1442 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patel161b2f42011-04-12 23:21:44 +00001443
Bill Wendling222c2fd2012-12-06 07:38:10 +00001444 // The LowerBound value defines the lower bounds which is typically zero for
1445 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1446 // Count == -1 then the array is unbounded and we do not emit
1447 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1448 // Count == 0, then the array has zero elements in which case we do not emit
1449 // an upper bound.
1450 int64_t LowerBound = SR.getLo();
Bill Wendling6afe4782012-12-06 07:55:19 +00001451 int64_t DefaultLowerBound = getDefaultLowerBound();
Bill Wendling9493dae2012-12-04 21:34:03 +00001452 int64_t Count = SR.getCount();
Devang Patel161b2f42011-04-12 23:21:44 +00001453
Bill Wendling6afe4782012-12-06 07:55:19 +00001454 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
Bill Wendling222c2fd2012-12-06 07:38:10 +00001455 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, LowerBound);
1456
1457 if (Count != -1 && Count != 0)
Bill Wendling9493dae2012-12-04 21:34:03 +00001458 // FIXME: An unbounded array should reference the expression that defines
1459 // the array.
Bill Wendling222c2fd2012-12-06 07:38:10 +00001460 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, LowerBound + Count - 1);
Bill Wendling9493dae2012-12-04 21:34:03 +00001461
Devang Patel161b2f42011-04-12 23:21:44 +00001462 Buffer.addChild(DW_Subrange);
1463}
1464
1465/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
1466void CompileUnit::constructArrayTypeDIE(DIE &Buffer,
1467 DICompositeType *CTy) {
1468 Buffer.setTag(dwarf::DW_TAG_array_type);
Eric Christopher9a1e0e22013-01-08 01:53:52 +00001469 if (CTy->isVector())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001470 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel161b2f42011-04-12 23:21:44 +00001471
1472 // Emit derived type.
1473 addType(&Buffer, CTy->getTypeDerivedFrom());
1474 DIArray Elements = CTy->getTypeArray();
1475
1476 // Get an anonymous type for index type.
Eric Christopher8cab6ed2013-01-04 21:51:53 +00001477 // FIXME: This type should be passed down from the front end
1478 // as different languages may have different sizes for indexes.
Devang Patel161b2f42011-04-12 23:21:44 +00001479 DIE *IdxTy = getIndexTyDie();
1480 if (!IdxTy) {
1481 // Construct an anonymous type for index type.
1482 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Eric Christopher8cab6ed2013-01-04 21:51:53 +00001483 addString(IdxTy, dwarf::DW_AT_name, "int");
Devang Patel161b2f42011-04-12 23:21:44 +00001484 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1485 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1486 dwarf::DW_ATE_signed);
1487 addDie(IdxTy);
1488 setIndexTyDie(IdxTy);
1489 }
1490
1491 // Add subranges to array type.
1492 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1493 DIDescriptor Element = Elements.getElement(i);
1494 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1495 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1496 }
1497}
1498
1499/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
1500DIE *CompileUnit::constructEnumTypeDIE(DIEnumerator ETy) {
1501 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
1502 StringRef Name = ETy.getName();
Nick Lewycky390c40d2011-10-27 06:44:11 +00001503 addString(Enumerator, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001504 int64_t Value = ETy.getEnumValue();
1505 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
1506 return Enumerator;
1507}
1508
Devang Pateldbc64af2011-08-15 17:24:54 +00001509/// constructContainingTypeDIEs - Construct DIEs for types that contain
1510/// vtables.
1511void CompileUnit::constructContainingTypeDIEs() {
1512 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
1513 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1514 DIE *SPDie = CI->first;
1515 const MDNode *N = CI->second;
1516 if (!N) continue;
1517 DIE *NDie = getDIE(N);
1518 if (!NDie) continue;
1519 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1520 }
1521}
1522
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001523/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Eric Christopherf61dbc12013-06-24 21:07:27 +00001524DIE *CompileUnit::constructVariableDIE(DbgVariable *DV,
1525 bool isScopeAbstract) {
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001526 StringRef Name = DV->getName();
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001527
1528 // Translate tag to proper Dwarf tag.
1529 unsigned Tag = DV->getTag();
1530
1531 // Define variable debug information entry.
1532 DIE *VariableDie = new DIE(Tag);
1533 DbgVariable *AbsVar = DV->getAbstractVariable();
1534 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
Manman Ren742671b2013-05-29 17:16:59 +00001535 if (AbsDIE)
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001536 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Manman Ren742671b2013-05-29 17:16:59 +00001537 dwarf::DW_FORM_ref4, AbsDIE);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001538 else {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001539 addString(VariableDie, dwarf::DW_AT_name, Name);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001540 addSourceLine(VariableDie, DV->getVariable());
1541 addType(VariableDie, DV->getType());
1542 }
1543
1544 if (DV->isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001545 addFlag(VariableDie, dwarf::DW_AT_artificial);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001546
1547 if (isScopeAbstract) {
1548 DV->setDIE(VariableDie);
1549 return VariableDie;
1550 }
1551
1552 // Add variable address.
1553
1554 unsigned Offset = DV->getDotDebugLocOffset();
1555 if (Offset != ~0U) {
Eric Christopherafbe0e22013-06-24 21:34:55 +00001556 addLabel(VariableDie, dwarf::DW_AT_location, dwarf::DW_FORM_data4,
1557 Asm->GetTempSymbol("debug_loc", Offset));
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001558 DV->setDIE(VariableDie);
1559 return VariableDie;
1560 }
1561
Eric Christopher8cf5e742011-10-03 15:49:20 +00001562 // Check if variable is described by a DBG_VALUE instruction.
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001563 if (const MachineInstr *DVInsn = DV->getMInsn()) {
1564 bool updated = false;
David Blaikie6d9dbd52013-06-16 20:34:15 +00001565 assert(DVInsn->getNumOperands() == 3);
1566 if (DVInsn->getOperand(0).isReg()) {
1567 const MachineOperand RegOp = DVInsn->getOperand(0);
1568 if (int64_t Offset = DVInsn->getOperand(1).getImm()) {
1569 MachineLocation Location(RegOp.getReg(), Offset);
Eric Christopherf61dbc12013-06-24 21:07:27 +00001570 addVariableAddress(*DV, VariableDie, Location);
David Blaikie6d9dbd52013-06-16 20:34:15 +00001571 } else if (RegOp.getReg())
Eric Christopherf61dbc12013-06-24 21:07:27 +00001572 addVariableAddress(*DV, VariableDie, MachineLocation(RegOp.getReg()));
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001573 updated = true;
David Blaikie6d9dbd52013-06-16 20:34:15 +00001574 } else if (DVInsn->getOperand(0).isImm())
1575 updated =
1576 addConstantValue(VariableDie, DVInsn->getOperand(0), DV->getType());
1577 else if (DVInsn->getOperand(0).isFPImm())
1578 updated = addConstantFPValue(VariableDie, DVInsn->getOperand(0));
1579 else if (DVInsn->getOperand(0).isCImm())
1580 updated = addConstantValue(VariableDie, DVInsn->getOperand(0).getCImm(),
1581 DV->getType().isUnsignedDIType());
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001582 if (!updated) {
1583 // If variableDie is not updated then DBG_VALUE instruction does not
1584 // have valid variable info.
1585 delete VariableDie;
1586 return NULL;
1587 }
1588 DV->setDIE(VariableDie);
1589 return VariableDie;
1590 } else {
1591 // .. else use frame index.
1592 int FI = DV->getFrameIndex();
1593 if (FI != ~0) {
1594 unsigned FrameReg = 0;
1595 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopher8b4310b2012-11-21 00:34:38 +00001596 int Offset =
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001597 TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
1598 MachineLocation Location(FrameReg, Offset);
Eric Christopherf61dbc12013-06-24 21:07:27 +00001599 addVariableAddress(*DV, VariableDie, Location);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001600 }
1601 }
1602
1603 DV->setDIE(VariableDie);
1604 return VariableDie;
1605}
1606
Devang Patel161b2f42011-04-12 23:21:44 +00001607/// createMemberDIE - Create new member DIE.
1608DIE *CompileUnit::createMemberDIE(DIDerivedType DT) {
1609 DIE *MemberDie = new DIE(DT.getTag());
1610 StringRef Name = DT.getName();
1611 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001612 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001613
1614 addType(MemberDie, DT.getTypeDerivedFrom());
1615
1616 addSourceLine(MemberDie, DT);
1617
1618 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
1619 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1620
1621 uint64_t Size = DT.getSizeInBits();
1622 uint64_t FieldSize = DT.getOriginalTypeSize();
1623
1624 if (Size != FieldSize) {
1625 // Handle bitfield.
1626 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1627 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
1628
1629 uint64_t Offset = DT.getOffsetInBits();
1630 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1631 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1632 uint64_t FieldOffset = (HiMark - FieldSize);
1633 Offset -= FieldOffset;
1634
1635 // Maybe we need to work from the other end.
Micah Villmow3574eca2012-10-08 16:38:25 +00001636 if (Asm->getDataLayout().isLittleEndian())
Devang Patel161b2f42011-04-12 23:21:44 +00001637 Offset = FieldSize - (Offset + Size);
1638 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
1639
1640 // Here WD_AT_data_member_location points to the anonymous
1641 // field that includes this bit field.
1642 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
1643
1644 } else
1645 // This is not a bitfield.
1646 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
1647
1648 if (DT.getTag() == dwarf::DW_TAG_inheritance
1649 && DT.isVirtual()) {
1650
1651 // For C++, virtual base classes are not at fixed offset. Use following
1652 // expression to extract appropriate offset from vtable.
1653 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1654
1655 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
1656 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1657 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1658 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1659 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1660 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1661 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1662 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1663
1664 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1665 VBaseLocationDie);
1666 } else
1667 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
1668
1669 if (DT.isProtected())
Nick Lewycky13aaca52011-12-13 05:09:11 +00001670 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001671 dwarf::DW_ACCESS_protected);
1672 else if (DT.isPrivate())
Nick Lewycky13aaca52011-12-13 05:09:11 +00001673 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001674 dwarf::DW_ACCESS_private);
1675 // Otherwise C++ member and base classes are considered public.
Eric Christopher8b4310b2012-11-21 00:34:38 +00001676 else
Nick Lewycky13aaca52011-12-13 05:09:11 +00001677 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001678 dwarf::DW_ACCESS_public);
1679 if (DT.isVirtual())
Nick Lewycky798313d2011-12-14 00:56:07 +00001680 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001681 dwarf::DW_VIRTUALITY_virtual);
Devang Patele9db5e22011-04-16 00:11:51 +00001682
1683 // Objective-C properties.
Devang Patel6588abf2012-02-06 17:49:43 +00001684 if (MDNode *PNode = DT.getObjCProperty())
1685 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
Eric Christopher8b4310b2012-11-21 00:34:38 +00001686 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
Devang Patel6588abf2012-02-06 17:49:43 +00001687 PropertyDie);
1688
David Blaikie01bc2b32012-12-13 22:43:07 +00001689 if (DT.isArtificial())
1690 addFlag(MemberDie, dwarf::DW_AT_artificial);
1691
Devang Patel161b2f42011-04-12 23:21:44 +00001692 return MemberDie;
1693}
Eric Christopher6b6061f2013-01-16 01:22:23 +00001694
1695/// createStaticMemberDIE - Create new DIE for C++ static member.
1696DIE *CompileUnit::createStaticMemberDIE(const DIDerivedType DT) {
1697 if (!DT.Verify())
1698 return NULL;
1699
1700 DIE *StaticMemberDIE = new DIE(DT.getTag());
1701 DIType Ty = DT.getTypeDerivedFrom();
1702
1703 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1704 addType(StaticMemberDIE, Ty);
1705 addSourceLine(StaticMemberDIE, DT);
1706 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1707 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1708
1709 // FIXME: We could omit private if the parent is a class_type, and
1710 // public if the parent is something else.
1711 if (DT.isProtected())
1712 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1713 dwarf::DW_ACCESS_protected);
1714 else if (DT.isPrivate())
1715 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1716 dwarf::DW_ACCESS_private);
1717 else
1718 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1719 dwarf::DW_ACCESS_public);
1720
1721 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
1722 addConstantValue(StaticMemberDIE, CI, Ty.isUnsignedDIType());
David Blaikie14268412013-01-20 01:18:01 +00001723 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
1724 addConstantFPValue(StaticMemberDIE, CFP);
Eric Christopher6b6061f2013-01-16 01:22:23 +00001725
1726 insertDIE(DT, StaticMemberDIE);
1727 return StaticMemberDIE;
1728}