blob: 339a5d2752b50b89b9e6b10ba0bcb9ca8daf55fa [file] [log] [blame]
Devang Patel161b2f42011-04-12 23:21:44 +00001//===-- llvm/CodeGen/DwarfCompileUnit.cpp - Dwarf Compile Unit ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Eric Christopher443c9ed2012-08-14 05:13:29 +000010// This file contains support for constructing a dwarf compile unit.
Devang Patel161b2f42011-04-12 23:21:44 +000011//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "dwarfdebug"
15
16#include "DwarfCompileUnit.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "DwarfAccelTable.h"
Devang Patel161b2f42011-04-12 23:21:44 +000018#include "DwarfDebug.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/ADT/APFloat.h"
Bill Wendling16eeb6f2012-06-29 08:32:07 +000020#include "llvm/DIBuilder.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000021#include "llvm/IR/Constants.h"
22#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/GlobalVariable.h"
24#include "llvm/IR/Instructions.h"
Eric Christopherd61c34b2011-11-11 03:16:32 +000025#include "llvm/Support/Debug.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000026#include "llvm/Support/ErrorHandling.h"
Devang Patel6f9d8ff2011-08-15 17:57:41 +000027#include "llvm/Target/Mangler.h"
Devang Patel161b2f42011-04-12 23:21:44 +000028#include "llvm/Target/TargetFrameLowering.h"
29#include "llvm/Target/TargetMachine.h"
David Blaikie59eaa382013-06-28 20:05:11 +000030#include "llvm/Target/TargetLoweringObjectFile.h"
Devang Patel161b2f42011-04-12 23:21:44 +000031#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel161b2f42011-04-12 23:21:44 +000032
33using namespace llvm;
34
35/// CompileUnit - Compile unit constructor.
Eric Christopher9c57ad22013-05-08 00:58:51 +000036CompileUnit::CompileUnit(unsigned UID, unsigned L, DIE *D, const MDNode *N,
Eric Christopher5592ba42013-05-30 00:43:32 +000037 AsmPrinter *A, DwarfDebug *DW, DwarfUnits *DWU)
Eric Christopher2e5d8702012-12-20 21:58:36 +000038 : UniqueID(UID), Language(L), CUDie(D), Asm(A), DD(DW), DU(DWU),
Manman Renbc3e96f2013-03-12 18:27:15 +000039 IndexTyDie(0), DebugInfoOffset(0) {
Devang Patel161b2f42011-04-12 23:21:44 +000040 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
Eric Christopher9c57ad22013-05-08 00:58:51 +000041 insertDIE(N, D);
Devang Patel161b2f42011-04-12 23:21:44 +000042}
43
44/// ~CompileUnit - Destructor for compile unit.
45CompileUnit::~CompileUnit() {
46 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
47 DIEBlocks[j]->~DIEBlock();
48}
49
50/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
51/// information entry.
52DIEEntry *CompileUnit::createDIEEntry(DIE *Entry) {
53 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
54 return Value;
55}
56
Bill Wendling6afe4782012-12-06 07:55:19 +000057/// getDefaultLowerBound - Return the default lower bound for an array. If the
Bill Wendling222c2fd2012-12-06 07:38:10 +000058/// DWARF version doesn't handle the language, return -1.
Bill Wendling6afe4782012-12-06 07:55:19 +000059int64_t CompileUnit::getDefaultLowerBound() const {
Bill Wendling222c2fd2012-12-06 07:38:10 +000060 switch (Language) {
61 default:
62 break;
63
64 case dwarf::DW_LANG_C89:
65 case dwarf::DW_LANG_C99:
66 case dwarf::DW_LANG_C:
67 case dwarf::DW_LANG_C_plus_plus:
68 case dwarf::DW_LANG_ObjC:
69 case dwarf::DW_LANG_ObjC_plus_plus:
70 return 0;
71
72 case dwarf::DW_LANG_Fortran77:
73 case dwarf::DW_LANG_Fortran90:
74 case dwarf::DW_LANG_Fortran95:
75 return 1;
76
77 // The languages below have valid values only if the DWARF version >= 4.
78 case dwarf::DW_LANG_Java:
79 case dwarf::DW_LANG_Python:
80 case dwarf::DW_LANG_UPC:
81 case dwarf::DW_LANG_D:
82 if (dwarf::DWARF_VERSION >= 4)
83 return 0;
84 break;
85
86 case dwarf::DW_LANG_Ada83:
87 case dwarf::DW_LANG_Ada95:
88 case dwarf::DW_LANG_Cobol74:
89 case dwarf::DW_LANG_Cobol85:
90 case dwarf::DW_LANG_Modula2:
91 case dwarf::DW_LANG_Pascal83:
92 case dwarf::DW_LANG_PLI:
93 if (dwarf::DWARF_VERSION >= 4)
94 return 1;
95 break;
96 }
97
98 return -1;
99}
100
Eric Christopher873cf0a2012-08-24 01:14:27 +0000101/// addFlag - Add a flag that is true.
102void CompileUnit::addFlag(DIE *Die, unsigned Attribute) {
103 if (!DD->useDarwinGDBCompat())
104 Die->addValue(Attribute, dwarf::DW_FORM_flag_present,
105 DIEIntegerOne);
106 else
107 addUInt(Die, Attribute, dwarf::DW_FORM_flag, 1);
108}
109
Devang Patel161b2f42011-04-12 23:21:44 +0000110/// addUInt - Add an unsigned integer attribute data and value.
111///
112void CompileUnit::addUInt(DIE *Die, unsigned Attribute,
113 unsigned Form, uint64_t Integer) {
114 if (!Form) Form = DIEInteger::BestForm(false, Integer);
115 DIEValue *Value = Integer == 1 ?
116 DIEIntegerOne : new (DIEValueAllocator) DIEInteger(Integer);
117 Die->addValue(Attribute, Form, Value);
118}
119
120/// addSInt - Add an signed integer attribute data and value.
121///
122void CompileUnit::addSInt(DIE *Die, unsigned Attribute,
123 unsigned Form, int64_t Integer) {
124 if (!Form) Form = DIEInteger::BestForm(true, Integer);
125 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
126 Die->addValue(Attribute, Form, Value);
127}
128
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000129/// addString - Add a string attribute data and value. We always emit a
130/// reference to the string pool instead of immediate strings so that DIEs have
Eric Christopher3cc42202013-01-07 19:32:45 +0000131/// more predictable sizes. In the case of split dwarf we emit an index
132/// into another table which gets us the static offset into the string
133/// table.
Nick Lewycky390c40d2011-10-27 06:44:11 +0000134void CompileUnit::addString(DIE *Die, unsigned Attribute, StringRef String) {
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000135 if (!DD->useSplitDwarf()) {
136 MCSymbol *Symb = DU->getStringPoolEntry(String);
137 DIEValue *Value;
138 if (Asm->needsRelocationsForDwarfStringPool())
David Blaikie95e72c92013-06-28 20:05:04 +0000139 Value = new (DIEValueAllocator) DIELabel(Symb, Asm->OutContext);
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000140 else {
141 MCSymbol *StringPool = DU->getStringPoolSym();
142 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
143 }
144 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value);
145 } else {
146 unsigned idx = DU->getStringPoolIndex(String);
147 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
148 Die->addValue(Attribute, dwarf::DW_FORM_GNU_str_index, Value);
149 }
150}
151
152/// addLocalString - Add a string attribute data and value. This is guaranteed
153/// to be in the local string pool instead of indirected.
154void CompileUnit::addLocalString(DIE *Die, unsigned Attribute,
155 StringRef String) {
Eric Christopher2e5d8702012-12-20 21:58:36 +0000156 MCSymbol *Symb = DU->getStringPoolEntry(String);
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000157 DIEValue *Value;
158 if (Asm->needsRelocationsForDwarfStringPool())
David Blaikie95e72c92013-06-28 20:05:04 +0000159 Value = new (DIEValueAllocator) DIELabel(Symb, Asm->OutContext);
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000160 else {
Eric Christopher2e5d8702012-12-20 21:58:36 +0000161 MCSymbol *StringPool = DU->getStringPoolSym();
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000162 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
Nick Lewycky390c40d2011-10-27 06:44:11 +0000163 }
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000164 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value);
Devang Patel161b2f42011-04-12 23:21:44 +0000165}
166
167/// addLabel - Add a Dwarf label attribute data and value.
168///
169void CompileUnit::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
David Blaikie95e72c92013-06-28 20:05:04 +0000170 const MCSymbolRefExpr *Label) {
Devang Patel161b2f42011-04-12 23:21:44 +0000171 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
172 Die->addValue(Attribute, Form, Value);
173}
174
David Blaikie95e72c92013-06-28 20:05:04 +0000175void CompileUnit::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
176 const MCSymbol *Label) {
177 addLabel(Die, Attribute, Form, MCSymbolRefExpr::Create(Label, Asm->OutContext));
178}
179
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000180/// addLabelAddress - Add a dwarf label attribute data and value using
181/// DW_FORM_addr or DW_FORM_GNU_addr_index.
182///
183void CompileUnit::addLabelAddress(DIE *Die, unsigned Attribute,
184 MCSymbol *Label) {
185 if (!DD->useSplitDwarf()) {
186 if (Label != NULL) {
David Blaikie95e72c92013-06-28 20:05:04 +0000187 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label, Asm->OutContext);
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000188 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
189 } else {
190 DIEValue *Value = new (DIEValueAllocator) DIEInteger(0);
191 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
192 }
193 } else {
194 unsigned idx = DU->getAddrPoolIndex(Label);
195 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
196 Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
197 }
198}
199
Eric Christopher0969ddf2013-01-18 22:11:33 +0000200/// addOpAddress - Add a dwarf op address data and value using the
201/// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
202///
David Blaikie95e72c92013-06-28 20:05:04 +0000203void CompileUnit::addOpAddress(DIE *Die, const MCSymbol *Sym) {
Eric Christopher0969ddf2013-01-18 22:11:33 +0000204
205 if (!DD->useSplitDwarf()) {
206 addUInt(Die, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
207 addLabel(Die, 0, dwarf::DW_FORM_udata, Sym);
208 } else {
209 unsigned idx = DU->getAddrPoolIndex(Sym);
210 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
211 addUInt(Die, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
212 Die->addValue(0, dwarf::DW_FORM_GNU_addr_index, Value);
213 }
214}
215
Devang Patel161b2f42011-04-12 23:21:44 +0000216/// addDelta - Add a label delta attribute data and value.
217///
218void CompileUnit::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
219 const MCSymbol *Hi, const MCSymbol *Lo) {
220 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
221 Die->addValue(Attribute, Form, Value);
222}
223
224/// addDIEEntry - Add a DIE attribute data and value.
225///
226void CompileUnit::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
227 DIE *Entry) {
228 Die->addValue(Attribute, Form, createDIEEntry(Entry));
229}
230
Devang Patel161b2f42011-04-12 23:21:44 +0000231/// addBlock - Add block data.
232///
233void CompileUnit::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
234 DIEBlock *Block) {
235 Block->ComputeSize(Asm);
236 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
237 Die->addValue(Attribute, Block->BestForm(), Block);
238}
239
240/// addSourceLine - Add location information to specified debug information
241/// entry.
242void CompileUnit::addSourceLine(DIE *Die, DIVariable V) {
243 // Verify variable.
244 if (!V.Verify())
245 return;
Eric Christopher8b4310b2012-11-21 00:34:38 +0000246
Devang Patel161b2f42011-04-12 23:21:44 +0000247 unsigned Line = V.getLineNumber();
248 if (Line == 0)
249 return;
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000250 unsigned FileID = DD->getOrCreateSourceID(V.getContext().getFilename(),
Manman Ren3de61b42013-03-07 01:42:00 +0000251 V.getContext().getDirectory(),
252 getUniqueID());
Devang Patel161b2f42011-04-12 23:21:44 +0000253 assert(FileID && "Invalid file id");
254 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
255 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
256}
257
258/// addSourceLine - Add location information to specified debug information
259/// entry.
260void CompileUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
261 // Verify global variable.
262 if (!G.Verify())
263 return;
264
265 unsigned Line = G.getLineNumber();
266 if (Line == 0)
267 return;
Manman Ren3de61b42013-03-07 01:42:00 +0000268 unsigned FileID = DD->getOrCreateSourceID(G.getFilename(), G.getDirectory(),
269 getUniqueID());
Devang Patel161b2f42011-04-12 23:21:44 +0000270 assert(FileID && "Invalid file id");
271 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
272 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
273}
274
275/// addSourceLine - Add location information to specified debug information
276/// entry.
277void CompileUnit::addSourceLine(DIE *Die, DISubprogram SP) {
278 // Verify subprogram.
279 if (!SP.Verify())
280 return;
Eric Christopher2125d5a2012-03-15 23:55:40 +0000281
Devang Patel161b2f42011-04-12 23:21:44 +0000282 // If the line number is 0, don't add it.
Eric Christopher2125d5a2012-03-15 23:55:40 +0000283 unsigned Line = SP.getLineNumber();
284 if (Line == 0)
Devang Patel161b2f42011-04-12 23:21:44 +0000285 return;
286
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000287 unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(),
Manman Ren3de61b42013-03-07 01:42:00 +0000288 SP.getDirectory(), getUniqueID());
Devang Patel161b2f42011-04-12 23:21:44 +0000289 assert(FileID && "Invalid file id");
290 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
291 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
292}
293
294/// addSourceLine - Add location information to specified debug information
295/// entry.
296void CompileUnit::addSourceLine(DIE *Die, DIType Ty) {
297 // Verify type.
298 if (!Ty.Verify())
299 return;
300
301 unsigned Line = Ty.getLineNumber();
Eric Christopher2125d5a2012-03-15 23:55:40 +0000302 if (Line == 0)
Devang Patel161b2f42011-04-12 23:21:44 +0000303 return;
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000304 unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(),
Manman Ren3de61b42013-03-07 01:42:00 +0000305 Ty.getDirectory(), getUniqueID());
Devang Patel161b2f42011-04-12 23:21:44 +0000306 assert(FileID && "Invalid file id");
307 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
308 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
309}
310
311/// addSourceLine - Add location information to specified debug information
312/// entry.
Eric Christopherb8ca9882012-03-29 08:42:56 +0000313void CompileUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
314 // Verify type.
315 if (!Ty.Verify())
316 return;
317
318 unsigned Line = Ty.getLineNumber();
319 if (Line == 0)
320 return;
321 DIFile File = Ty.getFile();
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000322 unsigned FileID = DD->getOrCreateSourceID(File.getFilename(),
Manman Ren3de61b42013-03-07 01:42:00 +0000323 File.getDirectory(), getUniqueID());
Eric Christopherb8ca9882012-03-29 08:42:56 +0000324 assert(FileID && "Invalid file id");
325 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
326 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
327}
328
329/// addSourceLine - Add location information to specified debug information
330/// entry.
Devang Patel161b2f42011-04-12 23:21:44 +0000331void CompileUnit::addSourceLine(DIE *Die, DINameSpace NS) {
332 // Verify namespace.
333 if (!NS.Verify())
334 return;
335
336 unsigned Line = NS.getLineNumber();
337 if (Line == 0)
338 return;
339 StringRef FN = NS.getFilename();
340
Manman Ren3de61b42013-03-07 01:42:00 +0000341 unsigned FileID = DD->getOrCreateSourceID(FN, NS.getDirectory(),
342 getUniqueID());
Devang Patel161b2f42011-04-12 23:21:44 +0000343 assert(FileID && "Invalid file id");
344 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
345 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
346}
347
Eric Christopher8b4310b2012-11-21 00:34:38 +0000348/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patele1cdf842011-04-27 22:45:24 +0000349/// DbgVariable based on provided MachineLocation.
Eric Christopherf61dbc12013-06-24 21:07:27 +0000350void CompileUnit::addVariableAddress(const DbgVariable &DV, DIE *Die,
Devang Patele1cdf842011-04-27 22:45:24 +0000351 MachineLocation Location) {
Eric Christopherf61dbc12013-06-24 21:07:27 +0000352 if (DV.variableHasComplexAddress())
Devang Patel161b2f42011-04-12 23:21:44 +0000353 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
Eric Christopherf61dbc12013-06-24 21:07:27 +0000354 else if (DV.isBlockByrefVariable())
Devang Patel161b2f42011-04-12 23:21:44 +0000355 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
356 else
David Blaikie4532c282013-06-20 00:25:24 +0000357 addAddress(Die, dwarf::DW_AT_location, Location,
Eric Christopherf61dbc12013-06-24 21:07:27 +0000358 DV.getVariable().isIndirect());
Devang Patel161b2f42011-04-12 23:21:44 +0000359}
360
Devang Patel116da2f2011-04-26 19:06:18 +0000361/// addRegisterOp - Add register operand.
362void CompileUnit::addRegisterOp(DIE *TheDie, unsigned Reg) {
363 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
364 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
365 if (DWReg < 32)
366 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
367 else {
368 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
369 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
370 }
371}
372
373/// addRegisterOffset - Add register offset.
374void CompileUnit::addRegisterOffset(DIE *TheDie, unsigned Reg,
375 int64_t Offset) {
376 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
377 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
378 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
379 if (Reg == TRI->getFrameRegister(*Asm->MF))
380 // If variable offset is based in frame register then use fbreg.
381 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
382 else if (DWReg < 32)
383 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
384 else {
385 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
386 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
387 }
388 addSInt(TheDie, 0, dwarf::DW_FORM_sdata, Offset);
389}
390
391/// addAddress - Add an address attribute to a die based on the location
392/// provided.
393void CompileUnit::addAddress(DIE *Die, unsigned Attribute,
David Blaikie4532c282013-06-20 00:25:24 +0000394 const MachineLocation &Location, bool Indirect) {
Devang Patel116da2f2011-04-26 19:06:18 +0000395 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
396
David Blaikie4532c282013-06-20 00:25:24 +0000397 if (Location.isReg() && !Indirect)
Devang Patel116da2f2011-04-26 19:06:18 +0000398 addRegisterOp(Block, Location.getReg());
David Blaikie4532c282013-06-20 00:25:24 +0000399 else {
Devang Patel116da2f2011-04-26 19:06:18 +0000400 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
David Blaikie4532c282013-06-20 00:25:24 +0000401 if (Indirect && !Location.isReg()) {
402 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
403 }
404 }
Devang Patel116da2f2011-04-26 19:06:18 +0000405
406 // Now attach the location information to the DIE.
407 addBlock(Die, Attribute, 0, Block);
408}
409
Devang Patel161b2f42011-04-12 23:21:44 +0000410/// addComplexAddress - Start with the address based on the location provided,
411/// and generate the DWARF information necessary to find the actual variable
412/// given the extra address information encoded in the DIVariable, starting from
413/// the starting location. Add the DWARF information to the die.
414///
Eric Christopherf61dbc12013-06-24 21:07:27 +0000415void CompileUnit::addComplexAddress(const DbgVariable &DV, DIE *Die,
Devang Patel161b2f42011-04-12 23:21:44 +0000416 unsigned Attribute,
417 const MachineLocation &Location) {
Devang Patel161b2f42011-04-12 23:21:44 +0000418 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Eric Christopherf61dbc12013-06-24 21:07:27 +0000419 unsigned N = DV.getNumAddrElements();
Devang Patelc26f5442011-04-28 02:22:40 +0000420 unsigned i = 0;
421 if (Location.isReg()) {
Eric Christopherf61dbc12013-06-24 21:07:27 +0000422 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Devang Patelc26f5442011-04-28 02:22:40 +0000423 // If first address element is OpPlus then emit
424 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Eric Christopherf61dbc12013-06-24 21:07:27 +0000425 addRegisterOffset(Block, Location.getReg(), DV.getAddrElement(1));
Devang Patelc26f5442011-04-28 02:22:40 +0000426 i = 2;
427 } else
428 addRegisterOp(Block, Location.getReg());
429 }
Devang Patel116da2f2011-04-26 19:06:18 +0000430 else
431 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel161b2f42011-04-12 23:21:44 +0000432
Devang Patelc26f5442011-04-28 02:22:40 +0000433 for (;i < N; ++i) {
Eric Christopherf61dbc12013-06-24 21:07:27 +0000434 uint64_t Element = DV.getAddrElement(i);
Devang Patel161b2f42011-04-12 23:21:44 +0000435 if (Element == DIBuilder::OpPlus) {
436 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Eric Christopherf61dbc12013-06-24 21:07:27 +0000437 addUInt(Block, 0, dwarf::DW_FORM_udata, DV.getAddrElement(++i));
Devang Patel161b2f42011-04-12 23:21:44 +0000438 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher50120762012-05-08 18:56:00 +0000439 if (!Location.isReg())
440 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel161b2f42011-04-12 23:21:44 +0000441 } else llvm_unreachable("unknown DIBuilder Opcode");
442 }
443
444 // Now attach the location information to the DIE.
445 addBlock(Die, Attribute, 0, Block);
446}
447
448/* Byref variables, in Blocks, are declared by the programmer as "SomeType
449 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
450 gives the variable VarName either the struct, or a pointer to the struct, as
451 its type. This is necessary for various behind-the-scenes things the
452 compiler needs to do with by-reference variables in Blocks.
453
454 However, as far as the original *programmer* is concerned, the variable
455 should still have type 'SomeType', as originally declared.
456
457 The function getBlockByrefType dives into the __Block_byref_x_VarName
458 struct to find the original type of the variable, which is then assigned to
459 the variable's Debug Information Entry as its real type. So far, so good.
460 However now the debugger will expect the variable VarName to have the type
461 SomeType. So we need the location attribute for the variable to be an
462 expression that explains to the debugger how to navigate through the
463 pointers and struct to find the actual variable of type SomeType.
464
465 The following function does just that. We start by getting
466 the "normal" location for the variable. This will be the location
467 of either the struct __Block_byref_x_VarName or the pointer to the
468 struct __Block_byref_x_VarName.
469
470 The struct will look something like:
471
472 struct __Block_byref_x_VarName {
473 ... <various fields>
474 struct __Block_byref_x_VarName *forwarding;
475 ... <various other fields>
476 SomeType VarName;
477 ... <maybe more fields>
478 };
479
480 If we are given the struct directly (as our starting point) we
481 need to tell the debugger to:
482
483 1). Add the offset of the forwarding field.
484
485 2). Follow that pointer to get the real __Block_byref_x_VarName
486 struct to use (the real one may have been copied onto the heap).
487
488 3). Add the offset for the field VarName, to find the actual variable.
489
490 If we started with a pointer to the struct, then we need to
491 dereference that pointer first, before the other steps.
492 Translating this into DWARF ops, we will need to append the following
493 to the current location description for the variable:
494
495 DW_OP_deref -- optional, if we start with a pointer
496 DW_OP_plus_uconst <forward_fld_offset>
497 DW_OP_deref
498 DW_OP_plus_uconst <varName_fld_offset>
499
500 That is what this function does. */
501
502/// addBlockByrefAddress - Start with the address based on the location
503/// provided, and generate the DWARF information necessary to find the
504/// actual Block variable (navigating the Block struct) based on the
505/// starting location. Add the DWARF information to the die. For
506/// more information, read large comment just above here.
507///
Eric Christopherf61dbc12013-06-24 21:07:27 +0000508void CompileUnit::addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
Devang Patel161b2f42011-04-12 23:21:44 +0000509 unsigned Attribute,
510 const MachineLocation &Location) {
Eric Christopherf61dbc12013-06-24 21:07:27 +0000511 DIType Ty = DV.getType();
Devang Patel161b2f42011-04-12 23:21:44 +0000512 DIType TmpTy = Ty;
513 unsigned Tag = Ty.getTag();
514 bool isPointer = false;
515
Eric Christopherf61dbc12013-06-24 21:07:27 +0000516 StringRef varName = DV.getName();
Devang Patel161b2f42011-04-12 23:21:44 +0000517
518 if (Tag == dwarf::DW_TAG_pointer_type) {
519 DIDerivedType DTy = DIDerivedType(Ty);
520 TmpTy = DTy.getTypeDerivedFrom();
521 isPointer = true;
522 }
523
524 DICompositeType blockStruct = DICompositeType(TmpTy);
525
526 // Find the __forwarding field and the variable field in the __Block_byref
527 // struct.
528 DIArray Fields = blockStruct.getTypeArray();
529 DIDescriptor varField = DIDescriptor();
530 DIDescriptor forwardingField = DIDescriptor();
531
532 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
533 DIDescriptor Element = Fields.getElement(i);
534 DIDerivedType DT = DIDerivedType(Element);
535 StringRef fieldName = DT.getName();
536 if (fieldName == "__forwarding")
537 forwardingField = Element;
538 else if (fieldName == varName)
539 varField = Element;
540 }
541
542 // Get the offsets for the forwarding field and the variable field.
543 unsigned forwardingFieldOffset =
544 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
545 unsigned varFieldOffset =
546 DIDerivedType(varField).getOffsetInBits() >> 3;
547
548 // Decode the original location, and use that as the start of the byref
549 // variable's location.
Devang Patel161b2f42011-04-12 23:21:44 +0000550 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
551
Eric Christophercaba2632012-07-04 02:02:18 +0000552 if (Location.isReg())
553 addRegisterOp(Block, Location.getReg());
554 else
555 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel161b2f42011-04-12 23:21:44 +0000556
557 // If we started with a pointer to the __Block_byref... struct, then
558 // the first thing we need to do is dereference the pointer (DW_OP_deref).
559 if (isPointer)
560 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
561
562 // Next add the offset for the '__forwarding' field:
563 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
564 // adding the offset if it's 0.
565 if (forwardingFieldOffset > 0) {
566 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
567 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
568 }
569
570 // Now dereference the __forwarding field to get to the real __Block_byref
571 // struct: DW_OP_deref.
572 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
573
574 // Now that we've got the real __Block_byref... struct, add the offset
575 // for the variable's field to get to the location of the actual variable:
576 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
577 if (varFieldOffset > 0) {
578 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
579 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
580 }
581
582 // Now attach the location information to the DIE.
583 addBlock(Die, Attribute, 0, Block);
584}
585
Devang Patel4ec14b02011-07-20 21:57:04 +0000586/// isTypeSigned - Return true if the type is signed.
587static bool isTypeSigned(DIType Ty, int *SizeInBits) {
588 if (Ty.isDerivedType())
589 return isTypeSigned(DIDerivedType(Ty).getTypeDerivedFrom(), SizeInBits);
590 if (Ty.isBasicType())
591 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed
592 || DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
593 *SizeInBits = Ty.getSizeInBits();
594 return true;
595 }
596 return false;
597}
598
Devang Patel161b2f42011-04-12 23:21:44 +0000599/// addConstantValue - Add constant value entry in variable DIE.
Devang Patelb58128e2011-05-27 16:45:18 +0000600bool CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
601 DIType Ty) {
David Blaikie4de9d722013-05-10 21:52:07 +0000602 // FIXME: This is a bit conservative/simple - it emits negative values at
603 // their maximum bit width which is a bit unfortunate (& doesn't prefer
604 // udata/sdata over dataN as suggested by the DWARF spec)
Nick Lewycky746cb672011-10-26 22:55:33 +0000605 assert(MO.isImm() && "Invalid machine operand!");
Devang Patel161b2f42011-04-12 23:21:44 +0000606 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel4ec14b02011-07-20 21:57:04 +0000607 int SizeInBits = -1;
608 bool SignedConstant = isTypeSigned(Ty, &SizeInBits);
609 unsigned Form = SignedConstant ? dwarf::DW_FORM_sdata : dwarf::DW_FORM_udata;
610 switch (SizeInBits) {
611 case 8: Form = dwarf::DW_FORM_data1; break;
612 case 16: Form = dwarf::DW_FORM_data2; break;
613 case 32: Form = dwarf::DW_FORM_data4; break;
614 case 64: Form = dwarf::DW_FORM_data8; break;
Devang Patel045c1d42011-05-27 19:13:26 +0000615 default: break;
616 }
Eric Christopher8b4310b2012-11-21 00:34:38 +0000617 SignedConstant ? addSInt(Block, 0, Form, MO.getImm())
Devang Patel4ec14b02011-07-20 21:57:04 +0000618 : addUInt(Block, 0, Form, MO.getImm());
Devang Patel72f0d9c2011-05-27 18:15:52 +0000619
Devang Patel161b2f42011-04-12 23:21:44 +0000620 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
621 return true;
622}
623
624/// addConstantFPValue - Add constant value entry in variable DIE.
625bool CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Nick Lewycky390c40d2011-10-27 06:44:11 +0000626 assert (MO.isFPImm() && "Invalid machine operand!");
Devang Patel161b2f42011-04-12 23:21:44 +0000627 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
628 APFloat FPImm = MO.getFPImm()->getValueAPF();
629
630 // Get the raw data form of the floating point.
631 const APInt FltVal = FPImm.bitcastToAPInt();
632 const char *FltPtr = (const char*)FltVal.getRawData();
633
634 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmow3574eca2012-10-08 16:38:25 +0000635 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel161b2f42011-04-12 23:21:44 +0000636 int Incr = (LittleEndian ? 1 : -1);
637 int Start = (LittleEndian ? 0 : NumBytes - 1);
638 int Stop = (LittleEndian ? NumBytes : -1);
639
640 // Output the constant to DWARF one byte at a time.
641 for (; Start != Stop; Start += Incr)
642 addUInt(Block, 0, dwarf::DW_FORM_data1,
643 (unsigned char)0xFF & FltPtr[Start]);
644
645 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
646 return true;
647}
648
David Blaikie14268412013-01-20 01:18:01 +0000649/// addConstantFPValue - Add constant value entry in variable DIE.
650bool CompileUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) {
651 return addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), false);
652}
653
Devang Patel161b2f42011-04-12 23:21:44 +0000654/// addConstantValue - Add constant value entry in variable DIE.
Devang Patel8594d422011-06-24 20:46:11 +0000655bool CompileUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
Devang Patel161b2f42011-04-12 23:21:44 +0000656 bool Unsigned) {
David Blaikie14268412013-01-20 01:18:01 +0000657 return addConstantValue(Die, CI->getValue(), Unsigned);
658}
659
660// addConstantValue - Add constant value entry in variable DIE.
661bool CompileUnit::addConstantValue(DIE *Die, const APInt &Val,
662 bool Unsigned) {
663 unsigned CIBitWidth = Val.getBitWidth();
Devang Pateld6a81362011-05-28 00:39:18 +0000664 if (CIBitWidth <= 64) {
665 unsigned form = 0;
666 switch (CIBitWidth) {
667 case 8: form = dwarf::DW_FORM_data1; break;
668 case 16: form = dwarf::DW_FORM_data2; break;
669 case 32: form = dwarf::DW_FORM_data4; break;
670 case 64: form = dwarf::DW_FORM_data8; break;
Eric Christopher8b4310b2012-11-21 00:34:38 +0000671 default:
Devang Pateld6a81362011-05-28 00:39:18 +0000672 form = Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata;
673 }
Devang Patel161b2f42011-04-12 23:21:44 +0000674 if (Unsigned)
David Blaikie14268412013-01-20 01:18:01 +0000675 addUInt(Die, dwarf::DW_AT_const_value, form, Val.getZExtValue());
Devang Patel161b2f42011-04-12 23:21:44 +0000676 else
David Blaikie14268412013-01-20 01:18:01 +0000677 addSInt(Die, dwarf::DW_AT_const_value, form, Val.getSExtValue());
Devang Patel161b2f42011-04-12 23:21:44 +0000678 return true;
679 }
680
681 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
682
683 // Get the raw data form of the large APInt.
NAKAMURA Takumic3e48c32011-10-28 14:12:22 +0000684 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel161b2f42011-04-12 23:21:44 +0000685
686 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmow3574eca2012-10-08 16:38:25 +0000687 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel161b2f42011-04-12 23:21:44 +0000688
689 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumic3e48c32011-10-28 14:12:22 +0000690 for (int i = 0; i < NumBytes; i++) {
691 uint8_t c;
692 if (LittleEndian)
693 c = Ptr64[i / 8] >> (8 * (i & 7));
694 else
695 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
696 addUInt(Block, 0, dwarf::DW_FORM_data1, c);
697 }
Devang Patel161b2f42011-04-12 23:21:44 +0000698
699 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
700 return true;
701}
702
Eric Christopher6c3bb942013-04-22 07:47:40 +0000703/// addTemplateParams - Add template parameters into buffer.
Devang Patel161b2f42011-04-12 23:21:44 +0000704void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
705 // Add template parameters.
706 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
707 DIDescriptor Element = TParams.getElement(i);
708 if (Element.isTemplateTypeParameter())
709 Buffer.addChild(getOrCreateTemplateTypeParameterDIE(
710 DITemplateTypeParameter(Element)));
711 else if (Element.isTemplateValueParameter())
712 Buffer.addChild(getOrCreateTemplateValueParameterDIE(
713 DITemplateValueParameter(Element)));
714 }
Devang Patel161b2f42011-04-12 23:21:44 +0000715}
Nick Lewycky746cb672011-10-26 22:55:33 +0000716
Eric Christopher6b6061f2013-01-16 01:22:23 +0000717/// getOrCreateContextDIE - Get context owner's DIE.
718DIE *CompileUnit::getOrCreateContextDIE(DIDescriptor Context) {
719 if (Context.isType())
720 return getOrCreateTypeDIE(DIType(Context));
721 else if (Context.isNameSpace())
722 return getOrCreateNameSpace(DINameSpace(Context));
723 else if (Context.isSubprogram())
724 return getOrCreateSubprogramDIE(DISubprogram(Context));
Eric Christopher6c3bb942013-04-22 07:47:40 +0000725 else
Eric Christopher6b6061f2013-01-16 01:22:23 +0000726 return getDIE(Context);
727}
728
Devang Patel161b2f42011-04-12 23:21:44 +0000729/// addToContextOwner - Add Die into the list of its context owner's children.
730void CompileUnit::addToContextOwner(DIE *Die, DIDescriptor Context) {
Eric Christopher6b6061f2013-01-16 01:22:23 +0000731 if (DIE *ContextDIE = getOrCreateContextDIE(Context))
Devang Patel161b2f42011-04-12 23:21:44 +0000732 ContextDIE->addChild(Die);
733 else
734 addDie(Die);
735}
736
737/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
738/// given DIType.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000739DIE *CompileUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
740 DIType Ty(TyNode);
741 if (!Ty.Verify())
742 return NULL;
Devang Patel161b2f42011-04-12 23:21:44 +0000743 DIE *TyDIE = getDIE(Ty);
744 if (TyDIE)
745 return TyDIE;
746
747 // Create new type.
748 TyDIE = new DIE(dwarf::DW_TAG_base_type);
749 insertDIE(Ty, TyDIE);
750 if (Ty.isBasicType())
751 constructTypeDIE(*TyDIE, DIBasicType(Ty));
752 else if (Ty.isCompositeType())
753 constructTypeDIE(*TyDIE, DICompositeType(Ty));
754 else {
755 assert(Ty.isDerivedType() && "Unknown kind of DIType");
756 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
757 }
Eric Christopher1b3f9192011-11-10 19:52:58 +0000758 // If this is a named finished type then include it in the list of types
759 // for the accelerator tables.
Eric Christopherc36145f2012-01-06 04:35:23 +0000760 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
761 bool IsImplementation = 0;
762 if (Ty.isCompositeType()) {
763 DICompositeType CT(Ty);
Eric Christophere0167892012-01-06 23:03:37 +0000764 // A runtime language of 0 actually means C/C++ and that any
765 // non-negative value is some version of Objective-C/C++.
Eric Christopherc36145f2012-01-06 04:35:23 +0000766 IsImplementation = (CT.getRunTimeLang() == 0) ||
Eric Christophere2dc9332012-02-22 08:46:02 +0000767 CT.isObjcClassComplete();
Eric Christopherc36145f2012-01-06 04:35:23 +0000768 }
Eric Christophere0167892012-01-06 23:03:37 +0000769 unsigned Flags = IsImplementation ?
770 DwarfAccelTable::eTypeFlagClassIsImplementation : 0;
771 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
Eric Christopherc36145f2012-01-06 04:35:23 +0000772 }
Eric Christopher8b4310b2012-11-21 00:34:38 +0000773
Devang Patel161b2f42011-04-12 23:21:44 +0000774 addToContextOwner(TyDIE, Ty.getContext());
775 return TyDIE;
776}
777
778/// addType - Add a new type attribute to the specified entity.
Eric Christopher4d069bf2012-05-22 18:45:24 +0000779void CompileUnit::addType(DIE *Entity, DIType Ty, unsigned Attribute) {
Devang Patel161b2f42011-04-12 23:21:44 +0000780 if (!Ty.Verify())
781 return;
782
783 // Check for pre-existence.
784 DIEEntry *Entry = getDIEEntry(Ty);
785 // If it exists then use the existing value.
786 if (Entry) {
Eric Christopher663e0cf2012-03-28 07:34:31 +0000787 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
Devang Patel161b2f42011-04-12 23:21:44 +0000788 return;
789 }
790
791 // Construct type.
792 DIE *Buffer = getOrCreateTypeDIE(Ty);
793
794 // Set up proxy.
795 Entry = createDIEEntry(Buffer);
796 insertDIEEntry(Ty, Entry);
Eric Christopher663e0cf2012-03-28 07:34:31 +0000797 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
Devang Patele9ae06c2011-05-31 22:56:51 +0000798
799 // If this is a complete composite type then include it in the
800 // list of global types.
Devang Patelc20bdf12011-06-01 00:23:24 +0000801 addGlobalType(Ty);
Devang Patel66658e42011-05-31 23:30:30 +0000802}
803
804/// addGlobalType - Add a new global type to the compile unit.
805///
Devang Patelc20bdf12011-06-01 00:23:24 +0000806void CompileUnit::addGlobalType(DIType Ty) {
Devang Patele9ae06c2011-05-31 22:56:51 +0000807 DIDescriptor Context = Ty.getContext();
Eric Christopher8b4310b2012-11-21 00:34:38 +0000808 if (Ty.isCompositeType() && !Ty.getName().empty() && !Ty.isForwardDecl()
809 && (!Context || Context.isCompileUnit() || Context.isFile()
Devang Patel94c7ddb2011-08-16 22:09:43 +0000810 || Context.isNameSpace()))
Devang Patelc20bdf12011-06-01 00:23:24 +0000811 if (DIEEntry *Entry = getDIEEntry(Ty))
812 GlobalTypes[Ty.getName()] = Entry->getEntry();
Devang Patel161b2f42011-04-12 23:21:44 +0000813}
814
Devang Patel31c5d052011-05-06 16:57:54 +0000815/// addPubTypes - Add type for pubtypes section.
816void CompileUnit::addPubTypes(DISubprogram SP) {
817 DICompositeType SPTy = SP.getType();
818 unsigned SPTag = SPTy.getTag();
819 if (SPTag != dwarf::DW_TAG_subroutine_type)
820 return;
821
822 DIArray Args = SPTy.getTypeArray();
823 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
824 DIType ATy(Args.getElement(i));
825 if (!ATy.Verify())
826 continue;
Devang Patelc20bdf12011-06-01 00:23:24 +0000827 addGlobalType(ATy);
Devang Patel31c5d052011-05-06 16:57:54 +0000828 }
829}
830
Devang Patel161b2f42011-04-12 23:21:44 +0000831/// constructTypeDIE - Construct basic type die from DIBasicType.
832void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
833 // Get core information.
834 StringRef Name = BTy.getName();
Devang Patel161b2f42011-04-12 23:21:44 +0000835 // Add name if not anonymous or intermediate type.
836 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000837 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel734a67c2011-09-14 23:13:28 +0000838
839 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type) {
840 Buffer.setTag(dwarf::DW_TAG_unspecified_type);
841 // Unspecified types has only name, nothing else.
842 return;
843 }
844
845 Buffer.setTag(dwarf::DW_TAG_base_type);
Nick Lewycky746cb672011-10-26 22:55:33 +0000846 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel30d409c2012-02-07 23:33:58 +0000847 BTy.getEncoding());
Devang Patel734a67c2011-09-14 23:13:28 +0000848
Devang Patel161b2f42011-04-12 23:21:44 +0000849 uint64_t Size = BTy.getSizeInBits() >> 3;
850 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
851}
852
853/// constructTypeDIE - Construct derived type die from DIDerivedType.
854void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
855 // Get core information.
856 StringRef Name = DTy.getName();
857 uint64_t Size = DTy.getSizeInBits() >> 3;
858 unsigned Tag = DTy.getTag();
859
860 // FIXME - Workaround for templates.
861 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
862
863 Buffer.setTag(Tag);
864
865 // Map to main type, void will not have a type.
866 DIType FromTy = DTy.getTypeDerivedFrom();
867 addType(&Buffer, FromTy);
868
869 // Add name if not anonymous or intermediate type.
870 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000871 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +0000872
873 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher35f225a2012-02-21 22:25:53 +0000874 if (Size && Tag != dwarf::DW_TAG_pointer_type)
Devang Patel161b2f42011-04-12 23:21:44 +0000875 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
876
David Blaikie62fdfb52013-01-07 05:51:15 +0000877 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
878 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
879 getOrCreateTypeDIE(DTy.getClassType()));
Devang Patel161b2f42011-04-12 23:21:44 +0000880 // Add source line info if available and TyDesc is not a forward declaration.
881 if (!DTy.isForwardDecl())
882 addSourceLine(&Buffer, DTy);
883}
884
885/// constructTypeDIE - Construct type DIE from DICompositeType.
886void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
887 // Get core information.
888 StringRef Name = CTy.getName();
889
890 uint64_t Size = CTy.getSizeInBits() >> 3;
891 unsigned Tag = CTy.getTag();
892 Buffer.setTag(Tag);
893
894 switch (Tag) {
Devang Patel161b2f42011-04-12 23:21:44 +0000895 case dwarf::DW_TAG_array_type:
896 constructArrayTypeDIE(Buffer, &CTy);
897 break;
898 case dwarf::DW_TAG_enumeration_type: {
899 DIArray Elements = CTy.getTypeArray();
900
901 // Add enumerators to enumeration type.
902 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
903 DIE *ElemDie = NULL;
904 DIDescriptor Enum(Elements.getElement(i));
905 if (Enum.isEnumerator()) {
906 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
907 Buffer.addChild(ElemDie);
908 }
909 }
Eric Christopherbb0f6ea2012-05-23 00:09:20 +0000910 DIType DTy = CTy.getTypeDerivedFrom();
911 if (DTy.Verify()) {
912 addType(&Buffer, DTy);
913 addUInt(&Buffer, dwarf::DW_AT_enum_class, dwarf::DW_FORM_flag, 1);
914 }
Devang Patel161b2f42011-04-12 23:21:44 +0000915 }
916 break;
917 case dwarf::DW_TAG_subroutine_type: {
918 // Add return type.
919 DIArray Elements = CTy.getTypeArray();
920 DIDescriptor RTy = Elements.getElement(0);
921 addType(&Buffer, DIType(RTy));
922
923 bool isPrototyped = true;
924 // Add arguments.
925 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
926 DIDescriptor Ty = Elements.getElement(i);
927 if (Ty.isUnspecifiedParameter()) {
928 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters);
929 Buffer.addChild(Arg);
930 isPrototyped = false;
931 } else {
932 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
933 addType(Arg, DIType(Ty));
David Blaikieaaf2e632013-01-29 19:35:24 +0000934 if (DIType(Ty).isArtificial())
935 addFlag(Arg, dwarf::DW_AT_artificial);
Devang Patel161b2f42011-04-12 23:21:44 +0000936 Buffer.addChild(Arg);
937 }
938 }
Eric Christopher8b6fe6b2012-02-22 08:46:21 +0000939 // Add prototype flag if we're dealing with a C language and the
940 // function has been prototyped.
941 if (isPrototyped &&
Eric Christopher4d069bf2012-05-22 18:45:24 +0000942 (Language == dwarf::DW_LANG_C89 ||
943 Language == dwarf::DW_LANG_C99 ||
944 Language == dwarf::DW_LANG_ObjC))
Eric Christopher873cf0a2012-08-24 01:14:27 +0000945 addFlag(&Buffer, dwarf::DW_AT_prototyped);
Devang Patel161b2f42011-04-12 23:21:44 +0000946 }
947 break;
948 case dwarf::DW_TAG_structure_type:
949 case dwarf::DW_TAG_union_type:
950 case dwarf::DW_TAG_class_type: {
951 // Add elements to structure type.
952 DIArray Elements = CTy.getTypeArray();
953
954 // A forward struct declared type may not have elements available.
955 unsigned N = Elements.getNumElements();
956 if (N == 0)
957 break;
958
959 // Add elements to structure type.
960 for (unsigned i = 0; i < N; ++i) {
961 DIDescriptor Element = Elements.getElement(i);
962 DIE *ElemDie = NULL;
963 if (Element.isSubprogram()) {
964 DISubprogram SP(Element);
Devang Pateldbc64af2011-08-15 17:24:54 +0000965 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
Devang Patel161b2f42011-04-12 23:21:44 +0000966 if (SP.isProtected())
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_protected);
969 else if (SP.isPrivate())
Nick Lewycky13aaca52011-12-13 05:09:11 +0000970 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000971 dwarf::DW_ACCESS_private);
Eric Christopher8b4310b2012-11-21 00:34:38 +0000972 else
Nick Lewycky13aaca52011-12-13 05:09:11 +0000973 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000974 dwarf::DW_ACCESS_public);
975 if (SP.isExplicit())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000976 addFlag(ElemDie, dwarf::DW_AT_explicit);
Eric Christopher663e0cf2012-03-28 07:34:31 +0000977 } else if (Element.isDerivedType()) {
Eric Christopher4d069bf2012-05-22 18:45:24 +0000978 DIDerivedType DDTy(Element);
979 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
980 ElemDie = new DIE(dwarf::DW_TAG_friend);
981 addType(ElemDie, DDTy.getTypeDerivedFrom(), dwarf::DW_AT_friend);
Eric Christopher6b6061f2013-01-16 01:22:23 +0000982 } else if (DDTy.isStaticMember())
983 ElemDie = createStaticMemberDIE(DDTy);
984 else
985 ElemDie = createMemberDIE(DDTy);
Eric Christopher663e0cf2012-03-28 07:34:31 +0000986 } else if (Element.isObjCProperty()) {
Devang Patel30d409c2012-02-07 23:33:58 +0000987 DIObjCProperty Property(Element);
988 ElemDie = new DIE(Property.getTag());
989 StringRef PropertyName = Property.getObjCPropertyName();
990 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Eric Christopher4d069bf2012-05-22 18:45:24 +0000991 addType(ElemDie, Property.getType());
992 addSourceLine(ElemDie, Property);
Devang Patel30d409c2012-02-07 23:33:58 +0000993 StringRef GetterName = Property.getObjCPropertyGetterName();
994 if (!GetterName.empty())
995 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
996 StringRef SetterName = Property.getObjCPropertySetterName();
997 if (!SetterName.empty())
998 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
999 unsigned PropertyAttributes = 0;
Devang Patel9e11eb12012-02-04 01:30:32 +00001000 if (Property.isReadOnlyObjCProperty())
1001 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1002 if (Property.isReadWriteObjCProperty())
1003 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1004 if (Property.isAssignObjCProperty())
1005 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1006 if (Property.isRetainObjCProperty())
1007 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1008 if (Property.isCopyObjCProperty())
1009 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1010 if (Property.isNonAtomicObjCProperty())
1011 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1012 if (PropertyAttributes)
Eric Christopher8b4310b2012-11-21 00:34:38 +00001013 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, 0,
Devang Patel9e11eb12012-02-04 01:30:32 +00001014 PropertyAttributes);
Devang Patel6588abf2012-02-06 17:49:43 +00001015
Devang Patel30d409c2012-02-07 23:33:58 +00001016 DIEEntry *Entry = getDIEEntry(Element);
1017 if (!Entry) {
1018 Entry = createDIEEntry(ElemDie);
1019 insertDIEEntry(Element, Entry);
1020 }
Devang Patel9e11eb12012-02-04 01:30:32 +00001021 } else
Devang Patel161b2f42011-04-12 23:21:44 +00001022 continue;
1023 Buffer.addChild(ElemDie);
1024 }
1025
1026 if (CTy.isAppleBlockExtension())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001027 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel161b2f42011-04-12 23:21:44 +00001028
Devang Patel161b2f42011-04-12 23:21:44 +00001029 DICompositeType ContainingType = CTy.getContainingType();
1030 if (DIDescriptor(ContainingType).isCompositeType())
1031 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
1032 getOrCreateTypeDIE(DIType(ContainingType)));
1033 else {
1034 DIDescriptor Context = CTy.getContext();
1035 addToContextOwner(&Buffer, Context);
1036 }
1037
Devang Patel201e6cd2011-05-12 21:29:42 +00001038 if (CTy.isObjcClassComplete())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001039 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patelb11f80e2011-05-12 19:06:16 +00001040
Eric Christopher1a8e8862011-12-16 23:42:42 +00001041 // Add template parameters to a class, structure or union types.
1042 // FIXME: The support isn't in the metadata for this yet.
1043 if (Tag == dwarf::DW_TAG_class_type ||
1044 Tag == dwarf::DW_TAG_structure_type ||
1045 Tag == dwarf::DW_TAG_union_type)
Devang Patel161b2f42011-04-12 23:21:44 +00001046 addTemplateParams(Buffer, CTy.getTemplateParams());
1047
1048 break;
1049 }
1050 default:
1051 break;
1052 }
1053
1054 // Add name if not anonymous or intermediate type.
1055 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001056 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001057
Eric Christopher4a5d8392012-05-22 18:45:18 +00001058 if (Tag == dwarf::DW_TAG_enumeration_type ||
1059 Tag == dwarf::DW_TAG_class_type ||
1060 Tag == dwarf::DW_TAG_structure_type ||
1061 Tag == dwarf::DW_TAG_union_type) {
Devang Patel161b2f42011-04-12 23:21:44 +00001062 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopherfc4199b2012-06-01 00:22:32 +00001063 // TODO: Do we care about size for enum forward declarations?
Devang Patel161b2f42011-04-12 23:21:44 +00001064 if (Size)
1065 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Eric Christopherfc4199b2012-06-01 00:22:32 +00001066 else if (!CTy.isForwardDecl())
Devang Patel161b2f42011-04-12 23:21:44 +00001067 // Add zero size if it is not a forward declaration.
Eric Christopherfc4199b2012-06-01 00:22:32 +00001068 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
1069
1070 // If we're a forward decl, say so.
1071 if (CTy.isForwardDecl())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001072 addFlag(&Buffer, dwarf::DW_AT_declaration);
Devang Patel161b2f42011-04-12 23:21:44 +00001073
1074 // Add source line info if available.
1075 if (!CTy.isForwardDecl())
1076 addSourceLine(&Buffer, CTy);
Eric Christopher89388952012-03-07 00:15:19 +00001077
1078 // No harm in adding the runtime language to the declaration.
1079 unsigned RLang = CTy.getRunTimeLang();
1080 if (RLang)
1081 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
1082 dwarf::DW_FORM_data1, RLang);
Devang Patel161b2f42011-04-12 23:21:44 +00001083 }
1084}
1085
Eric Christopher8b4310b2012-11-21 00:34:38 +00001086/// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
Devang Patel161b2f42011-04-12 23:21:44 +00001087/// for the given DITemplateTypeParameter.
1088DIE *
1089CompileUnit::getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP) {
1090 DIE *ParamDIE = getDIE(TP);
1091 if (ParamDIE)
1092 return ParamDIE;
1093
1094 ParamDIE = new DIE(dwarf::DW_TAG_template_type_parameter);
1095 addType(ParamDIE, TP.getType());
David Blaikiee88939c2013-06-22 18:59:11 +00001096 if (!TP.getName().empty())
1097 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel161b2f42011-04-12 23:21:44 +00001098 return ParamDIE;
1099}
1100
Eric Christopher8b4310b2012-11-21 00:34:38 +00001101/// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE
Devang Patel161b2f42011-04-12 23:21:44 +00001102/// for the given DITemplateValueParameter.
1103DIE *
Eric Christopher4d069bf2012-05-22 18:45:24 +00001104CompileUnit::getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TPV){
Devang Patel161b2f42011-04-12 23:21:44 +00001105 DIE *ParamDIE = getDIE(TPV);
1106 if (ParamDIE)
1107 return ParamDIE;
1108
David Blaikiee88939c2013-06-22 18:59:11 +00001109 ParamDIE = new DIE(TPV.getTag());
Devang Patel161b2f42011-04-12 23:21:44 +00001110 addType(ParamDIE, TPV.getType());
1111 if (!TPV.getName().empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001112 addString(ParamDIE, dwarf::DW_AT_name, TPV.getName());
David Blaikie4de9d722013-05-10 21:52:07 +00001113 if (Value *Val = TPV.getValue()) {
1114 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val))
1115 addConstantValue(ParamDIE, CI, TPV.getType().isUnsignedDIType());
1116 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) {
1117 // For declaration non-type template parameters (such as global values and
1118 // functions)
1119 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1120 addOpAddress(Block, Asm->Mang->getSymbol(GV));
1121 // Emit DW_OP_stack_value to use the address as the immediate value of the
1122 // parameter, rather than a pointer to it.
1123 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1124 addBlock(ParamDIE, dwarf::DW_AT_location, 0, Block);
David Blaikiee88939c2013-06-22 18:59:11 +00001125 } else if (TPV.getTag() == dwarf::DW_TAG_GNU_template_template_param) {
1126 assert(isa<MDString>(Val));
1127 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1128 cast<MDString>(Val)->getString());
1129 } else if (TPV.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
1130 assert(isa<MDNode>(Val));
1131 DIArray A(cast<MDNode>(Val));
1132 addTemplateParams(*ParamDIE, A);
David Blaikie4de9d722013-05-10 21:52:07 +00001133 }
1134 }
1135
Devang Patel161b2f42011-04-12 23:21:44 +00001136 return ParamDIE;
1137}
1138
Devang Patel31c5d052011-05-06 16:57:54 +00001139/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1140DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) {
1141 DIE *NDie = getDIE(NS);
1142 if (NDie)
1143 return NDie;
1144 NDie = new DIE(dwarf::DW_TAG_namespace);
1145 insertDIE(NS, NDie);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001146 if (!NS.getName().empty()) {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001147 addString(NDie, dwarf::DW_AT_name, NS.getName());
Eric Christopher09ac3d82011-11-07 09:24:32 +00001148 addAccelNamespace(NS.getName(), NDie);
1149 } else
1150 addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel31c5d052011-05-06 16:57:54 +00001151 addSourceLine(NDie, NS);
1152 addToContextOwner(NDie, NS.getContext());
1153 return NDie;
1154}
1155
Devang Pateldbc64af2011-08-15 17:24:54 +00001156/// getOrCreateSubprogramDIE - Create new DIE using SP.
1157DIE *CompileUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
1158 DIE *SPDie = getDIE(SP);
1159 if (SPDie)
1160 return SPDie;
1161
Peter Collingbourne27302f02012-05-27 18:36:44 +00001162 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1163
1164 // DW_TAG_inlined_subroutine may refer to this DIE.
1165 insertDIE(SP, SPDie);
1166
Rafael Espindola01b55b42011-11-10 22:34:29 +00001167 DISubprogram SPDecl = SP.getFunctionDeclaration();
1168 DIE *DeclDie = NULL;
1169 if (SPDecl.isSubprogram()) {
1170 DeclDie = getOrCreateSubprogramDIE(SPDecl);
1171 }
1172
Devang Pateldbc64af2011-08-15 17:24:54 +00001173 // Add to context owner.
1174 addToContextOwner(SPDie, SP.getContext());
1175
1176 // Add function template parameters.
1177 addTemplateParams(*SPDie, SP.getTemplateParams());
1178
Eric Christopherc4968752013-05-09 00:42:33 +00001179 // Unfortunately this code needs to stay here instead of below the
1180 // AT_specification code in order to work around a bug in older
1181 // gdbs that requires the linkage name to resolve multiple template
1182 // functions.
1183 // TODO: Remove this set of code when we get rid of the old gdb
1184 // compatibility.
Eric Christopher8d101c32012-03-15 08:19:33 +00001185 StringRef LinkageName = SP.getLinkageName();
Eric Christopherc4968752013-05-09 00:42:33 +00001186 if (!LinkageName.empty() && DD->useDarwinGDBCompat())
1187 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c2b4be2013-06-01 17:51:14 +00001188 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopher8d101c32012-03-15 08:19:33 +00001189
Devang Pateldbc64af2011-08-15 17:24:54 +00001190 // If this DIE is going to refer declaration info using AT_specification
Eric Christopherc4968752013-05-09 00:42:33 +00001191 // then there is no need to add other attributes.
Rafael Espindola01b55b42011-11-10 22:34:29 +00001192 if (DeclDie) {
1193 // Refer function declaration directly.
1194 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1195 DeclDie);
1196
Devang Pateldbc64af2011-08-15 17:24:54 +00001197 return SPDie;
Rafael Espindola01b55b42011-11-10 22:34:29 +00001198 }
Devang Pateldbc64af2011-08-15 17:24:54 +00001199
Eric Christophercbbd5b12012-08-23 22:52:55 +00001200 // Add the linkage name if we have one.
Eric Christopherc4968752013-05-09 00:42:33 +00001201 if (!LinkageName.empty() && !DD->useDarwinGDBCompat())
Eric Christophercbbd5b12012-08-23 22:52:55 +00001202 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c2b4be2013-06-01 17:51:14 +00001203 GlobalValue::getRealLinkageName(LinkageName));
Eric Christophercbbd5b12012-08-23 22:52:55 +00001204
Devang Pateldbc64af2011-08-15 17:24:54 +00001205 // Constructors and operators for anonymous aggregates do not have names.
1206 if (!SP.getName().empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001207 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Pateldbc64af2011-08-15 17:24:54 +00001208
1209 addSourceLine(SPDie, SP);
1210
Eric Christopher8b6fe6b2012-02-22 08:46:21 +00001211 // Add the prototype if we have a prototype and we have a C like
1212 // language.
1213 if (SP.isPrototyped() &&
1214 (Language == dwarf::DW_LANG_C89 ||
1215 Language == dwarf::DW_LANG_C99 ||
1216 Language == dwarf::DW_LANG_ObjC))
Eric Christopher873cf0a2012-08-24 01:14:27 +00001217 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Pateldbc64af2011-08-15 17:24:54 +00001218
1219 // Add Return Type.
1220 DICompositeType SPTy = SP.getType();
David Blaikie3d331842013-05-22 23:22:18 +00001221 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type &&
1222 "the type of a subprogram should be a subroutine");
Devang Pateldbc64af2011-08-15 17:24:54 +00001223
David Blaikie3d331842013-05-22 23:22:18 +00001224 DIArray Args = SPTy.getTypeArray();
1225 addType(SPDie, DIType(Args.getElement(0)));
Devang Pateldbc64af2011-08-15 17:24:54 +00001226
1227 unsigned VK = SP.getVirtuality();
1228 if (VK) {
Nick Lewycky798313d2011-12-14 00:56:07 +00001229 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Devang Pateldbc64af2011-08-15 17:24:54 +00001230 DIEBlock *Block = getDIEBlock();
1231 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1232 addUInt(Block, 0, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1233 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
1234 ContainingTypeMap.insert(std::make_pair(SPDie,
1235 SP.getContainingType()));
1236 }
1237
1238 if (!SP.isDefinition()) {
Eric Christopher873cf0a2012-08-24 01:14:27 +00001239 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher8b4310b2012-11-21 00:34:38 +00001240
Devang Pateldbc64af2011-08-15 17:24:54 +00001241 // Add arguments. Do not add arguments for subprogram definition. They will
1242 // be handled while processing variables.
David Blaikie3d331842013-05-22 23:22:18 +00001243 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1244 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1245 DIType ATy = DIType(Args.getElement(i));
1246 addType(Arg, ATy);
1247 if (ATy.isArtificial())
1248 addFlag(Arg, dwarf::DW_AT_artificial);
1249 SPDie->addChild(Arg);
1250 }
Devang Pateldbc64af2011-08-15 17:24:54 +00001251 }
1252
1253 if (SP.isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001254 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Pateldbc64af2011-08-15 17:24:54 +00001255
1256 if (!SP.isLocalToUnit())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001257 addFlag(SPDie, dwarf::DW_AT_external);
Devang Pateldbc64af2011-08-15 17:24:54 +00001258
1259 if (SP.isOptimized())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001260 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Pateldbc64af2011-08-15 17:24:54 +00001261
1262 if (unsigned isa = Asm->getISAEncoding()) {
1263 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1264 }
1265
1266 return SPDie;
1267}
1268
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001269// Return const expression if value is a GEP to access merged global
1270// constant. e.g.
1271// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1272static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1273 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1274 if (!CE || CE->getNumOperands() != 3 ||
1275 CE->getOpcode() != Instruction::GetElementPtr)
1276 return NULL;
1277
1278 // First operand points to a global struct.
1279 Value *Ptr = CE->getOperand(0);
1280 if (!isa<GlobalValue>(Ptr) ||
1281 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1282 return NULL;
1283
1284 // Second operand is zero.
1285 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1286 if (!CI || !CI->isZero())
1287 return NULL;
1288
1289 // Third operand is offset.
1290 if (!isa<ConstantInt>(CE->getOperand(2)))
1291 return NULL;
1292
1293 return CE;
1294}
1295
1296/// createGlobalVariableDIE - create global variable DIE.
1297void CompileUnit::createGlobalVariableDIE(const MDNode *N) {
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001298 // Check for pre-existence.
Devang Patel49e2f032011-08-18 22:21:50 +00001299 if (getDIE(N))
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001300 return;
1301
Devang Patel49e2f032011-08-18 22:21:50 +00001302 DIGlobalVariable GV(N);
Devang Patel28bea082011-08-18 23:17:55 +00001303 if (!GV.Verify())
1304 return;
1305
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001306 DIDescriptor GVContext = GV.getContext();
Eric Christopher6b6061f2013-01-16 01:22:23 +00001307 DIType GTy = GV.getType();
1308
1309 // If this is a static data member definition, some attributes belong
1310 // to the declaration DIE.
1311 DIE *VariableDIE = NULL;
Manman Ren945e8282013-02-01 23:54:37 +00001312 bool IsStaticMember = false;
Eric Christopher6b6061f2013-01-16 01:22:23 +00001313 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1314 if (SDMDecl.Verify()) {
1315 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1316 // We need the declaration DIE that is in the static member's class.
1317 // But that class might not exist in the DWARF yet.
1318 // Creating the class will create the static member decl DIE.
1319 getOrCreateContextDIE(SDMDecl.getContext());
1320 VariableDIE = getDIE(SDMDecl);
1321 assert(VariableDIE && "Static member decl has no context?");
Manman Ren945e8282013-02-01 23:54:37 +00001322 IsStaticMember = true;
Eric Christopher6b6061f2013-01-16 01:22:23 +00001323 }
1324
1325 // If this is not a static data member definition, create the variable
1326 // DIE and add the initial set of attributes to it.
1327 if (!VariableDIE) {
1328 VariableDIE = new DIE(GV.getTag());
1329 // Add to map.
1330 insertDIE(N, VariableDIE);
1331
1332 // Add name and type.
1333 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1334 addType(VariableDIE, GTy);
1335
1336 // Add scoping info.
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001337 if (!GV.isLocalToUnit()) {
Eric Christopher6b6061f2013-01-16 01:22:23 +00001338 addFlag(VariableDIE, dwarf::DW_AT_external);
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001339 addGlobalName(GV.getName(), VariableDIE);
1340 }
Eric Christopher6b6061f2013-01-16 01:22:23 +00001341
1342 // Add line number info.
1343 addSourceLine(VariableDIE, GV);
1344 // Add to context owner.
1345 addToContextOwner(VariableDIE, GVContext);
1346 }
1347
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001348 // Add location.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001349 bool addToAccelTable = false;
Eric Christopherd61c34b2011-11-11 03:16:32 +00001350 DIE *VariableSpecDIE = NULL;
Eric Christopher6b6061f2013-01-16 01:22:23 +00001351 bool isGlobalVariable = GV.getGlobal() != NULL;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001352 if (isGlobalVariable) {
Eric Christopher09ac3d82011-11-07 09:24:32 +00001353 addToAccelTable = true;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001354 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
David Blaikie59eaa382013-06-28 20:05:11 +00001355 const MCSymbol *Sym = Asm->Mang->getSymbol(GV.getGlobal());
1356 if (GV.getGlobal()->isThreadLocal()) {
1357 // FIXME: Make this work with -gsplit-dwarf.
1358 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
1359 assert((PointerSize == 4 || PointerSize == 8) &&
1360 "Add support for other sizes if necessary");
1361 // Based on GCC's support for TLS:
1362 // 1) Start with a constNu of the appropriate pointer size
1363 addUInt(Block, 0, dwarf::DW_FORM_data1,
1364 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
1365 // 2) containing the (relocated) address of the TLS variable
1366 addLabel(Block, 0, dwarf::DW_FORM_udata,
1367 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym));
1368 // 3) followed by a custom OP to tell the debugger about TLS (presumably)
1369 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_lo_user);
1370 } else
1371 addOpAddress(Block, Sym);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001372 // Do not create specification DIE if context is either compile unit
1373 // or a subprogram.
Devang Patel1dd4e562011-09-21 23:41:11 +00001374 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001375 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
1376 // Create specification DIE.
Eric Christopherd117fbb2011-11-11 01:55:22 +00001377 VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001378 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1379 dwarf::DW_FORM_ref4, VariableDIE);
1380 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopher6b6061f2013-01-16 01:22:23 +00001381 // A static member's declaration is already flagged as such.
1382 if (!SDMDecl.Verify())
1383 addFlag(VariableDIE, dwarf::DW_AT_declaration);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001384 addDie(VariableSpecDIE);
1385 } else {
1386 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001387 }
Eric Christopher6b6061f2013-01-16 01:22:23 +00001388 // Add linkage name.
1389 StringRef LinkageName = GV.getLinkageName();
Eric Christophera8ada252013-02-27 23:49:50 +00001390 if (!LinkageName.empty()) {
Eric Christopher8d45a982013-02-27 23:49:47 +00001391 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1392 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1393 // TAG_variable.
Manman Ren21a08a12013-02-27 23:21:02 +00001394 addString(IsStaticMember && VariableSpecDIE ?
1395 VariableSpecDIE : VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c2b4be2013-06-01 17:51:14 +00001396 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopher8d45a982013-02-27 23:49:47 +00001397 // In compatibility mode with older gdbs we put the linkage name on both
1398 // the TAG_variable DIE and on the TAG_member DIE.
Manman Ren21a08a12013-02-27 23:21:02 +00001399 if (IsStaticMember && VariableSpecDIE && DD->useDarwinGDBCompat())
1400 addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c2b4be2013-06-01 17:51:14 +00001401 GlobalValue::getRealLinkageName(LinkageName));
Manman Ren06df83c2013-02-27 00:02:32 +00001402 }
Eric Christopher8b4310b2012-11-21 00:34:38 +00001403 } else if (const ConstantInt *CI =
Manman Ren945e8282013-02-01 23:54:37 +00001404 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
Adrian Prantl2e892e42013-04-04 22:56:49 +00001405 // AT_const_value was added when the static member was created. To avoid
Manman Ren945e8282013-02-01 23:54:37 +00001406 // emitting AT_const_value multiple times, we only add AT_const_value when
1407 // it is not a static member.
1408 if (!IsStaticMember)
1409 addConstantValue(VariableDIE, CI, GTy.isUnsignedDIType());
1410 } else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) {
Eric Christopher09ac3d82011-11-07 09:24:32 +00001411 addToAccelTable = true;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001412 // GV is a merged global.
1413 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1414 Value *Ptr = CE->getOperand(0);
Eric Christopher0969ddf2013-01-18 22:11:33 +00001415 addOpAddress(Block, Asm->Mang->getSymbol(cast<GlobalValue>(Ptr)));
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001416 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1417 SmallVector<Value*, 3> Idx(CE->op_begin()+1, CE->op_end());
Eric Christopher8b4310b2012-11-21 00:34:38 +00001418 addUInt(Block, 0, dwarf::DW_FORM_udata,
Micah Villmow3574eca2012-10-08 16:38:25 +00001419 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001420 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1421 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1422 }
1423
Eric Christopherd117fbb2011-11-11 01:55:22 +00001424 if (addToAccelTable) {
1425 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1426 addAccelName(GV.getName(), AddrDIE);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001427
Eric Christopherd117fbb2011-11-11 01:55:22 +00001428 // If the linkage name is different than the name, go ahead and output
1429 // that as well into the name table.
1430 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1431 addAccelName(GV.getLinkageName(), AddrDIE);
1432 }
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001433}
1434
Devang Patel161b2f42011-04-12 23:21:44 +00001435/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Eric Christopher4d069bf2012-05-22 18:45:24 +00001436void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR,
1437 DIE *IndexTy) {
Devang Patel161b2f42011-04-12 23:21:44 +00001438 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1439 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patel161b2f42011-04-12 23:21:44 +00001440
Bill Wendling222c2fd2012-12-06 07:38:10 +00001441 // The LowerBound value defines the lower bounds which is typically zero for
1442 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1443 // Count == -1 then the array is unbounded and we do not emit
1444 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1445 // Count == 0, then the array has zero elements in which case we do not emit
1446 // an upper bound.
1447 int64_t LowerBound = SR.getLo();
Bill Wendling6afe4782012-12-06 07:55:19 +00001448 int64_t DefaultLowerBound = getDefaultLowerBound();
Bill Wendling9493dae2012-12-04 21:34:03 +00001449 int64_t Count = SR.getCount();
Devang Patel161b2f42011-04-12 23:21:44 +00001450
Bill Wendling6afe4782012-12-06 07:55:19 +00001451 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
Bill Wendling222c2fd2012-12-06 07:38:10 +00001452 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, LowerBound);
1453
1454 if (Count != -1 && Count != 0)
Bill Wendling9493dae2012-12-04 21:34:03 +00001455 // FIXME: An unbounded array should reference the expression that defines
1456 // the array.
Bill Wendling222c2fd2012-12-06 07:38:10 +00001457 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, LowerBound + Count - 1);
Bill Wendling9493dae2012-12-04 21:34:03 +00001458
Devang Patel161b2f42011-04-12 23:21:44 +00001459 Buffer.addChild(DW_Subrange);
1460}
1461
1462/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
1463void CompileUnit::constructArrayTypeDIE(DIE &Buffer,
1464 DICompositeType *CTy) {
1465 Buffer.setTag(dwarf::DW_TAG_array_type);
Eric Christopher9a1e0e22013-01-08 01:53:52 +00001466 if (CTy->isVector())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001467 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel161b2f42011-04-12 23:21:44 +00001468
1469 // Emit derived type.
1470 addType(&Buffer, CTy->getTypeDerivedFrom());
1471 DIArray Elements = CTy->getTypeArray();
1472
1473 // Get an anonymous type for index type.
Eric Christopher8cab6ed2013-01-04 21:51:53 +00001474 // FIXME: This type should be passed down from the front end
1475 // as different languages may have different sizes for indexes.
Devang Patel161b2f42011-04-12 23:21:44 +00001476 DIE *IdxTy = getIndexTyDie();
1477 if (!IdxTy) {
1478 // Construct an anonymous type for index type.
1479 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Eric Christopher8cab6ed2013-01-04 21:51:53 +00001480 addString(IdxTy, dwarf::DW_AT_name, "int");
Devang Patel161b2f42011-04-12 23:21:44 +00001481 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1482 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1483 dwarf::DW_ATE_signed);
1484 addDie(IdxTy);
1485 setIndexTyDie(IdxTy);
1486 }
1487
1488 // Add subranges to array type.
1489 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1490 DIDescriptor Element = Elements.getElement(i);
1491 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1492 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1493 }
1494}
1495
1496/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
1497DIE *CompileUnit::constructEnumTypeDIE(DIEnumerator ETy) {
1498 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
1499 StringRef Name = ETy.getName();
Nick Lewycky390c40d2011-10-27 06:44:11 +00001500 addString(Enumerator, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001501 int64_t Value = ETy.getEnumValue();
1502 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
1503 return Enumerator;
1504}
1505
Devang Pateldbc64af2011-08-15 17:24:54 +00001506/// constructContainingTypeDIEs - Construct DIEs for types that contain
1507/// vtables.
1508void CompileUnit::constructContainingTypeDIEs() {
1509 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
1510 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1511 DIE *SPDie = CI->first;
1512 const MDNode *N = CI->second;
1513 if (!N) continue;
1514 DIE *NDie = getDIE(N);
1515 if (!NDie) continue;
1516 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1517 }
1518}
1519
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001520/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Eric Christopherf61dbc12013-06-24 21:07:27 +00001521DIE *CompileUnit::constructVariableDIE(DbgVariable *DV,
1522 bool isScopeAbstract) {
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001523 StringRef Name = DV->getName();
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001524
1525 // Translate tag to proper Dwarf tag.
1526 unsigned Tag = DV->getTag();
1527
1528 // Define variable debug information entry.
1529 DIE *VariableDie = new DIE(Tag);
1530 DbgVariable *AbsVar = DV->getAbstractVariable();
1531 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
Manman Ren742671b2013-05-29 17:16:59 +00001532 if (AbsDIE)
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001533 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Manman Ren742671b2013-05-29 17:16:59 +00001534 dwarf::DW_FORM_ref4, AbsDIE);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001535 else {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001536 addString(VariableDie, dwarf::DW_AT_name, Name);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001537 addSourceLine(VariableDie, DV->getVariable());
1538 addType(VariableDie, DV->getType());
1539 }
1540
1541 if (DV->isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001542 addFlag(VariableDie, dwarf::DW_AT_artificial);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001543
1544 if (isScopeAbstract) {
1545 DV->setDIE(VariableDie);
1546 return VariableDie;
1547 }
1548
1549 // Add variable address.
1550
1551 unsigned Offset = DV->getDotDebugLocOffset();
1552 if (Offset != ~0U) {
Eric Christopherafbe0e22013-06-24 21:34:55 +00001553 addLabel(VariableDie, dwarf::DW_AT_location, dwarf::DW_FORM_data4,
1554 Asm->GetTempSymbol("debug_loc", Offset));
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001555 DV->setDIE(VariableDie);
1556 return VariableDie;
1557 }
1558
Eric Christopher8cf5e742011-10-03 15:49:20 +00001559 // Check if variable is described by a DBG_VALUE instruction.
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001560 if (const MachineInstr *DVInsn = DV->getMInsn()) {
1561 bool updated = false;
David Blaikie6d9dbd52013-06-16 20:34:15 +00001562 assert(DVInsn->getNumOperands() == 3);
1563 if (DVInsn->getOperand(0).isReg()) {
1564 const MachineOperand RegOp = DVInsn->getOperand(0);
1565 if (int64_t Offset = DVInsn->getOperand(1).getImm()) {
1566 MachineLocation Location(RegOp.getReg(), Offset);
Eric Christopherf61dbc12013-06-24 21:07:27 +00001567 addVariableAddress(*DV, VariableDie, Location);
David Blaikie6d9dbd52013-06-16 20:34:15 +00001568 } else if (RegOp.getReg())
Eric Christopherf61dbc12013-06-24 21:07:27 +00001569 addVariableAddress(*DV, VariableDie, MachineLocation(RegOp.getReg()));
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001570 updated = true;
David Blaikie6d9dbd52013-06-16 20:34:15 +00001571 } else if (DVInsn->getOperand(0).isImm())
1572 updated =
1573 addConstantValue(VariableDie, DVInsn->getOperand(0), DV->getType());
1574 else if (DVInsn->getOperand(0).isFPImm())
1575 updated = addConstantFPValue(VariableDie, DVInsn->getOperand(0));
1576 else if (DVInsn->getOperand(0).isCImm())
1577 updated = addConstantValue(VariableDie, DVInsn->getOperand(0).getCImm(),
1578 DV->getType().isUnsignedDIType());
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001579 if (!updated) {
1580 // If variableDie is not updated then DBG_VALUE instruction does not
1581 // have valid variable info.
1582 delete VariableDie;
1583 return NULL;
1584 }
1585 DV->setDIE(VariableDie);
1586 return VariableDie;
1587 } else {
1588 // .. else use frame index.
1589 int FI = DV->getFrameIndex();
1590 if (FI != ~0) {
1591 unsigned FrameReg = 0;
1592 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopher8b4310b2012-11-21 00:34:38 +00001593 int Offset =
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001594 TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
1595 MachineLocation Location(FrameReg, Offset);
Eric Christopherf61dbc12013-06-24 21:07:27 +00001596 addVariableAddress(*DV, VariableDie, Location);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001597 }
1598 }
1599
1600 DV->setDIE(VariableDie);
1601 return VariableDie;
1602}
1603
Devang Patel161b2f42011-04-12 23:21:44 +00001604/// createMemberDIE - Create new member DIE.
1605DIE *CompileUnit::createMemberDIE(DIDerivedType DT) {
1606 DIE *MemberDie = new DIE(DT.getTag());
1607 StringRef Name = DT.getName();
1608 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001609 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001610
1611 addType(MemberDie, DT.getTypeDerivedFrom());
1612
1613 addSourceLine(MemberDie, DT);
1614
1615 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
1616 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1617
1618 uint64_t Size = DT.getSizeInBits();
1619 uint64_t FieldSize = DT.getOriginalTypeSize();
1620
1621 if (Size != FieldSize) {
1622 // Handle bitfield.
1623 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1624 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
1625
1626 uint64_t Offset = DT.getOffsetInBits();
1627 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1628 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1629 uint64_t FieldOffset = (HiMark - FieldSize);
1630 Offset -= FieldOffset;
1631
1632 // Maybe we need to work from the other end.
Micah Villmow3574eca2012-10-08 16:38:25 +00001633 if (Asm->getDataLayout().isLittleEndian())
Devang Patel161b2f42011-04-12 23:21:44 +00001634 Offset = FieldSize - (Offset + Size);
1635 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
1636
1637 // Here WD_AT_data_member_location points to the anonymous
1638 // field that includes this bit field.
1639 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
1640
1641 } else
1642 // This is not a bitfield.
1643 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
1644
1645 if (DT.getTag() == dwarf::DW_TAG_inheritance
1646 && DT.isVirtual()) {
1647
1648 // For C++, virtual base classes are not at fixed offset. Use following
1649 // expression to extract appropriate offset from vtable.
1650 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1651
1652 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
1653 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1654 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1655 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1656 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1657 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1658 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1659 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1660
1661 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1662 VBaseLocationDie);
1663 } else
1664 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
1665
1666 if (DT.isProtected())
Nick Lewycky13aaca52011-12-13 05:09:11 +00001667 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001668 dwarf::DW_ACCESS_protected);
1669 else if (DT.isPrivate())
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_private);
1672 // Otherwise C++ member and base classes are considered public.
Eric Christopher8b4310b2012-11-21 00:34:38 +00001673 else
Nick Lewycky13aaca52011-12-13 05:09:11 +00001674 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001675 dwarf::DW_ACCESS_public);
1676 if (DT.isVirtual())
Nick Lewycky798313d2011-12-14 00:56:07 +00001677 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001678 dwarf::DW_VIRTUALITY_virtual);
Devang Patele9db5e22011-04-16 00:11:51 +00001679
1680 // Objective-C properties.
Devang Patel6588abf2012-02-06 17:49:43 +00001681 if (MDNode *PNode = DT.getObjCProperty())
1682 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
Eric Christopher8b4310b2012-11-21 00:34:38 +00001683 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
Devang Patel6588abf2012-02-06 17:49:43 +00001684 PropertyDie);
1685
David Blaikie01bc2b32012-12-13 22:43:07 +00001686 if (DT.isArtificial())
1687 addFlag(MemberDie, dwarf::DW_AT_artificial);
1688
Devang Patel161b2f42011-04-12 23:21:44 +00001689 return MemberDie;
1690}
Eric Christopher6b6061f2013-01-16 01:22:23 +00001691
1692/// createStaticMemberDIE - Create new DIE for C++ static member.
1693DIE *CompileUnit::createStaticMemberDIE(const DIDerivedType DT) {
1694 if (!DT.Verify())
1695 return NULL;
1696
1697 DIE *StaticMemberDIE = new DIE(DT.getTag());
1698 DIType Ty = DT.getTypeDerivedFrom();
1699
1700 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1701 addType(StaticMemberDIE, Ty);
1702 addSourceLine(StaticMemberDIE, DT);
1703 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1704 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1705
1706 // FIXME: We could omit private if the parent is a class_type, and
1707 // public if the parent is something else.
1708 if (DT.isProtected())
1709 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1710 dwarf::DW_ACCESS_protected);
1711 else if (DT.isPrivate())
1712 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1713 dwarf::DW_ACCESS_private);
1714 else
1715 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1716 dwarf::DW_ACCESS_public);
1717
1718 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
1719 addConstantValue(StaticMemberDIE, CI, Ty.isUnsignedDIType());
David Blaikie14268412013-01-20 01:18:01 +00001720 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
1721 addConstantFPValue(StaticMemberDIE, CFP);
Eric Christopher6b6061f2013-01-16 01:22:23 +00001722
1723 insertDIE(DT, StaticMemberDIE);
1724 return StaticMemberDIE;
1725}