blob: 7f0c33bb05c57e9b764d8863a6fc62729ef0f927 [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,
36 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
351 addAddress(Die, dwarf::DW_AT_location, Location);
352}
353
Devang Patel116da2f2011-04-26 19:06:18 +0000354/// addRegisterOp - Add register operand.
355void CompileUnit::addRegisterOp(DIE *TheDie, unsigned Reg) {
356 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
357 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
358 if (DWReg < 32)
359 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
360 else {
361 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
362 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
363 }
364}
365
366/// addRegisterOffset - Add register offset.
367void CompileUnit::addRegisterOffset(DIE *TheDie, unsigned Reg,
368 int64_t Offset) {
369 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
370 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
371 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
372 if (Reg == TRI->getFrameRegister(*Asm->MF))
373 // If variable offset is based in frame register then use fbreg.
374 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
375 else if (DWReg < 32)
376 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
377 else {
378 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
379 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
380 }
381 addSInt(TheDie, 0, dwarf::DW_FORM_sdata, Offset);
382}
383
384/// addAddress - Add an address attribute to a die based on the location
385/// provided.
386void CompileUnit::addAddress(DIE *Die, unsigned Attribute,
387 const MachineLocation &Location) {
388 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
389
390 if (Location.isReg())
391 addRegisterOp(Block, Location.getReg());
392 else
393 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
394
395 // Now attach the location information to the DIE.
396 addBlock(Die, Attribute, 0, Block);
397}
398
Devang Patel161b2f42011-04-12 23:21:44 +0000399/// addComplexAddress - Start with the address based on the location provided,
400/// and generate the DWARF information necessary to find the actual variable
401/// given the extra address information encoded in the DIVariable, starting from
402/// the starting location. Add the DWARF information to the die.
403///
404void CompileUnit::addComplexAddress(DbgVariable *&DV, DIE *Die,
405 unsigned Attribute,
406 const MachineLocation &Location) {
Devang Patel161b2f42011-04-12 23:21:44 +0000407 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patelc26f5442011-04-28 02:22:40 +0000408 unsigned N = DV->getNumAddrElements();
409 unsigned i = 0;
410 if (Location.isReg()) {
411 if (N >= 2 && DV->getAddrElement(0) == DIBuilder::OpPlus) {
412 // If first address element is OpPlus then emit
413 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
414 addRegisterOffset(Block, Location.getReg(), DV->getAddrElement(1));
415 i = 2;
416 } else
417 addRegisterOp(Block, Location.getReg());
418 }
Devang Patel116da2f2011-04-26 19:06:18 +0000419 else
420 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel161b2f42011-04-12 23:21:44 +0000421
Devang Patelc26f5442011-04-28 02:22:40 +0000422 for (;i < N; ++i) {
Devang Patel161b2f42011-04-12 23:21:44 +0000423 uint64_t Element = DV->getAddrElement(i);
Devang Patel161b2f42011-04-12 23:21:44 +0000424 if (Element == DIBuilder::OpPlus) {
425 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
426 addUInt(Block, 0, dwarf::DW_FORM_udata, DV->getAddrElement(++i));
427 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher50120762012-05-08 18:56:00 +0000428 if (!Location.isReg())
429 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel161b2f42011-04-12 23:21:44 +0000430 } else llvm_unreachable("unknown DIBuilder Opcode");
431 }
432
433 // Now attach the location information to the DIE.
434 addBlock(Die, Attribute, 0, Block);
435}
436
437/* Byref variables, in Blocks, are declared by the programmer as "SomeType
438 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
439 gives the variable VarName either the struct, or a pointer to the struct, as
440 its type. This is necessary for various behind-the-scenes things the
441 compiler needs to do with by-reference variables in Blocks.
442
443 However, as far as the original *programmer* is concerned, the variable
444 should still have type 'SomeType', as originally declared.
445
446 The function getBlockByrefType dives into the __Block_byref_x_VarName
447 struct to find the original type of the variable, which is then assigned to
448 the variable's Debug Information Entry as its real type. So far, so good.
449 However now the debugger will expect the variable VarName to have the type
450 SomeType. So we need the location attribute for the variable to be an
451 expression that explains to the debugger how to navigate through the
452 pointers and struct to find the actual variable of type SomeType.
453
454 The following function does just that. We start by getting
455 the "normal" location for the variable. This will be the location
456 of either the struct __Block_byref_x_VarName or the pointer to the
457 struct __Block_byref_x_VarName.
458
459 The struct will look something like:
460
461 struct __Block_byref_x_VarName {
462 ... <various fields>
463 struct __Block_byref_x_VarName *forwarding;
464 ... <various other fields>
465 SomeType VarName;
466 ... <maybe more fields>
467 };
468
469 If we are given the struct directly (as our starting point) we
470 need to tell the debugger to:
471
472 1). Add the offset of the forwarding field.
473
474 2). Follow that pointer to get the real __Block_byref_x_VarName
475 struct to use (the real one may have been copied onto the heap).
476
477 3). Add the offset for the field VarName, to find the actual variable.
478
479 If we started with a pointer to the struct, then we need to
480 dereference that pointer first, before the other steps.
481 Translating this into DWARF ops, we will need to append the following
482 to the current location description for the variable:
483
484 DW_OP_deref -- optional, if we start with a pointer
485 DW_OP_plus_uconst <forward_fld_offset>
486 DW_OP_deref
487 DW_OP_plus_uconst <varName_fld_offset>
488
489 That is what this function does. */
490
491/// addBlockByrefAddress - Start with the address based on the location
492/// provided, and generate the DWARF information necessary to find the
493/// actual Block variable (navigating the Block struct) based on the
494/// starting location. Add the DWARF information to the die. For
495/// more information, read large comment just above here.
496///
497void CompileUnit::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
498 unsigned Attribute,
499 const MachineLocation &Location) {
500 DIType Ty = DV->getType();
501 DIType TmpTy = Ty;
502 unsigned Tag = Ty.getTag();
503 bool isPointer = false;
504
505 StringRef varName = DV->getName();
506
507 if (Tag == dwarf::DW_TAG_pointer_type) {
508 DIDerivedType DTy = DIDerivedType(Ty);
509 TmpTy = DTy.getTypeDerivedFrom();
510 isPointer = true;
511 }
512
513 DICompositeType blockStruct = DICompositeType(TmpTy);
514
515 // Find the __forwarding field and the variable field in the __Block_byref
516 // struct.
517 DIArray Fields = blockStruct.getTypeArray();
518 DIDescriptor varField = DIDescriptor();
519 DIDescriptor forwardingField = DIDescriptor();
520
521 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
522 DIDescriptor Element = Fields.getElement(i);
523 DIDerivedType DT = DIDerivedType(Element);
524 StringRef fieldName = DT.getName();
525 if (fieldName == "__forwarding")
526 forwardingField = Element;
527 else if (fieldName == varName)
528 varField = Element;
529 }
530
531 // Get the offsets for the forwarding field and the variable field.
532 unsigned forwardingFieldOffset =
533 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
534 unsigned varFieldOffset =
535 DIDerivedType(varField).getOffsetInBits() >> 3;
536
537 // Decode the original location, and use that as the start of the byref
538 // variable's location.
Devang Patel161b2f42011-04-12 23:21:44 +0000539 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
540
Eric Christophercaba2632012-07-04 02:02:18 +0000541 if (Location.isReg())
542 addRegisterOp(Block, Location.getReg());
543 else
544 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel161b2f42011-04-12 23:21:44 +0000545
546 // If we started with a pointer to the __Block_byref... struct, then
547 // the first thing we need to do is dereference the pointer (DW_OP_deref).
548 if (isPointer)
549 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
550
551 // Next add the offset for the '__forwarding' field:
552 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
553 // adding the offset if it's 0.
554 if (forwardingFieldOffset > 0) {
555 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
556 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
557 }
558
559 // Now dereference the __forwarding field to get to the real __Block_byref
560 // struct: DW_OP_deref.
561 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
562
563 // Now that we've got the real __Block_byref... struct, add the offset
564 // for the variable's field to get to the location of the actual variable:
565 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
566 if (varFieldOffset > 0) {
567 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
568 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
569 }
570
571 // Now attach the location information to the DIE.
572 addBlock(Die, Attribute, 0, Block);
573}
574
Devang Patel4ec14b02011-07-20 21:57:04 +0000575/// isTypeSigned - Return true if the type is signed.
576static bool isTypeSigned(DIType Ty, int *SizeInBits) {
577 if (Ty.isDerivedType())
578 return isTypeSigned(DIDerivedType(Ty).getTypeDerivedFrom(), SizeInBits);
579 if (Ty.isBasicType())
580 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed
581 || DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
582 *SizeInBits = Ty.getSizeInBits();
583 return true;
584 }
585 return false;
586}
587
Devang Patel161b2f42011-04-12 23:21:44 +0000588/// addConstantValue - Add constant value entry in variable DIE.
Devang Patelb58128e2011-05-27 16:45:18 +0000589bool CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
590 DIType Ty) {
David Blaikie4de9d722013-05-10 21:52:07 +0000591 // FIXME: This is a bit conservative/simple - it emits negative values at
592 // their maximum bit width which is a bit unfortunate (& doesn't prefer
593 // udata/sdata over dataN as suggested by the DWARF spec)
Nick Lewycky746cb672011-10-26 22:55:33 +0000594 assert(MO.isImm() && "Invalid machine operand!");
Devang Patel161b2f42011-04-12 23:21:44 +0000595 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel4ec14b02011-07-20 21:57:04 +0000596 int SizeInBits = -1;
597 bool SignedConstant = isTypeSigned(Ty, &SizeInBits);
598 unsigned Form = SignedConstant ? dwarf::DW_FORM_sdata : dwarf::DW_FORM_udata;
599 switch (SizeInBits) {
600 case 8: Form = dwarf::DW_FORM_data1; break;
601 case 16: Form = dwarf::DW_FORM_data2; break;
602 case 32: Form = dwarf::DW_FORM_data4; break;
603 case 64: Form = dwarf::DW_FORM_data8; break;
Devang Patel045c1d42011-05-27 19:13:26 +0000604 default: break;
605 }
Eric Christopher8b4310b2012-11-21 00:34:38 +0000606 SignedConstant ? addSInt(Block, 0, Form, MO.getImm())
Devang Patel4ec14b02011-07-20 21:57:04 +0000607 : addUInt(Block, 0, Form, MO.getImm());
Devang Patel72f0d9c2011-05-27 18:15:52 +0000608
Devang Patel161b2f42011-04-12 23:21:44 +0000609 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
610 return true;
611}
612
613/// addConstantFPValue - Add constant value entry in variable DIE.
614bool CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Nick Lewycky390c40d2011-10-27 06:44:11 +0000615 assert (MO.isFPImm() && "Invalid machine operand!");
Devang Patel161b2f42011-04-12 23:21:44 +0000616 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
617 APFloat FPImm = MO.getFPImm()->getValueAPF();
618
619 // Get the raw data form of the floating point.
620 const APInt FltVal = FPImm.bitcastToAPInt();
621 const char *FltPtr = (const char*)FltVal.getRawData();
622
623 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmow3574eca2012-10-08 16:38:25 +0000624 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel161b2f42011-04-12 23:21:44 +0000625 int Incr = (LittleEndian ? 1 : -1);
626 int Start = (LittleEndian ? 0 : NumBytes - 1);
627 int Stop = (LittleEndian ? NumBytes : -1);
628
629 // Output the constant to DWARF one byte at a time.
630 for (; Start != Stop; Start += Incr)
631 addUInt(Block, 0, dwarf::DW_FORM_data1,
632 (unsigned char)0xFF & FltPtr[Start]);
633
634 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
635 return true;
636}
637
David Blaikie14268412013-01-20 01:18:01 +0000638/// addConstantFPValue - Add constant value entry in variable DIE.
639bool CompileUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) {
640 return addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), false);
641}
642
Devang Patel161b2f42011-04-12 23:21:44 +0000643/// addConstantValue - Add constant value entry in variable DIE.
Devang Patel8594d422011-06-24 20:46:11 +0000644bool CompileUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
Devang Patel161b2f42011-04-12 23:21:44 +0000645 bool Unsigned) {
David Blaikie14268412013-01-20 01:18:01 +0000646 return addConstantValue(Die, CI->getValue(), Unsigned);
647}
648
649// addConstantValue - Add constant value entry in variable DIE.
650bool CompileUnit::addConstantValue(DIE *Die, const APInt &Val,
651 bool Unsigned) {
652 unsigned CIBitWidth = Val.getBitWidth();
Devang Pateld6a81362011-05-28 00:39:18 +0000653 if (CIBitWidth <= 64) {
654 unsigned form = 0;
655 switch (CIBitWidth) {
656 case 8: form = dwarf::DW_FORM_data1; break;
657 case 16: form = dwarf::DW_FORM_data2; break;
658 case 32: form = dwarf::DW_FORM_data4; break;
659 case 64: form = dwarf::DW_FORM_data8; break;
Eric Christopher8b4310b2012-11-21 00:34:38 +0000660 default:
Devang Pateld6a81362011-05-28 00:39:18 +0000661 form = Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata;
662 }
Devang Patel161b2f42011-04-12 23:21:44 +0000663 if (Unsigned)
David Blaikie14268412013-01-20 01:18:01 +0000664 addUInt(Die, dwarf::DW_AT_const_value, form, Val.getZExtValue());
Devang Patel161b2f42011-04-12 23:21:44 +0000665 else
David Blaikie14268412013-01-20 01:18:01 +0000666 addSInt(Die, dwarf::DW_AT_const_value, form, Val.getSExtValue());
Devang Patel161b2f42011-04-12 23:21:44 +0000667 return true;
668 }
669
670 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
671
672 // Get the raw data form of the large APInt.
NAKAMURA Takumic3e48c32011-10-28 14:12:22 +0000673 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel161b2f42011-04-12 23:21:44 +0000674
675 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmow3574eca2012-10-08 16:38:25 +0000676 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel161b2f42011-04-12 23:21:44 +0000677
678 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumic3e48c32011-10-28 14:12:22 +0000679 for (int i = 0; i < NumBytes; i++) {
680 uint8_t c;
681 if (LittleEndian)
682 c = Ptr64[i / 8] >> (8 * (i & 7));
683 else
684 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
685 addUInt(Block, 0, dwarf::DW_FORM_data1, c);
686 }
Devang Patel161b2f42011-04-12 23:21:44 +0000687
688 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
689 return true;
690}
691
Eric Christopher6c3bb942013-04-22 07:47:40 +0000692/// addTemplateParams - Add template parameters into buffer.
Devang Patel161b2f42011-04-12 23:21:44 +0000693void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
694 // Add template parameters.
695 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
696 DIDescriptor Element = TParams.getElement(i);
697 if (Element.isTemplateTypeParameter())
698 Buffer.addChild(getOrCreateTemplateTypeParameterDIE(
699 DITemplateTypeParameter(Element)));
700 else if (Element.isTemplateValueParameter())
701 Buffer.addChild(getOrCreateTemplateValueParameterDIE(
702 DITemplateValueParameter(Element)));
703 }
Devang Patel161b2f42011-04-12 23:21:44 +0000704}
Nick Lewycky746cb672011-10-26 22:55:33 +0000705
Eric Christopher6b6061f2013-01-16 01:22:23 +0000706/// getOrCreateContextDIE - Get context owner's DIE.
707DIE *CompileUnit::getOrCreateContextDIE(DIDescriptor Context) {
708 if (Context.isType())
709 return getOrCreateTypeDIE(DIType(Context));
710 else if (Context.isNameSpace())
711 return getOrCreateNameSpace(DINameSpace(Context));
712 else if (Context.isSubprogram())
713 return getOrCreateSubprogramDIE(DISubprogram(Context));
Eric Christopher6c3bb942013-04-22 07:47:40 +0000714 else
Eric Christopher6b6061f2013-01-16 01:22:23 +0000715 return getDIE(Context);
716}
717
Devang Patel161b2f42011-04-12 23:21:44 +0000718/// addToContextOwner - Add Die into the list of its context owner's children.
719void CompileUnit::addToContextOwner(DIE *Die, DIDescriptor Context) {
Eric Christopher6b6061f2013-01-16 01:22:23 +0000720 if (DIE *ContextDIE = getOrCreateContextDIE(Context))
Devang Patel161b2f42011-04-12 23:21:44 +0000721 ContextDIE->addChild(Die);
722 else
723 addDie(Die);
724}
725
726/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
727/// given DIType.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000728DIE *CompileUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
729 DIType Ty(TyNode);
730 if (!Ty.Verify())
731 return NULL;
Devang Patel161b2f42011-04-12 23:21:44 +0000732 DIE *TyDIE = getDIE(Ty);
733 if (TyDIE)
734 return TyDIE;
735
736 // Create new type.
737 TyDIE = new DIE(dwarf::DW_TAG_base_type);
738 insertDIE(Ty, TyDIE);
739 if (Ty.isBasicType())
740 constructTypeDIE(*TyDIE, DIBasicType(Ty));
741 else if (Ty.isCompositeType())
742 constructTypeDIE(*TyDIE, DICompositeType(Ty));
743 else {
744 assert(Ty.isDerivedType() && "Unknown kind of DIType");
745 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
746 }
Eric Christopher1b3f9192011-11-10 19:52:58 +0000747 // If this is a named finished type then include it in the list of types
748 // for the accelerator tables.
Eric Christopherc36145f2012-01-06 04:35:23 +0000749 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
750 bool IsImplementation = 0;
751 if (Ty.isCompositeType()) {
752 DICompositeType CT(Ty);
Eric Christophere0167892012-01-06 23:03:37 +0000753 // A runtime language of 0 actually means C/C++ and that any
754 // non-negative value is some version of Objective-C/C++.
Eric Christopherc36145f2012-01-06 04:35:23 +0000755 IsImplementation = (CT.getRunTimeLang() == 0) ||
Eric Christophere2dc9332012-02-22 08:46:02 +0000756 CT.isObjcClassComplete();
Eric Christopherc36145f2012-01-06 04:35:23 +0000757 }
Eric Christophere0167892012-01-06 23:03:37 +0000758 unsigned Flags = IsImplementation ?
759 DwarfAccelTable::eTypeFlagClassIsImplementation : 0;
760 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
Eric Christopherc36145f2012-01-06 04:35:23 +0000761 }
Eric Christopher8b4310b2012-11-21 00:34:38 +0000762
Devang Patel161b2f42011-04-12 23:21:44 +0000763 addToContextOwner(TyDIE, Ty.getContext());
764 return TyDIE;
765}
766
767/// addType - Add a new type attribute to the specified entity.
Eric Christopher4d069bf2012-05-22 18:45:24 +0000768void CompileUnit::addType(DIE *Entity, DIType Ty, unsigned Attribute) {
Devang Patel161b2f42011-04-12 23:21:44 +0000769 if (!Ty.Verify())
770 return;
771
772 // Check for pre-existence.
773 DIEEntry *Entry = getDIEEntry(Ty);
774 // If it exists then use the existing value.
775 if (Entry) {
Eric Christopher663e0cf2012-03-28 07:34:31 +0000776 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
Devang Patel161b2f42011-04-12 23:21:44 +0000777 return;
778 }
779
780 // Construct type.
781 DIE *Buffer = getOrCreateTypeDIE(Ty);
782
783 // Set up proxy.
784 Entry = createDIEEntry(Buffer);
785 insertDIEEntry(Ty, Entry);
Eric Christopher663e0cf2012-03-28 07:34:31 +0000786 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
Devang Patele9ae06c2011-05-31 22:56:51 +0000787
788 // If this is a complete composite type then include it in the
789 // list of global types.
Devang Patelc20bdf12011-06-01 00:23:24 +0000790 addGlobalType(Ty);
Devang Patel66658e42011-05-31 23:30:30 +0000791}
792
793/// addGlobalType - Add a new global type to the compile unit.
794///
Devang Patelc20bdf12011-06-01 00:23:24 +0000795void CompileUnit::addGlobalType(DIType Ty) {
Devang Patele9ae06c2011-05-31 22:56:51 +0000796 DIDescriptor Context = Ty.getContext();
Eric Christopher8b4310b2012-11-21 00:34:38 +0000797 if (Ty.isCompositeType() && !Ty.getName().empty() && !Ty.isForwardDecl()
798 && (!Context || Context.isCompileUnit() || Context.isFile()
Devang Patel94c7ddb2011-08-16 22:09:43 +0000799 || Context.isNameSpace()))
Devang Patelc20bdf12011-06-01 00:23:24 +0000800 if (DIEEntry *Entry = getDIEEntry(Ty))
801 GlobalTypes[Ty.getName()] = Entry->getEntry();
Devang Patel161b2f42011-04-12 23:21:44 +0000802}
803
Devang Patel31c5d052011-05-06 16:57:54 +0000804/// addPubTypes - Add type for pubtypes section.
805void CompileUnit::addPubTypes(DISubprogram SP) {
806 DICompositeType SPTy = SP.getType();
807 unsigned SPTag = SPTy.getTag();
808 if (SPTag != dwarf::DW_TAG_subroutine_type)
809 return;
810
811 DIArray Args = SPTy.getTypeArray();
812 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
813 DIType ATy(Args.getElement(i));
814 if (!ATy.Verify())
815 continue;
Devang Patelc20bdf12011-06-01 00:23:24 +0000816 addGlobalType(ATy);
Devang Patel31c5d052011-05-06 16:57:54 +0000817 }
818}
819
Devang Patel161b2f42011-04-12 23:21:44 +0000820/// constructTypeDIE - Construct basic type die from DIBasicType.
821void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
822 // Get core information.
823 StringRef Name = BTy.getName();
Devang Patel161b2f42011-04-12 23:21:44 +0000824 // Add name if not anonymous or intermediate type.
825 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000826 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel734a67c2011-09-14 23:13:28 +0000827
828 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type) {
829 Buffer.setTag(dwarf::DW_TAG_unspecified_type);
830 // Unspecified types has only name, nothing else.
831 return;
832 }
833
834 Buffer.setTag(dwarf::DW_TAG_base_type);
Nick Lewycky746cb672011-10-26 22:55:33 +0000835 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel30d409c2012-02-07 23:33:58 +0000836 BTy.getEncoding());
Devang Patel734a67c2011-09-14 23:13:28 +0000837
Devang Patel161b2f42011-04-12 23:21:44 +0000838 uint64_t Size = BTy.getSizeInBits() >> 3;
839 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
840}
841
842/// constructTypeDIE - Construct derived type die from DIDerivedType.
843void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
844 // Get core information.
845 StringRef Name = DTy.getName();
846 uint64_t Size = DTy.getSizeInBits() >> 3;
847 unsigned Tag = DTy.getTag();
848
849 // FIXME - Workaround for templates.
850 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
851
852 Buffer.setTag(Tag);
853
854 // Map to main type, void will not have a type.
855 DIType FromTy = DTy.getTypeDerivedFrom();
856 addType(&Buffer, FromTy);
857
858 // Add name if not anonymous or intermediate type.
859 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000860 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +0000861
862 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher35f225a2012-02-21 22:25:53 +0000863 if (Size && Tag != dwarf::DW_TAG_pointer_type)
Devang Patel161b2f42011-04-12 23:21:44 +0000864 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
865
David Blaikie62fdfb52013-01-07 05:51:15 +0000866 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
867 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
868 getOrCreateTypeDIE(DTy.getClassType()));
Devang Patel161b2f42011-04-12 23:21:44 +0000869 // Add source line info if available and TyDesc is not a forward declaration.
870 if (!DTy.isForwardDecl())
871 addSourceLine(&Buffer, DTy);
872}
873
874/// constructTypeDIE - Construct type DIE from DICompositeType.
875void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
876 // Get core information.
877 StringRef Name = CTy.getName();
878
879 uint64_t Size = CTy.getSizeInBits() >> 3;
880 unsigned Tag = CTy.getTag();
881 Buffer.setTag(Tag);
882
883 switch (Tag) {
Devang Patel161b2f42011-04-12 23:21:44 +0000884 case dwarf::DW_TAG_array_type:
885 constructArrayTypeDIE(Buffer, &CTy);
886 break;
887 case dwarf::DW_TAG_enumeration_type: {
888 DIArray Elements = CTy.getTypeArray();
889
890 // Add enumerators to enumeration type.
891 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
892 DIE *ElemDie = NULL;
893 DIDescriptor Enum(Elements.getElement(i));
894 if (Enum.isEnumerator()) {
895 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
896 Buffer.addChild(ElemDie);
897 }
898 }
Eric Christopherbb0f6ea2012-05-23 00:09:20 +0000899 DIType DTy = CTy.getTypeDerivedFrom();
900 if (DTy.Verify()) {
901 addType(&Buffer, DTy);
902 addUInt(&Buffer, dwarf::DW_AT_enum_class, dwarf::DW_FORM_flag, 1);
903 }
Devang Patel161b2f42011-04-12 23:21:44 +0000904 }
905 break;
906 case dwarf::DW_TAG_subroutine_type: {
907 // Add return type.
908 DIArray Elements = CTy.getTypeArray();
909 DIDescriptor RTy = Elements.getElement(0);
910 addType(&Buffer, DIType(RTy));
911
912 bool isPrototyped = true;
913 // Add arguments.
914 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
915 DIDescriptor Ty = Elements.getElement(i);
916 if (Ty.isUnspecifiedParameter()) {
917 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters);
918 Buffer.addChild(Arg);
919 isPrototyped = false;
920 } else {
921 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
922 addType(Arg, DIType(Ty));
David Blaikieaaf2e632013-01-29 19:35:24 +0000923 if (DIType(Ty).isArtificial())
924 addFlag(Arg, dwarf::DW_AT_artificial);
Devang Patel161b2f42011-04-12 23:21:44 +0000925 Buffer.addChild(Arg);
926 }
927 }
Eric Christopher8b6fe6b2012-02-22 08:46:21 +0000928 // Add prototype flag if we're dealing with a C language and the
929 // function has been prototyped.
930 if (isPrototyped &&
Eric Christopher4d069bf2012-05-22 18:45:24 +0000931 (Language == dwarf::DW_LANG_C89 ||
932 Language == dwarf::DW_LANG_C99 ||
933 Language == dwarf::DW_LANG_ObjC))
Eric Christopher873cf0a2012-08-24 01:14:27 +0000934 addFlag(&Buffer, dwarf::DW_AT_prototyped);
Devang Patel161b2f42011-04-12 23:21:44 +0000935 }
936 break;
937 case dwarf::DW_TAG_structure_type:
938 case dwarf::DW_TAG_union_type:
939 case dwarf::DW_TAG_class_type: {
940 // Add elements to structure type.
941 DIArray Elements = CTy.getTypeArray();
942
943 // A forward struct declared type may not have elements available.
944 unsigned N = Elements.getNumElements();
945 if (N == 0)
946 break;
947
948 // Add elements to structure type.
949 for (unsigned i = 0; i < N; ++i) {
950 DIDescriptor Element = Elements.getElement(i);
951 DIE *ElemDie = NULL;
952 if (Element.isSubprogram()) {
953 DISubprogram SP(Element);
Devang Pateldbc64af2011-08-15 17:24:54 +0000954 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
Devang Patel161b2f42011-04-12 23:21:44 +0000955 if (SP.isProtected())
Nick Lewycky13aaca52011-12-13 05:09:11 +0000956 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000957 dwarf::DW_ACCESS_protected);
958 else if (SP.isPrivate())
Nick Lewycky13aaca52011-12-13 05:09:11 +0000959 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000960 dwarf::DW_ACCESS_private);
Eric Christopher8b4310b2012-11-21 00:34:38 +0000961 else
Nick Lewycky13aaca52011-12-13 05:09:11 +0000962 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000963 dwarf::DW_ACCESS_public);
964 if (SP.isExplicit())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000965 addFlag(ElemDie, dwarf::DW_AT_explicit);
Eric Christopher663e0cf2012-03-28 07:34:31 +0000966 } else if (Element.isDerivedType()) {
Eric Christopher4d069bf2012-05-22 18:45:24 +0000967 DIDerivedType DDTy(Element);
968 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
969 ElemDie = new DIE(dwarf::DW_TAG_friend);
970 addType(ElemDie, DDTy.getTypeDerivedFrom(), dwarf::DW_AT_friend);
Eric Christopher6b6061f2013-01-16 01:22:23 +0000971 } else if (DDTy.isStaticMember())
972 ElemDie = createStaticMemberDIE(DDTy);
973 else
974 ElemDie = createMemberDIE(DDTy);
Eric Christopher663e0cf2012-03-28 07:34:31 +0000975 } else if (Element.isObjCProperty()) {
Devang Patel30d409c2012-02-07 23:33:58 +0000976 DIObjCProperty Property(Element);
977 ElemDie = new DIE(Property.getTag());
978 StringRef PropertyName = Property.getObjCPropertyName();
979 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Eric Christopher4d069bf2012-05-22 18:45:24 +0000980 addType(ElemDie, Property.getType());
981 addSourceLine(ElemDie, Property);
Devang Patel30d409c2012-02-07 23:33:58 +0000982 StringRef GetterName = Property.getObjCPropertyGetterName();
983 if (!GetterName.empty())
984 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
985 StringRef SetterName = Property.getObjCPropertySetterName();
986 if (!SetterName.empty())
987 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
988 unsigned PropertyAttributes = 0;
Devang Patel9e11eb12012-02-04 01:30:32 +0000989 if (Property.isReadOnlyObjCProperty())
990 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
991 if (Property.isReadWriteObjCProperty())
992 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
993 if (Property.isAssignObjCProperty())
994 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
995 if (Property.isRetainObjCProperty())
996 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
997 if (Property.isCopyObjCProperty())
998 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
999 if (Property.isNonAtomicObjCProperty())
1000 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1001 if (PropertyAttributes)
Eric Christopher8b4310b2012-11-21 00:34:38 +00001002 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, 0,
Devang Patel9e11eb12012-02-04 01:30:32 +00001003 PropertyAttributes);
Devang Patel6588abf2012-02-06 17:49:43 +00001004
Devang Patel30d409c2012-02-07 23:33:58 +00001005 DIEEntry *Entry = getDIEEntry(Element);
1006 if (!Entry) {
1007 Entry = createDIEEntry(ElemDie);
1008 insertDIEEntry(Element, Entry);
1009 }
Devang Patel9e11eb12012-02-04 01:30:32 +00001010 } else
Devang Patel161b2f42011-04-12 23:21:44 +00001011 continue;
1012 Buffer.addChild(ElemDie);
1013 }
1014
1015 if (CTy.isAppleBlockExtension())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001016 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel161b2f42011-04-12 23:21:44 +00001017
Devang Patel161b2f42011-04-12 23:21:44 +00001018 DICompositeType ContainingType = CTy.getContainingType();
1019 if (DIDescriptor(ContainingType).isCompositeType())
1020 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
1021 getOrCreateTypeDIE(DIType(ContainingType)));
1022 else {
1023 DIDescriptor Context = CTy.getContext();
1024 addToContextOwner(&Buffer, Context);
1025 }
1026
Devang Patel201e6cd2011-05-12 21:29:42 +00001027 if (CTy.isObjcClassComplete())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001028 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patelb11f80e2011-05-12 19:06:16 +00001029
Eric Christopher1a8e8862011-12-16 23:42:42 +00001030 // Add template parameters to a class, structure or union types.
1031 // FIXME: The support isn't in the metadata for this yet.
1032 if (Tag == dwarf::DW_TAG_class_type ||
1033 Tag == dwarf::DW_TAG_structure_type ||
1034 Tag == dwarf::DW_TAG_union_type)
Devang Patel161b2f42011-04-12 23:21:44 +00001035 addTemplateParams(Buffer, CTy.getTemplateParams());
1036
1037 break;
1038 }
1039 default:
1040 break;
1041 }
1042
1043 // Add name if not anonymous or intermediate type.
1044 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001045 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001046
Eric Christopher4a5d8392012-05-22 18:45:18 +00001047 if (Tag == dwarf::DW_TAG_enumeration_type ||
1048 Tag == dwarf::DW_TAG_class_type ||
1049 Tag == dwarf::DW_TAG_structure_type ||
1050 Tag == dwarf::DW_TAG_union_type) {
Devang Patel161b2f42011-04-12 23:21:44 +00001051 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopherfc4199b2012-06-01 00:22:32 +00001052 // TODO: Do we care about size for enum forward declarations?
Devang Patel161b2f42011-04-12 23:21:44 +00001053 if (Size)
1054 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Eric Christopherfc4199b2012-06-01 00:22:32 +00001055 else if (!CTy.isForwardDecl())
Devang Patel161b2f42011-04-12 23:21:44 +00001056 // Add zero size if it is not a forward declaration.
Eric Christopherfc4199b2012-06-01 00:22:32 +00001057 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
1058
1059 // If we're a forward decl, say so.
1060 if (CTy.isForwardDecl())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001061 addFlag(&Buffer, dwarf::DW_AT_declaration);
Devang Patel161b2f42011-04-12 23:21:44 +00001062
1063 // Add source line info if available.
1064 if (!CTy.isForwardDecl())
1065 addSourceLine(&Buffer, CTy);
Eric Christopher89388952012-03-07 00:15:19 +00001066
1067 // No harm in adding the runtime language to the declaration.
1068 unsigned RLang = CTy.getRunTimeLang();
1069 if (RLang)
1070 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
1071 dwarf::DW_FORM_data1, RLang);
Devang Patel161b2f42011-04-12 23:21:44 +00001072 }
1073}
1074
Eric Christopher8b4310b2012-11-21 00:34:38 +00001075/// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
Devang Patel161b2f42011-04-12 23:21:44 +00001076/// for the given DITemplateTypeParameter.
1077DIE *
1078CompileUnit::getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP) {
1079 DIE *ParamDIE = getDIE(TP);
1080 if (ParamDIE)
1081 return ParamDIE;
1082
1083 ParamDIE = new DIE(dwarf::DW_TAG_template_type_parameter);
1084 addType(ParamDIE, TP.getType());
Nick Lewycky390c40d2011-10-27 06:44:11 +00001085 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel161b2f42011-04-12 23:21:44 +00001086 return ParamDIE;
1087}
1088
Eric Christopher8b4310b2012-11-21 00:34:38 +00001089/// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE
Devang Patel161b2f42011-04-12 23:21:44 +00001090/// for the given DITemplateValueParameter.
1091DIE *
Eric Christopher4d069bf2012-05-22 18:45:24 +00001092CompileUnit::getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TPV){
Devang Patel161b2f42011-04-12 23:21:44 +00001093 DIE *ParamDIE = getDIE(TPV);
1094 if (ParamDIE)
1095 return ParamDIE;
1096
1097 ParamDIE = new DIE(dwarf::DW_TAG_template_value_parameter);
1098 addType(ParamDIE, TPV.getType());
1099 if (!TPV.getName().empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001100 addString(ParamDIE, dwarf::DW_AT_name, TPV.getName());
David Blaikie4de9d722013-05-10 21:52:07 +00001101 if (Value *Val = TPV.getValue()) {
1102 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val))
1103 addConstantValue(ParamDIE, CI, TPV.getType().isUnsignedDIType());
1104 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) {
1105 // For declaration non-type template parameters (such as global values and
1106 // functions)
1107 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1108 addOpAddress(Block, Asm->Mang->getSymbol(GV));
1109 // Emit DW_OP_stack_value to use the address as the immediate value of the
1110 // parameter, rather than a pointer to it.
1111 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1112 addBlock(ParamDIE, dwarf::DW_AT_location, 0, Block);
1113 }
1114 }
1115
Devang Patel161b2f42011-04-12 23:21:44 +00001116 return ParamDIE;
1117}
1118
Devang Patel31c5d052011-05-06 16:57:54 +00001119/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1120DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) {
1121 DIE *NDie = getDIE(NS);
1122 if (NDie)
1123 return NDie;
1124 NDie = new DIE(dwarf::DW_TAG_namespace);
1125 insertDIE(NS, NDie);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001126 if (!NS.getName().empty()) {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001127 addString(NDie, dwarf::DW_AT_name, NS.getName());
Eric Christopher09ac3d82011-11-07 09:24:32 +00001128 addAccelNamespace(NS.getName(), NDie);
1129 } else
1130 addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel31c5d052011-05-06 16:57:54 +00001131 addSourceLine(NDie, NS);
1132 addToContextOwner(NDie, NS.getContext());
1133 return NDie;
1134}
1135
Devang Pateldbc64af2011-08-15 17:24:54 +00001136/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
1137/// printer to not emit usual symbol prefix before the symbol name is used then
1138/// return linkage name after skipping this special LLVM prefix.
1139static StringRef getRealLinkageName(StringRef LinkageName) {
1140 char One = '\1';
1141 if (LinkageName.startswith(StringRef(&One, 1)))
1142 return LinkageName.substr(1);
1143 return LinkageName;
1144}
1145
1146/// getOrCreateSubprogramDIE - Create new DIE using SP.
1147DIE *CompileUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
1148 DIE *SPDie = getDIE(SP);
1149 if (SPDie)
1150 return SPDie;
1151
Peter Collingbourne27302f02012-05-27 18:36:44 +00001152 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1153
1154 // DW_TAG_inlined_subroutine may refer to this DIE.
1155 insertDIE(SP, SPDie);
1156
Rafael Espindola01b55b42011-11-10 22:34:29 +00001157 DISubprogram SPDecl = SP.getFunctionDeclaration();
1158 DIE *DeclDie = NULL;
1159 if (SPDecl.isSubprogram()) {
1160 DeclDie = getOrCreateSubprogramDIE(SPDecl);
1161 }
1162
Devang Pateldbc64af2011-08-15 17:24:54 +00001163 // Add to context owner.
1164 addToContextOwner(SPDie, SP.getContext());
1165
1166 // Add function template parameters.
1167 addTemplateParams(*SPDie, SP.getTemplateParams());
1168
Eric Christopherc4968752013-05-09 00:42:33 +00001169 // Unfortunately this code needs to stay here instead of below the
1170 // AT_specification code in order to work around a bug in older
1171 // gdbs that requires the linkage name to resolve multiple template
1172 // functions.
1173 // TODO: Remove this set of code when we get rid of the old gdb
1174 // compatibility.
Eric Christopher8d101c32012-03-15 08:19:33 +00001175 StringRef LinkageName = SP.getLinkageName();
Eric Christopherc4968752013-05-09 00:42:33 +00001176 if (!LinkageName.empty() && DD->useDarwinGDBCompat())
1177 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
1178 getRealLinkageName(LinkageName));
Eric Christopher8d101c32012-03-15 08:19:33 +00001179
Devang Pateldbc64af2011-08-15 17:24:54 +00001180 // If this DIE is going to refer declaration info using AT_specification
Eric Christopherc4968752013-05-09 00:42:33 +00001181 // then there is no need to add other attributes.
Rafael Espindola01b55b42011-11-10 22:34:29 +00001182 if (DeclDie) {
1183 // Refer function declaration directly.
1184 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1185 DeclDie);
1186
Devang Pateldbc64af2011-08-15 17:24:54 +00001187 return SPDie;
Rafael Espindola01b55b42011-11-10 22:34:29 +00001188 }
Devang Pateldbc64af2011-08-15 17:24:54 +00001189
Eric Christophercbbd5b12012-08-23 22:52:55 +00001190 // Add the linkage name if we have one.
Eric Christopherc4968752013-05-09 00:42:33 +00001191 if (!LinkageName.empty() && !DD->useDarwinGDBCompat())
Eric Christophercbbd5b12012-08-23 22:52:55 +00001192 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
1193 getRealLinkageName(LinkageName));
1194
Devang Pateldbc64af2011-08-15 17:24:54 +00001195 // Constructors and operators for anonymous aggregates do not have names.
1196 if (!SP.getName().empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001197 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Pateldbc64af2011-08-15 17:24:54 +00001198
1199 addSourceLine(SPDie, SP);
1200
Eric Christopher8b6fe6b2012-02-22 08:46:21 +00001201 // Add the prototype if we have a prototype and we have a C like
1202 // language.
1203 if (SP.isPrototyped() &&
1204 (Language == dwarf::DW_LANG_C89 ||
1205 Language == dwarf::DW_LANG_C99 ||
1206 Language == dwarf::DW_LANG_ObjC))
Eric Christopher873cf0a2012-08-24 01:14:27 +00001207 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Pateldbc64af2011-08-15 17:24:54 +00001208
1209 // Add Return Type.
1210 DICompositeType SPTy = SP.getType();
1211 DIArray Args = SPTy.getTypeArray();
1212 unsigned SPTag = SPTy.getTag();
1213
1214 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
1215 addType(SPDie, SPTy);
1216 else
1217 addType(SPDie, DIType(Args.getElement(0)));
1218
1219 unsigned VK = SP.getVirtuality();
1220 if (VK) {
Nick Lewycky798313d2011-12-14 00:56:07 +00001221 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Devang Pateldbc64af2011-08-15 17:24:54 +00001222 DIEBlock *Block = getDIEBlock();
1223 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1224 addUInt(Block, 0, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1225 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
1226 ContainingTypeMap.insert(std::make_pair(SPDie,
1227 SP.getContainingType()));
1228 }
1229
1230 if (!SP.isDefinition()) {
Eric Christopher873cf0a2012-08-24 01:14:27 +00001231 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher8b4310b2012-11-21 00:34:38 +00001232
Devang Pateldbc64af2011-08-15 17:24:54 +00001233 // Add arguments. Do not add arguments for subprogram definition. They will
1234 // be handled while processing variables.
1235 DICompositeType SPTy = SP.getType();
1236 DIArray Args = SPTy.getTypeArray();
1237 unsigned SPTag = SPTy.getTag();
1238
1239 if (SPTag == dwarf::DW_TAG_subroutine_type)
1240 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1241 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Eric Christopher5c38de92012-09-10 23:33:57 +00001242 DIType ATy = DIType(Args.getElement(i));
Devang Pateldbc64af2011-08-15 17:24:54 +00001243 addType(Arg, ATy);
1244 if (ATy.isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001245 addFlag(Arg, dwarf::DW_AT_artificial);
Devang Pateldbc64af2011-08-15 17:24:54 +00001246 SPDie->addChild(Arg);
1247 }
1248 }
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();
Eric Christopher0969ddf2013-01-18 22:11:33 +00001352 addOpAddress(Block, Asm->Mang->getSymbol(GV.getGlobal()));
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001353 // Do not create specification DIE if context is either compile unit
1354 // or a subprogram.
Devang Patel1dd4e562011-09-21 23:41:11 +00001355 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001356 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
1357 // Create specification DIE.
Eric Christopherd117fbb2011-11-11 01:55:22 +00001358 VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001359 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1360 dwarf::DW_FORM_ref4, VariableDIE);
1361 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopher6b6061f2013-01-16 01:22:23 +00001362 // A static member's declaration is already flagged as such.
1363 if (!SDMDecl.Verify())
1364 addFlag(VariableDIE, dwarf::DW_AT_declaration);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001365 addDie(VariableSpecDIE);
1366 } else {
1367 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001368 }
Eric Christopher6b6061f2013-01-16 01:22:23 +00001369 // Add linkage name.
1370 StringRef LinkageName = GV.getLinkageName();
Eric Christophera8ada252013-02-27 23:49:50 +00001371 if (!LinkageName.empty()) {
Eric Christopher8d45a982013-02-27 23:49:47 +00001372 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1373 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1374 // TAG_variable.
Manman Ren21a08a12013-02-27 23:21:02 +00001375 addString(IsStaticMember && VariableSpecDIE ?
1376 VariableSpecDIE : VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
Eric Christopher6b6061f2013-01-16 01:22:23 +00001377 getRealLinkageName(LinkageName));
Eric Christopher8d45a982013-02-27 23:49:47 +00001378 // In compatibility mode with older gdbs we put the linkage name on both
1379 // the TAG_variable DIE and on the TAG_member DIE.
Manman Ren21a08a12013-02-27 23:21:02 +00001380 if (IsStaticMember && VariableSpecDIE && DD->useDarwinGDBCompat())
1381 addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
Manman Ren06df83c2013-02-27 00:02:32 +00001382 getRealLinkageName(LinkageName));
1383 }
Eric Christopher8b4310b2012-11-21 00:34:38 +00001384 } else if (const ConstantInt *CI =
Manman Ren945e8282013-02-01 23:54:37 +00001385 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
Adrian Prantl2e892e42013-04-04 22:56:49 +00001386 // AT_const_value was added when the static member was created. To avoid
Manman Ren945e8282013-02-01 23:54:37 +00001387 // emitting AT_const_value multiple times, we only add AT_const_value when
1388 // it is not a static member.
1389 if (!IsStaticMember)
1390 addConstantValue(VariableDIE, CI, GTy.isUnsignedDIType());
1391 } else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) {
Eric Christopher09ac3d82011-11-07 09:24:32 +00001392 addToAccelTable = true;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001393 // GV is a merged global.
1394 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1395 Value *Ptr = CE->getOperand(0);
Eric Christopher0969ddf2013-01-18 22:11:33 +00001396 addOpAddress(Block, Asm->Mang->getSymbol(cast<GlobalValue>(Ptr)));
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001397 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1398 SmallVector<Value*, 3> Idx(CE->op_begin()+1, CE->op_end());
Eric Christopher8b4310b2012-11-21 00:34:38 +00001399 addUInt(Block, 0, dwarf::DW_FORM_udata,
Micah Villmow3574eca2012-10-08 16:38:25 +00001400 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001401 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1402 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1403 }
1404
Eric Christopherd117fbb2011-11-11 01:55:22 +00001405 if (addToAccelTable) {
1406 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1407 addAccelName(GV.getName(), AddrDIE);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001408
Eric Christopherd117fbb2011-11-11 01:55:22 +00001409 // If the linkage name is different than the name, go ahead and output
1410 // that as well into the name table.
1411 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1412 addAccelName(GV.getLinkageName(), AddrDIE);
1413 }
Eric Christopher74d8a872011-11-08 21:56:23 +00001414
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001415 return;
1416}
1417
Devang Patel161b2f42011-04-12 23:21:44 +00001418/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Eric Christopher4d069bf2012-05-22 18:45:24 +00001419void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR,
1420 DIE *IndexTy) {
Devang Patel161b2f42011-04-12 23:21:44 +00001421 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1422 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patel161b2f42011-04-12 23:21:44 +00001423
Bill Wendling222c2fd2012-12-06 07:38:10 +00001424 // The LowerBound value defines the lower bounds which is typically zero for
1425 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1426 // Count == -1 then the array is unbounded and we do not emit
1427 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1428 // Count == 0, then the array has zero elements in which case we do not emit
1429 // an upper bound.
1430 int64_t LowerBound = SR.getLo();
Bill Wendling6afe4782012-12-06 07:55:19 +00001431 int64_t DefaultLowerBound = getDefaultLowerBound();
Bill Wendling9493dae2012-12-04 21:34:03 +00001432 int64_t Count = SR.getCount();
Devang Patel161b2f42011-04-12 23:21:44 +00001433
Bill Wendling6afe4782012-12-06 07:55:19 +00001434 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
Bill Wendling222c2fd2012-12-06 07:38:10 +00001435 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, LowerBound);
1436
1437 if (Count != -1 && Count != 0)
Bill Wendling9493dae2012-12-04 21:34:03 +00001438 // FIXME: An unbounded array should reference the expression that defines
1439 // the array.
Bill Wendling222c2fd2012-12-06 07:38:10 +00001440 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, LowerBound + Count - 1);
Bill Wendling9493dae2012-12-04 21:34:03 +00001441
Devang Patel161b2f42011-04-12 23:21:44 +00001442 Buffer.addChild(DW_Subrange);
1443}
1444
1445/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
1446void CompileUnit::constructArrayTypeDIE(DIE &Buffer,
1447 DICompositeType *CTy) {
1448 Buffer.setTag(dwarf::DW_TAG_array_type);
Eric Christopher9a1e0e22013-01-08 01:53:52 +00001449 if (CTy->isVector())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001450 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel161b2f42011-04-12 23:21:44 +00001451
1452 // Emit derived type.
1453 addType(&Buffer, CTy->getTypeDerivedFrom());
1454 DIArray Elements = CTy->getTypeArray();
1455
1456 // Get an anonymous type for index type.
Eric Christopher8cab6ed2013-01-04 21:51:53 +00001457 // FIXME: This type should be passed down from the front end
1458 // as different languages may have different sizes for indexes.
Devang Patel161b2f42011-04-12 23:21:44 +00001459 DIE *IdxTy = getIndexTyDie();
1460 if (!IdxTy) {
1461 // Construct an anonymous type for index type.
1462 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Eric Christopher8cab6ed2013-01-04 21:51:53 +00001463 addString(IdxTy, dwarf::DW_AT_name, "int");
Devang Patel161b2f42011-04-12 23:21:44 +00001464 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1465 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1466 dwarf::DW_ATE_signed);
1467 addDie(IdxTy);
1468 setIndexTyDie(IdxTy);
1469 }
1470
1471 // Add subranges to array type.
1472 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1473 DIDescriptor Element = Elements.getElement(i);
1474 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1475 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1476 }
1477}
1478
1479/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
1480DIE *CompileUnit::constructEnumTypeDIE(DIEnumerator ETy) {
1481 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
1482 StringRef Name = ETy.getName();
Nick Lewycky390c40d2011-10-27 06:44:11 +00001483 addString(Enumerator, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001484 int64_t Value = ETy.getEnumValue();
1485 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
1486 return Enumerator;
1487}
1488
Devang Pateldbc64af2011-08-15 17:24:54 +00001489/// constructContainingTypeDIEs - Construct DIEs for types that contain
1490/// vtables.
1491void CompileUnit::constructContainingTypeDIEs() {
1492 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
1493 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1494 DIE *SPDie = CI->first;
1495 const MDNode *N = CI->second;
1496 if (!N) continue;
1497 DIE *NDie = getDIE(N);
1498 if (!NDie) continue;
1499 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1500 }
1501}
1502
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001503/// constructVariableDIE - Construct a DIE for the given DbgVariable.
1504DIE *CompileUnit::constructVariableDIE(DbgVariable *DV, bool isScopeAbstract) {
1505 StringRef Name = DV->getName();
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001506
1507 // Translate tag to proper Dwarf tag.
1508 unsigned Tag = DV->getTag();
1509
1510 // Define variable debug information entry.
1511 DIE *VariableDie = new DIE(Tag);
1512 DbgVariable *AbsVar = DV->getAbstractVariable();
1513 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
1514 if (AbsDIE)
1515 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
1516 dwarf::DW_FORM_ref4, AbsDIE);
1517 else {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001518 addString(VariableDie, dwarf::DW_AT_name, Name);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001519 addSourceLine(VariableDie, DV->getVariable());
1520 addType(VariableDie, DV->getType());
1521 }
1522
1523 if (DV->isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001524 addFlag(VariableDie, dwarf::DW_AT_artificial);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001525
1526 if (isScopeAbstract) {
1527 DV->setDIE(VariableDie);
1528 return VariableDie;
1529 }
1530
1531 // Add variable address.
1532
1533 unsigned Offset = DV->getDotDebugLocOffset();
1534 if (Offset != ~0U) {
1535 addLabel(VariableDie, dwarf::DW_AT_location,
1536 dwarf::DW_FORM_data4,
1537 Asm->GetTempSymbol("debug_loc", Offset));
1538 DV->setDIE(VariableDie);
1539 return VariableDie;
1540 }
1541
Eric Christopher8cf5e742011-10-03 15:49:20 +00001542 // Check if variable is described by a DBG_VALUE instruction.
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001543 if (const MachineInstr *DVInsn = DV->getMInsn()) {
1544 bool updated = false;
1545 if (DVInsn->getNumOperands() == 3) {
1546 if (DVInsn->getOperand(0).isReg()) {
1547 const MachineOperand RegOp = DVInsn->getOperand(0);
1548 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
1549 if (DVInsn->getOperand(1).isImm() &&
1550 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
1551 unsigned FrameReg = 0;
1552 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopher8b4310b2012-11-21 00:34:38 +00001553 int Offset =
1554 TFI->getFrameIndexReference(*Asm->MF,
1555 DVInsn->getOperand(1).getImm(),
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001556 FrameReg);
1557 MachineLocation Location(FrameReg, Offset);
1558 addVariableAddress(DV, VariableDie, Location);
Eric Christopher8b4310b2012-11-21 00:34:38 +00001559
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001560 } else if (RegOp.getReg())
Eric Christopher8b4310b2012-11-21 00:34:38 +00001561 addVariableAddress(DV, VariableDie,
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001562 MachineLocation(RegOp.getReg()));
1563 updated = true;
1564 }
1565 else if (DVInsn->getOperand(0).isImm())
Eric Christopher8b4310b2012-11-21 00:34:38 +00001566 updated =
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001567 addConstantValue(VariableDie, DVInsn->getOperand(0),
1568 DV->getType());
1569 else if (DVInsn->getOperand(0).isFPImm())
1570 updated =
1571 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
1572 else if (DVInsn->getOperand(0).isCImm())
1573 updated =
Eric Christopher8b4310b2012-11-21 00:34:38 +00001574 addConstantValue(VariableDie,
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001575 DVInsn->getOperand(0).getCImm(),
1576 DV->getType().isUnsignedDIType());
1577 } else {
Eric Christopher8b4310b2012-11-21 00:34:38 +00001578 addVariableAddress(DV, VariableDie,
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001579 Asm->getDebugValueLocation(DVInsn));
1580 updated = true;
1581 }
1582 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);
1599 addVariableAddress(DV, VariableDie, Location);
1600 }
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}