blob: d3504f8a8f620c31152605a8846051aea8c119a4 [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
David Blaikie409dd9c2013-11-19 23:08:21 +000036static cl::opt<bool> GenerateTypeUnits("generate-type-units", cl::Hidden,
37 cl::desc("Generate DWARF4 type units."),
38 cl::init(false));
39
David Blaikie2a80e442013-12-02 22:09:48 +000040/// Unit - Unit constructor.
David Blaikie319a05f2013-12-02 19:33:10 +000041Unit::Unit(unsigned UID, DIE *D, DICompileUnit Node, AsmPrinter *A,
42 DwarfDebug *DW, DwarfUnits *DWU)
David Blaikie2a80e442013-12-02 22:09:48 +000043 : UniqueID(UID), Node(Node), UnitDie(D), DebugInfoOffset(0), Asm(A), DD(DW),
David Blaikie319a05f2013-12-02 19:33:10 +000044 DU(DWU), IndexTyDie(0) {
45 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
46}
47
David Blaikie5a152402013-11-15 23:52:02 +000048CompileUnit::CompileUnit(unsigned UID, DIE *D, DICompileUnit Node,
49 AsmPrinter *A, DwarfDebug *DW, DwarfUnits *DWU)
David Blaikie319a05f2013-12-02 19:33:10 +000050 : Unit(UID, D, Node, A, DW, DWU) {
David Blaikie5a152402013-11-15 23:52:02 +000051 insertDIE(Node, D);
Devang Patel0e821f42011-04-12 23:21:44 +000052}
53
David Blaikie319a05f2013-12-02 19:33:10 +000054TypeUnit::TypeUnit(unsigned UID, DIE *D, uint16_t Language, AsmPrinter *A,
55 DwarfDebug *DW, DwarfUnits *DWU)
56 : Unit(UID, D, DICompileUnit(), A, DW, DWU), Language(Language) {}
David Blaikie409dd9c2013-11-19 23:08:21 +000057
David Blaikie319a05f2013-12-02 19:33:10 +000058/// ~Unit - Destructor for compile unit.
59Unit::~Unit() {
Devang Patel0e821f42011-04-12 23:21:44 +000060 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
61 DIEBlocks[j]->~DIEBlock();
Eric Christopher0f63d062013-12-03 00:45:45 +000062 for (SmallVectorImpl<RangeSpanList *>::iterator RI = getRangeLists().begin(),
63 RE = getRangeLists().end();
64 RI != RE; ++RI)
65 delete *RI;
Devang Patel0e821f42011-04-12 23:21:44 +000066}
67
68/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
69/// information entry.
David Blaikie319a05f2013-12-02 19:33:10 +000070DIEEntry *Unit::createDIEEntry(DIE *Entry) {
Devang Patel0e821f42011-04-12 23:21:44 +000071 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
72 return Value;
73}
74
Bill Wendling3495f9b2012-12-06 07:55:19 +000075/// getDefaultLowerBound - Return the default lower bound for an array. If the
Bill Wendling28fe9e72012-12-06 07:38:10 +000076/// DWARF version doesn't handle the language, return -1.
David Blaikie319a05f2013-12-02 19:33:10 +000077int64_t Unit::getDefaultLowerBound() const {
David Blaikiecb8e4352013-11-15 23:50:53 +000078 switch (getLanguage()) {
Bill Wendling28fe9e72012-12-06 07:38:10 +000079 default:
80 break;
81
82 case dwarf::DW_LANG_C89:
83 case dwarf::DW_LANG_C99:
84 case dwarf::DW_LANG_C:
85 case dwarf::DW_LANG_C_plus_plus:
86 case dwarf::DW_LANG_ObjC:
87 case dwarf::DW_LANG_ObjC_plus_plus:
88 return 0;
89
90 case dwarf::DW_LANG_Fortran77:
91 case dwarf::DW_LANG_Fortran90:
92 case dwarf::DW_LANG_Fortran95:
93 return 1;
94
95 // The languages below have valid values only if the DWARF version >= 4.
96 case dwarf::DW_LANG_Java:
97 case dwarf::DW_LANG_Python:
98 case dwarf::DW_LANG_UPC:
99 case dwarf::DW_LANG_D:
100 if (dwarf::DWARF_VERSION >= 4)
101 return 0;
102 break;
103
104 case dwarf::DW_LANG_Ada83:
105 case dwarf::DW_LANG_Ada95:
106 case dwarf::DW_LANG_Cobol74:
107 case dwarf::DW_LANG_Cobol85:
108 case dwarf::DW_LANG_Modula2:
109 case dwarf::DW_LANG_Pascal83:
110 case dwarf::DW_LANG_PLI:
111 if (dwarf::DWARF_VERSION >= 4)
112 return 1;
113 break;
114 }
115
116 return -1;
117}
118
Manman Ren4dbdc902013-10-31 17:54:35 +0000119/// Check whether the DIE for this MDNode can be shared across CUs.
David Blaikie4201ddf2013-11-15 22:59:36 +0000120static bool isShareableAcrossCUs(DIDescriptor D) {
David Blaikiebcb418e2013-11-20 18:40:16 +0000121 // When the MDNode can be part of the type system, the DIE can be shared
122 // across CUs.
123 // Combining type units and cross-CU DIE sharing is lower value (since
124 // cross-CU DIE sharing is used in LTO and removes type redundancy at that
125 // level already) but may be implementable for some value in projects
126 // building multiple independent libraries with LTO and then linking those
127 // together.
David Blaikie409dd9c2013-11-19 23:08:21 +0000128 return (D.isType() ||
129 (D.isSubprogram() && !DISubprogram(D).isDefinition())) &&
130 !GenerateTypeUnits;
Manman Ren4dbdc902013-10-31 17:54:35 +0000131}
132
133/// getDIE - Returns the debug information entry map slot for the
134/// specified debug variable. We delegate the request to DwarfDebug
135/// when the DIE for this MDNode can be shared across CUs. The mappings
136/// will be kept in DwarfDebug for shareable DIEs.
David Blaikie319a05f2013-12-02 19:33:10 +0000137DIE *Unit::getDIE(DIDescriptor D) const {
David Blaikie2ad00162013-11-15 23:09:13 +0000138 if (isShareableAcrossCUs(D))
139 return DD->getDIE(D);
140 return MDNodeToDieMap.lookup(D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000141}
142
143/// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
144/// when the DIE for this MDNode can be shared across CUs. The mappings
145/// will be kept in DwarfDebug for shareable DIEs.
David Blaikie319a05f2013-12-02 19:33:10 +0000146void Unit::insertDIE(DIDescriptor Desc, DIE *D) {
David Blaikie2ad00162013-11-15 23:09:13 +0000147 if (isShareableAcrossCUs(Desc)) {
148 DD->insertDIE(Desc, D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000149 return;
150 }
David Blaikie2ad00162013-11-15 23:09:13 +0000151 MDNodeToDieMap.insert(std::make_pair(Desc, D));
Manman Ren4dbdc902013-10-31 17:54:35 +0000152}
153
Eric Christopherbb69a272012-08-24 01:14:27 +0000154/// addFlag - Add a flag that is true.
David Blaikie319a05f2013-12-02 19:33:10 +0000155void Unit::addFlag(DIE *Die, dwarf::Attribute Attribute) {
Michael Gottesmanc89466f2013-09-04 04:39:38 +0000156 if (DD->getDwarfVersion() >= 4)
Eric Christopherdccd3282013-10-04 22:40:05 +0000157 Die->addValue(Attribute, dwarf::DW_FORM_flag_present, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000158 else
Eric Christopherdccd3282013-10-04 22:40:05 +0000159 Die->addValue(Attribute, dwarf::DW_FORM_flag, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000160}
161
Devang Patel0e821f42011-04-12 23:21:44 +0000162/// addUInt - Add an unsigned integer attribute data and value.
163///
David Blaikie319a05f2013-12-02 19:33:10 +0000164void Unit::addUInt(DIE *Die, dwarf::Attribute Attribute,
165 Optional<dwarf::Form> Form, uint64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000166 if (!Form)
167 Form = DIEInteger::BestForm(false, Integer);
168 DIEValue *Value = Integer == 1 ? DIEIntegerOne : new (DIEValueAllocator)
169 DIEInteger(Integer);
David Blaikief2443192013-10-21 17:28:37 +0000170 Die->addValue(Attribute, *Form, Value);
171}
172
David Blaikie319a05f2013-12-02 19:33:10 +0000173void Unit::addUInt(DIEBlock *Block, dwarf::Form Form, uint64_t Integer) {
David Blaikief2443192013-10-21 17:28:37 +0000174 addUInt(Block, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000175}
176
177/// addSInt - Add an signed integer attribute data and value.
178///
David Blaikie319a05f2013-12-02 19:33:10 +0000179void Unit::addSInt(DIE *Die, dwarf::Attribute Attribute,
180 Optional<dwarf::Form> Form, int64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000181 if (!Form)
182 Form = DIEInteger::BestForm(true, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000183 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
David Blaikief2443192013-10-21 17:28:37 +0000184 Die->addValue(Attribute, *Form, Value);
185}
186
David Blaikie319a05f2013-12-02 19:33:10 +0000187void Unit::addSInt(DIEBlock *Die, Optional<dwarf::Form> Form, int64_t Integer) {
David Blaikief2443192013-10-21 17:28:37 +0000188 addSInt(Die, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000189}
190
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000191/// addString - Add a string attribute data and value. We always emit a
192/// reference to the string pool instead of immediate strings so that DIEs have
Eric Christopherfba22602013-01-07 19:32:45 +0000193/// more predictable sizes. In the case of split dwarf we emit an index
194/// into another table which gets us the static offset into the string
195/// table.
David Blaikie319a05f2013-12-02 19:33:10 +0000196void Unit::addString(DIE *Die, dwarf::Attribute Attribute, 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.
David Blaikie319a05f2013-12-02 19:33:10 +0000219void Unit::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///
David Blaikie319a05f2013-12-02 19:33:10 +0000234void Unit::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///
David Blaikie319a05f2013-12-02 19:33:10 +0000241void Unit::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
David Blaikie319a05f2013-12-02 19:33:10 +0000247void Unit::addLabel(DIEBlock *Die, dwarf::Form Form, const MCSymbol *Label) {
David Blaikief2443192013-10-21 17:28:37 +0000248 addLabel(Die, (dwarf::Attribute)0, Form, Label);
249}
250
Eric Christopher33ff6972013-11-21 23:46:41 +0000251/// addSectionLabel - Add a Dwarf section label attribute data and value.
252///
David Blaikie319a05f2013-12-02 19:33:10 +0000253void Unit::addSectionLabel(DIE *Die, dwarf::Attribute Attribute,
254 const MCSymbol *Label) {
Eric Christopher33ff6972013-11-21 23:46:41 +0000255 if (DD->getDwarfVersion() >= 4)
256 addLabel(Die, Attribute, dwarf::DW_FORM_sec_offset, Label);
257 else
258 addLabel(Die, Attribute, dwarf::DW_FORM_data4, Label);
259}
260
261/// addSectionOffset - Add an offset into a section attribute data and value.
262///
David Blaikie319a05f2013-12-02 19:33:10 +0000263void Unit::addSectionOffset(DIE *Die, dwarf::Attribute Attribute,
264 uint64_t Integer) {
Eric Christopher33ff6972013-11-21 23:46:41 +0000265 if (DD->getDwarfVersion() >= 4)
266 addUInt(Die, Attribute, dwarf::DW_FORM_sec_offset, Integer);
267 else
268 addUInt(Die, Attribute, dwarf::DW_FORM_data4, Integer);
269}
270
Eric Christopher962c9082013-01-15 23:56:56 +0000271/// addLabelAddress - Add a dwarf label attribute data and value using
272/// DW_FORM_addr or DW_FORM_GNU_addr_index.
273///
David Blaikief2443192013-10-21 17:28:37 +0000274void CompileUnit::addLabelAddress(DIE *Die, dwarf::Attribute Attribute,
Eric Christopher962c9082013-01-15 23:56:56 +0000275 MCSymbol *Label) {
Alexey Samsonov4436bf02013-10-03 08:54:43 +0000276 if (Label)
277 DD->addArangeLabel(SymbolCU(this, Label));
Richard Mitton21101b32013-09-19 23:21:01 +0000278
Eric Christopher962c9082013-01-15 23:56:56 +0000279 if (!DD->useSplitDwarf()) {
280 if (Label != NULL) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000281 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
Eric Christopher962c9082013-01-15 23:56:56 +0000282 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
283 } else {
284 DIEValue *Value = new (DIEValueAllocator) DIEInteger(0);
285 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
286 }
287 } else {
288 unsigned idx = DU->getAddrPoolIndex(Label);
289 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
290 Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
291 }
292}
293
Eric Christophere9ec2452013-01-18 22:11:33 +0000294/// addOpAddress - Add a dwarf op address data and value using the
295/// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
296///
David Blaikie319a05f2013-12-02 19:33:10 +0000297void Unit::addOpAddress(DIEBlock *Die, const MCSymbol *Sym) {
Eric Christophere9ec2452013-01-18 22:11:33 +0000298 if (!DD->useSplitDwarf()) {
David Blaikief2443192013-10-21 17:28:37 +0000299 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
300 addLabel(Die, dwarf::DW_FORM_udata, Sym);
Eric Christophere9ec2452013-01-18 22:11:33 +0000301 } else {
David Blaikief2443192013-10-21 17:28:37 +0000302 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
303 addUInt(Die, dwarf::DW_FORM_GNU_addr_index, DU->getAddrPoolIndex(Sym));
Eric Christophere9ec2452013-01-18 22:11:33 +0000304 }
305}
306
Eric Christopher33ff6972013-11-21 23:46:41 +0000307/// addSectionDelta - Add a section label delta attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000308///
David Blaikie319a05f2013-12-02 19:33:10 +0000309void Unit::addSectionDelta(DIE *Die, dwarf::Attribute Attribute,
310 const MCSymbol *Hi, const MCSymbol *Lo) {
Devang Patel0e821f42011-04-12 23:21:44 +0000311 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Eric Christopher33ff6972013-11-21 23:46:41 +0000312 if (DD->getDwarfVersion() >= 4)
313 Die->addValue(Attribute, dwarf::DW_FORM_sec_offset, Value);
314 else
315 Die->addValue(Attribute, dwarf::DW_FORM_data4, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000316}
317
318/// addDIEEntry - Add a DIE attribute data and value.
319///
David Blaikie319a05f2013-12-02 19:33:10 +0000320void Unit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +0000321 addDIEEntry(Die, Attribute, createDIEEntry(Entry));
322}
323
David Blaikie319a05f2013-12-02 19:33:10 +0000324void Unit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIEEntry *Entry) {
David Blaikie409dd9c2013-11-19 23:08:21 +0000325 const DIE *DieCU = Die->getUnitOrNull();
326 const DIE *EntryCU = Entry->getEntry()->getUnitOrNull();
Manman Ren4dbdc902013-10-31 17:54:35 +0000327 if (!DieCU)
328 // We assume that Die belongs to this CU, if it is not linked to any CU yet.
David Blaikie2a80e442013-12-02 22:09:48 +0000329 DieCU = getUnitDie();
Manman Ren4dbdc902013-10-31 17:54:35 +0000330 if (!EntryCU)
David Blaikie2a80e442013-12-02 22:09:48 +0000331 EntryCU = getUnitDie();
Manman Ren4dbdc902013-10-31 17:54:35 +0000332 Die->addValue(Attribute, EntryCU == DieCU ? dwarf::DW_FORM_ref4
333 : dwarf::DW_FORM_ref_addr,
334 Entry);
Devang Patel0e821f42011-04-12 23:21:44 +0000335}
336
Manman Renb987e512013-10-29 00:53:03 +0000337/// Create a DIE with the given Tag, add the DIE to its parent, and
338/// call insertDIE if MD is not null.
David Blaikie319a05f2013-12-02 19:33:10 +0000339DIE *Unit::createAndAddDIE(unsigned Tag, DIE &Parent, DIDescriptor N) {
Manman Renb987e512013-10-29 00:53:03 +0000340 DIE *Die = new DIE(Tag);
341 Parent.addChild(Die);
David Blaikie52c50202013-11-16 00:29:01 +0000342 if (N)
343 insertDIE(N, Die);
Manman Renb987e512013-10-29 00:53:03 +0000344 return Die;
345}
346
Devang Patel0e821f42011-04-12 23:21:44 +0000347/// addBlock - Add block data.
348///
David Blaikie319a05f2013-12-02 19:33:10 +0000349void Unit::addBlock(DIE *Die, dwarf::Attribute Attribute, DIEBlock *Block) {
Devang Patel0e821f42011-04-12 23:21:44 +0000350 Block->ComputeSize(Asm);
351 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
352 Die->addValue(Attribute, Block->BestForm(), Block);
353}
354
355/// addSourceLine - Add location information to specified debug information
356/// entry.
David Blaikie319a05f2013-12-02 19:33:10 +0000357void Unit::addSourceLine(DIE *Die, DIVariable V) {
Devang Patel0e821f42011-04-12 23:21:44 +0000358 // Verify variable.
Manman Ren7504ed42013-07-08 18:33:29 +0000359 if (!V.isVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000360 return;
Eric Christopher92331fd2012-11-21 00:34:38 +0000361
Devang Patel0e821f42011-04-12 23:21:44 +0000362 unsigned Line = V.getLineNumber();
363 if (Line == 0)
364 return;
Eric Christopherc2697f82013-10-19 01:04:47 +0000365 unsigned FileID =
366 DD->getOrCreateSourceID(V.getContext().getFilename(),
367 V.getContext().getDirectory(), getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000368 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000369 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
370 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000371}
372
373/// addSourceLine - Add location information to specified debug information
374/// entry.
David Blaikie319a05f2013-12-02 19:33:10 +0000375void Unit::addSourceLine(DIE *Die, DIGlobalVariable G) {
Devang Patel0e821f42011-04-12 23:21:44 +0000376 // Verify global variable.
Manman Ren7504ed42013-07-08 18:33:29 +0000377 if (!G.isGlobalVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000378 return;
379
380 unsigned Line = G.getLineNumber();
381 if (Line == 0)
382 return;
Eric Christopherc2697f82013-10-19 01:04:47 +0000383 unsigned FileID =
384 DD->getOrCreateSourceID(G.getFilename(), G.getDirectory(), getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000385 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000386 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
387 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000388}
389
390/// addSourceLine - Add location information to specified debug information
391/// entry.
David Blaikie319a05f2013-12-02 19:33:10 +0000392void Unit::addSourceLine(DIE *Die, DISubprogram SP) {
Devang Patel0e821f42011-04-12 23:21:44 +0000393 // Verify subprogram.
Manman Ren7504ed42013-07-08 18:33:29 +0000394 if (!SP.isSubprogram())
Devang Patel0e821f42011-04-12 23:21:44 +0000395 return;
Eric Christopher7734ca22012-03-15 23:55:40 +0000396
Devang Patel0e821f42011-04-12 23:21:44 +0000397 // If the line number is 0, don't add it.
Eric Christopher7734ca22012-03-15 23:55:40 +0000398 unsigned Line = SP.getLineNumber();
399 if (Line == 0)
Devang Patel0e821f42011-04-12 23:21:44 +0000400 return;
401
Eric Christopherc2697f82013-10-19 01:04:47 +0000402 unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(), SP.getDirectory(),
403 getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000404 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000405 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
406 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000407}
408
409/// addSourceLine - Add location information to specified debug information
410/// entry.
David Blaikie319a05f2013-12-02 19:33:10 +0000411void Unit::addSourceLine(DIE *Die, DIType Ty) {
Devang Patel0e821f42011-04-12 23:21:44 +0000412 // Verify type.
Manman Ren7504ed42013-07-08 18:33:29 +0000413 if (!Ty.isType())
Devang Patel0e821f42011-04-12 23:21:44 +0000414 return;
415
416 unsigned Line = Ty.getLineNumber();
Eric Christopher7734ca22012-03-15 23:55:40 +0000417 if (Line == 0)
Devang Patel0e821f42011-04-12 23:21:44 +0000418 return;
Eric Christopherc2697f82013-10-19 01:04:47 +0000419 unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(), Ty.getDirectory(),
420 getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000421 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000422 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
423 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000424}
425
426/// addSourceLine - Add location information to specified debug information
427/// entry.
David Blaikie319a05f2013-12-02 19:33:10 +0000428void Unit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
Eric Christopher70e1bd82012-03-29 08:42:56 +0000429 // Verify type.
Manman Ren7504ed42013-07-08 18:33:29 +0000430 if (!Ty.isObjCProperty())
Eric Christopher70e1bd82012-03-29 08:42:56 +0000431 return;
432
433 unsigned Line = Ty.getLineNumber();
434 if (Line == 0)
435 return;
436 DIFile File = Ty.getFile();
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000437 unsigned FileID = DD->getOrCreateSourceID(File.getFilename(),
Manman Ren1e427202013-03-07 01:42:00 +0000438 File.getDirectory(), getUniqueID());
Eric Christopher70e1bd82012-03-29 08:42:56 +0000439 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000440 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
441 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Eric Christopher70e1bd82012-03-29 08:42:56 +0000442}
443
444/// addSourceLine - Add location information to specified debug information
445/// entry.
David Blaikie319a05f2013-12-02 19:33:10 +0000446void Unit::addSourceLine(DIE *Die, DINameSpace NS) {
Devang Patel0e821f42011-04-12 23:21:44 +0000447 // Verify namespace.
448 if (!NS.Verify())
449 return;
450
451 unsigned Line = NS.getLineNumber();
452 if (Line == 0)
453 return;
454 StringRef FN = NS.getFilename();
455
Eric Christopherc2697f82013-10-19 01:04:47 +0000456 unsigned FileID =
457 DD->getOrCreateSourceID(FN, NS.getDirectory(), getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000458 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000459 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
460 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000461}
462
Eric Christopher92331fd2012-11-21 00:34:38 +0000463/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patel77dc5412011-04-27 22:45:24 +0000464/// DbgVariable based on provided MachineLocation.
David Blaikie319a05f2013-12-02 19:33:10 +0000465void Unit::addVariableAddress(const DbgVariable &DV, DIE *Die,
466 MachineLocation Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000467 if (DV.variableHasComplexAddress())
Devang Patel0e821f42011-04-12 23:21:44 +0000468 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000469 else if (DV.isBlockByrefVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000470 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
471 else
David Blaikieea2605d2013-06-20 00:25:24 +0000472 addAddress(Die, dwarf::DW_AT_location, Location,
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000473 DV.getVariable().isIndirect());
Devang Patel0e821f42011-04-12 23:21:44 +0000474}
475
Devang Patelba5fbf12011-04-26 19:06:18 +0000476/// addRegisterOp - Add register operand.
David Blaikie319a05f2013-12-02 19:33:10 +0000477void Unit::addRegisterOp(DIEBlock *TheDie, unsigned Reg) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000478 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
479 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
480 if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000481 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000482 else {
David Blaikief2443192013-10-21 17:28:37 +0000483 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
484 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000485 }
486}
487
488/// addRegisterOffset - Add register offset.
David Blaikie319a05f2013-12-02 19:33:10 +0000489void Unit::addRegisterOffset(DIEBlock *TheDie, unsigned Reg, int64_t Offset) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000490 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
491 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
492 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
493 if (Reg == TRI->getFrameRegister(*Asm->MF))
494 // If variable offset is based in frame register then use fbreg.
David Blaikief2443192013-10-21 17:28:37 +0000495 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000496 else if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000497 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000498 else {
David Blaikief2443192013-10-21 17:28:37 +0000499 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
500 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000501 }
David Blaikief2443192013-10-21 17:28:37 +0000502 addSInt(TheDie, dwarf::DW_FORM_sdata, Offset);
Devang Patelba5fbf12011-04-26 19:06:18 +0000503}
504
505/// addAddress - Add an address attribute to a die based on the location
506/// provided.
David Blaikie319a05f2013-12-02 19:33:10 +0000507void Unit::addAddress(DIE *Die, dwarf::Attribute Attribute,
508 const MachineLocation &Location, bool Indirect) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000509 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
510
David Blaikieea2605d2013-06-20 00:25:24 +0000511 if (Location.isReg() && !Indirect)
Devang Patelba5fbf12011-04-26 19:06:18 +0000512 addRegisterOp(Block, Location.getReg());
David Blaikieea2605d2013-06-20 00:25:24 +0000513 else {
Devang Patelba5fbf12011-04-26 19:06:18 +0000514 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
David Blaikieea2605d2013-06-20 00:25:24 +0000515 if (Indirect && !Location.isReg()) {
David Blaikief2443192013-10-21 17:28:37 +0000516 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
David Blaikieea2605d2013-06-20 00:25:24 +0000517 }
518 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000519
520 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000521 addBlock(Die, Attribute, Block);
Devang Patelba5fbf12011-04-26 19:06:18 +0000522}
523
Devang Patel0e821f42011-04-12 23:21:44 +0000524/// addComplexAddress - Start with the address based on the location provided,
525/// and generate the DWARF information necessary to find the actual variable
526/// given the extra address information encoded in the DIVariable, starting from
527/// the starting location. Add the DWARF information to the die.
528///
David Blaikie319a05f2013-12-02 19:33:10 +0000529void Unit::addComplexAddress(const DbgVariable &DV, DIE *Die,
530 dwarf::Attribute Attribute,
531 const MachineLocation &Location) {
Devang Patel0e821f42011-04-12 23:21:44 +0000532 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000533 unsigned N = DV.getNumAddrElements();
Devang Patel3e021532011-04-28 02:22:40 +0000534 unsigned i = 0;
535 if (Location.isReg()) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000536 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Devang Patel3e021532011-04-28 02:22:40 +0000537 // If first address element is OpPlus then emit
538 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000539 addRegisterOffset(Block, Location.getReg(), DV.getAddrElement(1));
Devang Patel3e021532011-04-28 02:22:40 +0000540 i = 2;
541 } else
542 addRegisterOp(Block, Location.getReg());
Eric Christopherc2697f82013-10-19 01:04:47 +0000543 } else
Devang Patelba5fbf12011-04-26 19:06:18 +0000544 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000545
Eric Christopherc2697f82013-10-19 01:04:47 +0000546 for (; i < N; ++i) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000547 uint64_t Element = DV.getAddrElement(i);
Devang Patel0e821f42011-04-12 23:21:44 +0000548 if (Element == DIBuilder::OpPlus) {
David Blaikief2443192013-10-21 17:28:37 +0000549 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
550 addUInt(Block, dwarf::DW_FORM_udata, DV.getAddrElement(++i));
Devang Patel0e821f42011-04-12 23:21:44 +0000551 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher4d250522012-05-08 18:56:00 +0000552 if (!Location.isReg())
David Blaikief2443192013-10-21 17:28:37 +0000553 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Eric Christopherc2697f82013-10-19 01:04:47 +0000554 } else
555 llvm_unreachable("unknown DIBuilder Opcode");
Devang Patel0e821f42011-04-12 23:21:44 +0000556 }
557
558 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000559 addBlock(Die, Attribute, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000560}
561
562/* Byref variables, in Blocks, are declared by the programmer as "SomeType
563 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
564 gives the variable VarName either the struct, or a pointer to the struct, as
565 its type. This is necessary for various behind-the-scenes things the
566 compiler needs to do with by-reference variables in Blocks.
567
568 However, as far as the original *programmer* is concerned, the variable
569 should still have type 'SomeType', as originally declared.
570
571 The function getBlockByrefType dives into the __Block_byref_x_VarName
572 struct to find the original type of the variable, which is then assigned to
573 the variable's Debug Information Entry as its real type. So far, so good.
574 However now the debugger will expect the variable VarName to have the type
575 SomeType. So we need the location attribute for the variable to be an
576 expression that explains to the debugger how to navigate through the
577 pointers and struct to find the actual variable of type SomeType.
578
579 The following function does just that. We start by getting
580 the "normal" location for the variable. This will be the location
581 of either the struct __Block_byref_x_VarName or the pointer to the
582 struct __Block_byref_x_VarName.
583
584 The struct will look something like:
585
586 struct __Block_byref_x_VarName {
587 ... <various fields>
588 struct __Block_byref_x_VarName *forwarding;
589 ... <various other fields>
590 SomeType VarName;
591 ... <maybe more fields>
592 };
593
594 If we are given the struct directly (as our starting point) we
595 need to tell the debugger to:
596
597 1). Add the offset of the forwarding field.
598
599 2). Follow that pointer to get the real __Block_byref_x_VarName
600 struct to use (the real one may have been copied onto the heap).
601
602 3). Add the offset for the field VarName, to find the actual variable.
603
604 If we started with a pointer to the struct, then we need to
605 dereference that pointer first, before the other steps.
606 Translating this into DWARF ops, we will need to append the following
607 to the current location description for the variable:
608
609 DW_OP_deref -- optional, if we start with a pointer
610 DW_OP_plus_uconst <forward_fld_offset>
611 DW_OP_deref
612 DW_OP_plus_uconst <varName_fld_offset>
613
614 That is what this function does. */
615
616/// addBlockByrefAddress - Start with the address based on the location
617/// provided, and generate the DWARF information necessary to find the
618/// actual Block variable (navigating the Block struct) based on the
619/// starting location. Add the DWARF information to the die. For
620/// more information, read large comment just above here.
621///
David Blaikie319a05f2013-12-02 19:33:10 +0000622void Unit::addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
623 dwarf::Attribute Attribute,
624 const MachineLocation &Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000625 DIType Ty = DV.getType();
Devang Patel0e821f42011-04-12 23:21:44 +0000626 DIType TmpTy = Ty;
Eric Christopher31b05762013-08-08 01:41:00 +0000627 uint16_t Tag = Ty.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +0000628 bool isPointer = false;
629
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000630 StringRef varName = DV.getName();
Devang Patel0e821f42011-04-12 23:21:44 +0000631
632 if (Tag == dwarf::DW_TAG_pointer_type) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000633 DIDerivedType DTy(Ty);
Manman Ren93b30902013-10-08 18:42:58 +0000634 TmpTy = resolve(DTy.getTypeDerivedFrom());
Devang Patel0e821f42011-04-12 23:21:44 +0000635 isPointer = true;
636 }
637
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000638 DICompositeType blockStruct(TmpTy);
Devang Patel0e821f42011-04-12 23:21:44 +0000639
640 // Find the __forwarding field and the variable field in the __Block_byref
641 // struct.
642 DIArray Fields = blockStruct.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000643 DIDerivedType varField;
644 DIDerivedType forwardingField;
Devang Patel0e821f42011-04-12 23:21:44 +0000645
646 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000647 DIDerivedType DT(Fields.getElement(i));
Devang Patel0e821f42011-04-12 23:21:44 +0000648 StringRef fieldName = DT.getName();
649 if (fieldName == "__forwarding")
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000650 forwardingField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000651 else if (fieldName == varName)
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000652 varField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000653 }
654
655 // Get the offsets for the forwarding field and the variable field.
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000656 unsigned forwardingFieldOffset = forwardingField.getOffsetInBits() >> 3;
657 unsigned varFieldOffset = varField.getOffsetInBits() >> 2;
Devang Patel0e821f42011-04-12 23:21:44 +0000658
659 // Decode the original location, and use that as the start of the byref
660 // variable's location.
Devang Patel0e821f42011-04-12 23:21:44 +0000661 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
662
Eric Christopheref9d7102012-07-04 02:02:18 +0000663 if (Location.isReg())
664 addRegisterOp(Block, Location.getReg());
665 else
666 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000667
668 // If we started with a pointer to the __Block_byref... struct, then
669 // the first thing we need to do is dereference the pointer (DW_OP_deref).
670 if (isPointer)
David Blaikief2443192013-10-21 17:28:37 +0000671 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000672
673 // Next add the offset for the '__forwarding' field:
674 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
675 // adding the offset if it's 0.
676 if (forwardingFieldOffset > 0) {
David Blaikief2443192013-10-21 17:28:37 +0000677 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
678 addUInt(Block, dwarf::DW_FORM_udata, forwardingFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000679 }
680
681 // Now dereference the __forwarding field to get to the real __Block_byref
682 // struct: DW_OP_deref.
David Blaikief2443192013-10-21 17:28:37 +0000683 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000684
685 // Now that we've got the real __Block_byref... struct, add the offset
686 // for the variable's field to get to the location of the actual variable:
687 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
688 if (varFieldOffset > 0) {
David Blaikief2443192013-10-21 17:28:37 +0000689 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
690 addUInt(Block, dwarf::DW_FORM_udata, varFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000691 }
692
693 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000694 addBlock(Die, Attribute, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000695}
696
Devang Patelbcd50a12011-07-20 21:57:04 +0000697/// isTypeSigned - Return true if the type is signed.
Manman Renb3388602013-10-05 01:43:03 +0000698static bool isTypeSigned(DwarfDebug *DD, DIType Ty, int *SizeInBits) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000699 if (Ty.isDerivedType())
Manman Renb3388602013-10-05 01:43:03 +0000700 return isTypeSigned(DD, DD->resolve(DIDerivedType(Ty).getTypeDerivedFrom()),
701 SizeInBits);
Devang Patelbcd50a12011-07-20 21:57:04 +0000702 if (Ty.isBasicType())
Eric Christopherc2697f82013-10-19 01:04:47 +0000703 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed ||
704 DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000705 *SizeInBits = Ty.getSizeInBits();
706 return true;
707 }
708 return false;
709}
710
Manman Renb3388602013-10-05 01:43:03 +0000711/// Return true if type encoding is unsigned.
712static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) {
713 DIDerivedType DTy(Ty);
714 if (DTy.isDerivedType())
715 return isUnsignedDIType(DD, DD->resolve(DTy.getTypeDerivedFrom()));
716
717 DIBasicType BTy(Ty);
718 if (BTy.isBasicType()) {
719 unsigned Encoding = BTy.getEncoding();
720 if (Encoding == dwarf::DW_ATE_unsigned ||
721 Encoding == dwarf::DW_ATE_unsigned_char ||
722 Encoding == dwarf::DW_ATE_boolean)
723 return true;
724 }
725 return false;
726}
727
728/// If this type is derived from a base type then return base type size.
Manman Renbda410f2013-10-08 18:46:58 +0000729static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) {
Manman Renb3388602013-10-05 01:43:03 +0000730 unsigned Tag = Ty.getTag();
731
732 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
733 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
734 Tag != dwarf::DW_TAG_restrict_type)
735 return Ty.getSizeInBits();
736
737 DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom());
738
739 // If this type is not derived from any type then take conservative approach.
740 if (!BaseType.isValid())
741 return Ty.getSizeInBits();
742
743 // If this is a derived type, go ahead and get the base type, unless it's a
744 // reference then it's just the size of the field. Pointer types have no need
745 // of this since they're a different type of qualification on the type.
746 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
747 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
748 return Ty.getSizeInBits();
749
750 if (BaseType.isDerivedType())
Manman Renbda410f2013-10-08 18:46:58 +0000751 return getBaseTypeSize(DD, DIDerivedType(BaseType));
Manman Renb3388602013-10-05 01:43:03 +0000752
753 return BaseType.getSizeInBits();
754}
755
Devang Patel0e821f42011-04-12 23:21:44 +0000756/// addConstantValue - Add constant value entry in variable DIE.
David Blaikie319a05f2013-12-02 19:33:10 +0000757void Unit::addConstantValue(DIE *Die, const MachineOperand &MO, DIType Ty) {
David Blaikiea1e813d2013-05-10 21:52:07 +0000758 // FIXME: This is a bit conservative/simple - it emits negative values at
759 // their maximum bit width which is a bit unfortunate (& doesn't prefer
760 // udata/sdata over dataN as suggested by the DWARF spec)
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000761 assert(MO.isImm() && "Invalid machine operand!");
Devang Patelbcd50a12011-07-20 21:57:04 +0000762 int SizeInBits = -1;
Manman Renb3388602013-10-05 01:43:03 +0000763 bool SignedConstant = isTypeSigned(DD, Ty, &SizeInBits);
David Blaikief2443192013-10-21 17:28:37 +0000764 dwarf::Form Form;
Devang Patelf1d04702011-05-27 18:15:52 +0000765
Eric Christopher9d1daa82013-08-27 23:49:04 +0000766 // If we're a signed constant definitely use sdata.
767 if (SignedConstant) {
768 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, MO.getImm());
769 return;
770 }
771
772 // Else use data for now unless it's larger than we can deal with.
773 switch (SizeInBits) {
774 case 8:
775 Form = dwarf::DW_FORM_data1;
776 break;
777 case 16:
778 Form = dwarf::DW_FORM_data2;
779 break;
780 case 32:
781 Form = dwarf::DW_FORM_data4;
782 break;
783 case 64:
784 Form = dwarf::DW_FORM_data8;
785 break;
786 default:
787 Form = dwarf::DW_FORM_udata;
788 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
789 return;
790 }
791 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
Devang Patel0e821f42011-04-12 23:21:44 +0000792}
793
794/// addConstantFPValue - Add constant value entry in variable DIE.
David Blaikie319a05f2013-12-02 19:33:10 +0000795void Unit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000796 assert(MO.isFPImm() && "Invalid machine operand!");
Devang Patel0e821f42011-04-12 23:21:44 +0000797 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
798 APFloat FPImm = MO.getFPImm()->getValueAPF();
799
800 // Get the raw data form of the floating point.
801 const APInt FltVal = FPImm.bitcastToAPInt();
Eric Christopherc2697f82013-10-19 01:04:47 +0000802 const char *FltPtr = (const char *)FltVal.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000803
804 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000805 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000806 int Incr = (LittleEndian ? 1 : -1);
807 int Start = (LittleEndian ? 0 : NumBytes - 1);
808 int Stop = (LittleEndian ? NumBytes : -1);
809
810 // Output the constant to DWARF one byte at a time.
811 for (; Start != Stop; Start += Incr)
Eric Christopher98b7f172013-11-11 18:52:36 +0000812 addUInt(Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]);
Devang Patel0e821f42011-04-12 23:21:44 +0000813
David Blaikief2443192013-10-21 17:28:37 +0000814 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000815}
816
David Blaikiea39a76e2013-01-20 01:18:01 +0000817/// addConstantFPValue - Add constant value entry in variable DIE.
David Blaikie319a05f2013-12-02 19:33:10 +0000818void Unit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000819 // Pass this down to addConstantValue as an unsigned bag of bits.
820 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
David Blaikiea39a76e2013-01-20 01:18:01 +0000821}
822
Devang Patel0e821f42011-04-12 23:21:44 +0000823/// addConstantValue - Add constant value entry in variable DIE.
David Blaikie319a05f2013-12-02 19:33:10 +0000824void Unit::addConstantValue(DIE *Die, const ConstantInt *CI, bool Unsigned) {
Eric Christopher78fcf4902013-07-03 01:08:30 +0000825 addConstantValue(Die, CI->getValue(), Unsigned);
David Blaikiea39a76e2013-01-20 01:18:01 +0000826}
827
828// addConstantValue - Add constant value entry in variable DIE.
David Blaikie319a05f2013-12-02 19:33:10 +0000829void Unit::addConstantValue(DIE *Die, const APInt &Val, bool Unsigned) {
David Blaikiea39a76e2013-01-20 01:18:01 +0000830 unsigned CIBitWidth = Val.getBitWidth();
Devang Patel8816bbc2011-05-28 00:39:18 +0000831 if (CIBitWidth <= 64) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000832 // If we're a signed constant definitely use sdata.
833 if (!Unsigned) {
834 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
835 Val.getSExtValue());
836 return;
Devang Patel8816bbc2011-05-28 00:39:18 +0000837 }
Eric Christopher9d1daa82013-08-27 23:49:04 +0000838
839 // Else use data for now unless it's larger than we can deal with.
David Blaikief2443192013-10-21 17:28:37 +0000840 dwarf::Form Form;
Eric Christopher9d1daa82013-08-27 23:49:04 +0000841 switch (CIBitWidth) {
842 case 8:
843 Form = dwarf::DW_FORM_data1;
844 break;
845 case 16:
846 Form = dwarf::DW_FORM_data2;
847 break;
848 case 32:
849 Form = dwarf::DW_FORM_data4;
850 break;
851 case 64:
852 Form = dwarf::DW_FORM_data8;
853 break;
854 default:
855 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
856 Val.getZExtValue());
857 return;
858 }
859 addUInt(Die, dwarf::DW_AT_const_value, Form, Val.getZExtValue());
Eric Christopher78fcf4902013-07-03 01:08:30 +0000860 return;
Devang Patel0e821f42011-04-12 23:21:44 +0000861 }
862
863 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
864
865 // Get the raw data form of the large APInt.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000866 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000867
868 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000869 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000870
871 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000872 for (int i = 0; i < NumBytes; i++) {
873 uint8_t c;
874 if (LittleEndian)
875 c = Ptr64[i / 8] >> (8 * (i & 7));
876 else
877 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
David Blaikief2443192013-10-21 17:28:37 +0000878 addUInt(Block, dwarf::DW_FORM_data1, c);
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000879 }
Devang Patel0e821f42011-04-12 23:21:44 +0000880
David Blaikief2443192013-10-21 17:28:37 +0000881 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000882}
883
Eric Christopher25e35092013-04-22 07:47:40 +0000884/// addTemplateParams - Add template parameters into buffer.
David Blaikie319a05f2013-12-02 19:33:10 +0000885void Unit::addTemplateParams(DIE &Buffer, DIArray TParams) {
Devang Patel0e821f42011-04-12 23:21:44 +0000886 // Add template parameters.
887 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
888 DIDescriptor Element = TParams.getElement(i);
889 if (Element.isTemplateTypeParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000890 constructTemplateTypeParameterDIE(Buffer,
891 DITemplateTypeParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000892 else if (Element.isTemplateValueParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000893 constructTemplateValueParameterDIE(Buffer,
894 DITemplateValueParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000895 }
Devang Patel0e821f42011-04-12 23:21:44 +0000896}
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000897
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000898/// getOrCreateContextDIE - Get context owner's DIE.
David Blaikie319a05f2013-12-02 19:33:10 +0000899DIE *Unit::getOrCreateContextDIE(DIScope Context) {
David Blaikiebd700e42013-11-14 21:24:34 +0000900 if (!Context || Context.isFile())
David Blaikie2a80e442013-12-02 22:09:48 +0000901 return getUnitDie();
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000902 if (Context.isType())
903 return getOrCreateTypeDIE(DIType(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000904 if (Context.isNameSpace())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000905 return getOrCreateNameSpace(DINameSpace(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000906 if (Context.isSubprogram())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000907 return getOrCreateSubprogramDIE(DISubprogram(Context));
Adrian Prantl7d828bb2013-11-15 21:05:09 +0000908 return getDIE(Context);
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000909}
910
David Blaikie319a05f2013-12-02 19:33:10 +0000911DIE *Unit::createTypeDIE(DICompositeType Ty) {
David Blaikie9d861be2013-11-26 00:15:27 +0000912 DIScope Context = resolve(Ty.getContext());
913 DIE *ContextDIE = getOrCreateContextDIE(Context);
David Blaikie409dd9c2013-11-19 23:08:21 +0000914
915 DIE *TyDIE = getDIE(Ty);
916 if (TyDIE)
917 return TyDIE;
918
919 // Create new type.
920 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
921
David Blaikiefbd29eb2013-11-26 00:35:04 +0000922 constructTypeDIE(*TyDIE, Ty);
David Blaikie409dd9c2013-11-19 23:08:21 +0000923
David Blaikie9d861be2013-11-26 00:15:27 +0000924 updateAcceleratorTables(Context, Ty, TyDIE);
David Blaikie409dd9c2013-11-19 23:08:21 +0000925 return TyDIE;
926}
927
David Blaikie8a263cb2013-11-26 00:22:37 +0000928/// Return true if the type is appropriately scoped to be contained inside
929/// its own type unit.
930static bool isTypeUnitScoped(DIType Ty, const DwarfDebug *DD) {
931 DIScope Parent = DD->resolve(Ty.getContext());
932 while (Parent) {
933 // Don't generate a hash for anything scoped inside a function.
934 if (Parent.isSubprogram())
935 return false;
936 Parent = DD->resolve(Parent.getContext());
937 }
938 return true;
939}
940
941/// Return true if the type should be split out into a type unit.
942static bool shouldCreateTypeUnit(DICompositeType CTy, const DwarfDebug *DD) {
943 if (!GenerateTypeUnits)
944 return false;
945
946 uint16_t Tag = CTy.getTag();
947
948 switch (Tag) {
949 case dwarf::DW_TAG_structure_type:
950 case dwarf::DW_TAG_union_type:
951 case dwarf::DW_TAG_enumeration_type:
952 case dwarf::DW_TAG_class_type:
953 // If this is a class, structure, union, or enumeration type
954 // that is a definition (not a declaration), and not scoped
955 // inside a function then separate this out as a type unit.
956 return !CTy.isForwardDecl() && isTypeUnitScoped(CTy, DD);
957 default:
958 return false;
959 }
960}
961
Devang Patel0e821f42011-04-12 23:21:44 +0000962/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
963/// given DIType.
David Blaikie319a05f2013-12-02 19:33:10 +0000964DIE *Unit::getOrCreateTypeDIE(const MDNode *TyNode) {
David Blaikie32887552013-11-14 22:25:02 +0000965 if (!TyNode)
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000966 return NULL;
Manman Renf4c339e2013-10-29 22:49:29 +0000967
David Blaikie32887552013-11-14 22:25:02 +0000968 DIType Ty(TyNode);
969 assert(Ty.isType());
970
Manman Renf4c339e2013-10-29 22:49:29 +0000971 // Construct the context before querying for the existence of the DIE in case
972 // such construction creates the DIE.
David Blaikie9d861be2013-11-26 00:15:27 +0000973 DIScope Context = resolve(Ty.getContext());
974 DIE *ContextDIE = getOrCreateContextDIE(Context);
Adrian Prantl4583f7d2013-11-15 23:21:39 +0000975 assert(ContextDIE);
Manman Renf4c339e2013-10-29 22:49:29 +0000976
Eric Christophere595bae2013-10-04 17:08:38 +0000977 DIE *TyDIE = getDIE(Ty);
Devang Patel0e821f42011-04-12 23:21:44 +0000978 if (TyDIE)
979 return TyDIE;
980
981 // Create new type.
Manman Renf4c339e2013-10-29 22:49:29 +0000982 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
983
Devang Patel0e821f42011-04-12 23:21:44 +0000984 if (Ty.isBasicType())
985 constructTypeDIE(*TyDIE, DIBasicType(Ty));
David Blaikie8a263cb2013-11-26 00:22:37 +0000986 else if (Ty.isCompositeType()) {
987 DICompositeType CTy(Ty);
988 if (shouldCreateTypeUnit(CTy, DD)) {
David Blaikie3c1d3322013-12-02 18:44:29 +0000989 DD->addTypeUnitType(getLanguage(), TyDIE, CTy);
David Blaikie8a263cb2013-11-26 00:22:37 +0000990 // Skip updating the accellerator tables since this is not the full type
991 return TyDIE;
992 }
David Blaikiefbd29eb2013-11-26 00:35:04 +0000993 constructTypeDIE(*TyDIE, CTy);
David Blaikie8a263cb2013-11-26 00:22:37 +0000994 } else {
Devang Patel0e821f42011-04-12 23:21:44 +0000995 assert(Ty.isDerivedType() && "Unknown kind of DIType");
996 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
997 }
David Blaikie2ea848b2013-11-19 22:51:04 +0000998
David Blaikie9d861be2013-11-26 00:15:27 +0000999 updateAcceleratorTables(Context, Ty, TyDIE);
David Blaikie2ea848b2013-11-19 22:51:04 +00001000
1001 return TyDIE;
1002}
1003
David Blaikie319a05f2013-12-02 19:33:10 +00001004void Unit::updateAcceleratorTables(DIScope Context, DIType Ty,
1005 const DIE *TyDIE) {
Eric Christopher21bde872012-01-06 04:35:23 +00001006 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
1007 bool IsImplementation = 0;
1008 if (Ty.isCompositeType()) {
1009 DICompositeType CT(Ty);
Eric Christopher8ea8e4f2012-01-06 23:03:37 +00001010 // A runtime language of 0 actually means C/C++ and that any
1011 // non-negative value is some version of Objective-C/C++.
Eric Christopherc2697f82013-10-19 01:04:47 +00001012 IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete();
Eric Christopher21bde872012-01-06 04:35:23 +00001013 }
Eric Christophercf7289f2013-09-05 18:20:16 +00001014 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
Eric Christopher8ea8e4f2012-01-06 23:03:37 +00001015 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
David Blaikie9d861be2013-11-26 00:15:27 +00001016
1017 if (!Context || Context.isCompileUnit() || Context.isFile() ||
1018 Context.isNameSpace())
1019 GlobalTypes[getParentContextString(Context) + Ty.getName().str()] = TyDIE;
Eric Christopher21bde872012-01-06 04:35:23 +00001020 }
Devang Patel0e821f42011-04-12 23:21:44 +00001021}
1022
1023/// addType - Add a new type attribute to the specified entity.
David Blaikie319a05f2013-12-02 19:33:10 +00001024void Unit::addType(DIE *Entity, DIType Ty, dwarf::Attribute Attribute) {
Eric Christopher0df08e22013-08-08 07:40:37 +00001025 assert(Ty && "Trying to add a type that doesn't exist?");
Devang Patel0e821f42011-04-12 23:21:44 +00001026
1027 // Check for pre-existence.
1028 DIEEntry *Entry = getDIEEntry(Ty);
1029 // If it exists then use the existing value.
1030 if (Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +00001031 addDIEEntry(Entity, Attribute, Entry);
Devang Patel0e821f42011-04-12 23:21:44 +00001032 return;
1033 }
1034
1035 // Construct type.
1036 DIE *Buffer = getOrCreateTypeDIE(Ty);
1037
1038 // Set up proxy.
1039 Entry = createDIEEntry(Buffer);
1040 insertDIEEntry(Ty, Entry);
Manman Ren4dbdc902013-10-31 17:54:35 +00001041 addDIEEntry(Entity, Attribute, Entry);
Devang Patel1cb8ab42011-05-31 23:30:30 +00001042}
1043
Eric Christopher9cd26af2013-09-20 23:22:52 +00001044// Accelerator table mutators - add each name along with its companion
1045// DIE to the proper table while ensuring that the name that we're going
1046// to reference is in the string table. We do this since the names we
1047// add may not only be identical to the names in the DIE.
David Blaikie319a05f2013-12-02 19:33:10 +00001048void Unit::addAccelName(StringRef Name, const DIE *Die) {
Eric Christopher9cd26af2013-09-20 23:22:52 +00001049 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001050 std::vector<const DIE *> &DIEs = AccelNames[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001051 DIEs.push_back(Die);
1052}
1053
David Blaikie319a05f2013-12-02 19:33:10 +00001054void Unit::addAccelObjC(StringRef Name, const DIE *Die) {
Eric Christopher9cd26af2013-09-20 23:22:52 +00001055 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001056 std::vector<const DIE *> &DIEs = AccelObjC[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001057 DIEs.push_back(Die);
1058}
1059
David Blaikie319a05f2013-12-02 19:33:10 +00001060void Unit::addAccelNamespace(StringRef Name, const DIE *Die) {
Eric Christopher9cd26af2013-09-20 23:22:52 +00001061 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001062 std::vector<const DIE *> &DIEs = AccelNamespace[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001063 DIEs.push_back(Die);
1064}
1065
David Blaikie319a05f2013-12-02 19:33:10 +00001066void Unit::addAccelType(StringRef Name, std::pair<const DIE *, unsigned> Die) {
Eric Christopher9cd26af2013-09-20 23:22:52 +00001067 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001068 std::vector<std::pair<const DIE *, unsigned> > &DIEs = AccelTypes[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001069 DIEs.push_back(Die);
1070}
1071
Eric Christopher9c58f312013-09-20 22:20:55 +00001072/// addGlobalName - Add a new global name to the compile unit.
David Blaikie319a05f2013-12-02 19:33:10 +00001073void Unit::addGlobalName(StringRef Name, DIE *Die, DIScope Context) {
Eric Christopher8dba0d52013-10-19 01:04:42 +00001074 std::string FullName = getParentContextString(Context) + Name.str();
Eric Christopher2c8b7902013-10-17 02:06:06 +00001075 GlobalNames[FullName] = Die;
Eric Christopher9c58f312013-09-20 22:20:55 +00001076}
1077
Eric Christopher2c8b7902013-10-17 02:06:06 +00001078/// getParentContextString - Walks the metadata parent chain in a language
1079/// specific manner (using the compile unit language) and returns
1080/// it as a string. This is done at the metadata level because DIEs may
1081/// not currently have been added to the parent context and walking the
1082/// DIEs looking for names is more expensive than walking the metadata.
David Blaikie319a05f2013-12-02 19:33:10 +00001083std::string Unit::getParentContextString(DIScope Context) const {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001084 if (!Context)
1085 return "";
1086
1087 // FIXME: Decide whether to implement this for non-C++ languages.
1088 if (getLanguage() != dwarf::DW_LANG_C_plus_plus)
1089 return "";
1090
Eric Christopher8dba0d52013-10-19 01:04:42 +00001091 std::string CS;
Eric Christopher2c8b7902013-10-17 02:06:06 +00001092 SmallVector<DIScope, 1> Parents;
1093 while (!Context.isCompileUnit()) {
1094 Parents.push_back(Context);
1095 if (Context.getContext())
1096 Context = resolve(Context.getContext());
1097 else
1098 // Structure, etc types will have a NULL context if they're at the top
1099 // level.
1100 break;
1101 }
1102
1103 // Reverse iterate over our list to go from the outermost construct to the
1104 // innermost.
1105 for (SmallVectorImpl<DIScope>::reverse_iterator I = Parents.rbegin(),
1106 E = Parents.rend();
1107 I != E; ++I) {
1108 DIScope Ctx = *I;
1109 StringRef Name = Ctx.getName();
Eric Christopher8dba0d52013-10-19 01:04:42 +00001110 if (!Name.empty()) {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001111 CS += Name;
1112 CS += "::";
1113 }
1114 }
1115 return CS;
Devang Patel0e821f42011-04-12 23:21:44 +00001116}
1117
1118/// constructTypeDIE - Construct basic type die from DIBasicType.
David Blaikie319a05f2013-12-02 19:33:10 +00001119void Unit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Devang Patel0e821f42011-04-12 23:21:44 +00001120 // Get core information.
1121 StringRef Name = BTy.getName();
Devang Patel0e821f42011-04-12 23:21:44 +00001122 // Add name if not anonymous or intermediate type.
1123 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001124 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel04d6d472011-09-14 23:13:28 +00001125
David Blaikiefac56122013-10-04 23:21:16 +00001126 // An unspecified type only has a name attribute.
1127 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type)
Devang Patel04d6d472011-09-14 23:13:28 +00001128 return;
Devang Patel04d6d472011-09-14 23:13:28 +00001129
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001130 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Pateld925d1a2012-02-07 23:33:58 +00001131 BTy.getEncoding());
Devang Patel04d6d472011-09-14 23:13:28 +00001132
Devang Patel0e821f42011-04-12 23:21:44 +00001133 uint64_t Size = BTy.getSizeInBits() >> 3;
David Blaikief2443192013-10-21 17:28:37 +00001134 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001135}
1136
1137/// constructTypeDIE - Construct derived type die from DIDerivedType.
David Blaikie319a05f2013-12-02 19:33:10 +00001138void Unit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Devang Patel0e821f42011-04-12 23:21:44 +00001139 // Get core information.
1140 StringRef Name = DTy.getName();
1141 uint64_t Size = DTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001142 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001143
1144 // Map to main type, void will not have a type.
Manman Ren93b30902013-10-08 18:42:58 +00001145 DIType FromTy = resolve(DTy.getTypeDerivedFrom());
Eric Christopher0df08e22013-08-08 07:40:37 +00001146 if (FromTy)
1147 addType(&Buffer, FromTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001148
1149 // Add name if not anonymous or intermediate type.
1150 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001151 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001152
1153 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher85757902012-02-21 22:25:53 +00001154 if (Size && Tag != dwarf::DW_TAG_pointer_type)
David Blaikief2443192013-10-21 17:28:37 +00001155 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001156
David Blaikie5d3249b2013-01-07 05:51:15 +00001157 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
Eric Christopherc2697f82013-10-19 01:04:47 +00001158 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
1159 getOrCreateTypeDIE(resolve(DTy.getClassType())));
Devang Patel0e821f42011-04-12 23:21:44 +00001160 // Add source line info if available and TyDesc is not a forward declaration.
1161 if (!DTy.isForwardDecl())
1162 addSourceLine(&Buffer, DTy);
1163}
1164
1165/// constructTypeDIE - Construct type DIE from DICompositeType.
David Blaikie319a05f2013-12-02 19:33:10 +00001166void Unit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
David Blaikie409dd9c2013-11-19 23:08:21 +00001167 // Add name if not anonymous or intermediate type.
Devang Patel0e821f42011-04-12 23:21:44 +00001168 StringRef Name = CTy.getName();
1169
1170 uint64_t Size = CTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001171 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001172
1173 switch (Tag) {
Devang Patel0e821f42011-04-12 23:21:44 +00001174 case dwarf::DW_TAG_array_type:
Eric Christopherdf9955d2013-11-11 18:52:31 +00001175 constructArrayTypeDIE(Buffer, CTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001176 break;
Eric Christopheraeb105f2013-11-11 18:52:39 +00001177 case dwarf::DW_TAG_enumeration_type:
1178 constructEnumTypeDIE(Buffer, CTy);
1179 break;
Devang Patel0e821f42011-04-12 23:21:44 +00001180 case dwarf::DW_TAG_subroutine_type: {
Eric Christopher0df08e22013-08-08 07:40:37 +00001181 // Add return type. A void return won't have a type.
Devang Patel0e821f42011-04-12 23:21:44 +00001182 DIArray Elements = CTy.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001183 DIType RTy(Elements.getElement(0));
Eric Christopher0df08e22013-08-08 07:40:37 +00001184 if (RTy)
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001185 addType(&Buffer, RTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001186
1187 bool isPrototyped = true;
1188 // Add arguments.
1189 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1190 DIDescriptor Ty = Elements.getElement(i);
1191 if (Ty.isUnspecifiedParameter()) {
Manman Renb987e512013-10-29 00:53:03 +00001192 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001193 isPrototyped = false;
1194 } else {
Manman Renb987e512013-10-29 00:53:03 +00001195 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001196 addType(Arg, DIType(Ty));
David Blaikie9a7a7a92013-01-29 19:35:24 +00001197 if (DIType(Ty).isArtificial())
1198 addFlag(Arg, dwarf::DW_AT_artificial);
Devang Patel0e821f42011-04-12 23:21:44 +00001199 }
1200 }
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001201 // Add prototype flag if we're dealing with a C language and the
1202 // function has been prototyped.
David Blaikiecb8e4352013-11-15 23:50:53 +00001203 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001204 if (isPrototyped &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001205 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopherd42b92f2012-05-22 18:45:24 +00001206 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001207 addFlag(&Buffer, dwarf::DW_AT_prototyped);
Eric Christopherc2697f82013-10-19 01:04:47 +00001208 } break;
Devang Patel0e821f42011-04-12 23:21:44 +00001209 case dwarf::DW_TAG_structure_type:
1210 case dwarf::DW_TAG_union_type:
1211 case dwarf::DW_TAG_class_type: {
Devang Patel0e821f42011-04-12 23:21:44 +00001212 // Add elements to structure type.
David Blaikiea1ae0e62013-08-01 20:30:22 +00001213 DIArray Elements = CTy.getTypeArray();
1214 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel0e821f42011-04-12 23:21:44 +00001215 DIDescriptor Element = Elements.getElement(i);
1216 DIE *ElemDie = NULL;
1217 if (Element.isSubprogram()) {
1218 DISubprogram SP(Element);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001219 ElemDie = getOrCreateSubprogramDIE(SP);
Devang Patel0e821f42011-04-12 23:21:44 +00001220 if (SP.isProtected())
Nick Lewyckycb918492011-12-13 05:09:11 +00001221 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001222 dwarf::DW_ACCESS_protected);
1223 else if (SP.isPrivate())
Nick Lewyckycb918492011-12-13 05:09:11 +00001224 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001225 dwarf::DW_ACCESS_private);
Eric Christopher92331fd2012-11-21 00:34:38 +00001226 else
Nick Lewyckycb918492011-12-13 05:09:11 +00001227 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Eric Christopherc2697f82013-10-19 01:04:47 +00001228 dwarf::DW_ACCESS_public);
Devang Patel0e821f42011-04-12 23:21:44 +00001229 if (SP.isExplicit())
Eric Christopherbb69a272012-08-24 01:14:27 +00001230 addFlag(ElemDie, dwarf::DW_AT_explicit);
Eric Christopher7285c7d2012-03-28 07:34:31 +00001231 } else if (Element.isDerivedType()) {
Eric Christopherd42b92f2012-05-22 18:45:24 +00001232 DIDerivedType DDTy(Element);
1233 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
Manman Renb987e512013-10-29 00:53:03 +00001234 ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
Manman Ren93b30902013-10-08 18:42:58 +00001235 addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()),
Manman Renb3388602013-10-05 01:43:03 +00001236 dwarf::DW_AT_friend);
Manman Renc6b63922013-10-14 20:33:57 +00001237 } else if (DDTy.isStaticMember()) {
Manman Ren57e6ff72013-10-23 22:57:12 +00001238 getOrCreateStaticMemberDIE(DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001239 } else {
Manman Ren230ec862013-10-23 23:00:44 +00001240 constructMemberDIE(Buffer, DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001241 }
Eric Christopher7285c7d2012-03-28 07:34:31 +00001242 } else if (Element.isObjCProperty()) {
Devang Pateld925d1a2012-02-07 23:33:58 +00001243 DIObjCProperty Property(Element);
Manman Renb987e512013-10-29 00:53:03 +00001244 ElemDie = createAndAddDIE(Property.getTag(), Buffer);
Devang Pateld925d1a2012-02-07 23:33:58 +00001245 StringRef PropertyName = Property.getObjCPropertyName();
1246 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Eric Christopherd42b92f2012-05-22 18:45:24 +00001247 addType(ElemDie, Property.getType());
1248 addSourceLine(ElemDie, Property);
Devang Pateld925d1a2012-02-07 23:33:58 +00001249 StringRef GetterName = Property.getObjCPropertyGetterName();
1250 if (!GetterName.empty())
1251 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
1252 StringRef SetterName = Property.getObjCPropertySetterName();
1253 if (!SetterName.empty())
1254 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
1255 unsigned PropertyAttributes = 0;
Devang Patel403e8192012-02-04 01:30:32 +00001256 if (Property.isReadOnlyObjCProperty())
1257 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1258 if (Property.isReadWriteObjCProperty())
1259 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1260 if (Property.isAssignObjCProperty())
1261 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1262 if (Property.isRetainObjCProperty())
1263 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1264 if (Property.isCopyObjCProperty())
1265 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1266 if (Property.isNonAtomicObjCProperty())
1267 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1268 if (PropertyAttributes)
David Blaikief2443192013-10-21 17:28:37 +00001269 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None,
Eric Christopherc2697f82013-10-19 01:04:47 +00001270 PropertyAttributes);
Devang Patel44882172012-02-06 17:49:43 +00001271
Devang Pateld925d1a2012-02-07 23:33:58 +00001272 DIEEntry *Entry = getDIEEntry(Element);
1273 if (!Entry) {
1274 Entry = createDIEEntry(ElemDie);
1275 insertDIEEntry(Element, Entry);
1276 }
Devang Patel403e8192012-02-04 01:30:32 +00001277 } else
Devang Patel0e821f42011-04-12 23:21:44 +00001278 continue;
Devang Patel0e821f42011-04-12 23:21:44 +00001279 }
1280
1281 if (CTy.isAppleBlockExtension())
Eric Christopherbb69a272012-08-24 01:14:27 +00001282 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel0e821f42011-04-12 23:21:44 +00001283
Eric Christopher9e429ae2013-10-05 00:27:02 +00001284 DICompositeType ContainingType(resolve(CTy.getContainingType()));
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001285 if (ContainingType)
Manman Ren4c4b69c2013-10-11 23:58:05 +00001286 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001287 getOrCreateTypeDIE(ContainingType));
Devang Patel0e821f42011-04-12 23:21:44 +00001288
Devang Patel12419ae2011-05-12 21:29:42 +00001289 if (CTy.isObjcClassComplete())
Eric Christopherbb69a272012-08-24 01:14:27 +00001290 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patel2409e782011-05-12 19:06:16 +00001291
Eric Christopherda011dd2011-12-16 23:42:42 +00001292 // Add template parameters to a class, structure or union types.
1293 // FIXME: The support isn't in the metadata for this yet.
1294 if (Tag == dwarf::DW_TAG_class_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001295 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
Devang Patel0e821f42011-04-12 23:21:44 +00001296 addTemplateParams(Buffer, CTy.getTemplateParams());
1297
1298 break;
1299 }
1300 default:
1301 break;
1302 }
1303
1304 // Add name if not anonymous or intermediate type.
1305 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001306 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001307
Eric Christopher775cbd22012-05-22 18:45:18 +00001308 if (Tag == dwarf::DW_TAG_enumeration_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001309 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
Eric Christopher775cbd22012-05-22 18:45:18 +00001310 Tag == dwarf::DW_TAG_union_type) {
Devang Patel0e821f42011-04-12 23:21:44 +00001311 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher1cf33382012-06-01 00:22:32 +00001312 // TODO: Do we care about size for enum forward declarations?
Devang Patel0e821f42011-04-12 23:21:44 +00001313 if (Size)
David Blaikief2443192013-10-21 17:28:37 +00001314 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Eric Christopher1cf33382012-06-01 00:22:32 +00001315 else if (!CTy.isForwardDecl())
Devang Patel0e821f42011-04-12 23:21:44 +00001316 // Add zero size if it is not a forward declaration.
David Blaikief2443192013-10-21 17:28:37 +00001317 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, 0);
Eric Christopher1cf33382012-06-01 00:22:32 +00001318
1319 // If we're a forward decl, say so.
1320 if (CTy.isForwardDecl())
Eric Christopherbb69a272012-08-24 01:14:27 +00001321 addFlag(&Buffer, dwarf::DW_AT_declaration);
Devang Patel0e821f42011-04-12 23:21:44 +00001322
1323 // Add source line info if available.
1324 if (!CTy.isForwardDecl())
1325 addSourceLine(&Buffer, CTy);
Eric Christopher54cf8ff2012-03-07 00:15:19 +00001326
1327 // No harm in adding the runtime language to the declaration.
1328 unsigned RLang = CTy.getRunTimeLang();
1329 if (RLang)
Eric Christopherc2697f82013-10-19 01:04:47 +00001330 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
1331 RLang);
Devang Patel0e821f42011-04-12 23:21:44 +00001332 }
1333}
1334
Manman Renffc9a712013-10-23 23:05:28 +00001335/// constructTemplateTypeParameterDIE - Construct new DIE for the given
1336/// DITemplateTypeParameter.
David Blaikie319a05f2013-12-02 19:33:10 +00001337void Unit::constructTemplateTypeParameterDIE(DIE &Buffer,
1338 DITemplateTypeParameter TP) {
Manman Renb987e512013-10-29 00:53:03 +00001339 DIE *ParamDIE =
1340 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
Eric Christopher056b6472013-08-08 08:09:43 +00001341 // Add the type if it exists, it could be void and therefore no type.
1342 if (TP.getType())
Manman Ren88b0f942013-10-09 19:46:28 +00001343 addType(ParamDIE, resolve(TP.getType()));
David Blaikie2b380232013-06-22 18:59:11 +00001344 if (!TP.getName().empty())
1345 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel0e821f42011-04-12 23:21:44 +00001346}
1347
Manman Renffc9a712013-10-23 23:05:28 +00001348/// constructTemplateValueParameterDIE - Construct new DIE for the given
1349/// DITemplateValueParameter.
David Blaikie319a05f2013-12-02 19:33:10 +00001350void Unit::constructTemplateValueParameterDIE(DIE &Buffer,
1351 DITemplateValueParameter VP) {
Manman Renb987e512013-10-29 00:53:03 +00001352 DIE *ParamDIE = createAndAddDIE(VP.getTag(), Buffer);
Eric Christopher0df08e22013-08-08 07:40:37 +00001353
1354 // Add the type if there is one, template template and template parameter
1355 // packs will not have a type.
Eric Christopher691281b2013-10-21 17:48:51 +00001356 if (VP.getTag() == dwarf::DW_TAG_template_value_parameter)
Manman Ren88b0f942013-10-09 19:46:28 +00001357 addType(ParamDIE, resolve(VP.getType()));
Eric Christopherafb2c412013-08-08 07:40:31 +00001358 if (!VP.getName().empty())
1359 addString(ParamDIE, dwarf::DW_AT_name, VP.getName());
1360 if (Value *Val = VP.getValue()) {
David Blaikiea1e813d2013-05-10 21:52:07 +00001361 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val))
Manman Ren88b0f942013-10-09 19:46:28 +00001362 addConstantValue(ParamDIE, CI,
1363 isUnsignedDIType(DD, resolve(VP.getType())));
David Blaikiea1e813d2013-05-10 21:52:07 +00001364 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) {
1365 // For declaration non-type template parameters (such as global values and
1366 // functions)
1367 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Rafael Espindola79858aa2013-10-29 17:07:16 +00001368 addOpAddress(Block, Asm->getSymbol(GV));
David Blaikiea1e813d2013-05-10 21:52:07 +00001369 // Emit DW_OP_stack_value to use the address as the immediate value of the
1370 // parameter, rather than a pointer to it.
David Blaikief2443192013-10-21 17:28:37 +00001371 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1372 addBlock(ParamDIE, dwarf::DW_AT_location, Block);
Eric Christopherafb2c412013-08-08 07:40:31 +00001373 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) {
David Blaikie2b380232013-06-22 18:59:11 +00001374 assert(isa<MDString>(Val));
1375 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1376 cast<MDString>(Val)->getString());
Eric Christopherafb2c412013-08-08 07:40:31 +00001377 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
David Blaikie2b380232013-06-22 18:59:11 +00001378 assert(isa<MDNode>(Val));
1379 DIArray A(cast<MDNode>(Val));
1380 addTemplateParams(*ParamDIE, A);
David Blaikiea1e813d2013-05-10 21:52:07 +00001381 }
1382 }
Devang Patel0e821f42011-04-12 23:21:44 +00001383}
1384
Devang Patel17b53272011-05-06 16:57:54 +00001385/// getOrCreateNameSpace - Create a DIE for DINameSpace.
David Blaikie319a05f2013-12-02 19:33:10 +00001386DIE *Unit::getOrCreateNameSpace(DINameSpace NS) {
Manman Renf6b936b2013-10-29 05:49:41 +00001387 // Construct the context before querying for the existence of the DIE in case
1388 // such construction creates the DIE.
1389 DIE *ContextDIE = getOrCreateContextDIE(NS.getContext());
Manman Renf6b936b2013-10-29 05:49:41 +00001390
Devang Patel17b53272011-05-06 16:57:54 +00001391 DIE *NDie = getDIE(NS);
1392 if (NDie)
1393 return NDie;
Manman Renf6b936b2013-10-29 05:49:41 +00001394 NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
1395
Eric Christopher4996c702011-11-07 09:24:32 +00001396 if (!NS.getName().empty()) {
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001397 addString(NDie, dwarf::DW_AT_name, NS.getName());
Eric Christopher4996c702011-11-07 09:24:32 +00001398 addAccelNamespace(NS.getName(), NDie);
Eric Christopher2c8b7902013-10-17 02:06:06 +00001399 addGlobalName(NS.getName(), NDie, NS.getContext());
Eric Christopher4996c702011-11-07 09:24:32 +00001400 } else
1401 addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel17b53272011-05-06 16:57:54 +00001402 addSourceLine(NDie, NS);
Devang Patel17b53272011-05-06 16:57:54 +00001403 return NDie;
1404}
1405
Devang Patel89543712011-08-15 17:24:54 +00001406/// getOrCreateSubprogramDIE - Create new DIE using SP.
David Blaikie319a05f2013-12-02 19:33:10 +00001407DIE *Unit::getOrCreateSubprogramDIE(DISubprogram SP) {
David Blaikie309ffe42013-10-04 01:39:59 +00001408 // Construct the context before querying for the existence of the DIE in case
1409 // such construction creates the DIE (as is the case for member function
1410 // declarations).
Manman Renc50fa112013-10-10 18:40:01 +00001411 DIE *ContextDIE = getOrCreateContextDIE(resolve(SP.getContext()));
David Blaikie309ffe42013-10-04 01:39:59 +00001412
Eric Christophere595bae2013-10-04 17:08:38 +00001413 DIE *SPDie = getDIE(SP);
Devang Patel89543712011-08-15 17:24:54 +00001414 if (SPDie)
1415 return SPDie;
1416
Manman Ren73d697c2013-10-29 00:58:04 +00001417 DISubprogram SPDecl = SP.getFunctionDeclaration();
1418 if (SPDecl.isSubprogram())
1419 // Add subprogram definitions to the CU die directly.
David Blaikie2a80e442013-12-02 22:09:48 +00001420 ContextDIE = UnitDie.get();
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001421
1422 // DW_TAG_inlined_subroutine may refer to this DIE.
Manman Ren73d697c2013-10-29 00:58:04 +00001423 SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001424
Rafael Espindola79278362011-11-10 22:34:29 +00001425 DIE *DeclDie = NULL;
Manman Renb9512a72013-10-23 22:12:26 +00001426 if (SPDecl.isSubprogram())
Rafael Espindola79278362011-11-10 22:34:29 +00001427 DeclDie = getOrCreateSubprogramDIE(SPDecl);
Rafael Espindola79278362011-11-10 22:34:29 +00001428
Devang Patel89543712011-08-15 17:24:54 +00001429 // Add function template parameters.
1430 addTemplateParams(*SPDie, SP.getTemplateParams());
1431
Devang Patel89543712011-08-15 17:24:54 +00001432 // If this DIE is going to refer declaration info using AT_specification
Eric Christopherf20ff972013-05-09 00:42:33 +00001433 // then there is no need to add other attributes.
Rafael Espindola79278362011-11-10 22:34:29 +00001434 if (DeclDie) {
1435 // Refer function declaration directly.
Manman Ren4c4b69c2013-10-11 23:58:05 +00001436 addDIEEntry(SPDie, dwarf::DW_AT_specification, DeclDie);
Rafael Espindola79278362011-11-10 22:34:29 +00001437
Devang Patel89543712011-08-15 17:24:54 +00001438 return SPDie;
Rafael Espindola79278362011-11-10 22:34:29 +00001439 }
Devang Patel89543712011-08-15 17:24:54 +00001440
Eric Christopheracb71152012-08-23 22:52:55 +00001441 // Add the linkage name if we have one.
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001442 StringRef LinkageName = SP.getLinkageName();
1443 if (!LinkageName.empty())
Eric Christopheracb71152012-08-23 22:52:55 +00001444 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001445 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopheracb71152012-08-23 22:52:55 +00001446
Devang Patel89543712011-08-15 17:24:54 +00001447 // Constructors and operators for anonymous aggregates do not have names.
1448 if (!SP.getName().empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001449 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Patel89543712011-08-15 17:24:54 +00001450
1451 addSourceLine(SPDie, SP);
1452
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001453 // Add the prototype if we have a prototype and we have a C like
1454 // language.
David Blaikiecb8e4352013-11-15 23:50:53 +00001455 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001456 if (SP.isPrototyped() &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001457 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001458 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001459 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Patel89543712011-08-15 17:24:54 +00001460
Devang Patel89543712011-08-15 17:24:54 +00001461 DICompositeType SPTy = SP.getType();
David Blaikie5174c842013-05-22 23:22:18 +00001462 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type &&
1463 "the type of a subprogram should be a subroutine");
Devang Patel89543712011-08-15 17:24:54 +00001464
David Blaikie5174c842013-05-22 23:22:18 +00001465 DIArray Args = SPTy.getTypeArray();
Eric Christopher691281b2013-10-21 17:48:51 +00001466 // Add a return type. If this is a type like a C/C++ void type we don't add a
1467 // return type.
Eric Christopher0df08e22013-08-08 07:40:37 +00001468 if (Args.getElement(0))
1469 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel89543712011-08-15 17:24:54 +00001470
1471 unsigned VK = SP.getVirtuality();
1472 if (VK) {
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001473 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Devang Patel89543712011-08-15 17:24:54 +00001474 DIEBlock *Block = getDIEBlock();
David Blaikief2443192013-10-21 17:28:37 +00001475 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1476 addUInt(Block, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1477 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
Eric Christopher98b7f172013-11-11 18:52:36 +00001478 ContainingTypeMap.insert(
1479 std::make_pair(SPDie, resolve(SP.getContainingType())));
Devang Patel89543712011-08-15 17:24:54 +00001480 }
1481
1482 if (!SP.isDefinition()) {
Eric Christopherbb69a272012-08-24 01:14:27 +00001483 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher92331fd2012-11-21 00:34:38 +00001484
Devang Patel89543712011-08-15 17:24:54 +00001485 // Add arguments. Do not add arguments for subprogram definition. They will
1486 // be handled while processing variables.
Eric Christopherc2697f82013-10-19 01:04:47 +00001487 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
Manman Renb987e512013-10-29 00:53:03 +00001488 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, *SPDie);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001489 DIType ATy(Args.getElement(i));
David Blaikie5174c842013-05-22 23:22:18 +00001490 addType(Arg, ATy);
1491 if (ATy.isArtificial())
1492 addFlag(Arg, dwarf::DW_AT_artificial);
David Blaikie5174c842013-05-22 23:22:18 +00001493 }
Devang Patel89543712011-08-15 17:24:54 +00001494 }
1495
1496 if (SP.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001497 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Patel89543712011-08-15 17:24:54 +00001498
1499 if (!SP.isLocalToUnit())
Eric Christopherbb69a272012-08-24 01:14:27 +00001500 addFlag(SPDie, dwarf::DW_AT_external);
Devang Patel89543712011-08-15 17:24:54 +00001501
1502 if (SP.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +00001503 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Patel89543712011-08-15 17:24:54 +00001504
1505 if (unsigned isa = Asm->getISAEncoding()) {
1506 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1507 }
1508
1509 return SPDie;
1510}
1511
Devang Pateldfd6ec32011-08-15 17:57:41 +00001512// Return const expression if value is a GEP to access merged global
1513// constant. e.g.
1514// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1515static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1516 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1517 if (!CE || CE->getNumOperands() != 3 ||
1518 CE->getOpcode() != Instruction::GetElementPtr)
1519 return NULL;
1520
1521 // First operand points to a global struct.
1522 Value *Ptr = CE->getOperand(0);
1523 if (!isa<GlobalValue>(Ptr) ||
1524 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1525 return NULL;
1526
1527 // Second operand is zero.
1528 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1529 if (!CI || !CI->isZero())
1530 return NULL;
1531
1532 // Third operand is offset.
1533 if (!isa<ConstantInt>(CE->getOperand(2)))
1534 return NULL;
1535
1536 return CE;
1537}
1538
1539/// createGlobalVariableDIE - create global variable DIE.
David Blaikiea781b25b2013-11-17 21:55:13 +00001540void CompileUnit::createGlobalVariableDIE(DIGlobalVariable GV) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001541 // Check for pre-existence.
David Blaikie2ad00162013-11-15 23:09:13 +00001542 if (getDIE(GV))
Devang Pateldfd6ec32011-08-15 17:57:41 +00001543 return;
1544
Manman Ren7504ed42013-07-08 18:33:29 +00001545 if (!GV.isGlobalVariable())
Devang Patel0ecbcbd2011-08-18 23:17:55 +00001546 return;
1547
Eric Christopher08f7c8f2013-10-04 23:49:26 +00001548 DIScope GVContext = GV.getContext();
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001549 DIType GTy = GV.getType();
1550
1551 // If this is a static data member definition, some attributes belong
1552 // to the declaration DIE.
1553 DIE *VariableDIE = NULL;
Manman Rene697d3c2013-02-01 23:54:37 +00001554 bool IsStaticMember = false;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001555 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1556 if (SDMDecl.Verify()) {
1557 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1558 // We need the declaration DIE that is in the static member's class.
Manman Renc6b63922013-10-14 20:33:57 +00001559 VariableDIE = getOrCreateStaticMemberDIE(SDMDecl);
Manman Rene697d3c2013-02-01 23:54:37 +00001560 IsStaticMember = true;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001561 }
1562
1563 // If this is not a static data member definition, create the variable
1564 // DIE and add the initial set of attributes to it.
1565 if (!VariableDIE) {
Manman Renf6b936b2013-10-29 05:49:41 +00001566 // Construct the context before querying for the existence of the DIE in
1567 // case such construction creates the DIE.
1568 DIE *ContextDIE = getOrCreateContextDIE(GVContext);
Manman Renf6b936b2013-10-29 05:49:41 +00001569
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001570 // Add to map.
David Blaikie52c50202013-11-16 00:29:01 +00001571 VariableDIE = createAndAddDIE(GV.getTag(), *ContextDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001572
1573 // Add name and type.
1574 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1575 addType(VariableDIE, GTy);
1576
1577 // Add scoping info.
Eric Christopherd2b497b2013-10-16 01:37:49 +00001578 if (!GV.isLocalToUnit())
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001579 addFlag(VariableDIE, dwarf::DW_AT_external);
1580
1581 // Add line number info.
1582 addSourceLine(VariableDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001583 }
1584
Devang Pateldfd6ec32011-08-15 17:57:41 +00001585 // Add location.
Eric Christopher4996c702011-11-07 09:24:32 +00001586 bool addToAccelTable = false;
Eric Christopher0a917b72011-11-11 03:16:32 +00001587 DIE *VariableSpecDIE = NULL;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001588 bool isGlobalVariable = GV.getGlobal() != NULL;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001589 if (isGlobalVariable) {
Eric Christopher4996c702011-11-07 09:24:32 +00001590 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001591 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Rafael Espindola79858aa2013-10-29 17:07:16 +00001592 const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal());
David Blaikief2694972013-06-28 20:05:11 +00001593 if (GV.getGlobal()->isThreadLocal()) {
1594 // FIXME: Make this work with -gsplit-dwarf.
1595 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
1596 assert((PointerSize == 4 || PointerSize == 8) &&
1597 "Add support for other sizes if necessary");
Ulrich Weigand2b6fc8d2013-07-02 18:47:09 +00001598 const MCExpr *Expr =
David Blaikie8466ca82013-07-01 23:55:52 +00001599 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym);
David Blaikief2694972013-06-28 20:05:11 +00001600 // Based on GCC's support for TLS:
David Blaikie8466ca82013-07-01 23:55:52 +00001601 if (!DD->useSplitDwarf()) {
1602 // 1) Start with a constNu of the appropriate pointer size
David Blaikief2443192013-10-21 17:28:37 +00001603 addUInt(Block, dwarf::DW_FORM_data1,
David Blaikie8466ca82013-07-01 23:55:52 +00001604 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
Richard Mitton0aafb582013-10-07 18:39:18 +00001605 // 2) containing the (relocated) offset of the TLS variable
1606 // within the module's TLS block.
David Blaikief2443192013-10-21 17:28:37 +00001607 addExpr(Block, dwarf::DW_FORM_udata, Expr);
David Blaikie8466ca82013-07-01 23:55:52 +00001608 } else {
David Blaikief2443192013-10-21 17:28:37 +00001609 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
1610 addUInt(Block, dwarf::DW_FORM_udata, DU->getAddrPoolIndex(Expr));
David Blaikie8466ca82013-07-01 23:55:52 +00001611 }
Richard Mitton0aafb582013-10-07 18:39:18 +00001612 // 3) followed by a custom OP to make the debugger do a TLS lookup.
David Blaikief2443192013-10-21 17:28:37 +00001613 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001614 } else {
1615 DD->addArangeLabel(SymbolCU(this, Sym));
David Blaikief2694972013-06-28 20:05:11 +00001616 addOpAddress(Block, Sym);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001617 }
Devang Pateldfd6ec32011-08-15 17:57:41 +00001618 // Do not create specification DIE if context is either compile unit
1619 // or a subprogram.
Devang Patel5e6b65c2011-09-21 23:41:11 +00001620 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Manman Ren3eb9dff2013-09-09 19:05:21 +00001621 !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001622 // Create specification DIE.
David Blaikie2a80e442013-12-02 22:09:48 +00001623 VariableSpecDIE = createAndAddDIE(dwarf::DW_TAG_variable, *UnitDie);
Manman Ren4c4b69c2013-10-11 23:58:05 +00001624 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification, VariableDIE);
David Blaikief2443192013-10-21 17:28:37 +00001625 addBlock(VariableSpecDIE, dwarf::DW_AT_location, Block);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001626 // A static member's declaration is already flagged as such.
1627 if (!SDMDecl.Verify())
1628 addFlag(VariableDIE, dwarf::DW_AT_declaration);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001629 } else {
David Blaikief2443192013-10-21 17:28:37 +00001630 addBlock(VariableDIE, dwarf::DW_AT_location, Block);
Eric Christopher4996c702011-11-07 09:24:32 +00001631 }
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001632 // Add the linkage name.
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001633 StringRef LinkageName = GV.getLinkageName();
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001634 if (!LinkageName.empty())
Eric Christopher3f79b8c2013-02-27 23:49:47 +00001635 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1636 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1637 // TAG_variable.
Eric Christopherc2697f82013-10-19 01:04:47 +00001638 addString(IsStaticMember && VariableSpecDIE ? VariableSpecDIE
1639 : VariableDIE,
1640 dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001641 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopher92331fd2012-11-21 00:34:38 +00001642 } else if (const ConstantInt *CI =
Eric Christopherc2697f82013-10-19 01:04:47 +00001643 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
Adrian Prantl322f41d2013-04-04 22:56:49 +00001644 // AT_const_value was added when the static member was created. To avoid
Manman Rene697d3c2013-02-01 23:54:37 +00001645 // emitting AT_const_value multiple times, we only add AT_const_value when
1646 // it is not a static member.
1647 if (!IsStaticMember)
Manman Renb3388602013-10-05 01:43:03 +00001648 addConstantValue(VariableDIE, CI, isUnsignedDIType(DD, GTy));
David Blaikiea781b25b2013-11-17 21:55:13 +00001649 } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getOperand(11))) {
Eric Christopher4996c702011-11-07 09:24:32 +00001650 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001651 // GV is a merged global.
1652 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1653 Value *Ptr = CE->getOperand(0);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001654 MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr));
1655 DD->addArangeLabel(SymbolCU(this, Sym));
1656 addOpAddress(Block, Sym);
David Blaikief2443192013-10-21 17:28:37 +00001657 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
Eric Christopherc2697f82013-10-19 01:04:47 +00001658 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
David Blaikief2443192013-10-21 17:28:37 +00001659 addUInt(Block, dwarf::DW_FORM_udata,
Eric Christopherc2697f82013-10-19 01:04:47 +00001660 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
David Blaikief2443192013-10-21 17:28:37 +00001661 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1662 addBlock(VariableDIE, dwarf::DW_AT_location, Block);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001663 }
1664
Eric Christopherc12c2112011-11-11 01:55:22 +00001665 if (addToAccelTable) {
1666 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1667 addAccelName(GV.getName(), AddrDIE);
Eric Christopher4996c702011-11-07 09:24:32 +00001668
Eric Christopherc12c2112011-11-11 01:55:22 +00001669 // If the linkage name is different than the name, go ahead and output
1670 // that as well into the name table.
1671 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1672 addAccelName(GV.getLinkageName(), AddrDIE);
1673 }
Eric Christopherd2b497b2013-10-16 01:37:49 +00001674
1675 if (!GV.isLocalToUnit())
Eric Christopher2c8b7902013-10-17 02:06:06 +00001676 addGlobalName(GV.getName(), VariableSpecDIE ? VariableSpecDIE : VariableDIE,
1677 GV.getContext());
Devang Pateldfd6ec32011-08-15 17:57:41 +00001678}
1679
Devang Patel0e821f42011-04-12 23:21:44 +00001680/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
David Blaikie319a05f2013-12-02 19:33:10 +00001681void Unit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Manman Renb987e512013-10-29 00:53:03 +00001682 DIE *DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
Manman Ren4c4b69c2013-10-11 23:58:05 +00001683 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, IndexTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001684
Bill Wendling28fe9e72012-12-06 07:38:10 +00001685 // The LowerBound value defines the lower bounds which is typically zero for
1686 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1687 // Count == -1 then the array is unbounded and we do not emit
1688 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1689 // Count == 0, then the array has zero elements in which case we do not emit
1690 // an upper bound.
1691 int64_t LowerBound = SR.getLo();
Bill Wendling3495f9b2012-12-06 07:55:19 +00001692 int64_t DefaultLowerBound = getDefaultLowerBound();
Bill Wendlingd7767122012-12-04 21:34:03 +00001693 int64_t Count = SR.getCount();
Devang Patel0e821f42011-04-12 23:21:44 +00001694
Bill Wendling3495f9b2012-12-06 07:55:19 +00001695 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
David Blaikief2443192013-10-21 17:28:37 +00001696 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound);
Bill Wendling28fe9e72012-12-06 07:38:10 +00001697
1698 if (Count != -1 && Count != 0)
Bill Wendlingd7767122012-12-04 21:34:03 +00001699 // FIXME: An unbounded array should reference the expression that defines
1700 // the array.
Eric Christopher98b7f172013-11-11 18:52:36 +00001701 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None,
1702 LowerBound + Count - 1);
Devang Patel0e821f42011-04-12 23:21:44 +00001703}
1704
1705/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
David Blaikie319a05f2013-12-02 19:33:10 +00001706void Unit::constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopherdf9955d2013-11-11 18:52:31 +00001707 if (CTy.isVector())
Eric Christopherbb69a272012-08-24 01:14:27 +00001708 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel0e821f42011-04-12 23:21:44 +00001709
Eric Christopher0df08e22013-08-08 07:40:37 +00001710 // Emit the element type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001711 addType(&Buffer, resolve(CTy.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001712
1713 // Get an anonymous type for index type.
Eric Christophercad9b532013-01-04 21:51:53 +00001714 // FIXME: This type should be passed down from the front end
1715 // as different languages may have different sizes for indexes.
Devang Patel0e821f42011-04-12 23:21:44 +00001716 DIE *IdxTy = getIndexTyDie();
1717 if (!IdxTy) {
1718 // Construct an anonymous type for index type.
David Blaikie2a80e442013-12-02 22:09:48 +00001719 IdxTy = createAndAddDIE(dwarf::DW_TAG_base_type, *UnitDie);
Eric Christophercad9b532013-01-04 21:51:53 +00001720 addString(IdxTy, dwarf::DW_AT_name, "int");
David Blaikief2443192013-10-21 17:28:37 +00001721 addUInt(IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int32_t));
Devang Patel0e821f42011-04-12 23:21:44 +00001722 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1723 dwarf::DW_ATE_signed);
Devang Patel0e821f42011-04-12 23:21:44 +00001724 setIndexTyDie(IdxTy);
1725 }
1726
1727 // Add subranges to array type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001728 DIArray Elements = CTy.getTypeArray();
Devang Patel0e821f42011-04-12 23:21:44 +00001729 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1730 DIDescriptor Element = Elements.getElement(i);
1731 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1732 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1733 }
1734}
1735
Eric Christopheraeb105f2013-11-11 18:52:39 +00001736/// constructEnumTypeDIE - Construct an enum type DIE from DICompositeType.
David Blaikie319a05f2013-12-02 19:33:10 +00001737void Unit::constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopheraeb105f2013-11-11 18:52:39 +00001738 DIArray Elements = CTy.getTypeArray();
1739
1740 // Add enumerators to enumeration type.
1741 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001742 DIEnumerator Enum(Elements.getElement(i));
Eric Christopheraeb105f2013-11-11 18:52:39 +00001743 if (Enum.isEnumerator()) {
1744 DIE *Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001745 StringRef Name = Enum.getName();
Eric Christopheraeb105f2013-11-11 18:52:39 +00001746 addString(Enumerator, dwarf::DW_AT_name, Name);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001747 int64_t Value = Enum.getEnumValue();
Eric Christophera07e4f52013-11-19 09:28:34 +00001748 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
1749 Value);
Eric Christopheraeb105f2013-11-11 18:52:39 +00001750 }
1751 }
1752 DIType DTy = resolve(CTy.getTypeDerivedFrom());
1753 if (DTy) {
1754 addType(&Buffer, DTy);
1755 addFlag(&Buffer, dwarf::DW_AT_enum_class);
1756 }
Devang Patel0e821f42011-04-12 23:21:44 +00001757}
1758
Devang Patel89543712011-08-15 17:24:54 +00001759/// constructContainingTypeDIEs - Construct DIEs for types that contain
1760/// vtables.
David Blaikie319a05f2013-12-02 19:33:10 +00001761void Unit::constructContainingTypeDIEs() {
Devang Patel89543712011-08-15 17:24:54 +00001762 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Eric Christopherc2697f82013-10-19 01:04:47 +00001763 CE = ContainingTypeMap.end();
1764 CI != CE; ++CI) {
Devang Patel89543712011-08-15 17:24:54 +00001765 DIE *SPDie = CI->first;
David Blaikie2ad00162013-11-15 23:09:13 +00001766 DIDescriptor D(CI->second);
1767 if (!D)
Eric Christopherc2697f82013-10-19 01:04:47 +00001768 continue;
David Blaikie2ad00162013-11-15 23:09:13 +00001769 DIE *NDie = getDIE(D);
Eric Christopherc2697f82013-10-19 01:04:47 +00001770 if (!NDie)
1771 continue;
Manman Ren4c4b69c2013-10-11 23:58:05 +00001772 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, NDie);
Devang Patel89543712011-08-15 17:24:54 +00001773 }
1774}
1775
Devang Patel3acc70e2011-08-15 22:04:40 +00001776/// constructVariableDIE - Construct a DIE for the given DbgVariable.
David Blaikie319a05f2013-12-02 19:33:10 +00001777DIE *Unit::constructVariableDIE(DbgVariable &DV, bool isScopeAbstract) {
Eric Christopher34a2c872013-11-15 01:43:19 +00001778 StringRef Name = DV.getName();
Devang Patel3acc70e2011-08-15 22:04:40 +00001779
Devang Patel3acc70e2011-08-15 22:04:40 +00001780 // Define variable debug information entry.
Eric Christopher34a2c872013-11-15 01:43:19 +00001781 DIE *VariableDie = new DIE(DV.getTag());
1782 DbgVariable *AbsVar = DV.getAbstractVariable();
Devang Patel3acc70e2011-08-15 22:04:40 +00001783 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
Manman Ren4213c392013-05-29 17:16:59 +00001784 if (AbsDIE)
Manman Ren4c4b69c2013-10-11 23:58:05 +00001785 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin, AbsDIE);
Devang Patel3acc70e2011-08-15 22:04:40 +00001786 else {
David Blaikie715528b2013-08-19 03:34:03 +00001787 if (!Name.empty())
1788 addString(VariableDie, dwarf::DW_AT_name, Name);
Eric Christopher34a2c872013-11-15 01:43:19 +00001789 addSourceLine(VariableDie, DV.getVariable());
1790 addType(VariableDie, DV.getType());
Devang Patel3acc70e2011-08-15 22:04:40 +00001791 }
1792
Eric Christopher34a2c872013-11-15 01:43:19 +00001793 if (DV.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001794 addFlag(VariableDie, dwarf::DW_AT_artificial);
Devang Patel3acc70e2011-08-15 22:04:40 +00001795
1796 if (isScopeAbstract) {
Eric Christopher34a2c872013-11-15 01:43:19 +00001797 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001798 return VariableDie;
1799 }
1800
1801 // Add variable address.
1802
Eric Christopher34a2c872013-11-15 01:43:19 +00001803 unsigned Offset = DV.getDotDebugLocOffset();
Devang Patel3acc70e2011-08-15 22:04:40 +00001804 if (Offset != ~0U) {
Eric Christopher33ff6972013-11-21 23:46:41 +00001805 addSectionLabel(VariableDie, dwarf::DW_AT_location,
1806 Asm->GetTempSymbol("debug_loc", Offset));
Eric Christopher34a2c872013-11-15 01:43:19 +00001807 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001808 return VariableDie;
1809 }
1810
Eric Christophercead0332011-10-03 15:49:20 +00001811 // Check if variable is described by a DBG_VALUE instruction.
Eric Christopher34a2c872013-11-15 01:43:19 +00001812 if (const MachineInstr *DVInsn = DV.getMInsn()) {
David Blaikie0252265b2013-06-16 20:34:15 +00001813 assert(DVInsn->getNumOperands() == 3);
1814 if (DVInsn->getOperand(0).isReg()) {
1815 const MachineOperand RegOp = DVInsn->getOperand(0);
Adrian Prantl19942882013-07-09 21:44:06 +00001816 // If the second operand is an immediate, this is an indirect value.
Adrian Prantl418d1d12013-07-09 20:28:37 +00001817 if (DVInsn->getOperand(1).isImm()) {
Eric Christopherc2697f82013-10-19 01:04:47 +00001818 MachineLocation Location(RegOp.getReg(),
1819 DVInsn->getOperand(1).getImm());
Eric Christopher34a2c872013-11-15 01:43:19 +00001820 addVariableAddress(DV, VariableDie, Location);
David Blaikie0252265b2013-06-16 20:34:15 +00001821 } else if (RegOp.getReg())
Eric Christopher34a2c872013-11-15 01:43:19 +00001822 addVariableAddress(DV, VariableDie, MachineLocation(RegOp.getReg()));
David Blaikie0252265b2013-06-16 20:34:15 +00001823 } else if (DVInsn->getOperand(0).isImm())
Eric Christopher34a2c872013-11-15 01:43:19 +00001824 addConstantValue(VariableDie, DVInsn->getOperand(0), DV.getType());
David Blaikie0252265b2013-06-16 20:34:15 +00001825 else if (DVInsn->getOperand(0).isFPImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001826 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
David Blaikie0252265b2013-06-16 20:34:15 +00001827 else if (DVInsn->getOperand(0).isCImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001828 addConstantValue(VariableDie, DVInsn->getOperand(0).getCImm(),
Eric Christopher34a2c872013-11-15 01:43:19 +00001829 isUnsignedDIType(DD, DV.getType()));
Eric Christopher78fcf4902013-07-03 01:08:30 +00001830
Eric Christopher34a2c872013-11-15 01:43:19 +00001831 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001832 return VariableDie;
1833 } else {
1834 // .. else use frame index.
Eric Christopher34a2c872013-11-15 01:43:19 +00001835 int FI = DV.getFrameIndex();
Devang Patel3acc70e2011-08-15 22:04:40 +00001836 if (FI != ~0) {
1837 unsigned FrameReg = 0;
1838 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopherc2697f82013-10-19 01:04:47 +00001839 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
Devang Patel3acc70e2011-08-15 22:04:40 +00001840 MachineLocation Location(FrameReg, Offset);
Eric Christopher34a2c872013-11-15 01:43:19 +00001841 addVariableAddress(DV, VariableDie, Location);
Devang Patel3acc70e2011-08-15 22:04:40 +00001842 }
1843 }
1844
Eric Christopher34a2c872013-11-15 01:43:19 +00001845 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001846 return VariableDie;
1847}
1848
Manman Ren230ec862013-10-23 23:00:44 +00001849/// constructMemberDIE - Construct member DIE from DIDerivedType.
David Blaikie319a05f2013-12-02 19:33:10 +00001850void Unit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) {
Manman Renb987e512013-10-29 00:53:03 +00001851 DIE *MemberDie = createAndAddDIE(DT.getTag(), Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001852 StringRef Name = DT.getName();
1853 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001854 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001855
Manman Ren93b30902013-10-08 18:42:58 +00001856 addType(MemberDie, resolve(DT.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001857
1858 addSourceLine(MemberDie, DT);
1859
1860 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
David Blaikief2443192013-10-21 17:28:37 +00001861 addUInt(MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel0e821f42011-04-12 23:21:44 +00001862
Eric Christopherc2697f82013-10-19 01:04:47 +00001863 if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) {
Devang Patel0e821f42011-04-12 23:21:44 +00001864
1865 // For C++, virtual base classes are not at fixed offset. Use following
1866 // expression to extract appropriate offset from vtable.
1867 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1868
1869 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
David Blaikief2443192013-10-21 17:28:37 +00001870 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1871 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1872 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1873 addUInt(VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1874 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1875 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1876 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
Devang Patel0e821f42011-04-12 23:21:44 +00001877
David Blaikief2443192013-10-21 17:28:37 +00001878 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
David Blaikie71d34a22013-11-01 00:25:45 +00001879 } else {
1880 uint64_t Size = DT.getSizeInBits();
1881 uint64_t FieldSize = getBaseTypeSize(DD, DT);
1882 uint64_t OffsetInBytes;
1883
1884 if (Size != FieldSize) {
1885 // Handle bitfield.
1886 addUInt(MemberDie, dwarf::DW_AT_byte_size, None,
1887 getBaseTypeSize(DD, DT) >> 3);
1888 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, DT.getSizeInBits());
1889
1890 uint64_t Offset = DT.getOffsetInBits();
1891 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1892 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1893 uint64_t FieldOffset = (HiMark - FieldSize);
1894 Offset -= FieldOffset;
1895
1896 // Maybe we need to work from the other end.
1897 if (Asm->getDataLayout().isLittleEndian())
1898 Offset = FieldSize - (Offset + Size);
1899 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
1900
1901 // Here WD_AT_data_member_location points to the anonymous
1902 // field that includes this bit field.
1903 OffsetInBytes = FieldOffset >> 3;
1904 } else
1905 // This is not a bitfield.
1906 OffsetInBytes = DT.getOffsetInBits() >> 3;
Eric Christopher98b7f172013-11-11 18:52:36 +00001907 addUInt(MemberDie, dwarf::DW_AT_data_member_location, None, OffsetInBytes);
David Blaikie71d34a22013-11-01 00:25:45 +00001908 }
Devang Patel0e821f42011-04-12 23:21:44 +00001909
1910 if (DT.isProtected())
Nick Lewyckycb918492011-12-13 05:09:11 +00001911 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001912 dwarf::DW_ACCESS_protected);
1913 else if (DT.isPrivate())
Nick Lewyckycb918492011-12-13 05:09:11 +00001914 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001915 dwarf::DW_ACCESS_private);
1916 // Otherwise C++ member and base classes are considered public.
Eric Christopher92331fd2012-11-21 00:34:38 +00001917 else
Nick Lewyckycb918492011-12-13 05:09:11 +00001918 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001919 dwarf::DW_ACCESS_public);
1920 if (DT.isVirtual())
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001921 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001922 dwarf::DW_VIRTUALITY_virtual);
Devang Patel514b4002011-04-16 00:11:51 +00001923
1924 // Objective-C properties.
Devang Patel44882172012-02-06 17:49:43 +00001925 if (MDNode *PNode = DT.getObjCProperty())
1926 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
Eric Christopher92331fd2012-11-21 00:34:38 +00001927 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
Devang Patel44882172012-02-06 17:49:43 +00001928 PropertyDie);
1929
David Blaikie37fefc32012-12-13 22:43:07 +00001930 if (DT.isArtificial())
1931 addFlag(MemberDie, dwarf::DW_AT_artificial);
Devang Patel0e821f42011-04-12 23:21:44 +00001932}
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001933
Manman Renc6b63922013-10-14 20:33:57 +00001934/// getOrCreateStaticMemberDIE - Create new DIE for C++ static member.
David Blaikie319a05f2013-12-02 19:33:10 +00001935DIE *Unit::getOrCreateStaticMemberDIE(DIDerivedType DT) {
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001936 if (!DT.Verify())
1937 return NULL;
1938
Manman Renc6b63922013-10-14 20:33:57 +00001939 // Construct the context before querying for the existence of the DIE in case
1940 // such construction creates the DIE.
1941 DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext()));
David Blaikiebd700e42013-11-14 21:24:34 +00001942 assert(dwarf::isType(ContextDIE->getTag()) &&
1943 "Static member should belong to a type.");
Manman Renc6b63922013-10-14 20:33:57 +00001944
1945 DIE *StaticMemberDIE = getDIE(DT);
1946 if (StaticMemberDIE)
1947 return StaticMemberDIE;
1948
Manman Renb987e512013-10-29 00:53:03 +00001949 StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT);
Manman Renc6b63922013-10-14 20:33:57 +00001950
Manman Ren93b30902013-10-08 18:42:58 +00001951 DIType Ty = resolve(DT.getTypeDerivedFrom());
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001952
1953 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1954 addType(StaticMemberDIE, Ty);
1955 addSourceLine(StaticMemberDIE, DT);
1956 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1957 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1958
1959 // FIXME: We could omit private if the parent is a class_type, and
1960 // public if the parent is something else.
1961 if (DT.isProtected())
1962 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1963 dwarf::DW_ACCESS_protected);
1964 else if (DT.isPrivate())
1965 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1966 dwarf::DW_ACCESS_private);
1967 else
1968 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1969 dwarf::DW_ACCESS_public);
1970
1971 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
Manman Renb3388602013-10-05 01:43:03 +00001972 addConstantValue(StaticMemberDIE, CI, isUnsignedDIType(DD, Ty));
David Blaikiea39a76e2013-01-20 01:18:01 +00001973 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
1974 addConstantFPValue(StaticMemberDIE, CFP);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001975
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001976 return StaticMemberDIE;
1977}
David Blaikie6b288cf2013-10-30 20:42:41 +00001978
David Blaikie319a05f2013-12-02 19:33:10 +00001979void Unit::emitHeader(const MCSection *ASection, const MCSymbol *ASectionSym) {
David Blaikie6b288cf2013-10-30 20:42:41 +00001980 Asm->OutStreamer.AddComment("DWARF version number");
1981 Asm->EmitInt16(DD->getDwarfVersion());
1982 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
David Blaikie91db9ab2013-12-04 18:12:28 +00001983 // We share one abbreviations table across all compilation units so it's
1984 // always at the start of the section. Use a relocatable offset where needed
1985 // to ensure linking doesn't invalidate that offset.
1986 Asm->EmitSectionOffset(ASectionSym, ASectionSym);
David Blaikie6b288cf2013-10-30 20:42:41 +00001987 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1988 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
1989}