blob: d2ac2897239c285572de4dded14ca860df60efb5 [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"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Constants.h"
Chandler Carruth12664a02014-03-06 00:22:06 +000021#include "llvm/IR/DIBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/GlobalVariable.h"
24#include "llvm/IR/Instructions.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000025#include "llvm/IR/Mangler.h"
Eric Christopher84588622013-12-28 01:39:17 +000026#include "llvm/MC/MCAsmInfo.h"
Adrian Prantlcbcd5782014-02-11 22:22:15 +000027#include "llvm/MC/MCContext.h"
David Blaikie6b288cf2013-10-30 20:42:41 +000028#include "llvm/MC/MCSection.h"
29#include "llvm/MC/MCStreamer.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000030#include "llvm/Support/CommandLine.h"
Devang Patel0e821f42011-04-12 23:21:44 +000031#include "llvm/Target/TargetFrameLowering.h"
David Blaikief2694972013-06-28 20:05:11 +000032#include "llvm/Target/TargetLoweringObjectFile.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000033#include "llvm/Target/TargetMachine.h"
Devang Patel0e821f42011-04-12 23:21:44 +000034#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel0e821f42011-04-12 23:21:44 +000035
36using namespace llvm;
37
Eric Christopher4287a492013-12-09 23:57:44 +000038static cl::opt<bool>
39GenerateDwarfTypeUnits("generate-type-units", cl::Hidden,
40 cl::desc("Generate DWARF4 type units."),
41 cl::init(false));
David Blaikie409dd9c2013-11-19 23:08:21 +000042
David Blaikie2a80e442013-12-02 22:09:48 +000043/// Unit - Unit constructor.
David Blaikie7480ae62014-01-09 03:03:27 +000044DwarfUnit::DwarfUnit(unsigned UID, DIE *D, DICompileUnit Node, AsmPrinter *A,
45 DwarfDebug *DW, DwarfFile *DWU)
David Blaikief645f962014-01-09 03:23:41 +000046 : UniqueID(UID), CUNode(Node), UnitDie(D), DebugInfoOffset(0), Asm(A),
47 DD(DW), DU(DWU), IndexTyDie(0), Section(0), Skeleton(0) {
David Blaikie319a05f2013-12-02 19:33:10 +000048 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
49}
50
Eric Christopher4287a492013-12-09 23:57:44 +000051DwarfCompileUnit::DwarfCompileUnit(unsigned UID, DIE *D, DICompileUnit Node,
52 AsmPrinter *A, DwarfDebug *DW,
53 DwarfFile *DWU)
David Blaikie7480ae62014-01-09 03:03:27 +000054 : DwarfUnit(UID, D, Node, A, DW, DWU) {
David Blaikie5a152402013-11-15 23:52:02 +000055 insertDIE(Node, D);
Devang Patel0e821f42011-04-12 23:21:44 +000056}
57
David Blaikie15632ae2014-02-12 00:31:30 +000058DwarfTypeUnit::DwarfTypeUnit(unsigned UID, DIE *D, DwarfCompileUnit &CU,
Eric Christopher4287a492013-12-09 23:57:44 +000059 AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU)
Eric Christopher4a741042014-02-16 08:46:55 +000060 : DwarfUnit(UID, D, CU.getCUNode(), A, DW, DWU), CU(CU) {}
David Blaikie409dd9c2013-11-19 23:08:21 +000061
David Blaikie319a05f2013-12-02 19:33:10 +000062/// ~Unit - Destructor for compile unit.
Eric Christophera5a79422013-12-09 23:32:48 +000063DwarfUnit::~DwarfUnit() {
Devang Patel0e821f42011-04-12 23:21:44 +000064 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
65 DIEBlocks[j]->~DIEBlock();
Eric Christopher4a741042014-02-16 08:46:55 +000066 for (unsigned j = 0, M = DIELocs.size(); j < M; ++j)
67 DIELocs[j]->~DIELoc();
Devang Patel0e821f42011-04-12 23:21:44 +000068}
69
70/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
71/// information entry.
Eric Christophera5a79422013-12-09 23:32:48 +000072DIEEntry *DwarfUnit::createDIEEntry(DIE *Entry) {
Devang Patel0e821f42011-04-12 23:21:44 +000073 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
74 return Value;
75}
76
Bill Wendling3495f9b2012-12-06 07:55:19 +000077/// getDefaultLowerBound - Return the default lower bound for an array. If the
Bill Wendling28fe9e72012-12-06 07:38:10 +000078/// DWARF version doesn't handle the language, return -1.
Eric Christophera5a79422013-12-09 23:32:48 +000079int64_t DwarfUnit::getDefaultLowerBound() const {
David Blaikiecb8e4352013-11-15 23:50:53 +000080 switch (getLanguage()) {
Bill Wendling28fe9e72012-12-06 07:38:10 +000081 default:
82 break;
83
84 case dwarf::DW_LANG_C89:
85 case dwarf::DW_LANG_C99:
86 case dwarf::DW_LANG_C:
87 case dwarf::DW_LANG_C_plus_plus:
88 case dwarf::DW_LANG_ObjC:
89 case dwarf::DW_LANG_ObjC_plus_plus:
90 return 0;
91
92 case dwarf::DW_LANG_Fortran77:
93 case dwarf::DW_LANG_Fortran90:
94 case dwarf::DW_LANG_Fortran95:
95 return 1;
96
97 // The languages below have valid values only if the DWARF version >= 4.
98 case dwarf::DW_LANG_Java:
99 case dwarf::DW_LANG_Python:
100 case dwarf::DW_LANG_UPC:
101 case dwarf::DW_LANG_D:
102 if (dwarf::DWARF_VERSION >= 4)
103 return 0;
104 break;
105
106 case dwarf::DW_LANG_Ada83:
107 case dwarf::DW_LANG_Ada95:
108 case dwarf::DW_LANG_Cobol74:
109 case dwarf::DW_LANG_Cobol85:
110 case dwarf::DW_LANG_Modula2:
111 case dwarf::DW_LANG_Pascal83:
112 case dwarf::DW_LANG_PLI:
113 if (dwarf::DWARF_VERSION >= 4)
114 return 1;
115 break;
116 }
117
118 return -1;
119}
120
Manman Ren4dbdc902013-10-31 17:54:35 +0000121/// Check whether the DIE for this MDNode can be shared across CUs.
David Blaikie4201ddf2013-11-15 22:59:36 +0000122static bool isShareableAcrossCUs(DIDescriptor D) {
David Blaikiebcb418e2013-11-20 18:40:16 +0000123 // When the MDNode can be part of the type system, the DIE can be shared
124 // across CUs.
125 // Combining type units and cross-CU DIE sharing is lower value (since
126 // cross-CU DIE sharing is used in LTO and removes type redundancy at that
127 // level already) but may be implementable for some value in projects
128 // building multiple independent libraries with LTO and then linking those
129 // together.
David Blaikie409dd9c2013-11-19 23:08:21 +0000130 return (D.isType() ||
131 (D.isSubprogram() && !DISubprogram(D).isDefinition())) &&
Eric Christopher4287a492013-12-09 23:57:44 +0000132 !GenerateDwarfTypeUnits;
Manman Ren4dbdc902013-10-31 17:54:35 +0000133}
134
135/// getDIE - Returns the debug information entry map slot for the
136/// specified debug variable. We delegate the request to DwarfDebug
137/// when the DIE for this MDNode can be shared across CUs. The mappings
138/// will be kept in DwarfDebug for shareable DIEs.
Eric Christophera5a79422013-12-09 23:32:48 +0000139DIE *DwarfUnit::getDIE(DIDescriptor D) const {
David Blaikie2ad00162013-11-15 23:09:13 +0000140 if (isShareableAcrossCUs(D))
141 return DD->getDIE(D);
142 return MDNodeToDieMap.lookup(D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000143}
144
145/// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
146/// when the DIE for this MDNode can be shared across CUs. The mappings
147/// will be kept in DwarfDebug for shareable DIEs.
Eric Christophera5a79422013-12-09 23:32:48 +0000148void DwarfUnit::insertDIE(DIDescriptor Desc, DIE *D) {
David Blaikie2ad00162013-11-15 23:09:13 +0000149 if (isShareableAcrossCUs(Desc)) {
150 DD->insertDIE(Desc, D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000151 return;
152 }
David Blaikie2ad00162013-11-15 23:09:13 +0000153 MDNodeToDieMap.insert(std::make_pair(Desc, D));
Manman Ren4dbdc902013-10-31 17:54:35 +0000154}
155
Eric Christopherbb69a272012-08-24 01:14:27 +0000156/// addFlag - Add a flag that is true.
Eric Christophera5a79422013-12-09 23:32:48 +0000157void DwarfUnit::addFlag(DIE *Die, dwarf::Attribute Attribute) {
Michael Gottesmanc89466f2013-09-04 04:39:38 +0000158 if (DD->getDwarfVersion() >= 4)
Eric Christopherdccd3282013-10-04 22:40:05 +0000159 Die->addValue(Attribute, dwarf::DW_FORM_flag_present, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000160 else
Eric Christopherdccd3282013-10-04 22:40:05 +0000161 Die->addValue(Attribute, dwarf::DW_FORM_flag, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000162}
163
Devang Patel0e821f42011-04-12 23:21:44 +0000164/// addUInt - Add an unsigned integer attribute data and value.
165///
Eric Christophera5a79422013-12-09 23:32:48 +0000166void DwarfUnit::addUInt(DIE *Die, dwarf::Attribute Attribute,
167 Optional<dwarf::Form> Form, uint64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000168 if (!Form)
169 Form = DIEInteger::BestForm(false, Integer);
170 DIEValue *Value = Integer == 1 ? DIEIntegerOne : new (DIEValueAllocator)
171 DIEInteger(Integer);
David Blaikief2443192013-10-21 17:28:37 +0000172 Die->addValue(Attribute, *Form, Value);
173}
174
Eric Christopher4a741042014-02-16 08:46:55 +0000175void DwarfUnit::addUInt(DIE *Block, dwarf::Form Form, uint64_t Integer) {
David Blaikief2443192013-10-21 17:28:37 +0000176 addUInt(Block, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000177}
178
179/// addSInt - Add an signed integer attribute data and value.
180///
Eric Christophera5a79422013-12-09 23:32:48 +0000181void DwarfUnit::addSInt(DIE *Die, dwarf::Attribute Attribute,
182 Optional<dwarf::Form> Form, int64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000183 if (!Form)
184 Form = DIEInteger::BestForm(true, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000185 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
David Blaikief2443192013-10-21 17:28:37 +0000186 Die->addValue(Attribute, *Form, Value);
187}
188
Eric Christopher4a741042014-02-16 08:46:55 +0000189void DwarfUnit::addSInt(DIELoc *Die, Optional<dwarf::Form> Form,
Eric Christophera5a79422013-12-09 23:32:48 +0000190 int64_t Integer) {
David Blaikief2443192013-10-21 17:28:37 +0000191 addSInt(Die, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000192}
193
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000194/// addString - Add a string attribute data and value. We always emit a
195/// reference to the string pool instead of immediate strings so that DIEs have
Eric Christopherfba22602013-01-07 19:32:45 +0000196/// more predictable sizes. In the case of split dwarf we emit an index
197/// into another table which gets us the static offset into the string
198/// table.
Eric Christophera5a79422013-12-09 23:32:48 +0000199void DwarfUnit::addString(DIE *Die, dwarf::Attribute Attribute,
200 StringRef String) {
Eric Christopher05893f42013-12-30 18:32:31 +0000201
202 if (!DD->useSplitDwarf())
203 return addLocalString(Die, Attribute, String);
204
205 unsigned idx = DU->getStringPoolIndex(String);
206 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
Eric Christopher67646432013-07-26 17:02:41 +0000207 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
Eric Christopher05893f42013-12-30 18:32:31 +0000208 Die->addValue(Attribute, dwarf::DW_FORM_GNU_str_index, Str);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000209}
210
211/// addLocalString - Add a string attribute data and value. This is guaranteed
212/// to be in the local string pool instead of indirected.
Eric Christophera5a79422013-12-09 23:32:48 +0000213void DwarfUnit::addLocalString(DIE *Die, dwarf::Attribute Attribute,
214 StringRef String) {
Eric Christophere698f532012-12-20 21:58:36 +0000215 MCSymbol *Symb = DU->getStringPoolEntry(String);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000216 DIEValue *Value;
Eric Christopher84588622013-12-28 01:39:17 +0000217 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000218 Value = new (DIEValueAllocator) DIELabel(Symb);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000219 else {
Eric Christophere698f532012-12-20 21:58:36 +0000220 MCSymbol *StringPool = DU->getStringPoolSym();
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000221 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000222 }
Eric Christopher05893f42013-12-30 18:32:31 +0000223 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
224 Die->addValue(Attribute, dwarf::DW_FORM_strp, Str);
Devang Patel0e821f42011-04-12 23:21:44 +0000225}
226
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000227/// addExpr - Add a Dwarf expression attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000228///
Eric Christopher4a741042014-02-16 08:46:55 +0000229void DwarfUnit::addExpr(DIELoc *Die, dwarf::Form Form, const MCExpr *Expr) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000230 DIEValue *Value = new (DIEValueAllocator) DIEExpr(Expr);
David Blaikief2443192013-10-21 17:28:37 +0000231 Die->addValue((dwarf::Attribute)0, Form, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000232}
233
Eric Christophera27220f2014-03-05 22:41:20 +0000234/// addLocationList - Add a Dwarf loclistptr attribute data and value.
235///
236void DwarfUnit::addLocationList(DIE *Die, dwarf::Attribute Attribute,
237 unsigned Index) {
238 DIEValue *Value = new (DIEValueAllocator) DIELocList(Index);
239 dwarf::Form Form = DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
240 : dwarf::DW_FORM_data4;
241 Die->addValue(Attribute, Form, Value);
242}
243
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000244/// addLabel - Add a Dwarf label attribute data and value.
245///
Eric Christophera5a79422013-12-09 23:32:48 +0000246void DwarfUnit::addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
247 const MCSymbol *Label) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000248 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
249 Die->addValue(Attribute, Form, Value);
David Blaikief3cd7c52013-06-28 20:05:04 +0000250}
251
Eric Christopher4a741042014-02-16 08:46:55 +0000252void DwarfUnit::addLabel(DIELoc *Die, dwarf::Form Form, const MCSymbol *Label) {
David Blaikief2443192013-10-21 17:28:37 +0000253 addLabel(Die, (dwarf::Attribute)0, Form, Label);
254}
255
Eric Christopher33ff6972013-11-21 23:46:41 +0000256/// addSectionLabel - Add a Dwarf section label attribute data and value.
257///
Eric Christophera5a79422013-12-09 23:32:48 +0000258void DwarfUnit::addSectionLabel(DIE *Die, dwarf::Attribute Attribute,
259 const MCSymbol *Label) {
Eric Christopher33ff6972013-11-21 23:46:41 +0000260 if (DD->getDwarfVersion() >= 4)
261 addLabel(Die, Attribute, dwarf::DW_FORM_sec_offset, Label);
262 else
263 addLabel(Die, Attribute, dwarf::DW_FORM_data4, Label);
264}
265
266/// addSectionOffset - Add an offset into a section attribute data and value.
267///
Eric Christophera5a79422013-12-09 23:32:48 +0000268void DwarfUnit::addSectionOffset(DIE *Die, dwarf::Attribute Attribute,
269 uint64_t Integer) {
Eric Christopher33ff6972013-11-21 23:46:41 +0000270 if (DD->getDwarfVersion() >= 4)
271 addUInt(Die, Attribute, dwarf::DW_FORM_sec_offset, Integer);
272 else
273 addUInt(Die, Attribute, dwarf::DW_FORM_data4, Integer);
274}
275
Eric Christopher962c9082013-01-15 23:56:56 +0000276/// addLabelAddress - Add a dwarf label attribute data and value using
277/// DW_FORM_addr or DW_FORM_GNU_addr_index.
278///
Eric Christopher4287a492013-12-09 23:57:44 +0000279void DwarfCompileUnit::addLabelAddress(DIE *Die, dwarf::Attribute Attribute,
280 MCSymbol *Label) {
Alexey Samsonov4436bf02013-10-03 08:54:43 +0000281 if (Label)
282 DD->addArangeLabel(SymbolCU(this, Label));
Richard Mitton21101b32013-09-19 23:21:01 +0000283
Eric Christophercf48ade2014-01-24 11:52:53 +0000284 if (!DD->useSplitDwarf()) {
285 if (Label) {
286 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
287 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
288 } else {
289 DIEValue *Value = new (DIEValueAllocator) DIEInteger(0);
290 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
291 }
Eric Christopher962c9082013-01-15 23:56:56 +0000292 } else {
Eric Christophercf48ade2014-01-24 11:52:53 +0000293 unsigned idx = DU->getAddrPoolIndex(Label);
294 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
295 Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
Eric Christopher962c9082013-01-15 23:56:56 +0000296 }
297}
298
Eric Christophere9ec2452013-01-18 22:11:33 +0000299/// addOpAddress - Add a dwarf op address data and value using the
300/// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
301///
Eric Christopher4a741042014-02-16 08:46:55 +0000302void DwarfUnit::addOpAddress(DIELoc *Die, const MCSymbol *Sym) {
Eric Christophere9ec2452013-01-18 22:11:33 +0000303 if (!DD->useSplitDwarf()) {
David Blaikief2443192013-10-21 17:28:37 +0000304 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
305 addLabel(Die, dwarf::DW_FORM_udata, Sym);
Eric Christophere9ec2452013-01-18 22:11:33 +0000306 } else {
David Blaikief2443192013-10-21 17:28:37 +0000307 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
308 addUInt(Die, dwarf::DW_FORM_GNU_addr_index, DU->getAddrPoolIndex(Sym));
Eric Christophere9ec2452013-01-18 22:11:33 +0000309 }
310}
311
Eric Christopher33ff6972013-11-21 23:46:41 +0000312/// addSectionDelta - Add a section label delta attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000313///
Eric Christophera5a79422013-12-09 23:32:48 +0000314void DwarfUnit::addSectionDelta(DIE *Die, dwarf::Attribute Attribute,
315 const MCSymbol *Hi, const MCSymbol *Lo) {
Devang Patel0e821f42011-04-12 23:21:44 +0000316 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Eric Christopher33ff6972013-11-21 23:46:41 +0000317 if (DD->getDwarfVersion() >= 4)
318 Die->addValue(Attribute, dwarf::DW_FORM_sec_offset, Value);
319 else
320 Die->addValue(Attribute, dwarf::DW_FORM_data4, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000321}
322
323/// addDIEEntry - Add a DIE attribute data and value.
324///
Eric Christophera5a79422013-12-09 23:32:48 +0000325void DwarfUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +0000326 addDIEEntry(Die, Attribute, createDIEEntry(Entry));
327}
328
David Blaikie47f615e2013-12-17 23:32:35 +0000329void DwarfUnit::addDIETypeSignature(DIE *Die, const DwarfTypeUnit &Type) {
330 Die->addValue(dwarf::DW_AT_signature, dwarf::DW_FORM_ref_sig8,
331 new (DIEValueAllocator) DIETypeSignature(Type));
332}
333
Eric Christophera5a79422013-12-09 23:32:48 +0000334void DwarfUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute,
335 DIEEntry *Entry) {
David Blaikie409dd9c2013-11-19 23:08:21 +0000336 const DIE *DieCU = Die->getUnitOrNull();
337 const DIE *EntryCU = Entry->getEntry()->getUnitOrNull();
Manman Ren4dbdc902013-10-31 17:54:35 +0000338 if (!DieCU)
339 // We assume that Die belongs to this CU, if it is not linked to any CU yet.
David Blaikie2a80e442013-12-02 22:09:48 +0000340 DieCU = getUnitDie();
Manman Ren4dbdc902013-10-31 17:54:35 +0000341 if (!EntryCU)
David Blaikie2a80e442013-12-02 22:09:48 +0000342 EntryCU = getUnitDie();
Manman Ren4dbdc902013-10-31 17:54:35 +0000343 Die->addValue(Attribute, EntryCU == DieCU ? dwarf::DW_FORM_ref4
344 : dwarf::DW_FORM_ref_addr,
345 Entry);
Devang Patel0e821f42011-04-12 23:21:44 +0000346}
347
Manman Renb987e512013-10-29 00:53:03 +0000348/// Create a DIE with the given Tag, add the DIE to its parent, and
349/// call insertDIE if MD is not null.
Eric Christophera5a79422013-12-09 23:32:48 +0000350DIE *DwarfUnit::createAndAddDIE(unsigned Tag, DIE &Parent, DIDescriptor N) {
Manman Renb987e512013-10-29 00:53:03 +0000351 DIE *Die = new DIE(Tag);
352 Parent.addChild(Die);
David Blaikie52c50202013-11-16 00:29:01 +0000353 if (N)
354 insertDIE(N, Die);
Manman Renb987e512013-10-29 00:53:03 +0000355 return Die;
356}
357
Devang Patel0e821f42011-04-12 23:21:44 +0000358/// addBlock - Add block data.
359///
Eric Christopher4a741042014-02-16 08:46:55 +0000360void DwarfUnit::addBlock(DIE *Die, dwarf::Attribute Attribute, DIELoc *Loc) {
Eric Christopher8bdab432014-02-27 18:36:10 +0000361 Loc->ComputeSize(Asm);
Eric Christopher4a741042014-02-16 08:46:55 +0000362 DIELocs.push_back(Loc); // Memoize so we can call the destructor later on.
363 Die->addValue(Attribute, Loc->BestForm(DD->getDwarfVersion()), Loc);
364}
365
Eric Christophera5a79422013-12-09 23:32:48 +0000366void DwarfUnit::addBlock(DIE *Die, dwarf::Attribute Attribute,
367 DIEBlock *Block) {
Eric Christopher8bdab432014-02-27 18:36:10 +0000368 Block->ComputeSize(Asm);
Devang Patel0e821f42011-04-12 23:21:44 +0000369 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
370 Die->addValue(Attribute, Block->BestForm(), Block);
371}
372
373/// addSourceLine - Add location information to specified debug information
374/// entry.
David Blaikie101613e2014-02-12 00:11:25 +0000375void DwarfUnit::addSourceLine(DIE *Die, unsigned Line, StringRef File,
376 StringRef Directory) {
Devang Patel0e821f42011-04-12 23:21:44 +0000377 if (Line == 0)
378 return;
David Blaikie101613e2014-02-12 00:11:25 +0000379
David Blaikie5b858582014-02-12 00:40:47 +0000380 unsigned FileID =
381 DD->getOrCreateSourceID(File, Directory, getCU().getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000382 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000383 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
384 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000385}
386
387/// addSourceLine - Add location information to specified debug information
388/// entry.
David Blaikie101613e2014-02-12 00:11:25 +0000389void DwarfUnit::addSourceLine(DIE *Die, DIVariable V) {
390 assert(V.isVariable());
391
Eric Christopher89a575c2014-02-12 22:38:04 +0000392 addSourceLine(Die, V.getLineNumber(), V.getContext().getFilename(),
393 V.getContext().getDirectory());
David Blaikie101613e2014-02-12 00:11:25 +0000394}
395
396/// addSourceLine - Add location information to specified debug information
397/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000398void DwarfUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
David Blaikie52019302014-02-11 23:57:03 +0000399 assert(G.isGlobalVariable());
Devang Patel0e821f42011-04-12 23:21:44 +0000400
David Blaikie101613e2014-02-12 00:11:25 +0000401 addSourceLine(Die, G.getLineNumber(), G.getFilename(), G.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000402}
403
404/// addSourceLine - Add location information to specified debug information
405/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000406void DwarfUnit::addSourceLine(DIE *Die, DISubprogram SP) {
David Blaikie52019302014-02-11 23:57:03 +0000407 assert(SP.isSubprogram());
Eric Christopher7734ca22012-03-15 23:55:40 +0000408
David Blaikie101613e2014-02-12 00:11:25 +0000409 addSourceLine(Die, SP.getLineNumber(), SP.getFilename(), SP.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000410}
411
412/// addSourceLine - Add location information to specified debug information
413/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000414void DwarfUnit::addSourceLine(DIE *Die, DIType Ty) {
David Blaikie52019302014-02-11 23:57:03 +0000415 assert(Ty.isType());
Devang Patel0e821f42011-04-12 23:21:44 +0000416
David Blaikie101613e2014-02-12 00:11:25 +0000417 addSourceLine(Die, Ty.getLineNumber(), Ty.getFilename(), Ty.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000418}
419
420/// addSourceLine - Add location information to specified debug information
421/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000422void DwarfUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
David Blaikie52019302014-02-11 23:57:03 +0000423 assert(Ty.isObjCProperty());
Eric Christopher70e1bd82012-03-29 08:42:56 +0000424
Eric Christopher70e1bd82012-03-29 08:42:56 +0000425 DIFile File = Ty.getFile();
David Blaikie101613e2014-02-12 00:11:25 +0000426 addSourceLine(Die, Ty.getLineNumber(), File.getFilename(),
427 File.getDirectory());
Eric Christopher70e1bd82012-03-29 08:42:56 +0000428}
429
430/// addSourceLine - Add location information to specified debug information
431/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000432void DwarfUnit::addSourceLine(DIE *Die, DINameSpace NS) {
David Blaikie52019302014-02-11 23:57:03 +0000433 assert(NS.Verify());
Devang Patel0e821f42011-04-12 23:21:44 +0000434
David Blaikie101613e2014-02-12 00:11:25 +0000435 addSourceLine(Die, NS.getLineNumber(), NS.getFilename(), NS.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000436}
437
Eric Christopher92331fd2012-11-21 00:34:38 +0000438/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patel77dc5412011-04-27 22:45:24 +0000439/// DbgVariable based on provided MachineLocation.
Eric Christophera5a79422013-12-09 23:32:48 +0000440void DwarfUnit::addVariableAddress(const DbgVariable &DV, DIE *Die,
441 MachineLocation Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000442 if (DV.variableHasComplexAddress())
Devang Patel0e821f42011-04-12 23:21:44 +0000443 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000444 else if (DV.isBlockByrefVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000445 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
446 else
David Blaikieea2605d2013-06-20 00:25:24 +0000447 addAddress(Die, dwarf::DW_AT_location, Location,
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000448 DV.getVariable().isIndirect());
Devang Patel0e821f42011-04-12 23:21:44 +0000449}
450
Devang Patelba5fbf12011-04-26 19:06:18 +0000451/// addRegisterOp - Add register operand.
Eric Christopher4a741042014-02-16 08:46:55 +0000452void DwarfUnit::addRegisterOp(DIELoc *TheDie, unsigned Reg) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000453 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000454 int DWReg = RI->getDwarfRegNum(Reg, false);
455 bool isSubRegister = DWReg < 0;
456
457 unsigned Idx = 0;
458
459 // Go up the super-register chain until we hit a valid dwarf register number.
460 for (MCSuperRegIterator SR(Reg, RI); SR.isValid() && DWReg < 0; ++SR) {
461 DWReg = RI->getDwarfRegNum(*SR, false);
462 if (DWReg >= 0)
463 Idx = RI->getSubRegIndex(*SR, Reg);
464 }
465
466 if (DWReg < 0) {
Eric Christophera13839f2014-02-26 23:27:16 +0000467 DEBUG(dbgs() << "Invalid Dwarf register number.\n");
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000468 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_nop);
469 return;
470 }
471
472 // Emit register
Devang Patelba5fbf12011-04-26 19:06:18 +0000473 if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000474 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000475 else {
David Blaikief2443192013-10-21 17:28:37 +0000476 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
477 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000478 }
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000479
480 // Emit Mask
481 if (isSubRegister) {
482 unsigned Size = RI->getSubRegIdxSize(Idx);
483 unsigned Offset = RI->getSubRegIdxOffset(Idx);
484 if (Offset > 0) {
485 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bit_piece);
486 addUInt(TheDie, dwarf::DW_FORM_data1, Size);
487 addUInt(TheDie, dwarf::DW_FORM_data1, Offset);
488 } else {
Adrian Prantl7199fd52014-02-12 19:34:44 +0000489 unsigned ByteSize = Size / 8; // Assuming 8 bits per byte.
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000490 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_piece);
Adrian Prantl7199fd52014-02-12 19:34:44 +0000491 addUInt(TheDie, dwarf::DW_FORM_data1, ByteSize);
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000492 }
493 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000494}
495
496/// addRegisterOffset - Add register offset.
Eric Christopher4a741042014-02-16 08:46:55 +0000497void DwarfUnit::addRegisterOffset(DIELoc *TheDie, unsigned Reg,
Eric Christophera5a79422013-12-09 23:32:48 +0000498 int64_t Offset) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000499 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
500 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
501 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
502 if (Reg == TRI->getFrameRegister(*Asm->MF))
503 // If variable offset is based in frame register then use fbreg.
David Blaikief2443192013-10-21 17:28:37 +0000504 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000505 else if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000506 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000507 else {
David Blaikief2443192013-10-21 17:28:37 +0000508 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
509 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000510 }
David Blaikief2443192013-10-21 17:28:37 +0000511 addSInt(TheDie, dwarf::DW_FORM_sdata, Offset);
Devang Patelba5fbf12011-04-26 19:06:18 +0000512}
513
514/// addAddress - Add an address attribute to a die based on the location
515/// provided.
Eric Christophera5a79422013-12-09 23:32:48 +0000516void DwarfUnit::addAddress(DIE *Die, dwarf::Attribute Attribute,
517 const MachineLocation &Location, bool Indirect) {
Eric Christopher4a741042014-02-16 08:46:55 +0000518 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Devang Patelba5fbf12011-04-26 19:06:18 +0000519
David Blaikieea2605d2013-06-20 00:25:24 +0000520 if (Location.isReg() && !Indirect)
Eric Christopher4a741042014-02-16 08:46:55 +0000521 addRegisterOp(Loc, Location.getReg());
David Blaikieea2605d2013-06-20 00:25:24 +0000522 else {
Eric Christopher4a741042014-02-16 08:46:55 +0000523 addRegisterOffset(Loc, Location.getReg(), Location.getOffset());
David Blaikieea2605d2013-06-20 00:25:24 +0000524 if (Indirect && !Location.isReg()) {
Eric Christopher4a741042014-02-16 08:46:55 +0000525 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
David Blaikieea2605d2013-06-20 00:25:24 +0000526 }
527 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000528
529 // Now attach the location information to the DIE.
Eric Christopher4a741042014-02-16 08:46:55 +0000530 addBlock(Die, Attribute, Loc);
Devang Patelba5fbf12011-04-26 19:06:18 +0000531}
532
Devang Patel0e821f42011-04-12 23:21:44 +0000533/// addComplexAddress - Start with the address based on the location provided,
534/// and generate the DWARF information necessary to find the actual variable
Eric Christopher1c70b672013-12-05 00:13:15 +0000535/// given the extra address information encoded in the DbgVariable, starting
536/// from the starting location. Add the DWARF information to the die.
Devang Patel0e821f42011-04-12 23:21:44 +0000537///
Eric Christophera5a79422013-12-09 23:32:48 +0000538void DwarfUnit::addComplexAddress(const DbgVariable &DV, DIE *Die,
539 dwarf::Attribute Attribute,
540 const MachineLocation &Location) {
Eric Christopher4a741042014-02-16 08:46:55 +0000541 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000542 unsigned N = DV.getNumAddrElements();
Devang Patel3e021532011-04-28 02:22:40 +0000543 unsigned i = 0;
544 if (Location.isReg()) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000545 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Devang Patel3e021532011-04-28 02:22:40 +0000546 // If first address element is OpPlus then emit
547 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Eric Christopher4a741042014-02-16 08:46:55 +0000548 addRegisterOffset(Loc, Location.getReg(), DV.getAddrElement(1));
Devang Patel3e021532011-04-28 02:22:40 +0000549 i = 2;
550 } else
Eric Christopher4a741042014-02-16 08:46:55 +0000551 addRegisterOp(Loc, Location.getReg());
Eric Christopherc2697f82013-10-19 01:04:47 +0000552 } else
Eric Christopher4a741042014-02-16 08:46:55 +0000553 addRegisterOffset(Loc, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000554
Eric Christopherc2697f82013-10-19 01:04:47 +0000555 for (; i < N; ++i) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000556 uint64_t Element = DV.getAddrElement(i);
Devang Patel0e821f42011-04-12 23:21:44 +0000557 if (Element == DIBuilder::OpPlus) {
Eric Christopher4a741042014-02-16 08:46:55 +0000558 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
559 addUInt(Loc, dwarf::DW_FORM_udata, DV.getAddrElement(++i));
Devang Patel0e821f42011-04-12 23:21:44 +0000560 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher4d250522012-05-08 18:56:00 +0000561 if (!Location.isReg())
Eric Christopher4a741042014-02-16 08:46:55 +0000562 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Eric Christopherc2697f82013-10-19 01:04:47 +0000563 } else
564 llvm_unreachable("unknown DIBuilder Opcode");
Devang Patel0e821f42011-04-12 23:21:44 +0000565 }
566
567 // Now attach the location information to the DIE.
Eric Christopher4a741042014-02-16 08:46:55 +0000568 addBlock(Die, Attribute, Loc);
Devang Patel0e821f42011-04-12 23:21:44 +0000569}
570
571/* Byref variables, in Blocks, are declared by the programmer as "SomeType
572 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
573 gives the variable VarName either the struct, or a pointer to the struct, as
574 its type. This is necessary for various behind-the-scenes things the
575 compiler needs to do with by-reference variables in Blocks.
576
577 However, as far as the original *programmer* is concerned, the variable
578 should still have type 'SomeType', as originally declared.
579
580 The function getBlockByrefType dives into the __Block_byref_x_VarName
581 struct to find the original type of the variable, which is then assigned to
582 the variable's Debug Information Entry as its real type. So far, so good.
583 However now the debugger will expect the variable VarName to have the type
584 SomeType. So we need the location attribute for the variable to be an
585 expression that explains to the debugger how to navigate through the
586 pointers and struct to find the actual variable of type SomeType.
587
588 The following function does just that. We start by getting
589 the "normal" location for the variable. This will be the location
590 of either the struct __Block_byref_x_VarName or the pointer to the
591 struct __Block_byref_x_VarName.
592
593 The struct will look something like:
594
595 struct __Block_byref_x_VarName {
596 ... <various fields>
597 struct __Block_byref_x_VarName *forwarding;
598 ... <various other fields>
599 SomeType VarName;
600 ... <maybe more fields>
601 };
602
603 If we are given the struct directly (as our starting point) we
604 need to tell the debugger to:
605
606 1). Add the offset of the forwarding field.
607
608 2). Follow that pointer to get the real __Block_byref_x_VarName
609 struct to use (the real one may have been copied onto the heap).
610
611 3). Add the offset for the field VarName, to find the actual variable.
612
613 If we started with a pointer to the struct, then we need to
614 dereference that pointer first, before the other steps.
615 Translating this into DWARF ops, we will need to append the following
616 to the current location description for the variable:
617
618 DW_OP_deref -- optional, if we start with a pointer
619 DW_OP_plus_uconst <forward_fld_offset>
620 DW_OP_deref
621 DW_OP_plus_uconst <varName_fld_offset>
622
623 That is what this function does. */
624
625/// addBlockByrefAddress - Start with the address based on the location
626/// provided, and generate the DWARF information necessary to find the
627/// actual Block variable (navigating the Block struct) based on the
628/// starting location. Add the DWARF information to the die. For
629/// more information, read large comment just above here.
630///
Eric Christophera5a79422013-12-09 23:32:48 +0000631void DwarfUnit::addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
632 dwarf::Attribute Attribute,
633 const MachineLocation &Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000634 DIType Ty = DV.getType();
Devang Patel0e821f42011-04-12 23:21:44 +0000635 DIType TmpTy = Ty;
Eric Christopher31b05762013-08-08 01:41:00 +0000636 uint16_t Tag = Ty.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +0000637 bool isPointer = false;
638
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000639 StringRef varName = DV.getName();
Devang Patel0e821f42011-04-12 23:21:44 +0000640
641 if (Tag == dwarf::DW_TAG_pointer_type) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000642 DIDerivedType DTy(Ty);
Manman Ren93b30902013-10-08 18:42:58 +0000643 TmpTy = resolve(DTy.getTypeDerivedFrom());
Devang Patel0e821f42011-04-12 23:21:44 +0000644 isPointer = true;
645 }
646
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000647 DICompositeType blockStruct(TmpTy);
Devang Patel0e821f42011-04-12 23:21:44 +0000648
649 // Find the __forwarding field and the variable field in the __Block_byref
650 // struct.
651 DIArray Fields = blockStruct.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000652 DIDerivedType varField;
653 DIDerivedType forwardingField;
Devang Patel0e821f42011-04-12 23:21:44 +0000654
655 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000656 DIDerivedType DT(Fields.getElement(i));
Devang Patel0e821f42011-04-12 23:21:44 +0000657 StringRef fieldName = DT.getName();
658 if (fieldName == "__forwarding")
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000659 forwardingField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000660 else if (fieldName == varName)
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000661 varField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000662 }
663
664 // Get the offsets for the forwarding field and the variable field.
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000665 unsigned forwardingFieldOffset = forwardingField.getOffsetInBits() >> 3;
666 unsigned varFieldOffset = varField.getOffsetInBits() >> 2;
Devang Patel0e821f42011-04-12 23:21:44 +0000667
668 // Decode the original location, and use that as the start of the byref
669 // variable's location.
Eric Christopher4a741042014-02-16 08:46:55 +0000670 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Devang Patel0e821f42011-04-12 23:21:44 +0000671
Eric Christopheref9d7102012-07-04 02:02:18 +0000672 if (Location.isReg())
Eric Christopher4a741042014-02-16 08:46:55 +0000673 addRegisterOp(Loc, Location.getReg());
Eric Christopheref9d7102012-07-04 02:02:18 +0000674 else
Eric Christopher4a741042014-02-16 08:46:55 +0000675 addRegisterOffset(Loc, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000676
677 // If we started with a pointer to the __Block_byref... struct, then
678 // the first thing we need to do is dereference the pointer (DW_OP_deref).
679 if (isPointer)
Eric Christopher4a741042014-02-16 08:46:55 +0000680 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000681
682 // Next add the offset for the '__forwarding' field:
683 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
684 // adding the offset if it's 0.
685 if (forwardingFieldOffset > 0) {
Eric Christopher4a741042014-02-16 08:46:55 +0000686 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
687 addUInt(Loc, dwarf::DW_FORM_udata, forwardingFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000688 }
689
690 // Now dereference the __forwarding field to get to the real __Block_byref
691 // struct: DW_OP_deref.
Eric Christopher4a741042014-02-16 08:46:55 +0000692 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000693
694 // Now that we've got the real __Block_byref... struct, add the offset
695 // for the variable's field to get to the location of the actual variable:
696 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
697 if (varFieldOffset > 0) {
Eric Christopher4a741042014-02-16 08:46:55 +0000698 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
699 addUInt(Loc, dwarf::DW_FORM_udata, varFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000700 }
701
702 // Now attach the location information to the DIE.
Eric Christopher4a741042014-02-16 08:46:55 +0000703 addBlock(Die, Attribute, Loc);
Devang Patel0e821f42011-04-12 23:21:44 +0000704}
705
Devang Patelbcd50a12011-07-20 21:57:04 +0000706/// isTypeSigned - Return true if the type is signed.
Manman Renb3388602013-10-05 01:43:03 +0000707static bool isTypeSigned(DwarfDebug *DD, DIType Ty, int *SizeInBits) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000708 if (Ty.isDerivedType())
Manman Renb3388602013-10-05 01:43:03 +0000709 return isTypeSigned(DD, DD->resolve(DIDerivedType(Ty).getTypeDerivedFrom()),
710 SizeInBits);
Devang Patelbcd50a12011-07-20 21:57:04 +0000711 if (Ty.isBasicType())
Eric Christopherc2697f82013-10-19 01:04:47 +0000712 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed ||
713 DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000714 *SizeInBits = Ty.getSizeInBits();
715 return true;
716 }
717 return false;
718}
719
Manman Renb3388602013-10-05 01:43:03 +0000720/// Return true if type encoding is unsigned.
721static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) {
722 DIDerivedType DTy(Ty);
723 if (DTy.isDerivedType())
724 return isUnsignedDIType(DD, DD->resolve(DTy.getTypeDerivedFrom()));
725
726 DIBasicType BTy(Ty);
727 if (BTy.isBasicType()) {
728 unsigned Encoding = BTy.getEncoding();
729 if (Encoding == dwarf::DW_ATE_unsigned ||
730 Encoding == dwarf::DW_ATE_unsigned_char ||
731 Encoding == dwarf::DW_ATE_boolean)
732 return true;
733 }
734 return false;
735}
736
737/// If this type is derived from a base type then return base type size.
Manman Renbda410f2013-10-08 18:46:58 +0000738static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) {
Manman Renb3388602013-10-05 01:43:03 +0000739 unsigned Tag = Ty.getTag();
740
741 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
742 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
743 Tag != dwarf::DW_TAG_restrict_type)
744 return Ty.getSizeInBits();
745
746 DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom());
747
748 // If this type is not derived from any type then take conservative approach.
749 if (!BaseType.isValid())
750 return Ty.getSizeInBits();
751
752 // If this is a derived type, go ahead and get the base type, unless it's a
753 // reference then it's just the size of the field. Pointer types have no need
754 // of this since they're a different type of qualification on the type.
755 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
756 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
757 return Ty.getSizeInBits();
758
759 if (BaseType.isDerivedType())
Manman Renbda410f2013-10-08 18:46:58 +0000760 return getBaseTypeSize(DD, DIDerivedType(BaseType));
Manman Renb3388602013-10-05 01:43:03 +0000761
762 return BaseType.getSizeInBits();
763}
764
Devang Patel0e821f42011-04-12 23:21:44 +0000765/// addConstantValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000766void DwarfUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
767 DIType Ty) {
David Blaikiea1e813d2013-05-10 21:52:07 +0000768 // FIXME: This is a bit conservative/simple - it emits negative values at
769 // their maximum bit width which is a bit unfortunate (& doesn't prefer
770 // udata/sdata over dataN as suggested by the DWARF spec)
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000771 assert(MO.isImm() && "Invalid machine operand!");
Devang Patelbcd50a12011-07-20 21:57:04 +0000772 int SizeInBits = -1;
Manman Renb3388602013-10-05 01:43:03 +0000773 bool SignedConstant = isTypeSigned(DD, Ty, &SizeInBits);
David Blaikief2443192013-10-21 17:28:37 +0000774 dwarf::Form Form;
Devang Patelf1d04702011-05-27 18:15:52 +0000775
Eric Christopher9d1daa82013-08-27 23:49:04 +0000776 // If we're a signed constant definitely use sdata.
777 if (SignedConstant) {
778 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, MO.getImm());
779 return;
780 }
781
782 // Else use data for now unless it's larger than we can deal with.
783 switch (SizeInBits) {
784 case 8:
785 Form = dwarf::DW_FORM_data1;
786 break;
787 case 16:
788 Form = dwarf::DW_FORM_data2;
789 break;
790 case 32:
791 Form = dwarf::DW_FORM_data4;
792 break;
793 case 64:
794 Form = dwarf::DW_FORM_data8;
795 break;
796 default:
797 Form = dwarf::DW_FORM_udata;
798 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
799 return;
800 }
801 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
Devang Patel0e821f42011-04-12 23:21:44 +0000802}
803
804/// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000805void DwarfUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000806 assert(MO.isFPImm() && "Invalid machine operand!");
Devang Patel0e821f42011-04-12 23:21:44 +0000807 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
808 APFloat FPImm = MO.getFPImm()->getValueAPF();
809
810 // Get the raw data form of the floating point.
811 const APInt FltVal = FPImm.bitcastToAPInt();
Eric Christopherc2697f82013-10-19 01:04:47 +0000812 const char *FltPtr = (const char *)FltVal.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000813
814 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000815 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000816 int Incr = (LittleEndian ? 1 : -1);
817 int Start = (LittleEndian ? 0 : NumBytes - 1);
818 int Stop = (LittleEndian ? NumBytes : -1);
819
820 // Output the constant to DWARF one byte at a time.
821 for (; Start != Stop; Start += Incr)
Eric Christopher98b7f172013-11-11 18:52:36 +0000822 addUInt(Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]);
Devang Patel0e821f42011-04-12 23:21:44 +0000823
David Blaikief2443192013-10-21 17:28:37 +0000824 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000825}
826
David Blaikiea39a76e2013-01-20 01:18:01 +0000827/// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000828void DwarfUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000829 // Pass this down to addConstantValue as an unsigned bag of bits.
830 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
David Blaikiea39a76e2013-01-20 01:18:01 +0000831}
832
Devang Patel0e821f42011-04-12 23:21:44 +0000833/// addConstantValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000834void DwarfUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
835 bool Unsigned) {
Eric Christopher78fcf4902013-07-03 01:08:30 +0000836 addConstantValue(Die, CI->getValue(), Unsigned);
David Blaikiea39a76e2013-01-20 01:18:01 +0000837}
838
839// addConstantValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000840void DwarfUnit::addConstantValue(DIE *Die, const APInt &Val, bool Unsigned) {
David Blaikiea39a76e2013-01-20 01:18:01 +0000841 unsigned CIBitWidth = Val.getBitWidth();
Devang Patel8816bbc2011-05-28 00:39:18 +0000842 if (CIBitWidth <= 64) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000843 // If we're a signed constant definitely use sdata.
844 if (!Unsigned) {
845 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
846 Val.getSExtValue());
847 return;
Devang Patel8816bbc2011-05-28 00:39:18 +0000848 }
Eric Christopher9d1daa82013-08-27 23:49:04 +0000849
850 // Else use data for now unless it's larger than we can deal with.
David Blaikief2443192013-10-21 17:28:37 +0000851 dwarf::Form Form;
Eric Christopher9d1daa82013-08-27 23:49:04 +0000852 switch (CIBitWidth) {
853 case 8:
854 Form = dwarf::DW_FORM_data1;
855 break;
856 case 16:
857 Form = dwarf::DW_FORM_data2;
858 break;
859 case 32:
860 Form = dwarf::DW_FORM_data4;
861 break;
862 case 64:
863 Form = dwarf::DW_FORM_data8;
864 break;
865 default:
866 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
867 Val.getZExtValue());
868 return;
869 }
870 addUInt(Die, dwarf::DW_AT_const_value, Form, Val.getZExtValue());
Eric Christopher78fcf4902013-07-03 01:08:30 +0000871 return;
Devang Patel0e821f42011-04-12 23:21:44 +0000872 }
873
874 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
875
876 // Get the raw data form of the large APInt.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000877 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000878
879 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000880 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000881
882 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000883 for (int i = 0; i < NumBytes; i++) {
884 uint8_t c;
885 if (LittleEndian)
886 c = Ptr64[i / 8] >> (8 * (i & 7));
887 else
888 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
David Blaikief2443192013-10-21 17:28:37 +0000889 addUInt(Block, dwarf::DW_FORM_data1, c);
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000890 }
Devang Patel0e821f42011-04-12 23:21:44 +0000891
David Blaikief2443192013-10-21 17:28:37 +0000892 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000893}
894
Eric Christopher25e35092013-04-22 07:47:40 +0000895/// addTemplateParams - Add template parameters into buffer.
Eric Christophera5a79422013-12-09 23:32:48 +0000896void DwarfUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
Devang Patel0e821f42011-04-12 23:21:44 +0000897 // Add template parameters.
898 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
899 DIDescriptor Element = TParams.getElement(i);
900 if (Element.isTemplateTypeParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000901 constructTemplateTypeParameterDIE(Buffer,
902 DITemplateTypeParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000903 else if (Element.isTemplateValueParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000904 constructTemplateValueParameterDIE(Buffer,
905 DITemplateValueParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000906 }
Devang Patel0e821f42011-04-12 23:21:44 +0000907}
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000908
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000909/// getOrCreateContextDIE - Get context owner's DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000910DIE *DwarfUnit::getOrCreateContextDIE(DIScope Context) {
David Blaikiebd700e42013-11-14 21:24:34 +0000911 if (!Context || Context.isFile())
David Blaikie2a80e442013-12-02 22:09:48 +0000912 return getUnitDie();
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000913 if (Context.isType())
914 return getOrCreateTypeDIE(DIType(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000915 if (Context.isNameSpace())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000916 return getOrCreateNameSpace(DINameSpace(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000917 if (Context.isSubprogram())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000918 return getOrCreateSubprogramDIE(DISubprogram(Context));
Adrian Prantl7d828bb2013-11-15 21:05:09 +0000919 return getDIE(Context);
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000920}
921
Eric Christophera5a79422013-12-09 23:32:48 +0000922DIE *DwarfUnit::createTypeDIE(DICompositeType Ty) {
David Blaikie9d861be2013-11-26 00:15:27 +0000923 DIScope Context = resolve(Ty.getContext());
924 DIE *ContextDIE = getOrCreateContextDIE(Context);
David Blaikie409dd9c2013-11-19 23:08:21 +0000925
926 DIE *TyDIE = getDIE(Ty);
927 if (TyDIE)
928 return TyDIE;
929
930 // Create new type.
931 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
932
David Blaikiefbd29eb2013-11-26 00:35:04 +0000933 constructTypeDIE(*TyDIE, Ty);
David Blaikie409dd9c2013-11-19 23:08:21 +0000934
David Blaikie9d861be2013-11-26 00:15:27 +0000935 updateAcceleratorTables(Context, Ty, TyDIE);
David Blaikie409dd9c2013-11-19 23:08:21 +0000936 return TyDIE;
937}
938
Devang Patel0e821f42011-04-12 23:21:44 +0000939/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
940/// given DIType.
Eric Christophera5a79422013-12-09 23:32:48 +0000941DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
David Blaikie32887552013-11-14 22:25:02 +0000942 if (!TyNode)
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000943 return NULL;
Manman Renf4c339e2013-10-29 22:49:29 +0000944
David Blaikie32887552013-11-14 22:25:02 +0000945 DIType Ty(TyNode);
946 assert(Ty.isType());
947
Manman Renf4c339e2013-10-29 22:49:29 +0000948 // Construct the context before querying for the existence of the DIE in case
949 // such construction creates the DIE.
David Blaikie9d861be2013-11-26 00:15:27 +0000950 DIScope Context = resolve(Ty.getContext());
951 DIE *ContextDIE = getOrCreateContextDIE(Context);
Adrian Prantl4583f7d2013-11-15 23:21:39 +0000952 assert(ContextDIE);
Manman Renf4c339e2013-10-29 22:49:29 +0000953
Eric Christophere595bae2013-10-04 17:08:38 +0000954 DIE *TyDIE = getDIE(Ty);
Devang Patel0e821f42011-04-12 23:21:44 +0000955 if (TyDIE)
956 return TyDIE;
957
958 // Create new type.
Manman Renf4c339e2013-10-29 22:49:29 +0000959 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
960
Devang Patel0e821f42011-04-12 23:21:44 +0000961 if (Ty.isBasicType())
962 constructTypeDIE(*TyDIE, DIBasicType(Ty));
David Blaikie8a263cb2013-11-26 00:22:37 +0000963 else if (Ty.isCompositeType()) {
964 DICompositeType CTy(Ty);
David Blaikiecfb21152014-01-03 18:59:42 +0000965 if (GenerateDwarfTypeUnits && !Ty.isForwardDecl())
966 if (MDString *TypeId = CTy.getIdentifier()) {
David Blaikie15632ae2014-02-12 00:31:30 +0000967 DD->addDwarfTypeUnitType(getCU(), TypeId->getString(), TyDIE, CTy);
David Blaikiecfb21152014-01-03 18:59:42 +0000968 // Skip updating the accellerator tables since this is not the full type
969 return TyDIE;
970 }
David Blaikiefbd29eb2013-11-26 00:35:04 +0000971 constructTypeDIE(*TyDIE, CTy);
David Blaikie8a263cb2013-11-26 00:22:37 +0000972 } else {
Devang Patel0e821f42011-04-12 23:21:44 +0000973 assert(Ty.isDerivedType() && "Unknown kind of DIType");
974 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
975 }
David Blaikie2ea848b2013-11-19 22:51:04 +0000976
David Blaikie9d861be2013-11-26 00:15:27 +0000977 updateAcceleratorTables(Context, Ty, TyDIE);
David Blaikie2ea848b2013-11-19 22:51:04 +0000978
979 return TyDIE;
980}
981
Eric Christophera5a79422013-12-09 23:32:48 +0000982void DwarfUnit::updateAcceleratorTables(DIScope Context, DIType Ty,
983 const DIE *TyDIE) {
Eric Christopher21bde872012-01-06 04:35:23 +0000984 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
985 bool IsImplementation = 0;
986 if (Ty.isCompositeType()) {
987 DICompositeType CT(Ty);
Eric Christopher8ea8e4f2012-01-06 23:03:37 +0000988 // A runtime language of 0 actually means C/C++ and that any
989 // non-negative value is some version of Objective-C/C++.
Eric Christopherc2697f82013-10-19 01:04:47 +0000990 IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete();
Eric Christopher21bde872012-01-06 04:35:23 +0000991 }
Eric Christophercf7289f2013-09-05 18:20:16 +0000992 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
Eric Christopher8ea8e4f2012-01-06 23:03:37 +0000993 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
David Blaikie9d861be2013-11-26 00:15:27 +0000994
995 if (!Context || Context.isCompileUnit() || Context.isFile() ||
996 Context.isNameSpace())
997 GlobalTypes[getParentContextString(Context) + Ty.getName().str()] = TyDIE;
Eric Christopher21bde872012-01-06 04:35:23 +0000998 }
Devang Patel0e821f42011-04-12 23:21:44 +0000999}
1000
1001/// addType - Add a new type attribute to the specified entity.
Eric Christophera5a79422013-12-09 23:32:48 +00001002void DwarfUnit::addType(DIE *Entity, DIType Ty, dwarf::Attribute Attribute) {
Eric Christopher0df08e22013-08-08 07:40:37 +00001003 assert(Ty && "Trying to add a type that doesn't exist?");
Devang Patel0e821f42011-04-12 23:21:44 +00001004
1005 // Check for pre-existence.
1006 DIEEntry *Entry = getDIEEntry(Ty);
1007 // If it exists then use the existing value.
1008 if (Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +00001009 addDIEEntry(Entity, Attribute, Entry);
Devang Patel0e821f42011-04-12 23:21:44 +00001010 return;
1011 }
1012
1013 // Construct type.
1014 DIE *Buffer = getOrCreateTypeDIE(Ty);
1015
1016 // Set up proxy.
1017 Entry = createDIEEntry(Buffer);
1018 insertDIEEntry(Ty, Entry);
Manman Ren4dbdc902013-10-31 17:54:35 +00001019 addDIEEntry(Entity, Attribute, Entry);
Devang Patel1cb8ab42011-05-31 23:30:30 +00001020}
1021
Eric Christopher9cd26af2013-09-20 23:22:52 +00001022// Accelerator table mutators - add each name along with its companion
1023// DIE to the proper table while ensuring that the name that we're going
1024// to reference is in the string table. We do this since the names we
1025// add may not only be identical to the names in the DIE.
Eric Christophera5a79422013-12-09 23:32:48 +00001026void DwarfUnit::addAccelName(StringRef Name, const DIE *Die) {
Eric Christopherd0d5bba2014-02-12 22:47:09 +00001027 if (!DD->useDwarfAccelTables())
1028 return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001029 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001030 std::vector<const DIE *> &DIEs = AccelNames[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001031 DIEs.push_back(Die);
1032}
1033
Eric Christophera5a79422013-12-09 23:32:48 +00001034void DwarfUnit::addAccelObjC(StringRef Name, const DIE *Die) {
Eric Christopherd0d5bba2014-02-12 22:47:09 +00001035 if (!DD->useDwarfAccelTables())
1036 return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001037 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001038 std::vector<const DIE *> &DIEs = AccelObjC[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001039 DIEs.push_back(Die);
1040}
1041
Eric Christophera5a79422013-12-09 23:32:48 +00001042void DwarfUnit::addAccelNamespace(StringRef Name, const DIE *Die) {
Eric Christopherd0d5bba2014-02-12 22:47:09 +00001043 if (!DD->useDwarfAccelTables())
1044 return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001045 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001046 std::vector<const DIE *> &DIEs = AccelNamespace[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001047 DIEs.push_back(Die);
1048}
1049
Eric Christophera5a79422013-12-09 23:32:48 +00001050void DwarfUnit::addAccelType(StringRef Name,
1051 std::pair<const DIE *, unsigned> Die) {
Eric Christopherd0d5bba2014-02-12 22:47:09 +00001052 if (!DD->useDwarfAccelTables())
1053 return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001054 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001055 std::vector<std::pair<const DIE *, unsigned> > &DIEs = AccelTypes[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001056 DIEs.push_back(Die);
1057}
1058
Eric Christopher9c58f312013-09-20 22:20:55 +00001059/// addGlobalName - Add a new global name to the compile unit.
Eric Christophera5a79422013-12-09 23:32:48 +00001060void DwarfUnit::addGlobalName(StringRef Name, DIE *Die, DIScope Context) {
Eric Christopher8dba0d52013-10-19 01:04:42 +00001061 std::string FullName = getParentContextString(Context) + Name.str();
Eric Christopher2c8b7902013-10-17 02:06:06 +00001062 GlobalNames[FullName] = Die;
Eric Christopher9c58f312013-09-20 22:20:55 +00001063}
1064
Eric Christopher2c8b7902013-10-17 02:06:06 +00001065/// getParentContextString - Walks the metadata parent chain in a language
1066/// specific manner (using the compile unit language) and returns
1067/// it as a string. This is done at the metadata level because DIEs may
1068/// not currently have been added to the parent context and walking the
1069/// DIEs looking for names is more expensive than walking the metadata.
Eric Christophera5a79422013-12-09 23:32:48 +00001070std::string DwarfUnit::getParentContextString(DIScope Context) const {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001071 if (!Context)
1072 return "";
1073
1074 // FIXME: Decide whether to implement this for non-C++ languages.
1075 if (getLanguage() != dwarf::DW_LANG_C_plus_plus)
1076 return "";
1077
Eric Christopher8dba0d52013-10-19 01:04:42 +00001078 std::string CS;
Eric Christopher2c8b7902013-10-17 02:06:06 +00001079 SmallVector<DIScope, 1> Parents;
1080 while (!Context.isCompileUnit()) {
1081 Parents.push_back(Context);
1082 if (Context.getContext())
1083 Context = resolve(Context.getContext());
1084 else
1085 // Structure, etc types will have a NULL context if they're at the top
1086 // level.
1087 break;
1088 }
1089
1090 // Reverse iterate over our list to go from the outermost construct to the
1091 // innermost.
1092 for (SmallVectorImpl<DIScope>::reverse_iterator I = Parents.rbegin(),
1093 E = Parents.rend();
1094 I != E; ++I) {
1095 DIScope Ctx = *I;
1096 StringRef Name = Ctx.getName();
Eric Christopher8dba0d52013-10-19 01:04:42 +00001097 if (!Name.empty()) {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001098 CS += Name;
1099 CS += "::";
1100 }
1101 }
1102 return CS;
Devang Patel0e821f42011-04-12 23:21:44 +00001103}
1104
1105/// constructTypeDIE - Construct basic type die from DIBasicType.
Eric Christophera5a79422013-12-09 23:32:48 +00001106void DwarfUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Devang Patel0e821f42011-04-12 23:21:44 +00001107 // Get core information.
1108 StringRef Name = BTy.getName();
Devang Patel0e821f42011-04-12 23:21:44 +00001109 // Add name if not anonymous or intermediate type.
1110 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001111 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel04d6d472011-09-14 23:13:28 +00001112
David Blaikiefac56122013-10-04 23:21:16 +00001113 // An unspecified type only has a name attribute.
1114 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type)
Devang Patel04d6d472011-09-14 23:13:28 +00001115 return;
Devang Patel04d6d472011-09-14 23:13:28 +00001116
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001117 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Pateld925d1a2012-02-07 23:33:58 +00001118 BTy.getEncoding());
Devang Patel04d6d472011-09-14 23:13:28 +00001119
Devang Patel0e821f42011-04-12 23:21:44 +00001120 uint64_t Size = BTy.getSizeInBits() >> 3;
David Blaikief2443192013-10-21 17:28:37 +00001121 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001122}
1123
1124/// constructTypeDIE - Construct derived type die from DIDerivedType.
Eric Christophera5a79422013-12-09 23:32:48 +00001125void DwarfUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Devang Patel0e821f42011-04-12 23:21:44 +00001126 // Get core information.
1127 StringRef Name = DTy.getName();
1128 uint64_t Size = DTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001129 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001130
1131 // Map to main type, void will not have a type.
Manman Ren93b30902013-10-08 18:42:58 +00001132 DIType FromTy = resolve(DTy.getTypeDerivedFrom());
Eric Christopher0df08e22013-08-08 07:40:37 +00001133 if (FromTy)
1134 addType(&Buffer, FromTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001135
1136 // Add name if not anonymous or intermediate type.
1137 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001138 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001139
1140 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher85757902012-02-21 22:25:53 +00001141 if (Size && Tag != dwarf::DW_TAG_pointer_type)
David Blaikief2443192013-10-21 17:28:37 +00001142 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001143
David Blaikie5d3249b2013-01-07 05:51:15 +00001144 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
Eric Christopherc2697f82013-10-19 01:04:47 +00001145 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
1146 getOrCreateTypeDIE(resolve(DTy.getClassType())));
Devang Patel0e821f42011-04-12 23:21:44 +00001147 // Add source line info if available and TyDesc is not a forward declaration.
1148 if (!DTy.isForwardDecl())
1149 addSourceLine(&Buffer, DTy);
1150}
1151
Adrian Prantl3f49c892014-02-25 19:57:42 +00001152/// constructSubprogramArguments - Construct function argument DIEs.
1153void DwarfUnit::constructSubprogramArguments(DIE &Buffer, DIArray Args) {
Adrian Prantl69140d22014-02-25 22:27:14 +00001154 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1155 DIDescriptor Ty = Args.getElement(i);
1156 if (Ty.isUnspecifiedParameter()) {
1157 assert(i == N-1 && "Unspecified parameter must be the last argument");
1158 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
1159 } else {
1160 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
1161 addType(Arg, DIType(Ty));
1162 if (DIType(Ty).isArtificial())
1163 addFlag(Arg, dwarf::DW_AT_artificial);
Adrian Prantl3f49c892014-02-25 19:57:42 +00001164 }
Adrian Prantl69140d22014-02-25 22:27:14 +00001165 }
Adrian Prantl3f49c892014-02-25 19:57:42 +00001166}
1167
Devang Patel0e821f42011-04-12 23:21:44 +00001168/// constructTypeDIE - Construct type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001169void DwarfUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
David Blaikie409dd9c2013-11-19 23:08:21 +00001170 // Add name if not anonymous or intermediate type.
Devang Patel0e821f42011-04-12 23:21:44 +00001171 StringRef Name = CTy.getName();
1172
1173 uint64_t Size = CTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001174 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001175
1176 switch (Tag) {
Devang Patel0e821f42011-04-12 23:21:44 +00001177 case dwarf::DW_TAG_array_type:
Eric Christopherdf9955d2013-11-11 18:52:31 +00001178 constructArrayTypeDIE(Buffer, CTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001179 break;
Eric Christopheraeb105f2013-11-11 18:52:39 +00001180 case dwarf::DW_TAG_enumeration_type:
1181 constructEnumTypeDIE(Buffer, CTy);
1182 break;
Devang Patel0e821f42011-04-12 23:21:44 +00001183 case dwarf::DW_TAG_subroutine_type: {
Eric Christopher0df08e22013-08-08 07:40:37 +00001184 // Add return type. A void return won't have a type.
Devang Patel0e821f42011-04-12 23:21:44 +00001185 DIArray Elements = CTy.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001186 DIType RTy(Elements.getElement(0));
Eric Christopher0df08e22013-08-08 07:40:37 +00001187 if (RTy)
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001188 addType(&Buffer, RTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001189
1190 bool isPrototyped = true;
Adrian Prantl3f49c892014-02-25 19:57:42 +00001191 if (Elements.getNumElements() == 2 &&
1192 Elements.getElement(1).isUnspecifiedParameter())
1193 isPrototyped = false;
1194
1195 constructSubprogramArguments(Buffer, Elements);
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);
Adrian Prantl99c7af22013-12-18 21:48:19 +00001204
1205 if (CTy.isLValueReference())
1206 addFlag(&Buffer, dwarf::DW_AT_reference);
1207
1208 if (CTy.isRValueReference())
1209 addFlag(&Buffer, dwarf::DW_AT_rvalue_reference);
Eric Christopherc2697f82013-10-19 01:04:47 +00001210 } break;
Devang Patel0e821f42011-04-12 23:21:44 +00001211 case dwarf::DW_TAG_structure_type:
1212 case dwarf::DW_TAG_union_type:
1213 case dwarf::DW_TAG_class_type: {
Devang Patel0e821f42011-04-12 23:21:44 +00001214 // Add elements to structure type.
David Blaikiea1ae0e62013-08-01 20:30:22 +00001215 DIArray Elements = CTy.getTypeArray();
1216 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel0e821f42011-04-12 23:21:44 +00001217 DIDescriptor Element = Elements.getElement(i);
1218 DIE *ElemDie = NULL;
Adrian Prantlef129fb2014-01-18 02:12:00 +00001219 if (Element.isSubprogram())
1220 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
1221 else if (Element.isDerivedType()) {
Eric Christopherd42b92f2012-05-22 18:45:24 +00001222 DIDerivedType DDTy(Element);
1223 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
Manman Renb987e512013-10-29 00:53:03 +00001224 ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
Manman Ren93b30902013-10-08 18:42:58 +00001225 addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()),
Manman Renb3388602013-10-05 01:43:03 +00001226 dwarf::DW_AT_friend);
Manman Renc6b63922013-10-14 20:33:57 +00001227 } else if (DDTy.isStaticMember()) {
Manman Ren57e6ff72013-10-23 22:57:12 +00001228 getOrCreateStaticMemberDIE(DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001229 } else {
Manman Ren230ec862013-10-23 23:00:44 +00001230 constructMemberDIE(Buffer, DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001231 }
Eric Christopher7285c7d2012-03-28 07:34:31 +00001232 } else if (Element.isObjCProperty()) {
Devang Pateld925d1a2012-02-07 23:33:58 +00001233 DIObjCProperty Property(Element);
Manman Renb987e512013-10-29 00:53:03 +00001234 ElemDie = createAndAddDIE(Property.getTag(), Buffer);
Devang Pateld925d1a2012-02-07 23:33:58 +00001235 StringRef PropertyName = Property.getObjCPropertyName();
1236 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Eric Christopher4c960562014-01-23 19:16:28 +00001237 if (Property.getType())
1238 addType(ElemDie, Property.getType());
Eric Christopherd42b92f2012-05-22 18:45:24 +00001239 addSourceLine(ElemDie, Property);
Devang Pateld925d1a2012-02-07 23:33:58 +00001240 StringRef GetterName = Property.getObjCPropertyGetterName();
1241 if (!GetterName.empty())
1242 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
1243 StringRef SetterName = Property.getObjCPropertySetterName();
1244 if (!SetterName.empty())
1245 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
1246 unsigned PropertyAttributes = 0;
Devang Patel403e8192012-02-04 01:30:32 +00001247 if (Property.isReadOnlyObjCProperty())
1248 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1249 if (Property.isReadWriteObjCProperty())
1250 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1251 if (Property.isAssignObjCProperty())
1252 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1253 if (Property.isRetainObjCProperty())
1254 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1255 if (Property.isCopyObjCProperty())
1256 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1257 if (Property.isNonAtomicObjCProperty())
1258 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1259 if (PropertyAttributes)
David Blaikief2443192013-10-21 17:28:37 +00001260 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None,
Eric Christopherc2697f82013-10-19 01:04:47 +00001261 PropertyAttributes);
Devang Patel44882172012-02-06 17:49:43 +00001262
Devang Pateld925d1a2012-02-07 23:33:58 +00001263 DIEEntry *Entry = getDIEEntry(Element);
1264 if (!Entry) {
1265 Entry = createDIEEntry(ElemDie);
1266 insertDIEEntry(Element, Entry);
1267 }
Devang Patel403e8192012-02-04 01:30:32 +00001268 } else
Devang Patel0e821f42011-04-12 23:21:44 +00001269 continue;
Devang Patel0e821f42011-04-12 23:21:44 +00001270 }
1271
1272 if (CTy.isAppleBlockExtension())
Eric Christopherbb69a272012-08-24 01:14:27 +00001273 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel0e821f42011-04-12 23:21:44 +00001274
Eric Christopher9e429ae2013-10-05 00:27:02 +00001275 DICompositeType ContainingType(resolve(CTy.getContainingType()));
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001276 if (ContainingType)
Manman Ren4c4b69c2013-10-11 23:58:05 +00001277 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001278 getOrCreateTypeDIE(ContainingType));
Devang Patel0e821f42011-04-12 23:21:44 +00001279
Devang Patel12419ae2011-05-12 21:29:42 +00001280 if (CTy.isObjcClassComplete())
Eric Christopherbb69a272012-08-24 01:14:27 +00001281 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patel2409e782011-05-12 19:06:16 +00001282
Eric Christopherda011dd2011-12-16 23:42:42 +00001283 // Add template parameters to a class, structure or union types.
1284 // FIXME: The support isn't in the metadata for this yet.
1285 if (Tag == dwarf::DW_TAG_class_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001286 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
Devang Patel0e821f42011-04-12 23:21:44 +00001287 addTemplateParams(Buffer, CTy.getTemplateParams());
1288
1289 break;
1290 }
1291 default:
1292 break;
1293 }
1294
1295 // Add name if not anonymous or intermediate type.
1296 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001297 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001298
Eric Christopher775cbd22012-05-22 18:45:18 +00001299 if (Tag == dwarf::DW_TAG_enumeration_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001300 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
Eric Christopher775cbd22012-05-22 18:45:18 +00001301 Tag == dwarf::DW_TAG_union_type) {
Devang Patel0e821f42011-04-12 23:21:44 +00001302 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher1cf33382012-06-01 00:22:32 +00001303 // TODO: Do we care about size for enum forward declarations?
Devang Patel0e821f42011-04-12 23:21:44 +00001304 if (Size)
David Blaikief2443192013-10-21 17:28:37 +00001305 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Eric Christopher1cf33382012-06-01 00:22:32 +00001306 else if (!CTy.isForwardDecl())
Devang Patel0e821f42011-04-12 23:21:44 +00001307 // Add zero size if it is not a forward declaration.
David Blaikief2443192013-10-21 17:28:37 +00001308 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, 0);
Eric Christopher1cf33382012-06-01 00:22:32 +00001309
1310 // If we're a forward decl, say so.
1311 if (CTy.isForwardDecl())
Eric Christopherbb69a272012-08-24 01:14:27 +00001312 addFlag(&Buffer, dwarf::DW_AT_declaration);
Devang Patel0e821f42011-04-12 23:21:44 +00001313
1314 // Add source line info if available.
1315 if (!CTy.isForwardDecl())
1316 addSourceLine(&Buffer, CTy);
Eric Christopher54cf8ff2012-03-07 00:15:19 +00001317
1318 // No harm in adding the runtime language to the declaration.
1319 unsigned RLang = CTy.getRunTimeLang();
1320 if (RLang)
Eric Christopherc2697f82013-10-19 01:04:47 +00001321 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
1322 RLang);
Devang Patel0e821f42011-04-12 23:21:44 +00001323 }
1324}
1325
Manman Renffc9a712013-10-23 23:05:28 +00001326/// constructTemplateTypeParameterDIE - Construct new DIE for the given
1327/// DITemplateTypeParameter.
Eric Christophera5a79422013-12-09 23:32:48 +00001328void DwarfUnit::constructTemplateTypeParameterDIE(DIE &Buffer,
1329 DITemplateTypeParameter TP) {
Manman Renb987e512013-10-29 00:53:03 +00001330 DIE *ParamDIE =
1331 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
Eric Christopher056b6472013-08-08 08:09:43 +00001332 // Add the type if it exists, it could be void and therefore no type.
1333 if (TP.getType())
Manman Ren88b0f942013-10-09 19:46:28 +00001334 addType(ParamDIE, resolve(TP.getType()));
David Blaikie2b380232013-06-22 18:59:11 +00001335 if (!TP.getName().empty())
1336 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel0e821f42011-04-12 23:21:44 +00001337}
1338
Manman Renffc9a712013-10-23 23:05:28 +00001339/// constructTemplateValueParameterDIE - Construct new DIE for the given
1340/// DITemplateValueParameter.
Eric Christophera5a79422013-12-09 23:32:48 +00001341void
1342DwarfUnit::constructTemplateValueParameterDIE(DIE &Buffer,
David Blaikie319a05f2013-12-02 19:33:10 +00001343 DITemplateValueParameter VP) {
Manman Renb987e512013-10-29 00:53:03 +00001344 DIE *ParamDIE = createAndAddDIE(VP.getTag(), Buffer);
Eric Christopher0df08e22013-08-08 07:40:37 +00001345
1346 // Add the type if there is one, template template and template parameter
1347 // packs will not have a type.
Eric Christopher691281b2013-10-21 17:48:51 +00001348 if (VP.getTag() == dwarf::DW_TAG_template_value_parameter)
Manman Ren88b0f942013-10-09 19:46:28 +00001349 addType(ParamDIE, resolve(VP.getType()));
Eric Christopherafb2c412013-08-08 07:40:31 +00001350 if (!VP.getName().empty())
1351 addString(ParamDIE, dwarf::DW_AT_name, VP.getName());
1352 if (Value *Val = VP.getValue()) {
David Blaikiea1e813d2013-05-10 21:52:07 +00001353 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val))
Manman Ren88b0f942013-10-09 19:46:28 +00001354 addConstantValue(ParamDIE, CI,
1355 isUnsignedDIType(DD, resolve(VP.getType())));
David Blaikiea1e813d2013-05-10 21:52:07 +00001356 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) {
1357 // For declaration non-type template parameters (such as global values and
1358 // functions)
Eric Christopher4a741042014-02-16 08:46:55 +00001359 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
1360 addOpAddress(Loc, Asm->getSymbol(GV));
David Blaikiea1e813d2013-05-10 21:52:07 +00001361 // Emit DW_OP_stack_value to use the address as the immediate value of the
1362 // parameter, rather than a pointer to it.
Eric Christopher4a741042014-02-16 08:46:55 +00001363 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1364 addBlock(ParamDIE, dwarf::DW_AT_location, Loc);
Eric Christopherafb2c412013-08-08 07:40:31 +00001365 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) {
David Blaikie2b380232013-06-22 18:59:11 +00001366 assert(isa<MDString>(Val));
1367 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1368 cast<MDString>(Val)->getString());
Eric Christopherafb2c412013-08-08 07:40:31 +00001369 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
David Blaikie2b380232013-06-22 18:59:11 +00001370 assert(isa<MDNode>(Val));
1371 DIArray A(cast<MDNode>(Val));
1372 addTemplateParams(*ParamDIE, A);
David Blaikiea1e813d2013-05-10 21:52:07 +00001373 }
1374 }
Devang Patel0e821f42011-04-12 23:21:44 +00001375}
1376
Devang Patel17b53272011-05-06 16:57:54 +00001377/// getOrCreateNameSpace - Create a DIE for DINameSpace.
Eric Christophera5a79422013-12-09 23:32:48 +00001378DIE *DwarfUnit::getOrCreateNameSpace(DINameSpace NS) {
Manman Renf6b936b2013-10-29 05:49:41 +00001379 // Construct the context before querying for the existence of the DIE in case
1380 // such construction creates the DIE.
1381 DIE *ContextDIE = getOrCreateContextDIE(NS.getContext());
Manman Renf6b936b2013-10-29 05:49:41 +00001382
Devang Patel17b53272011-05-06 16:57:54 +00001383 DIE *NDie = getDIE(NS);
1384 if (NDie)
1385 return NDie;
Manman Renf6b936b2013-10-29 05:49:41 +00001386 NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
1387
Eric Christopher4996c702011-11-07 09:24:32 +00001388 if (!NS.getName().empty()) {
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001389 addString(NDie, dwarf::DW_AT_name, NS.getName());
Eric Christopher4996c702011-11-07 09:24:32 +00001390 addAccelNamespace(NS.getName(), NDie);
Eric Christopher2c8b7902013-10-17 02:06:06 +00001391 addGlobalName(NS.getName(), NDie, NS.getContext());
Eric Christopher4996c702011-11-07 09:24:32 +00001392 } else
1393 addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel17b53272011-05-06 16:57:54 +00001394 addSourceLine(NDie, NS);
Devang Patel17b53272011-05-06 16:57:54 +00001395 return NDie;
1396}
1397
Devang Patel89543712011-08-15 17:24:54 +00001398/// getOrCreateSubprogramDIE - Create new DIE using SP.
Eric Christophera5a79422013-12-09 23:32:48 +00001399DIE *DwarfUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
David Blaikie309ffe42013-10-04 01:39:59 +00001400 // Construct the context before querying for the existence of the DIE in case
1401 // such construction creates the DIE (as is the case for member function
1402 // declarations).
Manman Renc50fa112013-10-10 18:40:01 +00001403 DIE *ContextDIE = getOrCreateContextDIE(resolve(SP.getContext()));
David Blaikie309ffe42013-10-04 01:39:59 +00001404
Eric Christophere595bae2013-10-04 17:08:38 +00001405 DIE *SPDie = getDIE(SP);
Devang Patel89543712011-08-15 17:24:54 +00001406 if (SPDie)
1407 return SPDie;
1408
Manman Ren73d697c2013-10-29 00:58:04 +00001409 DISubprogram SPDecl = SP.getFunctionDeclaration();
1410 if (SPDecl.isSubprogram())
1411 // Add subprogram definitions to the CU die directly.
David Blaikie2a80e442013-12-02 22:09:48 +00001412 ContextDIE = UnitDie.get();
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001413
1414 // DW_TAG_inlined_subroutine may refer to this DIE.
Manman Ren73d697c2013-10-29 00:58:04 +00001415 SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001416
Rafael Espindola79278362011-11-10 22:34:29 +00001417 DIE *DeclDie = NULL;
Manman Renb9512a72013-10-23 22:12:26 +00001418 if (SPDecl.isSubprogram())
Rafael Espindola79278362011-11-10 22:34:29 +00001419 DeclDie = getOrCreateSubprogramDIE(SPDecl);
Rafael Espindola79278362011-11-10 22:34:29 +00001420
Devang Patel89543712011-08-15 17:24:54 +00001421 // Add function template parameters.
1422 addTemplateParams(*SPDie, SP.getTemplateParams());
1423
Devang Patel89543712011-08-15 17:24:54 +00001424 // If this DIE is going to refer declaration info using AT_specification
Eric Christopherf20ff972013-05-09 00:42:33 +00001425 // then there is no need to add other attributes.
Rafael Espindola79278362011-11-10 22:34:29 +00001426 if (DeclDie) {
1427 // Refer function declaration directly.
Manman Ren4c4b69c2013-10-11 23:58:05 +00001428 addDIEEntry(SPDie, dwarf::DW_AT_specification, DeclDie);
Rafael Espindola79278362011-11-10 22:34:29 +00001429
Devang Patel89543712011-08-15 17:24:54 +00001430 return SPDie;
Rafael Espindola79278362011-11-10 22:34:29 +00001431 }
Devang Patel89543712011-08-15 17:24:54 +00001432
Eric Christopheracb71152012-08-23 22:52:55 +00001433 // Add the linkage name if we have one.
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001434 StringRef LinkageName = SP.getLinkageName();
1435 if (!LinkageName.empty())
Eric Christopheracb71152012-08-23 22:52:55 +00001436 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001437 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopheracb71152012-08-23 22:52:55 +00001438
Devang Patel89543712011-08-15 17:24:54 +00001439 // Constructors and operators for anonymous aggregates do not have names.
1440 if (!SP.getName().empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001441 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Patel89543712011-08-15 17:24:54 +00001442
1443 addSourceLine(SPDie, SP);
1444
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001445 // Add the prototype if we have a prototype and we have a C like
1446 // language.
David Blaikiecb8e4352013-11-15 23:50:53 +00001447 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001448 if (SP.isPrototyped() &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001449 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001450 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001451 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Patel89543712011-08-15 17:24:54 +00001452
Devang Patel89543712011-08-15 17:24:54 +00001453 DICompositeType SPTy = SP.getType();
David Blaikie5174c842013-05-22 23:22:18 +00001454 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type &&
1455 "the type of a subprogram should be a subroutine");
Devang Patel89543712011-08-15 17:24:54 +00001456
David Blaikie5174c842013-05-22 23:22:18 +00001457 DIArray Args = SPTy.getTypeArray();
Eric Christopher691281b2013-10-21 17:48:51 +00001458 // Add a return type. If this is a type like a C/C++ void type we don't add a
1459 // return type.
Eric Christopher0df08e22013-08-08 07:40:37 +00001460 if (Args.getElement(0))
1461 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel89543712011-08-15 17:24:54 +00001462
1463 unsigned VK = SP.getVirtuality();
1464 if (VK) {
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001465 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Eric Christopher4a741042014-02-16 08:46:55 +00001466 DIELoc *Block = getDIELoc();
David Blaikief2443192013-10-21 17:28:37 +00001467 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1468 addUInt(Block, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1469 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
Eric Christopher98b7f172013-11-11 18:52:36 +00001470 ContainingTypeMap.insert(
1471 std::make_pair(SPDie, resolve(SP.getContainingType())));
Devang Patel89543712011-08-15 17:24:54 +00001472 }
1473
1474 if (!SP.isDefinition()) {
Eric Christopherbb69a272012-08-24 01:14:27 +00001475 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher92331fd2012-11-21 00:34:38 +00001476
Devang Patel89543712011-08-15 17:24:54 +00001477 // Add arguments. Do not add arguments for subprogram definition. They will
1478 // be handled while processing variables.
Adrian Prantl3f49c892014-02-25 19:57:42 +00001479 constructSubprogramArguments(*SPDie, Args);
Devang Patel89543712011-08-15 17:24:54 +00001480 }
1481
1482 if (SP.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001483 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Patel89543712011-08-15 17:24:54 +00001484
1485 if (!SP.isLocalToUnit())
Eric Christopherbb69a272012-08-24 01:14:27 +00001486 addFlag(SPDie, dwarf::DW_AT_external);
Devang Patel89543712011-08-15 17:24:54 +00001487
1488 if (SP.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +00001489 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Patel89543712011-08-15 17:24:54 +00001490
1491 if (unsigned isa = Asm->getISAEncoding()) {
1492 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1493 }
1494
Adrian Prantl99c7af22013-12-18 21:48:19 +00001495 if (SP.isLValueReference())
1496 addFlag(SPDie, dwarf::DW_AT_reference);
1497
1498 if (SP.isRValueReference())
1499 addFlag(SPDie, dwarf::DW_AT_rvalue_reference);
1500
Adrian Prantlef129fb2014-01-18 02:12:00 +00001501 if (SP.isProtected())
1502 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1503 dwarf::DW_ACCESS_protected);
1504 else if (SP.isPrivate())
1505 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1506 dwarf::DW_ACCESS_private);
1507 else
1508 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1509 dwarf::DW_ACCESS_public);
1510
1511 if (SP.isExplicit())
1512 addFlag(SPDie, dwarf::DW_AT_explicit);
1513
Devang Patel89543712011-08-15 17:24:54 +00001514 return SPDie;
1515}
1516
Devang Pateldfd6ec32011-08-15 17:57:41 +00001517// Return const expression if value is a GEP to access merged global
1518// constant. e.g.
1519// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1520static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1521 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1522 if (!CE || CE->getNumOperands() != 3 ||
1523 CE->getOpcode() != Instruction::GetElementPtr)
1524 return NULL;
1525
1526 // First operand points to a global struct.
1527 Value *Ptr = CE->getOperand(0);
1528 if (!isa<GlobalValue>(Ptr) ||
1529 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1530 return NULL;
1531
1532 // Second operand is zero.
1533 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1534 if (!CI || !CI->isZero())
1535 return NULL;
1536
1537 // Third operand is offset.
1538 if (!isa<ConstantInt>(CE->getOperand(2)))
1539 return NULL;
1540
1541 return CE;
1542}
1543
1544/// createGlobalVariableDIE - create global variable DIE.
Eric Christopher4287a492013-12-09 23:57:44 +00001545void DwarfCompileUnit::createGlobalVariableDIE(DIGlobalVariable GV) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001546 // Check for pre-existence.
David Blaikie2ad00162013-11-15 23:09:13 +00001547 if (getDIE(GV))
Devang Pateldfd6ec32011-08-15 17:57:41 +00001548 return;
1549
David Blaikie5e390e42014-02-04 01:23:52 +00001550 assert(GV.isGlobalVariable());
Devang Patel0ecbcbd2011-08-18 23:17:55 +00001551
Eric Christopher08f7c8f2013-10-04 23:49:26 +00001552 DIScope GVContext = GV.getContext();
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001553 DIType GTy = GV.getType();
1554
1555 // If this is a static data member definition, some attributes belong
1556 // to the declaration DIE.
1557 DIE *VariableDIE = NULL;
Manman Rene697d3c2013-02-01 23:54:37 +00001558 bool IsStaticMember = false;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001559 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1560 if (SDMDecl.Verify()) {
1561 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1562 // We need the declaration DIE that is in the static member's class.
Manman Renc6b63922013-10-14 20:33:57 +00001563 VariableDIE = getOrCreateStaticMemberDIE(SDMDecl);
Manman Rene697d3c2013-02-01 23:54:37 +00001564 IsStaticMember = true;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001565 }
1566
1567 // If this is not a static data member definition, create the variable
1568 // DIE and add the initial set of attributes to it.
1569 if (!VariableDIE) {
Manman Renf6b936b2013-10-29 05:49:41 +00001570 // Construct the context before querying for the existence of the DIE in
1571 // case such construction creates the DIE.
1572 DIE *ContextDIE = getOrCreateContextDIE(GVContext);
Manman Renf6b936b2013-10-29 05:49:41 +00001573
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001574 // Add to map.
David Blaikie52c50202013-11-16 00:29:01 +00001575 VariableDIE = createAndAddDIE(GV.getTag(), *ContextDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001576
1577 // Add name and type.
1578 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1579 addType(VariableDIE, GTy);
1580
1581 // Add scoping info.
Eric Christopherd2b497b2013-10-16 01:37:49 +00001582 if (!GV.isLocalToUnit())
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001583 addFlag(VariableDIE, dwarf::DW_AT_external);
1584
1585 // Add line number info.
1586 addSourceLine(VariableDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001587 }
1588
Devang Pateldfd6ec32011-08-15 17:57:41 +00001589 // Add location.
Eric Christopher4996c702011-11-07 09:24:32 +00001590 bool addToAccelTable = false;
Eric Christopher0a917b72011-11-11 03:16:32 +00001591 DIE *VariableSpecDIE = NULL;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001592 bool isGlobalVariable = GV.getGlobal() != NULL;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001593 if (isGlobalVariable) {
Eric Christopher4996c702011-11-07 09:24:32 +00001594 addToAccelTable = true;
Eric Christopher4a741042014-02-16 08:46:55 +00001595 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Rafael Espindola79858aa2013-10-29 17:07:16 +00001596 const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal());
David Blaikief2694972013-06-28 20:05:11 +00001597 if (GV.getGlobal()->isThreadLocal()) {
1598 // FIXME: Make this work with -gsplit-dwarf.
1599 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
1600 assert((PointerSize == 4 || PointerSize == 8) &&
1601 "Add support for other sizes if necessary");
1602 // Based on GCC's support for TLS:
David Blaikie8466ca82013-07-01 23:55:52 +00001603 if (!DD->useSplitDwarf()) {
1604 // 1) Start with a constNu of the appropriate pointer size
Eric Christopher4a741042014-02-16 08:46:55 +00001605 addUInt(Loc, dwarf::DW_FORM_data1,
David Blaikie8466ca82013-07-01 23:55:52 +00001606 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
Richard Mitton0aafb582013-10-07 18:39:18 +00001607 // 2) containing the (relocated) offset of the TLS variable
1608 // within the module's TLS block.
Eric Christopher4a741042014-02-16 08:46:55 +00001609 addExpr(Loc, dwarf::DW_FORM_udata,
David Blaikief1a6dea2014-02-15 19:34:03 +00001610 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym));
David Blaikie8466ca82013-07-01 23:55:52 +00001611 } else {
Eric Christopher4a741042014-02-16 08:46:55 +00001612 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
1613 addUInt(Loc, dwarf::DW_FORM_udata,
David Blaikief1a6dea2014-02-15 19:34:03 +00001614 DU->getAddrPoolIndex(Sym, /* TLS */ true));
David Blaikie8466ca82013-07-01 23:55:52 +00001615 }
Richard Mitton0aafb582013-10-07 18:39:18 +00001616 // 3) followed by a custom OP to make the debugger do a TLS lookup.
Eric Christopher4a741042014-02-16 08:46:55 +00001617 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001618 } else {
1619 DD->addArangeLabel(SymbolCU(this, Sym));
Eric Christopher4a741042014-02-16 08:46:55 +00001620 addOpAddress(Loc, Sym);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001621 }
Devang Pateldfd6ec32011-08-15 17:57:41 +00001622 // Do not create specification DIE if context is either compile unit
1623 // or a subprogram.
Devang Patel5e6b65c2011-09-21 23:41:11 +00001624 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Manman Ren3eb9dff2013-09-09 19:05:21 +00001625 !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001626 // Create specification DIE.
David Blaikie2a80e442013-12-02 22:09:48 +00001627 VariableSpecDIE = createAndAddDIE(dwarf::DW_TAG_variable, *UnitDie);
Manman Ren4c4b69c2013-10-11 23:58:05 +00001628 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification, VariableDIE);
Eric Christopher4a741042014-02-16 08:46:55 +00001629 addBlock(VariableSpecDIE, dwarf::DW_AT_location, Loc);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001630 // A static member's declaration is already flagged as such.
1631 if (!SDMDecl.Verify())
1632 addFlag(VariableDIE, dwarf::DW_AT_declaration);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001633 } else {
Eric Christopher4a741042014-02-16 08:46:55 +00001634 addBlock(VariableDIE, dwarf::DW_AT_location, Loc);
Eric Christopher4996c702011-11-07 09:24:32 +00001635 }
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001636 // Add the linkage name.
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001637 StringRef LinkageName = GV.getLinkageName();
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001638 if (!LinkageName.empty())
Eric Christopher3f79b8c2013-02-27 23:49:47 +00001639 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1640 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1641 // TAG_variable.
Eric Christopherc2697f82013-10-19 01:04:47 +00001642 addString(IsStaticMember && VariableSpecDIE ? VariableSpecDIE
1643 : VariableDIE,
1644 dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001645 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopher92331fd2012-11-21 00:34:38 +00001646 } else if (const ConstantInt *CI =
Eric Christopherc2697f82013-10-19 01:04:47 +00001647 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
Adrian Prantl322f41d2013-04-04 22:56:49 +00001648 // AT_const_value was added when the static member was created. To avoid
Manman Rene697d3c2013-02-01 23:54:37 +00001649 // emitting AT_const_value multiple times, we only add AT_const_value when
1650 // it is not a static member.
1651 if (!IsStaticMember)
Manman Renb3388602013-10-05 01:43:03 +00001652 addConstantValue(VariableDIE, CI, isUnsignedDIType(DD, GTy));
David Blaikiea781b25b2013-11-17 21:55:13 +00001653 } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getOperand(11))) {
Eric Christopher4996c702011-11-07 09:24:32 +00001654 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001655 // GV is a merged global.
Eric Christopher4a741042014-02-16 08:46:55 +00001656 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Devang Pateldfd6ec32011-08-15 17:57:41 +00001657 Value *Ptr = CE->getOperand(0);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001658 MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr));
1659 DD->addArangeLabel(SymbolCU(this, Sym));
Eric Christopher4a741042014-02-16 08:46:55 +00001660 addOpAddress(Loc, Sym);
1661 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
Eric Christopherc2697f82013-10-19 01:04:47 +00001662 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
Eric Christopher4a741042014-02-16 08:46:55 +00001663 addUInt(Loc, dwarf::DW_FORM_udata,
Eric Christopherc2697f82013-10-19 01:04:47 +00001664 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
Eric Christopher4a741042014-02-16 08:46:55 +00001665 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1666 addBlock(VariableDIE, dwarf::DW_AT_location, Loc);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001667 }
1668
Eric Christopherc12c2112011-11-11 01:55:22 +00001669 if (addToAccelTable) {
1670 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1671 addAccelName(GV.getName(), AddrDIE);
Eric Christopher4996c702011-11-07 09:24:32 +00001672
Eric Christopherc12c2112011-11-11 01:55:22 +00001673 // If the linkage name is different than the name, go ahead and output
1674 // that as well into the name table.
1675 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1676 addAccelName(GV.getLinkageName(), AddrDIE);
1677 }
Eric Christopherd2b497b2013-10-16 01:37:49 +00001678
1679 if (!GV.isLocalToUnit())
Eric Christopher2c8b7902013-10-17 02:06:06 +00001680 addGlobalName(GV.getName(), VariableSpecDIE ? VariableSpecDIE : VariableDIE,
1681 GV.getContext());
Devang Pateldfd6ec32011-08-15 17:57:41 +00001682}
1683
Devang Patel0e821f42011-04-12 23:21:44 +00001684/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Eric Christophera5a79422013-12-09 23:32:48 +00001685void DwarfUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Manman Renb987e512013-10-29 00:53:03 +00001686 DIE *DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
Manman Ren4c4b69c2013-10-11 23:58:05 +00001687 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, IndexTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001688
Bill Wendling28fe9e72012-12-06 07:38:10 +00001689 // The LowerBound value defines the lower bounds which is typically zero for
1690 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1691 // Count == -1 then the array is unbounded and we do not emit
1692 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1693 // Count == 0, then the array has zero elements in which case we do not emit
1694 // an upper bound.
1695 int64_t LowerBound = SR.getLo();
Bill Wendling3495f9b2012-12-06 07:55:19 +00001696 int64_t DefaultLowerBound = getDefaultLowerBound();
Bill Wendlingd7767122012-12-04 21:34:03 +00001697 int64_t Count = SR.getCount();
Devang Patel0e821f42011-04-12 23:21:44 +00001698
Bill Wendling3495f9b2012-12-06 07:55:19 +00001699 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
David Blaikief2443192013-10-21 17:28:37 +00001700 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound);
Bill Wendling28fe9e72012-12-06 07:38:10 +00001701
1702 if (Count != -1 && Count != 0)
Bill Wendlingd7767122012-12-04 21:34:03 +00001703 // FIXME: An unbounded array should reference the expression that defines
1704 // the array.
Eric Christopher98b7f172013-11-11 18:52:36 +00001705 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None,
1706 LowerBound + Count - 1);
Devang Patel0e821f42011-04-12 23:21:44 +00001707}
1708
1709/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001710void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopherdf9955d2013-11-11 18:52:31 +00001711 if (CTy.isVector())
Eric Christopherbb69a272012-08-24 01:14:27 +00001712 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel0e821f42011-04-12 23:21:44 +00001713
Eric Christopher0df08e22013-08-08 07:40:37 +00001714 // Emit the element type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001715 addType(&Buffer, resolve(CTy.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001716
1717 // Get an anonymous type for index type.
Eric Christophercad9b532013-01-04 21:51:53 +00001718 // FIXME: This type should be passed down from the front end
1719 // as different languages may have different sizes for indexes.
Devang Patel0e821f42011-04-12 23:21:44 +00001720 DIE *IdxTy = getIndexTyDie();
1721 if (!IdxTy) {
1722 // Construct an anonymous type for index type.
David Blaikie2a80e442013-12-02 22:09:48 +00001723 IdxTy = createAndAddDIE(dwarf::DW_TAG_base_type, *UnitDie);
Eric Christophercad9b532013-01-04 21:51:53 +00001724 addString(IdxTy, dwarf::DW_AT_name, "int");
David Blaikief2443192013-10-21 17:28:37 +00001725 addUInt(IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int32_t));
Devang Patel0e821f42011-04-12 23:21:44 +00001726 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1727 dwarf::DW_ATE_signed);
Devang Patel0e821f42011-04-12 23:21:44 +00001728 setIndexTyDie(IdxTy);
1729 }
1730
1731 // Add subranges to array type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001732 DIArray Elements = CTy.getTypeArray();
Devang Patel0e821f42011-04-12 23:21:44 +00001733 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1734 DIDescriptor Element = Elements.getElement(i);
1735 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1736 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1737 }
1738}
1739
Eric Christopheraeb105f2013-11-11 18:52:39 +00001740/// constructEnumTypeDIE - Construct an enum type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001741void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopheraeb105f2013-11-11 18:52:39 +00001742 DIArray Elements = CTy.getTypeArray();
1743
1744 // Add enumerators to enumeration type.
1745 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001746 DIEnumerator Enum(Elements.getElement(i));
Eric Christopheraeb105f2013-11-11 18:52:39 +00001747 if (Enum.isEnumerator()) {
1748 DIE *Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001749 StringRef Name = Enum.getName();
Eric Christopheraeb105f2013-11-11 18:52:39 +00001750 addString(Enumerator, dwarf::DW_AT_name, Name);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001751 int64_t Value = Enum.getEnumValue();
Eric Christophera07e4f52013-11-19 09:28:34 +00001752 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
1753 Value);
Eric Christopheraeb105f2013-11-11 18:52:39 +00001754 }
1755 }
1756 DIType DTy = resolve(CTy.getTypeDerivedFrom());
1757 if (DTy) {
1758 addType(&Buffer, DTy);
1759 addFlag(&Buffer, dwarf::DW_AT_enum_class);
1760 }
Devang Patel0e821f42011-04-12 23:21:44 +00001761}
1762
Devang Patel89543712011-08-15 17:24:54 +00001763/// constructContainingTypeDIEs - Construct DIEs for types that contain
1764/// vtables.
Eric Christophera5a79422013-12-09 23:32:48 +00001765void DwarfUnit::constructContainingTypeDIEs() {
Devang Patel89543712011-08-15 17:24:54 +00001766 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Eric Christopherc2697f82013-10-19 01:04:47 +00001767 CE = ContainingTypeMap.end();
1768 CI != CE; ++CI) {
Devang Patel89543712011-08-15 17:24:54 +00001769 DIE *SPDie = CI->first;
David Blaikie2ad00162013-11-15 23:09:13 +00001770 DIDescriptor D(CI->second);
1771 if (!D)
Eric Christopherc2697f82013-10-19 01:04:47 +00001772 continue;
David Blaikie2ad00162013-11-15 23:09:13 +00001773 DIE *NDie = getDIE(D);
Eric Christopherc2697f82013-10-19 01:04:47 +00001774 if (!NDie)
1775 continue;
Manman Ren4c4b69c2013-10-11 23:58:05 +00001776 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, NDie);
Devang Patel89543712011-08-15 17:24:54 +00001777 }
1778}
1779
Devang Patel3acc70e2011-08-15 22:04:40 +00001780/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Eric Christophera5a79422013-12-09 23:32:48 +00001781DIE *DwarfUnit::constructVariableDIE(DbgVariable &DV, bool isScopeAbstract) {
Eric Christopher34a2c872013-11-15 01:43:19 +00001782 StringRef Name = DV.getName();
Devang Patel3acc70e2011-08-15 22:04:40 +00001783
Devang Patel3acc70e2011-08-15 22:04:40 +00001784 // Define variable debug information entry.
Eric Christopher34a2c872013-11-15 01:43:19 +00001785 DIE *VariableDie = new DIE(DV.getTag());
1786 DbgVariable *AbsVar = DV.getAbstractVariable();
Devang Patel3acc70e2011-08-15 22:04:40 +00001787 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
Manman Ren4213c392013-05-29 17:16:59 +00001788 if (AbsDIE)
Manman Ren4c4b69c2013-10-11 23:58:05 +00001789 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin, AbsDIE);
Devang Patel3acc70e2011-08-15 22:04:40 +00001790 else {
David Blaikie715528b2013-08-19 03:34:03 +00001791 if (!Name.empty())
1792 addString(VariableDie, dwarf::DW_AT_name, Name);
Eric Christopher34a2c872013-11-15 01:43:19 +00001793 addSourceLine(VariableDie, DV.getVariable());
1794 addType(VariableDie, DV.getType());
Devang Patel3acc70e2011-08-15 22:04:40 +00001795 }
1796
Eric Christopher34a2c872013-11-15 01:43:19 +00001797 if (DV.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001798 addFlag(VariableDie, dwarf::DW_AT_artificial);
Devang Patel3acc70e2011-08-15 22:04:40 +00001799
1800 if (isScopeAbstract) {
Eric Christopher34a2c872013-11-15 01:43:19 +00001801 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001802 return VariableDie;
1803 }
1804
1805 // Add variable address.
1806
Eric Christopher34a2c872013-11-15 01:43:19 +00001807 unsigned Offset = DV.getDotDebugLocOffset();
Devang Patel3acc70e2011-08-15 22:04:40 +00001808 if (Offset != ~0U) {
Eric Christophera27220f2014-03-05 22:41:20 +00001809 addLocationList(VariableDie, dwarf::DW_AT_location, Offset);
Eric Christopher34a2c872013-11-15 01:43:19 +00001810 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001811 return VariableDie;
1812 }
1813
Eric Christophercead0332011-10-03 15:49:20 +00001814 // Check if variable is described by a DBG_VALUE instruction.
Eric Christopher34a2c872013-11-15 01:43:19 +00001815 if (const MachineInstr *DVInsn = DV.getMInsn()) {
David Blaikie0252265b2013-06-16 20:34:15 +00001816 assert(DVInsn->getNumOperands() == 3);
1817 if (DVInsn->getOperand(0).isReg()) {
1818 const MachineOperand RegOp = DVInsn->getOperand(0);
Adrian Prantl19942882013-07-09 21:44:06 +00001819 // If the second operand is an immediate, this is an indirect value.
Adrian Prantl418d1d12013-07-09 20:28:37 +00001820 if (DVInsn->getOperand(1).isImm()) {
Eric Christopherc2697f82013-10-19 01:04:47 +00001821 MachineLocation Location(RegOp.getReg(),
1822 DVInsn->getOperand(1).getImm());
Eric Christopher34a2c872013-11-15 01:43:19 +00001823 addVariableAddress(DV, VariableDie, Location);
David Blaikie0252265b2013-06-16 20:34:15 +00001824 } else if (RegOp.getReg())
Eric Christopher34a2c872013-11-15 01:43:19 +00001825 addVariableAddress(DV, VariableDie, MachineLocation(RegOp.getReg()));
David Blaikie0252265b2013-06-16 20:34:15 +00001826 } else if (DVInsn->getOperand(0).isImm())
Eric Christopher34a2c872013-11-15 01:43:19 +00001827 addConstantValue(VariableDie, DVInsn->getOperand(0), DV.getType());
David Blaikie0252265b2013-06-16 20:34:15 +00001828 else if (DVInsn->getOperand(0).isFPImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001829 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
David Blaikie0252265b2013-06-16 20:34:15 +00001830 else if (DVInsn->getOperand(0).isCImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001831 addConstantValue(VariableDie, DVInsn->getOperand(0).getCImm(),
Eric Christopher34a2c872013-11-15 01:43:19 +00001832 isUnsignedDIType(DD, DV.getType()));
Eric Christopher78fcf4902013-07-03 01:08:30 +00001833
Eric Christopher34a2c872013-11-15 01:43:19 +00001834 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001835 return VariableDie;
1836 } else {
1837 // .. else use frame index.
Eric Christopher34a2c872013-11-15 01:43:19 +00001838 int FI = DV.getFrameIndex();
Devang Patel3acc70e2011-08-15 22:04:40 +00001839 if (FI != ~0) {
1840 unsigned FrameReg = 0;
1841 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopherc2697f82013-10-19 01:04:47 +00001842 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
Devang Patel3acc70e2011-08-15 22:04:40 +00001843 MachineLocation Location(FrameReg, Offset);
Eric Christopher34a2c872013-11-15 01:43:19 +00001844 addVariableAddress(DV, VariableDie, Location);
Devang Patel3acc70e2011-08-15 22:04:40 +00001845 }
1846 }
1847
Eric Christopher34a2c872013-11-15 01:43:19 +00001848 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001849 return VariableDie;
1850}
1851
Manman Ren230ec862013-10-23 23:00:44 +00001852/// constructMemberDIE - Construct member DIE from DIDerivedType.
Eric Christophera5a79422013-12-09 23:32:48 +00001853void DwarfUnit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) {
Manman Renb987e512013-10-29 00:53:03 +00001854 DIE *MemberDie = createAndAddDIE(DT.getTag(), Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001855 StringRef Name = DT.getName();
1856 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001857 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001858
Manman Ren93b30902013-10-08 18:42:58 +00001859 addType(MemberDie, resolve(DT.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001860
1861 addSourceLine(MemberDie, DT);
1862
Eric Christopherc2697f82013-10-19 01:04:47 +00001863 if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) {
Devang Patel0e821f42011-04-12 23:21:44 +00001864
1865 // For C++, virtual base classes are not at fixed offset. Use following
1866 // expression to extract appropriate offset from vtable.
1867 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1868
Eric Christopher4a741042014-02-16 08:46:55 +00001869 DIELoc *VBaseLocationDie = new (DIEValueAllocator) DIELoc();
David Blaikief2443192013-10-21 17:28:37 +00001870 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1871 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1872 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1873 addUInt(VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1874 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1875 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1876 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
Devang Patel0e821f42011-04-12 23:21:44 +00001877
David Blaikief2443192013-10-21 17:28:37 +00001878 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
David Blaikie71d34a22013-11-01 00:25:45 +00001879 } else {
1880 uint64_t Size = DT.getSizeInBits();
1881 uint64_t FieldSize = getBaseTypeSize(DD, DT);
1882 uint64_t OffsetInBytes;
1883
1884 if (Size != FieldSize) {
1885 // Handle bitfield.
1886 addUInt(MemberDie, dwarf::DW_AT_byte_size, None,
1887 getBaseTypeSize(DD, DT) >> 3);
1888 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, DT.getSizeInBits());
1889
1890 uint64_t Offset = DT.getOffsetInBits();
1891 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1892 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1893 uint64_t FieldOffset = (HiMark - FieldSize);
1894 Offset -= FieldOffset;
1895
1896 // Maybe we need to work from the other end.
1897 if (Asm->getDataLayout().isLittleEndian())
1898 Offset = FieldSize - (Offset + Size);
1899 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
1900
Adrian Prantlc67655a2014-01-28 18:13:47 +00001901 // Here DW_AT_data_member_location points to the anonymous
David Blaikie71d34a22013-11-01 00:25:45 +00001902 // field that includes this bit field.
1903 OffsetInBytes = FieldOffset >> 3;
1904 } else
1905 // This is not a bitfield.
1906 OffsetInBytes = DT.getOffsetInBits() >> 3;
David Blaikie2ada1162014-01-03 00:48:38 +00001907
David Blaikie22b29a52014-01-03 01:30:05 +00001908 if (DD->getDwarfVersion() <= 2) {
Eric Christopher4a741042014-02-16 08:46:55 +00001909 DIELoc *MemLocationDie = new (DIEValueAllocator) DIELoc();
David Blaikie22b29a52014-01-03 01:30:05 +00001910 addUInt(MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1911 addUInt(MemLocationDie, dwarf::DW_FORM_udata, OffsetInBytes);
1912 addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie);
1913 } else
1914 addUInt(MemberDie, dwarf::DW_AT_data_member_location, None,
1915 OffsetInBytes);
David Blaikie71d34a22013-11-01 00:25:45 +00001916 }
Devang Patel0e821f42011-04-12 23:21:44 +00001917
1918 if (DT.isProtected())
Nick Lewyckycb918492011-12-13 05:09:11 +00001919 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001920 dwarf::DW_ACCESS_protected);
1921 else if (DT.isPrivate())
Nick Lewyckycb918492011-12-13 05:09:11 +00001922 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001923 dwarf::DW_ACCESS_private);
1924 // Otherwise C++ member and base classes are considered public.
Eric Christopher92331fd2012-11-21 00:34:38 +00001925 else
Nick Lewyckycb918492011-12-13 05:09:11 +00001926 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001927 dwarf::DW_ACCESS_public);
1928 if (DT.isVirtual())
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001929 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001930 dwarf::DW_VIRTUALITY_virtual);
Devang Patel514b4002011-04-16 00:11:51 +00001931
1932 // Objective-C properties.
Devang Patel44882172012-02-06 17:49:43 +00001933 if (MDNode *PNode = DT.getObjCProperty())
1934 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
Eric Christopher92331fd2012-11-21 00:34:38 +00001935 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
Devang Patel44882172012-02-06 17:49:43 +00001936 PropertyDie);
1937
David Blaikie37fefc32012-12-13 22:43:07 +00001938 if (DT.isArtificial())
1939 addFlag(MemberDie, dwarf::DW_AT_artificial);
Devang Patel0e821f42011-04-12 23:21:44 +00001940}
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001941
Manman Renc6b63922013-10-14 20:33:57 +00001942/// getOrCreateStaticMemberDIE - Create new DIE for C++ static member.
Eric Christophera5a79422013-12-09 23:32:48 +00001943DIE *DwarfUnit::getOrCreateStaticMemberDIE(DIDerivedType DT) {
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001944 if (!DT.Verify())
1945 return NULL;
1946
Manman Renc6b63922013-10-14 20:33:57 +00001947 // Construct the context before querying for the existence of the DIE in case
1948 // such construction creates the DIE.
1949 DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext()));
David Blaikiebd700e42013-11-14 21:24:34 +00001950 assert(dwarf::isType(ContextDIE->getTag()) &&
1951 "Static member should belong to a type.");
Manman Renc6b63922013-10-14 20:33:57 +00001952
1953 DIE *StaticMemberDIE = getDIE(DT);
1954 if (StaticMemberDIE)
1955 return StaticMemberDIE;
1956
Manman Renb987e512013-10-29 00:53:03 +00001957 StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT);
Manman Renc6b63922013-10-14 20:33:57 +00001958
Manman Ren93b30902013-10-08 18:42:58 +00001959 DIType Ty = resolve(DT.getTypeDerivedFrom());
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001960
1961 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1962 addType(StaticMemberDIE, Ty);
1963 addSourceLine(StaticMemberDIE, DT);
1964 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1965 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1966
1967 // FIXME: We could omit private if the parent is a class_type, and
1968 // public if the parent is something else.
1969 if (DT.isProtected())
1970 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1971 dwarf::DW_ACCESS_protected);
1972 else if (DT.isPrivate())
1973 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1974 dwarf::DW_ACCESS_private);
1975 else
1976 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1977 dwarf::DW_ACCESS_public);
1978
1979 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
Manman Renb3388602013-10-05 01:43:03 +00001980 addConstantValue(StaticMemberDIE, CI, isUnsignedDIType(DD, Ty));
David Blaikiea39a76e2013-01-20 01:18:01 +00001981 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
1982 addConstantFPValue(StaticMemberDIE, CFP);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001983
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001984 return StaticMemberDIE;
1985}
David Blaikie6b288cf2013-10-30 20:42:41 +00001986
Eric Christophera5a79422013-12-09 23:32:48 +00001987void DwarfUnit::emitHeader(const MCSection *ASection,
David Blaikie3332d4c2013-12-11 21:14:02 +00001988 const MCSymbol *ASectionSym) const {
David Blaikie6b288cf2013-10-30 20:42:41 +00001989 Asm->OutStreamer.AddComment("DWARF version number");
1990 Asm->EmitInt16(DD->getDwarfVersion());
1991 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
David Blaikie6896e192013-12-04 23:39:02 +00001992 // We share one abbreviations table across all units so it's always at the
1993 // start of the section. Use a relocatable offset where needed to ensure
1994 // linking doesn't invalidate that offset.
David Blaikie91db9ab2013-12-04 18:12:28 +00001995 Asm->EmitSectionOffset(ASectionSym, ASectionSym);
David Blaikie6b288cf2013-10-30 20:42:41 +00001996 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1997 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
1998}
David Blaikiebc563272013-12-13 21:33:40 +00001999
David Blaikie2494fdb2014-02-14 22:41:51 +00002000void DwarfCompileUnit::initStmtList(MCSymbol *DwarfLineSectionSym) {
2001 // Define start line table label for each Compile Unit.
2002 MCSymbol *LineTableStartSym =
2003 Asm->GetTempSymbol("line_table_start", getUniqueID());
2004 Asm->OutStreamer.getContext().setMCLineTableSymbol(LineTableStartSym,
2005 getUniqueID());
2006
2007 // Use a single line table if we are generating assembly.
2008 bool UseTheFirstCU =
2009 Asm->OutStreamer.hasRawTextSupport() || (getUniqueID() == 0);
2010
David Blaikie60e63862014-02-14 23:58:13 +00002011 stmtListIndex = UnitDie->getValues().size();
2012
David Blaikie2494fdb2014-02-14 22:41:51 +00002013 // DW_AT_stmt_list is a offset of line number information for this
2014 // compile unit in debug_line section. For split dwarf this is
2015 // left in the skeleton CU and so not included.
2016 // The line table entries are not always emitted in assembly, so it
2017 // is not okay to use line_table_start here.
2018 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
2019 addSectionLabel(UnitDie.get(), dwarf::DW_AT_stmt_list,
2020 UseTheFirstCU ? DwarfLineSectionSym : LineTableStartSym);
2021 else if (UseTheFirstCU)
2022 addSectionOffset(UnitDie.get(), dwarf::DW_AT_stmt_list, 0);
2023 else
2024 addSectionDelta(UnitDie.get(), dwarf::DW_AT_stmt_list, LineTableStartSym,
2025 DwarfLineSectionSym);
2026}
2027
David Blaikie60e63862014-02-14 23:58:13 +00002028void DwarfCompileUnit::applyStmtList(DIE &D) {
2029 D.addValue(dwarf::DW_AT_stmt_list,
2030 UnitDie->getAbbrev().getData()[stmtListIndex].getForm(),
2031 UnitDie->getValues()[stmtListIndex]);
2032}
2033
David Blaikiebc563272013-12-13 21:33:40 +00002034void DwarfTypeUnit::emitHeader(const MCSection *ASection,
2035 const MCSymbol *ASectionSym) const {
2036 DwarfUnit::emitHeader(ASection, ASectionSym);
2037 Asm->OutStreamer.AddComment("Type Signature");
2038 Asm->OutStreamer.EmitIntValue(TypeSignature, sizeof(TypeSignature));
2039 Asm->OutStreamer.AddComment("Type DIE Offset");
David Blaikie15ed5eb2014-01-10 01:38:41 +00002040 // In a skeleton type unit there is no type DIE so emit a zero offset.
2041 Asm->OutStreamer.EmitIntValue(Ty ? Ty->getOffset() : 0,
2042 sizeof(Ty->getOffset()));
David Blaikiebc563272013-12-13 21:33:40 +00002043}
2044
2045void DwarfTypeUnit::initSection(const MCSection *Section) {
2046 assert(!this->Section);
2047 this->Section = Section;
2048 // Since each type unit is contained in its own COMDAT section, the begin
2049 // label and the section label are the same. Using the begin label emission in
2050 // DwarfDebug to emit the section label as well is slightly subtle/sneaky, but
2051 // the only other alternative of lazily constructing start-of-section labels
2052 // and storing a mapping in DwarfDebug (or AsmPrinter).
2053 this->SectionSym = this->LabelBegin =
2054 Asm->GetTempSymbol(Section->getLabelBeginName(), getUniqueID());
2055 this->LabelEnd =
2056 Asm->GetTempSymbol(Section->getLabelEndName(), getUniqueID());
2057 this->LabelRange = Asm->GetTempSymbol("gnu_ranges", getUniqueID());
2058}