blob: 893df15e50b84786c733ac49d73dbd1fdc1d6433 [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"
David Blaikie409dd9c2013-11-19 23:08:21 +000032#include "llvm/Support/CommandLine.h"
Devang Patel0e821f42011-04-12 23:21:44 +000033
34using namespace llvm;
35
David Blaikie409dd9c2013-11-19 23:08:21 +000036static cl::opt<bool> GenerateTypeUnits("generate-type-units", cl::Hidden,
37 cl::desc("Generate DWARF4 type units."),
38 cl::init(false));
39
Devang Patel0e821f42011-04-12 23:21:44 +000040/// CompileUnit - Compile unit constructor.
David Blaikie5a152402013-11-15 23:52:02 +000041CompileUnit::CompileUnit(unsigned UID, DIE *D, DICompileUnit Node,
42 AsmPrinter *A, DwarfDebug *DW, DwarfUnits *DWU)
Eric Christophera16725b2013-11-21 01:29:16 +000043 : UniqueID(UID), Node(Node), Language(Node.getLanguage()), CUDie(D),
44 DebugInfoOffset(0), Asm(A), DD(DW), DU(DWU), IndexTyDie(0) {
Devang Patel0e821f42011-04-12 23:21:44 +000045 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
David Blaikie5a152402013-11-15 23:52:02 +000046 insertDIE(Node, D);
Devang Patel0e821f42011-04-12 23:21:44 +000047}
48
David Blaikie409dd9c2013-11-19 23:08:21 +000049CompileUnit::CompileUnit(unsigned UID, DIE *D, uint16_t Language, AsmPrinter *A,
50 DwarfDebug *DD, DwarfUnits *DU)
Eric Christophera16725b2013-11-21 01:29:16 +000051 : UniqueID(UID), Node(NULL), Language(Language), CUDie(D),
52 DebugInfoOffset(0), Asm(A), DD(DD), DU(DU), IndexTyDie(0) {
David Blaikie409dd9c2013-11-19 23:08:21 +000053 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
54}
55
Devang Patel0e821f42011-04-12 23:21:44 +000056/// ~CompileUnit - Destructor for compile unit.
57CompileUnit::~CompileUnit() {
58 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
59 DIEBlocks[j]->~DIEBlock();
60}
61
62/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
63/// information entry.
64DIEEntry *CompileUnit::createDIEEntry(DIE *Entry) {
65 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
66 return Value;
67}
68
Bill Wendling3495f9b2012-12-06 07:55:19 +000069/// getDefaultLowerBound - Return the default lower bound for an array. If the
Bill Wendling28fe9e72012-12-06 07:38:10 +000070/// DWARF version doesn't handle the language, return -1.
Bill Wendling3495f9b2012-12-06 07:55:19 +000071int64_t CompileUnit::getDefaultLowerBound() const {
David Blaikiecb8e4352013-11-15 23:50:53 +000072 switch (getLanguage()) {
Bill Wendling28fe9e72012-12-06 07:38:10 +000073 default:
74 break;
75
76 case dwarf::DW_LANG_C89:
77 case dwarf::DW_LANG_C99:
78 case dwarf::DW_LANG_C:
79 case dwarf::DW_LANG_C_plus_plus:
80 case dwarf::DW_LANG_ObjC:
81 case dwarf::DW_LANG_ObjC_plus_plus:
82 return 0;
83
84 case dwarf::DW_LANG_Fortran77:
85 case dwarf::DW_LANG_Fortran90:
86 case dwarf::DW_LANG_Fortran95:
87 return 1;
88
89 // The languages below have valid values only if the DWARF version >= 4.
90 case dwarf::DW_LANG_Java:
91 case dwarf::DW_LANG_Python:
92 case dwarf::DW_LANG_UPC:
93 case dwarf::DW_LANG_D:
94 if (dwarf::DWARF_VERSION >= 4)
95 return 0;
96 break;
97
98 case dwarf::DW_LANG_Ada83:
99 case dwarf::DW_LANG_Ada95:
100 case dwarf::DW_LANG_Cobol74:
101 case dwarf::DW_LANG_Cobol85:
102 case dwarf::DW_LANG_Modula2:
103 case dwarf::DW_LANG_Pascal83:
104 case dwarf::DW_LANG_PLI:
105 if (dwarf::DWARF_VERSION >= 4)
106 return 1;
107 break;
108 }
109
110 return -1;
111}
112
Manman Ren4dbdc902013-10-31 17:54:35 +0000113/// Check whether the DIE for this MDNode can be shared across CUs.
David Blaikie4201ddf2013-11-15 22:59:36 +0000114static bool isShareableAcrossCUs(DIDescriptor D) {
David Blaikiebcb418e2013-11-20 18:40:16 +0000115 // When the MDNode can be part of the type system, the DIE can be shared
116 // across CUs.
117 // Combining type units and cross-CU DIE sharing is lower value (since
118 // cross-CU DIE sharing is used in LTO and removes type redundancy at that
119 // level already) but may be implementable for some value in projects
120 // building multiple independent libraries with LTO and then linking those
121 // together.
David Blaikie409dd9c2013-11-19 23:08:21 +0000122 return (D.isType() ||
123 (D.isSubprogram() && !DISubprogram(D).isDefinition())) &&
124 !GenerateTypeUnits;
Manman Ren4dbdc902013-10-31 17:54:35 +0000125}
126
127/// getDIE - Returns the debug information entry map slot for the
128/// specified debug variable. We delegate the request to DwarfDebug
129/// when the DIE for this MDNode can be shared across CUs. The mappings
130/// will be kept in DwarfDebug for shareable DIEs.
David Blaikie2ad00162013-11-15 23:09:13 +0000131DIE *CompileUnit::getDIE(DIDescriptor D) const {
132 if (isShareableAcrossCUs(D))
133 return DD->getDIE(D);
134 return MDNodeToDieMap.lookup(D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000135}
136
137/// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
138/// when the DIE for this MDNode can be shared across CUs. The mappings
139/// will be kept in DwarfDebug for shareable DIEs.
David Blaikie2ad00162013-11-15 23:09:13 +0000140void CompileUnit::insertDIE(DIDescriptor Desc, DIE *D) {
141 if (isShareableAcrossCUs(Desc)) {
142 DD->insertDIE(Desc, D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000143 return;
144 }
David Blaikie2ad00162013-11-15 23:09:13 +0000145 MDNodeToDieMap.insert(std::make_pair(Desc, D));
Manman Ren4dbdc902013-10-31 17:54:35 +0000146}
147
Eric Christopherbb69a272012-08-24 01:14:27 +0000148/// addFlag - Add a flag that is true.
David Blaikief2443192013-10-21 17:28:37 +0000149void CompileUnit::addFlag(DIE *Die, dwarf::Attribute Attribute) {
Michael Gottesmanc89466f2013-09-04 04:39:38 +0000150 if (DD->getDwarfVersion() >= 4)
Eric Christopherdccd3282013-10-04 22:40:05 +0000151 Die->addValue(Attribute, dwarf::DW_FORM_flag_present, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000152 else
Eric Christopherdccd3282013-10-04 22:40:05 +0000153 Die->addValue(Attribute, dwarf::DW_FORM_flag, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000154}
155
Devang Patel0e821f42011-04-12 23:21:44 +0000156/// addUInt - Add an unsigned integer attribute data and value.
157///
David Blaikief2443192013-10-21 17:28:37 +0000158void CompileUnit::addUInt(DIE *Die, dwarf::Attribute Attribute,
159 Optional<dwarf::Form> Form, uint64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000160 if (!Form)
161 Form = DIEInteger::BestForm(false, Integer);
162 DIEValue *Value = Integer == 1 ? DIEIntegerOne : new (DIEValueAllocator)
163 DIEInteger(Integer);
David Blaikief2443192013-10-21 17:28:37 +0000164 Die->addValue(Attribute, *Form, Value);
165}
166
167void CompileUnit::addUInt(DIEBlock *Block, dwarf::Form Form, uint64_t Integer) {
168 addUInt(Block, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000169}
170
171/// addSInt - Add an signed integer attribute data and value.
172///
David Blaikief2443192013-10-21 17:28:37 +0000173void CompileUnit::addSInt(DIE *Die, dwarf::Attribute Attribute,
174 Optional<dwarf::Form> Form, int64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000175 if (!Form)
176 Form = DIEInteger::BestForm(true, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000177 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
David Blaikief2443192013-10-21 17:28:37 +0000178 Die->addValue(Attribute, *Form, Value);
179}
180
181void CompileUnit::addSInt(DIEBlock *Die, Optional<dwarf::Form> Form,
182 int64_t Integer) {
183 addSInt(Die, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000184}
185
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000186/// addString - Add a string attribute data and value. We always emit a
187/// reference to the string pool instead of immediate strings so that DIEs have
Eric Christopherfba22602013-01-07 19:32:45 +0000188/// more predictable sizes. In the case of split dwarf we emit an index
189/// into another table which gets us the static offset into the string
190/// table.
Eric Christophere6c6c4d2013-11-11 18:52:33 +0000191void CompileUnit::addString(DIE *Die, dwarf::Attribute Attribute,
192 StringRef String) {
Eric Christopher67646432013-07-26 17:02:41 +0000193 DIEValue *Value;
David Blaikief2443192013-10-21 17:28:37 +0000194 dwarf::Form Form;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000195 if (!DD->useSplitDwarf()) {
196 MCSymbol *Symb = DU->getStringPoolEntry(String);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000197 if (Asm->needsRelocationsForDwarfStringPool())
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000198 Value = new (DIEValueAllocator) DIELabel(Symb);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000199 else {
200 MCSymbol *StringPool = DU->getStringPoolSym();
201 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
202 }
Eric Christopher67646432013-07-26 17:02:41 +0000203 Form = dwarf::DW_FORM_strp;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000204 } else {
205 unsigned idx = DU->getStringPoolIndex(String);
Eric Christopher67646432013-07-26 17:02:41 +0000206 Value = new (DIEValueAllocator) DIEInteger(idx);
207 Form = dwarf::DW_FORM_GNU_str_index;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000208 }
Eric Christopher67646432013-07-26 17:02:41 +0000209 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
210 Die->addValue(Attribute, Form, Str);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000211}
212
213/// addLocalString - Add a string attribute data and value. This is guaranteed
214/// to be in the local string pool instead of indirected.
David Blaikief2443192013-10-21 17:28:37 +0000215void CompileUnit::addLocalString(DIE *Die, dwarf::Attribute Attribute,
Eric Christopher2cbd5762013-01-07 19:32:41 +0000216 StringRef String) {
Eric Christophere698f532012-12-20 21:58:36 +0000217 MCSymbol *Symb = DU->getStringPoolEntry(String);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000218 DIEValue *Value;
219 if (Asm->needsRelocationsForDwarfStringPool())
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000220 Value = new (DIEValueAllocator) DIELabel(Symb);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000221 else {
Eric Christophere698f532012-12-20 21:58:36 +0000222 MCSymbol *StringPool = DU->getStringPoolSym();
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000223 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000224 }
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000225 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000226}
227
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000228/// addExpr - Add a Dwarf expression attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000229///
David Blaikief2443192013-10-21 17:28:37 +0000230void CompileUnit::addExpr(DIEBlock *Die, dwarf::Form Form, const MCExpr *Expr) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000231 DIEValue *Value = new (DIEValueAllocator) DIEExpr(Expr);
David Blaikief2443192013-10-21 17:28:37 +0000232 Die->addValue((dwarf::Attribute)0, Form, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000233}
234
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000235/// addLabel - Add a Dwarf label attribute data and value.
236///
Eric Christophere6c6c4d2013-11-11 18:52:33 +0000237void CompileUnit::addLabel(DIE *Die, dwarf::Attribute Attribute,
238 dwarf::Form Form, const MCSymbol *Label) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000239 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
240 Die->addValue(Attribute, Form, Value);
David Blaikief3cd7c52013-06-28 20:05:04 +0000241}
242
David Blaikief2443192013-10-21 17:28:37 +0000243void CompileUnit::addLabel(DIEBlock *Die, dwarf::Form Form,
244 const MCSymbol *Label) {
245 addLabel(Die, (dwarf::Attribute)0, Form, Label);
246}
247
Eric Christopher33ff6972013-11-21 23:46:41 +0000248/// addSectionLabel - Add a Dwarf section label attribute data and value.
249///
250void CompileUnit::addSectionLabel(DIE *Die, dwarf::Attribute Attribute,
251 const MCSymbol *Label) {
252 if (DD->getDwarfVersion() >= 4)
253 addLabel(Die, Attribute, dwarf::DW_FORM_sec_offset, Label);
254 else
255 addLabel(Die, Attribute, dwarf::DW_FORM_data4, Label);
256}
257
258/// addSectionOffset - Add an offset into a section attribute data and value.
259///
260void CompileUnit::addSectionOffset(DIE *Die, dwarf::Attribute Attribute,
261 uint64_t Integer) {
262 if (DD->getDwarfVersion() >= 4)
263 addUInt(Die, Attribute, dwarf::DW_FORM_sec_offset, Integer);
264 else
265 addUInt(Die, Attribute, dwarf::DW_FORM_data4, Integer);
266}
267
Eric Christopher962c9082013-01-15 23:56:56 +0000268/// addLabelAddress - Add a dwarf label attribute data and value using
269/// DW_FORM_addr or DW_FORM_GNU_addr_index.
270///
David Blaikief2443192013-10-21 17:28:37 +0000271void CompileUnit::addLabelAddress(DIE *Die, dwarf::Attribute Attribute,
Eric Christopher962c9082013-01-15 23:56:56 +0000272 MCSymbol *Label) {
Alexey Samsonov4436bf02013-10-03 08:54:43 +0000273 if (Label)
274 DD->addArangeLabel(SymbolCU(this, Label));
Richard Mitton21101b32013-09-19 23:21:01 +0000275
Eric Christopher962c9082013-01-15 23:56:56 +0000276 if (!DD->useSplitDwarf()) {
277 if (Label != NULL) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000278 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
Eric Christopher962c9082013-01-15 23:56:56 +0000279 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
280 } else {
281 DIEValue *Value = new (DIEValueAllocator) DIEInteger(0);
282 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
283 }
284 } else {
285 unsigned idx = DU->getAddrPoolIndex(Label);
286 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
287 Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
288 }
289}
290
Eric Christophere9ec2452013-01-18 22:11:33 +0000291/// addOpAddress - Add a dwarf op address data and value using the
292/// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
293///
David Blaikief2443192013-10-21 17:28:37 +0000294void CompileUnit::addOpAddress(DIEBlock *Die, const MCSymbol *Sym) {
Alexey Samsonov4436bf02013-10-03 08:54:43 +0000295 DD->addArangeLabel(SymbolCU(this, Sym));
Eric Christophere9ec2452013-01-18 22:11:33 +0000296 if (!DD->useSplitDwarf()) {
David Blaikief2443192013-10-21 17:28:37 +0000297 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
298 addLabel(Die, dwarf::DW_FORM_udata, Sym);
Eric Christophere9ec2452013-01-18 22:11:33 +0000299 } else {
David Blaikief2443192013-10-21 17:28:37 +0000300 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
301 addUInt(Die, dwarf::DW_FORM_GNU_addr_index, DU->getAddrPoolIndex(Sym));
Eric Christophere9ec2452013-01-18 22:11:33 +0000302 }
303}
304
Eric Christopher33ff6972013-11-21 23:46:41 +0000305/// addSectionDelta - Add a section label delta attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000306///
Eric Christopher33ff6972013-11-21 23:46:41 +0000307void CompileUnit::addSectionDelta(DIE *Die, dwarf::Attribute Attribute,
308 const MCSymbol *Hi, const MCSymbol *Lo) {
Devang Patel0e821f42011-04-12 23:21:44 +0000309 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Eric Christopher33ff6972013-11-21 23:46:41 +0000310 if (DD->getDwarfVersion() >= 4)
311 Die->addValue(Attribute, dwarf::DW_FORM_sec_offset, Value);
312 else
313 Die->addValue(Attribute, dwarf::DW_FORM_data4, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000314}
315
316/// addDIEEntry - Add a DIE attribute data and value.
317///
Eric Christopher98b7f172013-11-11 18:52:36 +0000318void CompileUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute,
319 DIE *Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +0000320 addDIEEntry(Die, Attribute, createDIEEntry(Entry));
321}
322
323void CompileUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute,
324 DIEEntry *Entry) {
David Blaikie409dd9c2013-11-19 23:08:21 +0000325 const DIE *DieCU = Die->getUnitOrNull();
326 const DIE *EntryCU = Entry->getEntry()->getUnitOrNull();
Manman Ren4dbdc902013-10-31 17:54:35 +0000327 if (!DieCU)
328 // We assume that Die belongs to this CU, if it is not linked to any CU yet.
329 DieCU = getCUDie();
330 if (!EntryCU)
331 EntryCU = getCUDie();
332 Die->addValue(Attribute, EntryCU == DieCU ? dwarf::DW_FORM_ref4
333 : dwarf::DW_FORM_ref_addr,
334 Entry);
Devang Patel0e821f42011-04-12 23:21:44 +0000335}
336
Manman Renb987e512013-10-29 00:53:03 +0000337/// Create a DIE with the given Tag, add the DIE to its parent, and
338/// call insertDIE if MD is not null.
David Blaikie52c50202013-11-16 00:29:01 +0000339DIE *CompileUnit::createAndAddDIE(unsigned Tag, DIE &Parent, DIDescriptor N) {
Manman Renb987e512013-10-29 00:53:03 +0000340 DIE *Die = new DIE(Tag);
341 Parent.addChild(Die);
David Blaikie52c50202013-11-16 00:29:01 +0000342 if (N)
343 insertDIE(N, Die);
Manman Renb987e512013-10-29 00:53:03 +0000344 return Die;
345}
346
Devang Patel0e821f42011-04-12 23:21:44 +0000347/// addBlock - Add block data.
348///
David Blaikief2443192013-10-21 17:28:37 +0000349void CompileUnit::addBlock(DIE *Die, dwarf::Attribute Attribute,
Devang Patel0e821f42011-04-12 23:21:44 +0000350 DIEBlock *Block) {
351 Block->ComputeSize(Asm);
352 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
353 Die->addValue(Attribute, Block->BestForm(), Block);
354}
355
356/// addSourceLine - Add location information to specified debug information
357/// entry.
358void CompileUnit::addSourceLine(DIE *Die, DIVariable V) {
359 // Verify variable.
Manman Ren7504ed42013-07-08 18:33:29 +0000360 if (!V.isVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000361 return;
Eric Christopher92331fd2012-11-21 00:34:38 +0000362
Devang Patel0e821f42011-04-12 23:21:44 +0000363 unsigned Line = V.getLineNumber();
364 if (Line == 0)
365 return;
Eric Christopherc2697f82013-10-19 01:04:47 +0000366 unsigned FileID =
367 DD->getOrCreateSourceID(V.getContext().getFilename(),
368 V.getContext().getDirectory(), getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000369 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000370 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
371 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000372}
373
374/// addSourceLine - Add location information to specified debug information
375/// entry.
376void CompileUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
377 // Verify global variable.
Manman Ren7504ed42013-07-08 18:33:29 +0000378 if (!G.isGlobalVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000379 return;
380
381 unsigned Line = G.getLineNumber();
382 if (Line == 0)
383 return;
Eric Christopherc2697f82013-10-19 01:04:47 +0000384 unsigned FileID =
385 DD->getOrCreateSourceID(G.getFilename(), G.getDirectory(), getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000386 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000387 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
388 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000389}
390
391/// addSourceLine - Add location information to specified debug information
392/// entry.
393void CompileUnit::addSourceLine(DIE *Die, DISubprogram SP) {
394 // Verify subprogram.
Manman Ren7504ed42013-07-08 18:33:29 +0000395 if (!SP.isSubprogram())
Devang Patel0e821f42011-04-12 23:21:44 +0000396 return;
Eric Christopher7734ca22012-03-15 23:55:40 +0000397
Devang Patel0e821f42011-04-12 23:21:44 +0000398 // If the line number is 0, don't add it.
Eric Christopher7734ca22012-03-15 23:55:40 +0000399 unsigned Line = SP.getLineNumber();
400 if (Line == 0)
Devang Patel0e821f42011-04-12 23:21:44 +0000401 return;
402
Eric Christopherc2697f82013-10-19 01:04:47 +0000403 unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(), SP.getDirectory(),
404 getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000405 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000406 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
407 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000408}
409
410/// addSourceLine - Add location information to specified debug information
411/// entry.
412void CompileUnit::addSourceLine(DIE *Die, DIType Ty) {
413 // Verify type.
Manman Ren7504ed42013-07-08 18:33:29 +0000414 if (!Ty.isType())
Devang Patel0e821f42011-04-12 23:21:44 +0000415 return;
416
417 unsigned Line = Ty.getLineNumber();
Eric Christopher7734ca22012-03-15 23:55:40 +0000418 if (Line == 0)
Devang Patel0e821f42011-04-12 23:21:44 +0000419 return;
Eric Christopherc2697f82013-10-19 01:04:47 +0000420 unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(), Ty.getDirectory(),
421 getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000422 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000423 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
424 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000425}
426
427/// addSourceLine - Add location information to specified debug information
428/// entry.
Eric Christopher70e1bd82012-03-29 08:42:56 +0000429void CompileUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
430 // Verify type.
Manman Ren7504ed42013-07-08 18:33:29 +0000431 if (!Ty.isObjCProperty())
Eric Christopher70e1bd82012-03-29 08:42:56 +0000432 return;
433
434 unsigned Line = Ty.getLineNumber();
435 if (Line == 0)
436 return;
437 DIFile File = Ty.getFile();
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000438 unsigned FileID = DD->getOrCreateSourceID(File.getFilename(),
Manman Ren1e427202013-03-07 01:42:00 +0000439 File.getDirectory(), getUniqueID());
Eric Christopher70e1bd82012-03-29 08:42:56 +0000440 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000441 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
442 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Eric Christopher70e1bd82012-03-29 08:42:56 +0000443}
444
445/// addSourceLine - Add location information to specified debug information
446/// entry.
Devang Patel0e821f42011-04-12 23:21:44 +0000447void CompileUnit::addSourceLine(DIE *Die, DINameSpace NS) {
448 // Verify namespace.
449 if (!NS.Verify())
450 return;
451
452 unsigned Line = NS.getLineNumber();
453 if (Line == 0)
454 return;
455 StringRef FN = NS.getFilename();
456
Eric Christopherc2697f82013-10-19 01:04:47 +0000457 unsigned FileID =
458 DD->getOrCreateSourceID(FN, NS.getDirectory(), getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000459 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000460 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
461 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000462}
463
Eric Christopher92331fd2012-11-21 00:34:38 +0000464/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patel77dc5412011-04-27 22:45:24 +0000465/// DbgVariable based on provided MachineLocation.
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000466void CompileUnit::addVariableAddress(const DbgVariable &DV, DIE *Die,
Devang Patel77dc5412011-04-27 22:45:24 +0000467 MachineLocation Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000468 if (DV.variableHasComplexAddress())
Devang Patel0e821f42011-04-12 23:21:44 +0000469 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000470 else if (DV.isBlockByrefVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000471 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
472 else
David Blaikieea2605d2013-06-20 00:25:24 +0000473 addAddress(Die, dwarf::DW_AT_location, Location,
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000474 DV.getVariable().isIndirect());
Devang Patel0e821f42011-04-12 23:21:44 +0000475}
476
Devang Patelba5fbf12011-04-26 19:06:18 +0000477/// addRegisterOp - Add register operand.
David Blaikief2443192013-10-21 17:28:37 +0000478void CompileUnit::addRegisterOp(DIEBlock *TheDie, unsigned Reg) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000479 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
480 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
481 if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000482 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000483 else {
David Blaikief2443192013-10-21 17:28:37 +0000484 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
485 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000486 }
487}
488
489/// addRegisterOffset - Add register offset.
David Blaikief2443192013-10-21 17:28:37 +0000490void CompileUnit::addRegisterOffset(DIEBlock *TheDie, unsigned Reg,
491 int64_t Offset) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000492 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
493 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
494 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
495 if (Reg == TRI->getFrameRegister(*Asm->MF))
496 // If variable offset is based in frame register then use fbreg.
David Blaikief2443192013-10-21 17:28:37 +0000497 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000498 else if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000499 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000500 else {
David Blaikief2443192013-10-21 17:28:37 +0000501 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
502 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000503 }
David Blaikief2443192013-10-21 17:28:37 +0000504 addSInt(TheDie, dwarf::DW_FORM_sdata, Offset);
Devang Patelba5fbf12011-04-26 19:06:18 +0000505}
506
507/// addAddress - Add an address attribute to a die based on the location
508/// provided.
David Blaikief2443192013-10-21 17:28:37 +0000509void CompileUnit::addAddress(DIE *Die, dwarf::Attribute Attribute,
David Blaikieea2605d2013-06-20 00:25:24 +0000510 const MachineLocation &Location, bool Indirect) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000511 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
512
David Blaikieea2605d2013-06-20 00:25:24 +0000513 if (Location.isReg() && !Indirect)
Devang Patelba5fbf12011-04-26 19:06:18 +0000514 addRegisterOp(Block, Location.getReg());
David Blaikieea2605d2013-06-20 00:25:24 +0000515 else {
Devang Patelba5fbf12011-04-26 19:06:18 +0000516 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
David Blaikieea2605d2013-06-20 00:25:24 +0000517 if (Indirect && !Location.isReg()) {
David Blaikief2443192013-10-21 17:28:37 +0000518 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
David Blaikieea2605d2013-06-20 00:25:24 +0000519 }
520 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000521
522 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000523 addBlock(Die, Attribute, Block);
Devang Patelba5fbf12011-04-26 19:06:18 +0000524}
525
Devang Patel0e821f42011-04-12 23:21:44 +0000526/// addComplexAddress - Start with the address based on the location provided,
527/// and generate the DWARF information necessary to find the actual variable
528/// given the extra address information encoded in the DIVariable, starting from
529/// the starting location. Add the DWARF information to the die.
530///
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000531void CompileUnit::addComplexAddress(const DbgVariable &DV, DIE *Die,
David Blaikief2443192013-10-21 17:28:37 +0000532 dwarf::Attribute Attribute,
Devang Patel0e821f42011-04-12 23:21:44 +0000533 const MachineLocation &Location) {
Devang Patel0e821f42011-04-12 23:21:44 +0000534 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000535 unsigned N = DV.getNumAddrElements();
Devang Patel3e021532011-04-28 02:22:40 +0000536 unsigned i = 0;
537 if (Location.isReg()) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000538 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Devang Patel3e021532011-04-28 02:22:40 +0000539 // If first address element is OpPlus then emit
540 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000541 addRegisterOffset(Block, Location.getReg(), DV.getAddrElement(1));
Devang Patel3e021532011-04-28 02:22:40 +0000542 i = 2;
543 } else
544 addRegisterOp(Block, Location.getReg());
Eric Christopherc2697f82013-10-19 01:04:47 +0000545 } else
Devang Patelba5fbf12011-04-26 19:06:18 +0000546 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000547
Eric Christopherc2697f82013-10-19 01:04:47 +0000548 for (; i < N; ++i) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000549 uint64_t Element = DV.getAddrElement(i);
Devang Patel0e821f42011-04-12 23:21:44 +0000550 if (Element == DIBuilder::OpPlus) {
David Blaikief2443192013-10-21 17:28:37 +0000551 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
552 addUInt(Block, dwarf::DW_FORM_udata, DV.getAddrElement(++i));
Devang Patel0e821f42011-04-12 23:21:44 +0000553 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher4d250522012-05-08 18:56:00 +0000554 if (!Location.isReg())
David Blaikief2443192013-10-21 17:28:37 +0000555 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Eric Christopherc2697f82013-10-19 01:04:47 +0000556 } else
557 llvm_unreachable("unknown DIBuilder Opcode");
Devang Patel0e821f42011-04-12 23:21:44 +0000558 }
559
560 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000561 addBlock(Die, Attribute, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000562}
563
564/* Byref variables, in Blocks, are declared by the programmer as "SomeType
565 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
566 gives the variable VarName either the struct, or a pointer to the struct, as
567 its type. This is necessary for various behind-the-scenes things the
568 compiler needs to do with by-reference variables in Blocks.
569
570 However, as far as the original *programmer* is concerned, the variable
571 should still have type 'SomeType', as originally declared.
572
573 The function getBlockByrefType dives into the __Block_byref_x_VarName
574 struct to find the original type of the variable, which is then assigned to
575 the variable's Debug Information Entry as its real type. So far, so good.
576 However now the debugger will expect the variable VarName to have the type
577 SomeType. So we need the location attribute for the variable to be an
578 expression that explains to the debugger how to navigate through the
579 pointers and struct to find the actual variable of type SomeType.
580
581 The following function does just that. We start by getting
582 the "normal" location for the variable. This will be the location
583 of either the struct __Block_byref_x_VarName or the pointer to the
584 struct __Block_byref_x_VarName.
585
586 The struct will look something like:
587
588 struct __Block_byref_x_VarName {
589 ... <various fields>
590 struct __Block_byref_x_VarName *forwarding;
591 ... <various other fields>
592 SomeType VarName;
593 ... <maybe more fields>
594 };
595
596 If we are given the struct directly (as our starting point) we
597 need to tell the debugger to:
598
599 1). Add the offset of the forwarding field.
600
601 2). Follow that pointer to get the real __Block_byref_x_VarName
602 struct to use (the real one may have been copied onto the heap).
603
604 3). Add the offset for the field VarName, to find the actual variable.
605
606 If we started with a pointer to the struct, then we need to
607 dereference that pointer first, before the other steps.
608 Translating this into DWARF ops, we will need to append the following
609 to the current location description for the variable:
610
611 DW_OP_deref -- optional, if we start with a pointer
612 DW_OP_plus_uconst <forward_fld_offset>
613 DW_OP_deref
614 DW_OP_plus_uconst <varName_fld_offset>
615
616 That is what this function does. */
617
618/// addBlockByrefAddress - Start with the address based on the location
619/// provided, and generate the DWARF information necessary to find the
620/// actual Block variable (navigating the Block struct) based on the
621/// starting location. Add the DWARF information to the die. For
622/// more information, read large comment just above here.
623///
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000624void CompileUnit::addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
David Blaikief2443192013-10-21 17:28:37 +0000625 dwarf::Attribute Attribute,
Devang Patel0e821f42011-04-12 23:21:44 +0000626 const MachineLocation &Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000627 DIType Ty = DV.getType();
Devang Patel0e821f42011-04-12 23:21:44 +0000628 DIType TmpTy = Ty;
Eric Christopher31b05762013-08-08 01:41:00 +0000629 uint16_t Tag = Ty.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +0000630 bool isPointer = false;
631
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000632 StringRef varName = DV.getName();
Devang Patel0e821f42011-04-12 23:21:44 +0000633
634 if (Tag == dwarf::DW_TAG_pointer_type) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000635 DIDerivedType DTy(Ty);
Manman Ren93b30902013-10-08 18:42:58 +0000636 TmpTy = resolve(DTy.getTypeDerivedFrom());
Devang Patel0e821f42011-04-12 23:21:44 +0000637 isPointer = true;
638 }
639
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000640 DICompositeType blockStruct(TmpTy);
Devang Patel0e821f42011-04-12 23:21:44 +0000641
642 // Find the __forwarding field and the variable field in the __Block_byref
643 // struct.
644 DIArray Fields = blockStruct.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000645 DIDerivedType varField;
646 DIDerivedType forwardingField;
Devang Patel0e821f42011-04-12 23:21:44 +0000647
648 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000649 DIDerivedType DT(Fields.getElement(i));
Devang Patel0e821f42011-04-12 23:21:44 +0000650 StringRef fieldName = DT.getName();
651 if (fieldName == "__forwarding")
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000652 forwardingField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000653 else if (fieldName == varName)
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000654 varField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000655 }
656
657 // Get the offsets for the forwarding field and the variable field.
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000658 unsigned forwardingFieldOffset = forwardingField.getOffsetInBits() >> 3;
659 unsigned varFieldOffset = varField.getOffsetInBits() >> 2;
Devang Patel0e821f42011-04-12 23:21:44 +0000660
661 // Decode the original location, and use that as the start of the byref
662 // variable's location.
Devang Patel0e821f42011-04-12 23:21:44 +0000663 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
664
Eric Christopheref9d7102012-07-04 02:02:18 +0000665 if (Location.isReg())
666 addRegisterOp(Block, Location.getReg());
667 else
668 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000669
670 // If we started with a pointer to the __Block_byref... struct, then
671 // the first thing we need to do is dereference the pointer (DW_OP_deref).
672 if (isPointer)
David Blaikief2443192013-10-21 17:28:37 +0000673 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000674
675 // Next add the offset for the '__forwarding' field:
676 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
677 // adding the offset if it's 0.
678 if (forwardingFieldOffset > 0) {
David Blaikief2443192013-10-21 17:28:37 +0000679 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
680 addUInt(Block, dwarf::DW_FORM_udata, forwardingFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000681 }
682
683 // Now dereference the __forwarding field to get to the real __Block_byref
684 // struct: DW_OP_deref.
David Blaikief2443192013-10-21 17:28:37 +0000685 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000686
687 // Now that we've got the real __Block_byref... struct, add the offset
688 // for the variable's field to get to the location of the actual variable:
689 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
690 if (varFieldOffset > 0) {
David Blaikief2443192013-10-21 17:28:37 +0000691 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
692 addUInt(Block, dwarf::DW_FORM_udata, varFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000693 }
694
695 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000696 addBlock(Die, Attribute, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000697}
698
Devang Patelbcd50a12011-07-20 21:57:04 +0000699/// isTypeSigned - Return true if the type is signed.
Manman Renb3388602013-10-05 01:43:03 +0000700static bool isTypeSigned(DwarfDebug *DD, DIType Ty, int *SizeInBits) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000701 if (Ty.isDerivedType())
Manman Renb3388602013-10-05 01:43:03 +0000702 return isTypeSigned(DD, DD->resolve(DIDerivedType(Ty).getTypeDerivedFrom()),
703 SizeInBits);
Devang Patelbcd50a12011-07-20 21:57:04 +0000704 if (Ty.isBasicType())
Eric Christopherc2697f82013-10-19 01:04:47 +0000705 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed ||
706 DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000707 *SizeInBits = Ty.getSizeInBits();
708 return true;
709 }
710 return false;
711}
712
Manman Renb3388602013-10-05 01:43:03 +0000713/// Return true if type encoding is unsigned.
714static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) {
715 DIDerivedType DTy(Ty);
716 if (DTy.isDerivedType())
717 return isUnsignedDIType(DD, DD->resolve(DTy.getTypeDerivedFrom()));
718
719 DIBasicType BTy(Ty);
720 if (BTy.isBasicType()) {
721 unsigned Encoding = BTy.getEncoding();
722 if (Encoding == dwarf::DW_ATE_unsigned ||
723 Encoding == dwarf::DW_ATE_unsigned_char ||
724 Encoding == dwarf::DW_ATE_boolean)
725 return true;
726 }
727 return false;
728}
729
730/// If this type is derived from a base type then return base type size.
Manman Renbda410f2013-10-08 18:46:58 +0000731static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) {
Manman Renb3388602013-10-05 01:43:03 +0000732 unsigned Tag = Ty.getTag();
733
734 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
735 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
736 Tag != dwarf::DW_TAG_restrict_type)
737 return Ty.getSizeInBits();
738
739 DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom());
740
741 // If this type is not derived from any type then take conservative approach.
742 if (!BaseType.isValid())
743 return Ty.getSizeInBits();
744
745 // If this is a derived type, go ahead and get the base type, unless it's a
746 // reference then it's just the size of the field. Pointer types have no need
747 // of this since they're a different type of qualification on the type.
748 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
749 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
750 return Ty.getSizeInBits();
751
752 if (BaseType.isDerivedType())
Manman Renbda410f2013-10-08 18:46:58 +0000753 return getBaseTypeSize(DD, DIDerivedType(BaseType));
Manman Renb3388602013-10-05 01:43:03 +0000754
755 return BaseType.getSizeInBits();
756}
757
Devang Patel0e821f42011-04-12 23:21:44 +0000758/// addConstantValue - Add constant value entry in variable DIE.
Eric Christopher78fcf4902013-07-03 01:08:30 +0000759void CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
Devang Patel3c6aed22011-05-27 16:45:18 +0000760 DIType Ty) {
David Blaikiea1e813d2013-05-10 21:52:07 +0000761 // FIXME: This is a bit conservative/simple - it emits negative values at
762 // their maximum bit width which is a bit unfortunate (& doesn't prefer
763 // udata/sdata over dataN as suggested by the DWARF spec)
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000764 assert(MO.isImm() && "Invalid machine operand!");
Devang Patelbcd50a12011-07-20 21:57:04 +0000765 int SizeInBits = -1;
Manman Renb3388602013-10-05 01:43:03 +0000766 bool SignedConstant = isTypeSigned(DD, Ty, &SizeInBits);
David Blaikief2443192013-10-21 17:28:37 +0000767 dwarf::Form Form;
Devang Patelf1d04702011-05-27 18:15:52 +0000768
Eric Christopher9d1daa82013-08-27 23:49:04 +0000769 // If we're a signed constant definitely use sdata.
770 if (SignedConstant) {
771 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, MO.getImm());
772 return;
773 }
774
775 // Else use data for now unless it's larger than we can deal with.
776 switch (SizeInBits) {
777 case 8:
778 Form = dwarf::DW_FORM_data1;
779 break;
780 case 16:
781 Form = dwarf::DW_FORM_data2;
782 break;
783 case 32:
784 Form = dwarf::DW_FORM_data4;
785 break;
786 case 64:
787 Form = dwarf::DW_FORM_data8;
788 break;
789 default:
790 Form = dwarf::DW_FORM_udata;
791 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
792 return;
793 }
794 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
Devang Patel0e821f42011-04-12 23:21:44 +0000795}
796
797/// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christopher78fcf4902013-07-03 01:08:30 +0000798void CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000799 assert(MO.isFPImm() && "Invalid machine operand!");
Devang Patel0e821f42011-04-12 23:21:44 +0000800 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
801 APFloat FPImm = MO.getFPImm()->getValueAPF();
802
803 // Get the raw data form of the floating point.
804 const APInt FltVal = FPImm.bitcastToAPInt();
Eric Christopherc2697f82013-10-19 01:04:47 +0000805 const char *FltPtr = (const char *)FltVal.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000806
807 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000808 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000809 int Incr = (LittleEndian ? 1 : -1);
810 int Start = (LittleEndian ? 0 : NumBytes - 1);
811 int Stop = (LittleEndian ? NumBytes : -1);
812
813 // Output the constant to DWARF one byte at a time.
814 for (; Start != Stop; Start += Incr)
Eric Christopher98b7f172013-11-11 18:52:36 +0000815 addUInt(Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]);
Devang Patel0e821f42011-04-12 23:21:44 +0000816
David Blaikief2443192013-10-21 17:28:37 +0000817 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000818}
819
David Blaikiea39a76e2013-01-20 01:18:01 +0000820/// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christopher78fcf4902013-07-03 01:08:30 +0000821void CompileUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000822 // Pass this down to addConstantValue as an unsigned bag of bits.
823 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
David Blaikiea39a76e2013-01-20 01:18:01 +0000824}
825
Devang Patel0e821f42011-04-12 23:21:44 +0000826/// addConstantValue - Add constant value entry in variable DIE.
Eric Christopher78fcf4902013-07-03 01:08:30 +0000827void CompileUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
Devang Patel0e821f42011-04-12 23:21:44 +0000828 bool Unsigned) {
Eric Christopher78fcf4902013-07-03 01:08:30 +0000829 addConstantValue(Die, CI->getValue(), Unsigned);
David Blaikiea39a76e2013-01-20 01:18:01 +0000830}
831
832// addConstantValue - Add constant value entry in variable DIE.
Eric Christopher78fcf4902013-07-03 01:08:30 +0000833void CompileUnit::addConstantValue(DIE *Die, const APInt &Val, bool Unsigned) {
David Blaikiea39a76e2013-01-20 01:18:01 +0000834 unsigned CIBitWidth = Val.getBitWidth();
Devang Patel8816bbc2011-05-28 00:39:18 +0000835 if (CIBitWidth <= 64) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000836 // If we're a signed constant definitely use sdata.
837 if (!Unsigned) {
838 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
839 Val.getSExtValue());
840 return;
Devang Patel8816bbc2011-05-28 00:39:18 +0000841 }
Eric Christopher9d1daa82013-08-27 23:49:04 +0000842
843 // Else use data for now unless it's larger than we can deal with.
David Blaikief2443192013-10-21 17:28:37 +0000844 dwarf::Form Form;
Eric Christopher9d1daa82013-08-27 23:49:04 +0000845 switch (CIBitWidth) {
846 case 8:
847 Form = dwarf::DW_FORM_data1;
848 break;
849 case 16:
850 Form = dwarf::DW_FORM_data2;
851 break;
852 case 32:
853 Form = dwarf::DW_FORM_data4;
854 break;
855 case 64:
856 Form = dwarf::DW_FORM_data8;
857 break;
858 default:
859 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
860 Val.getZExtValue());
861 return;
862 }
863 addUInt(Die, dwarf::DW_AT_const_value, Form, Val.getZExtValue());
Eric Christopher78fcf4902013-07-03 01:08:30 +0000864 return;
Devang Patel0e821f42011-04-12 23:21:44 +0000865 }
866
867 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
868
869 // Get the raw data form of the large APInt.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000870 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000871
872 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000873 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000874
875 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000876 for (int i = 0; i < NumBytes; i++) {
877 uint8_t c;
878 if (LittleEndian)
879 c = Ptr64[i / 8] >> (8 * (i & 7));
880 else
881 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
David Blaikief2443192013-10-21 17:28:37 +0000882 addUInt(Block, dwarf::DW_FORM_data1, c);
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000883 }
Devang Patel0e821f42011-04-12 23:21:44 +0000884
David Blaikief2443192013-10-21 17:28:37 +0000885 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000886}
887
Eric Christopher25e35092013-04-22 07:47:40 +0000888/// addTemplateParams - Add template parameters into buffer.
Devang Patel0e821f42011-04-12 23:21:44 +0000889void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
890 // Add template parameters.
891 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
892 DIDescriptor Element = TParams.getElement(i);
893 if (Element.isTemplateTypeParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000894 constructTemplateTypeParameterDIE(Buffer,
895 DITemplateTypeParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000896 else if (Element.isTemplateValueParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000897 constructTemplateValueParameterDIE(Buffer,
898 DITemplateValueParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000899 }
Devang Patel0e821f42011-04-12 23:21:44 +0000900}
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000901
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000902/// getOrCreateContextDIE - Get context owner's DIE.
Eric Christopher08f7c8f2013-10-04 23:49:26 +0000903DIE *CompileUnit::getOrCreateContextDIE(DIScope Context) {
David Blaikiebd700e42013-11-14 21:24:34 +0000904 if (!Context || Context.isFile())
905 return getCUDie();
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000906 if (Context.isType())
907 return getOrCreateTypeDIE(DIType(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000908 if (Context.isNameSpace())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000909 return getOrCreateNameSpace(DINameSpace(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000910 if (Context.isSubprogram())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000911 return getOrCreateSubprogramDIE(DISubprogram(Context));
Adrian Prantl7d828bb2013-11-15 21:05:09 +0000912 return getDIE(Context);
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000913}
914
David Blaikie409dd9c2013-11-19 23:08:21 +0000915DIE *CompileUnit::createTypeDIE(DICompositeType Ty) {
David Blaikie9d861be2013-11-26 00:15:27 +0000916 DIScope Context = resolve(Ty.getContext());
917 DIE *ContextDIE = getOrCreateContextDIE(Context);
David Blaikie409dd9c2013-11-19 23:08:21 +0000918
919 DIE *TyDIE = getDIE(Ty);
920 if (TyDIE)
921 return TyDIE;
922
923 // Create new type.
924 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
925
David Blaikiefbd29eb2013-11-26 00:35:04 +0000926 constructTypeDIE(*TyDIE, Ty);
David Blaikie409dd9c2013-11-19 23:08:21 +0000927
David Blaikie9d861be2013-11-26 00:15:27 +0000928 updateAcceleratorTables(Context, Ty, TyDIE);
David Blaikie409dd9c2013-11-19 23:08:21 +0000929 return TyDIE;
930}
931
David Blaikie8a263cb2013-11-26 00:22:37 +0000932/// Return true if the type is appropriately scoped to be contained inside
933/// its own type unit.
934static bool isTypeUnitScoped(DIType Ty, const DwarfDebug *DD) {
935 DIScope Parent = DD->resolve(Ty.getContext());
936 while (Parent) {
937 // Don't generate a hash for anything scoped inside a function.
938 if (Parent.isSubprogram())
939 return false;
940 Parent = DD->resolve(Parent.getContext());
941 }
942 return true;
943}
944
945/// Return true if the type should be split out into a type unit.
946static bool shouldCreateTypeUnit(DICompositeType CTy, const DwarfDebug *DD) {
947 if (!GenerateTypeUnits)
948 return false;
949
950 uint16_t Tag = CTy.getTag();
951
952 switch (Tag) {
953 case dwarf::DW_TAG_structure_type:
954 case dwarf::DW_TAG_union_type:
955 case dwarf::DW_TAG_enumeration_type:
956 case dwarf::DW_TAG_class_type:
957 // If this is a class, structure, union, or enumeration type
958 // that is a definition (not a declaration), and not scoped
959 // inside a function then separate this out as a type unit.
960 return !CTy.isForwardDecl() && isTypeUnitScoped(CTy, DD);
961 default:
962 return false;
963 }
964}
965
Devang Patel0e821f42011-04-12 23:21:44 +0000966/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
967/// given DIType.
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000968DIE *CompileUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
David Blaikie32887552013-11-14 22:25:02 +0000969 if (!TyNode)
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000970 return NULL;
Manman Renf4c339e2013-10-29 22:49:29 +0000971
David Blaikie32887552013-11-14 22:25:02 +0000972 DIType Ty(TyNode);
973 assert(Ty.isType());
974
Manman Renf4c339e2013-10-29 22:49:29 +0000975 // Construct the context before querying for the existence of the DIE in case
976 // such construction creates the DIE.
David Blaikie9d861be2013-11-26 00:15:27 +0000977 DIScope Context = resolve(Ty.getContext());
978 DIE *ContextDIE = getOrCreateContextDIE(Context);
Adrian Prantl4583f7d2013-11-15 23:21:39 +0000979 assert(ContextDIE);
Manman Renf4c339e2013-10-29 22:49:29 +0000980
Eric Christophere595bae2013-10-04 17:08:38 +0000981 DIE *TyDIE = getDIE(Ty);
Devang Patel0e821f42011-04-12 23:21:44 +0000982 if (TyDIE)
983 return TyDIE;
984
985 // Create new type.
Manman Renf4c339e2013-10-29 22:49:29 +0000986 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
987
Devang Patel0e821f42011-04-12 23:21:44 +0000988 if (Ty.isBasicType())
989 constructTypeDIE(*TyDIE, DIBasicType(Ty));
David Blaikie8a263cb2013-11-26 00:22:37 +0000990 else if (Ty.isCompositeType()) {
991 DICompositeType CTy(Ty);
992 if (shouldCreateTypeUnit(CTy, DD)) {
993 DD->addTypeUnitType(TyDIE, CTy);
994 // Skip updating the accellerator tables since this is not the full type
995 return TyDIE;
996 }
David Blaikiefbd29eb2013-11-26 00:35:04 +0000997 constructTypeDIE(*TyDIE, CTy);
David Blaikie8a263cb2013-11-26 00:22:37 +0000998 } else {
Devang Patel0e821f42011-04-12 23:21:44 +0000999 assert(Ty.isDerivedType() && "Unknown kind of DIType");
1000 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
1001 }
David Blaikie2ea848b2013-11-19 22:51:04 +00001002
David Blaikie9d861be2013-11-26 00:15:27 +00001003 updateAcceleratorTables(Context, Ty, TyDIE);
David Blaikie2ea848b2013-11-19 22:51:04 +00001004
1005 return TyDIE;
1006}
1007
David Blaikie9d861be2013-11-26 00:15:27 +00001008void CompileUnit::updateAcceleratorTables(DIScope Context, DIType Ty,
1009 const DIE *TyDIE) {
Eric Christopher21bde872012-01-06 04:35:23 +00001010 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
1011 bool IsImplementation = 0;
1012 if (Ty.isCompositeType()) {
1013 DICompositeType CT(Ty);
Eric Christopher8ea8e4f2012-01-06 23:03:37 +00001014 // A runtime language of 0 actually means C/C++ and that any
1015 // non-negative value is some version of Objective-C/C++.
Eric Christopherc2697f82013-10-19 01:04:47 +00001016 IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete();
Eric Christopher21bde872012-01-06 04:35:23 +00001017 }
Eric Christophercf7289f2013-09-05 18:20:16 +00001018 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
Eric Christopher8ea8e4f2012-01-06 23:03:37 +00001019 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
David Blaikie9d861be2013-11-26 00:15:27 +00001020
1021 if (!Context || Context.isCompileUnit() || Context.isFile() ||
1022 Context.isNameSpace())
1023 GlobalTypes[getParentContextString(Context) + Ty.getName().str()] = TyDIE;
Eric Christopher21bde872012-01-06 04:35:23 +00001024 }
Devang Patel0e821f42011-04-12 23:21:44 +00001025}
1026
1027/// addType - Add a new type attribute to the specified entity.
David Blaikief2443192013-10-21 17:28:37 +00001028void CompileUnit::addType(DIE *Entity, DIType Ty, dwarf::Attribute Attribute) {
Eric Christopher0df08e22013-08-08 07:40:37 +00001029 assert(Ty && "Trying to add a type that doesn't exist?");
Devang Patel0e821f42011-04-12 23:21:44 +00001030
1031 // Check for pre-existence.
1032 DIEEntry *Entry = getDIEEntry(Ty);
1033 // If it exists then use the existing value.
1034 if (Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +00001035 addDIEEntry(Entity, Attribute, Entry);
Devang Patel0e821f42011-04-12 23:21:44 +00001036 return;
1037 }
1038
1039 // Construct type.
1040 DIE *Buffer = getOrCreateTypeDIE(Ty);
1041
1042 // Set up proxy.
1043 Entry = createDIEEntry(Buffer);
1044 insertDIEEntry(Ty, Entry);
Manman Ren4dbdc902013-10-31 17:54:35 +00001045 addDIEEntry(Entity, Attribute, Entry);
Devang Patel1cb8ab42011-05-31 23:30:30 +00001046}
1047
Eric Christopher9cd26af2013-09-20 23:22:52 +00001048// Accelerator table mutators - add each name along with its companion
1049// DIE to the proper table while ensuring that the name that we're going
1050// to reference is in the string table. We do this since the names we
1051// add may not only be identical to the names in the DIE.
David Blaikie2ea848b2013-11-19 22:51:04 +00001052void CompileUnit::addAccelName(StringRef Name, const DIE *Die) {
Eric Christopher9cd26af2013-09-20 23:22:52 +00001053 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001054 std::vector<const DIE *> &DIEs = AccelNames[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001055 DIEs.push_back(Die);
1056}
1057
David Blaikie2ea848b2013-11-19 22:51:04 +00001058void CompileUnit::addAccelObjC(StringRef Name, const DIE *Die) {
Eric Christopher9cd26af2013-09-20 23:22:52 +00001059 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001060 std::vector<const DIE *> &DIEs = AccelObjC[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001061 DIEs.push_back(Die);
1062}
1063
David Blaikie2ea848b2013-11-19 22:51:04 +00001064void CompileUnit::addAccelNamespace(StringRef Name, const DIE *Die) {
Eric Christopher9cd26af2013-09-20 23:22:52 +00001065 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001066 std::vector<const DIE *> &DIEs = AccelNamespace[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001067 DIEs.push_back(Die);
1068}
1069
David Blaikie2ea848b2013-11-19 22:51:04 +00001070void CompileUnit::addAccelType(StringRef Name,
1071 std::pair<const DIE *, unsigned> Die) {
Eric Christopher9cd26af2013-09-20 23:22:52 +00001072 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001073 std::vector<std::pair<const DIE *, unsigned> > &DIEs = AccelTypes[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001074 DIEs.push_back(Die);
1075}
1076
Eric Christopher9c58f312013-09-20 22:20:55 +00001077/// addGlobalName - Add a new global name to the compile unit.
Eric Christopher2c8b7902013-10-17 02:06:06 +00001078void CompileUnit::addGlobalName(StringRef Name, DIE *Die, DIScope Context) {
Eric Christopher8dba0d52013-10-19 01:04:42 +00001079 std::string FullName = getParentContextString(Context) + Name.str();
Eric Christopher2c8b7902013-10-17 02:06:06 +00001080 GlobalNames[FullName] = Die;
Eric Christopher9c58f312013-09-20 22:20:55 +00001081}
1082
Eric Christopher2c8b7902013-10-17 02:06:06 +00001083/// getParentContextString - Walks the metadata parent chain in a language
1084/// specific manner (using the compile unit language) and returns
1085/// it as a string. This is done at the metadata level because DIEs may
1086/// not currently have been added to the parent context and walking the
1087/// DIEs looking for names is more expensive than walking the metadata.
1088std::string CompileUnit::getParentContextString(DIScope Context) const {
1089 if (!Context)
1090 return "";
1091
1092 // FIXME: Decide whether to implement this for non-C++ languages.
1093 if (getLanguage() != dwarf::DW_LANG_C_plus_plus)
1094 return "";
1095
Eric Christopher8dba0d52013-10-19 01:04:42 +00001096 std::string CS;
Eric Christopher2c8b7902013-10-17 02:06:06 +00001097 SmallVector<DIScope, 1> Parents;
1098 while (!Context.isCompileUnit()) {
1099 Parents.push_back(Context);
1100 if (Context.getContext())
1101 Context = resolve(Context.getContext());
1102 else
1103 // Structure, etc types will have a NULL context if they're at the top
1104 // level.
1105 break;
1106 }
1107
1108 // Reverse iterate over our list to go from the outermost construct to the
1109 // innermost.
1110 for (SmallVectorImpl<DIScope>::reverse_iterator I = Parents.rbegin(),
1111 E = Parents.rend();
1112 I != E; ++I) {
1113 DIScope Ctx = *I;
1114 StringRef Name = Ctx.getName();
Eric Christopher8dba0d52013-10-19 01:04:42 +00001115 if (!Name.empty()) {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001116 CS += Name;
1117 CS += "::";
1118 }
1119 }
1120 return CS;
Devang Patel0e821f42011-04-12 23:21:44 +00001121}
1122
1123/// constructTypeDIE - Construct basic type die from DIBasicType.
1124void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
1125 // Get core information.
1126 StringRef Name = BTy.getName();
Devang Patel0e821f42011-04-12 23:21:44 +00001127 // Add name if not anonymous or intermediate type.
1128 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001129 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel04d6d472011-09-14 23:13:28 +00001130
David Blaikiefac56122013-10-04 23:21:16 +00001131 // An unspecified type only has a name attribute.
1132 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type)
Devang Patel04d6d472011-09-14 23:13:28 +00001133 return;
Devang Patel04d6d472011-09-14 23:13:28 +00001134
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001135 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Pateld925d1a2012-02-07 23:33:58 +00001136 BTy.getEncoding());
Devang Patel04d6d472011-09-14 23:13:28 +00001137
Devang Patel0e821f42011-04-12 23:21:44 +00001138 uint64_t Size = BTy.getSizeInBits() >> 3;
David Blaikief2443192013-10-21 17:28:37 +00001139 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001140}
1141
1142/// constructTypeDIE - Construct derived type die from DIDerivedType.
1143void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
1144 // Get core information.
1145 StringRef Name = DTy.getName();
1146 uint64_t Size = DTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001147 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001148
1149 // Map to main type, void will not have a type.
Manman Ren93b30902013-10-08 18:42:58 +00001150 DIType FromTy = resolve(DTy.getTypeDerivedFrom());
Eric Christopher0df08e22013-08-08 07:40:37 +00001151 if (FromTy)
1152 addType(&Buffer, FromTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001153
1154 // Add name if not anonymous or intermediate type.
1155 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001156 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001157
1158 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher85757902012-02-21 22:25:53 +00001159 if (Size && Tag != dwarf::DW_TAG_pointer_type)
David Blaikief2443192013-10-21 17:28:37 +00001160 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001161
David Blaikie5d3249b2013-01-07 05:51:15 +00001162 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
Eric Christopherc2697f82013-10-19 01:04:47 +00001163 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
1164 getOrCreateTypeDIE(resolve(DTy.getClassType())));
Devang Patel0e821f42011-04-12 23:21:44 +00001165 // Add source line info if available and TyDesc is not a forward declaration.
1166 if (!DTy.isForwardDecl())
1167 addSourceLine(&Buffer, DTy);
1168}
1169
1170/// constructTypeDIE - Construct type DIE from DICompositeType.
1171void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
David Blaikie409dd9c2013-11-19 23:08:21 +00001172 // Add name if not anonymous or intermediate type.
Devang Patel0e821f42011-04-12 23:21:44 +00001173 StringRef Name = CTy.getName();
1174
1175 uint64_t Size = CTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001176 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001177
1178 switch (Tag) {
Devang Patel0e821f42011-04-12 23:21:44 +00001179 case dwarf::DW_TAG_array_type:
Eric Christopherdf9955d2013-11-11 18:52:31 +00001180 constructArrayTypeDIE(Buffer, CTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001181 break;
Eric Christopheraeb105f2013-11-11 18:52:39 +00001182 case dwarf::DW_TAG_enumeration_type:
1183 constructEnumTypeDIE(Buffer, CTy);
1184 break;
Devang Patel0e821f42011-04-12 23:21:44 +00001185 case dwarf::DW_TAG_subroutine_type: {
Eric Christopher0df08e22013-08-08 07:40:37 +00001186 // Add return type. A void return won't have a type.
Devang Patel0e821f42011-04-12 23:21:44 +00001187 DIArray Elements = CTy.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001188 DIType RTy(Elements.getElement(0));
Eric Christopher0df08e22013-08-08 07:40:37 +00001189 if (RTy)
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001190 addType(&Buffer, RTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001191
1192 bool isPrototyped = true;
1193 // Add arguments.
1194 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1195 DIDescriptor Ty = Elements.getElement(i);
1196 if (Ty.isUnspecifiedParameter()) {
Manman Renb987e512013-10-29 00:53:03 +00001197 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001198 isPrototyped = false;
1199 } else {
Manman Renb987e512013-10-29 00:53:03 +00001200 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001201 addType(Arg, DIType(Ty));
David Blaikie9a7a7a92013-01-29 19:35:24 +00001202 if (DIType(Ty).isArtificial())
1203 addFlag(Arg, dwarf::DW_AT_artificial);
Devang Patel0e821f42011-04-12 23:21:44 +00001204 }
1205 }
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001206 // Add prototype flag if we're dealing with a C language and the
1207 // function has been prototyped.
David Blaikiecb8e4352013-11-15 23:50:53 +00001208 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001209 if (isPrototyped &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001210 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopherd42b92f2012-05-22 18:45:24 +00001211 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001212 addFlag(&Buffer, dwarf::DW_AT_prototyped);
Eric Christopherc2697f82013-10-19 01:04:47 +00001213 } break;
Devang Patel0e821f42011-04-12 23:21:44 +00001214 case dwarf::DW_TAG_structure_type:
1215 case dwarf::DW_TAG_union_type:
1216 case dwarf::DW_TAG_class_type: {
Devang Patel0e821f42011-04-12 23:21:44 +00001217 // Add elements to structure type.
David Blaikiea1ae0e62013-08-01 20:30:22 +00001218 DIArray Elements = CTy.getTypeArray();
1219 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel0e821f42011-04-12 23:21:44 +00001220 DIDescriptor Element = Elements.getElement(i);
1221 DIE *ElemDie = NULL;
1222 if (Element.isSubprogram()) {
1223 DISubprogram SP(Element);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001224 ElemDie = getOrCreateSubprogramDIE(SP);
Devang Patel0e821f42011-04-12 23:21:44 +00001225 if (SP.isProtected())
Nick Lewyckycb918492011-12-13 05:09:11 +00001226 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001227 dwarf::DW_ACCESS_protected);
1228 else if (SP.isPrivate())
Nick Lewyckycb918492011-12-13 05:09:11 +00001229 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001230 dwarf::DW_ACCESS_private);
Eric Christopher92331fd2012-11-21 00:34:38 +00001231 else
Nick Lewyckycb918492011-12-13 05:09:11 +00001232 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Eric Christopherc2697f82013-10-19 01:04:47 +00001233 dwarf::DW_ACCESS_public);
Devang Patel0e821f42011-04-12 23:21:44 +00001234 if (SP.isExplicit())
Eric Christopherbb69a272012-08-24 01:14:27 +00001235 addFlag(ElemDie, dwarf::DW_AT_explicit);
Eric Christopher7285c7d2012-03-28 07:34:31 +00001236 } else if (Element.isDerivedType()) {
Eric Christopherd42b92f2012-05-22 18:45:24 +00001237 DIDerivedType DDTy(Element);
1238 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
Manman Renb987e512013-10-29 00:53:03 +00001239 ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
Manman Ren93b30902013-10-08 18:42:58 +00001240 addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()),
Manman Renb3388602013-10-05 01:43:03 +00001241 dwarf::DW_AT_friend);
Manman Renc6b63922013-10-14 20:33:57 +00001242 } else if (DDTy.isStaticMember()) {
Manman Ren57e6ff72013-10-23 22:57:12 +00001243 getOrCreateStaticMemberDIE(DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001244 } else {
Manman Ren230ec862013-10-23 23:00:44 +00001245 constructMemberDIE(Buffer, DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001246 }
Eric Christopher7285c7d2012-03-28 07:34:31 +00001247 } else if (Element.isObjCProperty()) {
Devang Pateld925d1a2012-02-07 23:33:58 +00001248 DIObjCProperty Property(Element);
Manman Renb987e512013-10-29 00:53:03 +00001249 ElemDie = createAndAddDIE(Property.getTag(), Buffer);
Devang Pateld925d1a2012-02-07 23:33:58 +00001250 StringRef PropertyName = Property.getObjCPropertyName();
1251 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Eric Christopherd42b92f2012-05-22 18:45:24 +00001252 addType(ElemDie, Property.getType());
1253 addSourceLine(ElemDie, Property);
Devang Pateld925d1a2012-02-07 23:33:58 +00001254 StringRef GetterName = Property.getObjCPropertyGetterName();
1255 if (!GetterName.empty())
1256 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
1257 StringRef SetterName = Property.getObjCPropertySetterName();
1258 if (!SetterName.empty())
1259 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
1260 unsigned PropertyAttributes = 0;
Devang Patel403e8192012-02-04 01:30:32 +00001261 if (Property.isReadOnlyObjCProperty())
1262 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1263 if (Property.isReadWriteObjCProperty())
1264 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1265 if (Property.isAssignObjCProperty())
1266 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1267 if (Property.isRetainObjCProperty())
1268 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1269 if (Property.isCopyObjCProperty())
1270 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1271 if (Property.isNonAtomicObjCProperty())
1272 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1273 if (PropertyAttributes)
David Blaikief2443192013-10-21 17:28:37 +00001274 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None,
Eric Christopherc2697f82013-10-19 01:04:47 +00001275 PropertyAttributes);
Devang Patel44882172012-02-06 17:49:43 +00001276
Devang Pateld925d1a2012-02-07 23:33:58 +00001277 DIEEntry *Entry = getDIEEntry(Element);
1278 if (!Entry) {
1279 Entry = createDIEEntry(ElemDie);
1280 insertDIEEntry(Element, Entry);
1281 }
Devang Patel403e8192012-02-04 01:30:32 +00001282 } else
Devang Patel0e821f42011-04-12 23:21:44 +00001283 continue;
Devang Patel0e821f42011-04-12 23:21:44 +00001284 }
1285
1286 if (CTy.isAppleBlockExtension())
Eric Christopherbb69a272012-08-24 01:14:27 +00001287 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel0e821f42011-04-12 23:21:44 +00001288
Eric Christopher9e429ae2013-10-05 00:27:02 +00001289 DICompositeType ContainingType(resolve(CTy.getContainingType()));
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001290 if (ContainingType)
Manman Ren4c4b69c2013-10-11 23:58:05 +00001291 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001292 getOrCreateTypeDIE(ContainingType));
Devang Patel0e821f42011-04-12 23:21:44 +00001293
Devang Patel12419ae2011-05-12 21:29:42 +00001294 if (CTy.isObjcClassComplete())
Eric Christopherbb69a272012-08-24 01:14:27 +00001295 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patel2409e782011-05-12 19:06:16 +00001296
Eric Christopherda011dd2011-12-16 23:42:42 +00001297 // Add template parameters to a class, structure or union types.
1298 // FIXME: The support isn't in the metadata for this yet.
1299 if (Tag == dwarf::DW_TAG_class_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001300 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
Devang Patel0e821f42011-04-12 23:21:44 +00001301 addTemplateParams(Buffer, CTy.getTemplateParams());
1302
1303 break;
1304 }
1305 default:
1306 break;
1307 }
1308
1309 // Add name if not anonymous or intermediate type.
1310 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001311 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001312
Eric Christopher775cbd22012-05-22 18:45:18 +00001313 if (Tag == dwarf::DW_TAG_enumeration_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001314 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
Eric Christopher775cbd22012-05-22 18:45:18 +00001315 Tag == dwarf::DW_TAG_union_type) {
Devang Patel0e821f42011-04-12 23:21:44 +00001316 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher1cf33382012-06-01 00:22:32 +00001317 // TODO: Do we care about size for enum forward declarations?
Devang Patel0e821f42011-04-12 23:21:44 +00001318 if (Size)
David Blaikief2443192013-10-21 17:28:37 +00001319 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Eric Christopher1cf33382012-06-01 00:22:32 +00001320 else if (!CTy.isForwardDecl())
Devang Patel0e821f42011-04-12 23:21:44 +00001321 // Add zero size if it is not a forward declaration.
David Blaikief2443192013-10-21 17:28:37 +00001322 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, 0);
Eric Christopher1cf33382012-06-01 00:22:32 +00001323
1324 // If we're a forward decl, say so.
1325 if (CTy.isForwardDecl())
Eric Christopherbb69a272012-08-24 01:14:27 +00001326 addFlag(&Buffer, dwarf::DW_AT_declaration);
Devang Patel0e821f42011-04-12 23:21:44 +00001327
1328 // Add source line info if available.
1329 if (!CTy.isForwardDecl())
1330 addSourceLine(&Buffer, CTy);
Eric Christopher54cf8ff2012-03-07 00:15:19 +00001331
1332 // No harm in adding the runtime language to the declaration.
1333 unsigned RLang = CTy.getRunTimeLang();
1334 if (RLang)
Eric Christopherc2697f82013-10-19 01:04:47 +00001335 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
1336 RLang);
Devang Patel0e821f42011-04-12 23:21:44 +00001337 }
1338}
1339
Manman Renffc9a712013-10-23 23:05:28 +00001340/// constructTemplateTypeParameterDIE - Construct new DIE for the given
1341/// DITemplateTypeParameter.
Manman Ren57e6ff72013-10-23 22:57:12 +00001342void
Manman Renffc9a712013-10-23 23:05:28 +00001343CompileUnit::constructTemplateTypeParameterDIE(DIE &Buffer,
1344 DITemplateTypeParameter TP) {
Manman Renb987e512013-10-29 00:53:03 +00001345 DIE *ParamDIE =
1346 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
Eric Christopher056b6472013-08-08 08:09:43 +00001347 // Add the type if it exists, it could be void and therefore no type.
1348 if (TP.getType())
Manman Ren88b0f942013-10-09 19:46:28 +00001349 addType(ParamDIE, resolve(TP.getType()));
David Blaikie2b380232013-06-22 18:59:11 +00001350 if (!TP.getName().empty())
1351 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel0e821f42011-04-12 23:21:44 +00001352}
1353
Manman Renffc9a712013-10-23 23:05:28 +00001354/// constructTemplateValueParameterDIE - Construct new DIE for the given
1355/// DITemplateValueParameter.
Manman Ren57e6ff72013-10-23 22:57:12 +00001356void
Manman Renffc9a712013-10-23 23:05:28 +00001357CompileUnit::constructTemplateValueParameterDIE(DIE &Buffer,
1358 DITemplateValueParameter VP) {
Manman Renb987e512013-10-29 00:53:03 +00001359 DIE *ParamDIE = createAndAddDIE(VP.getTag(), Buffer);
Eric Christopher0df08e22013-08-08 07:40:37 +00001360
1361 // Add the type if there is one, template template and template parameter
1362 // packs will not have a type.
Eric Christopher691281b2013-10-21 17:48:51 +00001363 if (VP.getTag() == dwarf::DW_TAG_template_value_parameter)
Manman Ren88b0f942013-10-09 19:46:28 +00001364 addType(ParamDIE, resolve(VP.getType()));
Eric Christopherafb2c412013-08-08 07:40:31 +00001365 if (!VP.getName().empty())
1366 addString(ParamDIE, dwarf::DW_AT_name, VP.getName());
1367 if (Value *Val = VP.getValue()) {
David Blaikiea1e813d2013-05-10 21:52:07 +00001368 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val))
Manman Ren88b0f942013-10-09 19:46:28 +00001369 addConstantValue(ParamDIE, CI,
1370 isUnsignedDIType(DD, resolve(VP.getType())));
David Blaikiea1e813d2013-05-10 21:52:07 +00001371 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) {
1372 // For declaration non-type template parameters (such as global values and
1373 // functions)
1374 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Rafael Espindola79858aa2013-10-29 17:07:16 +00001375 addOpAddress(Block, Asm->getSymbol(GV));
David Blaikiea1e813d2013-05-10 21:52:07 +00001376 // Emit DW_OP_stack_value to use the address as the immediate value of the
1377 // parameter, rather than a pointer to it.
David Blaikief2443192013-10-21 17:28:37 +00001378 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1379 addBlock(ParamDIE, dwarf::DW_AT_location, Block);
Eric Christopherafb2c412013-08-08 07:40:31 +00001380 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) {
David Blaikie2b380232013-06-22 18:59:11 +00001381 assert(isa<MDString>(Val));
1382 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1383 cast<MDString>(Val)->getString());
Eric Christopherafb2c412013-08-08 07:40:31 +00001384 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
David Blaikie2b380232013-06-22 18:59:11 +00001385 assert(isa<MDNode>(Val));
1386 DIArray A(cast<MDNode>(Val));
1387 addTemplateParams(*ParamDIE, A);
David Blaikiea1e813d2013-05-10 21:52:07 +00001388 }
1389 }
Devang Patel0e821f42011-04-12 23:21:44 +00001390}
1391
Devang Patel17b53272011-05-06 16:57:54 +00001392/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1393DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) {
Manman Renf6b936b2013-10-29 05:49:41 +00001394 // Construct the context before querying for the existence of the DIE in case
1395 // such construction creates the DIE.
1396 DIE *ContextDIE = getOrCreateContextDIE(NS.getContext());
Manman Renf6b936b2013-10-29 05:49:41 +00001397
Devang Patel17b53272011-05-06 16:57:54 +00001398 DIE *NDie = getDIE(NS);
1399 if (NDie)
1400 return NDie;
Manman Renf6b936b2013-10-29 05:49:41 +00001401 NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
1402
Eric Christopher4996c702011-11-07 09:24:32 +00001403 if (!NS.getName().empty()) {
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001404 addString(NDie, dwarf::DW_AT_name, NS.getName());
Eric Christopher4996c702011-11-07 09:24:32 +00001405 addAccelNamespace(NS.getName(), NDie);
Eric Christopher2c8b7902013-10-17 02:06:06 +00001406 addGlobalName(NS.getName(), NDie, NS.getContext());
Eric Christopher4996c702011-11-07 09:24:32 +00001407 } else
1408 addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel17b53272011-05-06 16:57:54 +00001409 addSourceLine(NDie, NS);
Devang Patel17b53272011-05-06 16:57:54 +00001410 return NDie;
1411}
1412
Devang Patel89543712011-08-15 17:24:54 +00001413/// getOrCreateSubprogramDIE - Create new DIE using SP.
1414DIE *CompileUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
David Blaikie309ffe42013-10-04 01:39:59 +00001415 // Construct the context before querying for the existence of the DIE in case
1416 // such construction creates the DIE (as is the case for member function
1417 // declarations).
Manman Renc50fa112013-10-10 18:40:01 +00001418 DIE *ContextDIE = getOrCreateContextDIE(resolve(SP.getContext()));
David Blaikie309ffe42013-10-04 01:39:59 +00001419
Eric Christophere595bae2013-10-04 17:08:38 +00001420 DIE *SPDie = getDIE(SP);
Devang Patel89543712011-08-15 17:24:54 +00001421 if (SPDie)
1422 return SPDie;
1423
Manman Ren73d697c2013-10-29 00:58:04 +00001424 DISubprogram SPDecl = SP.getFunctionDeclaration();
1425 if (SPDecl.isSubprogram())
1426 // Add subprogram definitions to the CU die directly.
1427 ContextDIE = CUDie.get();
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001428
1429 // DW_TAG_inlined_subroutine may refer to this DIE.
Manman Ren73d697c2013-10-29 00:58:04 +00001430 SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001431
Rafael Espindola79278362011-11-10 22:34:29 +00001432 DIE *DeclDie = NULL;
Manman Renb9512a72013-10-23 22:12:26 +00001433 if (SPDecl.isSubprogram())
Rafael Espindola79278362011-11-10 22:34:29 +00001434 DeclDie = getOrCreateSubprogramDIE(SPDecl);
Rafael Espindola79278362011-11-10 22:34:29 +00001435
Devang Patel89543712011-08-15 17:24:54 +00001436 // Add function template parameters.
1437 addTemplateParams(*SPDie, SP.getTemplateParams());
1438
Devang Patel89543712011-08-15 17:24:54 +00001439 // If this DIE is going to refer declaration info using AT_specification
Eric Christopherf20ff972013-05-09 00:42:33 +00001440 // then there is no need to add other attributes.
Rafael Espindola79278362011-11-10 22:34:29 +00001441 if (DeclDie) {
1442 // Refer function declaration directly.
Manman Ren4c4b69c2013-10-11 23:58:05 +00001443 addDIEEntry(SPDie, dwarf::DW_AT_specification, DeclDie);
Rafael Espindola79278362011-11-10 22:34:29 +00001444
Devang Patel89543712011-08-15 17:24:54 +00001445 return SPDie;
Rafael Espindola79278362011-11-10 22:34:29 +00001446 }
Devang Patel89543712011-08-15 17:24:54 +00001447
Eric Christopheracb71152012-08-23 22:52:55 +00001448 // Add the linkage name if we have one.
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001449 StringRef LinkageName = SP.getLinkageName();
1450 if (!LinkageName.empty())
Eric Christopheracb71152012-08-23 22:52:55 +00001451 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001452 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopheracb71152012-08-23 22:52:55 +00001453
Devang Patel89543712011-08-15 17:24:54 +00001454 // Constructors and operators for anonymous aggregates do not have names.
1455 if (!SP.getName().empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001456 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Patel89543712011-08-15 17:24:54 +00001457
1458 addSourceLine(SPDie, SP);
1459
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001460 // Add the prototype if we have a prototype and we have a C like
1461 // language.
David Blaikiecb8e4352013-11-15 23:50:53 +00001462 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001463 if (SP.isPrototyped() &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001464 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001465 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001466 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Patel89543712011-08-15 17:24:54 +00001467
Devang Patel89543712011-08-15 17:24:54 +00001468 DICompositeType SPTy = SP.getType();
David Blaikie5174c842013-05-22 23:22:18 +00001469 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type &&
1470 "the type of a subprogram should be a subroutine");
Devang Patel89543712011-08-15 17:24:54 +00001471
David Blaikie5174c842013-05-22 23:22:18 +00001472 DIArray Args = SPTy.getTypeArray();
Eric Christopher691281b2013-10-21 17:48:51 +00001473 // Add a return type. If this is a type like a C/C++ void type we don't add a
1474 // return type.
Eric Christopher0df08e22013-08-08 07:40:37 +00001475 if (Args.getElement(0))
1476 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel89543712011-08-15 17:24:54 +00001477
1478 unsigned VK = SP.getVirtuality();
1479 if (VK) {
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001480 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Devang Patel89543712011-08-15 17:24:54 +00001481 DIEBlock *Block = getDIEBlock();
David Blaikief2443192013-10-21 17:28:37 +00001482 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1483 addUInt(Block, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1484 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
Eric Christopher98b7f172013-11-11 18:52:36 +00001485 ContainingTypeMap.insert(
1486 std::make_pair(SPDie, resolve(SP.getContainingType())));
Devang Patel89543712011-08-15 17:24:54 +00001487 }
1488
1489 if (!SP.isDefinition()) {
Eric Christopherbb69a272012-08-24 01:14:27 +00001490 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher92331fd2012-11-21 00:34:38 +00001491
Devang Patel89543712011-08-15 17:24:54 +00001492 // Add arguments. Do not add arguments for subprogram definition. They will
1493 // be handled while processing variables.
Eric Christopherc2697f82013-10-19 01:04:47 +00001494 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
Manman Renb987e512013-10-29 00:53:03 +00001495 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, *SPDie);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001496 DIType ATy(Args.getElement(i));
David Blaikie5174c842013-05-22 23:22:18 +00001497 addType(Arg, ATy);
1498 if (ATy.isArtificial())
1499 addFlag(Arg, dwarf::DW_AT_artificial);
David Blaikie5174c842013-05-22 23:22:18 +00001500 }
Devang Patel89543712011-08-15 17:24:54 +00001501 }
1502
1503 if (SP.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001504 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Patel89543712011-08-15 17:24:54 +00001505
1506 if (!SP.isLocalToUnit())
Eric Christopherbb69a272012-08-24 01:14:27 +00001507 addFlag(SPDie, dwarf::DW_AT_external);
Devang Patel89543712011-08-15 17:24:54 +00001508
1509 if (SP.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +00001510 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Patel89543712011-08-15 17:24:54 +00001511
1512 if (unsigned isa = Asm->getISAEncoding()) {
1513 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1514 }
1515
1516 return SPDie;
1517}
1518
Devang Pateldfd6ec32011-08-15 17:57:41 +00001519// Return const expression if value is a GEP to access merged global
1520// constant. e.g.
1521// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1522static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1523 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1524 if (!CE || CE->getNumOperands() != 3 ||
1525 CE->getOpcode() != Instruction::GetElementPtr)
1526 return NULL;
1527
1528 // First operand points to a global struct.
1529 Value *Ptr = CE->getOperand(0);
1530 if (!isa<GlobalValue>(Ptr) ||
1531 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1532 return NULL;
1533
1534 // Second operand is zero.
1535 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1536 if (!CI || !CI->isZero())
1537 return NULL;
1538
1539 // Third operand is offset.
1540 if (!isa<ConstantInt>(CE->getOperand(2)))
1541 return NULL;
1542
1543 return CE;
1544}
1545
1546/// createGlobalVariableDIE - create global variable DIE.
David Blaikiea781b25b2013-11-17 21:55:13 +00001547void CompileUnit::createGlobalVariableDIE(DIGlobalVariable GV) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001548 // Check for pre-existence.
David Blaikie2ad00162013-11-15 23:09:13 +00001549 if (getDIE(GV))
Devang Pateldfd6ec32011-08-15 17:57:41 +00001550 return;
1551
Manman Ren7504ed42013-07-08 18:33:29 +00001552 if (!GV.isGlobalVariable())
Devang Patel0ecbcbd2011-08-18 23:17:55 +00001553 return;
1554
Eric Christopher08f7c8f2013-10-04 23:49:26 +00001555 DIScope GVContext = GV.getContext();
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001556 DIType GTy = GV.getType();
1557
1558 // If this is a static data member definition, some attributes belong
1559 // to the declaration DIE.
1560 DIE *VariableDIE = NULL;
Manman Rene697d3c2013-02-01 23:54:37 +00001561 bool IsStaticMember = false;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001562 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1563 if (SDMDecl.Verify()) {
1564 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1565 // We need the declaration DIE that is in the static member's class.
Manman Renc6b63922013-10-14 20:33:57 +00001566 VariableDIE = getOrCreateStaticMemberDIE(SDMDecl);
Manman Rene697d3c2013-02-01 23:54:37 +00001567 IsStaticMember = true;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001568 }
1569
1570 // If this is not a static data member definition, create the variable
1571 // DIE and add the initial set of attributes to it.
1572 if (!VariableDIE) {
Manman Renf6b936b2013-10-29 05:49:41 +00001573 // Construct the context before querying for the existence of the DIE in
1574 // case such construction creates the DIE.
1575 DIE *ContextDIE = getOrCreateContextDIE(GVContext);
Manman Renf6b936b2013-10-29 05:49:41 +00001576
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001577 // Add to map.
David Blaikie52c50202013-11-16 00:29:01 +00001578 VariableDIE = createAndAddDIE(GV.getTag(), *ContextDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001579
1580 // Add name and type.
1581 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1582 addType(VariableDIE, GTy);
1583
1584 // Add scoping info.
Eric Christopherd2b497b2013-10-16 01:37:49 +00001585 if (!GV.isLocalToUnit())
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001586 addFlag(VariableDIE, dwarf::DW_AT_external);
1587
1588 // Add line number info.
1589 addSourceLine(VariableDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001590 }
1591
Devang Pateldfd6ec32011-08-15 17:57:41 +00001592 // Add location.
Eric Christopher4996c702011-11-07 09:24:32 +00001593 bool addToAccelTable = false;
Eric Christopher0a917b72011-11-11 03:16:32 +00001594 DIE *VariableSpecDIE = NULL;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001595 bool isGlobalVariable = GV.getGlobal() != NULL;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001596 if (isGlobalVariable) {
Eric Christopher4996c702011-11-07 09:24:32 +00001597 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001598 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Rafael Espindola79858aa2013-10-29 17:07:16 +00001599 const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal());
David Blaikief2694972013-06-28 20:05:11 +00001600 if (GV.getGlobal()->isThreadLocal()) {
1601 // FIXME: Make this work with -gsplit-dwarf.
1602 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
1603 assert((PointerSize == 4 || PointerSize == 8) &&
1604 "Add support for other sizes if necessary");
Ulrich Weigand2b6fc8d2013-07-02 18:47:09 +00001605 const MCExpr *Expr =
David Blaikie8466ca82013-07-01 23:55:52 +00001606 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym);
David Blaikief2694972013-06-28 20:05:11 +00001607 // Based on GCC's support for TLS:
David Blaikie8466ca82013-07-01 23:55:52 +00001608 if (!DD->useSplitDwarf()) {
1609 // 1) Start with a constNu of the appropriate pointer size
David Blaikief2443192013-10-21 17:28:37 +00001610 addUInt(Block, dwarf::DW_FORM_data1,
David Blaikie8466ca82013-07-01 23:55:52 +00001611 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
Richard Mitton0aafb582013-10-07 18:39:18 +00001612 // 2) containing the (relocated) offset of the TLS variable
1613 // within the module's TLS block.
David Blaikief2443192013-10-21 17:28:37 +00001614 addExpr(Block, dwarf::DW_FORM_udata, Expr);
David Blaikie8466ca82013-07-01 23:55:52 +00001615 } else {
David Blaikief2443192013-10-21 17:28:37 +00001616 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
1617 addUInt(Block, dwarf::DW_FORM_udata, DU->getAddrPoolIndex(Expr));
David Blaikie8466ca82013-07-01 23:55:52 +00001618 }
Richard Mitton0aafb582013-10-07 18:39:18 +00001619 // 3) followed by a custom OP to make the debugger do a TLS lookup.
David Blaikief2443192013-10-21 17:28:37 +00001620 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address);
David Blaikief2694972013-06-28 20:05:11 +00001621 } else
1622 addOpAddress(Block, Sym);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001623 // Do not create specification DIE if context is either compile unit
1624 // or a subprogram.
Devang Patel5e6b65c2011-09-21 23:41:11 +00001625 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Manman Ren3eb9dff2013-09-09 19:05:21 +00001626 !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001627 // Create specification DIE.
David Blaikieeb0338fe2013-11-16 00:28:15 +00001628 VariableSpecDIE = createAndAddDIE(dwarf::DW_TAG_variable, *CUDie);
Manman Ren4c4b69c2013-10-11 23:58:05 +00001629 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification, VariableDIE);
David Blaikief2443192013-10-21 17:28:37 +00001630 addBlock(VariableSpecDIE, dwarf::DW_AT_location, Block);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001631 // A static member's declaration is already flagged as such.
1632 if (!SDMDecl.Verify())
1633 addFlag(VariableDIE, dwarf::DW_AT_declaration);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001634 } else {
David Blaikief2443192013-10-21 17:28:37 +00001635 addBlock(VariableDIE, dwarf::DW_AT_location, Block);
Eric Christopher4996c702011-11-07 09:24:32 +00001636 }
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001637 // Add the linkage name.
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001638 StringRef LinkageName = GV.getLinkageName();
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001639 if (!LinkageName.empty())
Eric Christopher3f79b8c2013-02-27 23:49:47 +00001640 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1641 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1642 // TAG_variable.
Eric Christopherc2697f82013-10-19 01:04:47 +00001643 addString(IsStaticMember && VariableSpecDIE ? VariableSpecDIE
1644 : VariableDIE,
1645 dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001646 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopher92331fd2012-11-21 00:34:38 +00001647 } else if (const ConstantInt *CI =
Eric Christopherc2697f82013-10-19 01:04:47 +00001648 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
Adrian Prantl322f41d2013-04-04 22:56:49 +00001649 // AT_const_value was added when the static member was created. To avoid
Manman Rene697d3c2013-02-01 23:54:37 +00001650 // emitting AT_const_value multiple times, we only add AT_const_value when
1651 // it is not a static member.
1652 if (!IsStaticMember)
Manman Renb3388602013-10-05 01:43:03 +00001653 addConstantValue(VariableDIE, CI, isUnsignedDIType(DD, GTy));
David Blaikiea781b25b2013-11-17 21:55:13 +00001654 } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getOperand(11))) {
Eric Christopher4996c702011-11-07 09:24:32 +00001655 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001656 // GV is a merged global.
1657 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1658 Value *Ptr = CE->getOperand(0);
Rafael Espindola79858aa2013-10-29 17:07:16 +00001659 addOpAddress(Block, Asm->getSymbol(cast<GlobalValue>(Ptr)));
David Blaikief2443192013-10-21 17:28:37 +00001660 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
Eric Christopherc2697f82013-10-19 01:04:47 +00001661 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
David Blaikief2443192013-10-21 17:28:37 +00001662 addUInt(Block, dwarf::DW_FORM_udata,
Eric Christopherc2697f82013-10-19 01:04:47 +00001663 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
David Blaikief2443192013-10-21 17:28:37 +00001664 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1665 addBlock(VariableDIE, dwarf::DW_AT_location, Block);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001666 }
1667
Eric Christopherc12c2112011-11-11 01:55:22 +00001668 if (addToAccelTable) {
1669 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1670 addAccelName(GV.getName(), AddrDIE);
Eric Christopher4996c702011-11-07 09:24:32 +00001671
Eric Christopherc12c2112011-11-11 01:55:22 +00001672 // If the linkage name is different than the name, go ahead and output
1673 // that as well into the name table.
1674 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1675 addAccelName(GV.getLinkageName(), AddrDIE);
1676 }
Eric Christopherd2b497b2013-10-16 01:37:49 +00001677
1678 if (!GV.isLocalToUnit())
Eric Christopher2c8b7902013-10-17 02:06:06 +00001679 addGlobalName(GV.getName(), VariableSpecDIE ? VariableSpecDIE : VariableDIE,
1680 GV.getContext());
Devang Pateldfd6ec32011-08-15 17:57:41 +00001681}
1682
Devang Patel0e821f42011-04-12 23:21:44 +00001683/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Eric Christopherd42b92f2012-05-22 18:45:24 +00001684void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR,
1685 DIE *IndexTy) {
Manman Renb987e512013-10-29 00:53:03 +00001686 DIE *DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
Manman Ren4c4b69c2013-10-11 23:58:05 +00001687 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, IndexTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001688
Bill Wendling28fe9e72012-12-06 07:38:10 +00001689 // The LowerBound value defines the lower bounds which is typically zero for
1690 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1691 // Count == -1 then the array is unbounded and we do not emit
1692 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1693 // Count == 0, then the array has zero elements in which case we do not emit
1694 // an upper bound.
1695 int64_t LowerBound = SR.getLo();
Bill Wendling3495f9b2012-12-06 07:55:19 +00001696 int64_t DefaultLowerBound = getDefaultLowerBound();
Bill Wendlingd7767122012-12-04 21:34:03 +00001697 int64_t Count = SR.getCount();
Devang Patel0e821f42011-04-12 23:21:44 +00001698
Bill Wendling3495f9b2012-12-06 07:55:19 +00001699 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
David Blaikief2443192013-10-21 17:28:37 +00001700 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound);
Bill Wendling28fe9e72012-12-06 07:38:10 +00001701
1702 if (Count != -1 && Count != 0)
Bill Wendlingd7767122012-12-04 21:34:03 +00001703 // FIXME: An unbounded array should reference the expression that defines
1704 // the array.
Eric Christopher98b7f172013-11-11 18:52:36 +00001705 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None,
1706 LowerBound + Count - 1);
Devang Patel0e821f42011-04-12 23:21:44 +00001707}
1708
1709/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001710void CompileUnit::constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy) {
1711 if (CTy.isVector())
Eric Christopherbb69a272012-08-24 01:14:27 +00001712 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel0e821f42011-04-12 23:21:44 +00001713
Eric Christopher0df08e22013-08-08 07:40:37 +00001714 // Emit the element type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001715 addType(&Buffer, resolve(CTy.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001716
1717 // Get an anonymous type for index type.
Eric Christophercad9b532013-01-04 21:51:53 +00001718 // FIXME: This type should be passed down from the front end
1719 // as different languages may have different sizes for indexes.
Devang Patel0e821f42011-04-12 23:21:44 +00001720 DIE *IdxTy = getIndexTyDie();
1721 if (!IdxTy) {
1722 // Construct an anonymous type for index type.
Manman Renb987e512013-10-29 00:53:03 +00001723 IdxTy = createAndAddDIE(dwarf::DW_TAG_base_type, *CUDie.get());
Eric Christophercad9b532013-01-04 21:51:53 +00001724 addString(IdxTy, dwarf::DW_AT_name, "int");
David Blaikief2443192013-10-21 17:28:37 +00001725 addUInt(IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int32_t));
Devang Patel0e821f42011-04-12 23:21:44 +00001726 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1727 dwarf::DW_ATE_signed);
Devang Patel0e821f42011-04-12 23:21:44 +00001728 setIndexTyDie(IdxTy);
1729 }
1730
1731 // Add subranges to array type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001732 DIArray Elements = CTy.getTypeArray();
Devang Patel0e821f42011-04-12 23:21:44 +00001733 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1734 DIDescriptor Element = Elements.getElement(i);
1735 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1736 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1737 }
1738}
1739
Eric Christopheraeb105f2013-11-11 18:52:39 +00001740/// constructEnumTypeDIE - Construct an enum type DIE from DICompositeType.
1741void CompileUnit::constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy) {
1742 DIArray Elements = CTy.getTypeArray();
1743
1744 // Add enumerators to enumeration type.
1745 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001746 DIEnumerator Enum(Elements.getElement(i));
Eric Christopheraeb105f2013-11-11 18:52:39 +00001747 if (Enum.isEnumerator()) {
1748 DIE *Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001749 StringRef Name = Enum.getName();
Eric Christopheraeb105f2013-11-11 18:52:39 +00001750 addString(Enumerator, dwarf::DW_AT_name, Name);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001751 int64_t Value = Enum.getEnumValue();
Eric Christophera07e4f52013-11-19 09:28:34 +00001752 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
1753 Value);
Eric Christopheraeb105f2013-11-11 18:52:39 +00001754 }
1755 }
1756 DIType DTy = resolve(CTy.getTypeDerivedFrom());
1757 if (DTy) {
1758 addType(&Buffer, DTy);
1759 addFlag(&Buffer, dwarf::DW_AT_enum_class);
1760 }
Devang Patel0e821f42011-04-12 23:21:44 +00001761}
1762
Devang Patel89543712011-08-15 17:24:54 +00001763/// constructContainingTypeDIEs - Construct DIEs for types that contain
1764/// vtables.
1765void CompileUnit::constructContainingTypeDIEs() {
1766 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Eric Christopherc2697f82013-10-19 01:04:47 +00001767 CE = ContainingTypeMap.end();
1768 CI != CE; ++CI) {
Devang Patel89543712011-08-15 17:24:54 +00001769 DIE *SPDie = CI->first;
David Blaikie2ad00162013-11-15 23:09:13 +00001770 DIDescriptor D(CI->second);
1771 if (!D)
Eric Christopherc2697f82013-10-19 01:04:47 +00001772 continue;
David Blaikie2ad00162013-11-15 23:09:13 +00001773 DIE *NDie = getDIE(D);
Eric Christopherc2697f82013-10-19 01:04:47 +00001774 if (!NDie)
1775 continue;
Manman Ren4c4b69c2013-10-11 23:58:05 +00001776 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, NDie);
Devang Patel89543712011-08-15 17:24:54 +00001777 }
1778}
1779
Devang Patel3acc70e2011-08-15 22:04:40 +00001780/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Eric Christopher34a2c872013-11-15 01:43:19 +00001781DIE *CompileUnit::constructVariableDIE(DbgVariable &DV, bool isScopeAbstract) {
1782 StringRef Name = DV.getName();
Devang Patel3acc70e2011-08-15 22:04:40 +00001783
Devang Patel3acc70e2011-08-15 22:04:40 +00001784 // Define variable debug information entry.
Eric Christopher34a2c872013-11-15 01:43:19 +00001785 DIE *VariableDie = new DIE(DV.getTag());
1786 DbgVariable *AbsVar = DV.getAbstractVariable();
Devang Patel3acc70e2011-08-15 22:04:40 +00001787 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
Manman Ren4213c392013-05-29 17:16:59 +00001788 if (AbsDIE)
Manman Ren4c4b69c2013-10-11 23:58:05 +00001789 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin, AbsDIE);
Devang Patel3acc70e2011-08-15 22:04:40 +00001790 else {
David Blaikie715528b2013-08-19 03:34:03 +00001791 if (!Name.empty())
1792 addString(VariableDie, dwarf::DW_AT_name, Name);
Eric Christopher34a2c872013-11-15 01:43:19 +00001793 addSourceLine(VariableDie, DV.getVariable());
1794 addType(VariableDie, DV.getType());
Devang Patel3acc70e2011-08-15 22:04:40 +00001795 }
1796
Eric Christopher34a2c872013-11-15 01:43:19 +00001797 if (DV.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001798 addFlag(VariableDie, dwarf::DW_AT_artificial);
Devang Patel3acc70e2011-08-15 22:04:40 +00001799
1800 if (isScopeAbstract) {
Eric Christopher34a2c872013-11-15 01:43:19 +00001801 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001802 return VariableDie;
1803 }
1804
1805 // Add variable address.
1806
Eric Christopher34a2c872013-11-15 01:43:19 +00001807 unsigned Offset = DV.getDotDebugLocOffset();
Devang Patel3acc70e2011-08-15 22:04:40 +00001808 if (Offset != ~0U) {
Eric Christopher33ff6972013-11-21 23:46:41 +00001809 addSectionLabel(VariableDie, dwarf::DW_AT_location,
1810 Asm->GetTempSymbol("debug_loc", Offset));
Eric Christopher34a2c872013-11-15 01:43:19 +00001811 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001812 return VariableDie;
1813 }
1814
Eric Christophercead0332011-10-03 15:49:20 +00001815 // Check if variable is described by a DBG_VALUE instruction.
Eric Christopher34a2c872013-11-15 01:43:19 +00001816 if (const MachineInstr *DVInsn = DV.getMInsn()) {
David Blaikie0252265b2013-06-16 20:34:15 +00001817 assert(DVInsn->getNumOperands() == 3);
1818 if (DVInsn->getOperand(0).isReg()) {
1819 const MachineOperand RegOp = DVInsn->getOperand(0);
Adrian Prantl19942882013-07-09 21:44:06 +00001820 // If the second operand is an immediate, this is an indirect value.
Adrian Prantl418d1d12013-07-09 20:28:37 +00001821 if (DVInsn->getOperand(1).isImm()) {
Eric Christopherc2697f82013-10-19 01:04:47 +00001822 MachineLocation Location(RegOp.getReg(),
1823 DVInsn->getOperand(1).getImm());
Eric Christopher34a2c872013-11-15 01:43:19 +00001824 addVariableAddress(DV, VariableDie, Location);
David Blaikie0252265b2013-06-16 20:34:15 +00001825 } else if (RegOp.getReg())
Eric Christopher34a2c872013-11-15 01:43:19 +00001826 addVariableAddress(DV, VariableDie, MachineLocation(RegOp.getReg()));
David Blaikie0252265b2013-06-16 20:34:15 +00001827 } else if (DVInsn->getOperand(0).isImm())
Eric Christopher34a2c872013-11-15 01:43:19 +00001828 addConstantValue(VariableDie, DVInsn->getOperand(0), DV.getType());
David Blaikie0252265b2013-06-16 20:34:15 +00001829 else if (DVInsn->getOperand(0).isFPImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001830 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
David Blaikie0252265b2013-06-16 20:34:15 +00001831 else if (DVInsn->getOperand(0).isCImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001832 addConstantValue(VariableDie, DVInsn->getOperand(0).getCImm(),
Eric Christopher34a2c872013-11-15 01:43:19 +00001833 isUnsignedDIType(DD, DV.getType()));
Eric Christopher78fcf4902013-07-03 01:08:30 +00001834
Eric Christopher34a2c872013-11-15 01:43:19 +00001835 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001836 return VariableDie;
1837 } else {
1838 // .. else use frame index.
Eric Christopher34a2c872013-11-15 01:43:19 +00001839 int FI = DV.getFrameIndex();
Devang Patel3acc70e2011-08-15 22:04:40 +00001840 if (FI != ~0) {
1841 unsigned FrameReg = 0;
1842 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopherc2697f82013-10-19 01:04:47 +00001843 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
Devang Patel3acc70e2011-08-15 22:04:40 +00001844 MachineLocation Location(FrameReg, Offset);
Eric Christopher34a2c872013-11-15 01:43:19 +00001845 addVariableAddress(DV, VariableDie, Location);
Devang Patel3acc70e2011-08-15 22:04:40 +00001846 }
1847 }
1848
Eric Christopher34a2c872013-11-15 01:43:19 +00001849 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001850 return VariableDie;
1851}
1852
Manman Ren230ec862013-10-23 23:00:44 +00001853/// constructMemberDIE - Construct member DIE from DIDerivedType.
1854void CompileUnit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) {
Manman Renb987e512013-10-29 00:53:03 +00001855 DIE *MemberDie = createAndAddDIE(DT.getTag(), Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001856 StringRef Name = DT.getName();
1857 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001858 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001859
Manman Ren93b30902013-10-08 18:42:58 +00001860 addType(MemberDie, resolve(DT.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001861
1862 addSourceLine(MemberDie, DT);
1863
1864 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
David Blaikief2443192013-10-21 17:28:37 +00001865 addUInt(MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel0e821f42011-04-12 23:21:44 +00001866
Eric Christopherc2697f82013-10-19 01:04:47 +00001867 if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) {
Devang Patel0e821f42011-04-12 23:21:44 +00001868
1869 // For C++, virtual base classes are not at fixed offset. Use following
1870 // expression to extract appropriate offset from vtable.
1871 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1872
1873 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
David Blaikief2443192013-10-21 17:28:37 +00001874 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1875 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1876 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1877 addUInt(VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1878 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1879 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1880 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
Devang Patel0e821f42011-04-12 23:21:44 +00001881
David Blaikief2443192013-10-21 17:28:37 +00001882 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
David Blaikie71d34a22013-11-01 00:25:45 +00001883 } else {
1884 uint64_t Size = DT.getSizeInBits();
1885 uint64_t FieldSize = getBaseTypeSize(DD, DT);
1886 uint64_t OffsetInBytes;
1887
1888 if (Size != FieldSize) {
1889 // Handle bitfield.
1890 addUInt(MemberDie, dwarf::DW_AT_byte_size, None,
1891 getBaseTypeSize(DD, DT) >> 3);
1892 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, DT.getSizeInBits());
1893
1894 uint64_t Offset = DT.getOffsetInBits();
1895 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1896 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1897 uint64_t FieldOffset = (HiMark - FieldSize);
1898 Offset -= FieldOffset;
1899
1900 // Maybe we need to work from the other end.
1901 if (Asm->getDataLayout().isLittleEndian())
1902 Offset = FieldSize - (Offset + Size);
1903 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
1904
1905 // Here WD_AT_data_member_location points to the anonymous
1906 // field that includes this bit field.
1907 OffsetInBytes = FieldOffset >> 3;
1908 } else
1909 // This is not a bitfield.
1910 OffsetInBytes = DT.getOffsetInBits() >> 3;
Eric Christopher98b7f172013-11-11 18:52:36 +00001911 addUInt(MemberDie, dwarf::DW_AT_data_member_location, None, OffsetInBytes);
David Blaikie71d34a22013-11-01 00:25:45 +00001912 }
Devang Patel0e821f42011-04-12 23:21:44 +00001913
1914 if (DT.isProtected())
Nick Lewyckycb918492011-12-13 05:09:11 +00001915 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001916 dwarf::DW_ACCESS_protected);
1917 else if (DT.isPrivate())
Nick Lewyckycb918492011-12-13 05:09:11 +00001918 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001919 dwarf::DW_ACCESS_private);
1920 // Otherwise C++ member and base classes are considered public.
Eric Christopher92331fd2012-11-21 00:34:38 +00001921 else
Nick Lewyckycb918492011-12-13 05:09:11 +00001922 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001923 dwarf::DW_ACCESS_public);
1924 if (DT.isVirtual())
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001925 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001926 dwarf::DW_VIRTUALITY_virtual);
Devang Patel514b4002011-04-16 00:11:51 +00001927
1928 // Objective-C properties.
Devang Patel44882172012-02-06 17:49:43 +00001929 if (MDNode *PNode = DT.getObjCProperty())
1930 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
Eric Christopher92331fd2012-11-21 00:34:38 +00001931 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
Devang Patel44882172012-02-06 17:49:43 +00001932 PropertyDie);
1933
David Blaikie37fefc32012-12-13 22:43:07 +00001934 if (DT.isArtificial())
1935 addFlag(MemberDie, dwarf::DW_AT_artificial);
Devang Patel0e821f42011-04-12 23:21:44 +00001936}
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001937
Manman Renc6b63922013-10-14 20:33:57 +00001938/// getOrCreateStaticMemberDIE - Create new DIE for C++ static member.
David Blaikie2ad00162013-11-15 23:09:13 +00001939DIE *CompileUnit::getOrCreateStaticMemberDIE(DIDerivedType DT) {
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001940 if (!DT.Verify())
1941 return NULL;
1942
Manman Renc6b63922013-10-14 20:33:57 +00001943 // Construct the context before querying for the existence of the DIE in case
1944 // such construction creates the DIE.
1945 DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext()));
David Blaikiebd700e42013-11-14 21:24:34 +00001946 assert(dwarf::isType(ContextDIE->getTag()) &&
1947 "Static member should belong to a type.");
Manman Renc6b63922013-10-14 20:33:57 +00001948
1949 DIE *StaticMemberDIE = getDIE(DT);
1950 if (StaticMemberDIE)
1951 return StaticMemberDIE;
1952
Manman Renb987e512013-10-29 00:53:03 +00001953 StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT);
Manman Renc6b63922013-10-14 20:33:57 +00001954
Manman Ren93b30902013-10-08 18:42:58 +00001955 DIType Ty = resolve(DT.getTypeDerivedFrom());
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001956
1957 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1958 addType(StaticMemberDIE, Ty);
1959 addSourceLine(StaticMemberDIE, DT);
1960 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1961 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1962
1963 // FIXME: We could omit private if the parent is a class_type, and
1964 // public if the parent is something else.
1965 if (DT.isProtected())
1966 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1967 dwarf::DW_ACCESS_protected);
1968 else if (DT.isPrivate())
1969 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1970 dwarf::DW_ACCESS_private);
1971 else
1972 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1973 dwarf::DW_ACCESS_public);
1974
1975 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
Manman Renb3388602013-10-05 01:43:03 +00001976 addConstantValue(StaticMemberDIE, CI, isUnsignedDIType(DD, Ty));
David Blaikiea39a76e2013-01-20 01:18:01 +00001977 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
1978 addConstantFPValue(StaticMemberDIE, CFP);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001979
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001980 return StaticMemberDIE;
1981}
David Blaikie6b288cf2013-10-30 20:42:41 +00001982
1983void CompileUnit::emitHeader(const MCSection *ASection,
1984 const MCSymbol *ASectionSym) {
1985 Asm->OutStreamer.AddComment("DWARF version number");
1986 Asm->EmitInt16(DD->getDwarfVersion());
1987 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
1988 Asm->EmitSectionOffset(Asm->GetTempSymbol(ASection->getLabelBeginName()),
1989 ASectionSym);
1990 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1991 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
1992}