blob: 2ac9f0194f4b08da4140eef0337feb59a3298c2a [file] [log] [blame]
David Blaikie319a05f2013-12-02 19:33:10 +00001//===-- llvm/CodeGen/DwarfUnit.cpp - Dwarf Type and Compile Units ---------===//
Devang Patel0e821f42011-04-12 23:21:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Eric Christopher160522c2012-08-14 05:13:29 +000010// This file contains support for constructing a dwarf compile unit.
Devang Patel0e821f42011-04-12 23:21:44 +000011//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "dwarfdebug"
15
David Blaikie2c86a722013-12-02 19:33:15 +000016#include "DwarfUnit.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "DwarfAccelTable.h"
Devang Patel0e821f42011-04-12 23:21:44 +000018#include "DwarfDebug.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/ADT/APFloat.h"
Bill Wendlingf799efd2012-06-29 08:32:07 +000020#include "llvm/DIBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Constants.h"
22#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/GlobalVariable.h"
24#include "llvm/IR/Instructions.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000025#include "llvm/IR/Mangler.h"
Eric Christopher84588622013-12-28 01:39:17 +000026#include "llvm/MC/MCAsmInfo.h"
Adrian Prantlcbcd5782014-02-11 22:22:15 +000027#include "llvm/MC/MCContext.h"
David Blaikie6b288cf2013-10-30 20:42:41 +000028#include "llvm/MC/MCSection.h"
29#include "llvm/MC/MCStreamer.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000030#include "llvm/Support/CommandLine.h"
Devang Patel0e821f42011-04-12 23:21:44 +000031#include "llvm/Target/TargetFrameLowering.h"
David Blaikief2694972013-06-28 20:05:11 +000032#include "llvm/Target/TargetLoweringObjectFile.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000033#include "llvm/Target/TargetMachine.h"
Devang Patel0e821f42011-04-12 23:21:44 +000034#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel0e821f42011-04-12 23:21:44 +000035
36using namespace llvm;
37
Eric Christopher4287a492013-12-09 23:57:44 +000038static cl::opt<bool>
39GenerateDwarfTypeUnits("generate-type-units", cl::Hidden,
40 cl::desc("Generate DWARF4 type units."),
41 cl::init(false));
David Blaikie409dd9c2013-11-19 23:08:21 +000042
David Blaikie2a80e442013-12-02 22:09:48 +000043/// Unit - Unit constructor.
David Blaikie7480ae62014-01-09 03:03:27 +000044DwarfUnit::DwarfUnit(unsigned UID, DIE *D, DICompileUnit Node, AsmPrinter *A,
45 DwarfDebug *DW, DwarfFile *DWU)
David Blaikief645f962014-01-09 03:23:41 +000046 : UniqueID(UID), CUNode(Node), UnitDie(D), DebugInfoOffset(0), Asm(A),
47 DD(DW), DU(DWU), IndexTyDie(0), Section(0), Skeleton(0) {
David Blaikie319a05f2013-12-02 19:33:10 +000048 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
49}
50
Eric Christopher4287a492013-12-09 23:57:44 +000051DwarfCompileUnit::DwarfCompileUnit(unsigned UID, DIE *D, DICompileUnit Node,
52 AsmPrinter *A, DwarfDebug *DW,
53 DwarfFile *DWU)
David Blaikie7480ae62014-01-09 03:03:27 +000054 : DwarfUnit(UID, D, Node, A, DW, DWU) {
David Blaikie5a152402013-11-15 23:52:02 +000055 insertDIE(Node, D);
Devang Patel0e821f42011-04-12 23:21:44 +000056}
57
David Blaikie15632ae2014-02-12 00:31:30 +000058DwarfTypeUnit::DwarfTypeUnit(unsigned UID, DIE *D, DwarfCompileUnit &CU,
Eric Christopher4287a492013-12-09 23:57:44 +000059 AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU)
Eric Christopher4a741042014-02-16 08:46:55 +000060 : DwarfUnit(UID, D, CU.getCUNode(), A, DW, DWU), CU(CU) {}
David Blaikie409dd9c2013-11-19 23:08:21 +000061
David Blaikie319a05f2013-12-02 19:33:10 +000062/// ~Unit - Destructor for compile unit.
Eric Christophera5a79422013-12-09 23:32:48 +000063DwarfUnit::~DwarfUnit() {
Devang Patel0e821f42011-04-12 23:21:44 +000064 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
65 DIEBlocks[j]->~DIEBlock();
Eric Christopher4a741042014-02-16 08:46:55 +000066 for (unsigned j = 0, M = DIELocs.size(); j < M; ++j)
67 DIELocs[j]->~DIELoc();
Devang Patel0e821f42011-04-12 23:21:44 +000068}
69
70/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
71/// information entry.
Eric Christophera5a79422013-12-09 23:32:48 +000072DIEEntry *DwarfUnit::createDIEEntry(DIE *Entry) {
Devang Patel0e821f42011-04-12 23:21:44 +000073 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
74 return Value;
75}
76
Bill Wendling3495f9b2012-12-06 07:55:19 +000077/// getDefaultLowerBound - Return the default lower bound for an array. If the
Bill Wendling28fe9e72012-12-06 07:38:10 +000078/// DWARF version doesn't handle the language, return -1.
Eric Christophera5a79422013-12-09 23:32:48 +000079int64_t DwarfUnit::getDefaultLowerBound() const {
David Blaikiecb8e4352013-11-15 23:50:53 +000080 switch (getLanguage()) {
Bill Wendling28fe9e72012-12-06 07:38:10 +000081 default:
82 break;
83
84 case dwarf::DW_LANG_C89:
85 case dwarf::DW_LANG_C99:
86 case dwarf::DW_LANG_C:
87 case dwarf::DW_LANG_C_plus_plus:
88 case dwarf::DW_LANG_ObjC:
89 case dwarf::DW_LANG_ObjC_plus_plus:
90 return 0;
91
92 case dwarf::DW_LANG_Fortran77:
93 case dwarf::DW_LANG_Fortran90:
94 case dwarf::DW_LANG_Fortran95:
95 return 1;
96
97 // The languages below have valid values only if the DWARF version >= 4.
98 case dwarf::DW_LANG_Java:
99 case dwarf::DW_LANG_Python:
100 case dwarf::DW_LANG_UPC:
101 case dwarf::DW_LANG_D:
102 if (dwarf::DWARF_VERSION >= 4)
103 return 0;
104 break;
105
106 case dwarf::DW_LANG_Ada83:
107 case dwarf::DW_LANG_Ada95:
108 case dwarf::DW_LANG_Cobol74:
109 case dwarf::DW_LANG_Cobol85:
110 case dwarf::DW_LANG_Modula2:
111 case dwarf::DW_LANG_Pascal83:
112 case dwarf::DW_LANG_PLI:
113 if (dwarf::DWARF_VERSION >= 4)
114 return 1;
115 break;
116 }
117
118 return -1;
119}
120
Manman Ren4dbdc902013-10-31 17:54:35 +0000121/// Check whether the DIE for this MDNode can be shared across CUs.
David Blaikie4201ddf2013-11-15 22:59:36 +0000122static bool isShareableAcrossCUs(DIDescriptor D) {
David Blaikiebcb418e2013-11-20 18:40:16 +0000123 // When the MDNode can be part of the type system, the DIE can be shared
124 // across CUs.
125 // Combining type units and cross-CU DIE sharing is lower value (since
126 // cross-CU DIE sharing is used in LTO and removes type redundancy at that
127 // level already) but may be implementable for some value in projects
128 // building multiple independent libraries with LTO and then linking those
129 // together.
David Blaikie409dd9c2013-11-19 23:08:21 +0000130 return (D.isType() ||
131 (D.isSubprogram() && !DISubprogram(D).isDefinition())) &&
Eric Christopher4287a492013-12-09 23:57:44 +0000132 !GenerateDwarfTypeUnits;
Manman Ren4dbdc902013-10-31 17:54:35 +0000133}
134
135/// getDIE - Returns the debug information entry map slot for the
136/// specified debug variable. We delegate the request to DwarfDebug
137/// when the DIE for this MDNode can be shared across CUs. The mappings
138/// will be kept in DwarfDebug for shareable DIEs.
Eric Christophera5a79422013-12-09 23:32:48 +0000139DIE *DwarfUnit::getDIE(DIDescriptor D) const {
David Blaikie2ad00162013-11-15 23:09:13 +0000140 if (isShareableAcrossCUs(D))
141 return DD->getDIE(D);
142 return MDNodeToDieMap.lookup(D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000143}
144
145/// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
146/// when the DIE for this MDNode can be shared across CUs. The mappings
147/// will be kept in DwarfDebug for shareable DIEs.
Eric Christophera5a79422013-12-09 23:32:48 +0000148void DwarfUnit::insertDIE(DIDescriptor Desc, DIE *D) {
David Blaikie2ad00162013-11-15 23:09:13 +0000149 if (isShareableAcrossCUs(Desc)) {
150 DD->insertDIE(Desc, D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000151 return;
152 }
David Blaikie2ad00162013-11-15 23:09:13 +0000153 MDNodeToDieMap.insert(std::make_pair(Desc, D));
Manman Ren4dbdc902013-10-31 17:54:35 +0000154}
155
Eric Christopherbb69a272012-08-24 01:14:27 +0000156/// addFlag - Add a flag that is true.
Eric Christophera5a79422013-12-09 23:32:48 +0000157void DwarfUnit::addFlag(DIE *Die, dwarf::Attribute Attribute) {
Michael Gottesmanc89466f2013-09-04 04:39:38 +0000158 if (DD->getDwarfVersion() >= 4)
Eric Christopherdccd3282013-10-04 22:40:05 +0000159 Die->addValue(Attribute, dwarf::DW_FORM_flag_present, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000160 else
Eric Christopherdccd3282013-10-04 22:40:05 +0000161 Die->addValue(Attribute, dwarf::DW_FORM_flag, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000162}
163
Devang Patel0e821f42011-04-12 23:21:44 +0000164/// addUInt - Add an unsigned integer attribute data and value.
165///
Eric Christophera5a79422013-12-09 23:32:48 +0000166void DwarfUnit::addUInt(DIE *Die, dwarf::Attribute Attribute,
167 Optional<dwarf::Form> Form, uint64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000168 if (!Form)
169 Form = DIEInteger::BestForm(false, Integer);
170 DIEValue *Value = Integer == 1 ? DIEIntegerOne : new (DIEValueAllocator)
171 DIEInteger(Integer);
David Blaikief2443192013-10-21 17:28:37 +0000172 Die->addValue(Attribute, *Form, Value);
173}
174
Eric Christopher4a741042014-02-16 08:46:55 +0000175void DwarfUnit::addUInt(DIE *Block, dwarf::Form Form, uint64_t Integer) {
David Blaikief2443192013-10-21 17:28:37 +0000176 addUInt(Block, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000177}
178
179/// addSInt - Add an signed integer attribute data and value.
180///
Eric Christophera5a79422013-12-09 23:32:48 +0000181void DwarfUnit::addSInt(DIE *Die, dwarf::Attribute Attribute,
182 Optional<dwarf::Form> Form, int64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000183 if (!Form)
184 Form = DIEInteger::BestForm(true, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000185 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
David Blaikief2443192013-10-21 17:28:37 +0000186 Die->addValue(Attribute, *Form, Value);
187}
188
Eric Christopher4a741042014-02-16 08:46:55 +0000189void DwarfUnit::addSInt(DIELoc *Die, Optional<dwarf::Form> Form,
Eric Christophera5a79422013-12-09 23:32:48 +0000190 int64_t Integer) {
David Blaikief2443192013-10-21 17:28:37 +0000191 addSInt(Die, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000192}
193
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000194/// addString - Add a string attribute data and value. We always emit a
195/// reference to the string pool instead of immediate strings so that DIEs have
Eric Christopherfba22602013-01-07 19:32:45 +0000196/// more predictable sizes. In the case of split dwarf we emit an index
197/// into another table which gets us the static offset into the string
198/// table.
Eric Christophera5a79422013-12-09 23:32:48 +0000199void DwarfUnit::addString(DIE *Die, dwarf::Attribute Attribute,
200 StringRef String) {
Eric Christopher05893f42013-12-30 18:32:31 +0000201
202 if (!DD->useSplitDwarf())
203 return addLocalString(Die, Attribute, String);
204
205 unsigned idx = DU->getStringPoolIndex(String);
206 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
Eric Christopher67646432013-07-26 17:02:41 +0000207 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
Eric Christopher05893f42013-12-30 18:32:31 +0000208 Die->addValue(Attribute, dwarf::DW_FORM_GNU_str_index, Str);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000209}
210
211/// addLocalString - Add a string attribute data and value. This is guaranteed
212/// to be in the local string pool instead of indirected.
Eric Christophera5a79422013-12-09 23:32:48 +0000213void DwarfUnit::addLocalString(DIE *Die, dwarf::Attribute Attribute,
214 StringRef String) {
Eric Christophere698f532012-12-20 21:58:36 +0000215 MCSymbol *Symb = DU->getStringPoolEntry(String);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000216 DIEValue *Value;
Eric Christopher84588622013-12-28 01:39:17 +0000217 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000218 Value = new (DIEValueAllocator) DIELabel(Symb);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000219 else {
Eric Christophere698f532012-12-20 21:58:36 +0000220 MCSymbol *StringPool = DU->getStringPoolSym();
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000221 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000222 }
Eric Christopher05893f42013-12-30 18:32:31 +0000223 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
224 Die->addValue(Attribute, dwarf::DW_FORM_strp, Str);
Devang Patel0e821f42011-04-12 23:21:44 +0000225}
226
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000227/// addExpr - Add a Dwarf expression attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000228///
Eric Christopher4a741042014-02-16 08:46:55 +0000229void DwarfUnit::addExpr(DIELoc *Die, dwarf::Form Form, const MCExpr *Expr) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000230 DIEValue *Value = new (DIEValueAllocator) DIEExpr(Expr);
David Blaikief2443192013-10-21 17:28:37 +0000231 Die->addValue((dwarf::Attribute)0, Form, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000232}
233
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000234/// addLabel - Add a Dwarf label attribute data and value.
235///
Eric Christophera5a79422013-12-09 23:32:48 +0000236void DwarfUnit::addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
237 const MCSymbol *Label) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000238 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
239 Die->addValue(Attribute, Form, Value);
David Blaikief3cd7c52013-06-28 20:05:04 +0000240}
241
Eric Christopher4a741042014-02-16 08:46:55 +0000242void DwarfUnit::addLabel(DIELoc *Die, dwarf::Form Form, const MCSymbol *Label) {
David Blaikief2443192013-10-21 17:28:37 +0000243 addLabel(Die, (dwarf::Attribute)0, Form, Label);
244}
245
Eric Christopher33ff6972013-11-21 23:46:41 +0000246/// addSectionLabel - Add a Dwarf section label attribute data and value.
247///
Eric Christophera5a79422013-12-09 23:32:48 +0000248void DwarfUnit::addSectionLabel(DIE *Die, dwarf::Attribute Attribute,
249 const MCSymbol *Label) {
Eric Christopher33ff6972013-11-21 23:46:41 +0000250 if (DD->getDwarfVersion() >= 4)
251 addLabel(Die, Attribute, dwarf::DW_FORM_sec_offset, Label);
252 else
253 addLabel(Die, Attribute, dwarf::DW_FORM_data4, Label);
254}
255
256/// addSectionOffset - Add an offset into a section attribute data and value.
257///
Eric Christophera5a79422013-12-09 23:32:48 +0000258void DwarfUnit::addSectionOffset(DIE *Die, dwarf::Attribute Attribute,
259 uint64_t Integer) {
Eric Christopher33ff6972013-11-21 23:46:41 +0000260 if (DD->getDwarfVersion() >= 4)
261 addUInt(Die, Attribute, dwarf::DW_FORM_sec_offset, Integer);
262 else
263 addUInt(Die, Attribute, dwarf::DW_FORM_data4, Integer);
264}
265
Eric Christopher962c9082013-01-15 23:56:56 +0000266/// addLabelAddress - Add a dwarf label attribute data and value using
267/// DW_FORM_addr or DW_FORM_GNU_addr_index.
268///
Eric Christopher4287a492013-12-09 23:57:44 +0000269void DwarfCompileUnit::addLabelAddress(DIE *Die, dwarf::Attribute Attribute,
270 MCSymbol *Label) {
Alexey Samsonov4436bf02013-10-03 08:54:43 +0000271 if (Label)
272 DD->addArangeLabel(SymbolCU(this, Label));
Richard Mitton21101b32013-09-19 23:21:01 +0000273
Eric Christophercf48ade2014-01-24 11:52:53 +0000274 if (!DD->useSplitDwarf()) {
275 if (Label) {
276 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
277 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
278 } else {
279 DIEValue *Value = new (DIEValueAllocator) DIEInteger(0);
280 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
281 }
Eric Christopher962c9082013-01-15 23:56:56 +0000282 } else {
Eric Christophercf48ade2014-01-24 11:52:53 +0000283 unsigned idx = DU->getAddrPoolIndex(Label);
284 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
285 Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
Eric Christopher962c9082013-01-15 23:56:56 +0000286 }
287}
288
Eric Christophere9ec2452013-01-18 22:11:33 +0000289/// addOpAddress - Add a dwarf op address data and value using the
290/// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
291///
Eric Christopher4a741042014-02-16 08:46:55 +0000292void DwarfUnit::addOpAddress(DIELoc *Die, const MCSymbol *Sym) {
Eric Christophere9ec2452013-01-18 22:11:33 +0000293 if (!DD->useSplitDwarf()) {
David Blaikief2443192013-10-21 17:28:37 +0000294 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
295 addLabel(Die, dwarf::DW_FORM_udata, Sym);
Eric Christophere9ec2452013-01-18 22:11:33 +0000296 } else {
David Blaikief2443192013-10-21 17:28:37 +0000297 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
298 addUInt(Die, dwarf::DW_FORM_GNU_addr_index, DU->getAddrPoolIndex(Sym));
Eric Christophere9ec2452013-01-18 22:11:33 +0000299 }
300}
301
Eric Christopher33ff6972013-11-21 23:46:41 +0000302/// addSectionDelta - Add a section label delta attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000303///
Eric Christophera5a79422013-12-09 23:32:48 +0000304void DwarfUnit::addSectionDelta(DIE *Die, dwarf::Attribute Attribute,
305 const MCSymbol *Hi, const MCSymbol *Lo) {
Devang Patel0e821f42011-04-12 23:21:44 +0000306 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Eric Christopher33ff6972013-11-21 23:46:41 +0000307 if (DD->getDwarfVersion() >= 4)
308 Die->addValue(Attribute, dwarf::DW_FORM_sec_offset, Value);
309 else
310 Die->addValue(Attribute, dwarf::DW_FORM_data4, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000311}
312
313/// addDIEEntry - Add a DIE attribute data and value.
314///
Eric Christophera5a79422013-12-09 23:32:48 +0000315void DwarfUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +0000316 addDIEEntry(Die, Attribute, createDIEEntry(Entry));
317}
318
David Blaikie47f615e2013-12-17 23:32:35 +0000319void DwarfUnit::addDIETypeSignature(DIE *Die, const DwarfTypeUnit &Type) {
320 Die->addValue(dwarf::DW_AT_signature, dwarf::DW_FORM_ref_sig8,
321 new (DIEValueAllocator) DIETypeSignature(Type));
322}
323
Eric Christophera5a79422013-12-09 23:32:48 +0000324void DwarfUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute,
325 DIEEntry *Entry) {
David Blaikie409dd9c2013-11-19 23:08:21 +0000326 const DIE *DieCU = Die->getUnitOrNull();
327 const DIE *EntryCU = Entry->getEntry()->getUnitOrNull();
Manman Ren4dbdc902013-10-31 17:54:35 +0000328 if (!DieCU)
329 // We assume that Die belongs to this CU, if it is not linked to any CU yet.
David Blaikie2a80e442013-12-02 22:09:48 +0000330 DieCU = getUnitDie();
Manman Ren4dbdc902013-10-31 17:54:35 +0000331 if (!EntryCU)
David Blaikie2a80e442013-12-02 22:09:48 +0000332 EntryCU = getUnitDie();
Manman Ren4dbdc902013-10-31 17:54:35 +0000333 Die->addValue(Attribute, EntryCU == DieCU ? dwarf::DW_FORM_ref4
334 : dwarf::DW_FORM_ref_addr,
335 Entry);
Devang Patel0e821f42011-04-12 23:21:44 +0000336}
337
Manman Renb987e512013-10-29 00:53:03 +0000338/// Create a DIE with the given Tag, add the DIE to its parent, and
339/// call insertDIE if MD is not null.
Eric Christophera5a79422013-12-09 23:32:48 +0000340DIE *DwarfUnit::createAndAddDIE(unsigned Tag, DIE &Parent, DIDescriptor N) {
Manman Renb987e512013-10-29 00:53:03 +0000341 DIE *Die = new DIE(Tag);
342 Parent.addChild(Die);
David Blaikie52c50202013-11-16 00:29:01 +0000343 if (N)
344 insertDIE(N, Die);
Manman Renb987e512013-10-29 00:53:03 +0000345 return Die;
346}
347
Devang Patel0e821f42011-04-12 23:21:44 +0000348/// addBlock - Add block data.
349///
Eric Christopher4a741042014-02-16 08:46:55 +0000350void DwarfUnit::addBlock(DIE *Die, dwarf::Attribute Attribute, DIELoc *Loc) {
Eric Christopher5d503b52014-02-20 02:40:45 +0000351 Loc->setSize(Loc->ComputeSize(Asm));
Eric Christopher4a741042014-02-16 08:46:55 +0000352 DIELocs.push_back(Loc); // Memoize so we can call the destructor later on.
353 Die->addValue(Attribute, Loc->BestForm(DD->getDwarfVersion()), Loc);
354}
355
Eric Christophera5a79422013-12-09 23:32:48 +0000356void DwarfUnit::addBlock(DIE *Die, dwarf::Attribute Attribute,
357 DIEBlock *Block) {
Eric Christopher5d503b52014-02-20 02:40:45 +0000358 Block->setSize(Block->ComputeSize(Asm));
Devang Patel0e821f42011-04-12 23:21:44 +0000359 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
360 Die->addValue(Attribute, Block->BestForm(), Block);
361}
362
363/// addSourceLine - Add location information to specified debug information
364/// entry.
David Blaikie101613e2014-02-12 00:11:25 +0000365void DwarfUnit::addSourceLine(DIE *Die, unsigned Line, StringRef File,
366 StringRef Directory) {
Devang Patel0e821f42011-04-12 23:21:44 +0000367 if (Line == 0)
368 return;
David Blaikie101613e2014-02-12 00:11:25 +0000369
David Blaikie5b858582014-02-12 00:40:47 +0000370 unsigned FileID =
371 DD->getOrCreateSourceID(File, Directory, getCU().getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000372 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000373 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
374 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000375}
376
377/// addSourceLine - Add location information to specified debug information
378/// entry.
David Blaikie101613e2014-02-12 00:11:25 +0000379void DwarfUnit::addSourceLine(DIE *Die, DIVariable V) {
380 assert(V.isVariable());
381
Eric Christopher89a575c2014-02-12 22:38:04 +0000382 addSourceLine(Die, V.getLineNumber(), V.getContext().getFilename(),
383 V.getContext().getDirectory());
David Blaikie101613e2014-02-12 00:11:25 +0000384}
385
386/// addSourceLine - Add location information to specified debug information
387/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000388void DwarfUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
David Blaikie52019302014-02-11 23:57:03 +0000389 assert(G.isGlobalVariable());
Devang Patel0e821f42011-04-12 23:21:44 +0000390
David Blaikie101613e2014-02-12 00:11:25 +0000391 addSourceLine(Die, G.getLineNumber(), G.getFilename(), G.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000392}
393
394/// addSourceLine - Add location information to specified debug information
395/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000396void DwarfUnit::addSourceLine(DIE *Die, DISubprogram SP) {
David Blaikie52019302014-02-11 23:57:03 +0000397 assert(SP.isSubprogram());
Eric Christopher7734ca22012-03-15 23:55:40 +0000398
David Blaikie101613e2014-02-12 00:11:25 +0000399 addSourceLine(Die, SP.getLineNumber(), SP.getFilename(), SP.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000400}
401
402/// addSourceLine - Add location information to specified debug information
403/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000404void DwarfUnit::addSourceLine(DIE *Die, DIType Ty) {
David Blaikie52019302014-02-11 23:57:03 +0000405 assert(Ty.isType());
Devang Patel0e821f42011-04-12 23:21:44 +0000406
David Blaikie101613e2014-02-12 00:11:25 +0000407 addSourceLine(Die, Ty.getLineNumber(), Ty.getFilename(), Ty.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000408}
409
410/// addSourceLine - Add location information to specified debug information
411/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000412void DwarfUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
David Blaikie52019302014-02-11 23:57:03 +0000413 assert(Ty.isObjCProperty());
Eric Christopher70e1bd82012-03-29 08:42:56 +0000414
Eric Christopher70e1bd82012-03-29 08:42:56 +0000415 DIFile File = Ty.getFile();
David Blaikie101613e2014-02-12 00:11:25 +0000416 addSourceLine(Die, Ty.getLineNumber(), File.getFilename(),
417 File.getDirectory());
Eric Christopher70e1bd82012-03-29 08:42:56 +0000418}
419
420/// addSourceLine - Add location information to specified debug information
421/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000422void DwarfUnit::addSourceLine(DIE *Die, DINameSpace NS) {
David Blaikie52019302014-02-11 23:57:03 +0000423 assert(NS.Verify());
Devang Patel0e821f42011-04-12 23:21:44 +0000424
David Blaikie101613e2014-02-12 00:11:25 +0000425 addSourceLine(Die, NS.getLineNumber(), NS.getFilename(), NS.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000426}
427
Eric Christopher92331fd2012-11-21 00:34:38 +0000428/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patel77dc5412011-04-27 22:45:24 +0000429/// DbgVariable based on provided MachineLocation.
Eric Christophera5a79422013-12-09 23:32:48 +0000430void DwarfUnit::addVariableAddress(const DbgVariable &DV, DIE *Die,
431 MachineLocation Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000432 if (DV.variableHasComplexAddress())
Devang Patel0e821f42011-04-12 23:21:44 +0000433 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000434 else if (DV.isBlockByrefVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000435 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
436 else
David Blaikieea2605d2013-06-20 00:25:24 +0000437 addAddress(Die, dwarf::DW_AT_location, Location,
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000438 DV.getVariable().isIndirect());
Devang Patel0e821f42011-04-12 23:21:44 +0000439}
440
Devang Patelba5fbf12011-04-26 19:06:18 +0000441/// addRegisterOp - Add register operand.
Eric Christopher4a741042014-02-16 08:46:55 +0000442void DwarfUnit::addRegisterOp(DIELoc *TheDie, unsigned Reg) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000443 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000444 int DWReg = RI->getDwarfRegNum(Reg, false);
445 bool isSubRegister = DWReg < 0;
446
447 unsigned Idx = 0;
448
449 // Go up the super-register chain until we hit a valid dwarf register number.
450 for (MCSuperRegIterator SR(Reg, RI); SR.isValid() && DWReg < 0; ++SR) {
451 DWReg = RI->getDwarfRegNum(*SR, false);
452 if (DWReg >= 0)
453 Idx = RI->getSubRegIndex(*SR, Reg);
454 }
455
456 if (DWReg < 0) {
Eric Christophera13839f2014-02-26 23:27:16 +0000457 DEBUG(dbgs() << "Invalid Dwarf register number.\n");
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000458 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_nop);
459 return;
460 }
461
462 // Emit register
Devang Patelba5fbf12011-04-26 19:06:18 +0000463 if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000464 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000465 else {
David Blaikief2443192013-10-21 17:28:37 +0000466 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
467 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000468 }
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000469
470 // Emit Mask
471 if (isSubRegister) {
472 unsigned Size = RI->getSubRegIdxSize(Idx);
473 unsigned Offset = RI->getSubRegIdxOffset(Idx);
474 if (Offset > 0) {
475 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bit_piece);
476 addUInt(TheDie, dwarf::DW_FORM_data1, Size);
477 addUInt(TheDie, dwarf::DW_FORM_data1, Offset);
478 } else {
Adrian Prantl7199fd52014-02-12 19:34:44 +0000479 unsigned ByteSize = Size / 8; // Assuming 8 bits per byte.
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000480 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_piece);
Adrian Prantl7199fd52014-02-12 19:34:44 +0000481 addUInt(TheDie, dwarf::DW_FORM_data1, ByteSize);
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000482 }
483 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000484}
485
486/// addRegisterOffset - Add register offset.
Eric Christopher4a741042014-02-16 08:46:55 +0000487void DwarfUnit::addRegisterOffset(DIELoc *TheDie, unsigned Reg,
Eric Christophera5a79422013-12-09 23:32:48 +0000488 int64_t Offset) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000489 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
490 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
491 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
492 if (Reg == TRI->getFrameRegister(*Asm->MF))
493 // If variable offset is based in frame register then use fbreg.
David Blaikief2443192013-10-21 17:28:37 +0000494 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000495 else if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000496 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000497 else {
David Blaikief2443192013-10-21 17:28:37 +0000498 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
499 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000500 }
David Blaikief2443192013-10-21 17:28:37 +0000501 addSInt(TheDie, dwarf::DW_FORM_sdata, Offset);
Devang Patelba5fbf12011-04-26 19:06:18 +0000502}
503
504/// addAddress - Add an address attribute to a die based on the location
505/// provided.
Eric Christophera5a79422013-12-09 23:32:48 +0000506void DwarfUnit::addAddress(DIE *Die, dwarf::Attribute Attribute,
507 const MachineLocation &Location, bool Indirect) {
Eric Christopher4a741042014-02-16 08:46:55 +0000508 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Devang Patelba5fbf12011-04-26 19:06:18 +0000509
David Blaikieea2605d2013-06-20 00:25:24 +0000510 if (Location.isReg() && !Indirect)
Eric Christopher4a741042014-02-16 08:46:55 +0000511 addRegisterOp(Loc, Location.getReg());
David Blaikieea2605d2013-06-20 00:25:24 +0000512 else {
Eric Christopher4a741042014-02-16 08:46:55 +0000513 addRegisterOffset(Loc, Location.getReg(), Location.getOffset());
David Blaikieea2605d2013-06-20 00:25:24 +0000514 if (Indirect && !Location.isReg()) {
Eric Christopher4a741042014-02-16 08:46:55 +0000515 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
David Blaikieea2605d2013-06-20 00:25:24 +0000516 }
517 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000518
519 // Now attach the location information to the DIE.
Eric Christopher4a741042014-02-16 08:46:55 +0000520 addBlock(Die, Attribute, Loc);
Devang Patelba5fbf12011-04-26 19:06:18 +0000521}
522
Devang Patel0e821f42011-04-12 23:21:44 +0000523/// addComplexAddress - Start with the address based on the location provided,
524/// and generate the DWARF information necessary to find the actual variable
Eric Christopher1c70b672013-12-05 00:13:15 +0000525/// given the extra address information encoded in the DbgVariable, starting
526/// from the starting location. Add the DWARF information to the die.
Devang Patel0e821f42011-04-12 23:21:44 +0000527///
Eric Christophera5a79422013-12-09 23:32:48 +0000528void DwarfUnit::addComplexAddress(const DbgVariable &DV, DIE *Die,
529 dwarf::Attribute Attribute,
530 const MachineLocation &Location) {
Eric Christopher4a741042014-02-16 08:46:55 +0000531 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000532 unsigned N = DV.getNumAddrElements();
Devang Patel3e021532011-04-28 02:22:40 +0000533 unsigned i = 0;
534 if (Location.isReg()) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000535 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Devang Patel3e021532011-04-28 02:22:40 +0000536 // If first address element is OpPlus then emit
537 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Eric Christopher4a741042014-02-16 08:46:55 +0000538 addRegisterOffset(Loc, Location.getReg(), DV.getAddrElement(1));
Devang Patel3e021532011-04-28 02:22:40 +0000539 i = 2;
540 } else
Eric Christopher4a741042014-02-16 08:46:55 +0000541 addRegisterOp(Loc, Location.getReg());
Eric Christopherc2697f82013-10-19 01:04:47 +0000542 } else
Eric Christopher4a741042014-02-16 08:46:55 +0000543 addRegisterOffset(Loc, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000544
Eric Christopherc2697f82013-10-19 01:04:47 +0000545 for (; i < N; ++i) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000546 uint64_t Element = DV.getAddrElement(i);
Devang Patel0e821f42011-04-12 23:21:44 +0000547 if (Element == DIBuilder::OpPlus) {
Eric Christopher4a741042014-02-16 08:46:55 +0000548 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
549 addUInt(Loc, dwarf::DW_FORM_udata, DV.getAddrElement(++i));
Devang Patel0e821f42011-04-12 23:21:44 +0000550 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher4d250522012-05-08 18:56:00 +0000551 if (!Location.isReg())
Eric Christopher4a741042014-02-16 08:46:55 +0000552 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Eric Christopherc2697f82013-10-19 01:04:47 +0000553 } else
554 llvm_unreachable("unknown DIBuilder Opcode");
Devang Patel0e821f42011-04-12 23:21:44 +0000555 }
556
557 // Now attach the location information to the DIE.
Eric Christopher4a741042014-02-16 08:46:55 +0000558 addBlock(Die, Attribute, Loc);
Devang Patel0e821f42011-04-12 23:21:44 +0000559}
560
561/* Byref variables, in Blocks, are declared by the programmer as "SomeType
562 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
563 gives the variable VarName either the struct, or a pointer to the struct, as
564 its type. This is necessary for various behind-the-scenes things the
565 compiler needs to do with by-reference variables in Blocks.
566
567 However, as far as the original *programmer* is concerned, the variable
568 should still have type 'SomeType', as originally declared.
569
570 The function getBlockByrefType dives into the __Block_byref_x_VarName
571 struct to find the original type of the variable, which is then assigned to
572 the variable's Debug Information Entry as its real type. So far, so good.
573 However now the debugger will expect the variable VarName to have the type
574 SomeType. So we need the location attribute for the variable to be an
575 expression that explains to the debugger how to navigate through the
576 pointers and struct to find the actual variable of type SomeType.
577
578 The following function does just that. We start by getting
579 the "normal" location for the variable. This will be the location
580 of either the struct __Block_byref_x_VarName or the pointer to the
581 struct __Block_byref_x_VarName.
582
583 The struct will look something like:
584
585 struct __Block_byref_x_VarName {
586 ... <various fields>
587 struct __Block_byref_x_VarName *forwarding;
588 ... <various other fields>
589 SomeType VarName;
590 ... <maybe more fields>
591 };
592
593 If we are given the struct directly (as our starting point) we
594 need to tell the debugger to:
595
596 1). Add the offset of the forwarding field.
597
598 2). Follow that pointer to get the real __Block_byref_x_VarName
599 struct to use (the real one may have been copied onto the heap).
600
601 3). Add the offset for the field VarName, to find the actual variable.
602
603 If we started with a pointer to the struct, then we need to
604 dereference that pointer first, before the other steps.
605 Translating this into DWARF ops, we will need to append the following
606 to the current location description for the variable:
607
608 DW_OP_deref -- optional, if we start with a pointer
609 DW_OP_plus_uconst <forward_fld_offset>
610 DW_OP_deref
611 DW_OP_plus_uconst <varName_fld_offset>
612
613 That is what this function does. */
614
615/// addBlockByrefAddress - Start with the address based on the location
616/// provided, and generate the DWARF information necessary to find the
617/// actual Block variable (navigating the Block struct) based on the
618/// starting location. Add the DWARF information to the die. For
619/// more information, read large comment just above here.
620///
Eric Christophera5a79422013-12-09 23:32:48 +0000621void DwarfUnit::addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
622 dwarf::Attribute Attribute,
623 const MachineLocation &Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000624 DIType Ty = DV.getType();
Devang Patel0e821f42011-04-12 23:21:44 +0000625 DIType TmpTy = Ty;
Eric Christopher31b05762013-08-08 01:41:00 +0000626 uint16_t Tag = Ty.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +0000627 bool isPointer = false;
628
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000629 StringRef varName = DV.getName();
Devang Patel0e821f42011-04-12 23:21:44 +0000630
631 if (Tag == dwarf::DW_TAG_pointer_type) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000632 DIDerivedType DTy(Ty);
Manman Ren93b30902013-10-08 18:42:58 +0000633 TmpTy = resolve(DTy.getTypeDerivedFrom());
Devang Patel0e821f42011-04-12 23:21:44 +0000634 isPointer = true;
635 }
636
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000637 DICompositeType blockStruct(TmpTy);
Devang Patel0e821f42011-04-12 23:21:44 +0000638
639 // Find the __forwarding field and the variable field in the __Block_byref
640 // struct.
641 DIArray Fields = blockStruct.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000642 DIDerivedType varField;
643 DIDerivedType forwardingField;
Devang Patel0e821f42011-04-12 23:21:44 +0000644
645 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000646 DIDerivedType DT(Fields.getElement(i));
Devang Patel0e821f42011-04-12 23:21:44 +0000647 StringRef fieldName = DT.getName();
648 if (fieldName == "__forwarding")
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000649 forwardingField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000650 else if (fieldName == varName)
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000651 varField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000652 }
653
654 // Get the offsets for the forwarding field and the variable field.
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000655 unsigned forwardingFieldOffset = forwardingField.getOffsetInBits() >> 3;
656 unsigned varFieldOffset = varField.getOffsetInBits() >> 2;
Devang Patel0e821f42011-04-12 23:21:44 +0000657
658 // Decode the original location, and use that as the start of the byref
659 // variable's location.
Eric Christopher4a741042014-02-16 08:46:55 +0000660 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Devang Patel0e821f42011-04-12 23:21:44 +0000661
Eric Christopheref9d7102012-07-04 02:02:18 +0000662 if (Location.isReg())
Eric Christopher4a741042014-02-16 08:46:55 +0000663 addRegisterOp(Loc, Location.getReg());
Eric Christopheref9d7102012-07-04 02:02:18 +0000664 else
Eric Christopher4a741042014-02-16 08:46:55 +0000665 addRegisterOffset(Loc, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000666
667 // If we started with a pointer to the __Block_byref... struct, then
668 // the first thing we need to do is dereference the pointer (DW_OP_deref).
669 if (isPointer)
Eric Christopher4a741042014-02-16 08:46:55 +0000670 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000671
672 // Next add the offset for the '__forwarding' field:
673 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
674 // adding the offset if it's 0.
675 if (forwardingFieldOffset > 0) {
Eric Christopher4a741042014-02-16 08:46:55 +0000676 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
677 addUInt(Loc, dwarf::DW_FORM_udata, forwardingFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000678 }
679
680 // Now dereference the __forwarding field to get to the real __Block_byref
681 // struct: DW_OP_deref.
Eric Christopher4a741042014-02-16 08:46:55 +0000682 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000683
684 // Now that we've got the real __Block_byref... struct, add the offset
685 // for the variable's field to get to the location of the actual variable:
686 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
687 if (varFieldOffset > 0) {
Eric Christopher4a741042014-02-16 08:46:55 +0000688 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
689 addUInt(Loc, dwarf::DW_FORM_udata, varFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000690 }
691
692 // Now attach the location information to the DIE.
Eric Christopher4a741042014-02-16 08:46:55 +0000693 addBlock(Die, Attribute, Loc);
Devang Patel0e821f42011-04-12 23:21:44 +0000694}
695
Devang Patelbcd50a12011-07-20 21:57:04 +0000696/// isTypeSigned - Return true if the type is signed.
Manman Renb3388602013-10-05 01:43:03 +0000697static bool isTypeSigned(DwarfDebug *DD, DIType Ty, int *SizeInBits) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000698 if (Ty.isDerivedType())
Manman Renb3388602013-10-05 01:43:03 +0000699 return isTypeSigned(DD, DD->resolve(DIDerivedType(Ty).getTypeDerivedFrom()),
700 SizeInBits);
Devang Patelbcd50a12011-07-20 21:57:04 +0000701 if (Ty.isBasicType())
Eric Christopherc2697f82013-10-19 01:04:47 +0000702 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed ||
703 DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000704 *SizeInBits = Ty.getSizeInBits();
705 return true;
706 }
707 return false;
708}
709
Manman Renb3388602013-10-05 01:43:03 +0000710/// Return true if type encoding is unsigned.
711static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) {
712 DIDerivedType DTy(Ty);
713 if (DTy.isDerivedType())
714 return isUnsignedDIType(DD, DD->resolve(DTy.getTypeDerivedFrom()));
715
716 DIBasicType BTy(Ty);
717 if (BTy.isBasicType()) {
718 unsigned Encoding = BTy.getEncoding();
719 if (Encoding == dwarf::DW_ATE_unsigned ||
720 Encoding == dwarf::DW_ATE_unsigned_char ||
721 Encoding == dwarf::DW_ATE_boolean)
722 return true;
723 }
724 return false;
725}
726
727/// If this type is derived from a base type then return base type size.
Manman Renbda410f2013-10-08 18:46:58 +0000728static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) {
Manman Renb3388602013-10-05 01:43:03 +0000729 unsigned Tag = Ty.getTag();
730
731 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
732 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
733 Tag != dwarf::DW_TAG_restrict_type)
734 return Ty.getSizeInBits();
735
736 DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom());
737
738 // If this type is not derived from any type then take conservative approach.
739 if (!BaseType.isValid())
740 return Ty.getSizeInBits();
741
742 // If this is a derived type, go ahead and get the base type, unless it's a
743 // reference then it's just the size of the field. Pointer types have no need
744 // of this since they're a different type of qualification on the type.
745 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
746 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
747 return Ty.getSizeInBits();
748
749 if (BaseType.isDerivedType())
Manman Renbda410f2013-10-08 18:46:58 +0000750 return getBaseTypeSize(DD, DIDerivedType(BaseType));
Manman Renb3388602013-10-05 01:43:03 +0000751
752 return BaseType.getSizeInBits();
753}
754
Devang Patel0e821f42011-04-12 23:21:44 +0000755/// addConstantValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000756void DwarfUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
757 DIType Ty) {
David Blaikiea1e813d2013-05-10 21:52:07 +0000758 // FIXME: This is a bit conservative/simple - it emits negative values at
759 // their maximum bit width which is a bit unfortunate (& doesn't prefer
760 // udata/sdata over dataN as suggested by the DWARF spec)
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000761 assert(MO.isImm() && "Invalid machine operand!");
Devang Patelbcd50a12011-07-20 21:57:04 +0000762 int SizeInBits = -1;
Manman Renb3388602013-10-05 01:43:03 +0000763 bool SignedConstant = isTypeSigned(DD, Ty, &SizeInBits);
David Blaikief2443192013-10-21 17:28:37 +0000764 dwarf::Form Form;
Devang Patelf1d04702011-05-27 18:15:52 +0000765
Eric Christopher9d1daa82013-08-27 23:49:04 +0000766 // If we're a signed constant definitely use sdata.
767 if (SignedConstant) {
768 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, MO.getImm());
769 return;
770 }
771
772 // Else use data for now unless it's larger than we can deal with.
773 switch (SizeInBits) {
774 case 8:
775 Form = dwarf::DW_FORM_data1;
776 break;
777 case 16:
778 Form = dwarf::DW_FORM_data2;
779 break;
780 case 32:
781 Form = dwarf::DW_FORM_data4;
782 break;
783 case 64:
784 Form = dwarf::DW_FORM_data8;
785 break;
786 default:
787 Form = dwarf::DW_FORM_udata;
788 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
789 return;
790 }
791 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
Devang Patel0e821f42011-04-12 23:21:44 +0000792}
793
794/// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000795void DwarfUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000796 assert(MO.isFPImm() && "Invalid machine operand!");
Devang Patel0e821f42011-04-12 23:21:44 +0000797 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
798 APFloat FPImm = MO.getFPImm()->getValueAPF();
799
800 // Get the raw data form of the floating point.
801 const APInt FltVal = FPImm.bitcastToAPInt();
Eric Christopherc2697f82013-10-19 01:04:47 +0000802 const char *FltPtr = (const char *)FltVal.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000803
804 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000805 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000806 int Incr = (LittleEndian ? 1 : -1);
807 int Start = (LittleEndian ? 0 : NumBytes - 1);
808 int Stop = (LittleEndian ? NumBytes : -1);
809
810 // Output the constant to DWARF one byte at a time.
811 for (; Start != Stop; Start += Incr)
Eric Christopher98b7f172013-11-11 18:52:36 +0000812 addUInt(Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]);
Devang Patel0e821f42011-04-12 23:21:44 +0000813
David Blaikief2443192013-10-21 17:28:37 +0000814 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000815}
816
David Blaikiea39a76e2013-01-20 01:18:01 +0000817/// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000818void DwarfUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000819 // Pass this down to addConstantValue as an unsigned bag of bits.
820 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
David Blaikiea39a76e2013-01-20 01:18:01 +0000821}
822
Devang Patel0e821f42011-04-12 23:21:44 +0000823/// addConstantValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000824void DwarfUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
825 bool Unsigned) {
Eric Christopher78fcf4902013-07-03 01:08:30 +0000826 addConstantValue(Die, CI->getValue(), Unsigned);
David Blaikiea39a76e2013-01-20 01:18:01 +0000827}
828
829// addConstantValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000830void DwarfUnit::addConstantValue(DIE *Die, const APInt &Val, bool Unsigned) {
David Blaikiea39a76e2013-01-20 01:18:01 +0000831 unsigned CIBitWidth = Val.getBitWidth();
Devang Patel8816bbc2011-05-28 00:39:18 +0000832 if (CIBitWidth <= 64) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000833 // If we're a signed constant definitely use sdata.
834 if (!Unsigned) {
835 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
836 Val.getSExtValue());
837 return;
Devang Patel8816bbc2011-05-28 00:39:18 +0000838 }
Eric Christopher9d1daa82013-08-27 23:49:04 +0000839
840 // Else use data for now unless it's larger than we can deal with.
David Blaikief2443192013-10-21 17:28:37 +0000841 dwarf::Form Form;
Eric Christopher9d1daa82013-08-27 23:49:04 +0000842 switch (CIBitWidth) {
843 case 8:
844 Form = dwarf::DW_FORM_data1;
845 break;
846 case 16:
847 Form = dwarf::DW_FORM_data2;
848 break;
849 case 32:
850 Form = dwarf::DW_FORM_data4;
851 break;
852 case 64:
853 Form = dwarf::DW_FORM_data8;
854 break;
855 default:
856 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
857 Val.getZExtValue());
858 return;
859 }
860 addUInt(Die, dwarf::DW_AT_const_value, Form, Val.getZExtValue());
Eric Christopher78fcf4902013-07-03 01:08:30 +0000861 return;
Devang Patel0e821f42011-04-12 23:21:44 +0000862 }
863
864 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
865
866 // Get the raw data form of the large APInt.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000867 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000868
869 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000870 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000871
872 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000873 for (int i = 0; i < NumBytes; i++) {
874 uint8_t c;
875 if (LittleEndian)
876 c = Ptr64[i / 8] >> (8 * (i & 7));
877 else
878 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
David Blaikief2443192013-10-21 17:28:37 +0000879 addUInt(Block, dwarf::DW_FORM_data1, c);
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000880 }
Devang Patel0e821f42011-04-12 23:21:44 +0000881
David Blaikief2443192013-10-21 17:28:37 +0000882 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000883}
884
Eric Christopher25e35092013-04-22 07:47:40 +0000885/// addTemplateParams - Add template parameters into buffer.
Eric Christophera5a79422013-12-09 23:32:48 +0000886void DwarfUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
Devang Patel0e821f42011-04-12 23:21:44 +0000887 // Add template parameters.
888 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
889 DIDescriptor Element = TParams.getElement(i);
890 if (Element.isTemplateTypeParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000891 constructTemplateTypeParameterDIE(Buffer,
892 DITemplateTypeParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000893 else if (Element.isTemplateValueParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000894 constructTemplateValueParameterDIE(Buffer,
895 DITemplateValueParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000896 }
Devang Patel0e821f42011-04-12 23:21:44 +0000897}
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000898
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000899/// getOrCreateContextDIE - Get context owner's DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000900DIE *DwarfUnit::getOrCreateContextDIE(DIScope Context) {
David Blaikiebd700e42013-11-14 21:24:34 +0000901 if (!Context || Context.isFile())
David Blaikie2a80e442013-12-02 22:09:48 +0000902 return getUnitDie();
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000903 if (Context.isType())
904 return getOrCreateTypeDIE(DIType(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000905 if (Context.isNameSpace())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000906 return getOrCreateNameSpace(DINameSpace(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000907 if (Context.isSubprogram())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000908 return getOrCreateSubprogramDIE(DISubprogram(Context));
Adrian Prantl7d828bb2013-11-15 21:05:09 +0000909 return getDIE(Context);
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000910}
911
Eric Christophera5a79422013-12-09 23:32:48 +0000912DIE *DwarfUnit::createTypeDIE(DICompositeType Ty) {
David Blaikie9d861be2013-11-26 00:15:27 +0000913 DIScope Context = resolve(Ty.getContext());
914 DIE *ContextDIE = getOrCreateContextDIE(Context);
David Blaikie409dd9c2013-11-19 23:08:21 +0000915
916 DIE *TyDIE = getDIE(Ty);
917 if (TyDIE)
918 return TyDIE;
919
920 // Create new type.
921 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
922
David Blaikiefbd29eb2013-11-26 00:35:04 +0000923 constructTypeDIE(*TyDIE, Ty);
David Blaikie409dd9c2013-11-19 23:08:21 +0000924
David Blaikie9d861be2013-11-26 00:15:27 +0000925 updateAcceleratorTables(Context, Ty, TyDIE);
David Blaikie409dd9c2013-11-19 23:08:21 +0000926 return TyDIE;
927}
928
Devang Patel0e821f42011-04-12 23:21:44 +0000929/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
930/// given DIType.
Eric Christophera5a79422013-12-09 23:32:48 +0000931DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
David Blaikie32887552013-11-14 22:25:02 +0000932 if (!TyNode)
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000933 return NULL;
Manman Renf4c339e2013-10-29 22:49:29 +0000934
David Blaikie32887552013-11-14 22:25:02 +0000935 DIType Ty(TyNode);
936 assert(Ty.isType());
937
Manman Renf4c339e2013-10-29 22:49:29 +0000938 // Construct the context before querying for the existence of the DIE in case
939 // such construction creates the DIE.
David Blaikie9d861be2013-11-26 00:15:27 +0000940 DIScope Context = resolve(Ty.getContext());
941 DIE *ContextDIE = getOrCreateContextDIE(Context);
Adrian Prantl4583f7d2013-11-15 23:21:39 +0000942 assert(ContextDIE);
Manman Renf4c339e2013-10-29 22:49:29 +0000943
Eric Christophere595bae2013-10-04 17:08:38 +0000944 DIE *TyDIE = getDIE(Ty);
Devang Patel0e821f42011-04-12 23:21:44 +0000945 if (TyDIE)
946 return TyDIE;
947
948 // Create new type.
Manman Renf4c339e2013-10-29 22:49:29 +0000949 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
950
Devang Patel0e821f42011-04-12 23:21:44 +0000951 if (Ty.isBasicType())
952 constructTypeDIE(*TyDIE, DIBasicType(Ty));
David Blaikie8a263cb2013-11-26 00:22:37 +0000953 else if (Ty.isCompositeType()) {
954 DICompositeType CTy(Ty);
David Blaikiecfb21152014-01-03 18:59:42 +0000955 if (GenerateDwarfTypeUnits && !Ty.isForwardDecl())
956 if (MDString *TypeId = CTy.getIdentifier()) {
David Blaikie15632ae2014-02-12 00:31:30 +0000957 DD->addDwarfTypeUnitType(getCU(), TypeId->getString(), TyDIE, CTy);
David Blaikiecfb21152014-01-03 18:59:42 +0000958 // Skip updating the accellerator tables since this is not the full type
959 return TyDIE;
960 }
David Blaikiefbd29eb2013-11-26 00:35:04 +0000961 constructTypeDIE(*TyDIE, CTy);
David Blaikie8a263cb2013-11-26 00:22:37 +0000962 } else {
Devang Patel0e821f42011-04-12 23:21:44 +0000963 assert(Ty.isDerivedType() && "Unknown kind of DIType");
964 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
965 }
David Blaikie2ea848b2013-11-19 22:51:04 +0000966
David Blaikie9d861be2013-11-26 00:15:27 +0000967 updateAcceleratorTables(Context, Ty, TyDIE);
David Blaikie2ea848b2013-11-19 22:51:04 +0000968
969 return TyDIE;
970}
971
Eric Christophera5a79422013-12-09 23:32:48 +0000972void DwarfUnit::updateAcceleratorTables(DIScope Context, DIType Ty,
973 const DIE *TyDIE) {
Eric Christopher21bde872012-01-06 04:35:23 +0000974 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
975 bool IsImplementation = 0;
976 if (Ty.isCompositeType()) {
977 DICompositeType CT(Ty);
Eric Christopher8ea8e4f2012-01-06 23:03:37 +0000978 // A runtime language of 0 actually means C/C++ and that any
979 // non-negative value is some version of Objective-C/C++.
Eric Christopherc2697f82013-10-19 01:04:47 +0000980 IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete();
Eric Christopher21bde872012-01-06 04:35:23 +0000981 }
Eric Christophercf7289f2013-09-05 18:20:16 +0000982 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
Eric Christopher8ea8e4f2012-01-06 23:03:37 +0000983 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
David Blaikie9d861be2013-11-26 00:15:27 +0000984
985 if (!Context || Context.isCompileUnit() || Context.isFile() ||
986 Context.isNameSpace())
987 GlobalTypes[getParentContextString(Context) + Ty.getName().str()] = TyDIE;
Eric Christopher21bde872012-01-06 04:35:23 +0000988 }
Devang Patel0e821f42011-04-12 23:21:44 +0000989}
990
991/// addType - Add a new type attribute to the specified entity.
Eric Christophera5a79422013-12-09 23:32:48 +0000992void DwarfUnit::addType(DIE *Entity, DIType Ty, dwarf::Attribute Attribute) {
Eric Christopher0df08e22013-08-08 07:40:37 +0000993 assert(Ty && "Trying to add a type that doesn't exist?");
Devang Patel0e821f42011-04-12 23:21:44 +0000994
995 // Check for pre-existence.
996 DIEEntry *Entry = getDIEEntry(Ty);
997 // If it exists then use the existing value.
998 if (Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +0000999 addDIEEntry(Entity, Attribute, Entry);
Devang Patel0e821f42011-04-12 23:21:44 +00001000 return;
1001 }
1002
1003 // Construct type.
1004 DIE *Buffer = getOrCreateTypeDIE(Ty);
1005
1006 // Set up proxy.
1007 Entry = createDIEEntry(Buffer);
1008 insertDIEEntry(Ty, Entry);
Manman Ren4dbdc902013-10-31 17:54:35 +00001009 addDIEEntry(Entity, Attribute, Entry);
Devang Patel1cb8ab42011-05-31 23:30:30 +00001010}
1011
Eric Christopher9cd26af2013-09-20 23:22:52 +00001012// Accelerator table mutators - add each name along with its companion
1013// DIE to the proper table while ensuring that the name that we're going
1014// to reference is in the string table. We do this since the names we
1015// add may not only be identical to the names in the DIE.
Eric Christophera5a79422013-12-09 23:32:48 +00001016void DwarfUnit::addAccelName(StringRef Name, const DIE *Die) {
Eric Christopherd0d5bba2014-02-12 22:47:09 +00001017 if (!DD->useDwarfAccelTables())
1018 return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001019 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001020 std::vector<const DIE *> &DIEs = AccelNames[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001021 DIEs.push_back(Die);
1022}
1023
Eric Christophera5a79422013-12-09 23:32:48 +00001024void DwarfUnit::addAccelObjC(StringRef Name, const DIE *Die) {
Eric Christopherd0d5bba2014-02-12 22:47:09 +00001025 if (!DD->useDwarfAccelTables())
1026 return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001027 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001028 std::vector<const DIE *> &DIEs = AccelObjC[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001029 DIEs.push_back(Die);
1030}
1031
Eric Christophera5a79422013-12-09 23:32:48 +00001032void DwarfUnit::addAccelNamespace(StringRef Name, const DIE *Die) {
Eric Christopherd0d5bba2014-02-12 22:47:09 +00001033 if (!DD->useDwarfAccelTables())
1034 return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001035 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001036 std::vector<const DIE *> &DIEs = AccelNamespace[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001037 DIEs.push_back(Die);
1038}
1039
Eric Christophera5a79422013-12-09 23:32:48 +00001040void DwarfUnit::addAccelType(StringRef Name,
1041 std::pair<const DIE *, unsigned> Die) {
Eric Christopherd0d5bba2014-02-12 22:47:09 +00001042 if (!DD->useDwarfAccelTables())
1043 return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001044 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001045 std::vector<std::pair<const DIE *, unsigned> > &DIEs = AccelTypes[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001046 DIEs.push_back(Die);
1047}
1048
Eric Christopher9c58f312013-09-20 22:20:55 +00001049/// addGlobalName - Add a new global name to the compile unit.
Eric Christophera5a79422013-12-09 23:32:48 +00001050void DwarfUnit::addGlobalName(StringRef Name, DIE *Die, DIScope Context) {
Eric Christopher8dba0d52013-10-19 01:04:42 +00001051 std::string FullName = getParentContextString(Context) + Name.str();
Eric Christopher2c8b7902013-10-17 02:06:06 +00001052 GlobalNames[FullName] = Die;
Eric Christopher9c58f312013-09-20 22:20:55 +00001053}
1054
Eric Christopher2c8b7902013-10-17 02:06:06 +00001055/// getParentContextString - Walks the metadata parent chain in a language
1056/// specific manner (using the compile unit language) and returns
1057/// it as a string. This is done at the metadata level because DIEs may
1058/// not currently have been added to the parent context and walking the
1059/// DIEs looking for names is more expensive than walking the metadata.
Eric Christophera5a79422013-12-09 23:32:48 +00001060std::string DwarfUnit::getParentContextString(DIScope Context) const {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001061 if (!Context)
1062 return "";
1063
1064 // FIXME: Decide whether to implement this for non-C++ languages.
1065 if (getLanguage() != dwarf::DW_LANG_C_plus_plus)
1066 return "";
1067
Eric Christopher8dba0d52013-10-19 01:04:42 +00001068 std::string CS;
Eric Christopher2c8b7902013-10-17 02:06:06 +00001069 SmallVector<DIScope, 1> Parents;
1070 while (!Context.isCompileUnit()) {
1071 Parents.push_back(Context);
1072 if (Context.getContext())
1073 Context = resolve(Context.getContext());
1074 else
1075 // Structure, etc types will have a NULL context if they're at the top
1076 // level.
1077 break;
1078 }
1079
1080 // Reverse iterate over our list to go from the outermost construct to the
1081 // innermost.
1082 for (SmallVectorImpl<DIScope>::reverse_iterator I = Parents.rbegin(),
1083 E = Parents.rend();
1084 I != E; ++I) {
1085 DIScope Ctx = *I;
1086 StringRef Name = Ctx.getName();
Eric Christopher8dba0d52013-10-19 01:04:42 +00001087 if (!Name.empty()) {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001088 CS += Name;
1089 CS += "::";
1090 }
1091 }
1092 return CS;
Devang Patel0e821f42011-04-12 23:21:44 +00001093}
1094
1095/// constructTypeDIE - Construct basic type die from DIBasicType.
Eric Christophera5a79422013-12-09 23:32:48 +00001096void DwarfUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Devang Patel0e821f42011-04-12 23:21:44 +00001097 // Get core information.
1098 StringRef Name = BTy.getName();
Devang Patel0e821f42011-04-12 23:21:44 +00001099 // Add name if not anonymous or intermediate type.
1100 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001101 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel04d6d472011-09-14 23:13:28 +00001102
David Blaikiefac56122013-10-04 23:21:16 +00001103 // An unspecified type only has a name attribute.
1104 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type)
Devang Patel04d6d472011-09-14 23:13:28 +00001105 return;
Devang Patel04d6d472011-09-14 23:13:28 +00001106
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001107 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Pateld925d1a2012-02-07 23:33:58 +00001108 BTy.getEncoding());
Devang Patel04d6d472011-09-14 23:13:28 +00001109
Devang Patel0e821f42011-04-12 23:21:44 +00001110 uint64_t Size = BTy.getSizeInBits() >> 3;
David Blaikief2443192013-10-21 17:28:37 +00001111 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001112}
1113
1114/// constructTypeDIE - Construct derived type die from DIDerivedType.
Eric Christophera5a79422013-12-09 23:32:48 +00001115void DwarfUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Devang Patel0e821f42011-04-12 23:21:44 +00001116 // Get core information.
1117 StringRef Name = DTy.getName();
1118 uint64_t Size = DTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001119 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001120
1121 // Map to main type, void will not have a type.
Manman Ren93b30902013-10-08 18:42:58 +00001122 DIType FromTy = resolve(DTy.getTypeDerivedFrom());
Eric Christopher0df08e22013-08-08 07:40:37 +00001123 if (FromTy)
1124 addType(&Buffer, FromTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001125
1126 // Add name if not anonymous or intermediate type.
1127 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001128 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001129
1130 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher85757902012-02-21 22:25:53 +00001131 if (Size && Tag != dwarf::DW_TAG_pointer_type)
David Blaikief2443192013-10-21 17:28:37 +00001132 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001133
David Blaikie5d3249b2013-01-07 05:51:15 +00001134 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
Eric Christopherc2697f82013-10-19 01:04:47 +00001135 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
1136 getOrCreateTypeDIE(resolve(DTy.getClassType())));
Devang Patel0e821f42011-04-12 23:21:44 +00001137 // Add source line info if available and TyDesc is not a forward declaration.
1138 if (!DTy.isForwardDecl())
1139 addSourceLine(&Buffer, DTy);
1140}
1141
Adrian Prantl3f49c892014-02-25 19:57:42 +00001142/// constructSubprogramArguments - Construct function argument DIEs.
1143void DwarfUnit::constructSubprogramArguments(DIE &Buffer, DIArray Args) {
Adrian Prantl69140d22014-02-25 22:27:14 +00001144 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1145 DIDescriptor Ty = Args.getElement(i);
1146 if (Ty.isUnspecifiedParameter()) {
1147 assert(i == N-1 && "Unspecified parameter must be the last argument");
1148 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
1149 } else {
1150 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
1151 addType(Arg, DIType(Ty));
1152 if (DIType(Ty).isArtificial())
1153 addFlag(Arg, dwarf::DW_AT_artificial);
Adrian Prantl3f49c892014-02-25 19:57:42 +00001154 }
Adrian Prantl69140d22014-02-25 22:27:14 +00001155 }
Adrian Prantl3f49c892014-02-25 19:57:42 +00001156}
1157
Devang Patel0e821f42011-04-12 23:21:44 +00001158/// constructTypeDIE - Construct type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001159void DwarfUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
David Blaikie409dd9c2013-11-19 23:08:21 +00001160 // Add name if not anonymous or intermediate type.
Devang Patel0e821f42011-04-12 23:21:44 +00001161 StringRef Name = CTy.getName();
1162
1163 uint64_t Size = CTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001164 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001165
1166 switch (Tag) {
Devang Patel0e821f42011-04-12 23:21:44 +00001167 case dwarf::DW_TAG_array_type:
Eric Christopherdf9955d2013-11-11 18:52:31 +00001168 constructArrayTypeDIE(Buffer, CTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001169 break;
Eric Christopheraeb105f2013-11-11 18:52:39 +00001170 case dwarf::DW_TAG_enumeration_type:
1171 constructEnumTypeDIE(Buffer, CTy);
1172 break;
Devang Patel0e821f42011-04-12 23:21:44 +00001173 case dwarf::DW_TAG_subroutine_type: {
Eric Christopher0df08e22013-08-08 07:40:37 +00001174 // Add return type. A void return won't have a type.
Devang Patel0e821f42011-04-12 23:21:44 +00001175 DIArray Elements = CTy.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001176 DIType RTy(Elements.getElement(0));
Eric Christopher0df08e22013-08-08 07:40:37 +00001177 if (RTy)
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001178 addType(&Buffer, RTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001179
1180 bool isPrototyped = true;
Adrian Prantl3f49c892014-02-25 19:57:42 +00001181 if (Elements.getNumElements() == 2 &&
1182 Elements.getElement(1).isUnspecifiedParameter())
1183 isPrototyped = false;
1184
1185 constructSubprogramArguments(Buffer, Elements);
1186
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001187 // Add prototype flag if we're dealing with a C language and the
1188 // function has been prototyped.
David Blaikiecb8e4352013-11-15 23:50:53 +00001189 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001190 if (isPrototyped &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001191 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopherd42b92f2012-05-22 18:45:24 +00001192 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001193 addFlag(&Buffer, dwarf::DW_AT_prototyped);
Adrian Prantl99c7af22013-12-18 21:48:19 +00001194
1195 if (CTy.isLValueReference())
1196 addFlag(&Buffer, dwarf::DW_AT_reference);
1197
1198 if (CTy.isRValueReference())
1199 addFlag(&Buffer, dwarf::DW_AT_rvalue_reference);
Eric Christopherc2697f82013-10-19 01:04:47 +00001200 } break;
Devang Patel0e821f42011-04-12 23:21:44 +00001201 case dwarf::DW_TAG_structure_type:
1202 case dwarf::DW_TAG_union_type:
1203 case dwarf::DW_TAG_class_type: {
Devang Patel0e821f42011-04-12 23:21:44 +00001204 // Add elements to structure type.
David Blaikiea1ae0e62013-08-01 20:30:22 +00001205 DIArray Elements = CTy.getTypeArray();
1206 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel0e821f42011-04-12 23:21:44 +00001207 DIDescriptor Element = Elements.getElement(i);
1208 DIE *ElemDie = NULL;
Adrian Prantlef129fb2014-01-18 02:12:00 +00001209 if (Element.isSubprogram())
1210 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
1211 else if (Element.isDerivedType()) {
Eric Christopherd42b92f2012-05-22 18:45:24 +00001212 DIDerivedType DDTy(Element);
1213 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
Manman Renb987e512013-10-29 00:53:03 +00001214 ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
Manman Ren93b30902013-10-08 18:42:58 +00001215 addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()),
Manman Renb3388602013-10-05 01:43:03 +00001216 dwarf::DW_AT_friend);
Manman Renc6b63922013-10-14 20:33:57 +00001217 } else if (DDTy.isStaticMember()) {
Manman Ren57e6ff72013-10-23 22:57:12 +00001218 getOrCreateStaticMemberDIE(DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001219 } else {
Manman Ren230ec862013-10-23 23:00:44 +00001220 constructMemberDIE(Buffer, DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001221 }
Eric Christopher7285c7d2012-03-28 07:34:31 +00001222 } else if (Element.isObjCProperty()) {
Devang Pateld925d1a2012-02-07 23:33:58 +00001223 DIObjCProperty Property(Element);
Manman Renb987e512013-10-29 00:53:03 +00001224 ElemDie = createAndAddDIE(Property.getTag(), Buffer);
Devang Pateld925d1a2012-02-07 23:33:58 +00001225 StringRef PropertyName = Property.getObjCPropertyName();
1226 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Eric Christopher4c960562014-01-23 19:16:28 +00001227 if (Property.getType())
1228 addType(ElemDie, Property.getType());
Eric Christopherd42b92f2012-05-22 18:45:24 +00001229 addSourceLine(ElemDie, Property);
Devang Pateld925d1a2012-02-07 23:33:58 +00001230 StringRef GetterName = Property.getObjCPropertyGetterName();
1231 if (!GetterName.empty())
1232 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
1233 StringRef SetterName = Property.getObjCPropertySetterName();
1234 if (!SetterName.empty())
1235 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
1236 unsigned PropertyAttributes = 0;
Devang Patel403e8192012-02-04 01:30:32 +00001237 if (Property.isReadOnlyObjCProperty())
1238 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1239 if (Property.isReadWriteObjCProperty())
1240 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1241 if (Property.isAssignObjCProperty())
1242 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1243 if (Property.isRetainObjCProperty())
1244 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1245 if (Property.isCopyObjCProperty())
1246 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1247 if (Property.isNonAtomicObjCProperty())
1248 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1249 if (PropertyAttributes)
David Blaikief2443192013-10-21 17:28:37 +00001250 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None,
Eric Christopherc2697f82013-10-19 01:04:47 +00001251 PropertyAttributes);
Devang Patel44882172012-02-06 17:49:43 +00001252
Devang Pateld925d1a2012-02-07 23:33:58 +00001253 DIEEntry *Entry = getDIEEntry(Element);
1254 if (!Entry) {
1255 Entry = createDIEEntry(ElemDie);
1256 insertDIEEntry(Element, Entry);
1257 }
Devang Patel403e8192012-02-04 01:30:32 +00001258 } else
Devang Patel0e821f42011-04-12 23:21:44 +00001259 continue;
Devang Patel0e821f42011-04-12 23:21:44 +00001260 }
1261
1262 if (CTy.isAppleBlockExtension())
Eric Christopherbb69a272012-08-24 01:14:27 +00001263 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel0e821f42011-04-12 23:21:44 +00001264
Eric Christopher9e429ae2013-10-05 00:27:02 +00001265 DICompositeType ContainingType(resolve(CTy.getContainingType()));
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001266 if (ContainingType)
Manman Ren4c4b69c2013-10-11 23:58:05 +00001267 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001268 getOrCreateTypeDIE(ContainingType));
Devang Patel0e821f42011-04-12 23:21:44 +00001269
Devang Patel12419ae2011-05-12 21:29:42 +00001270 if (CTy.isObjcClassComplete())
Eric Christopherbb69a272012-08-24 01:14:27 +00001271 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patel2409e782011-05-12 19:06:16 +00001272
Eric Christopherda011dd2011-12-16 23:42:42 +00001273 // Add template parameters to a class, structure or union types.
1274 // FIXME: The support isn't in the metadata for this yet.
1275 if (Tag == dwarf::DW_TAG_class_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001276 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
Devang Patel0e821f42011-04-12 23:21:44 +00001277 addTemplateParams(Buffer, CTy.getTemplateParams());
1278
1279 break;
1280 }
1281 default:
1282 break;
1283 }
1284
1285 // Add name if not anonymous or intermediate type.
1286 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001287 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001288
Eric Christopher775cbd22012-05-22 18:45:18 +00001289 if (Tag == dwarf::DW_TAG_enumeration_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001290 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
Eric Christopher775cbd22012-05-22 18:45:18 +00001291 Tag == dwarf::DW_TAG_union_type) {
Devang Patel0e821f42011-04-12 23:21:44 +00001292 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher1cf33382012-06-01 00:22:32 +00001293 // TODO: Do we care about size for enum forward declarations?
Devang Patel0e821f42011-04-12 23:21:44 +00001294 if (Size)
David Blaikief2443192013-10-21 17:28:37 +00001295 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Eric Christopher1cf33382012-06-01 00:22:32 +00001296 else if (!CTy.isForwardDecl())
Devang Patel0e821f42011-04-12 23:21:44 +00001297 // Add zero size if it is not a forward declaration.
David Blaikief2443192013-10-21 17:28:37 +00001298 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, 0);
Eric Christopher1cf33382012-06-01 00:22:32 +00001299
1300 // If we're a forward decl, say so.
1301 if (CTy.isForwardDecl())
Eric Christopherbb69a272012-08-24 01:14:27 +00001302 addFlag(&Buffer, dwarf::DW_AT_declaration);
Devang Patel0e821f42011-04-12 23:21:44 +00001303
1304 // Add source line info if available.
1305 if (!CTy.isForwardDecl())
1306 addSourceLine(&Buffer, CTy);
Eric Christopher54cf8ff2012-03-07 00:15:19 +00001307
1308 // No harm in adding the runtime language to the declaration.
1309 unsigned RLang = CTy.getRunTimeLang();
1310 if (RLang)
Eric Christopherc2697f82013-10-19 01:04:47 +00001311 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
1312 RLang);
Devang Patel0e821f42011-04-12 23:21:44 +00001313 }
1314}
1315
Manman Renffc9a712013-10-23 23:05:28 +00001316/// constructTemplateTypeParameterDIE - Construct new DIE for the given
1317/// DITemplateTypeParameter.
Eric Christophera5a79422013-12-09 23:32:48 +00001318void DwarfUnit::constructTemplateTypeParameterDIE(DIE &Buffer,
1319 DITemplateTypeParameter TP) {
Manman Renb987e512013-10-29 00:53:03 +00001320 DIE *ParamDIE =
1321 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
Eric Christopher056b6472013-08-08 08:09:43 +00001322 // Add the type if it exists, it could be void and therefore no type.
1323 if (TP.getType())
Manman Ren88b0f942013-10-09 19:46:28 +00001324 addType(ParamDIE, resolve(TP.getType()));
David Blaikie2b380232013-06-22 18:59:11 +00001325 if (!TP.getName().empty())
1326 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel0e821f42011-04-12 23:21:44 +00001327}
1328
Manman Renffc9a712013-10-23 23:05:28 +00001329/// constructTemplateValueParameterDIE - Construct new DIE for the given
1330/// DITemplateValueParameter.
Eric Christophera5a79422013-12-09 23:32:48 +00001331void
1332DwarfUnit::constructTemplateValueParameterDIE(DIE &Buffer,
David Blaikie319a05f2013-12-02 19:33:10 +00001333 DITemplateValueParameter VP) {
Manman Renb987e512013-10-29 00:53:03 +00001334 DIE *ParamDIE = createAndAddDIE(VP.getTag(), Buffer);
Eric Christopher0df08e22013-08-08 07:40:37 +00001335
1336 // Add the type if there is one, template template and template parameter
1337 // packs will not have a type.
Eric Christopher691281b2013-10-21 17:48:51 +00001338 if (VP.getTag() == dwarf::DW_TAG_template_value_parameter)
Manman Ren88b0f942013-10-09 19:46:28 +00001339 addType(ParamDIE, resolve(VP.getType()));
Eric Christopherafb2c412013-08-08 07:40:31 +00001340 if (!VP.getName().empty())
1341 addString(ParamDIE, dwarf::DW_AT_name, VP.getName());
1342 if (Value *Val = VP.getValue()) {
David Blaikiea1e813d2013-05-10 21:52:07 +00001343 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val))
Manman Ren88b0f942013-10-09 19:46:28 +00001344 addConstantValue(ParamDIE, CI,
1345 isUnsignedDIType(DD, resolve(VP.getType())));
David Blaikiea1e813d2013-05-10 21:52:07 +00001346 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) {
1347 // For declaration non-type template parameters (such as global values and
1348 // functions)
Eric Christopher4a741042014-02-16 08:46:55 +00001349 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
1350 addOpAddress(Loc, Asm->getSymbol(GV));
David Blaikiea1e813d2013-05-10 21:52:07 +00001351 // Emit DW_OP_stack_value to use the address as the immediate value of the
1352 // parameter, rather than a pointer to it.
Eric Christopher4a741042014-02-16 08:46:55 +00001353 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1354 addBlock(ParamDIE, dwarf::DW_AT_location, Loc);
Eric Christopherafb2c412013-08-08 07:40:31 +00001355 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) {
David Blaikie2b380232013-06-22 18:59:11 +00001356 assert(isa<MDString>(Val));
1357 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1358 cast<MDString>(Val)->getString());
Eric Christopherafb2c412013-08-08 07:40:31 +00001359 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
David Blaikie2b380232013-06-22 18:59:11 +00001360 assert(isa<MDNode>(Val));
1361 DIArray A(cast<MDNode>(Val));
1362 addTemplateParams(*ParamDIE, A);
David Blaikiea1e813d2013-05-10 21:52:07 +00001363 }
1364 }
Devang Patel0e821f42011-04-12 23:21:44 +00001365}
1366
Devang Patel17b53272011-05-06 16:57:54 +00001367/// getOrCreateNameSpace - Create a DIE for DINameSpace.
Eric Christophera5a79422013-12-09 23:32:48 +00001368DIE *DwarfUnit::getOrCreateNameSpace(DINameSpace NS) {
Manman Renf6b936b2013-10-29 05:49:41 +00001369 // Construct the context before querying for the existence of the DIE in case
1370 // such construction creates the DIE.
1371 DIE *ContextDIE = getOrCreateContextDIE(NS.getContext());
Manman Renf6b936b2013-10-29 05:49:41 +00001372
Devang Patel17b53272011-05-06 16:57:54 +00001373 DIE *NDie = getDIE(NS);
1374 if (NDie)
1375 return NDie;
Manman Renf6b936b2013-10-29 05:49:41 +00001376 NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
1377
Eric Christopher4996c702011-11-07 09:24:32 +00001378 if (!NS.getName().empty()) {
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001379 addString(NDie, dwarf::DW_AT_name, NS.getName());
Eric Christopher4996c702011-11-07 09:24:32 +00001380 addAccelNamespace(NS.getName(), NDie);
Eric Christopher2c8b7902013-10-17 02:06:06 +00001381 addGlobalName(NS.getName(), NDie, NS.getContext());
Eric Christopher4996c702011-11-07 09:24:32 +00001382 } else
1383 addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel17b53272011-05-06 16:57:54 +00001384 addSourceLine(NDie, NS);
Devang Patel17b53272011-05-06 16:57:54 +00001385 return NDie;
1386}
1387
Devang Patel89543712011-08-15 17:24:54 +00001388/// getOrCreateSubprogramDIE - Create new DIE using SP.
Eric Christophera5a79422013-12-09 23:32:48 +00001389DIE *DwarfUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
David Blaikie309ffe42013-10-04 01:39:59 +00001390 // Construct the context before querying for the existence of the DIE in case
1391 // such construction creates the DIE (as is the case for member function
1392 // declarations).
Manman Renc50fa112013-10-10 18:40:01 +00001393 DIE *ContextDIE = getOrCreateContextDIE(resolve(SP.getContext()));
David Blaikie309ffe42013-10-04 01:39:59 +00001394
Eric Christophere595bae2013-10-04 17:08:38 +00001395 DIE *SPDie = getDIE(SP);
Devang Patel89543712011-08-15 17:24:54 +00001396 if (SPDie)
1397 return SPDie;
1398
Manman Ren73d697c2013-10-29 00:58:04 +00001399 DISubprogram SPDecl = SP.getFunctionDeclaration();
1400 if (SPDecl.isSubprogram())
1401 // Add subprogram definitions to the CU die directly.
David Blaikie2a80e442013-12-02 22:09:48 +00001402 ContextDIE = UnitDie.get();
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001403
1404 // DW_TAG_inlined_subroutine may refer to this DIE.
Manman Ren73d697c2013-10-29 00:58:04 +00001405 SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001406
Rafael Espindola79278362011-11-10 22:34:29 +00001407 DIE *DeclDie = NULL;
Manman Renb9512a72013-10-23 22:12:26 +00001408 if (SPDecl.isSubprogram())
Rafael Espindola79278362011-11-10 22:34:29 +00001409 DeclDie = getOrCreateSubprogramDIE(SPDecl);
Rafael Espindola79278362011-11-10 22:34:29 +00001410
Devang Patel89543712011-08-15 17:24:54 +00001411 // Add function template parameters.
1412 addTemplateParams(*SPDie, SP.getTemplateParams());
1413
Devang Patel89543712011-08-15 17:24:54 +00001414 // If this DIE is going to refer declaration info using AT_specification
Eric Christopherf20ff972013-05-09 00:42:33 +00001415 // then there is no need to add other attributes.
Rafael Espindola79278362011-11-10 22:34:29 +00001416 if (DeclDie) {
1417 // Refer function declaration directly.
Manman Ren4c4b69c2013-10-11 23:58:05 +00001418 addDIEEntry(SPDie, dwarf::DW_AT_specification, DeclDie);
Rafael Espindola79278362011-11-10 22:34:29 +00001419
Devang Patel89543712011-08-15 17:24:54 +00001420 return SPDie;
Rafael Espindola79278362011-11-10 22:34:29 +00001421 }
Devang Patel89543712011-08-15 17:24:54 +00001422
Eric Christopheracb71152012-08-23 22:52:55 +00001423 // Add the linkage name if we have one.
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001424 StringRef LinkageName = SP.getLinkageName();
1425 if (!LinkageName.empty())
Eric Christopheracb71152012-08-23 22:52:55 +00001426 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001427 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopheracb71152012-08-23 22:52:55 +00001428
Devang Patel89543712011-08-15 17:24:54 +00001429 // Constructors and operators for anonymous aggregates do not have names.
1430 if (!SP.getName().empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001431 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Patel89543712011-08-15 17:24:54 +00001432
1433 addSourceLine(SPDie, SP);
1434
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001435 // Add the prototype if we have a prototype and we have a C like
1436 // language.
David Blaikiecb8e4352013-11-15 23:50:53 +00001437 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001438 if (SP.isPrototyped() &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001439 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001440 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001441 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Patel89543712011-08-15 17:24:54 +00001442
Devang Patel89543712011-08-15 17:24:54 +00001443 DICompositeType SPTy = SP.getType();
David Blaikie5174c842013-05-22 23:22:18 +00001444 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type &&
1445 "the type of a subprogram should be a subroutine");
Devang Patel89543712011-08-15 17:24:54 +00001446
David Blaikie5174c842013-05-22 23:22:18 +00001447 DIArray Args = SPTy.getTypeArray();
Eric Christopher691281b2013-10-21 17:48:51 +00001448 // Add a return type. If this is a type like a C/C++ void type we don't add a
1449 // return type.
Eric Christopher0df08e22013-08-08 07:40:37 +00001450 if (Args.getElement(0))
1451 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel89543712011-08-15 17:24:54 +00001452
1453 unsigned VK = SP.getVirtuality();
1454 if (VK) {
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001455 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Eric Christopher4a741042014-02-16 08:46:55 +00001456 DIELoc *Block = getDIELoc();
David Blaikief2443192013-10-21 17:28:37 +00001457 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1458 addUInt(Block, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1459 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
Eric Christopher98b7f172013-11-11 18:52:36 +00001460 ContainingTypeMap.insert(
1461 std::make_pair(SPDie, resolve(SP.getContainingType())));
Devang Patel89543712011-08-15 17:24:54 +00001462 }
1463
1464 if (!SP.isDefinition()) {
Eric Christopherbb69a272012-08-24 01:14:27 +00001465 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher92331fd2012-11-21 00:34:38 +00001466
Devang Patel89543712011-08-15 17:24:54 +00001467 // Add arguments. Do not add arguments for subprogram definition. They will
1468 // be handled while processing variables.
Adrian Prantl3f49c892014-02-25 19:57:42 +00001469 constructSubprogramArguments(*SPDie, Args);
Devang Patel89543712011-08-15 17:24:54 +00001470 }
1471
1472 if (SP.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001473 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Patel89543712011-08-15 17:24:54 +00001474
1475 if (!SP.isLocalToUnit())
Eric Christopherbb69a272012-08-24 01:14:27 +00001476 addFlag(SPDie, dwarf::DW_AT_external);
Devang Patel89543712011-08-15 17:24:54 +00001477
1478 if (SP.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +00001479 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Patel89543712011-08-15 17:24:54 +00001480
1481 if (unsigned isa = Asm->getISAEncoding()) {
1482 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1483 }
1484
Adrian Prantl99c7af22013-12-18 21:48:19 +00001485 if (SP.isLValueReference())
1486 addFlag(SPDie, dwarf::DW_AT_reference);
1487
1488 if (SP.isRValueReference())
1489 addFlag(SPDie, dwarf::DW_AT_rvalue_reference);
1490
Adrian Prantlef129fb2014-01-18 02:12:00 +00001491 if (SP.isProtected())
1492 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1493 dwarf::DW_ACCESS_protected);
1494 else if (SP.isPrivate())
1495 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1496 dwarf::DW_ACCESS_private);
1497 else
1498 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1499 dwarf::DW_ACCESS_public);
1500
1501 if (SP.isExplicit())
1502 addFlag(SPDie, dwarf::DW_AT_explicit);
1503
Devang Patel89543712011-08-15 17:24:54 +00001504 return SPDie;
1505}
1506
Devang Pateldfd6ec32011-08-15 17:57:41 +00001507// Return const expression if value is a GEP to access merged global
1508// constant. e.g.
1509// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1510static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1511 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1512 if (!CE || CE->getNumOperands() != 3 ||
1513 CE->getOpcode() != Instruction::GetElementPtr)
1514 return NULL;
1515
1516 // First operand points to a global struct.
1517 Value *Ptr = CE->getOperand(0);
1518 if (!isa<GlobalValue>(Ptr) ||
1519 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1520 return NULL;
1521
1522 // Second operand is zero.
1523 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1524 if (!CI || !CI->isZero())
1525 return NULL;
1526
1527 // Third operand is offset.
1528 if (!isa<ConstantInt>(CE->getOperand(2)))
1529 return NULL;
1530
1531 return CE;
1532}
1533
1534/// createGlobalVariableDIE - create global variable DIE.
Eric Christopher4287a492013-12-09 23:57:44 +00001535void DwarfCompileUnit::createGlobalVariableDIE(DIGlobalVariable GV) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001536 // Check for pre-existence.
David Blaikie2ad00162013-11-15 23:09:13 +00001537 if (getDIE(GV))
Devang Pateldfd6ec32011-08-15 17:57:41 +00001538 return;
1539
David Blaikie5e390e42014-02-04 01:23:52 +00001540 assert(GV.isGlobalVariable());
Devang Patel0ecbcbd2011-08-18 23:17:55 +00001541
Eric Christopher08f7c8f2013-10-04 23:49:26 +00001542 DIScope GVContext = GV.getContext();
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001543 DIType GTy = GV.getType();
1544
1545 // If this is a static data member definition, some attributes belong
1546 // to the declaration DIE.
1547 DIE *VariableDIE = NULL;
Manman Rene697d3c2013-02-01 23:54:37 +00001548 bool IsStaticMember = false;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001549 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1550 if (SDMDecl.Verify()) {
1551 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1552 // We need the declaration DIE that is in the static member's class.
Manman Renc6b63922013-10-14 20:33:57 +00001553 VariableDIE = getOrCreateStaticMemberDIE(SDMDecl);
Manman Rene697d3c2013-02-01 23:54:37 +00001554 IsStaticMember = true;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001555 }
1556
1557 // If this is not a static data member definition, create the variable
1558 // DIE and add the initial set of attributes to it.
1559 if (!VariableDIE) {
Manman Renf6b936b2013-10-29 05:49:41 +00001560 // Construct the context before querying for the existence of the DIE in
1561 // case such construction creates the DIE.
1562 DIE *ContextDIE = getOrCreateContextDIE(GVContext);
Manman Renf6b936b2013-10-29 05:49:41 +00001563
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001564 // Add to map.
David Blaikie52c50202013-11-16 00:29:01 +00001565 VariableDIE = createAndAddDIE(GV.getTag(), *ContextDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001566
1567 // Add name and type.
1568 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1569 addType(VariableDIE, GTy);
1570
1571 // Add scoping info.
Eric Christopherd2b497b2013-10-16 01:37:49 +00001572 if (!GV.isLocalToUnit())
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001573 addFlag(VariableDIE, dwarf::DW_AT_external);
1574
1575 // Add line number info.
1576 addSourceLine(VariableDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001577 }
1578
Devang Pateldfd6ec32011-08-15 17:57:41 +00001579 // Add location.
Eric Christopher4996c702011-11-07 09:24:32 +00001580 bool addToAccelTable = false;
Eric Christopher0a917b72011-11-11 03:16:32 +00001581 DIE *VariableSpecDIE = NULL;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001582 bool isGlobalVariable = GV.getGlobal() != NULL;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001583 if (isGlobalVariable) {
Eric Christopher4996c702011-11-07 09:24:32 +00001584 addToAccelTable = true;
Eric Christopher4a741042014-02-16 08:46:55 +00001585 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Rafael Espindola79858aa2013-10-29 17:07:16 +00001586 const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal());
David Blaikief2694972013-06-28 20:05:11 +00001587 if (GV.getGlobal()->isThreadLocal()) {
1588 // FIXME: Make this work with -gsplit-dwarf.
1589 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
1590 assert((PointerSize == 4 || PointerSize == 8) &&
1591 "Add support for other sizes if necessary");
1592 // Based on GCC's support for TLS:
David Blaikie8466ca82013-07-01 23:55:52 +00001593 if (!DD->useSplitDwarf()) {
1594 // 1) Start with a constNu of the appropriate pointer size
Eric Christopher4a741042014-02-16 08:46:55 +00001595 addUInt(Loc, dwarf::DW_FORM_data1,
David Blaikie8466ca82013-07-01 23:55:52 +00001596 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
Richard Mitton0aafb582013-10-07 18:39:18 +00001597 // 2) containing the (relocated) offset of the TLS variable
1598 // within the module's TLS block.
Eric Christopher4a741042014-02-16 08:46:55 +00001599 addExpr(Loc, dwarf::DW_FORM_udata,
David Blaikief1a6dea2014-02-15 19:34:03 +00001600 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym));
David Blaikie8466ca82013-07-01 23:55:52 +00001601 } else {
Eric Christopher4a741042014-02-16 08:46:55 +00001602 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
1603 addUInt(Loc, dwarf::DW_FORM_udata,
David Blaikief1a6dea2014-02-15 19:34:03 +00001604 DU->getAddrPoolIndex(Sym, /* TLS */ true));
David Blaikie8466ca82013-07-01 23:55:52 +00001605 }
Richard Mitton0aafb582013-10-07 18:39:18 +00001606 // 3) followed by a custom OP to make the debugger do a TLS lookup.
Eric Christopher4a741042014-02-16 08:46:55 +00001607 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001608 } else {
1609 DD->addArangeLabel(SymbolCU(this, Sym));
Eric Christopher4a741042014-02-16 08:46:55 +00001610 addOpAddress(Loc, Sym);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001611 }
Devang Pateldfd6ec32011-08-15 17:57:41 +00001612 // Do not create specification DIE if context is either compile unit
1613 // or a subprogram.
Devang Patel5e6b65c2011-09-21 23:41:11 +00001614 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Manman Ren3eb9dff2013-09-09 19:05:21 +00001615 !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001616 // Create specification DIE.
David Blaikie2a80e442013-12-02 22:09:48 +00001617 VariableSpecDIE = createAndAddDIE(dwarf::DW_TAG_variable, *UnitDie);
Manman Ren4c4b69c2013-10-11 23:58:05 +00001618 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification, VariableDIE);
Eric Christopher4a741042014-02-16 08:46:55 +00001619 addBlock(VariableSpecDIE, dwarf::DW_AT_location, Loc);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001620 // A static member's declaration is already flagged as such.
1621 if (!SDMDecl.Verify())
1622 addFlag(VariableDIE, dwarf::DW_AT_declaration);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001623 } else {
Eric Christopher4a741042014-02-16 08:46:55 +00001624 addBlock(VariableDIE, dwarf::DW_AT_location, Loc);
Eric Christopher4996c702011-11-07 09:24:32 +00001625 }
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001626 // Add the linkage name.
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001627 StringRef LinkageName = GV.getLinkageName();
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001628 if (!LinkageName.empty())
Eric Christopher3f79b8c2013-02-27 23:49:47 +00001629 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1630 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1631 // TAG_variable.
Eric Christopherc2697f82013-10-19 01:04:47 +00001632 addString(IsStaticMember && VariableSpecDIE ? VariableSpecDIE
1633 : VariableDIE,
1634 dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001635 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopher92331fd2012-11-21 00:34:38 +00001636 } else if (const ConstantInt *CI =
Eric Christopherc2697f82013-10-19 01:04:47 +00001637 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
Adrian Prantl322f41d2013-04-04 22:56:49 +00001638 // AT_const_value was added when the static member was created. To avoid
Manman Rene697d3c2013-02-01 23:54:37 +00001639 // emitting AT_const_value multiple times, we only add AT_const_value when
1640 // it is not a static member.
1641 if (!IsStaticMember)
Manman Renb3388602013-10-05 01:43:03 +00001642 addConstantValue(VariableDIE, CI, isUnsignedDIType(DD, GTy));
David Blaikiea781b25b2013-11-17 21:55:13 +00001643 } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getOperand(11))) {
Eric Christopher4996c702011-11-07 09:24:32 +00001644 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001645 // GV is a merged global.
Eric Christopher4a741042014-02-16 08:46:55 +00001646 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Devang Pateldfd6ec32011-08-15 17:57:41 +00001647 Value *Ptr = CE->getOperand(0);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001648 MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr));
1649 DD->addArangeLabel(SymbolCU(this, Sym));
Eric Christopher4a741042014-02-16 08:46:55 +00001650 addOpAddress(Loc, Sym);
1651 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
Eric Christopherc2697f82013-10-19 01:04:47 +00001652 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
Eric Christopher4a741042014-02-16 08:46:55 +00001653 addUInt(Loc, dwarf::DW_FORM_udata,
Eric Christopherc2697f82013-10-19 01:04:47 +00001654 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
Eric Christopher4a741042014-02-16 08:46:55 +00001655 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1656 addBlock(VariableDIE, dwarf::DW_AT_location, Loc);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001657 }
1658
Eric Christopherc12c2112011-11-11 01:55:22 +00001659 if (addToAccelTable) {
1660 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1661 addAccelName(GV.getName(), AddrDIE);
Eric Christopher4996c702011-11-07 09:24:32 +00001662
Eric Christopherc12c2112011-11-11 01:55:22 +00001663 // If the linkage name is different than the name, go ahead and output
1664 // that as well into the name table.
1665 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1666 addAccelName(GV.getLinkageName(), AddrDIE);
1667 }
Eric Christopherd2b497b2013-10-16 01:37:49 +00001668
1669 if (!GV.isLocalToUnit())
Eric Christopher2c8b7902013-10-17 02:06:06 +00001670 addGlobalName(GV.getName(), VariableSpecDIE ? VariableSpecDIE : VariableDIE,
1671 GV.getContext());
Devang Pateldfd6ec32011-08-15 17:57:41 +00001672}
1673
Devang Patel0e821f42011-04-12 23:21:44 +00001674/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Eric Christophera5a79422013-12-09 23:32:48 +00001675void DwarfUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Manman Renb987e512013-10-29 00:53:03 +00001676 DIE *DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
Manman Ren4c4b69c2013-10-11 23:58:05 +00001677 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, IndexTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001678
Bill Wendling28fe9e72012-12-06 07:38:10 +00001679 // The LowerBound value defines the lower bounds which is typically zero for
1680 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1681 // Count == -1 then the array is unbounded and we do not emit
1682 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1683 // Count == 0, then the array has zero elements in which case we do not emit
1684 // an upper bound.
1685 int64_t LowerBound = SR.getLo();
Bill Wendling3495f9b2012-12-06 07:55:19 +00001686 int64_t DefaultLowerBound = getDefaultLowerBound();
Bill Wendlingd7767122012-12-04 21:34:03 +00001687 int64_t Count = SR.getCount();
Devang Patel0e821f42011-04-12 23:21:44 +00001688
Bill Wendling3495f9b2012-12-06 07:55:19 +00001689 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
David Blaikief2443192013-10-21 17:28:37 +00001690 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound);
Bill Wendling28fe9e72012-12-06 07:38:10 +00001691
1692 if (Count != -1 && Count != 0)
Bill Wendlingd7767122012-12-04 21:34:03 +00001693 // FIXME: An unbounded array should reference the expression that defines
1694 // the array.
Eric Christopher98b7f172013-11-11 18:52:36 +00001695 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None,
1696 LowerBound + Count - 1);
Devang Patel0e821f42011-04-12 23:21:44 +00001697}
1698
1699/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001700void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopherdf9955d2013-11-11 18:52:31 +00001701 if (CTy.isVector())
Eric Christopherbb69a272012-08-24 01:14:27 +00001702 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel0e821f42011-04-12 23:21:44 +00001703
Eric Christopher0df08e22013-08-08 07:40:37 +00001704 // Emit the element type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001705 addType(&Buffer, resolve(CTy.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001706
1707 // Get an anonymous type for index type.
Eric Christophercad9b532013-01-04 21:51:53 +00001708 // FIXME: This type should be passed down from the front end
1709 // as different languages may have different sizes for indexes.
Devang Patel0e821f42011-04-12 23:21:44 +00001710 DIE *IdxTy = getIndexTyDie();
1711 if (!IdxTy) {
1712 // Construct an anonymous type for index type.
David Blaikie2a80e442013-12-02 22:09:48 +00001713 IdxTy = createAndAddDIE(dwarf::DW_TAG_base_type, *UnitDie);
Eric Christophercad9b532013-01-04 21:51:53 +00001714 addString(IdxTy, dwarf::DW_AT_name, "int");
David Blaikief2443192013-10-21 17:28:37 +00001715 addUInt(IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int32_t));
Devang Patel0e821f42011-04-12 23:21:44 +00001716 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1717 dwarf::DW_ATE_signed);
Devang Patel0e821f42011-04-12 23:21:44 +00001718 setIndexTyDie(IdxTy);
1719 }
1720
1721 // Add subranges to array type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001722 DIArray Elements = CTy.getTypeArray();
Devang Patel0e821f42011-04-12 23:21:44 +00001723 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1724 DIDescriptor Element = Elements.getElement(i);
1725 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1726 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1727 }
1728}
1729
Eric Christopheraeb105f2013-11-11 18:52:39 +00001730/// constructEnumTypeDIE - Construct an enum type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001731void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopheraeb105f2013-11-11 18:52:39 +00001732 DIArray Elements = CTy.getTypeArray();
1733
1734 // Add enumerators to enumeration type.
1735 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001736 DIEnumerator Enum(Elements.getElement(i));
Eric Christopheraeb105f2013-11-11 18:52:39 +00001737 if (Enum.isEnumerator()) {
1738 DIE *Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001739 StringRef Name = Enum.getName();
Eric Christopheraeb105f2013-11-11 18:52:39 +00001740 addString(Enumerator, dwarf::DW_AT_name, Name);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001741 int64_t Value = Enum.getEnumValue();
Eric Christophera07e4f52013-11-19 09:28:34 +00001742 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
1743 Value);
Eric Christopheraeb105f2013-11-11 18:52:39 +00001744 }
1745 }
1746 DIType DTy = resolve(CTy.getTypeDerivedFrom());
1747 if (DTy) {
1748 addType(&Buffer, DTy);
1749 addFlag(&Buffer, dwarf::DW_AT_enum_class);
1750 }
Devang Patel0e821f42011-04-12 23:21:44 +00001751}
1752
Devang Patel89543712011-08-15 17:24:54 +00001753/// constructContainingTypeDIEs - Construct DIEs for types that contain
1754/// vtables.
Eric Christophera5a79422013-12-09 23:32:48 +00001755void DwarfUnit::constructContainingTypeDIEs() {
Devang Patel89543712011-08-15 17:24:54 +00001756 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Eric Christopherc2697f82013-10-19 01:04:47 +00001757 CE = ContainingTypeMap.end();
1758 CI != CE; ++CI) {
Devang Patel89543712011-08-15 17:24:54 +00001759 DIE *SPDie = CI->first;
David Blaikie2ad00162013-11-15 23:09:13 +00001760 DIDescriptor D(CI->second);
1761 if (!D)
Eric Christopherc2697f82013-10-19 01:04:47 +00001762 continue;
David Blaikie2ad00162013-11-15 23:09:13 +00001763 DIE *NDie = getDIE(D);
Eric Christopherc2697f82013-10-19 01:04:47 +00001764 if (!NDie)
1765 continue;
Manman Ren4c4b69c2013-10-11 23:58:05 +00001766 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, NDie);
Devang Patel89543712011-08-15 17:24:54 +00001767 }
1768}
1769
Devang Patel3acc70e2011-08-15 22:04:40 +00001770/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Eric Christophera5a79422013-12-09 23:32:48 +00001771DIE *DwarfUnit::constructVariableDIE(DbgVariable &DV, bool isScopeAbstract) {
Eric Christopher34a2c872013-11-15 01:43:19 +00001772 StringRef Name = DV.getName();
Devang Patel3acc70e2011-08-15 22:04:40 +00001773
Devang Patel3acc70e2011-08-15 22:04:40 +00001774 // Define variable debug information entry.
Eric Christopher34a2c872013-11-15 01:43:19 +00001775 DIE *VariableDie = new DIE(DV.getTag());
1776 DbgVariable *AbsVar = DV.getAbstractVariable();
Devang Patel3acc70e2011-08-15 22:04:40 +00001777 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
Manman Ren4213c392013-05-29 17:16:59 +00001778 if (AbsDIE)
Manman Ren4c4b69c2013-10-11 23:58:05 +00001779 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin, AbsDIE);
Devang Patel3acc70e2011-08-15 22:04:40 +00001780 else {
David Blaikie715528b2013-08-19 03:34:03 +00001781 if (!Name.empty())
1782 addString(VariableDie, dwarf::DW_AT_name, Name);
Eric Christopher34a2c872013-11-15 01:43:19 +00001783 addSourceLine(VariableDie, DV.getVariable());
1784 addType(VariableDie, DV.getType());
Devang Patel3acc70e2011-08-15 22:04:40 +00001785 }
1786
Eric Christopher34a2c872013-11-15 01:43:19 +00001787 if (DV.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001788 addFlag(VariableDie, dwarf::DW_AT_artificial);
Devang Patel3acc70e2011-08-15 22:04:40 +00001789
1790 if (isScopeAbstract) {
Eric Christopher34a2c872013-11-15 01:43:19 +00001791 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001792 return VariableDie;
1793 }
1794
1795 // Add variable address.
1796
Eric Christopher34a2c872013-11-15 01:43:19 +00001797 unsigned Offset = DV.getDotDebugLocOffset();
Devang Patel3acc70e2011-08-15 22:04:40 +00001798 if (Offset != ~0U) {
Eric Christopher33ff6972013-11-21 23:46:41 +00001799 addSectionLabel(VariableDie, dwarf::DW_AT_location,
1800 Asm->GetTempSymbol("debug_loc", Offset));
Eric Christopher34a2c872013-11-15 01:43:19 +00001801 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001802 return VariableDie;
1803 }
1804
Eric Christophercead0332011-10-03 15:49:20 +00001805 // Check if variable is described by a DBG_VALUE instruction.
Eric Christopher34a2c872013-11-15 01:43:19 +00001806 if (const MachineInstr *DVInsn = DV.getMInsn()) {
David Blaikie0252265b2013-06-16 20:34:15 +00001807 assert(DVInsn->getNumOperands() == 3);
1808 if (DVInsn->getOperand(0).isReg()) {
1809 const MachineOperand RegOp = DVInsn->getOperand(0);
Adrian Prantl19942882013-07-09 21:44:06 +00001810 // If the second operand is an immediate, this is an indirect value.
Adrian Prantl418d1d12013-07-09 20:28:37 +00001811 if (DVInsn->getOperand(1).isImm()) {
Eric Christopherc2697f82013-10-19 01:04:47 +00001812 MachineLocation Location(RegOp.getReg(),
1813 DVInsn->getOperand(1).getImm());
Eric Christopher34a2c872013-11-15 01:43:19 +00001814 addVariableAddress(DV, VariableDie, Location);
David Blaikie0252265b2013-06-16 20:34:15 +00001815 } else if (RegOp.getReg())
Eric Christopher34a2c872013-11-15 01:43:19 +00001816 addVariableAddress(DV, VariableDie, MachineLocation(RegOp.getReg()));
David Blaikie0252265b2013-06-16 20:34:15 +00001817 } else if (DVInsn->getOperand(0).isImm())
Eric Christopher34a2c872013-11-15 01:43:19 +00001818 addConstantValue(VariableDie, DVInsn->getOperand(0), DV.getType());
David Blaikie0252265b2013-06-16 20:34:15 +00001819 else if (DVInsn->getOperand(0).isFPImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001820 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
David Blaikie0252265b2013-06-16 20:34:15 +00001821 else if (DVInsn->getOperand(0).isCImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001822 addConstantValue(VariableDie, DVInsn->getOperand(0).getCImm(),
Eric Christopher34a2c872013-11-15 01:43:19 +00001823 isUnsignedDIType(DD, DV.getType()));
Eric Christopher78fcf4902013-07-03 01:08:30 +00001824
Eric Christopher34a2c872013-11-15 01:43:19 +00001825 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001826 return VariableDie;
1827 } else {
1828 // .. else use frame index.
Eric Christopher34a2c872013-11-15 01:43:19 +00001829 int FI = DV.getFrameIndex();
Devang Patel3acc70e2011-08-15 22:04:40 +00001830 if (FI != ~0) {
1831 unsigned FrameReg = 0;
1832 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopherc2697f82013-10-19 01:04:47 +00001833 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
Devang Patel3acc70e2011-08-15 22:04:40 +00001834 MachineLocation Location(FrameReg, Offset);
Eric Christopher34a2c872013-11-15 01:43:19 +00001835 addVariableAddress(DV, VariableDie, Location);
Devang Patel3acc70e2011-08-15 22:04:40 +00001836 }
1837 }
1838
Eric Christopher34a2c872013-11-15 01:43:19 +00001839 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001840 return VariableDie;
1841}
1842
Manman Ren230ec862013-10-23 23:00:44 +00001843/// constructMemberDIE - Construct member DIE from DIDerivedType.
Eric Christophera5a79422013-12-09 23:32:48 +00001844void DwarfUnit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) {
Manman Renb987e512013-10-29 00:53:03 +00001845 DIE *MemberDie = createAndAddDIE(DT.getTag(), Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001846 StringRef Name = DT.getName();
1847 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001848 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001849
Manman Ren93b30902013-10-08 18:42:58 +00001850 addType(MemberDie, resolve(DT.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001851
1852 addSourceLine(MemberDie, DT);
1853
Eric Christopherc2697f82013-10-19 01:04:47 +00001854 if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) {
Devang Patel0e821f42011-04-12 23:21:44 +00001855
1856 // For C++, virtual base classes are not at fixed offset. Use following
1857 // expression to extract appropriate offset from vtable.
1858 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1859
Eric Christopher4a741042014-02-16 08:46:55 +00001860 DIELoc *VBaseLocationDie = new (DIEValueAllocator) DIELoc();
David Blaikief2443192013-10-21 17:28:37 +00001861 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1862 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1863 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1864 addUInt(VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1865 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1866 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1867 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
Devang Patel0e821f42011-04-12 23:21:44 +00001868
David Blaikief2443192013-10-21 17:28:37 +00001869 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
David Blaikie71d34a22013-11-01 00:25:45 +00001870 } else {
1871 uint64_t Size = DT.getSizeInBits();
1872 uint64_t FieldSize = getBaseTypeSize(DD, DT);
1873 uint64_t OffsetInBytes;
1874
1875 if (Size != FieldSize) {
1876 // Handle bitfield.
1877 addUInt(MemberDie, dwarf::DW_AT_byte_size, None,
1878 getBaseTypeSize(DD, DT) >> 3);
1879 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, DT.getSizeInBits());
1880
1881 uint64_t Offset = DT.getOffsetInBits();
1882 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1883 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1884 uint64_t FieldOffset = (HiMark - FieldSize);
1885 Offset -= FieldOffset;
1886
1887 // Maybe we need to work from the other end.
1888 if (Asm->getDataLayout().isLittleEndian())
1889 Offset = FieldSize - (Offset + Size);
1890 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
1891
Adrian Prantlc67655a2014-01-28 18:13:47 +00001892 // Here DW_AT_data_member_location points to the anonymous
David Blaikie71d34a22013-11-01 00:25:45 +00001893 // field that includes this bit field.
1894 OffsetInBytes = FieldOffset >> 3;
1895 } else
1896 // This is not a bitfield.
1897 OffsetInBytes = DT.getOffsetInBits() >> 3;
David Blaikie2ada1162014-01-03 00:48:38 +00001898
David Blaikie22b29a52014-01-03 01:30:05 +00001899 if (DD->getDwarfVersion() <= 2) {
Eric Christopher4a741042014-02-16 08:46:55 +00001900 DIELoc *MemLocationDie = new (DIEValueAllocator) DIELoc();
David Blaikie22b29a52014-01-03 01:30:05 +00001901 addUInt(MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1902 addUInt(MemLocationDie, dwarf::DW_FORM_udata, OffsetInBytes);
1903 addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie);
1904 } else
1905 addUInt(MemberDie, dwarf::DW_AT_data_member_location, None,
1906 OffsetInBytes);
David Blaikie71d34a22013-11-01 00:25:45 +00001907 }
Devang Patel0e821f42011-04-12 23:21:44 +00001908
1909 if (DT.isProtected())
Nick Lewyckycb918492011-12-13 05:09:11 +00001910 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001911 dwarf::DW_ACCESS_protected);
1912 else if (DT.isPrivate())
Nick Lewyckycb918492011-12-13 05:09:11 +00001913 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001914 dwarf::DW_ACCESS_private);
1915 // Otherwise C++ member and base classes are considered public.
Eric Christopher92331fd2012-11-21 00:34:38 +00001916 else
Nick Lewyckycb918492011-12-13 05:09:11 +00001917 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001918 dwarf::DW_ACCESS_public);
1919 if (DT.isVirtual())
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001920 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001921 dwarf::DW_VIRTUALITY_virtual);
Devang Patel514b4002011-04-16 00:11:51 +00001922
1923 // Objective-C properties.
Devang Patel44882172012-02-06 17:49:43 +00001924 if (MDNode *PNode = DT.getObjCProperty())
1925 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
Eric Christopher92331fd2012-11-21 00:34:38 +00001926 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
Devang Patel44882172012-02-06 17:49:43 +00001927 PropertyDie);
1928
David Blaikie37fefc32012-12-13 22:43:07 +00001929 if (DT.isArtificial())
1930 addFlag(MemberDie, dwarf::DW_AT_artificial);
Devang Patel0e821f42011-04-12 23:21:44 +00001931}
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001932
Manman Renc6b63922013-10-14 20:33:57 +00001933/// getOrCreateStaticMemberDIE - Create new DIE for C++ static member.
Eric Christophera5a79422013-12-09 23:32:48 +00001934DIE *DwarfUnit::getOrCreateStaticMemberDIE(DIDerivedType DT) {
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001935 if (!DT.Verify())
1936 return NULL;
1937
Manman Renc6b63922013-10-14 20:33:57 +00001938 // Construct the context before querying for the existence of the DIE in case
1939 // such construction creates the DIE.
1940 DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext()));
David Blaikiebd700e42013-11-14 21:24:34 +00001941 assert(dwarf::isType(ContextDIE->getTag()) &&
1942 "Static member should belong to a type.");
Manman Renc6b63922013-10-14 20:33:57 +00001943
1944 DIE *StaticMemberDIE = getDIE(DT);
1945 if (StaticMemberDIE)
1946 return StaticMemberDIE;
1947
Manman Renb987e512013-10-29 00:53:03 +00001948 StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT);
Manman Renc6b63922013-10-14 20:33:57 +00001949
Manman Ren93b30902013-10-08 18:42:58 +00001950 DIType Ty = resolve(DT.getTypeDerivedFrom());
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001951
1952 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1953 addType(StaticMemberDIE, Ty);
1954 addSourceLine(StaticMemberDIE, DT);
1955 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1956 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1957
1958 // FIXME: We could omit private if the parent is a class_type, and
1959 // public if the parent is something else.
1960 if (DT.isProtected())
1961 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1962 dwarf::DW_ACCESS_protected);
1963 else if (DT.isPrivate())
1964 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1965 dwarf::DW_ACCESS_private);
1966 else
1967 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1968 dwarf::DW_ACCESS_public);
1969
1970 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
Manman Renb3388602013-10-05 01:43:03 +00001971 addConstantValue(StaticMemberDIE, CI, isUnsignedDIType(DD, Ty));
David Blaikiea39a76e2013-01-20 01:18:01 +00001972 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
1973 addConstantFPValue(StaticMemberDIE, CFP);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001974
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001975 return StaticMemberDIE;
1976}
David Blaikie6b288cf2013-10-30 20:42:41 +00001977
Eric Christophera5a79422013-12-09 23:32:48 +00001978void DwarfUnit::emitHeader(const MCSection *ASection,
David Blaikie3332d4c2013-12-11 21:14:02 +00001979 const MCSymbol *ASectionSym) const {
David Blaikie6b288cf2013-10-30 20:42:41 +00001980 Asm->OutStreamer.AddComment("DWARF version number");
1981 Asm->EmitInt16(DD->getDwarfVersion());
1982 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
David Blaikie6896e192013-12-04 23:39:02 +00001983 // We share one abbreviations table across all units so it's always at the
1984 // start of the section. Use a relocatable offset where needed to ensure
1985 // linking doesn't invalidate that offset.
David Blaikie91db9ab2013-12-04 18:12:28 +00001986 Asm->EmitSectionOffset(ASectionSym, ASectionSym);
David Blaikie6b288cf2013-10-30 20:42:41 +00001987 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1988 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
1989}
David Blaikiebc563272013-12-13 21:33:40 +00001990
David Blaikie2494fdb2014-02-14 22:41:51 +00001991void DwarfCompileUnit::initStmtList(MCSymbol *DwarfLineSectionSym) {
1992 // Define start line table label for each Compile Unit.
1993 MCSymbol *LineTableStartSym =
1994 Asm->GetTempSymbol("line_table_start", getUniqueID());
1995 Asm->OutStreamer.getContext().setMCLineTableSymbol(LineTableStartSym,
1996 getUniqueID());
1997
1998 // Use a single line table if we are generating assembly.
1999 bool UseTheFirstCU =
2000 Asm->OutStreamer.hasRawTextSupport() || (getUniqueID() == 0);
2001
David Blaikie60e63862014-02-14 23:58:13 +00002002 stmtListIndex = UnitDie->getValues().size();
2003
David Blaikie2494fdb2014-02-14 22:41:51 +00002004 // DW_AT_stmt_list is a offset of line number information for this
2005 // compile unit in debug_line section. For split dwarf this is
2006 // left in the skeleton CU and so not included.
2007 // The line table entries are not always emitted in assembly, so it
2008 // is not okay to use line_table_start here.
2009 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
2010 addSectionLabel(UnitDie.get(), dwarf::DW_AT_stmt_list,
2011 UseTheFirstCU ? DwarfLineSectionSym : LineTableStartSym);
2012 else if (UseTheFirstCU)
2013 addSectionOffset(UnitDie.get(), dwarf::DW_AT_stmt_list, 0);
2014 else
2015 addSectionDelta(UnitDie.get(), dwarf::DW_AT_stmt_list, LineTableStartSym,
2016 DwarfLineSectionSym);
2017}
2018
David Blaikie60e63862014-02-14 23:58:13 +00002019void DwarfCompileUnit::applyStmtList(DIE &D) {
2020 D.addValue(dwarf::DW_AT_stmt_list,
2021 UnitDie->getAbbrev().getData()[stmtListIndex].getForm(),
2022 UnitDie->getValues()[stmtListIndex]);
2023}
2024
David Blaikiebc563272013-12-13 21:33:40 +00002025void DwarfTypeUnit::emitHeader(const MCSection *ASection,
2026 const MCSymbol *ASectionSym) const {
2027 DwarfUnit::emitHeader(ASection, ASectionSym);
2028 Asm->OutStreamer.AddComment("Type Signature");
2029 Asm->OutStreamer.EmitIntValue(TypeSignature, sizeof(TypeSignature));
2030 Asm->OutStreamer.AddComment("Type DIE Offset");
David Blaikie15ed5eb2014-01-10 01:38:41 +00002031 // In a skeleton type unit there is no type DIE so emit a zero offset.
2032 Asm->OutStreamer.EmitIntValue(Ty ? Ty->getOffset() : 0,
2033 sizeof(Ty->getOffset()));
David Blaikiebc563272013-12-13 21:33:40 +00002034}
2035
2036void DwarfTypeUnit::initSection(const MCSection *Section) {
2037 assert(!this->Section);
2038 this->Section = Section;
2039 // Since each type unit is contained in its own COMDAT section, the begin
2040 // label and the section label are the same. Using the begin label emission in
2041 // DwarfDebug to emit the section label as well is slightly subtle/sneaky, but
2042 // the only other alternative of lazily constructing start-of-section labels
2043 // and storing a mapping in DwarfDebug (or AsmPrinter).
2044 this->SectionSym = this->LabelBegin =
2045 Asm->GetTempSymbol(Section->getLabelBeginName(), getUniqueID());
2046 this->LabelEnd =
2047 Asm->GetTempSymbol(Section->getLabelEndName(), getUniqueID());
2048 this->LabelRange = Asm->GetTempSymbol("gnu_ranges", getUniqueID());
2049}