blob: 40b7476f4bd248fd15f67a1abad3e61c1e2dce8b [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,
Eric Christopherf8194852013-12-05 18:06:10 +000042 DwarfDebug *DW, DwarfFile *DWU)
David Blaikie2a80e442013-12-02 22:09:48 +000043 : UniqueID(UID), Node(Node), UnitDie(D), DebugInfoOffset(0), Asm(A), DD(DW),
David Blaikie03073f72013-12-06 22:14:48 +000044 DU(DWU), IndexTyDie(0), Section(0) {
David Blaikie319a05f2013-12-02 19:33:10 +000045 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
46}
47
David Blaikie5a152402013-11-15 23:52:02 +000048CompileUnit::CompileUnit(unsigned UID, DIE *D, DICompileUnit Node,
Eric Christopherf8194852013-12-05 18:06:10 +000049 AsmPrinter *A, DwarfDebug *DW, DwarfFile *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,
Eric Christopherf8194852013-12-05 18:06:10 +000055 DwarfDebug *DW, DwarfFile *DWU)
David Blaikie319a05f2013-12-02 19:33:10 +000056 : 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();
62}
63
64/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
65/// information entry.
David Blaikie319a05f2013-12-02 19:33:10 +000066DIEEntry *Unit::createDIEEntry(DIE *Entry) {
Devang Patel0e821f42011-04-12 23:21:44 +000067 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
68 return Value;
69}
70
Bill Wendling3495f9b2012-12-06 07:55:19 +000071/// getDefaultLowerBound - Return the default lower bound for an array. If the
Bill Wendling28fe9e72012-12-06 07:38:10 +000072/// DWARF version doesn't handle the language, return -1.
David Blaikie319a05f2013-12-02 19:33:10 +000073int64_t Unit::getDefaultLowerBound() const {
David Blaikiecb8e4352013-11-15 23:50:53 +000074 switch (getLanguage()) {
Bill Wendling28fe9e72012-12-06 07:38:10 +000075 default:
76 break;
77
78 case dwarf::DW_LANG_C89:
79 case dwarf::DW_LANG_C99:
80 case dwarf::DW_LANG_C:
81 case dwarf::DW_LANG_C_plus_plus:
82 case dwarf::DW_LANG_ObjC:
83 case dwarf::DW_LANG_ObjC_plus_plus:
84 return 0;
85
86 case dwarf::DW_LANG_Fortran77:
87 case dwarf::DW_LANG_Fortran90:
88 case dwarf::DW_LANG_Fortran95:
89 return 1;
90
91 // The languages below have valid values only if the DWARF version >= 4.
92 case dwarf::DW_LANG_Java:
93 case dwarf::DW_LANG_Python:
94 case dwarf::DW_LANG_UPC:
95 case dwarf::DW_LANG_D:
96 if (dwarf::DWARF_VERSION >= 4)
97 return 0;
98 break;
99
100 case dwarf::DW_LANG_Ada83:
101 case dwarf::DW_LANG_Ada95:
102 case dwarf::DW_LANG_Cobol74:
103 case dwarf::DW_LANG_Cobol85:
104 case dwarf::DW_LANG_Modula2:
105 case dwarf::DW_LANG_Pascal83:
106 case dwarf::DW_LANG_PLI:
107 if (dwarf::DWARF_VERSION >= 4)
108 return 1;
109 break;
110 }
111
112 return -1;
113}
114
Manman Ren4dbdc902013-10-31 17:54:35 +0000115/// Check whether the DIE for this MDNode can be shared across CUs.
David Blaikie4201ddf2013-11-15 22:59:36 +0000116static bool isShareableAcrossCUs(DIDescriptor D) {
David Blaikiebcb418e2013-11-20 18:40:16 +0000117 // When the MDNode can be part of the type system, the DIE can be shared
118 // across CUs.
119 // Combining type units and cross-CU DIE sharing is lower value (since
120 // cross-CU DIE sharing is used in LTO and removes type redundancy at that
121 // level already) but may be implementable for some value in projects
122 // building multiple independent libraries with LTO and then linking those
123 // together.
David Blaikie409dd9c2013-11-19 23:08:21 +0000124 return (D.isType() ||
125 (D.isSubprogram() && !DISubprogram(D).isDefinition())) &&
126 !GenerateTypeUnits;
Manman Ren4dbdc902013-10-31 17:54:35 +0000127}
128
129/// getDIE - Returns the debug information entry map slot for the
130/// specified debug variable. We delegate the request to DwarfDebug
131/// when the DIE for this MDNode can be shared across CUs. The mappings
132/// will be kept in DwarfDebug for shareable DIEs.
David Blaikie319a05f2013-12-02 19:33:10 +0000133DIE *Unit::getDIE(DIDescriptor D) const {
David Blaikie2ad00162013-11-15 23:09:13 +0000134 if (isShareableAcrossCUs(D))
135 return DD->getDIE(D);
136 return MDNodeToDieMap.lookup(D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000137}
138
139/// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
140/// when the DIE for this MDNode can be shared across CUs. The mappings
141/// will be kept in DwarfDebug for shareable DIEs.
David Blaikie319a05f2013-12-02 19:33:10 +0000142void Unit::insertDIE(DIDescriptor Desc, DIE *D) {
David Blaikie2ad00162013-11-15 23:09:13 +0000143 if (isShareableAcrossCUs(Desc)) {
144 DD->insertDIE(Desc, D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000145 return;
146 }
David Blaikie2ad00162013-11-15 23:09:13 +0000147 MDNodeToDieMap.insert(std::make_pair(Desc, D));
Manman Ren4dbdc902013-10-31 17:54:35 +0000148}
149
Eric Christopherbb69a272012-08-24 01:14:27 +0000150/// addFlag - Add a flag that is true.
David Blaikie319a05f2013-12-02 19:33:10 +0000151void Unit::addFlag(DIE *Die, dwarf::Attribute Attribute) {
Michael Gottesmanc89466f2013-09-04 04:39:38 +0000152 if (DD->getDwarfVersion() >= 4)
Eric Christopherdccd3282013-10-04 22:40:05 +0000153 Die->addValue(Attribute, dwarf::DW_FORM_flag_present, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000154 else
Eric Christopherdccd3282013-10-04 22:40:05 +0000155 Die->addValue(Attribute, dwarf::DW_FORM_flag, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000156}
157
Devang Patel0e821f42011-04-12 23:21:44 +0000158/// addUInt - Add an unsigned integer attribute data and value.
159///
David Blaikie319a05f2013-12-02 19:33:10 +0000160void Unit::addUInt(DIE *Die, dwarf::Attribute Attribute,
161 Optional<dwarf::Form> Form, uint64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000162 if (!Form)
163 Form = DIEInteger::BestForm(false, Integer);
164 DIEValue *Value = Integer == 1 ? DIEIntegerOne : new (DIEValueAllocator)
165 DIEInteger(Integer);
David Blaikief2443192013-10-21 17:28:37 +0000166 Die->addValue(Attribute, *Form, Value);
167}
168
David Blaikie319a05f2013-12-02 19:33:10 +0000169void Unit::addUInt(DIEBlock *Block, dwarf::Form Form, uint64_t Integer) {
David Blaikief2443192013-10-21 17:28:37 +0000170 addUInt(Block, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000171}
172
173/// addSInt - Add an signed integer attribute data and value.
174///
David Blaikie319a05f2013-12-02 19:33:10 +0000175void Unit::addSInt(DIE *Die, dwarf::Attribute Attribute,
176 Optional<dwarf::Form> Form, int64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000177 if (!Form)
178 Form = DIEInteger::BestForm(true, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000179 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
David Blaikief2443192013-10-21 17:28:37 +0000180 Die->addValue(Attribute, *Form, Value);
181}
182
David Blaikie319a05f2013-12-02 19:33:10 +0000183void Unit::addSInt(DIEBlock *Die, Optional<dwarf::Form> Form, int64_t Integer) {
David Blaikief2443192013-10-21 17:28:37 +0000184 addSInt(Die, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000185}
186
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000187/// addString - Add a string attribute data and value. We always emit a
188/// reference to the string pool instead of immediate strings so that DIEs have
Eric Christopherfba22602013-01-07 19:32:45 +0000189/// more predictable sizes. In the case of split dwarf we emit an index
190/// into another table which gets us the static offset into the string
191/// table.
David Blaikie319a05f2013-12-02 19:33:10 +0000192void Unit::addString(DIE *Die, dwarf::Attribute Attribute, StringRef String) {
Eric Christopher67646432013-07-26 17:02:41 +0000193 DIEValue *Value;
David Blaikief2443192013-10-21 17:28:37 +0000194 dwarf::Form Form;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000195 if (!DD->useSplitDwarf()) {
196 MCSymbol *Symb = DU->getStringPoolEntry(String);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000197 if (Asm->needsRelocationsForDwarfStringPool())
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000198 Value = new (DIEValueAllocator) DIELabel(Symb);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000199 else {
200 MCSymbol *StringPool = DU->getStringPoolSym();
201 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
202 }
Eric Christopher67646432013-07-26 17:02:41 +0000203 Form = dwarf::DW_FORM_strp;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000204 } else {
205 unsigned idx = DU->getStringPoolIndex(String);
Eric Christopher67646432013-07-26 17:02:41 +0000206 Value = new (DIEValueAllocator) DIEInteger(idx);
207 Form = dwarf::DW_FORM_GNU_str_index;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000208 }
Eric Christopher67646432013-07-26 17:02:41 +0000209 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
210 Die->addValue(Attribute, Form, Str);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000211}
212
213/// addLocalString - Add a string attribute data and value. This is guaranteed
214/// to be in the local string pool instead of indirected.
David Blaikie319a05f2013-12-02 19:33:10 +0000215void Unit::addLocalString(DIE *Die, dwarf::Attribute Attribute,
216 StringRef String) {
Eric Christophere698f532012-12-20 21:58:36 +0000217 MCSymbol *Symb = DU->getStringPoolEntry(String);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000218 DIEValue *Value;
219 if (Asm->needsRelocationsForDwarfStringPool())
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000220 Value = new (DIEValueAllocator) DIELabel(Symb);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000221 else {
Eric Christophere698f532012-12-20 21:58:36 +0000222 MCSymbol *StringPool = DU->getStringPoolSym();
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000223 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000224 }
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000225 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000226}
227
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000228/// addExpr - Add a Dwarf expression attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000229///
David Blaikie319a05f2013-12-02 19:33:10 +0000230void Unit::addExpr(DIEBlock *Die, dwarf::Form Form, const MCExpr *Expr) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000231 DIEValue *Value = new (DIEValueAllocator) DIEExpr(Expr);
David Blaikief2443192013-10-21 17:28:37 +0000232 Die->addValue((dwarf::Attribute)0, Form, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000233}
234
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000235/// addLabel - Add a Dwarf label attribute data and value.
236///
David Blaikie319a05f2013-12-02 19:33:10 +0000237void Unit::addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
238 const MCSymbol *Label) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000239 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
240 Die->addValue(Attribute, Form, Value);
David Blaikief3cd7c52013-06-28 20:05:04 +0000241}
242
David Blaikie319a05f2013-12-02 19:33:10 +0000243void Unit::addLabel(DIEBlock *Die, dwarf::Form Form, const MCSymbol *Label) {
David Blaikief2443192013-10-21 17:28:37 +0000244 addLabel(Die, (dwarf::Attribute)0, Form, Label);
245}
246
Eric Christopher33ff6972013-11-21 23:46:41 +0000247/// addSectionLabel - Add a Dwarf section label attribute data and value.
248///
David Blaikie319a05f2013-12-02 19:33:10 +0000249void Unit::addSectionLabel(DIE *Die, dwarf::Attribute Attribute,
250 const MCSymbol *Label) {
Eric Christopher33ff6972013-11-21 23:46:41 +0000251 if (DD->getDwarfVersion() >= 4)
252 addLabel(Die, Attribute, dwarf::DW_FORM_sec_offset, Label);
253 else
254 addLabel(Die, Attribute, dwarf::DW_FORM_data4, Label);
255}
256
257/// addSectionOffset - Add an offset into a section attribute data and value.
258///
David Blaikie319a05f2013-12-02 19:33:10 +0000259void Unit::addSectionOffset(DIE *Die, dwarf::Attribute Attribute,
260 uint64_t Integer) {
Eric Christopher33ff6972013-11-21 23:46:41 +0000261 if (DD->getDwarfVersion() >= 4)
262 addUInt(Die, Attribute, dwarf::DW_FORM_sec_offset, Integer);
263 else
264 addUInt(Die, Attribute, dwarf::DW_FORM_data4, Integer);
265}
266
Eric Christopher962c9082013-01-15 23:56:56 +0000267/// addLabelAddress - Add a dwarf label attribute data and value using
268/// DW_FORM_addr or DW_FORM_GNU_addr_index.
269///
David Blaikief2443192013-10-21 17:28:37 +0000270void CompileUnit::addLabelAddress(DIE *Die, dwarf::Attribute Attribute,
Eric Christopher962c9082013-01-15 23:56:56 +0000271 MCSymbol *Label) {
Alexey Samsonov4436bf02013-10-03 08:54:43 +0000272 if (Label)
273 DD->addArangeLabel(SymbolCU(this, Label));
Richard Mitton21101b32013-09-19 23:21:01 +0000274
Eric Christopher962c9082013-01-15 23:56:56 +0000275 if (!DD->useSplitDwarf()) {
276 if (Label != NULL) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000277 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
Eric Christopher962c9082013-01-15 23:56:56 +0000278 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
279 } else {
280 DIEValue *Value = new (DIEValueAllocator) DIEInteger(0);
281 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
282 }
283 } else {
284 unsigned idx = DU->getAddrPoolIndex(Label);
285 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
286 Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
287 }
288}
289
Eric Christophere9ec2452013-01-18 22:11:33 +0000290/// addOpAddress - Add a dwarf op address data and value using the
291/// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
292///
David Blaikie319a05f2013-12-02 19:33:10 +0000293void Unit::addOpAddress(DIEBlock *Die, const MCSymbol *Sym) {
Eric Christophere9ec2452013-01-18 22:11:33 +0000294 if (!DD->useSplitDwarf()) {
David Blaikief2443192013-10-21 17:28:37 +0000295 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
296 addLabel(Die, dwarf::DW_FORM_udata, Sym);
Eric Christophere9ec2452013-01-18 22:11:33 +0000297 } else {
David Blaikief2443192013-10-21 17:28:37 +0000298 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
299 addUInt(Die, dwarf::DW_FORM_GNU_addr_index, DU->getAddrPoolIndex(Sym));
Eric Christophere9ec2452013-01-18 22:11:33 +0000300 }
301}
302
Eric Christopher33ff6972013-11-21 23:46:41 +0000303/// addSectionDelta - Add a section label delta attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000304///
David Blaikie319a05f2013-12-02 19:33:10 +0000305void Unit::addSectionDelta(DIE *Die, dwarf::Attribute Attribute,
306 const MCSymbol *Hi, const MCSymbol *Lo) {
Devang Patel0e821f42011-04-12 23:21:44 +0000307 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Eric Christopher33ff6972013-11-21 23:46:41 +0000308 if (DD->getDwarfVersion() >= 4)
309 Die->addValue(Attribute, dwarf::DW_FORM_sec_offset, Value);
310 else
311 Die->addValue(Attribute, dwarf::DW_FORM_data4, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000312}
313
314/// addDIEEntry - Add a DIE attribute data and value.
315///
David Blaikie319a05f2013-12-02 19:33:10 +0000316void Unit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +0000317 addDIEEntry(Die, Attribute, createDIEEntry(Entry));
318}
319
David Blaikie319a05f2013-12-02 19:33:10 +0000320void Unit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIEEntry *Entry) {
David Blaikie409dd9c2013-11-19 23:08:21 +0000321 const DIE *DieCU = Die->getUnitOrNull();
322 const DIE *EntryCU = Entry->getEntry()->getUnitOrNull();
Manman Ren4dbdc902013-10-31 17:54:35 +0000323 if (!DieCU)
324 // We assume that Die belongs to this CU, if it is not linked to any CU yet.
David Blaikie2a80e442013-12-02 22:09:48 +0000325 DieCU = getUnitDie();
Manman Ren4dbdc902013-10-31 17:54:35 +0000326 if (!EntryCU)
David Blaikie2a80e442013-12-02 22:09:48 +0000327 EntryCU = getUnitDie();
Manman Ren4dbdc902013-10-31 17:54:35 +0000328 Die->addValue(Attribute, EntryCU == DieCU ? dwarf::DW_FORM_ref4
329 : dwarf::DW_FORM_ref_addr,
330 Entry);
Devang Patel0e821f42011-04-12 23:21:44 +0000331}
332
Manman Renb987e512013-10-29 00:53:03 +0000333/// Create a DIE with the given Tag, add the DIE to its parent, and
334/// call insertDIE if MD is not null.
David Blaikie319a05f2013-12-02 19:33:10 +0000335DIE *Unit::createAndAddDIE(unsigned Tag, DIE &Parent, DIDescriptor N) {
Manman Renb987e512013-10-29 00:53:03 +0000336 DIE *Die = new DIE(Tag);
337 Parent.addChild(Die);
David Blaikie52c50202013-11-16 00:29:01 +0000338 if (N)
339 insertDIE(N, Die);
Manman Renb987e512013-10-29 00:53:03 +0000340 return Die;
341}
342
Devang Patel0e821f42011-04-12 23:21:44 +0000343/// addBlock - Add block data.
344///
David Blaikie319a05f2013-12-02 19:33:10 +0000345void Unit::addBlock(DIE *Die, dwarf::Attribute Attribute, DIEBlock *Block) {
Devang Patel0e821f42011-04-12 23:21:44 +0000346 Block->ComputeSize(Asm);
347 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
348 Die->addValue(Attribute, Block->BestForm(), Block);
349}
350
351/// addSourceLine - Add location information to specified debug information
352/// entry.
David Blaikie319a05f2013-12-02 19:33:10 +0000353void Unit::addSourceLine(DIE *Die, DIVariable V) {
Devang Patel0e821f42011-04-12 23:21:44 +0000354 // Verify variable.
Manman Ren7504ed42013-07-08 18:33:29 +0000355 if (!V.isVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000356 return;
Eric Christopher92331fd2012-11-21 00:34:38 +0000357
Devang Patel0e821f42011-04-12 23:21:44 +0000358 unsigned Line = V.getLineNumber();
359 if (Line == 0)
360 return;
Eric Christopherc2697f82013-10-19 01:04:47 +0000361 unsigned FileID =
362 DD->getOrCreateSourceID(V.getContext().getFilename(),
363 V.getContext().getDirectory(), getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000364 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000365 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
366 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000367}
368
369/// addSourceLine - Add location information to specified debug information
370/// entry.
David Blaikie319a05f2013-12-02 19:33:10 +0000371void Unit::addSourceLine(DIE *Die, DIGlobalVariable G) {
Devang Patel0e821f42011-04-12 23:21:44 +0000372 // Verify global variable.
Manman Ren7504ed42013-07-08 18:33:29 +0000373 if (!G.isGlobalVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000374 return;
375
376 unsigned Line = G.getLineNumber();
377 if (Line == 0)
378 return;
Eric Christopherc2697f82013-10-19 01:04:47 +0000379 unsigned FileID =
380 DD->getOrCreateSourceID(G.getFilename(), G.getDirectory(), getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000381 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000382 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
383 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000384}
385
386/// addSourceLine - Add location information to specified debug information
387/// entry.
David Blaikie319a05f2013-12-02 19:33:10 +0000388void Unit::addSourceLine(DIE *Die, DISubprogram SP) {
Devang Patel0e821f42011-04-12 23:21:44 +0000389 // Verify subprogram.
Manman Ren7504ed42013-07-08 18:33:29 +0000390 if (!SP.isSubprogram())
Devang Patel0e821f42011-04-12 23:21:44 +0000391 return;
Eric Christopher7734ca22012-03-15 23:55:40 +0000392
Devang Patel0e821f42011-04-12 23:21:44 +0000393 // If the line number is 0, don't add it.
Eric Christopher7734ca22012-03-15 23:55:40 +0000394 unsigned Line = SP.getLineNumber();
395 if (Line == 0)
Devang Patel0e821f42011-04-12 23:21:44 +0000396 return;
397
Eric Christopherc2697f82013-10-19 01:04:47 +0000398 unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(), SP.getDirectory(),
399 getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000400 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000401 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
402 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000403}
404
405/// addSourceLine - Add location information to specified debug information
406/// entry.
David Blaikie319a05f2013-12-02 19:33:10 +0000407void Unit::addSourceLine(DIE *Die, DIType Ty) {
Devang Patel0e821f42011-04-12 23:21:44 +0000408 // Verify type.
Manman Ren7504ed42013-07-08 18:33:29 +0000409 if (!Ty.isType())
Devang Patel0e821f42011-04-12 23:21:44 +0000410 return;
411
412 unsigned Line = Ty.getLineNumber();
Eric Christopher7734ca22012-03-15 23:55:40 +0000413 if (Line == 0)
Devang Patel0e821f42011-04-12 23:21:44 +0000414 return;
Eric Christopherc2697f82013-10-19 01:04:47 +0000415 unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(), Ty.getDirectory(),
416 getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000417 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000418 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
419 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000420}
421
422/// addSourceLine - Add location information to specified debug information
423/// entry.
David Blaikie319a05f2013-12-02 19:33:10 +0000424void Unit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
Eric Christopher70e1bd82012-03-29 08:42:56 +0000425 // Verify type.
Manman Ren7504ed42013-07-08 18:33:29 +0000426 if (!Ty.isObjCProperty())
Eric Christopher70e1bd82012-03-29 08:42:56 +0000427 return;
428
429 unsigned Line = Ty.getLineNumber();
430 if (Line == 0)
431 return;
432 DIFile File = Ty.getFile();
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000433 unsigned FileID = DD->getOrCreateSourceID(File.getFilename(),
Manman Ren1e427202013-03-07 01:42:00 +0000434 File.getDirectory(), getUniqueID());
Eric Christopher70e1bd82012-03-29 08:42:56 +0000435 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000436 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
437 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Eric Christopher70e1bd82012-03-29 08:42:56 +0000438}
439
440/// addSourceLine - Add location information to specified debug information
441/// entry.
David Blaikie319a05f2013-12-02 19:33:10 +0000442void Unit::addSourceLine(DIE *Die, DINameSpace NS) {
Devang Patel0e821f42011-04-12 23:21:44 +0000443 // Verify namespace.
444 if (!NS.Verify())
445 return;
446
447 unsigned Line = NS.getLineNumber();
448 if (Line == 0)
449 return;
450 StringRef FN = NS.getFilename();
451
Eric Christopherc2697f82013-10-19 01:04:47 +0000452 unsigned FileID =
453 DD->getOrCreateSourceID(FN, NS.getDirectory(), getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000454 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000455 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
456 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000457}
458
Eric Christopher92331fd2012-11-21 00:34:38 +0000459/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patel77dc5412011-04-27 22:45:24 +0000460/// DbgVariable based on provided MachineLocation.
David Blaikie319a05f2013-12-02 19:33:10 +0000461void Unit::addVariableAddress(const DbgVariable &DV, DIE *Die,
462 MachineLocation Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000463 if (DV.variableHasComplexAddress())
Devang Patel0e821f42011-04-12 23:21:44 +0000464 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000465 else if (DV.isBlockByrefVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000466 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
467 else
David Blaikieea2605d2013-06-20 00:25:24 +0000468 addAddress(Die, dwarf::DW_AT_location, Location,
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000469 DV.getVariable().isIndirect());
Devang Patel0e821f42011-04-12 23:21:44 +0000470}
471
Devang Patelba5fbf12011-04-26 19:06:18 +0000472/// addRegisterOp - Add register operand.
David Blaikie319a05f2013-12-02 19:33:10 +0000473void Unit::addRegisterOp(DIEBlock *TheDie, unsigned Reg) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000474 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
475 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
476 if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000477 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000478 else {
David Blaikief2443192013-10-21 17:28:37 +0000479 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
480 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000481 }
482}
483
484/// addRegisterOffset - Add register offset.
David Blaikie319a05f2013-12-02 19:33:10 +0000485void Unit::addRegisterOffset(DIEBlock *TheDie, unsigned Reg, int64_t Offset) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000486 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
487 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
488 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
489 if (Reg == TRI->getFrameRegister(*Asm->MF))
490 // If variable offset is based in frame register then use fbreg.
David Blaikief2443192013-10-21 17:28:37 +0000491 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000492 else if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000493 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000494 else {
David Blaikief2443192013-10-21 17:28:37 +0000495 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
496 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000497 }
David Blaikief2443192013-10-21 17:28:37 +0000498 addSInt(TheDie, dwarf::DW_FORM_sdata, Offset);
Devang Patelba5fbf12011-04-26 19:06:18 +0000499}
500
501/// addAddress - Add an address attribute to a die based on the location
502/// provided.
David Blaikie319a05f2013-12-02 19:33:10 +0000503void Unit::addAddress(DIE *Die, dwarf::Attribute Attribute,
504 const MachineLocation &Location, bool Indirect) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000505 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
506
David Blaikieea2605d2013-06-20 00:25:24 +0000507 if (Location.isReg() && !Indirect)
Devang Patelba5fbf12011-04-26 19:06:18 +0000508 addRegisterOp(Block, Location.getReg());
David Blaikieea2605d2013-06-20 00:25:24 +0000509 else {
Devang Patelba5fbf12011-04-26 19:06:18 +0000510 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
David Blaikieea2605d2013-06-20 00:25:24 +0000511 if (Indirect && !Location.isReg()) {
David Blaikief2443192013-10-21 17:28:37 +0000512 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
David Blaikieea2605d2013-06-20 00:25:24 +0000513 }
514 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000515
516 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000517 addBlock(Die, Attribute, Block);
Devang Patelba5fbf12011-04-26 19:06:18 +0000518}
519
Devang Patel0e821f42011-04-12 23:21:44 +0000520/// addComplexAddress - Start with the address based on the location provided,
521/// and generate the DWARF information necessary to find the actual variable
Eric Christopher1c70b672013-12-05 00:13:15 +0000522/// given the extra address information encoded in the DbgVariable, starting
523/// from the starting location. Add the DWARF information to the die.
Devang Patel0e821f42011-04-12 23:21:44 +0000524///
David Blaikie319a05f2013-12-02 19:33:10 +0000525void Unit::addComplexAddress(const DbgVariable &DV, DIE *Die,
526 dwarf::Attribute Attribute,
527 const MachineLocation &Location) {
Devang Patel0e821f42011-04-12 23:21:44 +0000528 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000529 unsigned N = DV.getNumAddrElements();
Devang Patel3e021532011-04-28 02:22:40 +0000530 unsigned i = 0;
531 if (Location.isReg()) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000532 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Devang Patel3e021532011-04-28 02:22:40 +0000533 // If first address element is OpPlus then emit
534 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000535 addRegisterOffset(Block, Location.getReg(), DV.getAddrElement(1));
Devang Patel3e021532011-04-28 02:22:40 +0000536 i = 2;
537 } else
538 addRegisterOp(Block, Location.getReg());
Eric Christopherc2697f82013-10-19 01:04:47 +0000539 } else
Devang Patelba5fbf12011-04-26 19:06:18 +0000540 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000541
Eric Christopherc2697f82013-10-19 01:04:47 +0000542 for (; i < N; ++i) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000543 uint64_t Element = DV.getAddrElement(i);
Devang Patel0e821f42011-04-12 23:21:44 +0000544 if (Element == DIBuilder::OpPlus) {
David Blaikief2443192013-10-21 17:28:37 +0000545 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
546 addUInt(Block, dwarf::DW_FORM_udata, DV.getAddrElement(++i));
Devang Patel0e821f42011-04-12 23:21:44 +0000547 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher4d250522012-05-08 18:56:00 +0000548 if (!Location.isReg())
David Blaikief2443192013-10-21 17:28:37 +0000549 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Eric Christopherc2697f82013-10-19 01:04:47 +0000550 } else
551 llvm_unreachable("unknown DIBuilder Opcode");
Devang Patel0e821f42011-04-12 23:21:44 +0000552 }
553
554 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000555 addBlock(Die, Attribute, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000556}
557
558/* Byref variables, in Blocks, are declared by the programmer as "SomeType
559 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
560 gives the variable VarName either the struct, or a pointer to the struct, as
561 its type. This is necessary for various behind-the-scenes things the
562 compiler needs to do with by-reference variables in Blocks.
563
564 However, as far as the original *programmer* is concerned, the variable
565 should still have type 'SomeType', as originally declared.
566
567 The function getBlockByrefType dives into the __Block_byref_x_VarName
568 struct to find the original type of the variable, which is then assigned to
569 the variable's Debug Information Entry as its real type. So far, so good.
570 However now the debugger will expect the variable VarName to have the type
571 SomeType. So we need the location attribute for the variable to be an
572 expression that explains to the debugger how to navigate through the
573 pointers and struct to find the actual variable of type SomeType.
574
575 The following function does just that. We start by getting
576 the "normal" location for the variable. This will be the location
577 of either the struct __Block_byref_x_VarName or the pointer to the
578 struct __Block_byref_x_VarName.
579
580 The struct will look something like:
581
582 struct __Block_byref_x_VarName {
583 ... <various fields>
584 struct __Block_byref_x_VarName *forwarding;
585 ... <various other fields>
586 SomeType VarName;
587 ... <maybe more fields>
588 };
589
590 If we are given the struct directly (as our starting point) we
591 need to tell the debugger to:
592
593 1). Add the offset of the forwarding field.
594
595 2). Follow that pointer to get the real __Block_byref_x_VarName
596 struct to use (the real one may have been copied onto the heap).
597
598 3). Add the offset for the field VarName, to find the actual variable.
599
600 If we started with a pointer to the struct, then we need to
601 dereference that pointer first, before the other steps.
602 Translating this into DWARF ops, we will need to append the following
603 to the current location description for the variable:
604
605 DW_OP_deref -- optional, if we start with a pointer
606 DW_OP_plus_uconst <forward_fld_offset>
607 DW_OP_deref
608 DW_OP_plus_uconst <varName_fld_offset>
609
610 That is what this function does. */
611
612/// addBlockByrefAddress - Start with the address based on the location
613/// provided, and generate the DWARF information necessary to find the
614/// actual Block variable (navigating the Block struct) based on the
615/// starting location. Add the DWARF information to the die. For
616/// more information, read large comment just above here.
617///
David Blaikie319a05f2013-12-02 19:33:10 +0000618void Unit::addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
619 dwarf::Attribute Attribute,
620 const MachineLocation &Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000621 DIType Ty = DV.getType();
Devang Patel0e821f42011-04-12 23:21:44 +0000622 DIType TmpTy = Ty;
Eric Christopher31b05762013-08-08 01:41:00 +0000623 uint16_t Tag = Ty.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +0000624 bool isPointer = false;
625
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000626 StringRef varName = DV.getName();
Devang Patel0e821f42011-04-12 23:21:44 +0000627
628 if (Tag == dwarf::DW_TAG_pointer_type) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000629 DIDerivedType DTy(Ty);
Manman Ren93b30902013-10-08 18:42:58 +0000630 TmpTy = resolve(DTy.getTypeDerivedFrom());
Devang Patel0e821f42011-04-12 23:21:44 +0000631 isPointer = true;
632 }
633
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000634 DICompositeType blockStruct(TmpTy);
Devang Patel0e821f42011-04-12 23:21:44 +0000635
636 // Find the __forwarding field and the variable field in the __Block_byref
637 // struct.
638 DIArray Fields = blockStruct.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000639 DIDerivedType varField;
640 DIDerivedType forwardingField;
Devang Patel0e821f42011-04-12 23:21:44 +0000641
642 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000643 DIDerivedType DT(Fields.getElement(i));
Devang Patel0e821f42011-04-12 23:21:44 +0000644 StringRef fieldName = DT.getName();
645 if (fieldName == "__forwarding")
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000646 forwardingField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000647 else if (fieldName == varName)
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000648 varField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000649 }
650
651 // Get the offsets for the forwarding field and the variable field.
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000652 unsigned forwardingFieldOffset = forwardingField.getOffsetInBits() >> 3;
653 unsigned varFieldOffset = varField.getOffsetInBits() >> 2;
Devang Patel0e821f42011-04-12 23:21:44 +0000654
655 // Decode the original location, and use that as the start of the byref
656 // variable's location.
Devang Patel0e821f42011-04-12 23:21:44 +0000657 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
658
Eric Christopheref9d7102012-07-04 02:02:18 +0000659 if (Location.isReg())
660 addRegisterOp(Block, Location.getReg());
661 else
662 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000663
664 // If we started with a pointer to the __Block_byref... struct, then
665 // the first thing we need to do is dereference the pointer (DW_OP_deref).
666 if (isPointer)
David Blaikief2443192013-10-21 17:28:37 +0000667 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000668
669 // Next add the offset for the '__forwarding' field:
670 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
671 // adding the offset if it's 0.
672 if (forwardingFieldOffset > 0) {
David Blaikief2443192013-10-21 17:28:37 +0000673 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
674 addUInt(Block, dwarf::DW_FORM_udata, forwardingFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000675 }
676
677 // Now dereference the __forwarding field to get to the real __Block_byref
678 // struct: DW_OP_deref.
David Blaikief2443192013-10-21 17:28:37 +0000679 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000680
681 // Now that we've got the real __Block_byref... struct, add the offset
682 // for the variable's field to get to the location of the actual variable:
683 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
684 if (varFieldOffset > 0) {
David Blaikief2443192013-10-21 17:28:37 +0000685 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
686 addUInt(Block, dwarf::DW_FORM_udata, varFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000687 }
688
689 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000690 addBlock(Die, Attribute, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000691}
692
Devang Patelbcd50a12011-07-20 21:57:04 +0000693/// isTypeSigned - Return true if the type is signed.
Manman Renb3388602013-10-05 01:43:03 +0000694static bool isTypeSigned(DwarfDebug *DD, DIType Ty, int *SizeInBits) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000695 if (Ty.isDerivedType())
Manman Renb3388602013-10-05 01:43:03 +0000696 return isTypeSigned(DD, DD->resolve(DIDerivedType(Ty).getTypeDerivedFrom()),
697 SizeInBits);
Devang Patelbcd50a12011-07-20 21:57:04 +0000698 if (Ty.isBasicType())
Eric Christopherc2697f82013-10-19 01:04:47 +0000699 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed ||
700 DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000701 *SizeInBits = Ty.getSizeInBits();
702 return true;
703 }
704 return false;
705}
706
Manman Renb3388602013-10-05 01:43:03 +0000707/// Return true if type encoding is unsigned.
708static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) {
709 DIDerivedType DTy(Ty);
710 if (DTy.isDerivedType())
711 return isUnsignedDIType(DD, DD->resolve(DTy.getTypeDerivedFrom()));
712
713 DIBasicType BTy(Ty);
714 if (BTy.isBasicType()) {
715 unsigned Encoding = BTy.getEncoding();
716 if (Encoding == dwarf::DW_ATE_unsigned ||
717 Encoding == dwarf::DW_ATE_unsigned_char ||
718 Encoding == dwarf::DW_ATE_boolean)
719 return true;
720 }
721 return false;
722}
723
724/// If this type is derived from a base type then return base type size.
Manman Renbda410f2013-10-08 18:46:58 +0000725static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) {
Manman Renb3388602013-10-05 01:43:03 +0000726 unsigned Tag = Ty.getTag();
727
728 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
729 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
730 Tag != dwarf::DW_TAG_restrict_type)
731 return Ty.getSizeInBits();
732
733 DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom());
734
735 // If this type is not derived from any type then take conservative approach.
736 if (!BaseType.isValid())
737 return Ty.getSizeInBits();
738
739 // If this is a derived type, go ahead and get the base type, unless it's a
740 // reference then it's just the size of the field. Pointer types have no need
741 // of this since they're a different type of qualification on the type.
742 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
743 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
744 return Ty.getSizeInBits();
745
746 if (BaseType.isDerivedType())
Manman Renbda410f2013-10-08 18:46:58 +0000747 return getBaseTypeSize(DD, DIDerivedType(BaseType));
Manman Renb3388602013-10-05 01:43:03 +0000748
749 return BaseType.getSizeInBits();
750}
751
Devang Patel0e821f42011-04-12 23:21:44 +0000752/// addConstantValue - Add constant value entry in variable DIE.
David Blaikie319a05f2013-12-02 19:33:10 +0000753void Unit::addConstantValue(DIE *Die, const MachineOperand &MO, DIType Ty) {
David Blaikiea1e813d2013-05-10 21:52:07 +0000754 // FIXME: This is a bit conservative/simple - it emits negative values at
755 // their maximum bit width which is a bit unfortunate (& doesn't prefer
756 // udata/sdata over dataN as suggested by the DWARF spec)
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000757 assert(MO.isImm() && "Invalid machine operand!");
Devang Patelbcd50a12011-07-20 21:57:04 +0000758 int SizeInBits = -1;
Manman Renb3388602013-10-05 01:43:03 +0000759 bool SignedConstant = isTypeSigned(DD, Ty, &SizeInBits);
David Blaikief2443192013-10-21 17:28:37 +0000760 dwarf::Form Form;
Devang Patelf1d04702011-05-27 18:15:52 +0000761
Eric Christopher9d1daa82013-08-27 23:49:04 +0000762 // If we're a signed constant definitely use sdata.
763 if (SignedConstant) {
764 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, MO.getImm());
765 return;
766 }
767
768 // Else use data for now unless it's larger than we can deal with.
769 switch (SizeInBits) {
770 case 8:
771 Form = dwarf::DW_FORM_data1;
772 break;
773 case 16:
774 Form = dwarf::DW_FORM_data2;
775 break;
776 case 32:
777 Form = dwarf::DW_FORM_data4;
778 break;
779 case 64:
780 Form = dwarf::DW_FORM_data8;
781 break;
782 default:
783 Form = dwarf::DW_FORM_udata;
784 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
785 return;
786 }
787 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
Devang Patel0e821f42011-04-12 23:21:44 +0000788}
789
790/// addConstantFPValue - Add constant value entry in variable DIE.
David Blaikie319a05f2013-12-02 19:33:10 +0000791void Unit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000792 assert(MO.isFPImm() && "Invalid machine operand!");
Devang Patel0e821f42011-04-12 23:21:44 +0000793 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
794 APFloat FPImm = MO.getFPImm()->getValueAPF();
795
796 // Get the raw data form of the floating point.
797 const APInt FltVal = FPImm.bitcastToAPInt();
Eric Christopherc2697f82013-10-19 01:04:47 +0000798 const char *FltPtr = (const char *)FltVal.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000799
800 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000801 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000802 int Incr = (LittleEndian ? 1 : -1);
803 int Start = (LittleEndian ? 0 : NumBytes - 1);
804 int Stop = (LittleEndian ? NumBytes : -1);
805
806 // Output the constant to DWARF one byte at a time.
807 for (; Start != Stop; Start += Incr)
Eric Christopher98b7f172013-11-11 18:52:36 +0000808 addUInt(Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]);
Devang Patel0e821f42011-04-12 23:21:44 +0000809
David Blaikief2443192013-10-21 17:28:37 +0000810 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000811}
812
David Blaikiea39a76e2013-01-20 01:18:01 +0000813/// addConstantFPValue - Add constant value entry in variable DIE.
David Blaikie319a05f2013-12-02 19:33:10 +0000814void Unit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000815 // Pass this down to addConstantValue as an unsigned bag of bits.
816 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
David Blaikiea39a76e2013-01-20 01:18:01 +0000817}
818
Devang Patel0e821f42011-04-12 23:21:44 +0000819/// addConstantValue - Add constant value entry in variable DIE.
David Blaikie319a05f2013-12-02 19:33:10 +0000820void Unit::addConstantValue(DIE *Die, const ConstantInt *CI, bool Unsigned) {
Eric Christopher78fcf4902013-07-03 01:08:30 +0000821 addConstantValue(Die, CI->getValue(), Unsigned);
David Blaikiea39a76e2013-01-20 01:18:01 +0000822}
823
824// addConstantValue - Add constant value entry in variable DIE.
David Blaikie319a05f2013-12-02 19:33:10 +0000825void Unit::addConstantValue(DIE *Die, const APInt &Val, bool Unsigned) {
David Blaikiea39a76e2013-01-20 01:18:01 +0000826 unsigned CIBitWidth = Val.getBitWidth();
Devang Patel8816bbc2011-05-28 00:39:18 +0000827 if (CIBitWidth <= 64) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000828 // If we're a signed constant definitely use sdata.
829 if (!Unsigned) {
830 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
831 Val.getSExtValue());
832 return;
Devang Patel8816bbc2011-05-28 00:39:18 +0000833 }
Eric Christopher9d1daa82013-08-27 23:49:04 +0000834
835 // Else use data for now unless it's larger than we can deal with.
David Blaikief2443192013-10-21 17:28:37 +0000836 dwarf::Form Form;
Eric Christopher9d1daa82013-08-27 23:49:04 +0000837 switch (CIBitWidth) {
838 case 8:
839 Form = dwarf::DW_FORM_data1;
840 break;
841 case 16:
842 Form = dwarf::DW_FORM_data2;
843 break;
844 case 32:
845 Form = dwarf::DW_FORM_data4;
846 break;
847 case 64:
848 Form = dwarf::DW_FORM_data8;
849 break;
850 default:
851 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
852 Val.getZExtValue());
853 return;
854 }
855 addUInt(Die, dwarf::DW_AT_const_value, Form, Val.getZExtValue());
Eric Christopher78fcf4902013-07-03 01:08:30 +0000856 return;
Devang Patel0e821f42011-04-12 23:21:44 +0000857 }
858
859 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
860
861 // Get the raw data form of the large APInt.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000862 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000863
864 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000865 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000866
867 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000868 for (int i = 0; i < NumBytes; i++) {
869 uint8_t c;
870 if (LittleEndian)
871 c = Ptr64[i / 8] >> (8 * (i & 7));
872 else
873 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
David Blaikief2443192013-10-21 17:28:37 +0000874 addUInt(Block, dwarf::DW_FORM_data1, c);
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000875 }
Devang Patel0e821f42011-04-12 23:21:44 +0000876
David Blaikief2443192013-10-21 17:28:37 +0000877 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000878}
879
Eric Christopher25e35092013-04-22 07:47:40 +0000880/// addTemplateParams - Add template parameters into buffer.
David Blaikie319a05f2013-12-02 19:33:10 +0000881void Unit::addTemplateParams(DIE &Buffer, DIArray TParams) {
Devang Patel0e821f42011-04-12 23:21:44 +0000882 // Add template parameters.
883 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
884 DIDescriptor Element = TParams.getElement(i);
885 if (Element.isTemplateTypeParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000886 constructTemplateTypeParameterDIE(Buffer,
887 DITemplateTypeParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000888 else if (Element.isTemplateValueParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000889 constructTemplateValueParameterDIE(Buffer,
890 DITemplateValueParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000891 }
Devang Patel0e821f42011-04-12 23:21:44 +0000892}
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000893
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000894/// getOrCreateContextDIE - Get context owner's DIE.
David Blaikie319a05f2013-12-02 19:33:10 +0000895DIE *Unit::getOrCreateContextDIE(DIScope Context) {
David Blaikiebd700e42013-11-14 21:24:34 +0000896 if (!Context || Context.isFile())
David Blaikie2a80e442013-12-02 22:09:48 +0000897 return getUnitDie();
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000898 if (Context.isType())
899 return getOrCreateTypeDIE(DIType(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000900 if (Context.isNameSpace())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000901 return getOrCreateNameSpace(DINameSpace(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000902 if (Context.isSubprogram())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000903 return getOrCreateSubprogramDIE(DISubprogram(Context));
Adrian Prantl7d828bb2013-11-15 21:05:09 +0000904 return getDIE(Context);
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000905}
906
David Blaikie319a05f2013-12-02 19:33:10 +0000907DIE *Unit::createTypeDIE(DICompositeType Ty) {
David Blaikie9d861be2013-11-26 00:15:27 +0000908 DIScope Context = resolve(Ty.getContext());
909 DIE *ContextDIE = getOrCreateContextDIE(Context);
David Blaikie409dd9c2013-11-19 23:08:21 +0000910
911 DIE *TyDIE = getDIE(Ty);
912 if (TyDIE)
913 return TyDIE;
914
915 // Create new type.
916 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
917
David Blaikiefbd29eb2013-11-26 00:35:04 +0000918 constructTypeDIE(*TyDIE, Ty);
David Blaikie409dd9c2013-11-19 23:08:21 +0000919
David Blaikie9d861be2013-11-26 00:15:27 +0000920 updateAcceleratorTables(Context, Ty, TyDIE);
David Blaikie409dd9c2013-11-19 23:08:21 +0000921 return TyDIE;
922}
923
David Blaikie8a263cb2013-11-26 00:22:37 +0000924/// Return true if the type is appropriately scoped to be contained inside
925/// its own type unit.
926static bool isTypeUnitScoped(DIType Ty, const DwarfDebug *DD) {
927 DIScope Parent = DD->resolve(Ty.getContext());
928 while (Parent) {
929 // Don't generate a hash for anything scoped inside a function.
930 if (Parent.isSubprogram())
931 return false;
932 Parent = DD->resolve(Parent.getContext());
933 }
934 return true;
935}
936
937/// Return true if the type should be split out into a type unit.
938static bool shouldCreateTypeUnit(DICompositeType CTy, const DwarfDebug *DD) {
939 if (!GenerateTypeUnits)
940 return false;
941
942 uint16_t Tag = CTy.getTag();
943
944 switch (Tag) {
945 case dwarf::DW_TAG_structure_type:
946 case dwarf::DW_TAG_union_type:
947 case dwarf::DW_TAG_enumeration_type:
948 case dwarf::DW_TAG_class_type:
949 // If this is a class, structure, union, or enumeration type
950 // that is a definition (not a declaration), and not scoped
951 // inside a function then separate this out as a type unit.
952 return !CTy.isForwardDecl() && isTypeUnitScoped(CTy, DD);
953 default:
954 return false;
955 }
956}
957
Devang Patel0e821f42011-04-12 23:21:44 +0000958/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
959/// given DIType.
David Blaikie319a05f2013-12-02 19:33:10 +0000960DIE *Unit::getOrCreateTypeDIE(const MDNode *TyNode) {
David Blaikie32887552013-11-14 22:25:02 +0000961 if (!TyNode)
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000962 return NULL;
Manman Renf4c339e2013-10-29 22:49:29 +0000963
David Blaikie32887552013-11-14 22:25:02 +0000964 DIType Ty(TyNode);
965 assert(Ty.isType());
966
Manman Renf4c339e2013-10-29 22:49:29 +0000967 // Construct the context before querying for the existence of the DIE in case
968 // such construction creates the DIE.
David Blaikie9d861be2013-11-26 00:15:27 +0000969 DIScope Context = resolve(Ty.getContext());
970 DIE *ContextDIE = getOrCreateContextDIE(Context);
Adrian Prantl4583f7d2013-11-15 23:21:39 +0000971 assert(ContextDIE);
Manman Renf4c339e2013-10-29 22:49:29 +0000972
Eric Christophere595bae2013-10-04 17:08:38 +0000973 DIE *TyDIE = getDIE(Ty);
Devang Patel0e821f42011-04-12 23:21:44 +0000974 if (TyDIE)
975 return TyDIE;
976
977 // Create new type.
Manman Renf4c339e2013-10-29 22:49:29 +0000978 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
979
Devang Patel0e821f42011-04-12 23:21:44 +0000980 if (Ty.isBasicType())
981 constructTypeDIE(*TyDIE, DIBasicType(Ty));
David Blaikie8a263cb2013-11-26 00:22:37 +0000982 else if (Ty.isCompositeType()) {
983 DICompositeType CTy(Ty);
984 if (shouldCreateTypeUnit(CTy, DD)) {
David Blaikie3c1d3322013-12-02 18:44:29 +0000985 DD->addTypeUnitType(getLanguage(), TyDIE, CTy);
David Blaikie8a263cb2013-11-26 00:22:37 +0000986 // Skip updating the accellerator tables since this is not the full type
987 return TyDIE;
988 }
David Blaikiefbd29eb2013-11-26 00:35:04 +0000989 constructTypeDIE(*TyDIE, CTy);
David Blaikie8a263cb2013-11-26 00:22:37 +0000990 } else {
Devang Patel0e821f42011-04-12 23:21:44 +0000991 assert(Ty.isDerivedType() && "Unknown kind of DIType");
992 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
993 }
David Blaikie2ea848b2013-11-19 22:51:04 +0000994
David Blaikie9d861be2013-11-26 00:15:27 +0000995 updateAcceleratorTables(Context, Ty, TyDIE);
David Blaikie2ea848b2013-11-19 22:51:04 +0000996
997 return TyDIE;
998}
999
David Blaikie319a05f2013-12-02 19:33:10 +00001000void Unit::updateAcceleratorTables(DIScope Context, DIType Ty,
1001 const DIE *TyDIE) {
Eric Christopher21bde872012-01-06 04:35:23 +00001002 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
1003 bool IsImplementation = 0;
1004 if (Ty.isCompositeType()) {
1005 DICompositeType CT(Ty);
Eric Christopher8ea8e4f2012-01-06 23:03:37 +00001006 // A runtime language of 0 actually means C/C++ and that any
1007 // non-negative value is some version of Objective-C/C++.
Eric Christopherc2697f82013-10-19 01:04:47 +00001008 IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete();
Eric Christopher21bde872012-01-06 04:35:23 +00001009 }
Eric Christophercf7289f2013-09-05 18:20:16 +00001010 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
Eric Christopher8ea8e4f2012-01-06 23:03:37 +00001011 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
David Blaikie9d861be2013-11-26 00:15:27 +00001012
1013 if (!Context || Context.isCompileUnit() || Context.isFile() ||
1014 Context.isNameSpace())
1015 GlobalTypes[getParentContextString(Context) + Ty.getName().str()] = TyDIE;
Eric Christopher21bde872012-01-06 04:35:23 +00001016 }
Devang Patel0e821f42011-04-12 23:21:44 +00001017}
1018
1019/// addType - Add a new type attribute to the specified entity.
David Blaikie319a05f2013-12-02 19:33:10 +00001020void Unit::addType(DIE *Entity, DIType Ty, dwarf::Attribute Attribute) {
Eric Christopher0df08e22013-08-08 07:40:37 +00001021 assert(Ty && "Trying to add a type that doesn't exist?");
Devang Patel0e821f42011-04-12 23:21:44 +00001022
1023 // Check for pre-existence.
1024 DIEEntry *Entry = getDIEEntry(Ty);
1025 // If it exists then use the existing value.
1026 if (Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +00001027 addDIEEntry(Entity, Attribute, Entry);
Devang Patel0e821f42011-04-12 23:21:44 +00001028 return;
1029 }
1030
1031 // Construct type.
1032 DIE *Buffer = getOrCreateTypeDIE(Ty);
1033
1034 // Set up proxy.
1035 Entry = createDIEEntry(Buffer);
1036 insertDIEEntry(Ty, Entry);
Manman Ren4dbdc902013-10-31 17:54:35 +00001037 addDIEEntry(Entity, Attribute, Entry);
Devang Patel1cb8ab42011-05-31 23:30:30 +00001038}
1039
Eric Christopher9cd26af2013-09-20 23:22:52 +00001040// Accelerator table mutators - add each name along with its companion
1041// DIE to the proper table while ensuring that the name that we're going
1042// to reference is in the string table. We do this since the names we
1043// add may not only be identical to the names in the DIE.
David Blaikie319a05f2013-12-02 19:33:10 +00001044void Unit::addAccelName(StringRef Name, const DIE *Die) {
Eric Christopher9cd26af2013-09-20 23:22:52 +00001045 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001046 std::vector<const DIE *> &DIEs = AccelNames[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001047 DIEs.push_back(Die);
1048}
1049
David Blaikie319a05f2013-12-02 19:33:10 +00001050void Unit::addAccelObjC(StringRef Name, const DIE *Die) {
Eric Christopher9cd26af2013-09-20 23:22:52 +00001051 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001052 std::vector<const DIE *> &DIEs = AccelObjC[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001053 DIEs.push_back(Die);
1054}
1055
David Blaikie319a05f2013-12-02 19:33:10 +00001056void Unit::addAccelNamespace(StringRef Name, const DIE *Die) {
Eric Christopher9cd26af2013-09-20 23:22:52 +00001057 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001058 std::vector<const DIE *> &DIEs = AccelNamespace[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001059 DIEs.push_back(Die);
1060}
1061
David Blaikie319a05f2013-12-02 19:33:10 +00001062void Unit::addAccelType(StringRef Name, std::pair<const DIE *, unsigned> Die) {
Eric Christopher9cd26af2013-09-20 23:22:52 +00001063 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001064 std::vector<std::pair<const DIE *, unsigned> > &DIEs = AccelTypes[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001065 DIEs.push_back(Die);
1066}
1067
Eric Christopher9c58f312013-09-20 22:20:55 +00001068/// addGlobalName - Add a new global name to the compile unit.
David Blaikie319a05f2013-12-02 19:33:10 +00001069void Unit::addGlobalName(StringRef Name, DIE *Die, DIScope Context) {
Eric Christopher8dba0d52013-10-19 01:04:42 +00001070 std::string FullName = getParentContextString(Context) + Name.str();
Eric Christopher2c8b7902013-10-17 02:06:06 +00001071 GlobalNames[FullName] = Die;
Eric Christopher9c58f312013-09-20 22:20:55 +00001072}
1073
Eric Christopher2c8b7902013-10-17 02:06:06 +00001074/// getParentContextString - Walks the metadata parent chain in a language
1075/// specific manner (using the compile unit language) and returns
1076/// it as a string. This is done at the metadata level because DIEs may
1077/// not currently have been added to the parent context and walking the
1078/// DIEs looking for names is more expensive than walking the metadata.
David Blaikie319a05f2013-12-02 19:33:10 +00001079std::string Unit::getParentContextString(DIScope Context) const {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001080 if (!Context)
1081 return "";
1082
1083 // FIXME: Decide whether to implement this for non-C++ languages.
1084 if (getLanguage() != dwarf::DW_LANG_C_plus_plus)
1085 return "";
1086
Eric Christopher8dba0d52013-10-19 01:04:42 +00001087 std::string CS;
Eric Christopher2c8b7902013-10-17 02:06:06 +00001088 SmallVector<DIScope, 1> Parents;
1089 while (!Context.isCompileUnit()) {
1090 Parents.push_back(Context);
1091 if (Context.getContext())
1092 Context = resolve(Context.getContext());
1093 else
1094 // Structure, etc types will have a NULL context if they're at the top
1095 // level.
1096 break;
1097 }
1098
1099 // Reverse iterate over our list to go from the outermost construct to the
1100 // innermost.
1101 for (SmallVectorImpl<DIScope>::reverse_iterator I = Parents.rbegin(),
1102 E = Parents.rend();
1103 I != E; ++I) {
1104 DIScope Ctx = *I;
1105 StringRef Name = Ctx.getName();
Eric Christopher8dba0d52013-10-19 01:04:42 +00001106 if (!Name.empty()) {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001107 CS += Name;
1108 CS += "::";
1109 }
1110 }
1111 return CS;
Devang Patel0e821f42011-04-12 23:21:44 +00001112}
1113
1114/// constructTypeDIE - Construct basic type die from DIBasicType.
David Blaikie319a05f2013-12-02 19:33:10 +00001115void Unit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Devang Patel0e821f42011-04-12 23:21:44 +00001116 // Get core information.
1117 StringRef Name = BTy.getName();
Devang Patel0e821f42011-04-12 23:21:44 +00001118 // Add name if not anonymous or intermediate type.
1119 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001120 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel04d6d472011-09-14 23:13:28 +00001121
David Blaikiefac56122013-10-04 23:21:16 +00001122 // An unspecified type only has a name attribute.
1123 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type)
Devang Patel04d6d472011-09-14 23:13:28 +00001124 return;
Devang Patel04d6d472011-09-14 23:13:28 +00001125
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001126 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Pateld925d1a2012-02-07 23:33:58 +00001127 BTy.getEncoding());
Devang Patel04d6d472011-09-14 23:13:28 +00001128
Devang Patel0e821f42011-04-12 23:21:44 +00001129 uint64_t Size = BTy.getSizeInBits() >> 3;
David Blaikief2443192013-10-21 17:28:37 +00001130 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001131}
1132
1133/// constructTypeDIE - Construct derived type die from DIDerivedType.
David Blaikie319a05f2013-12-02 19:33:10 +00001134void Unit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Devang Patel0e821f42011-04-12 23:21:44 +00001135 // Get core information.
1136 StringRef Name = DTy.getName();
1137 uint64_t Size = DTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001138 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001139
1140 // Map to main type, void will not have a type.
Manman Ren93b30902013-10-08 18:42:58 +00001141 DIType FromTy = resolve(DTy.getTypeDerivedFrom());
Eric Christopher0df08e22013-08-08 07:40:37 +00001142 if (FromTy)
1143 addType(&Buffer, FromTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001144
1145 // Add name if not anonymous or intermediate type.
1146 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001147 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001148
1149 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher85757902012-02-21 22:25:53 +00001150 if (Size && Tag != dwarf::DW_TAG_pointer_type)
David Blaikief2443192013-10-21 17:28:37 +00001151 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001152
David Blaikie5d3249b2013-01-07 05:51:15 +00001153 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
Eric Christopherc2697f82013-10-19 01:04:47 +00001154 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
1155 getOrCreateTypeDIE(resolve(DTy.getClassType())));
Devang Patel0e821f42011-04-12 23:21:44 +00001156 // Add source line info if available and TyDesc is not a forward declaration.
1157 if (!DTy.isForwardDecl())
1158 addSourceLine(&Buffer, DTy);
1159}
1160
1161/// constructTypeDIE - Construct type DIE from DICompositeType.
David Blaikie319a05f2013-12-02 19:33:10 +00001162void Unit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
David Blaikie409dd9c2013-11-19 23:08:21 +00001163 // Add name if not anonymous or intermediate type.
Devang Patel0e821f42011-04-12 23:21:44 +00001164 StringRef Name = CTy.getName();
1165
1166 uint64_t Size = CTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001167 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001168
1169 switch (Tag) {
Devang Patel0e821f42011-04-12 23:21:44 +00001170 case dwarf::DW_TAG_array_type:
Eric Christopherdf9955d2013-11-11 18:52:31 +00001171 constructArrayTypeDIE(Buffer, CTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001172 break;
Eric Christopheraeb105f2013-11-11 18:52:39 +00001173 case dwarf::DW_TAG_enumeration_type:
1174 constructEnumTypeDIE(Buffer, CTy);
1175 break;
Devang Patel0e821f42011-04-12 23:21:44 +00001176 case dwarf::DW_TAG_subroutine_type: {
Eric Christopher0df08e22013-08-08 07:40:37 +00001177 // Add return type. A void return won't have a type.
Devang Patel0e821f42011-04-12 23:21:44 +00001178 DIArray Elements = CTy.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001179 DIType RTy(Elements.getElement(0));
Eric Christopher0df08e22013-08-08 07:40:37 +00001180 if (RTy)
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001181 addType(&Buffer, RTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001182
1183 bool isPrototyped = true;
1184 // Add arguments.
1185 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1186 DIDescriptor Ty = Elements.getElement(i);
1187 if (Ty.isUnspecifiedParameter()) {
Manman Renb987e512013-10-29 00:53:03 +00001188 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001189 isPrototyped = false;
1190 } else {
Manman Renb987e512013-10-29 00:53:03 +00001191 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001192 addType(Arg, DIType(Ty));
David Blaikie9a7a7a92013-01-29 19:35:24 +00001193 if (DIType(Ty).isArtificial())
1194 addFlag(Arg, dwarf::DW_AT_artificial);
Devang Patel0e821f42011-04-12 23:21:44 +00001195 }
1196 }
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001197 // Add prototype flag if we're dealing with a C language and the
1198 // function has been prototyped.
David Blaikiecb8e4352013-11-15 23:50:53 +00001199 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001200 if (isPrototyped &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001201 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopherd42b92f2012-05-22 18:45:24 +00001202 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001203 addFlag(&Buffer, dwarf::DW_AT_prototyped);
Eric Christopherc2697f82013-10-19 01:04:47 +00001204 } break;
Devang Patel0e821f42011-04-12 23:21:44 +00001205 case dwarf::DW_TAG_structure_type:
1206 case dwarf::DW_TAG_union_type:
1207 case dwarf::DW_TAG_class_type: {
Devang Patel0e821f42011-04-12 23:21:44 +00001208 // Add elements to structure type.
David Blaikiea1ae0e62013-08-01 20:30:22 +00001209 DIArray Elements = CTy.getTypeArray();
1210 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel0e821f42011-04-12 23:21:44 +00001211 DIDescriptor Element = Elements.getElement(i);
1212 DIE *ElemDie = NULL;
1213 if (Element.isSubprogram()) {
1214 DISubprogram SP(Element);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001215 ElemDie = getOrCreateSubprogramDIE(SP);
Devang Patel0e821f42011-04-12 23:21:44 +00001216 if (SP.isProtected())
Nick Lewyckycb918492011-12-13 05:09:11 +00001217 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001218 dwarf::DW_ACCESS_protected);
1219 else if (SP.isPrivate())
Nick Lewyckycb918492011-12-13 05:09:11 +00001220 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001221 dwarf::DW_ACCESS_private);
Eric Christopher92331fd2012-11-21 00:34:38 +00001222 else
Nick Lewyckycb918492011-12-13 05:09:11 +00001223 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Eric Christopherc2697f82013-10-19 01:04:47 +00001224 dwarf::DW_ACCESS_public);
Devang Patel0e821f42011-04-12 23:21:44 +00001225 if (SP.isExplicit())
Eric Christopherbb69a272012-08-24 01:14:27 +00001226 addFlag(ElemDie, dwarf::DW_AT_explicit);
Eric Christopher7285c7d2012-03-28 07:34:31 +00001227 } else if (Element.isDerivedType()) {
Eric Christopherd42b92f2012-05-22 18:45:24 +00001228 DIDerivedType DDTy(Element);
1229 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
Manman Renb987e512013-10-29 00:53:03 +00001230 ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
Manman Ren93b30902013-10-08 18:42:58 +00001231 addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()),
Manman Renb3388602013-10-05 01:43:03 +00001232 dwarf::DW_AT_friend);
Manman Renc6b63922013-10-14 20:33:57 +00001233 } else if (DDTy.isStaticMember()) {
Manman Ren57e6ff72013-10-23 22:57:12 +00001234 getOrCreateStaticMemberDIE(DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001235 } else {
Manman Ren230ec862013-10-23 23:00:44 +00001236 constructMemberDIE(Buffer, DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001237 }
Eric Christopher7285c7d2012-03-28 07:34:31 +00001238 } else if (Element.isObjCProperty()) {
Devang Pateld925d1a2012-02-07 23:33:58 +00001239 DIObjCProperty Property(Element);
Manman Renb987e512013-10-29 00:53:03 +00001240 ElemDie = createAndAddDIE(Property.getTag(), Buffer);
Devang Pateld925d1a2012-02-07 23:33:58 +00001241 StringRef PropertyName = Property.getObjCPropertyName();
1242 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Eric Christopherd42b92f2012-05-22 18:45:24 +00001243 addType(ElemDie, Property.getType());
1244 addSourceLine(ElemDie, Property);
Devang Pateld925d1a2012-02-07 23:33:58 +00001245 StringRef GetterName = Property.getObjCPropertyGetterName();
1246 if (!GetterName.empty())
1247 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
1248 StringRef SetterName = Property.getObjCPropertySetterName();
1249 if (!SetterName.empty())
1250 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
1251 unsigned PropertyAttributes = 0;
Devang Patel403e8192012-02-04 01:30:32 +00001252 if (Property.isReadOnlyObjCProperty())
1253 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1254 if (Property.isReadWriteObjCProperty())
1255 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1256 if (Property.isAssignObjCProperty())
1257 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1258 if (Property.isRetainObjCProperty())
1259 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1260 if (Property.isCopyObjCProperty())
1261 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1262 if (Property.isNonAtomicObjCProperty())
1263 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1264 if (PropertyAttributes)
David Blaikief2443192013-10-21 17:28:37 +00001265 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None,
Eric Christopherc2697f82013-10-19 01:04:47 +00001266 PropertyAttributes);
Devang Patel44882172012-02-06 17:49:43 +00001267
Devang Pateld925d1a2012-02-07 23:33:58 +00001268 DIEEntry *Entry = getDIEEntry(Element);
1269 if (!Entry) {
1270 Entry = createDIEEntry(ElemDie);
1271 insertDIEEntry(Element, Entry);
1272 }
Devang Patel403e8192012-02-04 01:30:32 +00001273 } else
Devang Patel0e821f42011-04-12 23:21:44 +00001274 continue;
Devang Patel0e821f42011-04-12 23:21:44 +00001275 }
1276
1277 if (CTy.isAppleBlockExtension())
Eric Christopherbb69a272012-08-24 01:14:27 +00001278 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel0e821f42011-04-12 23:21:44 +00001279
Eric Christopher9e429ae2013-10-05 00:27:02 +00001280 DICompositeType ContainingType(resolve(CTy.getContainingType()));
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001281 if (ContainingType)
Manman Ren4c4b69c2013-10-11 23:58:05 +00001282 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001283 getOrCreateTypeDIE(ContainingType));
Devang Patel0e821f42011-04-12 23:21:44 +00001284
Devang Patel12419ae2011-05-12 21:29:42 +00001285 if (CTy.isObjcClassComplete())
Eric Christopherbb69a272012-08-24 01:14:27 +00001286 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patel2409e782011-05-12 19:06:16 +00001287
Eric Christopherda011dd2011-12-16 23:42:42 +00001288 // Add template parameters to a class, structure or union types.
1289 // FIXME: The support isn't in the metadata for this yet.
1290 if (Tag == dwarf::DW_TAG_class_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001291 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
Devang Patel0e821f42011-04-12 23:21:44 +00001292 addTemplateParams(Buffer, CTy.getTemplateParams());
1293
1294 break;
1295 }
1296 default:
1297 break;
1298 }
1299
1300 // Add name if not anonymous or intermediate type.
1301 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001302 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001303
Eric Christopher775cbd22012-05-22 18:45:18 +00001304 if (Tag == dwarf::DW_TAG_enumeration_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001305 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
Eric Christopher775cbd22012-05-22 18:45:18 +00001306 Tag == dwarf::DW_TAG_union_type) {
Devang Patel0e821f42011-04-12 23:21:44 +00001307 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher1cf33382012-06-01 00:22:32 +00001308 // TODO: Do we care about size for enum forward declarations?
Devang Patel0e821f42011-04-12 23:21:44 +00001309 if (Size)
David Blaikief2443192013-10-21 17:28:37 +00001310 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Eric Christopher1cf33382012-06-01 00:22:32 +00001311 else if (!CTy.isForwardDecl())
Devang Patel0e821f42011-04-12 23:21:44 +00001312 // Add zero size if it is not a forward declaration.
David Blaikief2443192013-10-21 17:28:37 +00001313 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, 0);
Eric Christopher1cf33382012-06-01 00:22:32 +00001314
1315 // If we're a forward decl, say so.
1316 if (CTy.isForwardDecl())
Eric Christopherbb69a272012-08-24 01:14:27 +00001317 addFlag(&Buffer, dwarf::DW_AT_declaration);
Devang Patel0e821f42011-04-12 23:21:44 +00001318
1319 // Add source line info if available.
1320 if (!CTy.isForwardDecl())
1321 addSourceLine(&Buffer, CTy);
Eric Christopher54cf8ff2012-03-07 00:15:19 +00001322
1323 // No harm in adding the runtime language to the declaration.
1324 unsigned RLang = CTy.getRunTimeLang();
1325 if (RLang)
Eric Christopherc2697f82013-10-19 01:04:47 +00001326 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
1327 RLang);
Devang Patel0e821f42011-04-12 23:21:44 +00001328 }
1329}
1330
Manman Renffc9a712013-10-23 23:05:28 +00001331/// constructTemplateTypeParameterDIE - Construct new DIE for the given
1332/// DITemplateTypeParameter.
David Blaikie319a05f2013-12-02 19:33:10 +00001333void Unit::constructTemplateTypeParameterDIE(DIE &Buffer,
1334 DITemplateTypeParameter TP) {
Manman Renb987e512013-10-29 00:53:03 +00001335 DIE *ParamDIE =
1336 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
Eric Christopher056b6472013-08-08 08:09:43 +00001337 // Add the type if it exists, it could be void and therefore no type.
1338 if (TP.getType())
Manman Ren88b0f942013-10-09 19:46:28 +00001339 addType(ParamDIE, resolve(TP.getType()));
David Blaikie2b380232013-06-22 18:59:11 +00001340 if (!TP.getName().empty())
1341 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel0e821f42011-04-12 23:21:44 +00001342}
1343
Manman Renffc9a712013-10-23 23:05:28 +00001344/// constructTemplateValueParameterDIE - Construct new DIE for the given
1345/// DITemplateValueParameter.
David Blaikie319a05f2013-12-02 19:33:10 +00001346void Unit::constructTemplateValueParameterDIE(DIE &Buffer,
1347 DITemplateValueParameter VP) {
Manman Renb987e512013-10-29 00:53:03 +00001348 DIE *ParamDIE = createAndAddDIE(VP.getTag(), Buffer);
Eric Christopher0df08e22013-08-08 07:40:37 +00001349
1350 // Add the type if there is one, template template and template parameter
1351 // packs will not have a type.
Eric Christopher691281b2013-10-21 17:48:51 +00001352 if (VP.getTag() == dwarf::DW_TAG_template_value_parameter)
Manman Ren88b0f942013-10-09 19:46:28 +00001353 addType(ParamDIE, resolve(VP.getType()));
Eric Christopherafb2c412013-08-08 07:40:31 +00001354 if (!VP.getName().empty())
1355 addString(ParamDIE, dwarf::DW_AT_name, VP.getName());
1356 if (Value *Val = VP.getValue()) {
David Blaikiea1e813d2013-05-10 21:52:07 +00001357 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val))
Manman Ren88b0f942013-10-09 19:46:28 +00001358 addConstantValue(ParamDIE, CI,
1359 isUnsignedDIType(DD, resolve(VP.getType())));
David Blaikiea1e813d2013-05-10 21:52:07 +00001360 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) {
1361 // For declaration non-type template parameters (such as global values and
1362 // functions)
1363 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Rafael Espindola79858aa2013-10-29 17:07:16 +00001364 addOpAddress(Block, Asm->getSymbol(GV));
David Blaikiea1e813d2013-05-10 21:52:07 +00001365 // Emit DW_OP_stack_value to use the address as the immediate value of the
1366 // parameter, rather than a pointer to it.
David Blaikief2443192013-10-21 17:28:37 +00001367 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1368 addBlock(ParamDIE, dwarf::DW_AT_location, Block);
Eric Christopherafb2c412013-08-08 07:40:31 +00001369 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) {
David Blaikie2b380232013-06-22 18:59:11 +00001370 assert(isa<MDString>(Val));
1371 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1372 cast<MDString>(Val)->getString());
Eric Christopherafb2c412013-08-08 07:40:31 +00001373 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
David Blaikie2b380232013-06-22 18:59:11 +00001374 assert(isa<MDNode>(Val));
1375 DIArray A(cast<MDNode>(Val));
1376 addTemplateParams(*ParamDIE, A);
David Blaikiea1e813d2013-05-10 21:52:07 +00001377 }
1378 }
Devang Patel0e821f42011-04-12 23:21:44 +00001379}
1380
Devang Patel17b53272011-05-06 16:57:54 +00001381/// getOrCreateNameSpace - Create a DIE for DINameSpace.
David Blaikie319a05f2013-12-02 19:33:10 +00001382DIE *Unit::getOrCreateNameSpace(DINameSpace NS) {
Manman Renf6b936b2013-10-29 05:49:41 +00001383 // Construct the context before querying for the existence of the DIE in case
1384 // such construction creates the DIE.
1385 DIE *ContextDIE = getOrCreateContextDIE(NS.getContext());
Manman Renf6b936b2013-10-29 05:49:41 +00001386
Devang Patel17b53272011-05-06 16:57:54 +00001387 DIE *NDie = getDIE(NS);
1388 if (NDie)
1389 return NDie;
Manman Renf6b936b2013-10-29 05:49:41 +00001390 NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
1391
Eric Christopher4996c702011-11-07 09:24:32 +00001392 if (!NS.getName().empty()) {
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001393 addString(NDie, dwarf::DW_AT_name, NS.getName());
Eric Christopher4996c702011-11-07 09:24:32 +00001394 addAccelNamespace(NS.getName(), NDie);
Eric Christopher2c8b7902013-10-17 02:06:06 +00001395 addGlobalName(NS.getName(), NDie, NS.getContext());
Eric Christopher4996c702011-11-07 09:24:32 +00001396 } else
1397 addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel17b53272011-05-06 16:57:54 +00001398 addSourceLine(NDie, NS);
Devang Patel17b53272011-05-06 16:57:54 +00001399 return NDie;
1400}
1401
Devang Patel89543712011-08-15 17:24:54 +00001402/// getOrCreateSubprogramDIE - Create new DIE using SP.
David Blaikie319a05f2013-12-02 19:33:10 +00001403DIE *Unit::getOrCreateSubprogramDIE(DISubprogram SP) {
David Blaikie309ffe42013-10-04 01:39:59 +00001404 // Construct the context before querying for the existence of the DIE in case
1405 // such construction creates the DIE (as is the case for member function
1406 // declarations).
Manman Renc50fa112013-10-10 18:40:01 +00001407 DIE *ContextDIE = getOrCreateContextDIE(resolve(SP.getContext()));
David Blaikie309ffe42013-10-04 01:39:59 +00001408
Eric Christophere595bae2013-10-04 17:08:38 +00001409 DIE *SPDie = getDIE(SP);
Devang Patel89543712011-08-15 17:24:54 +00001410 if (SPDie)
1411 return SPDie;
1412
Manman Ren73d697c2013-10-29 00:58:04 +00001413 DISubprogram SPDecl = SP.getFunctionDeclaration();
1414 if (SPDecl.isSubprogram())
1415 // Add subprogram definitions to the CU die directly.
David Blaikie2a80e442013-12-02 22:09:48 +00001416 ContextDIE = UnitDie.get();
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001417
1418 // DW_TAG_inlined_subroutine may refer to this DIE.
Manman Ren73d697c2013-10-29 00:58:04 +00001419 SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001420
Rafael Espindola79278362011-11-10 22:34:29 +00001421 DIE *DeclDie = NULL;
Manman Renb9512a72013-10-23 22:12:26 +00001422 if (SPDecl.isSubprogram())
Rafael Espindola79278362011-11-10 22:34:29 +00001423 DeclDie = getOrCreateSubprogramDIE(SPDecl);
Rafael Espindola79278362011-11-10 22:34:29 +00001424
Devang Patel89543712011-08-15 17:24:54 +00001425 // Add function template parameters.
1426 addTemplateParams(*SPDie, SP.getTemplateParams());
1427
Devang Patel89543712011-08-15 17:24:54 +00001428 // If this DIE is going to refer declaration info using AT_specification
Eric Christopherf20ff972013-05-09 00:42:33 +00001429 // then there is no need to add other attributes.
Rafael Espindola79278362011-11-10 22:34:29 +00001430 if (DeclDie) {
1431 // Refer function declaration directly.
Manman Ren4c4b69c2013-10-11 23:58:05 +00001432 addDIEEntry(SPDie, dwarf::DW_AT_specification, DeclDie);
Rafael Espindola79278362011-11-10 22:34:29 +00001433
Devang Patel89543712011-08-15 17:24:54 +00001434 return SPDie;
Rafael Espindola79278362011-11-10 22:34:29 +00001435 }
Devang Patel89543712011-08-15 17:24:54 +00001436
Eric Christopheracb71152012-08-23 22:52:55 +00001437 // Add the linkage name if we have one.
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001438 StringRef LinkageName = SP.getLinkageName();
1439 if (!LinkageName.empty())
Eric Christopheracb71152012-08-23 22:52:55 +00001440 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001441 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopheracb71152012-08-23 22:52:55 +00001442
Devang Patel89543712011-08-15 17:24:54 +00001443 // Constructors and operators for anonymous aggregates do not have names.
1444 if (!SP.getName().empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001445 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Patel89543712011-08-15 17:24:54 +00001446
1447 addSourceLine(SPDie, SP);
1448
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001449 // Add the prototype if we have a prototype and we have a C like
1450 // language.
David Blaikiecb8e4352013-11-15 23:50:53 +00001451 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001452 if (SP.isPrototyped() &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001453 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001454 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001455 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Patel89543712011-08-15 17:24:54 +00001456
Devang Patel89543712011-08-15 17:24:54 +00001457 DICompositeType SPTy = SP.getType();
David Blaikie5174c842013-05-22 23:22:18 +00001458 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type &&
1459 "the type of a subprogram should be a subroutine");
Devang Patel89543712011-08-15 17:24:54 +00001460
David Blaikie5174c842013-05-22 23:22:18 +00001461 DIArray Args = SPTy.getTypeArray();
Eric Christopher691281b2013-10-21 17:48:51 +00001462 // Add a return type. If this is a type like a C/C++ void type we don't add a
1463 // return type.
Eric Christopher0df08e22013-08-08 07:40:37 +00001464 if (Args.getElement(0))
1465 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel89543712011-08-15 17:24:54 +00001466
1467 unsigned VK = SP.getVirtuality();
1468 if (VK) {
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001469 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Devang Patel89543712011-08-15 17:24:54 +00001470 DIEBlock *Block = getDIEBlock();
David Blaikief2443192013-10-21 17:28:37 +00001471 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1472 addUInt(Block, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1473 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
Eric Christopher98b7f172013-11-11 18:52:36 +00001474 ContainingTypeMap.insert(
1475 std::make_pair(SPDie, resolve(SP.getContainingType())));
Devang Patel89543712011-08-15 17:24:54 +00001476 }
1477
1478 if (!SP.isDefinition()) {
Eric Christopherbb69a272012-08-24 01:14:27 +00001479 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher92331fd2012-11-21 00:34:38 +00001480
Devang Patel89543712011-08-15 17:24:54 +00001481 // Add arguments. Do not add arguments for subprogram definition. They will
1482 // be handled while processing variables.
Eric Christopherc2697f82013-10-19 01:04:47 +00001483 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
Manman Renb987e512013-10-29 00:53:03 +00001484 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, *SPDie);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001485 DIType ATy(Args.getElement(i));
David Blaikie5174c842013-05-22 23:22:18 +00001486 addType(Arg, ATy);
1487 if (ATy.isArtificial())
1488 addFlag(Arg, dwarf::DW_AT_artificial);
David Blaikie5174c842013-05-22 23:22:18 +00001489 }
Devang Patel89543712011-08-15 17:24:54 +00001490 }
1491
1492 if (SP.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001493 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Patel89543712011-08-15 17:24:54 +00001494
1495 if (!SP.isLocalToUnit())
Eric Christopherbb69a272012-08-24 01:14:27 +00001496 addFlag(SPDie, dwarf::DW_AT_external);
Devang Patel89543712011-08-15 17:24:54 +00001497
1498 if (SP.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +00001499 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Patel89543712011-08-15 17:24:54 +00001500
1501 if (unsigned isa = Asm->getISAEncoding()) {
1502 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1503 }
1504
1505 return SPDie;
1506}
1507
Devang Pateldfd6ec32011-08-15 17:57:41 +00001508// Return const expression if value is a GEP to access merged global
1509// constant. e.g.
1510// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1511static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1512 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1513 if (!CE || CE->getNumOperands() != 3 ||
1514 CE->getOpcode() != Instruction::GetElementPtr)
1515 return NULL;
1516
1517 // First operand points to a global struct.
1518 Value *Ptr = CE->getOperand(0);
1519 if (!isa<GlobalValue>(Ptr) ||
1520 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1521 return NULL;
1522
1523 // Second operand is zero.
1524 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1525 if (!CI || !CI->isZero())
1526 return NULL;
1527
1528 // Third operand is offset.
1529 if (!isa<ConstantInt>(CE->getOperand(2)))
1530 return NULL;
1531
1532 return CE;
1533}
1534
1535/// createGlobalVariableDIE - create global variable DIE.
David Blaikiea781b25b2013-11-17 21:55:13 +00001536void CompileUnit::createGlobalVariableDIE(DIGlobalVariable GV) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001537 // Check for pre-existence.
David Blaikie2ad00162013-11-15 23:09:13 +00001538 if (getDIE(GV))
Devang Pateldfd6ec32011-08-15 17:57:41 +00001539 return;
1540
Manman Ren7504ed42013-07-08 18:33:29 +00001541 if (!GV.isGlobalVariable())
Devang Patel0ecbcbd2011-08-18 23:17:55 +00001542 return;
1543
Eric Christopher08f7c8f2013-10-04 23:49:26 +00001544 DIScope GVContext = GV.getContext();
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001545 DIType GTy = GV.getType();
1546
1547 // If this is a static data member definition, some attributes belong
1548 // to the declaration DIE.
1549 DIE *VariableDIE = NULL;
Manman Rene697d3c2013-02-01 23:54:37 +00001550 bool IsStaticMember = false;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001551 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1552 if (SDMDecl.Verify()) {
1553 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1554 // We need the declaration DIE that is in the static member's class.
Manman Renc6b63922013-10-14 20:33:57 +00001555 VariableDIE = getOrCreateStaticMemberDIE(SDMDecl);
Manman Rene697d3c2013-02-01 23:54:37 +00001556 IsStaticMember = true;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001557 }
1558
1559 // If this is not a static data member definition, create the variable
1560 // DIE and add the initial set of attributes to it.
1561 if (!VariableDIE) {
Manman Renf6b936b2013-10-29 05:49:41 +00001562 // Construct the context before querying for the existence of the DIE in
1563 // case such construction creates the DIE.
1564 DIE *ContextDIE = getOrCreateContextDIE(GVContext);
Manman Renf6b936b2013-10-29 05:49:41 +00001565
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001566 // Add to map.
David Blaikie52c50202013-11-16 00:29:01 +00001567 VariableDIE = createAndAddDIE(GV.getTag(), *ContextDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001568
1569 // Add name and type.
1570 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1571 addType(VariableDIE, GTy);
1572
1573 // Add scoping info.
Eric Christopherd2b497b2013-10-16 01:37:49 +00001574 if (!GV.isLocalToUnit())
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001575 addFlag(VariableDIE, dwarf::DW_AT_external);
1576
1577 // Add line number info.
1578 addSourceLine(VariableDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001579 }
1580
Devang Pateldfd6ec32011-08-15 17:57:41 +00001581 // Add location.
Eric Christopher4996c702011-11-07 09:24:32 +00001582 bool addToAccelTable = false;
Eric Christopher0a917b72011-11-11 03:16:32 +00001583 DIE *VariableSpecDIE = NULL;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001584 bool isGlobalVariable = GV.getGlobal() != NULL;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001585 if (isGlobalVariable) {
Eric Christopher4996c702011-11-07 09:24:32 +00001586 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001587 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Rafael Espindola79858aa2013-10-29 17:07:16 +00001588 const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal());
David Blaikief2694972013-06-28 20:05:11 +00001589 if (GV.getGlobal()->isThreadLocal()) {
1590 // FIXME: Make this work with -gsplit-dwarf.
1591 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
1592 assert((PointerSize == 4 || PointerSize == 8) &&
1593 "Add support for other sizes if necessary");
Ulrich Weigand2b6fc8d2013-07-02 18:47:09 +00001594 const MCExpr *Expr =
David Blaikie8466ca82013-07-01 23:55:52 +00001595 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym);
David Blaikief2694972013-06-28 20:05:11 +00001596 // Based on GCC's support for TLS:
David Blaikie8466ca82013-07-01 23:55:52 +00001597 if (!DD->useSplitDwarf()) {
1598 // 1) Start with a constNu of the appropriate pointer size
David Blaikief2443192013-10-21 17:28:37 +00001599 addUInt(Block, dwarf::DW_FORM_data1,
David Blaikie8466ca82013-07-01 23:55:52 +00001600 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
Richard Mitton0aafb582013-10-07 18:39:18 +00001601 // 2) containing the (relocated) offset of the TLS variable
1602 // within the module's TLS block.
David Blaikief2443192013-10-21 17:28:37 +00001603 addExpr(Block, dwarf::DW_FORM_udata, Expr);
David Blaikie8466ca82013-07-01 23:55:52 +00001604 } else {
David Blaikief2443192013-10-21 17:28:37 +00001605 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
1606 addUInt(Block, dwarf::DW_FORM_udata, DU->getAddrPoolIndex(Expr));
David Blaikie8466ca82013-07-01 23:55:52 +00001607 }
Richard Mitton0aafb582013-10-07 18:39:18 +00001608 // 3) followed by a custom OP to make the debugger do a TLS lookup.
David Blaikief2443192013-10-21 17:28:37 +00001609 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001610 } else {
1611 DD->addArangeLabel(SymbolCU(this, Sym));
David Blaikief2694972013-06-28 20:05:11 +00001612 addOpAddress(Block, Sym);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001613 }
Devang Pateldfd6ec32011-08-15 17:57:41 +00001614 // Do not create specification DIE if context is either compile unit
1615 // or a subprogram.
Devang Patel5e6b65c2011-09-21 23:41:11 +00001616 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Manman Ren3eb9dff2013-09-09 19:05:21 +00001617 !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001618 // Create specification DIE.
David Blaikie2a80e442013-12-02 22:09:48 +00001619 VariableSpecDIE = createAndAddDIE(dwarf::DW_TAG_variable, *UnitDie);
Manman Ren4c4b69c2013-10-11 23:58:05 +00001620 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification, VariableDIE);
David Blaikief2443192013-10-21 17:28:37 +00001621 addBlock(VariableSpecDIE, dwarf::DW_AT_location, Block);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001622 // A static member's declaration is already flagged as such.
1623 if (!SDMDecl.Verify())
1624 addFlag(VariableDIE, dwarf::DW_AT_declaration);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001625 } else {
David Blaikief2443192013-10-21 17:28:37 +00001626 addBlock(VariableDIE, dwarf::DW_AT_location, Block);
Eric Christopher4996c702011-11-07 09:24:32 +00001627 }
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001628 // Add the linkage name.
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001629 StringRef LinkageName = GV.getLinkageName();
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001630 if (!LinkageName.empty())
Eric Christopher3f79b8c2013-02-27 23:49:47 +00001631 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1632 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1633 // TAG_variable.
Eric Christopherc2697f82013-10-19 01:04:47 +00001634 addString(IsStaticMember && VariableSpecDIE ? VariableSpecDIE
1635 : VariableDIE,
1636 dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001637 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopher92331fd2012-11-21 00:34:38 +00001638 } else if (const ConstantInt *CI =
Eric Christopherc2697f82013-10-19 01:04:47 +00001639 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
Adrian Prantl322f41d2013-04-04 22:56:49 +00001640 // AT_const_value was added when the static member was created. To avoid
Manman Rene697d3c2013-02-01 23:54:37 +00001641 // emitting AT_const_value multiple times, we only add AT_const_value when
1642 // it is not a static member.
1643 if (!IsStaticMember)
Manman Renb3388602013-10-05 01:43:03 +00001644 addConstantValue(VariableDIE, CI, isUnsignedDIType(DD, GTy));
David Blaikiea781b25b2013-11-17 21:55:13 +00001645 } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getOperand(11))) {
Eric Christopher4996c702011-11-07 09:24:32 +00001646 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001647 // GV is a merged global.
1648 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1649 Value *Ptr = CE->getOperand(0);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001650 MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr));
1651 DD->addArangeLabel(SymbolCU(this, Sym));
1652 addOpAddress(Block, Sym);
David Blaikief2443192013-10-21 17:28:37 +00001653 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
Eric Christopherc2697f82013-10-19 01:04:47 +00001654 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
David Blaikief2443192013-10-21 17:28:37 +00001655 addUInt(Block, dwarf::DW_FORM_udata,
Eric Christopherc2697f82013-10-19 01:04:47 +00001656 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
David Blaikief2443192013-10-21 17:28:37 +00001657 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1658 addBlock(VariableDIE, dwarf::DW_AT_location, Block);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001659 }
1660
Eric Christopherc12c2112011-11-11 01:55:22 +00001661 if (addToAccelTable) {
1662 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1663 addAccelName(GV.getName(), AddrDIE);
Eric Christopher4996c702011-11-07 09:24:32 +00001664
Eric Christopherc12c2112011-11-11 01:55:22 +00001665 // If the linkage name is different than the name, go ahead and output
1666 // that as well into the name table.
1667 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1668 addAccelName(GV.getLinkageName(), AddrDIE);
1669 }
Eric Christopherd2b497b2013-10-16 01:37:49 +00001670
1671 if (!GV.isLocalToUnit())
Eric Christopher2c8b7902013-10-17 02:06:06 +00001672 addGlobalName(GV.getName(), VariableSpecDIE ? VariableSpecDIE : VariableDIE,
1673 GV.getContext());
Devang Pateldfd6ec32011-08-15 17:57:41 +00001674}
1675
Devang Patel0e821f42011-04-12 23:21:44 +00001676/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
David Blaikie319a05f2013-12-02 19:33:10 +00001677void Unit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Manman Renb987e512013-10-29 00:53:03 +00001678 DIE *DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
Manman Ren4c4b69c2013-10-11 23:58:05 +00001679 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, IndexTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001680
Bill Wendling28fe9e72012-12-06 07:38:10 +00001681 // The LowerBound value defines the lower bounds which is typically zero for
1682 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1683 // Count == -1 then the array is unbounded and we do not emit
1684 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1685 // Count == 0, then the array has zero elements in which case we do not emit
1686 // an upper bound.
1687 int64_t LowerBound = SR.getLo();
Bill Wendling3495f9b2012-12-06 07:55:19 +00001688 int64_t DefaultLowerBound = getDefaultLowerBound();
Bill Wendlingd7767122012-12-04 21:34:03 +00001689 int64_t Count = SR.getCount();
Devang Patel0e821f42011-04-12 23:21:44 +00001690
Bill Wendling3495f9b2012-12-06 07:55:19 +00001691 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
David Blaikief2443192013-10-21 17:28:37 +00001692 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound);
Bill Wendling28fe9e72012-12-06 07:38:10 +00001693
1694 if (Count != -1 && Count != 0)
Bill Wendlingd7767122012-12-04 21:34:03 +00001695 // FIXME: An unbounded array should reference the expression that defines
1696 // the array.
Eric Christopher98b7f172013-11-11 18:52:36 +00001697 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None,
1698 LowerBound + Count - 1);
Devang Patel0e821f42011-04-12 23:21:44 +00001699}
1700
1701/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
David Blaikie319a05f2013-12-02 19:33:10 +00001702void Unit::constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopherdf9955d2013-11-11 18:52:31 +00001703 if (CTy.isVector())
Eric Christopherbb69a272012-08-24 01:14:27 +00001704 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel0e821f42011-04-12 23:21:44 +00001705
Eric Christopher0df08e22013-08-08 07:40:37 +00001706 // Emit the element type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001707 addType(&Buffer, resolve(CTy.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001708
1709 // Get an anonymous type for index type.
Eric Christophercad9b532013-01-04 21:51:53 +00001710 // FIXME: This type should be passed down from the front end
1711 // as different languages may have different sizes for indexes.
Devang Patel0e821f42011-04-12 23:21:44 +00001712 DIE *IdxTy = getIndexTyDie();
1713 if (!IdxTy) {
1714 // Construct an anonymous type for index type.
David Blaikie2a80e442013-12-02 22:09:48 +00001715 IdxTy = createAndAddDIE(dwarf::DW_TAG_base_type, *UnitDie);
Eric Christophercad9b532013-01-04 21:51:53 +00001716 addString(IdxTy, dwarf::DW_AT_name, "int");
David Blaikief2443192013-10-21 17:28:37 +00001717 addUInt(IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int32_t));
Devang Patel0e821f42011-04-12 23:21:44 +00001718 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1719 dwarf::DW_ATE_signed);
Devang Patel0e821f42011-04-12 23:21:44 +00001720 setIndexTyDie(IdxTy);
1721 }
1722
1723 // Add subranges to array type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001724 DIArray Elements = CTy.getTypeArray();
Devang Patel0e821f42011-04-12 23:21:44 +00001725 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1726 DIDescriptor Element = Elements.getElement(i);
1727 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1728 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1729 }
1730}
1731
Eric Christopheraeb105f2013-11-11 18:52:39 +00001732/// constructEnumTypeDIE - Construct an enum type DIE from DICompositeType.
David Blaikie319a05f2013-12-02 19:33:10 +00001733void Unit::constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopheraeb105f2013-11-11 18:52:39 +00001734 DIArray Elements = CTy.getTypeArray();
1735
1736 // Add enumerators to enumeration type.
1737 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001738 DIEnumerator Enum(Elements.getElement(i));
Eric Christopheraeb105f2013-11-11 18:52:39 +00001739 if (Enum.isEnumerator()) {
1740 DIE *Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001741 StringRef Name = Enum.getName();
Eric Christopheraeb105f2013-11-11 18:52:39 +00001742 addString(Enumerator, dwarf::DW_AT_name, Name);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001743 int64_t Value = Enum.getEnumValue();
Eric Christophera07e4f52013-11-19 09:28:34 +00001744 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
1745 Value);
Eric Christopheraeb105f2013-11-11 18:52:39 +00001746 }
1747 }
1748 DIType DTy = resolve(CTy.getTypeDerivedFrom());
1749 if (DTy) {
1750 addType(&Buffer, DTy);
1751 addFlag(&Buffer, dwarf::DW_AT_enum_class);
1752 }
Devang Patel0e821f42011-04-12 23:21:44 +00001753}
1754
Devang Patel89543712011-08-15 17:24:54 +00001755/// constructContainingTypeDIEs - Construct DIEs for types that contain
1756/// vtables.
David Blaikie319a05f2013-12-02 19:33:10 +00001757void Unit::constructContainingTypeDIEs() {
Devang Patel89543712011-08-15 17:24:54 +00001758 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Eric Christopherc2697f82013-10-19 01:04:47 +00001759 CE = ContainingTypeMap.end();
1760 CI != CE; ++CI) {
Devang Patel89543712011-08-15 17:24:54 +00001761 DIE *SPDie = CI->first;
David Blaikie2ad00162013-11-15 23:09:13 +00001762 DIDescriptor D(CI->second);
1763 if (!D)
Eric Christopherc2697f82013-10-19 01:04:47 +00001764 continue;
David Blaikie2ad00162013-11-15 23:09:13 +00001765 DIE *NDie = getDIE(D);
Eric Christopherc2697f82013-10-19 01:04:47 +00001766 if (!NDie)
1767 continue;
Manman Ren4c4b69c2013-10-11 23:58:05 +00001768 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, NDie);
Devang Patel89543712011-08-15 17:24:54 +00001769 }
1770}
1771
Devang Patel3acc70e2011-08-15 22:04:40 +00001772/// constructVariableDIE - Construct a DIE for the given DbgVariable.
David Blaikie319a05f2013-12-02 19:33:10 +00001773DIE *Unit::constructVariableDIE(DbgVariable &DV, bool isScopeAbstract) {
Eric Christopher34a2c872013-11-15 01:43:19 +00001774 StringRef Name = DV.getName();
Devang Patel3acc70e2011-08-15 22:04:40 +00001775
Devang Patel3acc70e2011-08-15 22:04:40 +00001776 // Define variable debug information entry.
Eric Christopher34a2c872013-11-15 01:43:19 +00001777 DIE *VariableDie = new DIE(DV.getTag());
1778 DbgVariable *AbsVar = DV.getAbstractVariable();
Devang Patel3acc70e2011-08-15 22:04:40 +00001779 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
Manman Ren4213c392013-05-29 17:16:59 +00001780 if (AbsDIE)
Manman Ren4c4b69c2013-10-11 23:58:05 +00001781 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin, AbsDIE);
Devang Patel3acc70e2011-08-15 22:04:40 +00001782 else {
David Blaikie715528b2013-08-19 03:34:03 +00001783 if (!Name.empty())
1784 addString(VariableDie, dwarf::DW_AT_name, Name);
Eric Christopher34a2c872013-11-15 01:43:19 +00001785 addSourceLine(VariableDie, DV.getVariable());
1786 addType(VariableDie, DV.getType());
Devang Patel3acc70e2011-08-15 22:04:40 +00001787 }
1788
Eric Christopher34a2c872013-11-15 01:43:19 +00001789 if (DV.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001790 addFlag(VariableDie, dwarf::DW_AT_artificial);
Devang Patel3acc70e2011-08-15 22:04:40 +00001791
1792 if (isScopeAbstract) {
Eric Christopher34a2c872013-11-15 01:43:19 +00001793 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001794 return VariableDie;
1795 }
1796
1797 // Add variable address.
1798
Eric Christopher34a2c872013-11-15 01:43:19 +00001799 unsigned Offset = DV.getDotDebugLocOffset();
Devang Patel3acc70e2011-08-15 22:04:40 +00001800 if (Offset != ~0U) {
Eric Christopher33ff6972013-11-21 23:46:41 +00001801 addSectionLabel(VariableDie, dwarf::DW_AT_location,
1802 Asm->GetTempSymbol("debug_loc", Offset));
Eric Christopher34a2c872013-11-15 01:43:19 +00001803 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001804 return VariableDie;
1805 }
1806
Eric Christophercead0332011-10-03 15:49:20 +00001807 // Check if variable is described by a DBG_VALUE instruction.
Eric Christopher34a2c872013-11-15 01:43:19 +00001808 if (const MachineInstr *DVInsn = DV.getMInsn()) {
David Blaikie0252265b2013-06-16 20:34:15 +00001809 assert(DVInsn->getNumOperands() == 3);
1810 if (DVInsn->getOperand(0).isReg()) {
1811 const MachineOperand RegOp = DVInsn->getOperand(0);
Adrian Prantl19942882013-07-09 21:44:06 +00001812 // If the second operand is an immediate, this is an indirect value.
Adrian Prantl418d1d12013-07-09 20:28:37 +00001813 if (DVInsn->getOperand(1).isImm()) {
Eric Christopherc2697f82013-10-19 01:04:47 +00001814 MachineLocation Location(RegOp.getReg(),
1815 DVInsn->getOperand(1).getImm());
Eric Christopher34a2c872013-11-15 01:43:19 +00001816 addVariableAddress(DV, VariableDie, Location);
David Blaikie0252265b2013-06-16 20:34:15 +00001817 } else if (RegOp.getReg())
Eric Christopher34a2c872013-11-15 01:43:19 +00001818 addVariableAddress(DV, VariableDie, MachineLocation(RegOp.getReg()));
David Blaikie0252265b2013-06-16 20:34:15 +00001819 } else if (DVInsn->getOperand(0).isImm())
Eric Christopher34a2c872013-11-15 01:43:19 +00001820 addConstantValue(VariableDie, DVInsn->getOperand(0), DV.getType());
David Blaikie0252265b2013-06-16 20:34:15 +00001821 else if (DVInsn->getOperand(0).isFPImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001822 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
David Blaikie0252265b2013-06-16 20:34:15 +00001823 else if (DVInsn->getOperand(0).isCImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001824 addConstantValue(VariableDie, DVInsn->getOperand(0).getCImm(),
Eric Christopher34a2c872013-11-15 01:43:19 +00001825 isUnsignedDIType(DD, DV.getType()));
Eric Christopher78fcf4902013-07-03 01:08:30 +00001826
Eric Christopher34a2c872013-11-15 01:43:19 +00001827 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001828 return VariableDie;
1829 } else {
1830 // .. else use frame index.
Eric Christopher34a2c872013-11-15 01:43:19 +00001831 int FI = DV.getFrameIndex();
Devang Patel3acc70e2011-08-15 22:04:40 +00001832 if (FI != ~0) {
1833 unsigned FrameReg = 0;
1834 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopherc2697f82013-10-19 01:04:47 +00001835 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
Devang Patel3acc70e2011-08-15 22:04:40 +00001836 MachineLocation Location(FrameReg, Offset);
Eric Christopher34a2c872013-11-15 01:43:19 +00001837 addVariableAddress(DV, VariableDie, Location);
Devang Patel3acc70e2011-08-15 22:04:40 +00001838 }
1839 }
1840
Eric Christopher34a2c872013-11-15 01:43:19 +00001841 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001842 return VariableDie;
1843}
1844
Manman Ren230ec862013-10-23 23:00:44 +00001845/// constructMemberDIE - Construct member DIE from DIDerivedType.
David Blaikie319a05f2013-12-02 19:33:10 +00001846void Unit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) {
Manman Renb987e512013-10-29 00:53:03 +00001847 DIE *MemberDie = createAndAddDIE(DT.getTag(), Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001848 StringRef Name = DT.getName();
1849 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001850 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001851
Manman Ren93b30902013-10-08 18:42:58 +00001852 addType(MemberDie, resolve(DT.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001853
1854 addSourceLine(MemberDie, DT);
1855
1856 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
David Blaikief2443192013-10-21 17:28:37 +00001857 addUInt(MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel0e821f42011-04-12 23:21:44 +00001858
Eric Christopherc2697f82013-10-19 01:04:47 +00001859 if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) {
Devang Patel0e821f42011-04-12 23:21:44 +00001860
1861 // For C++, virtual base classes are not at fixed offset. Use following
1862 // expression to extract appropriate offset from vtable.
1863 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1864
1865 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
David Blaikief2443192013-10-21 17:28:37 +00001866 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1867 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1868 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1869 addUInt(VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1870 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1871 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1872 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
Devang Patel0e821f42011-04-12 23:21:44 +00001873
David Blaikief2443192013-10-21 17:28:37 +00001874 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
David Blaikie71d34a22013-11-01 00:25:45 +00001875 } else {
1876 uint64_t Size = DT.getSizeInBits();
1877 uint64_t FieldSize = getBaseTypeSize(DD, DT);
1878 uint64_t OffsetInBytes;
1879
1880 if (Size != FieldSize) {
1881 // Handle bitfield.
1882 addUInt(MemberDie, dwarf::DW_AT_byte_size, None,
1883 getBaseTypeSize(DD, DT) >> 3);
1884 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, DT.getSizeInBits());
1885
1886 uint64_t Offset = DT.getOffsetInBits();
1887 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1888 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1889 uint64_t FieldOffset = (HiMark - FieldSize);
1890 Offset -= FieldOffset;
1891
1892 // Maybe we need to work from the other end.
1893 if (Asm->getDataLayout().isLittleEndian())
1894 Offset = FieldSize - (Offset + Size);
1895 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
1896
1897 // Here WD_AT_data_member_location points to the anonymous
1898 // field that includes this bit field.
1899 OffsetInBytes = FieldOffset >> 3;
1900 } else
1901 // This is not a bitfield.
1902 OffsetInBytes = DT.getOffsetInBits() >> 3;
Eric Christopher98b7f172013-11-11 18:52:36 +00001903 addUInt(MemberDie, dwarf::DW_AT_data_member_location, None, OffsetInBytes);
David Blaikie71d34a22013-11-01 00:25:45 +00001904 }
Devang Patel0e821f42011-04-12 23:21:44 +00001905
1906 if (DT.isProtected())
Nick Lewyckycb918492011-12-13 05:09:11 +00001907 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001908 dwarf::DW_ACCESS_protected);
1909 else if (DT.isPrivate())
Nick Lewyckycb918492011-12-13 05:09:11 +00001910 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001911 dwarf::DW_ACCESS_private);
1912 // Otherwise C++ member and base classes are considered public.
Eric Christopher92331fd2012-11-21 00:34:38 +00001913 else
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_public);
1916 if (DT.isVirtual())
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001917 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001918 dwarf::DW_VIRTUALITY_virtual);
Devang Patel514b4002011-04-16 00:11:51 +00001919
1920 // Objective-C properties.
Devang Patel44882172012-02-06 17:49:43 +00001921 if (MDNode *PNode = DT.getObjCProperty())
1922 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
Eric Christopher92331fd2012-11-21 00:34:38 +00001923 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
Devang Patel44882172012-02-06 17:49:43 +00001924 PropertyDie);
1925
David Blaikie37fefc32012-12-13 22:43:07 +00001926 if (DT.isArtificial())
1927 addFlag(MemberDie, dwarf::DW_AT_artificial);
Devang Patel0e821f42011-04-12 23:21:44 +00001928}
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001929
Manman Renc6b63922013-10-14 20:33:57 +00001930/// getOrCreateStaticMemberDIE - Create new DIE for C++ static member.
David Blaikie319a05f2013-12-02 19:33:10 +00001931DIE *Unit::getOrCreateStaticMemberDIE(DIDerivedType DT) {
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001932 if (!DT.Verify())
1933 return NULL;
1934
Manman Renc6b63922013-10-14 20:33:57 +00001935 // Construct the context before querying for the existence of the DIE in case
1936 // such construction creates the DIE.
1937 DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext()));
David Blaikiebd700e42013-11-14 21:24:34 +00001938 assert(dwarf::isType(ContextDIE->getTag()) &&
1939 "Static member should belong to a type.");
Manman Renc6b63922013-10-14 20:33:57 +00001940
1941 DIE *StaticMemberDIE = getDIE(DT);
1942 if (StaticMemberDIE)
1943 return StaticMemberDIE;
1944
Manman Renb987e512013-10-29 00:53:03 +00001945 StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT);
Manman Renc6b63922013-10-14 20:33:57 +00001946
Manman Ren93b30902013-10-08 18:42:58 +00001947 DIType Ty = resolve(DT.getTypeDerivedFrom());
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001948
1949 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1950 addType(StaticMemberDIE, Ty);
1951 addSourceLine(StaticMemberDIE, DT);
1952 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1953 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1954
1955 // FIXME: We could omit private if the parent is a class_type, and
1956 // public if the parent is something else.
1957 if (DT.isProtected())
1958 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1959 dwarf::DW_ACCESS_protected);
1960 else if (DT.isPrivate())
1961 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1962 dwarf::DW_ACCESS_private);
1963 else
1964 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1965 dwarf::DW_ACCESS_public);
1966
1967 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
Manman Renb3388602013-10-05 01:43:03 +00001968 addConstantValue(StaticMemberDIE, CI, isUnsignedDIType(DD, Ty));
David Blaikiea39a76e2013-01-20 01:18:01 +00001969 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
1970 addConstantFPValue(StaticMemberDIE, CFP);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001971
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001972 return StaticMemberDIE;
1973}
David Blaikie6b288cf2013-10-30 20:42:41 +00001974
David Blaikie319a05f2013-12-02 19:33:10 +00001975void Unit::emitHeader(const MCSection *ASection, const MCSymbol *ASectionSym) {
David Blaikie6b288cf2013-10-30 20:42:41 +00001976 Asm->OutStreamer.AddComment("DWARF version number");
1977 Asm->EmitInt16(DD->getDwarfVersion());
1978 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
David Blaikie6896e192013-12-04 23:39:02 +00001979 // We share one abbreviations table across all units so it's always at the
1980 // start of the section. Use a relocatable offset where needed to ensure
1981 // linking doesn't invalidate that offset.
David Blaikie91db9ab2013-12-04 18:12:28 +00001982 Asm->EmitSectionOffset(ASectionSym, ASectionSym);
David Blaikie6b288cf2013-10-30 20:42:41 +00001983 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1984 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
1985}