blob: 508a6927c20968e158ebc425df8d4f5ff668cd8f [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"
David Blaikie6b288cf2013-10-30 20:42:41 +000025#include "llvm/MC/MCSection.h"
26#include "llvm/MC/MCStreamer.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)
Manman Rence20d462013-10-29 22:57:10 +000038 : 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);
David Blaikie2ad00162013-11-15 23:09:13 +000041 insertDIE(DIDescriptor(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
Manman Ren4dbdc902013-10-31 17:54:35 +0000101/// Check whether the DIE for this MDNode can be shared across CUs.
David Blaikie4201ddf2013-11-15 22:59:36 +0000102static bool isShareableAcrossCUs(DIDescriptor D) {
Manman Ren4dbdc902013-10-31 17:54:35 +0000103 // When the MDNode can be part of the type system, the DIE can be
104 // shared across CUs.
David Blaikie4201ddf2013-11-15 22:59:36 +0000105 return D.isType() ||
106 (D.isSubprogram() && !DISubprogram(D).isDefinition());
Manman Ren4dbdc902013-10-31 17:54:35 +0000107}
108
109/// getDIE - Returns the debug information entry map slot for the
110/// specified debug variable. We delegate the request to DwarfDebug
111/// when the DIE for this MDNode can be shared across CUs. The mappings
112/// will be kept in DwarfDebug for shareable DIEs.
David Blaikie2ad00162013-11-15 23:09:13 +0000113DIE *CompileUnit::getDIE(DIDescriptor D) const {
114 if (isShareableAcrossCUs(D))
115 return DD->getDIE(D);
116 return MDNodeToDieMap.lookup(D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000117}
118
119/// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
120/// when the DIE for this MDNode can be shared across CUs. The mappings
121/// will be kept in DwarfDebug for shareable DIEs.
David Blaikie2ad00162013-11-15 23:09:13 +0000122void CompileUnit::insertDIE(DIDescriptor Desc, DIE *D) {
123 if (isShareableAcrossCUs(Desc)) {
124 DD->insertDIE(Desc, D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000125 return;
126 }
David Blaikie2ad00162013-11-15 23:09:13 +0000127 MDNodeToDieMap.insert(std::make_pair(Desc, D));
Manman Ren4dbdc902013-10-31 17:54:35 +0000128}
129
Eric Christopherbb69a272012-08-24 01:14:27 +0000130/// addFlag - Add a flag that is true.
David Blaikief2443192013-10-21 17:28:37 +0000131void CompileUnit::addFlag(DIE *Die, dwarf::Attribute Attribute) {
Michael Gottesmanc89466f2013-09-04 04:39:38 +0000132 if (DD->getDwarfVersion() >= 4)
Eric Christopherdccd3282013-10-04 22:40:05 +0000133 Die->addValue(Attribute, dwarf::DW_FORM_flag_present, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000134 else
Eric Christopherdccd3282013-10-04 22:40:05 +0000135 Die->addValue(Attribute, dwarf::DW_FORM_flag, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000136}
137
Devang Patel0e821f42011-04-12 23:21:44 +0000138/// addUInt - Add an unsigned integer attribute data and value.
139///
David Blaikief2443192013-10-21 17:28:37 +0000140void CompileUnit::addUInt(DIE *Die, dwarf::Attribute Attribute,
141 Optional<dwarf::Form> Form, uint64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000142 if (!Form)
143 Form = DIEInteger::BestForm(false, Integer);
144 DIEValue *Value = Integer == 1 ? DIEIntegerOne : new (DIEValueAllocator)
145 DIEInteger(Integer);
David Blaikief2443192013-10-21 17:28:37 +0000146 Die->addValue(Attribute, *Form, Value);
147}
148
149void CompileUnit::addUInt(DIEBlock *Block, dwarf::Form Form, uint64_t Integer) {
150 addUInt(Block, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000151}
152
153/// addSInt - Add an signed integer attribute data and value.
154///
David Blaikief2443192013-10-21 17:28:37 +0000155void CompileUnit::addSInt(DIE *Die, dwarf::Attribute Attribute,
156 Optional<dwarf::Form> Form, int64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000157 if (!Form)
158 Form = DIEInteger::BestForm(true, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000159 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
David Blaikief2443192013-10-21 17:28:37 +0000160 Die->addValue(Attribute, *Form, Value);
161}
162
163void CompileUnit::addSInt(DIEBlock *Die, Optional<dwarf::Form> Form,
164 int64_t Integer) {
165 addSInt(Die, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000166}
167
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000168/// addString - Add a string attribute data and value. We always emit a
169/// reference to the string pool instead of immediate strings so that DIEs have
Eric Christopherfba22602013-01-07 19:32:45 +0000170/// more predictable sizes. In the case of split dwarf we emit an index
171/// into another table which gets us the static offset into the string
172/// table.
Eric Christophere6c6c4d2013-11-11 18:52:33 +0000173void CompileUnit::addString(DIE *Die, dwarf::Attribute Attribute,
174 StringRef String) {
Eric Christopher67646432013-07-26 17:02:41 +0000175 DIEValue *Value;
David Blaikief2443192013-10-21 17:28:37 +0000176 dwarf::Form Form;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000177 if (!DD->useSplitDwarf()) {
178 MCSymbol *Symb = DU->getStringPoolEntry(String);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000179 if (Asm->needsRelocationsForDwarfStringPool())
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000180 Value = new (DIEValueAllocator) DIELabel(Symb);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000181 else {
182 MCSymbol *StringPool = DU->getStringPoolSym();
183 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
184 }
Eric Christopher67646432013-07-26 17:02:41 +0000185 Form = dwarf::DW_FORM_strp;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000186 } else {
187 unsigned idx = DU->getStringPoolIndex(String);
Eric Christopher67646432013-07-26 17:02:41 +0000188 Value = new (DIEValueAllocator) DIEInteger(idx);
189 Form = dwarf::DW_FORM_GNU_str_index;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000190 }
Eric Christopher67646432013-07-26 17:02:41 +0000191 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
192 Die->addValue(Attribute, Form, Str);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000193}
194
195/// addLocalString - Add a string attribute data and value. This is guaranteed
196/// to be in the local string pool instead of indirected.
David Blaikief2443192013-10-21 17:28:37 +0000197void CompileUnit::addLocalString(DIE *Die, dwarf::Attribute Attribute,
Eric Christopher2cbd5762013-01-07 19:32:41 +0000198 StringRef String) {
Eric Christophere698f532012-12-20 21:58:36 +0000199 MCSymbol *Symb = DU->getStringPoolEntry(String);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000200 DIEValue *Value;
201 if (Asm->needsRelocationsForDwarfStringPool())
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000202 Value = new (DIEValueAllocator) DIELabel(Symb);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000203 else {
Eric Christophere698f532012-12-20 21:58:36 +0000204 MCSymbol *StringPool = DU->getStringPoolSym();
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000205 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000206 }
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000207 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000208}
209
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000210/// addExpr - Add a Dwarf expression attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000211///
David Blaikief2443192013-10-21 17:28:37 +0000212void CompileUnit::addExpr(DIEBlock *Die, dwarf::Form Form, const MCExpr *Expr) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000213 DIEValue *Value = new (DIEValueAllocator) DIEExpr(Expr);
David Blaikief2443192013-10-21 17:28:37 +0000214 Die->addValue((dwarf::Attribute)0, Form, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000215}
216
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000217/// addLabel - Add a Dwarf label attribute data and value.
218///
Eric Christophere6c6c4d2013-11-11 18:52:33 +0000219void CompileUnit::addLabel(DIE *Die, dwarf::Attribute Attribute,
220 dwarf::Form Form, const MCSymbol *Label) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000221 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
222 Die->addValue(Attribute, Form, Value);
David Blaikief3cd7c52013-06-28 20:05:04 +0000223}
224
David Blaikief2443192013-10-21 17:28:37 +0000225void CompileUnit::addLabel(DIEBlock *Die, dwarf::Form Form,
226 const MCSymbol *Label) {
227 addLabel(Die, (dwarf::Attribute)0, Form, Label);
228}
229
Eric Christopher962c9082013-01-15 23:56:56 +0000230/// addLabelAddress - Add a dwarf label attribute data and value using
231/// DW_FORM_addr or DW_FORM_GNU_addr_index.
232///
David Blaikief2443192013-10-21 17:28:37 +0000233void CompileUnit::addLabelAddress(DIE *Die, dwarf::Attribute Attribute,
Eric Christopher962c9082013-01-15 23:56:56 +0000234 MCSymbol *Label) {
Alexey Samsonov4436bf02013-10-03 08:54:43 +0000235 if (Label)
236 DD->addArangeLabel(SymbolCU(this, Label));
Richard Mitton21101b32013-09-19 23:21:01 +0000237
Eric Christopher962c9082013-01-15 23:56:56 +0000238 if (!DD->useSplitDwarf()) {
239 if (Label != NULL) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000240 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
Eric Christopher962c9082013-01-15 23:56:56 +0000241 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
242 } else {
243 DIEValue *Value = new (DIEValueAllocator) DIEInteger(0);
244 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
245 }
246 } else {
247 unsigned idx = DU->getAddrPoolIndex(Label);
248 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
249 Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
250 }
251}
252
Eric Christophere9ec2452013-01-18 22:11:33 +0000253/// addOpAddress - Add a dwarf op address data and value using the
254/// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
255///
David Blaikief2443192013-10-21 17:28:37 +0000256void CompileUnit::addOpAddress(DIEBlock *Die, const MCSymbol *Sym) {
Alexey Samsonov4436bf02013-10-03 08:54:43 +0000257 DD->addArangeLabel(SymbolCU(this, Sym));
Eric Christophere9ec2452013-01-18 22:11:33 +0000258 if (!DD->useSplitDwarf()) {
David Blaikief2443192013-10-21 17:28:37 +0000259 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
260 addLabel(Die, dwarf::DW_FORM_udata, Sym);
Eric Christophere9ec2452013-01-18 22:11:33 +0000261 } else {
David Blaikief2443192013-10-21 17:28:37 +0000262 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
263 addUInt(Die, dwarf::DW_FORM_GNU_addr_index, DU->getAddrPoolIndex(Sym));
Eric Christophere9ec2452013-01-18 22:11:33 +0000264 }
265}
266
Devang Patel0e821f42011-04-12 23:21:44 +0000267/// addDelta - Add a label delta attribute data and value.
268///
Eric Christophere6c6c4d2013-11-11 18:52:33 +0000269void CompileUnit::addDelta(DIE *Die, dwarf::Attribute Attribute,
270 dwarf::Form Form, const MCSymbol *Hi,
271 const MCSymbol *Lo) {
Devang Patel0e821f42011-04-12 23:21:44 +0000272 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
273 Die->addValue(Attribute, Form, Value);
274}
275
276/// addDIEEntry - Add a DIE attribute data and value.
277///
Eric Christopher98b7f172013-11-11 18:52:36 +0000278void CompileUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute,
279 DIE *Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +0000280 addDIEEntry(Die, Attribute, createDIEEntry(Entry));
281}
282
283void CompileUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute,
284 DIEEntry *Entry) {
285 const DIE *DieCU = Die->getCompileUnitOrNull();
286 const DIE *EntryCU = Entry->getEntry()->getCompileUnitOrNull();
287 if (!DieCU)
288 // We assume that Die belongs to this CU, if it is not linked to any CU yet.
289 DieCU = getCUDie();
290 if (!EntryCU)
291 EntryCU = getCUDie();
292 Die->addValue(Attribute, EntryCU == DieCU ? dwarf::DW_FORM_ref4
293 : dwarf::DW_FORM_ref_addr,
294 Entry);
Devang Patel0e821f42011-04-12 23:21:44 +0000295}
296
Manman Renb987e512013-10-29 00:53:03 +0000297/// Create a DIE with the given Tag, add the DIE to its parent, and
298/// call insertDIE if MD is not null.
299DIE *CompileUnit::createAndAddDIE(unsigned Tag, DIE &Parent, const MDNode *MD) {
300 DIE *Die = new DIE(Tag);
301 Parent.addChild(Die);
302 if (MD)
David Blaikie2ad00162013-11-15 23:09:13 +0000303 insertDIE(DIDescriptor(MD), Die);
Manman Renb987e512013-10-29 00:53:03 +0000304 return Die;
305}
306
Devang Patel0e821f42011-04-12 23:21:44 +0000307/// addBlock - Add block data.
308///
David Blaikief2443192013-10-21 17:28:37 +0000309void CompileUnit::addBlock(DIE *Die, dwarf::Attribute Attribute,
Devang Patel0e821f42011-04-12 23:21:44 +0000310 DIEBlock *Block) {
311 Block->ComputeSize(Asm);
312 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
313 Die->addValue(Attribute, Block->BestForm(), Block);
314}
315
316/// addSourceLine - Add location information to specified debug information
317/// entry.
318void CompileUnit::addSourceLine(DIE *Die, DIVariable V) {
319 // Verify variable.
Manman Ren7504ed42013-07-08 18:33:29 +0000320 if (!V.isVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000321 return;
Eric Christopher92331fd2012-11-21 00:34:38 +0000322
Devang Patel0e821f42011-04-12 23:21:44 +0000323 unsigned Line = V.getLineNumber();
324 if (Line == 0)
325 return;
Eric Christopherc2697f82013-10-19 01:04:47 +0000326 unsigned FileID =
327 DD->getOrCreateSourceID(V.getContext().getFilename(),
328 V.getContext().getDirectory(), getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000329 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000330 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
331 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000332}
333
334/// addSourceLine - Add location information to specified debug information
335/// entry.
336void CompileUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
337 // Verify global variable.
Manman Ren7504ed42013-07-08 18:33:29 +0000338 if (!G.isGlobalVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000339 return;
340
341 unsigned Line = G.getLineNumber();
342 if (Line == 0)
343 return;
Eric Christopherc2697f82013-10-19 01:04:47 +0000344 unsigned FileID =
345 DD->getOrCreateSourceID(G.getFilename(), G.getDirectory(), getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000346 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000347 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
348 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000349}
350
351/// addSourceLine - Add location information to specified debug information
352/// entry.
353void CompileUnit::addSourceLine(DIE *Die, DISubprogram SP) {
354 // Verify subprogram.
Manman Ren7504ed42013-07-08 18:33:29 +0000355 if (!SP.isSubprogram())
Devang Patel0e821f42011-04-12 23:21:44 +0000356 return;
Eric Christopher7734ca22012-03-15 23:55:40 +0000357
Devang Patel0e821f42011-04-12 23:21:44 +0000358 // If the line number is 0, don't add it.
Eric Christopher7734ca22012-03-15 23:55:40 +0000359 unsigned Line = SP.getLineNumber();
360 if (Line == 0)
Devang Patel0e821f42011-04-12 23:21:44 +0000361 return;
362
Eric Christopherc2697f82013-10-19 01:04:47 +0000363 unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(), SP.getDirectory(),
364 getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000365 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000366 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
367 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000368}
369
370/// addSourceLine - Add location information to specified debug information
371/// entry.
372void CompileUnit::addSourceLine(DIE *Die, DIType Ty) {
373 // Verify type.
Manman Ren7504ed42013-07-08 18:33:29 +0000374 if (!Ty.isType())
Devang Patel0e821f42011-04-12 23:21:44 +0000375 return;
376
377 unsigned Line = Ty.getLineNumber();
Eric Christopher7734ca22012-03-15 23:55:40 +0000378 if (Line == 0)
Devang Patel0e821f42011-04-12 23:21:44 +0000379 return;
Eric Christopherc2697f82013-10-19 01:04:47 +0000380 unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(), Ty.getDirectory(),
381 getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000382 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000383 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
384 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000385}
386
387/// addSourceLine - Add location information to specified debug information
388/// entry.
Eric Christopher70e1bd82012-03-29 08:42:56 +0000389void CompileUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
390 // Verify type.
Manman Ren7504ed42013-07-08 18:33:29 +0000391 if (!Ty.isObjCProperty())
Eric Christopher70e1bd82012-03-29 08:42:56 +0000392 return;
393
394 unsigned Line = Ty.getLineNumber();
395 if (Line == 0)
396 return;
397 DIFile File = Ty.getFile();
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000398 unsigned FileID = DD->getOrCreateSourceID(File.getFilename(),
Manman Ren1e427202013-03-07 01:42:00 +0000399 File.getDirectory(), getUniqueID());
Eric Christopher70e1bd82012-03-29 08:42:56 +0000400 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000401 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
402 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Eric Christopher70e1bd82012-03-29 08:42:56 +0000403}
404
405/// addSourceLine - Add location information to specified debug information
406/// entry.
Devang Patel0e821f42011-04-12 23:21:44 +0000407void CompileUnit::addSourceLine(DIE *Die, DINameSpace NS) {
408 // Verify namespace.
409 if (!NS.Verify())
410 return;
411
412 unsigned Line = NS.getLineNumber();
413 if (Line == 0)
414 return;
415 StringRef FN = NS.getFilename();
416
Eric Christopherc2697f82013-10-19 01:04:47 +0000417 unsigned FileID =
418 DD->getOrCreateSourceID(FN, NS.getDirectory(), getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000419 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000420 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
421 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000422}
423
Eric Christopher92331fd2012-11-21 00:34:38 +0000424/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patel77dc5412011-04-27 22:45:24 +0000425/// DbgVariable based on provided MachineLocation.
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000426void CompileUnit::addVariableAddress(const DbgVariable &DV, DIE *Die,
Devang Patel77dc5412011-04-27 22:45:24 +0000427 MachineLocation Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000428 if (DV.variableHasComplexAddress())
Devang Patel0e821f42011-04-12 23:21:44 +0000429 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000430 else if (DV.isBlockByrefVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000431 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
432 else
David Blaikieea2605d2013-06-20 00:25:24 +0000433 addAddress(Die, dwarf::DW_AT_location, Location,
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000434 DV.getVariable().isIndirect());
Devang Patel0e821f42011-04-12 23:21:44 +0000435}
436
Devang Patelba5fbf12011-04-26 19:06:18 +0000437/// addRegisterOp - Add register operand.
David Blaikief2443192013-10-21 17:28:37 +0000438void CompileUnit::addRegisterOp(DIEBlock *TheDie, unsigned Reg) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000439 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
440 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
441 if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000442 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000443 else {
David Blaikief2443192013-10-21 17:28:37 +0000444 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
445 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000446 }
447}
448
449/// addRegisterOffset - Add register offset.
David Blaikief2443192013-10-21 17:28:37 +0000450void CompileUnit::addRegisterOffset(DIEBlock *TheDie, unsigned Reg,
451 int64_t Offset) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000452 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
453 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
454 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
455 if (Reg == TRI->getFrameRegister(*Asm->MF))
456 // If variable offset is based in frame register then use fbreg.
David Blaikief2443192013-10-21 17:28:37 +0000457 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000458 else if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000459 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000460 else {
David Blaikief2443192013-10-21 17:28:37 +0000461 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
462 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000463 }
David Blaikief2443192013-10-21 17:28:37 +0000464 addSInt(TheDie, dwarf::DW_FORM_sdata, Offset);
Devang Patelba5fbf12011-04-26 19:06:18 +0000465}
466
467/// addAddress - Add an address attribute to a die based on the location
468/// provided.
David Blaikief2443192013-10-21 17:28:37 +0000469void CompileUnit::addAddress(DIE *Die, dwarf::Attribute Attribute,
David Blaikieea2605d2013-06-20 00:25:24 +0000470 const MachineLocation &Location, bool Indirect) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000471 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
472
David Blaikieea2605d2013-06-20 00:25:24 +0000473 if (Location.isReg() && !Indirect)
Devang Patelba5fbf12011-04-26 19:06:18 +0000474 addRegisterOp(Block, Location.getReg());
David Blaikieea2605d2013-06-20 00:25:24 +0000475 else {
Devang Patelba5fbf12011-04-26 19:06:18 +0000476 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
David Blaikieea2605d2013-06-20 00:25:24 +0000477 if (Indirect && !Location.isReg()) {
David Blaikief2443192013-10-21 17:28:37 +0000478 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
David Blaikieea2605d2013-06-20 00:25:24 +0000479 }
480 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000481
482 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000483 addBlock(Die, Attribute, Block);
Devang Patelba5fbf12011-04-26 19:06:18 +0000484}
485
Devang Patel0e821f42011-04-12 23:21:44 +0000486/// addComplexAddress - Start with the address based on the location provided,
487/// and generate the DWARF information necessary to find the actual variable
488/// given the extra address information encoded in the DIVariable, starting from
489/// the starting location. Add the DWARF information to the die.
490///
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000491void CompileUnit::addComplexAddress(const DbgVariable &DV, DIE *Die,
David Blaikief2443192013-10-21 17:28:37 +0000492 dwarf::Attribute Attribute,
Devang Patel0e821f42011-04-12 23:21:44 +0000493 const MachineLocation &Location) {
Devang Patel0e821f42011-04-12 23:21:44 +0000494 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000495 unsigned N = DV.getNumAddrElements();
Devang Patel3e021532011-04-28 02:22:40 +0000496 unsigned i = 0;
497 if (Location.isReg()) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000498 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Devang Patel3e021532011-04-28 02:22:40 +0000499 // If first address element is OpPlus then emit
500 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000501 addRegisterOffset(Block, Location.getReg(), DV.getAddrElement(1));
Devang Patel3e021532011-04-28 02:22:40 +0000502 i = 2;
503 } else
504 addRegisterOp(Block, Location.getReg());
Eric Christopherc2697f82013-10-19 01:04:47 +0000505 } else
Devang Patelba5fbf12011-04-26 19:06:18 +0000506 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000507
Eric Christopherc2697f82013-10-19 01:04:47 +0000508 for (; i < N; ++i) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000509 uint64_t Element = DV.getAddrElement(i);
Devang Patel0e821f42011-04-12 23:21:44 +0000510 if (Element == DIBuilder::OpPlus) {
David Blaikief2443192013-10-21 17:28:37 +0000511 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
512 addUInt(Block, dwarf::DW_FORM_udata, DV.getAddrElement(++i));
Devang Patel0e821f42011-04-12 23:21:44 +0000513 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher4d250522012-05-08 18:56:00 +0000514 if (!Location.isReg())
David Blaikief2443192013-10-21 17:28:37 +0000515 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Eric Christopherc2697f82013-10-19 01:04:47 +0000516 } else
517 llvm_unreachable("unknown DIBuilder Opcode");
Devang Patel0e821f42011-04-12 23:21:44 +0000518 }
519
520 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000521 addBlock(Die, Attribute, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000522}
523
524/* Byref variables, in Blocks, are declared by the programmer as "SomeType
525 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
526 gives the variable VarName either the struct, or a pointer to the struct, as
527 its type. This is necessary for various behind-the-scenes things the
528 compiler needs to do with by-reference variables in Blocks.
529
530 However, as far as the original *programmer* is concerned, the variable
531 should still have type 'SomeType', as originally declared.
532
533 The function getBlockByrefType dives into the __Block_byref_x_VarName
534 struct to find the original type of the variable, which is then assigned to
535 the variable's Debug Information Entry as its real type. So far, so good.
536 However now the debugger will expect the variable VarName to have the type
537 SomeType. So we need the location attribute for the variable to be an
538 expression that explains to the debugger how to navigate through the
539 pointers and struct to find the actual variable of type SomeType.
540
541 The following function does just that. We start by getting
542 the "normal" location for the variable. This will be the location
543 of either the struct __Block_byref_x_VarName or the pointer to the
544 struct __Block_byref_x_VarName.
545
546 The struct will look something like:
547
548 struct __Block_byref_x_VarName {
549 ... <various fields>
550 struct __Block_byref_x_VarName *forwarding;
551 ... <various other fields>
552 SomeType VarName;
553 ... <maybe more fields>
554 };
555
556 If we are given the struct directly (as our starting point) we
557 need to tell the debugger to:
558
559 1). Add the offset of the forwarding field.
560
561 2). Follow that pointer to get the real __Block_byref_x_VarName
562 struct to use (the real one may have been copied onto the heap).
563
564 3). Add the offset for the field VarName, to find the actual variable.
565
566 If we started with a pointer to the struct, then we need to
567 dereference that pointer first, before the other steps.
568 Translating this into DWARF ops, we will need to append the following
569 to the current location description for the variable:
570
571 DW_OP_deref -- optional, if we start with a pointer
572 DW_OP_plus_uconst <forward_fld_offset>
573 DW_OP_deref
574 DW_OP_plus_uconst <varName_fld_offset>
575
576 That is what this function does. */
577
578/// addBlockByrefAddress - Start with the address based on the location
579/// provided, and generate the DWARF information necessary to find the
580/// actual Block variable (navigating the Block struct) based on the
581/// starting location. Add the DWARF information to the die. For
582/// more information, read large comment just above here.
583///
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000584void CompileUnit::addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
David Blaikief2443192013-10-21 17:28:37 +0000585 dwarf::Attribute Attribute,
Devang Patel0e821f42011-04-12 23:21:44 +0000586 const MachineLocation &Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000587 DIType Ty = DV.getType();
Devang Patel0e821f42011-04-12 23:21:44 +0000588 DIType TmpTy = Ty;
Eric Christopher31b05762013-08-08 01:41:00 +0000589 uint16_t Tag = Ty.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +0000590 bool isPointer = false;
591
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000592 StringRef varName = DV.getName();
Devang Patel0e821f42011-04-12 23:21:44 +0000593
594 if (Tag == dwarf::DW_TAG_pointer_type) {
595 DIDerivedType DTy = DIDerivedType(Ty);
Manman Ren93b30902013-10-08 18:42:58 +0000596 TmpTy = resolve(DTy.getTypeDerivedFrom());
Devang Patel0e821f42011-04-12 23:21:44 +0000597 isPointer = true;
598 }
599
600 DICompositeType blockStruct = DICompositeType(TmpTy);
601
602 // Find the __forwarding field and the variable field in the __Block_byref
603 // struct.
604 DIArray Fields = blockStruct.getTypeArray();
605 DIDescriptor varField = DIDescriptor();
606 DIDescriptor forwardingField = DIDescriptor();
607
608 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
609 DIDescriptor Element = Fields.getElement(i);
610 DIDerivedType DT = DIDerivedType(Element);
611 StringRef fieldName = DT.getName();
612 if (fieldName == "__forwarding")
613 forwardingField = Element;
614 else if (fieldName == varName)
615 varField = Element;
616 }
617
618 // Get the offsets for the forwarding field and the variable field.
619 unsigned forwardingFieldOffset =
Eric Christopherc2697f82013-10-19 01:04:47 +0000620 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
621 unsigned varFieldOffset = DIDerivedType(varField).getOffsetInBits() >> 3;
Devang Patel0e821f42011-04-12 23:21:44 +0000622
623 // Decode the original location, and use that as the start of the byref
624 // variable's location.
Devang Patel0e821f42011-04-12 23:21:44 +0000625 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
626
Eric Christopheref9d7102012-07-04 02:02:18 +0000627 if (Location.isReg())
628 addRegisterOp(Block, Location.getReg());
629 else
630 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000631
632 // If we started with a pointer to the __Block_byref... struct, then
633 // the first thing we need to do is dereference the pointer (DW_OP_deref).
634 if (isPointer)
David Blaikief2443192013-10-21 17:28:37 +0000635 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000636
637 // Next add the offset for the '__forwarding' field:
638 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
639 // adding the offset if it's 0.
640 if (forwardingFieldOffset > 0) {
David Blaikief2443192013-10-21 17:28:37 +0000641 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
642 addUInt(Block, dwarf::DW_FORM_udata, forwardingFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000643 }
644
645 // Now dereference the __forwarding field to get to the real __Block_byref
646 // struct: DW_OP_deref.
David Blaikief2443192013-10-21 17:28:37 +0000647 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000648
649 // Now that we've got the real __Block_byref... struct, add the offset
650 // for the variable's field to get to the location of the actual variable:
651 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
652 if (varFieldOffset > 0) {
David Blaikief2443192013-10-21 17:28:37 +0000653 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
654 addUInt(Block, dwarf::DW_FORM_udata, varFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000655 }
656
657 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000658 addBlock(Die, Attribute, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000659}
660
Devang Patelbcd50a12011-07-20 21:57:04 +0000661/// isTypeSigned - Return true if the type is signed.
Manman Renb3388602013-10-05 01:43:03 +0000662static bool isTypeSigned(DwarfDebug *DD, DIType Ty, int *SizeInBits) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000663 if (Ty.isDerivedType())
Manman Renb3388602013-10-05 01:43:03 +0000664 return isTypeSigned(DD, DD->resolve(DIDerivedType(Ty).getTypeDerivedFrom()),
665 SizeInBits);
Devang Patelbcd50a12011-07-20 21:57:04 +0000666 if (Ty.isBasicType())
Eric Christopherc2697f82013-10-19 01:04:47 +0000667 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed ||
668 DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000669 *SizeInBits = Ty.getSizeInBits();
670 return true;
671 }
672 return false;
673}
674
Manman Renb3388602013-10-05 01:43:03 +0000675/// Return true if type encoding is unsigned.
676static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) {
677 DIDerivedType DTy(Ty);
678 if (DTy.isDerivedType())
679 return isUnsignedDIType(DD, DD->resolve(DTy.getTypeDerivedFrom()));
680
681 DIBasicType BTy(Ty);
682 if (BTy.isBasicType()) {
683 unsigned Encoding = BTy.getEncoding();
684 if (Encoding == dwarf::DW_ATE_unsigned ||
685 Encoding == dwarf::DW_ATE_unsigned_char ||
686 Encoding == dwarf::DW_ATE_boolean)
687 return true;
688 }
689 return false;
690}
691
692/// If this type is derived from a base type then return base type size.
Manman Renbda410f2013-10-08 18:46:58 +0000693static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) {
Manman Renb3388602013-10-05 01:43:03 +0000694 unsigned Tag = Ty.getTag();
695
696 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
697 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
698 Tag != dwarf::DW_TAG_restrict_type)
699 return Ty.getSizeInBits();
700
701 DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom());
702
703 // If this type is not derived from any type then take conservative approach.
704 if (!BaseType.isValid())
705 return Ty.getSizeInBits();
706
707 // If this is a derived type, go ahead and get the base type, unless it's a
708 // reference then it's just the size of the field. Pointer types have no need
709 // of this since they're a different type of qualification on the type.
710 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
711 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
712 return Ty.getSizeInBits();
713
714 if (BaseType.isDerivedType())
Manman Renbda410f2013-10-08 18:46:58 +0000715 return getBaseTypeSize(DD, DIDerivedType(BaseType));
Manman Renb3388602013-10-05 01:43:03 +0000716
717 return BaseType.getSizeInBits();
718}
719
Devang Patel0e821f42011-04-12 23:21:44 +0000720/// addConstantValue - Add constant value entry in variable DIE.
Eric Christopher78fcf4902013-07-03 01:08:30 +0000721void CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
Devang Patel3c6aed22011-05-27 16:45:18 +0000722 DIType Ty) {
David Blaikiea1e813d2013-05-10 21:52:07 +0000723 // FIXME: This is a bit conservative/simple - it emits negative values at
724 // their maximum bit width which is a bit unfortunate (& doesn't prefer
725 // udata/sdata over dataN as suggested by the DWARF spec)
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000726 assert(MO.isImm() && "Invalid machine operand!");
Devang Patelbcd50a12011-07-20 21:57:04 +0000727 int SizeInBits = -1;
Manman Renb3388602013-10-05 01:43:03 +0000728 bool SignedConstant = isTypeSigned(DD, Ty, &SizeInBits);
David Blaikief2443192013-10-21 17:28:37 +0000729 dwarf::Form Form;
Devang Patelf1d04702011-05-27 18:15:52 +0000730
Eric Christopher9d1daa82013-08-27 23:49:04 +0000731 // If we're a signed constant definitely use sdata.
732 if (SignedConstant) {
733 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, MO.getImm());
734 return;
735 }
736
737 // Else use data for now unless it's larger than we can deal with.
738 switch (SizeInBits) {
739 case 8:
740 Form = dwarf::DW_FORM_data1;
741 break;
742 case 16:
743 Form = dwarf::DW_FORM_data2;
744 break;
745 case 32:
746 Form = dwarf::DW_FORM_data4;
747 break;
748 case 64:
749 Form = dwarf::DW_FORM_data8;
750 break;
751 default:
752 Form = dwarf::DW_FORM_udata;
753 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
754 return;
755 }
756 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
Devang Patel0e821f42011-04-12 23:21:44 +0000757}
758
759/// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christopher78fcf4902013-07-03 01:08:30 +0000760void CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000761 assert(MO.isFPImm() && "Invalid machine operand!");
Devang Patel0e821f42011-04-12 23:21:44 +0000762 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
763 APFloat FPImm = MO.getFPImm()->getValueAPF();
764
765 // Get the raw data form of the floating point.
766 const APInt FltVal = FPImm.bitcastToAPInt();
Eric Christopherc2697f82013-10-19 01:04:47 +0000767 const char *FltPtr = (const char *)FltVal.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000768
769 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000770 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000771 int Incr = (LittleEndian ? 1 : -1);
772 int Start = (LittleEndian ? 0 : NumBytes - 1);
773 int Stop = (LittleEndian ? NumBytes : -1);
774
775 // Output the constant to DWARF one byte at a time.
776 for (; Start != Stop; Start += Incr)
Eric Christopher98b7f172013-11-11 18:52:36 +0000777 addUInt(Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]);
Devang Patel0e821f42011-04-12 23:21:44 +0000778
David Blaikief2443192013-10-21 17:28:37 +0000779 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000780}
781
David Blaikiea39a76e2013-01-20 01:18:01 +0000782/// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christopher78fcf4902013-07-03 01:08:30 +0000783void CompileUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000784 // Pass this down to addConstantValue as an unsigned bag of bits.
785 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
David Blaikiea39a76e2013-01-20 01:18:01 +0000786}
787
Devang Patel0e821f42011-04-12 23:21:44 +0000788/// addConstantValue - Add constant value entry in variable DIE.
Eric Christopher78fcf4902013-07-03 01:08:30 +0000789void CompileUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
Devang Patel0e821f42011-04-12 23:21:44 +0000790 bool Unsigned) {
Eric Christopher78fcf4902013-07-03 01:08:30 +0000791 addConstantValue(Die, CI->getValue(), Unsigned);
David Blaikiea39a76e2013-01-20 01:18:01 +0000792}
793
794// addConstantValue - Add constant value entry in variable DIE.
Eric Christopher78fcf4902013-07-03 01:08:30 +0000795void CompileUnit::addConstantValue(DIE *Die, const APInt &Val, bool Unsigned) {
David Blaikiea39a76e2013-01-20 01:18:01 +0000796 unsigned CIBitWidth = Val.getBitWidth();
Devang Patel8816bbc2011-05-28 00:39:18 +0000797 if (CIBitWidth <= 64) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000798 // If we're a signed constant definitely use sdata.
799 if (!Unsigned) {
800 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
801 Val.getSExtValue());
802 return;
Devang Patel8816bbc2011-05-28 00:39:18 +0000803 }
Eric Christopher9d1daa82013-08-27 23:49:04 +0000804
805 // Else use data for now unless it's larger than we can deal with.
David Blaikief2443192013-10-21 17:28:37 +0000806 dwarf::Form Form;
Eric Christopher9d1daa82013-08-27 23:49:04 +0000807 switch (CIBitWidth) {
808 case 8:
809 Form = dwarf::DW_FORM_data1;
810 break;
811 case 16:
812 Form = dwarf::DW_FORM_data2;
813 break;
814 case 32:
815 Form = dwarf::DW_FORM_data4;
816 break;
817 case 64:
818 Form = dwarf::DW_FORM_data8;
819 break;
820 default:
821 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
822 Val.getZExtValue());
823 return;
824 }
825 addUInt(Die, dwarf::DW_AT_const_value, Form, Val.getZExtValue());
Eric Christopher78fcf4902013-07-03 01:08:30 +0000826 return;
Devang Patel0e821f42011-04-12 23:21:44 +0000827 }
828
829 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
830
831 // Get the raw data form of the large APInt.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000832 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000833
834 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000835 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000836
837 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000838 for (int i = 0; i < NumBytes; i++) {
839 uint8_t c;
840 if (LittleEndian)
841 c = Ptr64[i / 8] >> (8 * (i & 7));
842 else
843 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
David Blaikief2443192013-10-21 17:28:37 +0000844 addUInt(Block, dwarf::DW_FORM_data1, c);
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000845 }
Devang Patel0e821f42011-04-12 23:21:44 +0000846
David Blaikief2443192013-10-21 17:28:37 +0000847 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000848}
849
Eric Christopher25e35092013-04-22 07:47:40 +0000850/// addTemplateParams - Add template parameters into buffer.
Devang Patel0e821f42011-04-12 23:21:44 +0000851void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
852 // Add template parameters.
853 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
854 DIDescriptor Element = TParams.getElement(i);
855 if (Element.isTemplateTypeParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000856 constructTemplateTypeParameterDIE(Buffer,
857 DITemplateTypeParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000858 else if (Element.isTemplateValueParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000859 constructTemplateValueParameterDIE(Buffer,
860 DITemplateValueParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000861 }
Devang Patel0e821f42011-04-12 23:21:44 +0000862}
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000863
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000864/// getOrCreateContextDIE - Get context owner's DIE.
Eric Christopher08f7c8f2013-10-04 23:49:26 +0000865DIE *CompileUnit::getOrCreateContextDIE(DIScope Context) {
David Blaikiebd700e42013-11-14 21:24:34 +0000866 if (!Context || Context.isFile())
867 return getCUDie();
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000868 if (Context.isType())
869 return getOrCreateTypeDIE(DIType(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000870 if (Context.isNameSpace())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000871 return getOrCreateNameSpace(DINameSpace(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000872 if (Context.isSubprogram())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000873 return getOrCreateSubprogramDIE(DISubprogram(Context));
Adrian Prantl7d828bb2013-11-15 21:05:09 +0000874 return getDIE(Context);
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000875}
876
Devang Patel0e821f42011-04-12 23:21:44 +0000877/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
878/// given DIType.
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000879DIE *CompileUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
David Blaikie32887552013-11-14 22:25:02 +0000880 if (!TyNode)
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000881 return NULL;
Manman Renf4c339e2013-10-29 22:49:29 +0000882
David Blaikie32887552013-11-14 22:25:02 +0000883 DIType Ty(TyNode);
884 assert(Ty.isType());
885
Manman Renf4c339e2013-10-29 22:49:29 +0000886 // Construct the context before querying for the existence of the DIE in case
887 // such construction creates the DIE.
888 DIE *ContextDIE = getOrCreateContextDIE(resolve(Ty.getContext()));
Adrian Prantl7d828bb2013-11-15 21:05:09 +0000889 // TODO: Investigate if this beavior is intentional and possibly
890 // replace it with an assert.
891 if (!ContextDIE)
892 ContextDIE = getCUDie();
Manman Renf4c339e2013-10-29 22:49:29 +0000893
Eric Christophere595bae2013-10-04 17:08:38 +0000894 DIE *TyDIE = getDIE(Ty);
Devang Patel0e821f42011-04-12 23:21:44 +0000895 if (TyDIE)
896 return TyDIE;
897
898 // Create new type.
Manman Renf4c339e2013-10-29 22:49:29 +0000899 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
900
Devang Patel0e821f42011-04-12 23:21:44 +0000901 if (Ty.isBasicType())
902 constructTypeDIE(*TyDIE, DIBasicType(Ty));
903 else if (Ty.isCompositeType())
904 constructTypeDIE(*TyDIE, DICompositeType(Ty));
905 else {
906 assert(Ty.isDerivedType() && "Unknown kind of DIType");
907 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
908 }
Eric Christophere2887932011-11-10 19:52:58 +0000909 // If this is a named finished type then include it in the list of types
910 // for the accelerator tables.
Eric Christopher21bde872012-01-06 04:35:23 +0000911 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
912 bool IsImplementation = 0;
913 if (Ty.isCompositeType()) {
914 DICompositeType CT(Ty);
Eric Christopher8ea8e4f2012-01-06 23:03:37 +0000915 // A runtime language of 0 actually means C/C++ and that any
916 // non-negative value is some version of Objective-C/C++.
Eric Christopherc2697f82013-10-19 01:04:47 +0000917 IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete();
Eric Christopher21bde872012-01-06 04:35:23 +0000918 }
Eric Christophercf7289f2013-09-05 18:20:16 +0000919 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
Eric Christopher8ea8e4f2012-01-06 23:03:37 +0000920 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
Eric Christopher21bde872012-01-06 04:35:23 +0000921 }
Eric Christopher92331fd2012-11-21 00:34:38 +0000922
Devang Patel0e821f42011-04-12 23:21:44 +0000923 return TyDIE;
924}
925
926/// addType - Add a new type attribute to the specified entity.
David Blaikief2443192013-10-21 17:28:37 +0000927void CompileUnit::addType(DIE *Entity, DIType Ty, dwarf::Attribute Attribute) {
Eric Christopher0df08e22013-08-08 07:40:37 +0000928 assert(Ty && "Trying to add a type that doesn't exist?");
Devang Patel0e821f42011-04-12 23:21:44 +0000929
930 // Check for pre-existence.
931 DIEEntry *Entry = getDIEEntry(Ty);
932 // If it exists then use the existing value.
933 if (Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +0000934 addDIEEntry(Entity, Attribute, Entry);
Devang Patel0e821f42011-04-12 23:21:44 +0000935 return;
936 }
937
938 // Construct type.
939 DIE *Buffer = getOrCreateTypeDIE(Ty);
940
941 // Set up proxy.
942 Entry = createDIEEntry(Buffer);
943 insertDIEEntry(Ty, Entry);
Manman Ren4dbdc902013-10-31 17:54:35 +0000944 addDIEEntry(Entity, Attribute, Entry);
Devang Patele9853f22011-05-31 22:56:51 +0000945
946 // If this is a complete composite type then include it in the
947 // list of global types.
Devang Patel562c7422011-06-01 00:23:24 +0000948 addGlobalType(Ty);
Devang Patel1cb8ab42011-05-31 23:30:30 +0000949}
950
Eric Christopher9cd26af2013-09-20 23:22:52 +0000951// Accelerator table mutators - add each name along with its companion
952// DIE to the proper table while ensuring that the name that we're going
953// to reference is in the string table. We do this since the names we
954// add may not only be identical to the names in the DIE.
955void CompileUnit::addAccelName(StringRef Name, DIE *Die) {
956 DU->getStringPoolEntry(Name);
Eric Christopherb0fc0b92013-09-23 22:59:11 +0000957 std::vector<DIE *> &DIEs = AccelNames[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +0000958 DIEs.push_back(Die);
959}
960
961void CompileUnit::addAccelObjC(StringRef Name, DIE *Die) {
962 DU->getStringPoolEntry(Name);
Eric Christopherb0fc0b92013-09-23 22:59:11 +0000963 std::vector<DIE *> &DIEs = AccelObjC[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +0000964 DIEs.push_back(Die);
965}
966
967void CompileUnit::addAccelNamespace(StringRef Name, DIE *Die) {
968 DU->getStringPoolEntry(Name);
Eric Christopherb0fc0b92013-09-23 22:59:11 +0000969 std::vector<DIE *> &DIEs = AccelNamespace[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +0000970 DIEs.push_back(Die);
971}
972
973void CompileUnit::addAccelType(StringRef Name, std::pair<DIE *, unsigned> Die) {
974 DU->getStringPoolEntry(Name);
975 std::vector<std::pair<DIE *, unsigned> > &DIEs = AccelTypes[Name];
976 DIEs.push_back(Die);
977}
978
Eric Christopher9c58f312013-09-20 22:20:55 +0000979/// addGlobalName - Add a new global name to the compile unit.
Eric Christopher2c8b7902013-10-17 02:06:06 +0000980void CompileUnit::addGlobalName(StringRef Name, DIE *Die, DIScope Context) {
Eric Christopher8dba0d52013-10-19 01:04:42 +0000981 std::string FullName = getParentContextString(Context) + Name.str();
Eric Christopher2c8b7902013-10-17 02:06:06 +0000982 GlobalNames[FullName] = Die;
Eric Christopher9c58f312013-09-20 22:20:55 +0000983}
984
Devang Patel1cb8ab42011-05-31 23:30:30 +0000985/// addGlobalType - Add a new global type to the compile unit.
986///
Devang Patel562c7422011-06-01 00:23:24 +0000987void CompileUnit::addGlobalType(DIType Ty) {
Eric Christopher9e429ae2013-10-05 00:27:02 +0000988 DIScope Context = resolve(Ty.getContext());
Eric Christopher6d0f1e62013-09-23 23:15:58 +0000989 if (!Ty.getName().empty() && !Ty.isForwardDecl() &&
Eric Christopherb0fc0b92013-09-23 22:59:11 +0000990 (!Context || Context.isCompileUnit() || Context.isFile() ||
991 Context.isNameSpace()))
Eric Christopher2c8b7902013-10-17 02:06:06 +0000992 if (DIEEntry *Entry = getDIEEntry(Ty)) {
Eric Christopher8dba0d52013-10-19 01:04:42 +0000993 std::string FullName =
994 getParentContextString(Context) + Ty.getName().str();
Eric Christopherc2697f82013-10-19 01:04:47 +0000995 GlobalTypes[FullName] = Entry->getEntry();
Eric Christopher2c8b7902013-10-17 02:06:06 +0000996 }
997}
998
999/// getParentContextString - Walks the metadata parent chain in a language
1000/// specific manner (using the compile unit language) and returns
1001/// it as a string. This is done at the metadata level because DIEs may
1002/// not currently have been added to the parent context and walking the
1003/// DIEs looking for names is more expensive than walking the metadata.
1004std::string CompileUnit::getParentContextString(DIScope Context) const {
1005 if (!Context)
1006 return "";
1007
1008 // FIXME: Decide whether to implement this for non-C++ languages.
1009 if (getLanguage() != dwarf::DW_LANG_C_plus_plus)
1010 return "";
1011
Eric Christopher8dba0d52013-10-19 01:04:42 +00001012 std::string CS;
Eric Christopher2c8b7902013-10-17 02:06:06 +00001013 SmallVector<DIScope, 1> Parents;
1014 while (!Context.isCompileUnit()) {
1015 Parents.push_back(Context);
1016 if (Context.getContext())
1017 Context = resolve(Context.getContext());
1018 else
1019 // Structure, etc types will have a NULL context if they're at the top
1020 // level.
1021 break;
1022 }
1023
1024 // Reverse iterate over our list to go from the outermost construct to the
1025 // innermost.
1026 for (SmallVectorImpl<DIScope>::reverse_iterator I = Parents.rbegin(),
1027 E = Parents.rend();
1028 I != E; ++I) {
1029 DIScope Ctx = *I;
1030 StringRef Name = Ctx.getName();
Eric Christopher8dba0d52013-10-19 01:04:42 +00001031 if (!Name.empty()) {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001032 CS += Name;
1033 CS += "::";
1034 }
1035 }
1036 return CS;
Devang Patel0e821f42011-04-12 23:21:44 +00001037}
1038
Eric Christopherfa205ca2013-10-05 00:05:51 +00001039/// addPubTypes - Add subprogram argument types for pubtypes section.
Devang Patel17b53272011-05-06 16:57:54 +00001040void CompileUnit::addPubTypes(DISubprogram SP) {
1041 DICompositeType SPTy = SP.getType();
Eric Christopher31b05762013-08-08 01:41:00 +00001042 uint16_t SPTag = SPTy.getTag();
Devang Patel17b53272011-05-06 16:57:54 +00001043 if (SPTag != dwarf::DW_TAG_subroutine_type)
1044 return;
1045
1046 DIArray Args = SPTy.getTypeArray();
1047 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
1048 DIType ATy(Args.getElement(i));
Manman Ren7504ed42013-07-08 18:33:29 +00001049 if (!ATy.isType())
Devang Patel17b53272011-05-06 16:57:54 +00001050 continue;
Devang Patel562c7422011-06-01 00:23:24 +00001051 addGlobalType(ATy);
Devang Patel17b53272011-05-06 16:57:54 +00001052 }
1053}
1054
Devang Patel0e821f42011-04-12 23:21:44 +00001055/// constructTypeDIE - Construct basic type die from DIBasicType.
1056void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
1057 // Get core information.
1058 StringRef Name = BTy.getName();
Devang Patel0e821f42011-04-12 23:21:44 +00001059 // Add name if not anonymous or intermediate type.
1060 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001061 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel04d6d472011-09-14 23:13:28 +00001062
David Blaikiefac56122013-10-04 23:21:16 +00001063 // An unspecified type only has a name attribute.
1064 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type)
Devang Patel04d6d472011-09-14 23:13:28 +00001065 return;
Devang Patel04d6d472011-09-14 23:13:28 +00001066
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001067 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Pateld925d1a2012-02-07 23:33:58 +00001068 BTy.getEncoding());
Devang Patel04d6d472011-09-14 23:13:28 +00001069
Devang Patel0e821f42011-04-12 23:21:44 +00001070 uint64_t Size = BTy.getSizeInBits() >> 3;
David Blaikief2443192013-10-21 17:28:37 +00001071 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001072}
1073
1074/// constructTypeDIE - Construct derived type die from DIDerivedType.
1075void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
1076 // Get core information.
1077 StringRef Name = DTy.getName();
1078 uint64_t Size = DTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001079 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001080
1081 // Map to main type, void will not have a type.
Manman Ren93b30902013-10-08 18:42:58 +00001082 DIType FromTy = resolve(DTy.getTypeDerivedFrom());
Eric Christopher0df08e22013-08-08 07:40:37 +00001083 if (FromTy)
1084 addType(&Buffer, FromTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001085
1086 // Add name if not anonymous or intermediate type.
1087 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001088 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001089
1090 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher85757902012-02-21 22:25:53 +00001091 if (Size && Tag != dwarf::DW_TAG_pointer_type)
David Blaikief2443192013-10-21 17:28:37 +00001092 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001093
David Blaikie5d3249b2013-01-07 05:51:15 +00001094 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
Eric Christopherc2697f82013-10-19 01:04:47 +00001095 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
1096 getOrCreateTypeDIE(resolve(DTy.getClassType())));
Devang Patel0e821f42011-04-12 23:21:44 +00001097 // Add source line info if available and TyDesc is not a forward declaration.
1098 if (!DTy.isForwardDecl())
1099 addSourceLine(&Buffer, DTy);
1100}
1101
Eric Christopher67646432013-07-26 17:02:41 +00001102/// Return true if the type is appropriately scoped to be contained inside
1103/// its own type unit.
Manman Ren33796c52013-09-09 19:23:58 +00001104static bool isTypeUnitScoped(DIType Ty, const DwarfDebug *DD) {
Manman Ren116868e2013-09-09 19:47:11 +00001105 DIScope Parent = DD->resolve(Ty.getContext());
Eric Christopher67646432013-07-26 17:02:41 +00001106 while (Parent) {
1107 // Don't generate a hash for anything scoped inside a function.
1108 if (Parent.isSubprogram())
1109 return false;
Manman Rende897a362013-09-09 22:35:23 +00001110 Parent = DD->resolve(Parent.getContext());
Eric Christopher67646432013-07-26 17:02:41 +00001111 }
1112 return true;
1113}
1114
1115/// Return true if the type should be split out into a type unit.
Manman Ren33796c52013-09-09 19:23:58 +00001116static bool shouldCreateTypeUnit(DICompositeType CTy, const DwarfDebug *DD) {
Eric Christopher31b05762013-08-08 01:41:00 +00001117 uint16_t Tag = CTy.getTag();
Eric Christopher67646432013-07-26 17:02:41 +00001118
1119 switch (Tag) {
1120 case dwarf::DW_TAG_structure_type:
1121 case dwarf::DW_TAG_union_type:
1122 case dwarf::DW_TAG_enumeration_type:
1123 case dwarf::DW_TAG_class_type:
1124 // If this is a class, structure, union, or enumeration type
David Blaikie93ff1eb2013-10-04 23:52:02 +00001125 // that is a definition (not a declaration), and not scoped
Eric Christopher67646432013-07-26 17:02:41 +00001126 // inside a function then separate this out as a type unit.
David Blaikie93ff1eb2013-10-04 23:52:02 +00001127 return !CTy.isForwardDecl() && isTypeUnitScoped(CTy, DD);
Eric Christopher67646432013-07-26 17:02:41 +00001128 default:
David Blaikie93ff1eb2013-10-04 23:52:02 +00001129 return false;
Eric Christopher67646432013-07-26 17:02:41 +00001130 }
1131}
1132
Devang Patel0e821f42011-04-12 23:21:44 +00001133/// constructTypeDIE - Construct type DIE from DICompositeType.
1134void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
1135 // Get core information.
1136 StringRef Name = CTy.getName();
1137
1138 uint64_t Size = CTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001139 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001140
1141 switch (Tag) {
Devang Patel0e821f42011-04-12 23:21:44 +00001142 case dwarf::DW_TAG_array_type:
Eric Christopherdf9955d2013-11-11 18:52:31 +00001143 constructArrayTypeDIE(Buffer, CTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001144 break;
Eric Christopheraeb105f2013-11-11 18:52:39 +00001145 case dwarf::DW_TAG_enumeration_type:
1146 constructEnumTypeDIE(Buffer, CTy);
1147 break;
Devang Patel0e821f42011-04-12 23:21:44 +00001148 case dwarf::DW_TAG_subroutine_type: {
Eric Christopher0df08e22013-08-08 07:40:37 +00001149 // Add return type. A void return won't have a type.
Devang Patel0e821f42011-04-12 23:21:44 +00001150 DIArray Elements = CTy.getTypeArray();
1151 DIDescriptor RTy = Elements.getElement(0);
Eric Christopher0df08e22013-08-08 07:40:37 +00001152 if (RTy)
1153 addType(&Buffer, DIType(RTy));
Devang Patel0e821f42011-04-12 23:21:44 +00001154
1155 bool isPrototyped = true;
1156 // Add arguments.
1157 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1158 DIDescriptor Ty = Elements.getElement(i);
1159 if (Ty.isUnspecifiedParameter()) {
Manman Renb987e512013-10-29 00:53:03 +00001160 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001161 isPrototyped = false;
1162 } else {
Manman Renb987e512013-10-29 00:53:03 +00001163 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001164 addType(Arg, DIType(Ty));
David Blaikie9a7a7a92013-01-29 19:35:24 +00001165 if (DIType(Ty).isArtificial())
1166 addFlag(Arg, dwarf::DW_AT_artificial);
Devang Patel0e821f42011-04-12 23:21:44 +00001167 }
1168 }
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001169 // Add prototype flag if we're dealing with a C language and the
1170 // function has been prototyped.
Eric Christopherbfceb2f2013-08-26 23:50:38 +00001171 uint16_t Language = DICompileUnit(Node).getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001172 if (isPrototyped &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001173 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopherd42b92f2012-05-22 18:45:24 +00001174 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001175 addFlag(&Buffer, dwarf::DW_AT_prototyped);
Eric Christopherc2697f82013-10-19 01:04:47 +00001176 } break;
Devang Patel0e821f42011-04-12 23:21:44 +00001177 case dwarf::DW_TAG_structure_type:
1178 case dwarf::DW_TAG_union_type:
1179 case dwarf::DW_TAG_class_type: {
Devang Patel0e821f42011-04-12 23:21:44 +00001180 // Add elements to structure type.
David Blaikiea1ae0e62013-08-01 20:30:22 +00001181 DIArray Elements = CTy.getTypeArray();
1182 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel0e821f42011-04-12 23:21:44 +00001183 DIDescriptor Element = Elements.getElement(i);
1184 DIE *ElemDie = NULL;
1185 if (Element.isSubprogram()) {
1186 DISubprogram SP(Element);
Devang Patel89543712011-08-15 17:24:54 +00001187 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
Devang Patel0e821f42011-04-12 23:21:44 +00001188 if (SP.isProtected())
Nick Lewyckycb918492011-12-13 05:09:11 +00001189 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001190 dwarf::DW_ACCESS_protected);
1191 else if (SP.isPrivate())
Nick Lewyckycb918492011-12-13 05:09:11 +00001192 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001193 dwarf::DW_ACCESS_private);
Eric Christopher92331fd2012-11-21 00:34:38 +00001194 else
Nick Lewyckycb918492011-12-13 05:09:11 +00001195 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Eric Christopherc2697f82013-10-19 01:04:47 +00001196 dwarf::DW_ACCESS_public);
Devang Patel0e821f42011-04-12 23:21:44 +00001197 if (SP.isExplicit())
Eric Christopherbb69a272012-08-24 01:14:27 +00001198 addFlag(ElemDie, dwarf::DW_AT_explicit);
Eric Christopher7285c7d2012-03-28 07:34:31 +00001199 } else if (Element.isDerivedType()) {
Eric Christopherd42b92f2012-05-22 18:45:24 +00001200 DIDerivedType DDTy(Element);
1201 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
Manman Renb987e512013-10-29 00:53:03 +00001202 ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
Manman Ren93b30902013-10-08 18:42:58 +00001203 addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()),
Manman Renb3388602013-10-05 01:43:03 +00001204 dwarf::DW_AT_friend);
Manman Renc6b63922013-10-14 20:33:57 +00001205 } else if (DDTy.isStaticMember()) {
Manman Ren57e6ff72013-10-23 22:57:12 +00001206 getOrCreateStaticMemberDIE(DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001207 } else {
Manman Ren230ec862013-10-23 23:00:44 +00001208 constructMemberDIE(Buffer, DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001209 }
Eric Christopher7285c7d2012-03-28 07:34:31 +00001210 } else if (Element.isObjCProperty()) {
Devang Pateld925d1a2012-02-07 23:33:58 +00001211 DIObjCProperty Property(Element);
Manman Renb987e512013-10-29 00:53:03 +00001212 ElemDie = createAndAddDIE(Property.getTag(), Buffer);
Devang Pateld925d1a2012-02-07 23:33:58 +00001213 StringRef PropertyName = Property.getObjCPropertyName();
1214 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Eric Christopherd42b92f2012-05-22 18:45:24 +00001215 addType(ElemDie, Property.getType());
1216 addSourceLine(ElemDie, Property);
Devang Pateld925d1a2012-02-07 23:33:58 +00001217 StringRef GetterName = Property.getObjCPropertyGetterName();
1218 if (!GetterName.empty())
1219 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
1220 StringRef SetterName = Property.getObjCPropertySetterName();
1221 if (!SetterName.empty())
1222 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
1223 unsigned PropertyAttributes = 0;
Devang Patel403e8192012-02-04 01:30:32 +00001224 if (Property.isReadOnlyObjCProperty())
1225 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1226 if (Property.isReadWriteObjCProperty())
1227 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1228 if (Property.isAssignObjCProperty())
1229 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1230 if (Property.isRetainObjCProperty())
1231 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1232 if (Property.isCopyObjCProperty())
1233 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1234 if (Property.isNonAtomicObjCProperty())
1235 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1236 if (PropertyAttributes)
David Blaikief2443192013-10-21 17:28:37 +00001237 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None,
Eric Christopherc2697f82013-10-19 01:04:47 +00001238 PropertyAttributes);
Devang Patel44882172012-02-06 17:49:43 +00001239
Devang Pateld925d1a2012-02-07 23:33:58 +00001240 DIEEntry *Entry = getDIEEntry(Element);
1241 if (!Entry) {
1242 Entry = createDIEEntry(ElemDie);
1243 insertDIEEntry(Element, Entry);
1244 }
Devang Patel403e8192012-02-04 01:30:32 +00001245 } else
Devang Patel0e821f42011-04-12 23:21:44 +00001246 continue;
Devang Patel0e821f42011-04-12 23:21:44 +00001247 }
1248
1249 if (CTy.isAppleBlockExtension())
Eric Christopherbb69a272012-08-24 01:14:27 +00001250 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel0e821f42011-04-12 23:21:44 +00001251
Eric Christopher9e429ae2013-10-05 00:27:02 +00001252 DICompositeType ContainingType(resolve(CTy.getContainingType()));
Devang Patel0e821f42011-04-12 23:21:44 +00001253 if (DIDescriptor(ContainingType).isCompositeType())
Manman Ren4c4b69c2013-10-11 23:58:05 +00001254 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
Devang Patel0e821f42011-04-12 23:21:44 +00001255 getOrCreateTypeDIE(DIType(ContainingType)));
Devang Patel0e821f42011-04-12 23:21:44 +00001256
Devang Patel12419ae2011-05-12 21:29:42 +00001257 if (CTy.isObjcClassComplete())
Eric Christopherbb69a272012-08-24 01:14:27 +00001258 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patel2409e782011-05-12 19:06:16 +00001259
Eric Christopherda011dd2011-12-16 23:42:42 +00001260 // Add template parameters to a class, structure or union types.
1261 // FIXME: The support isn't in the metadata for this yet.
1262 if (Tag == dwarf::DW_TAG_class_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001263 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
Devang Patel0e821f42011-04-12 23:21:44 +00001264 addTemplateParams(Buffer, CTy.getTemplateParams());
1265
1266 break;
1267 }
1268 default:
1269 break;
1270 }
1271
1272 // Add name if not anonymous or intermediate type.
1273 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001274 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001275
Eric Christopher775cbd22012-05-22 18:45:18 +00001276 if (Tag == dwarf::DW_TAG_enumeration_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001277 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
Eric Christopher775cbd22012-05-22 18:45:18 +00001278 Tag == dwarf::DW_TAG_union_type) {
Devang Patel0e821f42011-04-12 23:21:44 +00001279 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher1cf33382012-06-01 00:22:32 +00001280 // TODO: Do we care about size for enum forward declarations?
Devang Patel0e821f42011-04-12 23:21:44 +00001281 if (Size)
David Blaikief2443192013-10-21 17:28:37 +00001282 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Eric Christopher1cf33382012-06-01 00:22:32 +00001283 else if (!CTy.isForwardDecl())
Devang Patel0e821f42011-04-12 23:21:44 +00001284 // Add zero size if it is not a forward declaration.
David Blaikief2443192013-10-21 17:28:37 +00001285 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, 0);
Eric Christopher1cf33382012-06-01 00:22:32 +00001286
1287 // If we're a forward decl, say so.
1288 if (CTy.isForwardDecl())
Eric Christopherbb69a272012-08-24 01:14:27 +00001289 addFlag(&Buffer, dwarf::DW_AT_declaration);
Devang Patel0e821f42011-04-12 23:21:44 +00001290
1291 // Add source line info if available.
1292 if (!CTy.isForwardDecl())
1293 addSourceLine(&Buffer, CTy);
Eric Christopher54cf8ff2012-03-07 00:15:19 +00001294
1295 // No harm in adding the runtime language to the declaration.
1296 unsigned RLang = CTy.getRunTimeLang();
1297 if (RLang)
Eric Christopherc2697f82013-10-19 01:04:47 +00001298 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
1299 RLang);
Devang Patel0e821f42011-04-12 23:21:44 +00001300 }
Eric Christopher67646432013-07-26 17:02:41 +00001301 // If this is a type applicable to a type unit it then add it to the
1302 // list of types we'll compute a hash for later.
Manman Ren33796c52013-09-09 19:23:58 +00001303 if (shouldCreateTypeUnit(CTy, DD))
Eric Christopher67646432013-07-26 17:02:41 +00001304 DD->addTypeUnitType(&Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001305}
1306
Manman Renffc9a712013-10-23 23:05:28 +00001307/// constructTemplateTypeParameterDIE - Construct new DIE for the given
1308/// DITemplateTypeParameter.
Manman Ren57e6ff72013-10-23 22:57:12 +00001309void
Manman Renffc9a712013-10-23 23:05:28 +00001310CompileUnit::constructTemplateTypeParameterDIE(DIE &Buffer,
1311 DITemplateTypeParameter TP) {
Manman Renb987e512013-10-29 00:53:03 +00001312 DIE *ParamDIE =
1313 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
Eric Christopher056b6472013-08-08 08:09:43 +00001314 // Add the type if it exists, it could be void and therefore no type.
1315 if (TP.getType())
Manman Ren88b0f942013-10-09 19:46:28 +00001316 addType(ParamDIE, resolve(TP.getType()));
David Blaikie2b380232013-06-22 18:59:11 +00001317 if (!TP.getName().empty())
1318 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel0e821f42011-04-12 23:21:44 +00001319}
1320
Manman Renffc9a712013-10-23 23:05:28 +00001321/// constructTemplateValueParameterDIE - Construct new DIE for the given
1322/// DITemplateValueParameter.
Manman Ren57e6ff72013-10-23 22:57:12 +00001323void
Manman Renffc9a712013-10-23 23:05:28 +00001324CompileUnit::constructTemplateValueParameterDIE(DIE &Buffer,
1325 DITemplateValueParameter VP) {
Manman Renb987e512013-10-29 00:53:03 +00001326 DIE *ParamDIE = createAndAddDIE(VP.getTag(), Buffer);
Eric Christopher0df08e22013-08-08 07:40:37 +00001327
1328 // Add the type if there is one, template template and template parameter
1329 // packs will not have a type.
Eric Christopher691281b2013-10-21 17:48:51 +00001330 if (VP.getTag() == dwarf::DW_TAG_template_value_parameter)
Manman Ren88b0f942013-10-09 19:46:28 +00001331 addType(ParamDIE, resolve(VP.getType()));
Eric Christopherafb2c412013-08-08 07:40:31 +00001332 if (!VP.getName().empty())
1333 addString(ParamDIE, dwarf::DW_AT_name, VP.getName());
1334 if (Value *Val = VP.getValue()) {
David Blaikiea1e813d2013-05-10 21:52:07 +00001335 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val))
Manman Ren88b0f942013-10-09 19:46:28 +00001336 addConstantValue(ParamDIE, CI,
1337 isUnsignedDIType(DD, resolve(VP.getType())));
David Blaikiea1e813d2013-05-10 21:52:07 +00001338 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) {
1339 // For declaration non-type template parameters (such as global values and
1340 // functions)
1341 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Rafael Espindola79858aa2013-10-29 17:07:16 +00001342 addOpAddress(Block, Asm->getSymbol(GV));
David Blaikiea1e813d2013-05-10 21:52:07 +00001343 // Emit DW_OP_stack_value to use the address as the immediate value of the
1344 // parameter, rather than a pointer to it.
David Blaikief2443192013-10-21 17:28:37 +00001345 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1346 addBlock(ParamDIE, dwarf::DW_AT_location, Block);
Eric Christopherafb2c412013-08-08 07:40:31 +00001347 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) {
David Blaikie2b380232013-06-22 18:59:11 +00001348 assert(isa<MDString>(Val));
1349 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1350 cast<MDString>(Val)->getString());
Eric Christopherafb2c412013-08-08 07:40:31 +00001351 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
David Blaikie2b380232013-06-22 18:59:11 +00001352 assert(isa<MDNode>(Val));
1353 DIArray A(cast<MDNode>(Val));
1354 addTemplateParams(*ParamDIE, A);
David Blaikiea1e813d2013-05-10 21:52:07 +00001355 }
1356 }
Devang Patel0e821f42011-04-12 23:21:44 +00001357}
1358
Devang Patel17b53272011-05-06 16:57:54 +00001359/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1360DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) {
Manman Renf6b936b2013-10-29 05:49:41 +00001361 // Construct the context before querying for the existence of the DIE in case
1362 // such construction creates the DIE.
1363 DIE *ContextDIE = getOrCreateContextDIE(NS.getContext());
Manman Renf6b936b2013-10-29 05:49:41 +00001364
Devang Patel17b53272011-05-06 16:57:54 +00001365 DIE *NDie = getDIE(NS);
1366 if (NDie)
1367 return NDie;
Manman Renf6b936b2013-10-29 05:49:41 +00001368 NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
1369
Eric Christopher4996c702011-11-07 09:24:32 +00001370 if (!NS.getName().empty()) {
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001371 addString(NDie, dwarf::DW_AT_name, NS.getName());
Eric Christopher4996c702011-11-07 09:24:32 +00001372 addAccelNamespace(NS.getName(), NDie);
Eric Christopher2c8b7902013-10-17 02:06:06 +00001373 addGlobalName(NS.getName(), NDie, NS.getContext());
Eric Christopher4996c702011-11-07 09:24:32 +00001374 } else
1375 addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel17b53272011-05-06 16:57:54 +00001376 addSourceLine(NDie, NS);
Devang Patel17b53272011-05-06 16:57:54 +00001377 return NDie;
1378}
1379
Devang Patel89543712011-08-15 17:24:54 +00001380/// getOrCreateSubprogramDIE - Create new DIE using SP.
1381DIE *CompileUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
David Blaikie309ffe42013-10-04 01:39:59 +00001382 // Construct the context before querying for the existence of the DIE in case
1383 // such construction creates the DIE (as is the case for member function
1384 // declarations).
Manman Renc50fa112013-10-10 18:40:01 +00001385 DIE *ContextDIE = getOrCreateContextDIE(resolve(SP.getContext()));
David Blaikie309ffe42013-10-04 01:39:59 +00001386
Eric Christophere595bae2013-10-04 17:08:38 +00001387 DIE *SPDie = getDIE(SP);
Devang Patel89543712011-08-15 17:24:54 +00001388 if (SPDie)
1389 return SPDie;
1390
Manman Ren73d697c2013-10-29 00:58:04 +00001391 DISubprogram SPDecl = SP.getFunctionDeclaration();
1392 if (SPDecl.isSubprogram())
1393 // Add subprogram definitions to the CU die directly.
1394 ContextDIE = CUDie.get();
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001395
1396 // DW_TAG_inlined_subroutine may refer to this DIE.
Manman Ren73d697c2013-10-29 00:58:04 +00001397 SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001398
Rafael Espindola79278362011-11-10 22:34:29 +00001399 DIE *DeclDie = NULL;
Manman Renb9512a72013-10-23 22:12:26 +00001400 if (SPDecl.isSubprogram())
Rafael Espindola79278362011-11-10 22:34:29 +00001401 DeclDie = getOrCreateSubprogramDIE(SPDecl);
Rafael Espindola79278362011-11-10 22:34:29 +00001402
Devang Patel89543712011-08-15 17:24:54 +00001403 // Add function template parameters.
1404 addTemplateParams(*SPDie, SP.getTemplateParams());
1405
Devang Patel89543712011-08-15 17:24:54 +00001406 // If this DIE is going to refer declaration info using AT_specification
Eric Christopherf20ff972013-05-09 00:42:33 +00001407 // then there is no need to add other attributes.
Rafael Espindola79278362011-11-10 22:34:29 +00001408 if (DeclDie) {
1409 // Refer function declaration directly.
Manman Ren4c4b69c2013-10-11 23:58:05 +00001410 addDIEEntry(SPDie, dwarf::DW_AT_specification, DeclDie);
Rafael Espindola79278362011-11-10 22:34:29 +00001411
Devang Patel89543712011-08-15 17:24:54 +00001412 return SPDie;
Rafael Espindola79278362011-11-10 22:34:29 +00001413 }
Devang Patel89543712011-08-15 17:24:54 +00001414
Eric Christopheracb71152012-08-23 22:52:55 +00001415 // Add the linkage name if we have one.
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001416 StringRef LinkageName = SP.getLinkageName();
1417 if (!LinkageName.empty())
Eric Christopheracb71152012-08-23 22:52:55 +00001418 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001419 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopheracb71152012-08-23 22:52:55 +00001420
Devang Patel89543712011-08-15 17:24:54 +00001421 // Constructors and operators for anonymous aggregates do not have names.
1422 if (!SP.getName().empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001423 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Patel89543712011-08-15 17:24:54 +00001424
1425 addSourceLine(SPDie, SP);
1426
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001427 // Add the prototype if we have a prototype and we have a C like
1428 // language.
Eric Christopherbfceb2f2013-08-26 23:50:38 +00001429 uint16_t Language = DICompileUnit(Node).getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001430 if (SP.isPrototyped() &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001431 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001432 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001433 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Patel89543712011-08-15 17:24:54 +00001434
Devang Patel89543712011-08-15 17:24:54 +00001435 DICompositeType SPTy = SP.getType();
David Blaikie5174c842013-05-22 23:22:18 +00001436 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type &&
1437 "the type of a subprogram should be a subroutine");
Devang Patel89543712011-08-15 17:24:54 +00001438
David Blaikie5174c842013-05-22 23:22:18 +00001439 DIArray Args = SPTy.getTypeArray();
Eric Christopher691281b2013-10-21 17:48:51 +00001440 // Add a return type. If this is a type like a C/C++ void type we don't add a
1441 // return type.
Eric Christopher0df08e22013-08-08 07:40:37 +00001442 if (Args.getElement(0))
1443 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel89543712011-08-15 17:24:54 +00001444
1445 unsigned VK = SP.getVirtuality();
1446 if (VK) {
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001447 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Devang Patel89543712011-08-15 17:24:54 +00001448 DIEBlock *Block = getDIEBlock();
David Blaikief2443192013-10-21 17:28:37 +00001449 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1450 addUInt(Block, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1451 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
Eric Christopher98b7f172013-11-11 18:52:36 +00001452 ContainingTypeMap.insert(
1453 std::make_pair(SPDie, resolve(SP.getContainingType())));
Devang Patel89543712011-08-15 17:24:54 +00001454 }
1455
1456 if (!SP.isDefinition()) {
Eric Christopherbb69a272012-08-24 01:14:27 +00001457 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher92331fd2012-11-21 00:34:38 +00001458
Devang Patel89543712011-08-15 17:24:54 +00001459 // Add arguments. Do not add arguments for subprogram definition. They will
1460 // be handled while processing variables.
Eric Christopherc2697f82013-10-19 01:04:47 +00001461 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
Manman Renb987e512013-10-29 00:53:03 +00001462 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, *SPDie);
David Blaikie5174c842013-05-22 23:22:18 +00001463 DIType ATy = DIType(Args.getElement(i));
1464 addType(Arg, ATy);
1465 if (ATy.isArtificial())
1466 addFlag(Arg, dwarf::DW_AT_artificial);
David Blaikie5174c842013-05-22 23:22:18 +00001467 }
Devang Patel89543712011-08-15 17:24:54 +00001468 }
1469
1470 if (SP.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001471 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Patel89543712011-08-15 17:24:54 +00001472
1473 if (!SP.isLocalToUnit())
Eric Christopherbb69a272012-08-24 01:14:27 +00001474 addFlag(SPDie, dwarf::DW_AT_external);
Devang Patel89543712011-08-15 17:24:54 +00001475
1476 if (SP.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +00001477 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Patel89543712011-08-15 17:24:54 +00001478
1479 if (unsigned isa = Asm->getISAEncoding()) {
1480 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1481 }
1482
1483 return SPDie;
1484}
1485
Devang Pateldfd6ec32011-08-15 17:57:41 +00001486// Return const expression if value is a GEP to access merged global
1487// constant. e.g.
1488// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1489static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1490 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1491 if (!CE || CE->getNumOperands() != 3 ||
1492 CE->getOpcode() != Instruction::GetElementPtr)
1493 return NULL;
1494
1495 // First operand points to a global struct.
1496 Value *Ptr = CE->getOperand(0);
1497 if (!isa<GlobalValue>(Ptr) ||
1498 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1499 return NULL;
1500
1501 // Second operand is zero.
1502 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1503 if (!CI || !CI->isZero())
1504 return NULL;
1505
1506 // Third operand is offset.
1507 if (!isa<ConstantInt>(CE->getOperand(2)))
1508 return NULL;
1509
1510 return CE;
1511}
1512
1513/// createGlobalVariableDIE - create global variable DIE.
1514void CompileUnit::createGlobalVariableDIE(const MDNode *N) {
David Blaikie2ad00162013-11-15 23:09:13 +00001515 DIGlobalVariable GV(N);
1516
Devang Pateldfd6ec32011-08-15 17:57:41 +00001517 // Check for pre-existence.
David Blaikie2ad00162013-11-15 23:09:13 +00001518 if (getDIE(GV))
Devang Pateldfd6ec32011-08-15 17:57:41 +00001519 return;
1520
Manman Ren7504ed42013-07-08 18:33:29 +00001521 if (!GV.isGlobalVariable())
Devang Patel0ecbcbd2011-08-18 23:17:55 +00001522 return;
1523
Eric Christopher08f7c8f2013-10-04 23:49:26 +00001524 DIScope GVContext = GV.getContext();
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001525 DIType GTy = GV.getType();
1526
1527 // If this is a static data member definition, some attributes belong
1528 // to the declaration DIE.
1529 DIE *VariableDIE = NULL;
Manman Rene697d3c2013-02-01 23:54:37 +00001530 bool IsStaticMember = false;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001531 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1532 if (SDMDecl.Verify()) {
1533 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1534 // We need the declaration DIE that is in the static member's class.
Manman Renc6b63922013-10-14 20:33:57 +00001535 VariableDIE = getOrCreateStaticMemberDIE(SDMDecl);
Manman Rene697d3c2013-02-01 23:54:37 +00001536 IsStaticMember = true;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001537 }
1538
1539 // If this is not a static data member definition, create the variable
1540 // DIE and add the initial set of attributes to it.
1541 if (!VariableDIE) {
Manman Renf6b936b2013-10-29 05:49:41 +00001542 // Construct the context before querying for the existence of the DIE in
1543 // case such construction creates the DIE.
1544 DIE *ContextDIE = getOrCreateContextDIE(GVContext);
Manman Renf6b936b2013-10-29 05:49:41 +00001545
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001546 // Add to map.
Manman Renf6b936b2013-10-29 05:49:41 +00001547 VariableDIE = createAndAddDIE(GV.getTag(), *ContextDIE, N);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001548
1549 // Add name and type.
1550 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1551 addType(VariableDIE, GTy);
1552
1553 // Add scoping info.
Eric Christopherd2b497b2013-10-16 01:37:49 +00001554 if (!GV.isLocalToUnit())
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001555 addFlag(VariableDIE, dwarf::DW_AT_external);
1556
1557 // Add line number info.
1558 addSourceLine(VariableDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001559 }
1560
Devang Pateldfd6ec32011-08-15 17:57:41 +00001561 // Add location.
Eric Christopher4996c702011-11-07 09:24:32 +00001562 bool addToAccelTable = false;
Eric Christopher0a917b72011-11-11 03:16:32 +00001563 DIE *VariableSpecDIE = NULL;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001564 bool isGlobalVariable = GV.getGlobal() != NULL;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001565 if (isGlobalVariable) {
Eric Christopher4996c702011-11-07 09:24:32 +00001566 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001567 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Rafael Espindola79858aa2013-10-29 17:07:16 +00001568 const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal());
David Blaikief2694972013-06-28 20:05:11 +00001569 if (GV.getGlobal()->isThreadLocal()) {
1570 // FIXME: Make this work with -gsplit-dwarf.
1571 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
1572 assert((PointerSize == 4 || PointerSize == 8) &&
1573 "Add support for other sizes if necessary");
Ulrich Weigand2b6fc8d2013-07-02 18:47:09 +00001574 const MCExpr *Expr =
David Blaikie8466ca82013-07-01 23:55:52 +00001575 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym);
David Blaikief2694972013-06-28 20:05:11 +00001576 // Based on GCC's support for TLS:
David Blaikie8466ca82013-07-01 23:55:52 +00001577 if (!DD->useSplitDwarf()) {
1578 // 1) Start with a constNu of the appropriate pointer size
David Blaikief2443192013-10-21 17:28:37 +00001579 addUInt(Block, dwarf::DW_FORM_data1,
David Blaikie8466ca82013-07-01 23:55:52 +00001580 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
Richard Mitton0aafb582013-10-07 18:39:18 +00001581 // 2) containing the (relocated) offset of the TLS variable
1582 // within the module's TLS block.
David Blaikief2443192013-10-21 17:28:37 +00001583 addExpr(Block, dwarf::DW_FORM_udata, Expr);
David Blaikie8466ca82013-07-01 23:55:52 +00001584 } else {
David Blaikief2443192013-10-21 17:28:37 +00001585 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
1586 addUInt(Block, dwarf::DW_FORM_udata, DU->getAddrPoolIndex(Expr));
David Blaikie8466ca82013-07-01 23:55:52 +00001587 }
Richard Mitton0aafb582013-10-07 18:39:18 +00001588 // 3) followed by a custom OP to make the debugger do a TLS lookup.
David Blaikief2443192013-10-21 17:28:37 +00001589 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address);
David Blaikief2694972013-06-28 20:05:11 +00001590 } else
1591 addOpAddress(Block, Sym);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001592 // Do not create specification DIE if context is either compile unit
1593 // or a subprogram.
Devang Patel5e6b65c2011-09-21 23:41:11 +00001594 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Manman Ren3eb9dff2013-09-09 19:05:21 +00001595 !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001596 // Create specification DIE.
Manman Renb987e512013-10-29 00:53:03 +00001597 VariableSpecDIE = createAndAddDIE(dwarf::DW_TAG_variable, *CUDie.get());
Manman Ren4c4b69c2013-10-11 23:58:05 +00001598 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification, VariableDIE);
David Blaikief2443192013-10-21 17:28:37 +00001599 addBlock(VariableSpecDIE, dwarf::DW_AT_location, Block);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001600 // A static member's declaration is already flagged as such.
1601 if (!SDMDecl.Verify())
1602 addFlag(VariableDIE, dwarf::DW_AT_declaration);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001603 } else {
David Blaikief2443192013-10-21 17:28:37 +00001604 addBlock(VariableDIE, dwarf::DW_AT_location, Block);
Eric Christopher4996c702011-11-07 09:24:32 +00001605 }
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001606 // Add the linkage name.
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001607 StringRef LinkageName = GV.getLinkageName();
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001608 if (!LinkageName.empty())
Eric Christopher3f79b8c2013-02-27 23:49:47 +00001609 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1610 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1611 // TAG_variable.
Eric Christopherc2697f82013-10-19 01:04:47 +00001612 addString(IsStaticMember && VariableSpecDIE ? VariableSpecDIE
1613 : VariableDIE,
1614 dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001615 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopher92331fd2012-11-21 00:34:38 +00001616 } else if (const ConstantInt *CI =
Eric Christopherc2697f82013-10-19 01:04:47 +00001617 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
Adrian Prantl322f41d2013-04-04 22:56:49 +00001618 // AT_const_value was added when the static member was created. To avoid
Manman Rene697d3c2013-02-01 23:54:37 +00001619 // emitting AT_const_value multiple times, we only add AT_const_value when
1620 // it is not a static member.
1621 if (!IsStaticMember)
Manman Renb3388602013-10-05 01:43:03 +00001622 addConstantValue(VariableDIE, CI, isUnsignedDIType(DD, GTy));
Manman Rene697d3c2013-02-01 23:54:37 +00001623 } else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) {
Eric Christopher4996c702011-11-07 09:24:32 +00001624 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001625 // GV is a merged global.
1626 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1627 Value *Ptr = CE->getOperand(0);
Rafael Espindola79858aa2013-10-29 17:07:16 +00001628 addOpAddress(Block, Asm->getSymbol(cast<GlobalValue>(Ptr)));
David Blaikief2443192013-10-21 17:28:37 +00001629 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
Eric Christopherc2697f82013-10-19 01:04:47 +00001630 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
David Blaikief2443192013-10-21 17:28:37 +00001631 addUInt(Block, dwarf::DW_FORM_udata,
Eric Christopherc2697f82013-10-19 01:04:47 +00001632 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
David Blaikief2443192013-10-21 17:28:37 +00001633 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1634 addBlock(VariableDIE, dwarf::DW_AT_location, Block);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001635 }
1636
Eric Christopherc12c2112011-11-11 01:55:22 +00001637 if (addToAccelTable) {
1638 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1639 addAccelName(GV.getName(), AddrDIE);
Eric Christopher4996c702011-11-07 09:24:32 +00001640
Eric Christopherc12c2112011-11-11 01:55:22 +00001641 // If the linkage name is different than the name, go ahead and output
1642 // that as well into the name table.
1643 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1644 addAccelName(GV.getLinkageName(), AddrDIE);
1645 }
Eric Christopherd2b497b2013-10-16 01:37:49 +00001646
1647 if (!GV.isLocalToUnit())
Eric Christopher2c8b7902013-10-17 02:06:06 +00001648 addGlobalName(GV.getName(), VariableSpecDIE ? VariableSpecDIE : VariableDIE,
1649 GV.getContext());
Devang Pateldfd6ec32011-08-15 17:57:41 +00001650}
1651
Devang Patel0e821f42011-04-12 23:21:44 +00001652/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Eric Christopherd42b92f2012-05-22 18:45:24 +00001653void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR,
1654 DIE *IndexTy) {
Manman Renb987e512013-10-29 00:53:03 +00001655 DIE *DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
Manman Ren4c4b69c2013-10-11 23:58:05 +00001656 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, IndexTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001657
Bill Wendling28fe9e72012-12-06 07:38:10 +00001658 // The LowerBound value defines the lower bounds which is typically zero for
1659 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1660 // Count == -1 then the array is unbounded and we do not emit
1661 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1662 // Count == 0, then the array has zero elements in which case we do not emit
1663 // an upper bound.
1664 int64_t LowerBound = SR.getLo();
Bill Wendling3495f9b2012-12-06 07:55:19 +00001665 int64_t DefaultLowerBound = getDefaultLowerBound();
Bill Wendlingd7767122012-12-04 21:34:03 +00001666 int64_t Count = SR.getCount();
Devang Patel0e821f42011-04-12 23:21:44 +00001667
Bill Wendling3495f9b2012-12-06 07:55:19 +00001668 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
David Blaikief2443192013-10-21 17:28:37 +00001669 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound);
Bill Wendling28fe9e72012-12-06 07:38:10 +00001670
1671 if (Count != -1 && Count != 0)
Bill Wendlingd7767122012-12-04 21:34:03 +00001672 // FIXME: An unbounded array should reference the expression that defines
1673 // the array.
Eric Christopher98b7f172013-11-11 18:52:36 +00001674 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None,
1675 LowerBound + Count - 1);
Devang Patel0e821f42011-04-12 23:21:44 +00001676}
1677
1678/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001679void CompileUnit::constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy) {
1680 if (CTy.isVector())
Eric Christopherbb69a272012-08-24 01:14:27 +00001681 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel0e821f42011-04-12 23:21:44 +00001682
Eric Christopher0df08e22013-08-08 07:40:37 +00001683 // Emit the element type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001684 addType(&Buffer, resolve(CTy.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001685
1686 // Get an anonymous type for index type.
Eric Christophercad9b532013-01-04 21:51:53 +00001687 // FIXME: This type should be passed down from the front end
1688 // as different languages may have different sizes for indexes.
Devang Patel0e821f42011-04-12 23:21:44 +00001689 DIE *IdxTy = getIndexTyDie();
1690 if (!IdxTy) {
1691 // Construct an anonymous type for index type.
Manman Renb987e512013-10-29 00:53:03 +00001692 IdxTy = createAndAddDIE(dwarf::DW_TAG_base_type, *CUDie.get());
Eric Christophercad9b532013-01-04 21:51:53 +00001693 addString(IdxTy, dwarf::DW_AT_name, "int");
David Blaikief2443192013-10-21 17:28:37 +00001694 addUInt(IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int32_t));
Devang Patel0e821f42011-04-12 23:21:44 +00001695 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1696 dwarf::DW_ATE_signed);
Devang Patel0e821f42011-04-12 23:21:44 +00001697 setIndexTyDie(IdxTy);
1698 }
1699
1700 // Add subranges to array type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001701 DIArray Elements = CTy.getTypeArray();
Devang Patel0e821f42011-04-12 23:21:44 +00001702 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1703 DIDescriptor Element = Elements.getElement(i);
1704 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1705 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1706 }
1707}
1708
Eric Christopheraeb105f2013-11-11 18:52:39 +00001709/// constructEnumTypeDIE - Construct an enum type DIE from DICompositeType.
1710void CompileUnit::constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy) {
1711 DIArray Elements = CTy.getTypeArray();
1712
1713 // Add enumerators to enumeration type.
1714 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1715 DIDescriptor Enum(Elements.getElement(i));
1716 DIEnumerator ETy = DIEnumerator(Enum);
1717 if (Enum.isEnumerator()) {
1718 DIE *Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
1719 StringRef Name = ETy.getName();
1720 addString(Enumerator, dwarf::DW_AT_name, Name);
1721 int64_t Value = ETy.getEnumValue();
1722 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
1723 }
1724 }
1725 DIType DTy = resolve(CTy.getTypeDerivedFrom());
1726 if (DTy) {
1727 addType(&Buffer, DTy);
1728 addFlag(&Buffer, dwarf::DW_AT_enum_class);
1729 }
Devang Patel0e821f42011-04-12 23:21:44 +00001730}
1731
Devang Patel89543712011-08-15 17:24:54 +00001732/// constructContainingTypeDIEs - Construct DIEs for types that contain
1733/// vtables.
1734void CompileUnit::constructContainingTypeDIEs() {
1735 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Eric Christopherc2697f82013-10-19 01:04:47 +00001736 CE = ContainingTypeMap.end();
1737 CI != CE; ++CI) {
Devang Patel89543712011-08-15 17:24:54 +00001738 DIE *SPDie = CI->first;
David Blaikie2ad00162013-11-15 23:09:13 +00001739 DIDescriptor D(CI->second);
1740 if (!D)
Eric Christopherc2697f82013-10-19 01:04:47 +00001741 continue;
David Blaikie2ad00162013-11-15 23:09:13 +00001742 DIE *NDie = getDIE(D);
Eric Christopherc2697f82013-10-19 01:04:47 +00001743 if (!NDie)
1744 continue;
Manman Ren4c4b69c2013-10-11 23:58:05 +00001745 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, NDie);
Devang Patel89543712011-08-15 17:24:54 +00001746 }
1747}
1748
Devang Patel3acc70e2011-08-15 22:04:40 +00001749/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Eric Christopher34a2c872013-11-15 01:43:19 +00001750DIE *CompileUnit::constructVariableDIE(DbgVariable &DV, bool isScopeAbstract) {
1751 StringRef Name = DV.getName();
Devang Patel3acc70e2011-08-15 22:04:40 +00001752
Devang Patel3acc70e2011-08-15 22:04:40 +00001753 // Define variable debug information entry.
Eric Christopher34a2c872013-11-15 01:43:19 +00001754 DIE *VariableDie = new DIE(DV.getTag());
1755 DbgVariable *AbsVar = DV.getAbstractVariable();
Devang Patel3acc70e2011-08-15 22:04:40 +00001756 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
Manman Ren4213c392013-05-29 17:16:59 +00001757 if (AbsDIE)
Manman Ren4c4b69c2013-10-11 23:58:05 +00001758 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin, AbsDIE);
Devang Patel3acc70e2011-08-15 22:04:40 +00001759 else {
David Blaikie715528b2013-08-19 03:34:03 +00001760 if (!Name.empty())
1761 addString(VariableDie, dwarf::DW_AT_name, Name);
Eric Christopher34a2c872013-11-15 01:43:19 +00001762 addSourceLine(VariableDie, DV.getVariable());
1763 addType(VariableDie, DV.getType());
Devang Patel3acc70e2011-08-15 22:04:40 +00001764 }
1765
Eric Christopher34a2c872013-11-15 01:43:19 +00001766 if (DV.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001767 addFlag(VariableDie, dwarf::DW_AT_artificial);
Devang Patel3acc70e2011-08-15 22:04:40 +00001768
1769 if (isScopeAbstract) {
Eric Christopher34a2c872013-11-15 01:43:19 +00001770 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001771 return VariableDie;
1772 }
1773
1774 // Add variable address.
1775
Eric Christopher34a2c872013-11-15 01:43:19 +00001776 unsigned Offset = DV.getDotDebugLocOffset();
Devang Patel3acc70e2011-08-15 22:04:40 +00001777 if (Offset != ~0U) {
Eric Christopher40b6bf62013-06-24 21:34:55 +00001778 addLabel(VariableDie, dwarf::DW_AT_location, dwarf::DW_FORM_data4,
1779 Asm->GetTempSymbol("debug_loc", Offset));
Eric Christopher34a2c872013-11-15 01:43:19 +00001780 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001781 return VariableDie;
1782 }
1783
Eric Christophercead0332011-10-03 15:49:20 +00001784 // Check if variable is described by a DBG_VALUE instruction.
Eric Christopher34a2c872013-11-15 01:43:19 +00001785 if (const MachineInstr *DVInsn = DV.getMInsn()) {
David Blaikie0252265b2013-06-16 20:34:15 +00001786 assert(DVInsn->getNumOperands() == 3);
1787 if (DVInsn->getOperand(0).isReg()) {
1788 const MachineOperand RegOp = DVInsn->getOperand(0);
Adrian Prantl19942882013-07-09 21:44:06 +00001789 // If the second operand is an immediate, this is an indirect value.
Adrian Prantl418d1d12013-07-09 20:28:37 +00001790 if (DVInsn->getOperand(1).isImm()) {
Eric Christopherc2697f82013-10-19 01:04:47 +00001791 MachineLocation Location(RegOp.getReg(),
1792 DVInsn->getOperand(1).getImm());
Eric Christopher34a2c872013-11-15 01:43:19 +00001793 addVariableAddress(DV, VariableDie, Location);
David Blaikie0252265b2013-06-16 20:34:15 +00001794 } else if (RegOp.getReg())
Eric Christopher34a2c872013-11-15 01:43:19 +00001795 addVariableAddress(DV, VariableDie, MachineLocation(RegOp.getReg()));
David Blaikie0252265b2013-06-16 20:34:15 +00001796 } else if (DVInsn->getOperand(0).isImm())
Eric Christopher34a2c872013-11-15 01:43:19 +00001797 addConstantValue(VariableDie, DVInsn->getOperand(0), DV.getType());
David Blaikie0252265b2013-06-16 20:34:15 +00001798 else if (DVInsn->getOperand(0).isFPImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001799 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
David Blaikie0252265b2013-06-16 20:34:15 +00001800 else if (DVInsn->getOperand(0).isCImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001801 addConstantValue(VariableDie, DVInsn->getOperand(0).getCImm(),
Eric Christopher34a2c872013-11-15 01:43:19 +00001802 isUnsignedDIType(DD, DV.getType()));
Eric Christopher78fcf4902013-07-03 01:08:30 +00001803
Eric Christopher34a2c872013-11-15 01:43:19 +00001804 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001805 return VariableDie;
1806 } else {
1807 // .. else use frame index.
Eric Christopher34a2c872013-11-15 01:43:19 +00001808 int FI = DV.getFrameIndex();
Devang Patel3acc70e2011-08-15 22:04:40 +00001809 if (FI != ~0) {
1810 unsigned FrameReg = 0;
1811 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopherc2697f82013-10-19 01:04:47 +00001812 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
Devang Patel3acc70e2011-08-15 22:04:40 +00001813 MachineLocation Location(FrameReg, Offset);
Eric Christopher34a2c872013-11-15 01:43:19 +00001814 addVariableAddress(DV, VariableDie, Location);
Devang Patel3acc70e2011-08-15 22:04:40 +00001815 }
1816 }
1817
Eric Christopher34a2c872013-11-15 01:43:19 +00001818 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001819 return VariableDie;
1820}
1821
Manman Ren230ec862013-10-23 23:00:44 +00001822/// constructMemberDIE - Construct member DIE from DIDerivedType.
1823void CompileUnit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) {
Manman Renb987e512013-10-29 00:53:03 +00001824 DIE *MemberDie = createAndAddDIE(DT.getTag(), Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001825 StringRef Name = DT.getName();
1826 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001827 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001828
Manman Ren93b30902013-10-08 18:42:58 +00001829 addType(MemberDie, resolve(DT.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001830
1831 addSourceLine(MemberDie, DT);
1832
1833 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
David Blaikief2443192013-10-21 17:28:37 +00001834 addUInt(MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel0e821f42011-04-12 23:21:44 +00001835
Eric Christopherc2697f82013-10-19 01:04:47 +00001836 if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) {
Devang Patel0e821f42011-04-12 23:21:44 +00001837
1838 // For C++, virtual base classes are not at fixed offset. Use following
1839 // expression to extract appropriate offset from vtable.
1840 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1841
1842 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
David Blaikief2443192013-10-21 17:28:37 +00001843 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1844 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1845 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1846 addUInt(VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1847 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1848 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1849 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
Devang Patel0e821f42011-04-12 23:21:44 +00001850
David Blaikief2443192013-10-21 17:28:37 +00001851 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
David Blaikie71d34a22013-11-01 00:25:45 +00001852 } else {
1853 uint64_t Size = DT.getSizeInBits();
1854 uint64_t FieldSize = getBaseTypeSize(DD, DT);
1855 uint64_t OffsetInBytes;
1856
1857 if (Size != FieldSize) {
1858 // Handle bitfield.
1859 addUInt(MemberDie, dwarf::DW_AT_byte_size, None,
1860 getBaseTypeSize(DD, DT) >> 3);
1861 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, DT.getSizeInBits());
1862
1863 uint64_t Offset = DT.getOffsetInBits();
1864 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1865 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1866 uint64_t FieldOffset = (HiMark - FieldSize);
1867 Offset -= FieldOffset;
1868
1869 // Maybe we need to work from the other end.
1870 if (Asm->getDataLayout().isLittleEndian())
1871 Offset = FieldSize - (Offset + Size);
1872 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
1873
1874 // Here WD_AT_data_member_location points to the anonymous
1875 // field that includes this bit field.
1876 OffsetInBytes = FieldOffset >> 3;
1877 } else
1878 // This is not a bitfield.
1879 OffsetInBytes = DT.getOffsetInBits() >> 3;
Eric Christopher98b7f172013-11-11 18:52:36 +00001880 addUInt(MemberDie, dwarf::DW_AT_data_member_location, None, OffsetInBytes);
David Blaikie71d34a22013-11-01 00:25:45 +00001881 }
Devang Patel0e821f42011-04-12 23:21:44 +00001882
1883 if (DT.isProtected())
Nick Lewyckycb918492011-12-13 05:09:11 +00001884 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001885 dwarf::DW_ACCESS_protected);
1886 else if (DT.isPrivate())
Nick Lewyckycb918492011-12-13 05:09:11 +00001887 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001888 dwarf::DW_ACCESS_private);
1889 // Otherwise C++ member and base classes are considered public.
Eric Christopher92331fd2012-11-21 00:34:38 +00001890 else
Nick Lewyckycb918492011-12-13 05:09:11 +00001891 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001892 dwarf::DW_ACCESS_public);
1893 if (DT.isVirtual())
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001894 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001895 dwarf::DW_VIRTUALITY_virtual);
Devang Patel514b4002011-04-16 00:11:51 +00001896
1897 // Objective-C properties.
Devang Patel44882172012-02-06 17:49:43 +00001898 if (MDNode *PNode = DT.getObjCProperty())
1899 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
Eric Christopher92331fd2012-11-21 00:34:38 +00001900 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
Devang Patel44882172012-02-06 17:49:43 +00001901 PropertyDie);
1902
David Blaikie37fefc32012-12-13 22:43:07 +00001903 if (DT.isArtificial())
1904 addFlag(MemberDie, dwarf::DW_AT_artificial);
Devang Patel0e821f42011-04-12 23:21:44 +00001905}
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001906
Manman Renc6b63922013-10-14 20:33:57 +00001907/// getOrCreateStaticMemberDIE - Create new DIE for C++ static member.
David Blaikie2ad00162013-11-15 23:09:13 +00001908DIE *CompileUnit::getOrCreateStaticMemberDIE(DIDerivedType DT) {
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001909 if (!DT.Verify())
1910 return NULL;
1911
Manman Renc6b63922013-10-14 20:33:57 +00001912 // Construct the context before querying for the existence of the DIE in case
1913 // such construction creates the DIE.
1914 DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext()));
David Blaikiebd700e42013-11-14 21:24:34 +00001915 assert(dwarf::isType(ContextDIE->getTag()) &&
1916 "Static member should belong to a type.");
Manman Renc6b63922013-10-14 20:33:57 +00001917
1918 DIE *StaticMemberDIE = getDIE(DT);
1919 if (StaticMemberDIE)
1920 return StaticMemberDIE;
1921
Manman Renb987e512013-10-29 00:53:03 +00001922 StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT);
Manman Renc6b63922013-10-14 20:33:57 +00001923
Manman Ren93b30902013-10-08 18:42:58 +00001924 DIType Ty = resolve(DT.getTypeDerivedFrom());
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001925
1926 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1927 addType(StaticMemberDIE, Ty);
1928 addSourceLine(StaticMemberDIE, DT);
1929 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1930 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1931
1932 // FIXME: We could omit private if the parent is a class_type, and
1933 // public if the parent is something else.
1934 if (DT.isProtected())
1935 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1936 dwarf::DW_ACCESS_protected);
1937 else if (DT.isPrivate())
1938 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1939 dwarf::DW_ACCESS_private);
1940 else
1941 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1942 dwarf::DW_ACCESS_public);
1943
1944 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
Manman Renb3388602013-10-05 01:43:03 +00001945 addConstantValue(StaticMemberDIE, CI, isUnsignedDIType(DD, Ty));
David Blaikiea39a76e2013-01-20 01:18:01 +00001946 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
1947 addConstantFPValue(StaticMemberDIE, CFP);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001948
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001949 return StaticMemberDIE;
1950}
David Blaikie6b288cf2013-10-30 20:42:41 +00001951
1952void CompileUnit::emitHeader(const MCSection *ASection,
1953 const MCSymbol *ASectionSym) {
1954 Asm->OutStreamer.AddComment("DWARF version number");
1955 Asm->EmitInt16(DD->getDwarfVersion());
1956 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
1957 Asm->EmitSectionOffset(Asm->GetTempSymbol(ASection->getLabelBeginName()),
1958 ASectionSym);
1959 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1960 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
1961}