blob: 2638b3150f0bd6a8fc8d0fb4b9c9d88c0d2ee43a [file] [log] [blame]
Devang Patel0e821f42011-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 Christopher160522c2012-08-14 05:13:29 +000010// This file contains support for constructing a dwarf compile unit.
Devang Patel0e821f42011-04-12 23:21:44 +000011//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "dwarfdebug"
15
16#include "DwarfCompileUnit.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "DwarfAccelTable.h"
Devang Patel0e821f42011-04-12 23:21:44 +000018#include "DwarfDebug.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/ADT/APFloat.h"
Bill Wendlingf799efd2012-06-29 08:32:07 +000020#include "llvm/DIBuilder.h"
Chandler Carruth9fb823b2013-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 Christopher0a917b72011-11-11 03:16:32 +000025#include "llvm/Support/Debug.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/Support/ErrorHandling.h"
Devang Pateldfd6ec32011-08-15 17:57:41 +000027#include "llvm/Target/Mangler.h"
Devang Patel0e821f42011-04-12 23:21:44 +000028#include "llvm/Target/TargetFrameLowering.h"
29#include "llvm/Target/TargetMachine.h"
David Blaikief2694972013-06-28 20:05:11 +000030#include "llvm/Target/TargetLoweringObjectFile.h"
Devang Patel0e821f42011-04-12 23:21:44 +000031#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel0e821f42011-04-12 23:21:44 +000032
33using namespace llvm;
34
35/// CompileUnit - Compile unit constructor.
Eric Christopherbfceb2f2013-08-26 23:50:38 +000036CompileUnit::CompileUnit(unsigned UID, DIE *D, const MDNode *N, AsmPrinter *A,
37 DwarfDebug *DW, DwarfUnits *DWU)
38 : UniqueID(UID), Node(N), CUDie(D), Asm(A), DD(DW), DU(DWU), IndexTyDie(0),
39 DebugInfoOffset(0) {
Devang Patel0e821f42011-04-12 23:21:44 +000040 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
Eric Christopherc57baee2013-05-08 00:58:51 +000041 insertDIE(N, D);
Devang Patel0e821f42011-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 Wendling3495f9b2012-12-06 07:55:19 +000057/// getDefaultLowerBound - Return the default lower bound for an array. If the
Bill Wendling28fe9e72012-12-06 07:38:10 +000058/// DWARF version doesn't handle the language, return -1.
Bill Wendling3495f9b2012-12-06 07:55:19 +000059int64_t CompileUnit::getDefaultLowerBound() const {
Eric Christopherbfceb2f2013-08-26 23:50:38 +000060 switch (DICompileUnit(Node).getLanguage()) {
Bill Wendling28fe9e72012-12-06 07:38:10 +000061 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 Christopherbb69a272012-08-24 01:14:27 +0000101/// addFlag - Add a flag that is true.
Eric Christopher31b05762013-08-08 01:41:00 +0000102void CompileUnit::addFlag(DIE *Die, uint16_t Attribute) {
Michael Gottesmanc89466f2013-09-04 04:39:38 +0000103 if (DD->getDwarfVersion() >= 4)
Eric Christopherbb69a272012-08-24 01:14:27 +0000104 Die->addValue(Attribute, dwarf::DW_FORM_flag_present,
105 DIEIntegerOne);
106 else
107 addUInt(Die, Attribute, dwarf::DW_FORM_flag, 1);
108}
109
Devang Patel0e821f42011-04-12 23:21:44 +0000110/// addUInt - Add an unsigned integer attribute data and value.
111///
Eric Christopher31b05762013-08-08 01:41:00 +0000112void CompileUnit::addUInt(DIE *Die, uint16_t Attribute,
113 uint16_t Form, uint64_t Integer) {
Devang Patel0e821f42011-04-12 23:21:44 +0000114 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///
Eric Christopher31b05762013-08-08 01:41:00 +0000122void CompileUnit::addSInt(DIE *Die, uint16_t Attribute,
123 uint16_t Form, int64_t Integer) {
Devang Patel0e821f42011-04-12 23:21:44 +0000124 if (!Form) Form = DIEInteger::BestForm(true, Integer);
125 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
126 Die->addValue(Attribute, Form, Value);
127}
128
Nick Lewyckycc64ae12011-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 Christopherfba22602013-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.
Eric Christopher31b05762013-08-08 01:41:00 +0000134void CompileUnit::addString(DIE *Die, uint16_t Attribute, StringRef String) {
Eric Christopher67646432013-07-26 17:02:41 +0000135 DIEValue *Value;
Eric Christopher31b05762013-08-08 01:41:00 +0000136 uint16_t Form;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000137 if (!DD->useSplitDwarf()) {
138 MCSymbol *Symb = DU->getStringPoolEntry(String);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000139 if (Asm->needsRelocationsForDwarfStringPool())
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000140 Value = new (DIEValueAllocator) DIELabel(Symb);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000141 else {
142 MCSymbol *StringPool = DU->getStringPoolSym();
143 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
144 }
Eric Christopher67646432013-07-26 17:02:41 +0000145 Form = dwarf::DW_FORM_strp;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000146 } else {
147 unsigned idx = DU->getStringPoolIndex(String);
Eric Christopher67646432013-07-26 17:02:41 +0000148 Value = new (DIEValueAllocator) DIEInteger(idx);
149 Form = dwarf::DW_FORM_GNU_str_index;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000150 }
Eric Christopher67646432013-07-26 17:02:41 +0000151 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
152 Die->addValue(Attribute, Form, Str);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000153}
154
155/// addLocalString - Add a string attribute data and value. This is guaranteed
156/// to be in the local string pool instead of indirected.
Eric Christopher31b05762013-08-08 01:41:00 +0000157void CompileUnit::addLocalString(DIE *Die, uint16_t Attribute,
Eric Christopher2cbd5762013-01-07 19:32:41 +0000158 StringRef String) {
Eric Christophere698f532012-12-20 21:58:36 +0000159 MCSymbol *Symb = DU->getStringPoolEntry(String);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000160 DIEValue *Value;
161 if (Asm->needsRelocationsForDwarfStringPool())
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000162 Value = new (DIEValueAllocator) DIELabel(Symb);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000163 else {
Eric Christophere698f532012-12-20 21:58:36 +0000164 MCSymbol *StringPool = DU->getStringPoolSym();
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000165 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000166 }
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000167 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000168}
169
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000170/// addExpr - Add a Dwarf expression attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000171///
Eric Christopher31b05762013-08-08 01:41:00 +0000172void CompileUnit::addExpr(DIE *Die, uint16_t Attribute, uint16_t Form,
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000173 const MCExpr *Expr) {
174 DIEValue *Value = new (DIEValueAllocator) DIEExpr(Expr);
Devang Patel0e821f42011-04-12 23:21:44 +0000175 Die->addValue(Attribute, Form, Value);
176}
177
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000178/// addLabel - Add a Dwarf label attribute data and value.
179///
Eric Christopher31b05762013-08-08 01:41:00 +0000180void CompileUnit::addLabel(DIE *Die, uint16_t Attribute, uint16_t Form,
David Blaikief3cd7c52013-06-28 20:05:04 +0000181 const MCSymbol *Label) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000182 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
183 Die->addValue(Attribute, Form, Value);
Richard Mitton21101b32013-09-19 23:21:01 +0000184
185 SymbolCU Entry;
186 Entry.CU = this;
187 Entry.Sym = Label;
188
189 DD->addLabel(Entry);
David Blaikief3cd7c52013-06-28 20:05:04 +0000190}
191
Eric Christopher962c9082013-01-15 23:56:56 +0000192/// addLabelAddress - Add a dwarf label attribute data and value using
193/// DW_FORM_addr or DW_FORM_GNU_addr_index.
194///
Eric Christopher31b05762013-08-08 01:41:00 +0000195void CompileUnit::addLabelAddress(DIE *Die, uint16_t Attribute,
Eric Christopher962c9082013-01-15 23:56:56 +0000196 MCSymbol *Label) {
Richard Mitton21101b32013-09-19 23:21:01 +0000197 if (Label) {
198 SymbolCU Entry;
199 Entry.CU = this;
200 Entry.Sym = Label;
201
202 DD->addLabel(Entry);
203 }
204
Eric Christopher962c9082013-01-15 23:56:56 +0000205 if (!DD->useSplitDwarf()) {
206 if (Label != NULL) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000207 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
Eric Christopher962c9082013-01-15 23:56:56 +0000208 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
209 } else {
210 DIEValue *Value = new (DIEValueAllocator) DIEInteger(0);
211 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
212 }
213 } else {
214 unsigned idx = DU->getAddrPoolIndex(Label);
215 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
216 Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
217 }
218}
219
Eric Christophere9ec2452013-01-18 22:11:33 +0000220/// addOpAddress - Add a dwarf op address data and value using the
221/// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
222///
David Blaikief3cd7c52013-06-28 20:05:04 +0000223void CompileUnit::addOpAddress(DIE *Die, const MCSymbol *Sym) {
Eric Christophere9ec2452013-01-18 22:11:33 +0000224 if (!DD->useSplitDwarf()) {
225 addUInt(Die, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
226 addLabel(Die, 0, dwarf::DW_FORM_udata, Sym);
227 } else {
Eric Christophere9ec2452013-01-18 22:11:33 +0000228 addUInt(Die, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
David Blaikie8466ca82013-07-01 23:55:52 +0000229 addUInt(Die, 0, dwarf::DW_FORM_GNU_addr_index, DU->getAddrPoolIndex(Sym));
Eric Christophere9ec2452013-01-18 22:11:33 +0000230 }
231}
232
Devang Patel0e821f42011-04-12 23:21:44 +0000233/// addDelta - Add a label delta attribute data and value.
234///
Eric Christopher31b05762013-08-08 01:41:00 +0000235void CompileUnit::addDelta(DIE *Die, uint16_t Attribute, uint16_t Form,
Devang Patel0e821f42011-04-12 23:21:44 +0000236 const MCSymbol *Hi, const MCSymbol *Lo) {
237 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
238 Die->addValue(Attribute, Form, Value);
239}
240
241/// addDIEEntry - Add a DIE attribute data and value.
242///
Eric Christopher31b05762013-08-08 01:41:00 +0000243void CompileUnit::addDIEEntry(DIE *Die, uint16_t Attribute, uint16_t Form,
Devang Patel0e821f42011-04-12 23:21:44 +0000244 DIE *Entry) {
245 Die->addValue(Attribute, Form, createDIEEntry(Entry));
246}
247
Devang Patel0e821f42011-04-12 23:21:44 +0000248/// addBlock - Add block data.
249///
Eric Christopher31b05762013-08-08 01:41:00 +0000250void CompileUnit::addBlock(DIE *Die, uint16_t Attribute, uint16_t Form,
Devang Patel0e821f42011-04-12 23:21:44 +0000251 DIEBlock *Block) {
252 Block->ComputeSize(Asm);
253 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
254 Die->addValue(Attribute, Block->BestForm(), Block);
255}
256
257/// addSourceLine - Add location information to specified debug information
258/// entry.
259void CompileUnit::addSourceLine(DIE *Die, DIVariable V) {
260 // Verify variable.
Manman Ren7504ed42013-07-08 18:33:29 +0000261 if (!V.isVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000262 return;
Eric Christopher92331fd2012-11-21 00:34:38 +0000263
Devang Patel0e821f42011-04-12 23:21:44 +0000264 unsigned Line = V.getLineNumber();
265 if (Line == 0)
266 return;
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000267 unsigned FileID = DD->getOrCreateSourceID(V.getContext().getFilename(),
Manman Ren1e427202013-03-07 01:42:00 +0000268 V.getContext().getDirectory(),
269 getUniqueID());
Devang Patel0e821f42011-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, DIGlobalVariable G) {
278 // Verify global variable.
Manman Ren7504ed42013-07-08 18:33:29 +0000279 if (!G.isGlobalVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000280 return;
281
282 unsigned Line = G.getLineNumber();
283 if (Line == 0)
284 return;
Manman Ren1e427202013-03-07 01:42:00 +0000285 unsigned FileID = DD->getOrCreateSourceID(G.getFilename(), G.getDirectory(),
286 getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000287 assert(FileID && "Invalid file id");
288 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
289 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
290}
291
292/// addSourceLine - Add location information to specified debug information
293/// entry.
294void CompileUnit::addSourceLine(DIE *Die, DISubprogram SP) {
295 // Verify subprogram.
Manman Ren7504ed42013-07-08 18:33:29 +0000296 if (!SP.isSubprogram())
Devang Patel0e821f42011-04-12 23:21:44 +0000297 return;
Eric Christopher7734ca22012-03-15 23:55:40 +0000298
Devang Patel0e821f42011-04-12 23:21:44 +0000299 // If the line number is 0, don't add it.
Eric Christopher7734ca22012-03-15 23:55:40 +0000300 unsigned Line = SP.getLineNumber();
301 if (Line == 0)
Devang Patel0e821f42011-04-12 23:21:44 +0000302 return;
303
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000304 unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(),
Manman Ren1e427202013-03-07 01:42:00 +0000305 SP.getDirectory(), getUniqueID());
Devang Patel0e821f42011-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.
313void CompileUnit::addSourceLine(DIE *Die, DIType Ty) {
314 // Verify type.
Manman Ren7504ed42013-07-08 18:33:29 +0000315 if (!Ty.isType())
Devang Patel0e821f42011-04-12 23:21:44 +0000316 return;
317
318 unsigned Line = Ty.getLineNumber();
Eric Christopher7734ca22012-03-15 23:55:40 +0000319 if (Line == 0)
Devang Patel0e821f42011-04-12 23:21:44 +0000320 return;
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000321 unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(),
Manman Ren1e427202013-03-07 01:42:00 +0000322 Ty.getDirectory(), getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000323 assert(FileID && "Invalid file id");
324 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
325 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
326}
327
328/// addSourceLine - Add location information to specified debug information
329/// entry.
Eric Christopher70e1bd82012-03-29 08:42:56 +0000330void CompileUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
331 // Verify type.
Manman Ren7504ed42013-07-08 18:33:29 +0000332 if (!Ty.isObjCProperty())
Eric Christopher70e1bd82012-03-29 08:42:56 +0000333 return;
334
335 unsigned Line = Ty.getLineNumber();
336 if (Line == 0)
337 return;
338 DIFile File = Ty.getFile();
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000339 unsigned FileID = DD->getOrCreateSourceID(File.getFilename(),
Manman Ren1e427202013-03-07 01:42:00 +0000340 File.getDirectory(), getUniqueID());
Eric Christopher70e1bd82012-03-29 08:42:56 +0000341 assert(FileID && "Invalid file id");
342 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
343 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
344}
345
346/// addSourceLine - Add location information to specified debug information
347/// entry.
Devang Patel0e821f42011-04-12 23:21:44 +0000348void CompileUnit::addSourceLine(DIE *Die, DINameSpace NS) {
349 // Verify namespace.
350 if (!NS.Verify())
351 return;
352
353 unsigned Line = NS.getLineNumber();
354 if (Line == 0)
355 return;
356 StringRef FN = NS.getFilename();
357
Manman Ren1e427202013-03-07 01:42:00 +0000358 unsigned FileID = DD->getOrCreateSourceID(FN, NS.getDirectory(),
359 getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000360 assert(FileID && "Invalid file id");
361 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
362 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
363}
364
Eric Christopher92331fd2012-11-21 00:34:38 +0000365/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patel77dc5412011-04-27 22:45:24 +0000366/// DbgVariable based on provided MachineLocation.
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000367void CompileUnit::addVariableAddress(const DbgVariable &DV, DIE *Die,
Devang Patel77dc5412011-04-27 22:45:24 +0000368 MachineLocation Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000369 if (DV.variableHasComplexAddress())
Devang Patel0e821f42011-04-12 23:21:44 +0000370 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000371 else if (DV.isBlockByrefVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000372 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
373 else
David Blaikieea2605d2013-06-20 00:25:24 +0000374 addAddress(Die, dwarf::DW_AT_location, Location,
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000375 DV.getVariable().isIndirect());
Devang Patel0e821f42011-04-12 23:21:44 +0000376}
377
Devang Patelba5fbf12011-04-26 19:06:18 +0000378/// addRegisterOp - Add register operand.
379void CompileUnit::addRegisterOp(DIE *TheDie, unsigned Reg) {
380 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
381 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
382 if (DWReg < 32)
383 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
384 else {
385 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
386 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
387 }
388}
389
390/// addRegisterOffset - Add register offset.
391void CompileUnit::addRegisterOffset(DIE *TheDie, unsigned Reg,
392 int64_t Offset) {
393 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
394 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
395 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
396 if (Reg == TRI->getFrameRegister(*Asm->MF))
397 // If variable offset is based in frame register then use fbreg.
398 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
399 else if (DWReg < 32)
400 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
401 else {
402 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
403 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
404 }
405 addSInt(TheDie, 0, dwarf::DW_FORM_sdata, Offset);
406}
407
408/// addAddress - Add an address attribute to a die based on the location
409/// provided.
Eric Christopher31b05762013-08-08 01:41:00 +0000410void CompileUnit::addAddress(DIE *Die, uint16_t Attribute,
David Blaikieea2605d2013-06-20 00:25:24 +0000411 const MachineLocation &Location, bool Indirect) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000412 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
413
David Blaikieea2605d2013-06-20 00:25:24 +0000414 if (Location.isReg() && !Indirect)
Devang Patelba5fbf12011-04-26 19:06:18 +0000415 addRegisterOp(Block, Location.getReg());
David Blaikieea2605d2013-06-20 00:25:24 +0000416 else {
Devang Patelba5fbf12011-04-26 19:06:18 +0000417 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
David Blaikieea2605d2013-06-20 00:25:24 +0000418 if (Indirect && !Location.isReg()) {
419 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
420 }
421 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000422
423 // Now attach the location information to the DIE.
424 addBlock(Die, Attribute, 0, Block);
425}
426
Devang Patel0e821f42011-04-12 23:21:44 +0000427/// addComplexAddress - Start with the address based on the location provided,
428/// and generate the DWARF information necessary to find the actual variable
429/// given the extra address information encoded in the DIVariable, starting from
430/// the starting location. Add the DWARF information to the die.
431///
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000432void CompileUnit::addComplexAddress(const DbgVariable &DV, DIE *Die,
Eric Christopher31b05762013-08-08 01:41:00 +0000433 uint16_t Attribute,
Devang Patel0e821f42011-04-12 23:21:44 +0000434 const MachineLocation &Location) {
Devang Patel0e821f42011-04-12 23:21:44 +0000435 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000436 unsigned N = DV.getNumAddrElements();
Devang Patel3e021532011-04-28 02:22:40 +0000437 unsigned i = 0;
438 if (Location.isReg()) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000439 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Devang Patel3e021532011-04-28 02:22:40 +0000440 // If first address element is OpPlus then emit
441 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000442 addRegisterOffset(Block, Location.getReg(), DV.getAddrElement(1));
Devang Patel3e021532011-04-28 02:22:40 +0000443 i = 2;
444 } else
445 addRegisterOp(Block, Location.getReg());
446 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000447 else
448 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000449
Devang Patel3e021532011-04-28 02:22:40 +0000450 for (;i < N; ++i) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000451 uint64_t Element = DV.getAddrElement(i);
Devang Patel0e821f42011-04-12 23:21:44 +0000452 if (Element == DIBuilder::OpPlus) {
453 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000454 addUInt(Block, 0, dwarf::DW_FORM_udata, DV.getAddrElement(++i));
Devang Patel0e821f42011-04-12 23:21:44 +0000455 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher4d250522012-05-08 18:56:00 +0000456 if (!Location.isReg())
457 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000458 } else llvm_unreachable("unknown DIBuilder Opcode");
459 }
460
461 // Now attach the location information to the DIE.
462 addBlock(Die, Attribute, 0, Block);
463}
464
465/* Byref variables, in Blocks, are declared by the programmer as "SomeType
466 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
467 gives the variable VarName either the struct, or a pointer to the struct, as
468 its type. This is necessary for various behind-the-scenes things the
469 compiler needs to do with by-reference variables in Blocks.
470
471 However, as far as the original *programmer* is concerned, the variable
472 should still have type 'SomeType', as originally declared.
473
474 The function getBlockByrefType dives into the __Block_byref_x_VarName
475 struct to find the original type of the variable, which is then assigned to
476 the variable's Debug Information Entry as its real type. So far, so good.
477 However now the debugger will expect the variable VarName to have the type
478 SomeType. So we need the location attribute for the variable to be an
479 expression that explains to the debugger how to navigate through the
480 pointers and struct to find the actual variable of type SomeType.
481
482 The following function does just that. We start by getting
483 the "normal" location for the variable. This will be the location
484 of either the struct __Block_byref_x_VarName or the pointer to the
485 struct __Block_byref_x_VarName.
486
487 The struct will look something like:
488
489 struct __Block_byref_x_VarName {
490 ... <various fields>
491 struct __Block_byref_x_VarName *forwarding;
492 ... <various other fields>
493 SomeType VarName;
494 ... <maybe more fields>
495 };
496
497 If we are given the struct directly (as our starting point) we
498 need to tell the debugger to:
499
500 1). Add the offset of the forwarding field.
501
502 2). Follow that pointer to get the real __Block_byref_x_VarName
503 struct to use (the real one may have been copied onto the heap).
504
505 3). Add the offset for the field VarName, to find the actual variable.
506
507 If we started with a pointer to the struct, then we need to
508 dereference that pointer first, before the other steps.
509 Translating this into DWARF ops, we will need to append the following
510 to the current location description for the variable:
511
512 DW_OP_deref -- optional, if we start with a pointer
513 DW_OP_plus_uconst <forward_fld_offset>
514 DW_OP_deref
515 DW_OP_plus_uconst <varName_fld_offset>
516
517 That is what this function does. */
518
519/// addBlockByrefAddress - Start with the address based on the location
520/// provided, and generate the DWARF information necessary to find the
521/// actual Block variable (navigating the Block struct) based on the
522/// starting location. Add the DWARF information to the die. For
523/// more information, read large comment just above here.
524///
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000525void CompileUnit::addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
Eric Christopher31b05762013-08-08 01:41:00 +0000526 uint16_t Attribute,
Devang Patel0e821f42011-04-12 23:21:44 +0000527 const MachineLocation &Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000528 DIType Ty = DV.getType();
Devang Patel0e821f42011-04-12 23:21:44 +0000529 DIType TmpTy = Ty;
Eric Christopher31b05762013-08-08 01:41:00 +0000530 uint16_t Tag = Ty.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +0000531 bool isPointer = false;
532
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000533 StringRef varName = DV.getName();
Devang Patel0e821f42011-04-12 23:21:44 +0000534
535 if (Tag == dwarf::DW_TAG_pointer_type) {
536 DIDerivedType DTy = DIDerivedType(Ty);
537 TmpTy = DTy.getTypeDerivedFrom();
538 isPointer = true;
539 }
540
541 DICompositeType blockStruct = DICompositeType(TmpTy);
542
543 // Find the __forwarding field and the variable field in the __Block_byref
544 // struct.
545 DIArray Fields = blockStruct.getTypeArray();
546 DIDescriptor varField = DIDescriptor();
547 DIDescriptor forwardingField = DIDescriptor();
548
549 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
550 DIDescriptor Element = Fields.getElement(i);
551 DIDerivedType DT = DIDerivedType(Element);
552 StringRef fieldName = DT.getName();
553 if (fieldName == "__forwarding")
554 forwardingField = Element;
555 else if (fieldName == varName)
556 varField = Element;
557 }
558
559 // Get the offsets for the forwarding field and the variable field.
560 unsigned forwardingFieldOffset =
561 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
562 unsigned varFieldOffset =
563 DIDerivedType(varField).getOffsetInBits() >> 3;
564
565 // Decode the original location, and use that as the start of the byref
566 // variable's location.
Devang Patel0e821f42011-04-12 23:21:44 +0000567 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
568
Eric Christopheref9d7102012-07-04 02:02:18 +0000569 if (Location.isReg())
570 addRegisterOp(Block, Location.getReg());
571 else
572 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000573
574 // If we started with a pointer to the __Block_byref... struct, then
575 // the first thing we need to do is dereference the pointer (DW_OP_deref).
576 if (isPointer)
577 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
578
579 // Next add the offset for the '__forwarding' field:
580 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
581 // adding the offset if it's 0.
582 if (forwardingFieldOffset > 0) {
583 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
584 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
585 }
586
587 // Now dereference the __forwarding field to get to the real __Block_byref
588 // struct: DW_OP_deref.
589 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
590
591 // Now that we've got the real __Block_byref... struct, add the offset
592 // for the variable's field to get to the location of the actual variable:
593 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
594 if (varFieldOffset > 0) {
595 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
596 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
597 }
598
599 // Now attach the location information to the DIE.
600 addBlock(Die, Attribute, 0, Block);
601}
602
Devang Patelbcd50a12011-07-20 21:57:04 +0000603/// isTypeSigned - Return true if the type is signed.
604static bool isTypeSigned(DIType Ty, int *SizeInBits) {
605 if (Ty.isDerivedType())
606 return isTypeSigned(DIDerivedType(Ty).getTypeDerivedFrom(), SizeInBits);
607 if (Ty.isBasicType())
608 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed
609 || DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
610 *SizeInBits = Ty.getSizeInBits();
611 return true;
612 }
613 return false;
614}
615
Devang Patel0e821f42011-04-12 23:21:44 +0000616/// addConstantValue - Add constant value entry in variable DIE.
Eric Christopher78fcf4902013-07-03 01:08:30 +0000617void CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
Devang Patel3c6aed22011-05-27 16:45:18 +0000618 DIType Ty) {
David Blaikiea1e813d2013-05-10 21:52:07 +0000619 // FIXME: This is a bit conservative/simple - it emits negative values at
620 // their maximum bit width which is a bit unfortunate (& doesn't prefer
621 // udata/sdata over dataN as suggested by the DWARF spec)
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000622 assert(MO.isImm() && "Invalid machine operand!");
Devang Patelbcd50a12011-07-20 21:57:04 +0000623 int SizeInBits = -1;
624 bool SignedConstant = isTypeSigned(Ty, &SizeInBits);
Eric Christopher9d1daa82013-08-27 23:49:04 +0000625 uint16_t Form;
Devang Patelf1d04702011-05-27 18:15:52 +0000626
Eric Christopher9d1daa82013-08-27 23:49:04 +0000627 // If we're a signed constant definitely use sdata.
628 if (SignedConstant) {
629 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, MO.getImm());
630 return;
631 }
632
633 // Else use data for now unless it's larger than we can deal with.
634 switch (SizeInBits) {
635 case 8:
636 Form = dwarf::DW_FORM_data1;
637 break;
638 case 16:
639 Form = dwarf::DW_FORM_data2;
640 break;
641 case 32:
642 Form = dwarf::DW_FORM_data4;
643 break;
644 case 64:
645 Form = dwarf::DW_FORM_data8;
646 break;
647 default:
648 Form = dwarf::DW_FORM_udata;
649 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
650 return;
651 }
652 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
Devang Patel0e821f42011-04-12 23:21:44 +0000653}
654
655/// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christopher78fcf4902013-07-03 01:08:30 +0000656void CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000657 assert (MO.isFPImm() && "Invalid machine operand!");
Devang Patel0e821f42011-04-12 23:21:44 +0000658 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
659 APFloat FPImm = MO.getFPImm()->getValueAPF();
660
661 // Get the raw data form of the floating point.
662 const APInt FltVal = FPImm.bitcastToAPInt();
663 const char *FltPtr = (const char*)FltVal.getRawData();
664
665 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000666 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000667 int Incr = (LittleEndian ? 1 : -1);
668 int Start = (LittleEndian ? 0 : NumBytes - 1);
669 int Stop = (LittleEndian ? NumBytes : -1);
670
671 // Output the constant to DWARF one byte at a time.
672 for (; Start != Stop; Start += Incr)
673 addUInt(Block, 0, dwarf::DW_FORM_data1,
674 (unsigned char)0xFF & FltPtr[Start]);
675
676 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000677}
678
David Blaikiea39a76e2013-01-20 01:18:01 +0000679/// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christopher78fcf4902013-07-03 01:08:30 +0000680void CompileUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000681 // Pass this down to addConstantValue as an unsigned bag of bits.
682 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
David Blaikiea39a76e2013-01-20 01:18:01 +0000683}
684
Devang Patel0e821f42011-04-12 23:21:44 +0000685/// addConstantValue - Add constant value entry in variable DIE.
Eric Christopher78fcf4902013-07-03 01:08:30 +0000686void CompileUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
Devang Patel0e821f42011-04-12 23:21:44 +0000687 bool Unsigned) {
Eric Christopher78fcf4902013-07-03 01:08:30 +0000688 addConstantValue(Die, CI->getValue(), Unsigned);
David Blaikiea39a76e2013-01-20 01:18:01 +0000689}
690
691// addConstantValue - Add constant value entry in variable DIE.
Eric Christopher78fcf4902013-07-03 01:08:30 +0000692void CompileUnit::addConstantValue(DIE *Die, const APInt &Val, bool Unsigned) {
David Blaikiea39a76e2013-01-20 01:18:01 +0000693 unsigned CIBitWidth = Val.getBitWidth();
Devang Patel8816bbc2011-05-28 00:39:18 +0000694 if (CIBitWidth <= 64) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000695 // If we're a signed constant definitely use sdata.
696 if (!Unsigned) {
697 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
698 Val.getSExtValue());
699 return;
Devang Patel8816bbc2011-05-28 00:39:18 +0000700 }
Eric Christopher9d1daa82013-08-27 23:49:04 +0000701
702 // Else use data for now unless it's larger than we can deal with.
703 uint16_t Form;
704 switch (CIBitWidth) {
705 case 8:
706 Form = dwarf::DW_FORM_data1;
707 break;
708 case 16:
709 Form = dwarf::DW_FORM_data2;
710 break;
711 case 32:
712 Form = dwarf::DW_FORM_data4;
713 break;
714 case 64:
715 Form = dwarf::DW_FORM_data8;
716 break;
717 default:
718 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
719 Val.getZExtValue());
720 return;
721 }
722 addUInt(Die, dwarf::DW_AT_const_value, Form, Val.getZExtValue());
Eric Christopher78fcf4902013-07-03 01:08:30 +0000723 return;
Devang Patel0e821f42011-04-12 23:21:44 +0000724 }
725
726 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
727
728 // Get the raw data form of the large APInt.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000729 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000730
731 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000732 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000733
734 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000735 for (int i = 0; i < NumBytes; i++) {
736 uint8_t c;
737 if (LittleEndian)
738 c = Ptr64[i / 8] >> (8 * (i & 7));
739 else
740 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
741 addUInt(Block, 0, dwarf::DW_FORM_data1, c);
742 }
Devang Patel0e821f42011-04-12 23:21:44 +0000743
744 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000745}
746
Eric Christopher25e35092013-04-22 07:47:40 +0000747/// addTemplateParams - Add template parameters into buffer.
Devang Patel0e821f42011-04-12 23:21:44 +0000748void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
749 // Add template parameters.
750 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
751 DIDescriptor Element = TParams.getElement(i);
752 if (Element.isTemplateTypeParameter())
753 Buffer.addChild(getOrCreateTemplateTypeParameterDIE(
754 DITemplateTypeParameter(Element)));
755 else if (Element.isTemplateValueParameter())
756 Buffer.addChild(getOrCreateTemplateValueParameterDIE(
757 DITemplateValueParameter(Element)));
758 }
Devang Patel0e821f42011-04-12 23:21:44 +0000759}
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000760
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000761/// getOrCreateContextDIE - Get context owner's DIE.
762DIE *CompileUnit::getOrCreateContextDIE(DIDescriptor Context) {
763 if (Context.isType())
764 return getOrCreateTypeDIE(DIType(Context));
765 else if (Context.isNameSpace())
766 return getOrCreateNameSpace(DINameSpace(Context));
767 else if (Context.isSubprogram())
768 return getOrCreateSubprogramDIE(DISubprogram(Context));
Eric Christopher25e35092013-04-22 07:47:40 +0000769 else
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000770 return getDIE(Context);
771}
772
Devang Patel0e821f42011-04-12 23:21:44 +0000773/// addToContextOwner - Add Die into the list of its context owner's children.
774void CompileUnit::addToContextOwner(DIE *Die, DIDescriptor Context) {
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000775 if (DIE *ContextDIE = getOrCreateContextDIE(Context))
Devang Patel0e821f42011-04-12 23:21:44 +0000776 ContextDIE->addChild(Die);
777 else
778 addDie(Die);
779}
780
781/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
782/// given DIType.
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000783DIE *CompileUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
784 DIType Ty(TyNode);
Manman Ren7504ed42013-07-08 18:33:29 +0000785 if (!Ty.isType())
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000786 return NULL;
Devang Patel0e821f42011-04-12 23:21:44 +0000787 DIE *TyDIE = getDIE(Ty);
788 if (TyDIE)
789 return TyDIE;
790
791 // Create new type.
792 TyDIE = new DIE(dwarf::DW_TAG_base_type);
793 insertDIE(Ty, TyDIE);
794 if (Ty.isBasicType())
795 constructTypeDIE(*TyDIE, DIBasicType(Ty));
796 else if (Ty.isCompositeType())
797 constructTypeDIE(*TyDIE, DICompositeType(Ty));
798 else {
799 assert(Ty.isDerivedType() && "Unknown kind of DIType");
800 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
801 }
Eric Christophere2887932011-11-10 19:52:58 +0000802 // If this is a named finished type then include it in the list of types
803 // for the accelerator tables.
Eric Christopher21bde872012-01-06 04:35:23 +0000804 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
805 bool IsImplementation = 0;
806 if (Ty.isCompositeType()) {
807 DICompositeType CT(Ty);
Eric Christopher8ea8e4f2012-01-06 23:03:37 +0000808 // A runtime language of 0 actually means C/C++ and that any
809 // non-negative value is some version of Objective-C/C++.
Eric Christopher21bde872012-01-06 04:35:23 +0000810 IsImplementation = (CT.getRunTimeLang() == 0) ||
Eric Christopheref64b462012-02-22 08:46:02 +0000811 CT.isObjcClassComplete();
Eric Christopher21bde872012-01-06 04:35:23 +0000812 }
Eric Christophercf7289f2013-09-05 18:20:16 +0000813 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
Eric Christopher8ea8e4f2012-01-06 23:03:37 +0000814 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
Eric Christopher21bde872012-01-06 04:35:23 +0000815 }
Eric Christopher92331fd2012-11-21 00:34:38 +0000816
Manman Ren116868e2013-09-09 19:47:11 +0000817 addToContextOwner(TyDIE, DD->resolve(Ty.getContext()));
Devang Patel0e821f42011-04-12 23:21:44 +0000818 return TyDIE;
819}
820
821/// addType - Add a new type attribute to the specified entity.
Eric Christopher31b05762013-08-08 01:41:00 +0000822void CompileUnit::addType(DIE *Entity, DIType Ty, uint16_t Attribute) {
Eric Christopher0df08e22013-08-08 07:40:37 +0000823 assert(Ty && "Trying to add a type that doesn't exist?");
Devang Patel0e821f42011-04-12 23:21:44 +0000824
825 // Check for pre-existence.
826 DIEEntry *Entry = getDIEEntry(Ty);
827 // If it exists then use the existing value.
828 if (Entry) {
Eric Christopher7285c7d2012-03-28 07:34:31 +0000829 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
Devang Patel0e821f42011-04-12 23:21:44 +0000830 return;
831 }
832
833 // Construct type.
834 DIE *Buffer = getOrCreateTypeDIE(Ty);
835
836 // Set up proxy.
837 Entry = createDIEEntry(Buffer);
838 insertDIEEntry(Ty, Entry);
Eric Christopher7285c7d2012-03-28 07:34:31 +0000839 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
Devang Patele9853f22011-05-31 22:56:51 +0000840
841 // If this is a complete composite type then include it in the
842 // list of global types.
Devang Patel562c7422011-06-01 00:23:24 +0000843 addGlobalType(Ty);
Devang Patel1cb8ab42011-05-31 23:30:30 +0000844}
845
846/// addGlobalType - Add a new global type to the compile unit.
847///
Devang Patel562c7422011-06-01 00:23:24 +0000848void CompileUnit::addGlobalType(DIType Ty) {
Manman Ren116868e2013-09-09 19:47:11 +0000849 DIDescriptor Context = DD->resolve(Ty.getContext());
Eric Christopher92331fd2012-11-21 00:34:38 +0000850 if (Ty.isCompositeType() && !Ty.getName().empty() && !Ty.isForwardDecl()
851 && (!Context || Context.isCompileUnit() || Context.isFile()
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000852 || Context.isNameSpace()))
Devang Patel562c7422011-06-01 00:23:24 +0000853 if (DIEEntry *Entry = getDIEEntry(Ty))
854 GlobalTypes[Ty.getName()] = Entry->getEntry();
Devang Patel0e821f42011-04-12 23:21:44 +0000855}
856
Devang Patel17b53272011-05-06 16:57:54 +0000857/// addPubTypes - Add type for pubtypes section.
858void CompileUnit::addPubTypes(DISubprogram SP) {
859 DICompositeType SPTy = SP.getType();
Eric Christopher31b05762013-08-08 01:41:00 +0000860 uint16_t SPTag = SPTy.getTag();
Devang Patel17b53272011-05-06 16:57:54 +0000861 if (SPTag != dwarf::DW_TAG_subroutine_type)
862 return;
863
864 DIArray Args = SPTy.getTypeArray();
865 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
866 DIType ATy(Args.getElement(i));
Manman Ren7504ed42013-07-08 18:33:29 +0000867 if (!ATy.isType())
Devang Patel17b53272011-05-06 16:57:54 +0000868 continue;
Devang Patel562c7422011-06-01 00:23:24 +0000869 addGlobalType(ATy);
Devang Patel17b53272011-05-06 16:57:54 +0000870 }
871}
872
Devang Patel0e821f42011-04-12 23:21:44 +0000873/// constructTypeDIE - Construct basic type die from DIBasicType.
874void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
875 // Get core information.
876 StringRef Name = BTy.getName();
Devang Patel0e821f42011-04-12 23:21:44 +0000877 // Add name if not anonymous or intermediate type.
878 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000879 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel04d6d472011-09-14 23:13:28 +0000880
881 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type) {
882 Buffer.setTag(dwarf::DW_TAG_unspecified_type);
Eric Christopher93ebdd72013-07-09 23:48:45 +0000883 // An unspecified type only has a name attribute.
Devang Patel04d6d472011-09-14 23:13:28 +0000884 return;
885 }
886
887 Buffer.setTag(dwarf::DW_TAG_base_type);
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000888 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Pateld925d1a2012-02-07 23:33:58 +0000889 BTy.getEncoding());
Devang Patel04d6d472011-09-14 23:13:28 +0000890
Devang Patel0e821f42011-04-12 23:21:44 +0000891 uint64_t Size = BTy.getSizeInBits() >> 3;
892 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
893}
894
895/// constructTypeDIE - Construct derived type die from DIDerivedType.
896void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
897 // Get core information.
898 StringRef Name = DTy.getName();
899 uint64_t Size = DTy.getSizeInBits() >> 3;
Eric Christopher31b05762013-08-08 01:41:00 +0000900 uint16_t Tag = DTy.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +0000901
902 // FIXME - Workaround for templates.
903 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
904
905 Buffer.setTag(Tag);
906
907 // Map to main type, void will not have a type.
908 DIType FromTy = DTy.getTypeDerivedFrom();
Eric Christopher0df08e22013-08-08 07:40:37 +0000909 if (FromTy)
910 addType(&Buffer, FromTy);
Devang Patel0e821f42011-04-12 23:21:44 +0000911
912 // Add name if not anonymous or intermediate type.
913 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000914 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +0000915
916 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher85757902012-02-21 22:25:53 +0000917 if (Size && Tag != dwarf::DW_TAG_pointer_type)
Devang Patel0e821f42011-04-12 23:21:44 +0000918 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
919
David Blaikie5d3249b2013-01-07 05:51:15 +0000920 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
921 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
Manman Ren60352032013-09-05 18:48:31 +0000922 getOrCreateTypeDIE(DD->resolve(DTy.getClassType())));
Devang Patel0e821f42011-04-12 23:21:44 +0000923 // Add source line info if available and TyDesc is not a forward declaration.
924 if (!DTy.isForwardDecl())
925 addSourceLine(&Buffer, DTy);
926}
927
Eric Christopher67646432013-07-26 17:02:41 +0000928/// Return true if the type is appropriately scoped to be contained inside
929/// its own type unit.
Manman Ren33796c52013-09-09 19:23:58 +0000930static bool isTypeUnitScoped(DIType Ty, const DwarfDebug *DD) {
Manman Ren116868e2013-09-09 19:47:11 +0000931 DIScope Parent = DD->resolve(Ty.getContext());
Eric Christopher67646432013-07-26 17:02:41 +0000932 while (Parent) {
933 // Don't generate a hash for anything scoped inside a function.
934 if (Parent.isSubprogram())
935 return false;
Manman Rende897a362013-09-09 22:35:23 +0000936 Parent = DD->resolve(Parent.getContext());
Eric Christopher67646432013-07-26 17:02:41 +0000937 }
938 return true;
939}
940
941/// Return true if the type should be split out into a type unit.
Manman Ren33796c52013-09-09 19:23:58 +0000942static bool shouldCreateTypeUnit(DICompositeType CTy, const DwarfDebug *DD) {
Eric Christopher31b05762013-08-08 01:41:00 +0000943 uint16_t Tag = CTy.getTag();
Eric Christopher67646432013-07-26 17:02:41 +0000944
945 switch (Tag) {
946 case dwarf::DW_TAG_structure_type:
947 case dwarf::DW_TAG_union_type:
948 case dwarf::DW_TAG_enumeration_type:
949 case dwarf::DW_TAG_class_type:
950 // If this is a class, structure, union, or enumeration type
951 // that is not a declaration, is a type definition, and not scoped
952 // inside a function then separate this out as a type unit.
Manman Ren33796c52013-09-09 19:23:58 +0000953 if (CTy.isForwardDecl() || !isTypeUnitScoped(CTy, DD))
Eric Christopher67646432013-07-26 17:02:41 +0000954 return 0;
955 return 1;
956 default:
957 return 0;
958 }
959}
960
Devang Patel0e821f42011-04-12 23:21:44 +0000961/// constructTypeDIE - Construct type DIE from DICompositeType.
962void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
963 // Get core information.
964 StringRef Name = CTy.getName();
965
966 uint64_t Size = CTy.getSizeInBits() >> 3;
Eric Christopher31b05762013-08-08 01:41:00 +0000967 uint16_t Tag = CTy.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +0000968 Buffer.setTag(Tag);
969
970 switch (Tag) {
Devang Patel0e821f42011-04-12 23:21:44 +0000971 case dwarf::DW_TAG_array_type:
972 constructArrayTypeDIE(Buffer, &CTy);
973 break;
974 case dwarf::DW_TAG_enumeration_type: {
975 DIArray Elements = CTy.getTypeArray();
976
977 // Add enumerators to enumeration type.
978 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
979 DIE *ElemDie = NULL;
980 DIDescriptor Enum(Elements.getElement(i));
981 if (Enum.isEnumerator()) {
982 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
983 Buffer.addChild(ElemDie);
984 }
985 }
Eric Christopherc4964352012-05-23 00:09:20 +0000986 DIType DTy = CTy.getTypeDerivedFrom();
Eric Christopher49e17b22013-08-08 07:40:42 +0000987 if (DTy) {
Eric Christopherc4964352012-05-23 00:09:20 +0000988 addType(&Buffer, DTy);
989 addUInt(&Buffer, dwarf::DW_AT_enum_class, dwarf::DW_FORM_flag, 1);
990 }
Devang Patel0e821f42011-04-12 23:21:44 +0000991 }
992 break;
993 case dwarf::DW_TAG_subroutine_type: {
Eric Christopher0df08e22013-08-08 07:40:37 +0000994 // Add return type. A void return won't have a type.
Devang Patel0e821f42011-04-12 23:21:44 +0000995 DIArray Elements = CTy.getTypeArray();
996 DIDescriptor RTy = Elements.getElement(0);
Eric Christopher0df08e22013-08-08 07:40:37 +0000997 if (RTy)
998 addType(&Buffer, DIType(RTy));
Devang Patel0e821f42011-04-12 23:21:44 +0000999
1000 bool isPrototyped = true;
1001 // Add arguments.
1002 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1003 DIDescriptor Ty = Elements.getElement(i);
1004 if (Ty.isUnspecifiedParameter()) {
1005 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters);
1006 Buffer.addChild(Arg);
1007 isPrototyped = false;
1008 } else {
1009 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1010 addType(Arg, DIType(Ty));
David Blaikie9a7a7a92013-01-29 19:35:24 +00001011 if (DIType(Ty).isArtificial())
1012 addFlag(Arg, dwarf::DW_AT_artificial);
Devang Patel0e821f42011-04-12 23:21:44 +00001013 Buffer.addChild(Arg);
1014 }
1015 }
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001016 // Add prototype flag if we're dealing with a C language and the
1017 // function has been prototyped.
Eric Christopherbfceb2f2013-08-26 23:50:38 +00001018 uint16_t Language = DICompileUnit(Node).getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001019 if (isPrototyped &&
Eric Christopherd42b92f2012-05-22 18:45:24 +00001020 (Language == dwarf::DW_LANG_C89 ||
1021 Language == dwarf::DW_LANG_C99 ||
1022 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001023 addFlag(&Buffer, dwarf::DW_AT_prototyped);
Devang Patel0e821f42011-04-12 23:21:44 +00001024 }
1025 break;
1026 case dwarf::DW_TAG_structure_type:
1027 case dwarf::DW_TAG_union_type:
1028 case dwarf::DW_TAG_class_type: {
Devang Patel0e821f42011-04-12 23:21:44 +00001029 // Add elements to structure type.
David Blaikiea1ae0e62013-08-01 20:30:22 +00001030 DIArray Elements = CTy.getTypeArray();
1031 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel0e821f42011-04-12 23:21:44 +00001032 DIDescriptor Element = Elements.getElement(i);
1033 DIE *ElemDie = NULL;
1034 if (Element.isSubprogram()) {
1035 DISubprogram SP(Element);
Devang Patel89543712011-08-15 17:24:54 +00001036 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
Devang Patel0e821f42011-04-12 23:21:44 +00001037 if (SP.isProtected())
Nick Lewyckycb918492011-12-13 05:09:11 +00001038 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001039 dwarf::DW_ACCESS_protected);
1040 else if (SP.isPrivate())
Nick Lewyckycb918492011-12-13 05:09:11 +00001041 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001042 dwarf::DW_ACCESS_private);
Eric Christopher92331fd2012-11-21 00:34:38 +00001043 else
Nick Lewyckycb918492011-12-13 05:09:11 +00001044 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001045 dwarf::DW_ACCESS_public);
1046 if (SP.isExplicit())
Eric Christopherbb69a272012-08-24 01:14:27 +00001047 addFlag(ElemDie, dwarf::DW_AT_explicit);
Eric Christopher7285c7d2012-03-28 07:34:31 +00001048 } else if (Element.isDerivedType()) {
Eric Christopherd42b92f2012-05-22 18:45:24 +00001049 DIDerivedType DDTy(Element);
1050 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
1051 ElemDie = new DIE(dwarf::DW_TAG_friend);
1052 addType(ElemDie, DDTy.getTypeDerivedFrom(), dwarf::DW_AT_friend);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001053 } else if (DDTy.isStaticMember())
1054 ElemDie = createStaticMemberDIE(DDTy);
1055 else
1056 ElemDie = createMemberDIE(DDTy);
Eric Christopher7285c7d2012-03-28 07:34:31 +00001057 } else if (Element.isObjCProperty()) {
Devang Pateld925d1a2012-02-07 23:33:58 +00001058 DIObjCProperty Property(Element);
1059 ElemDie = new DIE(Property.getTag());
1060 StringRef PropertyName = Property.getObjCPropertyName();
1061 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Eric Christopherd42b92f2012-05-22 18:45:24 +00001062 addType(ElemDie, Property.getType());
1063 addSourceLine(ElemDie, Property);
Devang Pateld925d1a2012-02-07 23:33:58 +00001064 StringRef GetterName = Property.getObjCPropertyGetterName();
1065 if (!GetterName.empty())
1066 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
1067 StringRef SetterName = Property.getObjCPropertySetterName();
1068 if (!SetterName.empty())
1069 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
1070 unsigned PropertyAttributes = 0;
Devang Patel403e8192012-02-04 01:30:32 +00001071 if (Property.isReadOnlyObjCProperty())
1072 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1073 if (Property.isReadWriteObjCProperty())
1074 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1075 if (Property.isAssignObjCProperty())
1076 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1077 if (Property.isRetainObjCProperty())
1078 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1079 if (Property.isCopyObjCProperty())
1080 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1081 if (Property.isNonAtomicObjCProperty())
1082 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1083 if (PropertyAttributes)
Eric Christopher92331fd2012-11-21 00:34:38 +00001084 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, 0,
Devang Patel403e8192012-02-04 01:30:32 +00001085 PropertyAttributes);
Devang Patel44882172012-02-06 17:49:43 +00001086
Devang Pateld925d1a2012-02-07 23:33:58 +00001087 DIEEntry *Entry = getDIEEntry(Element);
1088 if (!Entry) {
1089 Entry = createDIEEntry(ElemDie);
1090 insertDIEEntry(Element, Entry);
1091 }
Devang Patel403e8192012-02-04 01:30:32 +00001092 } else
Devang Patel0e821f42011-04-12 23:21:44 +00001093 continue;
1094 Buffer.addChild(ElemDie);
1095 }
1096
1097 if (CTy.isAppleBlockExtension())
Eric Christopherbb69a272012-08-24 01:14:27 +00001098 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel0e821f42011-04-12 23:21:44 +00001099
Manman Rend8f798e2013-09-06 18:46:00 +00001100 DICompositeType ContainingType(DD->resolve(CTy.getContainingType()));
Devang Patel0e821f42011-04-12 23:21:44 +00001101 if (DIDescriptor(ContainingType).isCompositeType())
1102 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
1103 getOrCreateTypeDIE(DIType(ContainingType)));
Eric Christopher666dc632013-07-14 21:46:51 +00001104 else
Manman Ren116868e2013-09-09 19:47:11 +00001105 addToContextOwner(&Buffer, DD->resolve(CTy.getContext()));
Devang Patel0e821f42011-04-12 23:21:44 +00001106
Devang Patel12419ae2011-05-12 21:29:42 +00001107 if (CTy.isObjcClassComplete())
Eric Christopherbb69a272012-08-24 01:14:27 +00001108 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patel2409e782011-05-12 19:06:16 +00001109
Eric Christopherda011dd2011-12-16 23:42:42 +00001110 // Add template parameters to a class, structure or union types.
1111 // FIXME: The support isn't in the metadata for this yet.
1112 if (Tag == dwarf::DW_TAG_class_type ||
1113 Tag == dwarf::DW_TAG_structure_type ||
1114 Tag == dwarf::DW_TAG_union_type)
Devang Patel0e821f42011-04-12 23:21:44 +00001115 addTemplateParams(Buffer, CTy.getTemplateParams());
1116
1117 break;
1118 }
1119 default:
1120 break;
1121 }
1122
1123 // Add name if not anonymous or intermediate type.
1124 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001125 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001126
Eric Christopher775cbd22012-05-22 18:45:18 +00001127 if (Tag == dwarf::DW_TAG_enumeration_type ||
1128 Tag == dwarf::DW_TAG_class_type ||
1129 Tag == dwarf::DW_TAG_structure_type ||
1130 Tag == dwarf::DW_TAG_union_type) {
Devang Patel0e821f42011-04-12 23:21:44 +00001131 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher1cf33382012-06-01 00:22:32 +00001132 // TODO: Do we care about size for enum forward declarations?
Devang Patel0e821f42011-04-12 23:21:44 +00001133 if (Size)
1134 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Eric Christopher1cf33382012-06-01 00:22:32 +00001135 else if (!CTy.isForwardDecl())
Devang Patel0e821f42011-04-12 23:21:44 +00001136 // Add zero size if it is not a forward declaration.
Eric Christopher1cf33382012-06-01 00:22:32 +00001137 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
1138
1139 // If we're a forward decl, say so.
1140 if (CTy.isForwardDecl())
Eric Christopherbb69a272012-08-24 01:14:27 +00001141 addFlag(&Buffer, dwarf::DW_AT_declaration);
Devang Patel0e821f42011-04-12 23:21:44 +00001142
1143 // Add source line info if available.
1144 if (!CTy.isForwardDecl())
1145 addSourceLine(&Buffer, CTy);
Eric Christopher54cf8ff2012-03-07 00:15:19 +00001146
1147 // No harm in adding the runtime language to the declaration.
1148 unsigned RLang = CTy.getRunTimeLang();
1149 if (RLang)
1150 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
1151 dwarf::DW_FORM_data1, RLang);
Devang Patel0e821f42011-04-12 23:21:44 +00001152 }
Eric Christopher67646432013-07-26 17:02:41 +00001153 // If this is a type applicable to a type unit it then add it to the
1154 // list of types we'll compute a hash for later.
Manman Ren33796c52013-09-09 19:23:58 +00001155 if (shouldCreateTypeUnit(CTy, DD))
Eric Christopher67646432013-07-26 17:02:41 +00001156 DD->addTypeUnitType(&Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001157}
1158
Eric Christopher92331fd2012-11-21 00:34:38 +00001159/// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
Devang Patel0e821f42011-04-12 23:21:44 +00001160/// for the given DITemplateTypeParameter.
1161DIE *
1162CompileUnit::getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP) {
1163 DIE *ParamDIE = getDIE(TP);
1164 if (ParamDIE)
1165 return ParamDIE;
1166
1167 ParamDIE = new DIE(dwarf::DW_TAG_template_type_parameter);
Eric Christopher056b6472013-08-08 08:09:43 +00001168 // Add the type if it exists, it could be void and therefore no type.
1169 if (TP.getType())
1170 addType(ParamDIE, TP.getType());
David Blaikie2b380232013-06-22 18:59:11 +00001171 if (!TP.getName().empty())
1172 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel0e821f42011-04-12 23:21:44 +00001173 return ParamDIE;
1174}
1175
Eric Christopher92331fd2012-11-21 00:34:38 +00001176/// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE
Devang Patel0e821f42011-04-12 23:21:44 +00001177/// for the given DITemplateValueParameter.
1178DIE *
Eric Christopherafb2c412013-08-08 07:40:31 +00001179CompileUnit::getOrCreateTemplateValueParameterDIE(DITemplateValueParameter VP) {
1180 DIE *ParamDIE = getDIE(VP);
Devang Patel0e821f42011-04-12 23:21:44 +00001181 if (ParamDIE)
1182 return ParamDIE;
1183
Eric Christopherafb2c412013-08-08 07:40:31 +00001184 ParamDIE = new DIE(VP.getTag());
Eric Christopher0df08e22013-08-08 07:40:37 +00001185
1186 // Add the type if there is one, template template and template parameter
1187 // packs will not have a type.
1188 if (VP.getType())
1189 addType(ParamDIE, VP.getType());
Eric Christopherafb2c412013-08-08 07:40:31 +00001190 if (!VP.getName().empty())
1191 addString(ParamDIE, dwarf::DW_AT_name, VP.getName());
1192 if (Value *Val = VP.getValue()) {
David Blaikiea1e813d2013-05-10 21:52:07 +00001193 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val))
Eric Christopherafb2c412013-08-08 07:40:31 +00001194 addConstantValue(ParamDIE, CI, VP.getType().isUnsignedDIType());
David Blaikiea1e813d2013-05-10 21:52:07 +00001195 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) {
1196 // For declaration non-type template parameters (such as global values and
1197 // functions)
1198 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1199 addOpAddress(Block, Asm->Mang->getSymbol(GV));
1200 // Emit DW_OP_stack_value to use the address as the immediate value of the
1201 // parameter, rather than a pointer to it.
1202 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1203 addBlock(ParamDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopherafb2c412013-08-08 07:40:31 +00001204 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) {
David Blaikie2b380232013-06-22 18:59:11 +00001205 assert(isa<MDString>(Val));
1206 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1207 cast<MDString>(Val)->getString());
Eric Christopherafb2c412013-08-08 07:40:31 +00001208 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
David Blaikie2b380232013-06-22 18:59:11 +00001209 assert(isa<MDNode>(Val));
1210 DIArray A(cast<MDNode>(Val));
1211 addTemplateParams(*ParamDIE, A);
David Blaikiea1e813d2013-05-10 21:52:07 +00001212 }
1213 }
1214
Devang Patel0e821f42011-04-12 23:21:44 +00001215 return ParamDIE;
1216}
1217
Devang Patel17b53272011-05-06 16:57:54 +00001218/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1219DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) {
1220 DIE *NDie = getDIE(NS);
1221 if (NDie)
1222 return NDie;
1223 NDie = new DIE(dwarf::DW_TAG_namespace);
1224 insertDIE(NS, NDie);
Eric Christopher4996c702011-11-07 09:24:32 +00001225 if (!NS.getName().empty()) {
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001226 addString(NDie, dwarf::DW_AT_name, NS.getName());
Eric Christopher4996c702011-11-07 09:24:32 +00001227 addAccelNamespace(NS.getName(), NDie);
1228 } else
1229 addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel17b53272011-05-06 16:57:54 +00001230 addSourceLine(NDie, NS);
1231 addToContextOwner(NDie, NS.getContext());
1232 return NDie;
1233}
1234
Devang Patel89543712011-08-15 17:24:54 +00001235/// getOrCreateSubprogramDIE - Create new DIE using SP.
1236DIE *CompileUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
1237 DIE *SPDie = getDIE(SP);
1238 if (SPDie)
1239 return SPDie;
1240
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001241 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1242
1243 // DW_TAG_inlined_subroutine may refer to this DIE.
1244 insertDIE(SP, SPDie);
1245
Rafael Espindola79278362011-11-10 22:34:29 +00001246 DISubprogram SPDecl = SP.getFunctionDeclaration();
1247 DIE *DeclDie = NULL;
1248 if (SPDecl.isSubprogram()) {
1249 DeclDie = getOrCreateSubprogramDIE(SPDecl);
1250 }
1251
Devang Patel89543712011-08-15 17:24:54 +00001252 // Add to context owner.
1253 addToContextOwner(SPDie, SP.getContext());
1254
1255 // Add function template parameters.
1256 addTemplateParams(*SPDie, SP.getTemplateParams());
1257
Devang Patel89543712011-08-15 17:24:54 +00001258 // If this DIE is going to refer declaration info using AT_specification
Eric Christopherf20ff972013-05-09 00:42:33 +00001259 // then there is no need to add other attributes.
Rafael Espindola79278362011-11-10 22:34:29 +00001260 if (DeclDie) {
1261 // Refer function declaration directly.
1262 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1263 DeclDie);
1264
Devang Patel89543712011-08-15 17:24:54 +00001265 return SPDie;
Rafael Espindola79278362011-11-10 22:34:29 +00001266 }
Devang Patel89543712011-08-15 17:24:54 +00001267
Eric Christopheracb71152012-08-23 22:52:55 +00001268 // Add the linkage name if we have one.
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001269 StringRef LinkageName = SP.getLinkageName();
1270 if (!LinkageName.empty())
Eric Christopheracb71152012-08-23 22:52:55 +00001271 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001272 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopheracb71152012-08-23 22:52:55 +00001273
Devang Patel89543712011-08-15 17:24:54 +00001274 // Constructors and operators for anonymous aggregates do not have names.
1275 if (!SP.getName().empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001276 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Patel89543712011-08-15 17:24:54 +00001277
1278 addSourceLine(SPDie, SP);
1279
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001280 // Add the prototype if we have a prototype and we have a C like
1281 // language.
Eric Christopherbfceb2f2013-08-26 23:50:38 +00001282 uint16_t Language = DICompileUnit(Node).getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001283 if (SP.isPrototyped() &&
1284 (Language == dwarf::DW_LANG_C89 ||
1285 Language == dwarf::DW_LANG_C99 ||
1286 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001287 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Patel89543712011-08-15 17:24:54 +00001288
Eric Christopher0df08e22013-08-08 07:40:37 +00001289 // Add Return Type. A void return type will not have a type.
Devang Patel89543712011-08-15 17:24:54 +00001290 DICompositeType SPTy = SP.getType();
David Blaikie5174c842013-05-22 23:22:18 +00001291 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type &&
1292 "the type of a subprogram should be a subroutine");
Devang Patel89543712011-08-15 17:24:54 +00001293
David Blaikie5174c842013-05-22 23:22:18 +00001294 DIArray Args = SPTy.getTypeArray();
Eric Christopher0df08e22013-08-08 07:40:37 +00001295 if (Args.getElement(0))
1296 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel89543712011-08-15 17:24:54 +00001297
1298 unsigned VK = SP.getVirtuality();
1299 if (VK) {
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001300 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Devang Patel89543712011-08-15 17:24:54 +00001301 DIEBlock *Block = getDIEBlock();
1302 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1303 addUInt(Block, 0, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1304 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
1305 ContainingTypeMap.insert(std::make_pair(SPDie,
Manman Renc4ae9b32013-09-07 00:04:05 +00001306 DD->resolve(SP.getContainingType())));
Devang Patel89543712011-08-15 17:24:54 +00001307 }
1308
1309 if (!SP.isDefinition()) {
Eric Christopherbb69a272012-08-24 01:14:27 +00001310 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher92331fd2012-11-21 00:34:38 +00001311
Devang Patel89543712011-08-15 17:24:54 +00001312 // Add arguments. Do not add arguments for subprogram definition. They will
1313 // be handled while processing variables.
David Blaikie5174c842013-05-22 23:22:18 +00001314 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1315 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1316 DIType ATy = DIType(Args.getElement(i));
1317 addType(Arg, ATy);
1318 if (ATy.isArtificial())
1319 addFlag(Arg, dwarf::DW_AT_artificial);
1320 SPDie->addChild(Arg);
1321 }
Devang Patel89543712011-08-15 17:24:54 +00001322 }
1323
1324 if (SP.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001325 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Patel89543712011-08-15 17:24:54 +00001326
1327 if (!SP.isLocalToUnit())
Eric Christopherbb69a272012-08-24 01:14:27 +00001328 addFlag(SPDie, dwarf::DW_AT_external);
Devang Patel89543712011-08-15 17:24:54 +00001329
1330 if (SP.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +00001331 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Patel89543712011-08-15 17:24:54 +00001332
1333 if (unsigned isa = Asm->getISAEncoding()) {
1334 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1335 }
1336
1337 return SPDie;
1338}
1339
Devang Pateldfd6ec32011-08-15 17:57:41 +00001340// Return const expression if value is a GEP to access merged global
1341// constant. e.g.
1342// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1343static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1344 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1345 if (!CE || CE->getNumOperands() != 3 ||
1346 CE->getOpcode() != Instruction::GetElementPtr)
1347 return NULL;
1348
1349 // First operand points to a global struct.
1350 Value *Ptr = CE->getOperand(0);
1351 if (!isa<GlobalValue>(Ptr) ||
1352 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1353 return NULL;
1354
1355 // Second operand is zero.
1356 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1357 if (!CI || !CI->isZero())
1358 return NULL;
1359
1360 // Third operand is offset.
1361 if (!isa<ConstantInt>(CE->getOperand(2)))
1362 return NULL;
1363
1364 return CE;
1365}
1366
1367/// createGlobalVariableDIE - create global variable DIE.
1368void CompileUnit::createGlobalVariableDIE(const MDNode *N) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001369 // Check for pre-existence.
Devang Patela6576a12011-08-18 22:21:50 +00001370 if (getDIE(N))
Devang Pateldfd6ec32011-08-15 17:57:41 +00001371 return;
1372
Devang Patela6576a12011-08-18 22:21:50 +00001373 DIGlobalVariable GV(N);
Manman Ren7504ed42013-07-08 18:33:29 +00001374 if (!GV.isGlobalVariable())
Devang Patel0ecbcbd2011-08-18 23:17:55 +00001375 return;
1376
Devang Pateldfd6ec32011-08-15 17:57:41 +00001377 DIDescriptor GVContext = GV.getContext();
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001378 DIType GTy = GV.getType();
1379
1380 // If this is a static data member definition, some attributes belong
1381 // to the declaration DIE.
1382 DIE *VariableDIE = NULL;
Manman Rene697d3c2013-02-01 23:54:37 +00001383 bool IsStaticMember = false;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001384 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1385 if (SDMDecl.Verify()) {
1386 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1387 // We need the declaration DIE that is in the static member's class.
1388 // But that class might not exist in the DWARF yet.
1389 // Creating the class will create the static member decl DIE.
Manman Ren116868e2013-09-09 19:47:11 +00001390 getOrCreateContextDIE(DD->resolve(SDMDecl.getContext()));
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001391 VariableDIE = getDIE(SDMDecl);
1392 assert(VariableDIE && "Static member decl has no context?");
Manman Rene697d3c2013-02-01 23:54:37 +00001393 IsStaticMember = true;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001394 }
1395
1396 // If this is not a static data member definition, create the variable
1397 // DIE and add the initial set of attributes to it.
1398 if (!VariableDIE) {
1399 VariableDIE = new DIE(GV.getTag());
1400 // Add to map.
1401 insertDIE(N, VariableDIE);
1402
1403 // Add name and type.
1404 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1405 addType(VariableDIE, GTy);
1406
1407 // Add scoping info.
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00001408 if (!GV.isLocalToUnit()) {
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001409 addFlag(VariableDIE, dwarf::DW_AT_external);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00001410 addGlobalName(GV.getName(), VariableDIE);
1411 }
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001412
1413 // Add line number info.
1414 addSourceLine(VariableDIE, GV);
1415 // Add to context owner.
1416 addToContextOwner(VariableDIE, GVContext);
1417 }
1418
Devang Pateldfd6ec32011-08-15 17:57:41 +00001419 // Add location.
Eric Christopher4996c702011-11-07 09:24:32 +00001420 bool addToAccelTable = false;
Eric Christopher0a917b72011-11-11 03:16:32 +00001421 DIE *VariableSpecDIE = NULL;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001422 bool isGlobalVariable = GV.getGlobal() != NULL;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001423 if (isGlobalVariable) {
Eric Christopher4996c702011-11-07 09:24:32 +00001424 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001425 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
David Blaikief2694972013-06-28 20:05:11 +00001426 const MCSymbol *Sym = Asm->Mang->getSymbol(GV.getGlobal());
1427 if (GV.getGlobal()->isThreadLocal()) {
1428 // FIXME: Make this work with -gsplit-dwarf.
1429 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
1430 assert((PointerSize == 4 || PointerSize == 8) &&
1431 "Add support for other sizes if necessary");
Ulrich Weigand2b6fc8d2013-07-02 18:47:09 +00001432 const MCExpr *Expr =
David Blaikie8466ca82013-07-01 23:55:52 +00001433 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym);
David Blaikief2694972013-06-28 20:05:11 +00001434 // Based on GCC's support for TLS:
David Blaikie8466ca82013-07-01 23:55:52 +00001435 if (!DD->useSplitDwarf()) {
1436 // 1) Start with a constNu of the appropriate pointer size
1437 addUInt(Block, 0, dwarf::DW_FORM_data1,
1438 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
1439 // 2) containing the (relocated) address of the TLS variable
Ulrich Weigand2b6fc8d2013-07-02 18:47:09 +00001440 addExpr(Block, 0, dwarf::DW_FORM_udata, Expr);
David Blaikie8466ca82013-07-01 23:55:52 +00001441 } else {
1442 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
Ulrich Weigand2b6fc8d2013-07-02 18:47:09 +00001443 addUInt(Block, 0, dwarf::DW_FORM_udata, DU->getAddrPoolIndex(Expr));
David Blaikie8466ca82013-07-01 23:55:52 +00001444 }
David Blaikief2694972013-06-28 20:05:11 +00001445 // 3) followed by a custom OP to tell the debugger about TLS (presumably)
1446 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_lo_user);
1447 } else
1448 addOpAddress(Block, Sym);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001449 // Do not create specification DIE if context is either compile unit
1450 // or a subprogram.
Devang Patel5e6b65c2011-09-21 23:41:11 +00001451 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Manman Ren3eb9dff2013-09-09 19:05:21 +00001452 !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001453 // Create specification DIE.
Eric Christopherc12c2112011-11-11 01:55:22 +00001454 VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001455 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1456 dwarf::DW_FORM_ref4, VariableDIE);
1457 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001458 // A static member's declaration is already flagged as such.
1459 if (!SDMDecl.Verify())
1460 addFlag(VariableDIE, dwarf::DW_AT_declaration);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001461 addDie(VariableSpecDIE);
1462 } else {
1463 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopher4996c702011-11-07 09:24:32 +00001464 }
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001465 // Add the linkage name.
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001466 StringRef LinkageName = GV.getLinkageName();
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001467 if (!LinkageName.empty())
Eric Christopher3f79b8c2013-02-27 23:49:47 +00001468 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1469 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1470 // TAG_variable.
Manman Ren584e4c02013-02-27 23:21:02 +00001471 addString(IsStaticMember && VariableSpecDIE ?
1472 VariableSpecDIE : VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001473 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopher92331fd2012-11-21 00:34:38 +00001474 } else if (const ConstantInt *CI =
Manman Rene697d3c2013-02-01 23:54:37 +00001475 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
Adrian Prantl322f41d2013-04-04 22:56:49 +00001476 // AT_const_value was added when the static member was created. To avoid
Manman Rene697d3c2013-02-01 23:54:37 +00001477 // emitting AT_const_value multiple times, we only add AT_const_value when
1478 // it is not a static member.
1479 if (!IsStaticMember)
1480 addConstantValue(VariableDIE, CI, GTy.isUnsignedDIType());
1481 } else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) {
Eric Christopher4996c702011-11-07 09:24:32 +00001482 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001483 // GV is a merged global.
1484 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1485 Value *Ptr = CE->getOperand(0);
Eric Christophere9ec2452013-01-18 22:11:33 +00001486 addOpAddress(Block, Asm->Mang->getSymbol(cast<GlobalValue>(Ptr)));
Devang Pateldfd6ec32011-08-15 17:57:41 +00001487 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1488 SmallVector<Value*, 3> Idx(CE->op_begin()+1, CE->op_end());
Eric Christopher92331fd2012-11-21 00:34:38 +00001489 addUInt(Block, 0, dwarf::DW_FORM_udata,
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001490 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
Devang Pateldfd6ec32011-08-15 17:57:41 +00001491 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1492 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1493 }
1494
Eric Christopherc12c2112011-11-11 01:55:22 +00001495 if (addToAccelTable) {
1496 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1497 addAccelName(GV.getName(), AddrDIE);
Eric Christopher4996c702011-11-07 09:24:32 +00001498
Eric Christopherc12c2112011-11-11 01:55:22 +00001499 // If the linkage name is different than the name, go ahead and output
1500 // that as well into the name table.
1501 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1502 addAccelName(GV.getLinkageName(), AddrDIE);
1503 }
Devang Pateldfd6ec32011-08-15 17:57:41 +00001504}
1505
Devang Patel0e821f42011-04-12 23:21:44 +00001506/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Eric Christopherd42b92f2012-05-22 18:45:24 +00001507void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR,
1508 DIE *IndexTy) {
Devang Patel0e821f42011-04-12 23:21:44 +00001509 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1510 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001511
Bill Wendling28fe9e72012-12-06 07:38:10 +00001512 // The LowerBound value defines the lower bounds which is typically zero for
1513 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1514 // Count == -1 then the array is unbounded and we do not emit
1515 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1516 // Count == 0, then the array has zero elements in which case we do not emit
1517 // an upper bound.
1518 int64_t LowerBound = SR.getLo();
Bill Wendling3495f9b2012-12-06 07:55:19 +00001519 int64_t DefaultLowerBound = getDefaultLowerBound();
Bill Wendlingd7767122012-12-04 21:34:03 +00001520 int64_t Count = SR.getCount();
Devang Patel0e821f42011-04-12 23:21:44 +00001521
Bill Wendling3495f9b2012-12-06 07:55:19 +00001522 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
Bill Wendling28fe9e72012-12-06 07:38:10 +00001523 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, LowerBound);
1524
1525 if (Count != -1 && Count != 0)
Bill Wendlingd7767122012-12-04 21:34:03 +00001526 // FIXME: An unbounded array should reference the expression that defines
1527 // the array.
Bill Wendling28fe9e72012-12-06 07:38:10 +00001528 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, LowerBound + Count - 1);
Bill Wendlingd7767122012-12-04 21:34:03 +00001529
Devang Patel0e821f42011-04-12 23:21:44 +00001530 Buffer.addChild(DW_Subrange);
1531}
1532
1533/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
1534void CompileUnit::constructArrayTypeDIE(DIE &Buffer,
1535 DICompositeType *CTy) {
1536 Buffer.setTag(dwarf::DW_TAG_array_type);
Eric Christopher72a52952013-01-08 01:53:52 +00001537 if (CTy->isVector())
Eric Christopherbb69a272012-08-24 01:14:27 +00001538 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel0e821f42011-04-12 23:21:44 +00001539
Eric Christopher0df08e22013-08-08 07:40:37 +00001540 // Emit the element type.
Devang Patel0e821f42011-04-12 23:21:44 +00001541 addType(&Buffer, CTy->getTypeDerivedFrom());
Devang Patel0e821f42011-04-12 23:21:44 +00001542
1543 // Get an anonymous type for index type.
Eric Christophercad9b532013-01-04 21:51:53 +00001544 // FIXME: This type should be passed down from the front end
1545 // as different languages may have different sizes for indexes.
Devang Patel0e821f42011-04-12 23:21:44 +00001546 DIE *IdxTy = getIndexTyDie();
1547 if (!IdxTy) {
1548 // Construct an anonymous type for index type.
1549 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Eric Christophercad9b532013-01-04 21:51:53 +00001550 addString(IdxTy, dwarf::DW_AT_name, "int");
Devang Patel0e821f42011-04-12 23:21:44 +00001551 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1552 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1553 dwarf::DW_ATE_signed);
1554 addDie(IdxTy);
1555 setIndexTyDie(IdxTy);
1556 }
1557
1558 // Add subranges to array type.
Eric Christopher0df08e22013-08-08 07:40:37 +00001559 DIArray Elements = CTy->getTypeArray();
Devang Patel0e821f42011-04-12 23:21:44 +00001560 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1561 DIDescriptor Element = Elements.getElement(i);
1562 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1563 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1564 }
1565}
1566
1567/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
1568DIE *CompileUnit::constructEnumTypeDIE(DIEnumerator ETy) {
1569 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
1570 StringRef Name = ETy.getName();
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001571 addString(Enumerator, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001572 int64_t Value = ETy.getEnumValue();
1573 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
1574 return Enumerator;
1575}
1576
Devang Patel89543712011-08-15 17:24:54 +00001577/// constructContainingTypeDIEs - Construct DIEs for types that contain
1578/// vtables.
1579void CompileUnit::constructContainingTypeDIEs() {
1580 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
1581 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1582 DIE *SPDie = CI->first;
1583 const MDNode *N = CI->second;
1584 if (!N) continue;
1585 DIE *NDie = getDIE(N);
1586 if (!NDie) continue;
1587 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1588 }
1589}
1590
Devang Patel3acc70e2011-08-15 22:04:40 +00001591/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Eric Christopherbf2d23c2013-06-24 21:07:27 +00001592DIE *CompileUnit::constructVariableDIE(DbgVariable *DV,
1593 bool isScopeAbstract) {
Devang Patel3acc70e2011-08-15 22:04:40 +00001594 StringRef Name = DV->getName();
Devang Patel3acc70e2011-08-15 22:04:40 +00001595
1596 // Translate tag to proper Dwarf tag.
Eric Christopher31b05762013-08-08 01:41:00 +00001597 uint16_t Tag = DV->getTag();
Devang Patel3acc70e2011-08-15 22:04:40 +00001598
1599 // Define variable debug information entry.
1600 DIE *VariableDie = new DIE(Tag);
1601 DbgVariable *AbsVar = DV->getAbstractVariable();
1602 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
Manman Ren4213c392013-05-29 17:16:59 +00001603 if (AbsDIE)
Devang Patel3acc70e2011-08-15 22:04:40 +00001604 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Manman Ren4213c392013-05-29 17:16:59 +00001605 dwarf::DW_FORM_ref4, AbsDIE);
Devang Patel3acc70e2011-08-15 22:04:40 +00001606 else {
David Blaikie715528b2013-08-19 03:34:03 +00001607 if (!Name.empty())
1608 addString(VariableDie, dwarf::DW_AT_name, Name);
Devang Patel3acc70e2011-08-15 22:04:40 +00001609 addSourceLine(VariableDie, DV->getVariable());
1610 addType(VariableDie, DV->getType());
1611 }
1612
1613 if (DV->isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001614 addFlag(VariableDie, dwarf::DW_AT_artificial);
Devang Patel3acc70e2011-08-15 22:04:40 +00001615
1616 if (isScopeAbstract) {
1617 DV->setDIE(VariableDie);
1618 return VariableDie;
1619 }
1620
1621 // Add variable address.
1622
1623 unsigned Offset = DV->getDotDebugLocOffset();
1624 if (Offset != ~0U) {
Eric Christopher40b6bf62013-06-24 21:34:55 +00001625 addLabel(VariableDie, dwarf::DW_AT_location, dwarf::DW_FORM_data4,
1626 Asm->GetTempSymbol("debug_loc", Offset));
Devang Patel3acc70e2011-08-15 22:04:40 +00001627 DV->setDIE(VariableDie);
1628 return VariableDie;
1629 }
1630
Eric Christophercead0332011-10-03 15:49:20 +00001631 // Check if variable is described by a DBG_VALUE instruction.
Devang Patel3acc70e2011-08-15 22:04:40 +00001632 if (const MachineInstr *DVInsn = DV->getMInsn()) {
David Blaikie0252265b2013-06-16 20:34:15 +00001633 assert(DVInsn->getNumOperands() == 3);
1634 if (DVInsn->getOperand(0).isReg()) {
1635 const MachineOperand RegOp = DVInsn->getOperand(0);
Adrian Prantl19942882013-07-09 21:44:06 +00001636 // If the second operand is an immediate, this is an indirect value.
Adrian Prantl418d1d12013-07-09 20:28:37 +00001637 if (DVInsn->getOperand(1).isImm()) {
1638 MachineLocation Location(RegOp.getReg(), DVInsn->getOperand(1).getImm());
Eric Christopherbf2d23c2013-06-24 21:07:27 +00001639 addVariableAddress(*DV, VariableDie, Location);
David Blaikie0252265b2013-06-16 20:34:15 +00001640 } else if (RegOp.getReg())
Eric Christopherbf2d23c2013-06-24 21:07:27 +00001641 addVariableAddress(*DV, VariableDie, MachineLocation(RegOp.getReg()));
David Blaikie0252265b2013-06-16 20:34:15 +00001642 } else if (DVInsn->getOperand(0).isImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001643 addConstantValue(VariableDie, DVInsn->getOperand(0), DV->getType());
David Blaikie0252265b2013-06-16 20:34:15 +00001644 else if (DVInsn->getOperand(0).isFPImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001645 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
David Blaikie0252265b2013-06-16 20:34:15 +00001646 else if (DVInsn->getOperand(0).isCImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001647 addConstantValue(VariableDie, DVInsn->getOperand(0).getCImm(),
1648 DV->getType().isUnsignedDIType());
1649
Devang Patel3acc70e2011-08-15 22:04:40 +00001650 DV->setDIE(VariableDie);
1651 return VariableDie;
1652 } else {
1653 // .. else use frame index.
1654 int FI = DV->getFrameIndex();
1655 if (FI != ~0) {
1656 unsigned FrameReg = 0;
1657 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopher92331fd2012-11-21 00:34:38 +00001658 int Offset =
Devang Patel3acc70e2011-08-15 22:04:40 +00001659 TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
1660 MachineLocation Location(FrameReg, Offset);
Eric Christopherbf2d23c2013-06-24 21:07:27 +00001661 addVariableAddress(*DV, VariableDie, Location);
Devang Patel3acc70e2011-08-15 22:04:40 +00001662 }
1663 }
1664
1665 DV->setDIE(VariableDie);
1666 return VariableDie;
1667}
1668
Devang Patel0e821f42011-04-12 23:21:44 +00001669/// createMemberDIE - Create new member DIE.
1670DIE *CompileUnit::createMemberDIE(DIDerivedType DT) {
1671 DIE *MemberDie = new DIE(DT.getTag());
1672 StringRef Name = DT.getName();
1673 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001674 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001675
1676 addType(MemberDie, DT.getTypeDerivedFrom());
1677
1678 addSourceLine(MemberDie, DT);
1679
1680 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
1681 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1682
1683 uint64_t Size = DT.getSizeInBits();
1684 uint64_t FieldSize = DT.getOriginalTypeSize();
1685
1686 if (Size != FieldSize) {
1687 // Handle bitfield.
1688 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1689 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
1690
1691 uint64_t Offset = DT.getOffsetInBits();
1692 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1693 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1694 uint64_t FieldOffset = (HiMark - FieldSize);
1695 Offset -= FieldOffset;
1696
1697 // Maybe we need to work from the other end.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001698 if (Asm->getDataLayout().isLittleEndian())
Devang Patel0e821f42011-04-12 23:21:44 +00001699 Offset = FieldSize - (Offset + Size);
1700 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
1701
1702 // Here WD_AT_data_member_location points to the anonymous
1703 // field that includes this bit field.
1704 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
1705
1706 } else
1707 // This is not a bitfield.
1708 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
1709
1710 if (DT.getTag() == dwarf::DW_TAG_inheritance
1711 && DT.isVirtual()) {
1712
1713 // For C++, virtual base classes are not at fixed offset. Use following
1714 // expression to extract appropriate offset from vtable.
1715 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1716
1717 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
1718 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1719 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1720 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1721 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1722 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1723 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1724 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1725
1726 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1727 VBaseLocationDie);
1728 } else
1729 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
1730
1731 if (DT.isProtected())
Nick Lewyckycb918492011-12-13 05:09:11 +00001732 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001733 dwarf::DW_ACCESS_protected);
1734 else if (DT.isPrivate())
Nick Lewyckycb918492011-12-13 05:09:11 +00001735 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001736 dwarf::DW_ACCESS_private);
1737 // Otherwise C++ member and base classes are considered public.
Eric Christopher92331fd2012-11-21 00:34:38 +00001738 else
Nick Lewyckycb918492011-12-13 05:09:11 +00001739 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001740 dwarf::DW_ACCESS_public);
1741 if (DT.isVirtual())
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001742 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001743 dwarf::DW_VIRTUALITY_virtual);
Devang Patel514b4002011-04-16 00:11:51 +00001744
1745 // Objective-C properties.
Devang Patel44882172012-02-06 17:49:43 +00001746 if (MDNode *PNode = DT.getObjCProperty())
1747 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
Eric Christopher92331fd2012-11-21 00:34:38 +00001748 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
Devang Patel44882172012-02-06 17:49:43 +00001749 PropertyDie);
1750
David Blaikie37fefc32012-12-13 22:43:07 +00001751 if (DT.isArtificial())
1752 addFlag(MemberDie, dwarf::DW_AT_artificial);
1753
Devang Patel0e821f42011-04-12 23:21:44 +00001754 return MemberDie;
1755}
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001756
1757/// createStaticMemberDIE - Create new DIE for C++ static member.
1758DIE *CompileUnit::createStaticMemberDIE(const DIDerivedType DT) {
1759 if (!DT.Verify())
1760 return NULL;
1761
1762 DIE *StaticMemberDIE = new DIE(DT.getTag());
1763 DIType Ty = DT.getTypeDerivedFrom();
1764
1765 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1766 addType(StaticMemberDIE, Ty);
1767 addSourceLine(StaticMemberDIE, DT);
1768 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1769 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1770
1771 // FIXME: We could omit private if the parent is a class_type, and
1772 // public if the parent is something else.
1773 if (DT.isProtected())
1774 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1775 dwarf::DW_ACCESS_protected);
1776 else if (DT.isPrivate())
1777 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1778 dwarf::DW_ACCESS_private);
1779 else
1780 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1781 dwarf::DW_ACCESS_public);
1782
1783 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
1784 addConstantValue(StaticMemberDIE, CI, Ty.isUnsignedDIType());
David Blaikiea39a76e2013-01-20 01:18:01 +00001785 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
1786 addConstantFPValue(StaticMemberDIE, CFP);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001787
1788 insertDIE(DT, StaticMemberDIE);
1789 return StaticMemberDIE;
1790}