blob: bf377f52d41f1e5cfd8017a0d6beec0f452a199c [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,
David Blaikie4a2f95f2014-03-18 01:17:26 +000059 AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU,
David Blaikie8287aff2014-03-18 02:13:23 +000060 MCDwarfDwoLineTable *SplitLineTable)
David Blaikie4a2f95f2014-03-18 01:17:26 +000061 : DwarfUnit(UID, D, CU.getCUNode(), A, DW, DWU), CU(CU),
62 SplitLineTable(SplitLineTable) {
63 if (SplitLineTable)
64 addSectionOffset(UnitDie.get(), dwarf::DW_AT_stmt_list, 0);
65}
David Blaikie409dd9c2013-11-19 23:08:21 +000066
David Blaikie319a05f2013-12-02 19:33:10 +000067/// ~Unit - Destructor for compile unit.
Eric Christophera5a79422013-12-09 23:32:48 +000068DwarfUnit::~DwarfUnit() {
Devang Patel0e821f42011-04-12 23:21:44 +000069 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
70 DIEBlocks[j]->~DIEBlock();
Eric Christopher4a741042014-02-16 08:46:55 +000071 for (unsigned j = 0, M = DIELocs.size(); j < M; ++j)
72 DIELocs[j]->~DIELoc();
Devang Patel0e821f42011-04-12 23:21:44 +000073}
74
75/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
76/// information entry.
Eric Christophera5a79422013-12-09 23:32:48 +000077DIEEntry *DwarfUnit::createDIEEntry(DIE *Entry) {
Devang Patel0e821f42011-04-12 23:21:44 +000078 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
79 return Value;
80}
81
Bill Wendling3495f9b2012-12-06 07:55:19 +000082/// getDefaultLowerBound - Return the default lower bound for an array. If the
Bill Wendling28fe9e72012-12-06 07:38:10 +000083/// DWARF version doesn't handle the language, return -1.
Eric Christophera5a79422013-12-09 23:32:48 +000084int64_t DwarfUnit::getDefaultLowerBound() const {
David Blaikiecb8e4352013-11-15 23:50:53 +000085 switch (getLanguage()) {
Bill Wendling28fe9e72012-12-06 07:38:10 +000086 default:
87 break;
88
89 case dwarf::DW_LANG_C89:
90 case dwarf::DW_LANG_C99:
91 case dwarf::DW_LANG_C:
92 case dwarf::DW_LANG_C_plus_plus:
93 case dwarf::DW_LANG_ObjC:
94 case dwarf::DW_LANG_ObjC_plus_plus:
95 return 0;
96
97 case dwarf::DW_LANG_Fortran77:
98 case dwarf::DW_LANG_Fortran90:
99 case dwarf::DW_LANG_Fortran95:
100 return 1;
101
102 // The languages below have valid values only if the DWARF version >= 4.
103 case dwarf::DW_LANG_Java:
104 case dwarf::DW_LANG_Python:
105 case dwarf::DW_LANG_UPC:
106 case dwarf::DW_LANG_D:
107 if (dwarf::DWARF_VERSION >= 4)
108 return 0;
109 break;
110
111 case dwarf::DW_LANG_Ada83:
112 case dwarf::DW_LANG_Ada95:
113 case dwarf::DW_LANG_Cobol74:
114 case dwarf::DW_LANG_Cobol85:
115 case dwarf::DW_LANG_Modula2:
116 case dwarf::DW_LANG_Pascal83:
117 case dwarf::DW_LANG_PLI:
118 if (dwarf::DWARF_VERSION >= 4)
119 return 1;
120 break;
121 }
122
123 return -1;
124}
125
Manman Ren4dbdc902013-10-31 17:54:35 +0000126/// Check whether the DIE for this MDNode can be shared across CUs.
David Blaikie4201ddf2013-11-15 22:59:36 +0000127static bool isShareableAcrossCUs(DIDescriptor D) {
David Blaikiebcb418e2013-11-20 18:40:16 +0000128 // When the MDNode can be part of the type system, the DIE can be shared
129 // across CUs.
130 // Combining type units and cross-CU DIE sharing is lower value (since
131 // cross-CU DIE sharing is used in LTO and removes type redundancy at that
132 // level already) but may be implementable for some value in projects
133 // building multiple independent libraries with LTO and then linking those
134 // together.
David Blaikie409dd9c2013-11-19 23:08:21 +0000135 return (D.isType() ||
136 (D.isSubprogram() && !DISubprogram(D).isDefinition())) &&
Eric Christopher4287a492013-12-09 23:57:44 +0000137 !GenerateDwarfTypeUnits;
Manman Ren4dbdc902013-10-31 17:54:35 +0000138}
139
140/// getDIE - Returns the debug information entry map slot for the
141/// specified debug variable. We delegate the request to DwarfDebug
142/// when the DIE for this MDNode can be shared across CUs. The mappings
143/// will be kept in DwarfDebug for shareable DIEs.
Eric Christophera5a79422013-12-09 23:32:48 +0000144DIE *DwarfUnit::getDIE(DIDescriptor D) const {
David Blaikie2ad00162013-11-15 23:09:13 +0000145 if (isShareableAcrossCUs(D))
146 return DD->getDIE(D);
147 return MDNodeToDieMap.lookup(D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000148}
149
150/// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
151/// when the DIE for this MDNode can be shared across CUs. The mappings
152/// will be kept in DwarfDebug for shareable DIEs.
Eric Christophera5a79422013-12-09 23:32:48 +0000153void DwarfUnit::insertDIE(DIDescriptor Desc, DIE *D) {
David Blaikie2ad00162013-11-15 23:09:13 +0000154 if (isShareableAcrossCUs(Desc)) {
155 DD->insertDIE(Desc, D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000156 return;
157 }
David Blaikie2ad00162013-11-15 23:09:13 +0000158 MDNodeToDieMap.insert(std::make_pair(Desc, D));
Manman Ren4dbdc902013-10-31 17:54:35 +0000159}
160
Eric Christopherbb69a272012-08-24 01:14:27 +0000161/// addFlag - Add a flag that is true.
Eric Christophera5a79422013-12-09 23:32:48 +0000162void DwarfUnit::addFlag(DIE *Die, dwarf::Attribute Attribute) {
Michael Gottesmanc89466f2013-09-04 04:39:38 +0000163 if (DD->getDwarfVersion() >= 4)
Eric Christopherdccd3282013-10-04 22:40:05 +0000164 Die->addValue(Attribute, dwarf::DW_FORM_flag_present, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000165 else
Eric Christopherdccd3282013-10-04 22:40:05 +0000166 Die->addValue(Attribute, dwarf::DW_FORM_flag, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000167}
168
Devang Patel0e821f42011-04-12 23:21:44 +0000169/// addUInt - Add an unsigned integer attribute data and value.
170///
Eric Christophera5a79422013-12-09 23:32:48 +0000171void DwarfUnit::addUInt(DIE *Die, dwarf::Attribute Attribute,
172 Optional<dwarf::Form> Form, uint64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000173 if (!Form)
174 Form = DIEInteger::BestForm(false, Integer);
175 DIEValue *Value = Integer == 1 ? DIEIntegerOne : new (DIEValueAllocator)
176 DIEInteger(Integer);
David Blaikief2443192013-10-21 17:28:37 +0000177 Die->addValue(Attribute, *Form, Value);
178}
179
Eric Christopher4a741042014-02-16 08:46:55 +0000180void DwarfUnit::addUInt(DIE *Block, dwarf::Form Form, uint64_t Integer) {
David Blaikief2443192013-10-21 17:28:37 +0000181 addUInt(Block, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000182}
183
184/// addSInt - Add an signed integer attribute data and value.
185///
Eric Christophera5a79422013-12-09 23:32:48 +0000186void DwarfUnit::addSInt(DIE *Die, dwarf::Attribute Attribute,
187 Optional<dwarf::Form> Form, int64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000188 if (!Form)
189 Form = DIEInteger::BestForm(true, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000190 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
David Blaikief2443192013-10-21 17:28:37 +0000191 Die->addValue(Attribute, *Form, Value);
192}
193
Eric Christopher4a741042014-02-16 08:46:55 +0000194void DwarfUnit::addSInt(DIELoc *Die, Optional<dwarf::Form> Form,
Eric Christophera5a79422013-12-09 23:32:48 +0000195 int64_t Integer) {
David Blaikief2443192013-10-21 17:28:37 +0000196 addSInt(Die, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000197}
198
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000199/// addString - Add a string attribute data and value. We always emit a
200/// reference to the string pool instead of immediate strings so that DIEs have
Eric Christopherfba22602013-01-07 19:32:45 +0000201/// more predictable sizes. In the case of split dwarf we emit an index
202/// into another table which gets us the static offset into the string
203/// table.
Eric Christophera5a79422013-12-09 23:32:48 +0000204void DwarfUnit::addString(DIE *Die, dwarf::Attribute Attribute,
205 StringRef String) {
Eric Christopher05893f42013-12-30 18:32:31 +0000206
207 if (!DD->useSplitDwarf())
208 return addLocalString(Die, Attribute, String);
209
210 unsigned idx = DU->getStringPoolIndex(String);
211 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
Eric Christopher67646432013-07-26 17:02:41 +0000212 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
Eric Christopher05893f42013-12-30 18:32:31 +0000213 Die->addValue(Attribute, dwarf::DW_FORM_GNU_str_index, Str);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000214}
215
216/// addLocalString - Add a string attribute data and value. This is guaranteed
217/// to be in the local string pool instead of indirected.
Eric Christophera5a79422013-12-09 23:32:48 +0000218void DwarfUnit::addLocalString(DIE *Die, dwarf::Attribute Attribute,
219 StringRef String) {
Eric Christophere698f532012-12-20 21:58:36 +0000220 MCSymbol *Symb = DU->getStringPoolEntry(String);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000221 DIEValue *Value;
Eric Christopher84588622013-12-28 01:39:17 +0000222 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000223 Value = new (DIEValueAllocator) DIELabel(Symb);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000224 else {
Eric Christophere698f532012-12-20 21:58:36 +0000225 MCSymbol *StringPool = DU->getStringPoolSym();
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000226 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000227 }
Eric Christopher05893f42013-12-30 18:32:31 +0000228 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
229 Die->addValue(Attribute, dwarf::DW_FORM_strp, Str);
Devang Patel0e821f42011-04-12 23:21:44 +0000230}
231
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000232/// addExpr - Add a Dwarf expression attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000233///
Eric Christopher4a741042014-02-16 08:46:55 +0000234void DwarfUnit::addExpr(DIELoc *Die, dwarf::Form Form, const MCExpr *Expr) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000235 DIEValue *Value = new (DIEValueAllocator) DIEExpr(Expr);
David Blaikief2443192013-10-21 17:28:37 +0000236 Die->addValue((dwarf::Attribute)0, Form, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000237}
238
Eric Christophera27220f2014-03-05 22:41:20 +0000239/// addLocationList - Add a Dwarf loclistptr attribute data and value.
240///
241void DwarfUnit::addLocationList(DIE *Die, dwarf::Attribute Attribute,
242 unsigned Index) {
243 DIEValue *Value = new (DIEValueAllocator) DIELocList(Index);
244 dwarf::Form Form = DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
245 : dwarf::DW_FORM_data4;
246 Die->addValue(Attribute, Form, Value);
247}
248
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000249/// addLabel - Add a Dwarf label attribute data and value.
250///
Eric Christophera5a79422013-12-09 23:32:48 +0000251void DwarfUnit::addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
252 const MCSymbol *Label) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000253 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
254 Die->addValue(Attribute, Form, Value);
David Blaikief3cd7c52013-06-28 20:05:04 +0000255}
256
Eric Christopher4a741042014-02-16 08:46:55 +0000257void DwarfUnit::addLabel(DIELoc *Die, dwarf::Form Form, const MCSymbol *Label) {
David Blaikief2443192013-10-21 17:28:37 +0000258 addLabel(Die, (dwarf::Attribute)0, Form, Label);
259}
260
Eric Christopher33ff6972013-11-21 23:46:41 +0000261/// addSectionLabel - Add a Dwarf section label attribute data and value.
262///
Eric Christophera5a79422013-12-09 23:32:48 +0000263void DwarfUnit::addSectionLabel(DIE *Die, dwarf::Attribute Attribute,
264 const MCSymbol *Label) {
Eric Christopher33ff6972013-11-21 23:46:41 +0000265 if (DD->getDwarfVersion() >= 4)
266 addLabel(Die, Attribute, dwarf::DW_FORM_sec_offset, Label);
267 else
268 addLabel(Die, Attribute, dwarf::DW_FORM_data4, Label);
269}
270
271/// addSectionOffset - Add an offset into a section attribute data and value.
272///
Eric Christophera5a79422013-12-09 23:32:48 +0000273void DwarfUnit::addSectionOffset(DIE *Die, dwarf::Attribute Attribute,
274 uint64_t Integer) {
Eric Christopher33ff6972013-11-21 23:46:41 +0000275 if (DD->getDwarfVersion() >= 4)
276 addUInt(Die, Attribute, dwarf::DW_FORM_sec_offset, Integer);
277 else
278 addUInt(Die, Attribute, dwarf::DW_FORM_data4, Integer);
279}
280
Eric Christopher962c9082013-01-15 23:56:56 +0000281/// addLabelAddress - Add a dwarf label attribute data and value using
282/// DW_FORM_addr or DW_FORM_GNU_addr_index.
283///
Eric Christopher4287a492013-12-09 23:57:44 +0000284void DwarfCompileUnit::addLabelAddress(DIE *Die, dwarf::Attribute Attribute,
Eric Christophere9551ec2014-03-20 00:12:06 +0000285 MCSymbol *Label) {
Alexey Samsonov4436bf02013-10-03 08:54:43 +0000286 if (Label)
287 DD->addArangeLabel(SymbolCU(this, Label));
Richard Mitton21101b32013-09-19 23:21:01 +0000288
Eric Christophere9551ec2014-03-20 00:12:06 +0000289 if (!DD->useSplitDwarf()) {
290 if (Label) {
291 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
292 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
293 } else {
294 DIEValue *Value = new (DIEValueAllocator) DIEInteger(0);
295 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
296 }
Eric Christopher962c9082013-01-15 23:56:56 +0000297 } else {
Eric Christophere9551ec2014-03-20 00:12:06 +0000298 unsigned idx = DU->getAddrPoolIndex(Label);
299 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
300 Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
Eric Christopher962c9082013-01-15 23:56:56 +0000301 }
302}
303
David Blaikie0e8d4012014-03-17 23:53:25 +0000304unsigned DwarfCompileUnit::getOrCreateSourceID(StringRef FileName, StringRef DirName) {
305 // If we print assembly, we can't separate .file entries according to
306 // compile units. Thus all files will belong to the default compile unit.
307
308 // FIXME: add a better feature test than hasRawTextSupport. Even better,
309 // extend .file to support this.
310 return Asm->OutStreamer.EmitDwarfFileDirective(
311 0, DirName, FileName,
312 Asm->OutStreamer.hasRawTextSupport() ? 0 : getUniqueID());
313}
314
David Blaikie4a2f95f2014-03-18 01:17:26 +0000315unsigned DwarfTypeUnit::getOrCreateSourceID(StringRef FileName, StringRef DirName) {
316 return SplitLineTable ? SplitLineTable->getFile(DirName, FileName)
317 : getCU().getOrCreateSourceID(FileName, DirName);
318}
319
Eric Christophere9ec2452013-01-18 22:11:33 +0000320/// addOpAddress - Add a dwarf op address data and value using the
321/// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
322///
Eric Christopher4a741042014-02-16 08:46:55 +0000323void DwarfUnit::addOpAddress(DIELoc *Die, const MCSymbol *Sym) {
Eric Christophere9ec2452013-01-18 22:11:33 +0000324 if (!DD->useSplitDwarf()) {
David Blaikief2443192013-10-21 17:28:37 +0000325 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
326 addLabel(Die, dwarf::DW_FORM_udata, Sym);
Eric Christophere9ec2452013-01-18 22:11:33 +0000327 } else {
David Blaikief2443192013-10-21 17:28:37 +0000328 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
329 addUInt(Die, dwarf::DW_FORM_GNU_addr_index, DU->getAddrPoolIndex(Sym));
Eric Christophere9ec2452013-01-18 22:11:33 +0000330 }
331}
332
Eric Christopher33ff6972013-11-21 23:46:41 +0000333/// addSectionDelta - Add a section label delta attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000334///
Eric Christophera5a79422013-12-09 23:32:48 +0000335void DwarfUnit::addSectionDelta(DIE *Die, dwarf::Attribute Attribute,
336 const MCSymbol *Hi, const MCSymbol *Lo) {
Devang Patel0e821f42011-04-12 23:21:44 +0000337 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Eric Christopher33ff6972013-11-21 23:46:41 +0000338 if (DD->getDwarfVersion() >= 4)
339 Die->addValue(Attribute, dwarf::DW_FORM_sec_offset, Value);
340 else
341 Die->addValue(Attribute, dwarf::DW_FORM_data4, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000342}
343
David Blaikie48b1bdc2014-03-07 01:30:55 +0000344void DwarfUnit::addLabelDelta(DIE *Die, dwarf::Attribute Attribute,
345 const MCSymbol *Hi, const MCSymbol *Lo) {
346 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
347 Die->addValue(Attribute, dwarf::DW_FORM_data4, Value);
348}
349
Devang Patel0e821f42011-04-12 23:21:44 +0000350/// addDIEEntry - Add a DIE attribute data and value.
351///
Eric Christophera5a79422013-12-09 23:32:48 +0000352void DwarfUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +0000353 addDIEEntry(Die, Attribute, createDIEEntry(Entry));
354}
355
David Blaikie47f615e2013-12-17 23:32:35 +0000356void DwarfUnit::addDIETypeSignature(DIE *Die, const DwarfTypeUnit &Type) {
357 Die->addValue(dwarf::DW_AT_signature, dwarf::DW_FORM_ref_sig8,
358 new (DIEValueAllocator) DIETypeSignature(Type));
359}
360
Eric Christophera5a79422013-12-09 23:32:48 +0000361void DwarfUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute,
362 DIEEntry *Entry) {
David Blaikie409dd9c2013-11-19 23:08:21 +0000363 const DIE *DieCU = Die->getUnitOrNull();
364 const DIE *EntryCU = Entry->getEntry()->getUnitOrNull();
Manman Ren4dbdc902013-10-31 17:54:35 +0000365 if (!DieCU)
366 // We assume that Die belongs to this CU, if it is not linked to any CU yet.
David Blaikie2a80e442013-12-02 22:09:48 +0000367 DieCU = getUnitDie();
Manman Ren4dbdc902013-10-31 17:54:35 +0000368 if (!EntryCU)
David Blaikie2a80e442013-12-02 22:09:48 +0000369 EntryCU = getUnitDie();
Manman Ren4dbdc902013-10-31 17:54:35 +0000370 Die->addValue(Attribute, EntryCU == DieCU ? dwarf::DW_FORM_ref4
371 : dwarf::DW_FORM_ref_addr,
372 Entry);
Devang Patel0e821f42011-04-12 23:21:44 +0000373}
374
Manman Renb987e512013-10-29 00:53:03 +0000375/// Create a DIE with the given Tag, add the DIE to its parent, and
376/// call insertDIE if MD is not null.
Eric Christophera5a79422013-12-09 23:32:48 +0000377DIE *DwarfUnit::createAndAddDIE(unsigned Tag, DIE &Parent, DIDescriptor N) {
Manman Renb987e512013-10-29 00:53:03 +0000378 DIE *Die = new DIE(Tag);
379 Parent.addChild(Die);
David Blaikie52c50202013-11-16 00:29:01 +0000380 if (N)
381 insertDIE(N, Die);
Manman Renb987e512013-10-29 00:53:03 +0000382 return Die;
383}
384
Devang Patel0e821f42011-04-12 23:21:44 +0000385/// addBlock - Add block data.
386///
Eric Christopher4a741042014-02-16 08:46:55 +0000387void DwarfUnit::addBlock(DIE *Die, dwarf::Attribute Attribute, DIELoc *Loc) {
Eric Christopher8bdab432014-02-27 18:36:10 +0000388 Loc->ComputeSize(Asm);
Eric Christopher4a741042014-02-16 08:46:55 +0000389 DIELocs.push_back(Loc); // Memoize so we can call the destructor later on.
390 Die->addValue(Attribute, Loc->BestForm(DD->getDwarfVersion()), Loc);
391}
392
Eric Christophera5a79422013-12-09 23:32:48 +0000393void DwarfUnit::addBlock(DIE *Die, dwarf::Attribute Attribute,
394 DIEBlock *Block) {
Eric Christopher8bdab432014-02-27 18:36:10 +0000395 Block->ComputeSize(Asm);
Devang Patel0e821f42011-04-12 23:21:44 +0000396 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
397 Die->addValue(Attribute, Block->BestForm(), Block);
398}
399
400/// addSourceLine - Add location information to specified debug information
401/// entry.
David Blaikie101613e2014-02-12 00:11:25 +0000402void DwarfUnit::addSourceLine(DIE *Die, unsigned Line, StringRef File,
403 StringRef Directory) {
Devang Patel0e821f42011-04-12 23:21:44 +0000404 if (Line == 0)
405 return;
David Blaikie101613e2014-02-12 00:11:25 +0000406
David Blaikie4a2f95f2014-03-18 01:17:26 +0000407 unsigned FileID = getOrCreateSourceID(File, Directory);
Devang Patel0e821f42011-04-12 23:21:44 +0000408 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000409 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
410 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000411}
412
413/// addSourceLine - Add location information to specified debug information
414/// entry.
David Blaikie101613e2014-02-12 00:11:25 +0000415void DwarfUnit::addSourceLine(DIE *Die, DIVariable V) {
416 assert(V.isVariable());
417
Eric Christopher89a575c2014-02-12 22:38:04 +0000418 addSourceLine(Die, V.getLineNumber(), V.getContext().getFilename(),
419 V.getContext().getDirectory());
David Blaikie101613e2014-02-12 00:11:25 +0000420}
421
422/// addSourceLine - Add location information to specified debug information
423/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000424void DwarfUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
David Blaikie52019302014-02-11 23:57:03 +0000425 assert(G.isGlobalVariable());
Devang Patel0e821f42011-04-12 23:21:44 +0000426
David Blaikie101613e2014-02-12 00:11:25 +0000427 addSourceLine(Die, G.getLineNumber(), G.getFilename(), G.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +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, DISubprogram SP) {
David Blaikie52019302014-02-11 23:57:03 +0000433 assert(SP.isSubprogram());
Eric Christopher7734ca22012-03-15 23:55:40 +0000434
David Blaikie101613e2014-02-12 00:11:25 +0000435 addSourceLine(Die, SP.getLineNumber(), SP.getFilename(), SP.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000436}
437
438/// addSourceLine - Add location information to specified debug information
439/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000440void DwarfUnit::addSourceLine(DIE *Die, DIType Ty) {
David Blaikie52019302014-02-11 23:57:03 +0000441 assert(Ty.isType());
Devang Patel0e821f42011-04-12 23:21:44 +0000442
David Blaikie101613e2014-02-12 00:11:25 +0000443 addSourceLine(Die, Ty.getLineNumber(), Ty.getFilename(), Ty.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000444}
445
446/// addSourceLine - Add location information to specified debug information
447/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000448void DwarfUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
David Blaikie52019302014-02-11 23:57:03 +0000449 assert(Ty.isObjCProperty());
Eric Christopher70e1bd82012-03-29 08:42:56 +0000450
Eric Christopher70e1bd82012-03-29 08:42:56 +0000451 DIFile File = Ty.getFile();
David Blaikie101613e2014-02-12 00:11:25 +0000452 addSourceLine(Die, Ty.getLineNumber(), File.getFilename(),
453 File.getDirectory());
Eric Christopher70e1bd82012-03-29 08:42:56 +0000454}
455
456/// addSourceLine - Add location information to specified debug information
457/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000458void DwarfUnit::addSourceLine(DIE *Die, DINameSpace NS) {
David Blaikie52019302014-02-11 23:57:03 +0000459 assert(NS.Verify());
Devang Patel0e821f42011-04-12 23:21:44 +0000460
David Blaikie101613e2014-02-12 00:11:25 +0000461 addSourceLine(Die, NS.getLineNumber(), NS.getFilename(), NS.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000462}
463
Eric Christopher92331fd2012-11-21 00:34:38 +0000464/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patel77dc5412011-04-27 22:45:24 +0000465/// DbgVariable based on provided MachineLocation.
Eric Christophera5a79422013-12-09 23:32:48 +0000466void DwarfUnit::addVariableAddress(const DbgVariable &DV, DIE *Die,
467 MachineLocation Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000468 if (DV.variableHasComplexAddress())
Devang Patel0e821f42011-04-12 23:21:44 +0000469 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000470 else if (DV.isBlockByrefVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000471 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
472 else
David Blaikieea2605d2013-06-20 00:25:24 +0000473 addAddress(Die, dwarf::DW_AT_location, Location,
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000474 DV.getVariable().isIndirect());
Devang Patel0e821f42011-04-12 23:21:44 +0000475}
476
Devang Patelba5fbf12011-04-26 19:06:18 +0000477/// addRegisterOp - Add register operand.
Eric Christopher4a741042014-02-16 08:46:55 +0000478void DwarfUnit::addRegisterOp(DIELoc *TheDie, unsigned Reg) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000479 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000480 int DWReg = RI->getDwarfRegNum(Reg, false);
481 bool isSubRegister = DWReg < 0;
482
483 unsigned Idx = 0;
484
485 // Go up the super-register chain until we hit a valid dwarf register number.
486 for (MCSuperRegIterator SR(Reg, RI); SR.isValid() && DWReg < 0; ++SR) {
487 DWReg = RI->getDwarfRegNum(*SR, false);
488 if (DWReg >= 0)
489 Idx = RI->getSubRegIndex(*SR, Reg);
490 }
491
492 if (DWReg < 0) {
Eric Christophera13839f2014-02-26 23:27:16 +0000493 DEBUG(dbgs() << "Invalid Dwarf register number.\n");
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000494 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_nop);
495 return;
496 }
497
498 // Emit register
Devang Patelba5fbf12011-04-26 19:06:18 +0000499 if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000500 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000501 else {
David Blaikief2443192013-10-21 17:28:37 +0000502 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
503 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000504 }
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000505
506 // Emit Mask
507 if (isSubRegister) {
508 unsigned Size = RI->getSubRegIdxSize(Idx);
509 unsigned Offset = RI->getSubRegIdxOffset(Idx);
510 if (Offset > 0) {
511 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bit_piece);
512 addUInt(TheDie, dwarf::DW_FORM_data1, Size);
513 addUInt(TheDie, dwarf::DW_FORM_data1, Offset);
514 } else {
Adrian Prantl7199fd52014-02-12 19:34:44 +0000515 unsigned ByteSize = Size / 8; // Assuming 8 bits per byte.
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000516 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_piece);
Adrian Prantl7199fd52014-02-12 19:34:44 +0000517 addUInt(TheDie, dwarf::DW_FORM_data1, ByteSize);
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000518 }
519 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000520}
521
522/// addRegisterOffset - Add register offset.
Eric Christopher4a741042014-02-16 08:46:55 +0000523void DwarfUnit::addRegisterOffset(DIELoc *TheDie, unsigned Reg,
Eric Christophera5a79422013-12-09 23:32:48 +0000524 int64_t Offset) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000525 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
526 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
527 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
528 if (Reg == TRI->getFrameRegister(*Asm->MF))
529 // If variable offset is based in frame register then use fbreg.
David Blaikief2443192013-10-21 17:28:37 +0000530 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000531 else if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000532 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000533 else {
David Blaikief2443192013-10-21 17:28:37 +0000534 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
535 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000536 }
David Blaikief2443192013-10-21 17:28:37 +0000537 addSInt(TheDie, dwarf::DW_FORM_sdata, Offset);
Devang Patelba5fbf12011-04-26 19:06:18 +0000538}
539
540/// addAddress - Add an address attribute to a die based on the location
541/// provided.
Eric Christophera5a79422013-12-09 23:32:48 +0000542void DwarfUnit::addAddress(DIE *Die, dwarf::Attribute Attribute,
543 const MachineLocation &Location, bool Indirect) {
Eric Christopher4a741042014-02-16 08:46:55 +0000544 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Devang Patelba5fbf12011-04-26 19:06:18 +0000545
David Blaikieea2605d2013-06-20 00:25:24 +0000546 if (Location.isReg() && !Indirect)
Eric Christopher4a741042014-02-16 08:46:55 +0000547 addRegisterOp(Loc, Location.getReg());
David Blaikieea2605d2013-06-20 00:25:24 +0000548 else {
Eric Christopher4a741042014-02-16 08:46:55 +0000549 addRegisterOffset(Loc, Location.getReg(), Location.getOffset());
David Blaikieea2605d2013-06-20 00:25:24 +0000550 if (Indirect && !Location.isReg()) {
Eric Christopher4a741042014-02-16 08:46:55 +0000551 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
David Blaikieea2605d2013-06-20 00:25:24 +0000552 }
553 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000554
555 // Now attach the location information to the DIE.
Eric Christopher4a741042014-02-16 08:46:55 +0000556 addBlock(Die, Attribute, Loc);
Devang Patelba5fbf12011-04-26 19:06:18 +0000557}
558
Devang Patel0e821f42011-04-12 23:21:44 +0000559/// addComplexAddress - Start with the address based on the location provided,
560/// and generate the DWARF information necessary to find the actual variable
Eric Christopher1c70b672013-12-05 00:13:15 +0000561/// given the extra address information encoded in the DbgVariable, starting
562/// from the starting location. Add the DWARF information to the die.
Devang Patel0e821f42011-04-12 23:21:44 +0000563///
Eric Christophera5a79422013-12-09 23:32:48 +0000564void DwarfUnit::addComplexAddress(const DbgVariable &DV, DIE *Die,
565 dwarf::Attribute Attribute,
566 const MachineLocation &Location) {
Eric Christopher4a741042014-02-16 08:46:55 +0000567 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000568 unsigned N = DV.getNumAddrElements();
Devang Patel3e021532011-04-28 02:22:40 +0000569 unsigned i = 0;
570 if (Location.isReg()) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000571 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Devang Patel3e021532011-04-28 02:22:40 +0000572 // If first address element is OpPlus then emit
573 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Eric Christopher4a741042014-02-16 08:46:55 +0000574 addRegisterOffset(Loc, Location.getReg(), DV.getAddrElement(1));
Devang Patel3e021532011-04-28 02:22:40 +0000575 i = 2;
576 } else
Eric Christopher4a741042014-02-16 08:46:55 +0000577 addRegisterOp(Loc, Location.getReg());
Eric Christopherc2697f82013-10-19 01:04:47 +0000578 } else
Eric Christopher4a741042014-02-16 08:46:55 +0000579 addRegisterOffset(Loc, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000580
Eric Christopherc2697f82013-10-19 01:04:47 +0000581 for (; i < N; ++i) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000582 uint64_t Element = DV.getAddrElement(i);
Devang Patel0e821f42011-04-12 23:21:44 +0000583 if (Element == DIBuilder::OpPlus) {
Eric Christopher4a741042014-02-16 08:46:55 +0000584 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
585 addUInt(Loc, dwarf::DW_FORM_udata, DV.getAddrElement(++i));
Devang Patel0e821f42011-04-12 23:21:44 +0000586 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher4d250522012-05-08 18:56:00 +0000587 if (!Location.isReg())
Eric Christopher4a741042014-02-16 08:46:55 +0000588 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Eric Christopherc2697f82013-10-19 01:04:47 +0000589 } else
590 llvm_unreachable("unknown DIBuilder Opcode");
Devang Patel0e821f42011-04-12 23:21:44 +0000591 }
592
593 // Now attach the location information to the DIE.
Eric Christopher4a741042014-02-16 08:46:55 +0000594 addBlock(Die, Attribute, Loc);
Devang Patel0e821f42011-04-12 23:21:44 +0000595}
596
597/* Byref variables, in Blocks, are declared by the programmer as "SomeType
598 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
599 gives the variable VarName either the struct, or a pointer to the struct, as
600 its type. This is necessary for various behind-the-scenes things the
601 compiler needs to do with by-reference variables in Blocks.
602
603 However, as far as the original *programmer* is concerned, the variable
604 should still have type 'SomeType', as originally declared.
605
606 The function getBlockByrefType dives into the __Block_byref_x_VarName
607 struct to find the original type of the variable, which is then assigned to
608 the variable's Debug Information Entry as its real type. So far, so good.
609 However now the debugger will expect the variable VarName to have the type
610 SomeType. So we need the location attribute for the variable to be an
611 expression that explains to the debugger how to navigate through the
612 pointers and struct to find the actual variable of type SomeType.
613
614 The following function does just that. We start by getting
615 the "normal" location for the variable. This will be the location
616 of either the struct __Block_byref_x_VarName or the pointer to the
617 struct __Block_byref_x_VarName.
618
619 The struct will look something like:
620
621 struct __Block_byref_x_VarName {
622 ... <various fields>
623 struct __Block_byref_x_VarName *forwarding;
624 ... <various other fields>
625 SomeType VarName;
626 ... <maybe more fields>
627 };
628
629 If we are given the struct directly (as our starting point) we
630 need to tell the debugger to:
631
632 1). Add the offset of the forwarding field.
633
634 2). Follow that pointer to get the real __Block_byref_x_VarName
635 struct to use (the real one may have been copied onto the heap).
636
637 3). Add the offset for the field VarName, to find the actual variable.
638
639 If we started with a pointer to the struct, then we need to
640 dereference that pointer first, before the other steps.
641 Translating this into DWARF ops, we will need to append the following
642 to the current location description for the variable:
643
644 DW_OP_deref -- optional, if we start with a pointer
645 DW_OP_plus_uconst <forward_fld_offset>
646 DW_OP_deref
647 DW_OP_plus_uconst <varName_fld_offset>
648
649 That is what this function does. */
650
651/// addBlockByrefAddress - Start with the address based on the location
652/// provided, and generate the DWARF information necessary to find the
653/// actual Block variable (navigating the Block struct) based on the
654/// starting location. Add the DWARF information to the die. For
655/// more information, read large comment just above here.
656///
Eric Christophera5a79422013-12-09 23:32:48 +0000657void DwarfUnit::addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
658 dwarf::Attribute Attribute,
659 const MachineLocation &Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000660 DIType Ty = DV.getType();
Devang Patel0e821f42011-04-12 23:21:44 +0000661 DIType TmpTy = Ty;
Eric Christopher31b05762013-08-08 01:41:00 +0000662 uint16_t Tag = Ty.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +0000663 bool isPointer = false;
664
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000665 StringRef varName = DV.getName();
Devang Patel0e821f42011-04-12 23:21:44 +0000666
667 if (Tag == dwarf::DW_TAG_pointer_type) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000668 DIDerivedType DTy(Ty);
Manman Ren93b30902013-10-08 18:42:58 +0000669 TmpTy = resolve(DTy.getTypeDerivedFrom());
Devang Patel0e821f42011-04-12 23:21:44 +0000670 isPointer = true;
671 }
672
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000673 DICompositeType blockStruct(TmpTy);
Devang Patel0e821f42011-04-12 23:21:44 +0000674
675 // Find the __forwarding field and the variable field in the __Block_byref
676 // struct.
677 DIArray Fields = blockStruct.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000678 DIDerivedType varField;
679 DIDerivedType forwardingField;
Devang Patel0e821f42011-04-12 23:21:44 +0000680
681 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000682 DIDerivedType DT(Fields.getElement(i));
Devang Patel0e821f42011-04-12 23:21:44 +0000683 StringRef fieldName = DT.getName();
684 if (fieldName == "__forwarding")
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000685 forwardingField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000686 else if (fieldName == varName)
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000687 varField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000688 }
689
690 // Get the offsets for the forwarding field and the variable field.
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000691 unsigned forwardingFieldOffset = forwardingField.getOffsetInBits() >> 3;
692 unsigned varFieldOffset = varField.getOffsetInBits() >> 2;
Devang Patel0e821f42011-04-12 23:21:44 +0000693
694 // Decode the original location, and use that as the start of the byref
695 // variable's location.
Eric Christopher4a741042014-02-16 08:46:55 +0000696 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Devang Patel0e821f42011-04-12 23:21:44 +0000697
Eric Christopheref9d7102012-07-04 02:02:18 +0000698 if (Location.isReg())
Eric Christopher4a741042014-02-16 08:46:55 +0000699 addRegisterOp(Loc, Location.getReg());
Eric Christopheref9d7102012-07-04 02:02:18 +0000700 else
Eric Christopher4a741042014-02-16 08:46:55 +0000701 addRegisterOffset(Loc, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000702
703 // If we started with a pointer to the __Block_byref... struct, then
704 // the first thing we need to do is dereference the pointer (DW_OP_deref).
705 if (isPointer)
Eric Christopher4a741042014-02-16 08:46:55 +0000706 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000707
708 // Next add the offset for the '__forwarding' field:
709 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
710 // adding the offset if it's 0.
711 if (forwardingFieldOffset > 0) {
Eric Christopher4a741042014-02-16 08:46:55 +0000712 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
713 addUInt(Loc, dwarf::DW_FORM_udata, forwardingFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000714 }
715
716 // Now dereference the __forwarding field to get to the real __Block_byref
717 // struct: DW_OP_deref.
Eric Christopher4a741042014-02-16 08:46:55 +0000718 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000719
720 // Now that we've got the real __Block_byref... struct, add the offset
721 // for the variable's field to get to the location of the actual variable:
722 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
723 if (varFieldOffset > 0) {
Eric Christopher4a741042014-02-16 08:46:55 +0000724 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
725 addUInt(Loc, dwarf::DW_FORM_udata, varFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000726 }
727
728 // Now attach the location information to the DIE.
Eric Christopher4a741042014-02-16 08:46:55 +0000729 addBlock(Die, Attribute, Loc);
Devang Patel0e821f42011-04-12 23:21:44 +0000730}
731
Devang Patelbcd50a12011-07-20 21:57:04 +0000732/// isTypeSigned - Return true if the type is signed.
Manman Renb3388602013-10-05 01:43:03 +0000733static bool isTypeSigned(DwarfDebug *DD, DIType Ty, int *SizeInBits) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000734 if (Ty.isDerivedType())
Manman Renb3388602013-10-05 01:43:03 +0000735 return isTypeSigned(DD, DD->resolve(DIDerivedType(Ty).getTypeDerivedFrom()),
736 SizeInBits);
Devang Patelbcd50a12011-07-20 21:57:04 +0000737 if (Ty.isBasicType())
Eric Christopherc2697f82013-10-19 01:04:47 +0000738 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed ||
739 DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000740 *SizeInBits = Ty.getSizeInBits();
741 return true;
742 }
743 return false;
744}
745
Manman Renb3388602013-10-05 01:43:03 +0000746/// Return true if type encoding is unsigned.
747static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) {
748 DIDerivedType DTy(Ty);
749 if (DTy.isDerivedType())
750 return isUnsignedDIType(DD, DD->resolve(DTy.getTypeDerivedFrom()));
751
752 DIBasicType BTy(Ty);
753 if (BTy.isBasicType()) {
754 unsigned Encoding = BTy.getEncoding();
755 if (Encoding == dwarf::DW_ATE_unsigned ||
756 Encoding == dwarf::DW_ATE_unsigned_char ||
757 Encoding == dwarf::DW_ATE_boolean)
758 return true;
759 }
760 return false;
761}
762
763/// If this type is derived from a base type then return base type size.
Manman Renbda410f2013-10-08 18:46:58 +0000764static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) {
Manman Renb3388602013-10-05 01:43:03 +0000765 unsigned Tag = Ty.getTag();
766
767 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
768 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
769 Tag != dwarf::DW_TAG_restrict_type)
770 return Ty.getSizeInBits();
771
772 DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom());
773
Eric Christopher8cc04fc2014-03-12 18:18:05 +0000774 // If this type is not derived from any type or the type is a declaration then
775 // take conservative approach.
776 if (!BaseType.isValid() || BaseType.isForwardDecl())
Manman Renb3388602013-10-05 01:43:03 +0000777 return Ty.getSizeInBits();
778
779 // If this is a derived type, go ahead and get the base type, unless it's a
780 // reference then it's just the size of the field. Pointer types have no need
781 // of this since they're a different type of qualification on the type.
782 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
783 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
784 return Ty.getSizeInBits();
785
786 if (BaseType.isDerivedType())
Manman Renbda410f2013-10-08 18:46:58 +0000787 return getBaseTypeSize(DD, DIDerivedType(BaseType));
Manman Renb3388602013-10-05 01:43:03 +0000788
789 return BaseType.getSizeInBits();
790}
791
Devang Patel0e821f42011-04-12 23:21:44 +0000792/// addConstantValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000793void DwarfUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
794 DIType Ty) {
David Blaikiea1e813d2013-05-10 21:52:07 +0000795 // FIXME: This is a bit conservative/simple - it emits negative values at
796 // their maximum bit width which is a bit unfortunate (& doesn't prefer
797 // udata/sdata over dataN as suggested by the DWARF spec)
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000798 assert(MO.isImm() && "Invalid machine operand!");
Devang Patelbcd50a12011-07-20 21:57:04 +0000799 int SizeInBits = -1;
Manman Renb3388602013-10-05 01:43:03 +0000800 bool SignedConstant = isTypeSigned(DD, Ty, &SizeInBits);
David Blaikief2443192013-10-21 17:28:37 +0000801 dwarf::Form Form;
Devang Patelf1d04702011-05-27 18:15:52 +0000802
Eric Christopher9d1daa82013-08-27 23:49:04 +0000803 // If we're a signed constant definitely use sdata.
804 if (SignedConstant) {
805 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, MO.getImm());
806 return;
807 }
808
809 // Else use data for now unless it's larger than we can deal with.
810 switch (SizeInBits) {
811 case 8:
812 Form = dwarf::DW_FORM_data1;
813 break;
814 case 16:
815 Form = dwarf::DW_FORM_data2;
816 break;
817 case 32:
818 Form = dwarf::DW_FORM_data4;
819 break;
820 case 64:
821 Form = dwarf::DW_FORM_data8;
822 break;
823 default:
824 Form = dwarf::DW_FORM_udata;
825 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
826 return;
827 }
828 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
Devang Patel0e821f42011-04-12 23:21:44 +0000829}
830
831/// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000832void DwarfUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000833 assert(MO.isFPImm() && "Invalid machine operand!");
Devang Patel0e821f42011-04-12 23:21:44 +0000834 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
835 APFloat FPImm = MO.getFPImm()->getValueAPF();
836
837 // Get the raw data form of the floating point.
838 const APInt FltVal = FPImm.bitcastToAPInt();
Eric Christopherc2697f82013-10-19 01:04:47 +0000839 const char *FltPtr = (const char *)FltVal.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000840
841 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000842 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000843 int Incr = (LittleEndian ? 1 : -1);
844 int Start = (LittleEndian ? 0 : NumBytes - 1);
845 int Stop = (LittleEndian ? NumBytes : -1);
846
847 // Output the constant to DWARF one byte at a time.
848 for (; Start != Stop; Start += Incr)
Eric Christopher98b7f172013-11-11 18:52:36 +0000849 addUInt(Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]);
Devang Patel0e821f42011-04-12 23:21:44 +0000850
David Blaikief2443192013-10-21 17:28:37 +0000851 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000852}
853
David Blaikiea39a76e2013-01-20 01:18:01 +0000854/// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000855void DwarfUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000856 // Pass this down to addConstantValue as an unsigned bag of bits.
857 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
David Blaikiea39a76e2013-01-20 01:18:01 +0000858}
859
Devang Patel0e821f42011-04-12 23:21:44 +0000860/// addConstantValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000861void DwarfUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
862 bool Unsigned) {
Eric Christopher78fcf4902013-07-03 01:08:30 +0000863 addConstantValue(Die, CI->getValue(), Unsigned);
David Blaikiea39a76e2013-01-20 01:18:01 +0000864}
865
866// addConstantValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000867void DwarfUnit::addConstantValue(DIE *Die, const APInt &Val, bool Unsigned) {
David Blaikiea39a76e2013-01-20 01:18:01 +0000868 unsigned CIBitWidth = Val.getBitWidth();
Devang Patel8816bbc2011-05-28 00:39:18 +0000869 if (CIBitWidth <= 64) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000870 // If we're a signed constant definitely use sdata.
871 if (!Unsigned) {
872 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
873 Val.getSExtValue());
874 return;
Devang Patel8816bbc2011-05-28 00:39:18 +0000875 }
Eric Christopher9d1daa82013-08-27 23:49:04 +0000876
877 // Else use data for now unless it's larger than we can deal with.
David Blaikief2443192013-10-21 17:28:37 +0000878 dwarf::Form Form;
Eric Christopher9d1daa82013-08-27 23:49:04 +0000879 switch (CIBitWidth) {
880 case 8:
881 Form = dwarf::DW_FORM_data1;
882 break;
883 case 16:
884 Form = dwarf::DW_FORM_data2;
885 break;
886 case 32:
887 Form = dwarf::DW_FORM_data4;
888 break;
889 case 64:
890 Form = dwarf::DW_FORM_data8;
891 break;
892 default:
893 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
894 Val.getZExtValue());
895 return;
896 }
897 addUInt(Die, dwarf::DW_AT_const_value, Form, Val.getZExtValue());
Eric Christopher78fcf4902013-07-03 01:08:30 +0000898 return;
Devang Patel0e821f42011-04-12 23:21:44 +0000899 }
900
901 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
902
903 // Get the raw data form of the large APInt.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000904 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000905
906 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000907 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000908
909 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000910 for (int i = 0; i < NumBytes; i++) {
911 uint8_t c;
912 if (LittleEndian)
913 c = Ptr64[i / 8] >> (8 * (i & 7));
914 else
915 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
David Blaikief2443192013-10-21 17:28:37 +0000916 addUInt(Block, dwarf::DW_FORM_data1, c);
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000917 }
Devang Patel0e821f42011-04-12 23:21:44 +0000918
David Blaikief2443192013-10-21 17:28:37 +0000919 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000920}
921
Eric Christopher25e35092013-04-22 07:47:40 +0000922/// addTemplateParams - Add template parameters into buffer.
Eric Christophera5a79422013-12-09 23:32:48 +0000923void DwarfUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
Devang Patel0e821f42011-04-12 23:21:44 +0000924 // Add template parameters.
925 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
926 DIDescriptor Element = TParams.getElement(i);
927 if (Element.isTemplateTypeParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000928 constructTemplateTypeParameterDIE(Buffer,
929 DITemplateTypeParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000930 else if (Element.isTemplateValueParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000931 constructTemplateValueParameterDIE(Buffer,
932 DITemplateValueParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000933 }
Devang Patel0e821f42011-04-12 23:21:44 +0000934}
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000935
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000936/// getOrCreateContextDIE - Get context owner's DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000937DIE *DwarfUnit::getOrCreateContextDIE(DIScope Context) {
David Blaikiebd700e42013-11-14 21:24:34 +0000938 if (!Context || Context.isFile())
David Blaikie2a80e442013-12-02 22:09:48 +0000939 return getUnitDie();
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000940 if (Context.isType())
941 return getOrCreateTypeDIE(DIType(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000942 if (Context.isNameSpace())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000943 return getOrCreateNameSpace(DINameSpace(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000944 if (Context.isSubprogram())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000945 return getOrCreateSubprogramDIE(DISubprogram(Context));
Adrian Prantl7d828bb2013-11-15 21:05:09 +0000946 return getDIE(Context);
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000947}
948
Eric Christophera5a79422013-12-09 23:32:48 +0000949DIE *DwarfUnit::createTypeDIE(DICompositeType Ty) {
David Blaikie9d861be2013-11-26 00:15:27 +0000950 DIScope Context = resolve(Ty.getContext());
951 DIE *ContextDIE = getOrCreateContextDIE(Context);
David Blaikie409dd9c2013-11-19 23:08:21 +0000952
953 DIE *TyDIE = getDIE(Ty);
954 if (TyDIE)
955 return TyDIE;
956
957 // Create new type.
958 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
959
David Blaikiefbd29eb2013-11-26 00:35:04 +0000960 constructTypeDIE(*TyDIE, Ty);
David Blaikie409dd9c2013-11-19 23:08:21 +0000961
David Blaikie9d861be2013-11-26 00:15:27 +0000962 updateAcceleratorTables(Context, Ty, TyDIE);
David Blaikie409dd9c2013-11-19 23:08:21 +0000963 return TyDIE;
964}
965
Devang Patel0e821f42011-04-12 23:21:44 +0000966/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
967/// given DIType.
Eric Christophera5a79422013-12-09 23:32:48 +0000968DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
David Blaikie32887552013-11-14 22:25:02 +0000969 if (!TyNode)
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000970 return NULL;
Manman Renf4c339e2013-10-29 22:49:29 +0000971
David Blaikie32887552013-11-14 22:25:02 +0000972 DIType Ty(TyNode);
973 assert(Ty.isType());
Adrian Prantl0aa1aa22014-03-18 02:35:03 +0000974 assert(*&Ty == resolve(Ty.getRef()) &&
975 "type was not uniqued, possible ODR violation.");
David Blaikie32887552013-11-14 22:25:02 +0000976
Manman Renf4c339e2013-10-29 22:49:29 +0000977 // Construct the context before querying for the existence of the DIE in case
978 // such construction creates the DIE.
David Blaikie9d861be2013-11-26 00:15:27 +0000979 DIScope Context = resolve(Ty.getContext());
980 DIE *ContextDIE = getOrCreateContextDIE(Context);
Adrian Prantl4583f7d2013-11-15 23:21:39 +0000981 assert(ContextDIE);
Manman Renf4c339e2013-10-29 22:49:29 +0000982
Eric Christophere595bae2013-10-04 17:08:38 +0000983 DIE *TyDIE = getDIE(Ty);
Devang Patel0e821f42011-04-12 23:21:44 +0000984 if (TyDIE)
985 return TyDIE;
986
987 // Create new type.
Manman Renf4c339e2013-10-29 22:49:29 +0000988 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
989
David Blaikiec3d9e9e2014-03-06 01:42:00 +0000990 updateAcceleratorTables(Context, Ty, TyDIE);
991
Devang Patel0e821f42011-04-12 23:21:44 +0000992 if (Ty.isBasicType())
993 constructTypeDIE(*TyDIE, DIBasicType(Ty));
David Blaikie8a263cb2013-11-26 00:22:37 +0000994 else if (Ty.isCompositeType()) {
995 DICompositeType CTy(Ty);
David Blaikiecfb21152014-01-03 18:59:42 +0000996 if (GenerateDwarfTypeUnits && !Ty.isForwardDecl())
997 if (MDString *TypeId = CTy.getIdentifier()) {
David Blaikie15632ae2014-02-12 00:31:30 +0000998 DD->addDwarfTypeUnitType(getCU(), TypeId->getString(), TyDIE, CTy);
Adrian Prantle2da17f2014-03-14 23:08:17 +0000999 // Skip updating the accelerator tables since this is not the full type.
David Blaikiecfb21152014-01-03 18:59:42 +00001000 return TyDIE;
1001 }
David Blaikiefbd29eb2013-11-26 00:35:04 +00001002 constructTypeDIE(*TyDIE, CTy);
David Blaikie8a263cb2013-11-26 00:22:37 +00001003 } else {
Devang Patel0e821f42011-04-12 23:21:44 +00001004 assert(Ty.isDerivedType() && "Unknown kind of DIType");
1005 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
1006 }
David Blaikie2ea848b2013-11-19 22:51:04 +00001007
David Blaikie2ea848b2013-11-19 22:51:04 +00001008 return TyDIE;
1009}
1010
Eric Christophera5a79422013-12-09 23:32:48 +00001011void DwarfUnit::updateAcceleratorTables(DIScope Context, DIType Ty,
1012 const DIE *TyDIE) {
Eric Christopher21bde872012-01-06 04:35:23 +00001013 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
1014 bool IsImplementation = 0;
1015 if (Ty.isCompositeType()) {
1016 DICompositeType CT(Ty);
Eric Christopher8ea8e4f2012-01-06 23:03:37 +00001017 // A runtime language of 0 actually means C/C++ and that any
1018 // non-negative value is some version of Objective-C/C++.
Eric Christopherc2697f82013-10-19 01:04:47 +00001019 IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete();
Eric Christopher21bde872012-01-06 04:35:23 +00001020 }
Eric Christophercf7289f2013-09-05 18:20:16 +00001021 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
Eric Christopher8ea8e4f2012-01-06 23:03:37 +00001022 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
David Blaikie9d861be2013-11-26 00:15:27 +00001023
David Blaikieadbea1e2014-03-12 03:34:38 +00001024 if ((!Context || Context.isCompileUnit() || Context.isFile() ||
1025 Context.isNameSpace()) &&
1026 getCUNode().getEmissionKind() != DIBuilder::LineTablesOnly)
David Blaikie9d861be2013-11-26 00:15:27 +00001027 GlobalTypes[getParentContextString(Context) + Ty.getName().str()] = TyDIE;
Eric Christopher21bde872012-01-06 04:35:23 +00001028 }
Devang Patel0e821f42011-04-12 23:21:44 +00001029}
1030
1031/// addType - Add a new type attribute to the specified entity.
Eric Christophera5a79422013-12-09 23:32:48 +00001032void DwarfUnit::addType(DIE *Entity, DIType Ty, dwarf::Attribute Attribute) {
Eric Christopher0df08e22013-08-08 07:40:37 +00001033 assert(Ty && "Trying to add a type that doesn't exist?");
Devang Patel0e821f42011-04-12 23:21:44 +00001034
1035 // Check for pre-existence.
1036 DIEEntry *Entry = getDIEEntry(Ty);
1037 // If it exists then use the existing value.
1038 if (Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +00001039 addDIEEntry(Entity, Attribute, Entry);
Devang Patel0e821f42011-04-12 23:21:44 +00001040 return;
1041 }
1042
1043 // Construct type.
1044 DIE *Buffer = getOrCreateTypeDIE(Ty);
1045
1046 // Set up proxy.
1047 Entry = createDIEEntry(Buffer);
1048 insertDIEEntry(Ty, Entry);
Manman Ren4dbdc902013-10-31 17:54:35 +00001049 addDIEEntry(Entity, Attribute, Entry);
Devang Patel1cb8ab42011-05-31 23:30:30 +00001050}
1051
Eric Christopher9cd26af2013-09-20 23:22:52 +00001052// Accelerator table mutators - add each name along with its companion
1053// DIE to the proper table while ensuring that the name that we're going
1054// to reference is in the string table. We do this since the names we
1055// add may not only be identical to the names in the DIE.
Eric Christophera5a79422013-12-09 23:32:48 +00001056void DwarfUnit::addAccelName(StringRef Name, const DIE *Die) {
Eric Christopherd0d5bba2014-02-12 22:47:09 +00001057 if (!DD->useDwarfAccelTables())
1058 return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001059 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001060 std::vector<const DIE *> &DIEs = AccelNames[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001061 DIEs.push_back(Die);
1062}
1063
Eric Christophera5a79422013-12-09 23:32:48 +00001064void DwarfUnit::addAccelObjC(StringRef Name, const DIE *Die) {
Eric Christopherd0d5bba2014-02-12 22:47:09 +00001065 if (!DD->useDwarfAccelTables())
1066 return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001067 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001068 std::vector<const DIE *> &DIEs = AccelObjC[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001069 DIEs.push_back(Die);
1070}
1071
Eric Christophera5a79422013-12-09 23:32:48 +00001072void DwarfUnit::addAccelNamespace(StringRef Name, const DIE *Die) {
Eric Christopherd0d5bba2014-02-12 22:47:09 +00001073 if (!DD->useDwarfAccelTables())
1074 return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001075 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001076 std::vector<const DIE *> &DIEs = AccelNamespace[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001077 DIEs.push_back(Die);
1078}
1079
Eric Christophera5a79422013-12-09 23:32:48 +00001080void DwarfUnit::addAccelType(StringRef Name,
1081 std::pair<const DIE *, unsigned> Die) {
Eric Christopherd0d5bba2014-02-12 22:47:09 +00001082 if (!DD->useDwarfAccelTables())
1083 return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001084 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001085 std::vector<std::pair<const DIE *, unsigned> > &DIEs = AccelTypes[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001086 DIEs.push_back(Die);
1087}
1088
Eric Christopher9c58f312013-09-20 22:20:55 +00001089/// addGlobalName - Add a new global name to the compile unit.
Eric Christophera5a79422013-12-09 23:32:48 +00001090void DwarfUnit::addGlobalName(StringRef Name, DIE *Die, DIScope Context) {
David Blaikieadbea1e2014-03-12 03:34:38 +00001091 if (getCUNode().getEmissionKind() == DIBuilder::LineTablesOnly)
1092 return;
Eric Christopher8dba0d52013-10-19 01:04:42 +00001093 std::string FullName = getParentContextString(Context) + Name.str();
Eric Christopher2c8b7902013-10-17 02:06:06 +00001094 GlobalNames[FullName] = Die;
Eric Christopher9c58f312013-09-20 22:20:55 +00001095}
1096
Eric Christopher2c8b7902013-10-17 02:06:06 +00001097/// getParentContextString - Walks the metadata parent chain in a language
1098/// specific manner (using the compile unit language) and returns
1099/// it as a string. This is done at the metadata level because DIEs may
1100/// not currently have been added to the parent context and walking the
1101/// DIEs looking for names is more expensive than walking the metadata.
Eric Christophera5a79422013-12-09 23:32:48 +00001102std::string DwarfUnit::getParentContextString(DIScope Context) const {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001103 if (!Context)
1104 return "";
1105
1106 // FIXME: Decide whether to implement this for non-C++ languages.
1107 if (getLanguage() != dwarf::DW_LANG_C_plus_plus)
1108 return "";
1109
Eric Christopher8dba0d52013-10-19 01:04:42 +00001110 std::string CS;
Eric Christopher2c8b7902013-10-17 02:06:06 +00001111 SmallVector<DIScope, 1> Parents;
1112 while (!Context.isCompileUnit()) {
1113 Parents.push_back(Context);
1114 if (Context.getContext())
1115 Context = resolve(Context.getContext());
1116 else
1117 // Structure, etc types will have a NULL context if they're at the top
1118 // level.
1119 break;
1120 }
1121
1122 // Reverse iterate over our list to go from the outermost construct to the
1123 // innermost.
1124 for (SmallVectorImpl<DIScope>::reverse_iterator I = Parents.rbegin(),
1125 E = Parents.rend();
1126 I != E; ++I) {
1127 DIScope Ctx = *I;
1128 StringRef Name = Ctx.getName();
Eric Christopher8dba0d52013-10-19 01:04:42 +00001129 if (!Name.empty()) {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001130 CS += Name;
1131 CS += "::";
1132 }
1133 }
1134 return CS;
Devang Patel0e821f42011-04-12 23:21:44 +00001135}
1136
1137/// constructTypeDIE - Construct basic type die from DIBasicType.
Eric Christophera5a79422013-12-09 23:32:48 +00001138void DwarfUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Devang Patel0e821f42011-04-12 23:21:44 +00001139 // Get core information.
1140 StringRef Name = BTy.getName();
Devang Patel0e821f42011-04-12 23:21:44 +00001141 // Add name if not anonymous or intermediate type.
1142 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001143 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel04d6d472011-09-14 23:13:28 +00001144
David Blaikiefac56122013-10-04 23:21:16 +00001145 // An unspecified type only has a name attribute.
1146 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type)
Devang Patel04d6d472011-09-14 23:13:28 +00001147 return;
Devang Patel04d6d472011-09-14 23:13:28 +00001148
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001149 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Pateld925d1a2012-02-07 23:33:58 +00001150 BTy.getEncoding());
Devang Patel04d6d472011-09-14 23:13:28 +00001151
Devang Patel0e821f42011-04-12 23:21:44 +00001152 uint64_t Size = BTy.getSizeInBits() >> 3;
David Blaikief2443192013-10-21 17:28:37 +00001153 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001154}
1155
1156/// constructTypeDIE - Construct derived type die from DIDerivedType.
Eric Christophera5a79422013-12-09 23:32:48 +00001157void DwarfUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Devang Patel0e821f42011-04-12 23:21:44 +00001158 // Get core information.
1159 StringRef Name = DTy.getName();
1160 uint64_t Size = DTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001161 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001162
1163 // Map to main type, void will not have a type.
Manman Ren93b30902013-10-08 18:42:58 +00001164 DIType FromTy = resolve(DTy.getTypeDerivedFrom());
Eric Christopher0df08e22013-08-08 07:40:37 +00001165 if (FromTy)
1166 addType(&Buffer, FromTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001167
1168 // Add name if not anonymous or intermediate type.
1169 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001170 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001171
1172 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher85757902012-02-21 22:25:53 +00001173 if (Size && Tag != dwarf::DW_TAG_pointer_type)
David Blaikief2443192013-10-21 17:28:37 +00001174 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001175
David Blaikie5d3249b2013-01-07 05:51:15 +00001176 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
Eric Christopherc2697f82013-10-19 01:04:47 +00001177 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
1178 getOrCreateTypeDIE(resolve(DTy.getClassType())));
Devang Patel0e821f42011-04-12 23:21:44 +00001179 // Add source line info if available and TyDesc is not a forward declaration.
1180 if (!DTy.isForwardDecl())
1181 addSourceLine(&Buffer, DTy);
1182}
1183
Adrian Prantl3f49c892014-02-25 19:57:42 +00001184/// constructSubprogramArguments - Construct function argument DIEs.
1185void DwarfUnit::constructSubprogramArguments(DIE &Buffer, DIArray Args) {
Adrian Prantl69140d22014-02-25 22:27:14 +00001186 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1187 DIDescriptor Ty = Args.getElement(i);
1188 if (Ty.isUnspecifiedParameter()) {
1189 assert(i == N-1 && "Unspecified parameter must be the last argument");
1190 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
1191 } else {
1192 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
1193 addType(Arg, DIType(Ty));
1194 if (DIType(Ty).isArtificial())
1195 addFlag(Arg, dwarf::DW_AT_artificial);
Adrian Prantl3f49c892014-02-25 19:57:42 +00001196 }
Adrian Prantl69140d22014-02-25 22:27:14 +00001197 }
Adrian Prantl3f49c892014-02-25 19:57:42 +00001198}
1199
Devang Patel0e821f42011-04-12 23:21:44 +00001200/// constructTypeDIE - Construct type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001201void DwarfUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
David Blaikie409dd9c2013-11-19 23:08:21 +00001202 // Add name if not anonymous or intermediate type.
Devang Patel0e821f42011-04-12 23:21:44 +00001203 StringRef Name = CTy.getName();
1204
1205 uint64_t Size = CTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001206 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001207
1208 switch (Tag) {
Devang Patel0e821f42011-04-12 23:21:44 +00001209 case dwarf::DW_TAG_array_type:
Eric Christopherdf9955d2013-11-11 18:52:31 +00001210 constructArrayTypeDIE(Buffer, CTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001211 break;
Eric Christopheraeb105f2013-11-11 18:52:39 +00001212 case dwarf::DW_TAG_enumeration_type:
1213 constructEnumTypeDIE(Buffer, CTy);
1214 break;
Devang Patel0e821f42011-04-12 23:21:44 +00001215 case dwarf::DW_TAG_subroutine_type: {
Eric Christopher0df08e22013-08-08 07:40:37 +00001216 // Add return type. A void return won't have a type.
Devang Patel0e821f42011-04-12 23:21:44 +00001217 DIArray Elements = CTy.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001218 DIType RTy(Elements.getElement(0));
Eric Christopher0df08e22013-08-08 07:40:37 +00001219 if (RTy)
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001220 addType(&Buffer, RTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001221
1222 bool isPrototyped = true;
Adrian Prantl3f49c892014-02-25 19:57:42 +00001223 if (Elements.getNumElements() == 2 &&
1224 Elements.getElement(1).isUnspecifiedParameter())
1225 isPrototyped = false;
1226
1227 constructSubprogramArguments(Buffer, Elements);
1228
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001229 // Add prototype flag if we're dealing with a C language and the
1230 // function has been prototyped.
David Blaikiecb8e4352013-11-15 23:50:53 +00001231 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001232 if (isPrototyped &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001233 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopherd42b92f2012-05-22 18:45:24 +00001234 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001235 addFlag(&Buffer, dwarf::DW_AT_prototyped);
Adrian Prantl99c7af22013-12-18 21:48:19 +00001236
1237 if (CTy.isLValueReference())
1238 addFlag(&Buffer, dwarf::DW_AT_reference);
1239
1240 if (CTy.isRValueReference())
1241 addFlag(&Buffer, dwarf::DW_AT_rvalue_reference);
Eric Christopherc2697f82013-10-19 01:04:47 +00001242 } break;
Devang Patel0e821f42011-04-12 23:21:44 +00001243 case dwarf::DW_TAG_structure_type:
1244 case dwarf::DW_TAG_union_type:
1245 case dwarf::DW_TAG_class_type: {
Devang Patel0e821f42011-04-12 23:21:44 +00001246 // Add elements to structure type.
David Blaikiea1ae0e62013-08-01 20:30:22 +00001247 DIArray Elements = CTy.getTypeArray();
1248 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel0e821f42011-04-12 23:21:44 +00001249 DIDescriptor Element = Elements.getElement(i);
1250 DIE *ElemDie = NULL;
Adrian Prantlef129fb2014-01-18 02:12:00 +00001251 if (Element.isSubprogram())
1252 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
1253 else if (Element.isDerivedType()) {
Eric Christopherd42b92f2012-05-22 18:45:24 +00001254 DIDerivedType DDTy(Element);
1255 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
Manman Renb987e512013-10-29 00:53:03 +00001256 ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
Manman Ren93b30902013-10-08 18:42:58 +00001257 addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()),
Manman Renb3388602013-10-05 01:43:03 +00001258 dwarf::DW_AT_friend);
Manman Renc6b63922013-10-14 20:33:57 +00001259 } else if (DDTy.isStaticMember()) {
Manman Ren57e6ff72013-10-23 22:57:12 +00001260 getOrCreateStaticMemberDIE(DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001261 } else {
Manman Ren230ec862013-10-23 23:00:44 +00001262 constructMemberDIE(Buffer, DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001263 }
Eric Christopher7285c7d2012-03-28 07:34:31 +00001264 } else if (Element.isObjCProperty()) {
Devang Pateld925d1a2012-02-07 23:33:58 +00001265 DIObjCProperty Property(Element);
Manman Renb987e512013-10-29 00:53:03 +00001266 ElemDie = createAndAddDIE(Property.getTag(), Buffer);
Devang Pateld925d1a2012-02-07 23:33:58 +00001267 StringRef PropertyName = Property.getObjCPropertyName();
1268 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Eric Christopher4c960562014-01-23 19:16:28 +00001269 if (Property.getType())
1270 addType(ElemDie, Property.getType());
Eric Christopherd42b92f2012-05-22 18:45:24 +00001271 addSourceLine(ElemDie, Property);
Devang Pateld925d1a2012-02-07 23:33:58 +00001272 StringRef GetterName = Property.getObjCPropertyGetterName();
1273 if (!GetterName.empty())
1274 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
1275 StringRef SetterName = Property.getObjCPropertySetterName();
1276 if (!SetterName.empty())
1277 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
1278 unsigned PropertyAttributes = 0;
Devang Patel403e8192012-02-04 01:30:32 +00001279 if (Property.isReadOnlyObjCProperty())
1280 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1281 if (Property.isReadWriteObjCProperty())
1282 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1283 if (Property.isAssignObjCProperty())
1284 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1285 if (Property.isRetainObjCProperty())
1286 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1287 if (Property.isCopyObjCProperty())
1288 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1289 if (Property.isNonAtomicObjCProperty())
1290 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1291 if (PropertyAttributes)
David Blaikief2443192013-10-21 17:28:37 +00001292 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None,
Eric Christopherc2697f82013-10-19 01:04:47 +00001293 PropertyAttributes);
Devang Patel44882172012-02-06 17:49:43 +00001294
Devang Pateld925d1a2012-02-07 23:33:58 +00001295 DIEEntry *Entry = getDIEEntry(Element);
1296 if (!Entry) {
1297 Entry = createDIEEntry(ElemDie);
1298 insertDIEEntry(Element, Entry);
1299 }
Devang Patel403e8192012-02-04 01:30:32 +00001300 } else
Devang Patel0e821f42011-04-12 23:21:44 +00001301 continue;
Devang Patel0e821f42011-04-12 23:21:44 +00001302 }
1303
1304 if (CTy.isAppleBlockExtension())
Eric Christopherbb69a272012-08-24 01:14:27 +00001305 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel0e821f42011-04-12 23:21:44 +00001306
Eric Christopher9e429ae2013-10-05 00:27:02 +00001307 DICompositeType ContainingType(resolve(CTy.getContainingType()));
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001308 if (ContainingType)
Manman Ren4c4b69c2013-10-11 23:58:05 +00001309 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001310 getOrCreateTypeDIE(ContainingType));
Devang Patel0e821f42011-04-12 23:21:44 +00001311
Devang Patel12419ae2011-05-12 21:29:42 +00001312 if (CTy.isObjcClassComplete())
Eric Christopherbb69a272012-08-24 01:14:27 +00001313 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patel2409e782011-05-12 19:06:16 +00001314
Eric Christopherda011dd2011-12-16 23:42:42 +00001315 // Add template parameters to a class, structure or union types.
1316 // FIXME: The support isn't in the metadata for this yet.
1317 if (Tag == dwarf::DW_TAG_class_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001318 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
Devang Patel0e821f42011-04-12 23:21:44 +00001319 addTemplateParams(Buffer, CTy.getTemplateParams());
1320
1321 break;
1322 }
1323 default:
1324 break;
1325 }
1326
1327 // Add name if not anonymous or intermediate type.
1328 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001329 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001330
Eric Christopher775cbd22012-05-22 18:45:18 +00001331 if (Tag == dwarf::DW_TAG_enumeration_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001332 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
Eric Christopher775cbd22012-05-22 18:45:18 +00001333 Tag == dwarf::DW_TAG_union_type) {
Devang Patel0e821f42011-04-12 23:21:44 +00001334 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher1cf33382012-06-01 00:22:32 +00001335 // TODO: Do we care about size for enum forward declarations?
Devang Patel0e821f42011-04-12 23:21:44 +00001336 if (Size)
David Blaikief2443192013-10-21 17:28:37 +00001337 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Eric Christopher1cf33382012-06-01 00:22:32 +00001338 else if (!CTy.isForwardDecl())
Devang Patel0e821f42011-04-12 23:21:44 +00001339 // Add zero size if it is not a forward declaration.
David Blaikief2443192013-10-21 17:28:37 +00001340 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, 0);
Eric Christopher1cf33382012-06-01 00:22:32 +00001341
1342 // If we're a forward decl, say so.
1343 if (CTy.isForwardDecl())
Eric Christopherbb69a272012-08-24 01:14:27 +00001344 addFlag(&Buffer, dwarf::DW_AT_declaration);
Devang Patel0e821f42011-04-12 23:21:44 +00001345
1346 // Add source line info if available.
1347 if (!CTy.isForwardDecl())
1348 addSourceLine(&Buffer, CTy);
Eric Christopher54cf8ff2012-03-07 00:15:19 +00001349
1350 // No harm in adding the runtime language to the declaration.
1351 unsigned RLang = CTy.getRunTimeLang();
1352 if (RLang)
Eric Christopherc2697f82013-10-19 01:04:47 +00001353 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
1354 RLang);
Devang Patel0e821f42011-04-12 23:21:44 +00001355 }
1356}
1357
Manman Renffc9a712013-10-23 23:05:28 +00001358/// constructTemplateTypeParameterDIE - Construct new DIE for the given
1359/// DITemplateTypeParameter.
Eric Christophera5a79422013-12-09 23:32:48 +00001360void DwarfUnit::constructTemplateTypeParameterDIE(DIE &Buffer,
1361 DITemplateTypeParameter TP) {
Manman Renb987e512013-10-29 00:53:03 +00001362 DIE *ParamDIE =
1363 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
Eric Christopher056b6472013-08-08 08:09:43 +00001364 // Add the type if it exists, it could be void and therefore no type.
1365 if (TP.getType())
Manman Ren88b0f942013-10-09 19:46:28 +00001366 addType(ParamDIE, resolve(TP.getType()));
David Blaikie2b380232013-06-22 18:59:11 +00001367 if (!TP.getName().empty())
1368 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel0e821f42011-04-12 23:21:44 +00001369}
1370
Manman Renffc9a712013-10-23 23:05:28 +00001371/// constructTemplateValueParameterDIE - Construct new DIE for the given
1372/// DITemplateValueParameter.
Eric Christophera5a79422013-12-09 23:32:48 +00001373void
1374DwarfUnit::constructTemplateValueParameterDIE(DIE &Buffer,
David Blaikie319a05f2013-12-02 19:33:10 +00001375 DITemplateValueParameter VP) {
Manman Renb987e512013-10-29 00:53:03 +00001376 DIE *ParamDIE = createAndAddDIE(VP.getTag(), Buffer);
Eric Christopher0df08e22013-08-08 07:40:37 +00001377
1378 // Add the type if there is one, template template and template parameter
1379 // packs will not have a type.
Eric Christopher691281b2013-10-21 17:48:51 +00001380 if (VP.getTag() == dwarf::DW_TAG_template_value_parameter)
Manman Ren88b0f942013-10-09 19:46:28 +00001381 addType(ParamDIE, resolve(VP.getType()));
Eric Christopherafb2c412013-08-08 07:40:31 +00001382 if (!VP.getName().empty())
1383 addString(ParamDIE, dwarf::DW_AT_name, VP.getName());
1384 if (Value *Val = VP.getValue()) {
David Blaikiea1e813d2013-05-10 21:52:07 +00001385 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val))
Manman Ren88b0f942013-10-09 19:46:28 +00001386 addConstantValue(ParamDIE, CI,
1387 isUnsignedDIType(DD, resolve(VP.getType())));
David Blaikiea1e813d2013-05-10 21:52:07 +00001388 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) {
1389 // For declaration non-type template parameters (such as global values and
1390 // functions)
Eric Christopher4a741042014-02-16 08:46:55 +00001391 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
1392 addOpAddress(Loc, Asm->getSymbol(GV));
David Blaikiea1e813d2013-05-10 21:52:07 +00001393 // Emit DW_OP_stack_value to use the address as the immediate value of the
1394 // parameter, rather than a pointer to it.
Eric Christopher4a741042014-02-16 08:46:55 +00001395 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1396 addBlock(ParamDIE, dwarf::DW_AT_location, Loc);
Eric Christopherafb2c412013-08-08 07:40:31 +00001397 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) {
David Blaikie2b380232013-06-22 18:59:11 +00001398 assert(isa<MDString>(Val));
1399 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1400 cast<MDString>(Val)->getString());
Eric Christopherafb2c412013-08-08 07:40:31 +00001401 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
David Blaikie2b380232013-06-22 18:59:11 +00001402 assert(isa<MDNode>(Val));
1403 DIArray A(cast<MDNode>(Val));
1404 addTemplateParams(*ParamDIE, A);
David Blaikiea1e813d2013-05-10 21:52:07 +00001405 }
1406 }
Devang Patel0e821f42011-04-12 23:21:44 +00001407}
1408
Devang Patel17b53272011-05-06 16:57:54 +00001409/// getOrCreateNameSpace - Create a DIE for DINameSpace.
Eric Christophera5a79422013-12-09 23:32:48 +00001410DIE *DwarfUnit::getOrCreateNameSpace(DINameSpace NS) {
Manman Renf6b936b2013-10-29 05:49:41 +00001411 // Construct the context before querying for the existence of the DIE in case
1412 // such construction creates the DIE.
1413 DIE *ContextDIE = getOrCreateContextDIE(NS.getContext());
Manman Renf6b936b2013-10-29 05:49:41 +00001414
Devang Patel17b53272011-05-06 16:57:54 +00001415 DIE *NDie = getDIE(NS);
1416 if (NDie)
1417 return NDie;
Manman Renf6b936b2013-10-29 05:49:41 +00001418 NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
1419
Eric Christopher4996c702011-11-07 09:24:32 +00001420 if (!NS.getName().empty()) {
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001421 addString(NDie, dwarf::DW_AT_name, NS.getName());
Eric Christopher4996c702011-11-07 09:24:32 +00001422 addAccelNamespace(NS.getName(), NDie);
Eric Christopher2c8b7902013-10-17 02:06:06 +00001423 addGlobalName(NS.getName(), NDie, NS.getContext());
Eric Christopher4996c702011-11-07 09:24:32 +00001424 } else
1425 addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel17b53272011-05-06 16:57:54 +00001426 addSourceLine(NDie, NS);
Devang Patel17b53272011-05-06 16:57:54 +00001427 return NDie;
1428}
1429
Devang Patel89543712011-08-15 17:24:54 +00001430/// getOrCreateSubprogramDIE - Create new DIE using SP.
Eric Christophera5a79422013-12-09 23:32:48 +00001431DIE *DwarfUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
David Blaikie309ffe42013-10-04 01:39:59 +00001432 // Construct the context before querying for the existence of the DIE in case
1433 // such construction creates the DIE (as is the case for member function
1434 // declarations).
Adrian Prantld1e6a4e2014-03-14 23:08:25 +00001435 DIScope Context = resolve(SP.getContext());
1436 DIE *ContextDIE = getOrCreateContextDIE(Context);
Adrian Prantld486b342014-03-18 17:41:15 +00001437
Adrian Prantld1e6a4e2014-03-14 23:08:25 +00001438 // Unique declarations based on the ODR, where applicable.
Adrian Prantld486b342014-03-18 17:41:15 +00001439 SP = DISubprogram(DD->resolve(SP.getRef()));
1440 assert(SP.Verify());
David Blaikie309ffe42013-10-04 01:39:59 +00001441
Eric Christophere595bae2013-10-04 17:08:38 +00001442 DIE *SPDie = getDIE(SP);
Devang Patel89543712011-08-15 17:24:54 +00001443 if (SPDie)
1444 return SPDie;
1445
Manman Ren73d697c2013-10-29 00:58:04 +00001446 DISubprogram SPDecl = SP.getFunctionDeclaration();
1447 if (SPDecl.isSubprogram())
1448 // Add subprogram definitions to the CU die directly.
David Blaikie2a80e442013-12-02 22:09:48 +00001449 ContextDIE = UnitDie.get();
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001450
1451 // DW_TAG_inlined_subroutine may refer to this DIE.
Manman Ren73d697c2013-10-29 00:58:04 +00001452 SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001453
Rafael Espindola79278362011-11-10 22:34:29 +00001454 DIE *DeclDie = NULL;
Manman Renb9512a72013-10-23 22:12:26 +00001455 if (SPDecl.isSubprogram())
Rafael Espindola79278362011-11-10 22:34:29 +00001456 DeclDie = getOrCreateSubprogramDIE(SPDecl);
Rafael Espindola79278362011-11-10 22:34:29 +00001457
Devang Patel89543712011-08-15 17:24:54 +00001458 // Add function template parameters.
1459 addTemplateParams(*SPDie, SP.getTemplateParams());
1460
Devang Patel89543712011-08-15 17:24:54 +00001461 // If this DIE is going to refer declaration info using AT_specification
Eric Christopherf20ff972013-05-09 00:42:33 +00001462 // then there is no need to add other attributes.
Rafael Espindola79278362011-11-10 22:34:29 +00001463 if (DeclDie) {
1464 // Refer function declaration directly.
Manman Ren4c4b69c2013-10-11 23:58:05 +00001465 addDIEEntry(SPDie, dwarf::DW_AT_specification, DeclDie);
Rafael Espindola79278362011-11-10 22:34:29 +00001466
Devang Patel89543712011-08-15 17:24:54 +00001467 return SPDie;
Rafael Espindola79278362011-11-10 22:34:29 +00001468 }
Devang Patel89543712011-08-15 17:24:54 +00001469
Eric Christopheracb71152012-08-23 22:52:55 +00001470 // Add the linkage name if we have one.
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001471 StringRef LinkageName = SP.getLinkageName();
1472 if (!LinkageName.empty())
Eric Christopheracb71152012-08-23 22:52:55 +00001473 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001474 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopheracb71152012-08-23 22:52:55 +00001475
Devang Patel89543712011-08-15 17:24:54 +00001476 // Constructors and operators for anonymous aggregates do not have names.
1477 if (!SP.getName().empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001478 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Patel89543712011-08-15 17:24:54 +00001479
1480 addSourceLine(SPDie, SP);
1481
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001482 // Add the prototype if we have a prototype and we have a C like
1483 // language.
David Blaikiecb8e4352013-11-15 23:50:53 +00001484 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001485 if (SP.isPrototyped() &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001486 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001487 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001488 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Patel89543712011-08-15 17:24:54 +00001489
Devang Patel89543712011-08-15 17:24:54 +00001490 DICompositeType SPTy = SP.getType();
David Blaikie5174c842013-05-22 23:22:18 +00001491 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type &&
1492 "the type of a subprogram should be a subroutine");
Devang Patel89543712011-08-15 17:24:54 +00001493
David Blaikie5174c842013-05-22 23:22:18 +00001494 DIArray Args = SPTy.getTypeArray();
Eric Christopher691281b2013-10-21 17:48:51 +00001495 // Add a return type. If this is a type like a C/C++ void type we don't add a
1496 // return type.
Eric Christopher0df08e22013-08-08 07:40:37 +00001497 if (Args.getElement(0))
1498 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel89543712011-08-15 17:24:54 +00001499
1500 unsigned VK = SP.getVirtuality();
1501 if (VK) {
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001502 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Eric Christopher4a741042014-02-16 08:46:55 +00001503 DIELoc *Block = getDIELoc();
David Blaikief2443192013-10-21 17:28:37 +00001504 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1505 addUInt(Block, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1506 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
Eric Christopher98b7f172013-11-11 18:52:36 +00001507 ContainingTypeMap.insert(
1508 std::make_pair(SPDie, resolve(SP.getContainingType())));
Devang Patel89543712011-08-15 17:24:54 +00001509 }
1510
1511 if (!SP.isDefinition()) {
Eric Christopherbb69a272012-08-24 01:14:27 +00001512 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher92331fd2012-11-21 00:34:38 +00001513
Devang Patel89543712011-08-15 17:24:54 +00001514 // Add arguments. Do not add arguments for subprogram definition. They will
1515 // be handled while processing variables.
Adrian Prantl3f49c892014-02-25 19:57:42 +00001516 constructSubprogramArguments(*SPDie, Args);
Devang Patel89543712011-08-15 17:24:54 +00001517 }
1518
1519 if (SP.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001520 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Patel89543712011-08-15 17:24:54 +00001521
1522 if (!SP.isLocalToUnit())
Eric Christopherbb69a272012-08-24 01:14:27 +00001523 addFlag(SPDie, dwarf::DW_AT_external);
Devang Patel89543712011-08-15 17:24:54 +00001524
1525 if (SP.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +00001526 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Patel89543712011-08-15 17:24:54 +00001527
1528 if (unsigned isa = Asm->getISAEncoding()) {
1529 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1530 }
1531
Adrian Prantl99c7af22013-12-18 21:48:19 +00001532 if (SP.isLValueReference())
1533 addFlag(SPDie, dwarf::DW_AT_reference);
1534
1535 if (SP.isRValueReference())
1536 addFlag(SPDie, dwarf::DW_AT_rvalue_reference);
1537
Adrian Prantlef129fb2014-01-18 02:12:00 +00001538 if (SP.isProtected())
1539 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1540 dwarf::DW_ACCESS_protected);
1541 else if (SP.isPrivate())
1542 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1543 dwarf::DW_ACCESS_private);
1544 else
1545 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1546 dwarf::DW_ACCESS_public);
1547
1548 if (SP.isExplicit())
1549 addFlag(SPDie, dwarf::DW_AT_explicit);
1550
Devang Patel89543712011-08-15 17:24:54 +00001551 return SPDie;
1552}
1553
Devang Pateldfd6ec32011-08-15 17:57:41 +00001554// Return const expression if value is a GEP to access merged global
1555// constant. e.g.
1556// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1557static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1558 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1559 if (!CE || CE->getNumOperands() != 3 ||
1560 CE->getOpcode() != Instruction::GetElementPtr)
1561 return NULL;
1562
1563 // First operand points to a global struct.
1564 Value *Ptr = CE->getOperand(0);
1565 if (!isa<GlobalValue>(Ptr) ||
1566 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1567 return NULL;
1568
1569 // Second operand is zero.
1570 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1571 if (!CI || !CI->isZero())
1572 return NULL;
1573
1574 // Third operand is offset.
1575 if (!isa<ConstantInt>(CE->getOperand(2)))
1576 return NULL;
1577
1578 return CE;
1579}
1580
1581/// createGlobalVariableDIE - create global variable DIE.
Eric Christopher4287a492013-12-09 23:57:44 +00001582void DwarfCompileUnit::createGlobalVariableDIE(DIGlobalVariable GV) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001583 // Check for pre-existence.
David Blaikie2ad00162013-11-15 23:09:13 +00001584 if (getDIE(GV))
Devang Pateldfd6ec32011-08-15 17:57:41 +00001585 return;
1586
David Blaikie5e390e42014-02-04 01:23:52 +00001587 assert(GV.isGlobalVariable());
Devang Patel0ecbcbd2011-08-18 23:17:55 +00001588
Eric Christopher08f7c8f2013-10-04 23:49:26 +00001589 DIScope GVContext = GV.getContext();
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001590 DIType GTy = DD->resolve(GV.getType());
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001591
1592 // If this is a static data member definition, some attributes belong
1593 // to the declaration DIE.
1594 DIE *VariableDIE = NULL;
Manman Rene697d3c2013-02-01 23:54:37 +00001595 bool IsStaticMember = false;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001596 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1597 if (SDMDecl.Verify()) {
1598 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1599 // We need the declaration DIE that is in the static member's class.
Manman Renc6b63922013-10-14 20:33:57 +00001600 VariableDIE = getOrCreateStaticMemberDIE(SDMDecl);
Manman Rene697d3c2013-02-01 23:54:37 +00001601 IsStaticMember = true;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001602 }
1603
1604 // If this is not a static data member definition, create the variable
1605 // DIE and add the initial set of attributes to it.
1606 if (!VariableDIE) {
Manman Renf6b936b2013-10-29 05:49:41 +00001607 // Construct the context before querying for the existence of the DIE in
1608 // case such construction creates the DIE.
1609 DIE *ContextDIE = getOrCreateContextDIE(GVContext);
Manman Renf6b936b2013-10-29 05:49:41 +00001610
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001611 // Add to map.
David Blaikie52c50202013-11-16 00:29:01 +00001612 VariableDIE = createAndAddDIE(GV.getTag(), *ContextDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001613
1614 // Add name and type.
1615 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1616 addType(VariableDIE, GTy);
1617
1618 // Add scoping info.
Eric Christopherd2b497b2013-10-16 01:37:49 +00001619 if (!GV.isLocalToUnit())
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001620 addFlag(VariableDIE, dwarf::DW_AT_external);
1621
1622 // Add line number info.
1623 addSourceLine(VariableDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001624 }
1625
Devang Pateldfd6ec32011-08-15 17:57:41 +00001626 // Add location.
Eric Christopher4996c702011-11-07 09:24:32 +00001627 bool addToAccelTable = false;
Eric Christopher0a917b72011-11-11 03:16:32 +00001628 DIE *VariableSpecDIE = NULL;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001629 bool isGlobalVariable = GV.getGlobal() != NULL;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001630 if (isGlobalVariable) {
Eric Christopher4996c702011-11-07 09:24:32 +00001631 addToAccelTable = true;
Eric Christopher4a741042014-02-16 08:46:55 +00001632 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Rafael Espindola79858aa2013-10-29 17:07:16 +00001633 const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal());
David Blaikief2694972013-06-28 20:05:11 +00001634 if (GV.getGlobal()->isThreadLocal()) {
1635 // FIXME: Make this work with -gsplit-dwarf.
1636 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
1637 assert((PointerSize == 4 || PointerSize == 8) &&
1638 "Add support for other sizes if necessary");
1639 // Based on GCC's support for TLS:
David Blaikie8466ca82013-07-01 23:55:52 +00001640 if (!DD->useSplitDwarf()) {
1641 // 1) Start with a constNu of the appropriate pointer size
Eric Christopher4a741042014-02-16 08:46:55 +00001642 addUInt(Loc, dwarf::DW_FORM_data1,
David Blaikie8466ca82013-07-01 23:55:52 +00001643 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
Richard Mitton0aafb582013-10-07 18:39:18 +00001644 // 2) containing the (relocated) offset of the TLS variable
1645 // within the module's TLS block.
Eric Christopher4a741042014-02-16 08:46:55 +00001646 addExpr(Loc, dwarf::DW_FORM_udata,
David Blaikief1a6dea2014-02-15 19:34:03 +00001647 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym));
David Blaikie8466ca82013-07-01 23:55:52 +00001648 } else {
Eric Christopher4a741042014-02-16 08:46:55 +00001649 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
1650 addUInt(Loc, dwarf::DW_FORM_udata,
David Blaikief1a6dea2014-02-15 19:34:03 +00001651 DU->getAddrPoolIndex(Sym, /* TLS */ true));
David Blaikie8466ca82013-07-01 23:55:52 +00001652 }
Richard Mitton0aafb582013-10-07 18:39:18 +00001653 // 3) followed by a custom OP to make the debugger do a TLS lookup.
Eric Christopher4a741042014-02-16 08:46:55 +00001654 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001655 } else {
1656 DD->addArangeLabel(SymbolCU(this, Sym));
Eric Christopher4a741042014-02-16 08:46:55 +00001657 addOpAddress(Loc, Sym);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001658 }
Devang Pateldfd6ec32011-08-15 17:57:41 +00001659 // Do not create specification DIE if context is either compile unit
1660 // or a subprogram.
Devang Patel5e6b65c2011-09-21 23:41:11 +00001661 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Manman Ren3eb9dff2013-09-09 19:05:21 +00001662 !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001663 // Create specification DIE.
David Blaikie2a80e442013-12-02 22:09:48 +00001664 VariableSpecDIE = createAndAddDIE(dwarf::DW_TAG_variable, *UnitDie);
Manman Ren4c4b69c2013-10-11 23:58:05 +00001665 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification, VariableDIE);
Eric Christopher4a741042014-02-16 08:46:55 +00001666 addBlock(VariableSpecDIE, dwarf::DW_AT_location, Loc);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001667 // A static member's declaration is already flagged as such.
1668 if (!SDMDecl.Verify())
1669 addFlag(VariableDIE, dwarf::DW_AT_declaration);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001670 } else {
Eric Christopher4a741042014-02-16 08:46:55 +00001671 addBlock(VariableDIE, dwarf::DW_AT_location, Loc);
Eric Christopher4996c702011-11-07 09:24:32 +00001672 }
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001673 // Add the linkage name.
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001674 StringRef LinkageName = GV.getLinkageName();
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001675 if (!LinkageName.empty())
Eric Christopher3f79b8c2013-02-27 23:49:47 +00001676 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1677 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1678 // TAG_variable.
Eric Christopherc2697f82013-10-19 01:04:47 +00001679 addString(IsStaticMember && VariableSpecDIE ? VariableSpecDIE
1680 : VariableDIE,
Eric Christopheraf7eca22014-03-13 23:26:25 +00001681 DD->getDwarfVersion() >= 4 ? dwarf::DW_AT_linkage_name
1682 : dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001683 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopher92331fd2012-11-21 00:34:38 +00001684 } else if (const ConstantInt *CI =
Eric Christopherc2697f82013-10-19 01:04:47 +00001685 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
Adrian Prantl322f41d2013-04-04 22:56:49 +00001686 // AT_const_value was added when the static member was created. To avoid
Manman Rene697d3c2013-02-01 23:54:37 +00001687 // emitting AT_const_value multiple times, we only add AT_const_value when
1688 // it is not a static member.
1689 if (!IsStaticMember)
Manman Renb3388602013-10-05 01:43:03 +00001690 addConstantValue(VariableDIE, CI, isUnsignedDIType(DD, GTy));
David Blaikiea781b25b2013-11-17 21:55:13 +00001691 } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getOperand(11))) {
Eric Christopher4996c702011-11-07 09:24:32 +00001692 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001693 // GV is a merged global.
Eric Christopher4a741042014-02-16 08:46:55 +00001694 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Devang Pateldfd6ec32011-08-15 17:57:41 +00001695 Value *Ptr = CE->getOperand(0);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001696 MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr));
1697 DD->addArangeLabel(SymbolCU(this, Sym));
Eric Christopher4a741042014-02-16 08:46:55 +00001698 addOpAddress(Loc, Sym);
1699 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
Eric Christopherc2697f82013-10-19 01:04:47 +00001700 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
Eric Christopher4a741042014-02-16 08:46:55 +00001701 addUInt(Loc, dwarf::DW_FORM_udata,
Eric Christopherc2697f82013-10-19 01:04:47 +00001702 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
Eric Christopher4a741042014-02-16 08:46:55 +00001703 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1704 addBlock(VariableDIE, dwarf::DW_AT_location, Loc);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001705 }
1706
Eric Christopherc12c2112011-11-11 01:55:22 +00001707 if (addToAccelTable) {
1708 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1709 addAccelName(GV.getName(), AddrDIE);
Eric Christopher4996c702011-11-07 09:24:32 +00001710
Eric Christopherc12c2112011-11-11 01:55:22 +00001711 // If the linkage name is different than the name, go ahead and output
1712 // that as well into the name table.
1713 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1714 addAccelName(GV.getLinkageName(), AddrDIE);
1715 }
Eric Christopherd2b497b2013-10-16 01:37:49 +00001716
1717 if (!GV.isLocalToUnit())
Eric Christopher2c8b7902013-10-17 02:06:06 +00001718 addGlobalName(GV.getName(), VariableSpecDIE ? VariableSpecDIE : VariableDIE,
1719 GV.getContext());
Devang Pateldfd6ec32011-08-15 17:57:41 +00001720}
1721
Devang Patel0e821f42011-04-12 23:21:44 +00001722/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Eric Christophera5a79422013-12-09 23:32:48 +00001723void DwarfUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Manman Renb987e512013-10-29 00:53:03 +00001724 DIE *DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
Manman Ren4c4b69c2013-10-11 23:58:05 +00001725 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, IndexTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001726
Bill Wendling28fe9e72012-12-06 07:38:10 +00001727 // The LowerBound value defines the lower bounds which is typically zero for
1728 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1729 // Count == -1 then the array is unbounded and we do not emit
1730 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1731 // Count == 0, then the array has zero elements in which case we do not emit
1732 // an upper bound.
1733 int64_t LowerBound = SR.getLo();
Bill Wendling3495f9b2012-12-06 07:55:19 +00001734 int64_t DefaultLowerBound = getDefaultLowerBound();
Bill Wendlingd7767122012-12-04 21:34:03 +00001735 int64_t Count = SR.getCount();
Devang Patel0e821f42011-04-12 23:21:44 +00001736
Bill Wendling3495f9b2012-12-06 07:55:19 +00001737 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
David Blaikief2443192013-10-21 17:28:37 +00001738 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound);
Bill Wendling28fe9e72012-12-06 07:38:10 +00001739
1740 if (Count != -1 && Count != 0)
Bill Wendlingd7767122012-12-04 21:34:03 +00001741 // FIXME: An unbounded array should reference the expression that defines
1742 // the array.
Eric Christopher98b7f172013-11-11 18:52:36 +00001743 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None,
1744 LowerBound + Count - 1);
Devang Patel0e821f42011-04-12 23:21:44 +00001745}
1746
1747/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001748void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopherdf9955d2013-11-11 18:52:31 +00001749 if (CTy.isVector())
Eric Christopherbb69a272012-08-24 01:14:27 +00001750 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel0e821f42011-04-12 23:21:44 +00001751
Eric Christopher0df08e22013-08-08 07:40:37 +00001752 // Emit the element type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001753 addType(&Buffer, resolve(CTy.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001754
1755 // Get an anonymous type for index type.
Eric Christophercad9b532013-01-04 21:51:53 +00001756 // FIXME: This type should be passed down from the front end
1757 // as different languages may have different sizes for indexes.
Devang Patel0e821f42011-04-12 23:21:44 +00001758 DIE *IdxTy = getIndexTyDie();
1759 if (!IdxTy) {
1760 // Construct an anonymous type for index type.
David Blaikie2a80e442013-12-02 22:09:48 +00001761 IdxTy = createAndAddDIE(dwarf::DW_TAG_base_type, *UnitDie);
Eric Christophercad9b532013-01-04 21:51:53 +00001762 addString(IdxTy, dwarf::DW_AT_name, "int");
David Blaikief2443192013-10-21 17:28:37 +00001763 addUInt(IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int32_t));
Devang Patel0e821f42011-04-12 23:21:44 +00001764 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1765 dwarf::DW_ATE_signed);
Devang Patel0e821f42011-04-12 23:21:44 +00001766 setIndexTyDie(IdxTy);
1767 }
1768
1769 // Add subranges to array type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001770 DIArray Elements = CTy.getTypeArray();
Devang Patel0e821f42011-04-12 23:21:44 +00001771 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1772 DIDescriptor Element = Elements.getElement(i);
1773 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1774 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1775 }
1776}
1777
Eric Christopheraeb105f2013-11-11 18:52:39 +00001778/// constructEnumTypeDIE - Construct an enum type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001779void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopheraeb105f2013-11-11 18:52:39 +00001780 DIArray Elements = CTy.getTypeArray();
1781
1782 // Add enumerators to enumeration type.
1783 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001784 DIEnumerator Enum(Elements.getElement(i));
Eric Christopheraeb105f2013-11-11 18:52:39 +00001785 if (Enum.isEnumerator()) {
1786 DIE *Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001787 StringRef Name = Enum.getName();
Eric Christopheraeb105f2013-11-11 18:52:39 +00001788 addString(Enumerator, dwarf::DW_AT_name, Name);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001789 int64_t Value = Enum.getEnumValue();
Eric Christophera07e4f52013-11-19 09:28:34 +00001790 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
1791 Value);
Eric Christopheraeb105f2013-11-11 18:52:39 +00001792 }
1793 }
1794 DIType DTy = resolve(CTy.getTypeDerivedFrom());
1795 if (DTy) {
1796 addType(&Buffer, DTy);
1797 addFlag(&Buffer, dwarf::DW_AT_enum_class);
1798 }
Devang Patel0e821f42011-04-12 23:21:44 +00001799}
1800
Devang Patel89543712011-08-15 17:24:54 +00001801/// constructContainingTypeDIEs - Construct DIEs for types that contain
1802/// vtables.
Eric Christophera5a79422013-12-09 23:32:48 +00001803void DwarfUnit::constructContainingTypeDIEs() {
Devang Patel89543712011-08-15 17:24:54 +00001804 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Eric Christopherc2697f82013-10-19 01:04:47 +00001805 CE = ContainingTypeMap.end();
1806 CI != CE; ++CI) {
Devang Patel89543712011-08-15 17:24:54 +00001807 DIE *SPDie = CI->first;
David Blaikie2ad00162013-11-15 23:09:13 +00001808 DIDescriptor D(CI->second);
1809 if (!D)
Eric Christopherc2697f82013-10-19 01:04:47 +00001810 continue;
David Blaikie2ad00162013-11-15 23:09:13 +00001811 DIE *NDie = getDIE(D);
Eric Christopherc2697f82013-10-19 01:04:47 +00001812 if (!NDie)
1813 continue;
Manman Ren4c4b69c2013-10-11 23:58:05 +00001814 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, NDie);
Devang Patel89543712011-08-15 17:24:54 +00001815 }
1816}
1817
Devang Patel3acc70e2011-08-15 22:04:40 +00001818/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Eric Christophera5a79422013-12-09 23:32:48 +00001819DIE *DwarfUnit::constructVariableDIE(DbgVariable &DV, bool isScopeAbstract) {
Eric Christopher34a2c872013-11-15 01:43:19 +00001820 StringRef Name = DV.getName();
Devang Patel3acc70e2011-08-15 22:04:40 +00001821
Devang Patel3acc70e2011-08-15 22:04:40 +00001822 // Define variable debug information entry.
Eric Christopher34a2c872013-11-15 01:43:19 +00001823 DIE *VariableDie = new DIE(DV.getTag());
1824 DbgVariable *AbsVar = DV.getAbstractVariable();
Devang Patel3acc70e2011-08-15 22:04:40 +00001825 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
Manman Ren4213c392013-05-29 17:16:59 +00001826 if (AbsDIE)
Manman Ren4c4b69c2013-10-11 23:58:05 +00001827 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin, AbsDIE);
Devang Patel3acc70e2011-08-15 22:04:40 +00001828 else {
David Blaikie715528b2013-08-19 03:34:03 +00001829 if (!Name.empty())
1830 addString(VariableDie, dwarf::DW_AT_name, Name);
Eric Christopher34a2c872013-11-15 01:43:19 +00001831 addSourceLine(VariableDie, DV.getVariable());
1832 addType(VariableDie, DV.getType());
Devang Patel3acc70e2011-08-15 22:04:40 +00001833 }
1834
Eric Christopher34a2c872013-11-15 01:43:19 +00001835 if (DV.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001836 addFlag(VariableDie, dwarf::DW_AT_artificial);
Devang Patel3acc70e2011-08-15 22:04:40 +00001837
1838 if (isScopeAbstract) {
Eric Christopher34a2c872013-11-15 01:43:19 +00001839 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001840 return VariableDie;
1841 }
1842
1843 // Add variable address.
1844
Eric Christopher34a2c872013-11-15 01:43:19 +00001845 unsigned Offset = DV.getDotDebugLocOffset();
Devang Patel3acc70e2011-08-15 22:04:40 +00001846 if (Offset != ~0U) {
Eric Christophera27220f2014-03-05 22:41:20 +00001847 addLocationList(VariableDie, dwarf::DW_AT_location, Offset);
Eric Christopher34a2c872013-11-15 01:43:19 +00001848 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001849 return VariableDie;
1850 }
1851
Eric Christophercead0332011-10-03 15:49:20 +00001852 // Check if variable is described by a DBG_VALUE instruction.
Eric Christopher34a2c872013-11-15 01:43:19 +00001853 if (const MachineInstr *DVInsn = DV.getMInsn()) {
David Blaikie0252265b2013-06-16 20:34:15 +00001854 assert(DVInsn->getNumOperands() == 3);
1855 if (DVInsn->getOperand(0).isReg()) {
1856 const MachineOperand RegOp = DVInsn->getOperand(0);
Adrian Prantl19942882013-07-09 21:44:06 +00001857 // If the second operand is an immediate, this is an indirect value.
Adrian Prantl418d1d12013-07-09 20:28:37 +00001858 if (DVInsn->getOperand(1).isImm()) {
Eric Christopherc2697f82013-10-19 01:04:47 +00001859 MachineLocation Location(RegOp.getReg(),
1860 DVInsn->getOperand(1).getImm());
Eric Christopher34a2c872013-11-15 01:43:19 +00001861 addVariableAddress(DV, VariableDie, Location);
David Blaikie0252265b2013-06-16 20:34:15 +00001862 } else if (RegOp.getReg())
Eric Christopher34a2c872013-11-15 01:43:19 +00001863 addVariableAddress(DV, VariableDie, MachineLocation(RegOp.getReg()));
David Blaikie0252265b2013-06-16 20:34:15 +00001864 } else if (DVInsn->getOperand(0).isImm())
Eric Christopher34a2c872013-11-15 01:43:19 +00001865 addConstantValue(VariableDie, DVInsn->getOperand(0), DV.getType());
David Blaikie0252265b2013-06-16 20:34:15 +00001866 else if (DVInsn->getOperand(0).isFPImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001867 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
David Blaikie0252265b2013-06-16 20:34:15 +00001868 else if (DVInsn->getOperand(0).isCImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001869 addConstantValue(VariableDie, DVInsn->getOperand(0).getCImm(),
Eric Christopher34a2c872013-11-15 01:43:19 +00001870 isUnsignedDIType(DD, DV.getType()));
Eric Christopher78fcf4902013-07-03 01:08:30 +00001871
Eric Christopher34a2c872013-11-15 01:43:19 +00001872 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001873 return VariableDie;
1874 } else {
1875 // .. else use frame index.
Eric Christopher34a2c872013-11-15 01:43:19 +00001876 int FI = DV.getFrameIndex();
Devang Patel3acc70e2011-08-15 22:04:40 +00001877 if (FI != ~0) {
1878 unsigned FrameReg = 0;
1879 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopherc2697f82013-10-19 01:04:47 +00001880 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
Devang Patel3acc70e2011-08-15 22:04:40 +00001881 MachineLocation Location(FrameReg, Offset);
Eric Christopher34a2c872013-11-15 01:43:19 +00001882 addVariableAddress(DV, VariableDie, Location);
Devang Patel3acc70e2011-08-15 22:04:40 +00001883 }
1884 }
1885
Eric Christopher34a2c872013-11-15 01:43:19 +00001886 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001887 return VariableDie;
1888}
1889
Manman Ren230ec862013-10-23 23:00:44 +00001890/// constructMemberDIE - Construct member DIE from DIDerivedType.
Eric Christophera5a79422013-12-09 23:32:48 +00001891void DwarfUnit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) {
Manman Renb987e512013-10-29 00:53:03 +00001892 DIE *MemberDie = createAndAddDIE(DT.getTag(), Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001893 StringRef Name = DT.getName();
1894 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001895 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001896
Manman Ren93b30902013-10-08 18:42:58 +00001897 addType(MemberDie, resolve(DT.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001898
1899 addSourceLine(MemberDie, DT);
1900
Eric Christopherc2697f82013-10-19 01:04:47 +00001901 if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) {
Devang Patel0e821f42011-04-12 23:21:44 +00001902
1903 // For C++, virtual base classes are not at fixed offset. Use following
1904 // expression to extract appropriate offset from vtable.
1905 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1906
Eric Christopher4a741042014-02-16 08:46:55 +00001907 DIELoc *VBaseLocationDie = new (DIEValueAllocator) DIELoc();
David Blaikief2443192013-10-21 17:28:37 +00001908 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1909 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1910 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1911 addUInt(VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1912 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1913 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1914 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
Devang Patel0e821f42011-04-12 23:21:44 +00001915
David Blaikief2443192013-10-21 17:28:37 +00001916 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
David Blaikie71d34a22013-11-01 00:25:45 +00001917 } else {
1918 uint64_t Size = DT.getSizeInBits();
1919 uint64_t FieldSize = getBaseTypeSize(DD, DT);
1920 uint64_t OffsetInBytes;
1921
1922 if (Size != FieldSize) {
Eric Christopher1acdbb82014-03-12 17:14:46 +00001923 // Handle bitfield, assume bytes are 8 bits.
1924 addUInt(MemberDie, dwarf::DW_AT_byte_size, None, FieldSize/8);
1925 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, Size);
David Blaikie71d34a22013-11-01 00:25:45 +00001926
1927 uint64_t Offset = DT.getOffsetInBits();
1928 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1929 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1930 uint64_t FieldOffset = (HiMark - FieldSize);
1931 Offset -= FieldOffset;
1932
1933 // Maybe we need to work from the other end.
1934 if (Asm->getDataLayout().isLittleEndian())
1935 Offset = FieldSize - (Offset + Size);
1936 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
1937
Adrian Prantlc67655a2014-01-28 18:13:47 +00001938 // Here DW_AT_data_member_location points to the anonymous
David Blaikie71d34a22013-11-01 00:25:45 +00001939 // field that includes this bit field.
1940 OffsetInBytes = FieldOffset >> 3;
1941 } else
1942 // This is not a bitfield.
1943 OffsetInBytes = DT.getOffsetInBits() >> 3;
David Blaikie2ada1162014-01-03 00:48:38 +00001944
David Blaikie22b29a52014-01-03 01:30:05 +00001945 if (DD->getDwarfVersion() <= 2) {
Eric Christopher4a741042014-02-16 08:46:55 +00001946 DIELoc *MemLocationDie = new (DIEValueAllocator) DIELoc();
David Blaikie22b29a52014-01-03 01:30:05 +00001947 addUInt(MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1948 addUInt(MemLocationDie, dwarf::DW_FORM_udata, OffsetInBytes);
1949 addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie);
1950 } else
1951 addUInt(MemberDie, dwarf::DW_AT_data_member_location, None,
1952 OffsetInBytes);
David Blaikie71d34a22013-11-01 00:25:45 +00001953 }
Devang Patel0e821f42011-04-12 23:21:44 +00001954
1955 if (DT.isProtected())
Nick Lewyckycb918492011-12-13 05:09:11 +00001956 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001957 dwarf::DW_ACCESS_protected);
1958 else if (DT.isPrivate())
Nick Lewyckycb918492011-12-13 05:09:11 +00001959 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001960 dwarf::DW_ACCESS_private);
1961 // Otherwise C++ member and base classes are considered public.
Eric Christopher92331fd2012-11-21 00:34:38 +00001962 else
Nick Lewyckycb918492011-12-13 05:09:11 +00001963 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001964 dwarf::DW_ACCESS_public);
1965 if (DT.isVirtual())
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001966 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001967 dwarf::DW_VIRTUALITY_virtual);
Devang Patel514b4002011-04-16 00:11:51 +00001968
1969 // Objective-C properties.
Devang Patel44882172012-02-06 17:49:43 +00001970 if (MDNode *PNode = DT.getObjCProperty())
1971 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
Eric Christopher92331fd2012-11-21 00:34:38 +00001972 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
Devang Patel44882172012-02-06 17:49:43 +00001973 PropertyDie);
1974
David Blaikie37fefc32012-12-13 22:43:07 +00001975 if (DT.isArtificial())
1976 addFlag(MemberDie, dwarf::DW_AT_artificial);
Devang Patel0e821f42011-04-12 23:21:44 +00001977}
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001978
Manman Renc6b63922013-10-14 20:33:57 +00001979/// getOrCreateStaticMemberDIE - Create new DIE for C++ static member.
Eric Christophera5a79422013-12-09 23:32:48 +00001980DIE *DwarfUnit::getOrCreateStaticMemberDIE(DIDerivedType DT) {
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001981 if (!DT.Verify())
1982 return NULL;
1983
Manman Renc6b63922013-10-14 20:33:57 +00001984 // Construct the context before querying for the existence of the DIE in case
1985 // such construction creates the DIE.
1986 DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext()));
David Blaikiebd700e42013-11-14 21:24:34 +00001987 assert(dwarf::isType(ContextDIE->getTag()) &&
1988 "Static member should belong to a type.");
Manman Renc6b63922013-10-14 20:33:57 +00001989
1990 DIE *StaticMemberDIE = getDIE(DT);
1991 if (StaticMemberDIE)
1992 return StaticMemberDIE;
1993
Manman Renb987e512013-10-29 00:53:03 +00001994 StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT);
Manman Renc6b63922013-10-14 20:33:57 +00001995
Manman Ren93b30902013-10-08 18:42:58 +00001996 DIType Ty = resolve(DT.getTypeDerivedFrom());
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001997
1998 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1999 addType(StaticMemberDIE, Ty);
2000 addSourceLine(StaticMemberDIE, DT);
2001 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
2002 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
2003
2004 // FIXME: We could omit private if the parent is a class_type, and
2005 // public if the parent is something else.
2006 if (DT.isProtected())
2007 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
2008 dwarf::DW_ACCESS_protected);
2009 else if (DT.isPrivate())
2010 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
2011 dwarf::DW_ACCESS_private);
2012 else
2013 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
2014 dwarf::DW_ACCESS_public);
2015
2016 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
Manman Renb3388602013-10-05 01:43:03 +00002017 addConstantValue(StaticMemberDIE, CI, isUnsignedDIType(DD, Ty));
David Blaikiea39a76e2013-01-20 01:18:01 +00002018 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
2019 addConstantFPValue(StaticMemberDIE, CFP);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00002020
Eric Christopher4d23a4a2013-01-16 01:22:23 +00002021 return StaticMemberDIE;
2022}
David Blaikie6b288cf2013-10-30 20:42:41 +00002023
Eric Christophera5a79422013-12-09 23:32:48 +00002024void DwarfUnit::emitHeader(const MCSection *ASection,
David Blaikie3332d4c2013-12-11 21:14:02 +00002025 const MCSymbol *ASectionSym) const {
David Blaikie6b288cf2013-10-30 20:42:41 +00002026 Asm->OutStreamer.AddComment("DWARF version number");
2027 Asm->EmitInt16(DD->getDwarfVersion());
2028 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
David Blaikie6896e192013-12-04 23:39:02 +00002029 // We share one abbreviations table across all units so it's always at the
2030 // start of the section. Use a relocatable offset where needed to ensure
2031 // linking doesn't invalidate that offset.
David Blaikie91db9ab2013-12-04 18:12:28 +00002032 Asm->EmitSectionOffset(ASectionSym, ASectionSym);
David Blaikie6b288cf2013-10-30 20:42:41 +00002033 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2034 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
2035}
David Blaikiebc563272013-12-13 21:33:40 +00002036
David Blaikie2494fdb2014-02-14 22:41:51 +00002037void DwarfCompileUnit::initStmtList(MCSymbol *DwarfLineSectionSym) {
2038 // Define start line table label for each Compile Unit.
2039 MCSymbol *LineTableStartSym =
2040 Asm->GetTempSymbol("line_table_start", getUniqueID());
2041 Asm->OutStreamer.getContext().setMCLineTableSymbol(LineTableStartSym,
2042 getUniqueID());
2043
2044 // Use a single line table if we are generating assembly.
2045 bool UseTheFirstCU =
2046 Asm->OutStreamer.hasRawTextSupport() || (getUniqueID() == 0);
2047
David Blaikie60e63862014-02-14 23:58:13 +00002048 stmtListIndex = UnitDie->getValues().size();
2049
David Blaikie2494fdb2014-02-14 22:41:51 +00002050 // DW_AT_stmt_list is a offset of line number information for this
2051 // compile unit in debug_line section. For split dwarf this is
2052 // left in the skeleton CU and so not included.
2053 // The line table entries are not always emitted in assembly, so it
2054 // is not okay to use line_table_start here.
2055 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
2056 addSectionLabel(UnitDie.get(), dwarf::DW_AT_stmt_list,
2057 UseTheFirstCU ? DwarfLineSectionSym : LineTableStartSym);
2058 else if (UseTheFirstCU)
2059 addSectionOffset(UnitDie.get(), dwarf::DW_AT_stmt_list, 0);
2060 else
2061 addSectionDelta(UnitDie.get(), dwarf::DW_AT_stmt_list, LineTableStartSym,
2062 DwarfLineSectionSym);
2063}
2064
David Blaikie60e63862014-02-14 23:58:13 +00002065void DwarfCompileUnit::applyStmtList(DIE &D) {
2066 D.addValue(dwarf::DW_AT_stmt_list,
2067 UnitDie->getAbbrev().getData()[stmtListIndex].getForm(),
2068 UnitDie->getValues()[stmtListIndex]);
2069}
2070
David Blaikiebc563272013-12-13 21:33:40 +00002071void DwarfTypeUnit::emitHeader(const MCSection *ASection,
2072 const MCSymbol *ASectionSym) const {
2073 DwarfUnit::emitHeader(ASection, ASectionSym);
2074 Asm->OutStreamer.AddComment("Type Signature");
2075 Asm->OutStreamer.EmitIntValue(TypeSignature, sizeof(TypeSignature));
2076 Asm->OutStreamer.AddComment("Type DIE Offset");
David Blaikie15ed5eb2014-01-10 01:38:41 +00002077 // In a skeleton type unit there is no type DIE so emit a zero offset.
2078 Asm->OutStreamer.EmitIntValue(Ty ? Ty->getOffset() : 0,
2079 sizeof(Ty->getOffset()));
David Blaikiebc563272013-12-13 21:33:40 +00002080}
2081
2082void DwarfTypeUnit::initSection(const MCSection *Section) {
2083 assert(!this->Section);
2084 this->Section = Section;
2085 // Since each type unit is contained in its own COMDAT section, the begin
2086 // label and the section label are the same. Using the begin label emission in
2087 // DwarfDebug to emit the section label as well is slightly subtle/sneaky, but
2088 // the only other alternative of lazily constructing start-of-section labels
2089 // and storing a mapping in DwarfDebug (or AsmPrinter).
2090 this->SectionSym = this->LabelBegin =
2091 Asm->GetTempSymbol(Section->getLabelBeginName(), getUniqueID());
2092 this->LabelEnd =
2093 Asm->GetTempSymbol(Section->getLabelEndName(), getUniqueID());
2094 this->LabelRange = Asm->GetTempSymbol("gnu_ranges", getUniqueID());
2095}