blob: 87a3891244875c0607a6b4efa5a141b5b424a705 [file] [log] [blame]
David Blaikie319a05f2013-12-02 19:33:10 +00001//===-- llvm/CodeGen/DwarfUnit.cpp - Dwarf Type and Compile Units ---------===//
Devang Patel0e821f42011-04-12 23:21:44 +00002//
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
David Blaikie2c86a722013-12-02 19:33:15 +000016#include "DwarfUnit.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
Eric Christopher4287a492013-12-09 23:57:44 +000036static cl::opt<bool>
37GenerateDwarfTypeUnits("generate-type-units", cl::Hidden,
38 cl::desc("Generate DWARF4 type units."),
39 cl::init(false));
David Blaikie409dd9c2013-11-19 23:08:21 +000040
David Blaikie2a80e442013-12-02 22:09:48 +000041/// Unit - Unit constructor.
Eric Christophera5a79422013-12-09 23:32:48 +000042DwarfUnit::DwarfUnit(unsigned UID, DIE *D, DICompileUnit Node, AsmPrinter *A,
43 DwarfDebug *DW, DwarfFile *DWU)
David Blaikie2a80e442013-12-02 22:09:48 +000044 : UniqueID(UID), Node(Node), UnitDie(D), DebugInfoOffset(0), Asm(A), DD(DW),
David Blaikie03073f72013-12-06 22:14:48 +000045 DU(DWU), IndexTyDie(0), Section(0) {
David Blaikie319a05f2013-12-02 19:33:10 +000046 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
47}
48
Eric Christopher4287a492013-12-09 23:57:44 +000049DwarfCompileUnit::DwarfCompileUnit(unsigned UID, DIE *D, DICompileUnit Node,
50 AsmPrinter *A, DwarfDebug *DW,
51 DwarfFile *DWU)
Eric Christophera5a79422013-12-09 23:32:48 +000052 : DwarfUnit(UID, D, Node, A, DW, DWU) {
David Blaikie5a152402013-11-15 23:52:02 +000053 insertDIE(Node, D);
Devang Patel0e821f42011-04-12 23:21:44 +000054}
55
Eric Christopher4287a492013-12-09 23:57:44 +000056DwarfTypeUnit::DwarfTypeUnit(unsigned UID, DIE *D, uint16_t Language,
57 AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU)
Eric Christophera5a79422013-12-09 23:32:48 +000058 : DwarfUnit(UID, D, DICompileUnit(), A, DW, DWU), Language(Language) {}
David Blaikie409dd9c2013-11-19 23:08:21 +000059
David Blaikie319a05f2013-12-02 19:33:10 +000060/// ~Unit - Destructor for compile unit.
Eric Christophera5a79422013-12-09 23:32:48 +000061DwarfUnit::~DwarfUnit() {
Devang Patel0e821f42011-04-12 23:21:44 +000062 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
63 DIEBlocks[j]->~DIEBlock();
64}
65
66/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
67/// information entry.
Eric Christophera5a79422013-12-09 23:32:48 +000068DIEEntry *DwarfUnit::createDIEEntry(DIE *Entry) {
Devang Patel0e821f42011-04-12 23:21:44 +000069 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
70 return Value;
71}
72
Bill Wendling3495f9b2012-12-06 07:55:19 +000073/// getDefaultLowerBound - Return the default lower bound for an array. If the
Bill Wendling28fe9e72012-12-06 07:38:10 +000074/// DWARF version doesn't handle the language, return -1.
Eric Christophera5a79422013-12-09 23:32:48 +000075int64_t DwarfUnit::getDefaultLowerBound() const {
David Blaikiecb8e4352013-11-15 23:50:53 +000076 switch (getLanguage()) {
Bill Wendling28fe9e72012-12-06 07:38:10 +000077 default:
78 break;
79
80 case dwarf::DW_LANG_C89:
81 case dwarf::DW_LANG_C99:
82 case dwarf::DW_LANG_C:
83 case dwarf::DW_LANG_C_plus_plus:
84 case dwarf::DW_LANG_ObjC:
85 case dwarf::DW_LANG_ObjC_plus_plus:
86 return 0;
87
88 case dwarf::DW_LANG_Fortran77:
89 case dwarf::DW_LANG_Fortran90:
90 case dwarf::DW_LANG_Fortran95:
91 return 1;
92
93 // The languages below have valid values only if the DWARF version >= 4.
94 case dwarf::DW_LANG_Java:
95 case dwarf::DW_LANG_Python:
96 case dwarf::DW_LANG_UPC:
97 case dwarf::DW_LANG_D:
98 if (dwarf::DWARF_VERSION >= 4)
99 return 0;
100 break;
101
102 case dwarf::DW_LANG_Ada83:
103 case dwarf::DW_LANG_Ada95:
104 case dwarf::DW_LANG_Cobol74:
105 case dwarf::DW_LANG_Cobol85:
106 case dwarf::DW_LANG_Modula2:
107 case dwarf::DW_LANG_Pascal83:
108 case dwarf::DW_LANG_PLI:
109 if (dwarf::DWARF_VERSION >= 4)
110 return 1;
111 break;
112 }
113
114 return -1;
115}
116
Manman Ren4dbdc902013-10-31 17:54:35 +0000117/// Check whether the DIE for this MDNode can be shared across CUs.
David Blaikie4201ddf2013-11-15 22:59:36 +0000118static bool isShareableAcrossCUs(DIDescriptor D) {
David Blaikiebcb418e2013-11-20 18:40:16 +0000119 // When the MDNode can be part of the type system, the DIE can be shared
120 // across CUs.
121 // Combining type units and cross-CU DIE sharing is lower value (since
122 // cross-CU DIE sharing is used in LTO and removes type redundancy at that
123 // level already) but may be implementable for some value in projects
124 // building multiple independent libraries with LTO and then linking those
125 // together.
David Blaikie409dd9c2013-11-19 23:08:21 +0000126 return (D.isType() ||
127 (D.isSubprogram() && !DISubprogram(D).isDefinition())) &&
Eric Christopher4287a492013-12-09 23:57:44 +0000128 !GenerateDwarfTypeUnits;
Manman Ren4dbdc902013-10-31 17:54:35 +0000129}
130
131/// getDIE - Returns the debug information entry map slot for the
132/// specified debug variable. We delegate the request to DwarfDebug
133/// when the DIE for this MDNode can be shared across CUs. The mappings
134/// will be kept in DwarfDebug for shareable DIEs.
Eric Christophera5a79422013-12-09 23:32:48 +0000135DIE *DwarfUnit::getDIE(DIDescriptor D) const {
David Blaikie2ad00162013-11-15 23:09:13 +0000136 if (isShareableAcrossCUs(D))
137 return DD->getDIE(D);
138 return MDNodeToDieMap.lookup(D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000139}
140
141/// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
142/// when the DIE for this MDNode can be shared across CUs. The mappings
143/// will be kept in DwarfDebug for shareable DIEs.
Eric Christophera5a79422013-12-09 23:32:48 +0000144void DwarfUnit::insertDIE(DIDescriptor Desc, DIE *D) {
David Blaikie2ad00162013-11-15 23:09:13 +0000145 if (isShareableAcrossCUs(Desc)) {
146 DD->insertDIE(Desc, D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000147 return;
148 }
David Blaikie2ad00162013-11-15 23:09:13 +0000149 MDNodeToDieMap.insert(std::make_pair(Desc, D));
Manman Ren4dbdc902013-10-31 17:54:35 +0000150}
151
Eric Christopherbb69a272012-08-24 01:14:27 +0000152/// addFlag - Add a flag that is true.
Eric Christophera5a79422013-12-09 23:32:48 +0000153void DwarfUnit::addFlag(DIE *Die, dwarf::Attribute Attribute) {
Michael Gottesmanc89466f2013-09-04 04:39:38 +0000154 if (DD->getDwarfVersion() >= 4)
Eric Christopherdccd3282013-10-04 22:40:05 +0000155 Die->addValue(Attribute, dwarf::DW_FORM_flag_present, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000156 else
Eric Christopherdccd3282013-10-04 22:40:05 +0000157 Die->addValue(Attribute, dwarf::DW_FORM_flag, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000158}
159
Devang Patel0e821f42011-04-12 23:21:44 +0000160/// addUInt - Add an unsigned integer attribute data and value.
161///
Eric Christophera5a79422013-12-09 23:32:48 +0000162void DwarfUnit::addUInt(DIE *Die, dwarf::Attribute Attribute,
163 Optional<dwarf::Form> Form, uint64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000164 if (!Form)
165 Form = DIEInteger::BestForm(false, Integer);
166 DIEValue *Value = Integer == 1 ? DIEIntegerOne : new (DIEValueAllocator)
167 DIEInteger(Integer);
David Blaikief2443192013-10-21 17:28:37 +0000168 Die->addValue(Attribute, *Form, Value);
169}
170
Eric Christophera5a79422013-12-09 23:32:48 +0000171void DwarfUnit::addUInt(DIEBlock *Block, dwarf::Form Form, uint64_t Integer) {
David Blaikief2443192013-10-21 17:28:37 +0000172 addUInt(Block, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000173}
174
175/// addSInt - Add an signed integer attribute data and value.
176///
Eric Christophera5a79422013-12-09 23:32:48 +0000177void DwarfUnit::addSInt(DIE *Die, dwarf::Attribute Attribute,
178 Optional<dwarf::Form> Form, int64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000179 if (!Form)
180 Form = DIEInteger::BestForm(true, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000181 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
David Blaikief2443192013-10-21 17:28:37 +0000182 Die->addValue(Attribute, *Form, Value);
183}
184
Eric Christophera5a79422013-12-09 23:32:48 +0000185void DwarfUnit::addSInt(DIEBlock *Die, Optional<dwarf::Form> Form,
186 int64_t Integer) {
David Blaikief2443192013-10-21 17:28:37 +0000187 addSInt(Die, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000188}
189
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000190/// addString - Add a string attribute data and value. We always emit a
191/// reference to the string pool instead of immediate strings so that DIEs have
Eric Christopherfba22602013-01-07 19:32:45 +0000192/// more predictable sizes. In the case of split dwarf we emit an index
193/// into another table which gets us the static offset into the string
194/// table.
Eric Christophera5a79422013-12-09 23:32:48 +0000195void DwarfUnit::addString(DIE *Die, dwarf::Attribute Attribute,
196 StringRef String) {
Eric Christopher67646432013-07-26 17:02:41 +0000197 DIEValue *Value;
David Blaikief2443192013-10-21 17:28:37 +0000198 dwarf::Form Form;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000199 if (!DD->useSplitDwarf()) {
200 MCSymbol *Symb = DU->getStringPoolEntry(String);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000201 if (Asm->needsRelocationsForDwarfStringPool())
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000202 Value = new (DIEValueAllocator) DIELabel(Symb);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000203 else {
204 MCSymbol *StringPool = DU->getStringPoolSym();
205 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
206 }
Eric Christopher67646432013-07-26 17:02:41 +0000207 Form = dwarf::DW_FORM_strp;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000208 } else {
209 unsigned idx = DU->getStringPoolIndex(String);
Eric Christopher67646432013-07-26 17:02:41 +0000210 Value = new (DIEValueAllocator) DIEInteger(idx);
211 Form = dwarf::DW_FORM_GNU_str_index;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000212 }
Eric Christopher67646432013-07-26 17:02:41 +0000213 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
214 Die->addValue(Attribute, Form, Str);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000215}
216
217/// addLocalString - Add a string attribute data and value. This is guaranteed
218/// to be in the local string pool instead of indirected.
Eric Christophera5a79422013-12-09 23:32:48 +0000219void DwarfUnit::addLocalString(DIE *Die, dwarf::Attribute Attribute,
220 StringRef String) {
Eric Christophere698f532012-12-20 21:58:36 +0000221 MCSymbol *Symb = DU->getStringPoolEntry(String);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000222 DIEValue *Value;
223 if (Asm->needsRelocationsForDwarfStringPool())
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000224 Value = new (DIEValueAllocator) DIELabel(Symb);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000225 else {
Eric Christophere698f532012-12-20 21:58:36 +0000226 MCSymbol *StringPool = DU->getStringPoolSym();
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000227 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000228 }
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000229 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000230}
231
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000232/// addExpr - Add a Dwarf expression attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000233///
Eric Christophera5a79422013-12-09 23:32:48 +0000234void DwarfUnit::addExpr(DIEBlock *Die, dwarf::Form Form, const MCExpr *Expr) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000235 DIEValue *Value = new (DIEValueAllocator) DIEExpr(Expr);
David Blaikief2443192013-10-21 17:28:37 +0000236 Die->addValue((dwarf::Attribute)0, Form, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000237}
238
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000239/// addLabel - Add a Dwarf label attribute data and value.
240///
Eric Christophera5a79422013-12-09 23:32:48 +0000241void DwarfUnit::addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
242 const MCSymbol *Label) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000243 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
244 Die->addValue(Attribute, Form, Value);
David Blaikief3cd7c52013-06-28 20:05:04 +0000245}
246
Eric Christophera5a79422013-12-09 23:32:48 +0000247void DwarfUnit::addLabel(DIEBlock *Die, dwarf::Form Form,
248 const MCSymbol *Label) {
David Blaikief2443192013-10-21 17:28:37 +0000249 addLabel(Die, (dwarf::Attribute)0, Form, Label);
250}
251
Eric Christopher33ff6972013-11-21 23:46:41 +0000252/// addSectionLabel - Add a Dwarf section label attribute data and value.
253///
Eric Christophera5a79422013-12-09 23:32:48 +0000254void DwarfUnit::addSectionLabel(DIE *Die, dwarf::Attribute Attribute,
255 const MCSymbol *Label) {
Eric Christopher33ff6972013-11-21 23:46:41 +0000256 if (DD->getDwarfVersion() >= 4)
257 addLabel(Die, Attribute, dwarf::DW_FORM_sec_offset, Label);
258 else
259 addLabel(Die, Attribute, dwarf::DW_FORM_data4, Label);
260}
261
262/// addSectionOffset - Add an offset into a section attribute data and value.
263///
Eric Christophera5a79422013-12-09 23:32:48 +0000264void DwarfUnit::addSectionOffset(DIE *Die, dwarf::Attribute Attribute,
265 uint64_t Integer) {
Eric Christopher33ff6972013-11-21 23:46:41 +0000266 if (DD->getDwarfVersion() >= 4)
267 addUInt(Die, Attribute, dwarf::DW_FORM_sec_offset, Integer);
268 else
269 addUInt(Die, Attribute, dwarf::DW_FORM_data4, Integer);
270}
271
Eric Christopher962c9082013-01-15 23:56:56 +0000272/// addLabelAddress - Add a dwarf label attribute data and value using
273/// DW_FORM_addr or DW_FORM_GNU_addr_index.
274///
Eric Christopher4287a492013-12-09 23:57:44 +0000275void DwarfCompileUnit::addLabelAddress(DIE *Die, dwarf::Attribute Attribute,
276 MCSymbol *Label) {
Alexey Samsonov4436bf02013-10-03 08:54:43 +0000277 if (Label)
278 DD->addArangeLabel(SymbolCU(this, Label));
Richard Mitton21101b32013-09-19 23:21:01 +0000279
Eric Christopher962c9082013-01-15 23:56:56 +0000280 if (!DD->useSplitDwarf()) {
281 if (Label != NULL) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000282 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
Eric Christopher962c9082013-01-15 23:56:56 +0000283 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
284 } else {
285 DIEValue *Value = new (DIEValueAllocator) DIEInteger(0);
286 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
287 }
288 } else {
289 unsigned idx = DU->getAddrPoolIndex(Label);
290 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
291 Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
292 }
293}
294
Eric Christophere9ec2452013-01-18 22:11:33 +0000295/// addOpAddress - Add a dwarf op address data and value using the
296/// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
297///
Eric Christophera5a79422013-12-09 23:32:48 +0000298void DwarfUnit::addOpAddress(DIEBlock *Die, const MCSymbol *Sym) {
Eric Christophere9ec2452013-01-18 22:11:33 +0000299 if (!DD->useSplitDwarf()) {
David Blaikief2443192013-10-21 17:28:37 +0000300 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
301 addLabel(Die, dwarf::DW_FORM_udata, Sym);
Eric Christophere9ec2452013-01-18 22:11:33 +0000302 } else {
David Blaikief2443192013-10-21 17:28:37 +0000303 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
304 addUInt(Die, dwarf::DW_FORM_GNU_addr_index, DU->getAddrPoolIndex(Sym));
Eric Christophere9ec2452013-01-18 22:11:33 +0000305 }
306}
307
Eric Christopher33ff6972013-11-21 23:46:41 +0000308/// addSectionDelta - Add a section label delta attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000309///
Eric Christophera5a79422013-12-09 23:32:48 +0000310void DwarfUnit::addSectionDelta(DIE *Die, dwarf::Attribute Attribute,
311 const MCSymbol *Hi, const MCSymbol *Lo) {
Devang Patel0e821f42011-04-12 23:21:44 +0000312 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Eric Christopher33ff6972013-11-21 23:46:41 +0000313 if (DD->getDwarfVersion() >= 4)
314 Die->addValue(Attribute, dwarf::DW_FORM_sec_offset, Value);
315 else
316 Die->addValue(Attribute, dwarf::DW_FORM_data4, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000317}
318
319/// addDIEEntry - Add a DIE attribute data and value.
320///
Eric Christophera5a79422013-12-09 23:32:48 +0000321void DwarfUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +0000322 addDIEEntry(Die, Attribute, createDIEEntry(Entry));
323}
324
Eric Christophera5a79422013-12-09 23:32:48 +0000325void DwarfUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute,
326 DIEEntry *Entry) {
David Blaikie409dd9c2013-11-19 23:08:21 +0000327 const DIE *DieCU = Die->getUnitOrNull();
328 const DIE *EntryCU = Entry->getEntry()->getUnitOrNull();
Manman Ren4dbdc902013-10-31 17:54:35 +0000329 if (!DieCU)
330 // We assume that Die belongs to this CU, if it is not linked to any CU yet.
David Blaikie2a80e442013-12-02 22:09:48 +0000331 DieCU = getUnitDie();
Manman Ren4dbdc902013-10-31 17:54:35 +0000332 if (!EntryCU)
David Blaikie2a80e442013-12-02 22:09:48 +0000333 EntryCU = getUnitDie();
Manman Ren4dbdc902013-10-31 17:54:35 +0000334 Die->addValue(Attribute, EntryCU == DieCU ? dwarf::DW_FORM_ref4
335 : dwarf::DW_FORM_ref_addr,
336 Entry);
Devang Patel0e821f42011-04-12 23:21:44 +0000337}
338
Manman Renb987e512013-10-29 00:53:03 +0000339/// Create a DIE with the given Tag, add the DIE to its parent, and
340/// call insertDIE if MD is not null.
Eric Christophera5a79422013-12-09 23:32:48 +0000341DIE *DwarfUnit::createAndAddDIE(unsigned Tag, DIE &Parent, DIDescriptor N) {
Manman Renb987e512013-10-29 00:53:03 +0000342 DIE *Die = new DIE(Tag);
343 Parent.addChild(Die);
David Blaikie52c50202013-11-16 00:29:01 +0000344 if (N)
345 insertDIE(N, Die);
Manman Renb987e512013-10-29 00:53:03 +0000346 return Die;
347}
348
Devang Patel0e821f42011-04-12 23:21:44 +0000349/// addBlock - Add block data.
350///
Eric Christophera5a79422013-12-09 23:32:48 +0000351void DwarfUnit::addBlock(DIE *Die, dwarf::Attribute Attribute,
352 DIEBlock *Block) {
Devang Patel0e821f42011-04-12 23:21:44 +0000353 Block->ComputeSize(Asm);
354 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
355 Die->addValue(Attribute, Block->BestForm(), Block);
356}
357
358/// addSourceLine - Add location information to specified debug information
359/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000360void DwarfUnit::addSourceLine(DIE *Die, DIVariable V) {
Devang Patel0e821f42011-04-12 23:21:44 +0000361 // Verify variable.
Manman Ren7504ed42013-07-08 18:33:29 +0000362 if (!V.isVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000363 return;
Eric Christopher92331fd2012-11-21 00:34:38 +0000364
Devang Patel0e821f42011-04-12 23:21:44 +0000365 unsigned Line = V.getLineNumber();
366 if (Line == 0)
367 return;
Eric Christopherc2697f82013-10-19 01:04:47 +0000368 unsigned FileID =
369 DD->getOrCreateSourceID(V.getContext().getFilename(),
370 V.getContext().getDirectory(), getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000371 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000372 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
373 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000374}
375
376/// addSourceLine - Add location information to specified debug information
377/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000378void DwarfUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
Devang Patel0e821f42011-04-12 23:21:44 +0000379 // Verify global variable.
Manman Ren7504ed42013-07-08 18:33:29 +0000380 if (!G.isGlobalVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000381 return;
382
383 unsigned Line = G.getLineNumber();
384 if (Line == 0)
385 return;
Eric Christopherc2697f82013-10-19 01:04:47 +0000386 unsigned FileID =
387 DD->getOrCreateSourceID(G.getFilename(), G.getDirectory(), getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000388 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000389 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
390 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000391}
392
393/// addSourceLine - Add location information to specified debug information
394/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000395void DwarfUnit::addSourceLine(DIE *Die, DISubprogram SP) {
Devang Patel0e821f42011-04-12 23:21:44 +0000396 // Verify subprogram.
Manman Ren7504ed42013-07-08 18:33:29 +0000397 if (!SP.isSubprogram())
Devang Patel0e821f42011-04-12 23:21:44 +0000398 return;
Eric Christopher7734ca22012-03-15 23:55:40 +0000399
Devang Patel0e821f42011-04-12 23:21:44 +0000400 // If the line number is 0, don't add it.
Eric Christopher7734ca22012-03-15 23:55:40 +0000401 unsigned Line = SP.getLineNumber();
402 if (Line == 0)
Devang Patel0e821f42011-04-12 23:21:44 +0000403 return;
404
Eric Christopherc2697f82013-10-19 01:04:47 +0000405 unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(), SP.getDirectory(),
406 getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000407 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000408 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
409 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000410}
411
412/// addSourceLine - Add location information to specified debug information
413/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000414void DwarfUnit::addSourceLine(DIE *Die, DIType Ty) {
Devang Patel0e821f42011-04-12 23:21:44 +0000415 // Verify type.
Manman Ren7504ed42013-07-08 18:33:29 +0000416 if (!Ty.isType())
Devang Patel0e821f42011-04-12 23:21:44 +0000417 return;
418
419 unsigned Line = Ty.getLineNumber();
Eric Christopher7734ca22012-03-15 23:55:40 +0000420 if (Line == 0)
Devang Patel0e821f42011-04-12 23:21:44 +0000421 return;
Eric Christopherc2697f82013-10-19 01:04:47 +0000422 unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(), Ty.getDirectory(),
423 getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000424 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000425 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
426 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000427}
428
429/// addSourceLine - Add location information to specified debug information
430/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000431void DwarfUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
Eric Christopher70e1bd82012-03-29 08:42:56 +0000432 // Verify type.
Manman Ren7504ed42013-07-08 18:33:29 +0000433 if (!Ty.isObjCProperty())
Eric Christopher70e1bd82012-03-29 08:42:56 +0000434 return;
435
436 unsigned Line = Ty.getLineNumber();
437 if (Line == 0)
438 return;
439 DIFile File = Ty.getFile();
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000440 unsigned FileID = DD->getOrCreateSourceID(File.getFilename(),
Manman Ren1e427202013-03-07 01:42:00 +0000441 File.getDirectory(), getUniqueID());
Eric Christopher70e1bd82012-03-29 08:42:56 +0000442 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000443 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
444 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Eric Christopher70e1bd82012-03-29 08:42:56 +0000445}
446
447/// addSourceLine - Add location information to specified debug information
448/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000449void DwarfUnit::addSourceLine(DIE *Die, DINameSpace NS) {
Devang Patel0e821f42011-04-12 23:21:44 +0000450 // Verify namespace.
451 if (!NS.Verify())
452 return;
453
454 unsigned Line = NS.getLineNumber();
455 if (Line == 0)
456 return;
457 StringRef FN = NS.getFilename();
458
Eric Christopherc2697f82013-10-19 01:04:47 +0000459 unsigned FileID =
460 DD->getOrCreateSourceID(FN, NS.getDirectory(), getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000461 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000462 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
463 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000464}
465
Eric Christopher92331fd2012-11-21 00:34:38 +0000466/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patel77dc5412011-04-27 22:45:24 +0000467/// DbgVariable based on provided MachineLocation.
Eric Christophera5a79422013-12-09 23:32:48 +0000468void DwarfUnit::addVariableAddress(const DbgVariable &DV, DIE *Die,
469 MachineLocation Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000470 if (DV.variableHasComplexAddress())
Devang Patel0e821f42011-04-12 23:21:44 +0000471 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000472 else if (DV.isBlockByrefVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000473 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
474 else
David Blaikieea2605d2013-06-20 00:25:24 +0000475 addAddress(Die, dwarf::DW_AT_location, Location,
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000476 DV.getVariable().isIndirect());
Devang Patel0e821f42011-04-12 23:21:44 +0000477}
478
Devang Patelba5fbf12011-04-26 19:06:18 +0000479/// addRegisterOp - Add register operand.
Eric Christophera5a79422013-12-09 23:32:48 +0000480void DwarfUnit::addRegisterOp(DIEBlock *TheDie, unsigned Reg) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000481 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
482 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
483 if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000484 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000485 else {
David Blaikief2443192013-10-21 17:28:37 +0000486 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
487 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000488 }
489}
490
491/// addRegisterOffset - Add register offset.
Eric Christophera5a79422013-12-09 23:32:48 +0000492void DwarfUnit::addRegisterOffset(DIEBlock *TheDie, unsigned Reg,
493 int64_t Offset) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000494 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
495 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
496 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
497 if (Reg == TRI->getFrameRegister(*Asm->MF))
498 // If variable offset is based in frame register then use fbreg.
David Blaikief2443192013-10-21 17:28:37 +0000499 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000500 else if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000501 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000502 else {
David Blaikief2443192013-10-21 17:28:37 +0000503 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
504 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000505 }
David Blaikief2443192013-10-21 17:28:37 +0000506 addSInt(TheDie, dwarf::DW_FORM_sdata, Offset);
Devang Patelba5fbf12011-04-26 19:06:18 +0000507}
508
509/// addAddress - Add an address attribute to a die based on the location
510/// provided.
Eric Christophera5a79422013-12-09 23:32:48 +0000511void DwarfUnit::addAddress(DIE *Die, dwarf::Attribute Attribute,
512 const MachineLocation &Location, bool Indirect) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000513 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
514
David Blaikieea2605d2013-06-20 00:25:24 +0000515 if (Location.isReg() && !Indirect)
Devang Patelba5fbf12011-04-26 19:06:18 +0000516 addRegisterOp(Block, Location.getReg());
David Blaikieea2605d2013-06-20 00:25:24 +0000517 else {
Devang Patelba5fbf12011-04-26 19:06:18 +0000518 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
David Blaikieea2605d2013-06-20 00:25:24 +0000519 if (Indirect && !Location.isReg()) {
David Blaikief2443192013-10-21 17:28:37 +0000520 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
David Blaikieea2605d2013-06-20 00:25:24 +0000521 }
522 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000523
524 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000525 addBlock(Die, Attribute, Block);
Devang Patelba5fbf12011-04-26 19:06:18 +0000526}
527
Devang Patel0e821f42011-04-12 23:21:44 +0000528/// addComplexAddress - Start with the address based on the location provided,
529/// and generate the DWARF information necessary to find the actual variable
Eric Christopher1c70b672013-12-05 00:13:15 +0000530/// given the extra address information encoded in the DbgVariable, starting
531/// from the starting location. Add the DWARF information to the die.
Devang Patel0e821f42011-04-12 23:21:44 +0000532///
Eric Christophera5a79422013-12-09 23:32:48 +0000533void DwarfUnit::addComplexAddress(const DbgVariable &DV, DIE *Die,
534 dwarf::Attribute Attribute,
535 const MachineLocation &Location) {
Devang Patel0e821f42011-04-12 23:21:44 +0000536 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000537 unsigned N = DV.getNumAddrElements();
Devang Patel3e021532011-04-28 02:22:40 +0000538 unsigned i = 0;
539 if (Location.isReg()) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000540 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Devang Patel3e021532011-04-28 02:22:40 +0000541 // If first address element is OpPlus then emit
542 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000543 addRegisterOffset(Block, Location.getReg(), DV.getAddrElement(1));
Devang Patel3e021532011-04-28 02:22:40 +0000544 i = 2;
545 } else
546 addRegisterOp(Block, Location.getReg());
Eric Christopherc2697f82013-10-19 01:04:47 +0000547 } else
Devang Patelba5fbf12011-04-26 19:06:18 +0000548 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000549
Eric Christopherc2697f82013-10-19 01:04:47 +0000550 for (; i < N; ++i) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000551 uint64_t Element = DV.getAddrElement(i);
Devang Patel0e821f42011-04-12 23:21:44 +0000552 if (Element == DIBuilder::OpPlus) {
David Blaikief2443192013-10-21 17:28:37 +0000553 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
554 addUInt(Block, dwarf::DW_FORM_udata, DV.getAddrElement(++i));
Devang Patel0e821f42011-04-12 23:21:44 +0000555 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher4d250522012-05-08 18:56:00 +0000556 if (!Location.isReg())
David Blaikief2443192013-10-21 17:28:37 +0000557 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Eric Christopherc2697f82013-10-19 01:04:47 +0000558 } else
559 llvm_unreachable("unknown DIBuilder Opcode");
Devang Patel0e821f42011-04-12 23:21:44 +0000560 }
561
562 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000563 addBlock(Die, Attribute, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000564}
565
566/* Byref variables, in Blocks, are declared by the programmer as "SomeType
567 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
568 gives the variable VarName either the struct, or a pointer to the struct, as
569 its type. This is necessary for various behind-the-scenes things the
570 compiler needs to do with by-reference variables in Blocks.
571
572 However, as far as the original *programmer* is concerned, the variable
573 should still have type 'SomeType', as originally declared.
574
575 The function getBlockByrefType dives into the __Block_byref_x_VarName
576 struct to find the original type of the variable, which is then assigned to
577 the variable's Debug Information Entry as its real type. So far, so good.
578 However now the debugger will expect the variable VarName to have the type
579 SomeType. So we need the location attribute for the variable to be an
580 expression that explains to the debugger how to navigate through the
581 pointers and struct to find the actual variable of type SomeType.
582
583 The following function does just that. We start by getting
584 the "normal" location for the variable. This will be the location
585 of either the struct __Block_byref_x_VarName or the pointer to the
586 struct __Block_byref_x_VarName.
587
588 The struct will look something like:
589
590 struct __Block_byref_x_VarName {
591 ... <various fields>
592 struct __Block_byref_x_VarName *forwarding;
593 ... <various other fields>
594 SomeType VarName;
595 ... <maybe more fields>
596 };
597
598 If we are given the struct directly (as our starting point) we
599 need to tell the debugger to:
600
601 1). Add the offset of the forwarding field.
602
603 2). Follow that pointer to get the real __Block_byref_x_VarName
604 struct to use (the real one may have been copied onto the heap).
605
606 3). Add the offset for the field VarName, to find the actual variable.
607
608 If we started with a pointer to the struct, then we need to
609 dereference that pointer first, before the other steps.
610 Translating this into DWARF ops, we will need to append the following
611 to the current location description for the variable:
612
613 DW_OP_deref -- optional, if we start with a pointer
614 DW_OP_plus_uconst <forward_fld_offset>
615 DW_OP_deref
616 DW_OP_plus_uconst <varName_fld_offset>
617
618 That is what this function does. */
619
620/// addBlockByrefAddress - Start with the address based on the location
621/// provided, and generate the DWARF information necessary to find the
622/// actual Block variable (navigating the Block struct) based on the
623/// starting location. Add the DWARF information to the die. For
624/// more information, read large comment just above here.
625///
Eric Christophera5a79422013-12-09 23:32:48 +0000626void DwarfUnit::addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
627 dwarf::Attribute Attribute,
628 const MachineLocation &Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000629 DIType Ty = DV.getType();
Devang Patel0e821f42011-04-12 23:21:44 +0000630 DIType TmpTy = Ty;
Eric Christopher31b05762013-08-08 01:41:00 +0000631 uint16_t Tag = Ty.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +0000632 bool isPointer = false;
633
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000634 StringRef varName = DV.getName();
Devang Patel0e821f42011-04-12 23:21:44 +0000635
636 if (Tag == dwarf::DW_TAG_pointer_type) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000637 DIDerivedType DTy(Ty);
Manman Ren93b30902013-10-08 18:42:58 +0000638 TmpTy = resolve(DTy.getTypeDerivedFrom());
Devang Patel0e821f42011-04-12 23:21:44 +0000639 isPointer = true;
640 }
641
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000642 DICompositeType blockStruct(TmpTy);
Devang Patel0e821f42011-04-12 23:21:44 +0000643
644 // Find the __forwarding field and the variable field in the __Block_byref
645 // struct.
646 DIArray Fields = blockStruct.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000647 DIDerivedType varField;
648 DIDerivedType forwardingField;
Devang Patel0e821f42011-04-12 23:21:44 +0000649
650 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000651 DIDerivedType DT(Fields.getElement(i));
Devang Patel0e821f42011-04-12 23:21:44 +0000652 StringRef fieldName = DT.getName();
653 if (fieldName == "__forwarding")
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000654 forwardingField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000655 else if (fieldName == varName)
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000656 varField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000657 }
658
659 // Get the offsets for the forwarding field and the variable field.
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000660 unsigned forwardingFieldOffset = forwardingField.getOffsetInBits() >> 3;
661 unsigned varFieldOffset = varField.getOffsetInBits() >> 2;
Devang Patel0e821f42011-04-12 23:21:44 +0000662
663 // Decode the original location, and use that as the start of the byref
664 // variable's location.
Devang Patel0e821f42011-04-12 23:21:44 +0000665 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
666
Eric Christopheref9d7102012-07-04 02:02:18 +0000667 if (Location.isReg())
668 addRegisterOp(Block, Location.getReg());
669 else
670 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000671
672 // If we started with a pointer to the __Block_byref... struct, then
673 // the first thing we need to do is dereference the pointer (DW_OP_deref).
674 if (isPointer)
David Blaikief2443192013-10-21 17:28:37 +0000675 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000676
677 // Next add the offset for the '__forwarding' field:
678 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
679 // adding the offset if it's 0.
680 if (forwardingFieldOffset > 0) {
David Blaikief2443192013-10-21 17:28:37 +0000681 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
682 addUInt(Block, dwarf::DW_FORM_udata, forwardingFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000683 }
684
685 // Now dereference the __forwarding field to get to the real __Block_byref
686 // struct: DW_OP_deref.
David Blaikief2443192013-10-21 17:28:37 +0000687 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000688
689 // Now that we've got the real __Block_byref... struct, add the offset
690 // for the variable's field to get to the location of the actual variable:
691 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
692 if (varFieldOffset > 0) {
David Blaikief2443192013-10-21 17:28:37 +0000693 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
694 addUInt(Block, dwarf::DW_FORM_udata, varFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000695 }
696
697 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000698 addBlock(Die, Attribute, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000699}
700
Devang Patelbcd50a12011-07-20 21:57:04 +0000701/// isTypeSigned - Return true if the type is signed.
Manman Renb3388602013-10-05 01:43:03 +0000702static bool isTypeSigned(DwarfDebug *DD, DIType Ty, int *SizeInBits) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000703 if (Ty.isDerivedType())
Manman Renb3388602013-10-05 01:43:03 +0000704 return isTypeSigned(DD, DD->resolve(DIDerivedType(Ty).getTypeDerivedFrom()),
705 SizeInBits);
Devang Patelbcd50a12011-07-20 21:57:04 +0000706 if (Ty.isBasicType())
Eric Christopherc2697f82013-10-19 01:04:47 +0000707 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed ||
708 DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000709 *SizeInBits = Ty.getSizeInBits();
710 return true;
711 }
712 return false;
713}
714
Manman Renb3388602013-10-05 01:43:03 +0000715/// Return true if type encoding is unsigned.
716static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) {
717 DIDerivedType DTy(Ty);
718 if (DTy.isDerivedType())
719 return isUnsignedDIType(DD, DD->resolve(DTy.getTypeDerivedFrom()));
720
721 DIBasicType BTy(Ty);
722 if (BTy.isBasicType()) {
723 unsigned Encoding = BTy.getEncoding();
724 if (Encoding == dwarf::DW_ATE_unsigned ||
725 Encoding == dwarf::DW_ATE_unsigned_char ||
726 Encoding == dwarf::DW_ATE_boolean)
727 return true;
728 }
729 return false;
730}
731
732/// If this type is derived from a base type then return base type size.
Manman Renbda410f2013-10-08 18:46:58 +0000733static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) {
Manman Renb3388602013-10-05 01:43:03 +0000734 unsigned Tag = Ty.getTag();
735
736 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
737 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
738 Tag != dwarf::DW_TAG_restrict_type)
739 return Ty.getSizeInBits();
740
741 DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom());
742
743 // If this type is not derived from any type then take conservative approach.
744 if (!BaseType.isValid())
745 return Ty.getSizeInBits();
746
747 // If this is a derived type, go ahead and get the base type, unless it's a
748 // reference then it's just the size of the field. Pointer types have no need
749 // of this since they're a different type of qualification on the type.
750 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
751 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
752 return Ty.getSizeInBits();
753
754 if (BaseType.isDerivedType())
Manman Renbda410f2013-10-08 18:46:58 +0000755 return getBaseTypeSize(DD, DIDerivedType(BaseType));
Manman Renb3388602013-10-05 01:43:03 +0000756
757 return BaseType.getSizeInBits();
758}
759
Devang Patel0e821f42011-04-12 23:21:44 +0000760/// addConstantValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000761void DwarfUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
762 DIType Ty) {
David Blaikiea1e813d2013-05-10 21:52:07 +0000763 // FIXME: This is a bit conservative/simple - it emits negative values at
764 // their maximum bit width which is a bit unfortunate (& doesn't prefer
765 // udata/sdata over dataN as suggested by the DWARF spec)
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000766 assert(MO.isImm() && "Invalid machine operand!");
Devang Patelbcd50a12011-07-20 21:57:04 +0000767 int SizeInBits = -1;
Manman Renb3388602013-10-05 01:43:03 +0000768 bool SignedConstant = isTypeSigned(DD, Ty, &SizeInBits);
David Blaikief2443192013-10-21 17:28:37 +0000769 dwarf::Form Form;
Devang Patelf1d04702011-05-27 18:15:52 +0000770
Eric Christopher9d1daa82013-08-27 23:49:04 +0000771 // If we're a signed constant definitely use sdata.
772 if (SignedConstant) {
773 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, MO.getImm());
774 return;
775 }
776
777 // Else use data for now unless it's larger than we can deal with.
778 switch (SizeInBits) {
779 case 8:
780 Form = dwarf::DW_FORM_data1;
781 break;
782 case 16:
783 Form = dwarf::DW_FORM_data2;
784 break;
785 case 32:
786 Form = dwarf::DW_FORM_data4;
787 break;
788 case 64:
789 Form = dwarf::DW_FORM_data8;
790 break;
791 default:
792 Form = dwarf::DW_FORM_udata;
793 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
794 return;
795 }
796 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
Devang Patel0e821f42011-04-12 23:21:44 +0000797}
798
799/// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000800void DwarfUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000801 assert(MO.isFPImm() && "Invalid machine operand!");
Devang Patel0e821f42011-04-12 23:21:44 +0000802 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
803 APFloat FPImm = MO.getFPImm()->getValueAPF();
804
805 // Get the raw data form of the floating point.
806 const APInt FltVal = FPImm.bitcastToAPInt();
Eric Christopherc2697f82013-10-19 01:04:47 +0000807 const char *FltPtr = (const char *)FltVal.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000808
809 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000810 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000811 int Incr = (LittleEndian ? 1 : -1);
812 int Start = (LittleEndian ? 0 : NumBytes - 1);
813 int Stop = (LittleEndian ? NumBytes : -1);
814
815 // Output the constant to DWARF one byte at a time.
816 for (; Start != Stop; Start += Incr)
Eric Christopher98b7f172013-11-11 18:52:36 +0000817 addUInt(Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]);
Devang Patel0e821f42011-04-12 23:21:44 +0000818
David Blaikief2443192013-10-21 17:28:37 +0000819 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000820}
821
David Blaikiea39a76e2013-01-20 01:18:01 +0000822/// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000823void DwarfUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000824 // Pass this down to addConstantValue as an unsigned bag of bits.
825 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
David Blaikiea39a76e2013-01-20 01:18:01 +0000826}
827
Devang Patel0e821f42011-04-12 23:21:44 +0000828/// addConstantValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000829void DwarfUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
830 bool Unsigned) {
Eric Christopher78fcf4902013-07-03 01:08:30 +0000831 addConstantValue(Die, CI->getValue(), Unsigned);
David Blaikiea39a76e2013-01-20 01:18:01 +0000832}
833
834// addConstantValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000835void DwarfUnit::addConstantValue(DIE *Die, const APInt &Val, bool Unsigned) {
David Blaikiea39a76e2013-01-20 01:18:01 +0000836 unsigned CIBitWidth = Val.getBitWidth();
Devang Patel8816bbc2011-05-28 00:39:18 +0000837 if (CIBitWidth <= 64) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000838 // If we're a signed constant definitely use sdata.
839 if (!Unsigned) {
840 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
841 Val.getSExtValue());
842 return;
Devang Patel8816bbc2011-05-28 00:39:18 +0000843 }
Eric Christopher9d1daa82013-08-27 23:49:04 +0000844
845 // Else use data for now unless it's larger than we can deal with.
David Blaikief2443192013-10-21 17:28:37 +0000846 dwarf::Form Form;
Eric Christopher9d1daa82013-08-27 23:49:04 +0000847 switch (CIBitWidth) {
848 case 8:
849 Form = dwarf::DW_FORM_data1;
850 break;
851 case 16:
852 Form = dwarf::DW_FORM_data2;
853 break;
854 case 32:
855 Form = dwarf::DW_FORM_data4;
856 break;
857 case 64:
858 Form = dwarf::DW_FORM_data8;
859 break;
860 default:
861 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
862 Val.getZExtValue());
863 return;
864 }
865 addUInt(Die, dwarf::DW_AT_const_value, Form, Val.getZExtValue());
Eric Christopher78fcf4902013-07-03 01:08:30 +0000866 return;
Devang Patel0e821f42011-04-12 23:21:44 +0000867 }
868
869 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
870
871 // Get the raw data form of the large APInt.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000872 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000873
874 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000875 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000876
877 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000878 for (int i = 0; i < NumBytes; i++) {
879 uint8_t c;
880 if (LittleEndian)
881 c = Ptr64[i / 8] >> (8 * (i & 7));
882 else
883 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
David Blaikief2443192013-10-21 17:28:37 +0000884 addUInt(Block, dwarf::DW_FORM_data1, c);
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000885 }
Devang Patel0e821f42011-04-12 23:21:44 +0000886
David Blaikief2443192013-10-21 17:28:37 +0000887 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000888}
889
Eric Christopher25e35092013-04-22 07:47:40 +0000890/// addTemplateParams - Add template parameters into buffer.
Eric Christophera5a79422013-12-09 23:32:48 +0000891void DwarfUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
Devang Patel0e821f42011-04-12 23:21:44 +0000892 // Add template parameters.
893 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
894 DIDescriptor Element = TParams.getElement(i);
895 if (Element.isTemplateTypeParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000896 constructTemplateTypeParameterDIE(Buffer,
897 DITemplateTypeParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000898 else if (Element.isTemplateValueParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000899 constructTemplateValueParameterDIE(Buffer,
900 DITemplateValueParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000901 }
Devang Patel0e821f42011-04-12 23:21:44 +0000902}
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000903
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000904/// getOrCreateContextDIE - Get context owner's DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000905DIE *DwarfUnit::getOrCreateContextDIE(DIScope Context) {
David Blaikiebd700e42013-11-14 21:24:34 +0000906 if (!Context || Context.isFile())
David Blaikie2a80e442013-12-02 22:09:48 +0000907 return getUnitDie();
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000908 if (Context.isType())
909 return getOrCreateTypeDIE(DIType(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000910 if (Context.isNameSpace())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000911 return getOrCreateNameSpace(DINameSpace(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000912 if (Context.isSubprogram())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000913 return getOrCreateSubprogramDIE(DISubprogram(Context));
Adrian Prantl7d828bb2013-11-15 21:05:09 +0000914 return getDIE(Context);
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000915}
916
Eric Christophera5a79422013-12-09 23:32:48 +0000917DIE *DwarfUnit::createTypeDIE(DICompositeType Ty) {
David Blaikie9d861be2013-11-26 00:15:27 +0000918 DIScope Context = resolve(Ty.getContext());
919 DIE *ContextDIE = getOrCreateContextDIE(Context);
David Blaikie409dd9c2013-11-19 23:08:21 +0000920
921 DIE *TyDIE = getDIE(Ty);
922 if (TyDIE)
923 return TyDIE;
924
925 // Create new type.
926 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
927
David Blaikiefbd29eb2013-11-26 00:35:04 +0000928 constructTypeDIE(*TyDIE, Ty);
David Blaikie409dd9c2013-11-19 23:08:21 +0000929
David Blaikie9d861be2013-11-26 00:15:27 +0000930 updateAcceleratorTables(Context, Ty, TyDIE);
David Blaikie409dd9c2013-11-19 23:08:21 +0000931 return TyDIE;
932}
933
David Blaikie8a263cb2013-11-26 00:22:37 +0000934/// Return true if the type is appropriately scoped to be contained inside
935/// its own type unit.
Eric Christopher4287a492013-12-09 23:57:44 +0000936static bool isDwarfTypeUnitScoped(DIType Ty, const DwarfDebug *DD) {
David Blaikie8a263cb2013-11-26 00:22:37 +0000937 DIScope Parent = DD->resolve(Ty.getContext());
938 while (Parent) {
939 // Don't generate a hash for anything scoped inside a function.
940 if (Parent.isSubprogram())
941 return false;
942 Parent = DD->resolve(Parent.getContext());
943 }
944 return true;
945}
946
947/// Return true if the type should be split out into a type unit.
Eric Christopher4287a492013-12-09 23:57:44 +0000948static bool shouldCreateDwarfTypeUnit(DICompositeType CTy,
949 const DwarfDebug *DD) {
950 if (!GenerateDwarfTypeUnits)
David Blaikie8a263cb2013-11-26 00:22:37 +0000951 return false;
952
953 uint16_t Tag = CTy.getTag();
954
955 switch (Tag) {
956 case dwarf::DW_TAG_structure_type:
957 case dwarf::DW_TAG_union_type:
958 case dwarf::DW_TAG_enumeration_type:
959 case dwarf::DW_TAG_class_type:
960 // If this is a class, structure, union, or enumeration type
961 // that is a definition (not a declaration), and not scoped
962 // inside a function then separate this out as a type unit.
Eric Christopher4287a492013-12-09 23:57:44 +0000963 return !CTy.isForwardDecl() && isDwarfTypeUnitScoped(CTy, DD);
David Blaikie8a263cb2013-11-26 00:22:37 +0000964 default:
965 return false;
966 }
967}
968
Devang Patel0e821f42011-04-12 23:21:44 +0000969/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
970/// given DIType.
Eric Christophera5a79422013-12-09 23:32:48 +0000971DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
David Blaikie32887552013-11-14 22:25:02 +0000972 if (!TyNode)
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000973 return NULL;
Manman Renf4c339e2013-10-29 22:49:29 +0000974
David Blaikie32887552013-11-14 22:25:02 +0000975 DIType Ty(TyNode);
976 assert(Ty.isType());
977
Manman Renf4c339e2013-10-29 22:49:29 +0000978 // Construct the context before querying for the existence of the DIE in case
979 // such construction creates the DIE.
David Blaikie9d861be2013-11-26 00:15:27 +0000980 DIScope Context = resolve(Ty.getContext());
981 DIE *ContextDIE = getOrCreateContextDIE(Context);
Adrian Prantl4583f7d2013-11-15 23:21:39 +0000982 assert(ContextDIE);
Manman Renf4c339e2013-10-29 22:49:29 +0000983
Eric Christophere595bae2013-10-04 17:08:38 +0000984 DIE *TyDIE = getDIE(Ty);
Devang Patel0e821f42011-04-12 23:21:44 +0000985 if (TyDIE)
986 return TyDIE;
987
988 // Create new type.
Manman Renf4c339e2013-10-29 22:49:29 +0000989 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
990
Devang Patel0e821f42011-04-12 23:21:44 +0000991 if (Ty.isBasicType())
992 constructTypeDIE(*TyDIE, DIBasicType(Ty));
David Blaikie8a263cb2013-11-26 00:22:37 +0000993 else if (Ty.isCompositeType()) {
994 DICompositeType CTy(Ty);
Eric Christopher4287a492013-12-09 23:57:44 +0000995 if (shouldCreateDwarfTypeUnit(CTy, DD)) {
996 DD->addDwarfTypeUnitType(getLanguage(), TyDIE, CTy);
David Blaikie8a263cb2013-11-26 00:22:37 +0000997 // Skip updating the accellerator tables since this is not the full type
998 return TyDIE;
999 }
David Blaikiefbd29eb2013-11-26 00:35:04 +00001000 constructTypeDIE(*TyDIE, CTy);
David Blaikie8a263cb2013-11-26 00:22:37 +00001001 } else {
Devang Patel0e821f42011-04-12 23:21:44 +00001002 assert(Ty.isDerivedType() && "Unknown kind of DIType");
1003 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
1004 }
David Blaikie2ea848b2013-11-19 22:51:04 +00001005
David Blaikie9d861be2013-11-26 00:15:27 +00001006 updateAcceleratorTables(Context, Ty, TyDIE);
David Blaikie2ea848b2013-11-19 22:51:04 +00001007
1008 return TyDIE;
1009}
1010
Eric Christophera5a79422013-12-09 23:32:48 +00001011void DwarfUnit::updateAcceleratorTables(DIScope Context, DIType Ty,
1012 const DIE *TyDIE) {
Eric Christopher21bde872012-01-06 04:35:23 +00001013 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
1014 bool IsImplementation = 0;
1015 if (Ty.isCompositeType()) {
1016 DICompositeType CT(Ty);
Eric Christopher8ea8e4f2012-01-06 23:03:37 +00001017 // A runtime language of 0 actually means C/C++ and that any
1018 // non-negative value is some version of Objective-C/C++.
Eric Christopherc2697f82013-10-19 01:04:47 +00001019 IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete();
Eric Christopher21bde872012-01-06 04:35:23 +00001020 }
Eric Christophercf7289f2013-09-05 18:20:16 +00001021 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
Eric Christopher8ea8e4f2012-01-06 23:03:37 +00001022 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
David Blaikie9d861be2013-11-26 00:15:27 +00001023
1024 if (!Context || Context.isCompileUnit() || Context.isFile() ||
1025 Context.isNameSpace())
1026 GlobalTypes[getParentContextString(Context) + Ty.getName().str()] = TyDIE;
Eric Christopher21bde872012-01-06 04:35:23 +00001027 }
Devang Patel0e821f42011-04-12 23:21:44 +00001028}
1029
1030/// addType - Add a new type attribute to the specified entity.
Eric Christophera5a79422013-12-09 23:32:48 +00001031void DwarfUnit::addType(DIE *Entity, DIType Ty, dwarf::Attribute Attribute) {
Eric Christopher0df08e22013-08-08 07:40:37 +00001032 assert(Ty && "Trying to add a type that doesn't exist?");
Devang Patel0e821f42011-04-12 23:21:44 +00001033
1034 // Check for pre-existence.
1035 DIEEntry *Entry = getDIEEntry(Ty);
1036 // If it exists then use the existing value.
1037 if (Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +00001038 addDIEEntry(Entity, Attribute, Entry);
Devang Patel0e821f42011-04-12 23:21:44 +00001039 return;
1040 }
1041
1042 // Construct type.
1043 DIE *Buffer = getOrCreateTypeDIE(Ty);
1044
1045 // Set up proxy.
1046 Entry = createDIEEntry(Buffer);
1047 insertDIEEntry(Ty, Entry);
Manman Ren4dbdc902013-10-31 17:54:35 +00001048 addDIEEntry(Entity, Attribute, Entry);
Devang Patel1cb8ab42011-05-31 23:30:30 +00001049}
1050
Eric Christopher9cd26af2013-09-20 23:22:52 +00001051// Accelerator table mutators - add each name along with its companion
1052// DIE to the proper table while ensuring that the name that we're going
1053// to reference is in the string table. We do this since the names we
1054// add may not only be identical to the names in the DIE.
Eric Christophera5a79422013-12-09 23:32:48 +00001055void DwarfUnit::addAccelName(StringRef Name, const DIE *Die) {
Eric Christopher9cd26af2013-09-20 23:22:52 +00001056 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001057 std::vector<const DIE *> &DIEs = AccelNames[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001058 DIEs.push_back(Die);
1059}
1060
Eric Christophera5a79422013-12-09 23:32:48 +00001061void DwarfUnit::addAccelObjC(StringRef Name, const DIE *Die) {
Eric Christopher9cd26af2013-09-20 23:22:52 +00001062 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001063 std::vector<const DIE *> &DIEs = AccelObjC[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001064 DIEs.push_back(Die);
1065}
1066
Eric Christophera5a79422013-12-09 23:32:48 +00001067void DwarfUnit::addAccelNamespace(StringRef Name, const DIE *Die) {
Eric Christopher9cd26af2013-09-20 23:22:52 +00001068 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001069 std::vector<const DIE *> &DIEs = AccelNamespace[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001070 DIEs.push_back(Die);
1071}
1072
Eric Christophera5a79422013-12-09 23:32:48 +00001073void DwarfUnit::addAccelType(StringRef Name,
1074 std::pair<const DIE *, unsigned> Die) {
Eric Christopher9cd26af2013-09-20 23:22:52 +00001075 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001076 std::vector<std::pair<const DIE *, unsigned> > &DIEs = AccelTypes[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001077 DIEs.push_back(Die);
1078}
1079
Eric Christopher9c58f312013-09-20 22:20:55 +00001080/// addGlobalName - Add a new global name to the compile unit.
Eric Christophera5a79422013-12-09 23:32:48 +00001081void DwarfUnit::addGlobalName(StringRef Name, DIE *Die, DIScope Context) {
Eric Christopher8dba0d52013-10-19 01:04:42 +00001082 std::string FullName = getParentContextString(Context) + Name.str();
Eric Christopher2c8b7902013-10-17 02:06:06 +00001083 GlobalNames[FullName] = Die;
Eric Christopher9c58f312013-09-20 22:20:55 +00001084}
1085
Eric Christopher2c8b7902013-10-17 02:06:06 +00001086/// getParentContextString - Walks the metadata parent chain in a language
1087/// specific manner (using the compile unit language) and returns
1088/// it as a string. This is done at the metadata level because DIEs may
1089/// not currently have been added to the parent context and walking the
1090/// DIEs looking for names is more expensive than walking the metadata.
Eric Christophera5a79422013-12-09 23:32:48 +00001091std::string DwarfUnit::getParentContextString(DIScope Context) const {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001092 if (!Context)
1093 return "";
1094
1095 // FIXME: Decide whether to implement this for non-C++ languages.
1096 if (getLanguage() != dwarf::DW_LANG_C_plus_plus)
1097 return "";
1098
Eric Christopher8dba0d52013-10-19 01:04:42 +00001099 std::string CS;
Eric Christopher2c8b7902013-10-17 02:06:06 +00001100 SmallVector<DIScope, 1> Parents;
1101 while (!Context.isCompileUnit()) {
1102 Parents.push_back(Context);
1103 if (Context.getContext())
1104 Context = resolve(Context.getContext());
1105 else
1106 // Structure, etc types will have a NULL context if they're at the top
1107 // level.
1108 break;
1109 }
1110
1111 // Reverse iterate over our list to go from the outermost construct to the
1112 // innermost.
1113 for (SmallVectorImpl<DIScope>::reverse_iterator I = Parents.rbegin(),
1114 E = Parents.rend();
1115 I != E; ++I) {
1116 DIScope Ctx = *I;
1117 StringRef Name = Ctx.getName();
Eric Christopher8dba0d52013-10-19 01:04:42 +00001118 if (!Name.empty()) {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001119 CS += Name;
1120 CS += "::";
1121 }
1122 }
1123 return CS;
Devang Patel0e821f42011-04-12 23:21:44 +00001124}
1125
1126/// constructTypeDIE - Construct basic type die from DIBasicType.
Eric Christophera5a79422013-12-09 23:32:48 +00001127void DwarfUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Devang Patel0e821f42011-04-12 23:21:44 +00001128 // Get core information.
1129 StringRef Name = BTy.getName();
Devang Patel0e821f42011-04-12 23:21:44 +00001130 // Add name if not anonymous or intermediate type.
1131 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001132 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel04d6d472011-09-14 23:13:28 +00001133
David Blaikiefac56122013-10-04 23:21:16 +00001134 // An unspecified type only has a name attribute.
1135 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type)
Devang Patel04d6d472011-09-14 23:13:28 +00001136 return;
Devang Patel04d6d472011-09-14 23:13:28 +00001137
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001138 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Pateld925d1a2012-02-07 23:33:58 +00001139 BTy.getEncoding());
Devang Patel04d6d472011-09-14 23:13:28 +00001140
Devang Patel0e821f42011-04-12 23:21:44 +00001141 uint64_t Size = BTy.getSizeInBits() >> 3;
David Blaikief2443192013-10-21 17:28:37 +00001142 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001143}
1144
1145/// constructTypeDIE - Construct derived type die from DIDerivedType.
Eric Christophera5a79422013-12-09 23:32:48 +00001146void DwarfUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Devang Patel0e821f42011-04-12 23:21:44 +00001147 // Get core information.
1148 StringRef Name = DTy.getName();
1149 uint64_t Size = DTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001150 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001151
1152 // Map to main type, void will not have a type.
Manman Ren93b30902013-10-08 18:42:58 +00001153 DIType FromTy = resolve(DTy.getTypeDerivedFrom());
Eric Christopher0df08e22013-08-08 07:40:37 +00001154 if (FromTy)
1155 addType(&Buffer, FromTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001156
1157 // Add name if not anonymous or intermediate type.
1158 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001159 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001160
1161 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher85757902012-02-21 22:25:53 +00001162 if (Size && Tag != dwarf::DW_TAG_pointer_type)
David Blaikief2443192013-10-21 17:28:37 +00001163 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001164
David Blaikie5d3249b2013-01-07 05:51:15 +00001165 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
Eric Christopherc2697f82013-10-19 01:04:47 +00001166 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
1167 getOrCreateTypeDIE(resolve(DTy.getClassType())));
Devang Patel0e821f42011-04-12 23:21:44 +00001168 // Add source line info if available and TyDesc is not a forward declaration.
1169 if (!DTy.isForwardDecl())
1170 addSourceLine(&Buffer, DTy);
1171}
1172
1173/// constructTypeDIE - Construct type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001174void DwarfUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
David Blaikie409dd9c2013-11-19 23:08:21 +00001175 // Add name if not anonymous or intermediate type.
Devang Patel0e821f42011-04-12 23:21:44 +00001176 StringRef Name = CTy.getName();
1177
1178 uint64_t Size = CTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001179 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001180
1181 switch (Tag) {
Devang Patel0e821f42011-04-12 23:21:44 +00001182 case dwarf::DW_TAG_array_type:
Eric Christopherdf9955d2013-11-11 18:52:31 +00001183 constructArrayTypeDIE(Buffer, CTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001184 break;
Eric Christopheraeb105f2013-11-11 18:52:39 +00001185 case dwarf::DW_TAG_enumeration_type:
1186 constructEnumTypeDIE(Buffer, CTy);
1187 break;
Devang Patel0e821f42011-04-12 23:21:44 +00001188 case dwarf::DW_TAG_subroutine_type: {
Eric Christopher0df08e22013-08-08 07:40:37 +00001189 // Add return type. A void return won't have a type.
Devang Patel0e821f42011-04-12 23:21:44 +00001190 DIArray Elements = CTy.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001191 DIType RTy(Elements.getElement(0));
Eric Christopher0df08e22013-08-08 07:40:37 +00001192 if (RTy)
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001193 addType(&Buffer, RTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001194
1195 bool isPrototyped = true;
1196 // Add arguments.
1197 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1198 DIDescriptor Ty = Elements.getElement(i);
1199 if (Ty.isUnspecifiedParameter()) {
Manman Renb987e512013-10-29 00:53:03 +00001200 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001201 isPrototyped = false;
1202 } else {
Manman Renb987e512013-10-29 00:53:03 +00001203 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001204 addType(Arg, DIType(Ty));
David Blaikie9a7a7a92013-01-29 19:35:24 +00001205 if (DIType(Ty).isArtificial())
1206 addFlag(Arg, dwarf::DW_AT_artificial);
Devang Patel0e821f42011-04-12 23:21:44 +00001207 }
1208 }
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001209 // Add prototype flag if we're dealing with a C language and the
1210 // function has been prototyped.
David Blaikiecb8e4352013-11-15 23:50:53 +00001211 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001212 if (isPrototyped &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001213 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopherd42b92f2012-05-22 18:45:24 +00001214 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001215 addFlag(&Buffer, dwarf::DW_AT_prototyped);
Eric Christopherc2697f82013-10-19 01:04:47 +00001216 } break;
Devang Patel0e821f42011-04-12 23:21:44 +00001217 case dwarf::DW_TAG_structure_type:
1218 case dwarf::DW_TAG_union_type:
1219 case dwarf::DW_TAG_class_type: {
Devang Patel0e821f42011-04-12 23:21:44 +00001220 // Add elements to structure type.
David Blaikiea1ae0e62013-08-01 20:30:22 +00001221 DIArray Elements = CTy.getTypeArray();
1222 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel0e821f42011-04-12 23:21:44 +00001223 DIDescriptor Element = Elements.getElement(i);
1224 DIE *ElemDie = NULL;
1225 if (Element.isSubprogram()) {
1226 DISubprogram SP(Element);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001227 ElemDie = getOrCreateSubprogramDIE(SP);
Devang Patel0e821f42011-04-12 23:21:44 +00001228 if (SP.isProtected())
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_protected);
1231 else if (SP.isPrivate())
Nick Lewyckycb918492011-12-13 05:09:11 +00001232 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001233 dwarf::DW_ACCESS_private);
Eric Christopher92331fd2012-11-21 00:34:38 +00001234 else
Nick Lewyckycb918492011-12-13 05:09:11 +00001235 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Eric Christopherc2697f82013-10-19 01:04:47 +00001236 dwarf::DW_ACCESS_public);
Devang Patel0e821f42011-04-12 23:21:44 +00001237 if (SP.isExplicit())
Eric Christopherbb69a272012-08-24 01:14:27 +00001238 addFlag(ElemDie, dwarf::DW_AT_explicit);
Eric Christopher7285c7d2012-03-28 07:34:31 +00001239 } else if (Element.isDerivedType()) {
Eric Christopherd42b92f2012-05-22 18:45:24 +00001240 DIDerivedType DDTy(Element);
1241 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
Manman Renb987e512013-10-29 00:53:03 +00001242 ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
Manman Ren93b30902013-10-08 18:42:58 +00001243 addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()),
Manman Renb3388602013-10-05 01:43:03 +00001244 dwarf::DW_AT_friend);
Manman Renc6b63922013-10-14 20:33:57 +00001245 } else if (DDTy.isStaticMember()) {
Manman Ren57e6ff72013-10-23 22:57:12 +00001246 getOrCreateStaticMemberDIE(DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001247 } else {
Manman Ren230ec862013-10-23 23:00:44 +00001248 constructMemberDIE(Buffer, DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001249 }
Eric Christopher7285c7d2012-03-28 07:34:31 +00001250 } else if (Element.isObjCProperty()) {
Devang Pateld925d1a2012-02-07 23:33:58 +00001251 DIObjCProperty Property(Element);
Manman Renb987e512013-10-29 00:53:03 +00001252 ElemDie = createAndAddDIE(Property.getTag(), Buffer);
Devang Pateld925d1a2012-02-07 23:33:58 +00001253 StringRef PropertyName = Property.getObjCPropertyName();
1254 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Eric Christopherd42b92f2012-05-22 18:45:24 +00001255 addType(ElemDie, Property.getType());
1256 addSourceLine(ElemDie, Property);
Devang Pateld925d1a2012-02-07 23:33:58 +00001257 StringRef GetterName = Property.getObjCPropertyGetterName();
1258 if (!GetterName.empty())
1259 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
1260 StringRef SetterName = Property.getObjCPropertySetterName();
1261 if (!SetterName.empty())
1262 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
1263 unsigned PropertyAttributes = 0;
Devang Patel403e8192012-02-04 01:30:32 +00001264 if (Property.isReadOnlyObjCProperty())
1265 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1266 if (Property.isReadWriteObjCProperty())
1267 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1268 if (Property.isAssignObjCProperty())
1269 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1270 if (Property.isRetainObjCProperty())
1271 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1272 if (Property.isCopyObjCProperty())
1273 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1274 if (Property.isNonAtomicObjCProperty())
1275 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1276 if (PropertyAttributes)
David Blaikief2443192013-10-21 17:28:37 +00001277 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None,
Eric Christopherc2697f82013-10-19 01:04:47 +00001278 PropertyAttributes);
Devang Patel44882172012-02-06 17:49:43 +00001279
Devang Pateld925d1a2012-02-07 23:33:58 +00001280 DIEEntry *Entry = getDIEEntry(Element);
1281 if (!Entry) {
1282 Entry = createDIEEntry(ElemDie);
1283 insertDIEEntry(Element, Entry);
1284 }
Devang Patel403e8192012-02-04 01:30:32 +00001285 } else
Devang Patel0e821f42011-04-12 23:21:44 +00001286 continue;
Devang Patel0e821f42011-04-12 23:21:44 +00001287 }
1288
1289 if (CTy.isAppleBlockExtension())
Eric Christopherbb69a272012-08-24 01:14:27 +00001290 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel0e821f42011-04-12 23:21:44 +00001291
Eric Christopher9e429ae2013-10-05 00:27:02 +00001292 DICompositeType ContainingType(resolve(CTy.getContainingType()));
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001293 if (ContainingType)
Manman Ren4c4b69c2013-10-11 23:58:05 +00001294 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001295 getOrCreateTypeDIE(ContainingType));
Devang Patel0e821f42011-04-12 23:21:44 +00001296
Devang Patel12419ae2011-05-12 21:29:42 +00001297 if (CTy.isObjcClassComplete())
Eric Christopherbb69a272012-08-24 01:14:27 +00001298 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patel2409e782011-05-12 19:06:16 +00001299
Eric Christopherda011dd2011-12-16 23:42:42 +00001300 // Add template parameters to a class, structure or union types.
1301 // FIXME: The support isn't in the metadata for this yet.
1302 if (Tag == dwarf::DW_TAG_class_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001303 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
Devang Patel0e821f42011-04-12 23:21:44 +00001304 addTemplateParams(Buffer, CTy.getTemplateParams());
1305
1306 break;
1307 }
1308 default:
1309 break;
1310 }
1311
1312 // Add name if not anonymous or intermediate type.
1313 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001314 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001315
Eric Christopher775cbd22012-05-22 18:45:18 +00001316 if (Tag == dwarf::DW_TAG_enumeration_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001317 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
Eric Christopher775cbd22012-05-22 18:45:18 +00001318 Tag == dwarf::DW_TAG_union_type) {
Devang Patel0e821f42011-04-12 23:21:44 +00001319 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher1cf33382012-06-01 00:22:32 +00001320 // TODO: Do we care about size for enum forward declarations?
Devang Patel0e821f42011-04-12 23:21:44 +00001321 if (Size)
David Blaikief2443192013-10-21 17:28:37 +00001322 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Eric Christopher1cf33382012-06-01 00:22:32 +00001323 else if (!CTy.isForwardDecl())
Devang Patel0e821f42011-04-12 23:21:44 +00001324 // Add zero size if it is not a forward declaration.
David Blaikief2443192013-10-21 17:28:37 +00001325 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, 0);
Eric Christopher1cf33382012-06-01 00:22:32 +00001326
1327 // If we're a forward decl, say so.
1328 if (CTy.isForwardDecl())
Eric Christopherbb69a272012-08-24 01:14:27 +00001329 addFlag(&Buffer, dwarf::DW_AT_declaration);
Devang Patel0e821f42011-04-12 23:21:44 +00001330
1331 // Add source line info if available.
1332 if (!CTy.isForwardDecl())
1333 addSourceLine(&Buffer, CTy);
Eric Christopher54cf8ff2012-03-07 00:15:19 +00001334
1335 // No harm in adding the runtime language to the declaration.
1336 unsigned RLang = CTy.getRunTimeLang();
1337 if (RLang)
Eric Christopherc2697f82013-10-19 01:04:47 +00001338 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
1339 RLang);
Devang Patel0e821f42011-04-12 23:21:44 +00001340 }
1341}
1342
Manman Renffc9a712013-10-23 23:05:28 +00001343/// constructTemplateTypeParameterDIE - Construct new DIE for the given
1344/// DITemplateTypeParameter.
Eric Christophera5a79422013-12-09 23:32:48 +00001345void DwarfUnit::constructTemplateTypeParameterDIE(DIE &Buffer,
1346 DITemplateTypeParameter TP) {
Manman Renb987e512013-10-29 00:53:03 +00001347 DIE *ParamDIE =
1348 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
Eric Christopher056b6472013-08-08 08:09:43 +00001349 // Add the type if it exists, it could be void and therefore no type.
1350 if (TP.getType())
Manman Ren88b0f942013-10-09 19:46:28 +00001351 addType(ParamDIE, resolve(TP.getType()));
David Blaikie2b380232013-06-22 18:59:11 +00001352 if (!TP.getName().empty())
1353 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel0e821f42011-04-12 23:21:44 +00001354}
1355
Manman Renffc9a712013-10-23 23:05:28 +00001356/// constructTemplateValueParameterDIE - Construct new DIE for the given
1357/// DITemplateValueParameter.
Eric Christophera5a79422013-12-09 23:32:48 +00001358void
1359DwarfUnit::constructTemplateValueParameterDIE(DIE &Buffer,
David Blaikie319a05f2013-12-02 19:33:10 +00001360 DITemplateValueParameter VP) {
Manman Renb987e512013-10-29 00:53:03 +00001361 DIE *ParamDIE = createAndAddDIE(VP.getTag(), Buffer);
Eric Christopher0df08e22013-08-08 07:40:37 +00001362
1363 // Add the type if there is one, template template and template parameter
1364 // packs will not have a type.
Eric Christopher691281b2013-10-21 17:48:51 +00001365 if (VP.getTag() == dwarf::DW_TAG_template_value_parameter)
Manman Ren88b0f942013-10-09 19:46:28 +00001366 addType(ParamDIE, resolve(VP.getType()));
Eric Christopherafb2c412013-08-08 07:40:31 +00001367 if (!VP.getName().empty())
1368 addString(ParamDIE, dwarf::DW_AT_name, VP.getName());
1369 if (Value *Val = VP.getValue()) {
David Blaikiea1e813d2013-05-10 21:52:07 +00001370 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val))
Manman Ren88b0f942013-10-09 19:46:28 +00001371 addConstantValue(ParamDIE, CI,
1372 isUnsignedDIType(DD, resolve(VP.getType())));
David Blaikiea1e813d2013-05-10 21:52:07 +00001373 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) {
1374 // For declaration non-type template parameters (such as global values and
1375 // functions)
1376 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Rafael Espindola79858aa2013-10-29 17:07:16 +00001377 addOpAddress(Block, Asm->getSymbol(GV));
David Blaikiea1e813d2013-05-10 21:52:07 +00001378 // Emit DW_OP_stack_value to use the address as the immediate value of the
1379 // parameter, rather than a pointer to it.
David Blaikief2443192013-10-21 17:28:37 +00001380 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1381 addBlock(ParamDIE, dwarf::DW_AT_location, Block);
Eric Christopherafb2c412013-08-08 07:40:31 +00001382 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) {
David Blaikie2b380232013-06-22 18:59:11 +00001383 assert(isa<MDString>(Val));
1384 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1385 cast<MDString>(Val)->getString());
Eric Christopherafb2c412013-08-08 07:40:31 +00001386 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
David Blaikie2b380232013-06-22 18:59:11 +00001387 assert(isa<MDNode>(Val));
1388 DIArray A(cast<MDNode>(Val));
1389 addTemplateParams(*ParamDIE, A);
David Blaikiea1e813d2013-05-10 21:52:07 +00001390 }
1391 }
Devang Patel0e821f42011-04-12 23:21:44 +00001392}
1393
Devang Patel17b53272011-05-06 16:57:54 +00001394/// getOrCreateNameSpace - Create a DIE for DINameSpace.
Eric Christophera5a79422013-12-09 23:32:48 +00001395DIE *DwarfUnit::getOrCreateNameSpace(DINameSpace NS) {
Manman Renf6b936b2013-10-29 05:49:41 +00001396 // Construct the context before querying for the existence of the DIE in case
1397 // such construction creates the DIE.
1398 DIE *ContextDIE = getOrCreateContextDIE(NS.getContext());
Manman Renf6b936b2013-10-29 05:49:41 +00001399
Devang Patel17b53272011-05-06 16:57:54 +00001400 DIE *NDie = getDIE(NS);
1401 if (NDie)
1402 return NDie;
Manman Renf6b936b2013-10-29 05:49:41 +00001403 NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
1404
Eric Christopher4996c702011-11-07 09:24:32 +00001405 if (!NS.getName().empty()) {
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001406 addString(NDie, dwarf::DW_AT_name, NS.getName());
Eric Christopher4996c702011-11-07 09:24:32 +00001407 addAccelNamespace(NS.getName(), NDie);
Eric Christopher2c8b7902013-10-17 02:06:06 +00001408 addGlobalName(NS.getName(), NDie, NS.getContext());
Eric Christopher4996c702011-11-07 09:24:32 +00001409 } else
1410 addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel17b53272011-05-06 16:57:54 +00001411 addSourceLine(NDie, NS);
Devang Patel17b53272011-05-06 16:57:54 +00001412 return NDie;
1413}
1414
Devang Patel89543712011-08-15 17:24:54 +00001415/// getOrCreateSubprogramDIE - Create new DIE using SP.
Eric Christophera5a79422013-12-09 23:32:48 +00001416DIE *DwarfUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
David Blaikie309ffe42013-10-04 01:39:59 +00001417 // Construct the context before querying for the existence of the DIE in case
1418 // such construction creates the DIE (as is the case for member function
1419 // declarations).
Manman Renc50fa112013-10-10 18:40:01 +00001420 DIE *ContextDIE = getOrCreateContextDIE(resolve(SP.getContext()));
David Blaikie309ffe42013-10-04 01:39:59 +00001421
Eric Christophere595bae2013-10-04 17:08:38 +00001422 DIE *SPDie = getDIE(SP);
Devang Patel89543712011-08-15 17:24:54 +00001423 if (SPDie)
1424 return SPDie;
1425
Manman Ren73d697c2013-10-29 00:58:04 +00001426 DISubprogram SPDecl = SP.getFunctionDeclaration();
1427 if (SPDecl.isSubprogram())
1428 // Add subprogram definitions to the CU die directly.
David Blaikie2a80e442013-12-02 22:09:48 +00001429 ContextDIE = UnitDie.get();
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001430
1431 // DW_TAG_inlined_subroutine may refer to this DIE.
Manman Ren73d697c2013-10-29 00:58:04 +00001432 SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001433
Rafael Espindola79278362011-11-10 22:34:29 +00001434 DIE *DeclDie = NULL;
Manman Renb9512a72013-10-23 22:12:26 +00001435 if (SPDecl.isSubprogram())
Rafael Espindola79278362011-11-10 22:34:29 +00001436 DeclDie = getOrCreateSubprogramDIE(SPDecl);
Rafael Espindola79278362011-11-10 22:34:29 +00001437
Devang Patel89543712011-08-15 17:24:54 +00001438 // Add function template parameters.
1439 addTemplateParams(*SPDie, SP.getTemplateParams());
1440
Devang Patel89543712011-08-15 17:24:54 +00001441 // If this DIE is going to refer declaration info using AT_specification
Eric Christopherf20ff972013-05-09 00:42:33 +00001442 // then there is no need to add other attributes.
Rafael Espindola79278362011-11-10 22:34:29 +00001443 if (DeclDie) {
1444 // Refer function declaration directly.
Manman Ren4c4b69c2013-10-11 23:58:05 +00001445 addDIEEntry(SPDie, dwarf::DW_AT_specification, DeclDie);
Rafael Espindola79278362011-11-10 22:34:29 +00001446
Devang Patel89543712011-08-15 17:24:54 +00001447 return SPDie;
Rafael Espindola79278362011-11-10 22:34:29 +00001448 }
Devang Patel89543712011-08-15 17:24:54 +00001449
Eric Christopheracb71152012-08-23 22:52:55 +00001450 // Add the linkage name if we have one.
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001451 StringRef LinkageName = SP.getLinkageName();
1452 if (!LinkageName.empty())
Eric Christopheracb71152012-08-23 22:52:55 +00001453 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001454 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopheracb71152012-08-23 22:52:55 +00001455
Devang Patel89543712011-08-15 17:24:54 +00001456 // Constructors and operators for anonymous aggregates do not have names.
1457 if (!SP.getName().empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001458 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Patel89543712011-08-15 17:24:54 +00001459
1460 addSourceLine(SPDie, SP);
1461
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001462 // Add the prototype if we have a prototype and we have a C like
1463 // language.
David Blaikiecb8e4352013-11-15 23:50:53 +00001464 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001465 if (SP.isPrototyped() &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001466 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001467 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001468 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Patel89543712011-08-15 17:24:54 +00001469
Devang Patel89543712011-08-15 17:24:54 +00001470 DICompositeType SPTy = SP.getType();
David Blaikie5174c842013-05-22 23:22:18 +00001471 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type &&
1472 "the type of a subprogram should be a subroutine");
Devang Patel89543712011-08-15 17:24:54 +00001473
David Blaikie5174c842013-05-22 23:22:18 +00001474 DIArray Args = SPTy.getTypeArray();
Eric Christopher691281b2013-10-21 17:48:51 +00001475 // Add a return type. If this is a type like a C/C++ void type we don't add a
1476 // return type.
Eric Christopher0df08e22013-08-08 07:40:37 +00001477 if (Args.getElement(0))
1478 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel89543712011-08-15 17:24:54 +00001479
1480 unsigned VK = SP.getVirtuality();
1481 if (VK) {
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001482 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Devang Patel89543712011-08-15 17:24:54 +00001483 DIEBlock *Block = getDIEBlock();
David Blaikief2443192013-10-21 17:28:37 +00001484 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1485 addUInt(Block, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1486 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
Eric Christopher98b7f172013-11-11 18:52:36 +00001487 ContainingTypeMap.insert(
1488 std::make_pair(SPDie, resolve(SP.getContainingType())));
Devang Patel89543712011-08-15 17:24:54 +00001489 }
1490
1491 if (!SP.isDefinition()) {
Eric Christopherbb69a272012-08-24 01:14:27 +00001492 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher92331fd2012-11-21 00:34:38 +00001493
Devang Patel89543712011-08-15 17:24:54 +00001494 // Add arguments. Do not add arguments for subprogram definition. They will
1495 // be handled while processing variables.
Eric Christopherc2697f82013-10-19 01:04:47 +00001496 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
Manman Renb987e512013-10-29 00:53:03 +00001497 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, *SPDie);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001498 DIType ATy(Args.getElement(i));
David Blaikie5174c842013-05-22 23:22:18 +00001499 addType(Arg, ATy);
1500 if (ATy.isArtificial())
1501 addFlag(Arg, dwarf::DW_AT_artificial);
David Blaikie5174c842013-05-22 23:22:18 +00001502 }
Devang Patel89543712011-08-15 17:24:54 +00001503 }
1504
1505 if (SP.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001506 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Patel89543712011-08-15 17:24:54 +00001507
1508 if (!SP.isLocalToUnit())
Eric Christopherbb69a272012-08-24 01:14:27 +00001509 addFlag(SPDie, dwarf::DW_AT_external);
Devang Patel89543712011-08-15 17:24:54 +00001510
1511 if (SP.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +00001512 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Patel89543712011-08-15 17:24:54 +00001513
1514 if (unsigned isa = Asm->getISAEncoding()) {
1515 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1516 }
1517
1518 return SPDie;
1519}
1520
Devang Pateldfd6ec32011-08-15 17:57:41 +00001521// Return const expression if value is a GEP to access merged global
1522// constant. e.g.
1523// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1524static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1525 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1526 if (!CE || CE->getNumOperands() != 3 ||
1527 CE->getOpcode() != Instruction::GetElementPtr)
1528 return NULL;
1529
1530 // First operand points to a global struct.
1531 Value *Ptr = CE->getOperand(0);
1532 if (!isa<GlobalValue>(Ptr) ||
1533 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1534 return NULL;
1535
1536 // Second operand is zero.
1537 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1538 if (!CI || !CI->isZero())
1539 return NULL;
1540
1541 // Third operand is offset.
1542 if (!isa<ConstantInt>(CE->getOperand(2)))
1543 return NULL;
1544
1545 return CE;
1546}
1547
1548/// createGlobalVariableDIE - create global variable DIE.
Eric Christopher4287a492013-12-09 23:57:44 +00001549void DwarfCompileUnit::createGlobalVariableDIE(DIGlobalVariable GV) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001550 // Check for pre-existence.
David Blaikie2ad00162013-11-15 23:09:13 +00001551 if (getDIE(GV))
Devang Pateldfd6ec32011-08-15 17:57:41 +00001552 return;
1553
Manman Ren7504ed42013-07-08 18:33:29 +00001554 if (!GV.isGlobalVariable())
Devang Patel0ecbcbd2011-08-18 23:17:55 +00001555 return;
1556
Eric Christopher08f7c8f2013-10-04 23:49:26 +00001557 DIScope GVContext = GV.getContext();
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001558 DIType GTy = GV.getType();
1559
1560 // If this is a static data member definition, some attributes belong
1561 // to the declaration DIE.
1562 DIE *VariableDIE = NULL;
Manman Rene697d3c2013-02-01 23:54:37 +00001563 bool IsStaticMember = false;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001564 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1565 if (SDMDecl.Verify()) {
1566 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1567 // We need the declaration DIE that is in the static member's class.
Manman Renc6b63922013-10-14 20:33:57 +00001568 VariableDIE = getOrCreateStaticMemberDIE(SDMDecl);
Manman Rene697d3c2013-02-01 23:54:37 +00001569 IsStaticMember = true;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001570 }
1571
1572 // If this is not a static data member definition, create the variable
1573 // DIE and add the initial set of attributes to it.
1574 if (!VariableDIE) {
Manman Renf6b936b2013-10-29 05:49:41 +00001575 // Construct the context before querying for the existence of the DIE in
1576 // case such construction creates the DIE.
1577 DIE *ContextDIE = getOrCreateContextDIE(GVContext);
Manman Renf6b936b2013-10-29 05:49:41 +00001578
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001579 // Add to map.
David Blaikie52c50202013-11-16 00:29:01 +00001580 VariableDIE = createAndAddDIE(GV.getTag(), *ContextDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001581
1582 // Add name and type.
1583 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1584 addType(VariableDIE, GTy);
1585
1586 // Add scoping info.
Eric Christopherd2b497b2013-10-16 01:37:49 +00001587 if (!GV.isLocalToUnit())
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001588 addFlag(VariableDIE, dwarf::DW_AT_external);
1589
1590 // Add line number info.
1591 addSourceLine(VariableDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001592 }
1593
Devang Pateldfd6ec32011-08-15 17:57:41 +00001594 // Add location.
Eric Christopher4996c702011-11-07 09:24:32 +00001595 bool addToAccelTable = false;
Eric Christopher0a917b72011-11-11 03:16:32 +00001596 DIE *VariableSpecDIE = NULL;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001597 bool isGlobalVariable = GV.getGlobal() != NULL;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001598 if (isGlobalVariable) {
Eric Christopher4996c702011-11-07 09:24:32 +00001599 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001600 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Rafael Espindola79858aa2013-10-29 17:07:16 +00001601 const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal());
David Blaikief2694972013-06-28 20:05:11 +00001602 if (GV.getGlobal()->isThreadLocal()) {
1603 // FIXME: Make this work with -gsplit-dwarf.
1604 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
1605 assert((PointerSize == 4 || PointerSize == 8) &&
1606 "Add support for other sizes if necessary");
Ulrich Weigand2b6fc8d2013-07-02 18:47:09 +00001607 const MCExpr *Expr =
David Blaikie8466ca82013-07-01 23:55:52 +00001608 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym);
David Blaikief2694972013-06-28 20:05:11 +00001609 // Based on GCC's support for TLS:
David Blaikie8466ca82013-07-01 23:55:52 +00001610 if (!DD->useSplitDwarf()) {
1611 // 1) Start with a constNu of the appropriate pointer size
David Blaikief2443192013-10-21 17:28:37 +00001612 addUInt(Block, dwarf::DW_FORM_data1,
David Blaikie8466ca82013-07-01 23:55:52 +00001613 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
Richard Mitton0aafb582013-10-07 18:39:18 +00001614 // 2) containing the (relocated) offset of the TLS variable
1615 // within the module's TLS block.
David Blaikief2443192013-10-21 17:28:37 +00001616 addExpr(Block, dwarf::DW_FORM_udata, Expr);
David Blaikie8466ca82013-07-01 23:55:52 +00001617 } else {
David Blaikief2443192013-10-21 17:28:37 +00001618 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
1619 addUInt(Block, dwarf::DW_FORM_udata, DU->getAddrPoolIndex(Expr));
David Blaikie8466ca82013-07-01 23:55:52 +00001620 }
Richard Mitton0aafb582013-10-07 18:39:18 +00001621 // 3) followed by a custom OP to make the debugger do a TLS lookup.
David Blaikief2443192013-10-21 17:28:37 +00001622 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001623 } else {
1624 DD->addArangeLabel(SymbolCU(this, Sym));
David Blaikief2694972013-06-28 20:05:11 +00001625 addOpAddress(Block, Sym);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001626 }
Devang Pateldfd6ec32011-08-15 17:57:41 +00001627 // Do not create specification DIE if context is either compile unit
1628 // or a subprogram.
Devang Patel5e6b65c2011-09-21 23:41:11 +00001629 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Manman Ren3eb9dff2013-09-09 19:05:21 +00001630 !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001631 // Create specification DIE.
David Blaikie2a80e442013-12-02 22:09:48 +00001632 VariableSpecDIE = createAndAddDIE(dwarf::DW_TAG_variable, *UnitDie);
Manman Ren4c4b69c2013-10-11 23:58:05 +00001633 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification, VariableDIE);
David Blaikief2443192013-10-21 17:28:37 +00001634 addBlock(VariableSpecDIE, dwarf::DW_AT_location, Block);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001635 // A static member's declaration is already flagged as such.
1636 if (!SDMDecl.Verify())
1637 addFlag(VariableDIE, dwarf::DW_AT_declaration);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001638 } else {
David Blaikief2443192013-10-21 17:28:37 +00001639 addBlock(VariableDIE, dwarf::DW_AT_location, Block);
Eric Christopher4996c702011-11-07 09:24:32 +00001640 }
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001641 // Add the linkage name.
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001642 StringRef LinkageName = GV.getLinkageName();
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001643 if (!LinkageName.empty())
Eric Christopher3f79b8c2013-02-27 23:49:47 +00001644 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1645 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1646 // TAG_variable.
Eric Christopherc2697f82013-10-19 01:04:47 +00001647 addString(IsStaticMember && VariableSpecDIE ? VariableSpecDIE
1648 : VariableDIE,
1649 dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001650 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopher92331fd2012-11-21 00:34:38 +00001651 } else if (const ConstantInt *CI =
Eric Christopherc2697f82013-10-19 01:04:47 +00001652 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
Adrian Prantl322f41d2013-04-04 22:56:49 +00001653 // AT_const_value was added when the static member was created. To avoid
Manman Rene697d3c2013-02-01 23:54:37 +00001654 // emitting AT_const_value multiple times, we only add AT_const_value when
1655 // it is not a static member.
1656 if (!IsStaticMember)
Manman Renb3388602013-10-05 01:43:03 +00001657 addConstantValue(VariableDIE, CI, isUnsignedDIType(DD, GTy));
David Blaikiea781b25b2013-11-17 21:55:13 +00001658 } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getOperand(11))) {
Eric Christopher4996c702011-11-07 09:24:32 +00001659 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001660 // GV is a merged global.
1661 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1662 Value *Ptr = CE->getOperand(0);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001663 MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr));
1664 DD->addArangeLabel(SymbolCU(this, Sym));
1665 addOpAddress(Block, Sym);
David Blaikief2443192013-10-21 17:28:37 +00001666 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
Eric Christopherc2697f82013-10-19 01:04:47 +00001667 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
David Blaikief2443192013-10-21 17:28:37 +00001668 addUInt(Block, dwarf::DW_FORM_udata,
Eric Christopherc2697f82013-10-19 01:04:47 +00001669 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
David Blaikief2443192013-10-21 17:28:37 +00001670 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1671 addBlock(VariableDIE, dwarf::DW_AT_location, Block);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001672 }
1673
Eric Christopherc12c2112011-11-11 01:55:22 +00001674 if (addToAccelTable) {
1675 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1676 addAccelName(GV.getName(), AddrDIE);
Eric Christopher4996c702011-11-07 09:24:32 +00001677
Eric Christopherc12c2112011-11-11 01:55:22 +00001678 // If the linkage name is different than the name, go ahead and output
1679 // that as well into the name table.
1680 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1681 addAccelName(GV.getLinkageName(), AddrDIE);
1682 }
Eric Christopherd2b497b2013-10-16 01:37:49 +00001683
1684 if (!GV.isLocalToUnit())
Eric Christopher2c8b7902013-10-17 02:06:06 +00001685 addGlobalName(GV.getName(), VariableSpecDIE ? VariableSpecDIE : VariableDIE,
1686 GV.getContext());
Devang Pateldfd6ec32011-08-15 17:57:41 +00001687}
1688
Devang Patel0e821f42011-04-12 23:21:44 +00001689/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Eric Christophera5a79422013-12-09 23:32:48 +00001690void DwarfUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Manman Renb987e512013-10-29 00:53:03 +00001691 DIE *DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
Manman Ren4c4b69c2013-10-11 23:58:05 +00001692 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, IndexTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001693
Bill Wendling28fe9e72012-12-06 07:38:10 +00001694 // The LowerBound value defines the lower bounds which is typically zero for
1695 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1696 // Count == -1 then the array is unbounded and we do not emit
1697 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1698 // Count == 0, then the array has zero elements in which case we do not emit
1699 // an upper bound.
1700 int64_t LowerBound = SR.getLo();
Bill Wendling3495f9b2012-12-06 07:55:19 +00001701 int64_t DefaultLowerBound = getDefaultLowerBound();
Bill Wendlingd7767122012-12-04 21:34:03 +00001702 int64_t Count = SR.getCount();
Devang Patel0e821f42011-04-12 23:21:44 +00001703
Bill Wendling3495f9b2012-12-06 07:55:19 +00001704 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
David Blaikief2443192013-10-21 17:28:37 +00001705 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound);
Bill Wendling28fe9e72012-12-06 07:38:10 +00001706
1707 if (Count != -1 && Count != 0)
Bill Wendlingd7767122012-12-04 21:34:03 +00001708 // FIXME: An unbounded array should reference the expression that defines
1709 // the array.
Eric Christopher98b7f172013-11-11 18:52:36 +00001710 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None,
1711 LowerBound + Count - 1);
Devang Patel0e821f42011-04-12 23:21:44 +00001712}
1713
1714/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001715void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopherdf9955d2013-11-11 18:52:31 +00001716 if (CTy.isVector())
Eric Christopherbb69a272012-08-24 01:14:27 +00001717 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel0e821f42011-04-12 23:21:44 +00001718
Eric Christopher0df08e22013-08-08 07:40:37 +00001719 // Emit the element type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001720 addType(&Buffer, resolve(CTy.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001721
1722 // Get an anonymous type for index type.
Eric Christophercad9b532013-01-04 21:51:53 +00001723 // FIXME: This type should be passed down from the front end
1724 // as different languages may have different sizes for indexes.
Devang Patel0e821f42011-04-12 23:21:44 +00001725 DIE *IdxTy = getIndexTyDie();
1726 if (!IdxTy) {
1727 // Construct an anonymous type for index type.
David Blaikie2a80e442013-12-02 22:09:48 +00001728 IdxTy = createAndAddDIE(dwarf::DW_TAG_base_type, *UnitDie);
Eric Christophercad9b532013-01-04 21:51:53 +00001729 addString(IdxTy, dwarf::DW_AT_name, "int");
David Blaikief2443192013-10-21 17:28:37 +00001730 addUInt(IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int32_t));
Devang Patel0e821f42011-04-12 23:21:44 +00001731 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1732 dwarf::DW_ATE_signed);
Devang Patel0e821f42011-04-12 23:21:44 +00001733 setIndexTyDie(IdxTy);
1734 }
1735
1736 // Add subranges to array type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001737 DIArray Elements = CTy.getTypeArray();
Devang Patel0e821f42011-04-12 23:21:44 +00001738 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1739 DIDescriptor Element = Elements.getElement(i);
1740 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1741 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1742 }
1743}
1744
Eric Christopheraeb105f2013-11-11 18:52:39 +00001745/// constructEnumTypeDIE - Construct an enum type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001746void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopheraeb105f2013-11-11 18:52:39 +00001747 DIArray Elements = CTy.getTypeArray();
1748
1749 // Add enumerators to enumeration type.
1750 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001751 DIEnumerator Enum(Elements.getElement(i));
Eric Christopheraeb105f2013-11-11 18:52:39 +00001752 if (Enum.isEnumerator()) {
1753 DIE *Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001754 StringRef Name = Enum.getName();
Eric Christopheraeb105f2013-11-11 18:52:39 +00001755 addString(Enumerator, dwarf::DW_AT_name, Name);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001756 int64_t Value = Enum.getEnumValue();
Eric Christophera07e4f52013-11-19 09:28:34 +00001757 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
1758 Value);
Eric Christopheraeb105f2013-11-11 18:52:39 +00001759 }
1760 }
1761 DIType DTy = resolve(CTy.getTypeDerivedFrom());
1762 if (DTy) {
1763 addType(&Buffer, DTy);
1764 addFlag(&Buffer, dwarf::DW_AT_enum_class);
1765 }
Devang Patel0e821f42011-04-12 23:21:44 +00001766}
1767
Devang Patel89543712011-08-15 17:24:54 +00001768/// constructContainingTypeDIEs - Construct DIEs for types that contain
1769/// vtables.
Eric Christophera5a79422013-12-09 23:32:48 +00001770void DwarfUnit::constructContainingTypeDIEs() {
Devang Patel89543712011-08-15 17:24:54 +00001771 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Eric Christopherc2697f82013-10-19 01:04:47 +00001772 CE = ContainingTypeMap.end();
1773 CI != CE; ++CI) {
Devang Patel89543712011-08-15 17:24:54 +00001774 DIE *SPDie = CI->first;
David Blaikie2ad00162013-11-15 23:09:13 +00001775 DIDescriptor D(CI->second);
1776 if (!D)
Eric Christopherc2697f82013-10-19 01:04:47 +00001777 continue;
David Blaikie2ad00162013-11-15 23:09:13 +00001778 DIE *NDie = getDIE(D);
Eric Christopherc2697f82013-10-19 01:04:47 +00001779 if (!NDie)
1780 continue;
Manman Ren4c4b69c2013-10-11 23:58:05 +00001781 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, NDie);
Devang Patel89543712011-08-15 17:24:54 +00001782 }
1783}
1784
Devang Patel3acc70e2011-08-15 22:04:40 +00001785/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Eric Christophera5a79422013-12-09 23:32:48 +00001786DIE *DwarfUnit::constructVariableDIE(DbgVariable &DV, bool isScopeAbstract) {
Eric Christopher34a2c872013-11-15 01:43:19 +00001787 StringRef Name = DV.getName();
Devang Patel3acc70e2011-08-15 22:04:40 +00001788
Devang Patel3acc70e2011-08-15 22:04:40 +00001789 // Define variable debug information entry.
Eric Christopher34a2c872013-11-15 01:43:19 +00001790 DIE *VariableDie = new DIE(DV.getTag());
1791 DbgVariable *AbsVar = DV.getAbstractVariable();
Devang Patel3acc70e2011-08-15 22:04:40 +00001792 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
Manman Ren4213c392013-05-29 17:16:59 +00001793 if (AbsDIE)
Manman Ren4c4b69c2013-10-11 23:58:05 +00001794 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin, AbsDIE);
Devang Patel3acc70e2011-08-15 22:04:40 +00001795 else {
David Blaikie715528b2013-08-19 03:34:03 +00001796 if (!Name.empty())
1797 addString(VariableDie, dwarf::DW_AT_name, Name);
Eric Christopher34a2c872013-11-15 01:43:19 +00001798 addSourceLine(VariableDie, DV.getVariable());
1799 addType(VariableDie, DV.getType());
Devang Patel3acc70e2011-08-15 22:04:40 +00001800 }
1801
Eric Christopher34a2c872013-11-15 01:43:19 +00001802 if (DV.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001803 addFlag(VariableDie, dwarf::DW_AT_artificial);
Devang Patel3acc70e2011-08-15 22:04:40 +00001804
1805 if (isScopeAbstract) {
Eric Christopher34a2c872013-11-15 01:43:19 +00001806 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001807 return VariableDie;
1808 }
1809
1810 // Add variable address.
1811
Eric Christopher34a2c872013-11-15 01:43:19 +00001812 unsigned Offset = DV.getDotDebugLocOffset();
Devang Patel3acc70e2011-08-15 22:04:40 +00001813 if (Offset != ~0U) {
Eric Christopher33ff6972013-11-21 23:46:41 +00001814 addSectionLabel(VariableDie, dwarf::DW_AT_location,
1815 Asm->GetTempSymbol("debug_loc", Offset));
Eric Christopher34a2c872013-11-15 01:43:19 +00001816 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001817 return VariableDie;
1818 }
1819
Eric Christophercead0332011-10-03 15:49:20 +00001820 // Check if variable is described by a DBG_VALUE instruction.
Eric Christopher34a2c872013-11-15 01:43:19 +00001821 if (const MachineInstr *DVInsn = DV.getMInsn()) {
David Blaikie0252265b2013-06-16 20:34:15 +00001822 assert(DVInsn->getNumOperands() == 3);
1823 if (DVInsn->getOperand(0).isReg()) {
1824 const MachineOperand RegOp = DVInsn->getOperand(0);
Adrian Prantl19942882013-07-09 21:44:06 +00001825 // If the second operand is an immediate, this is an indirect value.
Adrian Prantl418d1d12013-07-09 20:28:37 +00001826 if (DVInsn->getOperand(1).isImm()) {
Eric Christopherc2697f82013-10-19 01:04:47 +00001827 MachineLocation Location(RegOp.getReg(),
1828 DVInsn->getOperand(1).getImm());
Eric Christopher34a2c872013-11-15 01:43:19 +00001829 addVariableAddress(DV, VariableDie, Location);
David Blaikie0252265b2013-06-16 20:34:15 +00001830 } else if (RegOp.getReg())
Eric Christopher34a2c872013-11-15 01:43:19 +00001831 addVariableAddress(DV, VariableDie, MachineLocation(RegOp.getReg()));
David Blaikie0252265b2013-06-16 20:34:15 +00001832 } else if (DVInsn->getOperand(0).isImm())
Eric Christopher34a2c872013-11-15 01:43:19 +00001833 addConstantValue(VariableDie, DVInsn->getOperand(0), DV.getType());
David Blaikie0252265b2013-06-16 20:34:15 +00001834 else if (DVInsn->getOperand(0).isFPImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001835 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
David Blaikie0252265b2013-06-16 20:34:15 +00001836 else if (DVInsn->getOperand(0).isCImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001837 addConstantValue(VariableDie, DVInsn->getOperand(0).getCImm(),
Eric Christopher34a2c872013-11-15 01:43:19 +00001838 isUnsignedDIType(DD, DV.getType()));
Eric Christopher78fcf4902013-07-03 01:08:30 +00001839
Eric Christopher34a2c872013-11-15 01:43:19 +00001840 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001841 return VariableDie;
1842 } else {
1843 // .. else use frame index.
Eric Christopher34a2c872013-11-15 01:43:19 +00001844 int FI = DV.getFrameIndex();
Devang Patel3acc70e2011-08-15 22:04:40 +00001845 if (FI != ~0) {
1846 unsigned FrameReg = 0;
1847 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopherc2697f82013-10-19 01:04:47 +00001848 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
Devang Patel3acc70e2011-08-15 22:04:40 +00001849 MachineLocation Location(FrameReg, Offset);
Eric Christopher34a2c872013-11-15 01:43:19 +00001850 addVariableAddress(DV, VariableDie, Location);
Devang Patel3acc70e2011-08-15 22:04:40 +00001851 }
1852 }
1853
Eric Christopher34a2c872013-11-15 01:43:19 +00001854 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001855 return VariableDie;
1856}
1857
Manman Ren230ec862013-10-23 23:00:44 +00001858/// constructMemberDIE - Construct member DIE from DIDerivedType.
Eric Christophera5a79422013-12-09 23:32:48 +00001859void DwarfUnit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) {
Manman Renb987e512013-10-29 00:53:03 +00001860 DIE *MemberDie = createAndAddDIE(DT.getTag(), Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001861 StringRef Name = DT.getName();
1862 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001863 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001864
Manman Ren93b30902013-10-08 18:42:58 +00001865 addType(MemberDie, resolve(DT.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001866
1867 addSourceLine(MemberDie, DT);
1868
1869 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
David Blaikief2443192013-10-21 17:28:37 +00001870 addUInt(MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel0e821f42011-04-12 23:21:44 +00001871
Eric Christopherc2697f82013-10-19 01:04:47 +00001872 if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) {
Devang Patel0e821f42011-04-12 23:21:44 +00001873
1874 // For C++, virtual base classes are not at fixed offset. Use following
1875 // expression to extract appropriate offset from vtable.
1876 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1877
1878 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
David Blaikief2443192013-10-21 17:28:37 +00001879 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1880 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1881 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1882 addUInt(VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1883 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1884 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1885 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
Devang Patel0e821f42011-04-12 23:21:44 +00001886
David Blaikief2443192013-10-21 17:28:37 +00001887 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
David Blaikie71d34a22013-11-01 00:25:45 +00001888 } else {
1889 uint64_t Size = DT.getSizeInBits();
1890 uint64_t FieldSize = getBaseTypeSize(DD, DT);
1891 uint64_t OffsetInBytes;
1892
1893 if (Size != FieldSize) {
1894 // Handle bitfield.
1895 addUInt(MemberDie, dwarf::DW_AT_byte_size, None,
1896 getBaseTypeSize(DD, DT) >> 3);
1897 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, DT.getSizeInBits());
1898
1899 uint64_t Offset = DT.getOffsetInBits();
1900 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1901 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1902 uint64_t FieldOffset = (HiMark - FieldSize);
1903 Offset -= FieldOffset;
1904
1905 // Maybe we need to work from the other end.
1906 if (Asm->getDataLayout().isLittleEndian())
1907 Offset = FieldSize - (Offset + Size);
1908 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
1909
1910 // Here WD_AT_data_member_location points to the anonymous
1911 // field that includes this bit field.
1912 OffsetInBytes = FieldOffset >> 3;
1913 } else
1914 // This is not a bitfield.
1915 OffsetInBytes = DT.getOffsetInBits() >> 3;
Eric Christopher98b7f172013-11-11 18:52:36 +00001916 addUInt(MemberDie, dwarf::DW_AT_data_member_location, None, OffsetInBytes);
David Blaikie71d34a22013-11-01 00:25:45 +00001917 }
Devang Patel0e821f42011-04-12 23:21:44 +00001918
1919 if (DT.isProtected())
Nick Lewyckycb918492011-12-13 05:09:11 +00001920 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001921 dwarf::DW_ACCESS_protected);
1922 else if (DT.isPrivate())
Nick Lewyckycb918492011-12-13 05:09:11 +00001923 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001924 dwarf::DW_ACCESS_private);
1925 // Otherwise C++ member and base classes are considered public.
Eric Christopher92331fd2012-11-21 00:34:38 +00001926 else
Nick Lewyckycb918492011-12-13 05:09:11 +00001927 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001928 dwarf::DW_ACCESS_public);
1929 if (DT.isVirtual())
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001930 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001931 dwarf::DW_VIRTUALITY_virtual);
Devang Patel514b4002011-04-16 00:11:51 +00001932
1933 // Objective-C properties.
Devang Patel44882172012-02-06 17:49:43 +00001934 if (MDNode *PNode = DT.getObjCProperty())
1935 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
Eric Christopher92331fd2012-11-21 00:34:38 +00001936 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
Devang Patel44882172012-02-06 17:49:43 +00001937 PropertyDie);
1938
David Blaikie37fefc32012-12-13 22:43:07 +00001939 if (DT.isArtificial())
1940 addFlag(MemberDie, dwarf::DW_AT_artificial);
Devang Patel0e821f42011-04-12 23:21:44 +00001941}
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001942
Manman Renc6b63922013-10-14 20:33:57 +00001943/// getOrCreateStaticMemberDIE - Create new DIE for C++ static member.
Eric Christophera5a79422013-12-09 23:32:48 +00001944DIE *DwarfUnit::getOrCreateStaticMemberDIE(DIDerivedType DT) {
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001945 if (!DT.Verify())
1946 return NULL;
1947
Manman Renc6b63922013-10-14 20:33:57 +00001948 // Construct the context before querying for the existence of the DIE in case
1949 // such construction creates the DIE.
1950 DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext()));
David Blaikiebd700e42013-11-14 21:24:34 +00001951 assert(dwarf::isType(ContextDIE->getTag()) &&
1952 "Static member should belong to a type.");
Manman Renc6b63922013-10-14 20:33:57 +00001953
1954 DIE *StaticMemberDIE = getDIE(DT);
1955 if (StaticMemberDIE)
1956 return StaticMemberDIE;
1957
Manman Renb987e512013-10-29 00:53:03 +00001958 StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT);
Manman Renc6b63922013-10-14 20:33:57 +00001959
Manman Ren93b30902013-10-08 18:42:58 +00001960 DIType Ty = resolve(DT.getTypeDerivedFrom());
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001961
1962 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1963 addType(StaticMemberDIE, Ty);
1964 addSourceLine(StaticMemberDIE, DT);
1965 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1966 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1967
1968 // FIXME: We could omit private if the parent is a class_type, and
1969 // public if the parent is something else.
1970 if (DT.isProtected())
1971 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1972 dwarf::DW_ACCESS_protected);
1973 else if (DT.isPrivate())
1974 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1975 dwarf::DW_ACCESS_private);
1976 else
1977 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1978 dwarf::DW_ACCESS_public);
1979
1980 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
Manman Renb3388602013-10-05 01:43:03 +00001981 addConstantValue(StaticMemberDIE, CI, isUnsignedDIType(DD, Ty));
David Blaikiea39a76e2013-01-20 01:18:01 +00001982 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
1983 addConstantFPValue(StaticMemberDIE, CFP);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001984
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001985 return StaticMemberDIE;
1986}
David Blaikie6b288cf2013-10-30 20:42:41 +00001987
Eric Christophera5a79422013-12-09 23:32:48 +00001988void DwarfUnit::emitHeader(const MCSection *ASection,
David Blaikie3332d4c2013-12-11 21:14:02 +00001989 const MCSymbol *ASectionSym) const {
David Blaikie6b288cf2013-10-30 20:42:41 +00001990 Asm->OutStreamer.AddComment("DWARF version number");
1991 Asm->EmitInt16(DD->getDwarfVersion());
1992 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
David Blaikie6896e192013-12-04 23:39:02 +00001993 // We share one abbreviations table across all units so it's always at the
1994 // start of the section. Use a relocatable offset where needed to ensure
1995 // linking doesn't invalidate that offset.
David Blaikie91db9ab2013-12-04 18:12:28 +00001996 Asm->EmitSectionOffset(ASectionSym, ASectionSym);
David Blaikie6b288cf2013-10-30 20:42:41 +00001997 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1998 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
1999}
David Blaikiebc563272013-12-13 21:33:40 +00002000
Juergen Ributzkadb9ee002013-12-14 12:23:14 +00002001DwarfCompileUnit::~DwarfCompileUnit() {}
2002DwarfTypeUnit::~DwarfTypeUnit() {}
2003
David Blaikiebc563272013-12-13 21:33:40 +00002004void DwarfTypeUnit::emitHeader(const MCSection *ASection,
2005 const MCSymbol *ASectionSym) const {
2006 DwarfUnit::emitHeader(ASection, ASectionSym);
2007 Asm->OutStreamer.AddComment("Type Signature");
2008 Asm->OutStreamer.EmitIntValue(TypeSignature, sizeof(TypeSignature));
2009 Asm->OutStreamer.AddComment("Type DIE Offset");
2010 Asm->OutStreamer.EmitIntValue(Ty->getOffset(), sizeof(Ty->getOffset()));
2011}
2012
2013void DwarfTypeUnit::initSection(const MCSection *Section) {
2014 assert(!this->Section);
2015 this->Section = Section;
2016 // Since each type unit is contained in its own COMDAT section, the begin
2017 // label and the section label are the same. Using the begin label emission in
2018 // DwarfDebug to emit the section label as well is slightly subtle/sneaky, but
2019 // the only other alternative of lazily constructing start-of-section labels
2020 // and storing a mapping in DwarfDebug (or AsmPrinter).
2021 this->SectionSym = this->LabelBegin =
2022 Asm->GetTempSymbol(Section->getLabelBeginName(), getUniqueID());
2023 this->LabelEnd =
2024 Asm->GetTempSymbol(Section->getLabelEndName(), getUniqueID());
2025 this->LabelRange = Asm->GetTempSymbol("gnu_ranges", getUniqueID());
2026}