blob: 4c465435e5914049bf5645ca62f7e0275809088a [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"
30#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel161b2f42011-04-12 23:21:44 +000031
32using namespace llvm;
33
34/// CompileUnit - Compile unit constructor.
Eric Christopher9c57ad22013-05-08 00:58:51 +000035CompileUnit::CompileUnit(unsigned UID, unsigned L, DIE *D, const MDNode *N,
Eric Christopher5592ba42013-05-30 00:43:32 +000036 AsmPrinter *A, DwarfDebug *DW, DwarfUnits *DWU)
Eric Christopher2e5d8702012-12-20 21:58:36 +000037 : UniqueID(UID), Language(L), CUDie(D), Asm(A), DD(DW), DU(DWU),
Manman Renbc3e96f2013-03-12 18:27:15 +000038 IndexTyDie(0), DebugInfoOffset(0) {
Devang Patel161b2f42011-04-12 23:21:44 +000039 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
Eric Christopher9c57ad22013-05-08 00:58:51 +000040 insertDIE(N, D);
Devang Patel161b2f42011-04-12 23:21:44 +000041}
42
43/// ~CompileUnit - Destructor for compile unit.
44CompileUnit::~CompileUnit() {
45 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
46 DIEBlocks[j]->~DIEBlock();
47}
48
49/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
50/// information entry.
51DIEEntry *CompileUnit::createDIEEntry(DIE *Entry) {
52 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
53 return Value;
54}
55
Bill Wendling6afe4782012-12-06 07:55:19 +000056/// getDefaultLowerBound - Return the default lower bound for an array. If the
Bill Wendling222c2fd2012-12-06 07:38:10 +000057/// DWARF version doesn't handle the language, return -1.
Bill Wendling6afe4782012-12-06 07:55:19 +000058int64_t CompileUnit::getDefaultLowerBound() const {
Bill Wendling222c2fd2012-12-06 07:38:10 +000059 switch (Language) {
60 default:
61 break;
62
63 case dwarf::DW_LANG_C89:
64 case dwarf::DW_LANG_C99:
65 case dwarf::DW_LANG_C:
66 case dwarf::DW_LANG_C_plus_plus:
67 case dwarf::DW_LANG_ObjC:
68 case dwarf::DW_LANG_ObjC_plus_plus:
69 return 0;
70
71 case dwarf::DW_LANG_Fortran77:
72 case dwarf::DW_LANG_Fortran90:
73 case dwarf::DW_LANG_Fortran95:
74 return 1;
75
76 // The languages below have valid values only if the DWARF version >= 4.
77 case dwarf::DW_LANG_Java:
78 case dwarf::DW_LANG_Python:
79 case dwarf::DW_LANG_UPC:
80 case dwarf::DW_LANG_D:
81 if (dwarf::DWARF_VERSION >= 4)
82 return 0;
83 break;
84
85 case dwarf::DW_LANG_Ada83:
86 case dwarf::DW_LANG_Ada95:
87 case dwarf::DW_LANG_Cobol74:
88 case dwarf::DW_LANG_Cobol85:
89 case dwarf::DW_LANG_Modula2:
90 case dwarf::DW_LANG_Pascal83:
91 case dwarf::DW_LANG_PLI:
92 if (dwarf::DWARF_VERSION >= 4)
93 return 1;
94 break;
95 }
96
97 return -1;
98}
99
Eric Christopher873cf0a2012-08-24 01:14:27 +0000100/// addFlag - Add a flag that is true.
101void CompileUnit::addFlag(DIE *Die, unsigned Attribute) {
102 if (!DD->useDarwinGDBCompat())
103 Die->addValue(Attribute, dwarf::DW_FORM_flag_present,
104 DIEIntegerOne);
105 else
106 addUInt(Die, Attribute, dwarf::DW_FORM_flag, 1);
107}
108
Devang Patel161b2f42011-04-12 23:21:44 +0000109/// addUInt - Add an unsigned integer attribute data and value.
110///
111void CompileUnit::addUInt(DIE *Die, unsigned Attribute,
112 unsigned Form, uint64_t Integer) {
113 if (!Form) Form = DIEInteger::BestForm(false, Integer);
114 DIEValue *Value = Integer == 1 ?
115 DIEIntegerOne : new (DIEValueAllocator) DIEInteger(Integer);
116 Die->addValue(Attribute, Form, Value);
117}
118
119/// addSInt - Add an signed integer attribute data and value.
120///
121void CompileUnit::addSInt(DIE *Die, unsigned Attribute,
122 unsigned Form, int64_t Integer) {
123 if (!Form) Form = DIEInteger::BestForm(true, Integer);
124 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
125 Die->addValue(Attribute, Form, Value);
126}
127
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000128/// addString - Add a string attribute data and value. We always emit a
129/// reference to the string pool instead of immediate strings so that DIEs have
Eric Christopher3cc42202013-01-07 19:32:45 +0000130/// more predictable sizes. In the case of split dwarf we emit an index
131/// into another table which gets us the static offset into the string
132/// table.
Nick Lewycky390c40d2011-10-27 06:44:11 +0000133void CompileUnit::addString(DIE *Die, unsigned Attribute, StringRef String) {
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000134 if (!DD->useSplitDwarf()) {
135 MCSymbol *Symb = DU->getStringPoolEntry(String);
136 DIEValue *Value;
137 if (Asm->needsRelocationsForDwarfStringPool())
138 Value = new (DIEValueAllocator) DIELabel(Symb);
139 else {
140 MCSymbol *StringPool = DU->getStringPoolSym();
141 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
142 }
143 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value);
144 } else {
145 unsigned idx = DU->getStringPoolIndex(String);
146 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
147 Die->addValue(Attribute, dwarf::DW_FORM_GNU_str_index, Value);
148 }
149}
150
151/// addLocalString - Add a string attribute data and value. This is guaranteed
152/// to be in the local string pool instead of indirected.
153void CompileUnit::addLocalString(DIE *Die, unsigned Attribute,
154 StringRef String) {
Eric Christopher2e5d8702012-12-20 21:58:36 +0000155 MCSymbol *Symb = DU->getStringPoolEntry(String);
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000156 DIEValue *Value;
157 if (Asm->needsRelocationsForDwarfStringPool())
158 Value = new (DIEValueAllocator) DIELabel(Symb);
159 else {
Eric Christopher2e5d8702012-12-20 21:58:36 +0000160 MCSymbol *StringPool = DU->getStringPoolSym();
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000161 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
Nick Lewycky390c40d2011-10-27 06:44:11 +0000162 }
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000163 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value);
Devang Patel161b2f42011-04-12 23:21:44 +0000164}
165
166/// addLabel - Add a Dwarf label attribute data and value.
167///
168void CompileUnit::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
169 const MCSymbol *Label) {
170 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
171 Die->addValue(Attribute, Form, Value);
172}
173
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000174/// addLabelAddress - Add a dwarf label attribute data and value using
175/// DW_FORM_addr or DW_FORM_GNU_addr_index.
176///
177void CompileUnit::addLabelAddress(DIE *Die, unsigned Attribute,
178 MCSymbol *Label) {
179 if (!DD->useSplitDwarf()) {
180 if (Label != NULL) {
181 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
182 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
183 } else {
184 DIEValue *Value = new (DIEValueAllocator) DIEInteger(0);
185 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
186 }
187 } else {
188 unsigned idx = DU->getAddrPoolIndex(Label);
189 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
190 Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
191 }
192}
193
Eric Christopher0969ddf2013-01-18 22:11:33 +0000194/// addOpAddress - Add a dwarf op address data and value using the
195/// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
196///
197void CompileUnit::addOpAddress(DIE *Die, MCSymbol *Sym) {
198
199 if (!DD->useSplitDwarf()) {
200 addUInt(Die, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
201 addLabel(Die, 0, dwarf::DW_FORM_udata, Sym);
202 } else {
203 unsigned idx = DU->getAddrPoolIndex(Sym);
204 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
205 addUInt(Die, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
206 Die->addValue(0, dwarf::DW_FORM_GNU_addr_index, Value);
207 }
208}
209
Devang Patel161b2f42011-04-12 23:21:44 +0000210/// addDelta - Add a label delta attribute data and value.
211///
212void CompileUnit::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
213 const MCSymbol *Hi, const MCSymbol *Lo) {
214 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
215 Die->addValue(Attribute, Form, Value);
216}
217
218/// addDIEEntry - Add a DIE attribute data and value.
219///
220void CompileUnit::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
221 DIE *Entry) {
222 Die->addValue(Attribute, Form, createDIEEntry(Entry));
223}
224
Devang Patel161b2f42011-04-12 23:21:44 +0000225/// addBlock - Add block data.
226///
227void CompileUnit::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
228 DIEBlock *Block) {
229 Block->ComputeSize(Asm);
230 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
231 Die->addValue(Attribute, Block->BestForm(), Block);
232}
233
234/// addSourceLine - Add location information to specified debug information
235/// entry.
236void CompileUnit::addSourceLine(DIE *Die, DIVariable V) {
237 // Verify variable.
238 if (!V.Verify())
239 return;
Eric Christopher8b4310b2012-11-21 00:34:38 +0000240
Devang Patel161b2f42011-04-12 23:21:44 +0000241 unsigned Line = V.getLineNumber();
242 if (Line == 0)
243 return;
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000244 unsigned FileID = DD->getOrCreateSourceID(V.getContext().getFilename(),
Manman Ren3de61b42013-03-07 01:42:00 +0000245 V.getContext().getDirectory(),
246 getUniqueID());
Devang Patel161b2f42011-04-12 23:21:44 +0000247 assert(FileID && "Invalid file id");
248 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
249 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
250}
251
252/// addSourceLine - Add location information to specified debug information
253/// entry.
254void CompileUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
255 // Verify global variable.
256 if (!G.Verify())
257 return;
258
259 unsigned Line = G.getLineNumber();
260 if (Line == 0)
261 return;
Manman Ren3de61b42013-03-07 01:42:00 +0000262 unsigned FileID = DD->getOrCreateSourceID(G.getFilename(), G.getDirectory(),
263 getUniqueID());
Devang Patel161b2f42011-04-12 23:21:44 +0000264 assert(FileID && "Invalid file id");
265 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
266 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
267}
268
269/// addSourceLine - Add location information to specified debug information
270/// entry.
271void CompileUnit::addSourceLine(DIE *Die, DISubprogram SP) {
272 // Verify subprogram.
273 if (!SP.Verify())
274 return;
Eric Christopher2125d5a2012-03-15 23:55:40 +0000275
Devang Patel161b2f42011-04-12 23:21:44 +0000276 // If the line number is 0, don't add it.
Eric Christopher2125d5a2012-03-15 23:55:40 +0000277 unsigned Line = SP.getLineNumber();
278 if (Line == 0)
Devang Patel161b2f42011-04-12 23:21:44 +0000279 return;
280
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000281 unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(),
Manman Ren3de61b42013-03-07 01:42:00 +0000282 SP.getDirectory(), getUniqueID());
Devang Patel161b2f42011-04-12 23:21:44 +0000283 assert(FileID && "Invalid file id");
284 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
285 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
286}
287
288/// addSourceLine - Add location information to specified debug information
289/// entry.
290void CompileUnit::addSourceLine(DIE *Die, DIType Ty) {
291 // Verify type.
292 if (!Ty.Verify())
293 return;
294
295 unsigned Line = Ty.getLineNumber();
Eric Christopher2125d5a2012-03-15 23:55:40 +0000296 if (Line == 0)
Devang Patel161b2f42011-04-12 23:21:44 +0000297 return;
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000298 unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(),
Manman Ren3de61b42013-03-07 01:42:00 +0000299 Ty.getDirectory(), getUniqueID());
Devang Patel161b2f42011-04-12 23:21:44 +0000300 assert(FileID && "Invalid file id");
301 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
302 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
303}
304
305/// addSourceLine - Add location information to specified debug information
306/// entry.
Eric Christopherb8ca9882012-03-29 08:42:56 +0000307void CompileUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
308 // Verify type.
309 if (!Ty.Verify())
310 return;
311
312 unsigned Line = Ty.getLineNumber();
313 if (Line == 0)
314 return;
315 DIFile File = Ty.getFile();
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000316 unsigned FileID = DD->getOrCreateSourceID(File.getFilename(),
Manman Ren3de61b42013-03-07 01:42:00 +0000317 File.getDirectory(), getUniqueID());
Eric Christopherb8ca9882012-03-29 08:42:56 +0000318 assert(FileID && "Invalid file id");
319 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
320 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
321}
322
323/// addSourceLine - Add location information to specified debug information
324/// entry.
Devang Patel161b2f42011-04-12 23:21:44 +0000325void CompileUnit::addSourceLine(DIE *Die, DINameSpace NS) {
326 // Verify namespace.
327 if (!NS.Verify())
328 return;
329
330 unsigned Line = NS.getLineNumber();
331 if (Line == 0)
332 return;
333 StringRef FN = NS.getFilename();
334
Manman Ren3de61b42013-03-07 01:42:00 +0000335 unsigned FileID = DD->getOrCreateSourceID(FN, NS.getDirectory(),
336 getUniqueID());
Devang Patel161b2f42011-04-12 23:21:44 +0000337 assert(FileID && "Invalid file id");
338 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
339 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
340}
341
Eric Christopher8b4310b2012-11-21 00:34:38 +0000342/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patele1cdf842011-04-27 22:45:24 +0000343/// DbgVariable based on provided MachineLocation.
Eric Christopher8b4310b2012-11-21 00:34:38 +0000344void CompileUnit::addVariableAddress(DbgVariable *&DV, DIE *Die,
Devang Patele1cdf842011-04-27 22:45:24 +0000345 MachineLocation Location) {
Devang Patel161b2f42011-04-12 23:21:44 +0000346 if (DV->variableHasComplexAddress())
347 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
348 else if (DV->isBlockByrefVariable())
349 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
350 else
David Blaikie4532c282013-06-20 00:25:24 +0000351 addAddress(Die, dwarf::DW_AT_location, Location,
352 DV->getVariable().isIndirect());
Devang Patel161b2f42011-04-12 23:21:44 +0000353}
354
Devang Patel116da2f2011-04-26 19:06:18 +0000355/// addRegisterOp - Add register operand.
356void CompileUnit::addRegisterOp(DIE *TheDie, unsigned Reg) {
357 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
358 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
359 if (DWReg < 32)
360 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
361 else {
362 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
363 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
364 }
365}
366
367/// addRegisterOffset - Add register offset.
368void CompileUnit::addRegisterOffset(DIE *TheDie, unsigned Reg,
369 int64_t Offset) {
370 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
371 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
372 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
373 if (Reg == TRI->getFrameRegister(*Asm->MF))
374 // If variable offset is based in frame register then use fbreg.
375 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
376 else if (DWReg < 32)
377 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
378 else {
379 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
380 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
381 }
382 addSInt(TheDie, 0, dwarf::DW_FORM_sdata, Offset);
383}
384
385/// addAddress - Add an address attribute to a die based on the location
386/// provided.
387void CompileUnit::addAddress(DIE *Die, unsigned Attribute,
David Blaikie4532c282013-06-20 00:25:24 +0000388 const MachineLocation &Location, bool Indirect) {
Devang Patel116da2f2011-04-26 19:06:18 +0000389 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
390
David Blaikie4532c282013-06-20 00:25:24 +0000391 if (Location.isReg() && !Indirect)
Devang Patel116da2f2011-04-26 19:06:18 +0000392 addRegisterOp(Block, Location.getReg());
David Blaikie4532c282013-06-20 00:25:24 +0000393 else {
Devang Patel116da2f2011-04-26 19:06:18 +0000394 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
David Blaikie4532c282013-06-20 00:25:24 +0000395 if (Indirect && !Location.isReg()) {
396 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
397 }
398 }
Devang Patel116da2f2011-04-26 19:06:18 +0000399
400 // Now attach the location information to the DIE.
401 addBlock(Die, Attribute, 0, Block);
402}
403
Devang Patel161b2f42011-04-12 23:21:44 +0000404/// addComplexAddress - Start with the address based on the location provided,
405/// and generate the DWARF information necessary to find the actual variable
406/// given the extra address information encoded in the DIVariable, starting from
407/// the starting location. Add the DWARF information to the die.
408///
409void CompileUnit::addComplexAddress(DbgVariable *&DV, DIE *Die,
410 unsigned Attribute,
411 const MachineLocation &Location) {
Devang Patel161b2f42011-04-12 23:21:44 +0000412 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patelc26f5442011-04-28 02:22:40 +0000413 unsigned N = DV->getNumAddrElements();
414 unsigned i = 0;
415 if (Location.isReg()) {
416 if (N >= 2 && DV->getAddrElement(0) == DIBuilder::OpPlus) {
417 // If first address element is OpPlus then emit
418 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
419 addRegisterOffset(Block, Location.getReg(), DV->getAddrElement(1));
420 i = 2;
421 } else
422 addRegisterOp(Block, Location.getReg());
423 }
Devang Patel116da2f2011-04-26 19:06:18 +0000424 else
425 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel161b2f42011-04-12 23:21:44 +0000426
Devang Patelc26f5442011-04-28 02:22:40 +0000427 for (;i < N; ++i) {
Devang Patel161b2f42011-04-12 23:21:44 +0000428 uint64_t Element = DV->getAddrElement(i);
Devang Patel161b2f42011-04-12 23:21:44 +0000429 if (Element == DIBuilder::OpPlus) {
430 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
431 addUInt(Block, 0, dwarf::DW_FORM_udata, DV->getAddrElement(++i));
432 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher50120762012-05-08 18:56:00 +0000433 if (!Location.isReg())
434 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel161b2f42011-04-12 23:21:44 +0000435 } else llvm_unreachable("unknown DIBuilder Opcode");
436 }
437
438 // Now attach the location information to the DIE.
439 addBlock(Die, Attribute, 0, Block);
440}
441
442/* Byref variables, in Blocks, are declared by the programmer as "SomeType
443 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
444 gives the variable VarName either the struct, or a pointer to the struct, as
445 its type. This is necessary for various behind-the-scenes things the
446 compiler needs to do with by-reference variables in Blocks.
447
448 However, as far as the original *programmer* is concerned, the variable
449 should still have type 'SomeType', as originally declared.
450
451 The function getBlockByrefType dives into the __Block_byref_x_VarName
452 struct to find the original type of the variable, which is then assigned to
453 the variable's Debug Information Entry as its real type. So far, so good.
454 However now the debugger will expect the variable VarName to have the type
455 SomeType. So we need the location attribute for the variable to be an
456 expression that explains to the debugger how to navigate through the
457 pointers and struct to find the actual variable of type SomeType.
458
459 The following function does just that. We start by getting
460 the "normal" location for the variable. This will be the location
461 of either the struct __Block_byref_x_VarName or the pointer to the
462 struct __Block_byref_x_VarName.
463
464 The struct will look something like:
465
466 struct __Block_byref_x_VarName {
467 ... <various fields>
468 struct __Block_byref_x_VarName *forwarding;
469 ... <various other fields>
470 SomeType VarName;
471 ... <maybe more fields>
472 };
473
474 If we are given the struct directly (as our starting point) we
475 need to tell the debugger to:
476
477 1). Add the offset of the forwarding field.
478
479 2). Follow that pointer to get the real __Block_byref_x_VarName
480 struct to use (the real one may have been copied onto the heap).
481
482 3). Add the offset for the field VarName, to find the actual variable.
483
484 If we started with a pointer to the struct, then we need to
485 dereference that pointer first, before the other steps.
486 Translating this into DWARF ops, we will need to append the following
487 to the current location description for the variable:
488
489 DW_OP_deref -- optional, if we start with a pointer
490 DW_OP_plus_uconst <forward_fld_offset>
491 DW_OP_deref
492 DW_OP_plus_uconst <varName_fld_offset>
493
494 That is what this function does. */
495
496/// addBlockByrefAddress - Start with the address based on the location
497/// provided, and generate the DWARF information necessary to find the
498/// actual Block variable (navigating the Block struct) based on the
499/// starting location. Add the DWARF information to the die. For
500/// more information, read large comment just above here.
501///
502void CompileUnit::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
503 unsigned Attribute,
504 const MachineLocation &Location) {
505 DIType Ty = DV->getType();
506 DIType TmpTy = Ty;
507 unsigned Tag = Ty.getTag();
508 bool isPointer = false;
509
510 StringRef varName = DV->getName();
511
512 if (Tag == dwarf::DW_TAG_pointer_type) {
513 DIDerivedType DTy = DIDerivedType(Ty);
514 TmpTy = DTy.getTypeDerivedFrom();
515 isPointer = true;
516 }
517
518 DICompositeType blockStruct = DICompositeType(TmpTy);
519
520 // Find the __forwarding field and the variable field in the __Block_byref
521 // struct.
522 DIArray Fields = blockStruct.getTypeArray();
523 DIDescriptor varField = DIDescriptor();
524 DIDescriptor forwardingField = DIDescriptor();
525
526 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
527 DIDescriptor Element = Fields.getElement(i);
528 DIDerivedType DT = DIDerivedType(Element);
529 StringRef fieldName = DT.getName();
530 if (fieldName == "__forwarding")
531 forwardingField = Element;
532 else if (fieldName == varName)
533 varField = Element;
534 }
535
536 // Get the offsets for the forwarding field and the variable field.
537 unsigned forwardingFieldOffset =
538 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
539 unsigned varFieldOffset =
540 DIDerivedType(varField).getOffsetInBits() >> 3;
541
542 // Decode the original location, and use that as the start of the byref
543 // variable's location.
Devang Patel161b2f42011-04-12 23:21:44 +0000544 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
545
Eric Christophercaba2632012-07-04 02:02:18 +0000546 if (Location.isReg())
547 addRegisterOp(Block, Location.getReg());
548 else
549 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel161b2f42011-04-12 23:21:44 +0000550
551 // If we started with a pointer to the __Block_byref... struct, then
552 // the first thing we need to do is dereference the pointer (DW_OP_deref).
553 if (isPointer)
554 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
555
556 // Next add the offset for the '__forwarding' field:
557 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
558 // adding the offset if it's 0.
559 if (forwardingFieldOffset > 0) {
560 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
561 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
562 }
563
564 // Now dereference the __forwarding field to get to the real __Block_byref
565 // struct: DW_OP_deref.
566 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
567
568 // Now that we've got the real __Block_byref... struct, add the offset
569 // for the variable's field to get to the location of the actual variable:
570 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
571 if (varFieldOffset > 0) {
572 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
573 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
574 }
575
576 // Now attach the location information to the DIE.
577 addBlock(Die, Attribute, 0, Block);
578}
579
Devang Patel4ec14b02011-07-20 21:57:04 +0000580/// isTypeSigned - Return true if the type is signed.
581static bool isTypeSigned(DIType Ty, int *SizeInBits) {
582 if (Ty.isDerivedType())
583 return isTypeSigned(DIDerivedType(Ty).getTypeDerivedFrom(), SizeInBits);
584 if (Ty.isBasicType())
585 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed
586 || DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
587 *SizeInBits = Ty.getSizeInBits();
588 return true;
589 }
590 return false;
591}
592
Devang Patel161b2f42011-04-12 23:21:44 +0000593/// addConstantValue - Add constant value entry in variable DIE.
Devang Patelb58128e2011-05-27 16:45:18 +0000594bool CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
595 DIType Ty) {
David Blaikie4de9d722013-05-10 21:52:07 +0000596 // FIXME: This is a bit conservative/simple - it emits negative values at
597 // their maximum bit width which is a bit unfortunate (& doesn't prefer
598 // udata/sdata over dataN as suggested by the DWARF spec)
Nick Lewycky746cb672011-10-26 22:55:33 +0000599 assert(MO.isImm() && "Invalid machine operand!");
Devang Patel161b2f42011-04-12 23:21:44 +0000600 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel4ec14b02011-07-20 21:57:04 +0000601 int SizeInBits = -1;
602 bool SignedConstant = isTypeSigned(Ty, &SizeInBits);
603 unsigned Form = SignedConstant ? dwarf::DW_FORM_sdata : dwarf::DW_FORM_udata;
604 switch (SizeInBits) {
605 case 8: Form = dwarf::DW_FORM_data1; break;
606 case 16: Form = dwarf::DW_FORM_data2; break;
607 case 32: Form = dwarf::DW_FORM_data4; break;
608 case 64: Form = dwarf::DW_FORM_data8; break;
Devang Patel045c1d42011-05-27 19:13:26 +0000609 default: break;
610 }
Eric Christopher8b4310b2012-11-21 00:34:38 +0000611 SignedConstant ? addSInt(Block, 0, Form, MO.getImm())
Devang Patel4ec14b02011-07-20 21:57:04 +0000612 : addUInt(Block, 0, Form, MO.getImm());
Devang Patel72f0d9c2011-05-27 18:15:52 +0000613
Devang Patel161b2f42011-04-12 23:21:44 +0000614 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
615 return true;
616}
617
618/// addConstantFPValue - Add constant value entry in variable DIE.
619bool CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Nick Lewycky390c40d2011-10-27 06:44:11 +0000620 assert (MO.isFPImm() && "Invalid machine operand!");
Devang Patel161b2f42011-04-12 23:21:44 +0000621 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
622 APFloat FPImm = MO.getFPImm()->getValueAPF();
623
624 // Get the raw data form of the floating point.
625 const APInt FltVal = FPImm.bitcastToAPInt();
626 const char *FltPtr = (const char*)FltVal.getRawData();
627
628 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmow3574eca2012-10-08 16:38:25 +0000629 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel161b2f42011-04-12 23:21:44 +0000630 int Incr = (LittleEndian ? 1 : -1);
631 int Start = (LittleEndian ? 0 : NumBytes - 1);
632 int Stop = (LittleEndian ? NumBytes : -1);
633
634 // Output the constant to DWARF one byte at a time.
635 for (; Start != Stop; Start += Incr)
636 addUInt(Block, 0, dwarf::DW_FORM_data1,
637 (unsigned char)0xFF & FltPtr[Start]);
638
639 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
640 return true;
641}
642
David Blaikie14268412013-01-20 01:18:01 +0000643/// addConstantFPValue - Add constant value entry in variable DIE.
644bool CompileUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) {
645 return addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), false);
646}
647
Devang Patel161b2f42011-04-12 23:21:44 +0000648/// addConstantValue - Add constant value entry in variable DIE.
Devang Patel8594d422011-06-24 20:46:11 +0000649bool CompileUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
Devang Patel161b2f42011-04-12 23:21:44 +0000650 bool Unsigned) {
David Blaikie14268412013-01-20 01:18:01 +0000651 return addConstantValue(Die, CI->getValue(), Unsigned);
652}
653
654// addConstantValue - Add constant value entry in variable DIE.
655bool CompileUnit::addConstantValue(DIE *Die, const APInt &Val,
656 bool Unsigned) {
657 unsigned CIBitWidth = Val.getBitWidth();
Devang Pateld6a81362011-05-28 00:39:18 +0000658 if (CIBitWidth <= 64) {
659 unsigned form = 0;
660 switch (CIBitWidth) {
661 case 8: form = dwarf::DW_FORM_data1; break;
662 case 16: form = dwarf::DW_FORM_data2; break;
663 case 32: form = dwarf::DW_FORM_data4; break;
664 case 64: form = dwarf::DW_FORM_data8; break;
Eric Christopher8b4310b2012-11-21 00:34:38 +0000665 default:
Devang Pateld6a81362011-05-28 00:39:18 +0000666 form = Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata;
667 }
Devang Patel161b2f42011-04-12 23:21:44 +0000668 if (Unsigned)
David Blaikie14268412013-01-20 01:18:01 +0000669 addUInt(Die, dwarf::DW_AT_const_value, form, Val.getZExtValue());
Devang Patel161b2f42011-04-12 23:21:44 +0000670 else
David Blaikie14268412013-01-20 01:18:01 +0000671 addSInt(Die, dwarf::DW_AT_const_value, form, Val.getSExtValue());
Devang Patel161b2f42011-04-12 23:21:44 +0000672 return true;
673 }
674
675 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
676
677 // Get the raw data form of the large APInt.
NAKAMURA Takumic3e48c32011-10-28 14:12:22 +0000678 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel161b2f42011-04-12 23:21:44 +0000679
680 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmow3574eca2012-10-08 16:38:25 +0000681 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel161b2f42011-04-12 23:21:44 +0000682
683 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumic3e48c32011-10-28 14:12:22 +0000684 for (int i = 0; i < NumBytes; i++) {
685 uint8_t c;
686 if (LittleEndian)
687 c = Ptr64[i / 8] >> (8 * (i & 7));
688 else
689 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
690 addUInt(Block, 0, dwarf::DW_FORM_data1, c);
691 }
Devang Patel161b2f42011-04-12 23:21:44 +0000692
693 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
694 return true;
695}
696
Eric Christopher6c3bb942013-04-22 07:47:40 +0000697/// addTemplateParams - Add template parameters into buffer.
Devang Patel161b2f42011-04-12 23:21:44 +0000698void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
699 // Add template parameters.
700 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
701 DIDescriptor Element = TParams.getElement(i);
702 if (Element.isTemplateTypeParameter())
703 Buffer.addChild(getOrCreateTemplateTypeParameterDIE(
704 DITemplateTypeParameter(Element)));
705 else if (Element.isTemplateValueParameter())
706 Buffer.addChild(getOrCreateTemplateValueParameterDIE(
707 DITemplateValueParameter(Element)));
708 }
Devang Patel161b2f42011-04-12 23:21:44 +0000709}
Nick Lewycky746cb672011-10-26 22:55:33 +0000710
Eric Christopher6b6061f2013-01-16 01:22:23 +0000711/// getOrCreateContextDIE - Get context owner's DIE.
712DIE *CompileUnit::getOrCreateContextDIE(DIDescriptor Context) {
713 if (Context.isType())
714 return getOrCreateTypeDIE(DIType(Context));
715 else if (Context.isNameSpace())
716 return getOrCreateNameSpace(DINameSpace(Context));
717 else if (Context.isSubprogram())
718 return getOrCreateSubprogramDIE(DISubprogram(Context));
Eric Christopher6c3bb942013-04-22 07:47:40 +0000719 else
Eric Christopher6b6061f2013-01-16 01:22:23 +0000720 return getDIE(Context);
721}
722
Devang Patel161b2f42011-04-12 23:21:44 +0000723/// addToContextOwner - Add Die into the list of its context owner's children.
724void CompileUnit::addToContextOwner(DIE *Die, DIDescriptor Context) {
Eric Christopher6b6061f2013-01-16 01:22:23 +0000725 if (DIE *ContextDIE = getOrCreateContextDIE(Context))
Devang Patel161b2f42011-04-12 23:21:44 +0000726 ContextDIE->addChild(Die);
727 else
728 addDie(Die);
729}
730
731/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
732/// given DIType.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000733DIE *CompileUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
734 DIType Ty(TyNode);
735 if (!Ty.Verify())
736 return NULL;
Devang Patel161b2f42011-04-12 23:21:44 +0000737 DIE *TyDIE = getDIE(Ty);
738 if (TyDIE)
739 return TyDIE;
740
741 // Create new type.
742 TyDIE = new DIE(dwarf::DW_TAG_base_type);
743 insertDIE(Ty, TyDIE);
744 if (Ty.isBasicType())
745 constructTypeDIE(*TyDIE, DIBasicType(Ty));
746 else if (Ty.isCompositeType())
747 constructTypeDIE(*TyDIE, DICompositeType(Ty));
748 else {
749 assert(Ty.isDerivedType() && "Unknown kind of DIType");
750 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
751 }
Eric Christopher1b3f9192011-11-10 19:52:58 +0000752 // If this is a named finished type then include it in the list of types
753 // for the accelerator tables.
Eric Christopherc36145f2012-01-06 04:35:23 +0000754 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
755 bool IsImplementation = 0;
756 if (Ty.isCompositeType()) {
757 DICompositeType CT(Ty);
Eric Christophere0167892012-01-06 23:03:37 +0000758 // A runtime language of 0 actually means C/C++ and that any
759 // non-negative value is some version of Objective-C/C++.
Eric Christopherc36145f2012-01-06 04:35:23 +0000760 IsImplementation = (CT.getRunTimeLang() == 0) ||
Eric Christophere2dc9332012-02-22 08:46:02 +0000761 CT.isObjcClassComplete();
Eric Christopherc36145f2012-01-06 04:35:23 +0000762 }
Eric Christophere0167892012-01-06 23:03:37 +0000763 unsigned Flags = IsImplementation ?
764 DwarfAccelTable::eTypeFlagClassIsImplementation : 0;
765 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
Eric Christopherc36145f2012-01-06 04:35:23 +0000766 }
Eric Christopher8b4310b2012-11-21 00:34:38 +0000767
Devang Patel161b2f42011-04-12 23:21:44 +0000768 addToContextOwner(TyDIE, Ty.getContext());
769 return TyDIE;
770}
771
772/// addType - Add a new type attribute to the specified entity.
Eric Christopher4d069bf2012-05-22 18:45:24 +0000773void CompileUnit::addType(DIE *Entity, DIType Ty, unsigned Attribute) {
Devang Patel161b2f42011-04-12 23:21:44 +0000774 if (!Ty.Verify())
775 return;
776
777 // Check for pre-existence.
778 DIEEntry *Entry = getDIEEntry(Ty);
779 // If it exists then use the existing value.
780 if (Entry) {
Eric Christopher663e0cf2012-03-28 07:34:31 +0000781 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
Devang Patel161b2f42011-04-12 23:21:44 +0000782 return;
783 }
784
785 // Construct type.
786 DIE *Buffer = getOrCreateTypeDIE(Ty);
787
788 // Set up proxy.
789 Entry = createDIEEntry(Buffer);
790 insertDIEEntry(Ty, Entry);
Eric Christopher663e0cf2012-03-28 07:34:31 +0000791 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
Devang Patele9ae06c2011-05-31 22:56:51 +0000792
793 // If this is a complete composite type then include it in the
794 // list of global types.
Devang Patelc20bdf12011-06-01 00:23:24 +0000795 addGlobalType(Ty);
Devang Patel66658e42011-05-31 23:30:30 +0000796}
797
798/// addGlobalType - Add a new global type to the compile unit.
799///
Devang Patelc20bdf12011-06-01 00:23:24 +0000800void CompileUnit::addGlobalType(DIType Ty) {
Devang Patele9ae06c2011-05-31 22:56:51 +0000801 DIDescriptor Context = Ty.getContext();
Eric Christopher8b4310b2012-11-21 00:34:38 +0000802 if (Ty.isCompositeType() && !Ty.getName().empty() && !Ty.isForwardDecl()
803 && (!Context || Context.isCompileUnit() || Context.isFile()
Devang Patel94c7ddb2011-08-16 22:09:43 +0000804 || Context.isNameSpace()))
Devang Patelc20bdf12011-06-01 00:23:24 +0000805 if (DIEEntry *Entry = getDIEEntry(Ty))
806 GlobalTypes[Ty.getName()] = Entry->getEntry();
Devang Patel161b2f42011-04-12 23:21:44 +0000807}
808
Devang Patel31c5d052011-05-06 16:57:54 +0000809/// addPubTypes - Add type for pubtypes section.
810void CompileUnit::addPubTypes(DISubprogram SP) {
811 DICompositeType SPTy = SP.getType();
812 unsigned SPTag = SPTy.getTag();
813 if (SPTag != dwarf::DW_TAG_subroutine_type)
814 return;
815
816 DIArray Args = SPTy.getTypeArray();
817 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
818 DIType ATy(Args.getElement(i));
819 if (!ATy.Verify())
820 continue;
Devang Patelc20bdf12011-06-01 00:23:24 +0000821 addGlobalType(ATy);
Devang Patel31c5d052011-05-06 16:57:54 +0000822 }
823}
824
Devang Patel161b2f42011-04-12 23:21:44 +0000825/// constructTypeDIE - Construct basic type die from DIBasicType.
826void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
827 // Get core information.
828 StringRef Name = BTy.getName();
Devang Patel161b2f42011-04-12 23:21:44 +0000829 // Add name if not anonymous or intermediate type.
830 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000831 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel734a67c2011-09-14 23:13:28 +0000832
833 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type) {
834 Buffer.setTag(dwarf::DW_TAG_unspecified_type);
835 // Unspecified types has only name, nothing else.
836 return;
837 }
838
839 Buffer.setTag(dwarf::DW_TAG_base_type);
Nick Lewycky746cb672011-10-26 22:55:33 +0000840 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel30d409c2012-02-07 23:33:58 +0000841 BTy.getEncoding());
Devang Patel734a67c2011-09-14 23:13:28 +0000842
Devang Patel161b2f42011-04-12 23:21:44 +0000843 uint64_t Size = BTy.getSizeInBits() >> 3;
844 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
845}
846
847/// constructTypeDIE - Construct derived type die from DIDerivedType.
848void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
849 // Get core information.
850 StringRef Name = DTy.getName();
851 uint64_t Size = DTy.getSizeInBits() >> 3;
852 unsigned Tag = DTy.getTag();
853
854 // FIXME - Workaround for templates.
855 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
856
857 Buffer.setTag(Tag);
858
859 // Map to main type, void will not have a type.
860 DIType FromTy = DTy.getTypeDerivedFrom();
861 addType(&Buffer, FromTy);
862
863 // Add name if not anonymous or intermediate type.
864 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000865 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +0000866
867 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher35f225a2012-02-21 22:25:53 +0000868 if (Size && Tag != dwarf::DW_TAG_pointer_type)
Devang Patel161b2f42011-04-12 23:21:44 +0000869 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
870
David Blaikie62fdfb52013-01-07 05:51:15 +0000871 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
872 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
873 getOrCreateTypeDIE(DTy.getClassType()));
Devang Patel161b2f42011-04-12 23:21:44 +0000874 // Add source line info if available and TyDesc is not a forward declaration.
875 if (!DTy.isForwardDecl())
876 addSourceLine(&Buffer, DTy);
877}
878
879/// constructTypeDIE - Construct type DIE from DICompositeType.
880void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
881 // Get core information.
882 StringRef Name = CTy.getName();
883
884 uint64_t Size = CTy.getSizeInBits() >> 3;
885 unsigned Tag = CTy.getTag();
886 Buffer.setTag(Tag);
887
888 switch (Tag) {
Devang Patel161b2f42011-04-12 23:21:44 +0000889 case dwarf::DW_TAG_array_type:
890 constructArrayTypeDIE(Buffer, &CTy);
891 break;
892 case dwarf::DW_TAG_enumeration_type: {
893 DIArray Elements = CTy.getTypeArray();
894
895 // Add enumerators to enumeration type.
896 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
897 DIE *ElemDie = NULL;
898 DIDescriptor Enum(Elements.getElement(i));
899 if (Enum.isEnumerator()) {
900 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
901 Buffer.addChild(ElemDie);
902 }
903 }
Eric Christopherbb0f6ea2012-05-23 00:09:20 +0000904 DIType DTy = CTy.getTypeDerivedFrom();
905 if (DTy.Verify()) {
906 addType(&Buffer, DTy);
907 addUInt(&Buffer, dwarf::DW_AT_enum_class, dwarf::DW_FORM_flag, 1);
908 }
Devang Patel161b2f42011-04-12 23:21:44 +0000909 }
910 break;
911 case dwarf::DW_TAG_subroutine_type: {
912 // Add return type.
913 DIArray Elements = CTy.getTypeArray();
914 DIDescriptor RTy = Elements.getElement(0);
915 addType(&Buffer, DIType(RTy));
916
917 bool isPrototyped = true;
918 // Add arguments.
919 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
920 DIDescriptor Ty = Elements.getElement(i);
921 if (Ty.isUnspecifiedParameter()) {
922 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters);
923 Buffer.addChild(Arg);
924 isPrototyped = false;
925 } else {
926 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
927 addType(Arg, DIType(Ty));
David Blaikieaaf2e632013-01-29 19:35:24 +0000928 if (DIType(Ty).isArtificial())
929 addFlag(Arg, dwarf::DW_AT_artificial);
Devang Patel161b2f42011-04-12 23:21:44 +0000930 Buffer.addChild(Arg);
931 }
932 }
Eric Christopher8b6fe6b2012-02-22 08:46:21 +0000933 // Add prototype flag if we're dealing with a C language and the
934 // function has been prototyped.
935 if (isPrototyped &&
Eric Christopher4d069bf2012-05-22 18:45:24 +0000936 (Language == dwarf::DW_LANG_C89 ||
937 Language == dwarf::DW_LANG_C99 ||
938 Language == dwarf::DW_LANG_ObjC))
Eric Christopher873cf0a2012-08-24 01:14:27 +0000939 addFlag(&Buffer, dwarf::DW_AT_prototyped);
Devang Patel161b2f42011-04-12 23:21:44 +0000940 }
941 break;
942 case dwarf::DW_TAG_structure_type:
943 case dwarf::DW_TAG_union_type:
944 case dwarf::DW_TAG_class_type: {
945 // Add elements to structure type.
946 DIArray Elements = CTy.getTypeArray();
947
948 // A forward struct declared type may not have elements available.
949 unsigned N = Elements.getNumElements();
950 if (N == 0)
951 break;
952
953 // Add elements to structure type.
954 for (unsigned i = 0; i < N; ++i) {
955 DIDescriptor Element = Elements.getElement(i);
956 DIE *ElemDie = NULL;
957 if (Element.isSubprogram()) {
958 DISubprogram SP(Element);
Devang Pateldbc64af2011-08-15 17:24:54 +0000959 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
Devang Patel161b2f42011-04-12 23:21:44 +0000960 if (SP.isProtected())
Nick Lewycky13aaca52011-12-13 05:09:11 +0000961 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000962 dwarf::DW_ACCESS_protected);
963 else if (SP.isPrivate())
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_private);
Eric Christopher8b4310b2012-11-21 00:34:38 +0000966 else
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_public);
969 if (SP.isExplicit())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000970 addFlag(ElemDie, dwarf::DW_AT_explicit);
Eric Christopher663e0cf2012-03-28 07:34:31 +0000971 } else if (Element.isDerivedType()) {
Eric Christopher4d069bf2012-05-22 18:45:24 +0000972 DIDerivedType DDTy(Element);
973 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
974 ElemDie = new DIE(dwarf::DW_TAG_friend);
975 addType(ElemDie, DDTy.getTypeDerivedFrom(), dwarf::DW_AT_friend);
Eric Christopher6b6061f2013-01-16 01:22:23 +0000976 } else if (DDTy.isStaticMember())
977 ElemDie = createStaticMemberDIE(DDTy);
978 else
979 ElemDie = createMemberDIE(DDTy);
Eric Christopher663e0cf2012-03-28 07:34:31 +0000980 } else if (Element.isObjCProperty()) {
Devang Patel30d409c2012-02-07 23:33:58 +0000981 DIObjCProperty Property(Element);
982 ElemDie = new DIE(Property.getTag());
983 StringRef PropertyName = Property.getObjCPropertyName();
984 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Eric Christopher4d069bf2012-05-22 18:45:24 +0000985 addType(ElemDie, Property.getType());
986 addSourceLine(ElemDie, Property);
Devang Patel30d409c2012-02-07 23:33:58 +0000987 StringRef GetterName = Property.getObjCPropertyGetterName();
988 if (!GetterName.empty())
989 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
990 StringRef SetterName = Property.getObjCPropertySetterName();
991 if (!SetterName.empty())
992 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
993 unsigned PropertyAttributes = 0;
Devang Patel9e11eb12012-02-04 01:30:32 +0000994 if (Property.isReadOnlyObjCProperty())
995 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
996 if (Property.isReadWriteObjCProperty())
997 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
998 if (Property.isAssignObjCProperty())
999 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1000 if (Property.isRetainObjCProperty())
1001 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1002 if (Property.isCopyObjCProperty())
1003 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1004 if (Property.isNonAtomicObjCProperty())
1005 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1006 if (PropertyAttributes)
Eric Christopher8b4310b2012-11-21 00:34:38 +00001007 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, 0,
Devang Patel9e11eb12012-02-04 01:30:32 +00001008 PropertyAttributes);
Devang Patel6588abf2012-02-06 17:49:43 +00001009
Devang Patel30d409c2012-02-07 23:33:58 +00001010 DIEEntry *Entry = getDIEEntry(Element);
1011 if (!Entry) {
1012 Entry = createDIEEntry(ElemDie);
1013 insertDIEEntry(Element, Entry);
1014 }
Devang Patel9e11eb12012-02-04 01:30:32 +00001015 } else
Devang Patel161b2f42011-04-12 23:21:44 +00001016 continue;
1017 Buffer.addChild(ElemDie);
1018 }
1019
1020 if (CTy.isAppleBlockExtension())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001021 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel161b2f42011-04-12 23:21:44 +00001022
Devang Patel161b2f42011-04-12 23:21:44 +00001023 DICompositeType ContainingType = CTy.getContainingType();
1024 if (DIDescriptor(ContainingType).isCompositeType())
1025 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
1026 getOrCreateTypeDIE(DIType(ContainingType)));
1027 else {
1028 DIDescriptor Context = CTy.getContext();
1029 addToContextOwner(&Buffer, Context);
1030 }
1031
Devang Patel201e6cd2011-05-12 21:29:42 +00001032 if (CTy.isObjcClassComplete())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001033 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patelb11f80e2011-05-12 19:06:16 +00001034
Eric Christopher1a8e8862011-12-16 23:42:42 +00001035 // Add template parameters to a class, structure or union types.
1036 // FIXME: The support isn't in the metadata for this yet.
1037 if (Tag == dwarf::DW_TAG_class_type ||
1038 Tag == dwarf::DW_TAG_structure_type ||
1039 Tag == dwarf::DW_TAG_union_type)
Devang Patel161b2f42011-04-12 23:21:44 +00001040 addTemplateParams(Buffer, CTy.getTemplateParams());
1041
1042 break;
1043 }
1044 default:
1045 break;
1046 }
1047
1048 // Add name if not anonymous or intermediate type.
1049 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001050 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001051
Eric Christopher4a5d8392012-05-22 18:45:18 +00001052 if (Tag == dwarf::DW_TAG_enumeration_type ||
1053 Tag == dwarf::DW_TAG_class_type ||
1054 Tag == dwarf::DW_TAG_structure_type ||
1055 Tag == dwarf::DW_TAG_union_type) {
Devang Patel161b2f42011-04-12 23:21:44 +00001056 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopherfc4199b2012-06-01 00:22:32 +00001057 // TODO: Do we care about size for enum forward declarations?
Devang Patel161b2f42011-04-12 23:21:44 +00001058 if (Size)
1059 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Eric Christopherfc4199b2012-06-01 00:22:32 +00001060 else if (!CTy.isForwardDecl())
Devang Patel161b2f42011-04-12 23:21:44 +00001061 // Add zero size if it is not a forward declaration.
Eric Christopherfc4199b2012-06-01 00:22:32 +00001062 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
1063
1064 // If we're a forward decl, say so.
1065 if (CTy.isForwardDecl())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001066 addFlag(&Buffer, dwarf::DW_AT_declaration);
Devang Patel161b2f42011-04-12 23:21:44 +00001067
1068 // Add source line info if available.
1069 if (!CTy.isForwardDecl())
1070 addSourceLine(&Buffer, CTy);
Eric Christopher89388952012-03-07 00:15:19 +00001071
1072 // No harm in adding the runtime language to the declaration.
1073 unsigned RLang = CTy.getRunTimeLang();
1074 if (RLang)
1075 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
1076 dwarf::DW_FORM_data1, RLang);
Devang Patel161b2f42011-04-12 23:21:44 +00001077 }
1078}
1079
Eric Christopher8b4310b2012-11-21 00:34:38 +00001080/// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
Devang Patel161b2f42011-04-12 23:21:44 +00001081/// for the given DITemplateTypeParameter.
1082DIE *
1083CompileUnit::getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP) {
1084 DIE *ParamDIE = getDIE(TP);
1085 if (ParamDIE)
1086 return ParamDIE;
1087
1088 ParamDIE = new DIE(dwarf::DW_TAG_template_type_parameter);
1089 addType(ParamDIE, TP.getType());
Nick Lewycky390c40d2011-10-27 06:44:11 +00001090 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel161b2f42011-04-12 23:21:44 +00001091 return ParamDIE;
1092}
1093
Eric Christopher8b4310b2012-11-21 00:34:38 +00001094/// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE
Devang Patel161b2f42011-04-12 23:21:44 +00001095/// for the given DITemplateValueParameter.
1096DIE *
Eric Christopher4d069bf2012-05-22 18:45:24 +00001097CompileUnit::getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TPV){
Devang Patel161b2f42011-04-12 23:21:44 +00001098 DIE *ParamDIE = getDIE(TPV);
1099 if (ParamDIE)
1100 return ParamDIE;
1101
1102 ParamDIE = new DIE(dwarf::DW_TAG_template_value_parameter);
1103 addType(ParamDIE, TPV.getType());
1104 if (!TPV.getName().empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001105 addString(ParamDIE, dwarf::DW_AT_name, TPV.getName());
David Blaikie4de9d722013-05-10 21:52:07 +00001106 if (Value *Val = TPV.getValue()) {
1107 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val))
1108 addConstantValue(ParamDIE, CI, TPV.getType().isUnsignedDIType());
1109 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) {
1110 // For declaration non-type template parameters (such as global values and
1111 // functions)
1112 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1113 addOpAddress(Block, Asm->Mang->getSymbol(GV));
1114 // Emit DW_OP_stack_value to use the address as the immediate value of the
1115 // parameter, rather than a pointer to it.
1116 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1117 addBlock(ParamDIE, dwarf::DW_AT_location, 0, Block);
1118 }
1119 }
1120
Devang Patel161b2f42011-04-12 23:21:44 +00001121 return ParamDIE;
1122}
1123
Devang Patel31c5d052011-05-06 16:57:54 +00001124/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1125DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) {
1126 DIE *NDie = getDIE(NS);
1127 if (NDie)
1128 return NDie;
1129 NDie = new DIE(dwarf::DW_TAG_namespace);
1130 insertDIE(NS, NDie);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001131 if (!NS.getName().empty()) {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001132 addString(NDie, dwarf::DW_AT_name, NS.getName());
Eric Christopher09ac3d82011-11-07 09:24:32 +00001133 addAccelNamespace(NS.getName(), NDie);
1134 } else
1135 addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel31c5d052011-05-06 16:57:54 +00001136 addSourceLine(NDie, NS);
1137 addToContextOwner(NDie, NS.getContext());
1138 return NDie;
1139}
1140
Devang Pateldbc64af2011-08-15 17:24:54 +00001141/// getOrCreateSubprogramDIE - Create new DIE using SP.
1142DIE *CompileUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
1143 DIE *SPDie = getDIE(SP);
1144 if (SPDie)
1145 return SPDie;
1146
Peter Collingbourne27302f02012-05-27 18:36:44 +00001147 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1148
1149 // DW_TAG_inlined_subroutine may refer to this DIE.
1150 insertDIE(SP, SPDie);
1151
Rafael Espindola01b55b42011-11-10 22:34:29 +00001152 DISubprogram SPDecl = SP.getFunctionDeclaration();
1153 DIE *DeclDie = NULL;
1154 if (SPDecl.isSubprogram()) {
1155 DeclDie = getOrCreateSubprogramDIE(SPDecl);
1156 }
1157
Devang Pateldbc64af2011-08-15 17:24:54 +00001158 // Add to context owner.
1159 addToContextOwner(SPDie, SP.getContext());
1160
1161 // Add function template parameters.
1162 addTemplateParams(*SPDie, SP.getTemplateParams());
1163
Eric Christopherc4968752013-05-09 00:42:33 +00001164 // Unfortunately this code needs to stay here instead of below the
1165 // AT_specification code in order to work around a bug in older
1166 // gdbs that requires the linkage name to resolve multiple template
1167 // functions.
1168 // TODO: Remove this set of code when we get rid of the old gdb
1169 // compatibility.
Eric Christopher8d101c32012-03-15 08:19:33 +00001170 StringRef LinkageName = SP.getLinkageName();
Eric Christopherc4968752013-05-09 00:42:33 +00001171 if (!LinkageName.empty() && DD->useDarwinGDBCompat())
1172 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c2b4be2013-06-01 17:51:14 +00001173 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopher8d101c32012-03-15 08:19:33 +00001174
Devang Pateldbc64af2011-08-15 17:24:54 +00001175 // If this DIE is going to refer declaration info using AT_specification
Eric Christopherc4968752013-05-09 00:42:33 +00001176 // then there is no need to add other attributes.
Rafael Espindola01b55b42011-11-10 22:34:29 +00001177 if (DeclDie) {
1178 // Refer function declaration directly.
1179 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1180 DeclDie);
1181
Devang Pateldbc64af2011-08-15 17:24:54 +00001182 return SPDie;
Rafael Espindola01b55b42011-11-10 22:34:29 +00001183 }
Devang Pateldbc64af2011-08-15 17:24:54 +00001184
Eric Christophercbbd5b12012-08-23 22:52:55 +00001185 // Add the linkage name if we have one.
Eric Christopherc4968752013-05-09 00:42:33 +00001186 if (!LinkageName.empty() && !DD->useDarwinGDBCompat())
Eric Christophercbbd5b12012-08-23 22:52:55 +00001187 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c2b4be2013-06-01 17:51:14 +00001188 GlobalValue::getRealLinkageName(LinkageName));
Eric Christophercbbd5b12012-08-23 22:52:55 +00001189
Devang Pateldbc64af2011-08-15 17:24:54 +00001190 // Constructors and operators for anonymous aggregates do not have names.
1191 if (!SP.getName().empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001192 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Pateldbc64af2011-08-15 17:24:54 +00001193
1194 addSourceLine(SPDie, SP);
1195
Eric Christopher8b6fe6b2012-02-22 08:46:21 +00001196 // Add the prototype if we have a prototype and we have a C like
1197 // language.
1198 if (SP.isPrototyped() &&
1199 (Language == dwarf::DW_LANG_C89 ||
1200 Language == dwarf::DW_LANG_C99 ||
1201 Language == dwarf::DW_LANG_ObjC))
Eric Christopher873cf0a2012-08-24 01:14:27 +00001202 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Pateldbc64af2011-08-15 17:24:54 +00001203
1204 // Add Return Type.
1205 DICompositeType SPTy = SP.getType();
David Blaikie3d331842013-05-22 23:22:18 +00001206 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type &&
1207 "the type of a subprogram should be a subroutine");
Devang Pateldbc64af2011-08-15 17:24:54 +00001208
David Blaikie3d331842013-05-22 23:22:18 +00001209 DIArray Args = SPTy.getTypeArray();
1210 addType(SPDie, DIType(Args.getElement(0)));
Devang Pateldbc64af2011-08-15 17:24:54 +00001211
1212 unsigned VK = SP.getVirtuality();
1213 if (VK) {
Nick Lewycky798313d2011-12-14 00:56:07 +00001214 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Devang Pateldbc64af2011-08-15 17:24:54 +00001215 DIEBlock *Block = getDIEBlock();
1216 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1217 addUInt(Block, 0, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1218 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
1219 ContainingTypeMap.insert(std::make_pair(SPDie,
1220 SP.getContainingType()));
1221 }
1222
1223 if (!SP.isDefinition()) {
Eric Christopher873cf0a2012-08-24 01:14:27 +00001224 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher8b4310b2012-11-21 00:34:38 +00001225
Devang Pateldbc64af2011-08-15 17:24:54 +00001226 // Add arguments. Do not add arguments for subprogram definition. They will
1227 // be handled while processing variables.
David Blaikie3d331842013-05-22 23:22:18 +00001228 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1229 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1230 DIType ATy = DIType(Args.getElement(i));
1231 addType(Arg, ATy);
1232 if (ATy.isArtificial())
1233 addFlag(Arg, dwarf::DW_AT_artificial);
1234 SPDie->addChild(Arg);
1235 }
Devang Pateldbc64af2011-08-15 17:24:54 +00001236 }
1237
1238 if (SP.isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001239 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Pateldbc64af2011-08-15 17:24:54 +00001240
1241 if (!SP.isLocalToUnit())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001242 addFlag(SPDie, dwarf::DW_AT_external);
Devang Pateldbc64af2011-08-15 17:24:54 +00001243
1244 if (SP.isOptimized())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001245 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Pateldbc64af2011-08-15 17:24:54 +00001246
1247 if (unsigned isa = Asm->getISAEncoding()) {
1248 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1249 }
1250
1251 return SPDie;
1252}
1253
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001254// Return const expression if value is a GEP to access merged global
1255// constant. e.g.
1256// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1257static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1258 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1259 if (!CE || CE->getNumOperands() != 3 ||
1260 CE->getOpcode() != Instruction::GetElementPtr)
1261 return NULL;
1262
1263 // First operand points to a global struct.
1264 Value *Ptr = CE->getOperand(0);
1265 if (!isa<GlobalValue>(Ptr) ||
1266 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1267 return NULL;
1268
1269 // Second operand is zero.
1270 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1271 if (!CI || !CI->isZero())
1272 return NULL;
1273
1274 // Third operand is offset.
1275 if (!isa<ConstantInt>(CE->getOperand(2)))
1276 return NULL;
1277
1278 return CE;
1279}
1280
1281/// createGlobalVariableDIE - create global variable DIE.
1282void CompileUnit::createGlobalVariableDIE(const MDNode *N) {
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001283 // Check for pre-existence.
Devang Patel49e2f032011-08-18 22:21:50 +00001284 if (getDIE(N))
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001285 return;
1286
Devang Patel49e2f032011-08-18 22:21:50 +00001287 DIGlobalVariable GV(N);
Devang Patel28bea082011-08-18 23:17:55 +00001288 if (!GV.Verify())
1289 return;
1290
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001291 DIDescriptor GVContext = GV.getContext();
Eric Christopher6b6061f2013-01-16 01:22:23 +00001292 DIType GTy = GV.getType();
1293
1294 // If this is a static data member definition, some attributes belong
1295 // to the declaration DIE.
1296 DIE *VariableDIE = NULL;
Manman Ren945e8282013-02-01 23:54:37 +00001297 bool IsStaticMember = false;
Eric Christopher6b6061f2013-01-16 01:22:23 +00001298 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1299 if (SDMDecl.Verify()) {
1300 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1301 // We need the declaration DIE that is in the static member's class.
1302 // But that class might not exist in the DWARF yet.
1303 // Creating the class will create the static member decl DIE.
1304 getOrCreateContextDIE(SDMDecl.getContext());
1305 VariableDIE = getDIE(SDMDecl);
1306 assert(VariableDIE && "Static member decl has no context?");
Manman Ren945e8282013-02-01 23:54:37 +00001307 IsStaticMember = true;
Eric Christopher6b6061f2013-01-16 01:22:23 +00001308 }
1309
1310 // If this is not a static data member definition, create the variable
1311 // DIE and add the initial set of attributes to it.
1312 if (!VariableDIE) {
1313 VariableDIE = new DIE(GV.getTag());
1314 // Add to map.
1315 insertDIE(N, VariableDIE);
1316
1317 // Add name and type.
1318 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1319 addType(VariableDIE, GTy);
1320
1321 // Add scoping info.
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001322 if (!GV.isLocalToUnit()) {
Eric Christopher6b6061f2013-01-16 01:22:23 +00001323 addFlag(VariableDIE, dwarf::DW_AT_external);
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001324 addGlobalName(GV.getName(), VariableDIE);
1325 }
Eric Christopher6b6061f2013-01-16 01:22:23 +00001326
1327 // Add line number info.
1328 addSourceLine(VariableDIE, GV);
1329 // Add to context owner.
1330 addToContextOwner(VariableDIE, GVContext);
1331 }
1332
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001333 // Add location.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001334 bool addToAccelTable = false;
Eric Christopherd61c34b2011-11-11 03:16:32 +00001335 DIE *VariableSpecDIE = NULL;
Eric Christopher6b6061f2013-01-16 01:22:23 +00001336 bool isGlobalVariable = GV.getGlobal() != NULL;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001337 if (isGlobalVariable) {
Eric Christopher09ac3d82011-11-07 09:24:32 +00001338 addToAccelTable = true;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001339 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Eric Christopher0969ddf2013-01-18 22:11:33 +00001340 addOpAddress(Block, Asm->Mang->getSymbol(GV.getGlobal()));
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001341 // Do not create specification DIE if context is either compile unit
1342 // or a subprogram.
Devang Patel1dd4e562011-09-21 23:41:11 +00001343 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001344 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
1345 // Create specification DIE.
Eric Christopherd117fbb2011-11-11 01:55:22 +00001346 VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001347 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1348 dwarf::DW_FORM_ref4, VariableDIE);
1349 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopher6b6061f2013-01-16 01:22:23 +00001350 // A static member's declaration is already flagged as such.
1351 if (!SDMDecl.Verify())
1352 addFlag(VariableDIE, dwarf::DW_AT_declaration);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001353 addDie(VariableSpecDIE);
1354 } else {
1355 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001356 }
Eric Christopher6b6061f2013-01-16 01:22:23 +00001357 // Add linkage name.
1358 StringRef LinkageName = GV.getLinkageName();
Eric Christophera8ada252013-02-27 23:49:50 +00001359 if (!LinkageName.empty()) {
Eric Christopher8d45a982013-02-27 23:49:47 +00001360 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1361 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1362 // TAG_variable.
Manman Ren21a08a12013-02-27 23:21:02 +00001363 addString(IsStaticMember && VariableSpecDIE ?
1364 VariableSpecDIE : VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c2b4be2013-06-01 17:51:14 +00001365 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopher8d45a982013-02-27 23:49:47 +00001366 // In compatibility mode with older gdbs we put the linkage name on both
1367 // the TAG_variable DIE and on the TAG_member DIE.
Manman Ren21a08a12013-02-27 23:21:02 +00001368 if (IsStaticMember && VariableSpecDIE && DD->useDarwinGDBCompat())
1369 addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c2b4be2013-06-01 17:51:14 +00001370 GlobalValue::getRealLinkageName(LinkageName));
Manman Ren06df83c2013-02-27 00:02:32 +00001371 }
Eric Christopher8b4310b2012-11-21 00:34:38 +00001372 } else if (const ConstantInt *CI =
Manman Ren945e8282013-02-01 23:54:37 +00001373 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
Adrian Prantl2e892e42013-04-04 22:56:49 +00001374 // AT_const_value was added when the static member was created. To avoid
Manman Ren945e8282013-02-01 23:54:37 +00001375 // emitting AT_const_value multiple times, we only add AT_const_value when
1376 // it is not a static member.
1377 if (!IsStaticMember)
1378 addConstantValue(VariableDIE, CI, GTy.isUnsignedDIType());
1379 } else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) {
Eric Christopher09ac3d82011-11-07 09:24:32 +00001380 addToAccelTable = true;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001381 // GV is a merged global.
1382 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1383 Value *Ptr = CE->getOperand(0);
Eric Christopher0969ddf2013-01-18 22:11:33 +00001384 addOpAddress(Block, Asm->Mang->getSymbol(cast<GlobalValue>(Ptr)));
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001385 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1386 SmallVector<Value*, 3> Idx(CE->op_begin()+1, CE->op_end());
Eric Christopher8b4310b2012-11-21 00:34:38 +00001387 addUInt(Block, 0, dwarf::DW_FORM_udata,
Micah Villmow3574eca2012-10-08 16:38:25 +00001388 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001389 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1390 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1391 }
1392
Eric Christopherd117fbb2011-11-11 01:55:22 +00001393 if (addToAccelTable) {
1394 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1395 addAccelName(GV.getName(), AddrDIE);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001396
Eric Christopherd117fbb2011-11-11 01:55:22 +00001397 // If the linkage name is different than the name, go ahead and output
1398 // that as well into the name table.
1399 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1400 addAccelName(GV.getLinkageName(), AddrDIE);
1401 }
Eric Christopher74d8a872011-11-08 21:56:23 +00001402
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001403 return;
1404}
1405
Devang Patel161b2f42011-04-12 23:21:44 +00001406/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Eric Christopher4d069bf2012-05-22 18:45:24 +00001407void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR,
1408 DIE *IndexTy) {
Devang Patel161b2f42011-04-12 23:21:44 +00001409 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1410 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patel161b2f42011-04-12 23:21:44 +00001411
Bill Wendling222c2fd2012-12-06 07:38:10 +00001412 // The LowerBound value defines the lower bounds which is typically zero for
1413 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1414 // Count == -1 then the array is unbounded and we do not emit
1415 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1416 // Count == 0, then the array has zero elements in which case we do not emit
1417 // an upper bound.
1418 int64_t LowerBound = SR.getLo();
Bill Wendling6afe4782012-12-06 07:55:19 +00001419 int64_t DefaultLowerBound = getDefaultLowerBound();
Bill Wendling9493dae2012-12-04 21:34:03 +00001420 int64_t Count = SR.getCount();
Devang Patel161b2f42011-04-12 23:21:44 +00001421
Bill Wendling6afe4782012-12-06 07:55:19 +00001422 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
Bill Wendling222c2fd2012-12-06 07:38:10 +00001423 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, LowerBound);
1424
1425 if (Count != -1 && Count != 0)
Bill Wendling9493dae2012-12-04 21:34:03 +00001426 // FIXME: An unbounded array should reference the expression that defines
1427 // the array.
Bill Wendling222c2fd2012-12-06 07:38:10 +00001428 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, LowerBound + Count - 1);
Bill Wendling9493dae2012-12-04 21:34:03 +00001429
Devang Patel161b2f42011-04-12 23:21:44 +00001430 Buffer.addChild(DW_Subrange);
1431}
1432
1433/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
1434void CompileUnit::constructArrayTypeDIE(DIE &Buffer,
1435 DICompositeType *CTy) {
1436 Buffer.setTag(dwarf::DW_TAG_array_type);
Eric Christopher9a1e0e22013-01-08 01:53:52 +00001437 if (CTy->isVector())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001438 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel161b2f42011-04-12 23:21:44 +00001439
1440 // Emit derived type.
1441 addType(&Buffer, CTy->getTypeDerivedFrom());
1442 DIArray Elements = CTy->getTypeArray();
1443
1444 // Get an anonymous type for index type.
Eric Christopher8cab6ed2013-01-04 21:51:53 +00001445 // FIXME: This type should be passed down from the front end
1446 // as different languages may have different sizes for indexes.
Devang Patel161b2f42011-04-12 23:21:44 +00001447 DIE *IdxTy = getIndexTyDie();
1448 if (!IdxTy) {
1449 // Construct an anonymous type for index type.
1450 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Eric Christopher8cab6ed2013-01-04 21:51:53 +00001451 addString(IdxTy, dwarf::DW_AT_name, "int");
Devang Patel161b2f42011-04-12 23:21:44 +00001452 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1453 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1454 dwarf::DW_ATE_signed);
1455 addDie(IdxTy);
1456 setIndexTyDie(IdxTy);
1457 }
1458
1459 // Add subranges to array type.
1460 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1461 DIDescriptor Element = Elements.getElement(i);
1462 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1463 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1464 }
1465}
1466
1467/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
1468DIE *CompileUnit::constructEnumTypeDIE(DIEnumerator ETy) {
1469 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
1470 StringRef Name = ETy.getName();
Nick Lewycky390c40d2011-10-27 06:44:11 +00001471 addString(Enumerator, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001472 int64_t Value = ETy.getEnumValue();
1473 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
1474 return Enumerator;
1475}
1476
Devang Pateldbc64af2011-08-15 17:24:54 +00001477/// constructContainingTypeDIEs - Construct DIEs for types that contain
1478/// vtables.
1479void CompileUnit::constructContainingTypeDIEs() {
1480 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
1481 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1482 DIE *SPDie = CI->first;
1483 const MDNode *N = CI->second;
1484 if (!N) continue;
1485 DIE *NDie = getDIE(N);
1486 if (!NDie) continue;
1487 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1488 }
1489}
1490
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001491/// constructVariableDIE - Construct a DIE for the given DbgVariable.
1492DIE *CompileUnit::constructVariableDIE(DbgVariable *DV, bool isScopeAbstract) {
1493 StringRef Name = DV->getName();
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001494
1495 // Translate tag to proper Dwarf tag.
1496 unsigned Tag = DV->getTag();
1497
1498 // Define variable debug information entry.
1499 DIE *VariableDie = new DIE(Tag);
1500 DbgVariable *AbsVar = DV->getAbstractVariable();
1501 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
Manman Ren742671b2013-05-29 17:16:59 +00001502 if (AbsDIE)
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001503 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Manman Ren742671b2013-05-29 17:16:59 +00001504 dwarf::DW_FORM_ref4, AbsDIE);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001505 else {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001506 addString(VariableDie, dwarf::DW_AT_name, Name);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001507 addSourceLine(VariableDie, DV->getVariable());
1508 addType(VariableDie, DV->getType());
1509 }
1510
1511 if (DV->isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001512 addFlag(VariableDie, dwarf::DW_AT_artificial);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001513
1514 if (isScopeAbstract) {
1515 DV->setDIE(VariableDie);
1516 return VariableDie;
1517 }
1518
1519 // Add variable address.
1520
1521 unsigned Offset = DV->getDotDebugLocOffset();
1522 if (Offset != ~0U) {
1523 addLabel(VariableDie, dwarf::DW_AT_location,
1524 dwarf::DW_FORM_data4,
1525 Asm->GetTempSymbol("debug_loc", Offset));
1526 DV->setDIE(VariableDie);
1527 return VariableDie;
1528 }
1529
Eric Christopher8cf5e742011-10-03 15:49:20 +00001530 // Check if variable is described by a DBG_VALUE instruction.
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001531 if (const MachineInstr *DVInsn = DV->getMInsn()) {
1532 bool updated = false;
David Blaikie6d9dbd52013-06-16 20:34:15 +00001533 assert(DVInsn->getNumOperands() == 3);
1534 if (DVInsn->getOperand(0).isReg()) {
1535 const MachineOperand RegOp = DVInsn->getOperand(0);
1536 if (int64_t Offset = DVInsn->getOperand(1).getImm()) {
1537 MachineLocation Location(RegOp.getReg(), Offset);
1538 addVariableAddress(DV, VariableDie, Location);
1539 } else if (RegOp.getReg())
1540 addVariableAddress(DV, VariableDie, MachineLocation(RegOp.getReg()));
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001541 updated = true;
David Blaikie6d9dbd52013-06-16 20:34:15 +00001542 } else if (DVInsn->getOperand(0).isImm())
1543 updated =
1544 addConstantValue(VariableDie, DVInsn->getOperand(0), DV->getType());
1545 else if (DVInsn->getOperand(0).isFPImm())
1546 updated = addConstantFPValue(VariableDie, DVInsn->getOperand(0));
1547 else if (DVInsn->getOperand(0).isCImm())
1548 updated = addConstantValue(VariableDie, DVInsn->getOperand(0).getCImm(),
1549 DV->getType().isUnsignedDIType());
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001550 if (!updated) {
1551 // If variableDie is not updated then DBG_VALUE instruction does not
1552 // have valid variable info.
1553 delete VariableDie;
1554 return NULL;
1555 }
1556 DV->setDIE(VariableDie);
1557 return VariableDie;
1558 } else {
1559 // .. else use frame index.
1560 int FI = DV->getFrameIndex();
1561 if (FI != ~0) {
1562 unsigned FrameReg = 0;
1563 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopher8b4310b2012-11-21 00:34:38 +00001564 int Offset =
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001565 TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
1566 MachineLocation Location(FrameReg, Offset);
1567 addVariableAddress(DV, VariableDie, Location);
1568 }
1569 }
1570
1571 DV->setDIE(VariableDie);
1572 return VariableDie;
1573}
1574
Devang Patel161b2f42011-04-12 23:21:44 +00001575/// createMemberDIE - Create new member DIE.
1576DIE *CompileUnit::createMemberDIE(DIDerivedType DT) {
1577 DIE *MemberDie = new DIE(DT.getTag());
1578 StringRef Name = DT.getName();
1579 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001580 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001581
1582 addType(MemberDie, DT.getTypeDerivedFrom());
1583
1584 addSourceLine(MemberDie, DT);
1585
1586 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
1587 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1588
1589 uint64_t Size = DT.getSizeInBits();
1590 uint64_t FieldSize = DT.getOriginalTypeSize();
1591
1592 if (Size != FieldSize) {
1593 // Handle bitfield.
1594 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1595 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
1596
1597 uint64_t Offset = DT.getOffsetInBits();
1598 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1599 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1600 uint64_t FieldOffset = (HiMark - FieldSize);
1601 Offset -= FieldOffset;
1602
1603 // Maybe we need to work from the other end.
Micah Villmow3574eca2012-10-08 16:38:25 +00001604 if (Asm->getDataLayout().isLittleEndian())
Devang Patel161b2f42011-04-12 23:21:44 +00001605 Offset = FieldSize - (Offset + Size);
1606 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
1607
1608 // Here WD_AT_data_member_location points to the anonymous
1609 // field that includes this bit field.
1610 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
1611
1612 } else
1613 // This is not a bitfield.
1614 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
1615
1616 if (DT.getTag() == dwarf::DW_TAG_inheritance
1617 && DT.isVirtual()) {
1618
1619 // For C++, virtual base classes are not at fixed offset. Use following
1620 // expression to extract appropriate offset from vtable.
1621 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1622
1623 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
1624 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1625 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1626 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1627 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1628 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1629 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1630 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1631
1632 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1633 VBaseLocationDie);
1634 } else
1635 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
1636
1637 if (DT.isProtected())
Nick Lewycky13aaca52011-12-13 05:09:11 +00001638 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001639 dwarf::DW_ACCESS_protected);
1640 else if (DT.isPrivate())
Nick Lewycky13aaca52011-12-13 05:09:11 +00001641 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001642 dwarf::DW_ACCESS_private);
1643 // Otherwise C++ member and base classes are considered public.
Eric Christopher8b4310b2012-11-21 00:34:38 +00001644 else
Nick Lewycky13aaca52011-12-13 05:09:11 +00001645 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001646 dwarf::DW_ACCESS_public);
1647 if (DT.isVirtual())
Nick Lewycky798313d2011-12-14 00:56:07 +00001648 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001649 dwarf::DW_VIRTUALITY_virtual);
Devang Patele9db5e22011-04-16 00:11:51 +00001650
1651 // Objective-C properties.
Devang Patel6588abf2012-02-06 17:49:43 +00001652 if (MDNode *PNode = DT.getObjCProperty())
1653 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
Eric Christopher8b4310b2012-11-21 00:34:38 +00001654 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
Devang Patel6588abf2012-02-06 17:49:43 +00001655 PropertyDie);
1656
David Blaikie01bc2b32012-12-13 22:43:07 +00001657 if (DT.isArtificial())
1658 addFlag(MemberDie, dwarf::DW_AT_artificial);
1659
Devang Patel161b2f42011-04-12 23:21:44 +00001660 return MemberDie;
1661}
Eric Christopher6b6061f2013-01-16 01:22:23 +00001662
1663/// createStaticMemberDIE - Create new DIE for C++ static member.
1664DIE *CompileUnit::createStaticMemberDIE(const DIDerivedType DT) {
1665 if (!DT.Verify())
1666 return NULL;
1667
1668 DIE *StaticMemberDIE = new DIE(DT.getTag());
1669 DIType Ty = DT.getTypeDerivedFrom();
1670
1671 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1672 addType(StaticMemberDIE, Ty);
1673 addSourceLine(StaticMemberDIE, DT);
1674 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1675 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1676
1677 // FIXME: We could omit private if the parent is a class_type, and
1678 // public if the parent is something else.
1679 if (DT.isProtected())
1680 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1681 dwarf::DW_ACCESS_protected);
1682 else if (DT.isPrivate())
1683 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1684 dwarf::DW_ACCESS_private);
1685 else
1686 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1687 dwarf::DW_ACCESS_public);
1688
1689 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
1690 addConstantValue(StaticMemberDIE, CI, Ty.isUnsignedDIType());
David Blaikie14268412013-01-20 01:18:01 +00001691 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
1692 addConstantFPValue(StaticMemberDIE, CFP);
Eric Christopher6b6061f2013-01-16 01:22:23 +00001693
1694 insertDIE(DT, StaticMemberDIE);
1695 return StaticMemberDIE;
1696}